get rid of unused variables
[project/uqmi.git] / main.c
1 #include <libubox/uloop.h>
2 #include <libubox/utils.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <getopt.h>
9 #include <signal.h>
10
11 #include "uqmi.h"
12 #include "commands.h"
13
14 static const char *device;
15
16 #define CMD_OPT(_arg) (-2 - _arg)
17
18 #define __uqmi_command(_name, _optname, _arg, _option) { #_optname, _arg##_argument, NULL, CMD_OPT(__UQMI_COMMAND_##_name) }
19 static const struct option uqmi_getopt[] = {
20         __uqmi_commands,
21         { "device", required_argument, NULL, 'd' },
22         { "keep-client-id", required_argument, NULL, 'K' },
23         { NULL, 0, NULL, 0 }
24 };
25 #undef __uqmi_command
26
27 static int usage(const char *progname)
28 {
29         fprintf(stderr, "Usage: %s <options|actions>\n"
30                 "Options:\n"
31                 "  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
32                 "  --keep-client-id <name>:          Keep Client ID for service <name>\n"
33                 "                                    (implies --keep-client-id)\n"
34                 "\n"
35                 "Services:                           dms, nas, pds, wds, wms\n"
36                 "\n"
37                 "Actions:\n"
38                 "  --get-versions:                   Get service versions\n"
39                 "  --set-client-id <name>,<id>:      Set Client ID for service <name> to <id>\n"
40                 "  --get-client-id <name>:           Connect and get Client ID for service <name>\n"
41                 "                                    (implies --keep-client-id)\n"
42                 wds_helptext
43                 dms_helptext
44                 nas_helptext
45                 wms_helptext
46                 "\n", progname);
47         return 1;
48 }
49
50 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
51 {
52         QmiService svc = qmi_service_get_by_name(optarg);
53         if (svc < 0) {
54                 fprintf(stderr, "Invalid service %s\n", optarg);
55                 exit(1);
56         }
57         qmi_service_get_client_id(qmi, svc);
58 }
59
60 static void handle_exit_signal(int signal)
61 {
62         cancel_all_requests = true;
63         uloop_end();
64 }
65
66 int main(int argc, char **argv)
67 {
68         static struct qmi_dev dev;
69         int ch;
70
71         uloop_init();
72         signal(SIGINT, handle_exit_signal);
73         signal(SIGTERM, handle_exit_signal);
74
75         while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
76                 int cmd_opt = CMD_OPT(ch);
77
78                 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
79                         uqmi_add_command(optarg, cmd_opt);
80                         continue;
81                 }
82
83                 switch(ch) {
84                 case 'k':
85                         keep_client_id(&dev, optarg);
86                         break;
87                 case 'd':
88                         device = optarg;
89                         break;
90                 default:
91                         return usage(argv[0]);
92                 }
93         }
94
95         if (!device) {
96                 fprintf(stderr, "No device given\n");
97                 return usage(argv[0]);
98         }
99
100         if (qmi_device_open(&dev, device)) {
101                 fprintf(stderr, "Failed to open device\n");
102                 return 2;
103         }
104
105         uqmi_run_commands(&dev);
106
107         qmi_device_close(&dev);
108
109         return 0;
110 }