Skip to content

Commit 485a408

Browse files
committed
basic arg parser with verbose flag
1 parent e1881b6 commit 485a408

File tree

1 file changed

+84
-27
lines changed

1 file changed

+84
-27
lines changed

ems.c

Lines changed: 84 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <errno.h>
2+
#include <getopt.h>
23
#include <signal.h>
34
#include <string.h>
45
#include <stdio.h>
@@ -9,6 +10,18 @@
910

1011
#include <libusb.h>
1112

13+
/* options */
14+
typedef struct _options_t {
15+
int verbose;
16+
char *file;
17+
} options_t;
18+
19+
// defaults
20+
options_t opts = {
21+
.verbose = 0,
22+
.file = NULL,
23+
};
24+
1225
#define EMS_EP_SEND (2 | LIBUSB_ENDPOINT_OUT)
1326
#define EMS_EP_RECV (1 | LIBUSB_ENDPOINT_IN)
1427

@@ -21,6 +34,7 @@ static struct libusb_device_handle *devh = NULL;
2134

2235
static int find_ems_device(void) {
2336
devh = libusb_open_device_with_vid_pid(NULL, 0x4670, 0x9394);
37+
2438
return devh ? 0 : -EIO;
2539
}
2640

@@ -84,64 +98,107 @@ static int ems_write(uint32_t offset, unsigned char *buf, size_t count) {
8498
return r;
8599
}
86100

101+
void get_options(int argc, char **argv) {
102+
int c;
103+
104+
while (1) {
105+
int this_option_optind = optind ? optind : 1;
106+
int option_index = 0;
107+
static struct option long_options[] = {
108+
{"verbose", 0, 0, 'v'},
109+
{0, 0, 0, 0}
110+
};
111+
112+
c = getopt_long(
113+
argc, argv, "v",
114+
long_options, &option_index
115+
);
116+
if (c == -1)
117+
break;
118+
119+
switch (c) {
120+
case 'v':
121+
opts.verbose = 1;
122+
break;
123+
default:
124+
break;
125+
}
126+
}
127+
128+
// extra argument: ROM file
129+
if (optind < argc)
130+
opts.file = argv[optind];
131+
}
132+
87133
int main(int argc, char **argv) {
88134
int r = 1;
89135
int i;
90136

137+
get_options(argc, argv);
138+
91139
r = libusb_init(NULL);
92140
if (r < 0) {
93-
fprintf(stderr, "failed to initialise libusb\n");
94-
exit(1);
141+
fprintf(stderr, "failed to initialise libusb\n");
142+
exit(1);
95143
}
96144

97145
r = find_ems_device();
98146
if (r < 0) {
99-
fprintf(stderr, "Could not find/open device\n");
100-
goto out;
147+
fprintf(stderr, "Could not find/open device\n");
148+
goto out;
101149
}
102150

103151
r = libusb_claim_interface(devh, 0);
104152
if (r < 0) {
105-
fprintf(stderr, "usb_claim_interface error %d\n", r);
106-
goto out;
153+
fprintf(stderr, "usb_claim_interface error %d\n", r);
154+
goto out;
107155
}
108-
printf("claimed interface\n");
109156

110-
if (argc > 1) {
111-
FILE *write_file = fopen(argv[1], "r");
112-
if (write_file == NULL)
113-
err(1, "fopen");
157+
if (opts.verbose)
158+
printf("claimed interface\n");
159+
160+
// file provided: write the file
161+
if (opts.file != NULL) {
162+
FILE *write_file = fopen(opts.file, "r");
163+
if (write_file == NULL) {
164+
warn("Can't open ROM file %s", opts.file);
165+
goto out_release;
166+
}
114167

115168
char buf[32];
116169
uint32_t offset = 0;
117170

118171
while (fread(buf, sizeof(buf), 1, write_file) == 1) {
119172
r = ems_write(offset, buf, sizeof(buf));
120-
if (r < 0)
121-
errx(1, "can't write %d", r);
173+
if (r < 0) {
174+
warn("can't write %d", r);
175+
goto out_release;
176+
}
122177

123178
offset += sizeof(buf);
124179
}
125180
}
126181

182+
// print the first 0x200 bytes in hex and ASCII
183+
if (opts.verbose) {
184+
unsigned char data[0x200];
185+
r = ems_read(0x0, data, sizeof(data));
127186

128-
unsigned char data[0x200];
129-
r = ems_read(0x0, data, sizeof(data));
187+
printf("received %d\n", r);
130188

131-
printf("received %d\n", r);
132-
133-
for (i = 0; i < r; ++i) {
134-
printf("%02x ", data[i]);
135-
if ((i & 0xf) == 0xf)
136-
printf("\n");
137-
}
189+
for (i = 0; i < r; ++i) {
190+
printf("%02x ", data[i]);
191+
if ((i & 0xf) == 0xf)
192+
printf("\n");
193+
}
138194

139-
for (i = 0; i < r; ++i) {
140-
char pr = isprint(data[i]) ? data[i] : '.';
141-
printf("%c", pr);
195+
for (i = 0; i < r; ++i) {
196+
char pr = isprint(data[i]) ? data[i] : '.';
197+
printf("%c", pr);
142198

143-
if ((i & 0xf) == 0xf)
144-
printf("\n");
199+
if ((i & 0xf) == 0xf)
200+
printf("\n");
201+
}
145202
}
146203

147204
out_release:

0 commit comments

Comments
 (0)