X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fgmc-mh-1x-2x%2Fapi.c;h=bf0b3691fc5f5831be233ad0f9dffdc783e0ccdc;hb=1a8639164e4e44a43fe1558e30823606f7b607b3;hp=96621bbfd6636f8f52a76b36dcdca496f19e99bc;hpb=155b680da482cea2381becb73c51cfb838bff31e;p=libsigrok.git diff --git a/src/hardware/gmc-mh-1x-2x/api.c b/src/hardware/gmc-mh-1x-2x/api.c index 96621bbf..bf0b3691 100644 --- a/src/hardware/gmc-mh-1x-2x/api.c +++ b/src/hardware/gmc-mh-1x-2x/api.c @@ -34,28 +34,28 @@ 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 int32_t hwopts[] = { +static const uint32_t scanopts[] = { SR_CONF_CONN, SR_CONF_SERIALCOMM, }; /** Hardware capabilities for Metrahit 1x/2x devices in send mode. */ -static const int32_t hwcaps_sm[] = { +static const uint32_t devopts_sm[] = { SR_CONF_MULTIMETER, SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ - SR_CONF_LIMIT_SAMPLES, - SR_CONF_LIMIT_MSEC, SR_CONF_CONTINUOUS, + SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, + SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, }; /** Hardware capabilities for Metrahit 2x devices in bidirectional Mode. */ -static const int32_t hwcaps_bd[] = { +static const uint32_t devopts_bd[] = { SR_CONF_MULTIMETER, SR_CONF_THERMOMETER, /**< All GMC 1x/2x multimeters seem to support this */ - SR_CONF_LIMIT_SAMPLES, - SR_CONF_LIMIT_MSEC, SR_CONF_CONTINUOUS, - SR_CONF_POWER_OFF, + 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, }; @@ -66,16 +66,9 @@ static const int32_t hwcaps_bd[] = { * Need to implement device-specific lists. */ -/** Init driver gmc_mh_1x_2x_rs232. */ -static int init_1x_2x_rs232(struct sr_context *sr_ctx) +static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) { - return std_init(sr_ctx, &gmc_mh_1x_2x_rs232_driver_info, LOG_PREFIX); -} - -/** Init driver gmc_mh_2x_bd232. */ -static int init_2x_bd232(struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, &gmc_mh_2x_bd232_driver_info, LOG_PREFIX); + return std_init(sr_ctx, di, LOG_PREFIX); } /** @@ -90,7 +83,7 @@ static int read_byte(struct sr_serial_dev_inst *serial, gint64 timeout) int rc = 0; for (;;) { - rc = serial_read(serial, &result, 1); + rc = serial_read_nonblocking(serial, &result, 1); if (rc == 1) { sr_spew("read: 0x%02x/%d", result, result); return result; @@ -156,13 +149,12 @@ static enum model scan_model_sm(struct sr_serial_dev_inst *serial) * on configuration and measurement mode the intervals can be much larger and * then the detection might not work. */ -static GSList *scan_1x_2x_rs232(GSList *options) +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_channel *ch; struct sr_serial_dev_inst *serial; GSList *l, *devices; const char *conn, *serialcomm; @@ -170,7 +162,7 @@ static GSList *scan_1x_2x_rs232(GSList *options) gboolean serialcomm_given; devices = NULL; - drvc = (&gmc_mh_1x_2x_rs232_driver_info)->priv; + drvc = di->priv; drvc->instances = NULL; conn = serialcomm = NULL; model = METRAHIT_NONE; @@ -195,10 +187,9 @@ static GSList *scan_1x_2x_rs232(GSList *options) if (!serialcomm) serialcomm = SERIALCOMM_2X_RS232; - if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) - return NULL; + serial = sr_serial_dev_inst_new(conn, serialcomm); - if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) { + if (serial_open(serial, SERIAL_RDWR) != SR_OK) { sr_serial_dev_inst_free(serial); return NULL; } @@ -223,26 +214,21 @@ static GSList *scan_1x_2x_rs232(GSList *options) if (model != METRAHIT_NONE) { sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(model)); - if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, - gmc_model_str(model), NULL))) - return NULL; - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { - sr_err("Device context malloc failed."); - return NULL; - } + sdi = g_malloc0(sizeof(struct sr_dev_inst)); + sdi->status = SR_ST_INACTIVE; + sdi->vendor = g_strdup(VENDOR_GMC); + sdi->model = g_strdup(gmc_model_str(model)); + devc = g_malloc0(sizeof(struct dev_context)); 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 = &gmc_mh_1x_2x_rs232_driver_info; - if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) - return NULL; - sdi->channels = g_slist_append(sdi->channels, ch); + 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); } @@ -250,16 +236,16 @@ static GSList *scan_1x_2x_rs232(GSList *options) return 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(GSList *options) +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_channel *ch; struct sr_serial_dev_inst *serial; GSList *l, *devices; const char *conn, *serialcomm; @@ -271,7 +257,7 @@ static GSList *scan_2x_bd232(GSList *options) conn = serialcomm = NULL; devices = NULL; - drvc = (&gmc_mh_2x_bd232_driver_info)->priv; + drvc = di->priv; drvc->instances = NULL; sr_spew("scan_2x_bd232() called!"); @@ -292,20 +278,16 @@ static GSList *scan_2x_bd232(GSList *options) if (!serialcomm) serialcomm = SERIALCOMM_2X; - if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) - return NULL; - - if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK) - goto exit_err; + serial = sr_serial_dev_inst_new(conn, serialcomm); - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { - sr_err("Device context malloc failed."); + if (serial_open(serial, SERIAL_RDWR) != SR_OK) goto exit_err; - } - if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, NULL, NULL))) - goto exit_err; + 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->priv = devc; /* Send message 03 "Query multimeter version and status" */ @@ -335,27 +317,19 @@ static GSList *scan_2x_bd232(GSList *options) if (devc->model != METRAHIT_NONE) { sr_spew("%s %s detected!", VENDOR_GMC, gmc_model_str(devc->model)); - devc->elapsed_msec = g_timer_new(); - 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 = &gmc_mh_2x_bd232_driver_info; - if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1"))) - goto exit_err; - sdi->channels = g_slist_append(sdi->channels, ch); + 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); - - if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { - sr_err("Device context malloc failed."); - goto exit_err; - } - - if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_GMC, NULL, NULL))) - goto exit_err; + 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); } }; @@ -380,17 +354,9 @@ exit_err: return NULL; } -/** Driver device list function */ -static GSList *dev_list_1x_2x_rs232(void) +static GSList *dev_list(const struct sr_dev_driver *di) { - return ((struct drv_context *)(gmc_mh_1x_2x_rs232_driver_info.priv))->instances; -} - -/** Driver device list function */ -static GSList *dev_list_2x_bd232(void) -{ - return ((struct drv_context *)(gmc_mh_2x_bd232_driver_info.priv)) - ->instances; + return ((struct drv_context *)(di->priv))->instances; } static int dev_close(struct sr_dev_inst *sdi) @@ -411,19 +377,13 @@ static int dev_close(struct sr_dev_inst *sdi) return SR_OK; } -static int cleanup_sm_rs232(void) -{ - return std_dev_clear(&gmc_mh_1x_2x_rs232_driver_info, NULL); -} - -static int cleanup_2x_bd232(void) +static int cleanup(const struct sr_dev_driver *di) { - return std_dev_clear(&gmc_mh_2x_bd232_driver_info, NULL); + return std_dev_clear(di, NULL); } -/** Get value of configuration item */ -static int config_get(int 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; @@ -453,17 +413,17 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, return ret; } -/** Implementation of config_list, auxiliary function for common parts, */ -static int config_list_common(int key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +/** 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_INT32, - hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); + *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, + scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); break; default: return SR_ERR_NA; @@ -473,13 +433,13 @@ static int config_list_common(int key, GVariant **data, const struct sr_dev_inst } /** Implementation of config_list for Metrahit 1x/2x send mode */ -static int config_list_sm(int key, GVariant **data, const struct sr_dev_inst *sdi, +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_INT32, - hwcaps_sm, ARRAY_SIZE(hwcaps_sm), sizeof(int32_t)); + *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); @@ -489,13 +449,13 @@ static int config_list_sm(int key, GVariant **data, const struct sr_dev_inst *sd } /** Implementation of config_list for Metrahit 2x bidirectional mode */ -static int config_list_bd(int key, GVariant **data, const struct sr_dev_inst *sdi, +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_INT32, - hwcaps_bd, ARRAY_SIZE(hwcaps_bd), sizeof(int32_t)); + *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); @@ -583,10 +543,10 @@ SR_PRIV 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_1x_2x_rs232, - .cleanup = cleanup_sm_rs232, + .init = init, + .cleanup = cleanup, .scan = scan_1x_2x_rs232, - .dev_list = dev_list_1x_2x_rs232, + .dev_list = dev_list, .dev_clear = NULL, .config_get = config_get, .config_set = config_set, @@ -602,10 +562,10 @@ SR_PRIV 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_2x_bd232, - .cleanup = cleanup_2x_bd232, + .init = init, + .cleanup = cleanup, .scan = scan_2x_bd232, - .dev_list = dev_list_2x_bd232, + .dev_list = dev_list, .dev_clear = NULL, .config_get = config_get, .config_set = config_set,