0

BLE _bleio.py unpack_from requires buffer of at least 1 byte
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BLE _bleio.py unpack_from requires buffer of at least 1 byte

by tlpada on Tue Apr 07, 2020 11:18 pm

Hi, when running "ble_broadcastnet_blinka_bridge.py" on Raspberry Pi, it scans for about 30 seconds, and then crashes with the exception:

Code: Select all | TOGGLE FULL SIZE
"struct.error: unpack_from requires a buffer of at least 1 bytes"
The error seems to come from this line in _bleio.py:

Code: Select all | TOGGLE FULL SIZE
key = struct.unpack_from(key_encoding, data, i)[0]
in the method _decode_data:

Code: Select all | TOGGLE FULL SIZE
@staticmethod
def _decode_data(data, *, key_encoding="B"):
    """Helper which decodes length encoded structures into a dictionary with the given key
       encoding."""
    i = 0
    data_dict = {}
    key_size = struct.calcsize(key_encoding)
    while i < len(data):
        item_length = data[i]
        i += 1
        if item_length == 0:
            break
        key = struct.unpack_from(key_encoding, data, i)[0]
        value = data[i + key_size : i + item_length]
        if key in data_dict:
            if not isinstance(data_dict[key], list):
                data_dict[key] = [data_dict[key]]
            data_dict[key].append(value)
        else:
            data_dict[key] = value
        i += item_length
    return data_dict
This is how the output from running ble_broadcastnet_blinka_bridge.py looks:

Code: Select all | TOGGLE FULL SIZE
This is BroadcastNet bridge: b827ebbf4b17

scanning

Traceback (most recent call last):
  File "./ble_broadcastnet_blinka_bridge.py", line 119, in <module>
    adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5
  File "/usr/local/lib/python3.5/dist-packages/adafruit_ble/__init__.py", line 243, in start_scan
    active=active,
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 266, in start_scan
    buffered, prefixes, minimum_rssi, active
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 197, in _parse_buffered
    scan_entry = ScanEntry(event_type, address, rssi, data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 78, in __init__
    self._data_dict = self._decode_data(data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 98, in _decode_data
    key = struct.unpack_from(key_encoding, data, i)[0]
struct.error: unpack_from requires a buffer of at least 1 bytes
Any ideas why this is choking here while scanning BLE advertisements?

Thanks.

tlpada
 
Posts: 76
Joined: Thu Feb 27, 2020 2:38 pm

Re: BLE _bleio.py unpack_from requires buffer of at least 1

by tlpada on Wed Apr 08, 2020 1:10 am

PS I added a line to show what 'data' is:

Code: Select all | TOGGLE FULL SIZE
print("Inside _decode_data, data is:",data)
in the first line of _decode_data():

Code: Select all | TOGGLE FULL SIZE
@staticmethod
def _decode_data(data, *, key_encoding="B"):
    """Helper which decodes length encoded structures into a dictionary with the given key
       encoding."""
    print("Inside _decode_data, data is:",data)
    i = 0
    data_dict = {}
    key_size = struct.calcsize(key_encoding)
The last lines before crashing are:

Code: Select all | TOGGLE FULL SIZE
Inside _decode_data, data is: b''
Inside _decode_data, data is: b'yw'
Inside _decode_data, data is: b'\x02\x01\x00\x00'
Inside _decode_data, data is: b'\x00\x0c\x0e\x00\xdcxf\x13\x9b\x06F\xf9Rv\xc2M\xf1'
Inside _decode_data, data is: b'\x00\x0c\x0e\x00\xdcxf\x13\x9b\x06F\xf9Rv\xc2M\xf1'
Inside _decode_data, data is: b'yw'
Inside _decode_data, data is: b'\x02\x01\x00\x01'
Traceback (most recent call last):
  File "./tlp-test-ble_broadcastnet_blinka_bridge.py", line 119, in <module>
    adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5
  File "/usr/local/lib/python3.5/dist-packages/adafruit_ble/__init__.py", line 243, in start_scan
    active=active,
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 267, in start_scan
    buffered, prefixes, minimum_rssi, active
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 198, in _parse_buffered
    scan_entry = ScanEntry(event_type, address, rssi, data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 78, in __init__
    self._data_dict = self._decode_data(data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 99, in _decode_data
    key = struct.unpack_from(key_encoding, data, i)[0]
struct.error: unpack_from requires a buffer of at least 1 bytes
and:

Code: Select all | TOGGLE FULL SIZE
Inside _decode_data, data is: b''
Inside _decode_data, data is: b'\xd0\x15l\xd4\xbc'
Inside _decode_data, data is: b'\xc8\xf7'
Inside _decode_data, data is: b'\xca\x92k\x8b'
Inside _decode_data, data is: b'\xc8\xf7'
Inside _decode_data, data is: b'\xca\x92k\x8b'
Inside _decode_data, data is: b'\xd0\x15l\xd4\xbc'
Inside _decode_data, data is: b'\x01\x02\x01\xa2\x1f'
Traceback (most recent call last):
  File "./tlp-test-ble_broadcastnet_blinka_bridge.py", line 119, in <module>
    adafruit_ble_broadcastnet.AdafruitSensorMeasurement, interval=0.5
  File "/usr/local/lib/python3.5/dist-packages/adafruit_ble/__init__.py", line 243, in start_scan
    active=active,
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 267, in start_scan
    buffered, prefixes, minimum_rssi, active
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 198, in _parse_buffered
    scan_entry = ScanEntry(event_type, address, rssi, data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 78, in __init__
    self._data_dict = self._decode_data(data)
  File "/usr/local/lib/python3.5/dist-packages/_bleio.py", line 99, in _decode_data
    key = struct.unpack_from(key_encoding, data, i)[0]
struct.error: unpack_from requires a buffer of at least 1 bytes
Some example output files are attached:
Attachments
OUTPUT-decode-2.txt
(53.75 KiB) Not downloaded yet
OUTPUT-decode-1.txt
(65.86 KiB) Downloaded 2 times

tlpada
 
Posts: 76
Joined: Thu Feb 27, 2020 2:38 pm

Please be positive and constructive with your questions and comments.