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

Rc6 #416

Changes from 1 commit
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
54b8e88
BLE: Allow static passwords, remove bonded when pin changes
oligauc Oct 8, 2019
0b8c8a8
Merge pull request #64 from pycom/sec_ble
Oct 8, 2019
90eb9de
Update Jenkins File
Oct 7, 2019
6638953
mods/pyblte.c: Fix the lenght field for AT commands
robert-hh Oct 7, 2019
415af4d
mods/pybadc.c: Fix the argument handling of bits=x for adc.init()
robert-hh Oct 7, 2019
aae6c26
Restore previous wifi_on_boot behaviour.
Xykon Oct 8, 2019
3024c89
Bugfixes
Xykon Oct 8, 2019
b16e92d
Update Jenkinsfile and pycom_version.h
Xykon Oct 8, 2019
881be0e
Merge branch 'Dev' of https://github.com/pycom/pycom-micropython-sigfox
Oct 14, 2019
57b1110
Update Pybytes to 1.2.0 for new release 1.20.1.r2
Xykon Oct 30, 2019
8bc1f0c
Changes needed for idf 3.3.
geza-pycom Nov 13, 2019
36763fa
refactor: updated pybytes-devices to 1.3.0
jirkadev Nov 19, 2019
a57ae0c
chore: updated .gitignore file
jirkadev Nov 19, 2019
aca4712
Merge pull request #65 from husigeza/idf_3.3_support
geza-pycom Nov 19, 2019
5ecc3d0
Revert "Merge pull request #65 from husigeza/idf_3.3_support" (#67)
geza-pycom Nov 22, 2019
2c2c229
Update _pybytes_connection.py
Xykon Nov 25, 2019
9f1479a
Support Sigfox registration for Pybytes
Xykon Nov 30, 2019
8ad73f9
Update _pybytes_constants.py
Xykon Nov 30, 2019
3697c2a
Update pycom_version.h
Xykon Nov 30, 2019
3c65d90
PYFW-390: Update .gitignore to allow esp32/lib libraries to be updated
geza-pycom Nov 30, 2019
c5d4973
Merge pull request #69 from husigeza/PYFW-390
geza-pycom Nov 30, 2019
188ffd2
MDNS advertisement works
geza-pycom Nov 17, 2019
0ac25ff
Adding text to advertisement works
geza-pycom Nov 17, 2019
c79bb20
Query works
Nov 22, 2019
f65490d
Free up internal query results
Nov 26, 2019
1902f34
Change host_name to hostname
Nov 29, 2019
996f300
Rename text to txt
Nov 29, 2019
c2c64b5
Adding libmdns.a to esp32/lib
geza-pycom Nov 30, 2019
c40694a
Merge branch 'master' into pybytes-devices-v1.3.0
Xykon Nov 30, 2019
2071a1f
Update modpycom.c
Xykon Nov 30, 2019
004c97a
Release updates & bugfixes
Xykon Nov 30, 2019
2b017e1
Update sqnsupgrade.py
Xykon Nov 30, 2019
ade08b6
Update pycom_version.h
Xykon Nov 30, 2019
9978dfd
Update _pybytes_config.py
Xykon Nov 30, 2019
9d29de9
Merge pull request #68 from husigeza/mdns
geza-pycom Dec 3, 2019
d19e340
Adding gdbinit
geza-pycom Dec 18, 2019
7166cc0
Adding scripts for PyJTAG and short Readme
geza-pycom Jan 7, 2020
6f1451e
Update readme, move gdbinit
geza-pycom Jan 7, 2020
284cd01
Merge pull request #72 from husigeza/PyJTAG
geza-pycom Jan 11, 2020
0373d7e
re-integrate sigfox sources
peter-pycom Jan 13, 2020
34408c7
Merge pull request #66 from pycom/pybytes-devices-v1.3.0
Jan 14, 2020
e1df781
Merge pull request #73 from doniks/sigfox_reintegrate
doniks Jan 16, 2020
9a2d6c1
typo
peter-pycom Dec 6, 2019
ae96346
pyjtag clarifications
doniks Jan 20, 2020
c39484b
clarification
peter-pycom Jan 20, 2020
05c58d5
Merge pull request #75 from doniks/sigfox-readme-fix
doniks Jan 21, 2020
a87cdd9
update sigfox libraries - prep for LoPy4 certification in Regions 1,2…
peter-pycom Jan 21, 2020
f63954f
Merge pull request #74 from pycom/doniks-pyjtag-docs-improvements
doniks Jan 22, 2020
9493536
Replace xQueueSendFromISR call with xQueueSend when called from BLE e…
geza-pycom Jan 24, 2020
d6c1ccb
Merge pull request #80 from husigeza/Minor_fixes_BLE_Lora
geza-pycom Jan 28, 2020
e6bc45f
Replacing "switch-case" with "if-else if" in Region.c to avoid genera…
geza-pycom Jan 28, 2020
e87e98f
Merge pull request #81 from husigeza/Fix_LoraRegion
geza-pycom Jan 28, 2020
33ffef7
Merge pull request #76 from doniks/sigfox-ready-for-cert
doniks Jan 29, 2020
6756537
BLE characteristic update messages are lost if they sent too frequently
geza-pycom Jan 28, 2020
b410946
Merge pull request #82 from husigeza/PYFW-395
geza-pycom Jan 30, 2020
013147c
PYFW-394: mod_ssl_setup_socket() allocates memory while GIL is not lo…
geza-pycom Jan 28, 2020
d04991c
PYFW-401: Mutex of LFS can be locked in wlan_read_file or mod_ssl_rea…
geza-pycom Jan 28, 2020
fbdb44d
PYFW-402: wlan_do_connect() allocates memory while GIL is not locked
geza-pycom Jan 28, 2020
e51543b
PYFW-404: bt_connect_helper() uses MicroPython functions outside of GIL
geza-pycom Jan 28, 2020
492ffd9
PYFW-405: lte_send_at_cmd() uses MicroPython functions outside of GIL
geza-pycom Jan 28, 2020
beebc21
refactor lte_add_band() out of lte_attach()
peter-pycom Feb 4, 2020
150611f
add bands=() parameter to lte.attach()
peter-pycom Feb 4, 2020
01de258
Merge pull request #83 from husigeza/GIL_fixes
geza-pycom Feb 4, 2020
35d737f
PYFW-391: ESP-IDF 3.3.1 support (#86)
geza-pycom Feb 4, 2020
66d7dcd
Add pybytes_on_boot functionality (#87)
Xykon Feb 4, 2020
16ce927
Merge pull request #84 from doniks/lte-multiple-bands-again
Xykon Feb 4, 2020
a356ce7
Update pycom_version.h
Xykon Feb 4, 2020
45c4822
Merge branch 'master' into Dev
Xykon Feb 6, 2020
b3d3ea5
Merge pull request #88 from pycom/Dev
doniks Feb 6, 2020
09d3ee1
Update pycom_version.h
Xykon Feb 9, 2020
ef3384e
Added interface to save and resume SSL sessions
DvdGiessen Jan 28, 2020
3bcb37d
Added typechecks, copyright updated to 2020
knagymate Feb 10, 2020
6b9c880
Merge pull request #89 from husigeza/PR390
geza-pycom Feb 14, 2020
1d3ad0e
Implemented configuration options for BLE security
geza-pycom Feb 14, 2020
1de0f9b
Sigfox: finalizing certification RCZ1, 2, 4
peter-pycom Feb 19, 2020
84b9fd1
Merge pull request #93 from pycom/sigfox_cert_ready
doniks Feb 20, 2020
f2db523
protect sigfox private key
peter-pycom Feb 21, 2020
68c3374
raise exception if NV writing failed
peter-pycom Feb 24, 2020
ac40811
Ignore Visual Studio Code project files
peter-pycom Feb 24, 2020
cfe72ee
add Troubleshooting (#91)
doniks Feb 24, 2020
bc2d085
Merge pull request #94 from pycom/sigfox_private_key
peter-pycom Feb 24, 2020
5df479c
IDF 3.3.1 compiler update
Xykon Feb 26, 2020
b8f24a8
Update sqnsupgrade.py
Xykon Feb 26, 2020
68d2608
Update _pybytes_config.py
Xykon Feb 26, 2020
987adcf
Merge pull request #98 from pycom/1.22.0-98-g4638c4f-IDF-libs-libgcc.…
peter-pycom Feb 26, 2020
d38988a
Update pycom_version.h
Xykon Feb 26, 2020
a25a686
Merge pull request #99 from pycom/update_sqnsupgrade_for_release
peter-pycom Feb 26, 2020
bc41407
Merge pull request #100 from pycom/update_pybytes_for_release
peter-pycom Feb 26, 2020
3e69677
Merge pull request #101 from pycom/update_version
peter-pycom Feb 26, 2020
8d9faa6
Update IDF libraries
Xykon Feb 26, 2020
dddee59
Merge pull request #95 from pycom/ignore_vscode
peter-pycom Feb 27, 2020
77d25aa
Merge pull request #103 from pycom/update-idf-libs
Xykon Feb 27, 2020
237a8fe
sleep in main thread to yield
peter-pycom Feb 27, 2020
a7c6e2c
Update pyexec.c
Xykon Feb 27, 2020
bbf355d
Merge pull request #105 from pycom/fix_thread_tests
Xykon Feb 27, 2020
5c818cb
Fix double build frozen (#102)
peter-pycom Feb 27, 2020
9fb2548
default path and make executable (#104)
peter-pycom Feb 27, 2020
a0e3edd
Merge pull request #106 from pycom/allow_ctrl-d_when_pybytes_on_boot_…
peter-pycom Feb 28, 2020
88afbf5
update fw_updater to 6e96ea3
peter-pycom Feb 28, 2020
3e7e176
Merge pull request #108 from pycom/update_fwup
Xykon Feb 28, 2020
77b511a
Update updater.py and disable pybytes in Jenkins
peter-pycom Feb 28, 2020
a53e651
fix
peter-pycom Feb 28, 2020
07cde9e
Merge pull request #109 from pycom/update_fwup_and_Jenkins
peter-pycom Feb 28, 2020
867d922
1.20.2.rc6
peter-pycom Feb 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Implemented configuration options for BLE security
* Original PR: #360 by DvdGiessen
* Some minor modifications were applied:
- added secure_connections to bt_obj_t struct
- restored remove_all_bonded_devices() instad of remove_bonded_devices
- secure parameters are set inside set_pin()
- secure connection is decided based on PIN set
- bt_init_args secure argument is removed
- removed 'BLE paring passkey: ' REPL message
- removed automatic secure connection in case of connection
- restored characteristics notification
  • Loading branch information
geza-pycom authored Feb 14, 2020
commit 1d3ad0e681cf8f6772216c552136afa929495756
188 changes: 96 additions & 92 deletions esp32/mods/modbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
#define MOD_BT_GATTS_CLOSE_EVT (0x0400)
#define MOD_BT_NVS_NAMESPACE "BT_NVS"
#define MOD_BT_HASH_SIZE (20)
#define MOD_BT_PIN_LENGTH (6)

/******************************************************************************
DEFINE PRIVATE TYPES
Expand All @@ -103,6 +102,8 @@ typedef struct {
bool advertising;
bool controller_active;
bool secure;
bool secure_connections;
bool privacy;
} bt_obj_t;

typedef struct {
Expand Down Expand Up @@ -255,16 +256,6 @@ static esp_ble_adv_params_t bt_adv_params = {
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};

static esp_ble_adv_params_t bt_adv_params_sec = {
.adv_int_min = 0x100,
.adv_int_max = 0x100,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};


static bool mod_bt_allow_resume_deinit;
static uint16_t mod_bt_gatts_mtu_restore = 0;
static bool mod_bt_is_conn_restore_available;
Expand Down Expand Up @@ -424,11 +415,7 @@ void bt_resume(bool reconnect)

/* See if there was an averstisment active before Sleep */
if(bt_obj.advertising) {
if (!bt_obj.secure){
esp_ble_gap_start_advertising(&bt_adv_params);
} else {
esp_ble_gap_start_advertising(&bt_adv_params_sec);
}
esp_ble_gap_start_advertising(&bt_adv_params);
}
}

Expand Down Expand Up @@ -480,22 +467,6 @@ static void create_hash(uint32_t pin, uint8_t *h_value)
mbedtls_sha1_free(&sha1_context);
}

static bool is_pin_valid(uint32_t pin)
{
int digits = 0;

while(pin != 0)
{
digits++;
pin /= 10;
}

if (digits != MOD_BT_PIN_LENGTH){
return false;
}

return true;
}
static bool pin_changed(uint32_t new_pin)
{
bool ret = false;
Expand Down Expand Up @@ -534,20 +505,11 @@ static void remove_all_bonded_devices(void)
for (int i = 0; i < dev_num; i++) {
esp_ble_remove_bond_device(dev_list[i].bd_addr);
}

free(dev_list);
}

static void set_secure_parameters(uint32_t passKey){

if (pin_changed(passKey)) {
remove_all_bonded_devices();
}

uint32_t passkey = passKey;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));

esp_ble_auth_req_t auth_req = ESP_LE_AUTH_REQ_SC_MITM_BOND;
static void set_secure_parameters(bool secure_connections) {
esp_ble_auth_req_t auth_req = secure_connections ? ESP_LE_AUTH_REQ_SC_MITM_BOND : ESP_LE_AUTH_BOND;
esp_ble_gap_set_security_param(ESP_BLE_SM_AUTHEN_REQ_MODE, &auth_req, sizeof(uint8_t));

esp_ble_io_cap_t iocap = ESP_IO_CAP_OUT;
Expand All @@ -558,14 +520,30 @@ static void set_secure_parameters(uint32_t passKey){

uint8_t auth_option = ESP_BLE_ONLY_ACCEPT_SPECIFIED_AUTH_DISABLE;
esp_ble_gap_set_security_param(ESP_BLE_SM_ONLY_ACCEPT_SPECIFIED_SEC_AUTH, &auth_option, sizeof(uint8_t));

uint8_t init_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_INIT_KEY, &init_key, sizeof(uint8_t));

uint8_t rsp_key = ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_RSP_KEY, &rsp_key, sizeof(uint8_t));
}

static void set_pin(uint32_t new_pin)
{
if (new_pin > 999999) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Pin code with 1-6 digit length is allowed only!"));
}

if (pin_changed(new_pin)) {
remove_all_bonded_devices();
}

uint32_t passkey = new_pin;
esp_ble_gap_set_security_param(ESP_BLE_SM_SET_STATIC_PASSKEY, &passkey, sizeof(uint32_t));
bt_obj.secure = true;
set_secure_parameters(bt_obj.secure_connections);
}

static void close_connection (int32_t conn_id) {
for (mp_uint_t i = 0; i < MP_STATE_PORT(btc_conn_list).len; i++) {
bt_connection_obj_t *connection_obj = ((bt_connection_obj_t *)(MP_STATE_PORT(btc_conn_list).items[i]));
Expand Down Expand Up @@ -650,7 +628,7 @@ static void gap_events_handler (esp_gap_ble_cb_event_t event, esp_ble_gap_cb_par
}
case ESP_GAP_BLE_NC_REQ_EVT:
case ESP_GAP_BLE_PASSKEY_NOTIF_EVT: {
printf("BLE paring passkey : %d\n", param->ble_security.key_notif.passkey);
//printf("BLE paring passkey : %d\n", param->ble_security.key_notif.passkey);
break;
}
case ESP_GAP_BLE_SEC_REQ_EVT: {
Expand Down Expand Up @@ -699,7 +677,7 @@ static void gattc_events_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc
status = p_data->reg.status;
bt_obj.gattc_if = gattc_if;
if (bt_obj.secure){
esp_ble_gap_config_local_privacy(true);
esp_ble_gap_config_local_privacy(bt_obj.privacy);
}
break;
case ESP_GATTC_OPEN_EVT:
Expand Down Expand Up @@ -874,7 +852,7 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
case ESP_GATTS_REG_EVT:
bt_obj.gatts_if = gatts_if;
if (bt_obj.secure){
esp_ble_gap_config_local_privacy(true);
esp_ble_gap_config_local_privacy(bt_obj.privacy);
}
break;
case ESP_GATTS_READ_EVT: {
Expand Down Expand Up @@ -1007,20 +985,13 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
if (bt_obj.trigger & MOD_BT_GATTS_CONN_EVT) {
mp_irq_queue_interrupt_non_ISR(bluetooth_callback_handler, (void *)&bt_obj);
}
if (bt_obj.secure){
esp_ble_set_encryption(p->connect.remote_bda, ESP_BLE_SEC_ENCRYPT_MITM);
}
}
break;
case ESP_GATTS_DISCONNECT_EVT:
bt_obj.gatts_conn_id = -1;
xEventGroupClearBits(bt_event_group, MOD_BT_GATTS_MTU_EVT);
if (bt_obj.advertising) {
if (!bt_obj.secure){
esp_ble_gap_start_advertising(&bt_adv_params);
} else {
esp_ble_gap_start_advertising(&bt_adv_params_sec);
}
esp_ble_gap_start_advertising(&bt_adv_params);
}
bt_obj.events |= MOD_BT_GATTS_DISCONN_EVT;
xEventGroupSetBits(bt_event_group, MOD_BT_GATTS_DISCONN_EVT);
Expand Down Expand Up @@ -1072,7 +1043,7 @@ static mp_obj_t bt_init_helper(bt_obj_t *self, const mp_arg_val_t *args) {
esp_ble_gatts_app_register(MOD_BT_SERVER_APP_ID);

//set MTU
uint16_t mtu = args[5].u_int;
uint16_t mtu = args[6].u_int;
if(mtu > BT_MTU_SIZE_MAX)
{
esp_ble_gatt_set_local_mtu(BT_MTU_SIZE_MAX);
Expand Down Expand Up @@ -1116,27 +1087,35 @@ static mp_obj_t bt_init_helper(bt_obj_t *self, const mp_arg_val_t *args) {
}
}

if (args[3].u_bool){
bt_obj.secure_connections = args[5].u_bool;

if (args[3].u_obj != MP_OBJ_NULL){
bt_obj.secure = true;

uint32_t passKey = args[4].u_int;
if (!is_pin_valid(passKey)){
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Only 6 digit (0-9) pins are allowed"));
}
set_secure_parameters(passKey);
// modified default advertisement parameters for secure
bt_adv_params.adv_int_min = 0x100;
bt_adv_params.adv_int_max = 0x100;
bt_adv_params.own_addr_type = args[4].u_bool ? BLE_ADDR_TYPE_RANDOM : BLE_ADDR_TYPE_PUBLIC;

set_pin(mp_obj_get_int(args[3].u_obj));
bt_obj.privacy = args[4].u_bool;
set_secure_parameters(bt_obj.secure_connections);
} else {
bt_obj.secure = false;
}

return mp_const_none;
}

STATIC const mp_arg_t bt_init_args[] = {
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = E_BT_STACK_MODE_BLE} },
{ MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_modem_sleep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_secure, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 123456} },
{ MP_QSTR_mtu, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = BT_MTU_SIZE_MAX} },
{ MP_QSTR_id, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = E_BT_STACK_MODE_BLE} },
{ MP_QSTR_antenna, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_modem_sleep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_privacy, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
{ MP_QSTR_secure_connections, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
{ MP_QSTR_mtu, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = BT_MTU_SIZE_MAX} },

};
STATIC mp_obj_t bt_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *all_args) {
Expand All @@ -1156,7 +1135,7 @@ STATIC mp_obj_t bt_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp_uint
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
}

