Skip to content

Commit fd1c175

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
Replace esp_bd_addr_t with generic mgos_bt_addr
CL: Replace esp_bd_addr_t with generic mgos_bt_addr PUBLISHED_FROM=dde0d234aba51afc77eedb71bd417c6d86ad04ae
1 parent ca3074c commit fd1c175

File tree

9 files changed

+151
-83
lines changed

9 files changed

+151
-83
lines changed

include/esp32/esp32_bt.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313

1414
#include "common/mg_str.h"
1515

16-
#define BT_ADDR_STR_LEN (ESP_BD_ADDR_LEN * 2 + ESP_BD_ADDR_LEN)
16+
#include "mgos_bt.h"
17+
18+
#define MGOS_BT_DEV_NAME_LEN 32
1719
#define BT_UUID_STR_LEN (ESP_UUID_LEN_128 * 2 + ESP_UUID_LEN_128)
1820

1921
#ifdef __cplusplus
2022
extern "C" {
2123
#endif
2224

25+
#define MGOS_BT_ADDR_LEN 6
2326
#define MGOS_BT_DEV_NAME_LEN 32
2427

2528
const uint16_t primary_service_uuid;
@@ -29,18 +32,18 @@ const uint8_t char_prop_read_write;
2932
const uint8_t char_prop_read_notify;
3033
const uint8_t char_prop_write;
3134

32-
const char *mgos_bt_addr_to_str(const esp_bd_addr_t addr, char *out);
33-
bool mgos_bt_addr_from_str(const struct mg_str addr_str, esp_bd_addr_t addr);
34-
int mgos_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b);
35-
bool mgos_bt_addr_is_null(const esp_bd_addr_t a);
35+
const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out);
36+
bool esp32_bt_addr_from_str(const struct mg_str addr_str, esp_bd_addr_t addr);
37+
int esp32_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b);
38+
bool esp32_bt_addr_is_null(const esp_bd_addr_t addr);
3639

3740
const char *mgos_bt_uuid_to_str(const esp_bt_uuid_t *uuid, char *out);
3841
bool mgos_bt_uuid_from_str(const struct mg_str uuid_str, esp_bt_uuid_t *uuid);
3942
int mgos_bt_uuid_cmp(const esp_bt_uuid_t *a, const esp_bt_uuid_t *b);
4043

