static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { struct sr_datafeed_packet packet; (void)sdi; sr_dbg("Stopping acquisition."); /* Send end packet to the session bus. */ sr_dbg("Sending SR_DF_END."); packet.type = SR_DF_END; sr_session_send(cb_data, &packet); /* TODO? */ sr_source_remove(0); return SR_OK; }
static int read_subframe(const struct sr_dev_inst *sdi, uint8_t *buf) { struct dev_context *devc = sdi->priv; uint8_t sig[3], ctrl; unsigned int num; gboolean interleave; interleave = readout_steps[devc->step].interleave; ctrl = C1284_NSTROBE; if ((interleave && devc->adc2) || (!interleave && devc->channel == 2)) ctrl |= C1284_NAUTOFD; ieee1284_write_control(sdi->conn, ctrl); num = readout_steps[devc->step].num; if (num < 1000) skip_samples(sdi->conn, ctrl, 1000 - num); read_samples(sdi->conn, ctrl, buf + (devc->adc2 ? 1 : 0), num, interleave ? 2 : 1); read_samples(sdi->conn, ctrl, sig, 3, 1); if (sig[0] != 0x01 || sig[1] != 0xfe || sig[2] != 0x80) { if (--devc->retries) { sr_dbg("Missing signature at end of buffer, %i tries remaining", devc->retries); return TRUE; } else { sr_err("Failed to read frame without transfer errors"); devc->step = 0; } } else { if (interleave && !devc->adc2) { devc->adc2 = TRUE; devc->retries = MAX_RETRIES; return TRUE; } else { if (interleave) num *= 2; if (!devc->step) { struct sr_datafeed_packet packet = { .type = SR_DF_TRIGGER }; push_samples(sdi, buf, 6); sr_session_send(sdi, &packet); buf += 6; num -= 6; } push_samples(sdi, buf, num); if (++devc->step > devc->last_step) devc->step = 0; } } devc->adc2 = FALSE; devc->retries = MAX_RETRIES; return devc->step > 0; }
SR_PRIV int cv_convert_trigger(const struct sr_dev_inst *sdi) { struct dev_context *devc; struct sr_trigger *trigger; struct sr_trigger_stage *stage; struct sr_trigger_match *match; const GSList *l, *m; uint16_t channel_bit; devc = sdi->priv; devc->trigger_pattern = 0x0000; /* Default to "low" trigger. */ devc->trigger_mask = 0x0000; /* Default to "don't care". */ devc->trigger_edgemask = 0x0000; /* Default to "state triggered". */ if (!(trigger = sr_session_trigger_get(sdi->session))) return SR_OK; if (g_slist_length(trigger->stages) > 1) { sr_err("This device only supports 1 trigger stage."); return SR_ERR; } for (l = trigger->stages; l; l = l->next) { stage = l->data; for (m = stage->matches; m; m = m->next) { match = m->data; if (!match->channel->enabled) /* Ignore disabled channels with a trigger. */ continue; if (devc->prof->model == CHRONOVU_LA8 && (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING)) { sr_err("This model supports only simple triggers."); return SR_ERR; } channel_bit = (1 << (match->channel->index)); /* state: 1 == high, edge: 1 == rising edge. */ if (match->match == SR_TRIGGER_ONE || match->match == SR_TRIGGER_RISING) devc->trigger_pattern |= channel_bit; /* LA16 (but not LA8) supports edge triggering. */ if ((devc->prof->model == CHRONOVU_LA16)) { if (match->match == SR_TRIGGER_RISING || match->match == SR_TRIGGER_FALLING) devc->trigger_edgemask |= channel_bit; } } } sr_dbg("Trigger pattern/mask/edgemask = 0x%04x / 0x%04x / 0x%04x.", devc->trigger_pattern, devc->trigger_mask, devc->trigger_edgemask); return SR_OK; }
SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb) { struct libusb_device **devlist; struct libusb_device_descriptor des; int ret, r, cnt, i, a, b; sr_dbg("Trying to open USB device %d.%d.", usb->bus, usb->address); if ((cnt = libusb_get_device_list(usb_ctx, &devlist)) < 0) { sr_err("Failed to retrieve device list: %s.", libusb_error_name(cnt)); return SR_ERR; } ret = SR_ERR; for (i = 0; i < cnt; i++) { if ((r = libusb_get_device_descriptor(devlist[i], &des)) < 0) { sr_err("Failed to get device descriptor: %s.", libusb_error_name(r)); continue; } b = libusb_get_bus_number(devlist[i]); a = libusb_get_device_address(devlist[i]); if (b != usb->bus || a != usb->address) continue; if ((r = libusb_open(devlist[i], &usb->devhdl)) < 0) { sr_err("Failed to open device: %s.", libusb_error_name(r)); break; } sr_dbg("Opened USB device (VID:PID = %04x:%04x, bus.address = " "%d.%d).", des.idVendor, des.idProduct, b, a); ret = SR_OK; break; } libusb_free_device_list(devlist, 1); return ret; }
static int scpi_usbtmc_remote(struct scpi_usbtmc_libusb *uscpi) { struct sr_usb_dev_inst *usb = uscpi->usb; struct libusb_device *dev; struct libusb_device_descriptor des; int ret; uint8_t status; if (!(uscpi->usb488_dev_cap & USB488_DEV_CAP_RL1)) return SR_OK; dev = libusb_get_device(usb->devhdl); libusb_get_device_descriptor(dev, &des); if (check_usbtmc_blacklist(blacklist_remote, des.idVendor, des.idProduct)) return SR_OK; sr_dbg("Locking out local control."); ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, REN_CONTROL, 1, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { if (ret < 0) sr_dbg("Failed to enter REN state: %s.", libusb_error_name(ret)); else sr_dbg("Failed to enter REN state: USBTMC status %d.", status); return SR_ERR; } ret = libusb_control_transfer(usb->devhdl, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, LOCAL_LOCKOUT, 0, uscpi->interface, &status, 1, TRANSFER_TIMEOUT); if (ret < 0 || status != USBTMC_STATUS_SUCCESS) { if (ret < 0) sr_dbg("Failed to enter local lockout state: %s.", libusb_error_name(ret)); else sr_dbg("Failed to enter local lockout state: USBTMC " "status %d.", status); return SR_ERR; } return SR_OK; }
static int resource_open_default(struct sr_resource *res, const char *name, void *cb_data) { int64_t filesize; #ifdef FIRMWARE_DIR const char *builtindir; #endif const char *subdir; const char *const *datadirs; FILE *file; (void)cb_data; switch (res->type) { case SR_RESOURCE_FIRMWARE: #ifdef FIRMWARE_DIR builtindir = FIRMWARE_DIR; #endif subdir = "sigrok-firmware"; break; default: sr_err("%s: unknown type %d.", __func__, res->type); return SR_ERR_ARG; } file = try_open_file(g_get_user_data_dir(), subdir, name); /* * Scan the hard-coded directory before the system directories to * avoid picking up possibly outdated files from a system install. */ #ifdef FIRMWARE_DIR if (!file) file = try_open_file(builtindir, "", name); #endif if (!file) { datadirs = g_get_system_data_dirs(); while (*datadirs && !file) file = try_open_file(*datadirs++, subdir, name); } if (!file) { sr_dbg("Failed to locate '%s'.", name); return SR_ERR; } filesize = sr_file_get_size(file); if (filesize < 0) { sr_err("Failed to obtain size of '%s': %s", name, g_strerror(errno)); fclose(file); return SR_ERR; } res->size = filesize; res->handle = file; return SR_OK; }
/** * Send the *IDN? SCPI command, receive the reply, parse it and store the * reply as a sr_scpi_hw_info structure in the supplied scpi_response pointer. * * The hw_info structure must be freed by the caller via sr_scpi_hw_info_free(). * * @param scpi Previously initialised SCPI device structure. * @param scpi_response Pointer where to store the hw_info structure. * * @return SR_OK upon success, SR_ERR on failure. */ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi, struct sr_scpi_hw_info **scpi_response) { int num_tokens; char *response; char *newline; gchar **tokens; struct sr_scpi_hw_info *hw_info; response = NULL; tokens = NULL; if (sr_scpi_get_string(scpi, SCPI_CMD_IDN, &response) != SR_OK) if (!response) return SR_ERR; sr_info("Got IDN string: '%s'", response); /* Remove trailing newline if present. */ if ((newline = g_strrstr(response, "\n"))) newline[0] = '\0'; /* * The response to a '*IDN?' is specified by the SCPI spec. It contains * a comma-separated list containing the manufacturer name, instrument * model, serial number of the instrument and the firmware version. */ tokens = g_strsplit(response, ",", 0); for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++); if (num_tokens != 4) { sr_dbg("IDN response not according to spec: %80.s.", response); g_strfreev(tokens); g_free(response); return SR_ERR; } g_free(response); hw_info = g_try_malloc(sizeof(struct sr_scpi_hw_info)); if (!hw_info) { g_strfreev(tokens); return SR_ERR_MALLOC; } hw_info->manufacturer = g_strdup(tokens[0]); hw_info->model = g_strdup(tokens[1]); hw_info->serial_number = g_strdup(tokens[2]); hw_info->firmware_version = g_strdup(tokens[3]); g_strfreev(tokens); *scpi_response = hw_info; return SR_OK; }
/** FD event source finalize() method. */ static void fd_source_finalize(GSource *source) { struct fd_source *fsource; fsource = (struct fd_source *)source; sr_dbg("%s: key %p", __func__, fsource->key); sr_session_source_destroyed(fsource->session, fsource->key, source); }
static void log_dmm_packet(const uint8_t *buf) { sr_dbg("DMM packet: %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x %02x %02x " "%02x %02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15], buf[16], buf[17], buf[18], buf[19], buf[20], buf[21], buf[22]); }
static int hw_init(struct sr_context *sr_ctx, int dmm) { if (dmm == UNI_T_UT61D) di = di_ut61d; else if (dmm == VOLTCRAFT_VC820) di = di_vc820; sr_dbg("Selected '%s' subdriver.", di->name); return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN); }
static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_probe_group *probe_group) { struct dev_context *devc; const char *tmp_str; unsigned int i; (void)probe_group; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; if (!(devc = sdi->priv)) { sr_err("sdi->priv was NULL."); return SR_ERR_BUG; } switch (key) { case SR_CONF_LIMIT_SAMPLES: devc->limit_samples = g_variant_get_uint64(data); sr_dbg("Setting sample limit to %" PRIu64 ".", devc->limit_samples); break; case SR_CONF_LIMIT_MSEC: devc->limit_msec = g_variant_get_uint64(data); sr_dbg("Setting time limit to %" PRIu64 "ms.", devc->limit_msec); break; case SR_CONF_DATA_SOURCE: { tmp_str = g_variant_get_string(data, NULL); for (i = 0; i < ARRAY_SIZE(data_sources); i++) if (!strcmp(tmp_str, data_sources[i])) { devc->data_source = i; break; } if (i == ARRAY_SIZE(data_sources)) return SR_ERR; break; } default: return SR_ERR_NA; } return SR_OK; }
/** * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic. * * @param devc The struct containing private per-device-instance data. * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. */ SR_PRIV int la8_close_usb_reset_sequencer(struct dev_context *devc) { /* Magic sequence of bytes for resetting the LA8 sequencer logic. */ uint8_t buf[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}; int ret; if (!devc) { sr_err("%s: devc was NULL.", __func__); return SR_ERR_ARG; } if (!devc->ftdic) { sr_err("%s: devc->ftdic was NULL.", __func__); return SR_ERR_ARG; } if (devc->ftdic->usb_dev) { /* Reset the LA8 sequencer logic, then wait 100ms. */ sr_dbg("Resetting sequencer logic."); (void) la8_write(devc, buf, 8); /* Ignore errors. */ g_usleep(100 * 1000); /* Purge FTDI buffers, then reset and close the FTDI device. */ sr_dbg("Purging buffers, resetting+closing FTDI device."); /* Log errors, but ignore them (i.e., don't abort). */ if ((ret = ftdi_usb_purge_buffers(devc->ftdic)) < 0) sr_err("%s: ftdi_usb_purge_buffers: (%d) %s.", __func__, ret, ftdi_get_error_string(devc->ftdic)); if ((ret = ftdi_usb_reset(devc->ftdic)) < 0) sr_err("%s: ftdi_usb_reset: (%d) %s.", __func__, ret, ftdi_get_error_string(devc->ftdic)); if ((ret = ftdi_usb_close(devc->ftdic)) < 0) sr_err("%s: ftdi_usb_close: (%d) %s.", __func__, ret, ftdi_get_error_string(devc->ftdic)); } /* Close USB device, deinitialize and free the FTDI context. */ ftdi_free(devc->ftdic); /* Returns void. */ devc->ftdic = NULL; return SR_OK; }
static int receive_data(int fd, int revents, const struct sr_dev_inst *cb_sdi) { struct sr_dev_inst *sdi; struct session_vdev *vdev; struct sr_datafeed_packet packet; struct sr_datafeed_logic logic; GSList *l; void *buf; int ret, got_data; (void)fd; (void)revents; sr_dbg("Feed chunk."); got_data = FALSE; for (l = dev_insts; l; l = l->next) { sdi = l->data; vdev = sdi->priv; if (!vdev) /* already done with this instance */ continue; if (!(buf = g_try_malloc(CHUNKSIZE))) { sr_err("%s: buf malloc failed", __func__); return FALSE; } ret = zip_fread(vdev->capfile, buf, CHUNKSIZE); if (ret > 0) { got_data = TRUE; packet.type = SR_DF_LOGIC; packet.payload = &logic; logic.length = ret; logic.unitsize = vdev->unitsize; logic.data = buf; vdev->bytes_read += ret; sr_session_send(cb_sdi, &packet); } else { /* done with this capture file */ zip_fclose(vdev->capfile); g_free(vdev->capturefile); g_free(vdev); sdi->priv = NULL; } } if (!got_data) { packet.type = SR_DF_END; sr_session_send(cb_sdi, &packet); sr_session_source_remove(-1); } return TRUE; }
SR_PRIV void sl2_configure_trigger(const struct sr_dev_inst *sdi) { struct dev_context *devc; struct sr_probe *probe; uint8_t trigger_type; int probe_index, num_triggers_anyedge; char *trigger; GSList *l; devc = sdi->priv; /* Disable the trigger by default. */ devc->trigger_channel = TRIGGER_CHANNEL_0; devc->trigger_type = TRIGGER_TYPE_NONE; num_triggers_anyedge = 0; for (l = sdi->probes, probe_index = 0; l; l = l->next, probe_index++) { probe = l->data; trigger = probe->trigger; if (!trigger || !probe->enabled) continue; switch (*trigger) { case 'r': trigger_type = TRIGGER_TYPE_POSEDGE; break; case 'f': trigger_type = TRIGGER_TYPE_NEGEDGE; break; case 'c': trigger_type = TRIGGER_TYPE_ANYEDGE; num_triggers_anyedge++; break; default: continue; } devc->trigger_channel = probe_index + 1; devc->trigger_type = trigger_type; } /* * Set trigger to any edge on all channels if the trigger for each * channel is set to any edge. */ if (num_triggers_anyedge == NUM_PROBES) { devc->trigger_channel = TRIGGER_CHANNEL_ALL; devc->trigger_type = TRIGGER_TYPE_ANYEDGE; } sr_dbg("Trigger set to channel 0x%02x and type 0x%02x.", devc->trigger_channel, devc->trigger_type); }
/* Read the header of a data block */ static int rigol_ds_read_header(struct sr_dev_inst *sdi) { struct sr_scpi_dev_inst *scpi = sdi->conn; struct dev_context *devc = sdi->priv; char *buf = (char *) devc->buffer; size_t header_length; int ret; /* Try to read the hashsign and length digit. */ if (devc->num_header_bytes < 2) { ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, 2 - devc->num_header_bytes); if (ret < 0) { sr_err("Read error while reading data header."); return SR_ERR; } devc->num_header_bytes += ret; } if (devc->num_header_bytes < 2) return 0; if (buf[0] != '#' || !isdigit(buf[1]) || buf[1] == '0') { sr_err("Received invalid data block header '%c%c'.", buf[0], buf[1]); return SR_ERR; } header_length = 2 + buf[1] - '0'; /* Try to read the length. */ if (devc->num_header_bytes < header_length) { ret = sr_scpi_read_data(scpi, buf + devc->num_header_bytes, header_length - devc->num_header_bytes); if (ret < 0) { sr_err("Read error while reading data header."); return SR_ERR; } devc->num_header_bytes += ret; } if (devc->num_header_bytes < header_length) return 0; /* Read the data length. */ buf[header_length] = '\0'; if (parse_int(buf + 2, &ret) != SR_OK) { sr_err("Received invalid data block length '%s'.", buf + 2); return -1; } sr_dbg("Received data block header: '%s' -> block length %d", buf, ret); return ret; }
static GSList *scan(GSList *options) { struct drv_context *drvc; struct dev_context *devc; struct sr_dev_inst *sdi; struct sr_config *src; struct sr_probe *probe; GSList *devices, *l; const char *conn, *serialcomm; drvc = di->priv; drvc->instances = NULL; devices = NULL; conn = serialcomm = NULL; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { case SR_CONF_CONN: conn = g_variant_get_string(src->data, NULL); break; case SR_CONF_SERIALCOMM: serialcomm = g_variant_get_string(src->data, NULL); break; } } if (!conn) return NULL; if (!serialcomm) serialcomm = SERIALCOMM; if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead", "SL-5868P", NULL))) return NULL; if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { sr_dbg("Device context malloc failed."); return NULL; } if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm))) return NULL; sdi->inst_type = SR_INST_SERIAL; sdi->priv = devc; sdi->driver = di; if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1"))) return NULL; sdi->probes = g_slist_append(sdi->probes, probe); drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); return devices; }
/** * Debug helper. * * @param packet The packet to show debugging information for. */ static void datafeed_dump(struct sr_datafeed_packet *packet) { struct sr_datafeed_logic *logic; struct sr_datafeed_analog *analog; switch (packet->type) { case SR_DF_HEADER: sr_dbg("bus: received SR_DF_HEADER"); break; case SR_DF_TRIGGER: sr_dbg("bus: received SR_DF_TRIGGER"); break; case SR_DF_META_LOGIC: sr_dbg("bus: received SR_DF_META_LOGIC"); break; case SR_DF_LOGIC: logic = packet->payload; /* TODO: Check for logic != NULL. */ sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); break; case SR_DF_META_ANALOG: sr_dbg("bus: received SR_DF_META_LOGIC"); break; case SR_DF_ANALOG: analog = packet->payload; /* TODO: Check for analog != NULL. */ sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples); break; case SR_DF_END: sr_dbg("bus: received SR_DF_END"); break; case SR_DF_FRAME_BEGIN: sr_dbg("bus: received SR_DF_FRAME_BEGIN"); break; case SR_DF_FRAME_END: sr_dbg("bus: received SR_DF_FRAME_END"); break; default: sr_dbg("bus: received unknown packet type %d", packet->type); break; } }
static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi) { struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; if (!(devc = sdi->priv)) { sr_err("%s: sdi->priv was NULL.", __func__); return SR_ERR_BUG; } switch (id) { case SR_CONF_SAMPLERATE: if (set_samplerate(sdi, g_variant_get_uint64(data)) == SR_ERR) { sr_err("%s: setting samplerate failed.", __func__); return SR_ERR; } sr_dbg("SAMPLERATE = %" PRIu64, devc->cur_samplerate); break; case SR_CONF_LIMIT_MSEC: if (g_variant_get_uint64(data) == 0) { sr_err("%s: LIMIT_MSEC can't be 0.", __func__); return SR_ERR; } devc->limit_msec = g_variant_get_uint64(data); sr_dbg("LIMIT_MSEC = %" PRIu64, devc->limit_msec); break; case SR_CONF_LIMIT_SAMPLES: if (g_variant_get_uint64(data) < MIN_NUM_SAMPLES) { sr_err("%s: LIMIT_SAMPLES too small.", __func__); return SR_ERR; } devc->limit_samples = g_variant_get_uint64(data); sr_dbg("LIMIT_SAMPLES = %" PRIu64, devc->limit_samples); break; default: return SR_ERR_NA; } return SR_OK; }
SR_PRIV int hantek_6xxx_init(const struct sr_dev_inst *sdi) { sr_dbg("Initializing"); hantek_6xxx_update_samplerate(sdi); hantek_6xxx_update_vdiv(sdi); hantek_6xxx_update_coupling(sdi); // hantek_6xxx_update_channels(sdi); /* Only 2 channel mode supported. */ return SR_OK; }
static int receive_data(int fd, int revents, void *cb_data) { int i, ret; struct sr_dev_inst *sdi; struct dev_context *devc; (void)fd; (void)revents; if (!(sdi = cb_data)) { sr_err("cb_data was NULL."); return FALSE; } if (!(devc = sdi->priv)) { sr_err("sdi->priv was NULL."); return FALSE; } if (!devc->ftdic) { sr_err("devc->ftdic was NULL."); return FALSE; } /* Get one block of data. */ if ((ret = cv_read_block(devc)) < 0) { sr_err("Failed to read data block: %d.", ret); dev_acquisition_stop(sdi, sdi); return FALSE; } /* We need to get exactly NUM_BLOCKS blocks (i.e. 8MB) of data. */ if (devc->block_counter != (NUM_BLOCKS - 1)) { devc->block_counter++; return TRUE; } sr_dbg("Sampling finished, sending data to session bus now."); /* * All data was received and demangled, send it to the session bus. * * Note: Due to the method how data is spread across the 8MByte of * SDRAM, we can _not_ send it to the session bus in a streaming * manner while we receive it. We have to receive and de-mangle the * full 8MByte first, only then the whole buffer contains valid data. */ for (i = 0; i < NUM_BLOCKS; i++) cv_send_block_to_session_bus(devc, i); dev_acquisition_stop(sdi, sdi); return TRUE; }
static void clear_dev_context(void *priv) { struct dev_context *devc; devc = priv; sr_dbg("Device context cleared."); lwla_free_acquisition_state(devc->acquisition); g_free(devc); }
static int dev_acquisition_stop(struct sr_dev_inst *sdi) { if (sdi->status != SR_ST_ACTIVE) return SR_ERR_DEV_CLOSED; sr_dbg("Stopping acquisition."); sdi->status = SR_ST_STOPPING; return SR_OK; }
/** Set up the main context the session will be executing in. * * Must be called just before the session starts, by the thread which * will execute the session main loop. Once acquired, the main context * pointer is immutable for the duration of the session run. */ static int set_main_context(struct sr_session *session) { GMainContext *def_context; /* May happen if sr_session_start() is called again after * sr_session_run(), but the session hasn't been stopped yet. */ if (session->main_loop) { sr_err("Cannot set main context; main loop already created."); return SR_ERR; } g_mutex_lock(&session->main_mutex); def_context = g_main_context_get_thread_default(); if (!def_context) def_context = g_main_context_default(); /* * Try to use an existing main context if possible, but only if we * can make it owned by the current thread. Otherwise, create our * own main context so that event source callbacks can execute in * the session thread. */ if (g_main_context_acquire(def_context)) { g_main_context_release(def_context); sr_dbg("Using thread-default main context."); session->main_context = def_context; session->main_context_is_default = TRUE; } else { sr_dbg("Creating our own main context."); session->main_context = g_main_context_new(); session->main_context_is_default = FALSE; } g_mutex_unlock(&session->main_mutex); return SR_OK; }
static void clear_dev_context(void *priv) { struct dev_context *devc; devc = priv; sr_dbg("Device context cleared."); libusb_free_transfer(devc->xfer_in); libusb_free_transfer(devc->xfer_out); g_free(devc); }
/** * Standard sr_session_stop() API helper. * * This function can be used to simplify most (serial port based) driver's * dev_acquisition_stop() API callback. * * @param sdi The device instance for which acquisition should stop. * Must not be NULL. * @param cb_data Opaque 'cb_data' pointer. Must not be NULL. * @param dev_close_fn Function pointer to the driver's dev_close(). * Must not be NULL. * @param serial The serial device instance (struct serial_dev_inst *). * Must not be NULL. * @param[in] prefix A driver-specific prefix string used for log messages. * Must not be NULL. An empty string is allowed. * * @retval SR_OK Success. * @retval SR_ERR_ARG Invalid arguments. * @retval SR_ERR_DEV_CLOSED Device is closed. * @retval SR_ERR Other errors. */ SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data, dev_close_callback dev_close_fn, struct sr_serial_dev_inst *serial, const char *prefix) { int ret; struct sr_datafeed_packet packet; if (!prefix) { sr_err("Invalid prefix."); return SR_ERR_ARG; } if (sdi->status != SR_ST_ACTIVE) { sr_err("%s: Device inactive, can't stop acquisition.", prefix); return SR_ERR_DEV_CLOSED; } sr_dbg("%s: Stopping acquisition.", prefix); if ((ret = serial_source_remove(sdi->session, serial)) < 0) { sr_err("%s: Failed to remove source: %d.", prefix, ret); return ret; } if ((ret = dev_close_fn(sdi)) < 0) { sr_err("%s: Failed to close device: %d.", prefix, ret); return ret; } /* Send SR_DF_END packet to the session bus. */ sr_dbg("%s: Sending SR_DF_END packet.", prefix); packet.type = SR_DF_END; packet.payload = NULL; if ((ret = sr_session_send(cb_data, &packet)) < 0) { sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret); return ret; } return SR_OK; }
static int scan_kecheng(struct sr_dev_driver *di, struct sr_usb_dev_inst *usb, char **model) { struct drv_context *drvc; int len, ret; unsigned char cmd, buf[32]; drvc = di->context; if (sr_usb_open(drvc->sr_ctx->libusb_ctx, usb) != SR_OK) return SR_ERR; cmd = CMD_IDENTIFY; ret = libusb_bulk_transfer(usb->devhdl, EP_OUT, &cmd, 1, &len, 5); if (ret != 0) { libusb_close(usb->devhdl); sr_dbg("Failed to send Identify command: %s", libusb_error_name(ret)); return SR_ERR; } ret = libusb_bulk_transfer(usb->devhdl, EP_IN, buf, 32, &len, 10); if (ret != 0) { libusb_close(usb->devhdl); sr_dbg("Failed to receive response: %s", libusb_error_name(ret)); return SR_ERR; } libusb_close(usb->devhdl); usb->devhdl = NULL; if (len < 2 || buf[0] != (CMD_IDENTIFY | 0x80) || buf[1] > 30) { sr_dbg("Invalid response to Identify command"); return SR_ERR; } buf[buf[1] + 2] = '\x0'; *model = g_strndup((const gchar *)buf + 2, 30); return SR_OK; }
SR_PRIV int hantek_6xxx_update_vdiv(const struct sr_dev_inst *sdi) { struct dev_context *devc = sdi->priv; int ret1, ret2; sr_dbg("update vdiv %d %d", voltage_to_reg(devc->voltage[0]), voltage_to_reg(devc->voltage[1])); ret1 = write_control(sdi, VDIV_CH1_REG, voltage_to_reg(devc->voltage[0])); ret2 = write_control(sdi, VDIV_CH2_REG, voltage_to_reg(devc->voltage[1])); return MIN(ret1, ret2); }
static gboolean flags_valid(const struct metex14_info *info) { int count; /* Does the packet have more than one multiplier? */ count = 0; count += (info->is_pico) ? 1 : 0; count += (info->is_nano) ? 1 : 0; count += (info->is_micro) ? 1 : 0; count += (info->is_milli) ? 1 : 0; count += (info->is_kilo) ? 1 : 0; count += (info->is_mega) ? 1 : 0; if (count > 1) { sr_dbg("More than one multiplier detected in packet."); return FALSE; } /* Does the packet "measure" more than one type of value? */ count = 0; count += (info->is_ac) ? 1 : 0; count += (info->is_dc) ? 1 : 0; count += (info->is_resistance) ? 1 : 0; count += (info->is_capacity) ? 1 : 0; count += (info->is_temperature) ? 1 : 0; count += (info->is_diode) ? 1 : 0; count += (info->is_frequency) ? 1 : 0; if (count > 1) { sr_dbg("More than one measurement type detected in packet."); return FALSE; } /* Both AC and DC set? */ if (info->is_ac && info->is_dc) { sr_dbg("Both AC and DC flags detected in packet."); return FALSE; } return TRUE; }
/** * Set the libsigrok loglevel. * * This influences the amount of log messages (debug messages, error messages, * and so on) libsigrok will output. Using SR_LOG_NONE disables all messages. * * @param loglevel The loglevel to set (SR_LOG_NONE, SR_LOG_ERR, SR_LOG_WARN, * SR_LOG_INFO, SR_LOG_DBG, or SR_LOG_SPEW). * @return SR_OK upon success, SR_ERR_ARG upon invalid loglevel. */ int sr_set_loglevel(int loglevel) { if (loglevel < SR_LOG_NONE || loglevel > SR_LOG_SPEW) { sr_err("log: %s: invalid loglevel %d", __func__, loglevel); return SR_ERR_ARG; } sr_loglevel = loglevel; sr_dbg("log: %s: libsigrok loglevel set to %d", __func__, loglevel); return SR_OK; }
static int hw_dev_status_get(int dev_index) { struct sr_dev_inst *sdi; if (!(sdi = sr_dev_inst_get(genericdmm_dev_insts, dev_index))) { sr_err("genericdmm: sdi was NULL, device not found."); return SR_ST_NOT_FOUND; } sr_dbg("genericdmm: Returning status: %d.", sdi->status); return sdi->status; }