Added NMBS_ERROR_CRC and NMBS_ERROR_INVALID_TCP_MBAP nmbs_error

This commit is contained in:
Valerio De Benedetto 2022-05-12 19:28:50 +02:00
parent 3904a6c52f
commit 271f689771
3 changed files with 13 additions and 6 deletions

View File

@ -130,9 +130,8 @@ int main(int argc, char* argv[]) {
err = nmbs_server_poll(&nmbs); err = nmbs_server_poll(&nmbs);
if (err != NMBS_ERROR_NONE) { if (err != NMBS_ERROR_NONE) {
fprintf(stderr, "Error polling modbus connection - %s\n", nmbs_strerror(err)); fprintf(stderr, "Error on modbus connection - %s\n", nmbs_strerror(err));
// In a more complete example, we should handle this error by closing the connection from our side // In a more complete example, we would handle this error by checking its nmbs_error value
break;
} }
} }

View File

@ -249,7 +249,7 @@ static nmbs_error recv_msg_footer(nmbs_t* nmbs) {
uint16_t recv_crc = get_2(nmbs); uint16_t recv_crc = get_2(nmbs);
if (recv_crc != crc) if (recv_crc != crc)
return NMBS_ERROR_TRANSPORT; return NMBS_ERROR_CRC;
} }
DEBUG("\n"); DEBUG("\n");
@ -315,10 +315,10 @@ static nmbs_error recv_msg_header(nmbs_t* nmbs, bool* first_byte_received) {
nmbs->msg.fc = get_1(nmbs); nmbs->msg.fc = get_1(nmbs);
if (protocol_id != 0) if (protocol_id != 0)
return NMBS_ERROR_TRANSPORT; return NMBS_ERROR_INVALID_TCP_MBAP;
if (length > 255) if (length > 255)
return NMBS_ERROR_TRANSPORT; return NMBS_ERROR_INVALID_TCP_MBAP;
} }
return NMBS_ERROR_NONE; return NMBS_ERROR_NONE;
@ -1288,6 +1288,12 @@ nmbs_error nmbs_receive_raw_pdu_response(nmbs_t* nmbs, void* data_out, uint32_t
#ifndef NMBS_STRERROR_DISABLED #ifndef NMBS_STRERROR_DISABLED
const char* nmbs_strerror(nmbs_error error) { const char* nmbs_strerror(nmbs_error error) {
switch (error) { switch (error) {
case NMBS_ERROR_INVALID_TCP_MBAP:
return "invalid TCP MBAP received";
case NMBS_ERROR_CRC:
return "invalid CRC received";
case NMBS_ERROR_TRANSPORT: case NMBS_ERROR_TRANSPORT:
return "transport error"; return "transport error";

View File

@ -54,6 +54,8 @@
*/ */
typedef enum nmbs_error { typedef enum nmbs_error {
// Library errors // Library errors
NMBS_ERROR_INVALID_TCP_MBAP = -6, /**< Received invalid TCP MBAP */
NMBS_ERROR_CRC = -5, /**< Received invalid CRC */
NMBS_ERROR_TRANSPORT = -4, /**< Transport error */ NMBS_ERROR_TRANSPORT = -4, /**< Transport error */
NMBS_ERROR_TIMEOUT = -3, /**< Read/write timeout occurred */ NMBS_ERROR_TIMEOUT = -3, /**< Read/write timeout occurred */
NMBS_ERROR_INVALID_RESPONSE = -2, /**< Received invalid response from server */ NMBS_ERROR_INVALID_RESPONSE = -2, /**< Received invalid response from server */