move some code to a separate source file
authorFelix Fietkau <nbd@openwrt.org>
Thu, 30 May 2013 13:50:44 +0000 (15:50 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Thu, 30 May 2013 13:50:44 +0000 (15:50 +0200)
CMakeLists.txt
main.c
switch.c [new file with mode: 0644]
switch.h [new file with mode: 0644]

index bdf8df4..260cfbe 100644 (file)
@@ -5,7 +5,7 @@ ADD_DEFINITIONS(-Os -ggdb -Wall -Werror --std=gnu99 -Wmissing-declarations)
 
 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
 
 
 SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
 
-SET(SOURCES main.c)
+SET(SOURCES main.c switch.c)
 
 find_package(PkgConfig)
 pkg_check_modules(LIBUSB1 REQUIRED libusb-1.0)
 
 find_package(PkgConfig)
 pkg_check_modules(LIBUSB1 REQUIRED libusb-1.0)
diff --git a/main.c b/main.c
index a837ddc..c00b5b9 100644 (file)
--- a/main.c
+++ b/main.c
@@ -5,7 +5,7 @@
 #include <libubox/blobmsg_json.h>
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
 #include <libubox/blobmsg_json.h>
 #include <libubox/avl.h>
 #include <libubox/avl-cmp.h>
-#include <libusb.h>
+#include "switch.h"
 
 #define DEFAULT_CONFIG "/etc/usb-mode.json"
 
 
 #define DEFAULT_CONFIG "/etc/usb-mode.json"
 
@@ -14,15 +14,6 @@ struct device {
        struct blob_attr *data;
 };
 
        struct blob_attr *data;
 };
 
-struct usbdev_data {
-       struct libusb_device_descriptor desc;
-       libusb_device_handle *devh;
-       struct blob_attr *info;
-
-       char idstr[10];
-       char mfg[128], prod[128], serial[128];
-};
-
 static int verbose = 0;
 static const char *config_file = DEFAULT_CONFIG;
 static struct blob_buf conf;
 static int verbose = 0;
 static const char *config_file = DEFAULT_CONFIG;
 static struct blob_buf conf;
@@ -176,124 +167,6 @@ static void handle_list(struct usbdev_data *data)
                data->idstr, data->mfg, data->prod, data->serial);
 }
 
                data->idstr, data->mfg, data->prod, data->serial);
 }
 
