Date:2011-04-20 13:58:17 (12 years 11 months ago)
Author:Werner Almesberger
Commit:2961482cac72ca292ad95a4e6bf53500014279b4
Message:tools: atrf-id option -s to retrieve driver spec, with necessary infrastructure

- include/atrf.h (atrf_driver_spec), lib/atrf.c (atrf_driver_spec):
new function to retrieve the local or remote/final driver spec
- lib/atrf.c (struct atrf_dsc, atrf_open, atrf_close): record the local
driver spec
- lib/driver.h (struct atrf_driver): new driver function "driver_spec"
to retrieve the driver spec
- lib/atnet.c (struct atnet_dsc, atnet_open, atnet_close): maintain a
cache for the driver spec
- lib/atnet.c (atnet_driver_spec, atnet_driver): added support for the
"driver_spec" function
- atrf-proxy/PROTOCOL, atrf-proxy/atrf-proxy.c (cmd_zero): added command
SPEC to retrieve the (final) driver spec
- atrf-id/atrf-id.c (usage, main): added option -s to retrieve the
driver spec. One -s retrieves the local spec, -s -s the remote/final.
Files: tools/atrf-id/atrf-id.c (3 diffs)
tools/atrf-proxy/PROTOCOL (1 diff)
tools/atrf-proxy/atrf-proxy.c (1 diff)
tools/include/atrf.h (1 diff)
tools/lib/atnet.c (4 diffs)
tools/lib/atrf.c (3 diffs)
tools/lib/driver.h (1 diff)

Change Details

tools/atrf-id/atrf-id.c
158158
159159static void usage(const char *name)
160160{
161    fprintf(stderr, "usage: %s [-d driver[:arg]]\n", name);
161    fprintf(stderr,
162"usage: %s [-d driver[:arg]] [-s [-s]]\n\n"
163" -d driver[:arg] use the specified driver (default: %s)\n"
164" -s print only the local driver specification\n"
165" -s -s print only the remote driver specification\n"
166    , name, atrf_default_driver_name());
162167    exit(1);
163168}
164169
...... 
167172{
168173    const char *driver = NULL;
169174    struct atrf_dsc *dsc;
175    int spec_only = 0;
170176    int c;
171177
172    while ((c = getopt(argc, argv, "d:")) != EOF)
178    while ((c = getopt(argc, argv, "d:s")) != EOF)
173179        switch (c) {
174180        case 'd':
175181            driver = optarg;
176182            break;
183        case 's':
184            spec_only++;
185            break;
177186        default:
178187            usage(*argv);
179188        }
...... 
184193    if (!dsc)
185194        return 1;
186195
187    show_info(dsc);
196    if (spec_only) {
197        const char *spec = atrf_driver_spec(dsc, spec_only > 1);
198
199        if (spec)
200            printf("%s\n", spec);
201        else {
202            fprintf(stderr, "can't obtain specification\n");
203            exit(1);
204        }
205    } else {
206        show_info(dsc);
207    }
188208
189209    atrf_close(dsc);
190210
tools/atrf-proxy/PROTOCOL
88+[greeting]
99-message
1010
11SPEC
12+driver_spec
13-message
14
1115RESET
1216+[comment]
1317-message
tools/atrf-proxy/atrf-proxy.c
151151{
152152    int res;
153153
154    if (!strcasecmp(cmd, "spec")) {
155        const char *spec = atrf_driver_spec(dsc, 1);
156
157        if (spec)
158            return netio_printf(netio, "+%s\n", spec);
159        else
160            return netio_printf(netio,
161                "-can't obtain specification\n");
162    }
154163    if (!strcasecmp(cmd, "reset")) {
155164        atrf_reset(dsc);
156165        return netio_printf(netio, "+\n");
tools/include/atrf.h
3232const char *atrf_default_driver_name(void);
3333struct atrf_dsc *atrf_open(const char *spec);
3434void atrf_close(struct atrf_dsc *dsc);
35const char *atrf_driver_spec(struct atrf_dsc *dsc, int last);
3536
3637int atrf_error(struct atrf_dsc *dsc);
3738int atrf_clear_error(struct atrf_dsc *dsc);
tools/lib/atnet.c
3131struct atnet_dsc {
3232    struct netio *netio;
3333    int error;
34    char *spec;
3435    char reply[1000];
3536};
3637
...... 
192193    }
193194
194195    dsc->error = 0;
196    dsc->spec = NULL;
195197    *dsc->reply = 0;
196198
197199    return dsc;
...... 
203205    struct atnet_dsc *dsc = handle;
204206
205207    netio_close(dsc->netio);
208    free(dsc->spec);
209}
210
211
212/* ----- driver specification ---------------------------------------------- */
213
214
215static const char *atnet_driver_spec(void *handle)
216{
217    struct atnet_dsc *dsc = handle;
218
219    if (dialog(dsc, "SPEC") < 0) {
220        dsc->error = 1;
221        return NULL;
222    }
223    free(dsc->spec);
224    dsc->spec = strdup(dsc->reply+1);
225    if (!dsc->spec) {
226        perror("strdup");
227        exit(1);
228    }
229    return dsc->spec;
206230}
207231
208232
...... 
421445    .name = "net",
422446    .open = atnet_open,
423447    .close = atnet_close,
448    .driver_spec = atnet_driver_spec,
424449    .error = atnet_error,
425450    .clear_error = atnet_clear_error,
426451    .reset = atnet_reset,
tools/lib/atrf.c
2424struct atrf_dsc {
2525    const struct atrf_driver *driver;
2626    void *handle;
27    char *spec;
2728    enum atrf_chip_id chip_id;
2829};
2930
...... 
164165    }
165166    dsc->driver = driver;
166167    dsc->handle = handle;
168    if (spec) {
169        dsc->spec = strdup(spec);
170        if (!dsc->spec) {
171            perror("strdup");
172            exit(1);
173        }
174    } else {
175        dsc->spec= NULL;
176    }
167177    dsc->chip_id = identify(dsc);
168178    return dsc;
169179}
...... 
173183{
174184    if (dsc->driver->close)
175185        dsc->driver->close(dsc->handle);
186    free(dsc->spec);
176187    free(dsc);
177188}
178189
179190
191const char *atrf_driver_spec(struct atrf_dsc *dsc, int last)
192{
193    if (!dsc->spec)
194        return dsc->driver->name;
195    if (!last || !dsc->driver->driver_spec)
196        return dsc->spec;
197    return dsc->driver->driver_spec(dsc->handle);
198}
199
200
180201void atrf_reset(struct atrf_dsc *dsc)
181202{
182203    if (dsc->driver->reset)
tools/lib/driver.h
2121    const char *name;
2222    void *(*open)(const char *arg);
2323    void (*close)(void *dsc);
24    const char *(*driver_spec)(void *dsc);
2425    int (*error)(void *dsc);
2526    int (*clear_error)(void *dsc);
2627    void (*reset)(void *dsc);

Archive Download the corresponding diff file



interactive