Root/tools/atrf-proxy/atrf-proxy.c

Source at commit f4ca3aed63ef282f418093ba649fa39e193252db created 9 years 16 days ago.
By Werner Almesberger, tools/atrf-proxy/: make WAIT 0 wait forever
1/*
2 * atrf-proxy/atrf-proxy.c - ATRF network proxy
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18#include <strings.h> /* for strcasecmp */
19#include <sys/types.h>
20#include <sys/socket.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23
24#include "atrf.h"
25#include "netio.h"
26#include "daemon.h"
27
28
29#define DEFAULT_PORT 0x1540 /* 5440 */
30
31
32static int verbose = 0;
33
34
35static int get_num(struct netio *netio, int last, int *ret)
36{
37    char buf[10];
38    ssize_t got;
39    unsigned long n;
40    char *end;
41
42    got = netio_read_until(netio,
43        last ? "\n" : " ", buf, sizeof(buf), NULL);
44    if (got < 0) {
45        *ret = -1;
46        return -1;
47    }
48    buf[got] = 0;
49    n = strtoul(buf, &end, 0);
50    if (*end) {
51        *ret = netio_printf(netio, "-bad number\n");
52        return -1;
53    }
54    /* @@@ check against INT_MAX */
55    return n;
56}
57
58
59static int cmd_two(struct atrf_dsc *dsc, struct netio *netio, const char *cmd)
60{
61    int n, ret;
62
63    n = get_num(netio, 0, &ret);
64    if (n < 0)
65        return ret;
66    if (n > 255)
67        return netio_printf(netio, "-bad argument\n");
68
69    if (!strcasecmp(cmd, "set")) {
70        int val;
71
72        val = get_num(netio, 1, &ret);
73        if (val < 0)
74            return ret;
75        if (val > 255)
76            return netio_printf(netio, "-bad argument\n");
77        atrf_reg_write(dsc, n, val);
78        if (atrf_error(dsc))
79            return netio_printf(netio, "-I/O error\n");
80        return netio_printf(netio, "+\n");
81    }
82    if (!strcasecmp(cmd, "write")) {
83        uint8_t *tmp;
84        ssize_t got;
85
86        if (n > 128)
87            return netio_printf(netio, "-bad argument\n");
88        tmp = malloc(n+1);
89        if (!tmp)
90            return netio_printf(netio, "-out of memory\n");
91        got = netio_read(netio, tmp, n+1);
92        if (got != n+1) {
93            free(tmp);
94            return -1;
95        }
96        if (tmp[n] != '\n') {
97            free(tmp);
98            return netio_printf(netio, "-unterminated command\n");
99        }
100        atrf_buf_write(dsc, tmp, n);
101        free(tmp);
102        if (atrf_error(dsc))
103            return netio_printf(netio, "-I/O error\n");
104        return netio_printf(netio, "+\n");
105    }
106    if (!strcasecmp(cmd, "setram")) {
107        int val;
108
109        val = get_num(netio, 1, &ret);
110        if (val < 0)
111            return ret;
112        if (val > 255)
113            return netio_printf(netio, "-bad argument\n");
114        atrf_sram_write(dsc, n, val);
115        if (atrf_error(dsc))
116            return netio_printf(netio, "-I/O error\n");
117        return netio_printf(netio, "+\n");
118    }
119    if (!strcasecmp(cmd, "slp_tr")) {
120        int val;
121
122        if (n > 1)
123            return netio_printf(netio, "-bad argument\n");
124        val = get_num(netio, 1, &ret);
125        if (val < 0)
126            return ret;
127        if (val > 1)
128            return netio_printf(netio, "-bad argument\n");
129        if (atrf_slp_tr(dsc, n, val) < 0)
130            return netio_printf(netio, "-I/O error\n");
131        return netio_printf(netio, "+\n");
132    }
133    abort();
134}
135
136
137static int cmd_more(struct atrf_dsc *dsc, struct netio *netio, const char *cmd)
138{
139    int n, ret;
140
141    if (!strcasecmp(cmd, "set"))
142        return cmd_two(dsc, netio, cmd);
143    if (!strcasecmp(cmd, "setram"))
144        return cmd_two(dsc, netio, cmd);
145    if (!strcasecmp(cmd, "write"))
146        return cmd_two(dsc, netio, cmd);
147    if (!strcasecmp(cmd, "slp_tr"))
148        return cmd_two(dsc, netio, cmd);
149
150    n = get_num(netio, 1, &ret);
151    if (n < 0)
152        return ret;
153
154    if (!strcasecmp(cmd, "clkm")) {
155        if (n > 16)
156            return netio_printf(netio, "-bad argument\n");
157        if (atrf_set_clkm(dsc, n) < 0)
158            return netio_printf(netio, "-error\n");
159        return netio_printf(netio, "+\n");
160    }
161    if (!strcasecmp(cmd, "get")) {
162        uint8_t res;
163
164        if (n > 255)
165            return netio_printf(netio, "-bad argument\n");
166        res = atrf_reg_read(dsc, n);
167        if (atrf_error(dsc))
168            return netio_printf(netio, "-I/O error\n");
169        return netio_printf(netio, "+0x%02x\n", res);
170    }
171    if (!strcasecmp(cmd, "wait")) {
172        int res;
173
174        res = atrf_interrupt_wait(dsc, n);
175        if (atrf_error(dsc))
176            return netio_printf(netio, "-I/O error\n");
177        return netio_printf(netio, "+0x%02x\n", res);
178    }
179    if (!strcasecmp(cmd, "getram")) {
180        uint8_t res;
181
182        if (n > 255)
183            return netio_printf(netio, "-bad argument\n");
184        res = atrf_sram_read(dsc, n);
185        if (atrf_error(dsc))
186            return netio_printf(netio, "-I/O error\n");
187        return netio_printf(netio, "+0x%02x\n", res);
188    }
189    return netio_printf(netio, "-unrecognized command\n");
190}
191
192
193static int cmd_zero(struct atrf_dsc *dsc, struct netio *netio, const char *cmd)
194{
195    if (!strcasecmp(cmd, "spec")) {
196        const char *spec = atrf_driver_spec(dsc, 1);
197
198        if (spec)
199            return netio_printf(netio, "+%s\n", spec);
200        else
201            return netio_printf(netio,
202                "-can't obtain specification\n");
203    }
204    if (!strcasecmp(cmd, "reset")) {
205        atrf_reset(dsc);
206        return netio_printf(netio, "+\n");
207    }
208    if (!strcasecmp(cmd, "reset_rf")) {
209        atrf_reset_rf(dsc);
210        return netio_printf(netio, "+\n");
211    }
212    if (!strcasecmp(cmd, "test")) {
213        atrf_test_mode(dsc);
214        return netio_printf(netio, "+\n");
215    }
216    if (!strcasecmp(cmd, "read")) {
217        uint8_t buf[128+1]; /* one more for the trailing \n */
218        int got;
219
220        got = atrf_buf_read(dsc, buf, sizeof(buf));
221        if (got < 0)
222            return netio_printf(netio, "-I/O error\n");
223        if (netio_printf(netio, "+%d ", got) < 0)
224            return -1;
225        buf[got] = '\n';
226        return netio_write(netio, buf, got+1);
227    }
228    return netio_printf(netio, "-unrecognized command\n");
229}
230
231
232static void session(const char *driver, struct netio *netio)
233{
234    struct atrf_dsc *dsc;
235
236    dsc = atrf_open(driver);
237    if (!dsc) {
238        netio_printf(netio, "-unable to open driver\n");
239        return;
240    }
241
242    if (netio_printf(netio,
243        "+connected to %s\n", driver ? driver : "default") < 0)
244        goto done;
245
246    while (1) {
247        char buf[100];
248        ssize_t got;
249        char last;
250
251        got = netio_read_until(netio, " \n", buf, sizeof(buf), &last);
252        if (got < 0)
253            break;
254        if (!got && last) {
255            netio_printf(netio, "-empty input\n");
256            continue;
257        }
258        buf[got] = 0;
259        switch (last) {
260        case ' ':
261            if (cmd_more(dsc, netio, buf) < 0)
262                goto done;
263            break;
264        case '\n':
265            if (cmd_zero(dsc, netio, buf) < 0)
266                goto done;
267            break;
268        case 0:
269            goto done;
270        default:
271            abort();
272        }
273    }
274    
275done:
276    atrf_close(dsc);
277}
278
279
280static void loop(const char *driver, int port)
281{
282    struct sockaddr_in addr;
283    int s;
284    int one = 1;
285
286    s = socket(PF_INET, SOCK_STREAM, 0);
287    if (s < 0) {
288        perror("socket");
289        exit(1);
290    }
291
292    if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0) {
293        perror("setsockopt");
294        exit(1);
295    }
296
297    memset(&addr, 0, sizeof(addr));
298    addr.sin_family = AF_INET;
299    addr.sin_port = htons(port);
300    if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
301        perror("bind");
302        exit(1);
303    }
304
305    if (listen(s, 0) < 0) {
306        perror("listen");
307        exit(1);
308    }
309
310    while (1) {
311        socklen_t len = sizeof(addr);
312        int s2;
313        struct netio *netio;
314
315        s2 = accept(s, (struct sockaddr *) &addr, &len);
316        if (s2 < 0) {
317            perror("accept");
318            continue;
319        }
320
321        if (verbose)
322            fprintf(stderr, "%s:%u\n", inet_ntoa(addr.sin_addr),
323              ntohs(addr.sin_port));
324
325        netio = netio_open(s2);
326        if (netio) {
327            session(driver, netio);
328            netio_close(netio);
329        } else {
330            if (close(s2) < 0)
331                perror("close");
332        }
333    }
334}
335
336
337static void usage(const char *name)
338{
339    fprintf(stderr,
340"usage: %s [-b] [-d driver[:arg]] [-v ...] [port]\n\n"
341" port listen on the specified port (default: %d)\n\n"
342" -b background the process after initialization\n"
343" -d driver[:arg] use the specified driver (default: %s)\n"
344" -v ... increase verbosity level\n"
345    , name, DEFAULT_PORT, atrf_default_driver_name());
346    exit(1);
347}
348
349
350int main(int argc, char **argv)
351{
352    unsigned long port = DEFAULT_PORT;
353    const char *driver = NULL;
354    int foreground = 1;
355    char *end;
356    int c;
357
358    while ((c = getopt(argc, argv, "bd:v")) != EOF)
359        switch (c) {
360        case 'b':
361            foreground = 0;
362            break;
363        case 'd':
364            driver = optarg;
365            break;
366        case 'v':
367            verbose++;
368            netio_verbose++;
369            break;
370        default:
371            usage(*argv);
372        }
373    
374    switch (argc-optind) {
375    case 0:
376        break;
377    case 1:
378        port = strtoul(argv[optind], &end, 0);
379        if (*end || !port || port > 0xffff)
380            usage(*argv);
381        break;
382    default:
383        usage(*argv);
384    }
385
386    if (foreground || !daemonize())
387        loop(driver, port);
388
389    return 0;
390}
391

Archive Download this file



interactive