Skip to content

Commit 452410c

Browse files
committed
pbio/drv/bluetooth: Use peripheral reference.
Start getting rid of the singleton instance so we can eventually support more than one.
1 parent 33dfc19 commit 452410c

6 files changed

Lines changed: 84 additions & 83 deletions

File tree

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,11 @@ void pbdrv_bluetooth_peripheral_release(pbdrv_bluetooth_peripheral_t *peripheral
177177
peripheral->user = NULL;
178178
}
179179

180-
const char *pbdrv_bluetooth_peripheral_get_name(void) {
181-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
180+
const char *pbdrv_bluetooth_peripheral_get_name(pbdrv_bluetooth_peripheral_t *peri) {
182181
return peri->name;
183182
}
184183

185-
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_connect_config_t *config) {
186-
187-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
184+
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_t *peri, pbdrv_bluetooth_peripheral_connect_config_t *config) {
188185

189186
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
190187
return PBIO_ERROR_INVALID_OP;
@@ -214,9 +211,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_periphe
214211
return PBIO_SUCCESS;
215212
}
216213

217-
pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void) {
218-
219-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
214+
pbio_error_t pbdrv_bluetooth_peripheral_disconnect(pbdrv_bluetooth_peripheral_t *peri) {
220215

221216
// Busy doing something else.
222217
if (peri->func) {
@@ -236,9 +231,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void) {
236231
return PBIO_SUCCESS;
237232
}
238233

239-
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_char_t *characteristic) {
240-
241-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
234+
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_t *peri, pbdrv_bluetooth_peripheral_char_t *characteristic) {
242235

243236
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)) {
244237
return PBIO_ERROR_NO_DEV;
@@ -249,16 +242,14 @@ pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_
249242

250243
// Initialize operation for handling on the main thread.
251244
characteristic->handle = 0;
252-
peripheral_singleton.char_now = characteristic;
245+
peri->char_now = characteristic;
253246
peri->func = pbdrv_bluetooth_peripheral_discover_characteristic_func;
254247
peri->err = PBIO_ERROR_AGAIN;
255248
pbio_os_request_poll();
256249
return PBIO_SUCCESS;
257250
}
258251

259-
pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peripheral_char_t *characteristic) {
260-
261-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
252+
pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peripheral_t *peri, pbdrv_bluetooth_peripheral_char_t *characteristic) {
262253

263254
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)) {
264255
return PBIO_ERROR_NO_DEV;
@@ -268,7 +259,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peri
268259
}
269260

270261
// Initialize operation for handling on the main thread.
271-
peripheral_singleton.char_now = characteristic;
262+
peri->char_now = characteristic;
272263
peri->func = pbdrv_bluetooth_peripheral_read_characteristic_func;
273264
peri->err = PBIO_ERROR_AGAIN;
274265
pbio_os_request_poll();
@@ -279,9 +270,7 @@ uint16_t pbdrv_bluetooth_char_write_handle;
279270
uint8_t pbdrv_bluetooth_char_write_data[PBDRV_BLUETOOTH_MAX_CHAR_SIZE];
280271
size_t pbdrv_bluetooth_char_write_size;
281272

282-
pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(uint16_t handle, const uint8_t *data, size_t size) {
283-
284-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
273+
pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(pbdrv_bluetooth_peripheral_t *peri, uint16_t handle, const uint8_t *data, size_t size) {
285274

286275
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL)) {
287276
return PBIO_ERROR_NO_DEV;
@@ -306,7 +295,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(uint16_t handle, co
306295

307296
pbio_error_t pbdrv_bluetooth_await_peripheral_command(pbio_os_state_t *state, void *context) {
308297

309-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
298+
pbdrv_bluetooth_peripheral_t *peri = context;
310299

311300
// If the user is no longer calling this then the operation is no longer
312301
// of interest and will be cancelled if the active function supports it.
@@ -626,12 +615,12 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
626615
pbdrv_bluetooth_cancel_operation_request();
627616

628617
// Let ongoing user tasks finish first.
629-
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, NULL));
618+
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, &peripheral_singleton));
630619
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL));
631620

632621
// Disconnect peripheral.
633-
pbdrv_bluetooth_peripheral_disconnect();
634-
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, NULL));
622+
pbdrv_bluetooth_peripheral_disconnect(&peripheral_singleton);
623+
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, &peripheral_singleton));
635624

