From: Michael Gernoth Date: Wed, 8 Mar 2017 12:38:15 +0000 (+0100) Subject: hmuartlgw: Add timeout during module initialization X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/hmcfgusb/commitdiff_plain/8eb016242854bf51e73df027a13d4d3fe5f6e561 hmuartlgw: Add timeout during module initialization --- diff --git a/flash-hmmoduart.c b/flash-hmmoduart.c index e8487d3..aa772bb 100644 --- a/flash-hmmoduart.c +++ b/flash-hmmoduart.c @@ -1,6 +1,6 @@ /* flasher for HM-MOD-UART * - * Copyright (c) 2016 Michael Gernoth + * Copyright (c) 2016-17 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 @@ -125,13 +125,15 @@ int main(int argc, char **argv) memset(&rdata, 0, sizeof(rdata)); + printf("\nInitializing HM-MOD-UART...\n"); + dev = hmuart_init(uart, parse_hmuartlgw, &rdata, 0); if (!dev) { fprintf(stderr, "Can't initialize HM-MOD-UART\n"); exit(EXIT_FAILURE); } - printf("\nHM-MOD-UART opened.\n\n"); + printf("HM-MOD-UART opened.\n\n"); printf("Flashing %d blocks", fw->fw_blocks); if (debug) { diff --git a/hmuartlgw.c b/hmuartlgw.c index 52aa2a4..b109b39 100644 --- a/hmuartlgw.c +++ b/hmuartlgw.c @@ -287,6 +287,7 @@ void hmuartlgw_enter_bootloader(struct hmuartlgw_dev *dev) void *cb_data_old = dev->cb_data; struct recv_data rdata = { 0 }; uint8_t buf[128] = { 0 }; + int ret; if (debug) { fprintf(stderr, "Entering bootloader\n"); @@ -300,7 +301,12 @@ void hmuartlgw_enter_bootloader(struct hmuartlgw_dev *dev) buf[0] = HMUARTLGW_OS_GET_APP; hmuartlgw_send(dev, buf, 1, HMUARTLGW_OS); do { - hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + errno = 0; + ret = hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + if (ret == -1 && errno == ETIMEDOUT) { + fprintf(stderr, "Communication with the module timed out, is the serial port configured correctly?\n"); + exit(1); + } } while (rdata.state == HMUARTLGW_QUERY_APPSTATE); if (rdata.state != HMUARTLGW_BOOTLOADER) { @@ -309,7 +315,12 @@ void hmuartlgw_enter_bootloader(struct hmuartlgw_dev *dev) buf[0] = HMUARTLGW_OS_CHANGE_APP; hmuartlgw_send(dev, buf, 1, HMUARTLGW_OS); do { - hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + errno = 0; + ret = hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + if (ret == -1 && errno == ETIMEDOUT) { + fprintf(stderr, "Communication with the module timed out, is the serial port configured correctly?\n"); + exit(1); + } } while (rdata.state != HMUARTLGW_BOOTLOADER); printf("Waiting for bootloader to settle...\n"); @@ -326,6 +337,7 @@ void hmuartlgw_enter_app(struct hmuartlgw_dev *dev) void *cb_data_old = dev->cb_data; struct recv_data rdata = { 0 }; uint8_t buf[128] = { 0 }; + int ret; if (debug) { fprintf(stderr, "Entering application\n"); @@ -339,7 +351,12 @@ void hmuartlgw_enter_app(struct hmuartlgw_dev *dev) buf[0] = HMUARTLGW_OS_GET_APP; hmuartlgw_send(dev, buf, 1, HMUARTLGW_OS); do { - hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + errno = 0; + ret = hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + if (ret == -1 && errno == ETIMEDOUT) { + fprintf(stderr, "Communication with the module timed out, is the serial port configured correctly?\n"); + exit(1); + } } while (rdata.state == HMUARTLGW_QUERY_APPSTATE); if ((rdata.state != HMUARTLGW_APPLICATION) && @@ -349,7 +366,12 @@ void hmuartlgw_enter_app(struct hmuartlgw_dev *dev) buf[0] = HMUARTLGW_OS_CHANGE_APP; hmuartlgw_send(dev, buf, 1, HMUARTLGW_OS); do { - hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + errno = 0; + ret = hmuartlgw_poll(dev, HMUARTLGW_INIT_TIMEOUT); + if (ret == -1 && errno == ETIMEDOUT) { + fprintf(stderr, "Communication with the module timed out, is the serial port configured correctly?\n"); + exit(1); + } } while ((rdata.state != HMUARTLGW_APPLICATION) && (rdata.state != HMUARTLGW_DUAL_APPLICATION));