]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/siglent-sds/protocol.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / siglent-sds / protocol.c
index af2f2d9b0b9a1c00f028e7b33013da4fbabf56d7..d59e52cbc1841365d88673492d951ef3d1985ac5 100644 (file)
@@ -75,6 +75,7 @@ static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
                        if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
                                return SR_ERR;
                        sr_atoi(buf, &out);
+                       g_free(buf);
                        g_usleep(s);
                } while (out == 0);
 
@@ -100,6 +101,7 @@ static int siglent_sds_event_wait(const struct sr_dev_inst *sdi)
                        if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
                                return SR_ERR;
                        sr_atoi(buf, &out);
+                       g_free(buf);
                        g_usleep(s);
                /* XXX
                 * Now this loop condition looks suspicious! A bitwise
@@ -168,6 +170,7 @@ SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
                        if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
                                return SR_ERR;
                        sr_atoi(buf, &out);
+                       g_free(buf);
                        if (out == DEVICE_STATE_TRIG_RDY) {
                                siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
                        } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
@@ -217,6 +220,7 @@ SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi)
                        if (sr_scpi_get_string(sdi->conn, ":INR?", &buf) != SR_OK)
                                return SR_ERR;
                        sr_atoi(buf, &out);
+                       g_free(buf);
                        if (out == DEVICE_STATE_TRIG_RDY) {
                                siglent_sds_set_wait_event(devc, WAIT_TRIGGER);
                        } else if (out == DEVICE_STATE_DATA_TRIG_RDY) {
@@ -348,7 +352,7 @@ static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_chan
        gboolean low_channels; /* Lower channels enabled */
        gboolean high_channels; /* Higher channels enabled */
        int len, channel_index;
-       long samples_index;
+       uint64_t samples_index;
 
        len = 0;
        channel_index = 0;
@@ -360,71 +364,71 @@ static int siglent_sds_get_digital(const struct sr_dev_inst *sdi, struct sr_chan
        for (l = sdi->channels; l; l = l->next) {
                ch = l->data;
                samples_index = 0;
-               if (ch->type == SR_CHANNEL_LOGIC) {
-                       if (ch->enabled) {
-                               if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK)
-                                       return SR_ERR;
-                               if (sr_scpi_read_begin(scpi) != SR_OK)
-                                       return TRUE;
-                               len = sr_scpi_read_data(scpi, buf, -1);
-                               if (len < 0)
-                                       return TRUE;
-                               len -= 15;
-                               buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
-                               buf += 15; /* Skipping the data header. */
-                               g_array_append_vals(buffdata, buf, len);
-                               tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */
-                               for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) {
-                                       char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index);
-                                       for (int ii = 0; ii < 8; ii++, sample >>= 1) {
-                                               if (ch->index < 8) {
-                                                       channel_index = ch->index;
-                                                       if (data_low_channels->len <= samples_index) {
-                                                               tmp_value = 0; /* New sample. */
-                                                               low_channels = TRUE; /* We have at least one enabled low channel. */
-                                                       } else {
-                                                               /* Get previous stored sample from low channel buffer. */
-                                                               tmp_value = g_array_index(data_low_channels, uint8_t, samples_index);
-                                                       }
-                                               } else {
-                                                       channel_index = ch->index - 8;
-                                                       if (data_high_channels->len <= samples_index) {
-                                                               tmp_value = 0; /* New sample. */
-                                                               high_channels = TRUE; /* We have at least one enabled high channel. */
-                                                       } else {
-                                                               /* Get previous stored sample from high channel buffer. */
-                                                               tmp_value = g_array_index(data_high_channels, uint8_t, samples_index);
-                                                       }
-                                               }
-                                               /* Check if the current scope sample bit is set. */
-                                               if (sample & 0x1)
-                                                       tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */
-                                               g_array_append_val(tmp_samplebuf, tmp_value);
-                                               samples_index++;
-                                       }
-                               }
-
-                               /* Clear the buffers to prepare for the new samples */
+               if (ch->type != SR_CHANNEL_LOGIC)
+                       continue;
+               if (!ch->enabled)
+                       continue;
+               if (sr_scpi_send(sdi->conn, "D%d:WF? DAT2", ch->index) != SR_OK)
+                       return SR_ERR;
+               if (sr_scpi_read_begin(scpi) != SR_OK)
+                       return TRUE;
+               len = sr_scpi_read_data(scpi, buf, -1);
+               if (len < 0)
+                       return TRUE;
+               len -= 15;
+               buffdata = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len);
+               buf += 15; /* Skipping the data header. */
+               g_array_append_vals(buffdata, buf, len);
+               tmp_samplebuf = g_array_sized_new(FALSE, FALSE, sizeof(uint8_t), len); /* New temp buffer. */
+               for (uint64_t cur_sample_index = 0; cur_sample_index < devc->memory_depth_digital; cur_sample_index++) {
+                       char sample = (char)g_array_index(buffdata, uint8_t, cur_sample_index);
+                       for (int ii = 0; ii < 8; ii++, sample >>= 1) {
                                if (ch->index < 8) {
-                                       g_array_free(data_low_channels, FALSE);
-                                       data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+                                       channel_index = ch->index;
+                                       if (data_low_channels->len <= samples_index) {
+                                               tmp_value = 0; /* New sample. */
+                                               low_channels = TRUE; /* We have at least one enabled low channel. */
+                                       } else {
+                                               /* Get previous stored sample from low channel buffer. */
+                                               tmp_value = g_array_index(data_low_channels, uint8_t, samples_index);
+                                       }
                                } else {
-                                       g_array_free(data_high_channels, FALSE);
-                                       data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
-                               }
-
-                               /* Storing the converted temp values from the the scope into the buffers. */
-                               for (long index = 0; index < tmp_samplebuf->len; index++) {
-                                       uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index);
-                                       if (ch->index < 8)
-                                               g_array_append_val(data_low_channels, value);
-                                       else
-                                               g_array_append_val(data_high_channels, value);
+                                       channel_index = ch->index - 8;
+                                       if (data_high_channels->len <= samples_index) {
+                                               tmp_value = 0; /* New sample. */
+                                               high_channels = TRUE; /* We have at least one enabled high channel. */
+                                       } else {
+                                               /* Get previous stored sample from high channel buffer. */
+                                               tmp_value = g_array_index(data_high_channels, uint8_t, samples_index);
+                                       }
                                }
-                               g_array_free(tmp_samplebuf, TRUE);
-                               g_array_free(buffdata, TRUE);
+                               /* Check if the current scope sample bit is set. */
+                               if (sample & 0x1)
+                                       tmp_value |= 1UL << channel_index; /* Set current scope sample bit based on channel index. */
+                               g_array_append_val(tmp_samplebuf, tmp_value);
+                               samples_index++;
                        }
                }
+
+               /* Clear the buffers to prepare for the new samples */
+               if (ch->index < 8) {
+                       g_array_free(data_low_channels, TRUE);
+                       data_low_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+               } else {
+                       g_array_free(data_high_channels, TRUE);
+                       data_high_channels = g_array_new(FALSE, FALSE, sizeof(uint8_t));
+               }
+
+               /* Storing the converted temp values from the the scope into the buffers. */
+               for (uint64_t index = 0; index < tmp_samplebuf->len; index++) {
+                       uint8_t value = g_array_index(tmp_samplebuf, uint8_t, index);
+                       if (ch->index < 8)
+                               g_array_append_val(data_low_channels, value);
+                       else
+                               g_array_append_val(data_high_channels, value);
+               }
+               g_array_free(tmp_samplebuf, TRUE);
+               g_array_free(buffdata, TRUE);
        }
 
        /* Combining the lower and higher channel buffers into one buffer for sigrok. */
@@ -539,8 +543,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                                return TRUE;
                        if (len == -1) {
                                sr_err("Read error, aborting capture.");
-                               packet.type = SR_DF_FRAME_END;
-                               sr_session_send(sdi, &packet);
+                               std_session_send_df_frame_end(sdi);
                                sdi->driver->dev_acquisition_stop(sdi);
                                return TRUE;
                        }
@@ -549,8 +552,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
 
                        if (len == -1) {
                                sr_err("Read error, aborting capture.");
-                               packet.type = SR_DF_FRAME_END;
-                               sr_session_send(sdi, &packet);
+                               std_session_send_df_frame_end(sdi);
                                sdi->driver->dev_acquisition_stop(sdi);
                                return TRUE;
                        }
@@ -563,12 +565,11 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                                        devc->buffer += devc->block_header_size;
                                        len = devc->num_samples;
                                } else {
-                                       sr_dbg("Requesting: %li bytes.", devc->num_samples - devc->num_block_bytes);
+                                       sr_dbg("Requesting: %" PRIu64 " bytes.", devc->num_samples - devc->num_block_bytes);
                                        len = sr_scpi_read_data(scpi, (char *)devc->buffer, devc->num_samples-devc->num_block_bytes);
                                        if (len == -1) {
                                                sr_err("Read error, aborting capture.");
-                                               packet.type = SR_DF_FRAME_END;
-                                               sr_session_send(sdi, &packet);
+                                               std_session_send_df_frame_end(sdi);
                                                sdi->driver->dev_acquisition_stop(sdi);
                                                return TRUE;
                                        }
@@ -615,8 +616,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                                        read_complete = TRUE;
                                        if (!sr_scpi_read_complete(scpi)) {
                                                sr_err("Read should have been completed.");
-                                               packet.type = SR_DF_FRAME_END;
-                                               sr_session_send(sdi, &packet);
+                                               std_session_send_df_frame_end(sdi);
                                                sdi->driver->dev_acquisition_stop(sdi);
                                                return TRUE;
                                        }
@@ -633,8 +633,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                                siglent_sds_channel_start(sdi);
                        } else {
                                /* Done with this frame. */
-                               packet.type = SR_DF_FRAME_END;
-                               sr_session_send(sdi, &packet);
+                               std_session_send_df_frame_end(sdi);
                                if (++devc->num_frames == devc->limit_frames) {
                                        /* Last frame, stop capture. */
                                        sdi->driver->dev_acquisition_stop(sdi);
@@ -644,8 +643,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                                        siglent_sds_capture_start(sdi);
 
                                        /* Start of next frame. */
-                                       packet.type = SR_DF_FRAME_BEGIN;
-                                       sr_session_send(sdi, &packet);
+                                       std_session_send_df_frame_begin(sdi);
                                }
                        }
                }
@@ -658,8 +656,7 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                packet.type = SR_DF_LOGIC;
                packet.payload = &logic;
                sr_session_send(sdi, &packet);
-               packet.type = SR_DF_FRAME_END;
-               sr_session_send(sdi, &packet);
+               std_session_send_df_frame_end(sdi);
                sdi->driver->dev_acquisition_stop(sdi);
 
                if (++devc->num_frames == devc->limit_frames) {
@@ -671,14 +668,12 @@ SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data)
                        siglent_sds_capture_start(sdi);
 
                        /* Start of next frame. */
-                       packet.type = SR_DF_FRAME_BEGIN;
-                       sr_session_send(sdi, &packet);
+                       std_session_send_df_frame_begin(sdi);
                }
        }
 
        // sr_session_send(sdi, &packet);
-       // packet.type = SR_DF_FRAME_END;
-       // sr_session_send(sdi, &packet);
+       // std_session_send_df_frame_end(sdi);
        // sdi->driver->dev_acquisition_stop(sdi);
 
        return TRUE;
@@ -769,6 +764,8 @@ SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
        /* Coupling. */
        for (i = 0; i < devc->model->analog_channels; i++) {
                cmd = g_strdup_printf("C%d:CPL?", i + 1);
+               g_free(devc->coupling[i]);
+               devc->coupling[i] = NULL;
                res = sr_scpi_get_string(sdi->conn, cmd, &devc->coupling[i]);
                g_free(cmd);
                if (res != SR_OK)
@@ -785,7 +782,7 @@ SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
        if (sr_scpi_get_string(sdi->conn, "TRSE?", &response) != SR_OK)
                return SR_ERR;
        tokens = g_strsplit(response, ",", 0);
-       for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
+       num_tokens = g_strv_length(tokens);
        if (num_tokens < 4) {
                sr_dbg("IDN response not according to spec: %80.s.", response);
                g_strfreev(tokens);
@@ -822,6 +819,8 @@ SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi)
 
        /* Trigger slope. */
        cmd = g_strdup_printf("%s:TRSL?", devc->trigger_source);
+       g_free(devc->trigger_slope);
+       devc->trigger_slope = NULL;
        res = sr_scpi_get_string(sdi->conn, cmd, &devc->trigger_slope);
        g_free(cmd);
        if (res != SR_OK)
@@ -895,12 +894,15 @@ SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
                g_free(cmd);
                samplerate_scope = 0;
                fvalue = 0;
-               if (res != SR_OK)
+               if (res != SR_OK) {
+                       g_free(sample_points_string);
                        return SR_ERR;
+               }
                if (g_strstr_len(sample_points_string, -1, "Mpts") != NULL) {
                        sample_points_string[strlen(sample_points_string) - 4] = '\0';
                        if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
                                sr_dbg("Invalid float converted from scope response.");
+                               g_free(sample_points_string);
                                return SR_ERR;
                        }
                        samplerate_scope = fvalue * 1000000;
@@ -908,6 +910,7 @@ SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
                        sample_points_string[strlen(sample_points_string) - 4] = '\0';
                        if (sr_atof_ascii(sample_points_string, &fvalue) != SR_OK) {
                                sr_dbg("Invalid float converted from scope response.");
+                               g_free(sample_points_string);
                                return SR_ERR;
                        }
                        samplerate_scope = fvalue * 10000;
@@ -939,7 +942,7 @@ SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi)
        sr_dbg("Current timebase: %g.", devc->timebase);
        devc->samplerate = devc->memory_depth_analog / (devc->timebase * devc->model->series->num_horizontal_divs);
        sr_dbg("Current samplerate: %0f.", devc->samplerate);
-       sr_dbg("Current memory depth: %lu.", devc->memory_depth_analog);
+       sr_dbg("Current memory depth: %" PRIu64 ".", devc->memory_depth_analog);
 
        return SR_OK;
 }