beep when GPS ready
[protos/xbee-avr.git] / xbee_user.c
index ca9d475..17a92af 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,8 @@ int xbee_cmdline_input_enabled = 1;
 
 /* parameters */
 int xbee_raw = 0;
-int xbee_hexdump = 0;
-int xbee_debug = 0;
+
+static struct callout xbee_rx_poll_timer;
 
 static void __hexdump(const void *buf, unsigned int len)
 {
@@ -82,7 +83,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", line);
        }
 }
 
@@ -90,17 +91,18 @@ 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);
        }
 }
 
-static void hexdump(const char *title, const void *buf, unsigned int len)
+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,46 +112,89 @@ 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) */
+       /* see if it matches a xmit query (atcmd_query must be \0) */
        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,
+/* Write in a human readable format the content of an atcmd response frame. It
+ * assumes the frame is valid .*/
+void atresp_to_str(char *buf, unsigned buflen, const struct xbee_atresp_hdr *frame,
        unsigned len)
 {
-       char atcmd_str[3];
-       const struct xbee_atcmd *cmd_pgm;
-       struct xbee_atcmd cmd;
        union {
                uint8_t u8;
                uint16_t u16;
                uint32_t u32;
                int16_t s16;
        } __attribute__((packed)) *result;
+       char atcmd_str[3];
+       const struct xbee_atcmd *cmd_pgm;
+       struct xbee_atcmd cmd;
+
+       /* get AT command from frame */
+       memcpy(atcmd_str, &frame->cmd, 2);
+       atcmd_str[2] = '\0';
+
+
+       /* see if it exists */
+       cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
+       if (cmd_pgm == NULL) {
+               snprintf(buf, buflen, "<%s> (unknown cmd)", atcmd_str);
+               return;
+       }
+       memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
+       len -= sizeof(*frame);
+
+       /* dump frame */
+       result = (void *)frame->data;
+
+       if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
+               snprintf(buf, buflen, "<%s> is 0x%x (%d)", atcmd_str,
+                       result->u8, result->u8);
+       else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
+               snprintf(buf, buflen, "<%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))
+               snprintf(buf, buflen, "<%s> is 0x%"PRIx32" (%"PRIu32")",
+                       atcmd_str, ntohl(result->u32), ntohl(result->u32));
+       else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
+               snprintf(buf, buflen, "<%s> is %d",
+                       atcmd_str, ntohs(result->s16));
+       else if (len == 0)
+               snprintf(buf, buflen, "<%s> no data", atcmd_str);
+       else
+               snprintf(buf, buflen, "invalid atresp");
+}
+
+static int parse_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
+       unsigned len)
+{
+       char atcmd_str[3];
+       char buf[32];
 
        if (ctx == NULL) {
-               printf_P(PSTR("no context\r\n"));
+               ERROR(E_USER_XBEE, "no context");
                return -1;
        }
 
@@ -159,151 +204,69 @@ 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);
                return -1;
        }
 
-       /* see if it exists */
-       cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
-       if (cmd_pgm == NULL) {
-               printf_P(PSTR("unknown response\r\n"));
-               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;
        }
 
-       /* callback */
-       if (ctx->func != NULL)
-               ctx->func(frame, len, ctx->arg);
+       atresp_to_str(buf, sizeof(buf), frame, len);
 
