Skip to content

Commit

Permalink
Add parameter to elektroid-cli to use a custom path to the JSON devic…
Browse files Browse the repository at this point in the history
…es file
  • Loading branch information
dagargo committed Jun 3, 2022
1 parent c736b56 commit d6bbe0f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
5 changes: 3 additions & 2 deletions src/connector.c
Original file line number Diff line number Diff line change
Expand Up @@ -2339,7 +2339,8 @@ connector_get_storage_stats_percent (struct connector_storage_stats *statfs)
}

gint
connector_init (struct connector *connector, gint card)
connector_init (struct connector *connector, gint card,
const char *device_filename)
{
gint err;
guint8 id;
Expand Down Expand Up @@ -2449,7 +2450,7 @@ connector_init (struct connector *connector, gint card)
strdup ((gchar *) & rx_msg->data[7 + rx_msg->data[6]]);
id = rx_msg->data[5];
free_msg (rx_msg);
if (load_device_desc (&connector->device_desc, id))
if (load_device_desc (&connector->device_desc, id, device_filename))
{
err = -ENODEV;
goto cleanup_params;
Expand Down
2 changes: 1 addition & 1 deletion src/connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct connector_storage_stats

const struct fs_operations *connector_get_fs_operations (enum connector_fs);

gint connector_init (struct connector *, gint);
gint connector_init (struct connector *, gint, const char *);

void connector_destroy (struct connector *);

Expand Down
10 changes: 8 additions & 2 deletions src/elektroid-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

static struct connector connector;
static struct job_control control;
static const char *devices_filename;

typedef void (*print_item) (struct item_iterator *);

Expand Down Expand Up @@ -109,7 +110,7 @@ static gint
cli_connect (const char *device_path, enum connector_fs fs)
{
gint card = atoi (device_path);
gint ret = connector_init (&connector, card);
gint ret = connector_init (&connector, card, devices_filename);
if (!ret && fs && !(connector.device_desc.filesystems & fs))
{
error_print ("Filesystem not supported for device '%s'\n",
Expand Down Expand Up @@ -602,10 +603,15 @@ main (int argc, char *argv[])
sigaction (SIGHUP, &action, NULL);
}

while ((c = getopt (argc, argv, "v")) != -1)
devices_filename = NULL;

while ((c = getopt (argc, argv, "f:v")) != -1)
{
switch (c)
{
case 'f':
devices_filename = optarg;
break;
case 'v':
vflg++;
break;
Expand Down
2 changes: 1 addition & 1 deletion src/elektroid.c
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ elektroid_set_device (GtkWidget * object, gpointer data)

card = g_value_get_uint (&cardv);

if (connector_init (&connector, card) < 0)
if (connector_init (&connector, card, NULL) < 0)
{
error_print ("Error while connecting\n");
}
Expand Down
38 changes: 27 additions & 11 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,8 @@ set_job_control_progress (struct job_control *control, gdouble p)
}

gint
load_device_desc (struct connector_device_desc *device_desc, guint8 id)
load_device_desc (struct connector_device_desc *device_desc, guint8 id,
const char *devices_filename_)
{
gint err, devices;
JsonParser *parser;
Expand All @@ -399,17 +400,9 @@ load_device_desc (struct connector_device_desc *device_desc, guint8 id)

parser = json_parser_new ();

devices_filename = get_expanded_dir (CONF_DIR DEVICES_FILE);

if (!json_parser_load_from_file (parser, devices_filename, &error))
if (devices_filename_)
{
debug_print (1, "%s\n", error->message);
g_clear_error (&error);

g_free (devices_filename);
devices_filename = strdup (DATADIR DEVICES_FILE);

debug_print (1, "Falling back to %s...\n", devices_filename);
devices_filename = strdup (devices_filename_);

if (!json_parser_load_from_file (parser, devices_filename, &error))
{
Expand All @@ -419,6 +412,29 @@ load_device_desc (struct connector_device_desc *device_desc, guint8 id)
goto cleanup_parser;
}
}
else
{
devices_filename = get_expanded_dir (CONF_DIR DEVICES_FILE);

if (!json_parser_load_from_file (parser, devices_filename, &error))
{
debug_print (1, "%s\n", error->message);
g_clear_error (&error);

g_free (devices_filename);
devices_filename = strdup (DATADIR DEVICES_FILE);

debug_print (1, "Falling back to %s...\n", devices_filename);

if (!json_parser_load_from_file (parser, devices_filename, &error))
{
error_print ("%s", error->message);
g_clear_error (&error);
err = -ENODEV;
goto cleanup_parser;
}
}
}

reader = json_reader_new (json_parser_get_root (parser));
if (!reader)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,6 @@ gchar *get_human_size (guint, gboolean);

void set_job_control_progress (struct job_control *, gdouble);

gint load_device_desc (struct connector_device_desc *, guint8);
gint load_device_desc (struct connector_device_desc *, guint8, const char *);

#endif
7 changes: 4 additions & 3 deletions test/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
TESTS = sample_fs_tests.sh data_fs_tests.sh

EXTRA_DIST = \
$(TESTS) \
res/square.wav
$(TESTS) \
res/square.wav \
res/devices.json

AM_TESTS_ENVIRONMENT = \
ecli='$(abs_top_builddir)'/src/elektroid-cli; \
ecli='$(abs_top_builddir)/src/elektroid-cli -f $(srcdir)/res/devices.json'; \
export ecli;
7 changes: 7 additions & 0 deletions test/res/devices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[{
"id": 12,
"name": "Digitakt",
"alias": "dt",
"filesystems": 57,
"storage": 3
}]

0 comments on commit d6bbe0f

Please sign in to comment.