1 /*********************************************************************
3 * Filename: parameters.c
5 * Description: A more general way to handle (pi,pl,pv) parameters
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Mon Jun 7 10:25:11 1999
9 * Modified at: Sun Jan 30 14:08:39 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1999-2000 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 ********************************************************************/
31 #include <linux/types.h>
32 #include <linux/module.h>
34 #include <asm/unaligned.h>
35 #include <asm/byteorder.h>
37 #include <net/irda/irda.h>
38 #include <net/irda/parameters.h>
40 static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
41 PV_TYPE type, PI_HANDLER func);
42 static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
43 PV_TYPE type, PI_HANDLER func);
44 static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
45 PV_TYPE type, PI_HANDLER func);
46 static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
47 PV_TYPE type, PI_HANDLER func);
49 static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
50 PV_TYPE type, PI_HANDLER func);
51 static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
52 PV_TYPE type, PI_HANDLER func);
54 /* Parameter value call table. Must match PV_TYPE */
55 static PV_HANDLER pv_extract_table[] = {
56 irda_extract_integer, /* Handler for any length integers */
57 irda_extract_integer, /* Handler for 8 bits integers */
58 irda_extract_integer, /* Handler for 16 bits integers */
59 irda_extract_string, /* Handler for strings */
60 irda_extract_integer, /* Handler for 32 bits integers */
61 irda_extract_octseq, /* Handler for octet sequences */
62 irda_extract_no_value /* Handler for no value parameters */
65 static PV_HANDLER pv_insert_table[] = {
66 irda_insert_integer, /* Handler for any length integers */
67 irda_insert_integer, /* Handler for 8 bits integers */
68 irda_insert_integer, /* Handler for 16 bits integers */
69 NULL, /* Handler for strings */
70 irda_insert_integer, /* Handler for 32 bits integers */
71 NULL, /* Handler for octet sequences */
72 irda_insert_no_value /* Handler for no value parameters */
76 * Function irda_insert_no_value (self, buf, len, pi, type, func)
78 static int irda_insert_no_value(void *self, __u8 *buf, int len, __u8 pi,
79 PV_TYPE type, PI_HANDLER func)
87 /* Call handler for this parameter */
88 ret = (*func)(self, &p, PV_GET);
90 /* Extract values anyway, since handler may need them */
91 irda_param_pack(buf, "bb", p.pi, p.pl);
96 return 2; /* Inserted pl+2 bytes */
100 * Function irda_extract_no_value (self, buf, len, type, func)
102 * Extracts a parameter without a pv field (pl=0)
105 static int irda_extract_no_value(void *self, __u8 *buf, int len, __u8 pi,
106 PV_TYPE type, PI_HANDLER func)
111 /* Extract values anyway, since handler may need them */
112 irda_param_unpack(buf, "bb", &p.pi, &p.pl);
114 /* Call handler for this parameter */
115 ret = (*func)(self, &p, PV_PUT);
120 return 2; /* Extracted pl+2 bytes */
124 * Function irda_insert_integer (self, buf, len, pi, type, func)
126 static int irda_insert_integer(void *self, __u8 *buf, int len, __u8 pi,
127 PV_TYPE type, PI_HANDLER func)
133 p.pi = pi; /* In case handler needs to know */
134 p.pl = type & PV_MASK; /* The integer type codes the lenght as well */
135 p.pv.i = 0; /* Clear value */
137 /* Call handler for this parameter */
138 err = (*func)(self, &p, PV_GET);
143 * If parameter lenght is still 0, then (1) this is an any length
144 * integer, and (2) the handler function does not care which length
145 * we choose to use, so we pick the one the gives the fewest bytes.
149 IRDA_DEBUG(2, "%s(), using 1 byte\n", __FUNCTION__);
151 } else if (p.pv.i < 0xffff) {
152 IRDA_DEBUG(2, "%s(), using 2 bytes\n", __FUNCTION__);
155 IRDA_DEBUG(2, "%s(), using 4 bytes\n", __FUNCTION__);
156 p.pl = 4; /* Default length */
159 /* Check if buffer is long enough for insertion */
160 if (len < (2+p.pl)) {
161 WARNING("%s: buffer to short for insertion!\n", __FUNCTION__);
164 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __FUNCTION__,
168 n += irda_param_pack(buf, "bbb", p.pi, p.pl, (__u8) p.pv.i);
171 if (type & PV_BIG_ENDIAN)
172 p.pv.i = cpu_to_be16((__u16) p.pv.i);
174 p.pv.i = cpu_to_le16((__u16) p.pv.i);
175 n += irda_param_pack(buf, "bbs", p.pi, p.pl, (__u16) p.pv.i);
178 if (type & PV_BIG_ENDIAN)
179 cpu_to_be32s(&p.pv.i);
181 cpu_to_le32s(&p.pv.i);
182 n += irda_param_pack(buf, "bbi", p.pi, p.pl, p.pv.i);
186 WARNING("%s: length %d not supported\n", __FUNCTION__, p.pl);
191 return p.pl+2; /* Inserted pl+2 bytes */
195 * Function irda_extract integer (self, buf, len, pi, type, func)
197 * Extract a possibly variable length integer from buffer, and call
198 * handler for processing of the parameter
200 static int irda_extract_integer(void *self, __u8 *buf, int len, __u8 pi,
201 PV_TYPE type, PI_HANDLER func)
205 int extract_len; /* Real lenght we extract */
208 p.pi = pi; /* In case handler needs to know */
209 p.pl = buf[1]; /* Extract lenght of value */
210 p.pv.i = 0; /* Clear value */
211 extract_len = p.pl; /* Default : extract all */
213 /* Check if buffer is long enough for parsing */
214 if (len < (2+p.pl)) {
215 WARNING("%s: buffer to short for parsing! "
216 "Need %d bytes, but len is only %d\n",
217 __FUNCTION__, p.pl, len);
222 * Check that the integer length is what we expect it to be. If the
223 * handler want a 16 bits integer then a 32 bits is not good enough
224 * PV_INTEGER means that the handler is flexible.
226 if (((type & PV_MASK) != PV_INTEGER) && ((type & PV_MASK) != p.pl)) {
227 ERROR("%s: invalid parameter length! "
228 "Expected %d bytes, but value had %d bytes!\n",
229 __FUNCTION__, type & PV_MASK, p.pl);
231 /* Most parameters are bit/byte fields or little endian,
232 * so it's ok to only extract a subset of it (the subset
233 * that the handler expect). This is necessary, as some
234 * broken implementations seems to add extra undefined bits.
235 * If the parameter is shorter than we expect or is big
236 * endian, we can't play those tricks. Jean II */
237 if((p.pl < (type & PV_MASK)) || (type & PV_BIG_ENDIAN)) {
241 /* Extract subset of it, fallthrough */
242 extract_len = type & PV_MASK;
247 switch (extract_len) {
249 n += irda_param_unpack(buf+2, "b", &p.pv.i);
252 n += irda_param_unpack(buf+2, "s", &p.pv.i);
253 if (type & PV_BIG_ENDIAN)
254 p.pv.i = be16_to_cpu((__u16) p.pv.i);
256 p.pv.i = le16_to_cpu((__u16) p.pv.i);
259 n += irda_param_unpack(buf+2, "i", &p.pv.i);
260 if (type & PV_BIG_ENDIAN)
261 be32_to_cpus(&p.pv.i);
263 le32_to_cpus(&p.pv.i);
266 WARNING("%s: length %d not supported\n", __FUNCTION__, p.pl);
272 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d, pi=%d\n", __FUNCTION__,
274 /* Call handler for this parameter */
275 err = (*func)(self, &p, PV_PUT);
279 return p.pl+2; /* Extracted pl+2 bytes */
283 * Function irda_extract_string (self, buf, len, type, func)
285 static int irda_extract_string(void *self, __u8 *buf, int len, __u8 pi,
286 PV_TYPE type, PI_HANDLER func)
292 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
294 p.pi = pi; /* In case handler needs to know */
295 p.pl = buf[1]; /* Extract lenght of value */
297 IRDA_DEBUG(2, "%s(), pi=%#x, pl=%d\n", __FUNCTION__,
300 /* Check if buffer is long enough for parsing */
301 if (len < (2+p.pl)) {
302 WARNING("%s: buffer to short for parsing! "
303 "Need %d bytes, but len is only %d\n",
304 __FUNCTION__, p.pl, len);
308 /* Should be safe to copy string like this since we have already
309 * checked that the buffer is long enough */
310 strncpy(str, buf+2, p.pl);
312 IRDA_DEBUG(2, "%s(), str=0x%02x 0x%02x\n", __FUNCTION__,
313 (__u8) str[0], (__u8) str[1]);
315 /* Null terminate string */
318 p.pv.c = str; /* Handler will need to take a copy */
320 /* Call handler for this parameter */
321 err = (*func)(self, &p, PV_PUT);
325 return p.pl+2; /* Extracted pl+2 bytes */
329 * Function irda_extract_octseq (self, buf, len, type, func)
331 static int irda_extract_octseq(void *self, __u8 *buf, int len, __u8 pi,
332 PV_TYPE type, PI_HANDLER func)
336 p.pi = pi; /* In case handler needs to know */
337 p.pl = buf[1]; /* Extract lenght of value */
339 /* Check if buffer is long enough for parsing */
340 if (len < (2+p.pl)) {
341 WARNING("%s: buffer to short for parsing! "
342 "Need %d bytes, but len is only %d\n",
343 __FUNCTION__, p.pl, len);
347 IRDA_DEBUG(0, "%s(), not impl\n", __FUNCTION__);
349 return p.pl+2; /* Extracted pl+2 bytes */
353 * Function irda_param_pack (skb, fmt, ...)
356 * 'i' = 32 bits integer
360 int irda_param_pack(__u8 *buf, char *fmt, ...)
369 for (p = fmt; *p != '\0'; p++) {
371 case 'b': /* 8 bits unsigned byte */
372 buf[n++] = (__u8)va_arg(args, int);
374 case 's': /* 16 bits unsigned short */
375 arg.i = (__u16)va_arg(args, int);
376 put_unaligned((__u16)arg.i, (__u16 *)(buf+n)); n+=2;
378 case 'i': /* 32 bits unsigned integer */
379 arg.i = va_arg(args, __u32);
380 put_unaligned(arg.i, (__u32 *)(buf+n)); n+=4;
383 case 'c': /* \0 terminated string */
384 arg.c = va_arg(args, char *);
385 strcpy(buf+n, arg.c);
386 n += strlen(arg.c) + 1;
398 EXPORT_SYMBOL(irda_param_pack);
401 * Function irda_param_unpack (skb, fmt, ...)
403 int irda_param_unpack(__u8 *buf, char *fmt, ...)
412 for (p = fmt; *p != '\0'; p++) {
414 case 'b': /* 8 bits byte */
415 arg.ip = va_arg(args, __u32 *);
418 case 's': /* 16 bits short */
419 arg.ip = va_arg(args, __u32 *);
420 *arg.ip = get_unaligned((__u16 *)(buf+n)); n+=2;
422 case 'i': /* 32 bits unsigned integer */
423 arg.ip = va_arg(args, __u32 *);
424 *arg.ip = get_unaligned((__u32 *)(buf+n)); n+=4;
427 case 'c': /* \0 terminated string */
428 arg.c = va_arg(args, char *);
429 strcpy(arg.c, buf+n);
430 n += strlen(arg.c) + 1;
443 EXPORT_SYMBOL(irda_param_unpack);
446 * Function irda_param_insert (self, pi, buf, len, info)
448 * Insert the specified parameter (pi) into buffer. Returns number of
451 int irda_param_insert(void *self, __u8 pi, __u8 *buf, int len,
452 pi_param_info_t *info)
454 pi_minor_info_t *pi_minor_info;
461 ASSERT(buf != NULL, return ret;);
462 ASSERT(info != 0, return ret;);
464 pi_minor = pi & info->pi_mask;
465 pi_major = pi >> info->pi_major_offset;
467 /* Check if the identifier value (pi) is valid */
468 if ((pi_major > info->len-1) ||
469 (pi_minor > info->tables[pi_major].len-1))
471 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
474 /* Skip this parameter */
478 /* Lookup the info on how to parse this parameter */
479 pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
481 /* Find expected data type for this parameter identifier (pi)*/
482 type = pi_minor_info->type;
484 /* Check if handler has been implemented */
485 if (!pi_minor_info->func) {
486 MESSAGE("%s: no handler for pi=%#x\n", __FUNCTION__, pi);
487 /* Skip this parameter */
491 /* Insert parameter value */
492 ret = (*pv_insert_table[type & PV_MASK])(self, buf+n, len, pi, type,
493 pi_minor_info->func);
496 EXPORT_SYMBOL(irda_param_insert);
499 * Function irda_param_extract_all (self, buf, len, info)
501 * Parse all parameters. If len is correct, then everything should be
502 * safe. Returns the number of bytes that was parsed
505 int irda_param_extract(void *self, __u8 *buf, int len, pi_param_info_t *info)
507 pi_minor_info_t *pi_minor_info;
514 ASSERT(buf != NULL, return ret;);
515 ASSERT(info != 0, return ret;);
517 pi_minor = buf[n] & info->pi_mask;
518 pi_major = buf[n] >> info->pi_major_offset;
520 /* Check if the identifier value (pi) is valid */
521 if ((pi_major > info->len-1) ||
522 (pi_minor > info->tables[pi_major].len-1))
524 IRDA_DEBUG(0, "%s(), no handler for parameter=0x%02x\n",
525 __FUNCTION__, buf[0]);
527 /* Skip this parameter */
528 return 2 + buf[n + 1]; /* Continue */
531 /* Lookup the info on how to parse this parameter */
532 pi_minor_info = &info->tables[pi_major].pi_minor_call_table[pi_minor];
534 /* Find expected data type for this parameter identifier (pi)*/
535 type = pi_minor_info->type;
537 IRDA_DEBUG(3, "%s(), pi=[%d,%d], type=%d\n", __FUNCTION__,
538 pi_major, pi_minor, type);
540 /* Check if handler has been implemented */
541 if (!pi_minor_info->func) {
542 MESSAGE("%s: no handler for pi=%#x\n", __FUNCTION__, buf[n]);
543 /* Skip this parameter */
544 return 2 + buf[n + 1]; /* Continue */
547 /* Parse parameter value */
548 ret = (*pv_extract_table[type & PV_MASK])(self, buf+n, len, buf[n],
549 type, pi_minor_info->func);
552 EXPORT_SYMBOL(irda_param_extract);
555 * Function irda_param_extract_all (self, buf, len, info)
557 * Parse all parameters. If len is correct, then everything should be
558 * safe. Returns the number of bytes that was parsed
561 int irda_param_extract_all(void *self, __u8 *buf, int len,
562 pi_param_info_t *info)
567 ASSERT(buf != NULL, return ret;);
568 ASSERT(info != 0, return ret;);
571 * Parse all parameters. Each parameter must be at least two bytes
572 * long or else there is no point in trying to parse it
575 ret = irda_param_extract(self, buf+n, len, info);
584 EXPORT_SYMBOL(irda_param_extract_all);