summaryrefslogtreecommitdiffstats
path: root/sys/netgraph/bluetooth/l2cap/ng_l2cap_cmds.c
blob: 0d28f1ad13443f32b0ce915251ac164a050b7416 (plain)
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
/*
 * ng_l2cap_cmds.c
 *
 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
 * All rights reserved.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR OR CONTRIBUTORS 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.
 *
 * $Id: ng_l2cap_cmds.c,v 1.14 2002/09/04 21:38:38 max Exp $
 * $FreeBSD$
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/endian.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/queue.h>
#include <netgraph/ng_message.h>
#include <netgraph/netgraph.h>
#include "ng_bluetooth.h"
#include "ng_hci.h"
#include "ng_l2cap.h"
#include "ng_l2cap_var.h"
#include "ng_l2cap_cmds.h"
#include "ng_l2cap_evnt.h"
#include "ng_l2cap_llpi.h"
#include "ng_l2cap_ulpi.h"
#include "ng_l2cap_misc.h"

/******************************************************************************
 ******************************************************************************
 **                    L2CAP commands processing module
 ******************************************************************************
 ******************************************************************************/

/*
 * Process L2CAP command queue on connection
 */

void
ng_l2cap_con_wakeup(ng_l2cap_con_p con)
{
	ng_l2cap_cmd_p	 cmd = NULL;
	struct mbuf	*m = NULL;
	int		 error = 0;

	/* Find first non-pending command in the queue */
	TAILQ_FOREACH(cmd, &con->cmd_list, next) {
		KASSERT((cmd->con == con),
("%s: %s - invalid connection pointer!\n",
		__func__, NG_NODE_NAME(con->l2cap->node)));

		if (!(cmd->flags & NG_L2CAP_CMD_PENDING))
			break;
	}

	if (cmd == NULL)
		return;

	/* Detach command packet */
	m = cmd->aux;
	cmd->aux = NULL;

	/* Process command */
	switch (cmd->code) {
	case NG_L2CAP_CMD_REJ:
	case NG_L2CAP_DISCON_RSP:
	case NG_L2CAP_ECHO_RSP:
	case NG_L2CAP_INFO_RSP:
		ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		ng_l2cap_unlink_cmd(cmd);
		ng_l2cap_free_cmd(cmd);
		break;

	case NG_L2CAP_CON_REQ:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		if (error != 0) {
			ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token,
				NG_L2CAP_NO_RESOURCES, 0);
			ng_l2cap_free_chan(cmd->ch); /* will free commands */
		} else
			ng_l2cap_command_timeout(cmd,
				bluetooth_l2cap_rtx_timeout());
		break;

	case NG_L2CAP_CON_RSP:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		ng_l2cap_unlink_cmd(cmd);
		if (cmd->ch != NULL) {
			ng_l2cap_l2ca_con_rsp_rsp(cmd->ch, cmd->token,
				(error == 0)? NG_L2CAP_SUCCESS : 
					NG_L2CAP_NO_RESOURCES);
			if (error != 0)
				ng_l2cap_free_chan(cmd->ch);
		}
		ng_l2cap_free_cmd(cmd);
		break;

	case NG_L2CAP_CFG_REQ:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		if (error != 0) {
			ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token,
				NG_L2CAP_NO_RESOURCES);
			ng_l2cap_unlink_cmd(cmd);
			ng_l2cap_free_cmd(cmd);
		} else
			ng_l2cap_command_timeout(cmd,
				bluetooth_l2cap_rtx_timeout());
		break;

	case NG_L2CAP_CFG_RSP:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		ng_l2cap_unlink_cmd(cmd);
		if (cmd->ch != NULL)
			ng_l2cap_l2ca_cfg_rsp_rsp(cmd->ch, cmd->token,
				(error == 0)? NG_L2CAP_SUCCESS :
					NG_L2CAP_NO_RESOURCES);
		ng_l2cap_free_cmd(cmd);
		break;

	case NG_L2CAP_DISCON_REQ:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token,
			(error == 0)? NG_L2CAP_SUCCESS : NG_L2CAP_NO_RESOURCES);
		if (error != 0)
			ng_l2cap_free_chan(cmd->ch); /* XXX free channel */
		else
			ng_l2cap_command_timeout(cmd,
				bluetooth_l2cap_rtx_timeout());
		break;

	case NG_L2CAP_ECHO_REQ:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		if (error != 0) {
			ng_l2cap_l2ca_ping_rsp(con, cmd->token,
					NG_L2CAP_NO_RESOURCES, NULL);
			ng_l2cap_unlink_cmd(cmd);
			ng_l2cap_free_cmd(cmd);
		} else
			ng_l2cap_command_timeout(cmd, 
				bluetooth_l2cap_rtx_timeout());
		break;

	case NG_L2CAP_INFO_REQ:
		error = ng_l2cap_lp_send(con, NG_L2CAP_SIGNAL_CID, m);
		if (error != 0) {
			ng_l2cap_l2ca_get_info_rsp(con, cmd->token, 
				NG_L2CAP_NO_RESOURCES, NULL);
			ng_l2cap_unlink_cmd(cmd);
			ng_l2cap_free_cmd(cmd);
		} else
			ng_l2cap_command_timeout(cmd, 
				bluetooth_l2cap_rtx_timeout());
		break;

	case NGM_L2CAP_L2CA_WRITE: {
		int	length = m->m_pkthdr.len;

		if (cmd->ch->dcid == NG_L2CAP_CLT_CID) {
			m = ng_l2cap_prepend(m, sizeof(ng_l2cap_clt_hdr_t));
			if (m == NULL)
				error = ENOBUFS;
			else
                		mtod(m, ng_l2cap_clt_hdr_t *)->psm =
							htole16(cmd->ch->psm);
		}

		if (error == 0)
			error = ng_l2cap_lp_send(con, cmd->ch->dcid, m);

		ng_l2cap_l2ca_write_rsp(cmd->ch, cmd->token,
			(error == 0)? NG_L2CAP_SUCCESS : NG_L2CAP_NO_RESOURCES,
			length);

		ng_l2cap_unlink_cmd(cmd);
		ng_l2cap_free_cmd(cmd);
		} break;

	/* XXX FIXME add other commands */

	default:
		KASSERT(0,
("%s: %s - unknown command code=%d\n",
			__func__, NG_NODE_NAME(con->l2cap->node), cmd->code));

		ng_l2cap_unlink_cmd(cmd);
		ng_l2cap_free_cmd(cmd);
		break;
	}
} /* ng_l2cap_con_wakeup */

