nymea / nymea-networkmanager

This daemon allows to set up the wireless network using a Bluetooth LE connection.
GNU General Public License v3.0
55 stars 16 forks source link

stopping nymea after network established and running secondary ble #43

Open uselessgames opened 3 years ago

uselessgames commented 3 years ago

Hello,

Thank you for the daemon, it is great!

I have a scenario where I need to run a secondary ble service after internet connectivity is established. ExecStartPre=/bin/sh -c 'until ping -c1 google.com; do sleep 5; done;' I have noticed that when my broadcast starts it still uses nymea's characteristics. I have to disconnect + refresh several times + connect in order to see mine.

So we tried the following to check + stop nymea before starting ours

ExecStartPre=/bin/sh -c "until ping -c1 google.com; do sleep 5; done;if  systemctl list-units -all | grep -Fq 'nymea-networkmanager.service'; then echo 'Stoping nymea before starting jawsaver'; sudo systemctl stop nymea-networkmanager.service; else echo 'nymea not found'; fi"

Which seems to properly stop and broadcast the correct ble characteristics. However it prematurely stops nymea during the wifi setup (whenever there is no wlan set up).

Any suggestions to properly stopping nymea after setup and starting a second ble service?

if it makes a difference we are using the installation as noted on berrylan repo.

t-mon commented 3 years ago

Hi @uselessgames

thanks for our feedback!

Depending on the mode you are running the nymea-networkmanager (default is offline mode) the server will be started if the system is offline. Once you connect to the bluetooth server, and the network connection has been established, the server will be shut down after the client application disconnects from the bluetooth server. That means: stop advertising. It is altough possible, that the services remain registered as peripheral. According to the docs from the Qt bluetooth library we are using, also the services will be removed from the peripheral instance in bluez.

You can try following:

  1. make sure you are using the offline mode in the nymea-networkmanager
  2. make sure, the bluetooth server is not starting again after you get online and disconnected from the bluetoothserver (you can check the logs with journalcrt -f -u nymea-networkmanager during the entire procedure
  3. Once you are sure the bluetooth server does not get started again, you could check if bluez still has a peripheral instance on dbus. If so, we might have found a bug.

We had the issue, that with bluetooth 4.0 you are not allowed to run a server and being a client (connect to an other bluetooth le device simultaneously), se we stop all connections while the bluetooth server is running (we provide a dbus interface for that) and re-enable all connections after the server stopped.

Having a second bluetooth server advertising right after the nymea-networkmanager one I have not tried yet. I will check here what happens on bluez with the advertising service data once the server stopped.

uselessgames commented 3 years ago

Hi @t-mon

  1. I can confirm we are using nymea-networkmanager in default mode offline
  2. We do check journalctl during the wlan setup process. For your reference enclosed is a copy of daemon.log of last test, as found /var/log
  3. At the time i was using nrf connect app to check ble service and could see nymea's under ours. being it was our advertise name but with nymea's characteristics. When I check journal for nymea it would say "post run service" and the journal for ours showed success advertise. I can test again this weekend and check dbus as you suggest.

We had the issue, that with bluetooth 4.0 you are not allowed to run a server and being a client (connect to an other bluetooth le device simultaneously), se we stop all connections while the bluetooth server is running (we provide a dbus interface for that) and re-enable all connections after the server stopped.

In your experience what time does nymea need to completely stop and re-enable after wifi is set up ? It may be possible we are starting our service too soon? if it matter we are using rpi4b (which is bluetooth 5, but we use ble)

Regards

t-mon commented 3 years ago

The server remains until the client application (connected to the bluetooth server) closes the connection, not when the system is online. The offline mode only makes sure the bluetooth server will not be started again if the system is online. The server shutdown can take a few seconds after the disconnect.

uselessgames commented 3 years ago

Okay, we will keep this in consideration for testing and revert with an update, thank you.

uselessgames commented 3 years ago

hi @t-mon

I have tested and confirm there is no bug.

The server remains until the client application (connected to the bluetooth server) closes the connection, not when the system is online. The offline mode only makes sure the bluetooth server will not be started again if the system is online. The server shutdown can take a few seconds after the disconnect.

to save from closing the client application and switch from one ble service to the second in a more seamless fashion within the same app, do you have suggestion for nymea server shutdown after a wifi is established? The few methods I have tried shuts the nymea server down prematurely when attempting to setup wifi.

t-mon commented 3 years ago

The bluetooth service is designed in a way to have all the time you want to establish a network connection, follow the connection state changes and never have a blind situation where you don't know what the device is doing or if the network connection failed. It was originally also designed to do other stuff too, switch the connection, or reconfigure something. Shutting down the connection right after you are online kills almost the entire usecase (like fetching the ip the device got, or doing further configuration) and many possibilities. One thing I could suggest is offering a local socket or something, giving you information if the bluetooth server is running or not, with that you would be able to start your bluetooth server right after the nymea networkmanager one has shut down. In your client application (communicating to both servers) you just need to disconnect from the nyma bt server, that shuts it down, you can start your bt server using the suggested mechanism, and connect to your bt server.

t-mon commented 3 years ago

FYI: I'm currently reworking the entire bluetooth server, introducing encryption and dynamic service registration. The current service for networkmanagment will remain for backwards compatibility, but new features will only implemented in the new service infrastructure and using encryption.

uselessgames commented 3 years ago

One thing I could suggest is offering a local socket or something, giving you information if the bluetooth server is running or not, with that you would be able to start your bluetooth server right after the nymea networkmanager one has shut down. In your client application (communicating to both servers) you just need to disconnect from the nyma bt server, that shuts it down, you can start your bt server using the suggested mechanism, and connect to your bt server.

This might work better, at the moment we use ping to determine if there is connectivity and start when there is, but as mentioned it still uses nymeas endpoints when it starts and continues until nymea properly shutsdown ( rpi or app is rebooted).