]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/motech-lps-30x/api.c
config_list: Don't check for sdi->priv != NULL.
[libsigrok.git] / src / hardware / motech-lps-30x / api.c
index ba647837caf58079f32266b8e9d14d833df01997..57befbfd6edf6e9c06e5b2c8bead3e2266a52c84 100644 (file)
@@ -23,6 +23,7 @@
  *  @internal
  */
 
+#include <config.h>
 #include <ctype.h>
 #include <math.h>
 #include <string.h>
 /* Forward declarations */
 SR_PRIV struct sr_dev_driver motech_lps_301_driver_info;
 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 charfmt, va_list args);
-SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const charfmt, ...);
-SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...);
-SR_PRIV int lps_query_status(struct sr_dev_instsdi);
+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, ...);
+SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...);
+SR_PRIV int lps_query_status(struct sr_dev_inst *sdi);
 
 /* Serial communication parameters */
 #define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
@@ -132,14 +133,9 @@ static const struct lps_modelspec models[] = {
        },
 };
 
-static int init_lps301(struct sr_dev_driver *di, struct sr_context *sr_ctx)
-{
-       return std_init(sr_ctx, di, LOG_PREFIX);
-}
-
 /** Send command to device with va_list.
  */
-SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const charfmt, va_list args)
+SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args)
 {
        int retc;
        char auxfmt[LINELEN_MAX];
@@ -150,7 +146,8 @@ 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_blocking(serial, buf, strlen(buf), 0);
+       retc = serial_write_blocking(serial, buf, strlen(buf),
+                       serial_timeout(serial, strlen(buf)));
 
        if (retc < 0)
                return SR_ERR;
@@ -160,7 +157,7 @@ SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_l
 
 /** Send command to device.
  */
-SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const charfmt, ...)
+SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char *fmt, ...)
 {
        int retc;
        va_list args;
@@ -173,12 +170,12 @@ SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char* fmt, ...
 }
 
 /** Send command and consume simple OK reply. */
-SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const charfmt, ...)
+SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
 {
        int retc;
        va_list args;
        char buf[LINELEN_MAX];
-       charbufptr;
+       char *bufptr;
        int  buflen;
 
        /* Send command */
@@ -201,14 +198,14 @@ SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...)
 }
 
 /** Send command and read reply string.
- *  \param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
+ *  @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, ...)
+SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...)
 {
        int retc;
        va_list args;
        char buf[LINELEN_MAX];
-       charbufptr;
+       char *bufptr;
        int  buflen;
 
        reply[0] = '\0';
@@ -235,12 +232,12 @@ SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const
 }
 
 /** Process integer value returned by STATUS command. */
-SR_PRIV int lps_process_status(struct sr_dev_instsdi, int stat)
+SR_PRIV int lps_process_status(struct sr_dev_inst *sdi, int stat)
 {
-       struct dev_contextdevc;
+       struct dev_context *devc;
        int tracking_mode;
 
-       devc = (struct dev_context*)sdi->priv;
+       devc = sdi->priv;
 
        sr_spew("Status: %d", stat);
        devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
@@ -281,13 +278,13 @@ SR_PRIV int lps_process_status(struct sr_dev_inst* sdi, int stat)
 }
 
 /** Send STATUS commend and process status string. */
