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