2 * Copyright (c) 2014, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <aversive/endian.h>
37 #include <aversive/pgmspace.h>
42 #include <xbee_rxtx.h>
45 #include "xbee_user.h"
48 #define XBEE_TIMEOUT_MS 1000
49 #define XBEE_POLL_TIMER_MS 5
51 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
53 int xbee_cmdline_input_enabled = 1;
60 static void __hexdump(const void *buf, unsigned int len)
62 unsigned int i, out, ofs;
63 const unsigned char *data = buf;
65 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
69 /* format 1 line in the buffer, then use printk to print them */
70 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
71 for (i=0; ofs+i < len && i<16; i++)
72 out += snprintf_P(line+out, LINE_LEN - out,
76 out += snprintf(line+out, LINE_LEN - out, " ");
77 for (i=0; ofs < len && i<16; i++, ofs++) {
78 unsigned char c = data[ofs];
79 if (!isascii(c) || !isprint(c))
81 out += snprintf_P(line+out,
85 printf_P(PSTR("%s\r\n"), line);
89 static void hexdump_msg(const char *title, const struct xbee_msg *msg)
93 printf_P(PSTR("dump %s\r\n"), title);
94 for (i = 0; i < msg->iovlen; i++) {
95 printf_P(PSTR("iovec %d at %p, len=%d\r\n"), i,
96 msg->iov[i].buf, msg->iov[i].len);
97 __hexdump(msg->iov[i].buf, msg->iov[i].len);
101 static void hexdump(const char *title, const void *buf, unsigned int len)
103 printf_P(PSTR("dump %s at [%p], len=%d\r\n"), title, buf, len);
107 static int parse_xmit_status(struct xbee_ctx *ctx,
108 struct xbee_xmit_status_hdr *frame, unsigned len)
113 printf_P(PSTR("no context\r\n"));
117 /* see if it matches a xmit query (atcmd_query must be NULL) */
118 if (ctx->atcmd_query[0] != '\0') {
119 printf_P(PSTR("invalid response 2\r\n"));
123 /* XXX use defines for these values */
124 if (frame->delivery_status == 0x00)
125 printf_P(PSTR("Success\r\n"));
126 else if (frame->delivery_status == 0x01)
127 printf_P(PSTR("MAC ACK Failure\r\n"));
128 else if (frame->delivery_status == 0x15)
129 printf_P(PSTR("Invalid destination endpoint\r\n"));
130 else if (frame->delivery_status == 0x21)
131 printf_P(PSTR("Network ACK Failure\r\n"));
132 else if (frame->delivery_status == 0x25)
133 printf_P(PSTR("Route Not Found\r\n"));
138 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
142 const struct xbee_atcmd *cmd_pgm;
143 struct xbee_atcmd cmd;
149 } __attribute__((packed)) *result;
152 printf_P(PSTR("no context\r\n"));
156 /* get AT command from frame */
157 memcpy(atcmd_str, &frame->cmd, 2);
160 /* see if it matches query */
161 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
162 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
164 (frame->cmd >> 8) & 0xFF,
165 atcmd_str, ctx->atcmd_query);
169 /* see if it exists */
170 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
171 if (cmd_pgm == NULL) {
172 printf_P(PSTR("unknown response\r\n"));
175 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
178 if (frame->status == 1) {
179 printf_P(PSTR("Status is error\r\n"));
182 else if (frame->status == 2) {
183 printf_P(PSTR("Invalid command\r\n"));
186 else if (frame->status == 3) {
187 printf_P(PSTR("Invalid parameter\r\n"));
190 else if (frame->status != 0) {
191 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
196 if (ctx->func != NULL)
197 ctx->func(frame, len, ctx->arg);
200 result = (void *)frame->data;
201 len -= offsetof(struct xbee_atresp_hdr, data);
202 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
203 printf_P(PSTR("<%s> is 0x%x (%d)\r\n"), atcmd_str, result->u8,
205 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
206 printf_P(PSTR("<%s> is 0x%x (%d)\r\n"),
208 ntohs(result->u16), ntohs(result->u16));
209 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
210 printf_P(PSTR("<%s> is 0x%"PRIx32" (%"PRIu32")\r\n"),
212 ntohl(result->u32), ntohs(result->u32));
213 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
214 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
216 printf_P(PSTR("no data, status ok\r\n"));
218 hexdump("atcmd answer", frame->data, len);
225 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
227 unsigned int datalen;
228 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
230 if (len < sizeof(*recvframe))
233 datalen = len - sizeof(*recvframe);
234 if (datalen < sizeof(struct rc_proto_hdr))
239 case RC_PROTO_TYPE_CHANNEL: {
240 struct rc_proto_channel *rcc =
241 (struct rc_proto_channel *) recvframe->data;
243 if (datalen != sizeof(struct rc_proto_channel))
245 val = ntohs(rcc->axis[0]);
248 spi_servo_set(0, val);
251 case RC_PROTO_TYPE_RANGE: {
252 struct rc_proto_range *rcr =
253 (struct rc_proto_range *) recvframe->data;
255 if (datalen != sizeof(struct rc_proto_range))
258 if (rcr->power_level >= MAX_POWER_LEVEL)
261 rc_proto_rx_range(rcr->power_level);
266 case RC_PROTO_HELLO: {
267 struct rc_proto_hello *rch =
268 (struct rc_proto_hello *) recvframe->data;
271 printf_P(PSTR("recv hello len=%d\r\n"),
283 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
284 int8_t xbeeapp_rx(struct xbee_dev *dev, int channel, int type,
285 void *frame, unsigned len, void *opaque)
287 struct xbee_ctx *ctx = opaque;
291 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
294 /* if ctx is !NULL, it is an answer to a query */
296 /* XXX only delete timeout if answer matched */
297 xbee_unload_timeout(ctx);
298 if (xbee_debug && ctx->atcmd_query)
299 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
300 ctx->atcmd_query[0], ctx->atcmd_query[1]);
303 /* some additional checks before sending */
305 case XBEE_TYPE_MODEM_STATUS: {
306 printf_P(PSTR("Received Modem Status frame\r\n"));
310 case XBEE_TYPE_RMT_ATRESP: {
314 #if BYTE_ORDER == LITTLE_ENDIAN
323 memcpy(&addr, frame, sizeof(addr));
324 addr.u64 = ntohll(addr.u64);
325 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
326 addr.u32.high, addr.u32.low);
328 /* this answer contains an atcmd answer at offset 10 */
329 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
334 case XBEE_TYPE_ATRESP: {
335 if (dump_atcmd(ctx, frame, len) < 0)
341 case XBEE_TYPE_XMIT_STATUS: {
342 if (parse_xmit_status(ctx, frame, len) < 0)
348 case XBEE_TYPE_RECV: {
349 if (xbee_recv_data(frame, len) < 0)
355 case XBEE_TYPE_ATCMD:
356 case XBEE_TYPE_ATCMD_Q:
358 case XBEE_TYPE_EXPL_XMIT:
359 case XBEE_TYPE_RMT_ATCMD:
360 case XBEE_TYPE_EXPL_RECV:
361 case XBEE_TYPE_NODE_ID:
363 printf_P(PSTR("Invalid frame\r\n"));
369 hexdump("undecoded rx frame", frame, len);
370 else if (xbee_hexdump)
371 hexdump("undecoded rx frame", frame, len);
373 /* restart command line if it was a blocking query */
375 xbee_unregister_channel(dev, channel);
376 if (ctx->foreground) {
378 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
385 static int xbeeapp_send(struct xbee_ctx *ctx, int type, struct xbee_msg *msg,
391 /* register a channel */
392 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
395 printf_P(PSTR("cannot send: no free channel\r\n"));
399 /* copy context in the static struct table (avoiding a malloc) */
400 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
401 ctx = &xbee_ctx[channel];
402 xbee_set_opaque(xbee_dev, channel, ctx);
405 printf_P(PSTR("send frame channel=%d type=0x%x\r\n"),
408 hexdump_msg("xmit frame", msg);
410 /* transmit the frame on this channel */
411 ret = xbee_tx_iovec(xbee_dev, channel, type, msg);
413 printf_P(PSTR("cannot send\r\n"));
414 xbee_unregister_channel(xbee_dev, channel);
418 ctx->channel = channel;
419 xbee_load_timeout(ctx); /* load a timeout event */
421 /* suspend command line until we have answer or timeout */
424 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
425 xbee_stdin_disable(); /* unload file descriptor polling */
431 /* send an AT command with parameters filled by caller. Disable
432 * command line until we get the answer or until a timeout occurs */
433 int xbeeapp_send_atcmd(char *atcmd_str, void *param,
434 unsigned param_len, int foreground,
435 int (*func)(void *frame, unsigned len, void *arg), void *arg)
438 /* struct xbee_atcmd_hdr atcmd_hdr; -> no needed same than atcmd_str */
443 memset(&ctx, 0, sizeof(ctx));
444 ctx.atcmd_query[0] = atcmd_str[0];
445 ctx.atcmd_query[1] = atcmd_str[1];
450 msg.iov[0].buf = atcmd_str;
452 msg.iov[1].buf = param;
453 msg.iov[1].len = param_len;
455 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
456 ret = xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &msg, foreground);
457 callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
462 int xbeeapp_send_msg(uint64_t addr, struct xbee_msg *msg, int foreground)
465 struct xbee_xmit_hdr xmit_hdr;
466 struct xbee_msg msg2;
471 if (msg->iovlen + 2 > XBEE_MSG_MAXIOV) {
472 printf_P(PSTR("too many iovecs\r\n"));
477 xmit_hdr.dstaddr = htonll(addr);
478 xmit_hdr.reserved = htons(0xFFFE);
479 xmit_hdr.bcast_radius = 0;
482 msg2.iovlen = msg->iovlen + 1;
483 msg2.iov[0].buf = &xmit_hdr;
484 msg2.iov[0].len = sizeof(xmit_hdr);
485 for (i = 0; i < msg->iovlen; i++)
486 msg2.iov[i+1] = msg->iov[i];
488 memset(&ctx, 0, sizeof(ctx));
489 ctx.atcmd_query[0] = '\0';
491 prio = callout_mgr_set_prio(&xbeeboard.intr_cm, XBEE_PRIO);
492 ret = xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &msg2, foreground);
493 callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
498 static void evt_timeout(struct callout_mgr *cm, struct callout *clt,
501 struct xbee_ctx *ctx = arg;
506 printf_P(PSTR("Timeout\r\n"));
508 /* restart command line */
510 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
513 xbee_unregister_channel(xbee_dev, ctx->channel);
515 callout_stop(cm, clt);
518 void xbee_load_timeout(struct xbee_ctx *ctx)
520 callout_init(&ctx->timeout, evt_timeout, ctx, 0);
521 callout_schedule(&xbeeboard.mainloop_cm, &ctx->timeout, XBEE_TIMEOUT_MS);
524 void xbee_unload_timeout(struct xbee_ctx *ctx)
526 callout_stop(&xbeeboard.mainloop_cm, &ctx->timeout);
529 static void xbee_rx_poll_timer_cb(struct callout_mgr *cm, struct callout *tim,
534 callout_reschedule(cm, tim, XBEE_POLL_TIMER_MS);
537 void xbee_mainloop(void)
542 callout_manage(&xbeeboard.mainloop_cm);
547 /* from xbee to cmdline */
548 c = xbee_dev_recv(NULL);
550 cmdline_dev_send((uint8_t)c, NULL);
552 /* from cmdline to xbee */
553 c = cmdline_dev_recv(NULL);
554 if (c == 4) { /* CTRL-d */
555 prio = callout_mgr_set_prio(&xbeeboard.intr_cm,
557 xbee_dev_send('A', NULL);
558 xbee_dev_send('T', NULL);
559 xbee_dev_send('C', NULL);
560 xbee_dev_send('N', NULL);
561 xbee_dev_send('\n', NULL);
562 callout_mgr_set_prio(&xbeeboard.intr_cm, prio);
564 rdline_newline(&xbeeboard.rdl,
569 xbee_dev_send((uint8_t)c, NULL);
571 /* echo on cmdline */
572 cmdline_dev_send((uint8_t)c, NULL);
576 if (xbee_cmdline_input_enabled)
578 /* xbee rx polling is done in a timer, so we can block
579 * the cmdline without loosing incoming packets */
584 void xbee_stdin_enable(void)
586 xbee_cmdline_input_enabled = 1;
589 void xbee_stdin_disable(void)
591 xbee_cmdline_input_enabled = 0;
594 void xbeeapp_init(void)
596 callout_init(&xbeeboard.xbee_rx_poll_timer, xbee_rx_poll_timer_cb,
598 callout_schedule(&xbeeboard.intr_cm,
599 &xbeeboard.xbee_rx_poll_timer, XBEE_POLL_TIMER_MS);