X-Git-Url: http://git.droids-corp.org/?p=protos%2Fxbee-avr.git;a=blobdiff_plain;f=xbee_user.c;h=17a92afa68952b3a13a69a8837c5ea126594b8fe;hp=0d264b52b4feee2c8e995bad4084420ecee10ded;hb=HEAD;hpb=852fd37145aaaee0ce2117f5c8173f5b86020fd0 diff --git a/xbee_user.c b/xbee_user.c index 0d264b5..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; @@ -81,7 +83,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 +92,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 +100,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 +116,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 +137,61 @@ static int parse_xmit_status(struct xbee_ctx *ctx, 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) { ERROR(E_USER_XBEE, "no context"); @@ -165,14 +211,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,106 +230,35 @@ 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); + 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)) - 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 + 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; - - NOTICE(E_USER_XBEE, "recv hello len=%d", - rch->datalen); - /* XXX stats */ - 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; 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 (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]); } @@ -318,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); - 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; + if (rc_proto_rx(frame, len) < 0) + ret = XBEE_USER_RETCODE_BAD_FRAME; break; } @@ -357,27 +325,28 @@ 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); + 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; @@ -395,8 +364,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 +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 */ @@ -435,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); + 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 +435,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 +447,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 +469,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); } @@ -591,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); }