xbee: support rc_proto and joystick
[protos/xbee.git] / commands.c
index a12ed42..be8e03c 100644 (file)
@@ -35,6 +35,7 @@
 #include <sys/queue.h>
 #include <arpa/inet.h>
 #include <inttypes.h>
+#include <linux/joystick.h>
 
 #include <event.h>
 
@@ -53,6 +54,8 @@
 #include "parse_atcmd.h"
 #include "parse_neighbor.h"
 #include "parse_monitor.h"
+#include "rc_proto.h"
+#include "joystick.h"
 #include "main.h"
 
 static struct monitor_reg_list monitor_list = LIST_HEAD_INITIALIZER(x/*XXX*/);
@@ -71,6 +74,10 @@ static struct event range_event;
 static int range_count = 100;
 static int range_cur_count = 0;
 
+static struct event rc_send_event;
+static int rc_send_period_ms = 100;
+static uint64_t rc_send_dstaddr = 0xFFFF; /* broadcast by default */
+static int rc_send_running = 0;
 static const char *xbee_logfilename = "/tmp/xbee.log";
 
 static void monitor_cb(int s, short event, void *arg)
@@ -122,6 +129,27 @@ static void range_cb(int s, short event, void *arg)
        evtimer_add(&range_event, &tv);
 }
 
+
+static void rc_send_cb(int fd, short event, void *arg)
+{
+       struct cmdline *cl = arg;
+       struct timeval tv;
+       struct rc_proto_channel rc_chan;
+       int i;
+
+       rc_chan.type = RC_PROTO_TYPE_CHANNEL;
+       for (i = 0; i< AXIS_NUMBER; i++){
+               rc_chan.axis[i] = htons(joyinfo.axis[i]);
+       }
+       xbeeapp_send_msg(rc_send_dstaddr, &rc_chan, sizeof(rc_chan), 0);
+
+       evtimer_set(&rc_send_event, rc_send_cb, cl);
+       tv.tv_sec = 0;
+       tv.tv_usec = 1000 * rc_send_period_ms;
+       evtimer_add(&rc_send_event, &tv);
+}
+
+
 /* ************* */
 
 /* this structure is filled when cmd_stats is parsed successfully */
@@ -610,6 +638,148 @@ cmdline_parse_inst_t cmd_range_dstaddr = {
        },
 };
 
+/* ************* */
+
+/* this structure is filled when cmd_rc_send is parsed successfully */
+struct cmd_rc_send_result {
+       cmdline_fixed_string_t rc_send;
+       cmdline_fixed_string_t action;
+};
+
+/* function called when cmd_rc_send is parsed successfully */
+static void cmd_rc_send_parsed(void *parsed_result, struct cmdline *cl,
+                              void *data)
+{
+       struct cmd_rc_send_result *res = parsed_result;
+
+       if (!strcmp(res->action, "show"))
+               ;//XXX
+       else if (!strcmp(res->action, "start")) {
+               struct timeval tv;
+               if (rc_send_running) {
+                       printf("already running\n");
+                       return;
+               }
+               evtimer_set(&rc_send_event, rc_send_cb, cl);
+               tv.tv_sec = 0;
+               tv.tv_usec = 0;
+               evtimer_add(&rc_send_event, &tv);
+               rc_send_running = 1;
+       }
+       else if (!strcmp(res->action, "end")) {
+               if (rc_send_running == 0) {
+                       printf("not running\n");
+                       return;
+               }
+               rc_send_running = 0;
+               evtimer_del(&rc_send_event);
+       }
+}
+
+cmdline_parse_token_string_t cmd_rc_send_rc_send =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_result, rc_send, "rc_send");
+cmdline_parse_token_string_t cmd_rc_send_action =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_result, action,
+                                "show#start#end");
+
+cmdline_parse_inst_t cmd_rc_send = {
+       .f = cmd_rc_send_parsed,  /* function to call */
+       .data = NULL,      /* 2nd arg of func */
+       .help_str = "start/stop/show current rc_sending",
+       .tokens = {        /* token list, NULL terminated */
+               (void *)&cmd_rc_send_rc_send,
+               (void *)&cmd_rc_send_action,
+               NULL,
+       },
+};
+
+/* ************* */
+
+/* this structure is filled when cmd_rc_send_period is parsed successfully */
+struct cmd_rc_send_period_result {
+       cmdline_fixed_string_t rc_send;
+       cmdline_fixed_string_t action;
+       uint32_t period;
+};
+
+/* function called when cmd_rc_send_period is parsed successfully */
+static void cmd_rc_send_period_parsed(void *parsed_result, struct cmdline *cl,
+                                  void *data)
+{
+       struct cmd_rc_send_period_result *res = parsed_result;
+
+       if (res->period < 10) {
+               printf("error, minimum period is 10 ms\n");
+               return;
+       }
+
+       rc_send_period_ms = res->period;
+}
+
+cmdline_parse_token_string_t cmd_rc_send_period_rc_send_period =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_period_result, rc_send,
+                                "rc_send");
+cmdline_parse_token_string_t cmd_rc_send_period_action =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_period_result, action,
+                                "period");
+cmdline_parse_token_num_t cmd_rc_send_period_period =
+       TOKEN_NUM_INITIALIZER(struct cmd_rc_send_period_result, period, UINT32);
+
+
+cmdline_parse_inst_t cmd_rc_send_period = {
+       .f = cmd_rc_send_period_parsed,  /* function to call */
+       .data = NULL,      /* 2nd arg of func */
+       .help_str = "set register rc_sending period",
+       .tokens = {        /* token list, NULL terminated */
+               (void *)&cmd_rc_send_period_rc_send_period,
+               (void *)&cmd_rc_send_period_action,
+               (void *)&cmd_rc_send_period_period,
+               NULL,
+       },
+};
+
+/* ************* */
+
+/* this structure is filled when cmd_rc_send_dstaddr is parsed successfully */
+struct cmd_rc_send_dstaddr_result {
+       cmdline_fixed_string_t rc_send;
+       cmdline_fixed_string_t action;
+       uint64_t dstaddr;
+};
+
+/* function called when cmd_rc_send_dstaddr is parsed successfully */
+static void cmd_rc_send_dstaddr_parsed(void *parsed_result, struct cmdline *cl,
+                                    void *data)
+{
+       struct cmd_rc_send_dstaddr_result *res = parsed_result;
+
+       rc_send_dstaddr = res->dstaddr;
+}
+
+cmdline_parse_token_string_t cmd_rc_send_dstaddr_rc_send_dstaddr =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_dstaddr_result, rc_send,
+                                "rc_send");
+cmdline_parse_token_string_t cmd_rc_send_dstaddr_action =
+       TOKEN_STRING_INITIALIZER(struct cmd_rc_send_dstaddr_result, action,
+                                "dstaddr");
+cmdline_parse_token_num_t cmd_rc_send_dstaddr_dstaddr =
+       TOKEN_NUM_INITIALIZER(struct cmd_rc_send_dstaddr_result, dstaddr, UINT64);
+
+
+cmdline_parse_inst_t cmd_rc_send_dstaddr = {
+       .f = cmd_rc_send_dstaddr_parsed,  /* function to call */
+       .data = NULL,      /* 2nd arg of func */
+       .help_str = "set register rc_sending dstaddr",
+       .tokens = {        /* token list, NULL terminated */
+               (void *)&cmd_rc_send_dstaddr_rc_send_dstaddr,
+               (void *)&cmd_rc_send_dstaddr_action,
+               (void *)&cmd_rc_send_dstaddr_dstaddr,
+               NULL,
+       },
+};
+
+
+
 /* ************* */
 
 /* this structure is filled when cmd_ping is parsed successfully */
@@ -1315,6 +1485,9 @@ cmdline_parse_ctx_t main_ctx = {
                &cmd_range_count,
                &cmd_range_powermask,
                &cmd_range_dstaddr,
+               &cmd_rc_send,
+               &cmd_rc_send_period,
+               &cmd_rc_send_dstaddr,
                &cmd_ping,
                &cmd_raw,
                &cmd_dump,