6 * Copyright (c) Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: ng_l2cap_cmds.h,v 1.4 2003/04/01 18:15:26 max Exp $
31 * $FreeBSD: src/sys/netgraph/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.5 2005/01/07 01:45:43 imp Exp $
32 * $DragonFly: src/sys/netgraph7/bluetooth/l2cap/ng_l2cap_cmds.h,v 1.2 2008/06/26 23:05:40 dillon Exp $
35 #ifndef _NETGRAPH_L2CAP_CMDS_H_
36 #define _NETGRAPH_L2CAP_CMDS_H_
38 /******************************************************************************
39 ******************************************************************************
40 ** L2CAP to L2CAP signaling command macros
41 ******************************************************************************
42 ******************************************************************************/
45 * Note: All L2CAP implementations are required to support minimal signaling
46 * MTU of 48 bytes. In order to simplify things we will send one command
47 * per one L2CAP packet. Given evrything above we can assume that one
48 * signaling packet will fit into single mbuf.
51 /* L2CAP_CommandRej */
52 #define _ng_l2cap_cmd_rej(_m, _ident, _reason, _mtu, _scid, _dcid) \
55 ng_l2cap_cmd_hdr_t hdr; \
56 ng_l2cap_cmd_rej_cp param; \
57 ng_l2cap_cmd_rej_data_t data; \
58 } __attribute__ ((packed)) *c = NULL; \
60 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
64 c = mtod((_m), struct _cmd_rej *); \
65 c->hdr.code = NG_L2CAP_CMD_REJ; \
66 c->hdr.ident = (_ident); \
67 c->hdr.length = sizeof(c->param); \
69 c->param.reason = htole16((_reason)); \
71 if ((_reason) == NG_L2CAP_REJ_MTU_EXCEEDED) { \
72 c->data.mtu.mtu = htole16((_mtu)); \
73 c->hdr.length += sizeof(c->data.mtu); \
74 } else if ((_reason) == NG_L2CAP_REJ_INVALID_CID) { \
75 c->data.cid.scid = htole16((_scid)); \
76 c->data.cid.dcid = htole16((_dcid)); \
77 c->hdr.length += sizeof(c->data.cid); \
80 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) + \
83 c->hdr.length = htole16(c->hdr.length); \
86 /* L2CAP_ConnectReq */
87 #define _ng_l2cap_con_req(_m, _ident, _psm, _scid) \
90 ng_l2cap_cmd_hdr_t hdr; \
91 ng_l2cap_con_req_cp param; \
92 } __attribute__ ((packed)) *c = NULL; \
94 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
98 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
100 c = mtod((_m), struct _con_req *); \
101 c->hdr.code = NG_L2CAP_CON_REQ; \
102 c->hdr.ident = (_ident); \
103 c->hdr.length = htole16(sizeof(c->param)); \
105 c->param.psm = htole16((_psm)); \
106 c->param.scid = htole16((_scid)); \
109 /* L2CAP_ConnectRsp */
110 #define _ng_l2cap_con_rsp(_m, _ident, _dcid, _scid, _result, _status) \
113 ng_l2cap_cmd_hdr_t hdr; \
114 ng_l2cap_con_rsp_cp param; \
115 } __attribute__ ((packed)) *c = NULL; \
117 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
121 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
123 c = mtod((_m), struct _con_rsp *); \
124 c->hdr.code = NG_L2CAP_CON_RSP; \
125 c->hdr.ident = (_ident); \
126 c->hdr.length = htole16(sizeof(c->param)); \
128 c->param.dcid = htole16((_dcid)); \
129 c->param.scid = htole16((_scid)); \
130 c->param.result = htole16((_result)); \
131 c->param.status = htole16((_status)); \
134 /* L2CAP_ConfigReq */
135 #define _ng_l2cap_cfg_req(_m, _ident, _dcid, _flags, _data) \
138 ng_l2cap_cmd_hdr_t hdr; \
139 ng_l2cap_cfg_req_cp param; \
140 } __attribute__ ((packed)) *c = NULL; \
142 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
143 if ((_m) == NULL) { \
144 NG_FREE_M((_data)); \
148 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
150 c = mtod((_m), struct _cfg_req *); \
151 c->hdr.code = NG_L2CAP_CFG_REQ; \
152 c->hdr.ident = (_ident); \
153 c->hdr.length = sizeof(c->param); \
155 c->param.dcid = htole16((_dcid)); \
156 c->param.flags = htole16((_flags)); \
157 if ((_data) != NULL) { \
158 int l = (_data)->m_pkthdr.len; \
160 m_cat((_m), (_data)); \
161 c->hdr.length += l; \
162 (_m)->m_pkthdr.len += l; \
165 c->hdr.length = htole16(c->hdr.length); \
168 /* L2CAP_ConfigRsp */
169 #define _ng_l2cap_cfg_rsp(_m, _ident, _scid, _flags, _result, _data) \
172 ng_l2cap_cmd_hdr_t hdr; \
173 ng_l2cap_cfg_rsp_cp param; \
174 } __attribute__ ((packed)) *c = NULL; \
176 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
177 if ((_m) == NULL) { \
178 NG_FREE_M((_data)); \
182 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
184 c = mtod((_m), struct _cfg_rsp *); \
185 c->hdr.code = NG_L2CAP_CFG_RSP; \
186 c->hdr.ident = (_ident); \
187 c->hdr.length = sizeof(c->param); \
189 c->param.scid = htole16((_scid)); \
190 c->param.flags = htole16((_flags)); \
191 c->param.result = htole16((_result)); \
192 if ((_data) != NULL) { \
193 int l = (_data)->m_pkthdr.len; \
195 m_cat((_m), (_data)); \
196 c->hdr.length += l; \
197 (_m)->m_pkthdr.len += l; \
200 c->hdr.length = htole16(c->hdr.length); \
203 /* Build configuration options */
204 #define _ng_l2cap_build_cfg_options(_m, _mtu, _flush_timo, _flow) \
206 u_int8_t *p = NULL; \
208 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
212 (_m)->m_pkthdr.len = (_m)->m_len = 0; \
213 p = mtod((_m), u_int8_t *); \
215 if ((_mtu) != NULL) { \
216 struct _cfg_opt_mtu { \
217 ng_l2cap_cfg_opt_t hdr; \
219 } __attribute__ ((packed)) *o = NULL; \
221 o = (struct _cfg_opt_mtu *) p; \
222 o->hdr.type = NG_L2CAP_OPT_MTU; \
223 o->hdr.length = sizeof(o->val); \
224 o->val = htole16(*(u_int16_t *)(_mtu)); \
226 (_m)->m_pkthdr.len += sizeof(*o); \
230 if ((_flush_timo) != NULL) { \
231 struct _cfg_opt_flush { \
232 ng_l2cap_cfg_opt_t hdr; \
234 } __attribute__ ((packed)) *o = NULL; \
236 o = (struct _cfg_opt_flush *) p; \
237 o->hdr.type = NG_L2CAP_OPT_FLUSH_TIMO; \
238 o->hdr.length = sizeof(o->val); \
239 o->val = htole16(*(u_int16_t *)(_flush_timo)); \
241 (_m)->m_pkthdr.len += sizeof(*o); \
245 if ((_flow) != NULL) { \
246 struct _cfg_opt_flow { \
247 ng_l2cap_cfg_opt_t hdr; \
248 ng_l2cap_flow_t val; \
249 } __attribute__ ((packed)) *o = NULL; \
251 o = (struct _cfg_opt_flow *) p; \
252 o->hdr.type = NG_L2CAP_OPT_QOS; \
253 o->hdr.length = sizeof(o->val); \
254 o->val.flags = ((ng_l2cap_flow_p)(_flow))->flags; \
255 o->val.service_type = ((ng_l2cap_flow_p) \
256 (_flow))->service_type; \
257 o->val.token_rate = \
258 htole32(((ng_l2cap_flow_p)(_flow))->token_rate);\
259 o->val.token_bucket_size = \
260 htole32(((ng_l2cap_flow_p) \
261 (_flow))->token_bucket_size); \
262 o->val.peak_bandwidth = \
263 htole32(((ng_l2cap_flow_p) \
264 (_flow))->peak_bandwidth); \
265 o->val.latency = htole32(((ng_l2cap_flow_p) \
266 (_flow))->latency); \
267 o->val.delay_variation = \
268 htole32(((ng_l2cap_flow_p) \
269 (_flow))->delay_variation); \
271 (_m)->m_pkthdr.len += sizeof(*o); \
274 (_m)->m_len = (_m)->m_pkthdr.len; \
277 /* L2CAP_DisconnectReq */
278 #define _ng_l2cap_discon_req(_m, _ident, _dcid, _scid) \
280 struct _discon_req { \
281 ng_l2cap_cmd_hdr_t hdr; \
282 ng_l2cap_discon_req_cp param; \
283 } __attribute__ ((packed)) *c = NULL; \
285 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
289 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
291 c = mtod((_m), struct _discon_req *); \
292 c->hdr.code = NG_L2CAP_DISCON_REQ; \
293 c->hdr.ident = (_ident); \
294 c->hdr.length = htole16(sizeof(c->param)); \
296 c->param.dcid = htole16((_dcid)); \
297 c->param.scid = htole16((_scid)); \
300 /* L2CA_DisconnectRsp */
301 #define _ng_l2cap_discon_rsp(_m, _ident, _dcid, _scid) \
303 struct _discon_rsp { \
304 ng_l2cap_cmd_hdr_t hdr; \
305 ng_l2cap_discon_rsp_cp param; \
306 } __attribute__ ((packed)) *c = NULL; \
308 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
312 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
314 c = mtod((_m), struct _discon_rsp *); \
315 c->hdr.code = NG_L2CAP_DISCON_RSP; \
316 c->hdr.ident = (_ident); \
317 c->hdr.length = htole16(sizeof(c->param)); \
319 c->param.dcid = htole16((_dcid)); \
320 c->param.scid = htole16((_scid)); \
324 #define _ng_l2cap_echo_req(_m, _ident, _data, _size) \
326 ng_l2cap_cmd_hdr_t *c = NULL; \
328 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
332 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
334 c = mtod((_m), ng_l2cap_cmd_hdr_t *); \
335 c->code = NG_L2CAP_ECHO_REQ; \
336 c->ident = (_ident); \
339 if ((_data) != NULL) { \
340 m_copyback((_m), sizeof(*c), (_size), (_data)); \
341 c->length += (_size); \
344 c->length = htole16(c->length); \
348 #define _ng_l2cap_info_req(_m, _ident, _type) \
351 ng_l2cap_cmd_hdr_t hdr; \
352 ng_l2cap_info_req_cp param; \
353 } __attribute__ ((packed)) *c = NULL; \
355 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
359 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(*c); \
361 c = mtod((_m), struct _info_req *); \
362 c->hdr.code = NG_L2CAP_INFO_REQ; \
363 c->hdr.ident = (_ident); \
364 c->hdr.length = htole16(sizeof(c->param)); \
366 c->param.type = htole16((_type)); \
370 #define _ng_l2cap_info_rsp(_m, _ident, _type, _result, _mtu) \
373 ng_l2cap_cmd_hdr_t hdr; \
374 ng_l2cap_info_rsp_cp param; \
375 ng_l2cap_info_rsp_data_t data; \
376 } __attribute__ ((packed)) *c = NULL; \
378 MGETHDR((_m), MB_DONTWAIT, MT_DATA); \
382 c = mtod((_m), struct _info_rsp *); \
383 c->hdr.code = NG_L2CAP_INFO_REQ; \
384 c->hdr.ident = (_ident); \
385 c->hdr.length = sizeof(c->param); \
387 c->param.type = htole16((_type)); \
388 c->param.result = htole16((_result)); \
390 if ((_result) == NG_L2CAP_SUCCESS) { \
392 case NG_L2CAP_CONNLESS_MTU: \
393 c->data.mtu.mtu = htole16((_mtu)); \
394 c->hdr.length += sizeof((c->data.mtu.mtu)); \
399 (_m)->m_pkthdr.len = (_m)->m_len = sizeof(c->hdr) + \
402 c->hdr.length = htole16(c->hdr.length); \
405 void ng_l2cap_con_wakeup (ng_l2cap_con_p);
406 void ng_l2cap_con_fail (ng_l2cap_con_p, u_int16_t);
407 void ng_l2cap_process_command_timeout (node_p, hook_p, void *, int);
409 #endif /* ndef _NETGRAPH_L2CAP_CMDS_H_ */