flash-ota: fix typo
[hmcfgusb] / flash-ota.c
index 71cb57e548bbe35970a571ba516185f6291f6925..b9b96cc05a3959c25e49647e1b7238c4fe08e61a 100644 (file)
 #include "culfw.h"
 #include "util.h"
 
-#define MAX_RETRIES    5
+#define MAX_RETRIES            5
+#define NORMAL_MAX_PAYLOAD     37
+#define LOWER_MAX_PAYLOAD      17
 
 extern char *optarg;
 
 uint32_t hmid = 0;
 uint32_t my_hmid = 0;
 
+/* Maximum payloadlen supported by IO */
+uint32_t max_payloadlen = NORMAL_MAX_PAYLOAD;
+
 enum device_type {
        DEVICE_TYPE_HMCFGUSB,
        DEVICE_TYPE_CULFW,
@@ -71,7 +76,7 @@ struct recv_data {
        enum message_type message_type;
        uint16_t status;
        int speed;
-       uint16_t hmcfgusb_version;
+       uint16_t version;
 };
 
 static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
@@ -102,7 +107,7 @@ static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data)
                        rdata->speed = buf[1];
                        break;
                case 'H':
-                       rdata->hmcfgusb_version = (buf[11] << 8) | buf[12];
+                       rdata->version = (buf[11] << 8) | buf[12];
                        my_hmid = (buf[0x1b] << 16) | (buf[0x1c] << 8) | buf[0x1d];
                        break;
                default:
@@ -125,25 +130,57 @@ static int parse_culfw(uint8_t *buf, int buf_len, void *data)
        if (buf_len <= 3)
                return 0;
 
-       if (buf[0] != 'A')
-               return 0;
+       switch(buf[0]) {
+               case 'A':
+                       if (buf[1] == 's')
+                               return 0;
+
+                       while(validate_nibble(buf[(pos * 2) + 1]) &&
+                             validate_nibble(buf[(pos * 2) + 2]) &&
+                             (pos + 1 < buf_len)) {
+                               rdata->message[pos] = ascii_to_nibble(buf[(pos * 2) + 1]) << 4;
+                               rdata->message[pos] |= ascii_to_nibble(buf[(pos * 2) + 2]);
+                               pos++;
+                       }
 
-       if (buf[1] == 's')
-               return 0;
+                       if (hmid && (SRC(rdata->message) != hmid))
+                               return 0;
 
-       while(validate_nibble(buf[(pos * 2) + 1]) &&
-             validate_nibble(buf[(pos * 2) + 2]) &&
-             (pos + 1 < buf_len)) {
-               rdata->message[pos] = ascii_to_nibble(buf[(pos * 2) + 1]) << 4;
-               rdata->message[pos] |= ascii_to_nibble(buf[(pos * 2) + 2]);
-               pos++;
+                       rdata->message_type = MESSAGE_TYPE_E;
+                       break;
+               case 'V':
+                       {
+                               uint8_t v;
+                               char *s;
+                               char *e;
+
+                               s = ((char*)buf) + 2;
+                               e = strchr(s, '.');
+                               if (!e) {
+                                       fprintf(stderr, "Unknown response from CUL: %s", buf);
+                                       return 0;
+                               }
+                               *e = '\0';
+                               v = atoi(s);
+                               rdata->version = v << 8;
+
+                               s = e + 1;
+                               e = strchr(s, ' ');
+                               if (!e) {
+                                       fprintf(stderr, "Unknown response from CUL: %s", buf);
+                                       return 0;
+                               }
+                               *e = '\0';
+                               v = atoi(s);
+                               rdata->version |= v;
+                       }
+                       break;
+               default:
+                       fprintf(stderr, "Unknown response from CUL: %s", buf);
+                       return 0;
+                       break;
        }
 
-       if (hmid && (SRC(rdata->message) != hmid))
-               return 0;
-
-       rdata->message_type = MESSAGE_TYPE_E;
-
        return 1;
 }
 
@@ -196,7 +233,7 @@ int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg)
                                        }
                                }
                                errno = 0;
