From 097d49ba54371626e7b8824d6a15b42b1e42795e Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Sat, 25 Feb 2023 00:47:45 +0100 Subject: [PATCH 01/13] added index to most characteristic methods --- main/ble.c | 44 ++++++++++++++++++++++++++++-------------- main/ble.h | 12 ++++++------ main/ble2mqtt.c | 50 +++++++++++++++++++++++++++++------------------- main/ble_utils.c | 26 +++++++++++++++++++++---- main/ble_utils.h | 4 ++-- 5 files changed, 90 insertions(+), 46 deletions(-) diff --git a/main/ble.c b/main/ble.c index e189601..583e720 100644 --- a/main/ble.c +++ b/main/ble.c @@ -442,10 +442,11 @@ int ble_foreach_characteristic(mac_addr_t mac, ble_device_t *dev; ble_service_t *service; ble_characteristic_t *characteristic; + ble_characteristic_t *characteristic_known; xSemaphoreTakeRecursive(devices_list_semaphore, portMAX_DELAY); - if (!(dev = ble_device_find_by_mac(devices_list, mac))) + if ((dev = ble_device_find_by_mac(devices_list, mac)) == NULL) { xSemaphoreGiveRecursive(devices_list_semaphore); return -1; @@ -460,7 +461,20 @@ int ble_foreach_characteristic(mac_addr_t mac, for (characteristic = service->characteristics; characteristic; characteristic = characteristic->next) { - cb(mac, service->uuid, characteristic->uuid, + uint16_t index = 0; + + // search all known characteristics and count duplicate UUID's + for( + characteristic_known = service->characteristics; + characteristic_known != characteristic; + characteristic_known = characteristic_known->next + ){ + if(memcmp(characteristic_known->uuid, characteristic->uuid, sizeof(ble_uuid_t)) == 0){ + index++; + } + } + + cb(mac, service->uuid, characteristic->uuid, index, characteristic->properties); } } @@ -470,7 +484,7 @@ int ble_foreach_characteristic(mac_addr_t mac, } int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid) + ble_uuid_t characteristic_uuid, uint16_t index) { ble_device_t *device; ble_service_t *service; @@ -486,7 +500,7 @@ int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, goto Exit; if (!(characteristic = ble_device_characteristic_find_by_uuid(service, - characteristic_uuid))) + characteristic_uuid, index))) { goto Exit; } @@ -504,7 +518,7 @@ int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, const uint8_t *value, size_t value_len) + ble_uuid_t characteristic_uuid, uint16_t index, const uint8_t *value, size_t value_len) { ble_device_t *device; ble_service_t *service; @@ -520,7 +534,7 @@ int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, goto Exit; if (!(characteristic = ble_device_characteristic_find_by_uuid(service, - characteristic_uuid))) + characteristic_uuid, index))) { goto Exit; } @@ -540,7 +554,7 @@ int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid) + ble_uuid_t characteristic_uuid, uint16_t index) { uint16_t enable = htole16(0x1); ble_device_t *device; @@ -557,7 +571,7 @@ int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, goto Exit; if (!(characteristic = ble_device_characteristic_find_by_uuid(service, - characteristic_uuid))) + characteristic_uuid, index))) { goto Exit; } @@ -589,7 +603,7 @@ int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_notify_unregister(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid) + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index) { ble_device_t *device; ble_service_t *service; @@ -605,7 +619,7 @@ int ble_characteristic_notify_unregister(mac_addr_t mac, goto Exit; if (!(characteristic = ble_device_characteristic_find_by_uuid(service, - characteristic_uuid))) + characteristic_uuid, index))) { goto Exit; } @@ -899,6 +913,7 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, ble_device_t *device; ble_service_t *service; ble_characteristic_t *characteristic; + uint16_t index; need_dequeue = 1; @@ -936,10 +951,10 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, } else if (!ble_device_info_get_by_conn_id_handle(devices_list, param->read.conn_id, param->read.handle, &device, &service, - &characteristic) && on_device_characteristic_value_cb) + &characteristic, &index) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, - characteristic->uuid, param->read.value, param->read.value_len); + characteristic->uuid, index, param->read.value, param->read.value_len); } xSemaphoreGiveRecursive(devices_list_semaphore); @@ -973,15 +988,16 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, ble_device_t *device; ble_service_t *service; ble_characteristic_t *characteristic; + uint16_t index; xSemaphoreTakeRecursive(devices_list_semaphore, portMAX_DELAY); if (!ble_device_info_get_by_conn_id_handle(devices_list, param->notify.conn_id, param->notify.handle, &device, &service, - &characteristic) && on_device_characteristic_value_cb) + &characteristic, &index) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, - characteristic->uuid, param->notify.value, + characteristic->uuid, index, param->notify.value, param->notify.value_len); } diff --git a/main/ble.h b/main/ble.h index 17528bd..35a3a68 100644 --- a/main/ble.h +++ b/main/ble.h @@ -31,10 +31,10 @@ typedef void (*ble_on_device_connected_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_disconnected_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_services_discovered_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_characteristic_found_cb_t)(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index, uint8_t properties); typedef void (*ble_on_device_characteristic_value_cb_t)(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, size_t value_len); typedef uint32_t (*ble_on_passkey_requested_cb_t)(mac_addr_t mac); @@ -65,13 +65,13 @@ int ble_foreach_characteristic(mac_addr_t mac, ble_on_device_characteristic_found_cb_t cb); int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid); + ble_uuid_t characteristic_uuid, uint16_t index); int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, const uint8_t *value, size_t value_len); + ble_uuid_t characteristic_uuid, uint16_t index, const uint8_t *value, size_t value_len); int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid); + ble_uuid_t characteristic_uuid, uint16_t index); int ble_characteristic_notify_unregister(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid); + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index); /* Management */ ble_dev_t *ble_devices_list_get(size_t *number_of_devices); diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index 6e1406e..6201b7e 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -29,6 +29,7 @@ typedef struct { mac_addr_t mac; ble_uuid_t service; ble_uuid_t characteristic; + uint16_t index; } mqtt_ctx_t; static const char *device_name_get(void) @@ -312,13 +313,14 @@ static void ble_publish_connected(mac_addr_t mac, uint8_t is_connected) } static mqtt_ctx_t *ble_ctx_gen(mac_addr_t mac, ble_uuid_t service, - ble_uuid_t characteristic) + ble_uuid_t characteristic, uint16_t index) { mqtt_ctx_t *ctx = malloc(sizeof(mqtt_ctx_t)); memcpy(ctx->mac, mac, sizeof(mac_addr_t)); memcpy(ctx->service, service, sizeof(ble_uuid_t)); memcpy(ctx->characteristic, characteristic, sizeof(ble_uuid_t)); + ctx->index = index; return ctx; } @@ -381,17 +383,21 @@ static char *ble_topic_suffix(char *base, uint8_t is_get) } static char *ble_topic(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid) + ble_uuid_t characteristic_uuid, uint16_t index) { static char topic[MAX_TOPIC_LEN]; - int i; + int i = 0; - i = snprintf(topic, MAX_TOPIC_LEN, "%s" MAC_FMT "/%s", + i += snprintf(topic + i, MAX_TOPIC_LEN, "%s" MAC_FMT "/%s", config_mqtt_prefix_get(), MAC_PARAM(mac), ble_service_name_get(service_uuid)); - snprintf(topic + i, MAX_TOPIC_LEN - i, "/%s", + i += snprintf(topic + i, MAX_TOPIC_LEN - i, "/%s", ble_characteristic_name_get(characteristic_uuid)); + if(index > 0){ + i += snprintf(topic + i, MAX_TOPIC_LEN - i, "_%u", index); + } + return topic; } @@ -412,7 +418,7 @@ static void ble_on_mqtt_get(const char *topic, const uint8_t *payload, ESP_LOGD(TAG, "Got read request: %s", topic); mqtt_ctx_t *data = (mqtt_ctx_t *)ctx; - ble_characteristic_read(data->mac, data->service, data->characteristic); + ble_characteristic_read(data->mac, data->service, data->characteristic, data->index); } static void ble_on_mqtt_set(const char *topic, const uint8_t *payload, @@ -424,11 +430,11 @@ static void ble_on_mqtt_set(const char *topic, const uint8_t *payload, uint8_t *buf = atochar(data->characteristic, (const char *)payload, len, &buf_len); - ble_characteristic_write(data->mac, data->service, data->characteristic, + ble_characteristic_write(data->mac, data->service, data->characteristic, data->index, buf, buf_len); /* Issue a read request to get latest value */ - ble_characteristic_read(data->mac, data->service, data->characteristic); + ble_characteristic_read(data->mac, data->service, data->characteristic, data->index); } static void _ble_on_mqtt_get(const char *topic, const uint8_t *payload, @@ -437,11 +443,11 @@ static void _ble_on_mqtt_set(const char *topic, const uint8_t *payload, size_t len, void *ctx); static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint8_t properties) + ble_uuid_t characteristic_uuid, uint16_t index, uint8_t properties) { - ESP_LOGD(TAG, "Found new characteristic: service: " UUID_FMT - ", characteristic: " UUID_FMT ", properties: 0x%x", - UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), properties); + ESP_LOGI(TAG, "Found new characteristic: service: " UUID_FMT + ", characteristic: " UUID_FMT " index %d, properties: 0x%x", + UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), index, properties); char *topic; if (!config_ble_service_should_include(uuidtoa(service_uuid)) || @@ -450,15 +456,15 @@ static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, return; } - topic = ble_topic(mac, service_uuid, characteristic_uuid); + topic = ble_topic(mac, service_uuid, characteristic_uuid, index); /* Characteristic is readable */ if (properties & CHAR_PROP_READ) { mqtt_subscribe(ble_topic_suffix(topic, 1), config_mqtt_qos_get(), _ble_on_mqtt_get, ble_ctx_gen(mac, service_uuid, - characteristic_uuid), free); - ble_characteristic_read(mac, service_uuid, characteristic_uuid); + characteristic_uuid, index), free); + ble_characteristic_read(mac, service_uuid, characteristic_uuid, index); } /* Characteristic is writable */ @@ -466,14 +472,14 @@ static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, { mqtt_subscribe(ble_topic_suffix(topic, 0), config_mqtt_qos_get(), _ble_on_mqtt_set, ble_ctx_gen(mac, service_uuid, - characteristic_uuid), free); + characteristic_uuid, index), free); } /* Characteristic can notify / indicate on changes */ if (properties & (CHAR_PROP_NOTIFY | CHAR_PROP_INDICATE)) { ble_characteristic_notify_register(mac, service_uuid, - characteristic_uuid); + characteristic_uuid, index); } } @@ -484,10 +490,10 @@ static void ble_on_device_services_discovered(mac_addr_t mac) } static void ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, size_t value_len) { - char *topic = ble_topic(mac, service, characteristic); + char *topic = ble_topic(mac, service, characteristic, index); char *payload = chartoa(characteristic, value, value_len); size_t payload_len = strlen(payload); @@ -565,6 +571,7 @@ typedef struct { mac_addr_t mac; ble_uuid_t service; ble_uuid_t characteristic; + uint16_t index; uint8_t *value; size_t value_len; } ble_device_characteristic_value; @@ -635,6 +642,7 @@ static void ble2mqtt_handle_event(event_t *event) event->ble_device_characteristic_value.mac, event->ble_device_characteristic_value.service, event->ble_device_characteristic_value.characteristic, + event->ble_device_characteristic_value.index, event->ble_device_characteristic_value.value, event->ble_device_characteristic_value.value_len); free(event->ble_device_characteristic_value.value); @@ -858,7 +866,7 @@ static void _ble_on_device_services_discovered(mac_addr_t mac) } static void _ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, size_t value_len) { event_t *event = malloc(sizeof(*event)); @@ -873,6 +881,8 @@ static void _ble_on_device_characteristic_value(mac_addr_t mac, memcpy(event->ble_device_characteristic_value.value, value, value_len); event->ble_device_characteristic_value.value_len = value_len; + event->ble_device_characteristic_value.index = index; + ESP_LOGD(TAG, "Queuing event BLE_DEVICE_CHARACTERISTIC_VALUE (" MAC_FMT ", " UUID_FMT ", %p, %u)", MAC_PARAM(mac), UUID_PARAM(characteristic), value, value_len); diff --git a/main/ble_utils.c b/main/ble_utils.c index 0722f68..2fe0e4b 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -873,14 +873,19 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, } ble_characteristic_t *ble_device_characteristic_find_by_uuid( - ble_service_t *service, ble_uuid_t uuid) + ble_service_t *service, ble_uuid_t uuid, uint16_t index) { ble_characteristic_t *cur; for (cur = service->characteristics; cur; cur = cur->next) { - if (!memcmp(cur->uuid, uuid, sizeof(ble_uuid_t))) + if (memcmp(cur->uuid, uuid, sizeof(ble_uuid_t)) == 0){ + if(index > 0){ + index--; + continue; + } break; + } } return cur; @@ -920,8 +925,10 @@ void ble_device_characteristics_free(ble_characteristic_t **list) int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, uint16_t handle, ble_device_t **device, ble_service_t **service, - ble_characteristic_t **characteristic) + ble_characteristic_t **characteristic, uint16_t *index) { + ble_characteristic_t *known_characteristic; + if (!(*device = ble_device_find_by_conn_id(list, conn_id))) return -1; @@ -930,8 +937,19 @@ int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, for (*characteristic = (*service)->characteristics; *characteristic; *characteristic = (*characteristic)->next) { - if ((*characteristic)->handle == handle) + if ((*characteristic)->handle == handle){ + *index = 0; + for( + known_characteristic = (*service)->characteristics; + known_characteristic != (*characteristic); + known_characteristic = known_characteristic->next + ){ + if(memcmp(known_characteristic->uuid, (*characteristic)->uuid, sizeof(ble_uuid_t)) == 0){ + (*index)++; + } + } return 0; + } } } diff --git a/main/ble_utils.h b/main/ble_utils.h index 6594906..f737796 100644 --- a/main/ble_utils.h +++ b/main/ble_utils.h @@ -84,7 +84,7 @@ void ble_device_services_free(ble_service_t **list); ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, ble_uuid_t uuid, uint16_t handle, uint8_t properties); ble_characteristic_t *ble_device_characteristic_find_by_uuid( - ble_service_t *service, ble_uuid_t uuid); + ble_service_t *service, ble_uuid_t uuid, uint16_t index); ble_characteristic_t *ble_device_characteristic_find_by_handle( ble_service_t *service, uint16_t handle); void ble_device_characteristic_free(ble_characteristic_t *characteristic); @@ -92,6 +92,6 @@ void ble_device_characteristics_free(ble_characteristic_t **list); int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, uint16_t handle, ble_device_t **device, ble_service_t **service, - ble_characteristic_t **characteristic); + ble_characteristic_t **characteristic, uint16_t *index); #endif From 625cc8941115761465f63ff657b1f48939988a3a Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Sat, 25 Feb 2023 01:42:38 +0100 Subject: [PATCH 02/13] fixed type for AIO characteristic --- main/gatt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/gatt.c b/main/gatt.c index e9e45ea..f8d9d88 100644 --- a/main/gatt.c +++ b/main/gatt.c @@ -249,8 +249,8 @@ static characteristic_type_t types_00002a52_0000_1000_8000_00805f9b34fb[] = { CH static characteristic_type_t types_00002a53_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_8BIT, CHAR_TYPE_UINT16, CHAR_TYPE_UINT8, CHAR_TYPE_UINT16, CHAR_TYPE_UINT32, -1 }; static characteristic_type_t types_00002a54_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_16BIT, -1 }; static characteristic_type_t types_00002a55_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT8, CHAR_TYPE_VARIABLE, CHAR_TYPE_UINT8, CHAR_TYPE_UINT8, CHAR_TYPE_UINT8, CHAR_TYPE_VARIABLE, -1 }; -static characteristic_type_t types_00002a56_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_2BIT, -1 }; -static characteristic_type_t types_00002a57_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT8, -1 }; +static characteristic_type_t types_00002a56_0000_1000_8000_00805f9b34fb[] = { -1 }; +static characteristic_type_t types_00002a57_0000_1000_8000_00805f9b34fb[] = { -1 }; static characteristic_type_t types_00002a58_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT16, -1 }; static characteristic_type_t types_00002a59_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT16, -1 }; static characteristic_type_t types_00002a5a_0000_1000_8000_00805f9b34fb[] = { -1 }; From 15b35af7f91a01555d1ebc46e66f2e35f927dd40 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 3 Mar 2023 10:02:11 +0100 Subject: [PATCH 03/13] switched back to old coding style --- main/ble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/ble.c b/main/ble.c index 583e720..a61a589 100644 --- a/main/ble.c +++ b/main/ble.c @@ -446,7 +446,7 @@ int ble_foreach_characteristic(mac_addr_t mac, xSemaphoreTakeRecursive(devices_list_semaphore, portMAX_DELAY); - if ((dev = ble_device_find_by_mac(devices_list, mac)) == NULL) + if (!(dev = ble_device_find_by_mac(devices_list, mac))) { xSemaphoreGiveRecursive(devices_list_semaphore); return -1; From dbe2aeaf8eb1c87ea28050feb808969fba25b676 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 3 Mar 2023 10:10:38 +0100 Subject: [PATCH 04/13] fixed format specifier --- main/ble2mqtt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index 6201b7e..5fb9b07 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -446,7 +446,7 @@ static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index, uint8_t properties) { ESP_LOGI(TAG, "Found new characteristic: service: " UUID_FMT - ", characteristic: " UUID_FMT " index %d, properties: 0x%x", + ", characteristic: " UUID_FMT ", index: " PRIu16 ", properties: 0x%x", UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), index, properties); char *topic; From d5b9f2ec769917c333d0d7cc24a391e608ff2cf3 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 3 Mar 2023 14:25:36 +0100 Subject: [PATCH 05/13] moved char. index to ble_characteristic_t --- main/ble.c | 21 +++------------------ main/ble_utils.c | 26 +++++++++----------------- main/ble_utils.h | 3 ++- 3 files changed, 14 insertions(+), 36 deletions(-) diff --git a/main/ble.c b/main/ble.c index c7d7704..8620ed1 100644 --- a/main/ble.c +++ b/main/ble.c @@ -461,20 +461,7 @@ int ble_foreach_characteristic(mac_addr_t mac, for (characteristic = service->characteristics; characteristic; characteristic = characteristic->next) { - uint16_t index = 0; - - // search all known characteristics and count duplicate UUID's - for( - characteristic_known = service->characteristics; - characteristic_known != characteristic; - characteristic_known = characteristic_known->next - ){ - if(memcmp(characteristic_known->uuid, characteristic->uuid, sizeof(ble_uuid_t)) == 0){ - index++; - } - } - - cb(mac, service->uuid, characteristic->uuid, index, + cb(mac, service->uuid, characteristic->uuid, characteristic->properties); } } @@ -913,7 +900,6 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, ble_device_t *device; ble_service_t *service; ble_characteristic_t *characteristic; - uint16_t index; need_dequeue = 1; @@ -951,7 +937,7 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, } else if (!ble_device_info_get_by_conn_id_handle(devices_list, param->read.conn_id, param->read.handle, &device, &service, - &characteristic, &index) && on_device_characteristic_value_cb) + &characteristic) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, characteristic->uuid, index, param->read.value, param->read.value_len); @@ -988,13 +974,12 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, ble_device_t *device; ble_service_t *service; ble_characteristic_t *characteristic; - uint16_t index; xSemaphoreTakeRecursive(devices_list_semaphore, portMAX_DELAY); if (!ble_device_info_get_by_conn_id_handle(devices_list, param->notify.conn_id, param->notify.handle, &device, &service, - &characteristic, &index) && on_device_characteristic_value_cb) + &characteristic) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, characteristic->uuid, index, param->notify.value, diff --git a/main/ble_utils.c b/main/ble_utils.c index df38554..8c35538 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -873,8 +873,14 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, characteristic->handle = handle; characteristic->properties = properties; characteristic->client_config_handle = 0; + characteristic->index = 0; + + for (cur = &service->characteristics; *cur; cur = &(*cur)->next){ + if ble_uuid_equal(cur->uuid, uuid){ + characteristic->index++; + } + }; - for (cur = &service->characteristics; *cur; cur = &(*cur)->next); *cur = characteristic; return characteristic; @@ -887,11 +893,7 @@ ble_characteristic_t *ble_device_characteristic_find_by_uuid( for (cur = service->characteristics; cur; cur = cur->next) { - if (ble_uuid_equal(cur->uuid, uuid)){ - if(index > 0){ - index--; - continue; - } + if (ble_uuid_equal(cur->uuid, uuid) && cur->index == index){ break; } } @@ -933,7 +935,7 @@ void ble_device_characteristics_free(ble_characteristic_t **list) int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, uint16_t handle, ble_device_t **device, ble_service_t **service, - ble_characteristic_t **characteristic, uint16_t *index) + ble_characteristic_t **characteristic) { ble_characteristic_t *known_characteristic; @@ -946,16 +948,6 @@ int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, *characteristic = (*characteristic)->next) { if ((*characteristic)->handle == handle){ - *index = 0; - for( - known_characteristic = (*service)->characteristics; - known_characteristic != (*characteristic); - known_characteristic = known_characteristic->next - ){ - if(memcmp(known_characteristic->uuid, (*characteristic)->uuid, sizeof(ble_uuid_t)) == 0){ - (*index)++; - } - } return 0; } } diff --git a/main/ble_utils.h b/main/ble_utils.h index e5ab494..4d69fce 100644 --- a/main/ble_utils.h +++ b/main/ble_utils.h @@ -23,6 +23,7 @@ typedef uint8_t ble_uuid_t[16]; typedef struct ble_characteristic_t { struct ble_characteristic_t *next; ble_uuid_t uuid; + uint8_t index; uint16_t handle; uint8_t properties; uint16_t client_config_handle; @@ -95,6 +96,6 @@ bool ble_mac_equal(mac_addr_t mac1, mac_addr_t mac2); int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, uint16_t handle, ble_device_t **device, ble_service_t **service, - ble_characteristic_t **characteristic, uint16_t *index); + ble_characteristic_t **characteristic); #endif From 7dbc6c12635298ce869437bb1d8e5bf03ea33498 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 3 Mar 2023 14:27:25 +0100 Subject: [PATCH 06/13] changed index value to uint8_t --- main/ble.c | 8 ++++---- main/ble.h | 12 ++++++------ main/ble2mqtt.c | 13 ++++++------- main/ble_utils.c | 2 +- main/ble_utils.h | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/main/ble.c b/main/ble.c index 8620ed1..5244d68 100644 --- a/main/ble.c +++ b/main/ble.c @@ -471,7 +471,7 @@ int ble_foreach_characteristic(mac_addr_t mac, } int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index) + ble_uuid_t characteristic_uuid, uint8_t index) { ble_device_t *device; ble_service_t *service; @@ -505,7 +505,7 @@ int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index, const uint8_t *value, size_t value_len) + ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, size_t value_len) { ble_device_t *device; ble_service_t *service; @@ -541,7 +541,7 @@ int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index) + ble_uuid_t characteristic_uuid, uint8_t index) { uint16_t enable = htole16(0x1); ble_device_t *device; @@ -590,7 +590,7 @@ int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_notify_unregister(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index) + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index) { ble_device_t *device; ble_service_t *service; diff --git a/main/ble.h b/main/ble.h index 35a3a68..d8aab34 100644 --- a/main/ble.h +++ b/main/ble.h @@ -31,10 +31,10 @@ typedef void (*ble_on_device_connected_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_disconnected_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_services_discovered_cb_t)(mac_addr_t mac); typedef void (*ble_on_device_characteristic_found_cb_t)(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index, + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index, uint8_t properties); typedef void (*ble_on_device_characteristic_value_cb_t)(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, size_t value_len); typedef uint32_t (*ble_on_passkey_requested_cb_t)(mac_addr_t mac); @@ -65,13 +65,13 @@ int ble_foreach_characteristic(mac_addr_t mac, ble_on_device_characteristic_found_cb_t cb); int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index); + ble_uuid_t characteristic_uuid, uint8_t index); int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index, const uint8_t *value, size_t value_len); + ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, size_t value_len); int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index); + ble_uuid_t characteristic_uuid, uint8_t index); int ble_characteristic_notify_unregister(mac_addr_t mac, - ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint16_t index); + ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index); /* Management */ ble_dev_t *ble_devices_list_get(size_t *number_of_devices); diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index 5fb9b07..0a73c41 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -29,7 +29,6 @@ typedef struct { mac_addr_t mac; ble_uuid_t service; ble_uuid_t characteristic; - uint16_t index; } mqtt_ctx_t; static const char *device_name_get(void) @@ -313,7 +312,7 @@ static void ble_publish_connected(mac_addr_t mac, uint8_t is_connected) } static mqtt_ctx_t *ble_ctx_gen(mac_addr_t mac, ble_uuid_t service, - ble_uuid_t characteristic, uint16_t index) + ble_uuid_t characteristic, uint8_t index) { mqtt_ctx_t *ctx = malloc(sizeof(mqtt_ctx_t)); @@ -383,7 +382,7 @@ static char *ble_topic_suffix(char *base, uint8_t is_get) } static char *ble_topic(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index) + ble_uuid_t characteristic_uuid, uint8_t index) { static char topic[MAX_TOPIC_LEN]; int i = 0; @@ -443,7 +442,7 @@ static void _ble_on_mqtt_set(const char *topic, const uint8_t *payload, size_t len, void *ctx); static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint16_t index, uint8_t properties) + ble_uuid_t characteristic_uuid, uint8_t index, uint8_t properties) { ESP_LOGI(TAG, "Found new characteristic: service: " UUID_FMT ", characteristic: " UUID_FMT ", index: " PRIu16 ", properties: 0x%x", @@ -490,7 +489,7 @@ static void ble_on_device_services_discovered(mac_addr_t mac) } static void ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, size_t value_len) { char *topic = ble_topic(mac, service, characteristic, index); @@ -571,7 +570,7 @@ typedef struct { mac_addr_t mac; ble_uuid_t service; ble_uuid_t characteristic; - uint16_t index; + uint8_t index; uint8_t *value; size_t value_len; } ble_device_characteristic_value; @@ -866,7 +865,7 @@ static void _ble_on_device_services_discovered(mac_addr_t mac) } static void _ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint16_t index, uint8_t *value, + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, size_t value_len) { event_t *event = malloc(sizeof(*event)); diff --git a/main/ble_utils.c b/main/ble_utils.c index 8c35538..0eecdac 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -887,7 +887,7 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, } ble_characteristic_t *ble_device_characteristic_find_by_uuid( - ble_service_t *service, ble_uuid_t uuid, uint16_t index) + ble_service_t *service, ble_uuid_t uuid, uint8_t index) { ble_characteristic_t *cur; diff --git a/main/ble_utils.h b/main/ble_utils.h index 4d69fce..968932b 100644 --- a/main/ble_utils.h +++ b/main/ble_utils.h @@ -85,7 +85,7 @@ void ble_device_services_free(ble_service_t **list); ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, ble_uuid_t uuid, uint16_t handle, uint8_t properties); ble_characteristic_t *ble_device_characteristic_find_by_uuid( - ble_service_t *service, ble_uuid_t uuid, uint16_t index); + ble_service_t *service, ble_uuid_t uuid, uint8_t index); ble_characteristic_t *ble_device_characteristic_find_by_handle( ble_service_t *service, uint16_t handle); void ble_device_characteristic_free(ble_characteristic_t *characteristic); From 3bf3046a38bab504ca77ad421c22a3734e475ae4 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 3 Mar 2023 14:52:12 +0100 Subject: [PATCH 07/13] fixed missing index parameters --- main/ble.c | 7 +++---- main/ble2mqtt.c | 3 ++- main/ble_utils.c | 4 +--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/main/ble.c b/main/ble.c index 5244d68..dcd83a3 100644 --- a/main/ble.c +++ b/main/ble.c @@ -442,7 +442,6 @@ int ble_foreach_characteristic(mac_addr_t mac, ble_device_t *dev; ble_service_t *service; ble_characteristic_t *characteristic; - ble_characteristic_t *characteristic_known; xSemaphoreTakeRecursive(devices_list_semaphore, portMAX_DELAY); @@ -462,7 +461,7 @@ int ble_foreach_characteristic(mac_addr_t mac, characteristic = characteristic->next) { cb(mac, service->uuid, characteristic->uuid, - characteristic->properties); + characteristic->index, characteristic->properties); } } @@ -940,7 +939,7 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, &characteristic) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, - characteristic->uuid, index, param->read.value, param->read.value_len); + characteristic->uuid, characteristic->index, param->read.value, param->read.value_len); } xSemaphoreGiveRecursive(devices_list_semaphore); @@ -982,7 +981,7 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, &characteristic) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, - characteristic->uuid, index, param->notify.value, + characteristic->uuid, characteristic->index, param->notify.value, param->notify.value_len); } diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index 0a73c41..8718a66 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -29,6 +29,7 @@ typedef struct { mac_addr_t mac; ble_uuid_t service; ble_uuid_t characteristic; + uint8_t index; } mqtt_ctx_t; static const char *device_name_get(void) @@ -445,7 +446,7 @@ static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index, uint8_t properties) { ESP_LOGI(TAG, "Found new characteristic: service: " UUID_FMT - ", characteristic: " UUID_FMT ", index: " PRIu16 ", properties: 0x%x", + ", characteristic: " UUID_FMT ", index: %u, properties: 0x%x", UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), index, properties); char *topic; diff --git a/main/ble_utils.c b/main/ble_utils.c index 0eecdac..34a72f6 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -876,7 +876,7 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, characteristic->index = 0; for (cur = &service->characteristics; *cur; cur = &(*cur)->next){ - if ble_uuid_equal(cur->uuid, uuid){ + if (ble_uuid_equal((*cur)->uuid, uuid)){ characteristic->index++; } }; @@ -937,8 +937,6 @@ int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, uint16_t handle, ble_device_t **device, ble_service_t **service, ble_characteristic_t **characteristic) { - ble_characteristic_t *known_characteristic; - if (!(*device = ble_device_find_by_conn_id(list, conn_id))) return -1; From 58d8116742b2f1e52dc72208dc17affda767411d Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 8 Mar 2023 01:36:52 +0100 Subject: [PATCH 08/13] formating --- main/ble.c | 12 +++++++----- main/ble.h | 7 ++++--- main/ble2mqtt.c | 7 +++---- main/ble_utils.c | 10 ++++------ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/main/ble.c b/main/ble.c index dcd83a3..fa3d24a 100644 --- a/main/ble.c +++ b/main/ble.c @@ -411,7 +411,7 @@ static void ble_update_cache(ble_device_t *dev) free(db); return; } - + /* Find all characteristics and cache them */ for (i = 0; i < count; i++) { @@ -460,7 +460,7 @@ int ble_foreach_characteristic(mac_addr_t mac, for (characteristic = service->characteristics; characteristic; characteristic = characteristic->next) { - cb(mac, service->uuid, characteristic->uuid, + cb(mac, service->uuid, characteristic->uuid, characteristic->index, characteristic->properties); } } @@ -504,7 +504,8 @@ int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, } int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, size_t value_len) + ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, + size_t value_len) { ble_device_t *device; ble_service_t *service; @@ -710,7 +711,7 @@ static void gap_cb(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) if (param->scan_rst.search_evt != ESP_GAP_SEARCH_INQ_RES_EVT) break; - + /* Check if this device is a broadcaster */ broadcaster_ops_t *broadcaster_ops = broadcaster_ops_get( param->scan_rst.ble_adv, param->scan_rst.adv_data_len); @@ -939,7 +940,8 @@ static void esp_gattc_cb(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, &characteristic) && on_device_characteristic_value_cb) { on_device_characteristic_value_cb(device->mac, service->uuid, - characteristic->uuid, characteristic->index, param->read.value, param->read.value_len); + characteristic->uuid, characteristic->index, + param->read.value, param->read.value_len); } xSemaphoreGiveRecursive(devices_list_semaphore); diff --git a/main/ble.h b/main/ble.h index d8aab34..ffa16e3 100644 --- a/main/ble.h +++ b/main/ble.h @@ -34,8 +34,8 @@ typedef void (*ble_on_device_characteristic_found_cb_t)(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index, uint8_t properties); typedef void (*ble_on_device_characteristic_value_cb_t)(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, - size_t value_len); + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, + uint8_t *value, size_t value_len); typedef uint32_t (*ble_on_passkey_requested_cb_t)(mac_addr_t mac); /* Event handlers */ @@ -67,7 +67,8 @@ int ble_foreach_characteristic(mac_addr_t mac, int ble_characteristic_read(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index); int ble_characteristic_write(mac_addr_t mac, ble_uuid_t service_uuid, - ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, size_t value_len); + ble_uuid_t characteristic_uuid, uint8_t index, const uint8_t *value, + size_t value_len); int ble_characteristic_notify_register(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index); int ble_characteristic_notify_unregister(mac_addr_t mac, diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index 8718a66..d7391c0 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -300,7 +300,7 @@ static void ble_publish_connected(mac_addr_t mac, uint8_t is_connected) if (is_connected) { const char *device_name = device_name_get(); - + /* Subscribe for other devices claiming this device is disconnected */ mqtt_subscribe(topic, config_mqtt_qos_get(), _ble_on_mqtt_connected_cb, strdup(mactoa(mac)), free); @@ -866,8 +866,8 @@ static void _ble_on_device_services_discovered(mac_addr_t mac) } static void _ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, - size_t value_len) + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, + uint8_t *value, size_t value_len) { event_t *event = malloc(sizeof(*event)); @@ -880,7 +880,6 @@ static void _ble_on_device_characteristic_value(mac_addr_t mac, event->ble_device_characteristic_value.value = malloc(value_len); memcpy(event->ble_device_characteristic_value.value, value, value_len); event->ble_device_characteristic_value.value_len = value_len; - event->ble_device_characteristic_value.index = index; ESP_LOGD(TAG, "Queuing event BLE_DEVICE_CHARACTERISTIC_VALUE (" MAC_FMT ", " diff --git a/main/ble_utils.c b/main/ble_utils.c index 34a72f6..4c6e7e5 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -122,7 +122,7 @@ int atouuid(const char *str, ble_uuid_t uuid) "%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx", &uuid[15], &uuid[14], &uuid[13], &uuid[12], &uuid[11], &uuid[10], - &uuid[9], &uuid[8], + &uuid[9], &uuid[8], &uuid[7], &uuid[6], &uuid[5], &uuid[4], &uuid[3], &uuid[2], &uuid[1], &uuid[0]) != 16; } @@ -893,9 +893,8 @@ ble_characteristic_t *ble_device_characteristic_find_by_uuid( for (cur = service->characteristics; cur; cur = cur->next) { - if (ble_uuid_equal(cur->uuid, uuid) && cur->index == index){ + if (ble_uuid_equal(cur->uuid, uuid) && cur->index == index) break; - } } return cur; @@ -939,15 +938,14 @@ int ble_device_info_get_by_conn_id_handle(ble_device_t *list, uint16_t conn_id, { if (!(*device = ble_device_find_by_conn_id(list, conn_id))) return -1; - + for (*service = (*device)->services; *service; *service = (*service)->next) { for (*characteristic = (*service)->characteristics; *characteristic; *characteristic = (*characteristic)->next) { - if ((*characteristic)->handle == handle){ + if ((*characteristic)->handle == handle) return 0; - } } } From 2648b90cd079546652c000d352d27ea50fd6f50e Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 8 Mar 2023 01:39:15 +0100 Subject: [PATCH 09/13] removed redundant work --- main/gatt.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/gatt.c b/main/gatt.c index f8d9d88..e9e45ea 100644 --- a/main/gatt.c +++ b/main/gatt.c @@ -249,8 +249,8 @@ static characteristic_type_t types_00002a52_0000_1000_8000_00805f9b34fb[] = { CH static characteristic_type_t types_00002a53_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_8BIT, CHAR_TYPE_UINT16, CHAR_TYPE_UINT8, CHAR_TYPE_UINT16, CHAR_TYPE_UINT32, -1 }; static characteristic_type_t types_00002a54_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_16BIT, -1 }; static characteristic_type_t types_00002a55_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT8, CHAR_TYPE_VARIABLE, CHAR_TYPE_UINT8, CHAR_TYPE_UINT8, CHAR_TYPE_UINT8, CHAR_TYPE_VARIABLE, -1 }; -static characteristic_type_t types_00002a56_0000_1000_8000_00805f9b34fb[] = { -1 }; -static characteristic_type_t types_00002a57_0000_1000_8000_00805f9b34fb[] = { -1 }; +static characteristic_type_t types_00002a56_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_2BIT, -1 }; +static characteristic_type_t types_00002a57_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT8, -1 }; static characteristic_type_t types_00002a58_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT16, -1 }; static characteristic_type_t types_00002a59_0000_1000_8000_00805f9b34fb[] = { CHAR_TYPE_UINT16, -1 }; static characteristic_type_t types_00002a5a_0000_1000_8000_00805f9b34fb[] = { -1 }; From 7ce10dbd3de216ec5cf84a0508a4e39a1ffb9258 Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 8 Mar 2023 01:49:23 +0100 Subject: [PATCH 10/13] moved duplicate UUID logic to ble.c --- main/ble.c | 12 +++++++++++- main/ble_utils.c | 10 +++------- main/ble_utils.h | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/main/ble.c b/main/ble.c index fa3d24a..88f4012 100644 --- a/main/ble.c +++ b/main/ble.c @@ -423,8 +423,18 @@ static void ble_update_cache(ble_device_t *dev) else if (db[i].type == ESP_GATT_DB_CHARACTERISTIC) { esp_uuid_to_bt_uuid(db[i].uuid, characteristic_uuid); + + uint8_t index = 0; + + for (ble_characteristic_t *cur = service->characteristics; cur; cur = cur->next){ + if (ble_uuid_equal(cur->uuid, characteristic_uuid)){ + index++; + } + }; + characteristic = ble_device_characteristic_add(service, - characteristic_uuid, db[i].attribute_handle, db[i].properties); + characteristic_uuid, index, db[i].attribute_handle, + db[i].properties); } else if (db[i].type == ESP_GATT_DB_DESCRIPTOR && db[i].uuid.len == ESP_UUID_LEN_16 && diff --git a/main/ble_utils.c b/main/ble_utils.c index 4c6e7e5..ba67af4 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -863,7 +863,7 @@ void ble_device_services_free(ble_service_t **list) } ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, - ble_uuid_t uuid, uint16_t handle, uint8_t properties) + ble_uuid_t uuid, uint8_t index, uint16_t handle, uint8_t properties) { ble_characteristic_t *characteristic, **cur; @@ -873,13 +873,9 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, characteristic->handle = handle; characteristic->properties = properties; characteristic->client_config_handle = 0; - characteristic->index = 0; + characteristic->index = index; - for (cur = &service->characteristics; *cur; cur = &(*cur)->next){ - if (ble_uuid_equal((*cur)->uuid, uuid)){ - characteristic->index++; - } - }; + for (cur = &service->characteristics; *cur; cur = &(*cur)->next); *cur = characteristic; diff --git a/main/ble_utils.h b/main/ble_utils.h index 968932b..4775c5e 100644 --- a/main/ble_utils.h +++ b/main/ble_utils.h @@ -83,7 +83,7 @@ void ble_device_service_free(ble_service_t *service); void ble_device_services_free(ble_service_t **list); ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, - ble_uuid_t uuid, uint16_t handle, uint8_t properties); + ble_uuid_t uuid, uint8_t index, uint16_t handle, uint8_t properties); ble_characteristic_t *ble_device_characteristic_find_by_uuid( ble_service_t *service, ble_uuid_t uuid, uint8_t index); ble_characteristic_t *ble_device_characteristic_find_by_handle( From 842b794a15e6e1fae008c96410a2759d3f7a7ddc Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Wed, 8 Mar 2023 01:50:53 +0100 Subject: [PATCH 11/13] formating --- main/ble_utils.c | 1 - 1 file changed, 1 deletion(-) diff --git a/main/ble_utils.c b/main/ble_utils.c index ba67af4..c5acd82 100644 --- a/main/ble_utils.c +++ b/main/ble_utils.c @@ -876,7 +876,6 @@ ble_characteristic_t *ble_device_characteristic_add(ble_service_t *service, characteristic->index = index; for (cur = &service->characteristics; *cur; cur = &(*cur)->next); - *cur = characteristic; return characteristic; From 8d75c9cfa8e3e263d3f5bc1a78332f5c1fed3fdc Mon Sep 17 00:00:00 2001 From: Daniel Dakhno Date: Fri, 10 Mar 2023 14:39:32 +0100 Subject: [PATCH 12/13] made for-loop more verbose --- main/ble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/ble.c b/main/ble.c index 88f4012..6a1755e 100644 --- a/main/ble.c +++ b/main/ble.c @@ -426,7 +426,7 @@ static void ble_update_cache(ble_device_t *dev) uint8_t index = 0; - for (ble_characteristic_t *cur = service->characteristics; cur; cur = cur->next){ + for (ble_characteristic_t *cur = service->characteristics; cur != NULL; cur = cur->next){ if (ble_uuid_equal(cur->uuid, characteristic_uuid)){ index++; } From 2963bed495ce73a51334a540ca68cdfb50dca00d Mon Sep 17 00:00:00 2001 From: Assaf Inbal Date: Fri, 31 Mar 2023 15:16:48 +0300 Subject: [PATCH 13/13] Aligned coding style --- main/ble.c | 9 +++++---- main/ble2mqtt.c | 22 ++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/main/ble.c b/main/ble.c index 6a1755e..c465981 100644 --- a/main/ble.c +++ b/main/ble.c @@ -426,11 +426,12 @@ static void ble_update_cache(ble_device_t *dev) uint8_t index = 0; - for (ble_characteristic_t *cur = service->characteristics; cur != NULL; cur = cur->next){ - if (ble_uuid_equal(cur->uuid, characteristic_uuid)){ + for (ble_characteristic_t *cur = service->characteristics; + cur != NULL; cur = cur->next) + { + if (ble_uuid_equal(cur->uuid, characteristic_uuid)) index++; - } - }; + } characteristic = ble_device_characteristic_add(service, characteristic_uuid, index, db[i].attribute_handle, diff --git a/main/ble2mqtt.c b/main/ble2mqtt.c index d7391c0..4474575 100644 --- a/main/ble2mqtt.c +++ b/main/ble2mqtt.c @@ -394,9 +394,8 @@ static char *ble_topic(mac_addr_t mac, ble_uuid_t service_uuid, i += snprintf(topic + i, MAX_TOPIC_LEN - i, "/%s", ble_characteristic_name_get(characteristic_uuid)); - if(index > 0){ + if (index > 0) i += snprintf(topic + i, MAX_TOPIC_LEN - i, "_%u", index); - } return topic; } @@ -418,7 +417,8 @@ static void ble_on_mqtt_get(const char *topic, const uint8_t *payload, ESP_LOGD(TAG, "Got read request: %s", topic); mqtt_ctx_t *data = (mqtt_ctx_t *)ctx; - ble_characteristic_read(data->mac, data->service, data->characteristic, data->index); + ble_characteristic_read(data->mac, data->service, data->characteristic, + data->index); } static void ble_on_mqtt_set(const char *topic, const uint8_t *payload, @@ -430,11 +430,12 @@ static void ble_on_mqtt_set(const char *topic, const uint8_t *payload, uint8_t *buf = atochar(data->characteristic, (const char *)payload, len, &buf_len); - ble_characteristic_write(data->mac, data->service, data->characteristic, data->index, - buf, buf_len); + ble_characteristic_write(data->mac, data->service, data->characteristic, + data->index, buf, buf_len); /* Issue a read request to get latest value */ - ble_characteristic_read(data->mac, data->service, data->characteristic, data->index); + ble_characteristic_read(data->mac, data->service, data->characteristic, + data->index); } static void _ble_on_mqtt_get(const char *topic, const uint8_t *payload, @@ -445,9 +446,10 @@ static void _ble_on_mqtt_set(const char *topic, const uint8_t *payload, static void ble_on_characteristic_found(mac_addr_t mac, ble_uuid_t service_uuid, ble_uuid_t characteristic_uuid, uint8_t index, uint8_t properties) { - ESP_LOGI(TAG, "Found new characteristic: service: " UUID_FMT + ESP_LOGD(TAG, "Found new characteristic: service: " UUID_FMT ", characteristic: " UUID_FMT ", index: %u, properties: 0x%x", - UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), index, properties); + UUID_PARAM(service_uuid), UUID_PARAM(characteristic_uuid), index, + properties); char *topic; if (!config_ble_service_should_include(uuidtoa(service_uuid)) || @@ -490,8 +492,8 @@ static void ble_on_device_services_discovered(mac_addr_t mac) } static void ble_on_device_characteristic_value(mac_addr_t mac, - ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, uint8_t *value, - size_t value_len) + ble_uuid_t service, ble_uuid_t characteristic, uint8_t index, + uint8_t *value, size_t value_len) { char *topic = ble_topic(mac, service, characteristic, index); char *payload = chartoa(characteristic, value, value_len);