@@ -44,9 +44,8 @@ static struct bt_vocs_client *lookup_vocs_by_handle(struct bt_conn *conn, uint16
44
44
45
45
for (int i = 0 ; i < ARRAY_SIZE (insts ); i ++ ) {
46
46
if (insts [i ].conn == conn &&
47
- insts [i ].active &&
48
- insts [i ].start_handle <= handle &&
49
- insts [i ].end_handle >= handle ) {
47
+ atomic_test_bit (insts [i ].flags , BT_VOCS_CLIENT_FLAG_ACTIVE ) &&
48
+ insts [i ].start_handle <= handle && insts [i ].end_handle >= handle ) {
50
49
return & insts [i ];
51
50
}
52
51
}
@@ -134,7 +133,7 @@ static uint8_t vocs_client_read_offset_state_cb(struct bt_conn *conn, uint8_t er
134
133
}
135
134
136
135
LOG_DBG ("Inst %p: err: 0x%02X" , inst , err );
137
- inst -> busy = false ;
136
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
138
137
139
138
if (cb_err ) {
140
139
LOG_DBG ("Offset state read failed: %d" , err );
@@ -175,7 +174,7 @@ static uint8_t vocs_client_read_location_cb(struct bt_conn *conn, uint8_t err,
175
174
}
176
175
177
176
LOG_DBG ("Inst %p: err: 0x%02X" , inst , err );
178
- inst -> busy = false ;
177
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
179
178
180
179
if (cb_err ) {
181
180
LOG_DBG ("Offset state read failed: %d" , err );
@@ -226,7 +225,7 @@ static uint8_t internal_read_volume_offset_state_cb(struct bt_conn *conn, uint8_
226
225
inst -> state .change_counter );
227
226
228
227
/* clear busy flag to reuse function */
229
- inst -> busy = false ;
228
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
230
229
write_err = bt_vocs_client_state_set (inst , inst -> cp .offset );
231
230
if (write_err ) {
232
231
cb_err = BT_ATT_ERR_UNLIKELY ;
@@ -242,7 +241,7 @@ static uint8_t internal_read_volume_offset_state_cb(struct bt_conn *conn, uint8_
242
241
}
243
242
244
243
if (cb_err ) {
245
- inst -> busy = false ;
244
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
246
245
247
246
if (inst -> cb && inst -> cb -> set_offset ) {
248
247
inst -> cb -> set_offset (& inst -> vocs , err );
@@ -272,7 +271,8 @@ static void vocs_client_write_vocs_cp_cb(struct bt_conn *conn, uint8_t err,
272
271
* change counter has been read, we restart the applications write request. If it fails
273
272
* the second time, we return an error to the application.
274
273
*/
275
- if (cb_err == BT_VOCS_ERR_INVALID_COUNTER && inst -> cp_retried ) {
274
+ if (cb_err == BT_VOCS_ERR_INVALID_COUNTER &&
275
+ atomic_test_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_CP_RETRIED )) {
276
276
cb_err = BT_ATT_ERR_UNLIKELY ;
277
277
} else if (cb_err == BT_VOCS_ERR_INVALID_COUNTER && inst -> state_handle ) {
278
278
LOG_DBG ("Invalid change counter. Reading volume offset state from server." );
@@ -281,18 +281,19 @@ static void vocs_client_write_vocs_cp_cb(struct bt_conn *conn, uint8_t err,
281
281
inst -> read_params .handle_count = 1 ;
282
282
inst -> read_params .single .handle = inst -> state_handle ;
283
283
284
+ atomic_set_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_CP_RETRIED );
285
+
284
286
cb_err = bt_gatt_read (conn , & inst -> read_params );
285
287
if (cb_err ) {
286
288
LOG_WRN ("Could not read Volume offset state: %d" , cb_err );
287
289
} else {
288
- inst -> cp_retried = true;
289
290
/* Wait for read callback */
290
291
return ;
291
292
}
292
293
}
293
294
294
- inst -> busy = false ;
295
- inst -> cp_retried = false ;
295
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_CP_RETRIED ) ;
296
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
296
297
297
298
if (inst -> cb && inst -> cb -> set_offset ) {
298
299
inst -> cb -> set_offset (& inst -> vocs , cb_err );
@@ -315,7 +316,7 @@ static uint8_t vocs_client_read_output_desc_cb(struct bt_conn *conn, uint8_t err
315
316
}
316
317
317
318
LOG_DBG ("Inst %p: err: 0x%02X" , inst , err );
318
- inst -> busy = false ;
319
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
319
320
320
321
if (cb_err ) {
321
322
LOG_DBG ("Description read failed: %d" , err );
@@ -358,7 +359,7 @@ static uint8_t vocs_discover_func(struct bt_conn *conn, const struct bt_gatt_att
358
359
359
360
if (!attr ) {
360
361
LOG_DBG ("Discovery complete for VOCS %p" , inst );
361
- inst -> busy = false ;
362
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
362
363
(void )memset (params , 0 , sizeof (* params ));
363
364
364
365
if (inst -> cb && inst -> cb -> discover ) {
@@ -393,7 +394,7 @@ static uint8_t vocs_discover_func(struct bt_conn *conn, const struct bt_gatt_att
393
394
sub_params = & inst -> location_sub_params ;
394
395
}
395
396
if (chrc -> properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP ) {
396
- inst -> location_writable = true ;
397
+ atomic_set_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_LOC_WRITABLE ) ;
397
398
}
398
399
} else if (!bt_uuid_cmp (chrc -> uuid , BT_UUID_VOCS_CONTROL )) {
399
400
LOG_DBG ("Control point" );
@@ -405,7 +406,7 @@ static uint8_t vocs_discover_func(struct bt_conn *conn, const struct bt_gatt_att
405
406
sub_params = & inst -> desc_sub_params ;
406
407
}
407
408
if (chrc -> properties & BT_GATT_CHRC_WRITE_WITHOUT_RESP ) {
408
- inst -> desc_writable = true ;
409
+ atomic_set_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_DESC_WRITABLE ) ;
409
410
}
410
411
}
411
412
@@ -455,8 +456,8 @@ int bt_vocs_client_state_get(struct bt_vocs_client *inst)
455
456
return - EINVAL ;
456
457
}
457
458
458
- if (inst -> busy ) {
459
- LOG_DBG ("Handle not set " );
459
+ if (atomic_test_and_set_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ) {
460
+ LOG_DBG ("Instance is busy " );
460
461
return - EBUSY ;
461
462
}
462
463
@@ -466,8 +467,8 @@ int bt_vocs_client_state_get(struct bt_vocs_client *inst)
466
467
inst -> read_params .single .offset = 0U ;
467
468
468
469
err = bt_gatt_read (inst -> conn , & inst -> read_params );
469
- if (! err ) {
470
- inst -> busy = true ;
470
+ if (err != 0 ) {
471
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
471
472
}
472
473
473
474
return err ;
@@ -493,13 +494,16 @@ int bt_vocs_client_location_set(struct bt_vocs_client *inst, uint32_t location)
493
494
if (!inst -> location_handle ) {
494
495
LOG_DBG ("Handle not set" );
495
496
return - EINVAL ;
496
- } else if (inst -> busy ) {
497
- return - EBUSY ;
498
- } else if (!inst -> location_writable ) {
497
+ } else if (!atomic_test_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_LOC_WRITABLE )) {
499
498
LOG_DBG ("Location is not writable on peer service instance" );
500
499
return - EPERM ;
500
+ } else if (atomic_test_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
501
+ LOG_DBG ("Instance is busy" );
502
+ return - EBUSY ;
501
503
}
502
504
505
+ /* When using write without response we do not set the busy flag */
506
+
503
507
return bt_gatt_write_without_response (inst -> conn ,
504
508
inst -> location_handle ,
505
509
& location , sizeof (location ),
@@ -523,7 +527,8 @@ int bt_vocs_client_location_get(struct bt_vocs_client *inst)
523
527
if (!inst -> location_handle ) {
524
528
LOG_DBG ("Handle not set" );
525
529
return - EINVAL ;
526
- } else if (inst -> busy ) {
530
+ } else if (atomic_test_and_set_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
531
+ LOG_DBG ("Instance is busy" );
527
532
return - EBUSY ;
528
533
}
529
534
@@ -533,8 +538,8 @@ int bt_vocs_client_location_get(struct bt_vocs_client *inst)
533
538
inst -> read_params .single .offset = 0U ;
534
539
535
540
err = bt_gatt_read (inst -> conn , & inst -> read_params );
536
- if (! err ) {
537
- inst -> busy = true ;
541
+ if (err != 0 ) {
542
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
538
543
}
539
544
540
545
return err ;
@@ -562,7 +567,8 @@ int bt_vocs_client_state_set(struct bt_vocs_client *inst, int16_t offset)
562
567
if (!inst -> control_handle ) {
563
568
LOG_DBG ("Handle not set" );
564
569
return - EINVAL ;
565
- } else if (inst -> busy ) {
570
+ } else if (atomic_test_and_set_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
571
+ LOG_DBG ("Instance is busy" );
566
572
return - EBUSY ;
567
573
}
568
574
@@ -577,8 +583,8 @@ int bt_vocs_client_state_set(struct bt_vocs_client *inst, int16_t offset)
577
583
inst -> write_params .func = vocs_client_write_vocs_cp_cb ;
578
584
579
585
err = bt_gatt_write (inst -> conn , & inst -> write_params );
580
- if (! err ) {
581
- inst -> busy = true ;
586
+ if (err != 0 ) {
587
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
582
588
}
583
589
584
590
return err ;
@@ -601,7 +607,8 @@ int bt_vocs_client_description_get(struct bt_vocs_client *inst)
601
607
if (!inst -> desc_handle ) {
602
608
LOG_DBG ("Handle not set" );
603
609
return - EINVAL ;
604
- } else if (inst -> busy ) {
610
+ } else if (atomic_test_and_set_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
611
+ LOG_DBG ("Instance is busy" );
605
612
return - EBUSY ;
606
613
}
607
614
@@ -611,8 +618,8 @@ int bt_vocs_client_description_get(struct bt_vocs_client *inst)
611
618
inst -> read_params .single .offset = 0U ;
612
619
613
620
err = bt_gatt_read (inst -> conn , & inst -> read_params );
614
- if (! err ) {
615
- inst -> busy = true ;
621
+ if (err != 0 ) {
622
+ atomic_clear_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY ) ;
616
623
}
617
624
618
625
return err ;
@@ -634,13 +641,16 @@ int bt_vocs_client_description_set(struct bt_vocs_client *inst,
634
641
if (!inst -> desc_handle ) {
635
642
LOG_DBG ("Handle not set" );
636
643
return - EINVAL ;
637
- } else if (inst -> busy ) {
638
- return - EBUSY ;
639
- } else if (!inst -> desc_writable ) {
644
+ } else if (!atomic_test_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_DESC_WRITABLE )) {
640
645
LOG_DBG ("Description is not writable on peer service instance" );
641
646
return - EPERM ;
647
+ } else if (atomic_test_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
648
+ LOG_DBG ("Instance is busy" );
649
+ return - EBUSY ;
642
650
}
643
651
652
+ /* When using write without response we do not set the busy flag */
653
+
644
654
return bt_gatt_write_without_response (inst -> conn ,
645
655
inst -> desc_handle ,
646
656
description ,
@@ -650,9 +660,8 @@ int bt_vocs_client_description_set(struct bt_vocs_client *inst,
650
660
struct bt_vocs * bt_vocs_client_free_instance_get (void )
651
661
{
652
662
for (int i = 0 ; i < ARRAY_SIZE (insts ); i ++ ) {
653
- if (!insts [i ].active ) {
663
+ if (!atomic_test_and_set_bit ( insts [i ].flags , BT_VOCS_CLIENT_FLAG_ACTIVE ) ) {
654
664
insts [i ].vocs .client_instance = true;
655
- insts [i ].active = true;
656
665
return & insts [i ].vocs ;
657
666
}
658
667
}
@@ -689,9 +698,11 @@ int bt_vocs_client_conn_get(const struct bt_vocs *vocs, struct bt_conn **conn)
689
698
static void vocs_client_reset (struct bt_vocs_client * inst )
690
699
{
691
700
memset (& inst -> state , 0 , sizeof (inst -> state ));
692
- inst -> location_writable = 0 ;
701
+ atomic_clear_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_LOC_WRITABLE );
702
+ atomic_clear_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_DESC_WRITABLE );
703
+ atomic_clear_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_CP_RETRIED );
704
+
693
705
inst -> location = 0 ;
694
- inst -> desc_writable = 0 ;
695
706
inst -> start_handle = 0 ;
696
707
inst -> end_handle = 0 ;
697
708
inst -> state_handle = 0 ;
@@ -733,12 +744,10 @@ int bt_vocs_discover(struct bt_conn *conn, struct bt_vocs *vocs,
733
744
734
745
inst = CONTAINER_OF (vocs , struct bt_vocs_client , vocs );
735
746
736
- CHECKIF (! inst -> active ) {
747
+ if (! atomic_test_bit ( inst -> flags , BT_VOCS_CLIENT_FLAG_ACTIVE ) ) {
737
748
LOG_DBG ("Inactive instance" );
738
749
return - EINVAL ;
739
- }
740
-
741
- if (inst -> busy ) {
750
+ } else if (atomic_test_and_set_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY )) {
742
751
LOG_DBG ("Instance is busy" );
743
752
return - EBUSY ;
744
753
}
@@ -752,10 +761,9 @@ int bt_vocs_discover(struct bt_conn *conn, struct bt_vocs *vocs,
752
761
inst -> discover_params .func = vocs_discover_func ;
753
762
754
763
err = bt_gatt_discover (conn , & inst -> discover_params );
755
- if (err ) {
764
+ if (err != 0 ) {
756
765
LOG_DBG ("Discover failed (err %d)" , err );
757
- } else {
758
- inst -> busy = true;
766
+ atomic_clear_bit (inst -> flags , BT_VOCS_CLIENT_FLAG_BUSY );
759
767
}
760
768
761
769
return err ;
0 commit comments