printf -> logs in xbee_user
[protos/xbee-avr.git] / xbee_user.c
index 81f2fb6..0d264b5 100644 (file)
 
 #include <aversive.h>
 #include <aversive/endian.h>
+#include <aversive/wait.h>
 #include <aversive/pgmspace.h>
 
+#include <callout.h>
 #include <rdline.h>
 #include <xbee.h>
+#include <xbee_rxtx.h>
 
 #include "rc_proto.h"
 #include "xbee_user.h"
 #include "main.h"
 
-#define XBEE_TIMEOUT_MS 1000
+#define XBEE_TIMEOUT_MS    1000
+#define XBEE_POLL_TIMER_MS 5
 
 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
 
@@ -51,17 +55,14 @@ int xbee_cmdline_input_enabled = 1;
 
 /* parameters */
 int xbee_raw = 0;
-int xbee_hexdump = 0;
-int xbee_debug = 0;
 
-static void hexdump(const char *title, const void *buf, unsigned int len)
+static void __hexdump(const void *buf, unsigned int len)
 {
        unsigned int i, out, ofs;
        const unsigned char *data = buf;
 #define LINE_LEN 80
        char line[LINE_LEN];    /* space needed 8+16*3+3+16 == 75 */
 
-       printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
        ofs = 0;
        while (ofs < len) {
                /* format 1 line in the buffer, then use printk to print them */
@@ -80,43 +81,61 @@ static void hexdump(const char *title, const void *buf, unsigned int len)
                                          LINE_LEN - out,
                                          PSTR("%c"), c);
                }
-               printf_P(PSTR("%s\r\n"), line);
+               DEBUG(E_USER_XBEE, "%s");
+       }
+}
+
+static void hexdump_msg(const char *title, const struct xbee_msg *msg)
+{
+       unsigned i;
+
+       DEBUG(E_USER_XBEE, "dump %s", title);
+       for (i = 0; i < msg->iovlen; 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);
        }
 }
 
+static void hexdump(const char *title, const void *buf, unsigned int len)
+{
+       DEBUG(E_USER_XBEE, "dump %s at [%p], len=%d", title, buf, len);
+       __hexdump(buf, len);
+}
+
 static int parse_xmit_status(struct xbee_ctx *ctx,
-                            struct xbee_xmit_status_hdr *frame, unsigned len)
+       struct xbee_xmit_status_hdr *frame, unsigned len)
 {
        (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;
 }
 
 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
-              unsigned len)
+       unsigned len)
 {
        char atcmd_str[3];
        const struct xbee_atcmd *cmd_pgm;
@@ -129,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;
        }
 
@@ -139,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);
@@ -149,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;
        }
 
@@ -180,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;
 }
 
@@ -247,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:
@@ -267,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;
                }
 
@@ -302,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 */
@@ -340,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) {
@@ -362,22 +376,17 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
        return ret;
 }
 
-static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
-                       int foreground)
+static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
+       int foreground)
 {
        int ret;
        int channel;
 
-       if (len > XBEE_MAX_FRAME_LEN) {
-               printf_P(PSTR("frame too large\r\n"));
-               return -1;
-       }
-
        /* register a channel */
        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;
        }
 
@@ -386,16 +395,14 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
        ctx = &xbee_ctx[channel];
        xbee_set_opaque(xbee_dev, channel, ctx);
 
-       if (xbee_debug)
-               printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
-                        channel, type, len);
-       if (xbee_hexdump)
-               hexdump("xmit frame", buf, len);
+       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(xbee_dev, channel, type, buf, len);
+       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;
        }
@@ -415,15 +422,15 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
 
 /* send an AT command with parameters filled by caller. Disable
  * command line until we get the answer or until a timeout occurs */
-int xbeeapp_send_atcmd(const char *atcmd_str,
-                      void *param, unsigned param_len, int foreground,
-                      int (*func)(void *frame, unsigned len, void *arg), void *arg)
+int xbeeapp_send_atcmd(char *atcmd_str, void *param,
+       unsigned param_len, int foreground,
+       int (*func)(void *frame, unsigned len, void *arg), void *arg)
 {
        struct xbee_ctx ctx;
-       struct {
-               struct xbee_atcmd_hdr atcmd;
-               char buf[XBEE_MAX_FRAME_LEN];
-       } __attribute__((packed)) frame;
+       /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */
+       struct xbee_msg msg;
+       uint8_t prio;
+       int ret;
 
        memset(&ctx, 0, sizeof(ctx));
        ctx.atcmd_query[0] = atcmd_str[0];
@@ -431,43 +438,53 @@ int xbeeapp_send_atcmd(const char *atcmd_str,
        ctx.func = func;
        ctx.arg = arg;
 
-       memcpy(&frame.atcmd.cmd, atcmd_str, 2);
-       memcpy(&frame.buf, param, param_len);
+       msg.iovlen = 2;
+       msg.iov[0].buf = atcmd_str;
+       msg.iov[0].len = 2;
+       msg.iov[1].buf = param;
+       msg.iov[1].len = param_len;
 
-       if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
-                        sizeof(struct xbee_atcmd_hdr) +
-                        param_len, foreground) < 0) {
-               return -1;
-       }
+       prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
+       ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 
-       return 0;
+       return ret;
 }
 
