if (monitor_current == NULL)
monitor_current = LIST_FIRST(&xbee_monitor_list);
- xbeeapp_send_atcmd(monitor_current->atcmd, NULL, 0, 0, NULL, NULL);
+ /* no rx_cb given: the user must check the monitored values in logs */
+ xbeeapp_send_atcmd(monitor_current->atcmd, NULL, 0, NULL, NULL);
monitor_current = LIST_NEXT(monitor_current, next);
callout_reschedule(cm, clt, monitor_period_ms / monitor_count);
}
callout_reschedule(cm, clt, range_period_ms);
#endif
+/* callback invoked when a xbee_send is done */
+static int8_t send_msg_cb(int8_t retcode, void *frame, unsigned len,
+ void *arg)
+{
+ struct xbee_recv_hdr *recvframe = frame;
+ uint8_t *done = arg;
+
+ *done = 1;
+ if (retcode == XBEE_USER_RETCODE_TIMEOUT) {
+ printf_P(PSTR("timeout\r\n"));
+ return retcode;
+ }
+ if (retcode == XBEE_USER_RETCODE_BAD_FRAME ||
+ len < sizeof(*recvframe)) {
+ printf_P(PSTR("invalid frame\r\n"));
+ return XBEE_USER_RETCODE_BAD_FRAME;
+ }
+
+ printf_P(PSTR("ok\r\n"));
+ return XBEE_USER_RETCODE_OK;
+}
+
+/* callback invoked to dump the response to AT command */
+static int8_t dump_xbee_atresp_cb(int8_t retcode, void *frame, unsigned len,
+ void *arg)
+{
+ struct xbee_atresp_hdr *recvframe = frame;
+ char atcmd_str[3];
+ char buf[32];
+ uint8_t *done = arg;
+
+ *done = 1;
+ if (retcode == XBEE_USER_RETCODE_TIMEOUT) {
+ printf_P(PSTR("timeout\r\n"));
+ return retcode;
+ }
+ if (retcode == XBEE_USER_RETCODE_BAD_FRAME ||
+ len < sizeof(*recvframe)) {
+ printf_P(PSTR("invalid frame\r\n"));
+ return XBEE_USER_RETCODE_BAD_FRAME;
+ }
+
+ /* get AT command from frame */
+ memcpy(atcmd_str, &recvframe->cmd, 2);
+ atcmd_str[2] = '\0';
+
+ len -= sizeof(*recvframe);
+ atresp_to_str(buf, sizeof(buf), frame);
+ NOTICE(E_USER_XBEE, "status ok, len=%d, %s", len, buf);
+ return XBEE_USER_RETCODE_OK;
}
/* this structure is filled when cmd_help is parsed successfully */
},
};
-
-
-
/* ************* */
/* this structure is filled when cmd_read is parsed successfully */
struct cmd_read_result *res = parsed_result;
struct xbee_atcmd copy;
char cmd[3];
+ volatile uint8_t done = 0;
(void)data;
memcpy_P(©, res->cmd, sizeof(copy));
memcpy_P(&cmd, copy.name, 2);
cmd[2] = '\0';
- xbeeapp_send_atcmd(cmd, NULL, 0, 1, NULL, NULL);
+ xbeeapp_send_atcmd(cmd, NULL, 0, dump_xbee_atresp_cb, (void *)&done);
+ while (done == 0);
}
const char PROGMEM str_read_read[] = "read";
char cmd[3];
int len;
void *param;
+ volatile uint8_t done = 0;
(void)data;
memcpy_P(©, res->cmd, sizeof(copy));
}
memcpy_P(&cmd, copy.name, 2);
cmd[2] = '\0';
- xbeeapp_send_atcmd(cmd, param, len, 1, NULL, NULL);
+ xbeeapp_send_atcmd(cmd, param, len, dump_xbee_atresp_cb, (void *)&done);
+ while (done == 0);
}
const char PROGMEM str_write_none[] = "write";
{
struct cmd_sendmsg_result *res = parsed_result;
struct xbee_msg msg;
+ volatile uint8_t done = 0;
(void)data;
msg.iov[0].buf = res->data;
msg.iov[0].len = strlen(res->data);
- xbeeapp_send_msg(res->addr, &msg, 1);
+ xbeeapp_send_msg(res->addr, &msg, send_msg_cb, (void *)&done);
+ while (done == 0);
}
const char PROGMEM str_sendmsg[] = "sendmsg";
{
struct cmd_sendmsg_name_result *res = parsed_result;
struct xbee_msg msg;
+ volatile uint8_t done = 0;
(void)data;
msg.iov[0].buf = res->data;
msg.iov[0].len = strlen(res->data);
- xbeeapp_send_msg(res->neigh->addr, &msg, 1);
+ xbeeapp_send_msg(res->neigh->addr, &msg, send_msg_cb, (void *)&done);
+ while (done == 0);
}
const parse_token_string_t PROGMEM cmd_sendmsg_name_sendmsg_name =
/* function called when cmd_ping is parsed successfully */
static void cmd_ping_parsed(void *parsed_result, void *data)
{
+ volatile uint8_t done = 0;
+
(void)parsed_result;
(void)data;
- xbeeapp_send_atcmd("VL", NULL, 0, 1, NULL, NULL);
+ xbeeapp_send_atcmd("VL", NULL, 0, dump_xbee_atresp_cb, (void *)&done);
+ while (done == 0);
}
const char PROGMEM str_ping[] = "ping";
LINE_LEN - out,
PSTR("%c"), c);
}
- DEBUG(E_USER_XBEE, "%s");
+ DEBUG(E_USER_XBEE, "%s", line);
}
}
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);
}
}
-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);
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;
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");
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");
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;
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;
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>",
} 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;
}
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;
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 */
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 */
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[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;
return -1;
}
-
xmit_hdr.dstaddr = htonll(addr);
xmit_hdr.reserved = htons(0xFFFE);
xmit_hdr.bcast_radius = 0;
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;
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);
}