rework xbee_user: remove the foreground param
[protos/xbee-avr.git] / commands.c
index 319e1fa..2c663c1 100644 (file)
 #include <parse_num.h>
 #include <uart.h>
 #include <xbee.h>
+#include <callout.h>
 
-#include "callout.h"
 #include "parse_atcmd.h"
 #include "parse_neighbor.h"
 #include "parse_monitor.h"
 
 #include "spi_servo.h"
 #include "rc_proto.h"
+#include "xbee_user.h"
 #include "main.h"
 #include "cmdline.h"
 #include "beep.h"
@@ -56,7 +57,7 @@ extern const parse_inst_t PROGMEM cmd_log;
 extern const parse_inst_t PROGMEM cmd_log_show;
 extern const parse_inst_t PROGMEM cmd_log_type;
 extern const parse_inst_t PROGMEM cmd_stack_space;
-extern const parse_inst_t PROGMEM cmd_scheduler;
+extern const parse_inst_t PROGMEM cmd_callout;
 
 static int monitor_period_ms = 1000;
 static int monitor_running = 0;
@@ -73,7 +74,7 @@ static struct callout range_event;
 static int range_count = 100;
 static int range_cur_count = 0;
 
-static void monitor_cb(struct callout_manager *cm,
+static void monitor_cb(struct callout_mgr *cm,
                       struct callout *clt, void *dummy)
 {
        (void)clt;
@@ -82,14 +83,13 @@ static void monitor_cb(struct callout_manager *cm,
        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_reset(cm, &monitor_event,
-                     monitor_period_ms / monitor_count,
-                     SINGLE, monitor_cb, NULL);
+       callout_reschedule(cm, clt, monitor_period_ms / monitor_count);
 }
 
-static void range_cb(struct callout_manager *cm,
+static void range_cb(struct callout_mgr *cm,
                     struct callout *clt, void *dummy)
 {
        (void)cm;
@@ -121,13 +121,61 @@ static void range_cb(struct callout_manager *cm,
 
        if (range_cur_count == 0) {
                range_running = 0;
-               return;
+               callout_stop(cm, clt);
        }
 
-       callout_reset(cm, &range_event,
-                     range_period_ms,
-                     SINGLE, range_cb, NULL);
+       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 */
@@ -327,9 +375,6 @@ const parse_inst_t PROGMEM cmd_neigh_list = {
        },
 };
 
-
-
-
 /* ************* */
 
 /* this structure is filled when cmd_read is parsed successfully */
@@ -345,12 +390,14 @@ static void cmd_read_parsed(void *parsed_result,
        struct cmd_read_result *res = parsed_result;
        struct xbee_atcmd copy;
        char cmd[3];
+       volatile uint8_t done = 0;
 
        (void)data;
        memcpy_P(&copy, 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";
@@ -397,6 +444,7 @@ static void cmd_write_parsed(void *parsed_result, void *data)
        char cmd[3];
        int len;
        void *param;
+       volatile uint8_t done = 0;
 
        (void)data;
        memcpy_P(&copy, res->cmd, sizeof(copy));
@@ -425,7 +473,8 @@ static void cmd_write_parsed(void *parsed_result, void *data)
        }
        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";
@@ -536,9 +585,17 @@ struct cmd_sendmsg_result {
 static void cmd_sendmsg_parsed(void *parsed_result, void *data)
 {
        struct cmd_sendmsg_result *res = parsed_result;
+       struct xbee_msg msg;
+       volatile uint8_t done = 0;
 
        (void)data;
-       xbeeapp_send_msg(res->addr, res->data, strlen(res->data), 1);
+
+       msg.iovlen = 1;
+       msg.iov[0].buf = res->data;
+       msg.iov[0].len = strlen(res->data);
+
+       xbeeapp_send_msg(res->addr, &msg, send_msg_cb, (void *)&done);
+       while (done == 0);
 }
 
 const char PROGMEM str_sendmsg[] = "sendmsg";
@@ -569,6 +626,105 @@ const parse_inst_t PROGMEM cmd_sendmsg = {
 
 /* ************* */
 
+/* this structure is filled when cmd_send_hello is parsed successfully */
+struct cmd_send_hello_result {
+       fixed_string_t send_hello;
+       uint64_t addr;
+       struct xbee_neigh *neigh;
+       uint16_t period;
+       uint16_t count;
+       fixed_string_t data;
+};
+
+/* function called when cmd_send_hello is parsed successfully */
+static void cmd_send_hello_parsed(void *parsed_result, void *use_neigh)
+{
+       struct cmd_send_hello_result *res = parsed_result;
+       uint16_t now, next, diff;
+       uint8_t flags;
+       uint64_t addr;
+
+       if (use_neigh)
+               addr = res->neigh->addr;
+       else
+               addr = res->addr;
+
+       IRQ_LOCK(flags);
+       now = global_ms;
+       IRQ_UNLOCK(flags);
+
+       next = now;
+
+       while (!cmdline_keypressed() && res->count != 0) {
+               IRQ_LOCK(flags);
+               now = global_ms;
+               IRQ_UNLOCK(flags);
+
+               diff = now - next;
+               if (diff < res->period)
+                       continue;
+
+               rc_proto_send_hello(addr, res->data, strlen(res->data));
+               next += res->period;
+               res->count--;
+       }
+}
+
+const char PROGMEM str_send_hello[] = "send_hello";
+
+const parse_token_string_t PROGMEM cmd_send_hello_send_hello =
+       TOKEN_STRING_INITIALIZER(struct cmd_send_hello_result, send_hello,
+                                str_send_hello);
+
+const parse_token_num_t PROGMEM cmd_send_hello_addr =
+       TOKEN_NUM_INITIALIZER(struct cmd_send_hello_result, addr, UINT64);
+
+const parse_token_num_t PROGMEM cmd_send_hello_period =
+       TOKEN_NUM_INITIALIZER(struct cmd_send_hello_result, period, UINT16);
+
+const parse_token_num_t PROGMEM cmd_send_hello_count =
+       TOKEN_NUM_INITIALIZER(struct cmd_send_hello_result, count, UINT16);
+
+const parse_token_string_t PROGMEM cmd_send_hello_data =
+       TOKEN_STRING_INITIALIZER(struct cmd_send_hello_result, data, NULL);
+
+const char PROGMEM help_send_hello[] =
+       "Send hello msg to a node: addr, period_ms, count, str";
+
+const parse_inst_t PROGMEM cmd_send_hello = {
+       .f = cmd_send_hello_parsed,  /* function to call */
+       .data = NULL,      /* 2nd arg of func */
+       .help_str = help_send_hello,
+       .tokens = {        /* token list, NULL terminated */
+               (PGM_P)&cmd_send_hello_send_hello,
+               (PGM_P)&cmd_send_hello_addr,
+               (PGM_P)&cmd_send_hello_period,
+               (PGM_P)&cmd_send_hello_count,
+               (PGM_P)&cmd_send_hello_data,
+               NULL,
+       },
+};
+
+const parse_token_neighbor_t PROGMEM cmd_send_hello_neigh =
+       TOKEN_NEIGHBOR_INITIALIZER(struct cmd_send_hello_result, neigh,
+                                  &xbee_dev);
+
+const parse_inst_t PROGMEM cmd_send_hello_name = {
+       .f = cmd_send_hello_parsed,  /* function to call */
+       .data = (void *)1,      /* 2nd arg of func */
+       .help_str = help_send_hello,
+       .tokens = {        /* token list, NULL terminated */
+               (PGM_P)&cmd_send_hello_send_hello,
+               (PGM_P)&cmd_send_hello_neigh,
+               (PGM_P)&cmd_send_hello_period,
+               (PGM_P)&cmd_send_hello_count,
+               (PGM_P)&cmd_send_hello_data,
+               NULL,
+       },
+};
+
+/* ************* */
+
 /* this structure is filled when cmd_sendmsg_name is parsed successfully */
 struct cmd_sendmsg_name_result {
        fixed_string_t sendmsg_name;
@@ -580,9 +736,17 @@ struct cmd_sendmsg_name_result {
 static void cmd_sendmsg_name_parsed(void *parsed_result, void *data)
 {
        struct cmd_sendmsg_name_result *res = parsed_result;
+       struct xbee_msg msg;
+       volatile uint8_t done = 0;
 
        (void)data;
-       xbeeapp_send_msg(res->neigh->addr, res->data, strlen(res->data), 1);
+
+       msg.iovlen = 1;
+       msg.iov[0].buf = res->data;
+       msg.iov[0].len = strlen(res->data);
+
+       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 =
@@ -645,18 +809,18 @@ static void cmd_range_parsed(void *parsed_result, void *data)
                        return;
                }
                range_cur_count = range_count;
-               callout_init(&range_event);
-               callout_reset(&cm, &range_event, 0,
-                             SINGLE, range_cb, NULL);
+               callout_init(&range_event, range_cb, NULL, LOW_PRIO);
                range_running = 1;
+               callout_schedule(&xbeeboard.intr_cm,
+                       &range_event, 0); /* immediate */
        }
        else if (!strcmp(res->action, "end")) {
                if (range_running == 0) {
                        printf_P(PSTR("not running\r\n"));
                        return;
                }
+               callout_stop(&xbeeboard.intr_cm, &range_event);
                range_running = 0;
-               callout_stop(&cm, &range_event);
        }
 }
 
@@ -895,10 +1059,11 @@ static void cmd_monitor_parsed(void *parsed_result, void *data)
                        printf_P(PSTR("no regs to be monitored\r\n"));
                        return;
                }
-               callout_init(&monitor_event);
-               callout_reset(&cm, &monitor_event, 0, SINGLE, monitor_cb, NULL);
+               callout_init(&monitor_event, monitor_cb, NULL, 1);
                monitor_running = 1;
                monitor_current = LIST_FIRST(&xbee_monitor_list);
+               callout_schedule(&xbeeboard.intr_cm,
+                       &monitor_event, 0); /* immediate */
                printf_P(PSTR("monitor cb: %S %s\r\n"),
                         monitor_current->desc,
                         monitor_current->atcmd);
@@ -909,8 +1074,8 @@ static void cmd_monitor_parsed(void *parsed_result, void *data)
                        printf_P(PSTR("not running\r\n"));
                        return;
                }
+               callout_stop(&xbeeboard.intr_cm, &monitor_event);
                monitor_running = 0;
-               callout_stop(&cm, &monitor_event);
        }
 }
 
@@ -1073,7 +1238,7 @@ static void cmd_monitor_del_parsed(void *parsed_result, void *data)
        monitor_count --;
        if (monitor_count == 0) {
                printf_P(PSTR("Disable monitoring, no more event\r\n"));
-               callout_stop(&cm, &monitor_event);
+               callout_stop(&xbeeboard.intr_cm, &monitor_event);
                monitor_running = 0;
                return;
        }
@@ -1116,9 +1281,12 @@ struct cmd_ping_result {
 /* 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";
@@ -1151,6 +1319,11 @@ static void cmd_raw_parsed(void *parsed_result, void *data)
 {
        (void)parsed_result;
        (void)data;
+
+       if (range_running || monitor_running) {
+               printf_P(PSTR("stop running range or monitor first\r\n"));
+               return;
+       }
        printf_P(PSTR("switched to raw mode, CTRL-D to exit\r\n"));
        rdline_stop(&xbeeboard.rdl); /* don't display prompt when return */
        xbee_raw = 1;
@@ -1174,94 +1347,6 @@ const parse_inst_t PROGMEM cmd_raw = {
        },
 };
 
-/* ************* */
-
-/* this structure is filled when cmd_dump is parsed successfully */
-struct cmd_dump_result {
-       fixed_string_t dump;
-       fixed_string_t onoff;
-};
-
-/* function called when cmd_dump is parsed successfully */
-static void cmd_dump_parsed(void *parsed_result, void *data)
-{
-       struct cmd_dump_result *res = parsed_result;
-
-       (void)data;
-       if (!strcmp(res->onoff, "on"))
-               xbee_hexdump = 1;
-       else
-               xbee_hexdump = 0;
-}
-
-const char PROGMEM str_dump[] = "dump";
-const char PROGMEM str_dump_onoff[] = "on#off";
-
-const parse_token_string_t PROGMEM cmd_dump_dump =
-       TOKEN_STRING_INITIALIZER(struct cmd_dump_result, dump,
-                                str_dump);
-
-const parse_token_string_t PROGMEM cmd_dump_onoff =
-       TOKEN_STRING_INITIALIZER(struct cmd_dump_result, onoff,
-                                str_dump_onoff);
-
-const char PROGMEM help_dump[] = "enable/disable hexdump of received packets";
-
-const parse_inst_t PROGMEM cmd_dump = {
-       .f = cmd_dump_parsed,  /* function to call */
-       .data = NULL,      /* 2nd arg of func */
-       .help_str = help_dump,
-       .tokens = {        /* token list, NULL terminated */
-               (PGM_P)&cmd_dump_dump,
-               (PGM_P)&cmd_dump_onoff,
-               NULL,
-       },
-};
-
-/* ************* */
-
-/* this structure is filled when cmd_debug is parsed successfully */
-struct cmd_debug_result {
-       fixed_string_t debug;
-       fixed_string_t onoff;
-};
-
-/* function called when cmd_debug is parsed successfully */
-static void cmd_debug_parsed(void *parsed_result, void *data)
-{
-       struct cmd_debug_result *res = parsed_result;
-
-       (void)data;
-       if (!strcmp(res->onoff, "on"))
-               xbee_debug = 1;
-       else
-               xbee_debug = 0;
-}
-
-const char PROGMEM str_debug[] = "debug";
-const char PROGMEM str_debug_onoff[] = "on#off";
-
-const parse_token_string_t PROGMEM cmd_debug_debug =
-       TOKEN_STRING_INITIALIZER(struct cmd_debug_result, debug,
-                                str_debug);
-
-const parse_token_string_t PROGMEM cmd_debug_onoff =
-       TOKEN_STRING_INITIALIZER(struct cmd_debug_result, onoff,
-                                str_debug_onoff);
-
-const char PROGMEM help_debug[] = "enable/disable additionnal debug";
-
-const parse_inst_t PROGMEM cmd_debug = {
-       .f = cmd_debug_parsed,  /* function to call */
-       .data = NULL,      /* 2nd arg of func */
-       .help_str = help_debug,
-       .tokens = {        /* token list, NULL terminated */
-               (PGM_P)&cmd_debug_debug,
-               (PGM_P)&cmd_debug_onoff,
-               NULL,
-       },
-};
-
 /**********************************************************/
 
 /* this structure is filled when cmd_baudrate is parsed successfully */
@@ -1798,7 +1883,7 @@ const parse_ctx_t PROGMEM main_ctx[] = {
        &cmd_log_show,
        &cmd_log_type,
        &cmd_stack_space,
-       &cmd_scheduler,
+       &cmd_callout,
        &cmd_help,
        &cmd_neigh_del,
        &cmd_neigh_add,
@@ -1809,6 +1894,8 @@ const parse_ctx_t PROGMEM main_ctx[] = {
        &cmd_write_u16,
        &cmd_write_u32,
        &cmd_sendmsg,
+       &cmd_send_hello,
+       &cmd_send_hello_name,
        &cmd_sendmsg_name,
        &cmd_range,
        &cmd_range_period,
@@ -1821,8 +1908,6 @@ const parse_ctx_t PROGMEM main_ctx[] = {
        &cmd_monitor_del,
        &cmd_ping,
        &cmd_raw,
-       &cmd_dump,
-       &cmd_debug,
        &cmd_baudrate,
        &cmd_beep,
        &cmd_servo_set,