Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*	$NetBSD: rfcomm_dlc.c,v 1.9 2018/09/03 16:29:36 riastradh Exp $	*/

/*-
 * Copyright (c) 2006 Itronix Inc.
 * All rights reserved.
 *
 * Written by Iain Hibbert for Itronix Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of Itronix Inc. may not be used to endorse
 *    or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: rfcomm_dlc.c,v 1.9 2018/09/03 16:29:36 riastradh Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/mbuf.h>
#include <sys/proc.h>
#include <sys/socketvar.h>
#include <sys/systm.h>

#include <netbt/bluetooth.h>
#include <netbt/hci.h>
#include <netbt/l2cap.h>
#include <netbt/rfcomm.h>

/*
 * rfcomm_dlc_lookup(rfcomm_session, dlci)
 *
 * Find DLC on session with matching dlci
 */
struct rfcomm_dlc *
rfcomm_dlc_lookup(struct rfcomm_session *rs, int dlci)
{
	struct rfcomm_dlc *dlc;

	LIST_FOREACH(dlc, &rs->rs_dlcs, rd_next) {
		if (dlc->rd_dlci == dlci)
			break;
	}

	return dlc;
}

/*
 * rfcomm_dlc_newconn(rfcomm_session, dlci)
 *
 * handle a new dlc request (since its called from a couple of places)
 */
struct rfcomm_dlc *
rfcomm_dlc_newconn(struct rfcomm_session *rs, int dlci)
{
	struct rfcomm_session *ls;
	struct rfcomm_dlc *new, *dlc, *any, *best;
	struct sockaddr_bt laddr, raddr, addr;
	int chan;

	/*
	 * Search amongst the listening DLC community for the best match for
	 * address & channel. We keep listening DLC's hanging on listening
	 * sessions in a last first order, so scan the entire bunch and keep
	 * a note of the best address and BDADDR_ANY matches in order to find
	 * the oldest and most specific match.
	 */
	l2cap_sockaddr_pcb(rs->rs_l2cap, &laddr);
	l2cap_peeraddr_pcb(rs->rs_l2cap, &raddr);
	chan = RFCOMM_CHANNEL(dlci);
	new = NULL;

	any = best = NULL;
	LIST_FOREACH(ls, &rfcomm_session_listen, rs_next) {
		l2cap_sockaddr_pcb(ls->rs_l2cap, &addr);

		if (addr.bt_psm != laddr.bt_psm)
			continue;

		if (bdaddr_same(&laddr.bt_bdaddr, &addr.bt_bdaddr)) {
			LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
				if (dlc->rd_laddr.bt_channel == chan)
					best = dlc;
			}
		}

		if (bdaddr_any(&addr.bt_bdaddr)) {
			LIST_FOREACH(dlc, &ls->rs_dlcs, rd_next) {
				if (dlc->rd_laddr.bt_channel == chan)
					any = dlc;
			}
		}
	}

	dlc = best ? best : any;

	/* XXX
	 * Note that if this fails, we could have missed a chance to open
	 * a connection - really need to rewrite the strategy for storing
	 * listening DLC's so all can be checked in turn..
	 */
	if (dlc != NULL)
		new = (*dlc->rd_proto->newconn)(dlc->rd_upper, &laddr, &raddr);

	if (new == NULL) {
		rfcomm_session_send_frame(rs, RFCOMM_FRAME_DM, dlci);
		return NULL;
	}

	new->rd_dlci = dlci;
	new->rd_mtu = rfcomm_mtu_default;
	new->rd_mode = dlc->rd_mode;

	memcpy(&new->rd_laddr, &laddr, sizeof(struct sockaddr_bt));
	new->rd_laddr.bt_channel = chan;

	memcpy(&new->rd_raddr, &raddr, sizeof(struct sockaddr_bt));
	new->rd_raddr.bt_channel = chan;

	new->rd_session = rs;
	new->rd_state = RFCOMM_DLC_WAIT_CONNECT;
	LIST_INSERT_HEAD(&rs->rs_dlcs, new, rd_next);

	return new;
}

/*
 * rfcomm_dlc_close(dlc, error)
 *
 * detach DLC from session and clean up
 */