-       /* dump 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);
-       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));
-       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));
-       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));
-       else if (len == 0)
-               printf_P(PSTR("no data, status ok\r\n"));
-       else
-               hexdump("atcmd answer", frame->data, len);
+       len -= sizeof(*frame);
+       NOTICE(E_USER_XBEE, "status ok, datalen=%d, %s", len, buf);
 
+       if (len != 0)
+               hexdump("atcmd answer", frame->data, len);
 
        return 0;
 }
 
 
-int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
-{
-       unsigned int datalen;
-       struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
-
-       if (len <  sizeof(*recvframe))
-               return -1;
-
-       datalen = len - sizeof(*recvframe);
-       if (datalen < sizeof(struct rc_proto_hdr))
-               return -1;
-
-       switch (rch->type) {
-#if 0
-               case RC_PROTO_TYPE_CHANNEL: {
-                       struct rc_proto_channel *rcc =
-                               (struct rc_proto_channel *) recvframe->data;
-                       int16_t val;
-                       if (datalen != sizeof(struct rc_proto_channel))
-                               return -1;
-                       val = ntohs(rcc->axis[0]);
-                       val >>= 6;
-                       val += 512;
-                       spi_servo_set(0, val);
-                       break;
-               }
-               case RC_PROTO_TYPE_RANGE: {
-                       struct rc_proto_range *rcr =
-                               (struct rc_proto_range *) recvframe->data;
-
-                       if (datalen != sizeof(struct rc_proto_range))
-                               return -1;
-
-                       if (rcr->power_level >= MAX_POWER_LEVEL)
-                               return -1;
-
-                       rc_proto_rx_range(rcr->power_level);
-
-                       break;
-               }
-#endif
-               case RC_PROTO_HELLO: {
-                       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);
-
-                       break;
-               }
-               default:
-                       return -1;
-       }
-
-       return 0;
-}
-
-/* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
+/* Main xbee rx entry point for application. It decodes the xbee frame type and
+ * dispatch to the application layer. Then "len" argument does not include the
+ * xbee_hdr structure (delimiter, len, type, id) and checksum. */
 int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
             void *frame, unsigned len, void *opaque)
 {
        struct xbee_ctx *ctx = opaque;
-       int8_t ret = 0;
+       int8_t ret = XBEE_USER_RETCODE_OK;
 
-       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);
+       __hexdump(frame, len);
 
        /* 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[0])
+                       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,32 +285,33 @@ 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 */
-                       if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
-                               ret = -1;
+                       if (parse_atcmd(ctx, frame + 10, len - 10) < 0)
+                               ret = XBEE_USER_RETCODE_BAD_FRAME;
 
                        break;
                }
                case XBEE_TYPE_ATRESP: {
-                       if (dump_atcmd(ctx, frame, len) < 0)
-                               ret = -1;
+                       if (parse_atcmd(ctx, frame, len) < 0)
+                               ret = XBEE_USER_RETCODE_BAD_FRAME;
 
                        break;
                }
 
                case XBEE_TYPE_XMIT_STATUS: {
                        if (parse_xmit_status(ctx, frame, len) < 0)
-                               ret = -1;
+                               ret = XBEE_USER_RETCODE_BAD_FRAME;
 
                        break;
                }
 
                case XBEE_TYPE_RECV: {
-                       if (xbee_recv_data(frame, len) < 0)
-                               ret = -1;
+                       if (rc_proto_rx(frame, len) < 0)
+                               ret = XBEE_USER_RETCODE_BAD_FRAME;
 
                        break;
                }
@@ -360,30 +324,29 @@ 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"));
-                       ret = -1;
+                       ERROR(E_USER_XBEE, "Invalid frame");
+                       ret = XBEE_USER_RETCODE_BAD_FRAME;
                        break;
        }
 
-       if (ret < 0)
-               hexdump("undecoded rx frame", frame, len);
-       else if (xbee_hexdump)
-               hexdump("undecoded rx frame", frame, len);
+       if (ret != XBEE_USER_RETCODE_OK) {
+               WARNING(E_USER_XBEE, "undecoded rx frame");
+       }
 
-       /* restart command line if it was a blocking query */
        if (ctx != NULL) {
+               /* callback */
+               if (ctx->rx_cb != NULL)
+                       ret = ctx->rx_cb(ret, frame, len, ctx->arg);
+
+               /* free channel now, it implicitely frees the context too */
                xbee_unregister_channel(dev, channel);
-               if (ctx->foreground) {
-                       xbee_stdin_enable();
-                       rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
-               }
        }
 
        return ret;
 }
 
-static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
-       int foreground)
+/* called with callout prio == XBEE_PRIO */
+static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg)
 {
        int ret;
        int channel;
@@ -392,7 +355,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 +364,13 @@ 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;
        }
@@ -418,21 +378,13 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
        ctx->channel = channel;
        xbee_load_timeout(ctx);   /* load a timeout event */
 
