]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/motech-lps-30x/api.c
Constify a few arrays and variables.
[libsigrok.git] / src / hardware / motech-lps-30x / api.c
index 164a9cc02321d88656018a93e0879cf765f6dc17..0ed6b09b458f56a00a396afaac19e13863a9c077 100644 (file)
@@ -43,37 +43,43 @@ SR_PRIV int lps_query_status(struct sr_dev_inst* sdi);
 
 #define VENDOR_MOTECH "Motech"
 
+/** Driver capabilities generic. */
+static const uint32_t drvopts[] = {
+       /* Device class */
+       SR_CONF_POWER_SUPPLY,
+};
+
 /** Driver scanning options. */
-static const int32_t hwopts[] = {
+static const uint32_t scanopts[] = {
        SR_CONF_CONN,
        SR_CONF_SERIALCOMM,
 };
 
 /** Hardware capabilities generic. */
-static const int32_t hwcaps[] = {
+static const uint32_t devopts[] = {
        /* Device class */
        SR_CONF_POWER_SUPPLY,
        /* Aquisition modes. */
-       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,
        /* Device configuration */
-       SR_CONF_OUTPUT_CHANNEL,
+       SR_CONF_OUTPUT_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
 };
 
 /** Hardware capabilities channel 1, 2. */
-static const int32_t hwcaps_ch12[] = {
-       SR_CONF_OUTPUT_VOLTAGE,
-       SR_CONF_OUTPUT_VOLTAGE_MAX,
-       SR_CONF_OUTPUT_CURRENT,
-       SR_CONF_OUTPUT_CURRENT_MAX,
-       SR_CONF_OUTPUT_ENABLED,
+static const uint32_t devopts_ch12[] = {
+       SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
+       SR_CONF_OUTPUT_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_OUTPUT_CURRENT | SR_CONF_GET,
+       SR_CONF_OUTPUT_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
+       SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
 };
 
 /** Hardware capabilities channel 3. (LPS-304/305 only). */
-static const int32_t hwcaps_ch3[] = {
-       SR_CONF_OUTPUT_VOLTAGE,
-       SR_CONF_OUTPUT_ENABLED,
+static const uint32_t devopts_ch3[] = {
+       SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
+       SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
 };
 
 static const char *channel_modes[] = {
@@ -82,7 +88,7 @@ static const char *channel_modes[] = {
        "Track2",
 };
 
-static struct lps_modelspec models[] = {
+static const struct lps_modelspec models[] = {
        { LPS_UNKNOWN, "Dummy", 0,
                {
 
@@ -128,9 +134,9 @@ static struct lps_modelspec models[] = {
        },
 };
 
-static int init_lps301(struct sr_context *sr_ctx)
+static int init_lps301(struct sr_dev_driver *di, struct sr_context *sr_ctx)
 {
-       return std_init(sr_ctx, &motech_lps_301_driver_info, LOG_PREFIX);
+       return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
 /** Send command to device with va_list.
@@ -146,7 +152,7 @@ SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_l
 
        sr_spew("lps_send_va: \"%s\"", buf);
 
-       retc = serial_write_nonblocking(serial, buf, strlen(buf));
+       retc = serial_write_blocking(serial, buf, strlen(buf), 0);
 
        if (retc < 0)
                return SR_ERR;
@@ -375,9 +381,9 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
        GSList *devices;
        const char *conn, *serialcomm;
        int cnt;
-       gchar  buf[LINELEN_MAX];
+       gchar buf[LINELEN_MAX];
        gchar channel[10];
-       char*  verstr;
+       char *verstr;
 
        sdi = NULL;
        devc = NULL;
@@ -396,10 +402,9 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
                serialcomm = SERIALCOMM;
 
        /* Init serial port. */
-       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)
                goto exit_err;
 
        /* Query and verify model string. */
@@ -436,7 +441,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
                        goto exit_err;
                }
 
-
                g_strstrip(buf);
                verstr = buf + 4;
        }
@@ -444,7 +448,11 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
                Therefore just print an error message, but do not exit with error. */
                sr_err("Failed to query for hardware version: %d %s", errno, strerror(errno));
 
-       sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_MOTECH, models[modelid].modelstr, verstr);
+       sdi = g_malloc0(sizeof(struct sr_dev_inst));
+       sdi->status = SR_ST_INACTIVE;
+       sdi->vendor = g_strdup(VENDOR_MOTECH);
+       sdi->model = g_strdup(models[modelid].modelstr);
+       sdi->version = g_strdup(verstr);
        sdi->driver = drv;
        sdi->inst_type = SR_INST_SERIAL;
        sdi->conn = serial;
@@ -461,8 +469,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
        /* Setup channels and channel groups. */
        for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
                snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
-               ch = sr_channel_new(cnt, SR_CHANNEL_ANALOG, TRUE, channel);
-               sdi->channels = g_slist_append(sdi->channels, ch);
+               ch = sr_channel_new(sdi, cnt, SR_CHANNEL_ANALOG, TRUE, channel);
 
                devc->channel_status[cnt].info = g_slist_append(NULL, ch);
 
@@ -504,19 +511,14 @@ exit_err:
 }
 
 /** Scan for LPS-301 device. */
-static GSList *scan_lps301(GSList *options)
+static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
 {
-       return do_scan(LPS_301, &motech_lps_301_driver_info, options);
+       return do_scan(LPS_301, di, options);
 }
 
-static GSList *doDevList(struct sr_dev_driver *drv)
+static GSList *dev_list_lps301(const struct sr_dev_driver *di)
 {
-       return ((struct drv_context *)(drv->priv))->instances;
-}
-
-static GSList *dev_list_lps301(void)
-{
-       return doDevList(&motech_lps_301_driver_info);
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
 static void dev_clear_private(struct dev_context* devc)
@@ -530,17 +532,17 @@ static void dev_clear_private(struct dev_context* devc)
        g_timer_destroy(devc->elapsed_msec);
 }
 
-static int dev_clear_lps301(void)
+static int dev_clear_lps301(const struct sr_dev_driver *di)
 {
-       return std_dev_clear(&motech_lps_301_driver_info, (std_dev_clear_callback)dev_clear_private);
+       return std_dev_clear(di, (std_dev_clear_callback)dev_clear_private);
 }
 
-static int cleanup(void)
+static int cleanup(const struct sr_dev_driver *di)
 {
-       return dev_clear_lps301();
+       return dev_clear_lps301(di);
 }
 
-static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
@@ -561,7 +563,7 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
                case SR_CONF_LIMIT_MSEC:
                        *data = g_variant_new_uint64(devc->limit_msec);
                        break;
-               case SR_CONF_OUTPUT_CHANNEL:
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
                        *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
                        break;
                default:
@@ -575,13 +577,13 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
                case SR_CONF_OUTPUT_VOLTAGE:
                        *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
                        break;
-               case SR_CONF_OUTPUT_VOLTAGE_MAX:
+               case SR_CONF_OUTPUT_VOLTAGE_TARGET:
                        *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
                        break;
                case SR_CONF_OUTPUT_CURRENT:
                        *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
                        break;
-               case SR_CONF_OUTPUT_CURRENT_MAX:
+               case SR_CONF_OUTPUT_CURRENT_LIMIT:
                        *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
                        break;
                case SR_CONF_OUTPUT_ENABLED:
@@ -595,7 +597,7 @@ static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
@@ -634,7 +636,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                        sr_dbg("Setting sample limit to %" PRIu64 ".",
                                devc->limit_samples);
                        break;
-               case SR_CONF_OUTPUT_CHANNEL:
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
                        sval = g_variant_get_string(data, NULL);
                        found = FALSE;
                        for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++)
@@ -664,7 +666,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                ch_idx = ch->index;
 
                switch (key) {
-               case SR_CONF_OUTPUT_VOLTAGE_MAX:
+               case SR_CONF_OUTPUT_VOLTAGE_TARGET:
                        dval = g_variant_get_double(data);
                        if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
                                return SR_ERR_ARG;
@@ -685,7 +687,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                        else
                                return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
                        break;
-               case SR_CONF_OUTPUT_CURRENT_MAX:
+               case SR_CONF_OUTPUT_CURRENT_LIMIT:
                        dval = g_variant_get_double(data);
                        if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
                                return SR_ERR_ARG;
@@ -717,7 +719,7 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
@@ -731,8 +733,14 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        /* Driver options, no device instance necessary. */
        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));
+               return SR_OK;
+       case SR_CONF_DEVICE_OPTIONS:
+               if (sdi != NULL)
+                       break;
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                               drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
                return SR_OK;
        default:
                if (sdi == NULL)
@@ -741,14 +749,14 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                devc = sdi->priv;
        }
 
-       /* Device options, independant from channel groups. */
+       /* Device options, independent from channel groups. */
        if (cg == NULL) {
                switch (key) {
                case SR_CONF_DEVICE_OPTIONS:
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                                         hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                        return SR_OK;
-               case SR_CONF_OUTPUT_CHANNEL:
+               case SR_CONF_OUTPUT_CHANNEL_CONFIG:
                        if (devc->model->modelid <= LPS_303) {
                                /* The 1-channel models. */
                                *data = g_variant_new_strv(channel_modes, 1);
@@ -769,13 +777,13 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        switch (key) {
        case SR_CONF_DEVICE_OPTIONS:
                if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                 hwcaps_ch12, ARRAY_SIZE(hwcaps_ch12), sizeof(int32_t));
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                 devopts_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t));
                else /* Must be CH3 */
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                                 hwcaps_ch3, ARRAY_SIZE(hwcaps_ch3), sizeof(int32_t));
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                 devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t));
                break;
-       case SR_CONF_OUTPUT_VOLTAGE_MAX:
+       case SR_CONF_OUTPUT_VOLTAGE_TARGET:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
                /* Min, max, step. */
                for (i = 0; i < 3; i++) {
@@ -784,7 +792,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                }
                *data = g_variant_builder_end(&gvb);
                break;
-       case SR_CONF_OUTPUT_CURRENT_MAX:
+       case SR_CONF_OUTPUT_CURRENT_LIMIT:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
                /* Min, max, step. */
                for (i = 0; i < 3; i++) {