]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/hantek-dso/api.c
output/csv: use intermediate time_t var, silence compiler warning
[libsigrok.git] / src / hardware / hantek-dso / api.c
index 663cd55dfcd668e732313b2a613be21015219469..bfad5f7135f07750901e04a9fda5eb4fe1a87fbd 100644 (file)
@@ -55,7 +55,7 @@ static const uint32_t drvopts[] = {
 static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_CONN | SR_CONF_GET,
-       SR_CONF_LIMIT_FRAMES | SR_CONF_SET,
+       SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
        SR_CONF_TIMEBASE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
        SR_CONF_NUM_HDIV | SR_CONF_GET,
        SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET,
@@ -148,7 +148,7 @@ static const uint64_t samplerates[] = {
        SR_MHZ(50),
        SR_MHZ(100),
        SR_MHZ(125),
-       /* fast mode not supported yet 
+       /* Fast mode not supported yet.
        SR_MHZ(200),
        SR_MHZ(250), */
 };
@@ -169,7 +169,6 @@ static const uint64_t vdivs[][2] = {
 
 static const char *trigger_sources[] = {
        "CH1", "CH2", "EXT",
-       /* TODO: forced */
 };
 
 static const char *trigger_slopes[] = {
@@ -199,10 +198,8 @@ static struct sr_dev_inst *dso_dev_new(const struct dso_profile *prof)
         */
        for (i = 0; i < ARRAY_SIZE(channel_names); i++) {
                ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
-               cg = g_malloc0(sizeof(struct sr_channel_group));
-               cg->name = g_strdup(channel_names[i]);
+               cg = sr_channel_group_new(sdi, channel_names[i], NULL);
                cg->channels = g_slist_append(cg->channels, ch);
-               sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
        }
 
        devc = g_malloc0(sizeof(struct dev_context));
@@ -221,7 +218,7 @@ static struct sr_dev_inst *dso_dev_new(const struct dso_profile *prof)
        devc->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
        devc->framesize = DEFAULT_FRAMESIZE;
        devc->triggerslope = SLOPE_POSITIVE;
-       devc->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
+       devc->triggersource = NULL;
        devc->capture_ratio = DEFAULT_CAPTURE_RATIO;
        sdi->priv = devc;
 
@@ -315,7 +312,8 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
 
                libusb_get_device_descriptor(devlist[i], &des);
 
-               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+               if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0)
+                       continue;
 
                prof = NULL;
                for (j = 0; dev_profiles[j].orig_vid; j++) {
@@ -329,11 +327,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                                devices = g_slist_append(devices, sdi);
                                devc = sdi->priv;
                                if (ezusb_upload_firmware(drvc->sr_ctx, devlist[i],
-                                               USB_CONFIGURATION, prof->firmware) == SR_OK)
+                                               USB_CONFIGURATION, prof->firmware) == SR_OK) {
                                        /* Remember when the firmware on this device was updated */
                                        devc->fw_updated = g_get_monotonic_time();
-                               else
-                                       sr_err("Firmware upload failed");
+                               } else {
+                                       sr_err("Firmware upload failed, name %s", prof->firmware);
+                               }
                                /* Dummy USB address of 0xff will get overwritten later. */
                                sdi->conn = sr_usb_dev_inst_new(
                                                libusb_get_bus_number(devlist[i]), 0xff, NULL);
@@ -466,6 +465,8 @@ static int config_get(uint32_t key, GVariant **data,
                        *data = g_variant_new_uint64(devc->framesize);
                        break;
                case SR_CONF_TRIGGER_SOURCE:
+                       if (!devc->triggersource)
+                               return SR_ERR_NA;
                        *data = g_variant_new_string(devc->triggersource);
                        break;
                case SR_CONF_TRIGGER_SLOPE:
@@ -475,6 +476,9 @@ static int config_get(uint32_t key, GVariant **data,
                case SR_CONF_CAPTURE_RATIO:
                        *data = g_variant_new_uint64(devc->capture_ratio);
                        break;
+               case SR_CONF_LIMIT_FRAMES:
+                       *data = g_variant_new_uint64(devc->limit_frames);
+                       break;
                default:
                        return SR_ERR_NA;
                }
@@ -506,7 +510,6 @@ static int config_set(uint32_t key, GVariant *data,
        const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
-       int rat;
        int ch_idx, idx;
        float flt;
 
@@ -522,11 +525,9 @@ static int config_set(uint32_t key, GVariant *data,
                                sr_err("Trigger level must be in [0.0,1.0].");
                                return SR_ERR_ARG;
                        }
-
                        devc->voffset_trigger = flt;
-                       if (dso_set_voffsets(sdi) != SR_OK) {
-                                       return SR_ERR;
-                       }
+                       if (dso_set_voffsets(sdi) != SR_OK)
+                               return SR_ERR;
                        break;
                case SR_CONF_TRIGGER_SLOPE:
                        if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0)
@@ -534,12 +535,7 @@ static int config_set(uint32_t key, GVariant *data,
                        devc->triggerslope = idx;
                        break;
                case SR_CONF_CAPTURE_RATIO:
-                       rat = g_variant_get_uint64(data);
-                       if (rat < 0 || rat > 100) {
-                               sr_err("Capture ratio must be in [0,100].");
-                               return SR_ERR_ARG;
-                       } else
-                               devc->capture_ratio = rat;
+                       devc->capture_ratio = g_variant_get_uint64(data);
                        break;
                case SR_CONF_BUFFERSIZE:
                        if ((idx = std_u64_idx(data, devc->profile->buffersizes, NUM_BUFFER_SIZES)) < 0)
@@ -557,11 +553,11 @@ static int config_set(uint32_t key, GVariant *data,
                        devc->samplerate = samplerates[idx];
                        if (dso_set_trigger_samplerate(sdi) != SR_OK)
                                return SR_ERR;
-                       sr_dbg("got new sample rate %d, idx %d", devc->samplerate, idx);
                        break;
                case SR_CONF_TRIGGER_SOURCE:
                        if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_sources))) < 0)
                                return SR_ERR_ARG;
