]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/motech-lps-30x/api.c
drivers: Provide proper drvopts.
[libsigrok.git] / src / hardware / motech-lps-30x / api.c
index 1b227e7844b143318caa8bf4ed750fe03b5643b6..d5a834f7e972376c093f7426dc1cb0e1f138d322 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/** @file
- *  <em>Motech LPS-30x series</em> power supply driver
- *  @internal
+/**
+ * @file
+ *
+ * <em>Motech LPS-30x series</em> power supply driver
+ *
+ * @internal
  */
 
 #include <config.h>
@@ -28,6 +31,7 @@
 #include <math.h>
 #include <string.h>
 #include "protocol.h"
+
 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args);
 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...);
@@ -39,22 +43,21 @@ 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 uint32_t scanopts[] = {
        SR_CONF_CONN,
        SR_CONF_SERIALCOMM,
 };
 
+/** Driver capabilities generic. */
+static const uint32_t drvopts[] = {
+       /* Device class */
+       SR_CONF_POWER_SUPPLY,
+};
+
 /** Hardware capabilities generic. */
 static const uint32_t devopts[] = {
        /* Device class */
-       SR_CONF_POWER_SUPPLY,
        /* Acquisition modes. */
        SR_CONF_CONTINUOUS,
        SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
@@ -173,7 +176,7 @@ SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
        va_list args;
        char buf[LINELEN_MAX];
        char *bufptr;
-       int  buflen;
+       int buflen;
 
        /* Send command */
        va_start(args, fmt);
@@ -194,8 +197,9 @@ SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
        return SR_ERR;
 }
 
-/** Send command and read reply string.
- *  @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
+/**
+ * Send command and read reply string.
+ * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
  */
 SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...)
 {
@@ -203,7 +207,7 @@ SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const
        va_list args;
        char buf[LINELEN_MAX];
        char *bufptr;
-       int  buflen;
+       int buflen;
 
        reply[0] = '\0';
 
@@ -307,9 +311,11 @@ static gint64 calc_timeout_ms(gint64 start_us)
        return result;
 }
 
-/** Read message into buf until "OK" received.
- *  @retval SR_OK Msg received; buf and buflen contain result, if any except OK.
- *  @retval SR_ERR Error, including timeout.
+/**
+ * Read message into buf until "OK" received.
+ *
+ * @retval SR_OK Msg received; buf and buflen contain result, if any except OK.
+ * @retval SR_ERR Error, including timeout.
 */
 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
 {
@@ -380,8 +386,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
        devc = NULL;
        conn = serialcomm = NULL;
 
-       sr_spew("scan() called!");
-
        /* Process and check options. */
        if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
                return NULL;
@@ -431,7 +435,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
                g_strstrip(buf);
                verstr = buf + 4;
        }
-       else  /* Bug in device FW 1.17: Querying version string fails while output is active.
+       else /* Bug in device FW 1.17: Querying version string fails while output is active.
                Therefore just print an error message, but do not exit with error. */
                sr_err("Failed to query for hardware version: %s.",
                        sr_strerror(ret));
@@ -477,13 +481,11 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
 exit_err:
        sr_info("%s: Error!", __func__);
 
-       if (serial) {
+       if (serial)
                serial_close(serial);
-               sr_serial_dev_inst_free(serial);
-       }
+       sr_serial_dev_inst_free(serial);
        g_free(devc);
-       if (sdi)
-               sr_dev_inst_free(sdi);
+       sr_dev_inst_free(sdi);
 
        return NULL;
 }
@@ -494,7 +496,7 @@ static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
        return do_scan(LPS_301, di, options);
 }
 
-static void dev_clear_private(struct dev_context *devc)
+static void clear_helper(struct dev_context *devc)
 {
        int ch_idx;
 
@@ -503,9 +505,9 @@ static void dev_clear_private(struct dev_context *devc)
                g_slist_free(devc->channel_status[ch_idx].info);
 }
 
-static int dev_clear_lps301(const struct sr_dev_driver *di)
+static int dev_clear(const struct sr_dev_driver *di)
 {
-       return std_dev_clear(di, (std_dev_clear_callback)dev_clear_private);
+       return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
 }
 
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
@@ -572,9 +574,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        int idx;
        gboolean found;
 
-       if (sdi->status != SR_ST_ACTIVE)
-               return SR_ERR_DEV_CLOSED;
-
        devc = sdi->priv;
 
        /* Cannot change settings while acquisition active, would cause a mess with commands.
@@ -678,32 +677,14 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        GVariant *gvar;
        GVariantBuilder gvb;
 
-       /* Driver options, no device instance necessary. */
-       switch (key) {
-       case SR_CONF_SCAN_OPTIONS:
-               *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)
-                       break;
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
-               return SR_OK;
-       default:
-               if (!sdi)
-                       return SR_ERR_ARG;
-               devc = sdi->priv;
-               break;
-       }
+       devc = (sdi) ? sdi->priv : NULL;
 
        /* Device options, independent from channel groups. */
        if (!cg) {
                switch (key) {
+               case SR_CONF_SCAN_OPTIONS:
                case SR_CONF_DEVICE_OPTIONS:
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
-                       return SR_OK;
+                       return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
                case SR_CONF_CHANNEL_CONFIG:
                        if (devc->model->modelid <= LPS_303) {
                                /* The 1-channel models. */
@@ -725,10 +706,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
        case SR_CONF_DEVICE_OPTIONS:
                if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                 devopts_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t));
+                               devopts_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t));
                else /* Must be CH3 */
                        *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
-                                 devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t));
+                               devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t));
                break;
        case SR_CONF_VOLTAGE_TARGET:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
@@ -760,9 +741,6 @@ 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;
-
        devc = sdi->priv;
 
        devc->acq_running = TRUE;
@@ -780,11 +758,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi)
-{
-       return std_serial_dev_acquisition_stop(sdi);
-}
-
 static struct sr_dev_driver motech_lps_301_driver_info = {
        .name = "motech-lps-301",
        .longname = "Motech LPS-301",
@@ -793,14 +766,14 @@ static struct sr_dev_driver motech_lps_301_driver_info = {
        .cleanup = std_cleanup,
        .scan = scan_lps301,
        .dev_list = std_dev_list,
-       .dev_clear = dev_clear_lps301,
+       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
        .dev_open = std_serial_dev_open,
        .dev_close = std_serial_dev_close,
        .dev_acquisition_start = dev_acquisition_start,
-       .dev_acquisition_stop = dev_acquisition_stop,
+       .dev_acquisition_stop = std_serial_dev_acquisition_stop,
        .context = NULL,
 };
 SR_REGISTER_DEV_DRIVER(motech_lps_301_driver_info);