Fixed bugs in wildcard search
- init slaves before search - corrected return codes - handle collisions (possibly more bytes than expected) - sync recv frame (tcp vs serial) - add tracing
This commit is contained in:
parent
980bbb1862
commit
f14b3beb84
@ -28,6 +28,7 @@ main(int argc, char **argv)
|
|||||||
char *device, *addr_mask;
|
char *device, *addr_mask;
|
||||||
int baudrate = 9600;
|
int baudrate = 9600;
|
||||||
mbus_handle *handle = NULL;
|
mbus_handle *handle = NULL;
|
||||||
|
mbus_frame *frame = NULL, *reply = NULL;
|
||||||
|
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
{
|
{
|
||||||
@ -111,6 +112,42 @@ main(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
|
||||||
|
|
||||||
|
if (frame == NULL)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to allocate mbus frame.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// init slaves
|
||||||
|
//
|
||||||
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
||||||
|
frame->address = 0xFD;
|
||||||
|
|
||||||
|
if (mbus_send_frame(handle, frame) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send SND_NKE #1.\n");
|
||||||
|
mbus_frame_free(frame);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) mbus_recv_frame(handle, reply);
|
||||||
|
|
||||||
|
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
|
||||||
|
frame->address = 0xFF;
|
||||||
|
|
||||||
|
if (mbus_send_frame(handle, frame) == -1)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "Failed to send SND_NKE #2.\n");
|
||||||
|
mbus_frame_free(frame);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
(void) mbus_recv_frame(handle, reply);
|
||||||
|
|
||||||
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
mbus_scan_2nd_address_range(handle, 0, addr_mask);
|
||||||
|
|
||||||
mbus_disconnect(handle);
|
mbus_disconnect(handle);
|
||||||
|
@ -1711,11 +1711,25 @@ mbus_probe_secondary_address(mbus_handle * handle, const char *mask, char *match
|
|||||||
|
|
||||||
if (ret == -2)
|
if (ret == -2)
|
||||||
{
|
{
|
||||||
|
/* check for more data (collision) */
|
||||||
|
while (mbus_recv_frame(handle, &reply) != -1);
|
||||||
|
|
||||||
return MBUS_PROBE_COLLISION;
|
return MBUS_PROBE_COLLISION;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK)
|
||||||
{
|
{
|
||||||
|
/* check for more data (collision) */
|
||||||
|
while (mbus_recv_frame(handle, &reply) != -1)
|
||||||
|
{
|
||||||
|
ret = -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret == -2)
|
||||||
|
{
|
||||||
|
return MBUS_PROBE_COLLISION;
|
||||||
|
}
|
||||||
|
|
||||||
/* send a data request command to find out the full address */
|
/* send a data request command to find out the full address */
|
||||||
if (mbus_send_request_frame(handle, 253) == -1)
|
if (mbus_send_request_frame(handle, 253) == -1)
|
||||||
{
|
{
|
||||||
@ -1726,11 +1740,18 @@ mbus_probe_secondary_address(mbus_handle * handle, const char *mask, char *match
|
|||||||
return MBUS_PROBE_ERROR;
|
return MBUS_PROBE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mbus_recv_frame(handle, &reply) == -1)
|
ret = mbus_recv_frame(handle, &reply);
|
||||||
|
|
||||||
|
if (ret == -1)
|
||||||
{
|
{
|
||||||
return MBUS_PROBE_NOTHING;
|
return MBUS_PROBE_NOTHING;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ret == -2)
|
||||||
|
{
|
||||||
|
return MBUS_PROBE_COLLISION;
|
||||||
|
}
|
||||||
|
|
||||||
if (mbus_frame_type(&reply) != MBUS_FRAME_TYPE_ACK)
|
if (mbus_frame_type(&reply) != MBUS_FRAME_TYPE_ACK)
|
||||||
{
|
{
|
||||||
snprintf(matching_addr, 17, "%s", mbus_frame_get_secondary_address(&reply));
|
snprintf(matching_addr, 17, "%s", mbus_frame_get_secondary_address(&reply));
|
||||||
|
@ -205,7 +205,7 @@ mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
|||||||
// call the send event function, if the callback function is registered
|
// call the send event function, if the callback function is registered
|
||||||
//
|
//
|
||||||
if (_mbus_send_event)
|
if (_mbus_send_event)
|
||||||
_mbus_send_event(MBUS_HANDLE_TYPE_SERIAL);
|
_mbus_send_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -236,17 +236,7 @@ mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
|||||||
do {
|
do {
|
||||||
//printf("%s: Attempt to read %d bytes [len = %d]\n", __PRETTY_FUNCTION__, remaining, len);
|
//printf("%s: Attempt to read %d bytes [len = %d]\n", __PRETTY_FUNCTION__, remaining, len);
|
||||||
|
|
||||||
nread = read(handle->fd, &buff[len], remaining);
|
if ((nread = read(handle->fd, &buff[len], remaining)) == -1)
|
||||||
|
|
||||||
if (nread > 0)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// call the receive event function, if the callback function is registered
|
|
||||||
//
|
|
||||||
if (_mbus_recv_event)
|
|
||||||
_mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL);
|
|
||||||
}
|
|
||||||
else if (nread == -1)
|
|
||||||
{
|
{
|
||||||
// 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);
|
||||||
@ -258,12 +248,24 @@ mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame)
|
|||||||
len += nread;
|
len += nread;
|
||||||
|
|
||||||
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
|
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
// No data received
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// call the receive event function, if the callback function is registered
|
||||||
|
//
|
||||||
|
if (_mbus_recv_event)
|
||||||
|
_mbus_recv_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
|
||||||
|
|
||||||
if(remaining < 0)
|
if (remaining < 0)
|
||||||
{
|
{
|
||||||
// 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 -1;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (len == -1)
|
if (len == -1)
|
||||||
|
@ -144,7 +144,7 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
|||||||
// call the send event function, if the callback function is registered
|
// call the send event function, if the callback function is registered
|
||||||
//
|
//
|
||||||
if (_mbus_send_event)
|
if (_mbus_send_event)
|
||||||
_mbus_send_event(MBUS_HANDLE_TYPE_TCP);
|
_mbus_send_event(MBUS_HANDLE_TYPE_TCP, buff, len);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -176,17 +176,7 @@ mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
|
|
||||||
nread = read(handle->sock, &buff[len], remaining);
|
if ((nread = read(handle->sock, &buff[len], remaining)) == -1)
|
||||||
|
|
||||||
if (nread > 0)
|
|
||||||
{
|
|
||||||
//
|
|
||||||
// call the receive event function, if the callback function is registered
|
|
||||||
//
|
|
||||||
if (_mbus_recv_event)
|
|
||||||
_mbus_recv_event(MBUS_HANDLE_TYPE_TCP);
|
|
||||||
}
|
|
||||||
else if (nread == -1)
|
|
||||||
{
|
{
|
||||||
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 -1;
|
||||||
@ -195,6 +185,18 @@ mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame)
|
|||||||
len += nread;
|
len += nread;
|
||||||
|
|
||||||
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
|
} while ((remaining = mbus_parse(frame, buff, len)) > 0);
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
{
|
||||||
|
// No data received
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// call the receive event function, if the callback function is registered
|
||||||
|
//
|
||||||
|
if (_mbus_recv_event)
|
||||||
|
_mbus_recv_event(MBUS_HANDLE_TYPE_TCP, buff, len);
|
||||||
|
|
||||||
if (remaining < 0)
|
if (remaining < 0)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user