diff mbox series

[11/25] efw-downloader: subcmd-device: implement 'device' sub command to operate actual device

Message ID 20200821073111.134857-12-o-takashi@sakamocchi.jp
State New
Headers show
Series [01/25] efw-downloader: start a new project to operate on-board flash memory for Fireworks board module | expand

Commit Message

Takashi Sakamoto Aug. 21, 2020, 7:30 a.m. UTC
This commit implements 'device' sub command to operate the target device.
This sub command consists of several operations and parser for the
operation will be implemented in future commits.

Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
---
 efw-downloader/src/main.c          |  4 ++
 efw-downloader/src/meson.build     |  2 +
 efw-downloader/src/subcmd-device.c | 77 ++++++++++++++++++++++++++++++
 efw-downloader/src/subcmds.h       |  8 ++++
 4 files changed, 91 insertions(+)
 create mode 100644 efw-downloader/src/subcmd-device.c
 create mode 100644 efw-downloader/src/subcmds.h
diff mbox series

Patch

diff --git a/efw-downloader/src/main.c b/efw-downloader/src/main.c
index 499e67e..e150cc8 100644
--- a/efw-downloader/src/main.c
+++ b/efw-downloader/src/main.c
@@ -4,6 +4,8 @@ 
 #include <stdlib.h>
 #include <string.h>
 
+#include "subcmds.h"
+
 static void print_help()
 {
     printf("Usage\n"
@@ -11,6 +13,7 @@  static void print_help()
            "\n"
            "where:\n"
            "  SUBCOMMAND:\n"
+           "    device:     operate for device for unit on IEEE 1394 bus\n"
            "    help:       print help\n"
            "  OPTIONS:      optional arguments dependent on the subcommand\n");
 }
@@ -22,6 +25,7 @@  int main(int argc, char **argv)
     size_t size;
         int (*op)(int argc, char **argv);
     } *entry, entries[] = {
+        { "device", sizeof("device"), subcmd_device },
     };
     const char *subcmd;
     int i;
diff --git a/efw-downloader/src/meson.build b/efw-downloader/src/meson.build
index ca894d9..07a6182 100644
--- a/efw-downloader/src/meson.build
+++ b/efw-downloader/src/meson.build
@@ -14,10 +14,12 @@  hinawa = dependency('hinawa',
 sources = [
   'main.c',
   'efw-proto.c',
+  'subcmd-device.c',
 ]
 
 headers = [
   'efw-proto.h',
+  'subcmds.h',
 ]
 
 gnome = import('gnome')
diff --git a/efw-downloader/src/subcmd-device.c b/efw-downloader/src/subcmd-device.c
new file mode 100644
index 0000000..739944e
--- /dev/null
+++ b/efw-downloader/src/subcmd-device.c
@@ -0,0 +1,77 @@ 
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (c) 2020 Takashi Sakamoto
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <assert.h>
+
+#include "efw-proto.h"
+
+static int print_help()
+{
+    printf("Usage\n"
+           "  efw-downloader device CDEV OPERATION ARGUMENTS\n"
+           "\n"
+           "where:\n"
+           "  CDEV:   The firewire character device corresponding to the node for transaction\n"
+           "  OPERATION:\n"
+           "    help:   print this help message\n"
+           "  ARGUMENTS:\n"
+           "    depending on OPERATION\n"
+           "\n");
+    return EXIT_FAILURE;
+}
+
+static int parse_args(int argc, char **argv, const char **path, const char **op_name)
+{
+    if (argc < 2)
+        return -EINVAL;
+    assert(strncmp(argv[1], "device", sizeof("device")) == 0);
+
+    if (argc < 3)
+        return -EINVAL;
+    *path = argv[2];
+
+    if (argc < 4)
+        return -EINVAL;
+    *op_name = argv[3];
+
+    return 0;
+}
+
+int subcmd_device(int argc, char **argv)
+{
+    struct {
+        const char *name;
+        size_t size;
+        void (*op)(int argc, char **argv, EfwProto *proto, GError **error);
+    } *entry, entries[] = {
+    };
+    GError *error = NULL;
+    const char *path;
+    const char *op_name;
+    EfwProto *proto;
+    int err;
+    int i;
+
+    err = parse_args(argc, argv, &path, &op_name);
+    if (err < 0)
+        return print_help(0, NULL, NULL, NULL);
+
+    for (i = 0; i < G_N_ELEMENTS(entries); ++i) {
+        entry = entries + i;
+        if (strncmp(op_name, entry->name, entry->size) == 0)
+            break;
+    }
+    if (i == G_N_ELEMENTS(entries))
+        return print_help();
+
+    entry->op(argc, argv, proto, &error);
+
+    if (error != NULL) {
+        g_clear_error(&error);
+        return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+}
diff --git a/efw-downloader/src/subcmds.h b/efw-downloader/src/subcmds.h
new file mode 100644
index 0000000..f10c420
--- /dev/null
+++ b/efw-downloader/src/subcmds.h
@@ -0,0 +1,8 @@ 
+// SPDX-License-Identifier: GPL-3.0-or-later
+// Copyright (c) 2020 Takashi Sakamoto
+#ifndef __SUBCMDS_H__
+#define __SUBCMDS_H__
+
+int subcmd_device(int argc, char **argv);
+
+#endif