if (args[4].u_bool) {
if (args[4].u_obj != MP_OBJ_NULL) {
if (heap_caps_get_free_size(MALLOC_CAP_SPIRAM) == 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_MemoryError,"Secure BLE not available for 512K RAM devices"));
}
Expand Down Expand Up @@ -1496,35 +1475,47 @@ static mp_obj_t modbt_connect(mp_obj_t addr)

STATIC mp_obj_t bt_set_advertisement_params (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_adv_int_min, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x20} },
{ MP_QSTR_adv_int_max, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x40} },
{ MP_QSTR_adv_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ADV_TYPE_IND} },
{ MP_QSTR_own_addr_type, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = BLE_ADDR_TYPE_PUBLIC} },
{ MP_QSTR_channel_map, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ADV_CHNL_ALL} },
{ MP_QSTR_adv_filter_policy, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY} },
{ MP_QSTR_adv_int_min, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_adv_int_max, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_adv_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_own_addr_type, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_channel_map, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_adv_filter_policy, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
};

// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), allowed_args, args);

// adv_int_min
bt_adv_params.adv_int_min = (uint16_t)args[0].u_int;

if (args[0].u_obj != MP_OBJ_NULL) {
bt_adv_params.adv_int_min = (uint16_t) mp_obj_get_int(args[0].u_obj);
}

