Skip to content

Commit 7511e1c

Browse files
authored
Merge pull request #3493 from gabewillen/main
Implemented the ability to set the transmitter power on nRF52 devices
2 parents 14a550c + 65813ef commit 7511e1c

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

devices/ble_hci/common-hal/_bleio/Adapter.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ STATIC void check_data_fit(size_t data_len, bool connectable) {
645645
// return true;
646646
// }
647647

648-
uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data, uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len) {
648+
uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data, uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len, mp_int_t tx_power) {
649649
check_enabled(self);
650650

651651
if (self->now_advertising) {
@@ -769,7 +769,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
769769
return 0;
770770
}
771771

772-
void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo) {
772+
void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo, mp_int_t tx_power) {
773773
check_enabled(self);
774774

775775
// interval value has already been validated.
@@ -793,12 +793,17 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
793793
}
794794
}
795795

796+
if (tx_power != 0) {
797+
mp_raise_NotImplementedError(translate("Only tx_power=0 supported"));
798+
}
799+
796800
const uint32_t result = _common_hal_bleio_adapter_start_advertising(
797801
self, connectable, anonymous, timeout, interval,
798802
advertising_data_bufinfo->buf,
799803
advertising_data_bufinfo->len,
800804
scan_response_data_bufinfo->buf,
801-
scan_response_data_bufinfo->len);
805+
scan_response_data_bufinfo->len,
806+
tx_power);
802807

803808
if (result) {
804809
mp_raise_bleio_BluetoothError(translate("Already advertising"));

locale/circuitpython.pot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,10 @@ msgstr ""
12111211
msgid "Invalid AuthMode"
12121212
msgstr ""
12131213

1214+
#: ports/nrf/common-hal/_bleio/__init__.c
1215+
msgid "Invalid BLE parameter"
1216+
msgstr ""
1217+
12141218
#: shared-module/displayio/OnDiskBitmap.c
12151219
msgid "Invalid BMP file"
12161220
msgstr ""
@@ -2316,7 +2320,7 @@ msgstr ""
23162320
msgid "Unsupported format"
23172321
msgstr ""
23182322

2319-
#: ports/raspberrypi/common-hal/pulseio/PulseOut.c py/moduerrno.c
2323+
#: py/moduerrno.c
23202324
msgid "Unsupported operation"
23212325
msgstr ""
23222326

ports/nrf/common-hal/_bleio/Adapter.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,8 +619,6 @@ mp_obj_t common_hal_bleio_adapter_connect(bleio_adapter_obj_t *self, bleio_addre
619619
return mp_const_none;
620620
}
621621

622-
// The nRF SD 6.1.0 can only do one concurrent advertisement so share the advertising handle.
623-
uint8_t adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
624622

625623
STATIC void check_data_fit(size_t data_len, bool connectable) {
626624
if (data_len > BLE_GAP_ADV_SET_DATA_SIZE_EXTENDED_MAX_SUPPORTED ||
@@ -629,6 +627,9 @@ STATIC void check_data_fit(size_t data_len, bool connectable) {
629627
}
630628
}
631629

630+
// The nRF SD 6.1.0 can only do one concurrent advertisement so share the advertising handle.
631+
uint8_t adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
632+
632633
STATIC bool advertising_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
633634
bleio_adapter_obj_t *self = (bleio_adapter_obj_t *)self_in;
634635

@@ -647,7 +648,10 @@ STATIC bool advertising_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
647648
return true;
648649
}
649650

650-
uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data, uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len) {
651+
uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable,
652+
bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data,
653+
uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len,
654+
mp_int_t tx_power) {
651655
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
652656
return NRF_ERROR_BUSY;
653657
}
@@ -725,7 +729,10 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
725729
}
726730

727731
ble_drv_add_event_handler(advertising_on_ble_evt, self);
728-
732+
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, adv_handle, tx_power);
733+
if (err_code != NRF_SUCCESS) {
734+
return err_code;
735+
}
729736
vm_used_ble = true;
730737
err_code = sd_ble_gap_adv_start(adv_handle, BLE_CONN_CFG_TAG_CUSTOM);
731738
if (err_code != NRF_SUCCESS) {
@@ -736,7 +743,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
736743
}
737744

738745

739-
void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo) {
746+
void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo, mp_int_t tx_power) {
740747
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
741748
mp_raise_bleio_BluetoothError(translate("Already advertising."));
742749
}
@@ -784,7 +791,8 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
784791
self->advertising_data,
785792
advertising_data_bufinfo->len,
786793
self->scan_response_data,
787-
scan_response_data_bufinfo->len));
794+
scan_response_data_bufinfo->len,
795+
tx_power));
788796
}
789797

