@@ -42,6 +42,8 @@ LOG_MODULE_REGISTER(bt_ascs, CONFIG_BT_ASCS_LOG_LEVEL);
42
42
(CONFIG_BT_ASCS_ASE_SNK_COUNT + \
43
43
CONFIG_BT_ASCS_ASE_SRC_COUNT)
44
44
45
+ #define NTF_HEADER_SIZE (3) /* opcode (1) + handle (2) */
46
+
45
47
BUILD_ASSERT (CONFIG_BT_ASCS_MAX_ACTIVE_ASES <= MAX (MAX_ASES_SESSIONS ,
46
48
CONFIG_BT_ISO_MAX_CHAN ),
47
49
"Max active ASEs are set to more than actual number of ASEs or ISOs" );
@@ -84,8 +86,9 @@ static struct bt_ascs_ase {
84
86
* writing
85
87
*/
86
88
BUILD_ASSERT (
87
- BT_ATT_BUF_SIZE - 3 >= ASE_BUF_SIZE ||
88
- DIV_ROUND_UP (ASE_BUF_SIZE , (BT_ATT_BUF_SIZE - 3 )) <= CONFIG_BT_ATT_PREPARE_COUNT ,
89
+ (BT_ATT_BUF_SIZE - NTF_HEADER_SIZE ) >= ASE_BUF_SIZE ||
90
+ DIV_ROUND_UP (ASE_BUF_SIZE , (BT_ATT_BUF_SIZE - NTF_HEADER_SIZE )) <=
91
+ CONFIG_BT_ATT_PREPARE_COUNT ,
89
92
"CONFIG_BT_ATT_PREPARE_COUNT not large enough to cover the maximum supported ASCS value" );
90
93
91
94
/* It is mandatory to support long writes in ASCS unconditionally, and thus
@@ -174,9 +177,19 @@ static void ase_free(struct bt_ascs_ase *ase)
174
177
(void )k_work_cancel_delayable (& ase -> state_transition_work );
175
178
}
176
179
180
+ static uint16_t get_max_ntf_size (struct bt_conn * conn )
181
+ {
182
+ const uint16_t mtu = conn == NULL ? 0 : bt_gatt_get_mtu (conn );
183
+
184
+ if (mtu > NTF_HEADER_SIZE ) {
185
+ return mtu - NTF_HEADER_SIZE ;
186
+ }
187
+
188
+ return 0U ;
189
+ }
190
+
177
191
static int ase_state_notify (struct bt_ascs_ase * ase )
178
192
{
179
- const uint8_t att_ntf_header_size = 3 ; /* opcode (1) + handle (2) */
180
193
struct bt_conn * conn = ase -> conn ;
181
194
struct bt_conn_info conn_info ;
182
195
uint16_t max_ntf_size ;
@@ -202,7 +215,7 @@ static int ase_state_notify(struct bt_ascs_ase *ase)
202
215
203
216
ascs_ep_get_status (& ase -> ep , & ase_buf );
204
217
205
- max_ntf_size = bt_gatt_get_mtu (conn ) - att_ntf_header_size ;
218
+ max_ntf_size = get_max_ntf_size (conn );
206
219
207
220
ntf_size = MIN (max_ntf_size , ase_buf .len );
208
221
if (ntf_size < ase_buf .len ) {
@@ -1079,29 +1092,37 @@ static void ascs_ase_cfg_changed(const struct bt_gatt_attr *attr,
1079
1092
LOG_DBG ("attr %p value 0x%04x" , attr , value );
1080
1093
}
1081
1094
1082
- NET_BUF_SIMPLE_DEFINE_STATIC (rsp_buf , CONFIG_BT_L2CAP_TX_MTU );
1095
+ #define CP_RSP_BUF_SIZE \
1096
+ (sizeof(struct bt_ascs_cp_rsp) + (ASE_COUNT * sizeof(struct bt_ascs_cp_ase_rsp)))
1097
+
1098
+ /* Ensure that the cp_rsp_buf can fit in any notification
1099
+ * (sizeof buffer - header for notification)
1100
+ */
1101
+ BUILD_ASSERT (BT_ATT_BUF_SIZE - NTF_HEADER_SIZE >= CP_RSP_BUF_SIZE ,
1102
+ "BT_ATT_BUF_SIZE not large enough to hold responses for all ASEs" );
1103
+ NET_BUF_SIMPLE_DEFINE_STATIC (cp_rsp_buf , CP_RSP_BUF_SIZE );
1083
1104
1084
1105
static void ascs_cp_rsp_init (uint8_t op )
1085
1106
{
1086
1107
struct bt_ascs_cp_rsp * rsp ;
1087
1108
1088
- net_buf_simple_reset (& rsp_buf );
1109
+ net_buf_simple_reset (& cp_rsp_buf );
1089
1110
1090
- rsp = net_buf_simple_add (& rsp_buf , sizeof (* rsp ));
1111
+ rsp = net_buf_simple_add (& cp_rsp_buf , sizeof (* rsp ));
1091
1112
rsp -> op = op ;
1092
1113
rsp -> num_ase = 0 ;
1093
1114
}
1094
1115
1095
1116
/* Add response to an opcode/ASE ID */
1096
1117
static void ascs_cp_rsp_add (uint8_t id , uint8_t code , uint8_t reason )
1097
1118
{
1098
- struct bt_ascs_cp_rsp * rsp = (void * )rsp_buf .__buf ;
1119
+ struct bt_ascs_cp_rsp * rsp = (void * )cp_rsp_buf .__buf ;
1099
1120
struct bt_ascs_cp_ase_rsp * ase_rsp ;
1100
1121
1101
1122
LOG_DBG ("id 0x%02x code %s (0x%02x) reason %s (0x%02x)" , id ,
1102
1123
bt_ascs_rsp_str (code ), code , bt_ascs_reason_str (reason ), reason );
1103
1124
1104
- if (rsp -> num_ase == 0xff ) {
1125
+ if (rsp -> num_ase == BT_ASCS_UNSUPP_OR_LENGTH_ERR_NUM_ASE ) {
1105
1126
return ;
1106
1127
}
1107
1128
@@ -1118,7 +1139,7 @@ static void ascs_cp_rsp_add(uint8_t id, uint8_t code, uint8_t reason)
1118
1139
break ;
1119
1140
}
1120
1141
1121
- ase_rsp = net_buf_simple_add (& rsp_buf , sizeof (* ase_rsp ));
1142
+ ase_rsp = net_buf_simple_add (& cp_rsp_buf , sizeof (* ase_rsp ));
1122
1143
ase_rsp -> id = id ;
1123
1144
ase_rsp -> code = code ;
1124
1145
ase_rsp -> reason = reason ;
@@ -1727,7 +1748,42 @@ int bt_ascs_config_ase(struct bt_conn *conn, struct bt_bap_stream *stream,
1727
1748
return 0 ;
1728
1749
}
1729
1750
1730
- static bool is_valid_config_len (struct net_buf_simple * buf )
1751
+ static uint16_t get_max_ase_rsp_for_conn (struct bt_conn * conn )
1752
+ {
1753
+ const uint16_t max_ntf_size = get_max_ntf_size (conn );
1754
+ const size_t rsp_hdr_size = sizeof (struct bt_ascs_cp_rsp );
1755
+
1756
+ if (max_ntf_size > rsp_hdr_size ) {
1757
+ return (max_ntf_size - rsp_hdr_size ) / sizeof (struct bt_ascs_cp_ase_rsp );
1758
+ }
1759
+
1760
+ return 0U ;
1761
+ }
1762
+
1763
+ static bool is_valid_num_ases (struct bt_conn * conn , uint8_t num_ases )
1764
+ {
1765
+ const uint16_t max_ase_rsp = get_max_ase_rsp_for_conn (conn );
1766
+
1767
+ if (num_ases < 1U ) {
1768
+ LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
1769
+ return false;
1770
+ } else if (num_ases > ASE_COUNT ) {
1771
+ /* If the request is for more ASEs than we have, we just reject the request */
1772
+ LOG_DBG ("Number_of_ASEs parameter value (%u) is greater than %d" , num_ases ,
1773
+ ASE_COUNT );
1774
+ return false;
1775
+ } else if (num_ases > max_ase_rsp ) {
1776
+ /* If the request is for more ASEs than we can respond to, we reject the request */
1777
+ LOG_DBG ("Number_of_ASEs parameter value (%u) is greater than what we can respond "
1778
+ "to (%u) based on the MTU" ,
1779
+ num_ases , max_ase_rsp );
1780
+ return false;
1781
+ }
1782
+
1783
+ return true;
1784
+ }
1785
+
1786
+ static bool is_valid_config_len (struct bt_conn * conn , struct net_buf_simple * buf )
1731
1787
{
1732
1788
const struct bt_ascs_config_op * op ;
1733
1789
struct net_buf_simple_state state ;
@@ -1740,8 +1796,7 @@ static bool is_valid_config_len(struct net_buf_simple *buf)
1740
1796
}
1741
1797
1742
1798
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
1743
- if (op -> num_ases < 1 ) {
1744
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
1799
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
1745
1800
return false;
1746
1801
}
1747
1802
@@ -1777,7 +1832,7 @@ static ssize_t ascs_config(struct bt_conn *conn, struct net_buf_simple *buf)
1777
1832
const struct bt_ascs_config_op * req ;
1778
1833
const struct bt_ascs_config * cfg ;
1779
1834
1780
- if (!is_valid_config_len (buf )) {
1835
+ if (!is_valid_config_len (conn , buf )) {
1781
1836
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
1782
1837
}
1783
1838
@@ -1945,7 +2000,7 @@ static void ase_qos(struct bt_ascs_ase *ase, uint8_t cig_id, uint8_t cis_id,
1945
2000
* rsp = BT_BAP_ASCS_RSP (BT_BAP_ASCS_RSP_CODE_SUCCESS , BT_BAP_ASCS_REASON_NONE );
1946
2001
}
1947
2002
1948
- static bool is_valid_qos_len (struct net_buf_simple * buf )
2003
+ static bool is_valid_qos_len (struct bt_conn * conn , struct net_buf_simple * buf )
1949
2004
{
1950
2005
const struct bt_ascs_qos_op * op ;
1951
2006
struct net_buf_simple_state state ;
@@ -1959,8 +2014,7 @@ static bool is_valid_qos_len(struct net_buf_simple *buf)
1959
2014
}
1960
2015
1961
2016
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
1962
- if (op -> num_ases < 1 ) {
1963
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2017
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
1964
2018
return false;
1965
2019
}
1966
2020
@@ -1986,7 +2040,7 @@ static ssize_t ascs_qos(struct bt_conn *conn, struct net_buf_simple *buf)
1986
2040
{
1987
2041
const struct bt_ascs_qos_op * req ;
1988
2042
1989
- if (!is_valid_qos_len (buf )) {
2043
+ if (!is_valid_qos_len (conn , buf )) {
1990
2044
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
1991
2045
}
1992
2046
@@ -2327,7 +2381,7 @@ static int ase_enable(struct bt_ascs_ase *ase, struct bt_ascs_metadata *meta)
2327
2381
return 0 ;
2328
2382
}
2329
2383
2330
- static bool is_valid_enable_len (struct net_buf_simple * buf )
2384
+ static bool is_valid_enable_len (struct bt_conn * conn , struct net_buf_simple * buf )
2331
2385
{
2332
2386
const struct bt_ascs_enable_op * op ;
2333
2387
struct net_buf_simple_state state ;
@@ -2340,8 +2394,7 @@ static bool is_valid_enable_len(struct net_buf_simple *buf)
2340
2394
}
2341
2395
2342
2396
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2343
- if (op -> num_ases < 1 ) {
2344
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2397
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
2345
2398
return false;
2346
2399
}
2347
2400
@@ -2378,7 +2431,7 @@ static ssize_t ascs_enable(struct bt_conn *conn, struct net_buf_simple *buf)
2378
2431
struct bt_ascs_metadata * meta ;
2379
2432
int i ;
2380
2433
2381
- if (!is_valid_enable_len (buf )) {
2434
+ if (!is_valid_enable_len (conn , buf )) {
2382
2435
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2383
2436
}
2384
2437
@@ -2472,7 +2525,7 @@ static void ase_start(struct bt_ascs_ase *ase)
2472
2525
ascs_cp_rsp_success (ASE_ID (ase ));
2473
2526
}
2474
2527
2475
- static bool is_valid_start_len (struct net_buf_simple * buf )
2528
+ static bool is_valid_start_len (struct bt_conn * conn , struct net_buf_simple * buf )
2476
2529
{
2477
2530
const struct bt_ascs_start_op * op ;
2478
2531
struct net_buf_simple_state state ;
@@ -2485,8 +2538,7 @@ static bool is_valid_start_len(struct net_buf_simple *buf)
2485
2538
}
2486
2539
2487
2540
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2488
- if (op -> num_ases < 1 ) {
2489
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2541
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
2490
2542
return false;
2491
2543
}
2492
2544
@@ -2505,7 +2557,7 @@ static ssize_t ascs_start(struct bt_conn *conn, struct net_buf_simple *buf)
2505
2557
const struct bt_ascs_start_op * req ;
2506
2558
int i ;
2507
2559
2508
- if (!is_valid_start_len (buf )) {
2560
+ if (!is_valid_start_len (conn , buf )) {
2509
2561
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2510
2562
}
2511
2563
@@ -2555,7 +2607,7 @@ static ssize_t ascs_start(struct bt_conn *conn, struct net_buf_simple *buf)
2555
2607
return buf -> size ;
2556
2608
}
2557
2609
2558
- static bool is_valid_disable_len (struct net_buf_simple * buf )
2610
+ static bool is_valid_disable_len (struct bt_conn * conn , struct net_buf_simple * buf )
2559
2611
{
2560
2612
const struct bt_ascs_disable_op * op ;
2561
2613
struct net_buf_simple_state state ;
@@ -2568,8 +2620,7 @@ static bool is_valid_disable_len(struct net_buf_simple *buf)
2568
2620
}
2569
2621
2570
2622
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2571
- if (op -> num_ases < 1 ) {
2572
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2623
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
2573
2624
return false;
2574
2625
}
2575
2626
@@ -2587,7 +2638,7 @@ static ssize_t ascs_disable(struct bt_conn *conn, struct net_buf_simple *buf)
2587
2638
{
2588
2639
const struct bt_ascs_disable_op * req ;
2589
2640
2590
- if (!is_valid_disable_len (buf )) {
2641
+ if (!is_valid_disable_len (conn , buf )) {
2591
2642
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2592
2643
}
2593
2644
@@ -2685,7 +2736,7 @@ static void ase_stop(struct bt_ascs_ase *ase)
2685
2736
ascs_cp_rsp_success (ASE_ID (ase ));
2686
2737
}
2687
2738
2688
- static bool is_valid_stop_len (struct net_buf_simple * buf )
2739
+ static bool is_valid_stop_len (struct bt_conn * conn , struct net_buf_simple * buf )
2689
2740
{
2690
2741
const struct bt_ascs_stop_op * op ;
2691
2742
struct net_buf_simple_state state ;
@@ -2698,7 +2749,7 @@ static bool is_valid_stop_len(struct net_buf_simple *buf)
2698
2749
}
2699
2750
2700
2751
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2701
- if (op -> num_ases < 1 ) {
2752
+ if (op -> num_ases < 1U ) {
2702
2753
LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2703
2754
return false;
2704
2755
}
@@ -2718,7 +2769,7 @@ static ssize_t ascs_stop(struct bt_conn *conn, struct net_buf_simple *buf)
2718
2769
const struct bt_ascs_start_op * req ;
2719
2770
int i ;
2720
2771
2721
- if (!is_valid_stop_len (buf )) {
2772
+ if (!is_valid_stop_len (conn , buf )) {
2722
2773
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2723
2774
}
2724
2775
@@ -2768,7 +2819,7 @@ static ssize_t ascs_stop(struct bt_conn *conn, struct net_buf_simple *buf)
2768
2819
return buf -> size ;
2769
2820
}
2770
2821
2771
- static bool is_valid_metadata_len (struct net_buf_simple * buf )
2822
+ static bool is_valid_metadata_len (struct bt_conn * conn , struct net_buf_simple * buf )
2772
2823
{
2773
2824
const struct bt_ascs_metadata_op * op ;
2774
2825
struct net_buf_simple_state state ;
@@ -2781,8 +2832,7 @@ static bool is_valid_metadata_len(struct net_buf_simple *buf)
2781
2832
}
2782
2833
2783
2834
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2784
- if (op -> num_ases < 1 ) {
2785
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2835
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
2786
2836
return false;
2787
2837
}
2788
2838
@@ -2819,7 +2869,7 @@ static ssize_t ascs_metadata(struct bt_conn *conn, struct net_buf_simple *buf)
2819
2869
struct bt_ascs_metadata * meta ;
2820
2870
int i ;
2821
2871
2822
- if (!is_valid_metadata_len (buf )) {
2872
+ if (!is_valid_metadata_len (conn , buf )) {
2823
2873
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2824
2874
}
2825
2875
@@ -2862,7 +2912,7 @@ static ssize_t ascs_metadata(struct bt_conn *conn, struct net_buf_simple *buf)
2862
2912
return buf -> size ;
2863
2913
}
2864
2914
2865
- static bool is_valid_release_len (struct net_buf_simple * buf )
2915
+ static bool is_valid_release_len (struct bt_conn * conn , struct net_buf_simple * buf )
2866
2916
{
2867
2917
const struct bt_ascs_release_op * op ;
2868
2918
struct net_buf_simple_state state ;
@@ -2875,8 +2925,7 @@ static bool is_valid_release_len(struct net_buf_simple *buf)
2875
2925
}
2876
2926
2877
2927
op = net_buf_simple_pull_mem (buf , sizeof (* op ));
2878
- if (op -> num_ases < 1 ) {
2879
- LOG_WRN ("Number_of_ASEs parameter value is less than 1" );
2928
+ if (!is_valid_num_ases (conn , op -> num_ases )) {
2880
2929
return false;
2881
2930
}
2882
2931
@@ -2895,7 +2944,7 @@ static ssize_t ascs_release(struct bt_conn *conn, struct net_buf_simple *buf)
2895
2944
const struct bt_ascs_release_op * req ;
2896
2945
int i ;
2897
2946
2898
- if (!is_valid_release_len (buf )) {
2947
+ if (!is_valid_release_len (conn , buf )) {
2899
2948
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
2900
2949
}
2901
2950
@@ -3003,7 +3052,7 @@ static ssize_t ascs_cp_write(struct bt_conn *conn,
3003
3052
}
3004
3053
3005
3054
respond :
3006
- control_point_notify (conn , rsp_buf .data , rsp_buf .len );
3055
+ control_point_notify (conn , cp_rsp_buf .data , cp_rsp_buf .len );
3007
3056
3008
3057
return len ;
3009
3058
}
0 commit comments