]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/norma-dmm/api.c
Put driver pointers into special section
[libsigrok.git] / src / hardware / norma-dmm / api.c
index 6fe8e39d8b37ae90c7d23c941fe16e0f4ff32f86..ee1c59ca30eb341cf2872204e92921e82c1795ce 100644 (file)
@@ -22,6 +22,7 @@
  *  @internal
  */
 
+#include <config.h>
 #include "protocol.h"
 
 static const uint32_t scanopts[] = {
@@ -40,38 +41,31 @@ static const uint32_t devopts[] = {
 
 #define SERIALCOMM "4800/8n1/dtr=1/rts=0/flow=1"
 
-SR_PRIV struct sr_dev_driver norma_dmm_driver_info;
-SR_PRIV struct sr_dev_driver siemens_b102x_driver_info;
+static struct sr_dev_driver norma_dmm_driver_info;
+static struct sr_dev_driver siemens_b102x_driver_info;
 
-static const char* get_brandstr(struct sr_dev_driver* drv)
+static const char *get_brandstr(struct sr_dev_driver *drv)
 {
-       if (drv == &norma_dmm_driver_info)
-               return "Norma";
-       else
-               return "Siemens";
+       return (drv == &norma_dmm_driver_info) ? "Norma" : "Siemens";
 }
 
-static const char* get_typestr(int type, struct sr_dev_driver* drv)
+static const char *get_typestr(int type, struct sr_dev_driver *drv)
 {
-       static const charnameref[5][2] = {
+       static const char *nameref[5][2] = {
                {"DM910", "B1024"},
                {"DM920", "B1025"},
                {"DM930", "B1026"},
                {"DM940", "B1027"},
-               {"DM950", "B1028"}};
+               {"DM950", "B1028"},
+       };
 
        if ((type < 1) || (type > 5))
                return "Unknown type!";
 
-       return nameref[type-1][(drv == &siemens_b102x_driver_info)];
+       return nameref[type - 1][(drv == &siemens_b102x_driver_info)];
 }
 
-static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static GSList *scan(struct sr_dev_driver* drv, GSList *options)
+static GSList *scan(struct sr_dev_driver *drv, GSList *options)
 {
        struct sr_dev_inst *sdi;
        struct drv_context *drvc;
@@ -79,14 +73,13 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
        struct sr_config *src;
        struct sr_serial_dev_inst *serial;
        GSList *l, *devices;
-       int len, cnt;
+       int len, cnt, auxtype;
        const char *conn, *serialcomm;
        char *buf;
        char req[10];
-       int auxtype;
 
        devices = NULL;
-       drvc = drv->priv;
+       drvc = drv->context;
        drvc->instances = NULL;
        conn = serialcomm = NULL;
 
@@ -113,16 +106,14 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
 
        serial_flush(serial);
 
-       if (!(buf = g_try_malloc(BUF_MAX))) {
-               sr_err("Serial buffer malloc failed.");
-               return NULL;
-       }
+       buf = g_malloc(BUF_MAX);
 
        snprintf(req, sizeof(req), "%s\r\n",
                 nmadmm_requests[NMADMM_REQ_IDN].req_str);
        g_usleep(150 * 1000); /* Wait a little to allow serial port to settle. */
        for (cnt = 0; cnt < 7; cnt++) {
-               if (serial_write_blocking(serial, req, strlen(req), 0) < 0) {
+               if (serial_write_blocking(serial, req, strlen(req),
+                               serial_timeout(serial, strlen(req))) < 0) {
                        sr_err("Unable to send identification request.");
                        return NULL;
                }
@@ -132,7 +123,7 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
                        continue;
                buf[BUF_MAX - 1] = '\0';
 
-               /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950) */
+               /* Match ID string, e.g. "1834 065 V1.06,IF V1.02" (DM950). */
                if (g_regex_match_simple("^1834 [^,]*,IF V*", (char *)buf, 0, 0)) {
                        auxtype = xgittoint(buf[7]);
                        sr_spew("%s %s DMM %s detected!", get_brandstr(drv), get_typestr(auxtype, drv), buf + 9);
@@ -143,9 +134,9 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
                        sdi->model = g_strdup(get_typestr(auxtype, drv));
                        sdi->version = g_strdup(buf + 9);
                        devc = g_malloc0(sizeof(struct dev_context));
+                       sr_sw_limits_init(&devc->limits);
                        devc->type = auxtype;
                        devc->version = g_strdup(&buf[9]);
-                       devc->elapsed_msec = g_timer_new();
                        sdi->conn = serial;
                        sdi->priv = devc;
                        sdi->driver = drv;
@@ -176,11 +167,6 @@ static GSList *scan(struct sr_dev_driver* drv, GSList *options)
        return devices;
 }
 
-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;
@@ -191,17 +177,11 @@ static int dev_close(struct sr_dev_inst *sdi)
        if ((devc = sdi->priv) && devc->version) {
                g_free(devc->version);
                devc->version = NULL;
-               g_timer_destroy(devc->elapsed_msec);
        }
 
        return SR_OK;
 }
 
-static int cleanup(const struct sr_dev_driver *di)
-{
-       return std_dev_clear(di, NULL);
-}
-
 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
@@ -212,31 +192,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        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;
-       }
+       devc = sdi->priv;
 
-       switch (key) {
-       case SR_CONF_LIMIT_MSEC:
-               if (g_variant_get_uint64(data) == 0) {
-                       sr_err("LIMIT_MSEC can't be 0.");
-                       return SR_ERR;
-               }
-               devc->limit_msec = g_variant_get_uint64(data);
-               sr_dbg("Setting time limit to %" PRIu64 "ms.",
-                      devc->limit_msec);
-               break;
-       case SR_CONF_LIMIT_SAMPLES:
-               devc->limit_samples = g_variant_get_uint64(data);
-               sr_dbg("Setting sample limit to %" PRIu64 ".",
-                      devc->limit_samples);
-               break;
-       default:
-               return SR_ERR_NA;
-       }
-
-       return SR_OK;
+       return sr_sw_limits_config_set(&devc->limits, key, data);
 }
 
 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
@@ -261,26 +219,18 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        return SR_OK;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+static int dev_acquisition_start(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;
-
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       devc = sdi->priv;
 
-       /* 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, LOG_PREFIX);
 
        /* Poll every 100ms, or whenever some data comes in. */
        serial = sdi->conn;
@@ -290,26 +240,20 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       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,
+       return std_serial_dev_acquisition_stop(sdi, dev_close,
                        sdi->conn, LOG_PREFIX);
 }
 
-SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
+static struct sr_dev_driver norma_dmm_driver_info = {
        .name = "norma-dmm",
        .longname = "Norma DM9x0 DMMs",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
+       .dev_list = std_dev_list,
        .dev_clear = NULL,
        .config_get = NULL,
        .config_set = config_set,
@@ -318,18 +262,18 @@ SR_PRIV struct sr_dev_driver norma_dmm_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(norma_dmm_driver_info);
 
-
-SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
+static struct sr_dev_driver siemens_b102x_driver_info = {
        .name = "siemens-b102x",
        .longname = "Siemens B102x DMMs",
        .api_version = 1,
-       .init = init,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
+       .dev_list = std_dev_list,
        .dev_clear = NULL,
        .config_get = NULL,
        .config_set = config_set,
@@ -338,5 +282,6 @@ SR_PRIV struct sr_dev_driver siemens_b102x_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(siemens_b102x_driver_info);