790798
void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self) {

ports/nrf/common-hal/_bleio/__init__.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ void check_nrf_error(uint32_t err_code) {
5252
case NRF_ERROR_TIMEOUT:
5353
mp_raise_msg(&mp_type_TimeoutError, NULL);
5454
return;
55+
case NRF_ERROR_INVALID_PARAM:
56+
mp_raise_ValueError(translate("Invalid BLE parameter"));
57+
return;
5558
case BLE_ERROR_INVALID_CONN_HANDLE:
5659
mp_raise_ConnectionError(translate("Not connected"));
5760
return;

shared-bindings/_bleio/Adapter.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ const mp_obj_property_t bleio_adapter_name_obj = {
190190
MP_ROM_NONE },
191191
};
192192

193-
//| def start_advertising(self, data: ReadableBuffer, *, scan_response: Optional[ReadableBuffer] = None, connectable: bool = True, anonymous: bool = False, timeout: int = 0, interval: float = 0.1) -> None:
193+
//| def start_advertising(self, data: ReadableBuffer, *, scan_response: Optional[ReadableBuffer] = None, connectable: bool = True, anonymous: bool = False, timeout: int = 0, interval: float = 0.1, tx_power: int = 0) -> None:
194194
//| """Starts advertising until `stop_advertising` is called or if connectable, another device
195195
//| connects to us.
196196
//|
@@ -205,20 +205,22 @@ const mp_obj_property_t bleio_adapter_name_obj = {
205205
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.
206206
//| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising.
207207
//| :param int timeout: If set, we will only advertise for this many seconds. Zero means no timeout.
208-
//| :param float interval: advertising interval, in seconds"""
208+
//| :param float interval: advertising interval, in seconds
209+
//| :param tx_power int: transmitter power while advertising in dBm"""
209210
//| ...
210211
//|
211212
STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
212213
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
213214

214-
enum { ARG_data, ARG_scan_response, ARG_connectable, ARG_anonymous, ARG_timeout, ARG_interval };
215+
enum { ARG_data, ARG_scan_response, ARG_connectable, ARG_anonymous, ARG_timeout, ARG_interval, ARG_tx_power };
215216
static const mp_arg_t allowed_args[] = {
216217
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
217218
{ MP_QSTR_scan_response, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
218219
{ MP_QSTR_connectable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
219220
{ MP_QSTR_anonymous, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
220221
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
221222
{ MP_QSTR_interval, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
223+
{ MP_QSTR_tx_power, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
222224
};
223225

224226
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -251,7 +253,7 @@ STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t
251253
}
252254

253255
common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval,
254-
&data_bufinfo, &scan_response_bufinfo);
256+
&data_bufinfo, &scan_response_bufinfo, args[ARG_tx_power].u_int);
255257

256258
return mp_const_none;
257259
}

shared-bindings/_bleio/Adapter.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ void common_hal_bleio_adapter_construct_hci_uart(bleio_adapter_obj_t *self, busi
4444
extern bool common_hal_bleio_adapter_get_advertising(bleio_adapter_obj_t *self);
4545
extern bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self);
4646
extern void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled);
47+
extern mp_int_t common_hal_bleio_adapter_get_tx_power(bleio_adapter_obj_t *self);
48+
extern void common_hal_bleio_adapter_set_tx_power(bleio_adapter_obj_t *self, mp_int_t tx_power);
4749
extern bool common_hal_bleio_adapter_get_connected(bleio_adapter_obj_t *self);
4850
extern bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self);
4951
extern bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, bleio_address_obj_t *address);
5052

5153
extern mp_obj_str_t *common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self);
5254
extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *name);
5355

54-
extern uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data, uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len);
56+
extern uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, float interval, uint8_t *advertising_data, uint16_t advertising_data_len, uint8_t *scan_response_data, uint16_t scan_response_data_len, mp_int_t tx_power);
5557

56-
extern void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo);
58+
extern void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool connectable, bool anonymous, uint32_t timeout, mp_float_t interval, mp_buffer_info_t *advertising_data_bufinfo, mp_buffer_info_t *scan_response_data_bufinfo, mp_int_t tx_power);
5759
extern void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self);
5860

5961
extern mp_obj_t common_hal_bleio_adapter_start_scan(bleio_adapter_obj_t *self, uint8_t *prefixes, size_t prefix_length, bool extended, mp_int_t buffer_size, mp_float_t timeout, mp_float_t interval, mp_float_t window, mp_int_t minimum_rssi, bool active);

0 commit comments

Comments
 (0)