void
rfcomm_dlc_close(struct rfcomm_dlc *dlc, int err)
{
	struct rfcomm_session *rs;
	struct rfcomm_credit *credit;

	KASSERT(dlc->rd_state != RFCOMM_DLC_CLOSED);

	/* Clear credit history */
	rs = dlc->rd_session;
	SIMPLEQ_FOREACH(credit, &rs->rs_credits, rc_next)
		if (credit->rc_dlc == dlc)
			credit->rc_dlc = NULL;

	callout_stop(&dlc->rd_timeout);

	LIST_REMOVE(dlc, rd_next);
	dlc->rd_session = NULL;
	dlc->rd_state = RFCOMM_DLC_CLOSED;

	(*dlc->rd_proto->disconnected)(dlc->rd_upper, err);

	/*
	 * It is the responsibility of the party who sends the last
	 * DISC(dlci) to disconnect the session, but we will schedule
	 * an expiry just in case that doesnt happen..
	 */
	if (LIST_EMPTY(&rs->rs_dlcs)) {
		if (rs->rs_state == RFCOMM_SESSION_LISTEN)
			rfcomm_session_free(rs);
		else
			callout_schedule(&rs->rs_timeout,
					rfcomm_ack_timeout * hz);
	}
}

/*
 * rfcomm_dlc_timeout(dlc)
 *
 * DLC timeout function is scheduled when we sent any of SABM,
 * DISC, MCC_MSC, or MCC_PN and should be cancelled when we get
 * the relevant response. There is nothing to do but shut this
 * DLC down.
 */
void
rfcomm_dlc_timeout(void *arg)
{
	struct rfcomm_dlc *dlc = arg;

	mutex_enter(bt_lock);
	callout_ack(&dlc->rd_timeout);

	if (dlc->rd_state != RFCOMM_DLC_CLOSED)
		rfcomm_dlc_close(dlc, ETIMEDOUT);
	else if (dlc->rd_flags & RFCOMM_DLC_DETACH) {
		callout_destroy(&dlc->rd_timeout);
		free(dlc, M_BLUETOOTH);
	}

	mutex_exit(bt_lock);
}

/*
 * rfcomm_dlc_setmode(rfcomm_dlc)
 *
 * Set link mode for DLC.  This is only called when the session is
 * already open, so we don't need to worry about any previous mode
 * settings.
 */
int
rfcomm_dlc_setmode(struct rfcomm_dlc *dlc)
{
	struct sockopt sopt;
	int mode = 0, err;

	KASSERT(dlc->rd_session != NULL);
	KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);

	DPRINTF("dlci %d, auth %s, encrypt %s, secure %s\n", dlc->rd_dlci,
		(dlc->rd_mode & RFCOMM_LM_AUTH ? "yes" : "no"),
		(dlc->rd_mode & RFCOMM_LM_ENCRYPT ? "yes" : "no"),
		(dlc->rd_mode & RFCOMM_LM_SECURE ? "yes" : "no"));

	if (dlc->rd_mode & RFCOMM_LM_AUTH)
		mode |= L2CAP_LM_AUTH;

	if (dlc->rd_mode & RFCOMM_LM_ENCRYPT)
		mode |= L2CAP_LM_ENCRYPT;

	if (dlc->rd_mode & RFCOMM_LM_SECURE)
		mode |= L2CAP_LM_SECURE;

	sockopt_init(&sopt, BTPROTO_L2CAP, SO_L2CAP_LM, 0);
	sockopt_setint(&sopt, mode);
	err = l2cap_setopt(dlc->rd_session->rs_l2cap, &sopt);
	sockopt_destroy(&sopt);

	return err;
}

/*
 * rfcomm_dlc_connect(rfcomm_dlc)
 *
 * initiate DLC connection (session is already connected)
 */
int
rfcomm_dlc_connect(struct rfcomm_dlc *dlc)
{
	struct rfcomm_mcc_pn pn;
	int err = 0;

	KASSERT(dlc->rd_session != NULL);
	KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);
	KASSERT(dlc->rd_state == RFCOMM_DLC_WAIT_SESSION);

	/*
	 * If we have not already sent a PN on the session, we must send
	 * a PN to negotiate Credit Flow Control, and this setting will
	 * apply to all future connections for this session. We ask for
	 * this every time, in order to establish initial credits.
	 */
	memset(&pn, 0, sizeof(pn));
	pn.dlci = dlc->rd_dlci;
	pn.priority = dlc->rd_dlci | 0x07;
	pn.mtu = htole16(dlc->rd_mtu);

	pn.flow_control = 0xf0;
	dlc->rd_rxcred = (dlc->rd_rxsize / dlc->rd_mtu);
	dlc->rd_rxcred = uimin(dlc->rd_rxcred, RFCOMM_CREDITS_DEFAULT);
	pn.credits = dlc->rd_rxcred;

	err = rfcomm_session_send_mcc(dlc->rd_session, 1,
					RFCOMM_MCC_PN, &pn, sizeof(pn));
	if (err)
		return err;

	dlc->rd_state = RFCOMM_DLC_WAIT_CONNECT;
	callout_schedule(&dlc->rd_timeout, rfcomm_mcc_timeout * hz);

	return 0;
}

