Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better detect BT devices and LTE devices #242

Merged
merged 12 commits into from
Nov 26, 2021
Merged

Conversation

posterzh
Copy link
Contributor

Why
Detect the BLE and LET devices in better way.

How
Get the BLE devices and LTE devices programatically using dbus.

References
#96
#88

@posterzh posterzh requested review from shawaj, marvinmarnold and vpetersson and removed request for marvinmarnold November 22, 2021 13:03
Dockerfile Outdated
pkg-config=0.29-6 \
libdbus-1-3 && \
# Because the PATH is already updated above, this command creates a new venv AND activates it
python3 -m venv /opt/venv && \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC @marvinmarnold and @vpetersson mentioned previously that there was little point in using venv in containerised setup.

Think was in relation to hm-config but guess it applies here too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have a soft spot for venv since it makes python portable. But yea, we did agree to stop using it.

@posterzh hm-pktfwd is an example of the new pattern: https://github.com/NebraLtd/hm-pktfwd/blob/e067bef1be17f9d3c1036cbe1100b317d5cc5f57/Dockerfile#L65

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marvinmarnold I am not fussed either way! Whichever you think is best TBH

Dockerfile Outdated
RUN \
install_packages \
i2c-tools \
bluez \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need bluez and network manager in the container if we are communicating with them over dbus?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shawaj Without the dependencies of bluez, network-manager, modem-manager, detecting BT, WIFI, LTE devices works well with only dbus dependency. I removed unnecessary dependencies in the Dockerfile.

Dockerfile Outdated
# Finally, use pip to install dependencies.
RUN \
install_packages \
python3-dev=3.7.3-1 \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC @vpetersson thought it best to unpin packages previously

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense

@posterzh posterzh force-pushed the posterzh/better-detect-bt branch 4 times, most recently from aa12bc8 to 23de222 Compare November 23, 2021 08:27
@vpetersson
Copy link
Contributor

Nice improvement, @posterzh!

@posterzh
Copy link
Contributor Author

Detecting BT and LTE devices using dbus:

image

Diagnostic Web page:

image

Diagnostic container from the multistage docker build:

image

@posterzh posterzh marked this pull request as ready for review November 24, 2021 03:46
@posterzh posterzh requested a review from a team as a code owner November 24, 2021 03:46
@posterzh posterzh requested a review from shawaj November 24, 2021 03:47
@shawaj
Copy link
Member

shawaj commented Nov 24, 2021

Looks good to me, but probably worth @vpetersson or @marvinmarnold or someone else from @NebraLtd/developers taking a deeper look.

@vpetersson
Copy link
Contributor

Good job @posterzh. This is a big improvement. I wonder however if we should move the detection logic to hm-pyhelper` instead as we should probably re-use this code in hm-diag too. Wdyt @marvinmarnold?

@posterzh
Copy link
Contributor Author

posterzh commented Nov 24, 2021

Good job @posterzh. This is a big improvement. I wonder however if we should move the detection logic to hm-pyhelper` instead as we should probably re-use this code in hm-diag too. Wdyt @marvinmarnold?

FYI, in the hm-diag, the only feature used is to check the presence of BT/LTE devices. Furthermore, I added the utility methods to get the list of BT, WiFi, LTE devices with details(get_bt_devices(), get_wifi_devices(), get_lte_devices()). These details of devices are used in checking the presence for validating their state. Meanwhile, almost the same code can be used for managing and connecting WiFi devices using dbus feature.

Copy link
Contributor

@marvinmarnold marvinmarnold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This work looks really good. The code is very readable and clear.

This logic also needs to be added to https://github.com/NebraLtd/hm-diag/blob/00c716dd4980e35ac7436d9f59aebb17bec07535/hw_diag/diagnostics/bt_lte_diagnostic.py Until we have completed https://github.com/NebraLtd/Hotspot-Production-Tool/issues/28 we will continue to have parallel logic for /json and /initFile.txt.

Lastly, my typical comment. Could you please add some basic unit tests?

from hm_pyhelper.miner_param import get_public_keys_rust
from hm_pyhelper.hardware_definitions import variant_definitions, is_rockpi
from hw_diag.utilities.shell import config_search_param

log = logging.getLogger()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use get_logger from hm_pyhelper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, make sense.



