epan/dissectors/pidl/samr/samr.cnf cnf_dissect_lsa_BinaryString => lsarpc_dissect_str...
[wireshark-sm.git] / epan / dissectors / packet-bthsp.c
blob6a64bd2fef8668119af58ef2b1b654aa5b23b825
1 /* packet-bthsp.c
2 * Routines for Bluetooth Headset Profile (HSP)
4 * Copyright 2013, Michal Labedzki for Tieto Corporation
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "config.h"
15 #include <epan/packet.h>
16 #include <epan/prefs.h>
17 #include <epan/expert.h>
18 #include <epan/strutil.h>
19 #include <epan/unit_strings.h>
21 #include "packet-btrfcomm.h"
22 #include "packet-btsdp.h"
24 static int proto_bthsp;
26 static int hf_command;
27 static int hf_parameters;
28 static int hf_command_in;
29 static int hf_unsolicited;
30 static int hf_role;
31 static int hf_at_cmd;
32 static int hf_at_cmd_type;
33 static int hf_at_command_line_prefix;
34 static int hf_at_ignored;
35 static int hf_parameter;
36 static int hf_unknown_parameter;
37 static int hf_data;
38 static int hf_fragment;
39 static int hf_fragmented;
40 static int hf_vgs;
41 static int hf_vgm;
42 static int hf_ckpd;
44 static expert_field ei_non_mandatory_command;
45 static expert_field ei_invalid_usage;
46 static expert_field ei_unknown_parameter;
47 static expert_field ei_vgm_gain;
48 static expert_field ei_vgs_gain;
49 static expert_field ei_ckpd;
51 static int ett_bthsp;
52 static int ett_bthsp_command;
53 static int ett_bthsp_parameters;
55 static dissector_handle_t bthsp_handle;
57 static wmem_tree_t *fragments;
59 #define ROLE_UNKNOWN 0
60 #define ROLE_AG 1
61 #define ROLE_HS 2
63 #define TYPE_UNKNOWN 0x0000
64 #define TYPE_RESPONSE_ACK 0x0d0a
65 #define TYPE_RESPONSE 0x003a
66 #define TYPE_ACTION 0x003d
67 #define TYPE_ACTION_SIMPLY 0x000d
68 #define TYPE_READ 0x003f
69 #define TYPE_TEST 0x3d3f
71 static int hsp_role = ROLE_UNKNOWN;
73 enum reassemble_state_t {
74 REASSEMBLE_FRAGMENT,
75 REASSEMBLE_PARTIALLY,
76 REASSEMBLE_DONE
79 typedef struct _fragment_t {
80 uint32_t interface_id;
81 uint32_t adapter_id;
82 uint32_t chandle;
83 uint32_t dlci;
84 uint32_t role;
86 unsigned idx;
87 unsigned length;
88 uint8_t *data;
89 struct _fragment_t *previous_fragment;
91 unsigned reassemble_start_offset;
92 unsigned reassemble_end_offset;
93 enum reassemble_state_t reassemble_state;
94 } fragment_t;
96 typedef struct _at_cmd_t {
97 const char *name;
98 const char *long_name;
100 bool (*check_command)(int role, uint16_t type);
101 bool (*dissect_parameter)(tvbuff_t *tvb, packet_info *pinfo,
102 proto_tree *tree, int offset, int role, uint16_t type,
103 uint8_t *parameter_stream, unsigned parameter_number,
104 int parameter_length, void **data);
105 } at_cmd_t;
107 static const value_string role_vals[] = {
108 { ROLE_UNKNOWN, "Unknown" },
109 { ROLE_AG, "AG - Audio Gate" },
110 { ROLE_HS, "HS - Headset" },
111 { 0, NULL }
114 static const value_string at_cmd_type_vals[] = {
115 { 0x0d, "Action Command" },
116 { 0x3a, "Response" },
117 { 0x3d, "Action Command" },
118 { 0x3f, "Read Command" },
119 { 0x0d0a, "Response" },
120 { 0x3d3f, "Test Command" },
121 { 0, NULL }
124 static const enum_val_t pref_hsp_role[] = {
125 { "off", "Off", ROLE_UNKNOWN },
126 { "ag", "Sent is AG, Rcvd is HS", ROLE_AG },
127 { "hs", "Sent is HS, Rcvd is AG", ROLE_HS },
128 { NULL, NULL, 0 }
131 static const unit_name_string units_slash15 = { "/15", NULL };
133 void proto_register_bthsp(void);
134 void proto_reg_handoff_bthsp(void);
136 static uint32_t get_uint_parameter(uint8_t *parameter_stream, int parameter_length)
138 uint32_t value;
139 char *val;
141 val = (uint8_t *) wmem_alloc(wmem_packet_scope(), parameter_length + 1);
142 memcpy(val, parameter_stream, parameter_length);
143 val[parameter_length] = '\0';
144 value = (uint32_t) g_ascii_strtoull(val, NULL, 10);
146 return value;
149 static bool check_vgs(int role, uint16_t type) {
150 if (role == ROLE_HS && type == TYPE_ACTION) return true;
151 if (role == ROLE_AG && type == TYPE_RESPONSE) return true;
153 return false;
156 static bool check_vgm(int role, uint16_t type) {
157 if (role == ROLE_HS && type == TYPE_ACTION) return true;
158 if (role == ROLE_AG && type == TYPE_RESPONSE) return true;
160 return false;
163 static bool check_ckpd(int role, uint16_t type) {
164 if (role == ROLE_HS && type == TYPE_ACTION) return true;
166 return false;
169 static bool check_only_ag_role(int role, uint16_t type) {
170 if (role == ROLE_AG && type == TYPE_RESPONSE_ACK) return true;
172 return false;
175 static bool
176 dissect_vgs_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
177 int offset, int role, uint16_t type, uint8_t *parameter_stream,
178 unsigned parameter_number, int parameter_length, void **data _U_)
180 proto_item *pitem;
181 uint32_t value;
183 if (!check_vgs(role, type)) return false;
185 if (parameter_number > 0) return false;
187 value = get_uint_parameter(parameter_stream, parameter_length);
189 pitem = proto_tree_add_uint(tree, hf_vgs, tvb, offset, parameter_length, value);
191 if (value > 15) {
192 expert_add_info(pinfo, pitem, &ei_vgs_gain);
195 return true;
198 static bool
199 dissect_vgm_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
200 int offset, int role, uint16_t type, uint8_t *parameter_stream,
201 unsigned parameter_number, int parameter_length, void **data _U_)
203 proto_item *pitem;
204 uint32_t value;
206 if (!check_vgm(role, type)) return false;
208 if (parameter_number > 0) return false;
210 value = get_uint_parameter(parameter_stream, parameter_length);
212 pitem = proto_tree_add_uint(tree, hf_vgm, tvb, offset, parameter_length, value);
214 if (value > 15) {
215 expert_add_info(pinfo, pitem, &ei_vgm_gain);
218 return true;
221 static bool
222 dissect_ckpd_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
223 int offset, int role, uint16_t type, uint8_t *parameter_stream,
224 unsigned parameter_number, int parameter_length, void **data _U_)
226 proto_item *pitem;
227 uint32_t value;
229 if (!check_ckpd(role, type)) return false;
232 if (parameter_number > 0) return false;
234 value = get_uint_parameter(parameter_stream, parameter_length);
236 pitem = proto_tree_add_uint(tree, hf_ckpd, tvb, offset, parameter_length, value);
238 if (value != 200) {
239 expert_add_info(pinfo, pitem, &ei_ckpd);
242 return true;
245 static bool
246 dissect_no_parameter(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_,
247 int offset _U_, int role _U_, uint16_t type _U_, uint8_t *parameter_stream _U_,
248 unsigned parameter_number _U_, int parameter_length _U_, void **data _U_)
250 return false;
253 static const at_cmd_t at_cmds[] = {
254 { "+VGS", "Gain of Speaker", check_vgs, dissect_vgs_parameter },
255 { "+VGM", "Gain of Microphone", check_vgm, dissect_vgm_parameter },
256 { "+CKPD", "Control Keypad", check_ckpd, dissect_ckpd_parameter },
257 { "ERROR", "ERROR", check_only_ag_role, dissect_no_parameter },
258 { "RING", "Incoming Call Indication", check_only_ag_role, dissect_no_parameter },
259 { "OK", "OK", check_only_ag_role, dissect_no_parameter },
260 { NULL, NULL, NULL, NULL }
264 static int
265 dissect_at_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
266 int offset, uint32_t role, int command_number)
268 proto_item *pitem;
269 proto_tree *command_item;
270 proto_item *command_tree;
271 proto_tree *parameters_item = NULL;
272 proto_item *parameters_tree = NULL;
273 char *col_str = NULL;
274 char *at_stream;
275 char *at_command = NULL;
276 int i_char = 0;
277 unsigned i_char_fix = 0;
278 int length;
279 const at_cmd_t *i_at_cmd;
280 int parameter_length;
281 unsigned parameter_number = 0;
282 int first_parameter_offset = offset;
283 int last_parameter_offset = offset;
284 uint16_t type = TYPE_UNKNOWN;
285 uint32_t brackets;
286 bool quotation;
287 bool next;
288 void *data;
290 length = tvb_reported_length_remaining(tvb, offset);
291 if (length <= 0)
292 return tvb_reported_length(tvb);
294 if (!command_number) {
295 proto_tree_add_item(tree, hf_data, tvb, offset, length, ENC_NA | ENC_ASCII);
296 col_str = (char *) wmem_alloc(pinfo->pool, length + 1);
297 tvb_memcpy(tvb, col_str, offset, length);
298 col_str[length] = '\0';
301 at_stream = (char *) wmem_alloc(pinfo->pool, length + 1);
302 tvb_memcpy(tvb, at_stream, offset, length);
303 at_stream[length] = '\0';
304 while (at_stream[i_char]) {
305 at_stream[i_char] = g_ascii_toupper(at_stream[i_char]);
306 if (!command_number) {
307 col_str[i_char] = g_ascii_toupper(col_str[i_char]);
308 if (!g_ascii_isgraph(col_str[i_char])) col_str[i_char] = ' ';
310 i_char += 1;
313 command_item = proto_tree_add_none_format(tree, hf_command, tvb,
314 offset, 0, "Command %u", command_number);
315 command_tree = proto_item_add_subtree(command_item, ett_bthsp_command);
317 if (!command_number) col_append_str(pinfo->cinfo, COL_INFO, col_str);
319 if (role == ROLE_HS) {
320 if (command_number) {
321 at_command = at_stream;
322 i_char = 0;
323 } else {
324 at_command = g_strstr_len(at_stream, length, "AT");
326 if (at_command) {
327 i_char = (int) (at_command - at_stream);
329 if (i_char) {
330 proto_tree_add_item(command_tree, hf_at_ignored, tvb, offset,
331 i_char, ENC_NA | ENC_ASCII);
332 offset += i_char;
335 proto_tree_add_item(command_tree, hf_at_command_line_prefix,
336 tvb, offset, 2, ENC_NA | ENC_ASCII);
337 offset += 2;
338 i_char += 2;
339 at_command = at_stream;
341 at_command += i_char;
342 length -= i_char;
343 i_char_fix += i_char;
344 i_char = 0;
347 } else {
348 at_command = at_stream;
349 i_char = 0;
350 while (i_char <= length &&
351 (at_command[i_char] == '\r' || at_command[i_char] == '\n' ||
352 at_command[i_char] == ' ' || at_command[i_char] == '\t')) {
353 /* ignore white characters */
354 i_char += 1;
357 offset += i_char;
358 at_command += i_char;
359 length -= i_char;
360 i_char_fix += i_char;
361 i_char = 0;
364 if (at_command) {
366 while (i_char < length &&
367 (at_command[i_char] != '\r' && at_command[i_char] != '=' &&
368 at_command[i_char] != ';' && at_command[i_char] != '?' &&
369 at_command[i_char] != ':')) {
370 i_char += 1;
373 i_at_cmd = at_cmds;
374 if (at_command[0] == '\r') {
375 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset - 2,
376 2, ENC_NA | ENC_ASCII);
377 i_at_cmd = NULL;
378 } else {
379 pitem = NULL;
380 while (i_at_cmd->name) {
381 if (g_str_has_prefix(&at_command[0], i_at_cmd->name)) {
382 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
383 (int) strlen(i_at_cmd->name), ENC_NA | ENC_ASCII);
384 proto_item_append_text(pitem, " (%s)", i_at_cmd->long_name);
385 break;
387 i_at_cmd += 1;
390 if (!pitem) {
391 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
392 i_char, ENC_NA | ENC_ASCII);
397 if (i_at_cmd && i_at_cmd->name == NULL) {
398 char *name;
400 name = format_text(pinfo->pool, at_command, i_char + 1);
401 proto_item_append_text(command_item, ": %s (Unknown)", name);
402 proto_item_append_text(pitem, " (Unknown - Non-Standard HSP Command)");
403 expert_add_info(pinfo, pitem, &ei_non_mandatory_command);
404 } else if (i_at_cmd == NULL) {
405 proto_item_append_text(command_item, ": AT");
406 } else {
407 proto_item_append_text(command_item, ": %s", i_at_cmd->name);
410 offset += i_char;
412 if (i_at_cmd && g_strcmp0(i_at_cmd->name, "D")) {
413 if (length >= 2 && at_command[i_char] == '=' && at_command[i_char + 1] == '?') {
414 type = at_command[i_char] << 8 | at_command[i_char + 1];
415 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
416 offset += 2;
417 i_char += 2;
418 } else if (role == ROLE_AG && length >= 2 && at_command[i_char] == '\r' && at_command[i_char + 1] == '\n') {
419 type = at_command[i_char] << 8 | at_command[i_char + 1];
420 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
421 offset += 2;
422 i_char += 2;
423 } else if (length >= 1 && (at_command[i_char] == '=' ||
424 at_command[i_char] == '\r' ||
425 at_command[i_char] == ':' ||
426 at_command[i_char] == '?')) {
427 type = at_command[i_char];
428 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 1, type);
429 offset += 1;
430 i_char += 1;
434 if (i_at_cmd && i_at_cmd->check_command && !i_at_cmd->check_command(role, type)) {
435 expert_add_info(pinfo, command_item, &ei_invalid_usage);
438 parameters_item = proto_tree_add_none_format(command_tree, hf_parameters, tvb,
439 offset, 0, "Parameters");
440 parameters_tree = proto_item_add_subtree(parameters_item, ett_bthsp_parameters);
442 data = NULL;
444 while (i_char < length) {
446 while (at_command[i_char] == ' ' || at_command[i_char] == '\t') {
447 offset += 1;
448 i_char += 1;
451 parameter_length = 0;
452 brackets = 0;
453 quotation = false;
454 next = false;
456 if (at_command[i_char + parameter_length] != '\r') {
457 while (i_char + parameter_length < length &&
458 at_command[i_char + parameter_length] != '\r') {
460 if (at_command[i_char + parameter_length] == ';') {
461 next = true;
462 break;
465 if (at_command[i_char + parameter_length] == '"') {
466 quotation = quotation ? false : true;
469 if (quotation == true) {
470 parameter_length += 1;
471 continue;
474 if (at_command[i_char + parameter_length] == '(') {
475 brackets += 1;
477 if (at_command[i_char + parameter_length] == ')') {
478 brackets -= 1;
481 if (brackets == 0 && at_command[i_char + parameter_length] == ',') {
482 break;
485 parameter_length += 1;
488 /* TODO: Save bthsp.at_cmd, bthsp.at_cmd.type, frame_time and frame_num here in
490 if (role == ROLE_HS && pinfo->fd->visited == 0) {
492 at_cmd_db = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
494 interface_id
495 adapter_id
496 chandle
497 dlci
499 frame_number
500 -------------------
501 at_command
502 at_type
503 frame_num
504 frame_time
505 status
506 first_response_in (if 0 - no response)
509 interface_id = interface_id;
510 adapter_id = adapter_id;
511 chandle = chandle;
512 dlci = dlci;
513 frame_number = pinfo->num;
516 key[0].length = 1;
517 key[0].key = &interface_id;
518 key[1].length = 1;
519 key[1].key = &adapter_id;
520 key[2].length = 1;
521 key[2].key = &chandle;
522 key[3].length = 1;
523 key[3].key = &dlci;
524 key[4].length = 1;
525 key[4].key = &frame_number;
526 key[5].length = 0;
527 key[5].key = NULL;
529 cmd = wmem_new(wmem_file_scope(), at_cmd_entry_t);
530 cmd->interface_id = interface_id;
531 cmd->adapter_id = adapter_id;
532 cmd->chandle = chandle;
533 cmd->dlci = dlci;
535 cmd->frame_number = pinfo->num;
536 cmd->status = STATUS_NO_RESPONSE;
537 cmd->time = pinfo->abs_ts;
538 cmd->at_command
539 cmd->at_type
540 cmd->first_response_in = 0;
542 wmem_tree_insert32_array(at_cmd_db, key, cmd);
547 first_parameter_offset = offset;
548 if (type == TYPE_ACTION || type == TYPE_RESPONSE) {
549 if (i_at_cmd && (i_at_cmd->dissect_parameter != NULL &&
550 !i_at_cmd->dissect_parameter(tvb, pinfo, parameters_tree, offset, role,
551 type, &at_command[i_char], parameter_number, parameter_length, &data) )) {
552 pitem = proto_tree_add_item(parameters_tree,
553 hf_unknown_parameter, tvb, offset,
554 parameter_length, ENC_NA | ENC_ASCII);
555 expert_add_info(pinfo, pitem, &ei_unknown_parameter);
556 } else if (i_at_cmd && i_at_cmd->dissect_parameter == NULL) {
557 proto_tree_add_item(parameters_tree, hf_parameter, tvb, offset,
558 parameter_length, ENC_NA | ENC_ASCII);
563 if (type != TYPE_ACTION_SIMPLY && type != TYPE_RESPONSE_ACK && type != TYPE_TEST && type != TYPE_READ)
564 parameter_number += 1;
565 i_char += parameter_length;
566 offset += parameter_length;
567 last_parameter_offset = offset;
569 if (role == ROLE_AG &&
570 i_char + 1 <= length &&
571 at_command[i_char] == '\r' &&
572 at_command[i_char + 1] == '\n') {
573 offset += 2;
574 i_char += 2;
575 break;
576 } else if (at_command[i_char] == ',' ||
577 at_command[i_char] == '\r' ||
578 at_command[i_char] == ';') {
579 i_char += 1;
580 offset += 1;
583 if (next) break;
586 i_char += i_char_fix;
587 proto_item_set_len(command_item, i_char);
588 } else {
589 length = tvb_reported_length_remaining(tvb, offset);
590 if (length < 0)
591 length = 0;
592 proto_item_set_len(command_item, length);
593 offset += length;
596 if (parameter_number > 0 && last_parameter_offset - first_parameter_offset > 0)
597 proto_item_set_len(parameters_item, last_parameter_offset - first_parameter_offset);
598 else
599 proto_item_append_text(parameters_item, ": No");
601 if (role == ROLE_AG) {
602 unsigned command_frame_number = 0;
604 if (command_frame_number) {
605 pitem = proto_tree_add_uint(command_tree, hf_command_in, tvb, offset,
606 0, command_frame_number);
607 proto_item_set_generated(pitem);
608 } else {
609 pitem = proto_tree_add_item(command_tree, hf_unsolicited, tvb, offset, 0, ENC_NA);
610 proto_item_set_generated(pitem);
614 return offset;
617 static int
618 dissect_bthsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
620 proto_item *main_item;
621 proto_tree *main_tree;
622 proto_item *pitem;
623 int offset = 0;
624 uint32_t role = ROLE_UNKNOWN;
625 wmem_tree_key_t key[10];
626 uint32_t interface_id;
627 uint32_t adapter_id;
628 uint32_t chandle;
629 uint32_t dlci;
630 uint32_t frame_number;
631 uint32_t direction;
632 uint32_t bd_addr_oui;
633 uint32_t bd_addr_id;
634 fragment_t *fragment;
635 fragment_t *previous_fragment;
636 fragment_t *i_fragment;
637 uint8_t *at_stream;
638 int length;
639 int command_number;
640 int i_length;
641 tvbuff_t *reassembled_tvb = NULL;
642 unsigned reassemble_start_offset = 0;
643 unsigned reassemble_end_offset = 0;
644 int previous_proto;
646 previous_proto = (GPOINTER_TO_INT(wmem_list_frame_data(wmem_list_frame_prev(wmem_list_tail(pinfo->layers)))));
647 if (data && previous_proto == proto_btrfcomm) {
648 btrfcomm_data_t *rfcomm_data;
650 rfcomm_data = (btrfcomm_data_t *) data;
652 interface_id = rfcomm_data->interface_id;
653 adapter_id = rfcomm_data->adapter_id;
654 chandle = rfcomm_data->chandle;
655 dlci = rfcomm_data->dlci;
656 direction = (rfcomm_data->is_local_psm) ? P2P_DIR_SENT : P2P_DIR_RECV;
658 if (direction == P2P_DIR_RECV) {
659 bd_addr_oui = rfcomm_data->remote_bd_addr_oui;
660 bd_addr_id = rfcomm_data->remote_bd_addr_id;
661 } else {
662 bd_addr_oui = 0;
663 bd_addr_id = 0;
665 } else {
666 interface_id = HCI_INTERFACE_DEFAULT;
667 adapter_id = HCI_ADAPTER_DEFAULT;
668 chandle = 0;
669 dlci = 0;
670 direction = P2P_DIR_UNKNOWN;
672 bd_addr_oui = 0;
673 bd_addr_id = 0;
676 main_item = proto_tree_add_item(tree, proto_bthsp, tvb, 0, tvb_captured_length(tvb), ENC_NA);
677 main_tree = proto_item_add_subtree(main_item, ett_bthsp);
679 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSP");
681 switch (pinfo->p2p_dir) {
682 case P2P_DIR_SENT:
683 col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
684 break;
685 case P2P_DIR_RECV:
686 col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
687 break;
688 default:
689 col_set_str(pinfo->cinfo, COL_INFO, "UnknownDirection ");
690 break;
693 if ((hsp_role == ROLE_AG && pinfo->p2p_dir == P2P_DIR_SENT) ||
694 (hsp_role == ROLE_HS && pinfo->p2p_dir == P2P_DIR_RECV)) {
695 role = ROLE_AG;
696 } else if (hsp_role != ROLE_UNKNOWN) {
697 role = ROLE_HS;
700 if (role == ROLE_UNKNOWN) {
701 uint32_t sdp_psm;
702 uint32_t service_type;
703 uint32_t service_channel;
704 service_info_t *service_info;
706 sdp_psm = SDP_PSM_DEFAULT;
708 service_type = BTSDP_RFCOMM_PROTOCOL_UUID;
709 service_channel = dlci >> 1;
710 frame_number = pinfo->num;
712 key[0].length = 1;
713 key[0].key = &interface_id;
714 key[1].length = 1;
715 key[1].key = &adapter_id;
716 key[2].length = 1;
717 key[2].key = &sdp_psm;
718 key[3].length = 1;
719 key[3].key = &direction;
720 key[4].length = 1;
721 key[4].key = &bd_addr_oui;
722 key[5].length = 1;
723 key[5].key = &bd_addr_id;
724 key[6].length = 1;
725 key[6].key = &service_type;
726 key[7].length = 1;
727 key[7].key = &service_channel;
728 key[8].length = 1;
729 key[8].key = &frame_number;
730 key[9].length = 0;
731 key[9].key = NULL;
733 service_info = btsdp_get_service_info(key);
734 if (service_info && service_info->interface_id == interface_id &&
735 service_info->adapter_id == adapter_id &&
736 service_info->sdp_psm == SDP_PSM_DEFAULT &&
737 ((service_info->direction == P2P_DIR_RECV &&
738 service_info->bd_addr_oui == bd_addr_oui &&
739 service_info->bd_addr_id == bd_addr_id) ||
740 (service_info->direction != P2P_DIR_RECV &&
741 service_info->bd_addr_oui == 0 &&
742 service_info->bd_addr_id == 0)) &&
743 service_info->type == BTSDP_RFCOMM_PROTOCOL_UUID &&
744 service_info->channel == (dlci >> 1)) {
745 if ((service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_RECV && pinfo->p2p_dir == P2P_DIR_SENT) ||
746 (service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_SENT && pinfo->p2p_dir == P2P_DIR_RECV) ||
747 ((service_info->uuid.bt_uuid == BTSDP_HSP_SERVICE_UUID || service_info->uuid.bt_uuid == BTSDP_HSP_HS_SERVICE_UUID) && service_info->direction == P2P_DIR_RECV && pinfo->p2p_dir == P2P_DIR_RECV) ||
748 ((service_info->uuid.bt_uuid == BTSDP_HSP_SERVICE_UUID || service_info->uuid.bt_uuid == BTSDP_HSP_HS_SERVICE_UUID) && service_info->direction == P2P_DIR_SENT && pinfo->p2p_dir == P2P_DIR_SENT)) {
749 role = ROLE_HS;
750 } else {
751 role = ROLE_AG;
756 pitem = proto_tree_add_uint(main_tree, hf_role, tvb, 0, 0, role);
757 proto_item_set_generated(pitem);
759 if (role == ROLE_UNKNOWN) {
760 col_append_fstr(pinfo->cinfo, COL_INFO, "Data: %s",
761 tvb_format_text(pinfo->pool, tvb, 0, tvb_reported_length(tvb)));
762 proto_tree_add_item(main_tree, hf_data, tvb, 0, tvb_captured_length(tvb), ENC_NA | ENC_ASCII);
763 return tvb_reported_length(tvb);
766 /* save fragments */
767 if (!pinfo->fd->visited) {
768 frame_number = pinfo->num - 1;
770 key[0].length = 1;
771 key[0].key = &interface_id;
772 key[1].length = 1;
773 key[1].key = &adapter_id;
774 key[2].length = 1;
775 key[2].key = &chandle;
776 key[3].length = 1;
777 key[3].key = &dlci;
778 key[4].length = 1;
779 key[4].key = &role;
780 key[5].length = 1;
781 key[5].key = &frame_number;
782 key[6].length = 0;
783 key[6].key = NULL;
785 previous_fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
786 if (!(previous_fragment && previous_fragment->interface_id == interface_id &&
787 previous_fragment->adapter_id == adapter_id &&
788 previous_fragment->chandle == chandle &&
789 previous_fragment->dlci == dlci &&
790 previous_fragment->role == role &&
791 previous_fragment->reassemble_state != REASSEMBLE_DONE)) {
792 previous_fragment = NULL;
795 frame_number = pinfo->num;
797 key[0].length = 1;
798 key[0].key = &interface_id;
799 key[1].length = 1;
800 key[1].key = &adapter_id;
801 key[2].length = 1;
802 key[2].key = &chandle;
803 key[3].length = 1;
804 key[3].key = &dlci;
805 key[4].length = 1;
806 key[4].key = &role;
807 key[5].length = 1;
808 key[5].key = &frame_number;
809 key[6].length = 0;
810 key[6].key = NULL;
812 fragment = wmem_new(wmem_file_scope(), fragment_t);
813 fragment->interface_id = interface_id;
814 fragment->adapter_id = adapter_id;
815 fragment->chandle = chandle;
816 fragment->dlci = dlci;
817 fragment->role = role;
818 fragment->idx = previous_fragment ? previous_fragment->idx + previous_fragment->length : 0;
819 fragment->reassemble_state = REASSEMBLE_FRAGMENT;
820 fragment->length = tvb_reported_length(tvb);
821 fragment->data = (uint8_t *) wmem_alloc(wmem_file_scope(), fragment->length);
822 fragment->previous_fragment = previous_fragment;
823 tvb_memcpy(tvb, fragment->data, offset, fragment->length);
825 wmem_tree_insert32_array(fragments, key, fragment);
827 /* Detect reassemble end character: \r for HS or \n for AG */
828 length = tvb_reported_length(tvb);
829 at_stream = tvb_get_string_enc(pinfo->pool, tvb, 0, length, ENC_ASCII);
831 reassemble_start_offset = 0;
833 for (i_length = 0; i_length < length; i_length += 1) {
834 if (!((role == ROLE_HS && at_stream[i_length] == '\r') ||
835 (role == ROLE_AG && at_stream[i_length] == '\n'))) {
836 continue;
839 if (role == ROLE_HS && at_stream[i_length] == '\r') {
840 reassemble_start_offset = i_length + 1;
841 if (reassemble_end_offset == 0) reassemble_end_offset = i_length + 1;
844 if (role == ROLE_AG && at_stream[i_length] == '\n') {
845 reassemble_start_offset = i_length + 1;
848 frame_number = pinfo->num;
850 key[0].length = 1;
851 key[0].key = &interface_id;
852 key[1].length = 1;
853 key[1].key = &adapter_id;
854 key[2].length = 1;
855 key[2].key = &chandle;
856 key[3].length = 1;
857 key[3].key = &dlci;
858 key[4].length = 1;
859 key[4].key = &role;
860 key[5].length = 1;
861 key[5].key = &frame_number;
862 key[6].length = 0;
863 key[6].key = NULL;
865 fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
866 if (fragment && fragment->interface_id == interface_id &&
867 fragment->adapter_id == adapter_id &&
868 fragment->chandle == chandle &&
869 fragment->dlci == dlci &&
870 fragment->role == role) {
871 i_fragment = fragment;
872 while (i_fragment && i_fragment->idx > 0) {
873 i_fragment = i_fragment->previous_fragment;
876 if (i_length + 1 == length &&
877 role == ROLE_HS &&
878 at_stream[i_length] == '\r') {
879 fragment->reassemble_state = REASSEMBLE_DONE;
880 } else if (i_length + 1 == length &&
881 role == ROLE_AG &&
882 i_length >= 4 &&
883 at_stream[i_length] == '\n' &&
884 at_stream[i_length - 1] == '\r' &&
885 at_stream[0] == '\r' &&
886 at_stream[1] == '\n') {
887 fragment->reassemble_state = REASSEMBLE_DONE;
888 } else if (i_length + 1 == length &&
889 role == ROLE_AG &&
890 i_length >= 2 &&
891 at_stream[i_length] == '\n' &&
892 at_stream[i_length - 1] == '\r' &&
893 i_fragment &&
894 i_fragment->reassemble_state == REASSEMBLE_FRAGMENT &&
895 i_fragment->length >= 2 &&
896 i_fragment->data[0] == '\r' &&
897 i_fragment->data[1] == '\n') {
898 fragment->reassemble_state = REASSEMBLE_DONE;
899 } else if (role == ROLE_HS) {
900 /* XXX: Temporary disable reassembling of partial message, it seems to be broken */
901 /* fragment->reassemble_state = REASSEMBLE_PARTIALLY;*/
903 fragment->reassemble_start_offset = reassemble_start_offset;
904 fragment->reassemble_end_offset = reassemble_end_offset;
909 /* recover reassembled payload */
910 frame_number = pinfo->num;
912 key[0].length = 1;
913 key[0].key = &interface_id;
914 key[1].length = 1;
915 key[1].key = &adapter_id;
916 key[2].length = 1;
917 key[2].key = &chandle;
918 key[3].length = 1;
919 key[3].key = &dlci;
920 key[4].length = 1;
921 key[4].key = &role;
922 key[5].length = 1;
923 key[5].key = &frame_number;
924 key[6].length = 0;
925 key[6].key = NULL;
927 fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
928 if (fragment && fragment->interface_id == interface_id &&
929 fragment->adapter_id == adapter_id &&
930 fragment->chandle == chandle &&
931 fragment->dlci == dlci &&
932 fragment->role == role &&
933 fragment->reassemble_state != REASSEMBLE_FRAGMENT) {
934 uint8_t *at_data;
935 unsigned i_data_offset;
937 i_data_offset = fragment->idx + fragment->length;
938 at_data = (uint8_t *) wmem_alloc(pinfo->pool, fragment->idx + fragment->length);
940 i_fragment = fragment;
942 if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
943 i_data_offset -= i_fragment->reassemble_end_offset;
944 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->reassemble_end_offset);
945 i_fragment = i_fragment->previous_fragment;
948 if (i_fragment) {
949 while (i_fragment && i_fragment->idx > 0) {
950 i_data_offset -= i_fragment->length;
951 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
952 i_fragment = i_fragment->previous_fragment;
955 if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
956 i_data_offset -= (i_fragment->length - i_fragment->reassemble_start_offset);
957 memcpy(at_data + i_data_offset, i_fragment->data + i_fragment->reassemble_start_offset,
958 i_fragment->length - i_fragment->reassemble_start_offset);
959 } else if (i_fragment) {
960 i_data_offset -= i_fragment->length;
961 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
965 if (fragment->idx > 0 && fragment->length > 0) {
966 proto_tree_add_item(main_tree, hf_fragment, tvb, offset,
967 tvb_captured_length_remaining(tvb, offset), ENC_ASCII | ENC_NA);
968 reassembled_tvb = tvb_new_child_real_data(tvb, at_data,
969 fragment->idx + fragment->length, fragment->idx + fragment->length);
970 add_new_data_source(pinfo, reassembled_tvb, "Reassembled HSP");
973 command_number = 0;
974 if (reassembled_tvb) {
975 unsigned reassembled_offset = 0;
977 while (tvb_reported_length(reassembled_tvb) > reassembled_offset) {
978 reassembled_offset = dissect_at_command(reassembled_tvb,
979 pinfo, main_tree, reassembled_offset, role, command_number);
980 command_number += 1;
982 offset = tvb_captured_length(tvb);
983 } else {
984 while (tvb_reported_length(tvb) > (unsigned) offset) {
985 offset = dissect_at_command(tvb, pinfo, main_tree, offset, role, command_number);
986 command_number += 1;
989 } else {
990 pitem = proto_tree_add_item(main_tree, hf_fragmented, tvb, 0, 0, ENC_NA);
991 proto_item_set_generated(pitem);
992 char *display_str;
993 proto_tree_add_item_ret_display_string(main_tree, hf_fragment, tvb, offset, -1, ENC_ASCII, pinfo->pool, &display_str);
994 col_append_fstr(pinfo->cinfo, COL_INFO, "Fragment: %s", display_str);
995 offset = tvb_captured_length(tvb);
998 return offset;
1001 void
1002 proto_register_bthsp(void)
1004 module_t *module;
1005 expert_module_t *expert_bthsp;
1007 static hf_register_info hf[] = {
1008 { &hf_command,
1009 { "Command", "bthsp.command",
1010 FT_NONE, BASE_NONE, NULL, 0,
1011 NULL, HFILL}
1013 { &hf_parameters,
1014 { "Parameters", "bthsp.parameters",
1015 FT_NONE, BASE_NONE, NULL, 0,
1016 NULL, HFILL}
1018 { &hf_command_in,
1019 { "Command frame number in", "bthsp.command_in",
1020 FT_FRAMENUM, BASE_NONE, NULL, 0,
1021 NULL, HFILL}
1023 { &hf_unsolicited,
1024 { "Unsolicited", "bthsp.unsolicited",
1025 FT_NONE, BASE_NONE, NULL, 0,
1026 NULL, HFILL}
1028 { &hf_data,
1029 { "AT Stream", "bthsp.data",
1030 FT_STRING, BASE_NONE, NULL, 0,
1031 NULL, HFILL}
1033 { &hf_fragment,
1034 { "Fragment", "bthsp.fragment",
1035 FT_STRING, BASE_NONE, NULL, 0,
1036 NULL, HFILL}
1038 { &hf_fragmented,
1039 { "Fragmented", "bthsp.fragmented",
1040 FT_NONE, BASE_NONE, NULL, 0,
1041 NULL, HFILL}
1043 { &hf_at_ignored,
1044 { "Ignored", "bthsp.ignored",
1045 FT_BYTES, BASE_NONE, NULL, 0,
1046 NULL, HFILL}
1048 { &hf_at_cmd,
1049 { "Command", "bthsp.at_cmd",
1050 FT_STRING, BASE_NONE, NULL, 0,
1051 NULL, HFILL}
1053 { &hf_at_cmd_type,
1054 { "Type", "bthsp.at_cmd.type",
1055 FT_UINT16, BASE_HEX, VALS(at_cmd_type_vals), 0,
1056 NULL, HFILL}
1058 { &hf_at_command_line_prefix,
1059 { "Command Line Prefix", "bthsp.command_line_prefix",
1060 FT_STRING, BASE_NONE, NULL, 0,
1061 NULL, HFILL}
1063 { &hf_parameter,
1064 { "Parameter", "bthsp.parameter",
1065 FT_STRING, BASE_NONE, NULL, 0,
1066 NULL, HFILL}
1068 { &hf_unknown_parameter,
1069 { "Unknown Parameter", "bthsp.unknown_parameter",
1070 FT_STRING, BASE_NONE, NULL, 0,
1071 NULL, HFILL}
1073 { &hf_role,
1074 { "Role", "bthsp.role",
1075 FT_UINT8, BASE_DEC, VALS(role_vals), 0,
1076 NULL, HFILL}
1078 { &hf_vgs,
1079 { "Gain", "bthsp.vgs",
1080 FT_UINT8, BASE_DEC|BASE_UNIT_STRING, UNS(&units_slash15), 0,
1081 NULL, HFILL}
1083 { &hf_vgm,
1084 { "Gain", "bthsp.vgm",
1085 FT_UINT8, BASE_DEC|BASE_UNIT_STRING, UNS(&units_slash15), 0,
1086 NULL, HFILL}
1088 { &hf_ckpd,
1089 { "Key", "bthsp.ckpd",
1090 FT_UINT8, BASE_DEC, NULL, 0,
1091 NULL, HFILL}
1095 static ei_register_info ei[] = {
1096 { &ei_non_mandatory_command, { "bthsp.expert.non_mandatory_command", PI_PROTOCOL, PI_NOTE, "Non-mandatory command in HSP", EXPFILL }},
1097 { &ei_invalid_usage, { "bthsp.expert.invalid_usage", PI_PROTOCOL, PI_WARN, "Non mandatory type or command in this role", EXPFILL }},
1098 { &ei_unknown_parameter, { "bthsp.expert.unknown_parameter", PI_PROTOCOL, PI_WARN, "Unknown parameter", EXPFILL }},
1099 { &ei_vgm_gain, { "bthsp.expert.vgm", PI_PROTOCOL, PI_WARN, "Gain of microphone exceeds range 0-15", EXPFILL }},
1100 { &ei_vgs_gain, { "bthsp.expert.vgs", PI_PROTOCOL, PI_WARN, "Gain of speaker exceeds range 0-15", EXPFILL }},
1101 { &ei_ckpd, { "bthsp.expert.ckpd", PI_PROTOCOL, PI_WARN, "Only key 200 is covered in HSP", EXPFILL }} };
1103 static int *ett[] = {
1104 &ett_bthsp,
1105 &ett_bthsp_command,
1106 &ett_bthsp_parameters
1109 fragments = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
1111 proto_bthsp = proto_register_protocol("Bluetooth HSP Profile", "BT HSP", "bthsp");
1112 bthsp_handle = register_dissector("bthsp", dissect_bthsp, proto_bthsp);
1114 proto_register_field_array(proto_bthsp, hf, array_length(hf));
1115 proto_register_subtree_array(ett, array_length(ett));
1117 module = prefs_register_protocol_subtree("Bluetooth", proto_bthsp, NULL);
1118 prefs_register_static_text_preference(module, "hsp.version",
1119 "Bluetooth Profile HSP version: 1.2",
1120 "Version of profile supported by this dissector.");
1122 prefs_register_enum_preference(module, "hsp.hsp_role",
1123 "Force treat packets as AG or HS role",
1124 "Force treat packets as AG or HS role",
1125 &hsp_role, pref_hsp_role, true);
1127 expert_bthsp = expert_register_protocol(proto_bthsp);
1128 expert_register_field_array(expert_bthsp, ei, array_length(ei));
1131 void
1132 proto_reg_handoff_bthsp(void)
1134 dissector_add_string("bluetooth.uuid", "1108", bthsp_handle);
1135 dissector_add_string("bluetooth.uuid", "1112", bthsp_handle);
1136 dissector_add_string("bluetooth.uuid", "1131", bthsp_handle);
1138 dissector_add_for_decode_as("btrfcomm.dlci", bthsp_handle);
1142 * Editor modelines - https://www.wireshark.org/tools/modelines.html
1144 * Local variables:
1145 * c-basic-offset: 4
1146 * tab-width: 8
1147 * indent-tabs-mode: nil
1148 * End:
1150 * vi: set shiftwidth=4 tabstop=8 expandtab:
1151 * :indentSize=4:tabSize=8:noTabs=true: