Skip to content

ManufacturerData tweak to offer control over ordering of its data fields #97

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 2 commits into from
Aug 17, 2020

Conversation

kevinjwalters
Copy link

This changes self.data from an {} to an OrderedDict() to offer more control over the data field ordering for the benefit of the prefix matching.

On a side note, my understanding is that big python (CPython) has used an insertion ordered implementation for normal dict since 3.6 and this has become part of the language spec now in 3.7

…control over order to increase the utility of prefix matching.
Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

This is a good idea, but could you add a comment to ManufacturerData describing how to take advantage of the ordering?

@kevinjwalters
Copy link
Author

I added a single but long line to the inline ManufacturerData docs, let me know if that needs further polishing.

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Can you please unify the setting of MICROPY_PY_COLLECTIONS_ORDEREDDICT into the py/circuitpy_* files so it's on by default too? Looks sporadic now.

@kevinjwalters
Copy link
Author

@tannewt Is that comment for me or @dhalbert ? I'm not comfortable with making that particular global change due to lack of experience with the internals and limited testing resources.

@tannewt
Copy link
Member

tannewt commented Aug 14, 2020

@kevinjwalters It was for you but it's fine if we do it later. I've opened adafruit/circuitpython#3278 to track it.

What prefix matching are you doing where this is important? Ordering of entries in the advertisement seems outside of the BLE spec.

@kevinjwalters
Copy link
Author

kevinjwalters commented Aug 15, 2020

It's not the ordering of entries in an advertising packet, it's the ordering of entries within manufacturer data field that interests me and that is what this PR affects.

In an application I am using the same approach as AdafruitColor where the prefix looks into the first part of the manufacturer data to spot an ID which identifies the type and therefore class for the data.

# This single prefix matches all color advertisements.
match_prefixes = (
struct.pack(
"<BHBH",
_MANUFACTURING_DATA_ADT,
_ADAFRUIT_COMPANY_ID,
struct.calcsize("<HI"),
_COLOR_DATA_ID,
),
)

I have multiple manufacturer fields so for prefix matching to work for an id number I need to ensure that one appears first. Multiple fields are attractive as I have an optional field and those are supported well by ManufacturerDataField.

https://github.com/adafruit/Adafruit_Learning_System_Guides/pull/1185/files#diff-9cfa0c5482e43d21a6ed0e9c4ba6e599R71-R79

@kevinjwalters
Copy link
Author

Is the adafruit_ble library used by any SAMD21 (M0) boards or are parts of it useful or usable for those? I've used the https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI library before on a Feather M0 Bluefruit.

@dhalbert
Copy link
Collaborator

Is the adafruit_ble library used by any SAMD21 (M0) boards or are parts of it useful or usable for those? I've used the https://github.com/adafruit/Adafruit_CircuitPython_BluefruitSPI library before on a Feather M0 Bluefruit.

Are you thinking about whether OrderedCollection might not be present?

Right now the library is only usable on nRF boards and via Blinka. However, I'm finishing up an implementation of _bleio for the ESP32 co-processor, used on Metro M4 Airlift, PyPortal, etc., and it's also available as a breakout.

I'm not sure there's enough RAM on SAMD21 boards. You could try importing some of adafruit_ble on a SAMD21 and see whether it fits at all.

@kevinjwalters
Copy link
Author

Yes, the other ticket shows it AWOL on some SAMD board and inevitably it is the SAMD21 but I wasn't sure what the plan was for adafruit_ble. Gemma M0 example below:

Adafruit CircuitPython 5.3.0 on 2020-04-29; Adafruit Gemma M0 with samd21e18
>>> from collections import OrderedDict
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name OrderedDict
>>> import collections
>>> dir(collections)
['__name__', 'namedtuple']

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

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

Ah! Right. I'm ok adding more semantics to ManufacturerData since it's controlled by us.

I'm also ok using OrderedDict. We should have it available on non-M0 builds going forwards. Just need to centralize it's definition to ensure that. :-)

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

Thanks for the doc additions. This looks good for the boards we need to support now. collections.OrderedDict is available on SAMD51.

@dhalbert dhalbert merged commit aee83fb into adafruit:master Aug 17, 2020
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Aug 18, 2020
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.

3 participants