xref: /netbsd-src/usr.sbin/btdevctl/print.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: print.c,v 1.6 2006/09/28 09:13:56 he Exp $	*/
2 
3 /*-
4  * Copyright (c) 2006 Itronix Inc.
5  * All rights reserved.
6  *
7  * Written by Iain Hibbert for Itronix Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  * 3. The name of Itronix Inc. may not be used to endorse
18  *    or promote products derived from this software without specific
19  *    prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  * ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*
34  * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com>
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  *
58  */
59 
60 #include <sys/cdefs.h>
61 __RCSID("$NetBSD: print.c,v 1.6 2006/09/28 09:13:56 he Exp $");
62 
63 #include <sys/types.h>
64 
65 #include <dev/bluetooth/btdev.h>
66 #include <dev/bluetooth/bthidev.h>
67 #include <dev/bluetooth/btsco.h>
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usbhid.h>
70 
71 #include <prop/proplib.h>
72 
73 #include <bluetooth.h>
74 #include <err.h>
75 #include <usbhid.h>
76 #include <stdlib.h>
77 
78 #include "btdevctl.h"
79 
80 static void cfg_bthidev(prop_dictionary_t dict);
81 static void cfg_btsco(prop_dictionary_t dict);
82 
83 static void hid_dump_item(char const *, struct hid_item *);
84 static void hid_parse(prop_data_t);
85 
86 void
87 cfg_print(prop_dictionary_t dict)
88 {
89 	prop_object_t obj;
90 	char *p;
91 
92 	obj = prop_dictionary_get(dict, BTDEVladdr);
93 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
94 		return;
95 	}
96 	printf("local bdaddr: %s\n", bt_ntoa(prop_data_data_nocopy(obj), NULL));
97 
98 	obj = prop_dictionary_get(dict, BTDEVraddr);
99 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
100 		return;
101 	}
102 	printf("remote bdaddr: %s\n", bt_ntoa(prop_data_data_nocopy(obj), NULL));
103 
104 	obj = prop_dictionary_get(dict, BTDEVtype);
105 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
106 		printf("No device type!\n");
107 		return;
108 	}
109 	p =  prop_string_cstring(obj);
110 	printf("device type: %s\n", p);
111 	free(p);
112 
113 	if (prop_string_equals_cstring(obj, "bthidev")) {
114 		cfg_bthidev(dict);
115 		return;
116 	}
117 
118 	if (prop_string_equals_cstring(obj, "btsco")) {
119 		cfg_btsco(dict);
120 		return;
121 	}
122 
123 	printf("Unknown device type!\n");
124 }
125 
126 static void
127 cfg_bthidev(prop_dictionary_t dict)
128 {
129 	prop_object_t obj;
130 
131 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
132 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
133 		printf("control psm: 0x%4.4" PRIx64 "\n",
134 			prop_number_integer_value(obj));
135 
136 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
137 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
138 		printf("interrupt psm: 0x%4.4" PRIx64 "\n",
139 			prop_number_integer_value(obj));
140 
141 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
142 	if (prop_bool_true(obj))
143 		printf("reconnect mode: TRUE\n");
144 
145 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
146 	if (prop_object_type(obj) == PROP_TYPE_DATA)
147 		hid_parse(obj);
148 }
149 
150 static void
151 cfg_btsco(prop_dictionary_t dict)
152 {
153 	prop_object_t obj;
154 
155 	obj = prop_dictionary_get(dict, BTSCOlisten);
156 	printf("mode: %s\n", prop_bool_true(obj) ? "listen" : "connect");
157 
158 	obj = prop_dictionary_get(dict, BTSCOchannel);
159 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
160 		printf("channel: %" PRId64 "\n",
161 			prop_number_integer_value(obj));
162 }
163 
164 static void
165 hid_parse(prop_data_t desc)
166 {
167 	report_desc_t		 r;
168 	hid_data_t		 d;
169 	struct hid_item		 h;
170 
171 	hid_init(NULL);
172 
173 	r = hid_use_report_desc((unsigned char *)prop_data_data_nocopy(desc),
174 				prop_data_size(desc));
175 	if (r == NULL)
176 		return;
177 
178 	d = hid_start_parse(r, ~0, -1);
179 	while (hid_get_item(d, &h)) {
180 		switch (h.kind) {
181 		case hid_collection:
182 			printf("Collection page=%s usage=%s\n",
183 				hid_usage_page(HID_PAGE(h.usage)),
184 				hid_usage_in_page(h.usage));
185 			break;
186 
187 		case hid_endcollection:
188 			printf("End collection\n");
189 			break;
190 
191 		case hid_input:
192 			hid_dump_item("  Input", &h);
193 			break;
194 
195 		case hid_output:
196 			hid_dump_item(" Output", &h);
197 			break;
198 
199 		case hid_feature:
200 			hid_dump_item("Feature", &h);
201 			break;
202 		}
203 	}
204 
205 	hid_end_parse(d);
206 	hid_dispose_report_desc(r);
207 }
208 
209 static void
210 hid_dump_item(char const *label, struct hid_item *h)
211 {
212 
213 	printf("%s id=%u size=%u count=%u page=%s usage=%s%s%s%s%s%s%s%s%s%s",
214 		label, (uint8_t) h->report_ID, h->report_size, h->report_count,
215 		hid_usage_page(HID_PAGE(h->usage)),
216 		hid_usage_in_page(h->usage),
217 		(h->flags & HIO_CONST) ? " Const" : "",
218 		(h->flags & HIO_VARIABLE) ? " Variable" : "",
219 		(h->flags & HIO_RELATIVE) ? " Relative" : "",
220 		(h->flags & HIO_WRAP) ? " Wrap" : "",
221 		(h->flags & HIO_NONLINEAR) ? " NonLinear" : "",
222 		(h->flags & HIO_NOPREF) ? " NoPref" : "",
223 		(h->flags & HIO_NULLSTATE) ? " NullState" : "",
224 		(h->flags & HIO_VOLATILE) ? " Volatile" : "",
225 		(h->flags & HIO_BUFBYTES) ? " BufBytes" : "");
226 
227 	printf(", logical range %d..%d",
228 		h->logical_minimum, h->logical_maximum);
229 
230 	if (h->physical_minimum != h->physical_maximum)
231 		printf(", physical range %d..%d",
232 			h->physical_minimum, h->physical_maximum);
233 
234 	if (h->unit)
235 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
236 
237 	printf("\n");
238 }
239