X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fgmc-mh-1x-2x%2Fapi.c;h=f2cbd7b20ae4d0b8e4117af4f41a5aabb434df3f;hb=b79c3422789d181ad47d4a3707d18d7feea73b5d;hp=13918c6365d6846ddcc4451f7d95c433ad16d08d;hpb=4f840ce965b1c30c5ab75afecc56193cbaf5c1b3;p=libsigrok.git
diff --git a/src/hardware/gmc-mh-1x-2x/api.c b/src/hardware/gmc-mh-1x-2x/api.c
index 13918c63..f2cbd7b2 100644
--- a/src/hardware/gmc-mh-1x-2x/api.c
+++ b/src/hardware/gmc-mh-1x-2x/api.c
@@ -17,11 +17,7 @@
* along with this program. If not, see .
*/
-/** @file
- * Gossen Metrawatt Metrahit 1x/2x drivers
- * @internal
- */
-
+#include
#include
#include "protocol.h"
@@ -29,20 +25,19 @@
#define SERIALCOMM_1X_RS232 "8228/6n1/dtr=1/rts=1/flow=0" /* =8192, closer with divider */
#define SERIALCOMM_2X_RS232 "9600/6n1/dtr=1/rts=1/flow=0"
#define SERIALCOMM_2X "9600/8n1/dtr=1/rts=1/flow=0"
-#define VENDOR_GMC "Gossen Metrawatt"
-
-SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info;
-SR_PRIV struct sr_dev_driver gmc_mh_2x_bd232_driver_info;
static const uint32_t scanopts[] = {
SR_CONF_CONN,
SR_CONF_SERIALCOMM,
};
+static const uint32_t drvopts[] = {
+ SR_CONF_MULTIMETER,
+ SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
+};
+
/** Hardware capabilities for Metrahit 1x/2x devices in send mode. */
static const uint32_t devopts_sm[] = {
- SR_CONF_MULTIMETER,
- SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
SR_CONF_CONTINUOUS,
SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
@@ -50,15 +45,12 @@ static const uint32_t devopts_sm[] = {
/** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */
static const uint32_t devopts_bd[] = {
- SR_CONF_MULTIMETER,
- SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */
SR_CONF_CONTINUOUS,
SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
SR_CONF_POWER_OFF | SR_CONF_GET | SR_CONF_SET,
};
-
/* TODO:
* - For the 29S SR_CONF_ENERGYMETER, too.
* - SR_CONF_PATTERN_MODE for some 2x devices
@@ -66,12 +58,6 @@ static const uint32_t devopts_bd[] = {
* Need to implement device-specific lists.
*/
-/** Init driver. */
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
- return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
/**
* Read single byte from serial port.
*
@@ -110,7 +96,7 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
gint64 timeout_us;
model = METRAHIT_NONE;
- timeout_us = g_get_monotonic_time() + 1 * 1000 * 1000;
+ timeout_us = g_get_monotonic_time() + (1 * 1000 * 1000);
/*
* Try to find message consisting of device code and several
@@ -127,7 +113,7 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
for (cnt = 0; cnt < 4; cnt++) {
byte = read_byte(serial, timeout_us);
if ((byte == -1) ||
- ((byte & MSGID_MASK) != MSGID_DATA))
+ ((byte & MSGID_MASK) != MSGID_DATA))
{
model = METRAHIT_NONE;
bytecnt = 100;
@@ -153,7 +139,6 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial)
static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
{
struct sr_dev_inst *sdi;
- struct drv_context *drvc;
struct dev_context *devc;
struct sr_config *src;
struct sr_serial_dev_inst *serial;
@@ -163,14 +148,9 @@ static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
gboolean serialcomm_given;
devices = NULL;
- drvc = di->priv;
- drvc->instances = NULL;
conn = serialcomm = NULL;
- model = METRAHIT_NONE;
serialcomm_given = FALSE;
- sr_spew("scan_1x_2x_rs232() called!");
-
for (l = options; l; l = l->next) {
src = l->data;
switch (src->key) {
@@ -214,36 +194,31 @@ static GSList *scan_1x_2x_rs232(struct sr_dev_driver *di, GSList *options)
}
if (model != METRAHIT_NONE) {
- sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model));
+ sr_spew("%s detected!", gmc_model_str(model));
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
- sdi->vendor = g_strdup(VENDOR_GMC);
+ sdi->vendor = g_strdup("Gossen Metrawatt");
sdi->model = g_strdup(gmc_model_str(model));
devc = g_malloc0(sizeof(struct dev_context));
+ sr_sw_limits_init(&devc->limits);
devc->model = model;
- devc->limit_samples = 0;
- devc->limit_msec = 0;
- devc->num_samples = 0;
- devc->elapsed_msec = g_timer_new();
devc->settings_ok = FALSE;
sdi->conn = serial;
sdi->priv = devc;
- sdi->driver = di;
sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
- drvc->instances = g_slist_append(drvc->instances, sdi);
devices = g_slist_append(devices, sdi);
}
- return devices;
+ return std_scan_complete(di, devices);
}
-/** Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt 'BD 232' interface.
- *
+/**
+ * Scan for Metrahit 2x in a bidirectional mode using Gossen Metrawatt
+ * 'BD 232' interface.
*/
static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
{
struct sr_dev_inst *sdi;
- struct drv_context *drvc;
struct dev_context *devc;
struct sr_config *src;
struct sr_serial_dev_inst *serial;
@@ -257,11 +232,6 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
conn = serialcomm = NULL;
devices = NULL;
- drvc = di->priv;
- drvc->instances = NULL;
-
- sr_spew("scan_2x_bd232() called!");
-
for (l = options; l; l = l->next) {
src = l->data;
switch (src->key) {
@@ -287,28 +257,27 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
- sdi->vendor = g_strdup(VENDOR_GMC);
+ sdi->vendor = g_strdup("Gossen Metrawatt");
sdi->priv = devc;
/* Send message 03 "Query multimeter version and status" */
sdi->conn = serial;
- sdi->priv = devc;
if (req_stat14(sdi, TRUE) != SR_OK)
goto exit_err;
/* Wait for reply from device(s) for up to 2s. */
- timeout_us = g_get_monotonic_time() + 2*1000*1000;
+ timeout_us = g_get_monotonic_time() + (2 * 1000 * 1000);
while (timeout_us > g_get_monotonic_time()) {
/* Receive reply (14 bytes) */
devc->buflen = 0;
- for (cnt = 0; cnt < 14; cnt++) {
+ for (cnt = 0; cnt < GMC_REPLY_SIZE; cnt++) {
byte = read_byte(serial, timeout_us);
if (byte != -1)
devc->buf[devc->buflen++] = (byte & MASK_6BITS);
}
- if (devc->buflen != 14)
+ if (devc->buflen != GMC_REPLY_SIZE)
continue;
devc->addr = devc->buf[0];
@@ -316,95 +285,62 @@ static GSList *scan_2x_bd232(struct sr_dev_driver *di, GSList *options)
devc->buflen = 0;
if (devc->model != METRAHIT_NONE) {
- sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model));
- devc->elapsed_msec = g_timer_new();
+ sr_spew("%s detected!", gmc_model_str(devc->model));
+ sr_sw_limits_init(&devc->limits);
sdi->model = g_strdup(gmc_model_str(devc->model));
sdi->version = g_strdup_printf("Firmware %d.%d", devc->fw_ver_maj, devc->fw_ver_min);
sdi->conn = serial;
sdi->priv = devc;
- sdi->driver = di;
sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
- drvc->instances = g_slist_append(drvc->instances, sdi);
devices = g_slist_append(devices, sdi);
devc = g_malloc0(sizeof(struct dev_context));
sdi = g_malloc0(sizeof(struct sr_dev_inst));
sdi->status = SR_ST_INACTIVE;
- sdi->vendor = g_strdup(VENDOR_GMC);
+ sdi->vendor = g_strdup("Gossen Metrawatt");
}
};
- /* Free last alloc if no device found */
- if (devc->model == METRAHIT_NONE) {
- g_free(devc);
- sr_dev_inst_free(sdi);
- }
+ /* Free last alloc that was done in preparation. */
+ g_free(devc);
+ sr_dev_inst_free(sdi);
- return devices;
+ return std_scan_complete(di, devices);
exit_err:
- sr_info("scan_2x_bd232(): Error!");
-
- if (serial)
- sr_serial_dev_inst_free(serial);
- if (devc)
- g_free(devc);
- if (sdi)
- sr_dev_inst_free(sdi);
+ sr_serial_dev_inst_free(serial);
+ g_free(devc);
+ sr_dev_inst_free(sdi);
return NULL;
}
-/** Driver device list function */
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
- return ((struct drv_context *)(di->priv))->instances;
-}
-
static int dev_close(struct sr_dev_inst *sdi)
{
struct dev_context *devc;
- std_serial_dev_close(sdi);
-
- sdi->status = SR_ST_INACTIVE;
-
- /* Free dynamically allocated resources. */
- if ((devc = sdi->priv) && devc->elapsed_msec) {
- g_timer_destroy(devc->elapsed_msec);
- devc->elapsed_msec = NULL;
- devc->model = METRAHIT_NONE;
- }
+ devc = sdi->priv;
- return SR_OK;
-}
+ devc->model = METRAHIT_NONE;
-static int cleanup(const struct sr_dev_driver *di)
-{
- return std_dev_clear(di, NULL);
+ return std_serial_dev_close(sdi);
}
-/** Get value of configuration item */
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
- int ret;
struct dev_context *devc;
(void)cg;
- ret = SR_OK;
-
- if (!sdi || !(devc = sdi->priv))
+ if (!sdi)
return SR_ERR_ARG;
- ret = SR_OK;
+ devc = sdi->priv;
+
switch (key) {
case SR_CONF_LIMIT_SAMPLES:
- *data = g_variant_new_uint64(devc->limit_samples);
- break;
case SR_CONF_LIMIT_MSEC:
- *data = g_variant_new_uint64(devc->limit_msec);
- break;
+ return sr_sw_limits_config_get(&devc->limits, key, data);
case SR_CONF_POWER_OFF:
*data = g_variant_new_boolean(FALSE);
break;
@@ -412,84 +348,35 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
return SR_ERR_NA;
}
- return ret;
-}
-
-/** Implementation of config_list, auxiliary function for common parts, */
-static int config_list_common(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
-{
- (void)sdi;
- (void)cg;
-
- switch (key) {
- case SR_CONF_SCAN_OPTIONS:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
- break;
- default:
- return SR_ERR_NA;
- }
-
return SR_OK;
}
/** Implementation of config_list for Metrahit 1x/2x send mode */
-static int config_list_sm(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_list_sm(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
- switch (key) {
- case SR_CONF_DEVICE_OPTIONS:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- devopts_sm, ARRAY_SIZE(devopts_sm), sizeof(uint32_t));
- break;
- default:
- return config_list_common(key, data, sdi, cg);
- }
-
- return SR_OK;
+ return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_sm);
}
/** Implementation of config_list for Metrahit 2x bidirectional mode */
-static int config_list_bd(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
- const struct sr_channel_group *cg)
+static int config_list_bd(uint32_t key, GVariant **data,
+ const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
{
- switch (key) {
- case SR_CONF_DEVICE_OPTIONS:
- *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
- devopts_bd, ARRAY_SIZE(devopts_bd), sizeof(uint32_t));
- break;
- default:
- return config_list_common(key, data, sdi, cg);
- }
-
- return SR_OK;
+ return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts_bd);
}
-static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
- void *cb_data)
+static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
struct sr_serial_dev_inst *serial;
- if (!sdi || !cb_data || !(devc = sdi->priv))
- return SR_ERR_BUG;
-
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- devc->cb_data = cb_data;
+ devc = sdi->priv;
devc->settings_ok = FALSE;
devc->buflen = 0;
- /* Send header packet to the session bus. */
- std_session_send_df_header(cb_data, LOG_PREFIX);
+ sr_sw_limits_acquisition_start(&devc->limits);
+ std_session_send_df_header(sdi);
- /* Start timer, if required. */
- if (devc->limit_msec)
- g_timer_start(devc->elapsed_msec);
-
- /* Poll every 40ms, or whenever some data comes in. */
serial = sdi->conn;
serial_source_add(sdi->session, serial, G_IO_IN, 40,
gmc_mh_1x_2x_receive_data, (void *)sdi);
@@ -497,30 +384,18 @@ static int dev_acquisition_start_1x_2x_rs232(const struct sr_dev_inst *sdi,
return SR_OK;
}
-static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi,
- void *cb_data)
+static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi)
{
struct dev_context *devc;
struct sr_serial_dev_inst *serial;
- if (!sdi || !cb_data || !(devc = sdi->priv))
- return SR_ERR_BUG;
-
- if (sdi->status != SR_ST_ACTIVE)
- return SR_ERR_DEV_CLOSED;
-
- devc->cb_data = cb_data;
+ devc = sdi->priv;
devc->settings_ok = FALSE;
devc->buflen = 0;
- /* Send header packet to the session bus. */
- std_session_send_df_header(cb_data, LOG_PREFIX);
-
- /* Start timer, if required. */
- if (devc->limit_msec)
- g_timer_start(devc->elapsed_msec);
+ sr_sw_limits_acquisition_start(&devc->limits);
+ std_session_send_df_header(sdi);
- /* Poll every 40ms, or whenever some data comes in. */
serial = sdi->conn;
serial_source_add(sdi->session, serial, G_IO_IN, 40,
gmc_mh_2x_receive_data, (void *)sdi);
@@ -529,52 +404,42 @@ static int dev_acquisition_start_2x_bd232(const struct sr_dev_inst *sdi,
return req_meas14(sdi);
}
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
-{
- struct dev_context *devc;
-
- /* Stop timer, if required. */
- if (sdi && (devc = sdi->priv) && devc->limit_msec)
- g_timer_stop(devc->elapsed_msec);
-
- return std_serial_dev_acquisition_stop(sdi, cb_data, dev_close,
- sdi->conn, LOG_PREFIX);
-}
-
-SR_PRIV struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
+static struct sr_dev_driver gmc_mh_1x_2x_rs232_driver_info = {
.name = "gmc-mh-1x-2x-rs232",
.longname = "Gossen Metrawatt Metrahit 1x/2x, RS232 interface",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan_1x_2x_rs232,
- .dev_list = dev_list,
- .dev_clear = NULL,
+ .dev_list = std_dev_list,
+ .dev_clear = std_dev_clear,
.config_get = config_get,
.config_set = config_set,
.config_list = config_list_sm,
.dev_open = std_serial_dev_open,
.dev_close = dev_close,
.dev_acquisition_start = dev_acquisition_start_1x_2x_rs232,
- .dev_acquisition_stop = dev_acquisition_stop,
- .priv = NULL,
+ .dev_acquisition_stop = std_serial_dev_acquisition_stop,
+ .context = NULL,
};
+SR_REGISTER_DEV_DRIVER(gmc_mh_1x_2x_rs232_driver_info);
-SR_PRIV struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
+static struct sr_dev_driver gmc_mh_2x_bd232_driver_info = {
.name = "gmc-mh-2x-bd232",
.longname = "Gossen Metrawatt Metrahit 2x, BD232/SI232-II interface",
.api_version = 1,
- .init = init,
- .cleanup = cleanup,
+ .init = std_init,
+ .cleanup = std_cleanup,
.scan = scan_2x_bd232,
- .dev_list = dev_list,
- .dev_clear = NULL,
+ .dev_list = std_dev_list,
+ .dev_clear = std_dev_clear,
.config_get = config_get,
.config_set = config_set,
.config_list = config_list_bd,
.dev_open = std_serial_dev_open,
.dev_close = dev_close,
.dev_acquisition_start = dev_acquisition_start_2x_bd232,
- .dev_acquisition_stop = dev_acquisition_stop,
- .priv = NULL,
+ .dev_acquisition_stop = std_serial_dev_acquisition_stop,
+ .context = NULL,
};
+SR_REGISTER_DEV_DRIVER(gmc_mh_2x_bd232_driver_info);