if __name__ == '__main__':
print('set_diagnostics_bt_lte: %s' % set_diagnostics_bt_lte({}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be log instead of print.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

Dockerfile Outdated
pip3 install --no-cache-dir -r /tmp/build/requirements.txt && \
python3 setup.py install && \
rm -rf /tmp/build
# Cleanup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this entirely. That's the whole purpose of using install_packages from Balena as it takes care of the cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the size of the docker image is same after removing the clean up.

from hm_pyhelper.miner_param import get_public_keys_rust
from hm_pyhelper.hardware_definitions import variant_definitions, is_rockpi
from hw_diag.utilities.shell import config_search_param

log = logging.getLogger()
log.setLevel(logging.DEBUG)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This log level needs to be controlled centrally. IIRC we already have a framework for this. We don't want debug logging in production unless we're troubleshooting something.

@@ -15,25 +74,112 @@ def should_display_lte(diagnostics):
return variant_data.get('CELLULAR')


def get_ble_devices():
log.info("Get BLE devices.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Retrieving list of BLE device(s)"

"Discovering": str(adapter.get("Discovering")),
})

log.info('BLE Devices: %s' % ble_devices)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Found the following BLE devices:"

log.info('BLE Devices: %s' % ble_devices)
except dbus.exceptions.DBusException as ex:
log.error(ex.get_dbus_message())
except Exception as ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the standardized way is to do Exception as e. Not a big deal, just to keep things consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I will keep in mind it.

log.info('WiFi Devices: %s' % wifi_devices)
except dbus.exceptions.DBusException as ex:
log.error(ex.get_dbus_message())
except Exception as ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as e (same as above).



def get_lte_devices():
log.info("Get LTE devices.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Retrieving list of LTE device(s)."

})

log.info('LTE Devices: %s' % lte_devices)
except dbus.exceptions.DBusException as ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as e (same as above).

log.info('LTE Devices: %s' % lte_devices)
except dbus.exceptions.DBusException as ex:
log.error(ex.get_dbus_message())
except Exception as ex:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as e (same as above).



if __name__ == '__main__':
print('set_diagnostics_bt_lte: %s' % set_diagnostics_bt_lte({}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid using print() in favor of logging.

@vpetersson
Copy link
Contributor

@posterzh Please fix the linting errors.

Copy link
Contributor

@vpetersson vpetersson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by me, but I would ideally like to see some unit tests here. Don't merge this before @marvinmarnold approves it.

Dockerfile Outdated
install_packages \
build-essential \
libdbus-glib-1-dev && \
pip3 install --no-cache-dir --target="$PYTHON_DEPENDENCIES_DIR" .
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, but I'd probably run this on a separate RUN line just to help with debugging if for whatever reason it would fail building.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would help debugging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by me, but I would ideally like to see some unit tests here. Don't merge this before @marvinmarnold approves it.

Yeah, I am writing the unit test and a parallel logic in DiagnosticsReport. These pushes are just for In-progress.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Please update the PR to 'Draft' mode if that is the case.

@posterzh posterzh marked this pull request as draft November 25, 2021 15:28
@shawaj
Copy link
Member

shawaj commented Nov 25, 2021

Good job @posterzh. This is a big improvement. I wonder however if we should move the detection logic to hm-pyhelper` instead as we should probably re-use this code in hm-diag too. Wdyt @marvinmarnold?

@vpetersson do you think we would need this in hm-config? We seem to be using some custom nmcli in there so not sure it'll be compatible https://github.com/NebraLtd/hm-config/blob/3b2e0fe138ab04533ffa44afa3d0a60d7972430e/gatewayconfig/processors/wifi_processor.py#L20

@posterzh
Copy link
Contributor Author

This work looks really good. The code is very readable and clear.

This logic also needs to be added to https://github.com/NebraLtd/hm-diag/blob/00c716dd4980e35ac7436d9f59aebb17bec07535/hw_diag/diagnostics/bt_lte_diagnostic.py Until we have completed NebraLtd/Hotspot-Production-Tool#28 we will continue to have parallel logic for /json and /initFile.txt.

Lastly, my typical comment. Could you please add some basic unit tests?

@marvinmarnold
Unit tests are added.
What about using a separate BtDiagnostics and LteDiagnostics rather than BtLteDiagnostics?

class BtLteDiagnostics():

Copy link
Contributor

@marvinmarnold marvinmarnold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a separate BtDiagnostics and LteDiagnostics rather than BtLteDiagnostics?

Good idea but lets not let that hold things up. Merge this and create a follow up ticket to do in a future sprint.

@posterzh posterzh marked this pull request as ready for review November 26, 2021 18:01
@posterzh posterzh merged commit 625ea72 into master Nov 26, 2021
@posterzh posterzh deleted the posterzh/better-detect-bt branch November 26, 2021 18:01
@posterzh
Copy link
Contributor Author

@marvinmarnold I created a follow up ticket as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants