beep when GPS ready
[protos/xbee-avr.git] / xbee_user.c
index 859d203..17a92af 100644 (file)
@@ -139,7 +139,8 @@ static int parse_xmit_status(struct xbee_ctx *ctx,
 
 /* 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)
+void atresp_to_str(char *buf, unsigned buflen, const struct xbee_atresp_hdr *frame,
+       unsigned len)
 {
        union {
                uint8_t u8;
@@ -155,31 +156,35 @@ void atresp_to_str(char *buf, unsigned len, const struct xbee_atresp_hdr *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);
+               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;
-       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,
+               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, len, "<%s> is 0x%x (%d)", atcmd_str,
+               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, len, "<%s> is 0x%"PRIx32" (%"PRIu32")",
+               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, len, "<%s> is %d",
+               snprintf(buf, buflen, "<%s> is %d",
                        atcmd_str, ntohs(result->s16));
        else if (len == 0)
-               snprintf(buf, len, "<%s> no data", atcmd_str);
+               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,
@@ -225,10 +230,10 @@ static int parse_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
                return -1;
        }
 
-       len -= offsetof(struct xbee_atresp_hdr, data);
+       atresp_to_str(buf, sizeof(buf), frame, len);
 
-       atresp_to_str(buf, sizeof(buf), frame);
-       NOTICE(E_USER_XBEE, "status ok, len=%d, %s", len, buf);
+       len -= sizeof(*frame);
+       NOTICE(E_USER_XBEE, "status ok, datalen=%d, %s", len, buf);
 
        if (len != 0)
                hexdump("atcmd answer", frame->data, len);
@@ -237,7 +242,9 @@ static int parse_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
 }
 
 
-/* main rx entry point for application */
+/* 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)
 {
@@ -246,11 +253,12 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
 
        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) {
                xbee_unload_timeout(ctx);
-               if (ctx->atcmd_query)
+               if (ctx->atcmd_query[0])
                        NOTICE(E_USER_XBEE, "Received answer to query <%c%c>",
                                ctx->atcmd_query[0], ctx->atcmd_query[1]);
        }
@@ -321,8 +329,9 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
                        break;
        }
 
-       WARNING(E_USER_XBEE, "undecoded rx frame");
-       hexdump("undecoded rx frame", frame, len);
+       if (ret != XBEE_USER_RETCODE_OK) {
+               WARNING(E_USER_XBEE, "undecoded rx frame");
+       }
 
        if (ctx != NULL) {
                /* callback */
@@ -389,11 +398,18 @@ int xbeeapp_send_atcmd(char *atcmd_str, void *param, unsigned param_len,
        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);
        if (prio > XBEE_PRIO)