add --get-serving-system
[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                 "\n", progname);
46         return 1;
47 }
48
49 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
50 {
51         QmiService svc = qmi_service_get_by_name(optarg);
52         if (svc < 0) {
53                 fprintf(stderr, "Invalid service %s\n", optarg);
54                 exit(1);
55         }
56         qmi_service_get_client_id(qmi, svc);
57 }
58
59 static void handle_exit_signal(int signal)
60 {
61         cancel_all_requests = true;
62         uloop_end();
63 }
64
65 int main(int argc, char **argv)
66 {
67         static struct qmi_dev dev;
68         int ch;
69
70         uloop_init();
71         signal(SIGINT, handle_exit_signal);
72         signal(SIGTERM, handle_exit_signal);
73
74         while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
75                 int cmd_opt = CMD_OPT(ch);
76
77                 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
78                         uqmi_add_command(optarg, cmd_opt);
79                         continue;
80                 }
81
82                 switch(ch) {
83                 case 'k':
84                         keep_client_id(&dev, optarg);
85                         break;
86                 case 'd':
87                         device = optarg;
88                         break;
89                 default:
90                         return usage(argv[0]);
91                 }
92         }
93
94         if (!device) {
95                 fprintf(stderr, "No device given\n");
96                 return usage(argv[0]);
97         }
98
99         if (qmi_device_open(&dev, device)) {
100                 fprintf(stderr, "Failed to open device\n");
101                 return 2;
102         }
103
104         uqmi_run_commands(&dev);
105
106         qmi_device_close(&dev);
107
108         return 0;
109 }