parser.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2000-2002 Maxim Krasnyansky <maxk@qualcomm.com>
  7. * Copyright (C) 2003-2011 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <ctype.h>
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "parser.h"
  22. #include "rfcomm.h"
  23. struct parser_t parser;
  24. void init_parser(unsigned long flags, unsigned long filter,
  25. unsigned short defpsm, unsigned short defcompid,
  26. int pppdump_fd, int audio_fd)
  27. {
  28. if ((flags & DUMP_RAW) && !(flags & DUMP_TYPE_MASK))
  29. flags |= DUMP_HEX;
  30. parser.flags = flags;
  31. parser.filter = filter;
  32. parser.defpsm = defpsm;
  33. parser.defcompid = defcompid;
  34. parser.state = 0;
  35. parser.pppdump_fd = pppdump_fd;
  36. parser.audio_fd = audio_fd;
  37. }
  38. #define PROTO_TABLE_SIZE 20
  39. static struct {
  40. uint16_t handle;
  41. uint16_t psm;
  42. uint8_t channel;
  43. uint32_t proto;
  44. } proto_table[PROTO_TABLE_SIZE];
  45. void set_proto(uint16_t handle, uint16_t psm, uint8_t channel, uint32_t proto)
  46. {
  47. int i, pos = -1;
  48. if (psm > 0 && psm < 0x1000 && !channel)
  49. return;
  50. if (!psm && channel)
  51. psm = RFCOMM_PSM;
  52. for (i = 0; i < PROTO_TABLE_SIZE; i++) {
  53. if (proto_table[i].handle == handle && proto_table[i].psm == psm && proto_table[i].channel == channel) {
  54. pos = i;
  55. break;
  56. }
  57. if (pos < 0 && !proto_table[i].handle && !proto_table[i].psm && !proto_table[i].channel)
  58. pos = i;
  59. }
  60. if (pos < 0)
  61. return;
  62. proto_table[pos].handle = handle;
  63. proto_table[pos].psm = psm;
  64. proto_table[pos].channel = channel;
  65. proto_table[pos].proto = proto;
  66. }
  67. uint32_t get_proto(uint16_t handle, uint16_t psm, uint8_t channel)
  68. {
  69. int i, pos = -1;
  70. if (!psm && channel)
  71. psm = RFCOMM_PSM;
  72. for (i = 0; i < PROTO_TABLE_SIZE; i++) {
  73. if (proto_table[i].handle == handle && proto_table[i].psm == psm && proto_table[i].channel == channel)
  74. return proto_table[i].proto;
  75. if (!proto_table[i].handle) {
  76. if (proto_table[i].psm == psm && proto_table[i].channel == channel)
  77. pos = i;
  78. }
  79. }
  80. return (pos < 0) ? 0 : proto_table[pos].proto;
  81. }
  82. #define FRAME_TABLE_SIZE 20
  83. static struct {
  84. uint16_t handle;
  85. uint8_t dlci;
  86. uint8_t opcode;
  87. uint8_t status;
  88. struct frame frm;
  89. } frame_table[FRAME_TABLE_SIZE];
  90. void del_frame(uint16_t handle, uint8_t dlci)
  91. {
  92. int i;
  93. for (i = 0; i < FRAME_TABLE_SIZE; i++)
  94. if (frame_table[i].handle == handle &&
  95. frame_table[i].dlci == dlci) {
  96. frame_table[i].handle = 0;
  97. frame_table[i].dlci = 0;
  98. frame_table[i].opcode = 0;
  99. frame_table[i].status = 0;
  100. if (frame_table[i].frm.data)
  101. free(frame_table[i].frm.data);
  102. memset(&frame_table[i].frm, 0, sizeof(struct frame));
  103. break;
  104. }
  105. }
  106. struct frame *add_frame(struct frame *frm)
  107. {
  108. struct frame *fr;
  109. void *data;
  110. int i, pos = -1;
  111. for (i = 0; i < FRAME_TABLE_SIZE; i++) {
  112. if (frame_table[i].handle == frm->handle &&
  113. frame_table[i].dlci == frm->dlci) {
  114. pos = i;
  115. break;
  116. }
  117. if (pos < 0 && !frame_table[i].handle && !frame_table[i].dlci)
  118. pos = i;
  119. }
  120. if (pos < 0)
  121. return frm;
  122. frame_table[pos].handle = frm->handle;
  123. frame_table[pos].dlci = frm->dlci;
  124. fr = &frame_table[pos].frm;
  125. data = malloc(fr->len + frm->len);
  126. if (!data) {
  127. perror("Can't allocate frame stream buffer");
  128. del_frame(frm->handle, frm->dlci);
  129. return frm;
  130. }
  131. if (fr->len > 0)
  132. memcpy(data, fr->ptr, fr->len);
  133. if (frm->len > 0)
  134. memcpy(data + fr->len, frm->ptr, frm->len);
  135. if (fr->data)
  136. free(fr->data);
  137. fr->data = data;
  138. fr->data_len = fr->len + frm->len;
  139. fr->len = fr->data_len;
  140. fr->ptr = fr->data;
  141. fr->dev_id = frm->dev_id;
  142. fr->in = frm->in;
  143. fr->ts = frm->ts;
  144. fr->handle = frm->handle;
  145. fr->cid = frm->cid;
  146. fr->num = frm->num;
  147. fr->dlci = frm->dlci;
  148. fr->channel = frm->channel;
  149. fr->pppdump_fd = frm->pppdump_fd;
  150. fr->audio_fd = frm->audio_fd;
  151. return fr;
  152. }
  153. uint8_t get_opcode(uint16_t handle, uint8_t dlci)
  154. {
  155. int i;
  156. for (i = 0; i < FRAME_TABLE_SIZE; i++)
  157. if (frame_table[i].handle == handle &&
  158. frame_table[i].dlci == dlci)
  159. return frame_table[i].opcode;
  160. return 0x00;
  161. }
  162. void set_opcode(uint16_t handle, uint8_t dlci, uint8_t opcode)
  163. {
  164. int i;
  165. for (i = 0; i < FRAME_TABLE_SIZE; i++)
  166. if (frame_table[i].handle == handle &&
  167. frame_table[i].dlci == dlci) {
  168. frame_table[i].opcode = opcode;
  169. break;
  170. }
  171. }
  172. uint8_t get_status(uint16_t handle, uint8_t dlci)
  173. {
  174. int i;
  175. for (i = 0; i < FRAME_TABLE_SIZE; i++)
  176. if (frame_table[i].handle == handle &&
  177. frame_table[i].dlci == dlci)
  178. return frame_table[i].status;
  179. return 0x00;
  180. }
  181. void set_status(uint16_t handle, uint8_t dlci, uint8_t status)
  182. {
  183. int i;
  184. for (i = 0; i < FRAME_TABLE_SIZE; i++)
  185. if (frame_table[i].handle == handle &&
  186. frame_table[i].dlci == dlci) {
  187. frame_table[i].status = status;
  188. break;
  189. }
  190. }
  191. void ascii_dump(int level, struct frame *frm, int num)
  192. {
  193. unsigned char *buf = frm->ptr;
  194. register int i, n;
  195. if ((num < 0) || (num > (int) frm->len))
  196. num = frm->len;
  197. for (i = 0, n = 1; i < num; i++, n++) {
  198. if (n == 1)
  199. p_indent(level, frm);
  200. printf("%1c ", isprint(buf[i]) ? buf[i] : '.');
  201. if (n == DUMP_WIDTH) {
  202. printf("\n");
  203. n = 0;
  204. }
  205. }
  206. if (i && n != 1)
  207. printf("\n");
  208. }
  209. void hex_dump(int level, struct frame *frm, int num)
  210. {
  211. unsigned char *buf = frm->ptr;
  212. register int i, n;
  213. if ((num < 0) || (num > (int) frm->len))
  214. num = frm->len;
  215. for (i = 0, n = 1; i < num; i++, n++) {
  216. if (n == 1)
  217. p_indent(level, frm);
  218. printf("%2.2X ", buf[i]);
  219. if (n == DUMP_WIDTH) {
  220. printf("\n");
  221. n = 0;
  222. }
  223. }
  224. if (i && n != 1)
  225. printf("\n");
  226. }
  227. void ext_dump(int level, struct frame *frm, int num)
  228. {
  229. unsigned char *buf = frm->ptr;
  230. register int i, n = 0, size;
  231. if ((num < 0) || (num > (int) frm->len))
  232. num = frm->len;
  233. while (num > 0) {
  234. p_indent(level, frm);
  235. printf("%04x: ", n);
  236. size = num > 16 ? 16 : num;
  237. for (i = 0; i < size; i++)
  238. printf("%02x%s", buf[i], (i + 1) % 8 ? " " : " ");
  239. for (i = size; i < 16; i++)
  240. printf(" %s", (i + 1) % 8 ? " " : " ");
  241. for (i = 0; i < size; i++)
  242. printf("%1c", isprint(buf[i]) ? buf[i] : '.');
  243. printf("\n");
  244. buf += size;
  245. num -= size;
  246. n += size;
  247. }
  248. }
  249. void raw_ndump(int level, struct frame *frm, int num)
  250. {
  251. if (!frm->len)
  252. return;
  253. switch (parser.flags & DUMP_TYPE_MASK) {
  254. case DUMP_ASCII:
  255. ascii_dump(level, frm, num);
  256. break;
  257. case DUMP_HEX:
  258. hex_dump(level, frm, num);
  259. break;
  260. case DUMP_EXT:
  261. ext_dump(level, frm, num);
  262. break;
  263. }
  264. }
  265. void raw_dump(int level, struct frame *frm)
  266. {
  267. raw_ndump(level, frm, -1);
  268. }