-                               pfd = hmcfgusb_poll(dev->hmcfgusb, 1);
+                               pfd = hmcfgusb_poll(dev->hmcfgusb, 1000);
                                if ((pfd < 0) && errno) {
                                        if (errno != ETIMEDOUT) {
                                                perror("\n\nhmcfgusb_poll");
@@ -207,7 +244,7 @@ int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg)
                        break;
                case DEVICE_TYPE_CULFW:
                        {
-                               char buf[128];
+                               char buf[256];
                                int i;
 
                                memset(buf, 0, sizeof(buf));
@@ -231,10 +268,10 @@ int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg)
                                        int pfd;
                                        do {
                                                errno = 0;
-                                               pfd = culfw_poll(dev->culfw, 1);
+                                               pfd = culfw_poll(dev->culfw, 200);
                                                if ((pfd < 0) && errno) {
                                                        if (errno != ETIMEDOUT) {
-                                                               perror("\n\nhmcfgusb_poll");
+                                                               perror("\n\nculfw_poll");
                                                                exit(EXIT_FAILURE);
                                                        }
                                                }
@@ -273,7 +310,7 @@ static int switch_speed(struct ota_dev *dev, struct recv_data *rdata, uint8_t sp
 
                        while (1) {
                                errno = 0;
-                               pfd = hmcfgusb_poll(dev->hmcfgusb, 1);
+                               pfd = hmcfgusb_poll(dev->hmcfgusb, 1000);
                                if ((pfd < 0) && errno) {
                                        if (errno != ETIMEDOUT) {
                                                perror("\n\nhmcfgusb_poll");
@@ -305,6 +342,7 @@ void flash_ota_syntax(char *prog)
        fprintf(stderr, "\nPossible options:\n");
        fprintf(stderr, "\t-c device\tenable CUL-mode with CUL at path \"device\"\n");
        fprintf(stderr, "\t-b bps\t\tuse CUL with speed \"bps\" (default: %u)\n", DEFAULT_CUL_BPS);
+       fprintf(stderr, "\t-l\t\tlower payloadlen (required for devices with little RAM, e.g. CUL v2 and CUL v4)\n");
        fprintf(stderr, "\t-h\t\tthis help\n");
 }
 
@@ -334,7 +372,7 @@ int main(int argc, char **argv)
 
        printf("HomeMatic OTA flasher version " VERSION "\n\n");
 
-       while((opt = getopt(argc, argv, "b:c:f:hs:")) != -1) {
+       while((opt = getopt(argc, argv, "b:c:f:hls:")) != -1) {
                switch (opt) {
                        case 'b':
                                bps = atoi(optarg);
@@ -345,6 +383,10 @@ int main(int argc, char **argv)
                        case 'f':
                                fw_file = optarg;
                                break;
+                       case 'l':
+                               printf("Reducing payload-len from %d to %d\n", max_payloadlen, LOWER_MAX_PAYLOAD);
+                               max_payloadlen = LOWER_MAX_PAYLOAD;
+                               break;
                        case 's':
                                serial = optarg;
                                break;
@@ -372,12 +414,41 @@ int main(int argc, char **argv)
        memset(&dev, 0, sizeof(struct ota_dev));
 
        if (culfw_dev) {
+               printf("Opening culfw-device at path %s with speed %u\n", culfw_dev, bps);
                dev.culfw = culfw_init(culfw_dev, bps, parse_culfw, &rdata);
                if (!dev.culfw) {
                        fprintf(stderr, "Can't initialize CUL at %s with rate %u\n", culfw_dev, bps);
                        exit(EXIT_FAILURE);
                }
                dev.type = DEVICE_TYPE_CULFW;
+
+               printf("Requesting firmware version\n");
+               culfw_send(dev.culfw, "\r\n", 2);
+               culfw_flush(dev.culfw);
+
+               while (1) {
+                       culfw_send(dev.culfw, "V\r\n", 3);
+
+                       errno = 0;
+                       pfd = culfw_poll(dev.culfw, 1000);
+                       if ((pfd < 0) && errno) {
+                               if (errno != ETIMEDOUT) {
+                                       perror("\n\nhmcfgusb_poll");
+                                       exit(EXIT_FAILURE);
+                               }
+                       }
+                       if (rdata.version)
+                               break;
+               }
+
+               printf("culfw-device firmware version: %u.%02u\n", 
+                       (rdata.version >> 8) & 0xff,
+                       rdata.version & 0xff);
+
+               if (rdata.version < 0x013a) {
+                       fprintf(stderr, "\nThis version does _not_ support firmware upgrade mode, you need at least 1.58!\n");
+                       exit(EXIT_FAILURE);
+               }
        } else {
                hmcfgusb_set_debug(debug);
 
@@ -392,11 +463,12 @@ int main(int argc, char **argv)
 
                if (!dev.hmcfgusb->bootloader) {
                        printf("HM-CFG-USB not in bootloader mode, entering bootloader.\n");
-                       hmcfgusb_enter_bootloader(dev.hmcfgusb);
                        printf("Waiting for device to reappear...\n");
 
                        do {
                                if (dev.hmcfgusb) {
+                                       if (!dev.hmcfgusb->bootloader)
+                                               hmcfgusb_enter_bootloader(dev.hmcfgusb);
                                        hmcfgusb_close(dev.hmcfgusb);
                                }
                                sleep(1);
@@ -405,10 +477,11 @@ int main(int argc, char **argv)
 
                if (dev.hmcfgusb->bootloader) {
                        printf("HM-CFG-USB in bootloader mode, rebooting\n");
-                       hmcfgusb_leave_bootloader(dev.hmcfgusb);
 
                        do {
                                if (dev.hmcfgusb) {
+                                       if (dev.hmcfgusb->bootloader)
+                                               hmcfgusb_leave_bootloader(dev.hmcfgusb);
                                        hmcfgusb_close(dev.hmcfgusb);
                                }
                                sleep(1);
@@ -423,23 +496,23 @@ int main(int argc, char **argv)
 
                while (1) {
                        errno = 0;
-                       pfd = hmcfgusb_poll(dev.hmcfgusb, 1);
+                       pfd = hmcfgusb_poll(dev.hmcfgusb, 1000);
                        if ((pfd < 0) && errno) {
                                if (errno != ETIMEDOUT) {
                                        perror("\n\nhmcfgusb_poll");
                                        exit(EXIT_FAILURE);
                                }
                        }
-                       if (rdata.hmcfgusb_version)
+                       if (rdata.version)
                                break;
                }
 
-               if (rdata.hmcfgusb_version < 0x3c7) {
-                       fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.hmcfgusb_version);
+               if (rdata.version < 0x3c7) {
+                       fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.version);
                        exit(EXIT_FAILURE);
                }
 
-               printf("HM-CFG-USB firmware version: %u\n", rdata.hmcfgusb_version);
+               printf("HM-CFG-USB firmware version: %u\n", rdata.version);
        }
 
        if (!switch_speed(&dev, &rdata, 10)) {
@@ -450,14 +523,14 @@ int main(int argc, char **argv)
        printf("Waiting for device with serial %s\n", serial);
 
        while (1) {
+               errno = 0;
                switch (dev.type) {
-                       errno = 0;
                        case DEVICE_TYPE_CULFW:
-                               pfd = culfw_poll(dev.culfw, 1);
+                               pfd = culfw_poll(dev.culfw, 1000);
                                break;
                        case DEVICE_TYPE_HMCFGUSB:
                        default:
-                               pfd = hmcfgusb_poll(dev.hmcfgusb, 1);
+                               pfd = hmcfgusb_poll(dev.hmcfgusb, 1000);
                                break;
                }
 
@@ -582,11 +655,11 @@ int main(int argc, char **argv)
                first = 1;
                cnt = 0;
                do {
-                       int payloadlen = 35;
+                       int payloadlen = max_payloadlen - 2;
                        int ack = 0;
 
                        if (first) {
-                               payloadlen = 37;
+                               payloadlen = max_payloadlen;
                                first = 0;
                        }
 
@@ -650,16 +723,16 @@ int main(int argc, char **argv)
                errno = 0;
                switch(dev.type) {
                        case DEVICE_TYPE_CULFW:
-                               pfd = culfw_poll(dev.culfw, 1);
+                               pfd = culfw_poll(dev.culfw, 1000);
                                break;
                        case DEVICE_TYPE_HMCFGUSB:
                        default:
-                               pfd = hmcfgusb_poll(dev.hmcfgusb, 1);
+                               pfd = hmcfgusb_poll(dev.hmcfgusb, 1000);
                                break;
                }
                if ((pfd < 0) && errno) {
                        if (errno != ETIMEDOUT) {
-                               perror("\n\nhmcfgusb_poll");
+                               perror("\n\npoll");
                                exit(EXIT_FAILURE);
                        }
                }
@@ -675,6 +748,7 @@ int main(int argc, char **argv)
        switch(dev.type) {
                case DEVICE_TYPE_HMCFGUSB:
                        hmcfgusb_close(dev.hmcfgusb);
+                       hmcfgusb_exit();
                        break;
                case DEVICE_TYPE_CULFW:
                        culfw_close(dev.culfw);
Impressum, Datenschutz