Skip to content

Commit 15ed856

Browse files
Thalleycarlescufi
authored andcommitted
Bluetooth: Audio: Update ASE EP state checks
Update the existing ASE EP state checks to be more correct and aligned with the ASCS specification. Signed-off-by: Emil Gydesen <[email protected]>
1 parent a659548 commit 15ed856

File tree

2 files changed

+196
-26
lines changed

2 files changed

+196
-26
lines changed

subsys/bluetooth/audio/ascs.c

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,33 +138,150 @@ void ascs_ep_set_state(struct bt_audio_ep *ep, uint8_t state)
138138
if (ops->released != NULL) {
139139
ops->released(stream);
140140
}
141+
141142
break;
142143
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
144+
switch (old_state) {
145+
case BT_AUDIO_EP_STATE_IDLE:
146+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
147+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
148+
case BT_AUDIO_EP_STATE_RELEASING:
149+
break;
150+
default:
151+
BT_ASSERT_MSG(false,
152+
"Invalid state transition: %s -> %s",
153+
bt_audio_ep_state_str(old_state),
154+
bt_audio_ep_state_str(ep->status.state));
155+
return;
156+
}
157+
143158
if (ops->configured != NULL) {
144159
ops->configured(stream, &ep->qos_pref);
145160
}
161+
146162
break;
147163
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
164+
/* QoS configured have different allowed states
165+
* depending on the endpoint type
166+
*/
167+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
168+
switch (old_state) {
169+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
170+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
171+
case BT_AUDIO_EP_STATE_DISABLING:
172+
break;
173+
default:
174+
BT_ASSERT_MSG(false,
175+
"Invalid state transition: %s -> %s",
176+
bt_audio_ep_state_str(old_state),
177+
bt_audio_ep_state_str(ep->status.state));
178+
return;
179+
}
180+
} else {
181+
switch (old_state) {
182+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
183+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
184+
case BT_AUDIO_EP_STATE_ENABLING:
185+
case BT_AUDIO_EP_STATE_STREAMING:
186+
break;
187+
default:
188+
BT_ASSERT_MSG(false,
189+
"Invalid state transition: %s -> %s",
190+
bt_audio_ep_state_str(old_state),
191+
bt_audio_ep_state_str(ep->status.state));
192+
return;
193+
}
194+
}
195+
148196
if (ops->qos_set != NULL) {
149197
ops->qos_set(stream);
150198
}
199+
151200
break;
152201
case BT_AUDIO_EP_STATE_ENABLING:
202+
switch (old_state) {
203+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
204+
case BT_AUDIO_EP_STATE_ENABLING:
205+
break;
206+
default:
207+
BT_ASSERT_MSG(false,
208+
"Invalid state transition: %s -> %s",
209+
bt_audio_ep_state_str(old_state),
210+
bt_audio_ep_state_str(ep->status.state));
211+
return;
212+
}
213+
153214
if (ops->enabled != NULL) {
154215
ops->enabled(stream);
155216
}
217+
156218
break;
157219
case BT_AUDIO_EP_STATE_STREAMING:
220+
switch (old_state) {
221+
case BT_AUDIO_EP_STATE_ENABLING:
222+
case BT_AUDIO_EP_STATE_STREAMING:
223+
break;
224+
default:
225+
BT_ASSERT_MSG(false,
226+
"Invalid state transition: %s -> %s",
227+
bt_audio_ep_state_str(old_state),
228+
bt_audio_ep_state_str(ep->status.state));
229+
return;
230+
}
231+
158232
if (ops->started != NULL) {
159233
ops->started(stream);
160234
}
235+
161236
break;
162237
case BT_AUDIO_EP_STATE_DISABLING:
238+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
239+
switch (old_state) {
240+
case BT_AUDIO_EP_STATE_ENABLING:
241+
case BT_AUDIO_EP_STATE_STREAMING:
242+
break;
243+
default:
244+
BT_ASSERT_MSG(false,
245+
"Invalid state transition: %s -> %s",
246+
bt_audio_ep_state_str(old_state),
247+
bt_audio_ep_state_str(ep->status.state));
248+
return;
249+
}
250+
} else {
251+
/* Sinks cannot go into the disabling state */
252+
BT_ASSERT_MSG(false,
253+
"Invalid state transition: %s -> %s",
254+
bt_audio_ep_state_str(old_state),
255+
bt_audio_ep_state_str(ep->status.state));
256+
return;
257+
}
258+
163259
if (ops->disabled != NULL) {
164260
ops->disabled(stream);
165261
}
262+
166263
break;
167264
case BT_AUDIO_EP_STATE_RELEASING:
265+
switch (old_state) {
266+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
267+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
268+
case BT_AUDIO_EP_STATE_ENABLING:
269+
case BT_AUDIO_EP_STATE_STREAMING:
270+
break;
271+
case BT_AUDIO_EP_STATE_DISABLING:
272+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
273+
break;
274+
} /* else fall through for sink */
275+
276+
/* fall through */
277+
default:
278+
BT_ASSERT_MSG(false,
279+
"Invalid state transition: %s -> %s",
280+
bt_audio_ep_state_str(old_state),
281+
bt_audio_ep_state_str(ep->status.state));
282+
return;
283+
}
284+
168285
break; /* no-op*/
169286
default:
170287
BT_ERR("Invalid state: %u", state);

