From ea2cd37c04880796338edb7253f0faa1faf83f3a Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Wed, 20 Jun 2012 11:31:56 -0300 Subject: [PATCH] properly implement "ping" and invoke it with antorcha -P --- fw/dispatch.c | 24 +++++++++++++++++--- tools/Makefile | 3 ++- tools/antorcha.c | 57 +++++++++++++++++++++++++++++++++--------------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/fw/dispatch.c b/fw/dispatch.c index feaa5bf..9684ac0 100644 --- a/fw/dispatch.c +++ b/fw/dispatch.c @@ -14,7 +14,9 @@ #include #include #include +#include +#include "version.h" #include "rf.h" #include "proto.h" #include "dispatch.h" @@ -36,11 +38,27 @@ static void send_ack(const uint8_t *buf) static bool answer_ping(const uint8_t *buf) { - uint8_t pong[] = { PONG, 0, 0 }; - + char num[6]; /* let's hope we won't need more than 9999 versions */ + char *num_end = num+sizeof(num); + int date_len = strlen(build_date); + int num_len; + uint8_t pong[3] = { PONG, 0, 0 }; + uint8_t tmp[3+6+date_len+1]; + char *s; + int n; + if (buf[1]) return 0; - rf_send(pong, sizeof(pong)); + s = num_end-1; + *s = ' '; + for (n = build_number; n; n /= 10) + *--s = '0'+n % 10; + *--s = '#'; + num_len = num_end-s; + memcpy(tmp, pong, 3); + memcpy(tmp+3, s, num_len); + memcpy(tmp+3+num_len, build_date, date_len); + rf_send(tmp, 3+num_len+date_len); return 1; } diff --git a/tools/Makefile b/tools/Makefile index d63242e..d598e87 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -14,7 +14,8 @@ MAIN = antorcha CFLAGS = -g -I../fw -I../../ben-wpan/tools/include \ - -I../../ben-wpan/atusb/fw/include + -I../../ben-wpan/atusb/fw/include \ + -Wall LDLIBS = -L../../ben-wpan/tools/lib -latrf -lusb OBJS = antorcha.o hash.o diff --git a/tools/antorcha.c b/tools/antorcha.c index c9d566c..6b7912f 100644 --- a/tools/antorcha.c +++ b/tools/antorcha.c @@ -84,21 +84,6 @@ static int rf_recv(struct atrf_dsc *dsc, void *buf, int size) } -static void ping(struct atrf_dsc *dsc) -{ - uint8_t ping[] = { 0 /*PING*/, 0, 0 }; - uint8_t buf[100]; - int got, i; - - rf_send(dsc, ping, sizeof(ping)); - got = rf_recv(dsc, buf, sizeof(buf)); - printf("%d:", got); - for (i = 0; i != got; i++) - printf(" %02x", buf[i]); - printf("\n"); -} - - static void packet_noack(struct atrf_dsc *dsc, uint8_t type, uint8_t seq, uint8_t last, const void *payload, int len) { @@ -141,6 +126,29 @@ static void packet(struct atrf_dsc *dsc, } +/* ----- Ping -------------------------------------------------------------- */ + + +static void ping(struct atrf_dsc *dsc) +{ + uint8_t ping[] = { PING, 0, 0 }; + uint8_t buf[100]; + int got, i; + + rf_send(dsc, ping, sizeof(ping)); + got = rf_recv(dsc, buf, sizeof(buf)); + if (!got) + return; + printf("%d: ", got); + for (i = 3; i != got; i++) + printf("%c", buf[i] >= ' ' && buf[i] <= '~' ? buf[i] : '?'); + printf("\n"); +} + + +/* ----- Firmware upload --------------------------------------------------- */ + + static const uint8_t unlock_secret[PAYLOAD] = { #include "unlock-secret.inc" }; @@ -194,9 +202,14 @@ static void firmware(struct atrf_dsc *dsc, const char *name) } +/* ----- Command-line processing ------------------------------------------- */ + + static void usage(const char *name) { - fprintf(stderr, "usage: %s -F firmware_file\n", name); + fprintf(stderr, +"usage: %s -F firmware_file\n" + "%6s %s -P\n", name, "", name); exit(1); } @@ -204,21 +217,27 @@ static void usage(const char *name) int main(int argc, char **argv) { const char *fw = NULL; + int do_ping = 0; struct atrf_dsc *dsc; int c; - while ((c = getopt(argc, argv, "F:")) != EOF) + while ((c = getopt(argc, argv, "F:P")) != EOF) switch (c) { case 'F': fw = optarg; break; + case 'P': + do_ping = 1; + break; default: usage(*argv); } + if (do_ping && fw) + usage(*argv); if (argc != optind) usage(*argv); - if (!fw) + if (!do_ping && !fw) usage(*argv); dsc = atrf_open(NULL); @@ -226,6 +245,8 @@ int main(int argc, char **argv) return 1; rf_init(dsc, 8, 15); + if (do_ping) + ping(dsc); if (fw) firmware(dsc, fw);