the switch_msg are actually CC1101-registers
[hmcfgusb] / flash-ota.c
index 952d78c8cb2ee28f6694e64e9d5816b42e14a320..4279aa9cac05010113d467a8e0e7407a1c6cc0f9 100644 (file)
@@ -41,6 +41,8 @@
 #include "version.h"
 #include "hmcfgusb.h"
 
+#define MAX_RETRIES    5
+
 uint32_t hmid = 0;
 uint32_t my_hmid = 0;
 
@@ -135,7 +137,13 @@ int send_hm_message(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t *
                            ((rdata->status & 0xff) == 0x02)) {
                                break;
                        } else {
-                               fprintf(stderr, "\nInvalid status: %04x\n", rdata->status);
+                               if ((rdata->status & 0xff00) == 0x0400) {
+                                       fprintf(stderr, "\nOut of credits!\n");
+                               } else if ((rdata->status & 0xff) == 0x08) {
+                                       fprintf(stderr, "\nMissing ACK!\n");
+                               } else {
+                                       fprintf(stderr, "\nInvalid status: %04x\n", rdata->status);
+                               }
                                return 0;
                        }
                }
@@ -185,7 +193,7 @@ static int switch_speed(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8
 int main(int argc, char **argv)
 {
        const char twiddlie[] = { '-', '\\', '|', '/' };
-       const uint8_t switch_msg[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 };
+       const uint8_t cc1101_regs[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 };
        struct hmcfgusb_dev *dev;
        struct recv_data rdata;
        uint8_t out[0x40];
@@ -228,12 +236,34 @@ int main(int argc, char **argv)
                exit(EXIT_FAILURE);
        }
 
+       printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n");
+
+       if (!dev->bootloader) {
+               printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
+               hmcfgusb_enter_bootloader(dev);
+               printf("Waiting for device to reappear...\n");
+
+               do {
+                       if (dev) {
+                               hmcfgusb_close(dev);
+                       }
+                       sleep(1);
+               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev->bootloader));
+       }
+
        if (dev->bootloader) {
-               fprintf(stderr, "\nHM-CFG-USB not in bootloader mode, aborting!\n");
-               exit(EXIT_FAILURE);
+               printf("HM-CFG-USB in bootloader mode, rebooting\n");
+               hmcfgusb_leave_bootloader(dev);
+
+               do {
+                       if (dev) {
+                               hmcfgusb_close(dev);
+                       }
+                       sleep(1);
+               } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev->bootloader));
        }
 
-       printf("\nHM-CFG-USB opened\n\n");
+       printf("\n\nHM-CFG-USB opened\n\n");
 
        memset(out, 0, sizeof(out));
        out[0] = 'K';
@@ -315,8 +345,8 @@ int main(int argc, char **argv)
                SET_SRC(out, my_hmid);
                SET_DST(out, hmid);
 
-               memcpy(&out[PAYLOAD], switch_msg, sizeof(switch_msg));
-               SET_LEN_FROM_PAYLOADLEN(out, sizeof(switch_msg));
+               memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs));
+               SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs));
 
                if (!send_hm_message(dev, &rdata, out)) {
                        exit(EXIT_FAILURE);
@@ -337,8 +367,8 @@ int main(int argc, char **argv)
                SET_SRC(out, my_hmid);
                SET_DST(out, hmid);
 
-               memcpy(&out[PAYLOAD], switch_msg, sizeof(switch_msg));
-               SET_LEN_FROM_PAYLOADLEN(out, sizeof(switch_msg));
+               memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs));
+               SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs));
 
                cnt = 3;
                do {
@@ -424,7 +454,7 @@ int main(int argc, char **argv)
                        } else {
                                pos = &(fw->fw[block][2]);
                                cnt++;
-                               if (cnt == 3) {
+                               if (cnt == MAX_RETRIES) {
                                        fprintf(stderr, "\nToo many errors, giving up!\n");
                                        exit(EXIT_FAILURE);
                                } else {
Impressum, Datenschutz