Path: blob/main/usr.sbin/bluetooth/hccontrol/status.c
105642 views
/*-1* status.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: status.c,v 1.2 2003/05/21 22:40:30 max Exp $30*/3132#include <sys/types.h>33#include <sys/endian.h>34#include <errno.h>35#include <netgraph/bluetooth/include/ng_hci.h>36#include <stdio.h>37#include "hccontrol.h"3839/* Send Read_Failed_Contact_Counter command to the unit */40static int41hci_read_failed_contact_counter(int s, int argc, char **argv)42{43ng_hci_read_failed_contact_cntr_cp cp;44ng_hci_read_failed_contact_cntr_rp rp;45int n;4647switch (argc) {48case 1:49/* connection handle */50if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)51return (USAGE);5253cp.con_handle = (uint16_t) (n & 0x0fff);54cp.con_handle = htole16(cp.con_handle);55break;5657default:58return (USAGE);59}6061/* send command */62n = sizeof(rp);63if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,64NG_HCI_OCF_READ_FAILED_CONTACT_CNTR),65(char const *) &cp, sizeof(cp),66(char *) &rp, &n) == ERROR)67return (ERROR);6869if (rp.status != 0x00) {70fprintf(stdout, "Status: %s [%#02x]\n",71hci_status2str(rp.status), rp.status);72return (FAILED);73}7475fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));76fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter));7778return (OK);79} /* hci_read_failed_contact_counter */8081/* Send Reset_Failed_Contact_Counter command to the unit */82static int83hci_reset_failed_contact_counter(int s, int argc, char **argv)84{85ng_hci_reset_failed_contact_cntr_cp cp;86ng_hci_reset_failed_contact_cntr_rp rp;87int n;8889switch (argc) {90case 1:91/* connection handle */92if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)93return (USAGE);9495cp.con_handle = (uint16_t) (n & 0x0fff);96cp.con_handle = htole16(cp.con_handle);97break;9899default:100return (USAGE);101}102103/* send command */104n = sizeof(rp);105if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,106NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR),107(char const *) &cp, sizeof(cp),108(char *) &rp, &n) == ERROR)109return (ERROR);110111if (rp.status != 0x00) {112fprintf(stdout, "Status: %s [%#02x]\n",113hci_status2str(rp.status), rp.status);114return (FAILED);115}116117return (OK);118} /* hci_reset_failed_contact_counter */119120/* Sent Get_Link_Quality command to the unit */121static int122hci_get_link_quality(int s, int argc, char **argv)123{124ng_hci_get_link_quality_cp cp;125ng_hci_get_link_quality_rp rp;126int n;127128switch (argc) {129case 1:130/* connection handle */131if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)132return (USAGE);133134cp.con_handle = (uint16_t) (n & 0x0fff);135cp.con_handle = htole16(cp.con_handle);136break;137138default:139return (USAGE);140}141142/* send command */143n = sizeof(rp);144if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,145NG_HCI_OCF_GET_LINK_QUALITY),146(char const *) &cp, sizeof(cp),147(char *) &rp, &n) == ERROR)148return (ERROR);149150if (rp.status != 0x00) {151fprintf(stdout, "Status: %s [%#02x]\n",152hci_status2str(rp.status), rp.status);153return (FAILED);154}155156fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));157fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality));158159return (OK);160} /* hci_get_link_quality */161162/* Send Read_RSSI command to the unit */163static int164hci_read_rssi(int s, int argc, char **argv)165{166ng_hci_read_rssi_cp cp;167ng_hci_read_rssi_rp rp;168int n;169170switch (argc) {171case 1:172/* connection handle */173if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff)174return (USAGE);175176cp.con_handle = (uint16_t) (n & 0x0fff);177cp.con_handle = htole16(cp.con_handle);178break;179180default:181return (USAGE);182}183184/* send command */185n = sizeof(rp);186if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS,187NG_HCI_OCF_READ_RSSI),188(char const *) &cp, sizeof(cp),189(char *) &rp, &n) == ERROR)190return (ERROR);191192if (rp.status != 0x00) {193fprintf(stdout, "Status: %s [%#02x]\n",194hci_status2str(rp.status), rp.status);195return (FAILED);196}197198fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle));199fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi);200201return (OK);202} /* hci_read_rssi */203204struct hci_command status_commands[] = {205{206"read_failed_contact_counter <connection_handle>",207"\nThis command will read the value for the Failed_Contact_Counter\n" \208"parameter for a particular ACL connection to another device.\n\n" \209"\t<connection_handle> - dddd; ACL connection handle\n",210&hci_read_failed_contact_counter211},212{213"reset_failed_contact_counter <connection_handle>",214"\nThis command will reset the value for the Failed_Contact_Counter\n" \215"parameter for a particular ACL connection to another device.\n\n" \216"\t<connection_handle> - dddd; ACL connection handle\n",217&hci_reset_failed_contact_counter218},219{220"get_link_quality <connection_handle>",221"\nThis command will return the value for the Link_Quality for the\n" \222"specified ACL connection handle. This command will return a Link_Quality\n" \223"value from 0-255, which represents the quality of the link between two\n" \224"Bluetooth devices. The higher the value, the better the link quality is.\n" \225"Each Bluetooth module vendor will determine how to measure the link quality." \226"\n\n" \227"\t<connection_handle> - dddd; ACL connection handle\n",228&hci_get_link_quality229},230{231"read_rssi <connection_handle>",232"\nThis command will read the value for the difference between the\n" \233"measured Received Signal Strength Indication (RSSI) and the limits of\n" \234"the Golden Receive Power Range for a ACL connection handle to another\n" \235"Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \236"indicates how many dB the RSSI is above the upper limit, any negative\n" \237"value indicates how many dB the RSSI is below the lower limit. The value\n" \238"zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \239"\t<connection_handle> - dddd; ACL connection handle\n",240&hci_read_rssi241},242{243NULL,244}};245246247248