Home | History | Annotate | Line # | Download | only in btdevctl
btdevctl.c revision 1.1
      1  1.1  plunky /*	$NetBSD: btdevctl.c,v 1.1 2006/08/13 09:03:23 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 #include <sys/cdefs.h>
     35  1.1  plunky __COPYRIGHT("@(#) Copyright (c) 2006 Itronix, Inc.\n"
     36  1.1  plunky 	    "All rights reserved.\n");
     37  1.1  plunky __RCSID("$NetBSD: btdevctl.c,v 1.1 2006/08/13 09:03:23 plunky Exp $");
     38  1.1  plunky 
     39  1.1  plunky #include <prop/proplib.h>
     40  1.1  plunky 
     41  1.1  plunky #include <err.h>
     42  1.1  plunky #include <fcntl.h>
     43  1.1  plunky #include <stdio.h>
     44  1.1  plunky #include <stdlib.h>
     45  1.1  plunky #include <string.h>
     46  1.1  plunky #include <unistd.h>
     47  1.1  plunky 
     48  1.1  plunky #include "btdevctl.h"
     49  1.1  plunky 
     50  1.1  plunky const char *config_file = "/var/db/btdev.xml";
     51  1.1  plunky const char *control_file = NULL;
     52  1.1  plunky 
     53  1.1  plunky struct command {
     54  1.1  plunky 	const char	*command;
     55  1.1  plunky 	int		(*handler)(int, char **);
     56  1.1  plunky 	const char	*description;
     57  1.1  plunky } commands[] = {
     58  1.1  plunky 	{ "Attach",	dev_attach,	"attach device"		},
     59  1.1  plunky 	{ "Detach",	dev_detach,	"detach device"		},
     60  1.1  plunky 	{ "Parse",	hid_parse,	"parse HID descriptor"	},
     61  1.1  plunky 	{ "Print",	cfg_print,	"print config entry"	},
     62  1.1  plunky 	{ "Query",	cfg_query,	"make config entry"	},
     63  1.1  plunky 	{ "Remove",	cfg_remove,	"remove config entry"	},
     64  1.1  plunky 	{ NULL }
     65  1.1  plunky };
     66  1.1  plunky 
     67  1.1  plunky static void
     68  1.1  plunky usage(void)
     69  1.1  plunky {
     70  1.1  plunky 	struct command *cmd;
     71  1.1  plunky 
     72  1.1  plunky 	fprintf(stderr, "Usage: %s <btdev> <command> [parameters]\n"
     73  1.1  plunky 			"Commands:\n",
     74  1.1  plunky 			getprogname());
     75  1.1  plunky 
     76  1.1  plunky 	for (cmd = commands ; cmd->command != NULL ; cmd++)
     77  1.1  plunky 		fprintf(stderr, "\t%-13s%s\n", cmd->command, cmd->description);
     78  1.1  plunky 
     79  1.1  plunky 	exit(EXIT_FAILURE);
     80  1.1  plunky }
     81  1.1  plunky 
     82  1.1  plunky int
     83  1.1  plunky main(int argc, char *argv[])
     84  1.1  plunky {
     85  1.1  plunky 	struct command *cmd;
     86  1.1  plunky 
     87  1.1  plunky 	if (argc < 3)
     88  1.1  plunky 		usage();
     89  1.1  plunky 
     90  1.1  plunky 	control_file = argv[1];
     91  1.1  plunky 
     92  1.1  plunky 	for (cmd = commands ; cmd->command != NULL ; cmd++) {
     93  1.1  plunky 		if (strcasecmp(argv[2], cmd->command) == 0)
     94  1.1  plunky 			return (cmd->handler)(argc - 2, argv + 2);
     95  1.1  plunky 	}
     96  1.1  plunky 
     97  1.1  plunky 	errx(EXIT_FAILURE, "%s: unknown command", argv[2]);
     98  1.1  plunky }
     99  1.1  plunky 
    100  1.1  plunky prop_dictionary_t
    101  1.1  plunky read_config(void)
    102  1.1  plunky {
    103  1.1  plunky 	prop_dictionary_t dict;
    104  1.1  plunky 	char *xml;
    105  1.1  plunky 	off_t len;
    106  1.1  plunky 	int fd;
    107  1.1  plunky 
    108  1.1  plunky 	fd = open(config_file, O_RDONLY, 0);
    109  1.1  plunky 	if (fd < 0)
    110  1.1  plunky 		return NULL;
    111  1.1  plunky 
    112  1.1  plunky 	len = lseek(fd, 0, SEEK_END);
    113  1.1  plunky 	if (len == 0) {
    114  1.1  plunky 		close(fd);
    115  1.1  plunky 		return NULL;
    116  1.1  plunky 	}
    117  1.1  plunky 
    118  1.1  plunky 	xml = malloc(len);
    119  1.1  plunky 	if (xml == NULL) {
    120  1.1  plunky 		close(fd);
    121  1.1  plunky 		return NULL;
    122  1.1  plunky 	}
    123  1.1  plunky 
    124  1.1  plunky 	(void)lseek(fd, 0, SEEK_SET);
    125  1.1  plunky 	if (read(fd, xml, len) != len) {
    126  1.1  plunky 		close(fd);
    127  1.1  plunky 		free(xml);
    128  1.1  plunky 		return NULL;
    129  1.1  plunky 	}
    130  1.1  plunky 
    131  1.1  plunky 	dict = prop_dictionary_internalize(xml);
    132  1.1  plunky 
    133  1.1  plunky 	free(xml);
    134  1.1  plunky 	close(fd);
    135  1.1  plunky 
    136  1.1  plunky 	return dict;
    137  1.1  plunky }
    138  1.1  plunky 
    139  1.1  plunky int
    140  1.1  plunky write_config(prop_dictionary_t dict)
    141  1.1  plunky {
    142  1.1  plunky 	char *xml;
    143  1.1  plunky 	size_t len;
    144  1.1  plunky 	int fd;
    145  1.1  plunky 
    146  1.1  plunky 	fd = open(config_file, O_WRONLY | O_CREAT | O_TRUNC, 0640);
    147  1.1  plunky 	if (fd < 0)
    148  1.1  plunky 		return 0;
    149  1.1  plunky 
    150  1.1  plunky 	xml = prop_dictionary_externalize(dict);
    151  1.1  plunky 	if (xml == NULL) {
    152  1.1  plunky 		close(fd);
    153  1.1  plunky 		return 0;
    154  1.1  plunky 	}
    155  1.1  plunky 
    156  1.1  plunky 	len = strlen(xml);
    157  1.1  plunky 	if (write(fd, xml, len) != len) {
    158  1.1  plunky 		free(xml);
    159  1.1  plunky 		close(fd);
    160  1.1  plunky 		return 0;
    161  1.1  plunky 	}
    162  1.1  plunky 
    163  1.1  plunky 	free(xml);
    164  1.1  plunky 	close(fd);
    165  1.1  plunky 
    166  1.1  plunky 	return 1;
    167  1.1  plunky }
    168