Update fontconfig to 2.6.0
[openwrt/svn-archive/archive.git] / utils / ucmb-tools / tools / ucmb.c
1 /*
2 * Microcontroller Message Bus
3 * Userspace commandline utility
4 *
5 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/ioctl.h>
25 #include <linux/ioctl.h>
26
27
28 #define UCMB_DEV "/dev/ucmb"
29
30 #define __UCMB_IOCTL ('U'|'C'|'M'|'B')
31 #define UCMB_IOCTL_RESETUC _IO(__UCMB_IOCTL, 0)
32
33
34 static void usage(int argc, char **argv)
35 {
36 fprintf(stderr, "Usage: %s read|write|reset [" UCMB_DEV "]\n", argv[0]);
37 }
38
39 int main(int argc, char **argv)
40 {
41 const char *command, *devpath = UCMB_DEV;
42 int res, errcode = 0;
43 int ucmb_fd;
44 char *buf;
45 size_t count, buflen;
46 ssize_t nrbytes;
47
48 if (argc != 2 && argc != 3) {
49 usage(argc, argv);
50 return 1;
51 }
52 if (argc == 3)
53 devpath = argv[2];
54 command = argv[1];
55
56 ucmb_fd = open(devpath, O_RDWR);
57 if (ucmb_fd == -1) {
58 fprintf(stderr, "Failed to open %s\n", UCMB_DEV);
59 errcode = 1;
60 goto out;
61 }
62
63 buflen = 4096;
64 buf = malloc(buflen);
65 if (!buf) {
66 fprintf(stderr, "Out of memory\n");
67 errcode = 1;
68 goto out_close;
69 }
70
71 if (strcasecmp(command, "read") == 0) {
72 nrbytes = read(ucmb_fd, buf, buflen);
73 if (nrbytes < 0) {
74 fprintf(stderr, "Failed to read UCMB: %s (%d)\n",
75 strerror(errno), errno);
76 errcode = 1;
77 goto out_free;
78 }
79 if (fwrite(buf, nrbytes, 1, stdout) != 1) {
80 fprintf(stderr, "Failed to write stdout\n");
81 errcode = 1;
82 goto out_free;
83 }
84 } else if (strcasecmp(command, "write") == 0) {
85 count = fread(buf, 1, buflen, stdin);
86 if (!count) {
87 fprintf(stderr, "Failed to read stdin\n");
88 errcode = 1;
89 goto out_free;
90 }
91 nrbytes = write(ucmb_fd, buf, count);
92 if (nrbytes != count) {
93 fprintf(stderr, "Failed to write UCMB: %s (%d)\n",
94 strerror(errno), errno);
95 errcode = 1;
96 goto out_free;
97 }
98 } else if (strcasecmp(command, "reset") == 0) {
99 res = ioctl(ucmb_fd, UCMB_IOCTL_RESETUC);
100 if (res) {
101 fprintf(stderr, "RESET ioctl failed: %s (%d)\n",
102 strerror(res < 0 ? -res : res), res);
103 errcode = 1;
104 goto out_free;
105 }
106 } else {
107 usage(argc, argv);
108 errcode = 1;
109 goto out_free;
110 }
111
112 out_free:
113 free(buf);
114 out_close:
115 close(ucmb_fd);
116 out:
117 return errcode;
118 }