@@ -113,6 +113,7 @@ typedef struct {
113
113
int32_t conn_id ;
114
114
uint16_t mtu ;
115
115
esp_gatt_if_t gatt_if ;
116
+ esp_ble_addr_type_t addr_type ;
116
117
} bt_connection_obj_t ;
117
118
118
119
typedef struct {
@@ -284,7 +285,7 @@ STATIC void gattc_char_callback_handler(void *arg);
284
285
STATIC void gatts_char_callback_handler (void * arg );
285
286
static mp_obj_t modbt_start_scan (mp_obj_t timeout );
286
287
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 );
288
289
289
290
/******************************************************************************
290
291
DEFINE PUBLIC FUNCTIONS
@@ -421,7 +422,7 @@ void bt_resume(bool reconnect)
421
422
for (mp_uint_t i = 0 ; i < btc_conn_list_tmp .len ; i ++ ) {
422
423
bt_connection_obj_t * connection_obj = ((bt_connection_obj_t * )(btc_conn_list_tmp .items [i ]));
423
424
// 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 );
425
426
// If new connection object has been created then overwrite the original one so from the MicroPython code the same reference can be used
426
427
if (new_connection_obj != mp_const_none ) {
427
428
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)
477
478
{
478
479
bt_hash_obj_t pin_hash ;
479
480
mbedtls_sha1_context sha1_context ;
480
-
481
+
481
482
mbedtls_sha1_init (& sha1_context );
482
483
mbedtls_sha1_starts_ret (& sha1_context );
483
-
484
+
484
485
pin_hash .pin = pin ;
485
486
mbedtls_sha1_update_ret (& sha1_context , pin_hash .value , 4 );
486
-
487
+
487
488
mbedtls_sha1_finish_ret (& sha1_context , h_value );
488
489
mbedtls_sha1_free (& sha1_context );
489
490
}
490
491
491
- static bool pin_changed (uint32_t new_pin )
492
- {
492
+ static bool pin_changed (uint32_t new_pin )
493
+ {
493
494
bool ret = false;
494
495
uint32_t h_size = MOD_BT_HASH_SIZE ;
495
496
uint8_t h_stored [MOD_BT_HASH_SIZE ] = {0 };
@@ -501,17 +502,17 @@ static bool pin_changed(uint32_t new_pin)
501
502
mp_printf (& mp_plat_print , "Error opening secure BLE NVS namespace!\n" );
502
503
}
503
504
nvs_get_blob (modbt_nvs_handle , key , h_stored , & h_size );
504
-
505
+
505
506
create_hash (new_pin , h_created );
506
-
507
+
507
508
if (memcmp (h_stored , h_created , MOD_BT_HASH_SIZE ) != 0 ) {
508
509
esp_err = nvs_set_blob (modbt_nvs_handle , key , h_created , h_size );
509
510
if (esp_err == ESP_OK ) {
510
511
nvs_commit (modbt_nvs_handle );
511
512
ret = true;
512
513
}
513
514
}
514
-
515
+
515
516
nvs_close (modbt_nvs_handle );
516
517
517
518
return ret ;
@@ -1415,7 +1416,7 @@ STATIC mp_obj_t bt_events(mp_obj_t self_in) {
1415
1416
}
1416
1417
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (bt_events_obj , bt_events );
1417
1418
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 ){
1419
1420
1420
1421
bt_event_result_t bt_event ;
1421
1422
EventBits_t uxBits ;
@@ -1443,7 +1444,7 @@ static mp_obj_t bt_connect_helper(mp_obj_t addr, TickType_t timeout){
1443
1444
bt_obj .busy = true;
1444
1445
1445
1446
/* 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)) {
1447
1448
// Only drop exception if not called from bt_resume() API, otherwise return with mp_const_none on error
1448
1449
if (mod_bt_allow_resume_deinit == false) {
1449
1450
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){
1473
1474
conn -> base .type = (mp_obj_t )& mod_bt_connection_type ;
1474
1475
conn -> conn_id = bt_event .connection .conn_id ;
1475
1476
conn -> gatt_if = bt_event .connection .gatt_if ;
1477
+ conn -> addr_type = addr_type ;
1476
1478
1477
1479
MP_THREAD_GIL_EXIT ();
1478
1480
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
1510
1512
STATIC const mp_arg_t allowed_args [] = {
1511
1513
{ MP_QSTR_addr , MP_ARG_REQUIRED | MP_ARG_OBJ , },
1512
1514
{ 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 }},
1513
1516
};
1514
1517
1515
1518
// 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
1518
1521
1519
1522
mp_obj_t addr = args [0 ].u_obj ;
1520
1523
1521
- /* Timeout parameter is in miliseconds */
1524
+ /* Timeout parameter is in milliseconds */
1522
1525
TickType_t timeout ;
1523
1526
if (args [1 ].u_obj == MP_OBJ_NULL ){
1524
1527
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
1534
1537
}
1535
1538
}
1536
1539
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 );
1538
1558
}
1539
1559
STATIC MP_DEFINE_CONST_FUN_OBJ_KW (bt_connect_obj , 1 , bt_connect );
1540
1560
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 )
1542
1562
{
1543
- return bt_connect_helper (addr , portMAX_DELAY );
1563
+ return bt_connect_helper (addr , portMAX_DELAY , addr_type );
1544
1564
}
1545
1565
1546
1566
@@ -1553,7 +1573,7 @@ STATIC mp_obj_t bt_set_advertisement_params (mp_uint_t n_args, const mp_obj_t *p
1553
1573
{ MP_QSTR_channel_map , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
1554
1574
{ MP_QSTR_adv_filter_policy , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
1555
1575
};
1556
-
1576
+
1557
1577
// parse args
1558
1578
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
1559
1579
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) {
1706
1726
memcpy (data , (uint8_t * )bufinfo .buf , sizeof (data ));
1707
1727
data_len = sizeof (data );
1708
1728
}
1709
-
1729
+
1710
1730
esp_ble_gap_config_adv_data_raw (data , data_len );
1711
-
1731
+
1712
1732
// wait for the advertisement data to be configured
1713
1733
bt_gatts_event_result_t gatts_event ;
1714
1734
xQueueReceive (xGattsQueue , & gatts_event , portMAX_DELAY );
1715
1735
}
1716
-
1736
+
1717
1737
return mp_const_none ;
1718
1738
}
1719
1739
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (bt_set_advertisement_raw_obj , bt_set_advertisement_raw );
0 commit comments