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 2 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
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
2 changes: 1 addition & 1 deletion main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ foreach(file ${www_files})
get_filename_component(www_file_dir ${build_dir}/www/${file}.gz DIRECTORY)
file(MAKE_DIRECTORY ${www_file_dir})
add_custom_command(OUTPUT ${build_dir}/www/${file}.gz
COMMAND gzip -c ${PROJECT_DIR}/www/${file} > ${build_dir}/www/${file}.gz
COMMAND tar -czf ${build_dir}/www/${file}.gz ${PROJECT_DIR}/www/${file}
DEPENDS ${PROJECT_DIR}/www/${file})
target_add_binary_data(${COMPONENT_LIB} ${build_dir}/www/${file}.gz
BINARY RENAME_TO ${c_name})
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
9 changes: 9 additions & 0 deletions main/ble_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ static characteristic_type_t ble_atotype(const char *type)
{ "reg-cert-data-list", CHAR_TYPE_REG_CERT_DATA_LIST },
{ "variable", CHAR_TYPE_VARIABLE },
{ "gatt-uuid", CHAR_TYPE_GATT_UUID },
{ "hex", CHAR_TYPE_HEX },
{ NULL, CHAR_TYPE_UNKNOWN },
};

Expand Down Expand Up @@ -241,6 +242,7 @@ static size_t ble_type_size(characteristic_type_t type)
case CHAR_TYPE_UINT16:
case CHAR_TYPE_SINT16:
case CHAR_TYPE_SFLOAT:
case CHAR_TYPE_HEX:
return 2;
case CHAR_TYPE_24BIT:
case CHAR_TYPE_UINT24:
Expand Down Expand Up @@ -293,6 +295,9 @@ char *chartoa(ble_uuid_t uuid, const uint8_t *data, size_t len)

switch (*types)
{
case CHAR_TYPE_HEX:
p += sprintf(p, "%2hhx,", data[i]);
break;
case CHAR_TYPE_BOOLEAN:
p += sprintf(p, "%s,", data[i] & 0x01 ? "true" : "false");
break;
Expand Down Expand Up @@ -462,6 +467,10 @@ uint8_t *atochar(ble_uuid_t uuid, const char *data, size_t len, size_t *ret_len)
{
switch (*types)
{
case CHAR_TYPE_HEX:
sscanf(val, "%2hhx", p);
p += 1;
break;
case CHAR_TYPE_BOOLEAN:
*p = !strcmp(val, "true") ? 1 : 0;
p += 1;
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
3 changes: 2 additions & 1 deletion main/gatt.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ typedef enum {
CHAR_TYPE_UINT48,
CHAR_TYPE_UINT8,
CHAR_TYPE_UTF8S,
CHAR_TYPE_VARIABLE
CHAR_TYPE_VARIABLE,
CHAR_TYPE_HEX
} characteristic_type_t;

typedef struct {
Expand Down