8000 Switch to start_advertising arg · domdfcoding/circuitpython@ee7a701 · GitHub
[go: up one dir, main page]

Skip to content

Commit ee7a701

Browse files
committed
Switch to start_advertising arg
1 parent e291584 commit ee7a701

File tree

5 files changed

+27
-55
lines changed

5 files changed

+27
-55
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();
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"));

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

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,6 @@ STATIC void bleio_adapter_reset_name(bleio_adapter_obj_t *self) {
333333
common_hal_bleio_adapter_set_name(self, (char *)default_ble_name);
334334
}
335335

336-
337-
// The nRF SD 6.1.0 can only do one concurrent advertisement so share the advertising handle.
338-
uint8_t adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET;
339-
340336
void common_hal_bleio_adapter_set_enabled(bleio_adapter_obj_t *self, bool enabled) {
341337
const bool is_enabled = common_hal_bleio_adapter_get_enabled(self);
342338

@@ -390,14 +386,6 @@ bool common_hal_bleio_adapter_get_enabled(bleio_adapter_obj_t *self) {
390386
return is_enabled;
391387
}
392388

393-
void common_hal_bleio_adapter_set_tx_power(bleio_adapter_obj_t *self, mp_int_t tx_power) {
394-
self->tx_power = tx_power;
395-
}
396-
397-
mp_int_t common_hal_bleio_adapter_get_tx_power(bleio_adapter_obj_t *self) {
398-
return self->tx_power;
399-
}
400-
401389
bleio_address_obj_t *common_hal_bleio_adapter_get_address(bleio_adapter_obj_t *self) {
402390
common_hal_bleio_adapter_set_enabled(self, true);
403391

@@ -639,6 +627,9 @@ STATIC void check_data_fit(size_t data_len, bool connectable) {
639627
}
640628
}
641629

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+
642633
STATIC bool advertising_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
643634
bleio_adapter_obj_t *self = (bleio_adapter_obj_t *)self_in;
644635

@@ -657,7 +648,10 @@ STATIC bool advertising_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
657648
return true;
658649
}
659650

660-
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) {
661655
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
662656
return NRF_ERROR_BUSY;
663657
}
@@ -735,7 +729,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
735729
}
736730

737731
ble_drv_add_event_handler(advertising_on_ble_evt, self);
738-
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, adv_handle, self->tx_power);
732+
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, adv_handle, tx_power);
739733
if (err_code != NRF_SUCCESS) {
740734
return err_code;
741735
}
@@ -749,7 +743,7 @@ uint32_t _common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self,
749743
}
750744

751745

752-
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) {
753747
if (self->current_advertising_data != NULL && self->current_advertising_data == self->advertising_data) {
754748
mp_raise_bleio_BluetoothError(translate("Already advertising."));
755749
}
@@ -797,7 +791,8 @@ void common_hal_bleio_adapter_start_advertising(bleio_adapter_obj_t *self, bool
797791
self->advertising_data,
798792
advertising_data_bufinfo->len,
799793
self->scan_response_data,
800-
scan_response_data_bufinfo->len));
794+
scan_response_data_bufinfo->len,
795+
tx_power));
801796
}
802797

803798
void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ typedef struct {
4848
uint8_t *current_advertising_data;
4949
bleio_scanresults_obj_t *scan_results;
5050
mp_obj_t name;
51-
mp_int_t tx_power;
5251
mp_obj_tuple_t *connection_objs;
5352
ble_drv_evt_handler_entry_t handler_entry;
5453
} bleio_adapter_obj_t;

shared-bindings/_bleio/Adapter.c

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -142,34 +142,6 @@ const mp_obj_property_t bleio_adapter_enabled_obj = {
142142
MP_ROM_NONE },
143143
};
144144

145-
//|
146-
//| tx_power: int
147-
//| """transmitter power"""
148-
//|
149-
150-
STATIC mp_obj_t bleio_adapter_get_tx_power(mp_obj_t self) {
151-
return mp_obj_new_int(common_hal_bleio_adapter_get_tx_power(self));
152-
}
153-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_get_tx_power_obj, bleio_adapter_get_tx_power);
154-
155-
static mp_obj_t bleio_adapter_set_tx_power(mp_obj_t self, mp_obj_t value) {
156-
const mp_int_t tx_power = mp_obj_get_int(value);
157-
158-
common_hal_bleio_adapter_set_tx_power(self, tx_power);
159-
160-
return mp_const_none;
161-
}
162-
163-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(bleio_adapter_set_tx_power_obj, bleio_adapter_set_tx_power);
164-
165-
const mp_obj_property_t bleio_adapter_tx_power_obj = {
166-
.base.type = &mp_type_property,
167-
.proxy = { (mp_obj_t)&bleio_adapter_get_tx_power_obj,
168-
(mp_obj_t)&bleio_adapter_set_tx_power_obj,
169-
(mp_obj_t)&mp_const_none_obj },
170-
};
171-
172-
173145
//| address: Address
174146
//| """MAC address of the BLE adapter."""
175147
//|
@@ -218,7 +190,7 @@ const mp_obj_property_t bleio_adapter_name_obj = {
218190
MP_ROM_NONE },
219191
};
220192

221-
//| 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:
222194
//| """Starts advertising until `stop_advertising` is called or if connectable, another device
223195
//| connects to us.
224196
//|
@@ -233,20 +205,22 @@ const mp_obj_property_t bleio_adapter_name_obj = {
233205
//| :param bool connectable: If `True` then other devices are allowed to connect to this peripheral.
234206
//| :param bool anonymous: If `True` then this device's MAC address is randomized before advertising.
235207
//| :param int timeout: If set, we will only advertise for this many seconds. Zero means no timeout.
236-
//| :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"""
237210
//| ...
238211
//|
239212
STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
240213
bleio_adapter_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
241214

242-
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 };
243216
static const mp_arg_t allowed_args[] = {
244217
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ },
245218
{ MP_QSTR_scan_response, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
246219
{ MP_QSTR_connectable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
247220
{ MP_QSTR_anonymous, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
248221
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
249222
{ 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} },
250224
};
251225

252226
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -279,7 +253,7 @@ STATIC mp_obj_t bleio_adapter_start_advertising(mp_uint_t n_args, const mp_obj_t
279253
}
280254

281255
common_hal_bleio_adapter_start_advertising(self, connectable, anonymous, timeout, interval,
282-
&data_bufinfo, &scan_response_bufinfo);
256+
&data_bufinfo, &scan_response_bufinfo, args[ARG_tx_power].u_int);
283257

284258
return mp_const_none;
285259
}
@@ -485,7 +459,6 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_adapter_erase_bonding_obj, bleio_adapter_
485459

486460
STATIC const mp_rom_map_elem_t bleio_adapter_locals_dict_table[] = {
487461
{ MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&bleio_adapter_enabled_obj) },
488-
{ MP_ROM_QSTR(MP_QSTR_tx_power), MP_ROM_PTR(&bleio_adapter_tx_power_obj) },
489462
{ MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&bleio_adapter_address_obj) },
490463
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&bleio_adapter_name_obj) },
491464

shared-bindings/_bleio/Adapter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ extern bool common_hal_bleio_adapter_set_address(bleio_adapter_obj_t *self, blei
5353
extern mp_obj_str_t *common_hal_bleio_adapter_get_name(bleio_adapter_obj_t *self);
5454
extern void common_hal_bleio_adapter_set_name(bleio_adapter_obj_t *self, const char *name);
5555

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);
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);
5757

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);
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);
5959
extern void common_hal_bleio_adapter_stop_advertising(bleio_adapter_obj_t *self);
6060

6161
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)
0