Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 6b4f392

Browse files
Xykonpeter-pycom
authored andcommitted
BLE update
1 parent 8e631ef commit 6b4f392

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

esp32/mods/modbt.c

+40-20
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ typedef struct {
113113
int32_t conn_id;
114114
uint16_t mtu;
115115
esp_gatt_if_t gatt_if;
116+
esp_ble_addr_type_t addr_type;
116117
} bt_connection_obj_t;
117118

118119
typedef struct {
@@ -284,7 +285,7 @@ STATIC void gattc_char_callback_handler(void *arg);
284285
STATIC void gatts_char_callback_handler(void *arg);
285286
static mp_obj_t modbt_start_scan(mp_obj_t timeout);
286287
static mp_obj_t modbt_conn_disconnect(mp_obj_t self_in);
287-
static mp_obj_t modbt_connect(mp_obj_t addr);
288+
static mp_obj_t modbt_connect(mp_obj_t addr, esp_ble_addr_type_t addr_type);
288289

289290
/******************************************************************************
290291
DEFINE PUBLIC FUNCTIONS
@@ -421,7 +422,7 @@ void bt_resume(bool reconnect)
421422
for (mp_uint_t i = 0; i < btc_conn_list_tmp.len; i++) {
422423
bt_connection_obj_t *connection_obj = ((bt_connection_obj_t *)(btc_conn_list_tmp.items[i]));
423424
// Initiates re-connection
424-
bt_connection_obj_t *new_connection_obj = modbt_connect(mp_obj_new_bytes((const byte *)connection_obj->srv_bda, 6));
425+
bt_connection_obj_t *new_connection_obj = modbt_connect(mp_obj_new_bytes((const byte *)connection_obj->srv_bda, 6), connection_obj->addr_type);
425426
// If new connection object has been created then overwrite the original one so from the MicroPython code the same reference can be used
426427
if(new_connection_obj != mp_const_none) {
427428
memcpy(connection_obj, new_connection_obj, sizeof(bt_connection_obj_t));
@@ -477,19 +478,19 @@ static void create_hash(uint32_t pin, uint8_t *h_value)
477478
{
478479
bt_hash_obj_t pin_hash;
479480
mbedtls_sha1_context sha1_context;
480-
481+
481482
mbedtls_sha1_init(&sha1_context);
482483
mbedtls_sha1_starts_ret(&sha1_context);
483-
484+
484485
pin_hash.pin = pin;
485486
mbedtls_sha1_update_ret(&sha1_context, pin_hash.value, 4);
486-
487+
487488
mbedtls_sha1_finish_ret(&sha1_context, h_value);
488489
mbedtls_sha1_free(&sha1_context);
489490
}
490491

491-
static bool pin_changed(uint32_t new_pin)
492-
{
492+
static bool pin_changed(uint32_t new_pin)
493+
{
493494
bool ret = false;
494495
uint32_t h_size = MOD_BT_HASH_SIZE;
495496
uint8_t h_stored[MOD_BT_HASH_SIZE] = {0};
@@ -501,17 +502,17 @@ static bool pin_changed(uint32_t new_pin)
501502
mp_printf(&mp_plat_print, "Error opening secure BLE NVS namespace!\n");
502503
}
503504
nvs_get_blob(modbt_nvs_handle, key, h_stored, &h_size);
504-
505+
505506
create_hash(new_pin, h_created);
506-
507+
507508
if (memcmp(h_stored, h_created, MOD_BT_HASH_SIZE) != 0) {
508509
esp_err = nvs_set_blob(modbt_nvs_handle, key, h_created, h_size);
509510
if (esp_err == ESP_OK) {
510511
nvs_commit(modbt_nvs_handle);
511512
ret = true;
512513
}
513514
}
514-
515+
515516
nvs_close(modbt_nvs_handle);
516517

517518
return ret;
@@ -1415,7 +1416,7 @@ STATIC mp_obj_t bt_events(mp_obj_t self_in) {
14151416
}
14161417
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bt_events_obj, bt_events);
14171418

1418-
static mp_obj_t bt_connect_helper(mp_obj_t addr, TickType_t timeout){
1419+
static mp_obj_t bt_connect_helper(mp_obj_t addr, TickType_t timeout, esp_ble_addr_type_t addr_type){
14191420

14201421
bt_event_result_t bt_event;
14211422
EventBits_t uxBits;
@@ -1443,7 +1444,7 @@ static mp_obj_t bt_connect_helper(mp_obj_t addr, TickType_t timeout){
14431444
bt_obj.busy = true;
14441445

14451446
/* Initiate a background connection, esp_ble_gattc_open returns immediately */
1446-
if (ESP_OK != esp_ble_gattc_open(bt_obj.gattc_if, bufinfo.buf, BLE_ADDR_TYPE_PUBLIC, true)) {
1447+
if (ESP_OK != esp_ble_gattc_open(bt_obj.gattc_if, bufinfo.buf, addr_type, true)) {
14471448
// Only drop exception if not called from bt_resume() API, otherwise return with mp_const_none on error
14481449
if(mod_bt_allow_resume_deinit == false) {
14491450
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
@@ -1473,6 +1474,7 @@ static mp_obj_t bt_connect_helper(mp_obj_t addr, TickType_t timeout){
14731474
conn->base.type = (mp_obj_t)&mod_bt_connection_type;
14741475
conn->conn_id = bt_event.connection.conn_id;
14751476
conn->gatt_if = bt_event.connection.gatt_if;
1477+
conn->addr_type = addr_type;
14761478

14771479
MP_THREAD_GIL_EXIT();
14781480
uxBits = xEventGroupWaitBits(bt_event_group, MOD_BT_GATTC_MTU_EVT, true, true, 1000/portTICK_PERIOD_MS);
@@ -1510,6 +1512,7 @@ STATIC mp_obj_t bt_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
15101512
STATIC const mp_arg_t allowed_args[] = {
15111513
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_OBJ, },
15121514
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
1515+
{ MP_QSTR_addr_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
15131516
};
15141517

15151518
// parse arguments
@@ -1518,7 +1521,7 @@ STATIC mp_obj_t bt_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
15181521

15191522
mp_obj_t addr = args[0].u_obj;
15201523

1521-
/* Timeout parameter is in miliseconds */
1524+
/* Timeout parameter is in milliseconds */
15221525
TickType_t timeout;
15231526
if(args[1].u_obj == MP_OBJ_NULL){
15241527
timeout = portMAX_DELAY;
@@ -1534,13 +1537,30 @@ STATIC mp_obj_t bt_connect(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
15341537
}
15351538
}
15361539

1537-
return bt_connect_helper(addr, timeout);
1540+
/* addr_type parameter */
1541+
uint32_t addr_type;
1542+
if(args[2].u_obj == MP_OBJ_NULL){
1543+
addr_type = BLE_ADDR_TYPE_PUBLIC;
1544+
}
1545+
else
1546+
{
1547+
if(MP_OBJ_IS_SMALL_INT(args[2].u_obj) == true) {
1548+
addr_type = mp_obj_get_int(args[2].u_obj);
1549+
}
1550+
else
1551+
{
1552+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "If addr_type is specified it must be a valid integer number"));
1553+
}
1554+
}
1555+
1556+
1557+
return bt_connect_helper(addr, timeout, addr_type);
15381558
}
15391559
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bt_connect_obj, 1, bt_connect);
15401560

1541-
static mp_obj_t modbt_connect(mp_obj_t addr)
1561+
static mp_obj_t modbt_connect(mp_obj_t addr, esp_ble_addr_type_t addr_type)
15421562
{
1543-
return bt_connect_helper(addr, portMAX_DELAY);
1563+
return bt_connect_helper(addr, portMAX_DELAY, addr_type);
15441564
}
15451565

15461566

@@ -1553,7 +1573,7 @@ STATIC mp_obj_t bt_set_advertisement_params (mp_uint_t n_args, const mp_obj_t *p
15531573
{ MP_QSTR_channel_map, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
15541574
{ MP_QSTR_adv_filter_policy, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
15551575
};
1556-
1576+
15571577
// parse args
15581578
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
15591579
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);
@@ -1706,14 +1726,14 @@ STATIC mp_obj_t bt_set_advertisement_raw(mp_obj_t self_in, mp_obj_t raw_data) {
17061726
memcpy(data, (uint8_t *)bufinfo.buf, sizeof(data));
17071727
data_len = sizeof(data);
17081728
}
1709-
1729+
17101730
esp_ble_gap_config_adv_data_raw(data, data_len);
1711-
1731+
17121732
// wait for the advertisement data to be configured
17131733
bt_gatts_event_result_t gatts_event;
17141734
xQueueReceive(xGattsQueue, &gatts_event, portMAX_DELAY);
17151735
}
1716-
1736+
17171737
return mp_const_none;
17181738
}
17191739
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bt_set_advertisement_raw_obj, bt_set_advertisement_raw);

0 commit comments

Comments
 (0)