Commit 12cac8a1 authored by Lucas Russo's avatar Lucas Russo

hal/msg/*/*_zmq_client.c: add option to receive empty data frame

Some messages have the possibility to return an empty data frame,
such as the read_block function when reading 0 bytes.

This fixes #64 github issue
parent 01363aae
......@@ -39,7 +39,7 @@ static ssize_t _thsafe_zmq_client_read_generic (smio_t *self, loff_t offs, uint8
static ssize_t _thsafe_zmq_client_write_generic (smio_t *self, loff_t offs, const uint8_t *data,
uint32_t size);
static ssize_t _thsafe_zmq_client_recv_rw (smio_t *self, uint8_t *data,
uint32_t size);
uint32_t size, bool accept_empty_data);
/**** Open device ****/
int thsafe_zmq_client_open (smio_t *self, llio_endpoint_t *endpoint)
......@@ -123,7 +123,7 @@ ssize_t thsafe_zmq_client_read_block (smio_t *self, loff_t offs, size_t size, ui
* frame 0: reply code
* frame 1: return code
* frame 2: data */
ret_size = _thsafe_zmq_client_recv_rw (self, (uint8_t *) data, size);
ret_size = _thsafe_zmq_client_recv_rw (self, (uint8_t *) data, size, true);
err_send_msg:
err_add_size:
......@@ -173,7 +173,7 @@ ssize_t thsafe_zmq_client_write_block (smio_t *self, loff_t offs, size_t size, c
* frame 2: data */
uint32_t ret_data = 0;
ssize_t ret_size = _thsafe_zmq_client_recv_rw (self, (uint8_t *) &ret_data,
sizeof (ret_data));
sizeof (ret_data), false);
ASSERT_TEST(ret_size == sizeof (ret_data), "Data size does not match the expected",
err_data_size);
......@@ -332,7 +332,7 @@ static ssize_t _thsafe_zmq_client_read_generic (smio_t *self, loff_t offs, uint8
* frame 0: reply code
* frame 1: return code
* frame 2: data */
ret_size = _thsafe_zmq_client_recv_rw (self, data, size);
ret_size = _thsafe_zmq_client_recv_rw (self, data, size, true);
err_send_msg:
err_add_offset:
......@@ -399,7 +399,7 @@ static ssize_t _thsafe_zmq_client_write_generic (smio_t *self, loff_t offs, cons
* frame 2: data */
uint32_t ret_data = 0;
ssize_t ret_size = _thsafe_zmq_client_recv_rw (self, (uint8_t *) &ret_data,
sizeof (ret_data));
sizeof (ret_data), false);
ASSERT_TEST(ret_size == sizeof (ret_data), "Data size does not match the expected",
err_data_size);
......@@ -460,45 +460,57 @@ err_recv_data:
}
static ssize_t _thsafe_zmq_client_recv_rw (smio_t *self, uint8_t *data,
uint32_t size)
uint32_t size, bool accept_empty_data)
{
ssize_t ret_size = -1;
/* Returns NULL if confirmation was not OK or in case of error.
* Returns the original message if the confirmation was OK */
zmsg_t *recv_msg = _thsafe_zmq_client_recv_confirmation (self);
ASSERT_TEST(recv_msg != NULL, "Could not receive confirmation code", err_null_recv_msg);
ASSERT_TEST(recv_msg != NULL, "Could not receive confirmation code",
err_null_recv_msg);
/* If we are here, confirmation code was OK. Check for second frame */
zframe_t *reply_frame = zmsg_pop (recv_msg);
zframe_destroy (&reply_frame); /* Don't do anything with the reply code */
zframe_t *return_frame = zmsg_pop (recv_msg);
ASSERT_TEST(return_frame != NULL, "Could not receive return code", err_null_ret_code_frame);
/* Check for return frame size */
ASSERT_TEST(zframe_size (return_frame) == THSAFE_RETURN_SIZE,
"Return frame size is wrong", err_wrong_size_ret_frame);
zframe_t *data_frame = zmsg_pop (recv_msg);
ASSERT_TEST(data_frame != NULL, "Could not receive data", err_recv_data);
/* Check if the frame has the number of bytes requested.
* For now, we consider a success only when the number of
* bytes requested is the same as the actually read*/
ssize_t data_frame_size = (ssize_t) zframe_size (data_frame);
ASSERT_TEST(data_frame_size ==
*(THSAFE_RETURN_TYPE *) zframe_data (return_frame),
"Received data frame size is wrong", err_data_frame_size);
ASSERT_TEST(data_frame_size >= size,
"Specified buffer size is bigger than the available data",
err_buf_size_data);
uint8_t* raw_data = (uint8_t *) zframe_data (data_frame);
memcpy (data, raw_data, size);
ret_size = size;
zframe_t *data_frame = NULL;
DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Received data successfully\n");
/* Some messages accept an empty data frame */
if (return_frame == NULL && accept_empty_data) {
DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Empty "
"data frame received\n");
ret_size = 0;
}
else {
ASSERT_TEST(return_frame != NULL, "Could not receive return code",
err_null_ret_code_frame);
/* Check for return frame size */
ASSERT_TEST(zframe_size (return_frame) == THSAFE_RETURN_SIZE,
"Return frame size is wrong", err_wrong_size_ret_frame);
data_frame = zmsg_pop (recv_msg);
ASSERT_TEST(data_frame != NULL, "Could not receive data", err_recv_data);
/* Check if the frame has the number of bytes requested.
* For now, we consider a success only when the number of
* bytes requested is the same as the actually read*/
ssize_t data_frame_size = (ssize_t) zframe_size (data_frame);
ASSERT_TEST(data_frame_size ==
*(THSAFE_RETURN_TYPE *) zframe_data (return_frame),
"Received data frame size is wrong", err_data_frame_size);
ASSERT_TEST(data_frame_size >= size,
"Specified buffer size is bigger than the available data",
err_buf_size_data);
uint8_t* raw_data = (uint8_t *) zframe_data (data_frame);
memcpy (data, raw_data, size);
ret_size = size;
}
DBE_DEBUG (DBG_MSG | DBG_LVL_TRACE, "[smio_thsafe_client:zmq] Received data "
"successfully\n");
err_buf_size_data:
err_data_frame_size:
zframe_destroy (&data_frame);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment