Skip to content

Commit

Permalink
Show driver detail even if no device was found.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Oct 27, 2014
1 parent 5e78186 commit d75c852
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 67 deletions.
55 changes: 1 addition & 54 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@

extern struct sr_context *sr_ctx;

/* Convert driver options hash to GSList of struct sr_config. */
static GSList *hash_to_hwopt(GHashTable *hash)
{
struct sr_config *src;
GList *gl, *keys;
GSList *opts;
char *key;

keys = g_hash_table_get_keys(hash);
opts = NULL;
for (gl = keys; gl; gl = gl->next) {
key = gl->data;
src = g_malloc(sizeof(struct sr_config));
if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
return NULL;
opts = g_slist_append(opts, src);
}
g_list_free(keys);

return opts;
}

static void free_drvopts(struct sr_config *src)
{
g_variant_unref(src->data);
Expand All @@ -55,43 +33,12 @@ static void free_drvopts(struct sr_config *src)
GSList *device_scan(void)
{
struct sr_dev_driver **drivers, *driver;
GHashTable *drvargs;
GSList *drvopts, *devices, *tmpdevs, *l;
int i;
char *drvname;

if (opt_drv) {
drvargs = parse_generic_arg(opt_drv, TRUE);
drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
g_hash_table_remove(drvargs, "sigrok_key");
driver = NULL;
drivers = sr_driver_list();
for (i = 0; drivers[i]; i++) {
if (strcmp(drivers[i]->name, drvname))
continue;
driver = drivers[i];
}
if (!driver) {
g_critical("Driver %s not found.", drvname);
g_hash_table_destroy(drvargs);
g_free(drvname);
return NULL;
}
g_free(drvname);
if (sr_driver_init(sr_ctx, driver) != SR_OK) {
g_critical("Failed to initialize driver.");
g_hash_table_destroy(drvargs);
if (!parse_driver(opt_drv, &driver, &drvopts))
return NULL;
}
drvopts = NULL;
if (g_hash_table_size(drvargs) > 0) {
if (!(drvopts = hash_to_hwopt(drvargs))) {
/* Unknown options, already logged. */
g_hash_table_destroy(drvargs);
return NULL;
}
}
g_hash_table_destroy(drvargs);
devices = sr_driver_scan(driver, drvopts);
g_slist_free_full(drvopts, (GDestroyNotify)free_drvopts);
} else {
Expand Down
72 changes: 72 additions & 0 deletions parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <string.h>
#include <glib.h>

extern struct sr_context *sr_ctx;

struct sr_channel *find_channel(GSList *channellist, const char *channelname)
{
struct sr_channel *ch;
Expand Down Expand Up @@ -370,3 +372,73 @@ int canon_cmp(const char *str1, const char *str2)

return ret;
}

/* Convert driver options hash to GSList of struct sr_config. */
static GSList *hash_to_hwopt(GHashTable *hash)
{
struct sr_config *src;
GList *gl, *keys;
GSList *opts;
char *key;

keys = g_hash_table_get_keys(hash);
opts = NULL;
for (gl = keys; gl; gl = gl->next) {
key = gl->data;
src = g_malloc(sizeof(struct sr_config));
if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
return NULL;
opts = g_slist_append(opts, src);
}
g_list_free(keys);

return opts;
}

int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
{
struct sr_dev_driver **drivers;
GHashTable *drvargs;
int i;
char *drvname;

drvargs = parse_generic_arg(arg, TRUE);

drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
g_hash_table_remove(drvargs, "sigrok_key");
*driver = NULL;
drivers = sr_driver_list();
for (i = 0; drivers[i]; i++) {
if (strcmp(drivers[i]->name, drvname))
continue;
*driver = drivers[i];
}
if (!*driver) {
g_critical("Driver %s not found.", drvname);
g_hash_table_destroy(drvargs);
g_free(drvname);
return FALSE;
}
g_free(drvname);
if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
g_critical("Failed to initialize driver.");
g_hash_table_destroy(drvargs);
return FALSE;
}

if (drvopts) {
*drvopts = NULL;
if (g_hash_table_size(drvargs) > 0) {
if (!(*drvopts = hash_to_hwopt(drvargs))) {
/* Unknown options, already logged. */
g_hash_table_destroy(drvargs);
return FALSE;
}
}
}

g_hash_table_destroy(drvargs);

return TRUE;
}

57 changes: 44 additions & 13 deletions show.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,47 @@ void show_dev_list(void)

}

void show_drv_detail(struct sr_dev_driver *driver)
{
const struct sr_config_info *srci;
GVariant *gvar_opts;
const uint32_t *opts;
gsize num_elements, i;

if ((sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS,
&gvar_opts) == SR_OK)) {
opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
sizeof(uint32_t));
if (num_elements) {
printf("Driver functions:\n");
for (i = 0; i < num_elements; i++) {
if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
continue;
printf(" %s\n", srci->name);
}
}
g_variant_unref(gvar_opts);
}

if ((sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
&gvar_opts) == SR_OK)) {
opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
sizeof(uint32_t));
if (num_elements) {
printf("Scan options:\n");
for (i = 0; i < num_elements; i++) {
if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
continue;
printf(" %s\n", srci->id);
}
}
g_variant_unref(gvar_opts);
}
}

void show_dev_detail(void)
{
struct sr_dev_driver *driver;
struct sr_dev_inst *sdi;
const struct sr_config_info *srci;
struct sr_channel *ch;
Expand All @@ -212,6 +251,11 @@ void show_dev_detail(void)
char *tmp_str, *s, c;
const char **stropts;

if (parse_driver(opt_drv, &driver, NULL)) {
/* A driver was specified, report driver-wide options now. */
show_drv_detail(driver);
}

if (!(devices = device_scan())) {
g_critical("No devices found.");
return;
Expand All @@ -233,19 +277,6 @@ void show_dev_detail(void)
return;
}

if ((sr_config_list(sdi->driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
&gvar_opts) == SR_OK)) {
opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
sizeof(int32_t));
printf("Supported driver options:\n");
for (i = 0; i < num_elements; i++) {
if (!(srci = sr_config_info_get(opts[i])))
continue;
printf(" %s\n", srci->id);
}
g_variant_unref(gvar_opts);
}

/* Selected channels and channel group may affect which options are
* returned, or which values for them. */
select_channels(sdi);
Expand Down
1 change: 1 addition & 0 deletions sigrok-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
GHashTable *parse_generic_arg(const char *arg, gboolean sep_first);
GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs);
int canon_cmp(const char *str1, const char *str2);
int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts);

/* anykey.c */
void add_anykey(struct sr_session *session);
Expand Down

0 comments on commit d75c852

Please sign in to comment.