636625
// Stop scanning.
637626
pbdrv_bluetooth_start_observing(NULL);

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *s
588588

589589
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *state, void *context) {
590590

591-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
591+
pbdrv_bluetooth_peripheral_t *peri = context;
592592
pbdrv_bluetooth_ad_match_result_flags_t flags;
593593
uint8_t btstack_error;
594594

@@ -803,7 +803,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
803803

804804
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic_func(pbio_os_state_t *state, void *context) {
805805

806-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
806+
pbdrv_bluetooth_peripheral_t *peri = context;
807807
gatt_client_characteristic_t *current_char = &peri->platform_state->current_char;
808808

809809
uint8_t btstack_error;
@@ -910,7 +910,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic_func(pbio_os_sta
910910

911911
pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic_func(pbio_os_state_t *state, void *context) {
912912

913-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
913+
pbdrv_bluetooth_peripheral_t *peri = context;
914914

915915
uint8_t btstack_error;
916916

@@ -959,7 +959,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic_func(pbio_os_state_t
959959

960960
pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic_func(pbio_os_state_t *state, void *context) {
961961

962-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
962+
pbdrv_bluetooth_peripheral_t *peri = context;
963963

964964
uint8_t btstack_error;
965965

@@ -998,7 +998,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic_func(pbio_os_state_
998998

999999
pbio_error_t pbdrv_bluetooth_peripheral_disconnect_func(pbio_os_state_t *state, void *context) {
10001000

1001-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
1001+
pbdrv_bluetooth_peripheral_t *peri = context;
10021002

10031003
PBIO_OS_ASYNC_BEGIN(state);
10041004

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *s
336336
}
337337

338338
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *state, void *context) {
339-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
339+
pbdrv_bluetooth_peripheral_t *peri = context;
340340

341341
// Scan and connect timeout, if applicable.
342342
bool timed_out = peri->config->timeout && pbio_os_timer_is_expired(&peri->timer);
@@ -455,7 +455,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
455455
}
456456

457457
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic_func(pbio_os_state_t *state, void *context) {
458-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
458+
pbdrv_bluetooth_peripheral_t *peri = context;
459459

460460
PBIO_OS_ASYNC_BEGIN(state);
461461

@@ -573,7 +573,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic_func(pbio_os_state_
573573

574574
pbio_error_t pbdrv_bluetooth_peripheral_disconnect_func(pbio_os_state_t *state, void *context) {
575575

576-
pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton;
576+
pbdrv_bluetooth_peripheral_t *peri = context;
577577

578578
PBIO_OS_ASYNC_BEGIN(state);
579579

lib/pbio/include/pbdrv/bluetooth.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ void pbdrv_bluetooth_peripheral_release(pbdrv_bluetooth_peripheral_t *peripheral
386386
*
387387
* @return The name of the connected peripheral. May not be set.
388388
*/
389-
const char *pbdrv_bluetooth_peripheral_get_name(void);
389+
const char *pbdrv_bluetooth_peripheral_get_name(pbdrv_bluetooth_peripheral_t *peripheral);
390390

391391
/**
392392
* Starts scanning for a BLE device and connects to it.
@@ -395,7 +395,7 @@ const char *pbdrv_bluetooth_peripheral_get_name(void);
395395
* @return ::PBIO_SUCCESS if the operation was scheduled.
396396
* ::PBIO_ERROR_BUSY if already connected or another peripheral operation is ongoing.
397397
*/
398-
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_connect_config_t *config);
398+
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_t *peripheral, pbdrv_bluetooth_peripheral_connect_config_t *config);
399399

400400
/**
401401
* Disconnect from the peripheral.
@@ -404,7 +404,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_periphe
404404
* @return ::PBIO_SUCCESS if disconnection schefuled or when already disconnected.
405405
* ::PBIO_ERROR_BUSY if another peripheral operation is ongoing.
406406
*/
407-
pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void);
407+
pbio_error_t pbdrv_bluetooth_peripheral_disconnect(pbdrv_bluetooth_peripheral_t *peripheral);
408408

409409
/**
410410
* Find a characteristic by UUID and properties.
@@ -417,7 +417,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void);
417417
* ::PBIO_ERROR_BUSY if another peripheral operation is ongoing.
418418
* ::PBIO_ERROR_NO_DEV if no peripheral is connected.
419419
*/
420-
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_char_t *characteristic);
420+
pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_t *peripheral, pbdrv_bluetooth_peripheral_char_t *characteristic);
421421

422422
/**
423423
* Read a characteristic.
@@ -427,13 +427,14 @@ pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_
427427
* ::PBIO_ERROR_NO_DEV if not connected to a peripheral.
428428
* ::PBIO_ERROR_BUSY if another operation is ongoing.
429429
*/
430-
pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peripheral_char_t *characteristic);
430+
pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peripheral_t *peripheral, pbdrv_bluetooth_peripheral_char_t *characteristic);
431431

432432
/**
433433
* Write a value to a peripheral characteristic without response.
434434
*
435435
* The write is queued for transmission and does not await completion.
436436
*
437+
* @param [in] peri The peripheral to write to.
437438
* @param [in] handle The handle of the characteristic value to write.
438439
* @param [in] data The data to write.
439440
* @param [in] size The size of @p data in bytes.
@@ -442,14 +443,14 @@ pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(pbdrv_bluetooth_peri
442443
* ::PBIO_ERROR_BUSY if another peripheral operation is ongoing.
443444
* ::PBIO_ERROR_INVALID_ARG if @p size is too big.
444445
*/
445-
pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(uint16_t handle, const uint8_t *data, size_t size);
446+
pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(pbdrv_bluetooth_peripheral_t *peripheral, uint16_t handle, const uint8_t *data, size_t size);
446447

447448
/**
448449
* Awaits for a task associated with a peripheral to complete. Used to await
449450
* characteristic discover/read/write, scan-and-connect, or disconnect.
450451
*
451452
* @param [in] state Protothread state. Not used in all implementations.
452-
* @param [in] context Not used.
453+
* @param [in] context The peripheral.
453454
* @return ::PBIO_SUCCESS on completion.
454455
* ::PBIO_ERROR_AGAIN while awaiting.
455456
* or a thread specific error code if the operation failed.
@@ -599,19 +600,19 @@ static inline pbio_error_t pbdrv_bluetooth_peripheral_get_available(pbdrv_blueto
599600
static inline void pbdrv_bluetooth_peripheral_release(pbdrv_bluetooth_peripheral_t *peripheral, void *user) {
600601
}
601602

602-
static inline const char *pbdrv_bluetooth_peripheral_get_name(void) {
603+
static inline const char *pbdrv_bluetooth_peripheral_get_name(pbdrv_bluetooth_peripheral_t *peripheral) {
603604
return NULL;
604605
}
605606

606-
static inline pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_connect_config_t *config) {
607+
static inline pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_t *peripheral, pbdrv_bluetooth_peripheral_connect_config_t *config) {
607608
return PBIO_ERROR_NOT_SUPPORTED;
608609
}
609610

610-
static inline pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void) {
611+
static inline pbio_error_t pbdrv_bluetooth_peripheral_disconnect(pbdrv_bluetooth_peripheral_t *peripheral) {
611612
return PBIO_ERROR_NOT_SUPPORTED;
612613
}
613614

614-
static inline pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_char_t *characteristic) {
615+
static inline pbio_error_t pbdrv_bluetooth_peripheral_discover_characteristic(pbdrv_bluetooth_peripheral_t *peripheral, pbdrv_bluetooth_peripheral_char_t *characteristic) {
615616
return PBIO_ERROR_NOT_SUPPORTED;
616617
}
617618

@@ -620,7 +621,7 @@ static inline pbio_error_t pbdrv_bluetooth_peripheral_read_characteristic(
620621
return PBIO_ERROR_NOT_SUPPORTED;
621622
}
622623

623-
static inline pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(uint16_t handle, const uint8_t *data, size_t size) {
624+
static inline pbio_error_t pbdrv_bluetooth_peripheral_write_characteristic(pbdrv_bluetooth_peripheral_t *peripheral, uint16_t handle, const uint8_t *data, size_t size) {
624625
return PBIO_ERROR_NOT_SUPPORTED;
625626
}
626627

0 commit comments

Comments
 (0)