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 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <epan/packet.h>
30 #include <epan/prefs.h>
31 #include <epan/expert.h>
32 #include <epan/wmem/wmem.h>
34 #include "packet-btrfcomm.h"
35 #include "packet-btsdp.h"
37 static int proto_bthsp = -1;
39 static int hf_command = -1;
40 static int hf_parameters = -1;
41 static int hf_command_in = -1;
42 static int hf_unsolicited = -1;
43 static int hf_role = -1;
44 static int hf_at_cmd = -1;
45 static int hf_at_cmd_type = -1;
46 static int hf_at_command_line_prefix = -1;
47 static int hf_at_ignored = -1;
48 static int hf_parameter = -1;
49 static int hf_unknown_parameter = -1;
50 static int hf_data = -1;
51 static int hf_fragment = -1;
52 static int hf_fragmented = -1;
53 static int hf_vgs = -1;
54 static int hf_vgm = -1;
55 static int hf_ckpd = -1;
57 static expert_field ei_non_mandatory_command = EI_INIT;
58 static expert_field ei_invalid_usage = EI_INIT;
59 static expert_field ei_unknown_parameter = EI_INIT;
60 static expert_field ei_vgm_gain = EI_INIT;
61 static expert_field ei_vgs_gain = EI_INIT;
62 static expert_field ei_ckpd = EI_INIT;
64 static gint ett_bthsp = -1;
65 static gint ett_bthsp_command = -1;
66 static gint ett_bthsp_parameters = -1;
68 static dissector_handle_t bthsp_handle;
70 static wmem_tree_t *fragments = NULL;
72 #define ROLE_UNKNOWN 0
76 #define TYPE_UNKNOWN 0x0000
77 #define TYPE_RESPONSE_ACK 0x0d0a
78 #define TYPE_RESPONSE 0x003a
79 #define TYPE_ACTION 0x003d
80 #define TYPE_ACTION_SIMPLY 0x000d
81 #define TYPE_READ 0x003f
82 #define TYPE_TEST 0x3d3f
84 static gint hsp_role = ROLE_UNKNOWN;
86 enum reassemble_state_t {
92 typedef struct _fragment_t {
102 struct _fragment_t *previous_fragment;
104 guint reassemble_start_offset;
105 guint reassemble_end_offset;
106 enum reassemble_state_t reassemble_state;
109 typedef struct _at_cmd_t {
111 const guint8 *long_name;
113 gboolean (*check_command)(gint role, guint16 type);
114 gboolean (*dissect_parameter)(tvbuff_t *tvb, packet_info *pinfo,
115 proto_tree *tree, gint offset, gint role, guint16 type,
116 guint8 *parameter_stream, guint parameter_number,
117 gint parameter_length, void **data);
120 static const value_string role_vals[] = {
121 { ROLE_UNKNOWN, "Unknown" },
122 { ROLE_AG, "AG - Audio Gate" },
123 { ROLE_HS, "HS - Headset" },
127 static const value_string at_cmd_type_vals[] = {
128 { 0x0d, "Action Command" },
129 { 0x3a, "Response" },
130 { 0x3d, "Action Command" },
131 { 0x3f, "Read Command" },
132 { 0x0d0a, "Response" },
133 { 0x3d3f, "Test Command" },
137 static const enum_val_t pref_hsp_role[] = {
138 { "off", "Off", ROLE_UNKNOWN },
139 { "ag", "Sent is AG, Rcvd is HS", ROLE_AG },
140 { "hs", "Sent is HS, Rcvd is AG", ROLE_HS },
144 void proto_register_bthsp(void);
145 void proto_reg_handoff_bthsp(void);
147 static guint32 get_uint_parameter(guint8 *parameter_stream, gint parameter_length)
152 val = (guint8 *) wmem_alloc(wmem_packet_scope(), parameter_length + 1);
153 memcpy(val, parameter_stream, parameter_length);
154 val[parameter_length] = '\0';
155 value = (guint32) g_ascii_strtoull(val, NULL, 10);
160 static gboolean check_vgs(gint role, guint16 type) {
161 if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
162 if (role == ROLE_AG && type == TYPE_RESPONSE) return TRUE;
167 static gboolean check_vgm(gint role, guint16 type) {
168 if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
169 if (role == ROLE_AG && type == TYPE_RESPONSE) return TRUE;
174 static gboolean check_ckpd(gint role, guint16 type) {
175 if (role == ROLE_HS && type == TYPE_ACTION) return TRUE;
180 static gboolean check_only_ag_role(gint role, guint16 type) {
181 if (role == ROLE_AG && type == TYPE_RESPONSE_ACK) return TRUE;
187 dissect_vgs_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
188 gint offset, gint role, guint16 type, guint8 *parameter_stream,
189 guint parameter_number, gint parameter_length, void **data _U_)
194 if (!check_vgs(role, type)) return FALSE;
196 if (parameter_number > 0) return FALSE;
198 value = get_uint_parameter(parameter_stream, parameter_length);
200 pitem = proto_tree_add_uint(tree, hf_vgs, tvb, offset, parameter_length, value);
201 proto_item_append_text(pitem, "/15");
204 expert_add_info(pinfo, pitem, &ei_vgs_gain);
211 dissect_vgm_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
212 gint offset, gint role, guint16 type, guint8 *parameter_stream,
213 guint parameter_number, gint parameter_length, void **data _U_)
218 if (!check_vgm(role, type)) return FALSE;
220 if (parameter_number > 0) return FALSE;
222 value = get_uint_parameter(parameter_stream, parameter_length);
224 pitem = proto_tree_add_uint(tree, hf_vgm, tvb, offset, parameter_length, value);
225 proto_item_append_text(pitem, "/15");
228 expert_add_info(pinfo, pitem, &ei_vgm_gain);
235 dissect_ckpd_parameter(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
236 gint offset, gint role, guint16 type, guint8 *parameter_stream,
237 guint parameter_number, gint parameter_length, void **data _U_)
242 if (!check_ckpd(role, type)) return FALSE;
245 if (parameter_number > 0) return FALSE;
247 value = get_uint_parameter(parameter_stream, parameter_length);
249 pitem = proto_tree_add_uint(tree, hf_ckpd, tvb, offset, parameter_length, value);
252 expert_add_info(pinfo, pitem, &ei_ckpd);
259 dissect_no_parameter(tvbuff_t *tvb _U_, packet_info *pinfo _U_, proto_tree *tree _U_,
260 gint offset _U_, gint role _U_, guint16 type _U_, guint8 *parameter_stream _U_,
261 guint parameter_number _U_, gint parameter_length _U_, void **data _U_)
266 static const at_cmd_t at_cmds[] = {
267 { "+VGS", "Gain of Speaker", check_vgs, dissect_vgs_parameter },
268 { "+VGM", "Gain of Microphone", check_vgm, dissect_vgm_parameter },
269 { "+CKPD", "Control Keypad", check_ckpd, dissect_ckpd_parameter },
270 { "ERROR", "ERROR", check_only_ag_role, dissect_no_parameter },
271 { "RING", "Incomming Call Indication", check_only_ag_role, dissect_no_parameter },
272 { "OK", "OK", check_only_ag_role, dissect_no_parameter },
273 { NULL, NULL, NULL, NULL }
278 dissect_at_command(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
279 gint offset, guint32 role, gint command_number)
282 proto_tree *command_item;
283 proto_item *command_tree;
284 proto_tree *parameters_item = NULL;
285 proto_item *parameters_tree = NULL;
286 guint8 *col_str = NULL;
288 guint8 *at_command = NULL;
290 guint i_char_fix = 0;
292 const at_cmd_t *i_at_cmd;
293 gint parameter_length;
294 guint parameter_number = 0;
295 gint first_parameter_offset = offset;
296 gint last_parameter_offset = offset;
297 guint16 type = TYPE_UNKNOWN;
303 length = tvb_length_remaining(tvb, offset);
305 return tvb_length(tvb);
307 if (!command_number) {
308 proto_tree_add_item(tree, hf_data, tvb, offset, length, ENC_NA | ENC_ASCII);
309 col_str = (guint8 *) wmem_alloc(wmem_packet_scope(), length + 1);
310 tvb_memcpy(tvb, col_str, offset, length);
311 col_str[length] = '\0';
314 at_stream = (guint8 *) wmem_alloc(wmem_packet_scope(), length + 1);
315 tvb_memcpy(tvb, at_stream, offset, length);
316 at_stream[length] = '\0';
317 while (at_stream[i_char]) {
318 at_stream[i_char] = toupper(at_stream[i_char]);
319 if (!command_number) {
320 col_str[i_char] = toupper(col_str[i_char]);
321 if (!g_ascii_isgraph(col_str[i_char])) col_str[i_char] = ' ';
326 command_item = proto_tree_add_none_format(tree, hf_command, tvb,
327 offset, 0, "Command %u", command_number);
328 command_tree = proto_item_add_subtree(command_item, ett_bthsp_command);
330 if (!command_number) col_append_fstr(pinfo->cinfo, COL_INFO, "%s", col_str);
332 if (role == ROLE_HS) {
333 if (command_number) {
334 at_command = at_stream;
337 at_command = g_strstr_len(at_stream, length, "AT");
340 i_char = (guint) (at_command - at_stream);
343 proto_tree_add_item(command_tree, hf_at_ignored, tvb, offset,
344 i_char, ENC_NA | ENC_ASCII);
348 proto_tree_add_item(command_tree, hf_at_command_line_prefix,
349 tvb, offset, 2, ENC_NA | ENC_ASCII);
352 at_command = at_stream;
354 at_command += i_char;
356 i_char_fix += i_char;
361 at_command = at_stream;
363 while (i_char <= length &&
364 (at_command[i_char] == '\r' || at_command[i_char] == '\n' ||
365 at_command[i_char] == ' ' || at_command[i_char] == '\t')) {
366 /* ignore white characters */
371 at_command += i_char;
373 i_char_fix += i_char;
379 while (i_char < length &&
380 (at_command[i_char] != '\r' && at_command[i_char] != '=' &&
381 at_command[i_char] != ';' && at_command[i_char] != '?' &&
382 at_command[i_char] != ':')) {
387 if (at_command[0] == '\r') {
388 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset - 2,
389 2, ENC_NA | ENC_ASCII);
393 while (i_at_cmd->name) {
394 if (g_str_has_prefix(&at_command[0], i_at_cmd->name)) {
395 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
396 (gint) strlen(i_at_cmd->name), ENC_NA | ENC_ASCII);
397 proto_item_append_text(pitem, " (%s)", i_at_cmd->long_name);
404 pitem = proto_tree_add_item(command_tree, hf_at_cmd, tvb, offset,
405 i_char, ENC_NA | ENC_ASCII);
410 if (i_at_cmd && i_at_cmd->name == NULL) {
413 name = (char *) wmem_alloc(wmem_packet_scope(), i_char + 2);
414 g_strlcpy(name, at_command, i_char + 1);
415 name[i_char + 1] = '\0';
416 proto_item_append_text(command_item, ": %s (Unknown)", name);
417 proto_item_append_text(pitem, " (Unknown - Non-Standard HSP Command)");
418 expert_add_info(pinfo, pitem, &ei_non_mandatory_command);
419 } else if (i_at_cmd == NULL) {
420 proto_item_append_text(command_item, ": AT");
422 proto_item_append_text(command_item, ": %s", i_at_cmd->name);
427 if (i_at_cmd && g_strcmp0(i_at_cmd->name, "D")) {
428 if (length >= 2 && at_command[i_char] == '=' && at_command[i_char + 1] == '?') {
429 type = at_command[i_char] << 8 | at_command[i_char + 1];
430 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
433 } else if (role == ROLE_AG && length >= 2 && at_command[i_char] == '\r' && at_command[i_char + 1] == '\n') {
434 type = at_command[i_char] << 8 | at_command[i_char + 1];
435 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 2, type);
438 } else if (length >= 1 && (at_command[i_char] == '=' ||
439 at_command[i_char] == '\r' ||
440 at_command[i_char] == ':' ||
441 at_command[i_char] == '?')) {
442 type = at_command[i_char];
443 proto_tree_add_uint(command_tree, hf_at_cmd_type, tvb, offset, 1, type);
449 if (i_at_cmd && i_at_cmd->check_command && !i_at_cmd->check_command(role, type)) {
450 expert_add_info(pinfo, command_item, &ei_invalid_usage);
453 parameters_item = proto_tree_add_none_format(command_tree, hf_parameters, tvb,
454 offset, 0, "Parameters");
455 parameters_tree = proto_item_add_subtree(parameters_item, ett_bthsp_parameters);
459 while (i_char < length) {
461 while (at_command[i_char] == ' ' || at_command[i_char] == '\t') {
466 parameter_length = 0;
471 if (at_command[i_char + parameter_length] != '\r') {
472 while (i_char + parameter_length < length &&
473 at_command[i_char + parameter_length] != '\r') {
475 if (at_command[i_char + parameter_length] == ';') {
480 if (at_command[i_char + parameter_length] == '"') {
481 quotation = quotation ? FALSE : TRUE;
484 if (quotation == TRUE) {
485 parameter_length += 1;
489 if (at_command[i_char + parameter_length] == '(') {
492 if (at_command[i_char + parameter_length] == ')') {
496 if (brackets == 0 && at_command[i_char + parameter_length] == ',') {
500 parameter_length += 1;
503 /* TODO: Save bthsp.at_cmd, bthsp.at_cmd.type, frame_time and frame_num here in
505 if (role == ROLE_HS && pinfo->fd->flags.visited == 0) {
507 at_cmd_db = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
521 first_response_in (if 0 - no response)
524 k_interface_id = interface_id;
525 k_adapter_id = adapter_id;
528 k_frame_number = pinfo->fd->num;
532 key[0].key = &k_interface_id;
534 key[1].key = &k_adapter_id;
536 key[2].key = &k_chandle;
538 key[3].key = &k_dlci;
540 key[4].key = &k_frame_number;
544 cmd = wmem_new(wmem_file_scope(), at_cmd_entry_t);
545 cmd->interface_id = interface_id;
546 cmd->adapter_id = adapter_id;
547 cmd->chandle = chandle;
550 cmd->frame_number = pinfo->fd->num;
551 cmd->status = STATUS_NO_RESPONSE;
552 cmd->time = pinfo->fd->abs_ts;
555 cmd->first_response_in = 0;
557 wmem_tree_insert32_array(at_cmd_db, key, cmd);
562 first_parameter_offset = offset;
563 if (type == TYPE_ACTION || type == TYPE_RESPONSE) {
564 if (i_at_cmd && (i_at_cmd->dissect_parameter != NULL &&
565 !i_at_cmd->dissect_parameter(tvb, pinfo, parameters_tree, offset, role,
566 type, &at_command[i_char], parameter_number, parameter_length, &data) )) {
567 pitem = proto_tree_add_item(parameters_tree,
568 hf_unknown_parameter, tvb, offset,
569 parameter_length, ENC_NA | ENC_ASCII);
570 expert_add_info(pinfo, pitem, &ei_unknown_parameter);
571 } else if (i_at_cmd && i_at_cmd->dissect_parameter == NULL) {
572 proto_tree_add_item(parameters_tree, hf_parameter, tvb, offset,
573 parameter_length, ENC_NA | ENC_ASCII);
578 if (type != TYPE_ACTION_SIMPLY && type != TYPE_RESPONSE_ACK && type != TYPE_TEST && type != TYPE_READ)
579 parameter_number += 1;
580 i_char += parameter_length;
581 offset += parameter_length;
582 last_parameter_offset = offset;
584 if (role == ROLE_AG &&
585 i_char + 1 <= length &&
586 at_command[i_char] == '\r' &&
587 at_command[i_char + 1] == '\n') {
591 } else if (at_command[i_char] == ',' ||
592 at_command[i_char] == '\r' ||
593 at_command[i_char] == ';') {
601 i_char += i_char_fix;
602 proto_item_set_len(command_item, i_char);
604 length = tvb_length_remaining(tvb, offset);
607 proto_item_set_len(command_item, length);
611 if (parameter_number > 0 && last_parameter_offset - first_parameter_offset > 0)
612 proto_item_set_len(parameters_item, last_parameter_offset - first_parameter_offset);
614 proto_item_append_text(parameters_item, ": No");
616 if (role == ROLE_AG) {
617 guint command_frame_number = 0;
619 if (command_frame_number) {
620 pitem = proto_tree_add_uint(command_tree, hf_command_in, tvb, offset,
621 0, command_frame_number);
622 PROTO_ITEM_SET_GENERATED(pitem);
624 pitem = proto_tree_add_item(command_tree, hf_unsolicited, tvb, offset, 0, ENC_NA);
625 PROTO_ITEM_SET_GENERATED(pitem);
633 dissect_bthsp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree, void *data)
635 proto_item *main_item;
636 proto_tree *main_tree;
639 guint32 role = ROLE_UNKNOWN;
640 wmem_tree_key_t key[10];
641 guint32 k_interface_id;
642 guint32 k_adapter_id;
646 guint32 k_frame_number;
647 guint32 interface_id;
651 fragment_t *fragment;
652 fragment_t *previous_fragment;
653 fragment_t *i_fragment;
654 btrfcomm_data_t *rfcomm_data;
659 tvbuff_t *reassembled_tvb = NULL;
660 guint reassemble_start_offset = 0;
661 guint reassemble_end_offset = 0;
663 /* Reject the packet if data is NULL */
666 rfcomm_data = (btrfcomm_data_t *) data;
668 main_item = proto_tree_add_item(tree, proto_bthsp, tvb, 0, -1, ENC_NA);
669 main_tree = proto_item_add_subtree(main_item, ett_bthsp);
671 col_set_str(pinfo->cinfo, COL_PROTOCOL, "HSP");
673 switch (pinfo->p2p_dir) {
675 col_set_str(pinfo->cinfo, COL_INFO, "Sent ");
678 col_set_str(pinfo->cinfo, COL_INFO, "Rcvd ");
681 col_add_fstr(pinfo->cinfo, COL_INFO, "Unknown direction %d ", pinfo->p2p_dir);
685 interface_id = rfcomm_data->interface_id;
686 adapter_id = rfcomm_data->adapter_id;
687 chandle = rfcomm_data->chandle;
688 dlci = rfcomm_data->dlci;
690 if ((hsp_role == ROLE_AG && pinfo->p2p_dir == P2P_DIR_SENT) ||
691 (hsp_role == ROLE_HS && pinfo->p2p_dir == P2P_DIR_RECV)) {
693 } else if (hsp_role != ROLE_UNKNOWN) {
697 if (role == ROLE_UNKNOWN) {
700 guint32 k_bd_addr_oui;
701 guint32 k_bd_addr_id;
702 guint32 k_service_type;
703 guint32 k_service_channel;
704 service_info_t *service_info;
706 k_interface_id = rfcomm_data->interface_id;
707 k_adapter_id = rfcomm_data->adapter_id;
708 k_sdp_psm = SDP_PSM_DEFAULT;
709 k_direction = (rfcomm_data->is_local_psm) ? P2P_DIR_SENT : P2P_DIR_RECV;
710 if (k_direction == P2P_DIR_RECV) {
711 k_bd_addr_oui = rfcomm_data->remote_bd_addr_oui;
712 k_bd_addr_id = rfcomm_data->remote_bd_addr_id;
717 k_service_type = BTSDP_RFCOMM_PROTOCOL_UUID;
718 k_service_channel = rfcomm_data->dlci >> 1;
719 k_frame_number = pinfo->fd->num;
722 key[0].key = &k_interface_id;
724 key[1].key = &k_adapter_id;
726 key[2].key = &k_sdp_psm;
728 key[3].key = &k_direction;
730 key[4].key = &k_bd_addr_oui;
732 key[5].key = &k_bd_addr_id;
734 key[6].key = &k_service_type;
736 key[7].key = &k_service_channel;
738 key[8].key = &k_frame_number;
742 service_info = btsdp_get_service_info(key);
743 if (service_info && service_info->interface_id == rfcomm_data->interface_id &&
744 service_info->adapter_id == rfcomm_data->adapter_id &&
745 service_info->sdp_psm == SDP_PSM_DEFAULT &&
746 ((service_info->direction == P2P_DIR_RECV &&
747 service_info->bd_addr_oui == rfcomm_data->remote_bd_addr_oui &&
748 service_info->bd_addr_id == rfcomm_data->remote_bd_addr_id) ||
749 (service_info->direction != P2P_DIR_RECV &&
750 service_info->bd_addr_oui == 0 &&
751 service_info->bd_addr_id == 0)) &&
752 service_info->type == BTSDP_RFCOMM_PROTOCOL_UUID &&
753 service_info->channel == (rfcomm_data->dlci >> 1)) {
754 if ((service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_RECV && pinfo->p2p_dir == P2P_DIR_SENT) ||
755 (service_info->uuid.bt_uuid == BTSDP_HSP_GW_SERVICE_UUID && service_info->direction == P2P_DIR_SENT && pinfo->p2p_dir == P2P_DIR_RECV) ||
756 ((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) ||
757 ((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)) {
765 pitem = proto_tree_add_uint(main_tree, hf_role, tvb, 0, 0, role);
766 PROTO_ITEM_SET_GENERATED(pitem);
768 if (role == ROLE_UNKNOWN) {
769 col_append_fstr(pinfo->cinfo, COL_INFO, "Data: %s",
770 tvb_format_text(tvb, 0, tvb_length(tvb)));
771 proto_tree_add_item(main_tree, hf_data, tvb, 0, -1, ENC_NA | ENC_ASCII);
772 return tvb_length(tvb);
776 if (!pinfo->fd->flags.visited) {
777 k_interface_id = interface_id;
778 k_adapter_id = adapter_id;
782 k_frame_number = pinfo->fd->num - 1;
785 key[0].key = &k_interface_id;
787 key[1].key = &k_adapter_id;
789 key[2].key = &k_chandle;
791 key[3].key = &k_dlci;
793 key[4].key = &k_role;
795 key[5].key = &k_frame_number;
799 previous_fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
800 if (!(previous_fragment && previous_fragment->interface_id == interface_id &&
801 previous_fragment->adapter_id == adapter_id &&
802 previous_fragment->chandle == chandle &&
803 previous_fragment->dlci == dlci &&
804 previous_fragment->role == role &&
805 previous_fragment->reassemble_state != REASSEMBLE_DONE)) {
806 previous_fragment = NULL;
809 k_interface_id = interface_id;
810 k_adapter_id = adapter_id;
814 k_frame_number = pinfo->fd->num;
817 key[0].key = &k_interface_id;
819 key[1].key = &k_adapter_id;
821 key[2].key = &k_chandle;
823 key[3].key = &k_dlci;
825 key[4].key = &k_role;
827 key[5].key = &k_frame_number;
831 fragment = wmem_new(wmem_file_scope(), fragment_t);
832 fragment->interface_id = interface_id;
833 fragment->adapter_id = adapter_id;
834 fragment->chandle = chandle;
835 fragment->dlci = dlci;
836 fragment->role = role;
837 fragment->index = previous_fragment ? previous_fragment->index + previous_fragment->length : 0;
838 fragment->reassemble_state = REASSEMBLE_FRAGMENT;
839 fragment->length = tvb_length(tvb);
840 fragment->data = (guint8 *) wmem_alloc(wmem_file_scope(), fragment->length);
841 fragment->previous_fragment = previous_fragment;
842 tvb_memcpy(tvb, fragment->data, offset, fragment->length);
844 wmem_tree_insert32_array(fragments, key, fragment);
846 /* Detect reassemble end character: \r for HS or \n for AG */
847 length = tvb_length(tvb);
848 at_stream = tvb_get_string_enc(wmem_packet_scope(), tvb, 0, length, ENC_ASCII);
850 reassemble_start_offset = 0;
852 for (i_length = 0; i_length < length; i_length += 1) {
853 if (!((role == ROLE_HS && at_stream[i_length] == '\r') ||
854 (role == ROLE_AG && at_stream[i_length] == '\n'))) {
858 if (role == ROLE_HS && at_stream[i_length] == '\r') {
859 reassemble_start_offset = i_length + 1;
860 if (reassemble_end_offset == 0) reassemble_end_offset = i_length + 1;
863 if (role == ROLE_AG && at_stream[i_length] == '\n') {
864 reassemble_start_offset = i_length + 1;
867 k_interface_id = interface_id;
868 k_adapter_id = adapter_id;
872 k_frame_number = pinfo->fd->num;
875 key[0].key = &k_interface_id;
877 key[1].key = &k_adapter_id;
879 key[2].key = &k_chandle;
881 key[3].key = &k_dlci;
883 key[4].key = &k_role;
885 key[5].key = &k_frame_number;
889 fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
890 if (fragment && fragment->interface_id == interface_id &&
891 fragment->adapter_id == adapter_id &&
892 fragment->chandle == chandle &&
893 fragment->dlci == dlci &&
894 fragment->role == role) {
895 i_fragment = fragment;
896 while (i_fragment && i_fragment->index > 0) {
897 i_fragment = i_fragment->previous_fragment;
900 if (i_length + 1 == length &&
902 at_stream[i_length] == '\r') {
903 fragment->reassemble_state = REASSEMBLE_DONE;
904 } else if (i_length + 1 == length &&
907 at_stream[i_length] == '\n' &&
908 at_stream[i_length - 1] == '\r' &&
909 at_stream[0] == '\r' &&
910 at_stream[1] == '\n') {
911 fragment->reassemble_state = REASSEMBLE_DONE;
912 } else if (i_length + 1 == length &&
915 at_stream[i_length] == '\n' &&
916 at_stream[i_length - 1] == '\r' &&
918 i_fragment->reassemble_state == REASSEMBLE_FRAGMENT &&
919 i_fragment->length >= 2 &&
920 i_fragment->data[0] == '\r' &&
921 i_fragment->data[1] == '\n') {
922 fragment->reassemble_state = REASSEMBLE_DONE;
923 } else if (role == ROLE_HS) {
924 fragment->reassemble_state = REASSEMBLE_PARTIALLY;
926 fragment->reassemble_start_offset = reassemble_start_offset;
927 fragment->reassemble_end_offset = reassemble_end_offset;
932 /* recover reassembled payload */
933 k_interface_id = interface_id;
934 k_adapter_id = adapter_id;
938 k_frame_number = pinfo->fd->num;
941 key[0].key = &k_interface_id;
943 key[1].key = &k_adapter_id;
945 key[2].key = &k_chandle;
947 key[3].key = &k_dlci;
949 key[4].key = &k_role;
951 key[5].key = &k_frame_number;
955 fragment = (fragment_t *) wmem_tree_lookup32_array_le(fragments, key);
956 if (fragment && fragment->interface_id == interface_id &&
957 fragment->adapter_id == adapter_id &&
958 fragment->chandle == chandle &&
959 fragment->dlci == dlci &&
960 fragment->role == role &&
961 fragment->reassemble_state != REASSEMBLE_FRAGMENT) {
965 i_data_offset = fragment->index + fragment->length;
966 at_data = (guint8 *) wmem_alloc(pinfo->pool, fragment->index + fragment->length);
968 i_fragment = fragment;
970 if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
971 i_data_offset -= i_fragment->reassemble_end_offset;
972 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->reassemble_end_offset);
974 i_fragment = i_fragment->previous_fragment;
978 while (i_fragment && i_fragment->index > 0) {
979 i_data_offset -= i_fragment->length;
980 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
981 i_fragment = i_fragment->previous_fragment;
984 if (i_fragment && i_fragment->reassemble_state == REASSEMBLE_PARTIALLY) {
985 i_data_offset -= (i_fragment->length - i_fragment->reassemble_start_offset);
986 memcpy(at_data + i_data_offset, i_fragment->data + i_fragment->reassemble_start_offset,
987 i_fragment->length - i_fragment->reassemble_start_offset);
988 } else if (i_fragment) {
989 i_data_offset -= i_fragment->length;
990 memcpy(at_data + i_data_offset, i_fragment->data, i_fragment->length);
994 if (fragment->index > 0 && fragment->length > 0) {
995 proto_tree_add_item(main_tree, hf_fragment, tvb, offset,
996 tvb_length_remaining(tvb, offset), ENC_ASCII | ENC_NA);
997 reassembled_tvb = tvb_new_child_real_data(tvb, at_data,
998 fragment->index + fragment->length, fragment->index + fragment->length);
999 add_new_data_source(pinfo, reassembled_tvb, "Reassembled HSP");
1003 if (reassembled_tvb) {
1004 guint reassembled_offset = 0;
1006 while (tvb_length(reassembled_tvb) > reassembled_offset) {
1007 reassembled_offset = dissect_at_command(reassembled_tvb,
1008 pinfo, main_tree, reassembled_offset, role, command_number);
1009 command_number += 1;
1012 while (tvb_length(tvb) > (guint) offset) {
1013 offset = dissect_at_command(tvb, pinfo, main_tree, offset, role, command_number);
1014 command_number += 1;
1018 col_append_fstr(pinfo->cinfo, COL_INFO, "Fragment: %s",
1019 tvb_format_text_wsp(tvb, offset, tvb_length_remaining(tvb, offset)));
1020 pitem = proto_tree_add_item(main_tree, hf_fragmented, tvb, 0, 0, ENC_NA);
1021 PROTO_ITEM_SET_GENERATED(pitem);
1022 proto_tree_add_item(main_tree, hf_fragment, tvb, offset,
1023 tvb_length_remaining(tvb, offset), ENC_ASCII | ENC_NA);
1030 proto_register_bthsp(void)
1033 expert_module_t *expert_bthsp;
1035 static hf_register_info hf[] = {
1037 { "Command", "bthsp.command",
1038 FT_NONE, BASE_NONE, NULL, 0,
1042 { "Parameters", "bthsp.parameters",
1043 FT_NONE, BASE_NONE, NULL, 0,
1047 { "Command frame number in", "bthsp.command_in",
1048 FT_FRAMENUM, BASE_NONE, NULL, 0,
1052 { "Unsolicited", "bthsp.unsolicited",
1053 FT_NONE, BASE_NONE, NULL, 0,
1057 { "AT Stream", "bthsp.data",
1058 FT_STRING, BASE_NONE, NULL, 0,
1062 { "Fragment", "bthsp.fragment",
1063 FT_STRING, BASE_NONE, NULL, 0,
1067 { "Fragmented", "bthsp.fragmented",
1068 FT_NONE, BASE_NONE, NULL, 0,
1072 { "Ignored", "bthsp.ignored",
1073 FT_BYTES, BASE_NONE, NULL, 0,
1077 { "Command", "bthsp.at_cmd",
1078 FT_STRING, BASE_NONE, NULL, 0,
1082 { "Type", "bthsp.at_cmd.type",
1083 FT_UINT16, BASE_HEX, VALS(at_cmd_type_vals), 0,
1086 { &hf_at_command_line_prefix,
1087 { "Command Line Prefix", "bthsp.command_line_prefix",
1088 FT_STRING, BASE_NONE, NULL, 0,
1092 { "Parameter", "bthsp.parameter",
1093 FT_STRING, BASE_NONE, NULL, 0,
1096 { &hf_unknown_parameter,
1097 { "Unknown Parameter", "bthsp.unknown_parameter",
1098 FT_STRING, BASE_NONE, NULL, 0,
1102 { "Role", "bthsp.role",
1103 FT_UINT8, BASE_DEC, VALS(role_vals), 0,
1107 { "Gain", "bthsp.vgs",
1108 FT_UINT8, BASE_DEC, NULL, 0,
1112 { "Gain", "bthsp.vgm",
1113 FT_UINT8, BASE_DEC, NULL, 0,
1117 { "Key", "bthsp.ckpd",
1118 FT_UINT8, BASE_DEC, NULL, 0,
1123 static ei_register_info ei[] = {
1124 { &ei_non_mandatory_command, { "bthsp.expert.non_mandatory_command", PI_PROTOCOL, PI_NOTE, "Non-mandatory command in HSP", EXPFILL }},
1125 { &ei_invalid_usage, { "bthsp.expert.invalid_usage", PI_PROTOCOL, PI_WARN, "Non mandatory type or command in this role", EXPFILL }},
1126 { &ei_unknown_parameter, { "bthsp.expert.unknown_parameter", PI_PROTOCOL, PI_WARN, "Unknown parameter", EXPFILL }},
1127 { &ei_vgm_gain, { "bthsp.expert.vgm", PI_PROTOCOL, PI_WARN, "Gain of microphone exceeds range 0-15", EXPFILL }},
1128 { &ei_vgs_gain, { "bthsp.expert.vgs", PI_PROTOCOL, PI_WARN, "Gain of speaker exceeds range 0-15", EXPFILL }},
1129 { &ei_ckpd, { "bthsp.expert.ckpd", PI_PROTOCOL, PI_WARN, "Only key 200 is covered in HSP", EXPFILL }} };
1131 static gint *ett[] = {
1134 &ett_bthsp_parameters
1137 fragments = wmem_tree_new_autoreset(wmem_epan_scope(), wmem_file_scope());
1139 proto_bthsp = proto_register_protocol("Bluetooth HSP Profile", "BT HSP", "bthsp");
1140 bthsp_handle = new_register_dissector("bthsp", dissect_bthsp, proto_bthsp);
1142 proto_register_field_array(proto_bthsp, hf, array_length(hf));
1143 proto_register_subtree_array(ett, array_length(ett));
1145 module = prefs_register_protocol(proto_bthsp, NULL);
1146 prefs_register_static_text_preference(module, "hsp.version",
1147 "Bluetooth Profile HSP version: 1.2",
1148 "Version of profile supported by this dissector.");
1150 prefs_register_enum_preference(module, "hsp.hsp_role",
1151 "Force treat packets as AG or HS role",
1152 "Force treat packets as AG or HS role",
1153 &hsp_role, pref_hsp_role, TRUE);
1155 expert_bthsp = expert_register_protocol(proto_bthsp);
1156 expert_register_field_array(expert_bthsp, ei, array_length(ei));
1160 proto_reg_handoff_bthsp(void)
1162 dissector_add_uint("btrfcomm.service", BTSDP_HSP_SERVICE_UUID, bthsp_handle);
1163 dissector_add_uint("btrfcomm.service", BTSDP_HSP_HS_SERVICE_UUID, bthsp_handle);
1164 dissector_add_uint("btrfcomm.service", BTSDP_HSP_GW_SERVICE_UUID, bthsp_handle);
1165 dissector_add_handle("btrfcomm.channel", bthsp_handle);
1169 * Editor modelines - http://www.wireshark.org/tools/modelines.html
1174 * indent-tabs-mode: nil
1177 * vi: set shiftwidth=4 tabstop=8 expandtab:
1178 * :indentSize=4:tabSize=8:noTabs=true: