X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fstd.c;h=fa668a7c8eba8efc552dbdf28610641d9f440df9;hb=7d786a962d11c1ed7bca29da3979aaf8a52ef618;hp=8f6f3786339472dd06136ceab46368ea3a0e0764;hpb=1f8f5bc08e0f684156baac513431b40d79ab4ea1;p=libsigrok.git diff --git a/src/std.c b/src/std.c index 8f6f3786..fa668a7c 100644 --- a/src/std.c +++ b/src/std.c @@ -41,20 +41,13 @@ * * @param di The driver instance to use. * @param sr_ctx The libsigrok context to assign. - * @param[in] prefix A driver-specific prefix string used for log messages. * * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. */ -SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx, - const char *prefix) +SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx) { struct drv_context *drvc; - if (!di) { - sr_err("%s: Invalid driver, cannot initialize.", prefix); - return SR_ERR_ARG; - } - drvc = g_malloc0(sizeof(struct drv_context)); drvc->sr_ctx = sr_ctx; drvc->instances = NULL; @@ -356,3 +349,62 @@ SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di) return drvc->instances; } + +/** + * Standard scan() callback API helper. + * + * This function can be used to perform common tasks required by a driver's + * scan() callback. It will initialize the driver for each device on the list + * and add the devices on the list to the driver's device instance list. + * Usually it should be used as the last step in the scan() callback, right + * before returning. + * + * Note: This function can only be used if std_init() has been called + * previously by the driver. + * + * Example: + * @code{c} + * static GSList *scan(struct sr_dev_driver *di, GSList *options) + * { + * struct GSList *device; + * struct sr_dev_inst *sdi; + * + * sdi = g_new0(sr_dev_inst, 1); + * sdi->vendor = ...; + * ... + * devices = g_slist_append(devices, sdi); + * ... + * return std_scan_complete(di, devices); + * } + * @endcode + * + * @param di The driver instance to use. Must not be NULL. + * @param devices List of newly discovered devices (struct sr_dev_inst). + * + * @return The @p devices list. + */ +SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices) +{ + struct drv_context *drvc; + GSList *l; + + if (!di) { + sr_err("Invalid driver instance (di), cannot complete scan."); + return NULL; + } + + drvc = di->context; + + for (l = devices; l; l = l->next) { + struct sr_dev_inst *sdi = l->data; + if (!sdi) { + sr_err("Invalid driver instance, cannot complete scan."); + return NULL; + } + sdi->driver = di; + } + + drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices)); + + return devices; +}