@@ -769,15 +769,17 @@ static void initialize_net_buf_read_buffer(struct bt_tbs_instance *inst)
769
769
static void tbs_client_gatt_read_complete (struct bt_tbs_instance * inst )
770
770
{
771
771
(void )memset (& inst -> read_params , 0 , sizeof (inst -> read_params ));
772
- inst -> busy = false ;
772
+ atomic_clear_bit ( inst -> flags , BT_TBS_CLIENT_FLAG_BUSY ) ;
773
773
}
774
774
775
775
static int tbs_client_gatt_read (struct bt_conn * conn , struct bt_tbs_instance * inst , uint16_t handle ,
776
776
bt_gatt_read_func_t func )
777
777
{
778
778
int err ;
779
779
780
- if (inst -> busy ) {
780
+ if (atomic_test_and_set_bit (inst -> flags , BT_TBS_CLIENT_FLAG_BUSY )) {
781
+ LOG_DBG ("Instance is busy" );
782
+
781
783
return - EBUSY ;
782
784
}
783
785
@@ -787,7 +789,6 @@ static int tbs_client_gatt_read(struct bt_conn *conn, struct bt_tbs_instance *in
787
789
inst -> read_params .handle_count = 1U ;
788
790
inst -> read_params .single .handle = handle ;
789
791
inst -> read_params .single .offset = 0U ;
790
- inst -> busy = true;
791
792
792
793
err = bt_gatt_read (conn , & inst -> read_params );
793
794
if (err != 0 ) {
@@ -827,6 +828,15 @@ static void tbs_client_discover_complete(struct bt_conn *conn, int err)
827
828
/* Clear the current instance in discovery */
828
829
srv_inst -> current_inst = NULL ;
829
830
831
+ #if defined(CONFIG_BT_TBS_CLIENT_GTBS )
832
+ atomic_clear_bit (srv_inst -> gtbs_inst .flags , BT_TBS_CLIENT_FLAG_BUSY );
833
+ #endif /* CONFIG_BT_TBS_CLIENT_GTBS */
834
+ #if defined(CONFIG_BT_TBS_CLIENT_TBS )
835
+ for (size_t i = 0U ; i < ARRAY_SIZE (srv_inst -> tbs_insts ); i ++ ) {
836
+ atomic_clear_bit (srv_inst -> tbs_insts [i ].flags , BT_TBS_CLIENT_FLAG_BUSY );
837
+ }
838
+ #endif /* CONFIG_BT_TBS_CLIENT_TBS */
839
+
830
840
SYS_SLIST_FOR_EACH_CONTAINER_SAFE (& tbs_client_cbs , listener , next , _node ) {
831
841
if (listener -> discover != NULL ) {
832
842
listener -> discover (conn , err , inst_cnt (srv_inst ), gtbs_found (srv_inst ));
@@ -2381,6 +2391,48 @@ int bt_tbs_client_read_friendly_name(struct bt_conn *conn, uint8_t inst_index)
2381
2391
}
2382
2392
#endif /* defined(CONFIG_BT_TBS_CLIENT_CALL_FRIENDLY_NAME) */
2383
2393
2394
+ static bool check_and_set_all_busy (struct bt_tbs_server_inst * srv_inst )
2395
+ {
2396
+ bool all_idle = true;
2397
+
2398
+ #if defined(CONFIG_BT_TBS_CLIENT_GTBS )
2399
+ if (atomic_test_and_set_bit (srv_inst -> gtbs_inst .flags , BT_TBS_CLIENT_FLAG_BUSY )) {
2400
+ LOG_DBG ("GTBS is busy" );
2401
+
2402
+ return false;
2403
+ }
2404
+ #endif /* CONFIG_BT_TBS_CLIENT_GTBS */
2405
+
2406
+ #if defined(CONFIG_BT_TBS_CLIENT_TBS )
2407
+ size_t num_free ;
2408
+
2409
+ for (num_free = 0U ; num_free < ARRAY_SIZE (srv_inst -> tbs_insts ); num_free ++ ) {
2410
+ struct bt_tbs_instance * tbs_inst = & srv_inst -> tbs_insts [num_free ];
2411
+
2412
+ if (atomic_test_and_set_bit (tbs_inst -> flags , BT_TBS_CLIENT_FLAG_BUSY )) {
2413
+ LOG_DBG ("inst[%zu] (%p) is busy" , num_free , tbs_inst );
2414
+ all_idle = false;
2415
+
2416
+ break ;
2417
+ }
2418
+ }
2419
+ #endif /* CONFIG_BT_TBS_CLIENT_TBS */
2420
+
2421
+ /* If any is busy, revert any busy states we've set */
2422
+ if (!all_idle ) {
2423
+ #if defined(CONFIG_BT_TBS_CLIENT_GTBS )
2424
+ atomic_clear_bit (srv_inst -> gtbs_inst .flags , BT_TBS_CLIENT_FLAG_BUSY );
2425
+ #endif /* CONFIG_BT_TBS_CLIENT_GTBS */
2426
+ #if defined(CONFIG_BT_TBS_CLIENT_TBS )
2427
+ for (uint8_t i = 0U ; i < num_free ; i ++ ) {
2428
+ atomic_clear_bit (srv_inst -> tbs_insts [i ].flags , BT_TBS_CLIENT_FLAG_BUSY );
2429
+ }
2430
+ #endif /* CONFIG_BT_TBS_CLIENT_TBS */
2431
+ }
2432
+
2433
+ return all_idle ;
2434
+ }
2435
+
2384
2436
int bt_tbs_client_discover (struct bt_conn * conn )
2385
2437
{
2386
2438
uint8_t conn_index ;
@@ -2393,7 +2445,10 @@ int bt_tbs_client_discover(struct bt_conn *conn)
2393
2445
conn_index = bt_conn_index (conn );
2394
2446
srv_inst = & srv_insts [conn_index ];
2395
2447
2396
- if (srv_inst -> current_inst ) {
2448
+ /* Before we do discovery we ensure that all TBS instances are currently not busy as to not
2449
+ * interfere with any procedures in progress
2450
+ */
2451
+ if (!check_and_set_all_busy (srv_inst )) {
2397
2452
return - EBUSY ;
2398
2453
}
2399
2454
0 commit comments