Add mandatory field to Initiate Network Register
[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         { "single", no_argument, NULL, 's' },
22         { "device", required_argument, NULL, 'd' },
23         { "keep-client-id", required_argument, NULL, 'k' },
24         { "release-client-id", required_argument, NULL, 'r' },
25         { NULL, 0, NULL, 0 }
26 };
27 #undef __uqmi_command
28
29 static int usage(const char *progname)
30 {
31         fprintf(stderr, "Usage: %s <options|actions>\n"
32                 "Options:\n"
33                 "  --single, -s:                     Print output as a single line (for scripts)\n"
34                 "  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
35                 "  --keep-client-id <name>:          Keep Client ID for service <name>\n"
36                 "  --release-client-id <name>:       Release Client ID after exiting\n"
37                 "\n"
38                 "Services:                           dms, nas, pds, wds, wms\n"
39                 "\n"
40                 "Actions:\n"
41                 "  --get-versions:                   Get service versions\n"
42                 "  --set-client-id <name>,<id>:      Set Client ID for service <name> to <id>\n"
43                 "                                    (implies --keep-client-id)\n"
44                 "  --get-client-id <name>:           Connect and get Client ID for service <name>\n"
45                 "                                    (implies --keep-client-id)\n"
46                 wds_helptext
47                 dms_helptext
48                 nas_helptext
49                 wms_helptext
50                 wda_helptext
51                 "\n", progname);
52         return 1;
53 }
54
55 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
56 {
57         QmiService svc = qmi_service_get_by_name(optarg);
58         if (svc < 0) {
59                 fprintf(stderr, "Invalid service %s\n", optarg);
60                 exit(1);
61         }
62         qmi_service_get_client_id(qmi, svc);
63 }
64
65 static void release_client_id(struct qmi_dev *qmi, const char *optarg)
66 {
67         QmiService svc = qmi_service_get_by_name(optarg);
68         if (svc < 0) {
69                 fprintf(stderr, "Invalid service %s\n", optarg);
70                 exit(1);
71         }
72         qmi_service_release_client_id(qmi, svc);
73 }
74
75 static void handle_exit_signal(int signal)
76 {
77         cancel_all_requests = true;
78         uloop_end();
79 }
80
81 int main(int argc, char **argv)
82 {
83         static struct qmi_dev dev;
84         int ch, ret;
85
86         uloop_init();
87         signal(SIGINT, handle_exit_signal);
88         signal(SIGTERM, handle_exit_signal);
89
90         while ((ch = getopt_long(argc, argv, "d:k:s", uqmi_getopt, NULL)) != -1) {
91                 int cmd_opt = CMD_OPT(ch);
92
93                 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
94                         uqmi_add_command(optarg, cmd_opt);
95                         continue;
96                 }
97
98                 switch(ch) {
99                 case 'r':
100                         release_client_id(&dev, optarg);
101                         break;
102                 case 'k':
103                         keep_client_id(&dev, optarg);
104                         break;
105                 case 'd':
106                         device = optarg;
107                         break;
108                 case 's':
109                         single_line = true;
110                         break;
111                 default:
112                         return usage(argv[0]);
113                 }
114         }
115
116         if (!device) {
117                 fprintf(stderr, "No device given\n");
118                 return usage(argv[0]);
119         }
120
121         if (qmi_device_open(&dev, device)) {
122                 fprintf(stderr, "Failed to open device\n");
123                 return 2;
124         }
125
126         ret = uqmi_run_commands(&dev) ? 0 : -1;
127
128         qmi_device_close(&dev);
129
130         return ret;
131 }