+                       g_free(devc->triggersource);
                        devc->triggersource = g_strdup(trigger_sources[idx]);
                        break;
                default:
@@ -711,7 +707,6 @@ static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
  */
 static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 {
-       struct sr_datafeed_packet packet;
        struct sr_dev_inst *sdi;
        struct dev_context *devc;
        int num_samples, pre;
@@ -789,10 +784,9 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
                devc->framebuf = NULL;
 
                /* Mark the end of this frame. */
-               packet.type = SR_DF_FRAME_END;
-               sr_session_send(sdi, &packet);
+               std_session_send_df_frame_end(sdi);
 
-               if (devc->limit_frames && ++devc->num_frames == devc->limit_frames) {
+               if (devc->limit_frames && ++devc->num_frames >= devc->limit_frames) {
                        /* Terminate session */
                        devc->dev_state = STOPPING;
                } else {
@@ -804,7 +798,6 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 static int handle_event(int fd, int revents, void *cb_data)
 {
        const struct sr_dev_inst *sdi;
-       struct sr_datafeed_packet packet;
        struct timeval tv;
        struct sr_dev_driver *di;
        struct dev_context *devc;
@@ -846,8 +839,10 @@ static int handle_event(int fd, int revents, void *cb_data)
                        return TRUE;
                if (dso_enable_trigger(sdi) != SR_OK)
                        return TRUE;
-//             if (dso_force_trigger(sdi) != SR_OK)
-//                     return TRUE;
+               if (!devc->triggersource) {
+                       if (dso_force_trigger(sdi) != SR_OK)
+                               return TRUE;
+               }
                sr_dbg("Successfully requested next chunk.");
                devc->dev_state = CAPTURE;
                return TRUE;
@@ -868,8 +863,10 @@ static int handle_event(int fd, int revents, void *cb_data)
                                break;
                        if (dso_enable_trigger(sdi) != SR_OK)
                                break;
-//                     if (dso_force_trigger(sdi) != SR_OK)
-//                             break;
+                       if (!devc->triggersource) {
+                               if (dso_force_trigger(sdi) != SR_OK)
+                                       break;
+                       }
                        sr_dbg("Successfully requested next chunk.");
                }
                break;
@@ -877,7 +874,7 @@ static int handle_event(int fd, int revents, void *cb_data)
                /* No data yet. */
                break;
        case CAPTURE_READY_8BIT:
-       case CAPTURE_READY2250:
+       case CAPTURE_READY_2250:
                /* Remember where in the captured frame the trigger is. */
                devc->trigger_offset = trigger_offset;
 
@@ -896,8 +893,7 @@ static int handle_event(int fd, int revents, void *cb_data)
                devc->dev_state = FETCH_DATA;
 
                /* Tell the frontend a new frame is on the way. */
-               packet.type = SR_DF_FRAME_BEGIN;
-               sr_session_send(sdi, &packet);
+               std_session_send_df_frame_begin(sdi);
                break;
        case CAPTURE_READY_9BIT:
                /* TODO */
@@ -947,6 +943,7 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
        devc->dev_state = STOPPING;
+       devc->num_frames = 0;
 
        return SR_OK;
 }