Skip to content

Fix broken class matching on receive #16

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

Merged
merged 5 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions adafruit_ble_broadcastnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

def broadcast(measurement, *, broadcast_time=0.1, extended=False):
"""Broadcasts the given measurement for the given broadcast time. If extended is False and the
measurement would be too long, it will be split into multiple measurements for transmission.
"""
measurement would be too long, it will be split into multiple measurements for transmission.
"""
global _sequence_number # pylint: disable=global-statement,invalid-name
for submeasurement in measurement.split(252 if extended else 31):
submeasurement.sequence_number = _sequence_number
Expand All @@ -61,14 +61,17 @@ def broadcast(measurement, *, broadcast_time=0.1, extended=False):
if not hasattr(os, "environ") or (
"GITHUB_ACTION" not in os.environ and "READTHEDOCS" not in os.environ
):
device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name
*reversed(
list(
_ble._adapter.address.address_bytes # pylint: disable=protected-access
if _ble._adapter.address: # pylint: disable=protected-access
device_address = "{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}".format( # pylint: disable=invalid-name
*reversed(
list(
_ble._adapter.address.address_bytes # pylint: disable=protected-access
)
)
)
)
"""Device address as a string."""
else:
device_address = "000000000000" # pylint: disable=invalid-name
"""Device address as a string."""

_MANUFACTURING_DATA_ADT = const(0xFF)
_ADAFRUIT_COMPANY_ID = const(0x0822)
Expand All @@ -79,7 +82,10 @@ class AdafruitSensorMeasurement(Advertisement):

# This prefix matches all
match_prefixes = (
struct.pack("<BBH", 3, _MANUFACTURING_DATA_ADT, _ADAFRUIT_COMPANY_ID),
# Matches the sequence number field header (length+ID)
struct.pack(
"<BHBH", _MANUFACTURING_DATA_ADT, _ADAFRUIT_COMPANY_ID, 0x03, 0x0003
),
)

manufacturer_data = LazyObjectField(
Expand Down Expand Up @@ -175,9 +181,16 @@ def __str__(self):
parts.append("{}={}".format(attr, str(value)))
return "<{} {} >".format(self.__class__.__name__, " ".join(parts))

def __bytes__(self):
"""The raw packet bytes."""
# Must reorder the ManufacturerData contents so the sequence number field is always first.
# Necessary to ensure that match_prefixes works right to reconstruct on the receiver.
self.data_dict[255].data.move_to_end(3, last=False)
return super().__bytes__()

def split(self, max_packet_size=31):
"""Split the measurement into multiple measurements with the given max_packet_size. Yields
each submeasurement."""
each submeasurement."""
current_size = 8 # baseline for mfg data and sequence number
if current_size + len(self.manufacturer_data) < max_packet_size:
yield self
Expand Down
13 changes: 13 additions & 0 deletions examples/ble_broadcastnet_scan_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""This example merely scans for broadcastnet packets to check that something is sending them."""

import adafruit_ble
import adafruit_ble_broadcastnet

ble = adafruit_ble.BLERadio()

print("scanning")
# By providing Advertisement as well we include everything, not just specific advertisements.
for advert in ble.start_scan(
adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5
):
print(advert)