@@ -19,14 +19,15 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_BTTESTER_LOG_LEVEL);
1919
2020#include "btp/btp.h"
2121
22- #define DATA_MTU_INITIAL 128
23- #define DATA_MTU 256
24- #define DATA_BUF_SIZE BT_L2CAP_SDU_BUF_SIZE(DATA_MTU)
22+ #define L2CAP_MPS 96
23+ #define DATA_MTU (3 * L2CAP_MPS)
24+ #define DATA_MTU_INITIAL (2 * L2CAP_MPS)
25+
2526#define CHANNELS 2
2627#define SERVERS 1
2728
28- NET_BUF_POOL_FIXED_DEFINE (data_pool , CHANNELS , DATA_BUF_SIZE , CONFIG_BT_CONN_TX_USER_DATA_SIZE ,
29- NULL );
29+ NET_BUF_POOL_FIXED_DEFINE (data_pool , CHANNELS , BT_L2CAP_SDU_BUF_SIZE ( DATA_MTU ) ,
30+ CONFIG_BT_CONN_TX_USER_DATA_SIZE , NULL );
3031
3132static bool authorize_flag ;
3233static uint8_t req_keysize ;
@@ -36,18 +37,51 @@ static struct channel {
3637 struct bt_l2cap_le_chan le ;
3738 bool in_use ;
3839 bool hold_credit ;
40+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
41+ unsigned int pending_credits ;
42+ uint8_t recv_cb_buf [DATA_MTU + sizeof (struct btp_l2cap_data_received_ev )];
43+ #else
3944 struct net_buf * pending_credit ;
45+ #endif
4046} channels [CHANNELS ];
4147
4248/* TODO Extend to support multiple servers */
4349static struct bt_l2cap_server servers [SERVERS ];
4450
51+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
52+ static void seg_recv_cb (struct bt_l2cap_chan * l2cap_chan , size_t sdu_len , off_t seg_offset ,
53+ struct net_buf_simple * seg )
54+ {
55+ struct btp_l2cap_data_received_ev * ev ;
56+ struct bt_l2cap_le_chan * l2cap_le_chan =
57+ CONTAINER_OF (l2cap_chan , struct bt_l2cap_le_chan , chan );
58+ struct channel * chan = CONTAINER_OF (l2cap_le_chan , struct channel , le );
59+
60+ ev = (void * )chan -> recv_cb_buf ;
61+ memcpy (& ev -> data [seg_offset ], seg -> data , seg -> len );
62+
63+ /* complete SDU received */
64+ if (seg_offset + seg -> len == sdu_len ) {
65+ ev -> chan_id = chan -> chan_id ;
66+ ev -> data_length = sys_cpu_to_le16 (sdu_len );
67+
68+ tester_event (BTP_SERVICE_ID_L2CAP , BTP_L2CAP_EV_DATA_RECEIVED , chan -> recv_cb_buf ,
69+ sizeof (* ev ) + sdu_len );
70+ }
71+
72+ if (chan -> hold_credit ) {
73+ chan -> pending_credits ++ ;
74+ } else {
75+ bt_l2cap_chan_give_credits (l2cap_chan , 1 );
76+ }
77+ }
78+ #else
4579static struct net_buf * alloc_buf_cb (struct bt_l2cap_chan * chan )
4680{
4781 return net_buf_alloc (& data_pool , K_FOREVER );
4882}
4983
50- static uint8_t recv_cb_buf [DATA_BUF_SIZE + sizeof (struct btp_l2cap_data_received_ev )];
84+ static uint8_t recv_cb_buf [DATA_MTU + sizeof (struct btp_l2cap_data_received_ev )];
5185
5286static int recv_cb (struct bt_l2cap_chan * l2cap_chan , struct net_buf * buf )
5387{
@@ -73,6 +107,7 @@ static int recv_cb(struct bt_l2cap_chan *l2cap_chan, struct net_buf *buf)
73107
74108 return 0 ;
75109}
110+ #endif
76111
77112static void connected_cb (struct bt_l2cap_chan * l2cap_chan )
78113{
@@ -111,11 +146,13 @@ static void disconnected_cb(struct bt_l2cap_chan *l2cap_chan)
111146 struct channel * chan = CONTAINER_OF (l2cap_le_chan , struct channel , le );
112147 struct bt_conn_info info ;
113148
149+ #if !defined(CONFIG_BT_L2CAP_SEG_RECV )
114150 /* release netbuf on premature disconnection */
115151 if (chan -> pending_credit ) {
116152 net_buf_unref (chan -> pending_credit );
117153 chan -> pending_credit = NULL ;
118154 }
155+ #endif
119156
120157 (void )memset (& ev , 0 , sizeof (struct btp_l2cap_disconnected_ev ));
121158
@@ -160,12 +197,16 @@ static void reconfigured_cb(struct bt_l2cap_chan *l2cap_chan)
160197#endif
161198
162199static const struct bt_l2cap_chan_ops l2cap_ops = {
163- .alloc_buf = alloc_buf_cb ,
164- .recv = recv_cb ,
165- .connected = connected_cb ,
166- .disconnected = disconnected_cb ,
200+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
201+ .seg_recv = seg_recv_cb ,
202+ #else
203+ .alloc_buf = alloc_buf_cb ,
204+ .recv = recv_cb ,
205+ #endif
206+ .connected = connected_cb ,
207+ .disconnected = disconnected_cb ,
167208#if defined(CONFIG_BT_L2CAP_ECRED )
168- .reconfigured = reconfigured_cb ,
209+ .reconfigured = reconfigured_cb ,
169210#endif
170211};
171212
@@ -222,10 +263,15 @@ static uint8_t connect(const void *cmd, uint16_t cmd_len,
222263 }
223264 chan -> le .chan .ops = & l2cap_ops ;
224265 chan -> le .rx .mtu = mtu ;
266+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
267+ chan -> le .rx .mps = L2CAP_MPS ;
268+ #endif
225269 rp -> chan_id [i ] = chan -> chan_id ;
226270 allocated_channels [i ] = & chan -> le .chan ;
227271
228272 chan -> hold_credit = cp -> options & BTP_L2CAP_CONNECT_OPT_HOLD_CREDIT ;
273+
274+ bt_l2cap_chan_give_credits (& chan -> le .chan , 1 );
229275 }
230276
231277 if (cp -> num == 1 && !ecfc ) {
@@ -289,6 +335,7 @@ static uint8_t reconfigure(const void *cmd, uint16_t cmd_len,
289335{
290336 const struct btp_l2cap_reconfigure_cmd * cp = cmd ;
291337 uint16_t mtu ;
338+ uint16_t mps ;
292339 struct bt_conn * conn ;
293340 int err ;
294341 struct bt_l2cap_chan * reconf_channels [CHANNELS + 1 ] = {};
@@ -321,7 +368,8 @@ static uint8_t reconfigure(const void *cmd, uint16_t cmd_len,
321368 return BTP_STATUS_FAILED ;
322369 }
323370
324- err = bt_l2cap_ecred_chan_reconfigure (reconf_channels , mtu );
371+ mps = MIN (L2CAP_MPS , BT_L2CAP_RX_MTU );
372+ err = bt_l2cap_ecred_chan_reconfigure_explicit (reconf_channels , mtu , mps );
325373 if (err ) {
326374 bt_conn_unref (conn );
327375 return BTP_STATUS_FAILED ;
@@ -454,9 +502,14 @@ static int accept(struct bt_conn *conn, struct bt_l2cap_server *server,
454502
455503 chan -> le .chan .ops = & l2cap_ops ;
456504 chan -> le .rx .mtu = DATA_MTU_INITIAL ;
505+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
506+ chan -> le .rx .mps = L2CAP_MPS ;
507+ #endif
457508
458509 * l2cap_chan = & chan -> le .chan ;
459510
511+ bt_l2cap_chan_give_credits (& chan -> le .chan , 1 );
512+
460513 return 0 ;
461514}
462515
@@ -524,7 +577,15 @@ static uint8_t credits(const void *cmd, uint16_t cmd_len,
524577 if (!chan -> in_use ) {
525578 return BTP_STATUS_FAILED ;
526579 }
580+ #if defined(CONFIG_BT_L2CAP_SEG_RECV )
581+ if (chan -> pending_credits ) {
582+ if (bt_l2cap_chan_give_credits (& chan -> le .chan , chan -> pending_credits ) < 0 ) {
583+ return BTP_STATUS_FAILED ;
584+ }
527585
586+ chan -> pending_credits = 0 ;
587+ }
588+ #else
528589 if (chan -> pending_credit ) {
529590 if (bt_l2cap_chan_recv_complete (& chan -> le .chan ,
530591 chan -> pending_credit ) < 0 ) {
@@ -533,6 +594,7 @@ static uint8_t credits(const void *cmd, uint16_t cmd_len,
533594
534595 chan -> pending_credit = NULL ;
535596 }
597+ #endif
536598
537599 return BTP_STATUS_SUCCESS ;
538600}
0 commit comments