From c75159192bb6aa368c17a06b0d989edbd6609ca3 Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sat, 31 Aug 2024 14:50:30 +0100 Subject: [PATCH] Better error handling for network_read and network_read_nb --- Changelog.md | 5 +++++ common/src/fn_network/network_read.c | 14 ++++++++++++-- common/src/fn_network/network_read_nb.c | 11 +++++++++-- fujinet-network.h | 8 ++++---- version.txt | 2 +- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 5b94458..26befd7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,6 +2,11 @@ ## [Unreleased] +## [4.5.3] - 2024-08-32 + +- network_read and network_read_nb will exit if there is a general error. + network_read will set fn_bytes_read to the bytes read into the buffer so far, for client to decide what to do. + ## [4.5.2] - 2024-08-25 - [atari] fuji_read_appkey no longer uses malloc, but requires the data buffer passed in to be at least 2 bytes larger than the keysize to work. diff --git a/common/src/fn_network/network_read.c b/common/src/fn_network/network_read.c index 67fade1..2cb05c3 100644 --- a/common/src/fn_network/network_read.c +++ b/common/src/fn_network/network_read.c @@ -101,12 +101,22 @@ int16_t network_read(char *devicespec, uint8_t *buf, uint16_t len) r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing. #endif - // check if the status failed - if (r != 0) return -r; + // check if the status failed. The buffer may be partially filled, up to client if they want to use any of it. The count is in fn_bytes_read + if (r != 0) { + fn_bytes_read = total_read; + // r is the FN_ERR code + return -r; + } // EOF hit, exit reading if (fn_network_error == 136) break; + // is there another error? The buffer may be partially filled, up to client if they want to use any of it. The count is in fn_bytes_read + if (fn_network_error != 1) { + fn_bytes_read = total_read; + return -FN_ERR_IO_ERROR; + } + // we are waiting for bytes to become available while still connected. // Causes tight loop if there's a long delay reading from network into FN, so we may see lots of status requests if (fn_network_bw == 0 && fn_network_conn == 1) { diff --git a/common/src/fn_network/network_read_nb.c b/common/src/fn_network/network_read_nb.c index 5caac36..3cc2992 100644 --- a/common/src/fn_network/network_read_nb.c +++ b/common/src/fn_network/network_read_nb.c @@ -95,12 +95,19 @@ int16_t network_read_nb(char *devicespec, uint8_t *buf, uint16_t len) r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing. #endif - // check if the status failed - if (r != 0) return -r; + // check if the status failed. + if (r != 0) { + return -r; + } // EOF hit, exit reading if (fn_network_error == 136) return 0; + // is there another error? + if (fn_network_error != 1) { + return -fn_network_error; + } + // we are waiting for bytes to become available while still connected, so no data can be read if (fn_network_bw == 0 && fn_network_conn == 1) { return 0; diff --git a/fujinet-network.h b/fujinet-network.h index a323e12..18ebea7 100644 --- a/fujinet-network.h +++ b/fujinet-network.h @@ -103,12 +103,12 @@ uint8_t network_open(char* devicespec, uint8_t mode, uint8_t trans); * @brief Non-blocking read from channel * * The read will grab whatever is waiting in the FujiNet buffer. If fewer than the requested len, the return count will reflect this. - * Errors are returned as the negative value of the error. + * Errors are returned as the negative value of the FUJI standard error. fn_network_error contains the device specific error code. fn_bytes_read will be 0 on errors. * * @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/" * @param buf Buffer * @param len length - * @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) + * @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code */ int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len); @@ -117,12 +117,12 @@ int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len); * * The read will block until it has read all the bytes requested from the device, or the EOF is hit. * This will block waiting for as much data as it can, so that the client does not need to handle counting. - * Errors are returned as the negative value of the error. + * Errors are returned as the negative value of the error. fn_network_error contains the device specific error code. fn_bytes_read will contain the count of bytes read before error occurred. * * @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/" * @param buf Buffer * @param len length - * @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) + * @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code */ int16_t network_read(char* devicespec, uint8_t *buf, uint16_t len); diff --git a/version.txt b/version.txt index 689f7fb..ae6e65b 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -4.5.2 \ No newline at end of file +4.5.3 \ No newline at end of file