X-Git-Url: http://git.droids-corp.org/?p=protos%2Fxbee-avr.git;a=blobdiff_plain;f=xbee_user.c;h=17a92afa68952b3a13a69a8837c5ea126594b8fe;hp=fca39b65cd96c406b3a4c71daa40bd46a946f848;hb=0704ac13884035e9124507092664910f9936bb5d;hpb=e39c5d534eaa4d29fea56d7851343612fdab9a23 diff --git a/xbee_user.c b/xbee_user.c index fca39b6..17a92af 100644 --- a/xbee_user.c +++ b/xbee_user.c @@ -56,6 +56,8 @@ int xbee_cmdline_input_enabled = 1; /* parameters */ int xbee_raw = 0; +static struct callout xbee_rx_poll_timer; + static void __hexdump(const void *buf, unsigned int len) { unsigned int i, out, ofs; @@ -137,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; @@ -153,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, @@ -223,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); @@ -235,65 +242,9 @@ static int parse_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame, } -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; - } -#endif - case RC_PROTO_POWER_PROBE: { - struct rc_proto_power_probe *rcpb = - (struct rc_proto_power_probe *) recvframe->data; - - if (datalen != sizeof(*rcpb)) - return -1; - - if (rcpb->power_level >= MAX_POWER_LEVEL) - return -1; - - //rc_proto_rx_range(rcpb->power_level); - - break; - } - - case RC_PROTO_HELLO: { - struct rc_proto_hello *rch = - (struct rc_proto_hello *) recvframe->data; - - NOTICE(E_USER_XBEE, "recv hello len=%d", - rch->datalen); - /* XXX stats */ - break; - } - default: - return -1; - } - - return 0; -} - -/* 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) { @@ -302,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]); } @@ -358,7 +310,7 @@ int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type, } case XBEE_TYPE_RECV: { - if (xbee_recv_data(frame, len) < 0) + if (rc_proto_rx(frame, len) < 0) ret = XBEE_USER_RETCODE_BAD_FRAME; break; @@ -377,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 */ @@ -445,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) @@ -602,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); }