From: Michael Gernoth Date: Wed, 5 Mar 2014 00:29:29 +0000 (+0100) Subject: add OTA support for culfw-based devices X-Git-Tag: v0.100~27 X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/commitdiff_plain/47ea478b44e3c529201c6678f5e2cbbe0fc60bf9 add OTA support for culfw-based devices --- diff --git a/.gitignore b/.gitignore index af9b617..17efcef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +culfw.d +culfw.o flash-hmcfgusb flash-hmcfgusb.d flash-hmcfgusb.o @@ -14,5 +16,7 @@ hmland.o hmsniff hmsniff.d hmsniff.o +util.d +util.o *.enc *.eq3 diff --git a/Makefile b/Makefile index 88b187a..ec23439 100644 --- a/Makefile +++ b/Makefile @@ -5,8 +5,8 @@ CC=gcc HMLAN_OBJS=hmcfgusb.o hmland.o HMSNIFF_OBJS=hmcfgusb.o hmsniff.o -FLASH_HMCFGUSB_OBJS=hmcfgusb.o firmware.o flash-hmcfgusb.o -FLASH_OTA_OBJS=hmcfgusb.o firmware.o flash-ota.o +FLASH_HMCFGUSB_OBJS=hmcfgusb.o firmware.o util.o flash-hmcfgusb.o +FLASH_OTA_OBJS=hmcfgusb.o culfw.o firmware.o util.o flash-ota.o OBJS=$(HMLAN_OBJS) $(HMSNIFF_OBJS) $(FLASH_HMCFGUSB_OBJS) $(FLASH_OTA_OBJS) diff --git a/culfw.c b/culfw.c new file mode 100644 index 0000000..1904bb7 --- /dev/null +++ b/culfw.c @@ -0,0 +1,176 @@ +/* culfw driver + * + * Copyright (c) 2013 Michael Gernoth + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "culfw.h" + +struct culfw_dev *culfw_init(char *device, uint32_t speed, culfw_cb_fn cb, void *data) +{ + struct culfw_dev *dev = NULL; + struct termios oldtio, tio; + uint32_t brate; + + switch(speed) { + case 115200: + brate = B115200; + break; + case 57600: + brate = B57600; + break; + case 38400: + brate = B38400; + break; + case 19200: + brate = B19200; + break; + case 9600: + brate = B9600; + break; + default: + fprintf(stderr, "Unsupported baud-rate: %u\n", speed); + return NULL; + break; + } + + dev = malloc(sizeof(struct culfw_dev)); + if (dev == NULL) { + perror("malloc(struct culfw_dev)"); + return NULL; + } + + memset(dev, 0, sizeof(struct culfw_dev)); + + dev->fd = open(device, O_RDWR | O_NOCTTY); + if (dev->fd < 0) { + perror("open(culfw)"); + goto out; + } + + if (tcgetattr(dev->fd, &oldtio) == -1) { + perror("tcgetattr"); + goto out2; + } + + memset(&tio, 0, sizeof(tio)); + + tio.c_cflag = brate | CS8 | CLOCAL | CREAD; + tio.c_iflag = IGNPAR | ICRNL; + tio.c_oflag = 0; + tio.c_lflag = ICANON; + tio.c_cc[VTIME] = 0; + tio.c_cc[VMIN] = 1; + + tcflush(dev->fd, TCIFLUSH); + if (tcsetattr(dev->fd, TCSANOW, &tio) == -1) { + perror("tcsetattr"); + goto out2; + } + + dev->cb = cb; + dev->cb_data = data; + + return dev; + +out2: + close(dev->fd); +out: + free(dev); + return NULL; +} + +int culfw_send(struct culfw_dev *dev, char *cmd, int cmdlen) +{ + int w = 0; + int ret; + + do { + ret = write(dev->fd, cmd + w, cmdlen - w); + if (ret < 0) { + perror("write"); + return 0; + } + w += ret; + } while (w < cmdlen); + + return 1; +} + +int culfw_poll(struct culfw_dev *dev, int timeout) +{ + struct pollfd pfds[1]; + int ret; + int r = 0; + uint8_t buf[1024]; + + errno = 0; + + memset(pfds, 0, sizeof(struct pollfd) * 1); + + pfds[0].fd = dev->fd; + pfds[0].events = POLLIN; + + ret = poll(pfds, 1, timeout * 1000); + if (ret == -1) + return -1; + + if (ret == 0) { + errno = ETIMEDOUT; + return -1; + } + + if (!(pfds[0].revents & POLLIN)) { + errno = EIO; + return -1; + } + + memset(buf, 0, sizeof(buf)); + r = read(dev->fd, buf, sizeof(buf)); + if (r < 0) + return -1; + + if (r == 0) { + errno = EOF; + return -1; + } + + dev->cb(buf, r, dev->cb_data); + + return pfds[0].fd; +} + +void culfw_close(struct culfw_dev *dev) +{ + close(dev->fd); +} diff --git a/culfw.h b/culfw.h new file mode 100644 index 0000000..c0b0aed --- /dev/null +++ b/culfw.h @@ -0,0 +1,37 @@ +/* culfw driver + * + * Copyright (c) 2013 Michael Gernoth + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#define DEFAULT_CUL_BPS 19200 + +typedef int (*culfw_cb_fn)(uint8_t *buf, int buf_len, void *data); + +struct culfw_dev { + int fd; + culfw_cb_fn cb; + void *cb_data; +}; + +struct culfw_dev *culfw_init(char *device, uint32_t speed, culfw_cb_fn cb, void *data); +int culfw_send(struct culfw_dev *dev, char *cmd, int cmdlen); +int culfw_poll(struct culfw_dev *dev, int timeout); +void culfw_close(struct culfw_dev *dev); diff --git a/firmware.c b/firmware.c index ff4c8be..650f035 100644 --- a/firmware.c +++ b/firmware.c @@ -33,36 +33,12 @@ #include #include +#include "util.h" #include "firmware.h" /* This might be wrong, but it works for current fw */ #define MAX_BLOCK_LENGTH 512 -static uint8_t ascii_to_nibble(uint8_t a) -{ - uint8_t c = 0x00; - - if ((a >= '0') && (a <= '9')) { - c = a - '0'; - } else if ((a >= 'A') && (a <= 'F')) { - c = (a - 'A') + 10; - } else if ((a >= 'a') && (a <= 'f')) { - c = (a - 'a') + 10; - } - - return c; -} - -static int validate_nibble(uint8_t a) -{ - if (((a >= '0') && (a <= '9')) || - ((a >= 'A') && (a <= 'F')) || - ((a >= 'a') && (a <= 'f'))) - return 1; - - return 0; -} - struct firmware* firmware_read_firmware(char *filename, int debug) { struct firmware *fw; diff --git a/flash-ota.c b/flash-ota.c index 4279aa9..ea2bcad 100644 --- a/flash-ota.c +++ b/flash-ota.c @@ -40,15 +40,30 @@ #include "hm.h" #include "version.h" #include "hmcfgusb.h" +#include "culfw.h" +#include "util.h" #define MAX_RETRIES 5 +extern char *optarg; + uint32_t hmid = 0; uint32_t my_hmid = 0; +enum device_type { + DEVICE_TYPE_HMCFGUSB, + DEVICE_TYPE_CULFW, +}; + +struct ota_dev { + int type; + struct hmcfgusb_dev *hmcfgusb; + struct culfw_dev *culfw; +}; + enum message_type { - MESSAGE_TYPE_E, - MESSAGE_TYPE_R, + MESSAGE_TYPE_E = 1, + MESSAGE_TYPE_R = 2, }; struct recv_data { @@ -100,101 +115,203 @@ static int parse_hmcfgusb(uint8_t *buf, int buf_len, void *data) return 1; } -int send_hm_message(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t *msg) +static int parse_culfw(uint8_t *buf, int buf_len, void *data) +{ + struct recv_data *rdata = data; + int pos = 0; + + memset(rdata, 0, sizeof(struct recv_data)); + + if (buf_len <= 3) + return 0; + + if (buf[0] != 'A') + return 0; + + 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 (hmid && (SRC(rdata->message) != hmid)) + return 0; + + rdata->message_type = MESSAGE_TYPE_E; + + return 1; +} + +int send_hm_message(struct ota_dev *dev, struct recv_data *rdata, uint8_t *msg) { static uint32_t id = 1; struct timeval tv; uint8_t out[0x40]; int pfd; - if (gettimeofday(&tv, NULL) == -1) { - perror("gettimeofay"); - return 0; - } - - memset(out, 0, sizeof(out)); + switch(dev->type) { + case DEVICE_TYPE_HMCFGUSB: + if (gettimeofday(&tv, NULL) == -1) { + perror("gettimeofay"); + return 0; + } - out[0] = 'S'; - out[1] = (id >> 24) & 0xff; - out[2] = (id >> 16) & 0xff; - out[3] = (id >> 8) & 0xff; - out[4] = id & 0xff; - out[10] = 0x01; - out[11] = (tv.tv_usec >> 24) & 0xff; - out[12] = (tv.tv_usec >> 16) & 0xff; - out[13] = (tv.tv_usec >> 8) & 0xff; - out[14] = tv.tv_usec & 0xff; - + memset(out, 0, sizeof(out)); - memcpy(&out[0x0f], msg, msg[0] + 1); + out[0] = 'S'; + out[1] = (id >> 24) & 0xff; + out[2] = (id >> 16) & 0xff; + out[3] = (id >> 8) & 0xff; + out[4] = id & 0xff; + out[10] = 0x01; + out[11] = (tv.tv_usec >> 24) & 0xff; + out[12] = (tv.tv_usec >> 16) & 0xff; + out[13] = (tv.tv_usec >> 8) & 0xff; + out[14] = tv.tv_usec & 0xff; + + memcpy(&out[0x0f], msg, msg[0] + 1); + + memset(rdata, 0, sizeof(struct recv_data)); + hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); + + while (1) { + if (rdata->message_type == MESSAGE_TYPE_R) { + if (((rdata->status & 0xff) == 0x01) || + ((rdata->status & 0xff) == 0x02)) { + break; + } else { + 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; + } + } + errno = 0; + pfd = hmcfgusb_poll(dev->hmcfgusb, 1); + if ((pfd < 0) && errno) { + if (errno != ETIMEDOUT) { + perror("\n\nhmcfgusb_poll"); + exit(EXIT_FAILURE); + } + } + } + break; + case DEVICE_TYPE_CULFW: + { + char buf[128]; + int i; + + memset(buf, 0, sizeof(buf)); + buf[0] = 'A'; + buf[1] = 's'; + for (i = 0; i < msg[0] + 1; i++) { + buf[2 + (i * 2)] = nibble_to_ascii((msg[i] >> 4) & 0xf); + buf[2 + (i * 2) + 1] = nibble_to_ascii(msg[i] & 0xf); + } + buf[2 + (i * 2) ] = '\r'; + buf[2 + (i * 2) + 1] = '\n'; - memset(rdata, 0, sizeof(struct recv_data)); - hmcfgusb_send(dev, out, sizeof(out), 1); + memset(rdata, 0, sizeof(struct recv_data)); + if (culfw_send(dev->culfw, buf, 2 + (i * 2) + 1) == 0) { + fprintf(stderr, "culfw_send failed!\n"); + exit(EXIT_FAILURE); + } - while (1) { - if (rdata->message_type == MESSAGE_TYPE_R) { - if (((rdata->status & 0xff) == 0x01) || - ((rdata->status & 0xff) == 0x02)) { - break; - } else { - 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); + if (msg[CTL] & 0x20) { + int cnt = 10; + int pfd; + do { + errno = 0; + pfd = culfw_poll(dev->culfw, 1); + if ((pfd < 0) && errno) { + if (errno != ETIMEDOUT) { + perror("\n\nhmcfgusb_poll"); + exit(EXIT_FAILURE); + } + } + if (rdata->message_type == MESSAGE_TYPE_E) { + break; + } + } while(cnt--); } - return 0; } - } - errno = 0; - pfd = hmcfgusb_poll(dev, 1); - if ((pfd < 0) && errno) { - if (errno != ETIMEDOUT) { - perror("\n\nhmcfgusb_poll"); - exit(EXIT_FAILURE); - } - } + break; } id++; return 1; } -static int switch_speed(struct hmcfgusb_dev *dev, struct recv_data *rdata, uint8_t speed) +static int switch_speed(struct ota_dev *dev, struct recv_data *rdata, uint8_t speed) { uint8_t out[0x40]; int pfd; printf("Entering %uk-mode\n", speed); - memset(out, 0, sizeof(out)); - out[0] = 'G'; - out[1] = speed; - - hmcfgusb_send(dev, out, sizeof(out), 1); - - while (1) { - errno = 0; - pfd = hmcfgusb_poll(dev, 1); - if ((pfd < 0) && errno) { - if (errno != ETIMEDOUT) { - perror("\n\nhmcfgusb_poll"); - exit(EXIT_FAILURE); + switch(dev->type) { + case DEVICE_TYPE_HMCFGUSB: + memset(out, 0, sizeof(out)); + out[0] = 'G'; + out[1] = speed; + + hmcfgusb_send(dev->hmcfgusb, out, sizeof(out), 1); + + while (1) { + errno = 0; + pfd = hmcfgusb_poll(dev->hmcfgusb, 1); + if ((pfd < 0) && errno) { + if (errno != ETIMEDOUT) { + perror("\n\nhmcfgusb_poll"); + exit(EXIT_FAILURE); + } + } + if (rdata->speed == speed) + break; + } + break; + case DEVICE_TYPE_CULFW: + if (speed == 100) { + return culfw_send(dev->culfw, "AR\r\n", 4); + } else { + return culfw_send(dev->culfw, "Ar\r\n", 4); } - } - if (rdata->speed == speed) break; } return 1; } +void flash_ota_syntax(char *prog) +{ + fprintf(stderr, "Syntax: %s parameters options\n\n", prog); + fprintf(stderr, "Mandatory parameters:\n"); + fprintf(stderr, "\t-f firmware.eq3\tfirmware file to flash\n"); + fprintf(stderr, "\t-s SERIAL\tserial of device to flash\n"); + 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-h\t\tthis help\n"); +} + int main(int argc, char **argv) { const char twiddlie[] = { '-', '\\', '|', '/' }; const uint8_t cc1101_regs[] = { 0x10, 0x5B, 0x11, 0xF8, 0x15, 0x47 }; - struct hmcfgusb_dev *dev; + char *fw_file = NULL; + char *serial = NULL; + char *culfw_dev = NULL; + unsigned int bps = DEFAULT_CUL_BPS; + struct ota_dev dev; struct recv_data rdata; uint8_t out[0x40]; uint8_t *pos; @@ -208,100 +325,140 @@ int main(int argc, char **argv) int switchcnt = 0; int msgnum = 0; int switched = 0; + int opt; printf("HomeMatic OTA flasher version " VERSION "\n\n"); - if (argc != 3) { - if (argc == 1) - fprintf(stderr, "Missing firmware filename!\n\n"); + while((opt = getopt(argc, argv, "f:s:c:s:h")) != -1) { + switch (opt) { + case 'b': + bps = atoi(optarg); + break; + case 'c': + culfw_dev = optarg; + break; + case 'f': + fw_file = optarg; + break; + case 's': + serial = optarg; + break; + case 'h': + case ':': + case '?': + default: + flash_ota_syntax(argv[0]); + exit(EXIT_FAILURE); + break; - if (argc == 2) - fprintf(stderr, "Missing serial!\n\n"); + } + } - fprintf(stderr, "Syntax: %s firmware.eq3 SERIALNUMBER\n\n", argv[0]); + if (!fw_file || !serial) { + flash_ota_syntax(argv[0]); exit(EXIT_FAILURE); } - fw = firmware_read_firmware(argv[1], debug); + fw = firmware_read_firmware(fw_file, debug); if (!fw) exit(EXIT_FAILURE); - hmcfgusb_set_debug(debug); - memset(&rdata, 0, sizeof(rdata)); + memset(&dev, 0, sizeof(struct ota_dev)); - dev = hmcfgusb_init(parse_hmcfgusb, &rdata); - if (!dev) { - fprintf(stderr, "Can't initialize HM-CFG-USB\n"); - exit(EXIT_FAILURE); - } + if (culfw_dev) { + 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; + } else { + hmcfgusb_set_debug(debug); - printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n"); + dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata); + if (!dev.hmcfgusb) { + fprintf(stderr, "Can't initialize HM-CFG-USB\n"); + exit(EXIT_FAILURE); + } + dev.type = DEVICE_TYPE_HMCFGUSB; - 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"); + printf("\nRebooting HM-CFG-USB to avoid running out of credits\n\n"); - do { - if (dev) { - hmcfgusb_close(dev); - } - sleep(1); - } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev->bootloader)); - } + 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"); - if (dev->bootloader) { - printf("HM-CFG-USB in bootloader mode, rebooting\n"); - hmcfgusb_leave_bootloader(dev); + do { + if (dev.hmcfgusb) { + hmcfgusb_close(dev.hmcfgusb); + } + sleep(1); + } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (!dev.hmcfgusb->bootloader)); + } - do { - if (dev) { - hmcfgusb_close(dev); - } - sleep(1); - } while (((dev = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev->bootloader)); - } + if (dev.hmcfgusb->bootloader) { + printf("HM-CFG-USB in bootloader mode, rebooting\n"); + hmcfgusb_leave_bootloader(dev.hmcfgusb); - printf("\n\nHM-CFG-USB opened\n\n"); + do { + if (dev.hmcfgusb) { + hmcfgusb_close(dev.hmcfgusb); + } + sleep(1); + } while (((dev.hmcfgusb = hmcfgusb_init(parse_hmcfgusb, &rdata)) == NULL) || (dev.hmcfgusb->bootloader)); + } - memset(out, 0, sizeof(out)); - out[0] = 'K'; - hmcfgusb_send(dev, out, sizeof(out), 1); + printf("\n\nHM-CFG-USB opened\n\n"); - while (1) { - errno = 0; - pfd = hmcfgusb_poll(dev, 1); - if ((pfd < 0) && errno) { - if (errno != ETIMEDOUT) { - perror("\n\nhmcfgusb_poll"); - exit(EXIT_FAILURE); + memset(out, 0, sizeof(out)); + out[0] = 'K'; + hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); + + while (1) { + errno = 0; + pfd = hmcfgusb_poll(dev.hmcfgusb, 1); + if ((pfd < 0) && errno) { + if (errno != ETIMEDOUT) { + perror("\n\nhmcfgusb_poll"); + exit(EXIT_FAILURE); + } } + if (rdata.hmcfgusb_version) + break; } - if (rdata.hmcfgusb_version) - break; - } - if (rdata.hmcfgusb_version < 0x3c7) { - fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.hmcfgusb_version); - exit(EXIT_FAILURE); - } + if (rdata.hmcfgusb_version < 0x3c7) { + fprintf(stderr, "HM-CFG-USB firmware too low: %u < 967\n", rdata.hmcfgusb_version); + exit(EXIT_FAILURE); + } - printf("HM-CFG-USB firmware version: %u\n", rdata.hmcfgusb_version); + printf("HM-CFG-USB firmware version: %u\n", rdata.hmcfgusb_version); + } - if (!switch_speed(dev, &rdata, 10)) { + if (!switch_speed(&dev, &rdata, 10)) { fprintf(stderr, "Can't switch speed!\n"); exit(EXIT_FAILURE); } - printf("Waiting for device with serial %s\n", argv[2]); + printf("Waiting for device with serial %s\n", serial); while (1) { - errno = 0; - pfd = hmcfgusb_poll(dev, 1); + switch (dev.type) { + errno = 0; + case DEVICE_TYPE_CULFW: + pfd = culfw_poll(dev.culfw, 1); + break; + case DEVICE_TYPE_HMCFGUSB: + default: + pfd = hmcfgusb_poll(dev.hmcfgusb, 1); + break; + } + if ((pfd < 0) && errno) { if (errno != ETIMEDOUT) { - perror("\n\nhmcfgusb_poll"); + perror("\n\npoll"); exit(EXIT_FAILURE); } } @@ -311,27 +468,27 @@ int main(int argc, char **argv) (rdata.message[CTL] == 0x00) && /* Control Byte */ (rdata.message[TYPE] == 0x10) && /* Messagte type: Information */ (DST(rdata.message) == 0x000000) && /* Broadcast */ - (rdata.message[PAYLOAD] == 0x00) && /* FUP? */ - (rdata.message[PAYLOAD+2] == 'E') && - (rdata.message[PAYLOAD+3] == 'Q')) { - if (!strncmp((char*)&(rdata.message[0x0b]), argv[2], 10)) { + (rdata.message[PAYLOAD] == 0x00)) { /* FUP? */ + if (!strncmp((char*)&(rdata.message[0x0b]), serial, 10)) { hmid = SRC(rdata.message); break; } } } - printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", argv[2], hmid); + printf("Device with serial %s (hmid: %06x) entered firmware-update-mode\n", serial, hmid); - printf("Adding HMID\n"); + if (dev.type == DEVICE_TYPE_HMCFGUSB) { + printf("Adding HMID\n"); - memset(out, 0, sizeof(out)); - out[0] = '+'; - out[1] = (hmid >> 16) & 0xff; - out[2] = (hmid >> 8) & 0xff; - out[3] = hmid & 0xff; + memset(out, 0, sizeof(out)); + out[0] = '+'; + out[1] = (hmid >> 16) & 0xff; + out[2] = (hmid >> 8) & 0xff; + out[3] = hmid & 0xff; - hmcfgusb_send(dev, out, sizeof(out), 1); + hmcfgusb_send(dev.hmcfgusb, out, sizeof(out), 1); + } switchcnt = 3; do { @@ -348,11 +505,11 @@ int main(int argc, char **argv) memcpy(&out[PAYLOAD], cc1101_regs, sizeof(cc1101_regs)); SET_LEN_FROM_PAYLOADLEN(out, sizeof(cc1101_regs)); - if (!send_hm_message(dev, &rdata, out)) { + if (!send_hm_message(&dev, &rdata, out)) { exit(EXIT_FAILURE); } - if (!switch_speed(dev, &rdata, 100)) { + if (!switch_speed(&dev, &rdata, 100)) { fprintf(stderr, "Can't switch speed!\n"); exit(EXIT_FAILURE); } @@ -372,18 +529,17 @@ int main(int argc, char **argv) cnt = 3; do { - if (send_hm_message(dev, &rdata, out)) { + if (send_hm_message(&dev, &rdata, out)) { /* A0A02000221B9AD00000000 */ switched = 1; break; - } } while (cnt--); if (!switched) { printf("No!\n"); - if (!switch_speed(dev, &rdata, 10)) { + if (!switch_speed(&dev, &rdata, 10)) { fprintf(stderr, "Can't switch speed!\n"); exit(EXIT_FAILURE); } @@ -449,7 +605,7 @@ int main(int argc, char **argv) memcpy(&out[PAYLOAD], pos, payloadlen); SET_LEN_FROM_PAYLOADLEN(out, payloadlen); - if (send_hm_message(dev, &rdata, out)) { + if (send_hm_message(&dev, &rdata, out)) { pos += payloadlen; } else { pos = &(fw->fw[block][2]); @@ -477,7 +633,7 @@ int main(int argc, char **argv) printf("\n"); - if (!switch_speed(dev, &rdata, 10)) { + if (!switch_speed(&dev, &rdata, 10)) { fprintf(stderr, "Can't switch speed!\n"); exit(EXIT_FAILURE); } @@ -487,7 +643,15 @@ int main(int argc, char **argv) cnt = 10; do { errno = 0; - pfd = hmcfgusb_poll(dev, 1); + switch(dev.type) { + case DEVICE_TYPE_CULFW: + pfd = culfw_poll(dev.culfw, 1); + break; + case DEVICE_TYPE_HMCFGUSB: + default: + pfd = hmcfgusb_poll(dev.hmcfgusb, 1); + break; + } if ((pfd < 0) && errno) { if (errno != ETIMEDOUT) { perror("\n\nhmcfgusb_poll"); @@ -503,7 +667,14 @@ int main(int argc, char **argv) printf("Device rebooted\n"); } - hmcfgusb_close(dev); + switch(dev.type) { + case DEVICE_TYPE_HMCFGUSB: + hmcfgusb_close(dev.hmcfgusb); + break; + case DEVICE_TYPE_CULFW: + culfw_close(dev.culfw); + break; + } return EXIT_SUCCESS; } diff --git a/util.c b/util.c new file mode 100644 index 0000000..00ecb7e --- /dev/null +++ b/util.c @@ -0,0 +1,57 @@ +/* utility functions + * + * Copyright (c) 2014 Michael Gernoth + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include + +uint8_t ascii_to_nibble(uint8_t a) +{ + uint8_t c = 0x00; + + if ((a >= '0') && (a <= '9')) { + c = a - '0'; + } else if ((a >= 'A') && (a <= 'F')) { + c = (a - 'A') + 10; + } else if ((a >= 'a') && (a <= 'f')) { + c = (a - 'a') + 10; + } + + return c; +} + +int validate_nibble(uint8_t a) +{ + if (((a >= '0') && (a <= '9')) || + ((a >= 'A') && (a <= 'F')) || + ((a >= 'a') && (a <= 'f'))) + return 1; + + return 0; +} + +char nibble_to_ascii(uint8_t n) +{ + const uint8_t nibble[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F'}; + + return nibble[n]; +} diff --git a/util.h b/util.h new file mode 100644 index 0000000..93947fc --- /dev/null +++ b/util.h @@ -0,0 +1,26 @@ +/* utility functions + * + * Copyright (c) 2014 Michael Gernoth + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +uint8_t ascii_to_nibble(uint8_t a); +int validate_nibble(uint8_t a); +char nibble_to_ascii(uint8_t n); diff --git a/version.h b/version.h index c5a8e57..417afbb 100644 --- a/version.h +++ b/version.h @@ -1 +1 @@ -#define VERSION "0.094-git" +#define VERSION "0.095-git"