]> sigrok.org Git - libsigrok.git/blobdiff - hardware/alsa/api.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / hardware / alsa / api.c
index 594b95e85bf4c749462f9f2a7b30eacbea5398a3..82af3d0ed70111127312780c26f21e1c7ad1f1ff 100644 (file)
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-static const int hwcaps[] = {
+static const int32_t hwcaps[] = {
        SR_CONF_SAMPLERATE,
        SR_CONF_LIMIT_SAMPLES,
        SR_CONF_CONTINUOUS,
-       0,
 };
 
 SR_PRIV struct sr_dev_driver alsa_driver_info;
 static struct sr_dev_driver *di = &alsa_driver_info;
 
-static int clear_instances(void)
+static void clear_helper(void *priv)
 {
-       struct drv_context *drvc;
+       struct dev_context *devc;
 
-       if (!(drvc = di->priv))
-               return SR_OK;
+       devc = priv;
 
-       g_slist_free_full(drvc->instances, (GDestroyNotify)alsa_dev_inst_clear);
-       drvc->instances = NULL;
+       snd_pcm_hw_params_free(devc->hw_params);
+       devc->hw_params = NULL;
 
-       return SR_OK;
+       g_free((void *)devc->samplerates);
+       devc->samplerates = NULL;
 }
 
-static int hw_init(struct sr_context *sr_ctx)
+static int dev_clear(void)
 {
-       struct drv_context *drvc;
-
-       if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
-               sr_err("Driver context malloc failed.");
-               return SR_ERR_MALLOC;
-       }
-
-       drvc->sr_ctx = sr_ctx;
-       di->priv = drvc;
+       return std_dev_clear(di, clear_helper);
+}
 
