Replaced magic resultcodes from mbus_recv_frame with defines

This commit is contained in:
Stefan Wahren 2012-07-11 23:33:52 +02:00
parent ff37c4af8c
commit e6a52f97ea
11 changed files with 43 additions and 33 deletions

View File

@ -132,7 +132,7 @@ main(int argc, char **argv)
} }
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
fprintf(stderr, "Failed to receive M-Bus response frame.\n"); fprintf(stderr, "Failed to receive M-Bus response frame.\n");
return 1; return 1;

View File

@ -103,7 +103,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
@ -111,7 +111,7 @@ main(int argc, char **argv)
if (debug) if (debug)
printf("\n"); printf("\n");
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);

View File

@ -79,13 +79,13 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device with secondary address %s: %s\n", argv[2], mbus_error_str()); printf("No reply from device with secondary address %s: %s\n", argv[2], mbus_error_str());
return 1; return 1;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[2], mbus_error_str()); printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[2], mbus_error_str());
return 1; return 1;
@ -99,7 +99,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -78,7 +78,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device\n"); printf("No reply from device\n");
return 1; return 1;

View File

@ -117,7 +117,7 @@ main(int argc, char **argv)
} }
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str()); fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -86,7 +86,7 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
continue; continue;
} }
@ -94,7 +94,7 @@ main(int argc, char **argv)
if (debug) if (debug)
printf("\n"); printf("\n");
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);

View File

@ -65,13 +65,13 @@ main(int argc, char **argv)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
printf("No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str()); printf("No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str());
return 1; return 1;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[3], mbus_error_str()); printf("Invalid reply from %s: The address address probably match more than one device: %s\n", argv[3], mbus_error_str());
return 1; return 1;
@ -85,7 +85,7 @@ main(int argc, char **argv)
return 1; return 1;
} }
if (mbus_recv_frame(handle, &reply) != 0) if (mbus_recv_frame(handle, &reply) != MBUS_RECV_RESULT_OK)
{ {
printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -1434,13 +1434,13 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame)
if (handle == NULL) if (handle == NULL)
{ {
MBUS_ERROR("%s: Invalid M-Bus handle for receive.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Invalid M-Bus handle for receive.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
if (frame == NULL) if (frame == NULL)
{ {
MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
result = handle->recv(handle, frame); result = handle->recv(handle, frame);
@ -1463,7 +1463,8 @@ int mbus_purge_frames(mbus_handle *handle)
while (1) while (1)
{ {
err = mbus_recv_frame(handle, &reply); err = mbus_recv_frame(handle, &reply);
if (err != -2 && err != 0) if (err != MBUS_RECV_RESULT_OK &&
err != MBUS_RECV_RESULT_INVALID)
break; break;
received = 1; received = 1;
@ -1660,7 +1661,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
if (debug) if (debug)
printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count); printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count);
if (mbus_recv_frame(handle, next_frame) != 0) if (mbus_recv_frame(handle, next_frame) != MBUS_RECV_RESULT_OK)
{ {
MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__);
retval = 1; retval = 1;
@ -1802,12 +1803,12 @@ mbus_select_secondary_address(mbus_handle * handle, const char *mask)
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
return MBUS_PROBE_NOTHING; return MBUS_PROBE_NOTHING;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
/* check for more data (collision) */ /* check for more data (collision) */
mbus_purge_frames(handle); mbus_purge_frames(handle);
@ -1862,12 +1863,12 @@ mbus_probe_secondary_address(mbus_handle * handle, const char *mask, char *match
ret = mbus_recv_frame(handle, &reply); ret = mbus_recv_frame(handle, &reply);
if (ret == -3) if (ret == MBUS_RECV_RESULT_TIMEOUT)
{ {
return MBUS_PROBE_NOTHING; return MBUS_PROBE_NOTHING;
} }
if (ret == -2) if (ret == MBUS_RECV_RESULT_INVALID)
{ {
return MBUS_PROBE_COLLISION; return MBUS_PROBE_COLLISION;
} }

View File

@ -106,6 +106,15 @@ typedef struct _mbus_slave_data {
#define MBUS_HANDLE_TYPE_TCP 0 #define MBUS_HANDLE_TYPE_TCP 0
#define MBUS_HANDLE_TYPE_SERIAL 1 #define MBUS_HANDLE_TYPE_SERIAL 1
//
// Resultcodes for mbus_recv_frame
//
#define MBUS_RECV_RESULT_OK 0
#define MBUS_RECV_RESULT_ERROR -1
#define MBUS_RECV_RESULT_INVALID -2
#define MBUS_RECV_RESULT_TIMEOUT -3
#define MBUS_RECV_RESULT_RESET -4
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// MBUS FRAME DATA FORMATS // MBUS FRAME DATA FORMATS
// //

View File

@ -245,7 +245,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (handle == NULL || frame == NULL) if (handle == NULL || frame == NULL)
{ {
fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
memset((void *)buff, 0, sizeof(buff)); memset((void *)buff, 0, sizeof(buff));
@ -264,7 +264,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
{ {
// fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n", // fprintf(stderr, "%s: aborting recv frame (remaining = %d, len = %d, nread = %d)\n",
// __PRETTY_FUNCTION__, remaining, len, nread); // __PRETTY_FUNCTION__, remaining, len, nread);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
// printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len); // printf("%s: Got %d byte [remaining %d, len %d]\n", __PRETTY_FUNCTION__, nread, remaining, len);
@ -288,7 +288,7 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (len == 0) if (len == 0)
{ {
// No data received // No data received
return -1; return MBUS_RECV_RESULT_TIMEOUT;
} }
// //
@ -301,16 +301,16 @@ mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame)
{ {
// Would be OK when e.g. scanning the bus, otherwise it is a failure. // Would be OK when e.g. scanning the bus, otherwise it is a failure.
// printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__); // printf("%s: M-Bus layer failed to receive complete data.\n", __PRETTY_FUNCTION__);
return -2; return MBUS_RECV_RESULT_INVALID;
} }
if (len == -1) if (len == -1)
{ {
fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: M-Bus layer failed to parse data.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
return 0; return MBUS_RECV_RESULT_OK;
} }

View File

@ -169,7 +169,7 @@ int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame)
if (handle == NULL || frame == NULL) { if (handle == NULL || frame == NULL) {
fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__);
return -1; return MBUS_RECV_RESULT_ERROR;
} }
memset((void *) buff, 0, sizeof(buff)); memset((void *) buff, 0, sizeof(buff));
@ -190,14 +190,14 @@ retry:
if (errno == EAGAIN || errno == EWOULDBLOCK) { if (errno == EAGAIN || errno == EWOULDBLOCK) {
mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached."); mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached.");
return -3; return MBUS_RECV_RESULT_TIMEOUT;
} }
mbus_error_str_set("M-Bus tcp transport layer failed to read data."); mbus_error_str_set("M-Bus tcp transport layer failed to read data.");
return -1; return MBUS_RECV_RESULT_ERROR;
case 0: case 0:
mbus_error_str_set("M-Bus tcp transport layer connection closed by remote host."); mbus_error_str_set("M-Bus tcp transport layer connection closed by remote host.");
return -4; return MBUS_RECV_RESULT_RESET;
default: default:
len += nread; len += nread;
} }
@ -211,10 +211,10 @@ retry:
if (remaining < 0) { if (remaining < 0) {
mbus_error_str_set("M-Bus layer failed to parse data."); mbus_error_str_set("M-Bus layer failed to parse data.");
return -2; return MBUS_RECV_RESULT_INVALID;
} }
return 0; return MBUS_RECV_RESULT_OK;
} }