subsys/bluetooth/audio/unicast_client.c

Lines changed: 79 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,9 @@ static void unicast_client_ep_set_status(struct bt_audio_ep *ep,
771771
old_state = ep->status.state;
772772
ep->status = *status;
773773

774-
BT_DBG("ep %p handle 0x%04x id 0x%02x state %s -> %s", ep, ep->client.handle,
775-
status->id, bt_audio_ep_state_str(old_state),
774+
BT_DBG("ep %p handle 0x%04x id 0x%02x dir %u state %s -> %s", ep,
775+
ep->client.handle, status->id, ep->dir,
776+
bt_audio_ep_state_str(old_state),
776777
bt_audio_ep_state_str(status->state));
777778

778779
switch (status->state) {
@@ -794,58 +795,103 @@ static void unicast_client_ep_set_status(struct bt_audio_ep *ep,
794795
BT_WARN("Invalid state transition: %s -> %s",
795796
bt_audio_ep_state_str(old_state),
796797
bt_audio_ep_state_str(ep->status.state));
798+
return;
797799
}
800+
798801
unicast_client_ep_config_state(ep, buf);
799802
break;
800803
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
801-
switch (old_state) {
802-
/* Valid only if ASE_State field = 0x01 (Codec Configured) */
803-
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
804-
/* or 0x02 (QoS Configured) */
805-
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
806-
break;
807-
default:
808-
BT_WARN("Invalid state transition: %s -> %s",
809-
bt_audio_ep_state_str(old_state),
810-
bt_audio_ep_state_str(ep->status.state));
804+
/* QoS configured have different allowed states depending on the endpoint type */
805+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
806+
switch (old_state) {
807+
/* Valid only if ASE_State field = 0x01 (Codec Configured) */
808+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
809+
/* or 0x02 (QoS Configured) */
810+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
811+
/* or 0x05 (Disabling) */
812+
case BT_AUDIO_EP_STATE_DISABLING:
813+
break;
814+
default:
815+
BT_WARN("Invalid state transition: %s -> %s",
816+
bt_audio_ep_state_str(old_state),
817+
bt_audio_ep_state_str(ep->status.state));
818+
return;
819+
}
820+
} else {
821+
switch (old_state) {
822+
/* Valid only if ASE_State field = 0x01 (Codec Configured) */
823+
case BT_AUDIO_EP_STATE_CODEC_CONFIGURED:
824+
/* or 0x02 (QoS Configured) */
825+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
826+
/* or 0x03 (Enabling) */
827+
case BT_AUDIO_EP_STATE_ENABLING:
828+
/* or 0x04 (Streaming)*/
829+
case BT_AUDIO_EP_STATE_STREAMING:
830+
break;
831+
default:
832+
BT_WARN("Invalid state transition: %s -> %s",
833+
bt_audio_ep_state_str(old_state),
834+
bt_audio_ep_state_str(ep->status.state));
835+
return;
836+
}
811837
}
838+
812839
unicast_client_ep_qos_state(ep, buf);
813840
break;
814841
case BT_AUDIO_EP_STATE_ENABLING:
842+
switch (old_state) {
815843
/* Valid only if ASE_State field = 0x02 (QoS Configured) */
816-
if (old_state != BT_AUDIO_EP_STATE_QOS_CONFIGURED) {
844+
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
845+
/* or 0x03 (Enabling) */
846+
case BT_AUDIO_EP_STATE_ENABLING:
847+
break;
848+
default:
817849
BT_WARN("Invalid state transition: %s -> %s",
818850
bt_audio_ep_state_str(old_state),
819851
bt_audio_ep_state_str(ep->status.state));
852+
return;
820853
}
854+
821855
unicast_client_ep_enabling_state(ep, buf);
822856
break;
823857
case BT_AUDIO_EP_STATE_STREAMING:
824858
switch (old_state) {
825-
/* Valid only if ASE_State field = 0x02 (QoS Configured) */
826-
case BT_AUDIO_EP_STATE_QOS_CONFIGURED:
827-
/* or 0x03 (Enabling)*/
859+
/* Valid only if ASE_State field = 0x03 (Enabling)*/
828860
case BT_AUDIO_EP_STATE_ENABLING:
861+
/* or 0x04 (Streaming)*/
862+
case BT_AUDIO_EP_STATE_STREAMING:
829863
break;
830864
default:
831865
BT_WARN("Invalid state transition: %s -> %s",
832866
bt_audio_ep_state_str(old_state),
833867
bt_audio_ep_state_str(ep->status.state));
868+
return;
834869
}
870+
835871
unicast_client_ep_streaming_state(ep, buf);
836872
break;
837873
case BT_AUDIO_EP_STATE_DISABLING:
838-
switch (old_state) {
839-
/* Valid only if ASE_State field = 0x03 (Enabling) */
840-
case BT_AUDIO_EP_STATE_ENABLING:
841-
/* or 0x04 (Streaming) */
842-
case BT_AUDIO_EP_STATE_STREAMING:
843-
break;
844-
default:
874+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
875+
switch (old_state) {
876+
/* Valid only if ASE_State field = 0x03 (Enabling) */
877+
case BT_AUDIO_EP_STATE_ENABLING:
878+
/* or 0x04 (Streaming) */
879+
case BT_AUDIO_EP_STATE_STREAMING:
880+
break;
881+
default:
882+
BT_WARN("Invalid state transition: %s -> %s",
883+
bt_audio_ep_state_str(old_state),
884+
bt_audio_ep_state_str(ep->status.state));
885+
return;
886+
}
887+
} else {
888+
/* Sinks cannot go into the disabling state */
845889
BT_WARN("Invalid state transition: %s -> %s",
846-
bt_audio_ep_state_str(old_state),
847-
bt_audio_ep_state_str(ep->status.state));
890+
bt_audio_ep_state_str(old_state),
891+
bt_audio_ep_state_str(ep->status.state));
892+
return;
848893
}
894+
849895
unicast_client_ep_disabling_state(ep, buf);
850896
break;
851897
case BT_AUDIO_EP_STATE_RELEASING:
@@ -858,14 +904,21 @@ static void unicast_client_ep_set_status(struct bt_audio_ep *ep,
858904
case BT_AUDIO_EP_STATE_ENABLING:
859905
/* or 0x04 (Streaming) */
860906
case BT_AUDIO_EP_STATE_STREAMING:
907+
break;
861908
/* or 0x04 (Disabling) */
862909
case BT_AUDIO_EP_STATE_DISABLING:
863-
break;
910+
if (ep->dir == BT_AUDIO_DIR_SOURCE) {
911+
break;
912+
} /* else fall through for sink */
913+
914+
/* fall through */
864915
default:
865916
BT_WARN("Invalid state transition: %s -> %s",
866917
bt_audio_ep_state_str(old_state),
867918
bt_audio_ep_state_str(ep->status.state));
919+
return;
868920
}
921+
869922
unicast_client_ep_releasing_state(ep, buf);
870923
break;
871924
}

0 commit comments

Comments
 (0)