/*
 * rfcomm_dlc_open(rfcomm_dlc)
 *
 * send "Modem Status Command" and mark DLC as open.
 */
int
rfcomm_dlc_open(struct rfcomm_dlc *dlc)
{
	struct rfcomm_mcc_msc msc;
	int err;

	KASSERT(dlc->rd_session != NULL);
	KASSERT(dlc->rd_session->rs_state == RFCOMM_SESSION_OPEN);

	memset(&msc, 0, sizeof(msc));
	msc.address = RFCOMM_MKADDRESS(1, dlc->rd_dlci);
	msc.modem = dlc->rd_lmodem & 0xfe;	/* EA = 0 */
	msc.brk =	0x00	   | 0x01;	/* EA = 1 */

	err = rfcomm_session_send_mcc(dlc->rd_session, 1,
				RFCOMM_MCC_MSC, &msc, sizeof(msc));
	if (err)
		return err;

	callout_schedule(&dlc->rd_timeout, rfcomm_mcc_timeout * hz);

	dlc->rd_state = RFCOMM_DLC_OPEN;
	(*dlc->rd_proto->connected)(dlc->rd_upper);

	return 0;
}

/*
 * rfcomm_dlc_start(rfcomm_dlc)
 *
 * Start sending data (and/or credits) for DLC. Our strategy is to
 * send anything we can down to the l2cap layer. When credits run
 * out, data will naturally bunch up. When not using credit flow
 * control, we limit the number of packets we have pending to reduce
 * flow control lag.
 * We should deal with channel priority somehow.
 */
void
rfcomm_dlc_start(struct rfcomm_dlc *dlc)
{
	struct rfcomm_session *rs = dlc->rd_session;
	struct mbuf *m;
	int len, credits;

	KASSERT(rs != NULL);
	KASSERT(rs->rs_state == RFCOMM_SESSION_OPEN);
	KASSERT(dlc->rd_state == RFCOMM_DLC_OPEN);

	for (;;) {
		credits = 0;
		len = dlc->rd_mtu;
		if (rs->rs_flags & RFCOMM_SESSION_CFC) {
			credits = (dlc->rd_rxsize / dlc->rd_mtu);
			credits -= dlc->rd_rxcred;
			credits = uimin(credits, RFCOMM_CREDITS_MAX);

			if (credits > 0)
				len--;

			if (dlc->rd_txcred == 0)
				len = 0;
		} else {
			if (rs->rs_flags & RFCOMM_SESSION_RFC)
				break;

			if (dlc->rd_rmodem & RFCOMM_MSC_FC)
				break;

			if (dlc->rd_pending > RFCOMM_CREDITS_DEFAULT)
				break;
		}

		if (dlc->rd_txbuf == NULL)
			len = 0;

		if (len == 0) {
			if (credits == 0)
				break;

			/*
			 * No need to send small numbers of credits on their
			 * own unless the other end hasn't many left.
			 */
			if (credits < RFCOMM_CREDITS_DEFAULT
			    && dlc->rd_rxcred > RFCOMM_CREDITS_DEFAULT)
				break;

			m = NULL;
		} else {
			/*
			 * take what data we can from (front of) txbuf
			 */
			m = dlc->rd_txbuf;
			if (len < m->m_pkthdr.len) {
				dlc->rd_txbuf = m_split(m, len, M_DONTWAIT);
				if (dlc->rd_txbuf == NULL) {
					dlc->rd_txbuf = m;
					break;
				}
			} else {
				dlc->rd_txbuf = NULL;
				len = m->m_pkthdr.len;
			}
		}

		DPRINTFN(10, "dlci %d send %d bytes, %d credits, rxcred = %d\n",
			dlc->rd_dlci, len, credits, dlc->rd_rxcred);

		if (rfcomm_session_send_uih(rs, dlc, credits, m)) {
			printf("%s: lost %d bytes on DLCI %d\n",
				__func__, len, dlc->rd_dlci);

			break;
		}

		dlc->rd_pending++;

		if (rs->rs_flags & RFCOMM_SESSION_CFC) {
			if (len > 0)
				dlc->rd_txcred--;

			if (credits > 0)
				dlc->rd_rxcred += credits;
		}
	}
}