-SR_PRIV int lps_query_status(struct sr_dev_instsdi)
+SR_PRIV int lps_query_status(struct sr_dev_inst *sdi)
 {
        char buf[LINELEN_MAX];
        int stat, ret;
-       struct dev_contextdevc;
+       struct dev_context *devc;
 
-       devc = (struct dev_context*)sdi->priv;
+       devc = sdi->priv;
 
        devc->req_sent_at = g_get_real_time();
 
@@ -314,8 +311,8 @@ static gint64 calc_timeout_ms(gint64 start_us)
 }
 
 /** 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.
+ *  @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)
 {
@@ -458,11 +455,8 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o
        sdi->conn = serial;
 
        devc = g_malloc0(sizeof(struct dev_context));
+       sr_sw_limits_init(&devc->limits);
        devc->model = &models[modelid];
-       devc->limit_samples = 0;
-       devc->limit_msec = 0;
-       devc->num_samples = 0;
-       devc->elapsed_msec = g_timer_new();
 
        sdi->priv = devc;
 
@@ -515,20 +509,13 @@ static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
        return do_scan(LPS_301, di, options);
 }
 
-static GSList *dev_list_lps301(const struct sr_dev_driver *di)
-{
-       return ((struct drv_context *)(di->context))->instances;
-}
-
-static void dev_clear_private(struct dev_context* devc)
+static void dev_clear_private(struct dev_context *devc)
 {
        int ch_idx;
 
        /* Free channel_status.info (list only, data owned by sdi). */
        for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
                g_slist_free(devc->channel_status[ch_idx].info);
-
-       g_timer_destroy(devc->elapsed_msec);
 }
 
 static int dev_clear_lps301(const struct sr_dev_driver *di)
@@ -536,11 +523,6 @@ static int dev_clear_lps301(const struct sr_dev_driver *di)
        return std_dev_clear(di, (std_dev_clear_callback)dev_clear_private);
 }
 
-static int cleanup(const struct sr_dev_driver *di)
-{
-       return dev_clear_lps301(di);
-}
-
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
@@ -557,11 +539,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
                /* No channel group: global options. */
                switch (key) {
                case SR_CONF_LIMIT_SAMPLES:
-                       *data = g_variant_new_uint64(devc->limit_samples);
-                       break;
                case SR_CONF_LIMIT_MSEC:
-                       *data = g_variant_new_uint64(devc->limit_msec);
-                       break;
+                       return sr_sw_limits_config_get(&devc->limits, key, data);
                case SR_CONF_CHANNEL_CONFIG:
                        *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
                        break;
@@ -622,11 +601,8 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                /* No channel group: global options. */
                switch (key) {
                case SR_CONF_LIMIT_MSEC:
-                       devc->limit_msec = g_variant_get_uint64(data);
-                       break;
                case SR_CONF_LIMIT_SAMPLES:
-                       devc->limit_samples = g_variant_get_uint64(data);
-                       break;
+                       return sr_sw_limits_config_set(&devc->limits, key, data);
                case SR_CONF_CHANNEL_CONFIG:
                        sval = g_variant_get_string(data, NULL);
                        found = FALSE;
@@ -724,7 +700,7 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *
                        scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
                return SR_OK;
        case SR_CONF_DEVICE_OPTIONS:
-               if (sdi != NULL)
+               if (sdi)
                        break;
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
                        drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
@@ -794,8 +770,7 @@ 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;
@@ -810,11 +785,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi,
        serial = sdi->conn;
        serial_source_add(sdi->session, serial, G_IO_IN, 50,
                        motech_lps_30x_receive_data, (void *)sdi);
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
-       /* Start timer, if required. */
-       if (devc->limit_msec)
-               g_timer_start(devc->elapsed_msec);
+       sr_sw_limits_acquisition_start(&devc->limits);
 
        devc->acq_req = AQ_NONE;
        /* Do not start polling device here, the read function will do it in 50 ms. */
@@ -822,15 +795,9 @@ 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, std_serial_dev_close,
+       return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
                        sdi->conn, LOG_PREFIX);
 }
 
@@ -838,10 +805,10 @@ SR_PRIV struct sr_dev_driver motech_lps_301_driver_info = {
        .name = "motech-lps-301",
        .longname = "Motech LPS-301",
        .api_version = 1,
-       .init = init_lps301,
-       .cleanup = cleanup,
+       .init = std_init,
+       .cleanup = std_cleanup,
        .scan = scan_lps301,
-       .dev_list = dev_list_lps301,
+       .dev_list = std_dev_list,
        .dev_clear = dev_clear_lps301,
        .config_get = config_get,
        .config_set = config_set,