]> sigrok.org Git - sigrok-util.git/blobdiff - source/drv-api.c
new-driver: Change 'probe_group' to 'channel_group'.
[sigrok-util.git] / source / drv-api.c
index 64f5ee18601abe1d922a00a4a6319b0d9beed092..26707b336380b3b4b3cb33947e0d39be78662db9 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <glib.h>
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
 #include "protocol.h"
 
 SR_PRIV struct sr_dev_driver ${lib}_driver_info;
 static struct sr_dev_driver *di = &${lib}_driver_info;
 
-/* Properly close and free all devices. */
-static int clear_instances(void)
+static int init(struct sr_context *sr_ctx)
 {
-       struct sr_dev_inst *sdi;
-       struct drv_context *drvc;
-       struct dev_context *devc;
-       GSList *l;
-
-       if (!(drvc = di->priv))
-               return SR_OK;
-
-       for (l = drvc->instances; l; l = l->next) {
-               if (!(sdi = l->data))
-                       continue;
-               if (!(devc = sdi->priv))
-                       continue;
-
-               /* TODO */
-
-               sr_dev_inst_free(sdi);
-       }
-
-       g_slist_free(drvc->instances);
-       drvc->instances = NULL;
-
-       return SR_OK;
+       return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static int hw_init(struct sr_context *sr_ctx)
-{
-       struct drv_context *drvc;
-
-       if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
-               sr_err("Driver context malloc failed.");
-               return SR_ERR_MALLOC;
-       }
-
-       /* TODO */
-
-       drvc->sr_ctx = sr_ctx;
-       di->priv = drvc;
-
-       return SR_OK;
-}
-
-static GSList *hw_scan(GSList *options)
+static GSList *scan(GSList *options)
 {
        struct drv_context *drvc;
        GSList *devices;
@@ -81,94 +38,135 @@ static GSList *hw_scan(GSList *options)
        drvc = di->priv;
        drvc->instances = NULL;
 
-       /* TODO */
+       /* TODO: scan for devices, either based on a SR_CONF_CONN option
+        * or on a USB scan. */
 
        return devices;
 }
 
-static GSList *hw_dev_list(void)
+static GSList *dev_list(void)
 {
-       struct drv_context *drvc;
-
-       drvc = di->priv;
+       return ((struct drv_context *)(di->priv))->instances;
+}
 
-       return drvc->instances;
+static int dev_clear(void)
+{
+       return std_dev_clear(di, NULL);
 }
 
-static int hw_dev_open(struct sr_dev_inst *sdi)
+static int dev_open(struct sr_dev_inst *sdi)
 {
-       /* TODO */
+       (void)sdi;
+
+       /* TODO: get handle from sdi->conn and open it. */
+
+       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)
 {
-       /* TODO */
+       (void)sdi;
+
+       /* TODO: get handle from sdi->conn and close it. */
+
+       sdi->status = SR_ST_INACTIVE;
 
        return SR_OK;
 }
 
-static int hw_cleanup(void)
+static int cleanup(void)
 {
-       clear_instances();
+       dev_clear();
 
-       /* TODO */
+       /* TODO: free other driver resources, if any. */
 
        return SR_OK;
 }
 
-static int hw_info_get(int info_id, const void **data,
-                      const struct sr_dev_inst *sdi)
+static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
-       switch (info_id) {
+       int ret;
+
+       (void)sdi;
+       (void)data;
+       (void)cg;
+
+       ret = SR_OK;
+       switch (key) {
        /* TODO */
        default:
-               return SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
-       return SR_OK;
+       return ret;
 }
 
-static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
-                            const void *value)
+static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
 {
        int ret;
 
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't set config options.");
-               return SR_ERR;
+       (void)data;
+       (void)cg;
+
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
+
+       ret = SR_OK;
+       switch (key) {
+       /* TODO */
+       default:
+               ret = SR_ERR_NA;
        }
 
+       return ret;
+}
+
+static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+               const struct sr_channel_group *cg)
+{
+       int ret;
+
+       (void)sdi;
+       (void)data;
+       (void)cg;
+
        ret = SR_OK;
-       switch (hwcap) {
+       switch (key) {
        /* TODO */
        default:
-               sr_err("Unknown hardware capability: %d.", hwcap);
-               ret = SR_ERR_ARG;
+               return SR_ERR_NA;
        }
 
        return ret;
 }
 
-static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
+static int dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       /* TODO */
+       (void)sdi;
+       (void)cb_data;
+
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
+
+       /* TODO: configure hardware, reset acquisition state, set up
+        * callbacks and send header packet. */
 
        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)
 {
        (void)cb_data;
 
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't stop acquisition.");
-               return SR_ERR;
-       }
+       if (sdi->status != SR_ST_ACTIVE)
+               return SR_ERR_DEV_CLOSED;
 
-       /* TODO */
+       /* TODO: stop acquisition. */
 
        return SR_OK;
 }
@@ -177,16 +175,17 @@ SR_PRIV struct sr_dev_driver ${lib}_driver_info = {
        .name = "${short}",
        .longname = "${name}",
        .api_version = 1,
-       .init = hw_init,
-       .cleanup = hw_cleanup,
-       .scan = hw_scan,
-       .dev_list = hw_dev_list,
-       .dev_clear = clear_instances,
-       .dev_open = hw_dev_open,
-       .dev_close = hw_dev_close,
-       .info_get = hw_info_get,
-       .dev_config_set = hw_dev_config_set,
-       .dev_acquisition_start = hw_dev_acquisition_start,
-       .dev_acquisition_stop = hw_dev_acquisition_stop,
+       .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 = dev_open,
+       .dev_close = dev_close,
+       .dev_acquisition_start = dev_acquisition_start,
+       .dev_acquisition_stop = dev_acquisition_stop,
        .priv = NULL,
 };