// adv_int_max
bt_adv_params.adv_int_max = (uint16_t)args[1].u_int;
if (args[1].u_obj != MP_OBJ_NULL) {
bt_adv_params.adv_int_max = (uint16_t) mp_obj_get_int(args[1].u_obj);
}

// adv_type
bt_adv_params.adv_type = (esp_ble_adv_type_t)args[2].u_int;

if (args[2].u_obj != MP_OBJ_NULL) {
bt_adv_params.adv_type = (esp_ble_adv_type_t) mp_obj_get_int(args[2].u_obj);
}

// own_addr_type
bt_adv_params.own_addr_type = (esp_ble_addr_type_t)args[3].u_int;

if (args[3].u_obj != MP_OBJ_NULL) {
bt_adv_params.own_addr_type = (esp_ble_addr_type_t) mp_obj_get_int(args[3].u_obj);
}

// channel_map
bt_adv_params.channel_map = (esp_ble_adv_channel_t)args[4].u_int;

if (args[4].u_obj != MP_OBJ_NULL) {
bt_adv_params.channel_map = (esp_ble_adv_channel_t) mp_obj_get_int(args[4].u_obj);
}

// adv_filter_policy
bt_adv_params.adv_filter_policy = (esp_ble_adv_filter_t)args[5].u_int;
if (args[5].u_obj != MP_OBJ_NULL) {
bt_adv_params.adv_filter_policy = (esp_ble_adv_filter_t) mp_obj_get_int(args[5].u_obj);
}

