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.
This commit is contained in:
Uwe Hermann 2013-01-28 20:00:54 +01:00
parent c0eea11c45
commit 4b97c74e0b
13 changed files with 47 additions and 29 deletions

View File

@ -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)))) {

View File

@ -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. */

View File

@ -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;
}

View File

@ -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);

View File

@ -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. */

View File

@ -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();

View File

@ -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();

View File

@ -85,7 +85,9 @@ static GSList *hw_scan(GSList *options)
char buf[8];
(void)options;
drvc = di->priv;
devices = NULL;
conn = serialcomm = NULL;

View File

@ -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)

View File

@ -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)) {

View File

@ -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();

View File

@ -317,6 +317,7 @@ static GSList *hw_scan(GSList *options)
(void)options;
drvc = di->priv;
devices = NULL;
clear_instances();

View File

@ -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 */