printf -> logs in xbee_user
authorOlivier Matz <zer0@droids-corp.org>
Thu, 6 Mar 2014 18:50:06 +0000 (19:50 +0100)
committerOlivier Matz <zer0@droids-corp.org>
Sun, 9 Mar 2014 21:13:09 +0000 (22:13 +0100)
commands.c
commands_gen.c
main.h
xbee_user.c
xbee_user.h

index cf060fc..3448ded 100644 (file)
@@ -1288,94 +1288,6 @@ const parse_inst_t PROGMEM cmd_raw = {
        },
 };
 
-/* ************* */
-
-/* this structure is filled when cmd_dump is parsed successfully */
-struct cmd_dump_result {
-       fixed_string_t dump;
-       fixed_string_t onoff;
-};
-
-/* function called when cmd_dump is parsed successfully */
-static void cmd_dump_parsed(void *parsed_result, void *data)
-{
-       struct cmd_dump_result *res = parsed_result;
-
-       (void)data;
-       if (!strcmp(res->onoff, "on"))
-               xbee_hexdump = 1;
-       else
-               xbee_hexdump = 0;
-}
-
-const char PROGMEM str_dump[] = "dump";
-const char PROGMEM str_dump_onoff[] = "on#off";
-
-const parse_token_string_t PROGMEM cmd_dump_dump =
-       TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
-                                str_dump);
-
-const parse_token_string_t PROGMEM cmd_dump_onoff =
-       TOKEN_STRING_INITIALIZER(struct cmd_dump_result, onoff,
-                                str_dump_onoff);
-
-const char PROGMEM help_dump[] = "enable/disable hexdump of received packets";
-
-const parse_inst_t PROGMEM cmd_dump = {
-       .f = cmd_dump_parsed,  /* function to call */
-       .data = NULL,      /* 2nd arg of func */
-       .help_str = help_dump,
-       .tokens = {        /* token list, NULL terminated */
-               (PGM_P)&cmd_dump_dump,
-               (PGM_P)&cmd_dump_onoff,
-               NULL,
-       },
-};
-
-/* ************* */
-
-/* this structure is filled when cmd_debug is parsed successfully */
-struct cmd_debug_result {
-       fixed_string_t debug;
-       fixed_string_t onoff;
-};
-
-/* function called when cmd_debug is parsed successfully */
-static void cmd_debug_parsed(void *parsed_result, void *data)
-{
-       struct cmd_debug_result *res = parsed_result;
-
-       (void)data;
-       if (!strcmp(res->onoff, "on"))
-               xbee_debug = 1;
-       else
-               xbee_debug = 0;
-}
-
-const char PROGMEM str_debug[] = "debug";
-const char PROGMEM str_debug_onoff[] = "on#off";
-
-const parse_token_string_t PROGMEM cmd_debug_debug =
-       TOKEN_STRING_INITIALIZER(struct cmd_debug_result, debug,
-                                str_debug);
-
-const parse_token_string_t PROGMEM cmd_debug_onoff =
-       TOKEN_STRING_INITIALIZER(struct cmd_debug_result, onoff,
-                                str_debug_onoff);
-
-const char PROGMEM help_debug[] = "enable/disable additionnal debug";
-
-const parse_inst_t PROGMEM cmd_debug = {
-       .f = cmd_debug_parsed,  /* function to call */
-       .data = NULL,      /* 2nd arg of func */
-       .help_str = help_debug,
-       .tokens = {        /* token list, NULL terminated */
-               (PGM_P)&cmd_debug_debug,
-               (PGM_P)&cmd_debug_onoff,
-               NULL,
-       },
-};
-
 /**********************************************************/
 
 /* this structure is filled when cmd_baudrate is parsed successfully */
