Home | History | Annotate | Line # | Download | only in btdevctl
print.c revision 1.10
      1  1.10  plunky /*	$NetBSD: print.c,v 1.10 2010/04/28 06:18:07 plunky 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.10  plunky __RCSID("$NetBSD: print.c,v 1.10 2010/04/28 06:18:07 plunky 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.10  plunky 	uint16_t v;
     90   1.1  plunky 
     91   1.1  plunky 	obj = prop_dictionary_get(dict, BTDEVladdr);
     92   1.1  plunky 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
     93   1.1  plunky 		return;
     94   1.1  plunky 	}
     95   1.1  plunky 	printf("local bdaddr: %s\n", bt_ntoa(prop_data_data_nocopy(obj), NULL));
     96   1.1  plunky 
     97   1.1  plunky 	obj = prop_dictionary_get(dict, BTDEVraddr);
     98   1.1  plunky 	if (prop_object_type(obj) != PROP_TYPE_DATA) {
     99   1.1  plunky 		return;
    100   1.1  plunky 	}
    101   1.1  plunky 	printf("remote bdaddr: %s\n", bt_ntoa(prop_data_data_nocopy(obj), NULL));
    102   1.1  plunky 
    103   1.8  plunky 	obj = prop_dictionary_get(dict, BTDEVmode);
    104   1.8  plunky 	if (prop_object_type(obj) == PROP_TYPE_STRING)
    105   1.8  plunky 		printf("link mode: %s\n", prop_string_cstring_nocopy(obj));
    106   1.8  plunky 
    107  1.10  plunky 	if (prop_dictionary_get_uint16(dict, BTDEVvendor, &v))
    108  1.10  plunky 		printf("vendor id: 0x%04x\n", v);
    109  1.10  plunky 
    110  1.10  plunky 	if (prop_dictionary_get_uint16(dict, BTDEVproduct, &v))
    111  1.10  plunky 		printf("product id: 0x%04x\n", v);
    112  1.10  plunky 
    113   1.1  plunky 	obj = prop_dictionary_get(dict, BTDEVtype);
    114   1.1  plunky 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
    115   1.1  plunky 		printf("No device type!\n");
    116   1.1  plunky 		return;
    117   1.1  plunky 	}
    118   1.7  plunky 	printf("device type: %s\n", prop_string_cstring_nocopy(obj));
    119   1.1  plunky 
    120   1.1  plunky 	if (prop_string_equals_cstring(obj, "bthidev")) {
    121   1.1  plunky 		cfg_bthidev(dict);
    122   1.1  plunky 		return;
    123   1.1  plunky 	}
    124   1.1  plunky 
    125   1.1  plunky 	if (prop_string_equals_cstring(obj, "btsco")) {
    126   1.1  plunky 		cfg_btsco(dict);
    127   1.1  plunky 		return;
    128   1.1  plunky 	}
    129   1.1  plunky 
    130   1.1  plunky 	printf("Unknown device type!\n");
    131   1.1  plunky }
    132   1.1  plunky 
    133   1.1  plunky static void
    134   1.1  plunky cfg_bthidev(prop_dictionary_t dict)
    135   1.1  plunky {
    136   1.1  plunky 	prop_object_t obj;
    137   1.1  plunky 
    138   1.1  plunky 	obj = prop_dictionary_get(dict, BTHIDEVcontrolpsm);
    139   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
    140   1.2      he 		printf("control psm: 0x%4.4" PRIx64 "\n",
    141   1.2      he 			prop_number_integer_value(obj));
    142   1.1  plunky 
    143   1.1  plunky 	obj = prop_dictionary_get(dict, BTHIDEVinterruptpsm);
    144   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
    145   1.2      he 		printf("interrupt psm: 0x%4.4" PRIx64 "\n",
    146   1.2      he 			prop_number_integer_value(obj));
    147   1.1  plunky 
    148   1.1  plunky 	obj = prop_dictionary_get(dict, BTHIDEVreconnect);
    149   1.1  plunky 	if (prop_bool_true(obj))
    150   1.9   pavel 		printf("reconnect mode: true\n");
    151   1.1  plunky 
    152   1.1  plunky 	obj = prop_dictionary_get(dict, BTHIDEVdescriptor);
    153   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_DATA)
    154   1.1  plunky 		hid_parse(obj);
    155   1.1  plunky }
    156   1.1  plunky 
    157   1.1  plunky static void
    158   1.1  plunky cfg_btsco(prop_dictionary_t dict)
    159   1.1  plunky {
    160   1.1  plunky 	prop_object_t obj;
    161   1.1  plunky 
    162   1.1  plunky 	obj = prop_dictionary_get(dict, BTSCOlisten);
    163   1.1  plunky 	printf("mode: %s\n", prop_bool_true(obj) ? "listen" : "connect");
    164   1.1  plunky 
    165   1.1  plunky 	obj = prop_dictionary_get(dict, BTSCOchannel);
    166   1.1  plunky 	if (prop_object_type(obj) == PROP_TYPE_NUMBER)
    167   1.2      he 		printf("channel: %" PRId64 "\n",
    168   1.2      he 			prop_number_integer_value(obj));
    169   1.1  plunky }
    170   1.1  plunky 
    171   1.1  plunky static void
    172   1.1  plunky hid_parse(prop_data_t desc)
    173   1.1  plunky {
    174   1.1  plunky 	report_desc_t		 r;
    175   1.1  plunky 	hid_data_t		 d;
    176   1.1  plunky 	struct hid_item		 h;
    177   1.1  plunky 
    178   1.1  plunky 	hid_init(NULL);
    179   1.1  plunky 
    180   1.8  plunky 	r = hid_use_report_desc(prop_data_data_nocopy(desc),
    181   1.1  plunky 				prop_data_size(desc));
    182   1.1  plunky 	if (r == NULL)
    183   1.1  plunky 		return;
    184   1.1  plunky 
    185   1.1  plunky 	d = hid_start_parse(r, ~0, -1);
    186   1.1  plunky 	while (hid_get_item(d, &h)) {
    187   1.1  plunky 		switch (h.kind) {
    188   1.1  plunky 		case hid_collection:
    189   1.1  plunky 			printf("Collection page=%s usage=%s\n",
    190   1.1  plunky 				hid_usage_page(HID_PAGE(h.usage)),
    191   1.1  plunky 				hid_usage_in_page(h.usage));
    192   1.1  plunky 			break;
    193   1.1  plunky 
    194   1.1  plunky 		case hid_endcollection:
    195   1.1  plunky 			printf("End collection\n");
    196   1.1  plunky 			break;
    197   1.1  plunky 
    198   1.1  plunky 		case hid_input:
    199   1.1  plunky 			hid_dump_item("  Input", &h);
    200   1.1  plunky 			break;
    201   1.1  plunky 
    202   1.1  plunky 		case hid_output:
    203   1.1  plunky 			hid_dump_item(" Output", &h);
    204   1.1  plunky 			break;
    205   1.1  plunky 
    206   1.1  plunky 		case hid_feature:
    207   1.1  plunky 			hid_dump_item("Feature", &h);
    208   1.1  plunky 			break;
    209   1.1  plunky 		}
    210   1.1  plunky 	}
    211   1.1  plunky 
    212   1.1  plunky 	hid_end_parse(d);
    213   1.1  plunky 	hid_dispose_report_desc(r);
    214   1.1  plunky }
    215   1.1  plunky 
    216   1.1  plunky static void
    217   1.1  plunky hid_dump_item(char const *label, struct hid_item *h)
    218   1.1  plunky {
    219   1.1  plunky 
    220   1.1  plunky 	printf("%s id=%u size=%u count=%u page=%s usage=%s%s%s%s%s%s%s%s%s%s",
    221   1.1  plunky 		label, (uint8_t) h->report_ID, h->report_size, h->report_count,
    222   1.1  plunky 		hid_usage_page(HID_PAGE(h->usage)),
    223   1.1  plunky 		hid_usage_in_page(h->usage),
    224   1.1  plunky 		(h->flags & HIO_CONST) ? " Const" : "",
    225   1.1  plunky 		(h->flags & HIO_VARIABLE) ? " Variable" : "",
    226   1.1  plunky 		(h->flags & HIO_RELATIVE) ? " Relative" : "",
    227   1.1  plunky 		(h->flags & HIO_WRAP) ? " Wrap" : "",
    228   1.1  plunky 		(h->flags & HIO_NONLINEAR) ? " NonLinear" : "",
    229   1.1  plunky 		(h->flags & HIO_NOPREF) ? " NoPref" : "",
    230   1.1  plunky 		(h->flags & HIO_NULLSTATE) ? " NullState" : "",
    231   1.1  plunky 		(h->flags & HIO_VOLATILE) ? " Volatile" : "",
    232   1.1  plunky 		(h->flags & HIO_BUFBYTES) ? " BufBytes" : "");
    233   1.1  plunky 
    234   1.1  plunky 	printf(", logical range %d..%d",
    235   1.1  plunky 		h->logical_minimum, h->logical_maximum);
    236   1.1  plunky 
    237   1.1  plunky 	if (h->physical_minimum != h->physical_maximum)
    238   1.1  plunky 		printf(", physical range %d..%d",
    239   1.1  plunky 			h->physical_minimum, h->physical_maximum);
    240   1.1  plunky 
    241   1.1  plunky 	if (h->unit)
    242   1.1  plunky 		printf(", unit=0x%02x exp=%d", h->unit, h->unit_exponent);
    243   1.1  plunky 
    244   1.1  plunky 	printf("\n");
    245   1.1  plunky }
    246