-       /* suspend command line until we have answer or timeout */
-       if (foreground) {
-               ctx->foreground = 1;
-               rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
-               xbee_stdin_disable();  /* unload file descriptor polling */
-       }
-
        return 0;
 }
 
-/* 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(char *atcmd_str, void *param,
-       unsigned param_len, int foreground,
-       int (*func)(void *frame, unsigned len, void *arg), void *arg)
+/* send an AT command with parameters filled by caller. param is the argument
+ * of the atcmd (if any). */
+int xbeeapp_send_atcmd(char *atcmd_str, void *param, unsigned param_len,
+       xbee_user_rx_cb_t *rx_cb, void *arg)
 {
        struct xbee_ctx ctx;
        /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */
@@ -443,23 +395,33 @@ int xbeeapp_send_atcmd(char *atcmd_str, void *param,
        memset(&ctx, 0, sizeof(ctx));
        ctx.atcmd_query[0] = atcmd_str[0];
        ctx.atcmd_query[1] = atcmd_str[1];
-       ctx.func = func;
+       ctx.rx_cb = rx_cb;
        ctx.arg = arg;
 
-       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 (param_len == 0) {
+               msg.iovlen = 1;
+               msg.iov[0].buf = atcmd_str;
+               msg.iov[0].len = 2;
+       }
+       else {
+               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;
+       }
 
        prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
-       ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground);
-       callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
+       if (prio > XBEE_PRIO)
+               ERROR(E_USER_XBEE, "invalid prio = %d\n", prio);
+       ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 
        return ret;
 }
 
-int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
+int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg,
+       xbee_user_rx_cb_t *rx_cb, void *arg)
 {
        struct xbee_ctx ctx;
        struct xbee_xmit_hdr xmit_hdr;
@@ -469,11 +431,10 @@ 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;
        }
 
-
        xmit_hdr.dstaddr = htonll(addr);
        xmit_hdr.reserved = htons(0xFFFE);
        xmit_hdr.bcast_radius = 0;
@@ -486,11 +447,14 @@ int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
                msg2.iov[i+1] = msg->iov[i];
 
        memset(&ctx, 0, sizeof(ctx));
-       ctx.atcmd_query[0] = '\0';
+       ctx.rx_cb = rx_cb;
+       ctx.arg = arg;
 
        prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
-       ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground);
-       callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
+       if (prio > XBEE_PRIO)
+               ERROR(E_USER_XBEE, "invalid prio = %d\n", prio);
+       ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2);
+       callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
 
        return ret;
 }
@@ -503,27 +467,34 @@ 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();
-       rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
+       /* callback */
+       if (ctx->rx_cb != NULL)
+               ctx->rx_cb(XBEE_USER_RETCODE_TIMEOUT, NULL, 0, ctx->arg);
 
-       /* free event */
+       /* free channel, it implicitely frees the context too */
        xbee_unregister_channel(xbee_dev, ctx->channel);
-
        callout_stop(cm, 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,
@@ -539,8 +510,6 @@ void xbee_mainloop(void)
        uint8_t prio;
 
        while (1) {
-               callout_manage(&xbeeboard.mainloop_cm);
-
                if (xbee_raw) {
                        int16_t c;
 
@@ -559,7 +528,7 @@ void xbee_mainloop(void)
                                xbee_dev_send('C', NULL);
                                xbee_dev_send('N', NULL);
                                xbee_dev_send('\n', NULL);
-                               callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
+                               callout_mgr_restore_prio(&xbeeboard.intr_cm, prio);
                                xbee_raw = 0;
                                rdline_newline(&xbeeboard.rdl,
                                               xbeeboard.prompt);
@@ -593,8 +562,8 @@ void xbee_stdin_disable(void)
 
 void xbeeapp_init(void)
 {
-       callout_init(&xbeeboard.xbee_rx_poll_timer, xbee_rx_poll_timer_cb,
+       callout_init(&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);
+       callout_schedule(&xbeeboard.intr_cm, &xbee_rx_poll_timer,
+               XBEE_POLL_TIMER_MS);
 }