@@ -1937,8 +1849,6 @@ const parse_ctx_t PROGMEM main_ctx[] = {
        &cmd_monitor_del,
        &cmd_ping,
        &cmd_raw,
-       &cmd_dump,
-       &cmd_debug,
        &cmd_baudrate,
        &cmd_beep,
        &cmd_servo_set,
index f1039b3..752b998 100644 (file)
@@ -157,6 +157,7 @@ struct cmd_log_result {
 static const char PROGMEM uart_log[] = "uart";
 static const char PROGMEM i2c_log[] = "i2c";
 static const char PROGMEM default_log[] = "default";
+static const char PROGMEM xbee_log[] = "xbee";
 
 struct log_name_and_num {
        const char *name;
@@ -167,6 +168,7 @@ static const struct log_name_and_num log_name_and_num[] = {
        { uart_log, E_UART },
        { i2c_log, E_I2C },
        { default_log, E_USER_DEFAULT },
+       { xbee_log, E_USER_XBEE },
 };
 
 static uint8_t
diff --git a/main.h b/main.h
index afb1c3e..ba0836e 100644 (file)
--- a/main.h
+++ b/main.h
@@ -45,6 +45,7 @@
 
 /** ERROR NUMS */
 #define E_USER_DEFAULT           194
+#define E_USER_XBEE              195
 
 #define LED1_ON()       sbi(PORTA, 2)
 #define LED1_OFF()      cbi(PORTA, 2)
index 26fe261..0d264b5 100644 (file)
@@ -34,6 +34,7 @@
 
 #include <aversive.h>
 #include <aversive/endian.h>
+#include <aversive/wait.h>
 #include <aversive/pgmspace.h>
 
 #include <callout.h>
@@ -54,8 +55,6 @@ int xbee_cmdline_input_enabled = 1;
 
 /* parameters */
 int xbee_raw = 0;
-int xbee_hexdump = 0;
-int xbee_debug = 0;
 
 static void __hexdump(const void *buf, unsigned int len)
 {
@@ -82,7 +81,7 @@ static void __hexdump(const void *buf, unsigned int len)
                                          LINE_LEN - out,
                                          PSTR("%c"), c);
                }
-               printf_P(PSTR("%s\r\n"), line);
+               DEBUG(E_USER_XBEE, "%s");
        }
 }
 
