Skip to content

Commit

Permalink
Transport: expose all mqtt standard parameters (#28)
Browse files Browse the repository at this point in the history
- Allign them with backend-client
- Keep old ones with deprecated mention
- Directly send global settings in different classes to avoid specifying
  all the different parameters everywhere
  • Loading branch information
GwendalRaoul committed Jul 8, 2019
1 parent 4255537 commit 21ae4e3
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 151 deletions.
60 changes: 29 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ be built at installation time.
pip3 install wirepas_gateway-*.tar.gz
```

#### Configuration and starting services
## Configuration and starting services

- A sink service must be started for each connected sink on Gateway:
### Sink service configuration

A sink service must be started for each connected sink on Gateway:

sink_service/build/sinkService -p <uart_port> -b <bitrate> -i <sink_id>

Expand All @@ -103,17 +105,26 @@ Parameters are:
- **bitrate:** bitrate of sink uart (default 125000)
- **sink_id:** value between 0 and 9 (default 0).

If multiple sinks are present, they must have a different sink_id and a
transport service must be launched.
If multiple sinks are present, they must have a different sink_id

### Transport service configuration

Parameters can be set from cmd line or from a setting file in YAML format.
To get an exhausted list of parameters, please run:

```shell
wm-gw --help
```

Parameters can be set from cmd line of from a setting file in YAML format:

#### From cmd line

Here is an example to start the transport module from cmd line:

```shell
wm-gw -s "<server>" -p <port> -u <user> -pw <password> \
-i <gwid> [-t <tls_cert_file>][-fp] [-ua][-iepf <endpoints list>] \
[-wepf <endpoints list>]
wm-gw --mqtt_hostname "<server>" --mqtt_port <port> --mqtt_username <user> --mqtt_password <password> \
--gateway_id <gwid> [--ignored_endpoints_filter <ignored endpoints list>] \
[--whitened_endpoints_filter <whitened endpoints list>]
```

where:
Expand All @@ -130,25 +141,18 @@ where:

> It must be unique for each gateway reporting to same broker.
- **tls_cert_file:** filepath to the root certificate to override
system one (**Cannot be used with -ua**)

- **ua:** Disable TLS secure authentication

- **fp:** Do not use the C extension (full python version)

- **iepf:** Destination endpoints list to ignore (not published)
- **ignored endpoints list:** Destination endpoints list to ignore (not published)

*Example:*

> -iepf "\[1,2, 10-12\]" to filter out destination ep 1, 2, 10, 11, 12
> --ignored_endpoints_filter "\[1,2, 10-12\]" to filter out destination ep 1, 2, 10, 11, 12
- **wepf:** Destination endpoints list to whiten
- **whitened endpoints list:** Destination endpoints list to whiten
(no payload content, only size)

*Example:*

> -wepf "\[1,2, 10-12\]" to whiten destination ep 1, 2, 10, 11, 12
> --whitened_endpoints_filter "\[1,2, 10-12\]" to whiten destination ep 1, 2, 10, 11, 12
#### From configuration file

Expand All @@ -164,32 +168,26 @@ file is given below:
#
# MQTT brocker Settings
#
host: <IP or hostname where the MQTT broker is located>
port: <MQTT port (default: 8883 (secure) or 1883 (local))>
username: <MQTT user>
password: <MQTT password>
unsecure_authentication: <True to disable TLS secure authentication>
mqtt_hostname: <IP or hostname where the MQTT broker is located>
mqtt_port: <MQTT port (default: 8883 (secure) or 1883 (local))>
mqtt_username: <MQTT user>
mqtt_password: <MQTT password>

#
# Gateway settings
#
gwid: <the desired gateway id, must be unique for each gateway>
gateway_id: <the desired gateway id, must be unique for each gateway>
gateway_model: <Custom gateway model, can be omitted>
gateway_version: <Custom gateway version, can be omitted>

#
# Implementation options
#
full_python: <Set to true to not use the C extension>

#
# Filtering Destination Endpoints
#
ignored_endpoints_filter: <Endpoints to filter out. Ex: [1, 2, 10-12]>
whitened_endpoints_filter: <Endpoints to whiten. Ex: [1, 2, 10-12]>
```
#### Optional
### Optional
Launch local gateway process to see messages received from sinks at Dbus level
It can be launched from command line:
Expand Down
3 changes: 2 additions & 1 deletion python_transport/wirepas_gateway/dbus/dbus_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ def __init__(self, logger=None, c_extension=True, ignored_ep_filter=None, **kwar

# Register for packet on Dbus
if c_extension:
self.logger.info("Starting c extension")
self.logger.info("Starting dbus client with c extension")
self.c_extension_thread = DbusEventHandler(
self._on_data_received_c, self.logger
)
else:
self.logger.info("Starting dbus client without c extension")
# Subscribe to all massages received from any sink (no need for
# connected sink for that)
self.bus.subscribe(
Expand Down
34 changes: 20 additions & 14 deletions python_transport/wirepas_gateway/protocol/mqtt_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,42 @@ class MQTTWrapper(Thread):
# Reconnect timeout in Seconds
TIMEOUT_RECONNECT_S = 120

def __init__(self, logger, username, password, host, port, secure_auth=True, ca_certs=None,
on_termination_cb=None, on_connect_cb=None):
def __init__(self,
settings,
logger,
on_termination_cb=None,
on_connect_cb=None):
Thread.__init__(self)
self.daemon = True
self.running = False
self.logger = logger
self.on_termination_cb = on_termination_cb
self.on_connect_cb = on_connect_cb

self._client = mqtt.Client()
if secure_auth:
self._client = mqtt.Client(client_id=settings.gateway_id)
if not settings.mqtt_force_unsecure:
try:
self._client.tls_set(
ca_certs=ca_certs,
certfile=None,
keyfile=None,
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None,
ca_certs=settings.mqtt_ca_certs,
certfile=settings.mqtt_certfile,
keyfile=settings.mqtt_keyfile,
cert_reqs=settings.mqtt_cert_reqs,
tls_version=settings.mqtt_tls_version,
ciphers=settings.mqtt_ciphers
)
except:
except Exception as e:
self.logger.error(
"Cannot use secure authentication. attempting unsecure connection"
"Cannot use secure authentication {}"
.format(e)
)
exit(-1)

self._client.username_pw_set(username, password)
self._client.username_pw_set(settings.mqtt_username,
settings.mqtt_password)
self._client.on_connect = self._on_connect

try:
self._client.connect(host, port, keepalive=MQTTWrapper.KEEP_ALIVE_S)
self._client.connect(settings.mqtt_hostname, settings.mqtt_port, keepalive=MQTTWrapper.KEEP_ALIVE_S)
except (socket.gaierror, ValueError) as e:
self.logger.error("Cannot connect to mqtt {}".format(e))
exit(-1)
Expand Down
Loading

0 comments on commit 21ae4e3

Please sign in to comment.