2 * Microcontroller Message Bus
3 * Userspace commandline utility
5 * Copyright (c) 2009 Michael Buesch <mb@bu3sch.de>
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.
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.
24 #include <sys/ioctl.h>
25 #include <linux/ioctl.h>
28 #define UCMB_DEV "/dev/ucmb"
30 #define __UCMB_IOCTL ('U'|'C'|'M'|'B')
31 #define UCMB_IOCTL_RESETUC _IO(__UCMB_IOCTL, 0)
34 static void usage(int argc
, char **argv
)
36 fprintf(stderr
, "Usage: %s read|write|reset [" UCMB_DEV
"]\n", argv
[0]);
39 int main(int argc
, char **argv
)
41 const char *command
, *devpath
= UCMB_DEV
;
48 if (argc
!= 2 && argc
!= 3) {
56 ucmb_fd
= open(devpath
, O_RDWR
);
58 fprintf(stderr
, "Failed to open %s\n", UCMB_DEV
);
66 fprintf(stderr
, "Out of memory\n");
71 if (strcasecmp(command
, "read") == 0) {
72 nrbytes
= read(ucmb_fd
, buf
, buflen
);
74 fprintf(stderr
, "Failed to read UCMB: %s (%d)\n",
75 strerror(errno
), errno
);
79 if (fwrite(buf
, nrbytes
, 1, stdout
) != 1) {
80 fprintf(stderr
, "Failed to write stdout\n");
84 } else if (strcasecmp(command
, "write") == 0) {
85 count
= fread(buf
, 1, buflen
, stdin
);
87 fprintf(stderr
, "Failed to read stdin\n");
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
);
98 } else if (strcasecmp(command
, "reset") == 0) {
99 res
= ioctl(ucmb_fd
, UCMB_IOCTL_RESETUC
);
101 fprintf(stderr
, "RESET ioctl failed: %s (%d)\n",
102 strerror(res
< 0 ? -res
: res
), res
);