@@ -707,77 +707,89 @@ static sys_slist_t *pacs_get_pac(enum bt_audio_dir dir)
707707 BT_PAC_SUPPORTED_CONTEXT(supported_context_read) \
708708}
709709
710+ static const struct bt_gatt_attr _pacs_attrs [] = BT_PACS_SERVICE_DEFINITION ();
710711static struct bt_gatt_attr pacs_attrs [] = BT_PACS_SERVICE_DEFINITION ();
711712static struct bt_gatt_service pacs_svc = (struct bt_gatt_service )BT_GATT_SERVICE (pacs_attrs );
712713
713-
714-
715-
716- #if defined(BT_PAC_SNK_NOTIFIABLE )
717- #define PACS_SINK_PAC_CHAR_ATTR_COUNT 3 /* declaration + value + cccd */
718- #else
719- #define PACS_SINK_PAC_CHAR_ATTR_COUNT 2 /* declaration + value */
720- #endif /* BT_PAC_SNK_NOTIFIABLE */
721-
722- #if defined(BT_PAC_SNK_LOC_NOTIFIABLE )
723- #define PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT 3 /* declaration + value + cccd */
724- #else
725- #define PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT 2 /* declaration + value*/
726- #endif /* BT_PAC_SNK_LOC_NOTIFIABLE */
727-
728- #if defined(BT_PAC_SRC_NOTIFIABLE )
729- #define PACS_SOURCE_PAC_CHAR_ATTR_COUNT 3 /* declaration + value + cccd */
714+ #if defined(CONFIG_BT_PAC_SNK )
715+ /* declaration + value [+ cccd] */
716+ #define PACS_SNK_PAC_CHAR_ATTR_COUNT COND_CODE_1(IS_ENABLED(CONFIG_BT_PAC_SNK_NOTIFIABLE), (3), (2))
717+ /* declaration + value [+ cccd] */
718+ #define PACS_SNK_PAC_LOC_CHAR_ATTR_COUNT \
719+ COND_CODE_1(IS_ENABLED(CONFIG_BT_PAC_SNK_LOC_NOTIFIABLE), (3), (2))
730720#else
731- #define PACS_SOURCE_PAC_CHAR_ATTR_COUNT 2 /* declaration + value */
732- #endif /* BT_PAC_SRC_NOTIFIABLE */
721+ #define PACS_SNK_PAC_CHAR_ATTR_COUNT 0
722+ #define PACS_SNK_PAC_LOC_CHAR_ATTR_COUNT 0
723+ #endif /* CONFIG_BT_PAC_SNK */
733724
734- #if defined(BT_PAC_SRC_LOC_NOTIFIABLE )
735- #define PACS_SOURCE_PAC_LOC_CHAR_ATTR_COUNT 3 /* declaration + value + cccd */
725+ #if defined(CONFIG_BT_PAC_SRC )
726+ /* declaration + value [+ cccd] */
727+ #define PACS_SRC_PAC_CHAR_ATTR_COUNT COND_CODE_1(IS_ENABLED(CONFIG_BT_PAC_SRC_NOTIFIABLE), (3), (2))
728+ /* declaration + value [+ cccd] */
729+ #define PACS_SRC_PAC_LOC_CHAR_ATTR_COUNT \
730+ COND_CODE_1(IS_ENABLED(CONFIG_BT_PAC_SRC_LOC_NOTIFIABLE), (3), (2))
736731#else
737- #define PACS_SOURCE_PAC_LOC_CHAR_ATTR_COUNT 2 /* declaration + value*/
738- #endif /* BT_PAC_SRC_LOC_NOTIFIABLE */
732+ #define PACS_SRC_PAC_CHAR_ATTR_COUNT 0
733+ #define PACS_SRC_PAC_LOC_CHAR_ATTR_COUNT 0
734+ #endif /* CONFIG_BT_PAC_SNK */
739735
740736static void configure_pacs_char (const struct bt_bap_pacs_register_param * param )
741737{
742- size_t attrs_to_rem ;
743- uint8_t first_to_rem ;
738+ const uint8_t first_attr_offset = 1U ;
739+ struct bt_gatt_attr * svc_attrs =
740+ & pacs_svc .attrs [first_attr_offset ]; /* first attribute is the service */
741+ uint8_t attrs_to_rem = 0U ;
742+ uint8_t first_to_rem = 0U ;
743+ uint8_t src_pac_offset ;
744744
745745 /* Remove the Sink PAC and Location */
746+ #if defined(CONFIG_BT_PAC_SNK_LOC )
747+ if (!param -> snk_loc ) {
748+ first_to_rem = PACS_SNK_PAC_CHAR_ATTR_COUNT ;
749+ attrs_to_rem = PACS_SNK_PAC_LOC_CHAR_ATTR_COUNT ;
750+ }
751+ #endif /* CONFIG_BT_PAC_SNK_LOC */
752+ #if defined(CONFIG_BT_PAC_SNK )
746753 if (!param -> snk_pac ) {
747- first_to_rem = 0 ;
748- attrs_to_rem = PACS_SINK_PAC_CHAR_ATTR_COUNT + PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT ;
749- } else if (!param -> snk_loc ) {
750- first_to_rem = PACS_SINK_PAC_CHAR_ATTR_COUNT ;
751- attrs_to_rem = PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT ;
752- } else {
753- first_to_rem = pacs_svc .attr_count ;
754- attrs_to_rem = 0 ;
754+ first_to_rem = 0U ;
755+ attrs_to_rem = PACS_SNK_PAC_CHAR_ATTR_COUNT + PACS_SNK_PAC_LOC_CHAR_ATTR_COUNT ;
755756 }
757+ #endif /* CONFIG_BT_PAC_SNK */
756758
757- for (size_t i = first_to_rem + attrs_to_rem ; i < pacs_svc .attr_count ; i ++ ) {
758- pacs_svc .attrs [i - attrs_to_rem ] = pacs_svc .attrs [i ];
759+ if (attrs_to_rem > 0U ) {
760+ for (uint8_t i = first_to_rem + attrs_to_rem ;
761+ i < pacs_svc .attr_count - first_attr_offset ; i ++ ) {
762+ svc_attrs [i - attrs_to_rem ] = svc_attrs [i ];
763+ }
764+ pacs_svc .attr_count -= attrs_to_rem ;
759765 }
760- pacs_svc .attr_count -= attrs_to_rem ;
761766
762767 /* Set first_to_rem to the start of Source PAC Char, for cleaner offset calc */
763- first_to_rem = PACS_SINK_PAC_CHAR_ATTR_COUNT + PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT ;
768+ src_pac_offset =
769+ (PACS_SNK_PAC_CHAR_ATTR_COUNT + PACS_SNK_PAC_LOC_CHAR_ATTR_COUNT ) - attrs_to_rem ;
770+ attrs_to_rem = 0U ;
764771
765772 /* Remove the Source PAC and Location */
766- if (!param -> snk_pac ) {
767- first_to_rem -= attrs_to_rem ;
768- attrs_to_rem = PACS_SOURCE_PAC_CHAR_ATTR_COUNT +
769- PACS_SOURCE_PAC_LOC_CHAR_ATTR_COUNT ;
770- } else if (!param -> snk_loc ) {
771- first_to_rem = first_to_rem + PACS_SOURCE_PAC_CHAR_ATTR_COUNT - attrs_to_rem ;
772- attrs_to_rem = PACS_SINK_PAC_LOC_CHAR_ATTR_COUNT ;
773- } else {
774- return ;
773+ #if defined(CONFIG_BT_PAC_SRC_LOC )
774+ if (!param -> src_loc ) {
775+ first_to_rem = src_pac_offset + PACS_SRC_PAC_CHAR_ATTR_COUNT ;
776+ attrs_to_rem = PACS_SRC_PAC_LOC_CHAR_ATTR_COUNT ;
777+ }
778+ #endif /* CONFIG_BT_PAC_SRC_LOC */
779+ #if defined(CONFIG_BT_PAC_SRC )
780+ if (!param -> src_pac ) {
781+ first_to_rem = src_pac_offset ;
782+ attrs_to_rem = PACS_SRC_PAC_CHAR_ATTR_COUNT + PACS_SRC_PAC_LOC_CHAR_ATTR_COUNT ;
775783 }
784+ #endif /* CONFIG_BT_PAC_SRC */
776785
777- for (size_t i = first_to_rem ; i < pacs_svc .attr_count ; i ++ ) {
778- pacs_svc .attrs [i - attrs_to_rem ] = pacs_svc .attrs [i ];
786+ if (attrs_to_rem > 0U ) {
787+ for (uint8_t i = first_to_rem + attrs_to_rem ;
788+ i < pacs_svc .attr_count - first_attr_offset ; i ++ ) {
789+ svc_attrs [i - attrs_to_rem ] = svc_attrs [i ];
790+ }
791+ pacs_svc .attr_count -= attrs_to_rem ;
779792 }
780- pacs_svc .attr_count -= attrs_to_rem ;
781793}
782794
783795static bool valid_pacs_register_param (const struct bt_bap_pacs_register_param * param )
@@ -861,7 +873,6 @@ int bt_pacs_register(const struct bt_bap_pacs_register_param *param)
861873int bt_pacs_unregister (void )
862874{
863875 int err ;
864- struct bt_gatt_attr _pacs_attrs [] = BT_PACS_SERVICE_DEFINITION ();
865876
866877 if (!atomic_test_bit (pacs .flags , PACS_FLAG_REGISTERED )) {
867878 LOG_DBG ("No pacs instance registered" );
@@ -888,7 +899,8 @@ int bt_pacs_unregister(void)
888899 return err ;
889900 }
890901
891- memcpy (pacs_svc .attrs , & _pacs_attrs , sizeof (struct bt_gatt_attr ));
902+ /* Restore to original definition */
903+ memcpy (pacs_svc .attrs , & _pacs_attrs , sizeof (_pacs_attrs ));
892904 pacs_svc .attr_count = ARRAY_SIZE (pacs_attrs );
893905
894906 atomic_clear_bit (pacs .flags , PACS_FLAG_REGISTERED );
0 commit comments