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