2 * Copyright (c) 2011, 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.
29 #include <aversive/queue.h>
30 #include <aversive/endian.h>
31 #include <aversive/wait.h>
32 #include <aversive/error.h>
45 #include <scheduler.h>
46 #include <clock_time.h>
51 #include "xbee_neighbor.h"
52 #include "xbee_atcmd.h"
53 #include "xbee_stats.h"
55 #include "xbee_proto.h"
62 struct xbeeboard xbeeboard;
64 #define TIMEOUT_MS 1000
66 /* global xbee device */
67 struct xbee_dev *xbee_dev;
70 //static struct event stdin_read_event, xbee_read_event;
77 int xbee_cmdline_input_enabled = 1;
79 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
81 static void hexdump(const char *title, const void *buf, unsigned int len)
83 unsigned int i, out, ofs;
84 const unsigned char *data = buf;
86 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
88 printf_P(PSTR("%s at [%p], len=%d\n"), title, data, len);
91 /* format 1 line in the buffer, then use printk to print them */
92 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
93 for (i=0; ofs+i < len && i<16; i++)
94 out += snprintf_P(line+out, LINE_LEN - out,
98 out += snprintf(line+out, LINE_LEN - out, " ");
99 for (i=0; ofs < len && i<16; i++, ofs++) {
100 unsigned char c = data[ofs];
101 if (!isascii(c) || !isprint(c))
103 out += snprintf_P(line+out,
107 printf_P(PSTR("%s\r\n"), line);
111 static int parse_xmit_status(struct xbee_ctx *ctx,
112 struct xbee_xmit_status_hdr *frame, unsigned len)
115 printf_P(PSTR("no context\r\n"));
119 /* see if it matches a xmit query (atcmd_query must be NULL) */
120 if (ctx->atcmd_query[0] != '\0') {
121 printf_P(PSTR("invalid response 2\r\n"));
125 /* XXX use defines for these values */
126 if (frame->delivery_status == 0x00)
127 printf_P(PSTR("Success\r\n"));
128 else if (frame->delivery_status == 0x01)
129 printf_P(PSTR("MAC ACK Failure\r\n"));
130 else if (frame->delivery_status == 0x15)
131 printf_P(PSTR("Invalid destination endpoint\r\n"));
132 else if (frame->delivery_status == 0x21)
133 printf_P(PSTR("Network ACK Failure\r\n"));
134 else if (frame->delivery_status == 0x25)
135 printf_P(PSTR("Route Not Found\r\n"));
140 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
144 struct xbee_atcmd_pgm *cmd_pgm;
145 struct xbee_atcmd cmd;
151 } __attribute__((packed)) *result;
154 printf_P(PSTR("no context\r\n"));
158 /* get AT command from frame */
159 memcpy(atcmd_str, &frame->cmd, 2);
162 /* see if it matches query */
163 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
164 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
166 (frame->cmd >> 8) & 0xFF,
167 atcmd_str, ctx->atcmd_query);
171 /* see if it exists */
172 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
173 if (cmd_pgm == NULL) {
174 printf_P(PSTR("unknown response\r\n"));
177 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
180 if (frame->status == 1) {
181 printf_P(PSTR("Status is error\r\n"));
184 else if (frame->status == 2) {
185 printf_P(PSTR("Invalid command\r\n"));
188 else if (frame->status == 3) {
189 printf_P(PSTR("Invalid parameter\r\n"));
192 else if (frame->status != 0) {
193 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
198 if (ctx->func != NULL)
199 ctx->func(frame, len, ctx->arg);
202 result = (void *)frame->data;
203 len -= offsetof(struct xbee_atresp_hdr, data);
204 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
205 printf_P(PSTR("<%s> is 0x%x\r\n"), atcmd_str, result->u8);
206 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
207 printf_P(PSTR("<%s> is 0x%x\r\n"),
210 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
211 printf_P(PSTR("<%s> is 0x%"PRIx32"\r\n"),
214 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
215 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
217 printf_P(PSTR("no data, status ok\r\n"));
219 hexdump("atcmd answer", frame->data, len);
226 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
228 int datalen = len - sizeof(*recvframe);
229 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
231 if (datalen < sizeof(struct rc_proto_hdr))
235 case RC_PROTO_TYPE_CHANNEL:
236 if (datalen != sizeof(struct rc_proto_channel))
239 case RC_PROTO_TYPE_RANGE: {
240 struct rc_proto_range *rcr =
241 (struct rc_proto_range *) recvframe->data;
243 if (datalen != sizeof(struct rc_proto_range))
246 if (rcr->power_level >= MAX_POWER_LEVEL)
249 rc_proto_rx_range(rcr->power_level);
260 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
261 void xbee_rx(struct xbee_dev *dev, int channel, int type,
262 void *frame, unsigned len, void *opaque)
264 struct xbee_ctx *ctx = opaque;
265 int do_hexdump = xbee_hexdump;
268 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
271 /* if ctx is !NULL, it is an answer to a query */
273 /* XXX only delete timeout if answer matched */
274 xbee_unload_timeout(ctx);
275 if (xbee_debug && ctx->atcmd_query)
276 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
277 ctx->atcmd_query[0], ctx->atcmd_query[1]);
280 /* some additional checks before sending */
282 case XBEE_TYPE_MODEM_STATUS: {
283 printf_P(PSTR("Received Modem Status frame\r\n"));
287 case XBEE_TYPE_RMT_ATRESP: {
291 #if BYTE_ORDER == LITTLE_ENDIAN
300 memcpy(&addr, frame, sizeof(addr));
301 addr.u64 = ntohll(addr.u64);
302 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
303 addr.u32.high, addr.u32.low);
305 /* this answer contains an atcmd answer at offset 10 */
306 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
310 case XBEE_TYPE_ATRESP: {
311 if (dump_atcmd(ctx, frame, len) < 0)
316 case XBEE_TYPE_XMIT_STATUS: {
317 if (parse_xmit_status(ctx, frame, len) < 0)
322 case XBEE_TYPE_RECV: {
323 if (xbee_recv_data(frame, len) < 0)
328 case XBEE_TYPE_ATCMD:
329 case XBEE_TYPE_ATCMD_Q:
331 case XBEE_TYPE_EXPL_XMIT:
332 case XBEE_TYPE_RMT_ATCMD:
333 case XBEE_TYPE_EXPL_RECV:
334 case XBEE_TYPE_NODE_ID:
336 printf_P(PSTR("Invalid frame\r\n"));
342 hexdump("undecoded rx frame", frame, len);
344 /* restart command line if it was a blocking query */
346 xbee_unregister_channel(dev, channel);
347 if (ctx->foreground) {
349 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
354 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
360 if (len > XBEE_MAX_FRAME_LEN) {
361 printf_P(PSTR("frame too large\r\n"));
365 /* register a channel */
366 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
369 printf_P(PSTR("cannot send: no free channel\r\n"));
373 /* copy context in the static struct table (avoiding a malloc) */
374 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
375 ctx = &xbee_ctx[channel];
376 xbee_set_opaque(xbee_dev, channel, ctx);
379 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
382 hexdump("xmit frame", buf, len);
384 /* transmit the frame on this channel */
385 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
388 printf_P(PSTR("cannot send\r\n"));
389 xbee_unregister_channel(xbee_dev, channel);
393 ctx->channel = channel;
394 xbee_load_timeout(ctx); /* load a timeout event */
396 /* suspend command line until we have answer or timeout */
399 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
400 xbee_stdin_disable(); /* unload file descriptor polling */
406 /* send an AT command with parameters filled by caller. Disable
407 * command line until we get the answer or until a timeout occurs */
408 int xbeeapp_send_atcmd(const char *atcmd_str,
409 void *param, unsigned param_len, int foreground,
410 int (*func)(void *frame, unsigned len, void *arg), void *arg)
414 struct xbee_atcmd_hdr atcmd;
415 char buf[XBEE_MAX_FRAME_LEN];
416 } __attribute__((packed)) frame;
418 memset(&ctx, 0, sizeof(ctx));
419 ctx.atcmd_query[0] = atcmd_str[0];
420 ctx.atcmd_query[1] = atcmd_str[1];
424 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
425 memcpy(&frame.buf, param, param_len);
427 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
428 sizeof(struct xbee_atcmd_hdr) +
429 param_len, foreground) < 0) {
436 int xbeeapp_send_msg(uint64_t addr, void *data,
437 unsigned data_len, int foreground)
441 struct xbee_xmit_hdr xmit;
442 char buf[XBEE_MAX_FRAME_LEN];
443 } __attribute__((packed)) frame;
445 memset(&ctx, 0, sizeof(ctx));
446 ctx.atcmd_query[0] = '\0';
448 frame.xmit.dstaddr = htonll(addr);
449 frame.xmit.reserved = htons(0xFFFE);
450 frame.xmit.bcast_radius = 0;
452 memcpy(&frame.buf, data, data_len);
454 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
455 sizeof(struct xbee_xmit_hdr) +
456 data_len, foreground) < 0) {
463 void xbee_stdin_enable(void)
465 xbee_cmdline_input_enabled = 1;
468 void xbee_stdin_disable(void)
470 xbee_cmdline_input_enabled = 0;
473 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
476 struct xbee_ctx *ctx = arg;
478 printf_P(PSTR("Timeout\r\n"));
480 /* restart command line */
482 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
485 xbee_unregister_channel(xbee_dev, ctx->channel);
488 void xbee_load_timeout(struct xbee_ctx *ctx)
490 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
493 void xbee_unload_timeout(struct xbee_ctx *ctx)
495 callout_stop(&cm, &ctx->timeout);
498 void bootloader(void)
500 #define BOOTLOADER_ADDR 0x1e000
501 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
502 printf_P(PSTR("Bootloader is not present\r\n"));
506 /* ... very specific :( */
513 __asm__ __volatile__ ("ldi r31,0xf0\n");
514 __asm__ __volatile__ ("ldi r30,0x00\n");
515 __asm__ __volatile__ ("ijmp\n");
520 void xbee_mainloop(void)
528 /* from xbee to cmdline */
529 c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
531 CDC_Device_SendByte(&VirtualSerial1_CDC_Interface,
534 /* from cmdline to xbee */
535 c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
536 if (c == 4) { /* CTRL-d */
538 rdline_newline(&xbeeboard.rdl,
543 CDC_Device_SendByte(&VirtualSerial2_CDC_Interface,
545 /* echo on cmdline */
546 CDC_Device_SendByte(&VirtualSerial1_CDC_Interface,
551 if (xbee_cmdline_input_enabled)
553 xbee_proto_rx(xbee_dev);
556 CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
557 CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);