1878ed226SJulian Elischer /* 2878ed226SJulian Elischer * status.c 3878ed226SJulian Elischer * 4878ed226SJulian Elischer * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5878ed226SJulian Elischer * All rights reserved. 6878ed226SJulian Elischer * 7878ed226SJulian Elischer * Redistribution and use in source and binary forms, with or without 8878ed226SJulian Elischer * modification, are permitted provided that the following conditions 9878ed226SJulian Elischer * are met: 10878ed226SJulian Elischer * 1. Redistributions of source code must retain the above copyright 11878ed226SJulian Elischer * notice, this list of conditions and the following disclaimer. 12878ed226SJulian Elischer * 2. Redistributions in binary form must reproduce the above copyright 13878ed226SJulian Elischer * notice, this list of conditions and the following disclaimer in the 14878ed226SJulian Elischer * documentation and/or other materials provided with the distribution. 15878ed226SJulian Elischer * 16878ed226SJulian Elischer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17878ed226SJulian Elischer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18878ed226SJulian Elischer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19878ed226SJulian Elischer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20878ed226SJulian Elischer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21878ed226SJulian Elischer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22878ed226SJulian Elischer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23878ed226SJulian Elischer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24878ed226SJulian Elischer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25878ed226SJulian Elischer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26878ed226SJulian Elischer * SUCH DAMAGE. 27878ed226SJulian Elischer * 280986ab12SMaksim Yevmenkin * $Id: status.c,v 1.2 2003/05/21 22:40:30 max Exp $ 29878ed226SJulian Elischer * $FreeBSD$ 30878ed226SJulian Elischer */ 31878ed226SJulian Elischer 32878ed226SJulian Elischer #include <sys/types.h> 33878ed226SJulian Elischer #include <sys/endian.h> 34878ed226SJulian Elischer #include <errno.h> 350986ab12SMaksim Yevmenkin #include <netgraph/bluetooth/include/ng_hci.h> 36878ed226SJulian Elischer #include <stdio.h> 37878ed226SJulian Elischer #include "hccontrol.h" 38878ed226SJulian Elischer 39878ed226SJulian Elischer /* Send Read_Failed_Contact_Counter command to the unit */ 40878ed226SJulian Elischer static int 41878ed226SJulian Elischer hci_read_failed_contact_counter(int s, int argc, char **argv) 42878ed226SJulian Elischer { 43878ed226SJulian Elischer ng_hci_read_failed_contact_cntr_cp cp; 44878ed226SJulian Elischer ng_hci_read_failed_contact_cntr_rp rp; 45878ed226SJulian Elischer int n; 46878ed226SJulian Elischer 47878ed226SJulian Elischer switch (argc) { 48878ed226SJulian Elischer case 1: 49878ed226SJulian Elischer /* connection handle */ 50878ed226SJulian Elischer if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 51878ed226SJulian Elischer return (USAGE); 52878ed226SJulian Elischer 53878ed226SJulian Elischer cp.con_handle = (u_int16_t) (n & 0x0fff); 54878ed226SJulian Elischer cp.con_handle = htole16(cp.con_handle); 55878ed226SJulian Elischer break; 56878ed226SJulian Elischer 57878ed226SJulian Elischer default: 58878ed226SJulian Elischer return (USAGE); 59878ed226SJulian Elischer } 60878ed226SJulian Elischer 61878ed226SJulian Elischer /* send command */ 62878ed226SJulian Elischer n = sizeof(rp); 63878ed226SJulian Elischer if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS, 64878ed226SJulian Elischer NG_HCI_OCF_READ_FAILED_CONTACT_CNTR), 65878ed226SJulian Elischer (char const *) &cp, sizeof(cp), 66878ed226SJulian Elischer (char *) &rp, &n) == ERROR) 67878ed226SJulian Elischer return (ERROR); 68878ed226SJulian Elischer 69878ed226SJulian Elischer if (rp.status != 0x00) { 70878ed226SJulian Elischer fprintf(stdout, "Status: %s [%#02x]\n", 71878ed226SJulian Elischer hci_status2str(rp.status), rp.status); 72878ed226SJulian Elischer return (FAILED); 73878ed226SJulian Elischer } 74878ed226SJulian Elischer 75878ed226SJulian Elischer fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle)); 76878ed226SJulian Elischer fprintf(stdout, "Failed contact counter: %d\n", le16toh(rp.counter)); 77878ed226SJulian Elischer 78878ed226SJulian Elischer return (OK); 79878ed226SJulian Elischer } /* hci_read_failed_contact_counter */ 80878ed226SJulian Elischer 81878ed226SJulian Elischer /* Send Reset_Failed_Contact_Counter command to the unit */ 82878ed226SJulian Elischer static int 83878ed226SJulian Elischer hci_reset_failed_contact_counter(int s, int argc, char **argv) 84878ed226SJulian Elischer { 85878ed226SJulian Elischer ng_hci_reset_failed_contact_cntr_cp cp; 86878ed226SJulian Elischer ng_hci_reset_failed_contact_cntr_rp rp; 87878ed226SJulian Elischer int n; 88878ed226SJulian Elischer 89878ed226SJulian Elischer switch (argc) { 90878ed226SJulian Elischer case 1: 91878ed226SJulian Elischer /* connection handle */ 92878ed226SJulian Elischer if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 93878ed226SJulian Elischer return (USAGE); 94878ed226SJulian Elischer 95878ed226SJulian Elischer cp.con_handle = (u_int16_t) (n & 0x0fff); 96878ed226SJulian Elischer cp.con_handle = htole16(cp.con_handle); 97878ed226SJulian Elischer break; 98878ed226SJulian Elischer 99878ed226SJulian Elischer default: 100878ed226SJulian Elischer return (USAGE); 101878ed226SJulian Elischer } 102878ed226SJulian Elischer 103878ed226SJulian Elischer /* send command */ 104878ed226SJulian Elischer n = sizeof(rp); 105878ed226SJulian Elischer if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS, 106878ed226SJulian Elischer NG_HCI_OCF_RESET_FAILED_CONTACT_CNTR), 107878ed226SJulian Elischer (char const *) &cp, sizeof(cp), 108878ed226SJulian Elischer (char *) &rp, &n) == ERROR) 109878ed226SJulian Elischer return (ERROR); 110878ed226SJulian Elischer 111878ed226SJulian Elischer if (rp.status != 0x00) { 112878ed226SJulian Elischer fprintf(stdout, "Status: %s [%#02x]\n", 113878ed226SJulian Elischer hci_status2str(rp.status), rp.status); 114878ed226SJulian Elischer return (FAILED); 115878ed226SJulian Elischer } 116878ed226SJulian Elischer 117878ed226SJulian Elischer return (OK); 118878ed226SJulian Elischer } /* hci_reset_failed_contact_counter */ 119878ed226SJulian Elischer 120878ed226SJulian Elischer /* Sent Get_Link_Quality command to the unit */ 121878ed226SJulian Elischer static int 122878ed226SJulian Elischer hci_get_link_quality(int s, int argc, char **argv) 123878ed226SJulian Elischer { 124878ed226SJulian Elischer ng_hci_get_link_quality_cp cp; 125878ed226SJulian Elischer ng_hci_get_link_quality_rp rp; 126878ed226SJulian Elischer int n; 127878ed226SJulian Elischer 128878ed226SJulian Elischer switch (argc) { 129878ed226SJulian Elischer case 1: 130878ed226SJulian Elischer /* connection handle */ 131878ed226SJulian Elischer if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 132878ed226SJulian Elischer return (USAGE); 133878ed226SJulian Elischer 134878ed226SJulian Elischer cp.con_handle = (u_int16_t) (n & 0x0fff); 135878ed226SJulian Elischer cp.con_handle = htole16(cp.con_handle); 136878ed226SJulian Elischer break; 137878ed226SJulian Elischer 138878ed226SJulian Elischer default: 139878ed226SJulian Elischer return (USAGE); 140878ed226SJulian Elischer } 141878ed226SJulian Elischer 142878ed226SJulian Elischer /* send command */ 143878ed226SJulian Elischer n = sizeof(rp); 144878ed226SJulian Elischer if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS, 145878ed226SJulian Elischer NG_HCI_OCF_GET_LINK_QUALITY), 146878ed226SJulian Elischer (char const *) &cp, sizeof(cp), 147878ed226SJulian Elischer (char *) &rp, &n) == ERROR) 148878ed226SJulian Elischer return (ERROR); 149878ed226SJulian Elischer 150878ed226SJulian Elischer if (rp.status != 0x00) { 151878ed226SJulian Elischer fprintf(stdout, "Status: %s [%#02x]\n", 152878ed226SJulian Elischer hci_status2str(rp.status), rp.status); 153878ed226SJulian Elischer return (FAILED); 154878ed226SJulian Elischer } 155878ed226SJulian Elischer 156878ed226SJulian Elischer fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle)); 157878ed226SJulian Elischer fprintf(stdout, "Link quality: %d\n", le16toh(rp.quality)); 158878ed226SJulian Elischer 159878ed226SJulian Elischer return (OK); 160878ed226SJulian Elischer } /* hci_get_link_quality */ 161878ed226SJulian Elischer 162878ed226SJulian Elischer /* Send Read_RSSI command to the unit */ 163878ed226SJulian Elischer static int 164878ed226SJulian Elischer hci_read_rssi(int s, int argc, char **argv) 165878ed226SJulian Elischer { 166878ed226SJulian Elischer ng_hci_read_rssi_cp cp; 167878ed226SJulian Elischer ng_hci_read_rssi_rp rp; 168878ed226SJulian Elischer int n; 169878ed226SJulian Elischer 170878ed226SJulian Elischer switch (argc) { 171878ed226SJulian Elischer case 1: 172878ed226SJulian Elischer /* connection handle */ 173878ed226SJulian Elischer if (sscanf(argv[0], "%d", &n) != 1 || n <= 0 || n > 0x0eff) 174878ed226SJulian Elischer return (USAGE); 175878ed226SJulian Elischer 176878ed226SJulian Elischer cp.con_handle = (u_int16_t) (n & 0x0fff); 177878ed226SJulian Elischer cp.con_handle = htole16(cp.con_handle); 178878ed226SJulian Elischer break; 179878ed226SJulian Elischer 180878ed226SJulian Elischer default: 181878ed226SJulian Elischer return (USAGE); 182878ed226SJulian Elischer } 183878ed226SJulian Elischer 184878ed226SJulian Elischer /* send command */ 185878ed226SJulian Elischer n = sizeof(rp); 186878ed226SJulian Elischer if (hci_request(s, NG_HCI_OPCODE(NG_HCI_OGF_STATUS, 187878ed226SJulian Elischer NG_HCI_OCF_READ_RSSI), 188878ed226SJulian Elischer (char const *) &cp, sizeof(cp), 189878ed226SJulian Elischer (char *) &rp, &n) == ERROR) 190878ed226SJulian Elischer return (ERROR); 191878ed226SJulian Elischer 192878ed226SJulian Elischer if (rp.status != 0x00) { 193878ed226SJulian Elischer fprintf(stdout, "Status: %s [%#02x]\n", 194878ed226SJulian Elischer hci_status2str(rp.status), rp.status); 195878ed226SJulian Elischer return (FAILED); 196878ed226SJulian Elischer } 197878ed226SJulian Elischer 198878ed226SJulian Elischer fprintf(stdout, "Connection handle: %d\n", le16toh(rp.con_handle)); 199878ed226SJulian Elischer fprintf(stdout, "RSSI: %d dB\n", (int) rp.rssi); 200878ed226SJulian Elischer 201878ed226SJulian Elischer return (OK); 202878ed226SJulian Elischer } /* hci_read_rssi */ 203878ed226SJulian Elischer 204878ed226SJulian Elischer struct hci_command status_commands[] = { 205878ed226SJulian Elischer { 206878ed226SJulian Elischer "read_failed_contact_counter <connection_handle>", 207878ed226SJulian Elischer "\nThis command will read the value for the Failed_Contact_Counter\n" \ 208878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \ 209878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n", 210878ed226SJulian Elischer &hci_read_failed_contact_counter 211878ed226SJulian Elischer }, 212878ed226SJulian Elischer { 213878ed226SJulian Elischer "reset_failed_contact_counter <connection_handle>", 214878ed226SJulian Elischer "\nThis command will reset the value for the Failed_Contact_Counter\n" \ 215878ed226SJulian Elischer "parameter for a particular ACL connection to another device.\n\n" \ 216878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n", 217878ed226SJulian Elischer &hci_reset_failed_contact_counter 218878ed226SJulian Elischer }, 219878ed226SJulian Elischer { 220878ed226SJulian Elischer "get_link_quality <connection_handle>", 221878ed226SJulian Elischer "\nThis command will return the value for the Link_Quality for the\n" \ 222878ed226SJulian Elischer "specified ACL connection handle. This command will return a Link_Quality\n" \ 223878ed226SJulian Elischer "value from 0-255, which represents the quality of the link between two\n" \ 224878ed226SJulian Elischer "Bluetooth devices. The higher the value, the better the link quality is.\n" \ 225878ed226SJulian Elischer "Each Bluetooth module vendor will determine how to measure the link quality." \ 226878ed226SJulian Elischer "\n\n" \ 227878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n", 228878ed226SJulian Elischer &hci_get_link_quality 229878ed226SJulian Elischer }, 230878ed226SJulian Elischer { 231878ed226SJulian Elischer "read_rssi <connection_handle>", 232878ed226SJulian Elischer "\nThis command will read the value for the difference between the\n" \ 233878ed226SJulian Elischer "measured Received Signal Strength Indication (RSSI) and the limits of\n" \ 2341a63eb31SJulian Elischer "the Golden Receive Power Range for a ACL connection handle to another\n" \ 235878ed226SJulian Elischer "Bluetooth device. Any positive RSSI value returned by the Host Controller\n" \ 236878ed226SJulian Elischer "indicates how many dB the RSSI is above the upper limit, any negative\n" \ 237878ed226SJulian Elischer "value indicates how many dB the RSSI is below the lower limit. The value\n" \ 238878ed226SJulian Elischer "zero indicates that the RSSI is inside the Golden Receive Power Range.\n\n" \ 239878ed226SJulian Elischer "\t<connection_handle> - dddd; ACL connection handle\n", 240878ed226SJulian Elischer &hci_read_rssi 241878ed226SJulian Elischer }, 242878ed226SJulian Elischer { 243878ed226SJulian Elischer NULL, 244878ed226SJulian Elischer }}; 245878ed226SJulian Elischer 246