printf -> logs in xbee_user
[protos/xbee-avr.git] / xbee_user.c
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();