return SR_OK;
for (l = drvc->instances; l; l = l->next) {
+
if (!(sdi = l->data))
continue;
- if (!(devc = sdi->priv))
- continue;
- g_free(devc->device);
- g_free(devc->coupling[0]);
- g_free(devc->coupling[1]);
- g_free(devc->trigger_source);
- g_free(devc->trigger_slope);
- close(devc->fd);
+ if (sdi->conn) {
+ serial_close(sdi->conn);
+ sr_serial_dev_inst_free(sdi->conn);
+ }
+
+ if ((devc = sdi->priv)) {
+ g_free(devc->device);
+ g_free(devc->coupling[0]);
+ g_free(devc->coupling[1]);
+ g_free(devc->trigger_source);
+ g_free(devc->trigger_slope);
+ }
sr_dev_inst_free(sdi);
}
static int set_cfg(const struct sr_dev_inst *sdi, const char *format, ...)
{
- struct dev_context *devc;
va_list args;
char buf[256];
- devc = sdi->priv;
-
va_start(args, format);
vsnprintf(buf, 255, format, args);
va_end(args);
- if (rigol_ds1xx2_send(devc, buf) != SR_OK)
+ if (rigol_ds1xx2_send(sdi, buf) != SR_OK)
return SR_ERR;
/* When setting a bunch of parameters in a row, the DS1052E scrambles
static int probe_device(struct drv_context *drvc, const char *device)
{
+ struct sr_serial_dev_inst *serial;
const gchar *idn_query = "*IDN?";
unsigned int i;
- int len, num_tokens, fd;
+ int len, num_tokens;
const gchar *delimiter = ",";
gchar **tokens;
const char *manufacturer, *model, *version;
gchar *channel_name;
struct sr_probe *probe;
- fd = open(device, O_RDWR);
- len = write(fd, idn_query, strlen(idn_query));
- len = read(fd, buf, sizeof(buf));
- close(fd);
+ if (!(serial = sr_serial_dev_inst_new(device, NULL)))
+ return SR_ERR_MALLOC;
+
+ if (serial_open(serial, SERIAL_RDWR) != SR_OK)
+ return SR_ERR;
+ len = serial_write(serial, idn_query, strlen(idn_query));
+ len = serial_read(serial, buf, sizeof(buf));
+ if (serial_close(serial) != SR_OK)
+ return SR_ERR;
+
+ sr_serial_dev_inst_free(serial);
+
if (len == 0)
return SR_ERR_NA;
buf[len] = 0;
tokens = g_strsplit(buf, delimiter, 0);
- close(fd);
sr_dbg("response: %s %d [%s]", device, len, buf);
for (num_tokens = 0; tokens[num_tokens] != NULL; num_tokens++);
static int hw_dev_open(struct sr_dev_inst *sdi)
{
struct dev_context *devc;
- int fd;
devc = sdi->priv;
- if ((fd = open(devc->device, O_RDWR)) == -1)
+ if (!(sdi->conn = sr_serial_dev_inst_new(devc->device, NULL)))
+ return SR_ERR;
+
+ if (serial_open(sdi->conn, SERIAL_RDWR) != SR_OK)
return SR_ERR;
- devc->fd = fd;
if (rigol_ds1xx2_get_dev_cfg(sdi) != SR_OK)
/* TODO: force configuration? */
static int hw_dev_close(struct sr_dev_inst *sdi)
{
- struct dev_context *devc;
-
- devc = sdi->priv;
+ if (serial_close(sdi->conn) != SR_OK)
+ return SR_ERR;
- close(devc->fd);
+ sr_serial_dev_inst_free(sdi->conn);
+ sdi->conn = NULL;
return SR_OK;
}
static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
{
+ struct sr_serial_dev_inst *serial;
struct dev_context *devc;
struct sr_probe *probe;
GSList *l;
(void)cb_data;
+ serial = sdi->conn;
devc = sdi->priv;
for (l = sdi->probes; l; l = l->next) {
/* Enabled channel is currently disabled, or vice versa. */
sprintf(cmd, ":CHAN%d:DISP %s", probe->index + 1,
probe->enabled ? "ON" : "OFF");
- if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+ if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
return SR_ERR;
}
} else if (probe->type == SR_PROBE_LOGIC) {
/* Enabled channel is currently disabled, or vice versa. */
sprintf(cmd, ":DIG%d:TURN %s", probe->index,
probe->enabled ? "ON" : "OFF");
- if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+ if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
return SR_ERR;
}
}
if (!devc->enabled_analog_probes && !devc->enabled_digital_probes)
return SR_ERR;
- sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
+ sr_source_add(serial->fd, G_IO_IN, 50, rigol_ds1xx2_receive, (void *)sdi);
/* Send header packet to the session bus. */
std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
/* Fetch the first frame. */
if (devc->enabled_analog_probes) {
devc->channel_frame = devc->enabled_analog_probes->data;
- if (rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%d",
+ if (rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%d",
devc->channel_frame->index + 1) != SR_OK)
return SR_ERR;
} else {
devc->channel_frame = devc->enabled_digital_probes->data;
- if (rigol_ds1xx2_send(devc, ":WAV:DATA? DIG") != SR_OK)
+ if (rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG") != SR_OK)
return SR_ERR;
}
SR_PRIV int rigol_ds1xx2_receive(int fd, int revents, void *cb_data)
{
struct sr_dev_inst *sdi;
+ struct sr_serial_dev_inst *serial;
struct dev_context *devc;
struct sr_datafeed_packet packet;
struct sr_datafeed_analog analog;
int len, i, waveform_size;
struct sr_probe *probe;
+ (void) fd;
+
if (!(sdi = cb_data))
return TRUE;
if (!(devc = sdi->priv))
return TRUE;
+ serial = sdi->conn;
+
if (revents == G_IO_IN) {
probe = devc->channel_frame;
waveform_size = probe->type == SR_PROBE_ANALOG ?
ANALOG_WAVEFORM_SIZE : DIGITAL_WAVEFORM_SIZE;
- len = read(fd, buf, waveform_size - devc->num_frame_bytes);
+ len = serial_read(serial, buf, waveform_size - devc->num_frame_bytes);
sr_dbg("Received %d bytes.", len);
if (len == -1)
return TRUE;
/* We got the frame for the first analog channel, but
* there's a second analog channel. */
devc->channel_frame = devc->enabled_analog_probes->next->data;
- rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+ rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%c",
devc->channel_frame->name[2]);
} else {
/* Done with both analog channels in this frame. */
&& devc->channel_frame != devc->enabled_digital_probes->data) {
/* Now we need to get the digital data. */
devc->channel_frame = devc->enabled_digital_probes->data;
- rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
+ rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG");
} else if (++devc->num_frames == devc->limit_frames) {
/* End of last frame. */
sdi->driver->dev_acquisition_stop(sdi, cb_data);
/* Get the next frame, starting with the first analog channel. */
if (devc->enabled_analog_probes) {
devc->channel_frame = devc->enabled_analog_probes->data;
- rigol_ds1xx2_send(devc, ":WAV:DATA? CHAN%c",
+ rigol_ds1xx2_send(sdi, ":WAV:DATA? CHAN%c",
devc->channel_frame->name[2]);
} else {
devc->channel_frame = devc->enabled_digital_probes->data;
- rigol_ds1xx2_send(devc, ":WAV:DATA? DIG");
+ rigol_ds1xx2_send(sdi, ":WAV:DATA? DIG");
}
}
}
return TRUE;
}
-SR_PRIV int rigol_ds1xx2_send(struct dev_context *devc, const char *format, ...)
+SR_PRIV int rigol_ds1xx2_send(const struct sr_dev_inst *sdi, const char *format, ...)
{
va_list args;
char buf[256];
va_end(args);
strcat(buf, "\n");
len++;
- out = write(devc->fd, buf, len);
+ out = serial_write(sdi->conn, buf, len);
buf[len - 1] = '\0';
if (out != len) {
sr_dbg("Only sent %d/%d bytes of '%s'.", out, len, buf);
static int get_cfg(const struct sr_dev_inst *sdi, char *cmd, char *reply)
{
- struct dev_context *devc;
int len;
- devc = sdi->priv;
-
- if (rigol_ds1xx2_send(devc, cmd) != SR_OK)
+ if (rigol_ds1xx2_send(sdi, cmd) != SR_OK)
return SR_ERR;
- if ((len = read(devc->fd, reply, 255)) < 0)
+ if ((len = serial_read(sdi->conn, reply, 255)) < 0)
return SR_ERR;
reply[len] = '\0';
sr_spew("Received '%s'.", reply);