4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 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: node.c,v 1.6 2003/07/22 21:14:02 max Exp $
34 #include <sys/ioctl.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
38 #include <netgraph/ng_message.h>
44 #include "hccontrol.h"
46 /* Send Read_Node_State command to the node */
48 hci_read_node_state(int s, int argc, char **argv)
50 struct ng_btsocket_hci_raw_node_state r;
52 memset(&r, 0, sizeof(r));
53 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STATE, &r, sizeof(r)) < 0)
56 fprintf(stdout, "State: %#x\n", r.state);
59 } /* hci_read_node_state */
61 /* Send Intitialize command to the node */
63 hci_node_initialize(int s, int argc, char **argv)
65 if (ioctl(s, SIOC_HCI_RAW_NODE_INIT) < 0)
69 } /* hci_node_initialize */
71 /* Send Read_Debug_Level command to the node */
73 hci_read_debug_level(int s, int argc, char **argv)
75 struct ng_btsocket_hci_raw_node_debug r;
77 memset(&r, 0, sizeof(r));
78 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_DEBUG, &r, sizeof(r)) < 0)
81 fprintf(stdout, "Debug level: %d\n", r.debug);
84 } /* hci_read_debug_level */
86 /* Send Write_Debug_Level command to the node */
88 hci_write_debug_level(int s, int argc, char **argv)
90 struct ng_btsocket_hci_raw_node_debug r;
92 memset(&r, 0, sizeof(r));
95 r.debug = atoi(argv[0]);
102 if (ioctl(s, SIOC_HCI_RAW_NODE_SET_DEBUG, &r, sizeof(r)) < 0)
106 } /* hci_write_debug_level */
108 /* Send Read_Node_Buffer_Size command to the node */
110 hci_read_node_buffer_size(int s, int argc, char **argv)
112 struct ng_btsocket_hci_raw_node_buffer r;
114 memset(&r, 0, sizeof(r));
115 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BUFFER, &r, sizeof(r)) < 0)
118 fprintf(stdout, "Number of free command buffers: %d\n",
120 fprintf(stdout, "Max. ACL packet size: %d\n",
122 fprintf(stdout, "Numbef of free ACL buffers: %d\n",
124 fprintf(stdout, "Total number of ACL buffers: %d\n",
126 fprintf(stdout, "Max. SCO packet size: %d\n",
128 fprintf(stdout, "Numbef of free SCO buffers: %d\n",
130 fprintf(stdout, "Total number of SCO buffers: %d\n",
134 } /* hci_read_node_buffer_size */
136 /* Send Read_Node_BD_ADDR command to the node */
138 hci_read_node_bd_addr(int s, int argc, char **argv)
140 struct ng_btsocket_hci_raw_node_bdaddr r;
142 memset(&r, 0, sizeof(r));
143 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BDADDR, &r, sizeof(r)) < 0)
146 fprintf(stdout, "BD_ADDR: %s\n", bt_ntoa(&r.bdaddr, NULL));
149 } /* hci_read_node_bd_addr */
151 /* Send Read_Node_Features command to the node */
153 hci_read_node_features(int s, int argc, char **argv)
155 struct ng_btsocket_hci_raw_node_features r;
159 memset(&r, 0, sizeof(r));
160 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_FEATURES, &r, sizeof(r)) < 0)
163 fprintf(stdout, "Features: ");
164 for (n = 0; n < sizeof(r.features)/sizeof(r.features[0]); n++)
165 fprintf(stdout, "%#02x ", r.features[n]);
166 fprintf(stdout, "\n%s\n", hci_features2str(r.features,
167 buffer, sizeof(buffer)));
170 } /* hci_read_node_features */
172 /* Send Read_Node_Stat command to the node */
174 hci_read_node_stat(int s, int argc, char **argv)
176 struct ng_btsocket_hci_raw_node_stat r;
178 memset(&r, 0, sizeof(r));
179 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STAT, &r, sizeof(r)) < 0)
182 fprintf(stdout, "Commands sent: %d\n", r.stat.cmd_sent);
183 fprintf(stdout, "Events received: %d\n", r.stat.evnt_recv);
184 fprintf(stdout, "ACL packets received: %d\n", r.stat.acl_recv);
185 fprintf(stdout, "ACL packets sent: %d\n", r.stat.acl_sent);
186 fprintf(stdout, "SCO packets received: %d\n", r.stat.sco_recv);
187 fprintf(stdout, "SCO packets sent: %d\n", r.stat.sco_sent);
188 fprintf(stdout, "Bytes received: %d\n", r.stat.bytes_recv);
189 fprintf(stdout, "Bytes sent: %d\n", r.stat.bytes_sent);
192 } /* hci_read_node_stat */
194 /* Send Reset_Node_Stat command to the node */
196 hci_reset_node_stat(int s, int argc, char **argv)
198 if (ioctl(s, SIOC_HCI_RAW_NODE_RESET_STAT) < 0)
202 } /* hci_reset_node_stat */
204 /* Send Flush_Neighbor_Cache command to the node */
206 hci_flush_neighbor_cache(int s, int argc, char **argv)
208 if (ioctl(s, SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE) < 0)
212 } /* hci_flush_neighbor_cache */
214 /* Send Read_Neighbor_Cache command to the node */
216 hci_read_neighbor_cache(int s, int argc, char **argv)
218 struct ng_btsocket_hci_raw_node_neighbor_cache r;
220 const char *addrtype2str[] = {"B", "P", "R", "E"};
222 memset(&r, 0, sizeof(r));
223 r.num_entries = NG_HCI_MAX_NEIGHBOR_NUM;
224 r.entries = calloc(NG_HCI_MAX_NEIGHBOR_NUM,
225 sizeof(ng_hci_node_neighbor_cache_entry_ep));
226 if (r.entries == NULL) {
231 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE, &r,
245 for (n = 0; n < r.num_entries; n++) {
246 uint8_t addrtype = r.entries[n].addrtype;
247 if(addrtype >= sizeof(addrtype2str)/sizeof(addrtype2str[0]))
248 addrtype = sizeof(addrtype2str)/sizeof(addrtype2str[0]) - 1;
251 "%02x %02x %02x %02x %02x %02x %02x %02x " \
255 addrtype2str[addrtype],
256 hci_bdaddr2str(&r.entries[n].bdaddr),
257 r.entries[n].features[0], r.entries[n].features[1],
258 r.entries[n].features[2], r.entries[n].features[3],
259 r.entries[n].features[4], r.entries[n].features[5],
260 r.entries[n].features[6], r.entries[n].features[7],
261 r.entries[n].clock_offset, r.entries[n].page_scan_mode,
262 r.entries[n].page_scan_rep_mode);
263 print_adv_data(r.entries[n].extinq_size,
264 r.entries[n].extinq_data);
265 fprintf(stdout,"\n");
271 } /* hci_read_neightbor_cache */
273 /* Send Read_Connection_List command to the node */
275 hci_read_connection_list(int s, int argc, char **argv)
277 struct ng_btsocket_hci_raw_con_list r;
280 memset(&r, 0, sizeof(r));
281 r.num_connections = NG_HCI_MAX_CON_NUM;
282 r.connections = calloc(NG_HCI_MAX_CON_NUM, sizeof(ng_hci_node_con_ep));
283 if (r.connections == NULL) {
288 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_CON_LIST, &r, sizeof(r)) < 0) {
304 for (n = 0; n < r.num_connections; n++) {
315 hci_bdaddr2str(&r.connections[n].bdaddr),
316 r.connections[n].con_handle,
317 (r.connections[n].link_type == NG_HCI_LINK_ACL)?
319 r.connections[n].mode,
320 (r.connections[n].role == NG_HCI_ROLE_MASTER)?
322 hci_encrypt2str(r.connections[n].encryption_mode, 1),
323 r.connections[n].pending,
324 r.connections[n].queue_len,
325 hci_con_state2str(r.connections[n].state));
331 } /* hci_read_connection_list */
333 /* Send Read_Node_Link_Policy_Settings_Mask command to the node */
335 hci_read_node_link_policy_settings_mask(int s, int argc, char **argv)
337 struct ng_btsocket_hci_raw_node_link_policy_mask r;
339 memset(&r, 0, sizeof(r));
340 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK, &r, sizeof(r)) < 0)
343 fprintf(stdout, "Link Policy Settings mask: %#04x\n", r.policy_mask);
346 } /* hci_read_node_link_policy_settings_mask */
348 /* Send Write_Node_Link_Policy_Settings_Mask command to the node */
350 hci_write_node_link_policy_settings_mask(int s, int argc, char **argv)
352 struct ng_btsocket_hci_raw_node_link_policy_mask r;
355 memset(&r, 0, sizeof(r));
359 if (sscanf(argv[0], "%x", &m) != 1)
362 r.policy_mask = (m & 0xffff);
369 if (ioctl(s, SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK, &r, sizeof(r)) < 0)
373 } /* hci_write_node_link_policy_settings_mask */
375 /* Send Read_Node_Packet_Mask command to the node */
377 hci_read_node_packet_mask(int s, int argc, char **argv)
379 struct ng_btsocket_hci_raw_node_packet_mask r;
381 memset(&r, 0, sizeof(r));
382 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_PACKET_MASK, &r, sizeof(r)) < 0)
385 fprintf(stdout, "Packet mask: %#04x\n", r.packet_mask);
388 } /* hci_read_node_packet_mask */
390 /* Send Write_Node_Packet_Mask command to the node */
392 hci_write_node_packet_mask(int s, int argc, char **argv)
394 struct ng_btsocket_hci_raw_node_packet_mask r;
397 memset(&r, 0, sizeof(r));
401 if (sscanf(argv[0], "%x", &m) != 1)
404 r.packet_mask = (m & 0xffff);
411 if (ioctl(s, SIOC_HCI_RAW_NODE_SET_PACKET_MASK, &r, sizeof(r)) < 0)
415 } /* hci_write_node_packet_mask */
417 /* Send Read_Node_Role_Switch command to the node */
419 hci_read_node_role_switch(int s, int argc, char **argv)
421 struct ng_btsocket_hci_raw_node_role_switch r;
423 memset(&r, 0, sizeof(r));
424 if (ioctl(s, SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH, &r, sizeof(r)) < 0)
427 fprintf(stdout, "Role switch: %d\n", r.role_switch);
430 } /* hci_read_node_role_switch */
432 /* Send Write_Node_Role_Switch command to the node */
434 hci_write_node_role_switch(int s, int argc, char **argv)
436 struct ng_btsocket_hci_raw_node_role_switch r;
439 memset(&r, 0, sizeof(r));
443 if (sscanf(argv[0], "%d", &m) != 1)
446 r.role_switch = m? 1 : 0;
453 if (ioctl(s, SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH, &r, sizeof(r)) < 0)
457 } /* hci_write_node_role_switch */
459 /* Send Read_Node_List command to the node */
461 hci_read_node_list(int s, int argc, char **argv)
463 struct ng_btsocket_hci_raw_node_list_names r;
466 r.num_names = MAX_NODE_NUM;
467 r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo));
471 if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) {
476 fprintf(stdout, "Name ID Num hooks\n");
477 for (i = 0; i < r.num_names; ++i)
478 fprintf(stdout, "%-15s %08x %9d\n",
479 r.names[i].name, r.names[i].id, r.names[i].hooks);
484 } /* hci_read_node_list */
486 struct hci_command node_commands[] = {
489 "Get the HCI node state",
494 "Initialize the HCI node",
499 "Read the HCI node debug level",
500 &hci_read_debug_level
503 "write_debug_level <level>",
504 "Write the HCI node debug level",
505 &hci_write_debug_level
508 "read_node_buffer_size",
509 "Read the HCI node buffer information. This will return current state of the\n"\
510 "HCI buffer for the HCI node",
511 &hci_read_node_buffer_size
515 "Read the HCI node BD_ADDR. Returns device BD_ADDR as cached by the HCI node",
516 &hci_read_node_bd_addr
519 "read_node_features",
520 "Read the HCI node features. This will return list of supported features as\n" \
521 "cached by the HCI node",
522 &hci_read_node_features
526 "Read packets and bytes counters for the HCI node",
531 "Reset packets and bytes counters for the HCI node",
535 "flush_neighbor_cache",
536 "Flush content of the HCI node neighbor cache",
537 &hci_flush_neighbor_cache
540 "read_neighbor_cache",
541 "Read content of the HCI node neighbor cache",
542 &hci_read_neighbor_cache
545 "read_connection_list",
546 "Read the baseband connection descriptors list for the HCI node",
547 &hci_read_connection_list
550 "read_node_link_policy_settings_mask",
551 "Read the value of the Link Policy Settinngs mask for the HCI node",
552 &hci_read_node_link_policy_settings_mask
555 "write_node_link_policy_settings_mask <policy_mask>",
556 "Write the value of the Link Policy Settings mask for the HCI node. By default\n" \
557 "all supported Link Policy modes (as reported by the local device features) are\n"\
558 "enabled. The particular Link Policy mode is enabled if local device supports\n"\
559 "it and correspinding bit in the mask was set\n\n" \
560 "\t<policy_mask> - xxxx; Link Policy mask\n" \
561 "\t\t0x0000 - Disable All LM Modes\n" \
562 "\t\t0x0001 - Enable Master Slave Switch\n" \
563 "\t\t0x0002 - Enable Hold Mode\n" \
564 "\t\t0x0004 - Enable Sniff Mode\n" \
565 "\t\t0x0008 - Enable Park Mode\n",
566 &hci_write_node_link_policy_settings_mask
569 "read_node_packet_mask",
570 "Read the value of the Packet mask for the HCI node",
571 &hci_read_node_packet_mask
574 "write_node_packet_mask <packet_mask>",
575 "Write the value of the Packet mask for the HCI node. By default all supported\n" \
576 "packet types (as reported by the local device features) are enabled. The\n" \
577 "particular packet type is enabled if local device supports it and corresponding\n" \
578 "bit in the mask was set\n\n" \
579 "\t<packet_mask> - xxxx; packet type mask\n" \
581 "\t\tACL packets\n" \
582 "\t\t-----------\n" \
590 "\t\tSCO packets\n" \
591 "\t\t-----------\n" \
595 &hci_write_node_packet_mask
598 "read_node_role_switch",
599 "Read the value of the Role Switch parameter for the HCI node",
600 &hci_read_node_role_switch
603 "write_node_role_switch {0|1}",
604 "Write the value of the Role Switch parameter for the HCI node. By default,\n" \
605 "if Role Switch is supported, local device will try to perform Role Switch\n" \
606 "and become Master on incoming connection. Some devices do not support Role\n" \
607 "Switch and thus incoming connections from such devices will fail. Setting\n" \
608 "this parameter to zero will prevent Role Switch and thus accepting device\n" \
610 &hci_write_node_role_switch
614 "Get a list of HCI nodes, their Netgraph IDs and connected hooks.",