-enum {
-       DATA_MODE,
-       DATA_MSG,
-       DATA_MSG2,
-       DATA_MSG3,
-       __DATA_MAX
-};
-
-static void handle_generic(struct usbdev_data *data, struct blob_attr **tb)
-{
-       fprintf(stderr, "Do generic switch!\n");
-}
-
-static void handle_huawei(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_sierra(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_sony(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_qisda(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_gct(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_kobil(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_sequans(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_mobile_action(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-static void handle_cisco(struct usbdev_data *data, struct blob_attr **tb)
-{
-       /* TODO */
-}
-
-enum {
-       MODE_GENERIC,
-       MODE_HUAWEI,
-       MODE_SIERRA,
-       MODE_SONY,
-       MODE_QISDA,
-       MODE_GCT,
-       MODE_KOBIL,
-       MODE_SEQUANS,
-       MODE_MOBILE_ACTION,
-       MODE_CISCO,
-       __MODE_MAX
-};
-
-static const struct {
-       const char *name;
-       void (*cb)(struct usbdev_data *data, struct blob_attr **tb);
-} modeswitch_cb[__MODE_MAX] = {
-       [MODE_GENERIC] = { "Generic", handle_generic },
-       [MODE_HUAWEI] = { "Huawei", handle_huawei },
-       [MODE_SIERRA] = { "Sierra", handle_sierra },
-       [MODE_SONY] = { "Sony", handle_sony },
-       [MODE_QISDA] = { "Qisda", handle_qisda },
-       [MODE_GCT] = { "GCT", handle_gct },
-       [MODE_KOBIL] = { "Kobil", handle_kobil },
-       [MODE_SEQUANS] = { "Sequans", handle_sequans },
-       [MODE_MOBILE_ACTION] = { "MobileAction", handle_mobile_action },
-       [MODE_CISCO] = { "Cisco", handle_cisco },
-};
-
-static void handle_switch(struct usbdev_data *data)
-{
-       static const struct blobmsg_policy data_policy[__DATA_MAX] = {
-               [DATA_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
-               [DATA_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_INT32 },
-               [DATA_MSG2] = { .name = "msg2", .type = BLOBMSG_TYPE_INT32 },
-               [DATA_MSG3] = { .name = "msg3", .type = BLOBMSG_TYPE_INT32 },
-       };
-       struct blob_attr *tb[__DATA_MAX];
-       int mode = MODE_GENERIC;
-
-       blobmsg_parse(data_policy, __DATA_MAX, tb, blobmsg_data(data->info), blobmsg_data_len(data->info));
-
-       if (tb[DATA_MODE]) {
-               const char *modestr;
-               int i;
-
-               modestr = blobmsg_data(tb[DATA_MODE]);
-               for (i = 0; i < __MODE_MAX; i++) {
-                       if (strcmp(modeswitch_cb[i].name, modestr) != 0)
-                               continue;
-
-                       mode = i;
-                       break;
-               }
-       }
-
-       modeswitch_cb[mode].cb(data, tb);
-}
-
 int main(int argc, char **argv)
 {
        cmd_cb_t cb = NULL;
 int main(int argc, char **argv)
 {
        cmd_cb_t cb = NULL;
diff --git a/switch.c b/switch.c
new file mode 100644 (file)
index 0000000..8061322
--- /dev/null
+++ b/switch.c
@@ -0,0 +1,113 @@
+#include "switch.h"
+
+static void handle_generic(struct usbdev_data *data, struct blob_attr **tb)
+{
+       fprintf(stderr, "Do generic switch!\n");
+}
+
+static void handle_huawei(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_sierra(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_sony(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_qisda(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_gct(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_kobil(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_sequans(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_mobile_action(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+static void handle_cisco(struct usbdev_data *data, struct blob_attr **tb)
+{
+       /* TODO */
+}
+
+enum {
+       MODE_GENERIC,
+       MODE_HUAWEI,
+       MODE_SIERRA,
+       MODE_SONY,
+       MODE_QISDA,
+       MODE_GCT,
+       MODE_KOBIL,
+       MODE_SEQUANS,
+       MODE_MOBILE_ACTION,
+       MODE_CISCO,
+       __MODE_MAX
+};
+
+static const struct {
+       const char *name;
+       void (*cb)(struct usbdev_data *data, struct blob_attr **tb);
+} modeswitch_cb[__MODE_MAX] = {
+       [MODE_GENERIC] = { "Generic", handle_generic },
+       [MODE_HUAWEI] = { "Huawei", handle_huawei },
+       [MODE_SIERRA] = { "Sierra", handle_sierra },
+       [MODE_SONY] = { "Sony", handle_sony },
+       [MODE_QISDA] = { "Qisda", handle_qisda },
+       [MODE_GCT] = { "GCT", handle_gct },
+       [MODE_KOBIL] = { "Kobil", handle_kobil },
+       [MODE_SEQUANS] = { "Sequans", handle_sequans },
+       [MODE_MOBILE_ACTION] = { "MobileAction", handle_mobile_action },
+       [MODE_CISCO] = { "Cisco", handle_cisco },
+};
+
+void handle_switch(struct usbdev_data *data)
+{
+       static const struct blobmsg_policy data_policy[__DATA_MAX] = {
+               [DATA_MODE] = { .name = "mode", .type = BLOBMSG_TYPE_STRING },
+               [DATA_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_INT32 },
+               [DATA_MSG2] = { .name = "msg2", .type = BLOBMSG_TYPE_INT32 },
+               [DATA_MSG3] = { .name = "msg3", .type = BLOBMSG_TYPE_INT32 },
+       };
+       struct blob_attr *tb[__DATA_MAX];
+       int mode = MODE_GENERIC;
+
+       blobmsg_parse(data_policy, __DATA_MAX, tb, blobmsg_data(data->info), blobmsg_data_len(data->info));
+
+       if (tb[DATA_MODE]) {
+               const char *modestr;
+               int i;
+
+               modestr = blobmsg_data(tb[DATA_MODE]);
+               for (i = 0; i < __MODE_MAX; i++) {
+                       if (strcmp(modeswitch_cb[i].name, modestr) != 0)
+                               continue;
+
+                       mode = i;
+                       break;
+               }
+       }
+
+       modeswitch_cb[mode].cb(data, tb);
+}
+
+
diff --git a/switch.h b/switch.h
new file mode 100644 (file)
index 0000000..eda94c2
--- /dev/null
+++ b/switch.h
@@ -0,0 +1,26 @@
+#ifndef __USBMODE_SWITCH_H
+#define __USBMODE_SWITCH_H
+
+#include <libubox/blobmsg.h>
+#include <libusb.h>
+
+struct usbdev_data {
+       struct libusb_device_descriptor desc;
+       libusb_device_handle *devh;
+       struct blob_attr *info;
+
+       char idstr[10];
+       char mfg[128], prod[128], serial[128];
+};
+
+enum {
+       DATA_MODE,
+       DATA_MSG,
+       DATA_MSG2,
+       DATA_MSG3,
+       __DATA_MAX
+};
+
+void handle_switch(struct usbdev_data *data);
+
+#endif