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

Add prefer_notify option to gatt_client subscribe() #71

Merged
merged 1 commit into from
Nov 14, 2022

Conversation

mogenson
Copy link
Collaborator

If characteristic supports Notify and Indicate, the prefer_notify option will subscribe with Notify if True or Indicate if False.

If characteristic only supports one property, Notify or Indicate, that mode will be selected, regardless of the prefer_notify setting.

Tested with a characteristic that supports both Notify and Indicate and verified that prefer_notify sets the desired mode.

@mogenson
Copy link
Collaborator Author

Fix for #67

bits |= ClientCharacteristicConfigurationBits.INDICATION
subscriber_sets.append(self.indication_subscribers.setdefault(characteristic.handle, set()))
def characteristic_is_notify(characteristic, prefer_notify):
if (
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggest: more readable as: if characteristic.properties & Characteristic.NOTIFY and characteristic.properties & Characteristic.INDICATE

Copy link
Collaborator

Choose a reason for hiding this comment

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

parens aren't needed

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the formatting that black chose, so I'll stick with it.

if (
characteristic.properties & (Characteristic.NOTIFY | Characteristic.INDICATE)
) == (Characteristic.NOTIFY | Characteristic.INDICATE):
return True if prefer_notify else False
Copy link
Collaborator

Choose a reason for hiding this comment

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

return prefer_notify would be sufficient

Copy link
Collaborator

@AlanRosenthal AlanRosenthal left a comment

Choose a reason for hiding this comment

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

lgtm

# emitting an 'update' event when a notification or indication is received
subscriber_set.add(characteristic)
subscriber_set = subscribers.setdefault(characteristic.handle, set())
if subscriber is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

if subscriber: is sufficient

@@ -558,23 +555,32 @@ async def subscribe(self, characteristic, subscriber=None):
logger.warning('subscribing to characteristic with no CCCD descriptor')
Copy link
Collaborator

Choose a reason for hiding this comment

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

OOS, but this warning is wrong. we don't know if theres no CCCD as we just discovered descriptors and haven't checked if CCCD exists

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The line above gets the CCCD only if it exists:
cccd = characteristic.get_descriptor(GATT_CLIENT_CHARACTERISTIC_CONFIGURATION_DESCRIPTOR)


bits, subscribers = (
(ClientCharacteristicConfigurationBits.NOTIFICATION, self.notification_subscribers)
if characteristic_is_notify(characteristic, prefer_notify)
Copy link
Collaborator

Choose a reason for hiding this comment

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

i found this block (571-575) a bit hard to read. it's a tuple + if expression.
consider converting it to a if statement

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This may be a matter of opinion. I prefer the more succinct if-expression to the following if-statement.

bits = None
subscribers = None
if characteristic_is_notify(characteristic, prefer_notify):
    bits = ClientCharacteristicConfigurationBits.Notification
    subscribers = self.notification_subscribers
else:
    bits = ClientCharacteristicConfigurationBits.INDICATION
    subscribers = self.indication_subscribers

Since they both do exactly the same thing, I'll leave it as-is.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could also put the bits and subscribers assignments in the if/else statement used for characteristic_is_notify (and thus get rid of the local function).
Also, since there's always at least one branch taken, you don't need to initialize bits and subscribers to None, they'll always be set in one of the branches.

@mogenson mogenson force-pushed the prefer-notify branch 2 times, most recently from 5e718e3 to accb03f Compare November 13, 2022 22:51
# emitting an 'update' event when a notification or indication is received
subscriber_set.add(characteristic)
subscriber_set = subscribers.setdefault(characteristic.handle, set())
if subscriber:
Copy link
Collaborator

Choose a reason for hiding this comment

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

comparing to None should be explicit here (if subscriber is not None:). Even though unlikely, a not-None argument could still be "falsy", and thus would be ignored here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ok, I'll change it back.

If characteristic supports Notify and Indicate, the prefer_notify option
will subscribe with Notify if True or Indicate if False.

If characteristic only supports one property, Notify or Indicate, that
mode will be selected, regardless of the prefer_notify setting.

Tested with a characteristic that supports both Notify and Indicate and
verified that prefer_notify sets the desired mode.
@mogenson mogenson merged commit 27c46ee into google:main Nov 14, 2022
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