From e7da652711f8d7ccdffce1a4da2a25aaf61ae150 Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Fri, 7 Aug 2020 23:01:49 +0800 Subject: [PATCH 1/3] able to change ble mac address --- ports/nrf/common-hal/_bleio/Adapter.c | 9 +++++++++ shared-bindings/_bleio/Adapter.c | 10 ++++++++-- shared-bindings/_bleio/Adapter.h | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ports/nrf/common-hal/_bleio/Adapter.c b/ports/nrf/common-hal/_bleio/Adapter.c index 0b23bb7bfa..d34f5d2877 100644 --- a/ports/nrf/common-hal/_bleio/Adapter.c +++ b/ports/nrf/common-hal/_bleio/Adapter.c @@ -386,6 +386,15 @@ bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *s return address; } +uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) +{ + ble_gap_addr_t local_address; + local_address.addr_type = address->type; + const char *data = mp_obj_str_get_str(address->bytes); + memcpy(local_address.addr, data, NUM_BLEIO_ADDRESS_BYTES); + return sd_ble_gap_addr_set(&local_address); +} + mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) { uint16_t len = 0; sd_ble_gap_device_name_get(NULL, &len); diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c index 7a44d560fd..861b640b5b 100644 --- a/shared-bindings/_bleio/Adapter.c +++ b/shared-bindings/_bleio/Adapter.c @@ -96,7 +96,7 @@ const mp_obj_property_t bleio_adapter_enabled_obj = { }; //| address: Address -//| """MAC address of the BLE adapter. (read-only)""" +//| """MAC address of the BLE adapter.""" //| STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { return MP_OBJ_FROM_PTR(common_hal_bleio_adapter_get_address(self)); @@ -104,10 +104,16 @@ STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { } MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address); +STATIC mp_obj_t bleio_adapter_set_address(mp_obj_t self, mp_obj_t new_address) { + common_hal_bleio_adapter_set_address(self, new_address); + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_address_obj, bleio_adapter_set_address); + const mp_obj_property_t bleio_adapter_address_obj = { .base.type = &mp_type_property, .proxy = { (mp_obj_t)&bleio_adapter_get_address_obj, - (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&bleio_adapter_set_address_obj, (mp_obj_t)&mp_const_none_obj }, }; diff --git a/shared-bindings/_bleio/Adapter.h b/shared-bindings/_bleio/Adapter.h index 39147b6ebc..ad84bf9aa9 100644 --- a/shared-bindings/_bleio/Adapter.h +++ b/shared-bindings/_bleio/Adapter.h @@ -42,6 +42,7 @@ extern bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled); extern bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self); extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self); +extern uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); extern mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char* name); From 52990a332d602db17564ae3b1d27444f25f4531e Mon Sep 17 00:00:00 2001 From: Yihui Xiong Date: Fri, 14 Aug 2020 17:48:15 +0800 Subject: [PATCH 2/3] fix --- ports/nrf/common-hal/_bleio/Adapter.c | 12 +++++++----- shared-bindings/_bleio/Adapter.c | 4 +++- shared-bindings/_bleio/Adapter.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/ports/nrf/common-hal/_bleio/Adapter.c b/ports/nrf/common-hal/_bleio/Adapter.c index d34f5d2877..6d6669c5ea 100644 --- a/ports/nrf/common-hal/_bleio/Adapter.c +++ b/ports/nrf/common-hal/_bleio/Adapter.c @@ -386,13 +386,15 @@ bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *s return address; } -uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) -{ +bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address) { ble_gap_addr_t local_address; + mp_buffer_info_t bufinfo; + if (!mp_get_buffer(address->bytes, &bufinfo, MP_BUFFER_READ)) { + return false; + } local_address.addr_type = address->type; - const char *data = mp_obj_str_get_str(address->bytes); - memcpy(local_address.addr, data, NUM_BLEIO_ADDRESS_BYTES); - return sd_ble_gap_addr_set(&local_address); + memcpy(local_address.addr, bufinfo.buf, NUM_BLEIO_ADDRESS_BYTES); + return sd_ble_gap_addr_set(&local_address) == NRF_SUCCESS; } mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self) { diff --git a/shared-bindings/_bleio/Adapter.c b/shared-bindings/_bleio/Adapter.c index 861b640b5b..3ffe65be3d 100644 --- a/shared-bindings/_bleio/Adapter.c +++ b/shared-bindings/_bleio/Adapter.c @@ -105,7 +105,9 @@ STATIC mp_obj_t bleio_adapter_get_address(mp_obj_t self) { MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_address_obj, bleio_adapter_get_address); STATIC mp_obj_t bleio_adapter_set_address(mp_obj_t self, mp_obj_t new_address) { - common_hal_bleio_adapter_set_address(self, new_address); + if (!common_hal_bleio_adapter_set_address(self, new_address)) { + mp_raise_bleio_BluetoothError(translate("Could not set address")); + } return mp_const_none; } MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_address_obj, bleio_adapter_set_address); diff --git a/shared-bindings/_bleio/Adapter.h b/shared-bindings/_bleio/Adapter.h index ad84bf9aa9..3523733577 100644 --- a/shared-bindings/_bleio/Adapter.h +++ b/shared-bindings/_bleio/Adapter.h @@ -42,7 +42,7 @@ extern bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled); extern bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self); extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self); -extern uint32_t common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); +extern bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address); extern mp_obj_str_t* common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self); extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char* name); From e81fa3d1818a5e2548f17b0613b20882c3e6eb0c Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Fri, 14 Aug 2020 09:37:09 -0400 Subject: [PATCH 3/3] make translate --- locale/circuitpython.pot | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 32b5df5ee3..a1b286e2ed 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-08-11 15:37-0500\n" +"POT-Creation-Date: 2020-08-14 09:36-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -641,6 +641,10 @@ msgstr "" msgid "Could not restart PWM" msgstr "" +#: shared-bindings/_bleio/Adapter.c +msgid "Could not set address" +msgstr "" + #: ports/stm/common-hal/pulseio/PWMOut.c msgid "Could not start PWM" msgstr ""