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;
63 volatile uint16_t global_ms;
64 struct callout_manager cm;
66 #define TIMEOUT_MS 1000
68 /* global xbee device */
69 struct xbee_dev *xbee_dev;
72 //static struct event stdin_read_event, xbee_read_event;
79 int xbee_cmdline_input_enabled = 1;
81 static struct xbee_ctx xbee_ctx[XBEE_MAX_CHANNEL];
83 static void hexdump(const char *title, const void *buf, unsigned int len)
85 unsigned int i, out, ofs;
86 const unsigned char *data = buf;
88 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
90 printf_P(PSTR("%s at [%p], len=%d\r\n"), title, data, len);
93 /* format 1 line in the buffer, then use printk to print them */
94 out = snprintf_P(line, LINE_LEN, PSTR("%08X"), ofs);
95 for (i=0; ofs+i < len && i<16; i++)
96 out += snprintf_P(line+out, LINE_LEN - out,
100 out += snprintf(line+out, LINE_LEN - out, " ");
101 for (i=0; ofs < len && i<16; i++, ofs++) {
102 unsigned char c = data[ofs];
103 if (!isascii(c) || !isprint(c))
105 out += snprintf_P(line+out,
109 printf_P(PSTR("%s\r\n"), line);
113 static int parse_xmit_status(struct xbee_ctx *ctx,
114 struct xbee_xmit_status_hdr *frame, unsigned len)
117 printf_P(PSTR("no context\r\n"));
121 /* see if it matches a xmit query (atcmd_query must be NULL) */
122 if (ctx->atcmd_query[0] != '\0') {
123 printf_P(PSTR("invalid response 2\r\n"));
127 /* XXX use defines for these values */
128 if (frame->delivery_status == 0x00)
129 printf_P(PSTR("Success\r\n"));
130 else if (frame->delivery_status == 0x01)
131 printf_P(PSTR("MAC ACK Failure\r\n"));
132 else if (frame->delivery_status == 0x15)
133 printf_P(PSTR("Invalid destination endpoint\r\n"));
134 else if (frame->delivery_status == 0x21)
135 printf_P(PSTR("Network ACK Failure\r\n"));
136 else if (frame->delivery_status == 0x25)
137 printf_P(PSTR("Route Not Found\r\n"));
142 static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
146 struct xbee_atcmd_pgm *cmd_pgm;
147 struct xbee_atcmd cmd;
153 } __attribute__((packed)) *result;
156 printf_P(PSTR("no context\r\n"));
160 /* get AT command from frame */
161 memcpy(atcmd_str, &frame->cmd, 2);
164 /* see if it matches query */
165 if (memcmp(atcmd_str, ctx->atcmd_query, 2)) {
166 printf_P(PSTR("invalid response <%c%c><%s><%s>\r\n"),
168 (frame->cmd >> 8) & 0xFF,
169 atcmd_str, ctx->atcmd_query);
173 /* see if it exists */
174 cmd_pgm = xbee_atcmd_lookup_name(atcmd_str);
175 if (cmd_pgm == NULL) {
176 printf_P(PSTR("unknown response\r\n"));
179 memcpy_P(&cmd, cmd_pgm, sizeof(cmd));
182 if (frame->status == 1) {
183 printf_P(PSTR("Status is error\r\n"));
186 else if (frame->status == 2) {
187 printf_P(PSTR("Invalid command\r\n"));
190 else if (frame->status == 3) {
191 printf_P(PSTR("Invalid parameter\r\n"));
194 else if (frame->status != 0) {
195 printf_P(PSTR("Unknown status error %d\r\n"), frame->status);
200 if (ctx->func != NULL)
201 ctx->func(frame, len, ctx->arg);
204 result = (void *)frame->data;
205 len -= offsetof(struct xbee_atresp_hdr, data);
206 if (cmd.flags & XBEE_ATCMD_F_PARAM_U8 && len == sizeof(uint8_t))
207 printf_P(PSTR("<%s> is 0x%x\r\n"), atcmd_str, result->u8);
208 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U16 && len == sizeof(uint16_t))
209 printf_P(PSTR("<%s> is 0x%x\r\n"),
212 else if (cmd.flags & XBEE_ATCMD_F_PARAM_U32 && len == sizeof(uint32_t))
213 printf_P(PSTR("<%s> is 0x%"PRIx32"\r\n"),
216 else if (cmd.flags & XBEE_ATCMD_F_PARAM_S16 && len == sizeof(int16_t))
217 printf_P(PSTR("<%s> is %d\r\n"), atcmd_str, ntohs(result->s16));
219 printf_P(PSTR("no data, status ok\r\n"));
221 hexdump("atcmd answer", frame->data, len);
228 int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
230 int datalen = len - sizeof(*recvframe);
231 struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
233 if (datalen < sizeof(struct rc_proto_hdr))
237 case RC_PROTO_TYPE_CHANNEL:
238 if (datalen != sizeof(struct rc_proto_channel))
241 case RC_PROTO_TYPE_RANGE: {
242 struct rc_proto_range *rcr =
243 (struct rc_proto_range *) recvframe->data;
245 if (datalen != sizeof(struct rc_proto_range))
248 if (rcr->power_level >= MAX_POWER_LEVEL)
251 rc_proto_rx_range(rcr->power_level);
262 /* socat /dev/ttyUSB0,raw,echo=0,b115200 /dev/ttyACM1,raw,echo=0,b115200 */
263 void xbee_rx(struct xbee_dev *dev, int channel, int type,
264 void *frame, unsigned len, void *opaque)
266 struct xbee_ctx *ctx = opaque;
267 int do_hexdump = xbee_hexdump;
270 printf_P(PSTR("type=0x%x, channel=%d, ctx=%p\r\n"),
273 /* if ctx is !NULL, it is an answer to a query */
275 /* XXX only delete timeout if answer matched */
276 xbee_unload_timeout(ctx);
277 if (xbee_debug && ctx->atcmd_query)
278 printf_P(PSTR("Received answer to query <%c%c>\r\n"),
279 ctx->atcmd_query[0], ctx->atcmd_query[1]);
282 /* some additional checks before sending */
284 case XBEE_TYPE_MODEM_STATUS: {
285 printf_P(PSTR("Received Modem Status frame\r\n"));
289 case XBEE_TYPE_RMT_ATRESP: {
293 #if BYTE_ORDER == LITTLE_ENDIAN
302 memcpy(&addr, frame, sizeof(addr));
303 addr.u64 = ntohll(addr.u64);
304 printf_P(PSTR("from remote address %"PRIx32"%"PRIx32"\r\n"),
305 addr.u32.high, addr.u32.low);
307 /* this answer contains an atcmd answer at offset 10 */
308 if (dump_atcmd(ctx, frame + 10, len - 10) < 0)
312 case XBEE_TYPE_ATRESP: {
313 if (dump_atcmd(ctx, frame, len) < 0)
318 case XBEE_TYPE_XMIT_STATUS: {
319 if (parse_xmit_status(ctx, frame, len) < 0)
324 case XBEE_TYPE_RECV: {
325 if (xbee_recv_data(frame, len) < 0)
330 case XBEE_TYPE_ATCMD:
331 case XBEE_TYPE_ATCMD_Q:
333 case XBEE_TYPE_EXPL_XMIT:
334 case XBEE_TYPE_RMT_ATCMD:
335 case XBEE_TYPE_EXPL_RECV:
336 case XBEE_TYPE_NODE_ID:
338 printf_P(PSTR("Invalid frame\r\n"));
344 hexdump("undecoded rx frame", frame, len);
346 /* restart command line if it was a blocking query */
348 xbee_unregister_channel(dev, channel);
349 if (ctx->foreground) {
351 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
356 static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
362 if (len > XBEE_MAX_FRAME_LEN) {
363 printf_P(PSTR("frame too large\r\n"));
367 /* register a channel */
368 channel = xbee_register_channel(xbee_dev, XBEE_CHANNEL_ANY,
371 printf_P(PSTR("cannot send: no free channel\r\n"));
375 /* copy context in the static struct table (avoiding a malloc) */
376 memcpy(&xbee_ctx[channel], ctx, sizeof(*ctx));
377 ctx = &xbee_ctx[channel];
378 xbee_set_opaque(xbee_dev, channel, ctx);
381 printf_P(PSTR("send frame channel=%d type=0x%x len=%d\r\n"),
384 hexdump("xmit frame", buf, len);
386 /* transmit the frame on this channel */
387 ret = xbee_proto_xmit(xbee_dev, channel, type, buf,
390 printf_P(PSTR("cannot send\r\n"));
391 xbee_unregister_channel(xbee_dev, channel);
395 ctx->channel = channel;
396 xbee_load_timeout(ctx); /* load a timeout event */
398 /* suspend command line until we have answer or timeout */
401 rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
402 xbee_stdin_disable(); /* unload file descriptor polling */
408 /* send an AT command with parameters filled by caller. Disable
409 * command line until we get the answer or until a timeout occurs */
410 int xbeeapp_send_atcmd(const char *atcmd_str,
411 void *param, unsigned param_len, int foreground,
412 int (*func)(void *frame, unsigned len, void *arg), void *arg)
416 struct xbee_atcmd_hdr atcmd;
417 char buf[XBEE_MAX_FRAME_LEN];
418 } __attribute__((packed)) frame;
420 memset(&ctx, 0, sizeof(ctx));
421 ctx.atcmd_query[0] = atcmd_str[0];
422 ctx.atcmd_query[1] = atcmd_str[1];
426 memcpy(&frame.atcmd.cmd, atcmd_str, 2);
427 memcpy(&frame.buf, param, param_len);
429 if (xbeeapp_send(&ctx, XBEE_TYPE_ATCMD, &frame,
430 sizeof(struct xbee_atcmd_hdr) +
431 param_len, foreground) < 0) {
438 int xbeeapp_send_msg(uint64_t addr, void *data,
439 unsigned data_len, int foreground)
443 struct xbee_xmit_hdr xmit;
444 char buf[XBEE_MAX_FRAME_LEN];
445 } __attribute__((packed)) frame;
447 memset(&ctx, 0, sizeof(ctx));
448 ctx.atcmd_query[0] = '\0';
450 frame.xmit.dstaddr = htonll(addr);
451 frame.xmit.reserved = htons(0xFFFE);
452 frame.xmit.bcast_radius = 0;
454 memcpy(&frame.buf, data, data_len);
456 if (xbeeapp_send(&ctx, XBEE_TYPE_XMIT, &frame,
457 sizeof(struct xbee_xmit_hdr) +
458 data_len, foreground) < 0) {
465 void xbee_stdin_enable(void)
467 xbee_cmdline_input_enabled = 1;
470 void xbee_stdin_disable(void)
472 xbee_cmdline_input_enabled = 0;
475 static void evt_timeout(struct callout_manager *cm, struct callout *clt,
478 struct xbee_ctx *ctx = arg;
480 printf_P(PSTR("Timeout\r\n"));
482 /* restart command line */
484 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
487 xbee_unregister_channel(xbee_dev, ctx->channel);
490 void xbee_load_timeout(struct xbee_ctx *ctx)
492 callout_reset(&cm, &ctx->timeout, TIMEOUT_MS, SINGLE, evt_timeout, ctx);
495 void xbee_unload_timeout(struct xbee_ctx *ctx)
497 callout_stop(&cm, &ctx->timeout);
500 void bootloader(void)
503 #define BOOTLOADER_ADDR 0x3f000
504 if (pgm_read_byte_far(BOOTLOADER_ADDR) == 0xff) {
505 printf_P(PSTR("Bootloader is not present\r\n"));
509 /* ... very specific :( */
527 __asm__ __volatile__ ("ldi r31,0xf8\n");
528 __asm__ __volatile__ ("ldi r30,0x00\n");
529 __asm__ __volatile__ ("eijmp\n");
533 void xbee_mainloop(void)
541 /* from xbee to cmdline */
542 c = xbee_dev_recv(NULL);
544 cmdline_dev_send((uint8_t)c, NULL);
546 /* from cmdline to xbee */
547 c = cmdline_dev_recv(NULL);
548 if (c == 4) { /* CTRL-d */
550 rdline_newline(&xbeeboard.rdl,
555 xbee_dev_send((uint8_t)c, NULL);
557 /* echo on cmdline */
558 cmdline_dev_send((uint8_t)c, NULL);
562 if (xbee_cmdline_input_enabled)
564 xbee_proto_rx(xbee_dev);
568 CDC_Device_USBTask(&VirtualSerial1_CDC_Interface);
569 CDC_Device_USBTask(&VirtualSerial2_CDC_Interface);
575 /* return time in milliseconds on unsigned 16 bits */
576 static uint16_t get_time_ms(void)
581 static void do_led_blink(struct callout_manager *cm,
582 struct callout *clt, void *dummy)
584 static uint8_t a = 0;
590 LEDs_SetAllLEDs(0xff);
597 static void increment_ms(void *dummy)
602 static void main_timer_interrupt(void)
604 static uint8_t cpt = 0;
607 if ((cpt & 0x3) == 0)
608 scheduler_interrupt();
611 /** Main program entry point. This routine contains the overall program flow, including initial
612 * setup of all components and the main program loop.
624 LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
629 fdevopen(cmdline_dev_send, cmdline_dev_recv);
630 xbee_file = fdevopen(xbee_dev_send, xbee_dev_recv);
633 timer0_register_OV_intr(main_timer_interrupt);
635 scheduler_add_periodical_event_priority(increment_ms, NULL,
636 1000L / SCHEDULER_UNIT,
640 /* in usb mode, it's done in usb callback */
641 printf_P(PSTR("\r\n"));
642 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);
644 callout_manager_init(&cm, get_time_ms);
645 callout_reset(&cm, &t1, 500, PERIODICAL, do_led_blink, NULL);
647 /* initialize libxbee */
654 /* open xbee device */
655 if (xbee_open(xbee_dev, xbee_file) < 0)
658 /* register default channel with a callback */
659 if (xbee_register_channel(xbee_dev, XBEE_DEFAULT_CHANNEL,
660 xbee_rx, NULL) < 0) {
661 fprintf(stderr, "cannot register default channel\n");