-int xbeeapp_send_msg(uint64_t addr, void *data,
-                    unsigned data_len, int foreground)
+int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
 {
        struct xbee_ctx ctx;
-       struct {
-               struct xbee_xmit_hdr xmit;
-               char buf[XBEE_MAX_FRAME_LEN];
-       } __attribute__((packed)) frame;
+       struct xbee_xmit_hdr xmit_hdr;
+       struct xbee_msg msg2;
+       unsigned i;
+       uint8_t prio;
+       int ret;
+
+       if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) {
+               ERROR(E_USER_XBEE, "too many iovecs");
+               return -1;
+       }
+
+
+       xmit_hdr.dstaddr = htonll(addr);
+       xmit_hdr.reserved = htons(0xFFFE);
+       xmit_hdr.bcast_radius = 0;
+       xmit_hdr.opts = 0;
+
+       msg2.iovlen = msg->iovlen + 1;
+       msg2.iov[0].buf = &xmit_hdr;
+       msg2.iov[0].len = sizeof(xmit_hdr);
+       for (i = 0; i < msg->iovlen; i++)
+               msg2.iov[i+1] = msg->iov[i];
 
        memset(&ctx, 0, sizeof(ctx));
        ctx.atcmd_query[0] = '\0';
 
-       frame.xmit.dstaddr = htonll(addr);
-       frame.xmit.reserved = htons(0xFFFE);
-       frame.xmit.bcast_radius = 0;
-       frame.xmit.opts = 0;
-       memcpy(&frame.buf, data, data_len);
+       prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
+       ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 
-       if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
-                        sizeof(struct xbee_xmit_hdr) +
-                        data_len, foreground) < 0) {
-               return -1;
-       }
-
-       return 0;
+       return ret;
 }
 
 static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
@@ -478,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();
@@ -492,20 +509,36 @@ static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
 
 void xbee_load_timeout(struct xbee_ctx *ctx)
 {
-       callout_init(&ctx->timeout, evt_timeout, ctx, 0);
-       callout_schedule(&xbeeboard.mainloop_cm, &ctx->timeout, XBEE_TIMEOUT_MS);
+       uint8_t prio;
+
+       callout_init(&ctx->timeout, evt_timeout, ctx, XBEE_PRIO);
+       prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
+       callout_schedule(&xbeeboard.intr_cm, &ctx->timeout, XBEE_TIMEOUT_MS);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 }
 
 void xbee_unload_timeout(struct xbee_ctx *ctx)
 {
-       callout_stop(&xbeeboard.mainloop_cm, &ctx->timeout);
+       uint8_t prio;
+
+       prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
+       callout_stop(&xbeeboard.intr_cm, &ctx->timeout);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
+}
+
+static void xbee_rx_poll_timer_cb(struct callout_mgr *cm, struct callout *tim,
+       void *arg)
+{
+       (void) arg;
+       xbee_rx(xbee_dev);
+       callout_reschedule(cm, tim, XBEE_POLL_TIMER_MS);
 }
 
 void xbee_mainloop(void)
 {
-       while (1) {
-               callout_manage(&xbeeboard.mainloop_cm);
+       uint8_t prio;
 
+       while (1) {
                if (xbee_raw) {
                        int16_t c;
 
@@ -517,11 +550,14 @@ void xbee_mainloop(void)
                        /* from cmdline to xbee */
                        c = cmdline_dev_recv(NULL);
                        if (c == 4) { /* CTRL-d */
+                               prio = callout_mgr_set_prio(&xbeeboard.intr_cm,
+                                       XBEE_PRIO);
                                xbee_dev_send('A', NULL);
                                xbee_dev_send('T', NULL);
                                xbee_dev_send('C', NULL);
                                xbee_dev_send('N', NULL);
                                xbee_dev_send('\n', NULL);
+                               callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
                                xbee_raw = 0;
                                rdline_newline(&xbeeboard.rdl,
                                               xbeeboard.prompt);
@@ -537,7 +573,8 @@ void xbee_mainloop(void)
                else {
                        if (xbee_cmdline_input_enabled)
                                cmdline_poll();
-                       xbee_rx(xbee_dev);
+                       /* xbee rx polling is done in a timer, so we can block
+                        * the cmdline without loosing incoming packets */
                }
        }
 }
@@ -551,3 +588,11 @@ void xbee_stdin_disable(void)
 {
        xbee_cmdline_input_enabled = 0;
 }
+
+void xbeeapp_init(void)
+{
+       callout_init(&xbeeboard.xbee_rx_poll_timer, xbee_rx_poll_timer_cb,
+               NULL, XBEE_PRIO);
+       callout_schedule(&xbeeboard.intr_cm,
+               &xbeeboard.xbee_rx_poll_timer, XBEE_POLL_TIMER_MS);
+}