Path: blob/main/usr.sbin/bluetooth/hccontrol/node.c
103095 views
/*-1* node.c2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2001-2002 Maksim Yevmenkin <[email protected]>6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*29* $Id: node.c,v 1.6 2003/07/22 21:14:02 max Exp $30*/3132#include <sys/ioctl.h>33#include <sys/param.h>34#define L2CAP_SOCKET_CHECKED35#include <bluetooth.h>36#include <errno.h>37#include <netgraph/ng_message.h>38#include <stdio.h>39#include <stdlib.h>40#include <string.h>41#include <unistd.h>42#include <uuid.h>43#include "hccontrol.h"4445/* Send Read_Node_State command to the node */46static int47hci_read_node_state(int s, int argc, char **argv)48{49struct ng_btsocket_hci_raw_node_state r;5051memset(&r, 0, sizeof(r));52if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STATE, &r, sizeof(r)) < 0)53return (ERROR);5455fprintf(stdout, "State: %#x\n", r.state);5657return (OK);58} /* hci_read_node_state */5960/* Send Intitialize command to the node */61static int62hci_node_initialize(int s, int argc, char **argv)63{64if (ioctl(s, SIOC_HCI_RAW_NODE_INIT) < 0)65return (ERROR);6667return (OK);68} /* hci_node_initialize */6970/* Send Read_Debug_Level command to the node */71static int72hci_read_debug_level(int s, int argc, char **argv)73{74struct ng_btsocket_hci_raw_node_debug r;7576memset(&r, 0, sizeof(r));77if (ioctl(s, SIOC_HCI_RAW_NODE_GET_DEBUG, &r, sizeof(r)) < 0)78return (ERROR);7980fprintf(stdout, "Debug level: %d\n", r.debug);8182return (OK);83} /* hci_read_debug_level */8485/* Send Write_Debug_Level command to the node */86static int87hci_write_debug_level(int s, int argc, char **argv)88{89struct ng_btsocket_hci_raw_node_debug r;9091memset(&r, 0, sizeof(r));92switch (argc) {93case 1:94r.debug = atoi(argv[0]);95break;9697default:98return (USAGE);99}100101if (ioctl(s, SIOC_HCI_RAW_NODE_SET_DEBUG, &r, sizeof(r)) < 0)102return (ERROR);103104return (OK);105} /* hci_write_debug_level */106107/* Send Read_Node_Buffer_Size command to the node */108static int109hci_read_node_buffer_size(int s, int argc, char **argv)110{111struct ng_btsocket_hci_raw_node_buffer r;112113memset(&r, 0, sizeof(r));114if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BUFFER, &r, sizeof(r)) < 0)115return (ERROR);116117fprintf(stdout, "Number of free command buffers: %d\n",118r.buffer.cmd_free);119fprintf(stdout, "Max. ACL packet size: %d\n",120r.buffer.acl_size);121fprintf(stdout, "Numbef of free ACL buffers: %d\n",122r.buffer.acl_free);123fprintf(stdout, "Total number of ACL buffers: %d\n",124r.buffer.acl_pkts);125fprintf(stdout, "Max. SCO packet size: %d\n",126r.buffer.sco_size);127fprintf(stdout, "Numbef of free SCO buffers: %d\n",128r.buffer.sco_free);129fprintf(stdout, "Total number of SCO buffers: %d\n",130r.buffer.sco_pkts);131132return (OK);133} /* hci_read_node_buffer_size */134135/* Send Read_Node_BD_ADDR command to the node */136static int137hci_read_node_bd_addr(int s, int argc, char **argv)138{139struct ng_btsocket_hci_raw_node_bdaddr r;140141memset(&r, 0, sizeof(r));142if (ioctl(s, SIOC_HCI_RAW_NODE_GET_BDADDR, &r, sizeof(r)) < 0)143return (ERROR);144145fprintf(stdout, "BD_ADDR: %s\n", bt_ntoa(&r.bdaddr, NULL));146147return (OK);148} /* hci_read_node_bd_addr */149150/* Send Read_Node_Features command to the node */151static int152hci_read_node_features(int s, int argc, char **argv)153{154struct ng_btsocket_hci_raw_node_features r;155int n;156char buffer[2048];157158memset(&r, 0, sizeof(r));159if (ioctl(s, SIOC_HCI_RAW_NODE_GET_FEATURES, &r, sizeof(r)) < 0)160return (ERROR);161162fprintf(stdout, "Features: ");163for (n = 0; n < nitems(r.features); n++)164fprintf(stdout, "%#02x ", r.features[n]);165fprintf(stdout, "\n%s\n", hci_features2str(r.features,166buffer, sizeof(buffer)));167168return (OK);169} /* hci_read_node_features */170171/* Send Read_Node_Stat command to the node */172static int173hci_read_node_stat(int s, int argc, char **argv)174{175struct ng_btsocket_hci_raw_node_stat r;176177memset(&r, 0, sizeof(r));178if (ioctl(s, SIOC_HCI_RAW_NODE_GET_STAT, &r, sizeof(r)) < 0)179return (ERROR);180181fprintf(stdout, "Commands sent: %d\n", r.stat.cmd_sent);182fprintf(stdout, "Events received: %d\n", r.stat.evnt_recv);183fprintf(stdout, "ACL packets received: %d\n", r.stat.acl_recv);184fprintf(stdout, "ACL packets sent: %d\n", r.stat.acl_sent);185fprintf(stdout, "SCO packets received: %d\n", r.stat.sco_recv);186fprintf(stdout, "SCO packets sent: %d\n", r.stat.sco_sent);187fprintf(stdout, "Bytes received: %d\n", r.stat.bytes_recv);188fprintf(stdout, "Bytes sent: %d\n", r.stat.bytes_sent);189190return (OK);191} /* hci_read_node_stat */192193/* Send Reset_Node_Stat command to the node */194static int195hci_reset_node_stat(int s, int argc, char **argv)196{197if (ioctl(s, SIOC_HCI_RAW_NODE_RESET_STAT) < 0)198return (ERROR);199200return (OK);201} /* hci_reset_node_stat */202203/* Send Flush_Neighbor_Cache command to the node */204static int205hci_flush_neighbor_cache(int s, int argc, char **argv)206{207if (ioctl(s, SIOC_HCI_RAW_NODE_FLUSH_NEIGHBOR_CACHE) < 0)208return (ERROR);209210return (OK);211} /* hci_flush_neighbor_cache */212213/* Send Read_Neighbor_Cache command to the node */214static int215hci_read_neighbor_cache(int s, int argc, char **argv)216{217struct ng_btsocket_hci_raw_node_neighbor_cache r;218int n, error = OK;219const char *addrtype2str[] = {"B", "P", "R", "E"};220221memset(&r, 0, sizeof(r));222r.num_entries = NG_HCI_MAX_NEIGHBOR_NUM;223r.entries = calloc(NG_HCI_MAX_NEIGHBOR_NUM,224sizeof(ng_hci_node_neighbor_cache_entry_ep));225if (r.entries == NULL) {226errno = ENOMEM;227return (ERROR);228}229230if (ioctl(s, SIOC_HCI_RAW_NODE_GET_NEIGHBOR_CACHE, &r,231sizeof(r)) < 0) {232error = ERROR;233goto out;234}235236fprintf(stdout,237"T " \238"BD_ADDR " \239"Features " \240"Clock offset " \241"Page scan " \242"Rep. scan\n");243244for (n = 0; n < r.num_entries; n++) {245uint8_t addrtype = r.entries[n].addrtype;246if(addrtype >= nitems(addrtype2str))247addrtype = nitems(addrtype2str) - 1;248fprintf(stdout,249"%1s %-17.17s " \250"%02x %02x %02x %02x %02x %02x %02x %02x " \251"%#12x " \252"%#9x " \253"%#9x\n",254addrtype2str[addrtype],255hci_bdaddr2str(&r.entries[n].bdaddr),256r.entries[n].features[0], r.entries[n].features[1],257r.entries[n].features[2], r.entries[n].features[3],258r.entries[n].features[4], r.entries[n].features[5],259r.entries[n].features[6], r.entries[n].features[7],260r.entries[n].clock_offset, r.entries[n].page_scan_mode,261r.entries[n].page_scan_rep_mode);262print_adv_data(r.entries[n].extinq_size,263r.entries[n].extinq_data);264fprintf(stdout,"\n");265}266out:267free(r.entries);268269return (error);270} /* hci_read_neightbor_cache */271272/* Send Read_Connection_List command to the node */273static int274hci_read_connection_list(int s, int argc, char **argv)275{276struct ng_btsocket_hci_raw_con_list r;277int n, error = OK;278279memset(&r, 0, sizeof(r));280r.num_connections = NG_HCI_MAX_CON_NUM;281r.connections = calloc(NG_HCI_MAX_CON_NUM, sizeof(ng_hci_node_con_ep));282if (r.connections == NULL) {283errno = ENOMEM;284return (ERROR);285}286287if (ioctl(s, SIOC_HCI_RAW_NODE_GET_CON_LIST, &r, sizeof(r)) < 0) {288error = ERROR;289goto out;290}291292fprintf(stdout,293"Remote BD_ADDR " \294"Handle " \295"Type " \296"Mode " \297"Role " \298"Encrypt " \299"Pending " \300"Queue " \301"State\n");302303for (n = 0; n < r.num_connections; n++) {304fprintf(stdout,305"%-17.17s " \306"%6d " \307"%4.4s " \308"%4d " \309"%4.4s " \310"%7.7s " \311"%7d " \312"%5d " \313"%s\n",314hci_bdaddr2str(&r.connections[n].bdaddr),315r.connections[n].con_handle,316(r.connections[n].link_type == NG_HCI_LINK_ACL)?317"ACL" : "SCO",318r.connections[n].mode,319(r.connections[n].role == NG_HCI_ROLE_MASTER)?320"MAST" : "SLAV",321hci_encrypt2str(r.connections[n].encryption_mode, 1),322r.connections[n].pending,323r.connections[n].queue_len,324hci_con_state2str(r.connections[n].state));325}326out:327free(r.connections);328329return (error);330} /* hci_read_connection_list */331332/* Send Read_Node_Link_Policy_Settings_Mask command to the node */333int334hci_read_node_link_policy_settings_mask(int s, int argc, char **argv)335{336struct ng_btsocket_hci_raw_node_link_policy_mask r;337338memset(&r, 0, sizeof(r));339if (ioctl(s, SIOC_HCI_RAW_NODE_GET_LINK_POLICY_MASK, &r, sizeof(r)) < 0)340return (ERROR);341342fprintf(stdout, "Link Policy Settings mask: %#04x\n", r.policy_mask);343344return (OK);345} /* hci_read_node_link_policy_settings_mask */346347/* Send Write_Node_Link_Policy_Settings_Mask command to the node */348int349hci_write_node_link_policy_settings_mask(int s, int argc, char **argv)350{351struct ng_btsocket_hci_raw_node_link_policy_mask r;352int m;353354memset(&r, 0, sizeof(r));355356switch (argc) {357case 1:358if (sscanf(argv[0], "%x", &m) != 1)359return (USAGE);360361r.policy_mask = (m & 0xffff);362break;363364default:365return (USAGE);366}367368if (ioctl(s, SIOC_HCI_RAW_NODE_SET_LINK_POLICY_MASK, &r, sizeof(r)) < 0)369return (ERROR);370371return (OK);372} /* hci_write_node_link_policy_settings_mask */373374/* Send Read_Node_Packet_Mask command to the node */375int376hci_read_node_packet_mask(int s, int argc, char **argv)377{378struct ng_btsocket_hci_raw_node_packet_mask r;379380memset(&r, 0, sizeof(r));381if (ioctl(s, SIOC_HCI_RAW_NODE_GET_PACKET_MASK, &r, sizeof(r)) < 0)382return (ERROR);383384fprintf(stdout, "Packet mask: %#04x\n", r.packet_mask);385386return (OK);387} /* hci_read_node_packet_mask */388389/* Send Write_Node_Packet_Mask command to the node */390int391hci_write_node_packet_mask(int s, int argc, char **argv)392{393struct ng_btsocket_hci_raw_node_packet_mask r;394int m;395396memset(&r, 0, sizeof(r));397398switch (argc) {399case 1:400if (sscanf(argv[0], "%x", &m) != 1)401return (USAGE);402403r.packet_mask = (m & 0xffff);404break;405406default:407return (USAGE);408}409410if (ioctl(s, SIOC_HCI_RAW_NODE_SET_PACKET_MASK, &r, sizeof(r)) < 0)411return (ERROR);412413return (OK);414} /* hci_write_node_packet_mask */415416/* Send Read_Node_Role_Switch command to the node */417int418hci_read_node_role_switch(int s, int argc, char **argv)419{420struct ng_btsocket_hci_raw_node_role_switch r;421422memset(&r, 0, sizeof(r));423if (ioctl(s, SIOC_HCI_RAW_NODE_GET_ROLE_SWITCH, &r, sizeof(r)) < 0)424return (ERROR);425426fprintf(stdout, "Role switch: %d\n", r.role_switch);427428return (OK);429} /* hci_read_node_role_switch */430431/* Send Write_Node_Role_Switch command to the node */432int433hci_write_node_role_switch(int s, int argc, char **argv)434{435struct ng_btsocket_hci_raw_node_role_switch r;436int m;437438memset(&r, 0, sizeof(r));439440switch (argc) {441case 1:442if (sscanf(argv[0], "%d", &m) != 1)443return (USAGE);444445r.role_switch = m? 1 : 0;446break;447448default:449return (USAGE);450}451452if (ioctl(s, SIOC_HCI_RAW_NODE_SET_ROLE_SWITCH, &r, sizeof(r)) < 0)453return (ERROR);454455return (OK);456} /* hci_write_node_role_switch */457458/* Send Read_Node_List command to the node */459int460hci_read_node_list(int s, int argc, char **argv)461{462struct ng_btsocket_hci_raw_node_list_names r;463int i;464465r.num_names = MAX_NODE_NUM;466r.names = (struct nodeinfo*)calloc(MAX_NODE_NUM, sizeof(struct nodeinfo));467if (r.names == NULL)468return (ERROR);469470if (ioctl(s, SIOC_HCI_RAW_NODE_LIST_NAMES, &r, sizeof(r)) < 0) {471free(r.names);472return (ERROR);473}474475fprintf(stdout, "Name ID Num hooks\n");476for (i = 0; i < r.num_names; ++i)477fprintf(stdout, "%-15s %08x %9d\n",478r.names[i].name, r.names[i].id, r.names[i].hooks);479480free(r.names);481482return (OK);483} /* hci_read_node_list */484485struct hci_command node_commands[] = {486{487"read_node_state",488"Get the HCI node state",489&hci_read_node_state490},491{492"initialize",493"Initialize the HCI node",494&hci_node_initialize495},496{497"read_debug_level",498"Read the HCI node debug level",499&hci_read_debug_level500},501{502"write_debug_level <level>",503"Write the HCI node debug level",504&hci_write_debug_level505},506{507"read_node_buffer_size",508"Read the HCI node buffer information. This will return current state of the\n"\509"HCI buffer for the HCI node",510&hci_read_node_buffer_size511},512{513"read_node_bd_addr",514"Read the HCI node BD_ADDR. Returns device BD_ADDR as cached by the HCI node",515&hci_read_node_bd_addr516},517{518"read_node_features",519"Read the HCI node features. This will return list of supported features as\n" \520"cached by the HCI node",521&hci_read_node_features522},523{524"read_node_stat",525"Read packets and bytes counters for the HCI node",526&hci_read_node_stat527},528{529"reset_node_stat",530"Reset packets and bytes counters for the HCI node",531&hci_reset_node_stat532},533{534"flush_neighbor_cache",535"Flush content of the HCI node neighbor cache",536&hci_flush_neighbor_cache537},538{539"read_neighbor_cache",540"Read content of the HCI node neighbor cache",541&hci_read_neighbor_cache542},543{544"read_connection_list",545"Read the baseband connection descriptors list for the HCI node",546&hci_read_connection_list547},548{549"read_node_link_policy_settings_mask",550"Read the value of the Link Policy Settinngs mask for the HCI node",551&hci_read_node_link_policy_settings_mask552},553{554"write_node_link_policy_settings_mask <policy_mask>",555"Write the value of the Link Policy Settings mask for the HCI node. By default\n" \556"all supported Link Policy modes (as reported by the local device features) are\n"\557"enabled. The particular Link Policy mode is enabled if local device supports\n"\558"it and correspinding bit in the mask was set\n\n" \559"\t<policy_mask> - xxxx; Link Policy mask\n" \560"\t\t0x0000 - Disable All LM Modes\n" \561"\t\t0x0001 - Enable Master Slave Switch\n" \562"\t\t0x0002 - Enable Hold Mode\n" \563"\t\t0x0004 - Enable Sniff Mode\n" \564"\t\t0x0008 - Enable Park Mode\n",565&hci_write_node_link_policy_settings_mask566},567{568"read_node_packet_mask",569"Read the value of the Packet mask for the HCI node",570&hci_read_node_packet_mask571},572{573"write_node_packet_mask <packet_mask>",574"Write the value of the Packet mask for the HCI node. By default all supported\n" \575"packet types (as reported by the local device features) are enabled. The\n" \576"particular packet type is enabled if local device supports it and corresponding\n" \577"bit in the mask was set\n\n" \578"\t<packet_mask> - xxxx; packet type mask\n" \579"" \580"\t\tACL packets\n" \581"\t\t-----------\n" \582"\t\t0x0008 DM1\n" \583"\t\t0x0010 DH1\n" \584"\t\t0x0400 DM3\n" \585"\t\t0x0800 DH3\n" \586"\t\t0x4000 DM5\n" \587"\t\t0x8000 DH5\n" \588"\n" \589"\t\tSCO packets\n" \590"\t\t-----------\n" \591"\t\t0x0020 HV1\n" \592"\t\t0x0040 HV2\n" \593"\t\t0x0080 HV3\n",594&hci_write_node_packet_mask595},596{597"read_node_role_switch",598"Read the value of the Role Switch parameter for the HCI node",599&hci_read_node_role_switch600},601{602"write_node_role_switch {0|1}",603"Write the value of the Role Switch parameter for the HCI node. By default,\n" \604"if Role Switch is supported, local device will try to perform Role Switch\n" \605"and become Master on incoming connection. Some devices do not support Role\n" \606"Switch and thus incoming connections from such devices will fail. Setting\n" \607"this parameter to zero will prevent Role Switch and thus accepting device\n" \608"will remain Slave",609&hci_write_node_role_switch610},611{612"read_node_list",613"Get a list of HCI nodes, their Netgraph IDs and connected hooks.",614&hci_read_node_list615},616{617NULL,618}};619620621622