/*
 * We have failed to open ACL connection to the remote unit. Could be negative
 * confirmation or timeout. So fail any "delayed" commands, notify upper layer,
 * remove all channels and remove connection descriptor.
 */

void
ng_l2cap_con_fail(ng_l2cap_con_p con, u_int16_t result)
{
	ng_l2cap_p	l2cap = con->l2cap;
	ng_l2cap_cmd_p	cmd = NULL;
	ng_l2cap_chan_p	ch = NULL;

	NG_L2CAP_INFO(
"%s: %s - ACL connection failed, result=%d\n",
		__func__, NG_NODE_NAME(l2cap->node), result);

	/* Clean command queue */
	while (!TAILQ_EMPTY(&con->cmd_list)) {
		cmd = TAILQ_FIRST(&con->cmd_list);

		ng_l2cap_unlink_cmd(cmd);
		if(cmd->flags & NG_L2CAP_CMD_PENDING)
			ng_l2cap_command_untimeout(cmd);

		KASSERT((cmd->con == con),
("%s: %s - invalid connection pointer!\n",
			__func__, NG_NODE_NAME(con->l2cap->node)));

		switch (cmd->code) {
		case NG_L2CAP_CMD_REJ:
		case NG_L2CAP_DISCON_RSP:
		case NG_L2CAP_ECHO_RSP:
		case NG_L2CAP_INFO_RSP:
			break;

		case NG_L2CAP_CON_REQ:
			ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token, result, 0);
			break;

		case NG_L2CAP_CON_RSP:
			if (cmd->ch != NULL)
				ng_l2cap_l2ca_con_rsp_rsp(cmd->ch, cmd->token,
					result);
			break;

		case NG_L2CAP_CFG_REQ:
		case NG_L2CAP_CFG_RSP:
		case NGM_L2CAP_L2CA_WRITE:
			ng_l2cap_l2ca_discon_ind(cmd->ch);
			break;

		case NG_L2CAP_DISCON_REQ:
			ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token,
				NG_L2CAP_SUCCESS);
			break;

		case NG_L2CAP_ECHO_REQ:
			ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token,
				result, NULL);
			break;

		case NG_L2CAP_INFO_REQ:
			ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
				result, NULL);
			break;

		/* XXX FIXME add other commands */

		default:
			KASSERT(0,
("%s: %s - unexpected command code=%d\n",
				__func__, NG_NODE_NAME(con->l2cap->node),
				cmd->code));
			break;
		}

		if (cmd->ch != NULL)
			ng_l2cap_free_chan(cmd->ch);

		ng_l2cap_free_cmd(cmd);
	}

	/*
	 * There still might be channels (in OPEN state?) that
	 * did not submit any commands, so diconnect them
	 */

	LIST_FOREACH(ch, &l2cap->chan_list, next)
		if (ch->con == con)
			ng_l2cap_l2ca_discon_ind(ch);

	/* Free connection descriptor */
	ng_l2cap_free_con(con);
} /* ng_l2cap_con_fail */

