xref: /netbsd-src/usr.sbin/btdevctl/print.c (revision bcd1eb151dd460c33b0aaeef288c719648c890e3)
1*bcd1eb15Sthorpej /*	$NetBSD: print.c,v 1.12 2020/06/07 00:12:00 thorpej Exp $	*/
24f1cbddcSplunky 
34f1cbddcSplunky /*-
44f1cbddcSplunky  * Copyright (c) 2006 Itronix Inc.
54f1cbddcSplunky  * All rights reserved.
64f1cbddcSplunky  *
74f1cbddcSplunky  * Written by Iain Hibbert for Itronix Inc.
84f1cbddcSplunky  *
94f1cbddcSplunky  * Redistribution and use in source and binary forms, with or without
104f1cbddcSplunky  * modification, are permitted provided that the following conditions
114f1cbddcSplunky  * are met:
124f1cbddcSplunky  * 1. Redistributions of source code must retain the above copyright
134f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer.
144f1cbddcSplunky  * 2. Redistributions in binary form must reproduce the above copyright
154f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer in the
164f1cbddcSplunky  *    documentation and/or other materials provided with the distribution.
174f1cbddcSplunky  * 3. The name of Itronix Inc. may not be used to endorse
184f1cbddcSplunky  *    or promote products derived from this software without specific
194f1cbddcSplunky  *    prior written permission.
204f1cbddcSplunky  *
214f1cbddcSplunky  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
224f1cbddcSplunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
234f1cbddcSplunky  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
244f1cbddcSplunky  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
254f1cbddcSplunky  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
264f1cbddcSplunky  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
274f1cbddcSplunky  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
284f1cbddcSplunky  * ON ANY THEORY OF LIABILITY, WHETHER IN
294f1cbddcSplunky  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
304f1cbddcSplunky  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
314f1cbddcSplunky  * POSSIBILITY OF SUCH DAMAGE.
324f1cbddcSplunky  */
334f1cbddcSplunky /*
344f1cbddcSplunky  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
354f1cbddcSplunky  * All rights reserved.
364f1cbddcSplunky  *
374f1cbddcSplunky  * Redistribution and use in source and binary forms, with or without
384f1cbddcSplunky  * modification, are permitted provided that the following conditions
394f1cbddcSplunky  * are met:
404f1cbddcSplunky  * 1. Redistributions of source code must retain the above copyright
414f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer.
424f1cbddcSplunky  * 2. Redistributions in binary form must reproduce the above copyright
434f1cbddcSplunky  *    notice, this list of conditions and the following disclaimer in the
444f1cbddcSplunky  *    documentation and/or other materials provided with the distribution.
454f1cbddcSplunky  *
464f1cbddcSplunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
474f1cbddcSplunky  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
484f1cbddcSplunky  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
494f1cbddcSplunky  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
504f1cbddcSplunky  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
514f1cbddcSplunky  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
524f1cbddcSplunky  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
534f1cbddcSplunky  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
544f1cbddcSplunky  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
554f1cbddcSplunky  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
564f1cbddcSplunky  * SUCH DAMAGE.
574f1cbddcSplunky  *
584f1cbddcSplunky  */
594f1cbddcSplunky 
604f1cbddcSplunky #include <sys/cdefs.h>
61*bcd1eb15Sthorpej __RCSID("$NetBSD: print.c,v 1.12 2020/06/07 00:12:00 thorpej Exp $");
624f1cbddcSplunky 
634f1cbddcSplunky #include <sys/types.h>
644f1cbddcSplunky 
654f1cbddcSplunky #include <dev/bluetooth/btdev.h>
664f1cbddcSplunky #include <dev/bluetooth/bthidev.h>
674f1cbddcSplunky #include <dev/bluetooth/btsco.h>
684f1cbddcSplunky #include <dev/usb/usb.h>
69878cb1cfSbouyer #include <dev/hid/hid.h>
704f1cbddcSplunky 
714f1cbddcSplunky #include <prop/proplib.h>
724f1cbddcSplunky 
734f1cbddcSplunky #include <bluetooth.h>
744f1cbddcSplunky #include <err.h>
754f1cbddcSplunky #include <usbhid.h>
764f1cbddcSplunky 
774f1cbddcSplunky #include "btdevctl.h"
784f1cbddcSplunky 
794f1cbddcSplunky static void cfg_bthidev(prop_dictionary_t dict);
804f1cbddcSplunky static void cfg_btsco(prop_dictionary_t dict);
814f1cbddcSplunky 
824f1cbddcSplunky static void hid_dump_item(char const *, struct hid_item *);
834f1cbddcSplunky static void hid_parse(prop_data_t);
844f1cbddcSplunky 
854f1cbddcSplunky void
cfg_print(prop_dictionary_t dict)864f1cbddcSplunky cfg_print(prop_dictionary_t dict)
874f1cbddcSplunky {
884f1cbddcSplunky 	prop_object_t obj;
89366a13e7Splunky 	uint16_t v;
904f1cbddcSplunky 
914f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTDEVladdr);
924f1cbddcSplunky 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
934f1cbddcSplunky 		return;
944f1cbddcSplunky 	}
95*bcd1eb15Sthorpej 	printf("local bdaddr: %s\n", bt_ntoa(prop_data_value(obj), NULL));
964f1cbddcSplunky 
974f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTDEVraddr);
984f1cbddcSplunky 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
994f1cbddcSplunky 		return;
1004f1cbddcSplunky 	}
101*bcd1eb15Sthorpej 	printf("remote bdaddr: %s\n", bt_ntoa(prop_data_value(obj), NULL));
1024f1cbddcSplunky 
103f5db72e7Splunky 	obj = prop_dictionary_get(dict, BTDEVmode);
104f5db72e7Splunky 	if (prop_object_type(obj) == PROP_TYPE_STRING)
105*bcd1eb15Sthorpej 		printf("link mode: %s\n", prop_string_value(obj));
106f5db72e7Splunky 
107366a13e7Splunky 	if (prop_dictionary_get_uint16(dict, BTDEVvendor, &v))
108366a13e7Splunky 		printf("vendor id: 0x%04x\n", v);
109366a13e7Splunky 
110366a13e7Splunky 	if (prop_dictionary_get_uint16(dict, BTDEVproduct, &v))
111366a13e7Splunky 		printf("product id: 0x%04x\n", v);
112366a13e7Splunky 
1134f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTDEVtype);
1144f1cbddcSplunky 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
1154f1cbddcSplunky 		printf("No device type!\n");
1164f1cbddcSplunky 		return;
1174f1cbddcSplunky 	}
118*bcd1eb15Sthorpej 	printf("device type: %s\n", prop_string_value(obj));
1194f1cbddcSplunky 
120*bcd1eb15Sthorpej 	if (prop_string_equals_string(obj, "bthidev")) {
1214f1cbddcSplunky 		cfg_bthidev(dict);
1224f1cbddcSplunky 		return;
1234f1cbddcSplunky 	}
1244f1cbddcSplunky 
125*bcd1eb15Sthorpej 	if (prop_string_equals_string(obj, "btsco")) {
1264f1cbddcSplunky 		cfg_btsco(dict);
1274f1cbddcSplunky 		return;
1284f1cbddcSplunky 	}
1294f1cbddcSplunky 
1304f1cbddcSplunky 	printf("Unknown device type!\n");
1314f1cbddcSplunky }
1324f1cbddcSplunky 
1334f1cbddcSplunky static void
cfg_bthidev(prop_dictionary_t dict)1344f1cbddcSplunky cfg_bthidev(prop_dictionary_t dict)
1354f1cbddcSplunky {
1364f1cbddcSplunky 	prop_object_t obj;
1374f1cbddcSplunky 
1384f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
1394f1cbddcSplunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
140d4831ce4She 		printf("control psm: 0x%4.4" PRIx64 "\n",
141*bcd1eb15Sthorpej 			prop_number_signed_value(obj));
1424f1cbddcSplunky 
1434f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
1444f1cbddcSplunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
145d4831ce4She 		printf("interrupt psm: 0x%4.4" PRIx64 "\n",
146*bcd1eb15Sthorpej 			prop_number_signed_value(obj));
1474f1cbddcSplunky 
1484f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
1494f1cbddcSplunky 	if (prop_bool_true(obj))
1505e809e89Spavel 		printf("reconnect mode: true\n");
1514f1cbddcSplunky 
1524f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
1534f1cbddcSplunky 	if (prop_object_type(obj) == PROP_TYPE_DATA)
1544f1cbddcSplunky 		hid_parse(obj);
1554f1cbddcSplunky }
1564f1cbddcSplunky 
1574f1cbddcSplunky static void
cfg_btsco(prop_dictionary_t dict)1584f1cbddcSplunky cfg_btsco(prop_dictionary_t dict)
1594f1cbddcSplunky {
1604f1cbddcSplunky 	prop_object_t obj;
1614f1cbddcSplunky 
1624f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTSCOlisten);
1634f1cbddcSplunky 	printf("mode: %s\n", prop_bool_true(obj) ? "listen" : "connect");
1644f1cbddcSplunky 
1654f1cbddcSplunky 	obj = prop_dictionary_get(dict, BTSCOchannel);
1664f1cbddcSplunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
167d4831ce4She 		printf("channel: %" PRId64 "\n",
168*bcd1eb15Sthorpej 			prop_number_signed_value(obj));
1694f1cbddcSplunky }
1704f1cbddcSplunky 
1714f1cbddcSplunky static void
hid_parse(prop_data_t desc)1724f1cbddcSplunky hid_parse(prop_data_t desc)
1734f1cbddcSplunky {
1744f1cbddcSplunky 	report_desc_t		 r;
1754f1cbddcSplunky 	hid_data_t		 d;
1764f1cbddcSplunky 	struct hid_item		 h;
1774f1cbddcSplunky 
1784f1cbddcSplunky 	hid_init(NULL);
1794f1cbddcSplunky 
180*bcd1eb15Sthorpej 	r = hid_use_report_desc(prop_data_value(desc),
1814f1cbddcSplunky 				prop_data_size(desc));
1824f1cbddcSplunky 	if (r == NULL)
1834f1cbddcSplunky 		return;
1844f1cbddcSplunky 
1854f1cbddcSplunky 	d = hid_start_parse(r, ~0, -1);
1864f1cbddcSplunky 	while (hid_get_item(d, &h)) {
1874f1cbddcSplunky 		switch (h.kind) {
1884f1cbddcSplunky 		case hid_collection:
1894f1cbddcSplunky 			printf("Collection page=%s usage=%s\n",
1904f1cbddcSplunky 				hid_usage_page(HID_PAGE(h.usage)),
1914f1cbddcSplunky 				hid_usage_in_page(h.usage));
1924f1cbddcSplunky 			break;
1934f1cbddcSplunky 
1944f1cbddcSplunky 		case hid_endcollection:
1954f1cbddcSplunky 			printf("End collection\n");
1964f1cbddcSplunky 			break;
1974f1cbddcSplunky 
1984f1cbddcSplunky 		case hid_input:
1994f1cbddcSplunky 			hid_dump_item("  Input", &h);
2004f1cbddcSplunky 			break;
2014f1cbddcSplunky 
2024f1cbddcSplunky 		case hid_output:
2034f1cbddcSplunky 			hid_dump_item(" Output", &h);
2044f1cbddcSplunky 			break;
2054f1cbddcSplunky 
2064f1cbddcSplunky 		case hid_feature:
2074f1cbddcSplunky 			hid_dump_item("Feature", &h);
2084f1cbddcSplunky 			break;
2094f1cbddcSplunky 		}
2104f1cbddcSplunky 	}
2114f1cbddcSplunky 
2124f1cbddcSplunky 	hid_end_parse(d);
2134f1cbddcSplunky 	hid_dispose_report_desc(r);
2144f1cbddcSplunky }
2154f1cbddcSplunky 
2164f1cbddcSplunky static void
hid_dump_item(char const * label,struct hid_item * h)2174f1cbddcSplunky hid_dump_item(char const *label, struct hid_item *h)
2184f1cbddcSplunky {
2194f1cbddcSplunky 
2204f1cbddcSplunky 	printf("%s id=%u size=%u count=%u page=%s usage=%s%s%s%s%s%s%s%s%s%s",
2214f1cbddcSplunky 		label, (uint8_t) h->report_ID, h->report_size, h->report_count,
2224f1cbddcSplunky 		hid_usage_page(HID_PAGE(h->usage)),
2234f1cbddcSplunky 		hid_usage_in_page(h->usage),
2244f1cbddcSplunky 		(h->flags & HIO_CONST) ? " Const" : "",
2254f1cbddcSplunky 		(h->flags & HIO_VARIABLE) ? " Variable" : "",
2264f1cbddcSplunky 		(h->flags & HIO_RELATIVE) ? " Relative" : "",
2274f1cbddcSplunky 		(h->flags & HIO_WRAP) ? " Wrap" : "",
2284f1cbddcSplunky 		(h->flags & HIO_NONLINEAR) ? " NonLinear" : "",
2294f1cbddcSplunky 		(h->flags & HIO_NOPREF) ? " NoPref" : "",
2304f1cbddcSplunky 		(h->flags & HIO_NULLSTATE) ? " NullState" : "",
2314f1cbddcSplunky 		(h->flags & HIO_VOLATILE) ? " Volatile" : "",
2324f1cbddcSplunky 		(h->flags & HIO_BUFBYTES) ? " BufBytes" : "");
2334f1cbddcSplunky 
2344f1cbddcSplunky 	printf(", logical range %d..%d",
2354f1cbddcSplunky 		h->logical_minimum, h->logical_maximum);
2364f1cbddcSplunky 
2374f1cbddcSplunky 	if (h->physical_minimum != h->physical_maximum)
2384f1cbddcSplunky 		printf(", physical range %d..%d",
2394f1cbddcSplunky 			h->physical_minimum, h->physical_maximum);
2404f1cbddcSplunky 
2414f1cbddcSplunky 	if (h->unit)
2424f1cbddcSplunky 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
2434f1cbddcSplunky 
2444f1cbddcSplunky 	printf("\n");
2454f1cbddcSplunky }
246