]> git.zerfleddert.de Git - hmcfgusb/blobdiff - hmsniff.c
add and export function to send only a null frame
[hmcfgusb] / hmsniff.c
index 6260cb60245cef4f1691622b663a3cbbde3cba75..e8424533009813c0f47fc8d0cd3ee4020a6c96a5 100644 (file)
--- a/hmsniff.c
+++ b/hmsniff.c
 #include <strings.h>
 #include <poll.h>
 #include <errno.h>
 #include <strings.h>
 #include <poll.h>
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
+#include <sys/time.h>
 #include <libusb-1.0/libusb.h>
 
 #include "hexdump.h"
 #include <libusb-1.0/libusb.h>
 
 #include "hexdump.h"
@@ -95,8 +92,17 @@ char *hm_message_types(uint8_t type)
 
 static void dissect_hm(uint8_t *buf, int len)
 {
 
 static void dissect_hm(uint8_t *buf, int len)
 {
+       struct timeval tv;
+       struct tm *tmp;
+       char ts[32];
        int i;
 
        int i;
 
+       gettimeofday(&tv, NULL);
+       tmp = localtime(&tv.tv_sec);
+       memset(ts, 0, sizeof(ts));
+       strftime(ts, sizeof(ts)-1, "%Y-%m-%d %H:%M:%S", tmp);
+       printf("%s.%06ld: ", ts, tv.tv_usec);
+
        for (i = 0; i < len; i++) {
                printf("%02X", buf[i]);
        }
        for (i = 0; i < len; i++) {
                printf("%02X", buf[i]);
        }
@@ -110,7 +116,7 @@ static void dissect_hm(uint8_t *buf, int len)
        printf("\t\tFlags: ");
        if (buf[2] & (1 << 0)) printf("WAKEUP ");
        if (buf[2] & (1 << 1)) printf("WAKEMEUP ");
        printf("\t\tFlags: ");
        if (buf[2] & (1 << 0)) printf("WAKEUP ");
        if (buf[2] & (1 << 1)) printf("WAKEMEUP ");
-       if (buf[2] & (1 << 2)) printf("BCAST ");
+       if (buf[2] & (1 << 2)) printf("CFG ");
        if (buf[2] & (1 << 3)) printf("? ");
        if (buf[2] & (1 << 4)) printf("BURST ");
        if (buf[2] & (1 << 5)) printf("BIDI ");
        if (buf[2] & (1 << 3)) printf("? ");
        if (buf[2] & (1 << 4)) printf("BURST ");
        if (buf[2] & (1 << 5)) printf("BIDI ");
@@ -128,16 +134,29 @@ static void dissect_hm(uint8_t *buf, int len)
 
 }
 
 
 }
 
-static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
+struct recv_data {
+       int wrong_hmid;
+};
+
+static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
 {
 {
+       struct recv_data *rdata = data;
+
        if (buf_len < 1)
        if (buf_len < 1)
-               return;
+               return 1;
 
        switch(buf[0]) {
                case 'E':
                        dissect_hm(buf + 13, buf[13] + 1);
                        break;
                case 'H':
 
        switch(buf[0]) {
                case 'E':
                        dissect_hm(buf + 13, buf[13] + 1);
                        break;
                case 'H':
+                       if ((buf[27] != 0x00) ||
+                           (buf[28] != 0x00) ||
+                           (buf[29] != 0x00)) {
+                               printf("hmId is currently set to: %02x%02x%02x\n", buf[27], buf[28], buf[29]);
+                               rdata->wrong_hmid = 1;
+                       }
+                       break;
                case 'R':
                case 'I':
                        break;
                case 'R':
                case 'I':
                        break;
@@ -145,39 +164,55 @@ static void parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
                        hexdump(buf, buf_len, "Unknown> ");
                        break;
        }
                        hexdump(buf, buf_len, "Unknown> ");
                        break;
        }
-}
 
 
+       return 1;
+}
 
 int main(int argc, char **argv)
 {
        struct hmcfgusb_dev *dev;
 
 int main(int argc, char **argv)
 {
        struct hmcfgusb_dev *dev;
+       struct recv_data rdata;
        int quit = 0;
 
        hmcfgusb_set_debug(0);
 
        int quit = 0;
 
        hmcfgusb_set_debug(0);
 
-       dev = hmcfgusb_init(parse_hmcfgusb, NULL);
-       if (!dev) {
-               fprintf(stderr, "Can't initialize HM-CFG-USB!\n");
-               return EXIT_FAILURE;
-       }
+       do {
+               memset(&rdata, 0, sizeof(rdata));
+               rdata.wrong_hmid = 0;
 
 
-       hmcfgusb_send(dev, (unsigned char*)"A\00\00\00", 3, 1);
+               dev = hmcfgusb_init(parse_hmcfgusb, &rdata);
+               if (!dev) {
+                       fprintf(stderr, "Can't initialize HM-CFG-USB, retrying in 1s...\n");
+                       sleep(1);
+                       continue;
+               }
+               printf("HM-CFG-USB opened!\n");
 
 
-       while(!quit) {
-               int fd;
+               hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
 
 
-               fd = hmcfgusb_poll(dev, 3600);
-               if (fd >= 0) {
-                       fprintf(stderr, "activity on unknown fd %d!\n", fd);
-                       continue;
-               } else if (fd == -1) {
-                       if (errno) {
-                               perror("hmcfgusb_poll");
-                               quit = 1;
+               while(!quit) {
+                       int fd;
+
+                       if (rdata.wrong_hmid) {
+                               printf("changing hmId to 000000, this might reboot the device!\n");
+                               hmcfgusb_send(dev, (unsigned char*)"A\00\00\00", 4, 1);
+                               rdata.wrong_hmid = 0;
+                               hmcfgusb_send(dev, (unsigned char*)"K", 1, 1);
+                       }
+                       fd = hmcfgusb_poll(dev, 10);
+                       if (fd >= 0) {
+                               fprintf(stderr, "activity on unknown fd %d!\n", fd);
+                               continue;
+                       } else if (fd == -1) {
+                               if (errno) {
+                                       perror("hmcfgusb_poll");
+                                       break;
+                               }
                        }
                }
                        }
                }
-       }
 
 
-       hmcfgusb_close(dev);
+               hmcfgusb_close(dev);
+       } while (!quit);
+
        return EXIT_SUCCESS;
 }
        return EXIT_SUCCESS;
 }
Impressum, Datenschutz