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:
parent
c0eea11c45
commit
4b97c74e0b
|
@ -448,7 +448,9 @@ static GSList *hw_scan(GSList *options)
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
|
|
||||||
devices = NULL;
|
devices = NULL;
|
||||||
|
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
||||||
if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
|
if (!(devc = g_try_malloc(sizeof(struct dev_context)))) {
|
||||||
|
|
|
@ -96,6 +96,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
|
|
||||||
devices = NULL;
|
devices = NULL;
|
||||||
|
|
||||||
/* Allocate memory for our private device context. */
|
/* Allocate memory for our private device context. */
|
||||||
|
@ -169,8 +170,6 @@ static GSList *hw_scan(GSList *options)
|
||||||
devices = g_slist_append(devices, sdi);
|
devices = g_slist_append(devices, sdi);
|
||||||
drvc->instances = g_slist_append(drvc->instances, 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. */
|
/* Close device. We'll reopen it again when we need it. */
|
||||||
(void) la8_close(devc); /* Log, but ignore errors. */
|
(void) la8_close(devc); /* Log, but ignore errors. */
|
||||||
|
|
||||||
|
|
|
@ -97,10 +97,11 @@ static GSList *hw_scan(GSList *options)
|
||||||
GSList *devices, *l;
|
GSList *devices, *l;
|
||||||
const char *conn, *serialcomm;
|
const char *conn, *serialcomm;
|
||||||
|
|
||||||
devices = NULL;
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
drvc->instances = NULL;
|
drvc->instances = NULL;
|
||||||
|
|
||||||
|
devices = NULL;
|
||||||
|
|
||||||
conn = serialcomm = NULL;
|
conn = serialcomm = NULL;
|
||||||
for (l = options; l; l = l->next) {
|
for (l = options; l; l = l->next) {
|
||||||
src = l->data;
|
src = l->data;
|
||||||
|
@ -123,7 +124,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
|
if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
|
||||||
sr_dbg("failed to malloc devc");
|
sr_dbg("Device context malloc failed.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -172,6 +172,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
|
|
||||||
devices = NULL;
|
devices = NULL;
|
||||||
|
|
||||||
sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, DEMONAME, NULL, NULL);
|
sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, DEMONAME, NULL, NULL);
|
||||||
|
|
|
@ -285,11 +285,12 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
devcnt = 0;
|
|
||||||
devices = 0;
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
drvc->instances = NULL;
|
drvc->instances = NULL;
|
||||||
|
|
||||||
|
devcnt = 0;
|
||||||
|
devices = 0;
|
||||||
|
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
||||||
/* Find all Hantek DSO devices and upload firmware to all of them. */
|
/* Find all Hantek DSO devices and upload firmware to all of them. */
|
||||||
|
|
|
@ -95,10 +95,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
if (!(drvc = di->priv)) {
|
drvc = di->priv;
|
||||||
sr_err("Driver was not initialized.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* USB scan is always authoritative. */
|
/* USB scan is always authoritative. */
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
|
@ -167,10 +167,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
if (!(drvc = di->priv)) {
|
drvc = di->priv;
|
||||||
sr_err("Driver was not initialized.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* USB scan is always authoritative. */
|
/* USB scan is always authoritative. */
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
|
@ -85,7 +85,9 @@ static GSList *hw_scan(GSList *options)
|
||||||
char buf[8];
|
char buf[8];
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
|
|
||||||
devices = NULL;
|
devices = NULL;
|
||||||
|
|
||||||
conn = serialcomm = NULL;
|
conn = serialcomm = NULL;
|
||||||
|
|
|
@ -188,10 +188,11 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
devices = NULL;
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
drvc->instances = NULL;
|
drvc->instances = NULL;
|
||||||
|
|
||||||
|
devices = NULL;
|
||||||
|
|
||||||
dir = g_dir_open("/sys/class/usb/", 0, NULL);
|
dir = g_dir_open("/sys/class/usb/", 0, NULL);
|
||||||
|
|
||||||
if (dir == NULL)
|
if (dir == NULL)
|
||||||
|
|
|
@ -93,10 +93,11 @@ static GSList *hw_scan(GSList *options)
|
||||||
GSList *devices, *l;
|
GSList *devices, *l;
|
||||||
const char *conn, *serialcomm;
|
const char *conn, *serialcomm;
|
||||||
|
|
||||||
devices = NULL;
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
drvc->instances = NULL;
|
drvc->instances = NULL;
|
||||||
|
|
||||||
|
devices = NULL;
|
||||||
|
|
||||||
conn = serialcomm = NULL;
|
conn = serialcomm = NULL;
|
||||||
for (l = options; l; l = l->next) {
|
for (l = options; l; l = l->next) {
|
||||||
if (!(src = l->data)) {
|
if (!(src = l->data)) {
|
||||||
|
|
|
@ -100,10 +100,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
|
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
if (!(drvc = di->priv)) {
|
drvc = di->priv;
|
||||||
sr_err("Driver was not initialized.");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* USB scan is always authoritative. */
|
/* USB scan is always authoritative. */
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
|
@ -317,6 +317,7 @@ static GSList *hw_scan(GSList *options)
|
||||||
(void)options;
|
(void)options;
|
||||||
|
|
||||||
drvc = di->priv;
|
drvc = di->priv;
|
||||||
|
|
||||||
devices = NULL;
|
devices = NULL;
|
||||||
|
|
||||||
clear_instances();
|
clear_instances();
|
||||||
|
|
36
hwdriver.c
36
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
|
* The order in which the system is scanned for devices is not specified. The
|
||||||
* caller should not assume or rely on any specific order.
|
* caller should not assume or rely on any specific order.
|
||||||
*
|
*
|
||||||
* @param driver The driver.
|
* Before calling sr_driver_scan(), the user must have previously initialized
|
||||||
* @param options A list of struct sr_hwopt options to pass to the driver's
|
* the driver by calling sr_driver_init().
|
||||||
* scanner.
|
|
||||||
*
|
*
|
||||||
* @return A GSList * of struct sr_dev_inst, or NULL if no devices were found.
|
* @param driver The driver that should scan. This must be a pointer to one of
|
||||||
* This list must be freed by the caller, but without freeing the data
|
* the entries returned by sr_driver_list(). Must not be NULL.
|
||||||
* pointed to in the list.
|
* @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)
|
SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options)
|
||||||
{
|
{
|
||||||
|
GSList *l;
|
||||||
|
|
||||||
if (driver->scan)
|
if (!driver) {
|
||||||
return driver->scan(options);
|
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 */
|
/** @private */
|
||||||
|
|
Loading…
Reference in New Issue