Skip to content

Commit c751591

Browse files
committed
Better error handling for network_read and network_read_nb
1 parent 971197a commit c751591

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased]
44

5+
## [4.5.3] - 2024-08-32
6+
7+
- network_read and network_read_nb will exit if there is a general error.
8+
network_read will set fn_bytes_read to the bytes read into the buffer so far, for client to decide what to do.
9+
510
## [4.5.2] - 2024-08-25
611

712
- [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.

common/src/fn_network/network_read.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,22 @@ int16_t network_read(char *devicespec, uint8_t *buf, uint16_t len)
101101
r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing.
102102
#endif
103103

104-
// check if the status failed
105-
if (r != 0) return -r;
104+
// 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
105+
if (r != 0) {
106+
fn_bytes_read = total_read;
107+
// r is the FN_ERR code
108+
return -r;
109+
}
106110

107111
// EOF hit, exit reading
108112
if (fn_network_error == 136) break;
109113

114+
// 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
115+
if (fn_network_error != 1) {
116+
fn_bytes_read = total_read;
117+
return -FN_ERR_IO_ERROR;
118+
}
119+
110120
// we are waiting for bytes to become available while still connected.
111121
// Causes tight loop if there's a long delay reading from network into FN, so we may see lots of status requests
112122
if (fn_network_bw == 0 && fn_network_conn == 1) {

common/src/fn_network/network_read_nb.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,19 @@ int16_t network_read_nb(char *devicespec, uint8_t *buf, uint16_t len)
9595
r = network_status(devicespec, &fn_network_bw, &fn_network_conn, &fn_network_error); // TODO: Status return needs fixing.
9696
#endif
9797

98-
// check if the status failed
99-
if (r != 0) return -r;
98+
// check if the status failed.
99+
if (r != 0) {
100+
return -r;
101+
}
100102

101103
// EOF hit, exit reading
102104
if (fn_network_error == 136) return 0;
103105

106+
// is there another error?
107+
if (fn_network_error != 1) {
108+
return -fn_network_error;
109+
}
110+
104111
// we are waiting for bytes to become available while still connected, so no data can be read
105112
if (fn_network_bw == 0 && fn_network_conn == 1) {
106113
return 0;

fujinet-network.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ uint8_t network_open(char* devicespec, uint8_t mode, uint8_t trans);
103103
* @brief Non-blocking read from channel
104104
*
105105
* The read will grab whatever is waiting in the FujiNet buffer. If fewer than the requested len, the return count will reflect this.
106-
* Errors are returned as the negative value of the error.
106+
* 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.
107107
*
108108
* @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/"
109109
* @param buf Buffer
110110
* @param len length
111-
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values)
111+
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code
112112
*/
113113
int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len);
114114

@@ -117,12 +117,12 @@ int16_t network_read_nb(char* devicespec, uint8_t *buf, uint16_t len);
117117
*
118118
* The read will block until it has read all the bytes requested from the device, or the EOF is hit.
119119
* This will block waiting for as much data as it can, so that the client does not need to handle counting.
120-
* Errors are returned as the negative value of the error.
120+
* 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.
121121
*
122122
* @param devicespec pointer to device specification, e.g. "N1:HTTPS://fujinet.online/"
123123
* @param buf Buffer
124124
* @param len length
125-
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values)
125+
* @return Bytes read, or negative value of fujinet-network error code (See FN_ERR_* values) with fn_network_error containing real error code
126126
*/
127127
int16_t network_read(char* devicespec, uint8_t *buf, uint16_t len);
128128

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.5.2
1+
4.5.3

0 commit comments

Comments
 (0)