From: Uwe Hermann Date: Mon, 28 Jan 2013 19:00:54 +0000 (+0100) Subject: sr_driver_scan(): Improve checks. X-Git-Tag: dsupstream~292 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=4b97c74e0b5c19ad0ee04620b618461248ab69d7;p=libsigrok.git sr_driver_scan(): Improve checks. Check the relevant arguments for != NULL before calling the actual driver-specific function, so that the driver can safely assume those arguments are non-NULL. This removes the need to duplicate these checks in every driver. Also, do some minor whitespace and consistency improvements. --- diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index ec50a2b2..d40c911b 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -448,7 +448,9 @@ static GSList *hw_scan(GSList *options) (void)options; drvc = di->priv; + devices = NULL; + clear_instances(); if (!(devc = g_try_malloc(sizeof(struct dev_context)))) { diff --git a/hardware/chronovu-la8/api.c b/hardware/chronovu-la8/api.c index 478f0a1c..2a97440f 100644 --- a/hardware/chronovu-la8/api.c +++ b/hardware/chronovu-la8/api.c @@ -96,6 +96,7 @@ static GSList *hw_scan(GSList *options) (void)options; drvc = di->priv; + devices = NULL; /* Allocate memory for our private device context. */ @@ -169,8 +170,6 @@ static GSList *hw_scan(GSList *options) devices = g_slist_append(devices, sdi); drvc->instances = g_slist_append(drvc->instances, sdi); - sr_spew("Device init successful."); - /* Close device. We'll reopen it again when we need it. */ (void) la8_close(devc); /* Log, but ignore errors. */ diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index fd6112d8..39e557a6 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -97,10 +97,11 @@ static GSList *hw_scan(GSList *options) GSList *devices, *l; const char *conn, *serialcomm; - devices = NULL; drvc = di->priv; drvc->instances = NULL; + devices = NULL; + conn = serialcomm = NULL; for (l = options; l; l = l->next) { src = l->data; @@ -123,7 +124,7 @@ static GSList *hw_scan(GSList *options) return NULL; if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { - sr_dbg("failed to malloc devc"); + sr_dbg("Device context malloc failed."); return NULL; } diff --git a/hardware/demo/demo.c b/hardware/demo/demo.c index b20239a1..923d20a9 100644 --- a/hardware/demo/demo.c +++ b/hardware/demo/demo.c @@ -172,6 +172,7 @@ static GSList *hw_scan(GSList *options) (void)options; drvc = di->priv; + devices = NULL; sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, DEMONAME, NULL, NULL); diff --git a/hardware/hantek-dso/api.c b/hardware/hantek-dso/api.c index 7c2eaa95..acb6d815 100644 --- a/hardware/hantek-dso/api.c +++ b/hardware/hantek-dso/api.c @@ -285,11 +285,12 @@ static GSList *hw_scan(GSList *options) (void)options; - devcnt = 0; - devices = 0; drvc = di->priv; drvc->instances = NULL; + devcnt = 0; + devices = 0; + clear_instances(); /* Find all Hantek DSO devices and upload firmware to all of them. */ diff --git a/hardware/lascar-el-usb/api.c b/hardware/lascar-el-usb/api.c index 76285388..1804f2d5 100644 --- a/hardware/lascar-el-usb/api.c +++ b/hardware/lascar-el-usb/api.c @@ -95,10 +95,7 @@ static GSList *hw_scan(GSList *options) (void)options; - if (!(drvc = di->priv)) { - sr_err("Driver was not initialized."); - return NULL; - } + drvc = di->priv; /* USB scan is always authoritative. */ clear_instances(); diff --git a/hardware/nexus-osciprime/api.c b/hardware/nexus-osciprime/api.c index c35e2cfb..1b8d8083 100644 --- a/hardware/nexus-osciprime/api.c +++ b/hardware/nexus-osciprime/api.c @@ -167,10 +167,7 @@ static GSList *hw_scan(GSList *options) (void)options; - if (!(drvc = di->priv)) { - sr_err("Driver was not initialized."); - return NULL; - } + drvc = di->priv; /* USB scan is always authoritative. */ clear_instances(); diff --git a/hardware/openbench-logic-sniffer/api.c b/hardware/openbench-logic-sniffer/api.c index c7e1e687..6da0eab2 100644 --- a/hardware/openbench-logic-sniffer/api.c +++ b/hardware/openbench-logic-sniffer/api.c @@ -85,7 +85,9 @@ static GSList *hw_scan(GSList *options) char buf[8]; (void)options; + drvc = di->priv; + devices = NULL; conn = serialcomm = NULL; diff --git a/hardware/rigol-ds1xx2/api.c b/hardware/rigol-ds1xx2/api.c index 7cb57912..894f5afc 100644 --- a/hardware/rigol-ds1xx2/api.c +++ b/hardware/rigol-ds1xx2/api.c @@ -188,10 +188,11 @@ static GSList *hw_scan(GSList *options) (void)options; - devices = NULL; drvc = di->priv; drvc->instances = NULL; + devices = NULL; + dir = g_dir_open("/sys/class/usb/", 0, NULL); if (dir == NULL) diff --git a/hardware/tondaj-sl-814/api.c b/hardware/tondaj-sl-814/api.c index 9edc441b..c621eeb8 100644 --- a/hardware/tondaj-sl-814/api.c +++ b/hardware/tondaj-sl-814/api.c @@ -93,10 +93,11 @@ static GSList *hw_scan(GSList *options) GSList *devices, *l; const char *conn, *serialcomm; - devices = NULL; drvc = di->priv; drvc->instances = NULL; + devices = NULL; + conn = serialcomm = NULL; for (l = options; l; l = l->next) { if (!(src = l->data)) { diff --git a/hardware/victor-dmm/api.c b/hardware/victor-dmm/api.c index d6bc5a34..0edd79e4 100644 --- a/hardware/victor-dmm/api.c +++ b/hardware/victor-dmm/api.c @@ -100,10 +100,7 @@ static GSList *hw_scan(GSList *options) (void)options; - if (!(drvc = di->priv)) { - sr_err("Driver was not initialized."); - return NULL; - } + drvc = di->priv; /* USB scan is always authoritative. */ clear_instances(); diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index f8cf81f8..8ccd40f1 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -317,6 +317,7 @@ static GSList *hw_scan(GSList *options) (void)options; drvc = di->priv; + devices = NULL; clear_instances(); diff --git a/hwdriver.c b/hwdriver.c index 364d97bd..d8748e93 100644 --- a/hwdriver.c +++ b/hwdriver.c @@ -289,21 +289,39 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) * The order in which the system is scanned for devices is not specified. The * caller should not assume or rely on any specific order. * - * @param driver The driver. - * @param options A list of struct sr_hwopt options to pass to the driver's - * scanner. + * Before calling sr_driver_scan(), the user must have previously initialized + * the driver by calling sr_driver_init(). * - * @return A GSList * of struct sr_dev_inst, or NULL if no devices were found. - * This list must be freed by the caller, but without freeing the data - * pointed to in the list. + * @param driver The driver that should scan. This must be a pointer to one of + * the entries returned by sr_driver_list(). Must not be NULL. + * @param options A list of 'struct sr_hwopt' options to pass to the driver's + * scanner. Can be NULL/empty. + * + * @return A GSList * of 'struct sr_dev_inst', or NULL if no devices were + * found (or errors were encountered). This list must be freed by the + * caller using g_slist_free(), but without freeing the data pointed + * to in the list. */ SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) { + GSList *l; - if (driver->scan) - return driver->scan(options); + if (!driver) { + sr_err("Invalid driver, can't scan for devices."); + return NULL; + } - return NULL; + if (!driver->priv) { + sr_err("Driver not initialized, can't scan for devices."); + return NULL; + } + + l = driver->scan(options); + + sr_spew("Scan of '%s' found %d devices.", driver->name, + g_slist_length(l)); + + return l; } /** @private */