]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/agilent-dmm/api.c
Put driver pointers into special section
[libsigrok.git] / src / hardware / agilent-dmm / api.c
index 9dcbd230761042590eba8c117fda520693a4a416..514a2d276881b8444f8099b2edbbe76285cf7e05 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <glib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
-#include <errno.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "agilent-dmm.h"
 
@@ -32,8 +32,11 @@ static const uint32_t scanopts[] = {
        SR_CONF_SERIALCOMM,
 };
 
-static const uint32_t devopts[] = {
+static const uint32_t drvopts[] = {
        SR_CONF_MULTIMETER,
+};
+
+static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
        SR_CONF_LIMIT_MSEC | SR_CONF_SET,
@@ -67,28 +70,19 @@ static const struct agdmm_profile supported_agdmm[] = {
        ALL_ZERO
 };
 
-SR_PRIV struct sr_dev_driver agdmm_driver_info;
-static struct sr_dev_driver *di = &agdmm_driver_info;
-
-static int init(struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
-static GSList *scan(GSList *options)
+static GSList *scan(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;
        int len, i;
        const char *conn, *serialcomm;
        char *buf, **tokens;
 
-       drvc = di->priv;
+       drvc = di->context;
        drvc->instances = NULL;
 
        devices = NULL;
@@ -109,8 +103,7 @@ static GSList *scan(GSList *options)
        if (!serialcomm)
                serialcomm = SERIALCOMM;
 
-       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) != SR_OK)
                return NULL;
@@ -122,10 +115,7 @@ static GSList *scan(GSList *options)
        }
 
        len = 128;
-       if (!(buf = g_try_malloc(len))) {
-               sr_err("Serial buffer malloc failed.");
-               return NULL;
-       }
+       buf = g_malloc(len);
        serial_readline(serial, &buf, &len, 250);
        if (!len)
                return NULL;
@@ -136,24 +126,19 @@ static GSList *scan(GSList *options)
                for (i = 0; supported_agdmm[i].model; i++) {
                        if (strcmp(supported_agdmm[i].modelname, tokens[1]))
                                continue;
-                       sdi = sr_dev_inst_new();
+                       sdi = g_malloc0(sizeof(struct sr_dev_inst));
                        sdi->status = SR_ST_INACTIVE;
                        sdi->vendor = g_strdup("Agilent");
                        sdi->model = g_strdup(tokens[1]);
                        sdi->version = g_strdup(tokens[3]);
-                       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
-                               sr_err("Device context malloc failed.");
-                               return NULL;
-                       }
+                       devc = g_malloc0(sizeof(struct dev_context));
                        devc->profile = &supported_agdmm[i];
                        devc->cur_mq = -1;
                        sdi->inst_type = SR_INST_SERIAL;
                        sdi->conn = serial;
                        sdi->priv = devc;
                        sdi->driver = di;
-                       if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
-                               return NULL;
-                       sdi->channels = g_slist_append(sdi->channels, ch);
+                       sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
                        drvc->instances = g_slist_append(drvc->instances, sdi);
                        devices = g_slist_append(devices, sdi);
                        break;
@@ -169,16 +154,6 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
-{
-       return ((struct drv_context *)(di->priv))->instances;
-}
-
-static int cleanup(void)
-{
-       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)
 {
@@ -189,26 +164,15 @@ 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:
                /* TODO: not yet implemented */
-               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;
@@ -220,7 +184,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
-       (void)sdi;
        (void)cg;
 
        switch (key) {
@@ -229,8 +192,12 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                                scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                               devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
+               if (!sdi)
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
+               else
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        default:
                return SR_ERR_NA;
@@ -239,23 +206,14 @@ 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->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
-
-       devc->cb_data = cb_data;
-
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
        /* Poll every 100ms, or whenever some data comes in. */
        serial = sdi->conn;
@@ -265,20 +223,20 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        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)
 {
-       return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
+       return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
                        sdi->conn, LOG_PREFIX);
 }
 
-SR_PRIV struct sr_dev_driver agdmm_driver_info = {
+static struct sr_dev_driver agdmm_driver_info = {
        .name = "agilent-dmm",
        .longname = "Agilent U12xx series 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,
@@ -287,5 +245,6 @@ SR_PRIV struct sr_dev_driver agdmm_driver_info = {
        .dev_close = std_serial_dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };
+SR_REGISTER_DEV_DRIVER(agdmm_driver_info);