Routine sr_scpi_get_data() checks for free space in the receive buffer,
and resizes the buffer when free space drops below a threshold. The
previous logic assumed that the resize and the read logic would interact
in some specific way to achieve the desired operation.
Adjust the buffer resize such that more free space is pre-allocated, yet
the payload size of the buffer is not affected. This eliminates the
dependency of the optional resize logic from subsequent activity for
reception of data that is non-optional.
Add comments while we are here, outline the steps taken in the
sr_scpi_get_data() routine.
unsigned int offset;
int space;
unsigned int offset;
int space;
+ /* Optionally send caller provided command. */
if (command) {
if (sr_scpi_send(scpi, command) != SR_OK)
return SR_ERR;
}
if (command) {
if (sr_scpi_send(scpi, command) != SR_OK)
return SR_ERR;
}
+ /* Initiate SCPI read operation. */
if (sr_scpi_read_begin(scpi) != SR_OK)
return SR_ERR;
if (sr_scpi_read_begin(scpi) != SR_OK)
return SR_ERR;
+ /* Keep reading until completion or until timeout. */
laststart = g_get_monotonic_time();
response = *scpi_response;
laststart = g_get_monotonic_time();
response = *scpi_response;
offset = response->len;
while (!sr_scpi_read_complete(scpi)) {
offset = response->len;
while (!sr_scpi_read_complete(scpi)) {
+ /* Resize the buffer when free space drops below a threshold. */
space = response->allocated_len - response->len;
if (space < 128) {
g_string_set_size(response, response->len + 1024);
space = response->allocated_len - response->len;
if (space < 128) {
g_string_set_size(response, response->len + 1024);
+ g_string_set_size(response, offset);
space = response->allocated_len - response->len;
}
space = response->allocated_len - response->len;
}
+ /* Read another chunk of the response. */
len = sr_scpi_read_data(scpi, &response->str[offset], space);
if (len < 0) {
sr_err("Incompletely read SCPI response.");
len = sr_scpi_read_data(scpi, &response->str[offset], space);
if (len < 0) {
sr_err("Incompletely read SCPI response.");
}
offset += len;
g_string_set_size(response, offset);
}
offset += len;
g_string_set_size(response, offset);
+ /* Quit reading after a period of time without receive data. */
elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
if (elapsed_ms >= scpi->read_timeout_ms) {
sr_err("Timed out waiting for SCPI response.");
elapsed_ms = (g_get_monotonic_time() - laststart) / 1000;
if (elapsed_ms >= scpi->read_timeout_ms) {
sr_err("Timed out waiting for SCPI response.");