fix home directory
[protos/xbee.git] / main.c
diff --git a/main.c b/main.c
index 94c0f34..e878a34 100644 (file)
--- a/main.c
+++ b/main.c
@@ -59,6 +59,7 @@
 #include "xbee_proto.h"
 #include "xbee.h"
 #include "joystick.h"
 #include "xbee_proto.h"
 #include "xbee.h"
 #include "joystick.h"
+#include "rc_proto.h"
 #include "main.h"
 
 #define TIMEOUT_US 1000000
 #include "main.h"
 
 #define TIMEOUT_US 1000000
@@ -243,6 +244,10 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        /* dump frame */
        if (atcmd_frame_status(frame, len) == 0) {
 
        /* dump frame */
        if (atcmd_frame_status(frame, len) == 0) {
 
+               /* callback */
+               if (ctx->func != NULL)
+                       ctx->func(frame, len, ctx->arg);
+
                if (len == sizeof(struct xbee_atresp_hdr))
                        xbeeapp_log(ctx->foreground, "<%s>: ok\n",
                                    ctx->atcmd_query->name);
                if (len == sizeof(struct xbee_atresp_hdr))
                        xbeeapp_log(ctx->foreground, "<%s>: ok\n",
                                    ctx->atcmd_query->name);
@@ -256,6 +261,41 @@ static int dump_atcmd(struct xbee_ctx *ctx, struct xbee_atresp_hdr *frame,
        return 0;
 }
 
        return 0;
 }
 
+int xbee_recv_data(struct xbee_recv_hdr *recvframe, unsigned len)
+{
+       int datalen = len - sizeof(*recvframe);
+       struct rc_proto_hdr *rch = (struct rc_proto_hdr *) &recvframe->data;
+
+       if (datalen < sizeof(struct rc_proto_hdr))
+               return -1;
+
+       switch (rch->type) {
+               case RC_PROTO_TYPE_CHANNEL:
+                       if (datalen != sizeof(struct rc_proto_channel))
+                               return -1;
+                       break;
+               case RC_PROTO_TYPE_RANGE: {
+                       struct rc_proto_range *rcr =
+                               (struct rc_proto_range *) recvframe->data;
+
+                       if (datalen != sizeof(struct rc_proto_range))
+                               return -1;
+
+                       if (rcr->power_level >= MAX_POWER_LEVEL)
+                               return -1;
+
+                       rc_proto_rx_range(rcr->power_level);
+
+                       break;
+               }
+               default:
+                       return -1;
+       }
+
+       return 0;
+}
+
+
 void xbee_rx(struct xbee_dev *dev, int channel, int type,
             void *frame, unsigned len, void *opaque)
 {
 void xbee_rx(struct xbee_dev *dev, int channel, int type,
             void *frame, unsigned len, void *opaque)
 {
@@ -322,19 +362,8 @@ void xbee_rx(struct xbee_dev *dev, int channel, int type,
                }
 
                case XBEE_TYPE_RECV: {
                }
 
                case XBEE_TYPE_RECV: {
-                       struct xbee_recv_hdr *recvframe = frame;
-                       int recvlen = len - sizeof(*recvframe);
-                       int on_stdout = 1;
-
-                       /* if we receive a range-test frame, ask for RSSI now */
-                       if (recvlen >= strlen("range") &&
-                           !strncmp((char *)recvframe->data,
-                                    "range", strlen("range"))) {
-                               xbeeapp_send_atcmd("DB", NULL, 0, 0);
-                               on_stdout = 0;
-                       }
-                       hexdump(on_stdout, "rx data", recvframe->data,
-                               recvlen);
+                       if (xbee_recv_data(frame, len) < 0)
+                               do_hexdump = 1;
                        break;
                }
 
                        break;
                }
 
@@ -415,8 +444,10 @@ static int xbeeapp_send(struct xbee_ctx *ctx, int type, void *buf, unsigned len,
 
 /* send an AT command with parameters filled by caller. Disable
  * command line until we get the answer or until a timeout occurs */
 
 /* send an AT command with parameters filled by caller. Disable
  * command line until we get the answer or until a timeout occurs */
-int xbeeapp_send_atcmd(const char *atcmd_str, void *param, unsigned param_len,
-                      int foreground)
+int xbeeapp_send_atcmd(const char *atcmd_str,
+                      void *param, unsigned param_len, int foreground,
+                      int (*func)(void *frame, unsigned len, void *arg),
+                      void *arg)
 {
        struct xbee_ctx *ctx;
        struct xbee_atcmd *cmd;
 {
        struct xbee_ctx *ctx;
        struct xbee_atcmd *cmd;
@@ -441,6 +472,8 @@ int xbeeapp_send_atcmd(const char *atcmd_str, void *param, unsigned param_len,
        memset(ctx, 0, sizeof(*ctx));
        ctx->type = ATCMD;
        ctx->atcmd_query = cmd;
        memset(ctx, 0, sizeof(*ctx));
        ctx->type = ATCMD;
        ctx->atcmd_query = cmd;
+       ctx->func = func;
+       ctx->arg = arg;
 
        memcpy(&frame.atcmd.cmd, atcmd_str, 2);
        memcpy(&frame.buf, param, param_len);
 
        memcpy(&frame.atcmd.cmd, atcmd_str, 2);
        memcpy(&frame.buf, param, param_len);
@@ -773,6 +806,9 @@ int main(int argc, char **argv)
        if (err < 0)
                return -1;
 
        if (err < 0)
                return -1;
 
+       /* init rc_proto */
+       rc_proto_init();
+
        /* init joystick */
        if (joystick_devname != NULL) {
                if (joystick_init(joystick_devname, &joyinfo) < 0) {
        /* init joystick */
        if (joystick_devname != NULL) {
                if (joystick_init(joystick_devname, &joyinfo) < 0) {
@@ -816,7 +852,7 @@ int main(int argc, char **argv)
                if (access(xbeerc_path, R_OK) == 0) {
 
                        xbee_cl = cmdline_file_new(&main_ctx, "xbeerc> ",
                if (access(xbeerc_path, R_OK) == 0) {
 
                        xbee_cl = cmdline_file_new(&main_ctx, "xbeerc> ",
-                                             "/home/zer0/.xbeerc", 1);
+                                             xbeerc_path, 1);
                        if (xbee_cl != NULL) {
                                cmdline_interact(xbee_cl);
                                cmdline_free(xbee_cl);
                        if (xbee_cl != NULL) {
                                cmdline_interact(xbee_cl);
                                cmdline_free(xbee_cl);