@@ -90,9 +89,9 @@ static void hexdump_msg(const char *title, const struct xbee_msg *msg)
 {
        unsigned i;
 
-       printf_P(PSTR("dump %s\r\n"), title);
+       DEBUG(E_USER_XBEE, "dump %s", title);
        for (i = 0; i < msg->iovlen; i++) {
-               printf_P(PSTR("iovec %d at %p, len=%d\r\n"), i,
+               DEBUG(E_USER_XBEE, "iovec %d at %p, len=%d", i,
                        msg->iov[i].buf, msg->iov[i].len);
                __hexdump(msg->iov[i].buf, msg->iov[i].len);
        }
@@ -100,7 +99,7 @@ static void hexdump_msg(const char *title, const struct xbee_msg *msg)
 
 static void hexdump(const char *title, const void *buf, unsigned int len)
 {
-       printf_P(PSTR("dump %s at [%p], len=%d\r\n"), title, buf, len);
+       DEBUG(E_USER_XBEE, "dump %s at [%p], len=%d", title, buf, len);
        __hexdump(buf, len);
 }
 
@@ -110,27 +109,27 @@ static int parse_xmit_status(struct xbee_ctx *ctx,
        (void)len;
 
        if (ctx == NULL) {
-               printf_P(PSTR("no context\r\n"));
+               ERROR(E_USER_XBEE, "no context");
                return -1;
        }
 
        /* see if it matches a xmit query (atcmd_query must be NULL) */
        if (ctx->atcmd_query[0] != '\0') {
-               printf_P(PSTR("invalid response 2\r\n"));
+               ERROR(E_USER_XBEE, "invalid response 2");
                return -1;
        }
 
        /* XXX use defines for these values */
        if (frame->delivery_status == 0x00)
-               printf_P(PSTR("Success\r\n"));
+               NOTICE(E_USER_XBEE, "Success");
        else if (frame->delivery_status == 0x01)
-               printf_P(PSTR("MAC ACK Failure\r\n"));
+               WARNING(E_USER_XBEE, "MAC ACK Failure");
        else if (frame->delivery_status == 0x15)
-               printf_P(PSTR("Invalid destination endpoint\r\n"));
+               WARNING(E_USER_XBEE, "Invalid destination endpoint");
        else if (frame->delivery_status == 0x21)
-               printf_P(PSTR("Network ACK Failure\r\n"));
+               WARNING(E_USER_XBEE, "Network ACK Failure");
        else if (frame->delivery_status == 0x25)
-               printf_P(PSTR("Route Not Found\r\n"));
+               WARNING(E_USER_XBEE, "Route Not Found");
 
        return 0;
 }
@@ -149,7 +148,7 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        } __attribute__((packed)) *result;
 
        if (ctx == NULL) {
-               printf_P(PSTR("no context\r\n"));
+               ERROR(E_USER_XBEE, "no context");
                return -1;
        }
 
@@ -159,7 +158,7 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
 
        /* see if it matches query */
        if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
-               printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
+               ERROR(E_USER_XBEE, "invalid response <%c%c><%s><%s>",
                         frame->cmd & 0xFF,
                         (frame->cmd >> 8) & 0xFF,
                         atcmd_str, ctx->atcmd_query);
@@ -169,26 +168,27 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        /* see if it exists */
        cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
        if (cmd_pgm == NULL) {
-               printf_P(PSTR("unknown response\r\n"));
+               ERROR(E_USER_XBEE, "unknown response");
                return -1;
        }
        memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
 
        /* bad status */
        if (frame->status == 1) {
-               printf_P(PSTR("Status is error\r\n"));
+               WARNING(E_USER_XBEE, "Status is error");
                return -1;
        }
        else if (frame->status == 2) {
-               printf_P(PSTR("Invalid command\r\n"));
+               WARNING(E_USER_XBEE, "Invalid command");
                return -1;
        }
        else if (frame->status == 3) {
-               printf_P(PSTR("Invalid parameter\r\n"));
+               WARNING(E_USER_XBEE, "Invalid parameter");
                return -1;
        }
        else if (frame->status != 0) {
-               printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
+               WARNING(E_USER_XBEE, "Unknown status error %d",
+                       frame->status);
                return -1;
        }
 
@@ -200,24 +200,22 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        result = (void *)frame->data;
        len -= offsetof(struct xbee_atresp_hdr, data);
        if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
-               printf_P(PSTR("<%s> is 0x%x (%d)\r\n"), atcmd_str, result->u8,
-                        result->u8);
+               NOTICE(E_USER_XBEE, "<%s> is 0x%x (%d)", atcmd_str,
+                       result->u8, result->u8);
        else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
-               printf_P(PSTR("<%s> is 0x%x (%d)\r\n"),
-                        atcmd_str,
-                        ntohs(result->u16), ntohs(result->u16));
+               NOTICE(E_USER_XBEE, "<%s> is 0x%x (%d)", atcmd_str,
+                       ntohs(result->u16), ntohs(result->u16));
        else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
-               printf_P(PSTR("<%s> is 0x%"PRIx32" (%"PRIu32")\r\n"),
-                        atcmd_str,
-                        ntohl(result->u32), ntohs(result->u32));
+               NOTICE(E_USER_XBEE, "<%s> is 0x%"PRIx32" (%"PRIu32")",
+                       atcmd_str, ntohl(result->u32), ntohs(result->u32));
        else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
-               printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
+               NOTICE(E_USER_XBEE, "<%s> is %d",
+                       atcmd_str, ntohs(result->s16));
        else if (len == 0)
-               printf_P(PSTR("no data, status ok\r\n"));
+               NOTICE(E_USER_XBEE, "no data, status ok");
        else
                hexdump("atcmd answer", frame->data, len);
 
-
        return 0;
 }
 
@@ -267,10 +265,9 @@ int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
                        struct rc_proto_hello *rch =
                                (struct rc_proto_hello *) recvframe->data;
 
-                       if (xbee_debug)
-                               printf_P(PSTR("recv hello len=%d\r\n"),
-                                        rch->datalen);
-
+                       NOTICE(E_USER_XBEE, "recv hello len=%d",
+                               rch->datalen);
+                       /* XXX stats */
                        break;
                }
                default:
@@ -287,23 +284,22 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
        struct xbee_ctx *ctx = opaque;
        int8_t ret = 0;
 
