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

added HEX string type and option to disable auto-subscribe #134

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ configuration:
http://www.bluetooth.org. Each characteristic can include a `name` field which
will be used in the MQTT topic instead of its UUID and a `types` array
defining how to parse the byte array reflecting the characteristic's value.
The `subscribe` attribute can be set to "false" if you don't want to automatically
subscribe for Notifications and Indications on a Characteristic.
Copy link
Owner

Choose a reason for hiding this comment

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

No need to capitalize Notifications, Indications nor Characteristic.

In addition, it's possible to define a white/black list for discovered
characteristics. The white/black list UUIDs may contain the wildcard character
`?` to denote any value for a nibble. For example:
Expand All @@ -265,6 +267,7 @@ configuration:
"definitions": {
"00002f01-0000-1000-8000-00805f9b34fb": {
"name": "Relay State",
"subscribe": true,
"types": [
"boolean"
]
Expand Down
9 changes: 7 additions & 2 deletions main/ble2mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,13 @@ static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid,
/* Characteristic can notify / indicate on changes */
if (properties & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE))
{
Copy link
Owner

Choose a reason for hiding this comment

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

Please just change this condition to:

    if (properties & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE) &&
        config_ble_characteristic_should_subscribe(uuidtoa(characteristic_uuid)))
    {

And leave the below as it was.

ble_characteristic_notify_register(mac, service_uuid,
characteristic_uuid);
if (config_ble_characteristic_should_subscribe(uuidtoa(characteristic_uuid)))
{
ESP_LOGI(TAG, "Register Notify characteristic: " UUID_FMT ".",
UUID_PARAM(characteristic_uuid));
ble_characteristic_notify_register(mac, service_uuid,
characteristic_uuid);
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions main/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ uint8_t config_ble_characteristic_should_include(const char *uuid)
return json_is_in_lists(characteristics, uuid);
}

uint8_t config_ble_characteristic_should_subscribe(const char *uuid)
{
cJSON *subscribe = config_ble_get_name_by_uuid(0, uuid, "subscribe");

/* Not defined, to subscribe is default */
if (cJSON_IsTrue(subscribe))
{
return true;
}
else if (cJSON_IsFalse(subscribe))
{
return false;
}
return true;
Copy link
Owner

Choose a reason for hiding this comment

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

This can all be changed to simply

return !cJSON_IsFalse(subscribe);

}

uint8_t config_ble_service_should_include(const char *uuid)
{
cJSON *ble = cJSON_GetObjectItemCaseSensitive(config, "ble");
Expand Down
1 change: 1 addition & 0 deletions main/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const char *config_ble_service_name_get(const char *uuid);
const char *config_ble_characteristic_name_get(const char *uuid);
const char **config_ble_characteristic_types_get(const char *uuid);
uint8_t config_ble_characteristic_should_include(const char *uuid);
uint8_t config_ble_characteristic_should_subscribe(const char *uuid);
uint8_t config_ble_service_should_include(const char *uuid);
uint8_t config_ble_should_connect(const char *mac);
uint32_t config_ble_passkey_get(const char *mac);
Expand Down