Skip to content

Commit 510996b

Browse files
cpqcesantabot
authored andcommitted
Commonify UUID API
CL: make BT UUID utility functions platform-independent PUBLISHED_FROM=d5d3f95e4a688f0f3af6a210fe343d5b3b41d35c
1 parent fd1c175 commit 510996b

File tree

7 files changed

+162
-128
lines changed

7 files changed

+162
-128
lines changed

include/esp32/esp32_bt.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ bool esp32_bt_addr_from_str(const struct mg_str addr_str, esp_bd_addr_t addr);
3737
int esp32_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b);
3838
bool esp32_bt_addr_is_null(const esp_bd_addr_t addr);
3939

40-
const char *mgos_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out);
41-
bool mgos_bt_uuid_from_str(const struct mg_str uuid_str, esp_bt_uuid_t *uuid);
42-
int mgos_bt_uuid_cmp(const esp_bt_uuid_t *a, const esp_bt_uuid_t *b);
40+
const char *esp32_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out);
41+
bool esp32_bt_uuid_from_str(const struct mg_str uuid_str, esp_bt_uuid_t *uuid);
42+
int esp32_bt_uuid_cmp(const esp_bt_uuid_t *a, const esp_bt_uuid_t *b);
4343

4444
struct esp32_bt_connection {
4545
esp_gatt_if_t gatt_if;

include/esp32/esp32_bt_gattc.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,28 @@ void mgos_bt_gattc_list_services(int conn_id,
3333
void *cb_arg);
3434

3535
struct mgos_bt_gattc_list_chars_result {
36-
esp_bt_uuid_t char_id;
36+
struct mgos_bt_uuid char_id;
3737
esp_gatt_char_prop_t char_prop;
3838
};
3939

4040
typedef void (*mgos_bt_gattc_list_chars_cb_t)(
41-
int conn_id, const esp_bt_uuid_t *svc_id, int num_res,
41+
int conn_id, const struct mgos_bt_uuid *svc_id, int num_res,
4242
const struct mgos_bt_gattc_list_chars_result *res, void *arg);
43-
void mgos_bt_gattc_list_chars(int conn_id, const esp_bt_uuid_t *svc_id,
43+
void mgos_bt_gattc_list_chars(int conn_id, const struct mgos_bt_uuid *svc_id,
4444
mgos_bt_gattc_list_chars_cb_t cb, void *cb_arg);
4545

4646
typedef void (*mgos_bt_gattc_read_char_cb_t)(int conn_id, bool success,
4747
const struct mg_str value,
4848
void *arg);
49-
void mgos_bt_gattc_read_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
50-
const esp_bt_uuid_t *char_uuid,
49+
void mgos_bt_gattc_read_char(int conn_id, const struct mgos_bt_uuid *svc_uuid,
50+
const struct mgos_bt_uuid *char_uuid,
5151
esp_gatt_auth_req_t auth_req,
5252
mgos_bt_gattc_read_char_cb_t cb, void *cb_arg);
5353

5454
typedef void (*mgos_bt_gattc_write_char_cb_t)(int conn_id, bool success,
5555
void *arg);
56-
void mgos_bt_gattc_write_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
57-
const esp_bt_uuid_t *char_uuid,
56+
void mgos_bt_gattc_write_char(int conn_id, const struct mgos_bt_uuid *svc_uuid,
57+
const struct mgos_bt_uuid *char_uuid,
5858
bool response_required,
5959
esp_gatt_auth_req_t auth_req,
6060
const struct mg_str value,
@@ -63,8 +63,8 @@ void mgos_bt_gattc_write_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
6363
typedef void (*mgos_bt_gattc_subscribe_cb_t)(int conn_id, bool success,
6464
const struct mg_str value,
6565
void *arg);
66-
void mgos_bt_gattc_subscribe(int conn_id, const esp_bt_uuid_t *svc_uuid,
67-
const esp_bt_uuid_t *char_uuid,
66+
void mgos_bt_gattc_subscribe(int conn_id, const struct mgos_bt_uuid *svc_uuid,
67+
const struct mgos_bt_uuid *char_uuid,
6868
mgos_bt_gattc_subscribe_cb_t cb, void *cb_arg);
6969

7070
void mgos_bt_gattc_close(int conn_id);

include/mgos_bt.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,26 @@
1515
extern "C" {
1616
#endif
1717

18-
#define MGOS_BT_ADDR_LEN 6
1918
struct mgos_bt_addr {
20-
uint8_t addr[MGOS_BT_ADDR_LEN];
19+
uint8_t addr[6];
2120
};
2221

23-
#define BT_ADDR_STR_LEN (MGOS_BT_ADDR_LEN * 2 + MGOS_BT_ADDR_LEN)
22+
/* Binary-equivalent to the ESP32 esp_bt_uuid_t */
23+
struct mgos_bt_uuid {
24+
uint16_t len;
25+
union {
26+
uint16_t uuid16;
27+
uint32_t uuid32;
28+
uint8_t uuid128[16];
29+
} uuid;
30+
} __attribute__((packed));
31+
32+
/* Each byte is transformed into 3 bytes: "XX:", and last byte into "XX\0" */
33+
#define MGOS_BT_ADDR_STR_LEN (sizeof(struct mgos_bt_addr) * 3)
34+
#define MGOS_BT_UUID_STR_LEN (sizeof(struct mgos_bt_uuid) * 3)
35+
#define MGOS_BT_DEV_NAME_LEN 32
36+
37+
#define BT_ADDR_STR_LEN MGOS_BT_ADDR_STR_LEN
2438

2539
const char *mgos_bt_addr_to_str(const struct mgos_bt_addr *addr, char *out);
2640
bool mgos_bt_addr_from_str(const struct mg_str addr_str,
@@ -29,6 +43,11 @@ int mgos_bt_addr_cmp(const struct mgos_bt_addr *a,
2943
const struct mgos_bt_addr *b);
3044
bool mgos_bt_addr_is_null(const struct mgos_bt_addr *addr);
3145

46+
const char *mgos_bt_uuid_to_str(const struct mgos_bt_uuid *uuid, char *out);
47+
bool mgos_bt_uuid_from_str(const struct mg_str str, struct mgos_bt_uuid *uuid);
48+
int mgos_bt_uuid_cmp(const struct mgos_bt_uuid *a,
49+
const struct mgos_bt_uuid *b);
50+
3251
#ifdef __cplusplus
3352
}
3453
#endif

src/esp32/esp32_bt.c

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -49,87 +49,16 @@ const char *bt_uuid128_to_str(const uint8_t *u, char *out) {
4949
return out;
5050
}
5151

52-
const char *mgos_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out) {
53-
switch (uuid->len) {
54-
case ESP_UUID_LEN_16: {
55-
sprintf(out, "%04x", uuid->uuid.uuid16);
56-
break;
57-
}
58-
case ESP_UUID_LEN_32: {
59-
sprintf(out, "%08x", uuid->uuid.uuid32);
60-
break;
61-
}
62-
case ESP_UUID_LEN_128: {
63-
bt_uuid128_to_str(uuid->uuid.uuid128, out);
64-
break;
65-
}
66-
default: { sprintf(out, "?(%u)", uuid->len); }
67-
}
68-
return out;
52+
const char *esp32_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out) {
53+
return mgos_bt_uuid_to_str((struct mgos_bt_uuid *) uuid, out);
6954
}
7055

71-
bool mgos_bt_uuid_from_str(const struct mg_str uuid_str, esp_bt_uuid_t *uuid) {
72-
bool result = false;
73-
struct mg_str uuid_str_nul = mg_strdup_nul(uuid_str);
74-
if (uuid_str.len == 36) {
75-
unsigned int u[16];
76-
if (sscanf(uuid_str_nul.p,
77-
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
78-
"%02x%02x%02x%02x%02x%02x",
79-
&u[15], &u[14], &u[13], &u[12], &u[11], &u[10], &u[9], &u[8],
80-
&u[7], &u[6], &u[5], &u[4], &u[3], &u[2], &u[1], &u[0]) == 16) {
81-
result = true;
82-
uuid->len = ESP_UUID_LEN_128;
83-
for (int i = 0; i < 16; i++) {
84-
uuid->uuid.uuid128[i] = u[i];
85-
}
86-
}
87-
} else if (uuid_str.len <= 8) {
88-
unsigned int u;
89-
if (sscanf(uuid_str_nul.p, "%08x", &u) == 1) {
90-
result = true;
91-
if (u & 0xffff0000) {
92-
uuid->len = ESP_UUID_LEN_32;
93-
uuid->uuid.uuid32 = u;
94-
} else {
95-
uuid->len = ESP_UUID_LEN_16;
96-
uuid->uuid.uuid16 = u;
97-
}
98-
}
99-
}
100-
free((void *) uuid_str_nul.p);
101-
return result;
56+
bool esp32_bt_uuid_from_str(const struct mg_str uuid_str, esp_bt_uuid_t *uuid) {
57+
return mgos_bt_uuid_from_str(uuid_str, (struct mgos_bt_uuid *) uuid);
10258
}
10359

104-
int mgos_bt_uuid_cmp(const esp_bt_uuid_t *a, const esp_bt_uuid_t *b) {
105-
int result = 0;
106-
if (a->len == ESP_UUID_LEN_128 || b->len == ESP_UUID_LEN_128) {
107-
/* 128-bit UUID is always > 16 or 32-bit */
108-
if (a->len != ESP_UUID_LEN_128 && b->len == ESP_UUID_LEN_128) {
109-
result = -1;
110-
} else if (a->len == ESP_UUID_LEN_128 && b->len != ESP_UUID_LEN_128) {
111-
result = 1;
112-
} else {
113-
for (int i = 15; i >= 0; i--) {
114-
uint8_t va = a->uuid.uuid128[i];
115-
uint8_t vb = b->uuid.uuid128[i];
116-
if (va != vb) {
117-
result = (va > vb ? 1 : -1);
118-
break;
119-
}
120-
}
121-
}
122-
} else {
123-
uint32_t va, vb;
124-
va = (a->len == ESP_UUID_LEN_16 ? a->uuid.uuid16 : a->uuid.uuid32);
125-
vb = (b->len == ESP_UUID_LEN_16 ? b->uuid.uuid16 : b->uuid.uuid32);
126-
if (va < vb) {
127-
result = -1;
128-
} else if (va > vb) {
129-
result = 1;
130-
}
131-
}
132-
return result;
60+
int esp32_bt_uuid_cmp(const esp_bt_uuid_t *a, const esp_bt_uuid_t *b) {
61+
return mgos_bt_uuid_cmp((struct mgos_bt_uuid *) a, (struct mgos_bt_uuid *) b);
13362
}
13463

13564
enum cs_log_level ll_from_status(esp_bt_status_t status) {

src/esp32/esp32_bt_gattc.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ static void esp32_bt_gattc_ev(esp_gattc_cb_event_t ev, esp_gatt_if_t gattc_if,
263263
const struct gattc_search_res_evt_param *p = &ep->search_res;
264264
LOG(LL_DEBUG,
265265
("SEARCH_RES cid %u svc %s %d", p->conn_id,
266-
mgos_bt_uuid_to_str(&p->srvc_id.uuid, buf), p->srvc_id.inst_id));
266+
esp32_bt_uuid_to_str(&p->srvc_id.uuid, buf), p->srvc_id.inst_id));
267267
struct esp32_gattc_connection_entry *ce =
268268
find_connection_by_esp_conn_id(p->conn_id);
269269
if (ce == NULL) break;
@@ -496,8 +496,8 @@ static void esp32_bt_gattc_ev(esp_gattc_cb_event_t ev, esp_gatt_if_t gattc_if,
496496
if (count == 1) {
497497
LOG(LL_DEBUG,
498498
("%s %s -> CCCD handle %u",
499-
mgos_bt_uuid_to_str(&sc->svc_uuid, buf),
500-
mgos_bt_uuid_to_str(&sc->char_uuid, buf2), cccd.handle));
499+
esp32_bt_uuid_to_str(&sc->svc_uuid, buf),
500+
esp32_bt_uuid_to_str(&sc->char_uuid, buf2), cccd.handle));
501501
uint8_t notify_en[2] = {0x01, 0x00};
502502
sc->cccd_handle = cccd.handle;
503503
status = esp_ble_gattc_write_char_descr(
@@ -510,8 +510,8 @@ static void esp32_bt_gattc_ev(esp_gattc_cb_event_t ev, esp_gatt_if_t gattc_if,
510510
}
511511
} else {
512512
LOG(LL_ERROR,
513-
("No CCCD for %s %s", mgos_bt_uuid_to_str(&sc->svc_uuid, buf),
514-
mgos_bt_uuid_to_str(&sc->char_uuid, buf2)));
513+
("No CCCD for %s %s", esp32_bt_uuid_to_str(&sc->svc_uuid, buf),
514+
esp32_bt_uuid_to_str(&sc->char_uuid, buf2)));
515515
sc->success = false;
516516
}
517517
}
@@ -748,13 +748,13 @@ struct esp32_gattc_list_chars_ctx {
748748
static void lc_done_mgos_cb(void *arg) {
749749
struct esp32_gattc_list_chars_ctx *lc_ctx =
750750
(struct esp32_gattc_list_chars_ctx *) arg;
751-
lc_ctx->cb(lc_ctx->conn_id, &lc_ctx->svc_id, lc_ctx->num_res, lc_ctx->res,
752-
lc_ctx->cb_arg);
751+
lc_ctx->cb(lc_ctx->conn_id, (struct mgos_bt_uuid *) &lc_ctx->svc_id,
752+
lc_ctx->num_res, lc_ctx->res, lc_ctx->cb_arg);
753753
free(lc_ctx->res);
754754
free(lc_ctx);
755755
}
756756

757-
void mgos_bt_gattc_list_chars(int conn_id, const esp_bt_uuid_t *svc_id,
757+
void mgos_bt_gattc_list_chars(int conn_id, const struct mgos_bt_uuid *svc_id,
758758
mgos_bt_gattc_list_chars_cb_t cb, void *cb_arg) {
759759
esp_gattc_char_elem_t *char_res = NULL;
760760
struct esp32_gattc_list_chars_ctx *lc_ctx = calloc(1, sizeof(*lc_ctx));
@@ -842,8 +842,8 @@ static esp_gatt_status_t esp32_gattc_get_char_handle(
842842
clean : {
843843
enum cs_log_level ll = (res == ESP_GATT_OK ? LL_DEBUG : LL_ERROR);
844844
char buf1[BT_UUID_STR_LEN], buf2[BT_UUID_STR_LEN];
845-
LOG(ll, ("%s %s -> %u", mgos_bt_uuid_to_str(svc_id, buf1),
846-
mgos_bt_uuid_to_str(char_id, buf2), *handle));
845+
LOG(ll, ("%s %s -> %u", esp32_bt_uuid_to_str(svc_id, buf1),
846+
esp32_bt_uuid_to_str(char_id, buf2), *handle));
847847
}
848848
return res;
849849
}
@@ -861,8 +861,8 @@ static void read_done_mgos_cb(void *arg) {
861861
free(rc);
862862
}
863863

864-
void mgos_bt_gattc_read_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
865-
const esp_bt_uuid_t *char_uuid,
864+
void mgos_bt_gattc_read_char(int conn_id, const struct mgos_bt_uuid *svc_uuid,
865+
const struct mgos_bt_uuid *char_uuid,
866866
esp_gatt_auth_req_t auth_req,
867867
mgos_bt_gattc_read_char_cb_t cb, void *cb_arg) {
868868
struct esp32_gattc_read_char_ctx *rc = calloc(1, sizeof(*rc));
@@ -877,7 +877,8 @@ void mgos_bt_gattc_read_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
877877
rc->cb_arg = cb_arg;
878878
struct esp32_gattc_connection_entry *ce = NULL;
879879
esp_gatt_status_t st = esp32_gattc_get_char_handle(
880-
conn_id, svc_uuid, char_uuid, &ce, &rc->handle);
880+
conn_id, (esp_bt_uuid_t *) svc_uuid, (esp_bt_uuid_t *) char_uuid, &ce,
881+
&rc->handle);
881882
if (st != ESP_GATT_OK) {
882883
rc->value_len = -1;
883884
goto clean;
@@ -903,8 +904,8 @@ static void write_done_mgos_cb(void *arg) {
903904
free(wc);
904905
}
905906

906-
void mgos_bt_gattc_write_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
907-
const esp_bt_uuid_t *char_uuid,
907+
void mgos_bt_gattc_write_char(int conn_id, const struct mgos_bt_uuid *svc_uuid,
908+
const struct mgos_bt_uuid *char_uuid,
908909
bool response_required,
909910
esp_gatt_auth_req_t auth_req,
910911
const struct mg_str value,
@@ -921,7 +922,8 @@ void mgos_bt_gattc_write_char(int conn_id, const esp_bt_uuid_t *svc_uuid,
921922
wc->cb_arg = cb_arg;
922923
struct esp32_gattc_connection_entry *ce;
923924
esp_gatt_status_t st = esp32_gattc_get_char_handle(
924-
conn_id, svc_uuid, char_uuid, &ce, &wc->handle);
925+
conn_id, (esp_bt_uuid_t *) svc_uuid, (esp_bt_uuid_t *) char_uuid, &ce,
926+
&wc->handle);
925927
if (st != ESP_GATT_OK) {
926928
wc->success = false;
927929
goto clean;
@@ -967,8 +969,8 @@ static void subscribe_mgos_cb(void *arg) {
967969
if (!sc->success) free(sc);
968970
}
969971

970-
void mgos_bt_gattc_subscribe(int conn_id, const esp_bt_uuid_t *svc_uuid,
971-
const esp_bt_uuid_t *char_uuid,
972+
void mgos_bt_gattc_subscribe(int conn_id, const struct mgos_bt_uuid *svc_uuid,
973+
const struct mgos_bt_uuid *char_uuid,
972974
mgos_bt_gattc_subscribe_cb_t cb, void *cb_arg) {
973975
struct esp32_gattc_subscribe_ctx *sc = calloc(1, sizeof(*sc));
974976
if (sc == NULL) {
@@ -982,7 +984,8 @@ void mgos_bt_gattc_subscribe(int conn_id, const esp_bt_uuid_t *svc_uuid,
982984
sc->cb_arg = cb_arg;
983985
struct esp32_gattc_connection_entry *ce;
984986
esp_gatt_status_t st = esp32_gattc_get_char_handle(
985-
conn_id, svc_uuid, char_uuid, &ce, &sc->handle);
987+
conn_id, (esp_bt_uuid_t *) svc_uuid, (esp_bt_uuid_t *) char_uuid, &ce,
988+
&sc->handle);
986989
if (st != ESP_GATT_OK) {
987990
sc->success = false;
988991
goto clean;

src/esp32/esp32_bt_gatts.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static void esp32_bt_gatts_ev(esp_gatts_cb_event_t ev, esp_gatt_if_t gatts_if,
498498
enum cs_log_level ll = ll_from_status(p->status);
499499
LOG(ll,
500500
("CREATE st %d svch %d svcid %s %d%s", p->status, p->service_handle,
501-
mgos_bt_uuid_to_str(&p->service_id.id.uuid, buf),
501+
esp32_bt_uuid_to_str(&p->service_id.id.uuid, buf),
502502
p->service_id.id.inst_id,
503503
(p->service_id.is_primary ? " primary" : "")));
504504
break;
@@ -515,15 +515,15 @@ static void esp32_bt_gatts_ev(esp_gatts_cb_event_t ev, esp_gatt_if_t gatts_if,
515515
enum cs_log_level ll = ll_from_status(p->status);
516516
LOG(ll,
517517
("ADD_CHAR st %d ah %u svch %u uuid %s", p->status, p->attr_handle,
518-
p->service_handle, mgos_bt_uuid_to_str(&p->char_uuid, buf)));
518+
p->service_handle, esp32_bt_uuid_to_str(&p->char_uuid, buf)));
519519
break;
520520
}
521521
case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
522522
const struct gatts_add_char_descr_evt_param *p = &ep->add_char_descr;
523523
enum cs_log_level ll = ll_from_status(p->status);
524524
LOG(ll, ("ADD_CHAR_DESCR st %d ah %u svch %u uuid %s", p->status,
525525
p->attr_handle, p->service_handle,
526-
mgos_bt_uuid_to_str(&p->char_uuid, buf)));
526+
esp32_bt_uuid_to_str(&p->char_uuid, buf)));
527527
break;
528528
}
529529
case ESP_GATTS_DELETE_EVT: {
@@ -671,7 +671,7 @@ static void esp32_bt_gatts_ev(esp_gatts_cb_event_t ev, esp_gatt_if_t gatts_if,
671671
enum cs_log_level ll = ll_from_status(p->status);
672672
LOG(ll,
673673
("CREAT_ATTR_TAB st %d svc_uuid %s nh %d hh %p", p->status,
674-
mgos_bt_uuid_to_str(&p->svc_uuid, buf), p->num_handle, p->handles));
674+
esp32_bt_uuid_to_str(&p->svc_uuid, buf), p->num_handle, p->handles));
675675
if (p->status != 0) {
676676
LOG(LL_ERROR,
677677
("Failed to register service attribute table: %d", p->status));
@@ -686,7 +686,7 @@ static void esp32_bt_gatts_ev(esp_gatts_cb_event_t ev, esp_gatt_if_t gatts_if,
686686
run_on_mgos_task(gatts_if, NULL, se, ev, ep);
687687
uint16_t svch = se->attr_handles[0];
688688
LOG(LL_INFO,
689-
("Starting BT service %s", mgos_bt_uuid_to_str(&p->svc_uuid, buf)));
689+
("Starting BT service %s", esp32_bt_uuid_to_str(&p->svc_uuid, buf)));
690690
esp_ble_gatts_start_service(svch);
691691
break;
692692
}

0 commit comments

Comments
 (0)