rework xbee_user: remove the foreground param
[protos/xbee-avr.git] / xbee_user.c
index 0d264b5..fca39b6 100644 (file)
@@ -81,7 +81,7 @@ static void __hexdump(const void *buf, unsigned int len)
                                          LINE_LEN - out,
                                          PSTR("%c"), c);
                }
-               DEBUG(E_USER_XBEE, "%s");
+               DEBUG(E_USER_XBEE, "%s", line);
        }
 }
 
@@ -90,6 +90,7 @@ 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);
@@ -97,7 +98,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)
+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);
@@ -113,7 +114,7 @@ static int parse_xmit_status(struct xbee_ctx *ctx,
                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') {
                ERROR(E_USER_XBEE, "invalid response 2");
                return -1;
@@ -134,18 +135,56 @@ static int parse_xmit_status(struct xbee_ctx *ctx,
        return 0;
 }
 
-static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
-       unsigned len)
+/* 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 len, const struct xbee_atresp_hdr *frame)
 {
-       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, len, "<%s> (unknown cmd)", atcmd_str);
+               return;
+       }
+       memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
+
+       /* 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))
+               snprintf(buf, len, "<%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, len, "<%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, len, "<%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, len, "<%s> is %d",
+                       atcmd_str, ntohs(result->s16));
+       else if (len == 0)
+               snprintf(buf, len, "<%s> no data", atcmd_str);
+}
+
+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) {
                ERROR(E_USER_XBEE, "no context");
@@ -165,14 +204,6 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
                return -1;
        }
 
-       /* see if it exists */
-       cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
-       if (cmd_pgm == NULL) {
-               ERROR(E_USER_XBEE, "unknown response");
-               return -1;
-       }
-       memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
-
        /* bad status */
        if (frame->status == 1) {
                WARNING(E_USER_XBEE, "Status is error");
@@ -192,28 +223,12 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
                return -1;
        }
 
-       /* callback */
-       if (ctx->func != NULL)
-               ctx->func(frame, len, ctx->arg);
-
-       /* 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))
-               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))
-               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))
-               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))
-               NOTICE(E_USER_XBEE, "<%s> is %d",
-                       atcmd_str, ntohs(result->s16));
-       else if (len == 0)
-               NOTICE(E_USER_XBEE, "no data, status ok");
-       else
+
+       atresp_to_str(buf, sizeof(buf), frame);
+       NOTICE(E_USER_XBEE, "status ok, len=%d, %s", len, buf);
+
+       if (len != 0)
                hexdump("atcmd answer", frame->data, len);
 
        return 0;
@@ -246,21 +261,22 @@ int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
                        spi_servo_set(0, val);
                        break;
                }
-               case RC_PROTO_TYPE_RANGE: {
-                       struct rc_proto_range *rcr =
-                               (struct rc_proto_range *) recvframe->data;
+#endif
+               case RC_PROTO_POWER_PROBE: {
+                       struct rc_proto_power_probe *rcpb =
+                               (struct rc_proto_power_probe *) recvframe->data;
 
-                       if (datalen != sizeof(struct rc_proto_range))
+                       if (datalen != sizeof(*rcpb))
                                return -1;
 
-                       if (rcr->power_level >= MAX_POWER_LEVEL)
+                       if (rcpb->power_level >= MAX_POWER_LEVEL)
                                return -1;
 
-                       rc_proto_rx_range(rcr->power_level);
+                       //rc_proto_rx_range(rcpb->power_level);
 
                        break;
                }
-#endif
+
                case RC_PROTO_HELLO: {
                        struct rc_proto_hello *rch =
                                (struct rc_proto_hello *) recvframe->data;
@@ -277,19 +293,18 @@ int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
        return 0;
 }
 
-/* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
+/* main rx entry point for application */
 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;
 
        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 (ctx->atcmd_query)
                        NOTICE(E_USER_XBEE, "Received answer to query <%c%c>",
@@ -318,32 +333,33 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
                        } addr;
                        memcpy(&addr, frame, sizeof(addr));
                        addr.u64 = ntohll(addr.u64);
-                       NOTICE(E_USER_XBEE, "from remote address %"PRIx32"%"PRIx32"",
+                       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;
+                               ret = XBEE_USER_RETCODE_BAD_FRAME;
 
                        break;
                }
@@ -357,27 +373,27 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
                case XBEE_TYPE_NODE_ID:
                default:
                        ERROR(E_USER_XBEE, "Invalid frame");
-                       ret = -1;
+                       ret = XBEE_USER_RETCODE_BAD_FRAME;
                        break;
        }
 
        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) {
+               /* 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;
@@ -395,8 +411,7 @@ 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);
 
-       NOTICE(E_USER_XBEE, "send frame channel=%d type=0x%x",
-               channel, type);
+       NOTICE(E_USER_XBEE, "send frame channel=%d type=0x%x", channel, type);
        hexdump_msg("xmit frame", msg);
 
        /* transmit the frame on this channel */
@@ -410,21 +425,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 */
@@ -435,7 +442,7 @@ 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;
@@ -445,13 +452,16 @@ int xbeeapp_send_atcmd(char *atcmd_str, void *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);
+       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;
@@ -465,7 +475,6 @@ int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
                return -1;
        }
 
-
        xmit_hdr.dstaddr = htonll(addr);
        xmit_hdr.reserved = htons(0xFFFE);
        xmit_hdr.bcast_radius = 0;
@@ -478,10 +487,13 @@ 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);
+       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;
@@ -497,13 +509,12 @@ static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
 
        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);
 }