return mp_const_none;
}
Expand Down Expand Up @@ -1657,15 +1648,18 @@ STATIC mp_obj_t bt_set_advertisement_raw(mp_obj_t self_in, mp_obj_t raw_data) {
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bt_set_advertisement_raw_obj, bt_set_advertisement_raw);


STATIC mp_obj_t bt_set_pin(mp_obj_t self_in, mp_obj_t arg) {
set_pin(mp_obj_get_int(arg));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bt_set_pin_obj, bt_set_pin);


STATIC mp_obj_t bt_advertise(mp_obj_t self_in, mp_obj_t enable) {
if (mp_obj_is_true(enable)) {
// some sensible time to wait for the advertisement configuration to complete
mp_hal_delay_ms(50);
if (!bt_obj.secure){
esp_ble_gap_start_advertising(&bt_adv_params);
} else {
esp_ble_gap_start_advertising(&bt_adv_params_sec);
}
esp_ble_gap_start_advertising(&bt_adv_params);
bt_obj.advertising = true;
} else {
esp_ble_gap_stop_advertising();
Expand Down Expand Up @@ -2107,6 +2101,7 @@ STATIC const mp_map_elem_t bt_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_advertisement_params),(mp_obj_t)&bt_set_advertisement_params_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_advertisement), (mp_obj_t)&bt_set_advertisement_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_advertisement_raw), (mp_obj_t)&bt_set_advertisement_raw_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_pin), (mp_obj_t)&bt_set_pin_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_advertise), (mp_obj_t)&bt_advertise_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_service), (mp_obj_t)&bt_service_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&bt_callback_obj },
Expand Down Expand Up @@ -2160,6 +2155,15 @@ STATIC const mp_map_elem_t bt_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_AUTH), MP_OBJ_NEW_SMALL_INT(ESP_GATT_CHAR_PROP_BIT_AUTH) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PROP_EXT_PROP), MP_OBJ_NEW_SMALL_INT(ESP_GATT_CHAR_PROP_BIT_EXT_PROP) },

{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_READ), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_READ) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_READ_ENCRYPTED), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_READ_ENCRYPTED) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_READ_ENC_MITM), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_READ_ENC_MITM) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_WRITE), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_WRITE) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_WRITE_ENCRYPTED), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_WRITE_ENCRYPTED) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_WRITE_ENC_MITM), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_WRITE_ENC_MITM) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_WRITE_SIGNED), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_WRITE_SIGNED) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PERM_WRITE_SIGNED_MITM), MP_OBJ_NEW_SMALL_INT(ESP_GATT_PERM_WRITE_SIGNED_MITM) },

// Defined at https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
{ MP_OBJ_NEW_QSTR(MP_QSTR_CHAR_CONFIG_NOTIFY), MP_OBJ_NEW_SMALL_INT(1 << 0) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_CHAR_CONFIG_INDICATE), MP_OBJ_NEW_SMALL_INT(1 << 1) },
Expand Down