-       if (xbee_debug)
-               printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
-                        type, channel, ctx);
+       NOTICE(E_USER_XBEE, "type=0x%x, channel=%d, ctx=%p",
+               type, channel, ctx);
 
        /* if ctx is !NULL, it is an answer to a query */
        if (ctx != NULL) {
                /* XXX only delete timeout if answer matched */
                xbee_unload_timeout(ctx);
-               if (xbee_debug && ctx->atcmd_query)
-                       printf_P(PSTR("Received answer to query <%c%c>\r\n"),
-                                ctx->atcmd_query[0], ctx->atcmd_query[1]);
+               if (ctx->atcmd_query)
+                       NOTICE(E_USER_XBEE, "Received answer to query <%c%c>",
+                               ctx->atcmd_query[0], ctx->atcmd_query[1]);
        }
 
        /* some additional checks before sending */
        switch (type) {
                case XBEE_TYPE_MODEM_STATUS: {
-                       printf_P(PSTR("Received Modem Status frame\r\n"));
+                       NOTICE(E_USER_XBEE, "Received Modem Status frame");
                        break;
                }
 
@@ -322,7 +318,7 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
                        } addr;
                        memcpy(&addr, frame, sizeof(addr));
                        addr.u64 = ntohll(addr.u64);
-                       printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
+                       NOTICE(E_USER_XBEE, "from remote address %"PRIx32"%"PRIx32"",
                                 addr.u32.high, addr.u32.low);
 
                        /* this answer contains an atcmd answer at offset 10 */
@@ -360,15 +356,13 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
                case XBEE_TYPE_EXPL_RECV:
                case XBEE_TYPE_NODE_ID:
                default:
-                       printf_P(PSTR("Invalid frame\r\n"));
+                       ERROR(E_USER_XBEE, "Invalid frame");
                        ret = -1;
                        break;
        }
 
-       if (ret < 0)
-               hexdump("undecoded rx frame", frame, len);
-       else if (xbee_hexdump)
-               hexdump("undecoded rx frame", frame, len);
+       WARNING(E_USER_XBEE, "undecoded rx frame");
+       hexdump("undecoded rx frame", frame, len);
 
        /* restart command line if it was a blocking query */
        if (ctx != NULL) {
@@ -392,7 +386,7 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
        channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
                                        xbeeapp_rx, NULL);
        if (channel < 0) {
-               printf_P(PSTR("cannot send: no free channel\r\n"));
+               ERROR(E_USER_XBEE, "cannot send: no free channel");
                return -1;
        }
 
@@ -401,16 +395,14 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
        ctx = &xbee_ctx[channel];
        xbee_set_opaque(xbee_dev, channel, ctx);
 
-       if (xbee_debug)
-               printf_P(PSTR("send frame channel=%d type=0x%x\r\n"),
-                       channel, type);
-       if (xbee_hexdump)
-               hexdump_msg("xmit frame", msg);
+       NOTICE(E_USER_XBEE, "send frame channel=%d type=0x%x",
+               channel, type);
+       hexdump_msg("xmit frame", msg);
 
        /* transmit the frame on this channel */
        ret = xbee_tx_iovec(xbee_dev, channel, type, msg);
        if (ret < 0) {
-               printf_P(PSTR("cannot send\r\n"));
+               ERROR(E_USER_XBEE, "cannot send");
                xbee_unregister_channel(xbee_dev, channel);
                return -1;
        }
@@ -469,7 +461,7 @@ int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
        int ret;
 
        if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) {
-               printf_P(PSTR("too many iovecs\r\n"));
+               ERROR(E_USER_XBEE, "too many iovecs");
                return -1;
        }
 
@@ -503,7 +495,7 @@ static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
        (void)cm;
        (void)clt;
 
-       printf_P(PSTR("Timeout\r\n"));
+       WARNING(E_USER_XBEE, "Timeout");
 
        /* restart command line */
        xbee_stdin_enable();
index 0634d00..cfb2ea8 100644 (file)
@@ -44,8 +44,6 @@ struct xbee_ctx {
 //extern cmdline_parse_ctx_t main_ctx;
 extern struct xbee_dev *xbee_dev;
 extern int xbee_raw;
-extern int xbee_hexdump;
-extern int xbee_debug;
 
 /* we use a specific structure to send packets. It allows to prepend some
  * data in the frame without doing a copy. */