-       return SR_OK;
+static int init(struct sr_context *sr_ctx)
+{
+       return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static GSList *hw_scan(GSList *options)
+static GSList *scan(GSList *options)
 {
        return alsa_scan(options, di);
 }
 
-static GSList *hw_dev_list(void)
+static GSList *dev_list(void)
 {
-       struct drv_context *drvc;
-
-       drvc = di->priv;
-
-       return drvc->instances;
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
-static int hw_dev_open(struct sr_dev_inst *sdi)
+static int dev_open(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        int ret;
@@ -107,25 +97,25 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
                return SR_ERR;
        }
 
+       sdi->status = SR_ST_ACTIVE;
+
        return SR_OK;
 }
 
-static int hw_dev_close(struct sr_dev_inst *sdi)
+static int dev_close(struct sr_dev_inst *sdi)
 {
        int ret;
        struct dev_context *devc;
 
        devc = sdi->priv;
 
-       sr_dbg("Closing device.");
-
        if (devc->capture_handle) {
                sr_dbg("Closing PCM device.");
-               if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
+               if ((ret = snd_pcm_close(devc->capture_handle)) < 0)
                        sr_err("Failed to close device: %s.",
                               snd_strerror(ret));
-                       devc->capture_handle = NULL;
-               }
+               devc->capture_handle = NULL;
+               sdi->status = SR_ST_INACTIVE;
        } else {
                sr_dbg("No capture handle, no need to close audio device.");
        }
@@ -133,95 +123,103 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int hw_cleanup(void)
+static int cleanup(void)
 {
-       clear_instances();
-
-       return SR_OK;
+       return dev_clear();
 }
 
-static int config_get(int id, const void **data, const struct sr_dev_inst *sdi)
+static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
 
-       if (id != SR_DI_HWCAPS) /* For SR_DI_HWCAPS sdi will be NULL. */
-               devc = sdi->priv;
+       (void)cg;
 
        switch (id) {
-       case SR_DI_HWCAPS:
-               *data = hwcaps;
-               break;
-       case SR_DI_CUR_SAMPLERATE:
-               *data = &devc->cur_samplerate;
-               break;
-       case SR_DI_SAMPLERATES:
-               if (!devc->supp_rates.list) {
-                       sr_err("Instance did not contain a samplerate list.");
-                       return SR_ERR_ARG;
-               }
-               *data = &devc->supp_rates;
+       case SR_CONF_SAMPLERATE:
+               devc = sdi->priv;
+               *data = g_variant_new_uint64(devc->cur_samplerate);
                break;
        default:
-               return SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
+static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
 
+       (void)cg;
+
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
+
        devc = sdi->priv;
 
        switch (id) {
        case SR_CONF_SAMPLERATE:
-               alsa_set_samplerate(sdi, *(const uint64_t *)value);
+               alsa_set_samplerate(sdi, g_variant_get_uint64(data));
                break;
        case SR_CONF_LIMIT_SAMPLES:
-               devc->limit_samples = *(const uint64_t *)value;
+               devc->limit_samples = g_variant_get_uint64(data);
                break;
        default:
-               sr_err("Unknown capability: %d.", id);
-               return SR_ERR;
+               return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
+       GVariant *gvar;
+       GVariantBuilder gvb;
+       int i;
 
-       (void)sdi;
+       (void)cg;
 
        switch (key) {
+       case SR_CONF_DEVICE_OPTIONS:
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               break;
        case SR_CONF_SAMPLERATE:
                if (!sdi || !sdi->priv)
                        return SR_ERR_ARG;
                devc = sdi->priv;
-               if (!devc->supp_rates.list) {
+               if (!devc->samplerates) {
                        sr_err("Instance did not contain a samplerate list.");
                        return SR_ERR_ARG;
                }
-               *data = &devc->supp_rates;
+               for (i = 0; devc->samplerates[i]; i++)
+                       ;
+               g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
+               gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
+                               devc->samplerates, i, sizeof(uint64_t));
+               g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
+               *data = g_variant_builder_end(&gvb);
                break;
        default:
-               return SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
        return SR_OK;
 }
 
-static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
-                                   void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
        struct dev_context *devc;
        int count, ret;
        char *endianness;
 
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
+
        devc = sdi->priv;
        devc->cb_data = cb_data;
        devc->num_samples = 0;
@@ -257,9 +255,9 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
-       sr_dbg("Setting audio channel count to %d.", devc->num_probes);
+       sr_dbg("Setting audio channel count to %d.", devc->num_channels);
        ret = snd_pcm_hw_params_set_channels(devc->capture_handle,
-                                            devc->hw_params, devc->num_probes);
+                                            devc->hw_params, devc->num_channels);
        if (ret < 0) {
                sr_err("Can't set channel count: %s.", snd_strerror(ret));
                return SR_ERR;
@@ -301,12 +299,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        }
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER packet.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(devc->cb_data, &packet);
+       std_session_send_df_header(cb_data, LOG_PREFIX);
 
        /* Poll every 10ms, or whenever some data comes in. */
        sr_source_add(devc->ufds[0].fd, devc->ufds[0].events, 10,
@@ -317,7 +310,7 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
        struct sr_datafeed_packet packet;
        struct dev_context *devc;
@@ -339,17 +332,17 @@ SR_PRIV struct sr_dev_driver alsa_driver_info = {
        .name = "alsa",
        .longname = "ALSA driver",
        .api_version = 1,
-       .init = hw_init,
-       .cleanup = hw_cleanup,
-       .scan = hw_scan,
-       .dev_list = hw_dev_list,
-       .dev_clear = clear_instances,
+       .init = init,
+       .cleanup = cleanup,
+       .scan = scan,
+       .dev_list = dev_list,
+       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
-       .dev_open = hw_dev_open,
-       .dev_close = hw_dev_close,
-       .dev_acquisition_start = hw_dev_acquisition_start,
-       .dev_acquisition_stop = hw_dev_acquisition_stop,
+       .dev_open = dev_open,
+       .dev_close = dev_close,
+       .dev_acquisition_start = dev_acquisition_start,
+       .dev_acquisition_stop = dev_acquisition_stop,
        .priv = NULL,
 };