/*
 * Process L2CAP command timeout. In general - notify upper layer and destroy
 * channel. Do not pay much attension to return code, just do our best.
 */

void
ng_l2cap_process_command_timeout(node_p node, hook_p hook, void *arg1, int arg2)
{
	ng_l2cap_cmd_p	cmd = (ng_l2cap_cmd_p) arg1;

	KASSERT((cmd->flags & NG_L2CAP_CMD_PENDING),
("%s: %s - invalid command flags flags=%#x!\n",
		__func__, NG_NODE_NAME(cmd->con->l2cap->node), cmd->flags));

	cmd->flags &= ~NG_L2CAP_CMD_PENDING;
	ng_l2cap_unlink_cmd(cmd);

	switch (cmd->code) {
 	case NG_L2CAP_CON_REQ:
		ng_l2cap_l2ca_con_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT, 0);
		ng_l2cap_free_chan(cmd->ch); 
		break;

	case NG_L2CAP_CFG_REQ:
		ng_l2cap_l2ca_cfg_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT);
		break;

 	case NG_L2CAP_DISCON_REQ:
		ng_l2cap_l2ca_discon_rsp(cmd->ch, cmd->token, NG_L2CAP_TIMEOUT);
		ng_l2cap_free_chan(cmd->ch); /* XXX free channel */
		break;

	case NG_L2CAP_ECHO_REQ:
		/* Echo request timed out. Let the upper layer know */
		ng_l2cap_l2ca_ping_rsp(cmd->con, cmd->token, 
			NG_L2CAP_TIMEOUT, NULL);
		break;

	case NG_L2CAP_INFO_REQ:
		/* Info request timed out. Let the upper layer know */
		ng_l2cap_l2ca_get_info_rsp(cmd->con, cmd->token,
			NG_L2CAP_TIMEOUT, NULL);
		break;

	/* XXX FIXME add other commands */

	default:
		KASSERT(0,
("%s: %s - unexpected command code=%d\n",
			__func__, NG_NODE_NAME(cmd->con->l2cap->node),
			cmd->code));
		break;
	}

	ng_l2cap_free_cmd(cmd);
} /* ng_l2cap_process_command_timeout */

OpenPOWER on IntegriCloud