4144
struct esp32_bt_connection {
4245
esp_gatt_if_t gatt_if;
43-
esp_bd_addr_t peer_addr;
46+
struct mgos_bt_addr peer_addr;
4447
uint16_t conn_id;
4548
uint16_t mtu;
4649
};
@@ -58,7 +61,7 @@ struct mgos_bt_ble_scan_opts {
5861
struct mg_str name;
5962
};
6063
struct mgos_bt_ble_scan_result {
61-
esp_bd_addr_t addr;
64+
struct mgos_bt_addr addr;
6265
struct mg_str adv_data; /* Raw adv data */
6366
struct mg_str scan_rsp; /* Raw scan response (for active scans) */
6467
char name[MGOS_BT_DEV_NAME_LEN + 1]; /* NUL-terminated */

include/esp32/esp32_bt_gattc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ extern "C" {
1818
#endif
1919

2020
typedef void (*mgos_bt_gattc_open_cb)(int conn_id, bool result, void *arg);
21-
void mgos_bt_gattc_open_addr(const esp_bd_addr_t addr, mgos_bt_gattc_open_cb cb,
22-
void *cb_arg);
21+
void mgos_bt_gattc_open_addr(const struct mgos_bt_addr *addr,
22+
mgos_bt_gattc_open_cb cb, void *cb_arg);
2323
void mgos_bt_gattc_open_name(const struct mg_str name, mgos_bt_gattc_open_cb cb,
2424
void *cb_arg);
2525

include/mgos_bt.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright (c) 2014-2018 Cesanta Software Limited
3+
* All rights reserved
4+
*/
5+
6+
#ifndef CS_MOS_LIBS_BT_COMMON_INCLUDE_MGOS_BT_H_
7+
#define CS_MOS_LIBS_BT_COMMON_INCLUDE_MGOS_BT_H_
8+
9+
#include <stdbool.h>
10+
#include <stdint.h>
11+
12+
#include "common/mg_str.h"
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#define MGOS_BT_ADDR_LEN 6
19+
struct mgos_bt_addr {
20+
uint8_t addr[MGOS_BT_ADDR_LEN];
21+
};
22+
23+
#define BT_ADDR_STR_LEN (MGOS_BT_ADDR_LEN * 2 + MGOS_BT_ADDR_LEN)
24+
25+
const char *mgos_bt_addr_to_str(const struct mgos_bt_addr *addr, char *out);
26+
bool mgos_bt_addr_from_str(const struct mg_str addr_str,
27+
struct mgos_bt_addr *addr);
28+
int mgos_bt_addr_cmp(const struct mgos_bt_addr *a,
29+
const struct mgos_bt_addr *b);
30+
bool mgos_bt_addr_is_null(const struct mgos_bt_addr *addr);
31+
32+
#ifdef __cplusplus
33+
}
34+
#endif
35+
36+
#endif /* CS_MOS_LIBS_BT_COMMON_INCLUDE_MGOS_BT_H_ */

mos.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ version: 1.0
66
platforms: [ esp32 ]
77

88
sources:
9+
- src
910
- src/${arch}
11+
1012
includes:
13+
- include
1114
- include/${arch}
1215

1316
config_schema:

src/esp32/esp32_bt.c

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "esp32_bt_internal.h"
88

99
#include <stdbool.h>
10-
#include <stdio.h>
1110
#include <stdlib.h>
1211

1312
#include "bta_api.h"
@@ -24,33 +23,21 @@
2423
#include "mgos_net.h"
2524
#include "mgos_sys_config.h"
2625

27-
const char *mgos_bt_addr_to_str(const esp_bd_addr_t addr, char *out) {
28-
sprintf(out, "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1], addr[2],
29-
addr[3], addr[4], addr[5]);
30-
return out;
26+
const char *esp32_bt_addr_to_str(const esp_bd_addr_t addr, char *out) {
27+
return mgos_bt_addr_to_str((const struct mgos_bt_addr *) &addr[0], out);
3128
}
3229

33-
bool mgos_bt_addr_from_str(const struct mg_str addr_str, esp_bd_addr_t addr) {
34-
unsigned int a[6];
35-
struct mg_str addr_str_nul = mg_strdup_nul(addr_str);
36-
bool result = (sscanf(addr_str_nul.p, "%02x:%02x:%02x:%02x:%02x:%02x", &a[0],
37-
&a[1], &a[2], &a[3], &a[4], &a[5]) == 6);
38-
if (result) {
39-
for (int i = 0; i < 6; i++) {
40-
addr[i] = a[i];
41-
}
42-
}
43-
free((void *) addr_str_nul.p);
44-
return result;
30+
bool esp32_bt_addr_from_str(const struct mg_str addr_str, esp_bd_addr_t addr) {
31+
return mgos_bt_addr_from_str(addr_str, (struct mgos_bt_addr *) &addr[0]);
4532
}
4633

47-
int mgos_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b) {
48-
return memcmp(a, b, ESP_BD_ADDR_LEN);
34+
int esp32_bt_addr_cmp(const esp_bd_addr_t a, const esp_bd_addr_t b) {
35+
return mgos_bt_addr_cmp((const struct mgos_bt_addr *) &a[0],
36+
(const struct mgos_bt_addr *) &b[0]);
4937
}
5038

51-
bool mgos_bt_addr_is_null(const esp_bd_addr_t a) {
52-
const esp_bd_addr_t null_addr = {0};
53-
return (mgos_bt_addr_cmp(a, null_addr) == 0);
39+
bool esp32_bt_addr_is_null(const esp_bd_addr_t addr) {
40+
return mgos_bt_addr_is_null((const struct mgos_bt_addr *) &addr[0]);
5441
}
5542

5643
const char *bt_uuid128_to_str(const uint8_t *u, char *out) {

src/esp32/esp32_bt_gap.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "esp32_bt_gatts.h"
2525

2626
struct scan_cb_info {
27-
esp_bd_addr_t target_addr;
27+
struct mgos_bt_addr target_addr;
2828
struct mg_str target_name;
2929
mgos_bt_ble_scan_cb_t cb;
3030
void *cb_arg;
@@ -150,14 +150,14 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
150150
p->ble_adv, ESP_BLE_AD_TYPE_NAME_CMPL, &name_len);
151151
LOG(LL_DEBUG,
152152
("SCAN_RESULT addr %s name %.*s type %d RSSI %d adl %d srl %d",
153-
mgos_bt_addr_to_str(p->bda, buf), (int) name_len,
153+
esp32_bt_addr_to_str(p->bda, buf), (int) name_len,
154154
(name ? (const char *) name : ""), p->dev_type, p->rssi,
155155
p->adv_data_len, p->scan_rsp_len));
156156
struct mgos_bt_ble_scan_result *r = NULL;
157157
struct scan_ctx *sctx = s_scan_ctx;
158158
if (sctx == NULL) break;
159159
for (int i = 0; i < sctx->num_res; i++) {
160-
if (mgos_bt_addr_cmp(sctx->res[i].addr, p->bda) == 0) {
160+
if (esp32_bt_addr_cmp(sctx->res[i].addr.addr, p->bda) == 0) {
161161
r = &sctx->res[i];
162162
free((void *) r->adv_data.p);
163163
break;
@@ -181,7 +181,7 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
181181
/* See if there are any scans waiting for this specific device */
182182
struct scan_cb_info *cbi, *cbit;
183183
SLIST_FOREACH_SAFE(cbi, &sctx->cbs, next, cbit) {
184-
if (mgos_bt_addr_cmp(r->addr, cbi->target_addr) == 0 ||
184+
if (mgos_bt_addr_cmp(&r->addr, &cbi->target_addr) == 0 ||
185185
(name_len > 0 &&
186186
mg_strcmp(mg_mk_str_n(r->name, name_len), cbi->target_name) ==
187187
0)) {
@@ -248,33 +248,33 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
248248
const esp_ble_auth_cmpl_t *p = &ep->ble_security.auth_cmpl;
249249
enum cs_log_level ll = (p->success ? LL_INFO : LL_ERROR);
250250
LOG(ll, ("AUTH_CMPL peer %s at %d dt %d success %d (fr %d) kp %d kt %d",
251-
mgos_bt_addr_to_str(p->bd_addr, buf), p->addr_type, p->dev_type,
251+
esp32_bt_addr_to_str(p->bd_addr, buf), p->addr_type, p->dev_type,
252252
p->success, p->fail_reason, p->key_present, p->key_type));
253253
if (p->success) esp32_bt_gatts_auth_cmpl(p->bd_addr);
254254
break;
255255
}
256256
case ESP_GAP_BLE_KEY_EVT: {
257257
const esp_ble_key_t *p = &ep->ble_security.ble_key;
258-
LOG(LL_DEBUG, ("KEY peer %s kt %d", mgos_bt_addr_to_str(p->bd_addr, buf),
258+
LOG(LL_DEBUG, ("KEY peer %s kt %d", esp32_bt_addr_to_str(p->bd_addr, buf),
259259
p->key_type));
260260
break;
261261
}
262262
case ESP_GAP_BLE_SEC_REQ_EVT: {
263263
esp_ble_sec_req_t *p = &ep->ble_security.ble_req;
264-
LOG(LL_DEBUG, ("SEC_REQ peer %s", mgos_bt_addr_to_str(p->bd_addr, buf)));
264+
LOG(LL_DEBUG, ("SEC_REQ peer %s", esp32_bt_addr_to_str(p->bd_addr, buf)));
265265
esp_ble_gap_security_rsp(p->bd_addr, true /* accept */);
266266
break;
267267
}
268268
case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: {
269269
esp_ble_sec_key_notif_t *p = &ep->ble_security.key_notif;
270270
LOG(LL_DEBUG, ("PASSKEY_NOTIF peer %s pk %u",
271-
mgos_bt_addr_to_str(p->bd_addr, buf), p->passkey));
271+
esp32_bt_addr_to_str(p->bd_addr, buf), p->passkey));
272272
/*
273273
* TODO(rojer): Provide a callback interface for user to display the code.
274274
* For now, hope people read the logs. Yeah.
275275
*/
276276
LOG(LL_ERROR, ("The passkey to pair with %s is %u",
277-
mgos_bt_addr_to_str(p->bd_addr, buf), p->passkey));
277+
esp32_bt_addr_to_str(p->bd_addr, buf), p->passkey));
278278
break;
279279
}
280280
case ESP_GAP_BLE_PASSKEY_REQ_EVT: {
@@ -329,7 +329,7 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
329329
&ep->update_conn_params;
330330
LOG(LL_DEBUG, ("UPDATE_CONN_PARAMS st %d addr %s int %u-%u lat %u "
331331
"conn_int %u tout %u",
332-
p->status, mgos_bt_addr_to_str(p->bda, buf), p->min_int,
332+
p->status, esp32_bt_addr_to_str(p->bda, buf), p->min_int,
333333
p->max_int, p->latency, p->conn_int, p->timeout));
334334
break;
335335
}
@@ -352,7 +352,7 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
352352
&ep->remove_bond_dev_cmpl;
353353
enum cs_log_level ll = ll_from_status(p->status);
354354
LOG(ll, ("REMOVE_BOND_DEV_COMPLETE st %d bda %s", p->status,
355-
mgos_bt_addr_to_str(p->bd_addr, buf)));
355+
esp32_bt_addr_to_str(p->bd_addr, buf)));
356356
break;
357357
}
358358
case ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT: {
@@ -370,15 +370,15 @@ static void esp32_bt_gap_ev(esp_gap_ble_cb_event_t ev,
370370
} else {
371371
LOG(ll,
372372
("GET_BOND_DEV_COMPLETE st %d dev_num %d peer_addr %s", p->status,
373-
p->dev_num, mgos_bt_addr_to_str(p->bond_dev->bd_addr, buf)));
373+
p->dev_num, esp32_bt_addr_to_str(p->bond_dev->bd_addr, buf)));
374374
}
375375
break;
376376
}
377377
case ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT: {
378378
const struct ble_read_rssi_cmpl_evt_param *p = &ep->read_rssi_cmpl;
379379
enum cs_log_level ll = ll_from_status(p->status);
380380
LOG(ll, ("READ_RSSI_COMPLETE st %d rssi %d ra %s", p->status, p->rssi,
381-
mgos_bt_addr_to_str(p->remote_addr, buf)));
381+
esp32_bt_addr_to_str(p->remote_addr, buf)));
382382
break;
383383
}
384384
case ESP_GAP_BLE_ADD_WHITELIST_COMPLETE_EVT: {
@@ -402,8 +402,8 @@ static void scan_done_mgos_cb(void *arg) {
402402
if (cbi->cb == NULL) continue;
403403
int num_res = sctx->num_res;
404404
if (num_res >= 1 &&
405-
((!mgos_bt_addr_is_null(cbi->target_addr) &&
406-
mgos_bt_addr_cmp(sctx->res->addr, cbi->target_addr) != 0) ||
405+
((!mgos_bt_addr_is_null(&cbi->target_addr) &&
406+
mgos_bt_addr_cmp(&sctx->res->addr, &cbi->target_addr) != 0) ||
407407
(cbi->target_name.len > 0 &&
408408
mg_strcmp(mg_mk_str(sctx->res->name), cbi->target_name) != 0))) {
409409
num_res = 0;
@@ -441,7 +441,7 @@ void mgos_bt_ble_scan(const struct mgos_bt_ble_scan_opts *opts,
441441
if (cbi == NULL) return;
442442
cbi->cb = cb;
443443
cbi->cb_arg = cb_arg;
444-
memcpy(cbi->target_addr, opts->addr, sizeof(cbi->target_addr));
444+
memcpy(&cbi->target_addr, &opts->addr, sizeof(cbi->target_addr));
445445
cbi->target_name = mg_strdup(opts->name);
446446
int scan_interval_ms =
447447
(opts->interval_ms > 0 ? opts->interval_ms

0 commit comments

Comments
 (0)