1 /* rfcomm.c - RFCOMM handling */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <ble_os.h>
10 #include <string.h>
11 #include <bt_errno.h>
12 #include <atomic.h>
13 #include <misc/byteorder.h>
14 #include <misc/util.h>
15 #include <misc/stack.h>
16 
17 #include <bluetooth/hci.h>
18 #include <bluetooth/bluetooth.h>
19 #include <bluetooth/conn.h>
20 #include <bluetooth/hci_driver.h>
21 #include <bluetooth/l2cap.h>
22 
23 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_RFCOMM)
24 #define LOG_MODULE_NAME bt_rfcomm
25 #include "common/log.h"
26 
27 #include <bluetooth/rfcomm.h>
28 
29 #include "hci_core.h"
30 #include "conn_internal.h"
31 #include "l2cap_internal.h"
32 #include "rfcomm_internal.h"
33 
34 #define RFCOMM_CHANNEL_START	0x01
35 #define RFCOMM_CHANNEL_END	0x1e
36 
37 #define RFCOMM_MIN_MTU		BT_RFCOMM_SIG_MIN_MTU
38 #define RFCOMM_DEFAULT_MTU	127
39 
40 #if defined(CONFIG_BT_HCI_ACL_FLOW_CONTROL)
41 #define RFCOMM_MAX_CREDITS		(CONFIG_BT_ACL_RX_COUNT - 1)
42 #else
43 #define RFCOMM_MAX_CREDITS		(CONFIG_BT_RX_BUF_COUNT - 1)
44 #endif
45 
46 #define RFCOMM_CREDITS_THRESHOLD	(RFCOMM_MAX_CREDITS / 2)
47 #define RFCOMM_DEFAULT_CREDIT		RFCOMM_MAX_CREDITS
48 
49 #define RFCOMM_CONN_TIMEOUT     K_SECONDS(60)
50 #define RFCOMM_DISC_TIMEOUT     K_SECONDS(20)
51 #define RFCOMM_IDLE_TIMEOUT     K_SECONDS(2)
52 
53 #define DLC_RTX(_w) CONTAINER_OF(_w, struct bt_rfcomm_dlc, rtx_work)
54 
55 #define SESSION_RTX(_w) CONTAINER_OF(_w, struct bt_rfcomm_session, rtx_work)
56 
57 static struct bt_rfcomm_server *servers;
58 
59 /* Pool for dummy buffers to wake up the tx threads */
60 NET_BUF_POOL_DEFINE(dummy_pool, CONFIG_BT_MAX_CONN, 0, 0, NULL);
61 
62 #define RFCOMM_SESSION(_ch) CONTAINER_OF(_ch, \
63 					 struct bt_rfcomm_session, br_chan.chan)
64 
65 static struct bt_rfcomm_session bt_rfcomm_pool[CONFIG_BT_MAX_CONN];
66 
67 /* reversed, 8-bit, poly=0x07 */
68 static const u8_t rfcomm_crc_table[256] = {
69 	0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75,
70 	0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b,
71 	0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69,
72 	0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67,
73 
74 	0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d,
75 	0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43,
76 	0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51,
77 	0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f,
78 
79 	0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05,
80 	0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b,
81 	0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19,
82 	0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17,
83 
84 	0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d,
85 	0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33,
86 	0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21,
87 	0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f,
88 
89 	0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95,
90 	0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b,
91 	0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89,
92 	0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87,
93 
94 	0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad,
95 	0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3,
96 	0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1,
97 	0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf,
98 
99 	0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5,
100 	0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb,
101 	0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9,
102 	0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7,
103 
104 	0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd,
105 	0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3,
106 	0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1,
107 	0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf
108 };
109 
rfcomm_calc_fcs(u16_t len,const u8_t * data)110 static u8_t rfcomm_calc_fcs(u16_t len, const u8_t *data)
111 {
112 	u8_t fcs = 0xff;
113 
114 	while (len--) {
115 		fcs = rfcomm_crc_table[fcs ^ *data++];
116 	}
117 
118 	/* Ones compliment */
119 	return (0xff - fcs);
120 }
121 
rfcomm_check_fcs(u16_t len,const u8_t * data,u8_t recvd_fcs)122 static bool rfcomm_check_fcs(u16_t len, const u8_t *data,
123 			     u8_t recvd_fcs)
124 {
125 	u8_t fcs = 0xff;
126 
127 	while (len--) {
128 		fcs = rfcomm_crc_table[fcs ^ *data++];
129 	}
130 
131 	/* Ones compliment */
132 	fcs = rfcomm_crc_table[fcs ^ recvd_fcs];
133 
134 	/*0xCF is the reversed order of 11110011.*/
135 	return (fcs == 0xcf);
136 }
137 
rfcomm_dlcs_lookup_dlci(struct bt_rfcomm_dlc * dlcs,u8_t dlci)138 static struct bt_rfcomm_dlc *rfcomm_dlcs_lookup_dlci(struct bt_rfcomm_dlc *dlcs,
139 						     u8_t dlci)
140 {
141 	for (; dlcs; dlcs = dlcs->_next) {
142 		if (dlcs->dlci == dlci) {
143 			return dlcs;
144 		}
145 	}
146 
147 	return NULL;
148 }
149 
rfcomm_dlcs_remove_dlci(struct bt_rfcomm_dlc * dlcs,u8_t dlci)150 static struct bt_rfcomm_dlc *rfcomm_dlcs_remove_dlci(struct bt_rfcomm_dlc *dlcs,
151 						     u8_t dlci)
152 {
153 	struct bt_rfcomm_dlc *tmp;
154 
155 	if (!dlcs) {
156 		return NULL;
157 	}
158 
159 	/* If first node is the one to be removed */
160 	if (dlcs->dlci == dlci) {
161 		dlcs->session->dlcs = dlcs->_next;
162 		return dlcs;
163 	}
164 
165 	for (tmp = dlcs, dlcs = dlcs->_next; dlcs; dlcs = dlcs->_next) {
166 		if (dlcs->dlci == dlci) {
167 			tmp->_next = dlcs->_next;
168 			return dlcs;
169 		}
170 		tmp = dlcs;
171 	}
172 
173 	return NULL;
174 }
175 
rfcomm_server_lookup_channel(u8_t channel)176 static struct bt_rfcomm_server *rfcomm_server_lookup_channel(u8_t channel)
177 {
178 	struct bt_rfcomm_server *server;
179 
180 	for (server = servers; server; server = server->_next) {
181 		if (server->channel == channel) {
182 			return server;
183 		}
184 	}
185 
186 	return NULL;
187 }
188 
189 static struct bt_rfcomm_session *
rfcomm_sessions_lookup_bt_conn(struct bt_conn * conn)190 rfcomm_sessions_lookup_bt_conn(struct bt_conn *conn)
191 {
192 	int i;
193 
194 	for (i = 0; i < ARRAY_SIZE(bt_rfcomm_pool); i++) {
195 		struct bt_rfcomm_session *session = &bt_rfcomm_pool[i];
196 
197 		if (session->br_chan.chan.conn == conn) {
198 			return session;
199 		}
200 	}
201 
202 	return NULL;
203 }
204 
bt_rfcomm_server_register(struct bt_rfcomm_server * server)205 int bt_rfcomm_server_register(struct bt_rfcomm_server *server)
206 {
207 	if (server->channel < RFCOMM_CHANNEL_START ||
208 	    server->channel > RFCOMM_CHANNEL_END || !server->accept) {
209 		return -EINVAL;
210 	}
211 
212 	/* Check if given channel is already in use */
213 	if (rfcomm_server_lookup_channel(server->channel)) {
214 		BT_DBG("Channel already registered");
215 		return -EADDRINUSE;
216 	}
217 
218 	BT_DBG("Channel 0x%02x", server->channel);
219 
220 	server->_next = servers;
221 	servers = server;
222 
223 	return 0;
224 }
225 
rfcomm_dlc_tx_give_credits(struct bt_rfcomm_dlc * dlc,u8_t credits)226 static void rfcomm_dlc_tx_give_credits(struct bt_rfcomm_dlc *dlc,
227 				       u8_t credits)
228 {
229 	BT_DBG("dlc %p credits %u", dlc, credits);
230 
231 	while (credits--) {
232 		k_sem_give(&dlc->tx_credits);
233 	}
234 
235 	BT_DBG("dlc %p updated credits %u", dlc,
236 	       k_sem_count_get(&dlc->tx_credits));
237 }
238 
rfcomm_dlc_destroy(struct bt_rfcomm_dlc * dlc)239 static void rfcomm_dlc_destroy(struct bt_rfcomm_dlc *dlc)
240 {
241 	BT_DBG("dlc %p", dlc);
242 
243 	k_delayed_work_cancel(&dlc->rtx_work);
244 	dlc->state = BT_RFCOMM_STATE_IDLE;
245 	dlc->session = NULL;
246 
247 	if (dlc->ops && dlc->ops->disconnected) {
248 		dlc->ops->disconnected(dlc);
249 	}
250 }
251 
rfcomm_dlc_disconnect(struct bt_rfcomm_dlc * dlc)252 static void rfcomm_dlc_disconnect(struct bt_rfcomm_dlc *dlc)
253 {
254 	u8_t old_state = dlc->state;
255 
256 	BT_DBG("dlc %p", dlc);
257 
258 	if (dlc->state == BT_RFCOMM_STATE_DISCONNECTED) {
259 		return;
260 	}
261 
262 	dlc->state = BT_RFCOMM_STATE_DISCONNECTED;
263 
264 	switch (old_state) {
265 	case BT_RFCOMM_STATE_CONNECTED:
266 		/* Queue a dummy buffer to wake up and stop the
267 		 * tx thread for states where it was running.
268 		 */
269 		net_buf_put(&dlc->tx_queue,
270 			    net_buf_alloc(&dummy_pool, K_NO_WAIT));
271 
272 		/* There could be a writer waiting for credits so return a
273 		 * dummy credit to wake it up.
274 		 */
275 		rfcomm_dlc_tx_give_credits(dlc, 1);
276 		k_sem_give(&dlc->session->fc);
277 		break;
278 	default:
279 		rfcomm_dlc_destroy(dlc);
280 		break;
281 	}
282 }
283 
rfcomm_session_disconnected(struct bt_rfcomm_session * session)284 static void rfcomm_session_disconnected(struct bt_rfcomm_session *session)
285 {
286 	struct bt_rfcomm_dlc *dlc;
287 
288 	BT_DBG("Session %p", session);
289 
290 	if (session->state == BT_RFCOMM_STATE_DISCONNECTED) {
291 		return;
292 	}
293 
294 	for (dlc = session->dlcs; dlc;) {
295 		struct bt_rfcomm_dlc *next;
296 
297 		/* prefetch since disconnected callback may cleanup */
298 		next = dlc->_next;
299 		dlc->_next = NULL;
300 
301 		rfcomm_dlc_disconnect(dlc);
302 
303 		dlc = next;
304 	}
305 
306 	session->state = BT_RFCOMM_STATE_DISCONNECTED;
307 	session->dlcs = NULL;
308 }
309 
bt_rfcomm_create_pdu(struct net_buf_pool * pool)310 struct net_buf *bt_rfcomm_create_pdu(struct net_buf_pool *pool)
311 {
312 	/* Length in RFCOMM header can be 2 bytes depending on length of user
313 	 * data
314 	 */
315 	return bt_conn_create_pdu(pool,
316 				  sizeof(struct bt_l2cap_hdr) +
317 				  sizeof(struct bt_rfcomm_hdr) + 1);
318 }
319 
rfcomm_send_sabm(struct bt_rfcomm_session * session,u8_t dlci)320 static int rfcomm_send_sabm(struct bt_rfcomm_session *session, u8_t dlci)
321 {
322 	struct bt_rfcomm_hdr *hdr;
323 	struct net_buf *buf;
324 	u8_t cr, fcs;
325 
326 	buf = bt_l2cap_create_pdu(NULL, 0);
327 
328 	hdr = net_buf_add(buf, sizeof(*hdr));
329 	cr = BT_RFCOMM_CMD_CR(session->role);
330 	hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
331 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_SABM, BT_RFCOMM_PF_NON_UIH);
332 	hdr->length = BT_RFCOMM_SET_LEN_8(0);
333 
334 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_NON_UIH, buf->data);
335 	net_buf_add_u8(buf, fcs);
336 
337 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
338 }
339 
rfcomm_send_disc(struct bt_rfcomm_session * session,u8_t dlci)340 static int rfcomm_send_disc(struct bt_rfcomm_session *session, u8_t dlci)
341 {
342 	struct bt_rfcomm_hdr *hdr;
343 	struct net_buf *buf;
344 	u8_t fcs, cr;
345 
346 	BT_DBG("dlci %d", dlci);
347 
348 	buf = bt_l2cap_create_pdu(NULL, 0);
349 
350 	hdr = net_buf_add(buf, sizeof(*hdr));
351 	cr = BT_RFCOMM_RESP_CR(session->role);
352 	hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
353 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_DISC, BT_RFCOMM_PF_NON_UIH);
354 	hdr->length = BT_RFCOMM_SET_LEN_8(0);
355 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_NON_UIH, buf->data);
356 	net_buf_add_u8(buf, fcs);
357 
358 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
359 }
360 
rfcomm_session_disconnect(struct bt_rfcomm_session * session)361 static void rfcomm_session_disconnect(struct bt_rfcomm_session *session)
362 {
363 	if (session->dlcs) {
364 		return;
365 	}
366 
367 	session->state = BT_RFCOMM_STATE_DISCONNECTING;
368 	rfcomm_send_disc(session, 0);
369 	k_delayed_work_submit(&session->rtx_work, RFCOMM_DISC_TIMEOUT);
370 }
371 
rfcomm_make_uih_msg(struct bt_rfcomm_session * session,u8_t cr,u8_t type,u8_t len)372 static struct net_buf *rfcomm_make_uih_msg(struct bt_rfcomm_session *session,
373 					   u8_t cr, u8_t type,
374 					   u8_t len)
375 {
376 	struct bt_rfcomm_hdr *hdr;
377 	struct bt_rfcomm_msg_hdr *msg_hdr;
378 	struct net_buf *buf;
379 	u8_t hdr_cr;
380 
381 	buf = bt_l2cap_create_pdu(NULL, 0);
382 
383 	hdr = net_buf_add(buf, sizeof(*hdr));
384 	hdr_cr = BT_RFCOMM_UIH_CR(session->role);
385 	hdr->address = BT_RFCOMM_SET_ADDR(0, hdr_cr);
386 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH, BT_RFCOMM_PF_UIH);
387 	hdr->length = BT_RFCOMM_SET_LEN_8(sizeof(*msg_hdr) + len);
388 
389 	msg_hdr = net_buf_add(buf, sizeof(*msg_hdr));
390 	msg_hdr->type = BT_RFCOMM_SET_MSG_TYPE(type, cr);
391 	msg_hdr->len = BT_RFCOMM_SET_LEN_8(len);
392 
393 	return buf;
394 }
395 
rfcomm_connected(struct bt_l2cap_chan * chan)396 static void rfcomm_connected(struct bt_l2cap_chan *chan)
397 {
398 	struct bt_rfcomm_session *session = RFCOMM_SESSION(chan);
399 
400 	BT_DBG("Session %p", session);
401 
402 	/* Need to include UIH header and FCS*/
403 	session->mtu = MIN(session->br_chan.rx.mtu,
404 			   session->br_chan.tx.mtu) -
405 			   BT_RFCOMM_HDR_SIZE + BT_RFCOMM_FCS_SIZE;
406 
407 	if (session->state == BT_RFCOMM_STATE_CONNECTING) {
408 		rfcomm_send_sabm(session, 0);
409 	}
410 }
411 
rfcomm_disconnected(struct bt_l2cap_chan * chan)412 static void rfcomm_disconnected(struct bt_l2cap_chan *chan)
413 {
414 	struct bt_rfcomm_session *session = RFCOMM_SESSION(chan);
415 
416 	BT_DBG("Session %p", session);
417 
418 	k_delayed_work_cancel(&session->rtx_work);
419 	rfcomm_session_disconnected(session);
420 	session->state = BT_RFCOMM_STATE_IDLE;
421 }
422 
rfcomm_dlc_rtx_timeout(struct k_work * work)423 static void rfcomm_dlc_rtx_timeout(struct k_work *work)
424 {
425 	struct bt_rfcomm_dlc *dlc = DLC_RTX(work);
426 	struct bt_rfcomm_session *session = dlc->session;
427 
428 	BT_WARN("dlc %p state %d timeout", dlc, dlc->state);
429 
430 	rfcomm_dlcs_remove_dlci(session->dlcs, dlc->dlci);
431 	rfcomm_dlc_disconnect(dlc);
432 	rfcomm_session_disconnect(session);
433 }
434 
rfcomm_dlc_init(struct bt_rfcomm_dlc * dlc,struct bt_rfcomm_session * session,u8_t dlci,bt_rfcomm_role_t role)435 static void rfcomm_dlc_init(struct bt_rfcomm_dlc *dlc,
436 			    struct bt_rfcomm_session *session,
437 			    u8_t dlci,
438 			    bt_rfcomm_role_t role)
439 {
440 	BT_DBG("dlc %p", dlc);
441 
442 	dlc->dlci = dlci;
443 	dlc->session = session;
444 	dlc->rx_credit = RFCOMM_DEFAULT_CREDIT;
445 	dlc->state = BT_RFCOMM_STATE_INIT;
446 	dlc->role = role;
447 	k_delayed_work_init(&dlc->rtx_work, rfcomm_dlc_rtx_timeout);
448 
449 	/* Start a conn timer which includes auth as well */
450 	k_delayed_work_submit(&dlc->rtx_work, RFCOMM_CONN_TIMEOUT);
451 
452 	dlc->_next = session->dlcs;
453 	session->dlcs = dlc;
454 }
455 
rfcomm_dlc_accept(struct bt_rfcomm_session * session,u8_t dlci)456 static struct bt_rfcomm_dlc *rfcomm_dlc_accept(struct bt_rfcomm_session *session,
457 					       u8_t dlci)
458 {
459 	struct bt_rfcomm_server *server;
460 	struct bt_rfcomm_dlc *dlc;
461 	u8_t channel;
462 
463 	channel = BT_RFCOMM_GET_CHANNEL(dlci);
464 	server = rfcomm_server_lookup_channel(channel);
465 	if (!server) {
466 		BT_ERR("Server Channel not registered");
467 		return NULL;
468 	}
469 
470 	if (server->accept(session->br_chan.chan.conn, &dlc) < 0) {
471 		BT_DBG("Incoming connection rejected");
472 		return NULL;
473 	}
474 
475 	if (!BT_RFCOMM_CHECK_MTU(dlc->mtu)) {
476 		rfcomm_dlc_destroy(dlc);
477 		return NULL;
478 	}
479 
480 	rfcomm_dlc_init(dlc, session, dlci, BT_RFCOMM_ROLE_ACCEPTOR);
481 	dlc->mtu = MIN(dlc->mtu, session->mtu);
482 
483 	return dlc;
484 }
485 
rfcomm_send_dm(struct bt_rfcomm_session * session,u8_t dlci)486 static int rfcomm_send_dm(struct bt_rfcomm_session *session, u8_t dlci)
487 {
488 	struct bt_rfcomm_hdr *hdr;
489 	struct net_buf *buf;
490 	u8_t fcs, cr;
491 
492 	BT_DBG("dlci %d", dlci);
493 
494 	buf = bt_l2cap_create_pdu(NULL, 0);
495 
496 	hdr = net_buf_add(buf, sizeof(*hdr));
497 	cr = BT_RFCOMM_RESP_CR(session->role);
498 	hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
499 	/* For DM PF bit is not relevant, we set it 1 */
500 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_DM, BT_RFCOMM_PF_NON_UIH);
501 	hdr->length = BT_RFCOMM_SET_LEN_8(0);
502 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_NON_UIH, buf->data);
503 	net_buf_add_u8(buf, fcs);
504 
505 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
506 }
507 
rfcomm_check_fc(struct bt_rfcomm_dlc * dlc)508 static void rfcomm_check_fc(struct bt_rfcomm_dlc *dlc)
509 {
510 	BT_DBG("%p", dlc);
511 
512 	BT_DBG("Wait for credits or MSC FC %p", dlc);
513 	/* Wait for credits or MSC FC */
514 	k_sem_take(&dlc->tx_credits, K_FOREVER);
515 
516 	if (dlc->session->cfc == BT_RFCOMM_CFC_SUPPORTED) {
517 		return;
518 	}
519 
520 	k_sem_take(&dlc->session->fc, K_FOREVER);
521 
522 	/* Give the sems immediately so that sem will be available for all
523 	 * the bufs in the queue. It will be blocked only once all the bufs
524 	 * are sent (which will preempt this thread) and FCOFF / FC bit
525 	 * with 1, is received.
526 	 */
527 	k_sem_give(&dlc->session->fc);
528 	k_sem_give(&dlc->tx_credits);
529 }
530 
rfcomm_dlc_tx_thread(void * p1,void * p2,void * p3)531 static void rfcomm_dlc_tx_thread(void *p1, void *p2, void *p3)
532 {
533 	struct bt_rfcomm_dlc *dlc = p1;
534 	k_timeout_t timeout = K_FOREVER;
535 	struct net_buf *buf;
536 
537 	BT_DBG("Started for dlc %p", dlc);
538 
539 	while (dlc->state == BT_RFCOMM_STATE_CONNECTED ||
540 	       dlc->state == BT_RFCOMM_STATE_USER_DISCONNECT) {
541 		/* Get next packet for dlc */
542 		BT_DBG("Wait for buf %p", dlc);
543 		buf = net_buf_get(&dlc->tx_queue, timeout);
544 		/* If its dummy buffer or non user disconnect then break */
545 		if ((dlc->state != BT_RFCOMM_STATE_CONNECTED &&
546 		     dlc->state != BT_RFCOMM_STATE_USER_DISCONNECT) ||
547 		    !buf || !buf->len) {
548 			if (buf) {
549 				net_buf_unref(buf);
550 			}
551 			break;
552 		}
553 
554 		rfcomm_check_fc(dlc);
555 		if (dlc->state != BT_RFCOMM_STATE_CONNECTED &&
556 		    dlc->state != BT_RFCOMM_STATE_USER_DISCONNECT) {
557 			net_buf_unref(buf);
558 			break;
559 		}
560 
561 		if (bt_l2cap_chan_send(&dlc->session->br_chan.chan, buf) < 0) {
562 			/* This fails only if channel is disconnected */
563 			dlc->state = BT_RFCOMM_STATE_DISCONNECTED;
564 			net_buf_unref(buf);
565 			break;
566 		}
567 
568 		if (dlc->state == BT_RFCOMM_STATE_USER_DISCONNECT) {
569 			timeout = K_NO_WAIT;
570 		}
571 	}
572 
573 	BT_DBG("dlc %p disconnected - cleaning up", dlc);
574 
575 	/* Give back any allocated buffers */
576 	while ((buf = net_buf_get(&dlc->tx_queue, K_NO_WAIT))) {
577 		net_buf_unref(buf);
578 	}
579 
580 	if (dlc->state == BT_RFCOMM_STATE_USER_DISCONNECT) {
581 		dlc->state = BT_RFCOMM_STATE_DISCONNECTING;
582 	}
583 
584 	if (dlc->state == BT_RFCOMM_STATE_DISCONNECTING) {
585 		rfcomm_send_disc(dlc->session, dlc->dlci);
586 		k_delayed_work_submit(&dlc->rtx_work, RFCOMM_DISC_TIMEOUT);
587 	} else {
588 		rfcomm_dlc_destroy(dlc);
589 	}
590 
591 	BT_DBG("dlc %p exiting", dlc);
592 }
593 
rfcomm_send_ua(struct bt_rfcomm_session * session,u8_t dlci)594 static int rfcomm_send_ua(struct bt_rfcomm_session *session, u8_t dlci)
595 {
596 	struct bt_rfcomm_hdr *hdr;
597 	struct net_buf *buf;
598 	u8_t cr, fcs;
599 
600 	buf = bt_l2cap_create_pdu(NULL, 0);
601 
602 	hdr = net_buf_add(buf, sizeof(*hdr));
603 	cr = BT_RFCOMM_RESP_CR(session->role);
604 	hdr->address = BT_RFCOMM_SET_ADDR(dlci, cr);
605 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UA, BT_RFCOMM_PF_NON_UIH);
606 	hdr->length = BT_RFCOMM_SET_LEN_8(0);
607 
608 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_NON_UIH, buf->data);
609 	net_buf_add_u8(buf, fcs);
610 
611 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
612 }
613 
rfcomm_send_msc(struct bt_rfcomm_dlc * dlc,u8_t cr,u8_t v24_signal)614 static int rfcomm_send_msc(struct bt_rfcomm_dlc *dlc, u8_t cr,
615 			   u8_t v24_signal)
616 {
617 	struct bt_rfcomm_msc *msc;
618 	struct net_buf *buf;
619 	u8_t fcs;
620 
621 	buf = rfcomm_make_uih_msg(dlc->session, cr, BT_RFCOMM_MSC,
622 				  sizeof(*msc));
623 
624 	msc = net_buf_add(buf, sizeof(*msc));
625 	/* cr bit should be always 1 in MSC */
626 	msc->dlci = BT_RFCOMM_SET_ADDR(dlc->dlci, 1);
627 	msc->v24_signal = v24_signal;
628 
629 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
630 	net_buf_add_u8(buf, fcs);
631 
632 	return bt_l2cap_chan_send(&dlc->session->br_chan.chan, buf);
633 }
634 
rfcomm_send_rls(struct bt_rfcomm_dlc * dlc,u8_t cr,u8_t line_status)635 static int rfcomm_send_rls(struct bt_rfcomm_dlc *dlc, u8_t cr,
636 			   u8_t line_status)
637 {
638 	struct bt_rfcomm_rls *rls;
639 	struct net_buf *buf;
640 	u8_t fcs;
641 
642 	buf = rfcomm_make_uih_msg(dlc->session, cr, BT_RFCOMM_RLS,
643 				  sizeof(*rls));
644 
645 	rls = net_buf_add(buf, sizeof(*rls));
646 	/* cr bit should be always 1 in RLS */
647 	rls->dlci = BT_RFCOMM_SET_ADDR(dlc->dlci, 1);
648 	rls->line_status = line_status;
649 
650 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
651 	net_buf_add_u8(buf, fcs);
652 
653 	return bt_l2cap_chan_send(&dlc->session->br_chan.chan, buf);
654 }
655 
rfcomm_send_rpn(struct bt_rfcomm_session * session,u8_t cr,struct bt_rfcomm_rpn * rpn)656 static int rfcomm_send_rpn(struct bt_rfcomm_session *session, u8_t cr,
657 			   struct bt_rfcomm_rpn *rpn)
658 {
659 	struct net_buf *buf;
660 	u8_t fcs;
661 
662 	buf = rfcomm_make_uih_msg(session, cr, BT_RFCOMM_RPN, sizeof(*rpn));
663 
664 	net_buf_add_mem(buf, rpn, sizeof(*rpn));
665 
666 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
667 	net_buf_add_u8(buf, fcs);
668 
669 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
670 }
671 
rfcomm_send_test(struct bt_rfcomm_session * session,u8_t cr,u8_t * pattern,u8_t len)672 static int rfcomm_send_test(struct bt_rfcomm_session *session, u8_t cr,
673 			    u8_t *pattern, u8_t len)
674 {
675 	struct net_buf *buf;
676 	u8_t fcs;
677 
678 	buf = rfcomm_make_uih_msg(session, cr, BT_RFCOMM_TEST, len);
679 
680 	net_buf_add_mem(buf, pattern, len);
681 
682 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
683 	net_buf_add_u8(buf, fcs);
684 
685 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
686 }
687 
rfcomm_send_nsc(struct bt_rfcomm_session * session,u8_t cmd_type)688 static int rfcomm_send_nsc(struct bt_rfcomm_session *session, u8_t cmd_type)
689 {
690 	struct net_buf *buf;
691 	u8_t fcs;
692 
693 	buf = rfcomm_make_uih_msg(session, BT_RFCOMM_MSG_RESP_CR,
694 				  BT_RFCOMM_NSC, sizeof(cmd_type));
695 
696 	net_buf_add_u8(buf, cmd_type);
697 
698 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
699 	net_buf_add_u8(buf, fcs);
700 
701 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
702 }
703 
rfcomm_send_fcon(struct bt_rfcomm_session * session,u8_t cr)704 static int rfcomm_send_fcon(struct bt_rfcomm_session *session, u8_t cr)
705 {
706 	struct net_buf *buf;
707 	u8_t fcs;
708 
709 	buf = rfcomm_make_uih_msg(session, cr, BT_RFCOMM_FCON, 0);
710 
711 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
712 	net_buf_add_u8(buf, fcs);
713 
714 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
715 }
716 
rfcomm_send_fcoff(struct bt_rfcomm_session * session,u8_t cr)717 static int rfcomm_send_fcoff(struct bt_rfcomm_session *session, u8_t cr)
718 {
719 	struct net_buf *buf;
720 	u8_t fcs;
721 
722 	buf = rfcomm_make_uih_msg(session, cr, BT_RFCOMM_FCOFF, 0);
723 
724 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
725 	net_buf_add_u8(buf, fcs);
726 
727 	return bt_l2cap_chan_send(&session->br_chan.chan, buf);
728 }
729 
rfcomm_dlc_connected(struct bt_rfcomm_dlc * dlc)730 static void rfcomm_dlc_connected(struct bt_rfcomm_dlc *dlc)
731 {
732 	dlc->state = BT_RFCOMM_STATE_CONNECTED;
733 
734 	rfcomm_send_msc(dlc, BT_RFCOMM_MSG_CMD_CR, BT_RFCOMM_DEFAULT_V24_SIG);
735 
736 	if (dlc->session->cfc == BT_RFCOMM_CFC_UNKNOWN) {
737 		/* This means PN negotiation is not done for this session and
738 		 * can happen only for 1.0b device.
739 		 */
740 		dlc->session->cfc = BT_RFCOMM_CFC_NOT_SUPPORTED;
741 	}
742 
743 	if (dlc->session->cfc == BT_RFCOMM_CFC_NOT_SUPPORTED) {
744 		BT_DBG("CFC not supported %p", dlc);
745 		rfcomm_send_fcon(dlc->session, BT_RFCOMM_MSG_CMD_CR);
746 		/* Use tx_credits as binary sem for MSC FC */
747 		k_sem_init(&dlc->tx_credits, 0, 1);
748 	}
749 
750 	/* Cancel conn timer */
751 	k_delayed_work_cancel(&dlc->rtx_work);
752 
753 	k_fifo_init(&dlc->tx_queue);
754 	k_thread_create(&dlc->tx_thread, dlc->stack,
755 			K_THREAD_STACK_SIZEOF(dlc->stack),
756 			rfcomm_dlc_tx_thread, dlc, NULL, NULL, K_PRIO_COOP(7),
757 			0, K_NO_WAIT);
758 	k_thread_name_set(&dlc->tx_thread, "BT DLC");
759 
760 	if (dlc->ops && dlc->ops->connected) {
761 		dlc->ops->connected(dlc);
762 	}
763 }
764 
765 enum security_result {
766 	RFCOMM_SECURITY_PASSED,
767 	RFCOMM_SECURITY_REJECT,
768 	RFCOMM_SECURITY_PENDING
769 };
770 
rfcomm_dlc_security(struct bt_rfcomm_dlc * dlc)771 static enum security_result rfcomm_dlc_security(struct bt_rfcomm_dlc *dlc)
772 {
773 	struct bt_conn *conn = dlc->session->br_chan.chan.conn;
774 
775 	BT_DBG("dlc %p", dlc);
776 
777 	/* If current security level is greater than or equal to required
778 	 * security level  then return SUCCESS.
779 	 * For SSP devices the current security will be atleast MEDIUM
780 	 * since L2CAP is enforcing it
781 	 */
782 	if (conn->sec_level >= dlc->required_sec_level) {
783 		return RFCOMM_SECURITY_PASSED;
784 	}
785 
786 	if (!bt_conn_set_security(conn, dlc->required_sec_level)) {
787 		/* If Security elevation is initiated or in progress */
788 		return RFCOMM_SECURITY_PENDING;
789 	}
790 
791 	/* Security request failed */
792 	return RFCOMM_SECURITY_REJECT;
793 }
794 
rfcomm_dlc_drop(struct bt_rfcomm_dlc * dlc)795 static void rfcomm_dlc_drop(struct bt_rfcomm_dlc *dlc)
796 {
797 	BT_DBG("dlc %p", dlc);
798 
799 	rfcomm_dlcs_remove_dlci(dlc->session->dlcs, dlc->dlci);
800 	rfcomm_dlc_destroy(dlc);
801 }
802 
rfcomm_dlc_close(struct bt_rfcomm_dlc * dlc)803 static int rfcomm_dlc_close(struct bt_rfcomm_dlc *dlc)
804 {
805 	BT_DBG("dlc %p", dlc);
806 
807 	switch (dlc->state) {
808 	case BT_RFCOMM_STATE_SECURITY_PENDING:
809 		if (dlc->role == BT_RFCOMM_ROLE_ACCEPTOR) {
810 			rfcomm_send_dm(dlc->session, dlc->dlci);
811 		}
812 		/* Fall Through */
813 	case BT_RFCOMM_STATE_INIT:
814 		rfcomm_dlc_drop(dlc);
815 		break;
816 	case BT_RFCOMM_STATE_CONNECTING:
817 	case BT_RFCOMM_STATE_CONFIG:
818 		dlc->state = BT_RFCOMM_STATE_DISCONNECTING;
819 		rfcomm_send_disc(dlc->session, dlc->dlci);
820 		k_delayed_work_submit(&dlc->rtx_work, RFCOMM_DISC_TIMEOUT);
821 		break;
822 	case BT_RFCOMM_STATE_CONNECTED:
823 		dlc->state = BT_RFCOMM_STATE_DISCONNECTING;
824 
825 		/* Queue a dummy buffer to wake up and stop the
826 		 * tx thread.
827 		 */
828 		net_buf_put(&dlc->tx_queue,
829 			    net_buf_alloc(&dummy_pool, K_NO_WAIT));
830 
831 		/* There could be a writer waiting for credits so return a
832 		 * dummy credit to wake it up.
833 		 */
834 		rfcomm_dlc_tx_give_credits(dlc, 1);
835 		break;
836 	case BT_RFCOMM_STATE_DISCONNECTING:
837 	case BT_RFCOMM_STATE_DISCONNECTED:
838 		break;
839 	case BT_RFCOMM_STATE_IDLE:
840 	default:
841 		return -EINVAL;
842 	}
843 
844 	return 0;
845 }
846 
rfcomm_handle_sabm(struct bt_rfcomm_session * session,u8_t dlci)847 static void rfcomm_handle_sabm(struct bt_rfcomm_session *session, u8_t dlci)
848 {
849 	if (!dlci) {
850 		if (rfcomm_send_ua(session, dlci) < 0) {
851 			return;
852 		}
853 
854 		session->state = BT_RFCOMM_STATE_CONNECTED;
855 	} else {
856 		struct bt_rfcomm_dlc *dlc;
857 		enum security_result result;
858 
859 		dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, dlci);
860 		if (!dlc) {
861 			dlc = rfcomm_dlc_accept(session, dlci);
862 			if (!dlc) {
863 				rfcomm_send_dm(session, dlci);
864 				return;
865 			}
866 		}
867 
868 		result = rfcomm_dlc_security(dlc);
869 		switch (result) {
870 		case RFCOMM_SECURITY_PENDING:
871 			dlc->state = BT_RFCOMM_STATE_SECURITY_PENDING;
872 			return;
873 		case RFCOMM_SECURITY_PASSED:
874 			break;
875 		case RFCOMM_SECURITY_REJECT:
876 		default:
877 			rfcomm_send_dm(session, dlci);
878 			rfcomm_dlc_drop(dlc);
879 			return;
880 		}
881 
882 		if (rfcomm_send_ua(session, dlci) < 0) {
883 			return;
884 		}
885 
886 		/* Cancel idle timer if any */
887 		k_delayed_work_cancel(&session->rtx_work);
888 
889 		rfcomm_dlc_connected(dlc);
890 	}
891 }
892 
rfcomm_send_pn(struct bt_rfcomm_dlc * dlc,u8_t cr)893 static int rfcomm_send_pn(struct bt_rfcomm_dlc *dlc, u8_t cr)
894 {
895 	struct bt_rfcomm_pn *pn;
896 	struct net_buf *buf;
897 	u8_t fcs;
898 
899 	buf = rfcomm_make_uih_msg(dlc->session, cr, BT_RFCOMM_PN, sizeof(*pn));
900 
901 	BT_DBG("mtu %x", dlc->mtu);
902 
903 	pn = net_buf_add(buf, sizeof(*pn));
904 	pn->dlci = dlc->dlci;
905 	pn->mtu = sys_cpu_to_le16(dlc->mtu);
906 	if (dlc->state == BT_RFCOMM_STATE_CONFIG &&
907 	    (dlc->session->cfc == BT_RFCOMM_CFC_UNKNOWN ||
908 	     dlc->session->cfc == BT_RFCOMM_CFC_SUPPORTED)) {
909 		pn->credits = dlc->rx_credit;
910 		if (cr) {
911 			pn->flow_ctrl = BT_RFCOMM_PN_CFC_CMD;
912 		} else {
913 			pn->flow_ctrl = BT_RFCOMM_PN_CFC_RESP;
914 		}
915 	} else {
916 		/* If PN comes in already opened dlc or cfc not supported
917 		 * these should be 0
918 		 */
919 		pn->credits = 0U;
920 		pn->flow_ctrl = 0U;
921 	}
922 	pn->max_retrans = 0U;
923 	pn->ack_timer = 0U;
924 	pn->priority = 0U;
925 
926 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
927 	net_buf_add_u8(buf, fcs);
928 
929 	return bt_l2cap_chan_send(&dlc->session->br_chan.chan, buf);
930 }
931 
rfcomm_send_credit(struct bt_rfcomm_dlc * dlc,u8_t credits)932 static int rfcomm_send_credit(struct bt_rfcomm_dlc *dlc, u8_t credits)
933 {
934 	struct bt_rfcomm_hdr *hdr;
935 	struct net_buf *buf;
936 	u8_t fcs, cr;
937 
938 	BT_DBG("Dlc %p credits %d", dlc, credits);
939 
940 	buf = bt_l2cap_create_pdu(NULL, 0);
941 
942 	hdr = net_buf_add(buf, sizeof(*hdr));
943 	cr = BT_RFCOMM_UIH_CR(dlc->session->role);
944 	hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, cr);
945 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH,
946 					  BT_RFCOMM_PF_UIH_CREDIT);
947 	hdr->length = BT_RFCOMM_SET_LEN_8(0);
948 	net_buf_add_u8(buf, credits);
949 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
950 	net_buf_add_u8(buf, fcs);
951 
952 	return bt_l2cap_chan_send(&dlc->session->br_chan.chan, buf);
953 }
954 
rfcomm_dlc_start(struct bt_rfcomm_dlc * dlc)955 static int rfcomm_dlc_start(struct bt_rfcomm_dlc *dlc)
956 {
957 	enum security_result result;
958 
959 	BT_DBG("dlc %p", dlc);
960 
961 	result = rfcomm_dlc_security(dlc);
962 	switch (result) {
963 	case RFCOMM_SECURITY_PASSED:
964 		dlc->mtu = MIN(dlc->mtu, dlc->session->mtu);
965 		dlc->state = BT_RFCOMM_STATE_CONFIG;
966 		rfcomm_send_pn(dlc, BT_RFCOMM_MSG_CMD_CR);
967 		break;
968 	case RFCOMM_SECURITY_PENDING:
969 		dlc->state = BT_RFCOMM_STATE_SECURITY_PENDING;
970 		break;
971 	case RFCOMM_SECURITY_REJECT:
972 	default:
973 		return -EIO;
974 	}
975 
976 	return 0;
977 }
978 
rfcomm_handle_ua(struct bt_rfcomm_session * session,u8_t dlci)979 static void rfcomm_handle_ua(struct bt_rfcomm_session *session, u8_t dlci)
980 {
981 	struct bt_rfcomm_dlc *dlc, *next;
982 	int err;
983 
984 	if (!dlci) {
985 		switch (session->state) {
986 		case BT_RFCOMM_STATE_CONNECTING:
987 			session->state = BT_RFCOMM_STATE_CONNECTED;
988 			for (dlc = session->dlcs; dlc; dlc = next) {
989 				next = dlc->_next;
990 				if (dlc->role == BT_RFCOMM_ROLE_INITIATOR &&
991 				    dlc->state == BT_RFCOMM_STATE_INIT) {
992 					if (rfcomm_dlc_start(dlc) < 0) {
993 						rfcomm_dlc_drop(dlc);
994 					}
995 				}
996 			}
997 			/* Disconnect session if there is no dlcs left */
998 			rfcomm_session_disconnect(session);
999 			break;
1000 		case BT_RFCOMM_STATE_DISCONNECTING:
1001 			session->state = BT_RFCOMM_STATE_DISCONNECTED;
1002 			/* Cancel disc timer */
1003 			k_delayed_work_cancel(&session->rtx_work);
1004 			err = bt_l2cap_chan_disconnect(&session->br_chan.chan);
1005 			if (err < 0) {
1006 				session->state = BT_RFCOMM_STATE_IDLE;
1007 			}
1008 			break;
1009 		default:
1010 			break;
1011 		}
1012 	} else {
1013 		dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, dlci);
1014 		if (!dlc) {
1015 			return;
1016 		}
1017 
1018 		switch (dlc->state) {
1019 		case BT_RFCOMM_STATE_CONNECTING:
1020 			rfcomm_dlc_connected(dlc);
1021 			break;
1022 		case BT_RFCOMM_STATE_DISCONNECTING:
1023 			rfcomm_dlc_drop(dlc);
1024 			rfcomm_session_disconnect(session);
1025 			break;
1026 		default:
1027 			break;
1028 		}
1029 	}
1030 }
1031 
rfcomm_handle_dm(struct bt_rfcomm_session * session,u8_t dlci)1032 static void rfcomm_handle_dm(struct bt_rfcomm_session *session, u8_t dlci)
1033 {
1034 	struct bt_rfcomm_dlc *dlc;
1035 
1036 	BT_DBG("dlci %d", dlci);
1037 
1038 	dlc = rfcomm_dlcs_remove_dlci(session->dlcs, dlci);
1039 	if (!dlc) {
1040 		return;
1041 	}
1042 
1043 	rfcomm_dlc_disconnect(dlc);
1044 	rfcomm_session_disconnect(session);
1045 }
1046 
rfcomm_handle_msc(struct bt_rfcomm_session * session,struct net_buf * buf,u8_t cr)1047 static void rfcomm_handle_msc(struct bt_rfcomm_session *session,
1048 			      struct net_buf *buf, u8_t cr)
1049 {
1050 	struct bt_rfcomm_msc *msc = (void *)buf->data;
1051 	struct bt_rfcomm_dlc *dlc;
1052 	u8_t dlci = BT_RFCOMM_GET_DLCI(msc->dlci);
1053 
1054 	BT_DBG("dlci %d", dlci);
1055 
1056 	dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, dlci);
1057 	if (!dlc) {
1058 		return;
1059 	}
1060 
1061 	if (cr == BT_RFCOMM_MSG_RESP_CR) {
1062 		return;
1063 	}
1064 
1065 	if (dlc->session->cfc == BT_RFCOMM_CFC_NOT_SUPPORTED) {
1066 		/* Only FC bit affects the flow on RFCOMM level */
1067 		if (BT_RFCOMM_GET_FC(msc->v24_signal)) {
1068 			/* If FC bit is 1 the device is unable to accept frames.
1069 			 * Take the semaphore with timeout K_NO_WAIT so that
1070 			 * dlc thread will be blocked when it tries sem_take
1071 			 * before sending the data. K_NO_WAIT timeout will make
1072 			 * sure that RX thread will not be blocked while taking
1073 			 * the semaphore.
1074 			 */
1075 			k_sem_take(&dlc->tx_credits, K_NO_WAIT);
1076 		} else {
1077 			/* Give the sem so that it will unblock the waiting dlc
1078 			 * thread in sem_take().
1079 			 */
1080 			k_sem_give(&dlc->tx_credits);
1081 		}
1082 	}
1083 
1084 	rfcomm_send_msc(dlc, BT_RFCOMM_MSG_RESP_CR, msc->v24_signal);
1085 }
1086 
rfcomm_handle_rls(struct bt_rfcomm_session * session,struct net_buf * buf,u8_t cr)1087 static void rfcomm_handle_rls(struct bt_rfcomm_session *session,
1088 			      struct net_buf *buf, u8_t cr)
1089 {
1090 	struct bt_rfcomm_rls *rls = (void *)buf->data;
1091 	u8_t dlci = BT_RFCOMM_GET_DLCI(rls->dlci);
1092 	struct bt_rfcomm_dlc *dlc;
1093 
1094 	BT_DBG("dlci %d", dlci);
1095 
1096 	if (!cr) {
1097 		/* Ignore if its a response */
1098 		return;
1099 	}
1100 
1101 	dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, dlci);
1102 	if (!dlc) {
1103 		return;
1104 	}
1105 
1106 	/* As per the ETSI same line status has to returned in the response */
1107 	rfcomm_send_rls(dlc, BT_RFCOMM_MSG_RESP_CR, rls->line_status);
1108 }
1109 
rfcomm_handle_rpn(struct bt_rfcomm_session * session,struct net_buf * buf,u8_t cr)1110 static void rfcomm_handle_rpn(struct bt_rfcomm_session *session,
1111 			      struct net_buf *buf, u8_t cr)
1112 {
1113 	struct bt_rfcomm_rpn default_rpn, *rpn = (void *)buf->data;
1114 	u8_t dlci = BT_RFCOMM_GET_DLCI(rpn->dlci);
1115 	u8_t data_bits, stop_bits, parity_bits;
1116 	/* Exclude fcs to get number of value bytes */
1117 	u8_t value_len = buf->len - 1;
1118 
1119 	BT_DBG("dlci %d", dlci);
1120 
1121 	if (!cr) {
1122 		/* Ignore if its a response */
1123 		return;
1124 	}
1125 
1126 	if (value_len == sizeof(*rpn)) {
1127 		/* Accept all the values proposed by the sender */
1128 		rpn->param_mask = sys_cpu_to_le16(BT_RFCOMM_RPN_PARAM_MASK_ALL);
1129 		rfcomm_send_rpn(session, BT_RFCOMM_MSG_RESP_CR, rpn);
1130 		return;
1131 	}
1132 
1133 	if (value_len != 1U) {
1134 		return;
1135 	}
1136 
1137 	/* If only one value byte then current port settings has to be returned
1138 	 * We will send default values
1139 	 */
1140 	default_rpn.dlci = BT_RFCOMM_SET_ADDR(dlci, 1);
1141 	default_rpn.baud_rate = BT_RFCOMM_RPN_BAUD_RATE_9600;
1142 	default_rpn.flow_control = BT_RFCOMM_RPN_FLOW_NONE;
1143 	default_rpn.xoff_char = BT_RFCOMM_RPN_XOFF_CHAR;
1144 	default_rpn.xon_char = BT_RFCOMM_RPN_XON_CHAR;
1145 	data_bits = BT_RFCOMM_RPN_DATA_BITS_8;
1146 	stop_bits = BT_RFCOMM_RPN_STOP_BITS_1;
1147 	parity_bits = BT_RFCOMM_RPN_PARITY_NONE;
1148 	default_rpn.line_settings = BT_RFCOMM_SET_LINE_SETTINGS(data_bits,
1149 								stop_bits,
1150 								parity_bits);
1151 	default_rpn.param_mask = sys_cpu_to_le16(BT_RFCOMM_RPN_PARAM_MASK_ALL);
1152 
1153 	rfcomm_send_rpn(session, BT_RFCOMM_MSG_RESP_CR, &default_rpn);
1154 }
1155 
rfcomm_handle_pn(struct bt_rfcomm_session * session,struct net_buf * buf,u8_t cr)1156 static void rfcomm_handle_pn(struct bt_rfcomm_session *session,
1157 			     struct net_buf *buf, u8_t cr)
1158 {
1159 	struct bt_rfcomm_pn *pn = (void *)buf->data;
1160 	struct bt_rfcomm_dlc *dlc;
1161 
1162 	dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, pn->dlci);
1163 	if (!dlc) {
1164 		/*  Ignore if it is a response */
1165 		if (!cr) {
1166 			return;
1167 		}
1168 
1169 		if (!BT_RFCOMM_CHECK_MTU(pn->mtu)) {
1170 			BT_ERR("Invalid mtu %d", pn->mtu);
1171 			rfcomm_send_dm(session, pn->dlci);
1172 			return;
1173 		}
1174 
1175 		dlc = rfcomm_dlc_accept(session, pn->dlci);
1176 		if (!dlc) {
1177 			rfcomm_send_dm(session, pn->dlci);
1178 			return;
1179 		}
1180 
1181 		BT_DBG("Incoming connection accepted dlc %p", dlc);
1182 
1183 		dlc->mtu = MIN(dlc->mtu, sys_le16_to_cpu(pn->mtu));
1184 
1185 		if (pn->flow_ctrl == BT_RFCOMM_PN_CFC_CMD) {
1186 			if (session->cfc == BT_RFCOMM_CFC_UNKNOWN) {
1187 				session->cfc = BT_RFCOMM_CFC_SUPPORTED;
1188 			}
1189 			k_sem_init(&dlc->tx_credits, 0, UINT32_MAX);
1190 			rfcomm_dlc_tx_give_credits(dlc, pn->credits);
1191 		} else {
1192 			session->cfc = BT_RFCOMM_CFC_NOT_SUPPORTED;
1193 		}
1194 
1195 		dlc->state = BT_RFCOMM_STATE_CONFIG;
1196 		rfcomm_send_pn(dlc, BT_RFCOMM_MSG_RESP_CR);
1197 		/* Cancel idle timer if any */
1198 		k_delayed_work_cancel(&session->rtx_work);
1199 	} else {
1200 		/* If its a command */
1201 		if (cr) {
1202 			if (!BT_RFCOMM_CHECK_MTU(pn->mtu)) {
1203 				BT_ERR("Invalid mtu %d", pn->mtu);
1204 				rfcomm_dlc_close(dlc);
1205 				return;
1206 			}
1207 			dlc->mtu = MIN(dlc->mtu, sys_le16_to_cpu(pn->mtu));
1208 			rfcomm_send_pn(dlc, BT_RFCOMM_MSG_RESP_CR);
1209 		} else {
1210 			if (dlc->state != BT_RFCOMM_STATE_CONFIG) {
1211 				return;
1212 			}
1213 
1214 			dlc->mtu = MIN(dlc->mtu, sys_le16_to_cpu(pn->mtu));
1215 			if (pn->flow_ctrl == BT_RFCOMM_PN_CFC_RESP) {
1216 				if (session->cfc == BT_RFCOMM_CFC_UNKNOWN) {
1217 					session->cfc = BT_RFCOMM_CFC_SUPPORTED;
1218 				}
1219 				k_sem_init(&dlc->tx_credits, 0, UINT32_MAX);
1220 				rfcomm_dlc_tx_give_credits(dlc, pn->credits);
1221 			} else {
1222 				session->cfc = BT_RFCOMM_CFC_NOT_SUPPORTED;
1223 			}
1224 
1225 			dlc->state = BT_RFCOMM_STATE_CONNECTING;
1226 			rfcomm_send_sabm(session, dlc->dlci);
1227 		}
1228 	}
1229 }
1230 
rfcomm_handle_disc(struct bt_rfcomm_session * session,u8_t dlci)1231 static void rfcomm_handle_disc(struct bt_rfcomm_session *session, u8_t dlci)
1232 {
1233 	struct bt_rfcomm_dlc *dlc;
1234 
1235 	BT_DBG("Dlci %d", dlci);
1236 
1237 	if (dlci) {
1238 		dlc = rfcomm_dlcs_remove_dlci(session->dlcs, dlci);
1239 		if (!dlc) {
1240 			rfcomm_send_dm(session, dlci);
1241 			return;
1242 		}
1243 
1244 		rfcomm_send_ua(session, dlci);
1245 		rfcomm_dlc_disconnect(dlc);
1246 
1247 		if (!session->dlcs) {
1248 			/* Start a session idle timer */
1249 			k_delayed_work_submit(&dlc->session->rtx_work,
1250 					      RFCOMM_IDLE_TIMEOUT);
1251 		}
1252 	} else {
1253 		/* Cancel idle timer */
1254 		k_delayed_work_cancel(&session->rtx_work);
1255 		rfcomm_send_ua(session, 0);
1256 		rfcomm_session_disconnected(session);
1257 	}
1258 }
1259 
rfcomm_handle_msg(struct bt_rfcomm_session * session,struct net_buf * buf)1260 static void rfcomm_handle_msg(struct bt_rfcomm_session *session,
1261 			      struct net_buf *buf)
1262 {
1263 	struct bt_rfcomm_msg_hdr *hdr;
1264 	u8_t msg_type, len, cr;
1265 
1266 	if (buf->len < sizeof(*hdr)) {
1267 		BT_ERR("Too small RFCOMM message");
1268 		return;
1269 	}
1270 
1271 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
1272 	msg_type = BT_RFCOMM_GET_MSG_TYPE(hdr->type);
1273 	cr = BT_RFCOMM_GET_MSG_CR(hdr->type);
1274 	len = BT_RFCOMM_GET_LEN(hdr->len);
1275 
1276 	BT_DBG("msg type %x cr %x", msg_type, cr);
1277 
1278 	switch (msg_type) {
1279 	case BT_RFCOMM_PN:
1280 		rfcomm_handle_pn(session, buf, cr);
1281 		break;
1282 	case BT_RFCOMM_MSC:
1283 		rfcomm_handle_msc(session, buf, cr);
1284 		break;
1285 	case BT_RFCOMM_RLS:
1286 		rfcomm_handle_rls(session, buf, cr);
1287 		break;
1288 	case BT_RFCOMM_RPN:
1289 		rfcomm_handle_rpn(session, buf, cr);
1290 		break;
1291 	case BT_RFCOMM_TEST:
1292 		if (!cr) {
1293 			break;
1294 		}
1295 		rfcomm_send_test(session, BT_RFCOMM_MSG_RESP_CR, buf->data,
1296 				 buf->len - 1);
1297 		break;
1298 	case BT_RFCOMM_FCON:
1299 		if (session->cfc == BT_RFCOMM_CFC_SUPPORTED) {
1300 			BT_ERR("FCON received when CFC is supported ");
1301 			return;
1302 		}
1303 
1304 		if (!cr) {
1305 			break;
1306 		}
1307 
1308 		/* Give the sem so that it will unblock the waiting dlc threads
1309 		 * of this session in sem_take().
1310 		 */
1311 		k_sem_give(&session->fc);
1312 		rfcomm_send_fcon(session, BT_RFCOMM_MSG_RESP_CR);
1313 		break;
1314 	case BT_RFCOMM_FCOFF:
1315 		if (session->cfc == BT_RFCOMM_CFC_SUPPORTED) {
1316 			BT_ERR("FCOFF received when CFC is supported ");
1317 			return;
1318 		}
1319 
1320 		if (!cr) {
1321 			break;
1322 		}
1323 
1324 		/* Take the semaphore with timeout K_NO_WAIT so that all the
1325 		 * dlc threads in this session will be blocked when it tries
1326 		 * sem_take before sending the data. K_NO_WAIT timeout will
1327 		 * make sure that RX thread will not be blocked while taking
1328 		 * the semaphore.
1329 		 */
1330 		k_sem_take(&session->fc, K_NO_WAIT);
1331 		rfcomm_send_fcoff(session, BT_RFCOMM_MSG_RESP_CR);
1332 		break;
1333 	default:
1334 		BT_WARN("Unknown/Unsupported RFCOMM Msg type 0x%02x", msg_type);
1335 		rfcomm_send_nsc(session, hdr->type);
1336 		break;
1337 	}
1338 }
1339 
rfcomm_dlc_update_credits(struct bt_rfcomm_dlc * dlc)1340 static void rfcomm_dlc_update_credits(struct bt_rfcomm_dlc *dlc)
1341 {
1342 	u8_t credits;
1343 
1344 	if (dlc->session->cfc == BT_RFCOMM_CFC_NOT_SUPPORTED) {
1345 		return;
1346 	}
1347 
1348 	BT_DBG("dlc %p credits %u", dlc, dlc->rx_credit);
1349 
1350 	/* Only give more credits if it went below the defined threshold */
1351 	if (dlc->rx_credit > RFCOMM_CREDITS_THRESHOLD) {
1352 		return;
1353 	}
1354 
1355 	/* Restore credits */
1356 	credits = RFCOMM_MAX_CREDITS - dlc->rx_credit;
1357 	dlc->rx_credit += credits;
1358 
1359 	rfcomm_send_credit(dlc, credits);
1360 }
1361 
rfcomm_handle_data(struct bt_rfcomm_session * session,struct net_buf * buf,u8_t dlci,u8_t pf)1362 static void rfcomm_handle_data(struct bt_rfcomm_session *session,
1363 			       struct net_buf *buf, u8_t dlci, u8_t pf)
1364 
1365 {
1366 	struct bt_rfcomm_dlc *dlc;
1367 
1368 	BT_DBG("dlci %d, pf %d", dlci, pf);
1369 
1370 	dlc = rfcomm_dlcs_lookup_dlci(session->dlcs, dlci);
1371 	if (!dlc) {
1372 		BT_ERR("Data recvd in non existing DLC");
1373 		rfcomm_send_dm(session, dlci);
1374 		return;
1375 	}
1376 
1377 	BT_DBG("dlc %p rx credit %d", dlc, dlc->rx_credit);
1378 
1379 	if (dlc->state != BT_RFCOMM_STATE_CONNECTED) {
1380 		return;
1381 	}
1382 
1383 	if (pf == BT_RFCOMM_PF_UIH_CREDIT) {
1384 		rfcomm_dlc_tx_give_credits(dlc, net_buf_pull_u8(buf));
1385 	}
1386 
1387 	if (buf->len > BT_RFCOMM_FCS_SIZE) {
1388 		if (dlc->session->cfc == BT_RFCOMM_CFC_SUPPORTED &&
1389 		    !dlc->rx_credit) {
1390 			BT_ERR("Data recvd when rx credit is 0");
1391 			rfcomm_dlc_close(dlc);
1392 			return;
1393 		}
1394 
1395 		/* Remove FCS */
1396 		buf->len -= BT_RFCOMM_FCS_SIZE;
1397 		if (dlc->ops && dlc->ops->recv) {
1398 			dlc->ops->recv(dlc, buf);
1399 		}
1400 
1401 		dlc->rx_credit--;
1402 		rfcomm_dlc_update_credits(dlc);
1403 	}
1404 }
1405 
bt_rfcomm_dlc_send(struct bt_rfcomm_dlc * dlc,struct net_buf * buf)1406 int bt_rfcomm_dlc_send(struct bt_rfcomm_dlc *dlc, struct net_buf *buf)
1407 {
1408 	struct bt_rfcomm_hdr *hdr;
1409 	u8_t fcs, cr;
1410 
1411 	if (!buf) {
1412 		return -EINVAL;
1413 	}
1414 
1415 	BT_DBG("dlc %p tx credit %d", dlc, k_sem_count_get(&dlc->tx_credits));
1416 
1417 	if (dlc->state != BT_RFCOMM_STATE_CONNECTED) {
1418 		return -ENOTCONN;
1419 	}
1420 
1421 	if (buf->len > dlc->mtu) {
1422 		return -EMSGSIZE;
1423 	}
1424 
1425 	if (buf->len > BT_RFCOMM_MAX_LEN_8) {
1426 		u16_t *len;
1427 
1428 		/* Length is 2 byte */
1429 		hdr = net_buf_push(buf, sizeof(*hdr) + 1);
1430 		len = (u16_t *)&hdr->length;
1431 		*len = BT_RFCOMM_SET_LEN_16(sys_cpu_to_le16(buf->len -
1432 							    sizeof(*hdr) - 1));
1433 	} else {
1434 		hdr = net_buf_push(buf, sizeof(*hdr));
1435 		hdr->length = BT_RFCOMM_SET_LEN_8(buf->len - sizeof(*hdr));
1436 	}
1437 
1438 	cr = BT_RFCOMM_UIH_CR(dlc->session->role);
1439 	hdr->address = BT_RFCOMM_SET_ADDR(dlc->dlci, cr);
1440 	hdr->control = BT_RFCOMM_SET_CTRL(BT_RFCOMM_UIH,
1441 					  BT_RFCOMM_PF_UIH_NO_CREDIT);
1442 
1443 	fcs = rfcomm_calc_fcs(BT_RFCOMM_FCS_LEN_UIH, buf->data);
1444 	net_buf_add_u8(buf, fcs);
1445 
1446 	net_buf_put(&dlc->tx_queue, buf);
1447 
1448 	return buf->len;
1449 }
1450 
rfcomm_recv(struct bt_l2cap_chan * chan,struct net_buf * buf)1451 static int rfcomm_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
1452 {
1453 	struct bt_rfcomm_session *session = RFCOMM_SESSION(chan);
1454 	struct bt_rfcomm_hdr *hdr = (void *)buf->data;
1455 	u8_t dlci, frame_type, fcs, fcs_len;
1456 
1457 	/* Need to consider FCS also*/
1458 	if (buf->len < (sizeof(*hdr) + 1)) {
1459 		BT_ERR("Too small RFCOMM Frame");
1460 		return 0;
1461 	}
1462 
1463 	dlci = BT_RFCOMM_GET_DLCI(hdr->address);
1464 	frame_type = BT_RFCOMM_GET_FRAME_TYPE(hdr->control);
1465 
1466 	BT_DBG("session %p dlci %x type %x", session, dlci, frame_type);
1467 
1468 	fcs_len = (frame_type == BT_RFCOMM_UIH) ? BT_RFCOMM_FCS_LEN_UIH :
1469 		   BT_RFCOMM_FCS_LEN_NON_UIH;
1470 	fcs = *(net_buf_tail(buf) - 1);
1471 	if (!rfcomm_check_fcs(fcs_len, buf->data, fcs)) {
1472 		BT_ERR("FCS check failed");
1473 		return 0;
1474 	}
1475 
1476 	if (BT_RFCOMM_LEN_EXTENDED(hdr->length)) {
1477 		net_buf_pull(buf, sizeof(*hdr) + 1);
1478 	} else {
1479 		net_buf_pull(buf, sizeof(*hdr));
1480 	}
1481 
1482 	switch (frame_type) {
1483 	case BT_RFCOMM_SABM:
1484 		rfcomm_handle_sabm(session, dlci);
1485 		break;
1486 	case BT_RFCOMM_UIH:
1487 		if (!dlci) {
1488 			rfcomm_handle_msg(session, buf);
1489 		} else {
1490 			rfcomm_handle_data(session, buf, dlci,
1491 					   BT_RFCOMM_GET_PF(hdr->control));
1492 		}
1493 		break;
1494 	case BT_RFCOMM_DISC:
1495 		rfcomm_handle_disc(session, dlci);
1496 		break;
1497 	case BT_RFCOMM_UA:
1498 		rfcomm_handle_ua(session, dlci);
1499 		break;
1500 	case BT_RFCOMM_DM:
1501 		rfcomm_handle_dm(session, dlci);
1502 		break;
1503 	default:
1504 		BT_WARN("Unknown/Unsupported RFCOMM Frame type 0x%02x",
1505 			frame_type);
1506 		break;
1507 	}
1508 
1509 	return 0;
1510 }
1511 
rfcomm_encrypt_change(struct bt_l2cap_chan * chan,u8_t hci_status)1512 static void rfcomm_encrypt_change(struct bt_l2cap_chan *chan,
1513 				  u8_t hci_status)
1514 {
1515 	struct bt_rfcomm_session *session = RFCOMM_SESSION(chan);
1516 	struct bt_conn *conn = chan->conn;
1517 	struct bt_rfcomm_dlc *dlc, *next;
1518 
1519 	BT_DBG("session %p status 0x%02x encr 0x%02x", session, hci_status,
1520 	       conn->encrypt);
1521 
1522 	for (dlc = session->dlcs; dlc; dlc = next) {
1523 		next = dlc->_next;
1524 
1525 		if (dlc->state != BT_RFCOMM_STATE_SECURITY_PENDING) {
1526 			continue;
1527 		}
1528 
1529 		if (hci_status || !conn->encrypt ||
1530 		    conn->sec_level < dlc->required_sec_level) {
1531 			rfcomm_dlc_close(dlc);
1532 			continue;
1533 		}
1534 
1535 		if (dlc->role == BT_RFCOMM_ROLE_ACCEPTOR) {
1536 			rfcomm_send_ua(session, dlc->dlci);
1537 			rfcomm_dlc_connected(dlc);
1538 		} else {
1539 			dlc->mtu = MIN(dlc->mtu, session->mtu);
1540 			dlc->state = BT_RFCOMM_STATE_CONFIG;
1541 			rfcomm_send_pn(dlc, BT_RFCOMM_MSG_CMD_CR);
1542 		}
1543 	}
1544 }
1545 
rfcomm_session_rtx_timeout(struct k_work * work)1546 static void rfcomm_session_rtx_timeout(struct k_work *work)
1547 {
1548 	struct bt_rfcomm_session *session = SESSION_RTX(work);
1549 
1550 	BT_WARN("session %p state %d timeout", session, session->state);
1551 
1552 	switch (session->state) {
1553 	case BT_RFCOMM_STATE_CONNECTED:
1554 		rfcomm_session_disconnect(session);
1555 		break;
1556 	case BT_RFCOMM_STATE_DISCONNECTING:
1557 		session->state = BT_RFCOMM_STATE_DISCONNECTED;
1558 		if (bt_l2cap_chan_disconnect(&session->br_chan.chan) < 0) {
1559 			session->state = BT_RFCOMM_STATE_IDLE;
1560 		}
1561 		break;
1562 	}
1563 }
1564 
rfcomm_session_new(bt_rfcomm_role_t role)1565 static struct bt_rfcomm_session *rfcomm_session_new(bt_rfcomm_role_t role)
1566 {
1567 	int i;
1568 	static const struct bt_l2cap_chan_ops ops = {
1569 		.connected = rfcomm_connected,
1570 		.disconnected = rfcomm_disconnected,
1571 		.recv = rfcomm_recv,
1572 		.encrypt_change = rfcomm_encrypt_change,
1573 	};
1574 
1575 	for (i = 0; i < ARRAY_SIZE(bt_rfcomm_pool); i++) {
1576 		struct bt_rfcomm_session *session = &bt_rfcomm_pool[i];
1577 
1578 		if (session->br_chan.chan.conn) {
1579 			continue;
1580 		}
1581 
1582 		BT_DBG("session %p initialized", session);
1583 
1584 		session->br_chan.chan.ops = &ops;
1585 		session->br_chan.rx.mtu	= CONFIG_BT_RFCOMM_L2CAP_MTU;
1586 		session->state = BT_RFCOMM_STATE_INIT;
1587 		session->role = role;
1588 		session->cfc = BT_RFCOMM_CFC_UNKNOWN;
1589 		k_delayed_work_init(&session->rtx_work,
1590 				    rfcomm_session_rtx_timeout);
1591 		k_sem_init(&session->fc, 0, 1);
1592 
1593 		return session;
1594 	}
1595 
1596 	return NULL;
1597 }
1598 
bt_rfcomm_dlc_connect(struct bt_conn * conn,struct bt_rfcomm_dlc * dlc,u8_t channel)1599 int bt_rfcomm_dlc_connect(struct bt_conn *conn, struct bt_rfcomm_dlc *dlc,
1600 			  u8_t channel)
1601 {
1602 	struct bt_rfcomm_session *session;
1603 	struct bt_l2cap_chan *chan;
1604 	u8_t dlci;
1605 	int ret;
1606 
1607 	BT_DBG("conn %p dlc %p channel %d", conn, dlc, channel);
1608 
1609 	if (!dlc) {
1610 		return -EINVAL;
1611 	}
1612 
1613 	if (!conn || conn->state != BT_CONN_CONNECTED) {
1614 		return -ENOTCONN;
1615 	}
1616 
1617 	if (channel < RFCOMM_CHANNEL_START || channel > RFCOMM_CHANNEL_END) {
1618 		return -EINVAL;
1619 	}
1620 
1621 	if (!BT_RFCOMM_CHECK_MTU(dlc->mtu)) {
1622 		return -EINVAL;
1623 	}
1624 
1625 	session = rfcomm_sessions_lookup_bt_conn(conn);
1626 	if (!session) {
1627 		session = rfcomm_session_new(BT_RFCOMM_ROLE_INITIATOR);
1628 		if (!session) {
1629 			return -ENOMEM;
1630 		}
1631 	}
1632 
1633 	dlci = BT_RFCOMM_DLCI(session->role, channel);
1634 
1635 	if (rfcomm_dlcs_lookup_dlci(session->dlcs, dlci)) {
1636 		return -EBUSY;
1637 	}
1638 
1639 	rfcomm_dlc_init(dlc, session, dlci, BT_RFCOMM_ROLE_INITIATOR);
1640 
1641 	switch (session->state) {
1642 	case BT_RFCOMM_STATE_INIT:
1643 		if (session->role == BT_RFCOMM_ROLE_ACCEPTOR) {
1644 			/* There is an ongoing incoming conn */
1645 			break;
1646 		}
1647 		chan = &session->br_chan.chan;
1648 		chan->required_sec_level = dlc->required_sec_level;
1649 		ret = bt_l2cap_chan_connect(conn, chan, BT_L2CAP_PSM_RFCOMM);
1650 		if (ret < 0) {
1651 			session->state = BT_RFCOMM_STATE_IDLE;
1652 			goto fail;
1653 		}
1654 		session->state = BT_RFCOMM_STATE_CONNECTING;
1655 		break;
1656 	case BT_RFCOMM_STATE_CONNECTING:
1657 		break;
1658 	case BT_RFCOMM_STATE_CONNECTED:
1659 		ret = rfcomm_dlc_start(dlc);
1660 		if (ret < 0) {
1661 			goto fail;
1662 		}
1663 		/* Cancel idle timer if any */
1664 		k_delayed_work_cancel(&session->rtx_work);
1665 		break;
1666 	default:
1667 		BT_ERR("Invalid session state %d", session->state);
1668 		ret = -EINVAL;
1669 		goto fail;
1670 	}
1671 
1672 	return 0;
1673 
1674 fail:
1675 	rfcomm_dlcs_remove_dlci(session->dlcs, dlc->dlci);
1676 	dlc->state = BT_RFCOMM_STATE_IDLE;
1677 	dlc->session = NULL;
1678 	return ret;
1679 }
1680 
bt_rfcomm_dlc_disconnect(struct bt_rfcomm_dlc * dlc)1681 int bt_rfcomm_dlc_disconnect(struct bt_rfcomm_dlc *dlc)
1682 {
1683 	BT_DBG("dlc %p", dlc);
1684 
1685 	if (!dlc) {
1686 		return -EINVAL;
1687 	}
1688 
1689 	if (dlc->state == BT_RFCOMM_STATE_CONNECTED) {
1690 		/* This is to handle user initiated disconnect to send pending
1691 		 * bufs in the queue before disconnecting
1692 		 * Queue a dummy buffer (in case if queue is empty) to wake up
1693 		 * and stop the tx thread.
1694 		 */
1695 		dlc->state = BT_RFCOMM_STATE_USER_DISCONNECT;
1696 		net_buf_put(&dlc->tx_queue,
1697 			    net_buf_alloc(&dummy_pool, K_NO_WAIT));
1698 
1699 		k_delayed_work_submit(&dlc->rtx_work, RFCOMM_DISC_TIMEOUT);
1700 
1701 		return 0;
1702 	}
1703 
1704 	return rfcomm_dlc_close(dlc);
1705 }
1706 
rfcomm_accept(struct bt_conn * conn,struct bt_l2cap_chan ** chan)1707 static int rfcomm_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan)
1708 {
1709 	struct bt_rfcomm_session *session;
1710 
1711 	BT_DBG("conn %p", conn);
1712 
1713 	session = rfcomm_session_new(BT_RFCOMM_ROLE_ACCEPTOR);
1714 	if (session) {
1715 		*chan = &session->br_chan.chan;
1716 		return 0;
1717 	}
1718 
1719 	BT_ERR("No available RFCOMM context for conn %p", conn);
1720 
1721 	return -ENOMEM;
1722 }
1723 
bt_rfcomm_init(void)1724 void bt_rfcomm_init(void)
1725 {
1726 	static struct bt_l2cap_server server = {
1727 		.psm       = BT_L2CAP_PSM_RFCOMM,
1728 		.accept    = rfcomm_accept,
1729 		.sec_level = BT_SECURITY_L1,
1730 	};
1731 
1732     NET_BUF_POOL_INIT(dummy_pool);
1733 
1734 	bt_l2cap_br_server_register(&server);
1735 }
1736