ucmb: Add msg-delay ioctls; Move device registration out of the driver; Add open...
[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 #define UCMB_IOCTL_GMSGDELAY _IOR(__UCMB_IOCTL, 1, unsigned int)
33 #define UCMB_IOCTL_SMSGDELAY _IOW(__UCMB_IOCTL, 2, unsigned int)
34
35
36 static void usage(int argc, char **argv)
37 {
38 fprintf(stderr, "Usage: %s read|write|reset [" UCMB_DEV "]\n", argv[0]);
39 }
40
41 int main(int argc, char **argv)
42 {
43 const char *command, *devpath = UCMB_DEV;
44 int res, errcode = 0;
45 int ucmb_fd;
46 char *buf;
47 size_t count, buflen;
48 ssize_t nrbytes;
49
50 if (argc != 2 && argc != 3) {
51 usage(argc, argv);
52 return 1;
53 }
54 if (argc == 3)
55 devpath = argv[2];
56 command = argv[1];
57
58 ucmb_fd = open(devpath, O_RDWR);
59 if (ucmb_fd == -1) {
60 fprintf(stderr, "Failed to open %s\n", UCMB_DEV);
61 errcode = 1;
62 goto out;
63 }
64
65 buflen = 4096;
66 buf = malloc(buflen);
67 if (!buf) {
68 fprintf(stderr, "Out of memory\n");
69 errcode = 1;
70 goto out_close;
71 }
72
73 if (strcasecmp(command, "read") == 0) {
74 nrbytes = read(ucmb_fd, buf, buflen);
75 if (nrbytes < 0) {
76 fprintf(stderr, "Failed to read UCMB: %s (%d)\n",
77 strerror(errno), errno);
78 errcode = 1;
79 goto out_free;
80 }
81 if (fwrite(buf, nrbytes, 1, stdout) != 1) {
82 fprintf(stderr, "Failed to write stdout\n");
83 errcode = 1;
84 goto out_free;
85 }
86 } else if (strcasecmp(command, "write") == 0) {
87 count = fread(buf, 1, buflen, stdin);
88 if (!count) {
89 fprintf(stderr, "Failed to read stdin\n");
90 errcode = 1;
91 goto out_free;
92 }
93 nrbytes = write(ucmb_fd, buf, count);
94 if (nrbytes != count) {
95 fprintf(stderr, "Failed to write UCMB: %s (%d)\n",
96 strerror(errno), errno);
97 errcode = 1;
98 goto out_free;
99 }
100 } else if (strcasecmp(command, "reset") == 0) {
101 res = ioctl(ucmb_fd, UCMB_IOCTL_RESETUC);
102 if (res) {
103 fprintf(stderr, "RESET ioctl failed: %s (%d)\n",
104 strerror(res < 0 ? -res : res), res);
105 errcode = 1;
106 goto out_free;
107 }
108 } else {
109 usage(argc, argv);
110 errcode = 1;
111 goto out_free;
112 }
113
114 out_free:
115 free(buf);
116 out_close:
117 close(ucmb_fd);
118 out:
119 return errcode;
120 }