remove service prefix from options, add helptext for services
[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
10 #include "uqmi.h"
11 #include "commands.h"
12
13 static const char *device;
14
15 #define CMD_OPT(_arg) (-2 - _arg)
16
17 #define __uqmi_command(_name, _optname, _arg, _option) { #_optname, _arg##_argument, NULL, CMD_OPT(__UQMI_COMMAND_##_name) }
18 static const struct option uqmi_getopt[] = {
19         __uqmi_commands,
20         { "device", required_argument, NULL, 'd' },
21         { "keep-client-id", required_argument, NULL, 'K' },
22         { NULL, 0, NULL, 0 }
23 };
24 #undef __uqmi_command
25
26 static int usage(const char *progname)
27 {
28         fprintf(stderr, "Usage: %s <options|actions>\n"
29                 "Options:\n"
30                 "  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
31                 "  --keep-client-id <name>:          Keep Client ID for service <name>\n"
32                 "                                    (implies --keep-client-id)\n"
33                 "\n"
34                 "Services:                           dms, nas, pds, wds, wms\n"
35                 "\n"
36                 "Actions:\n"
37                 "  --get-versions:                   Get service versions\n"
38                 "  --set-client-id <name>,<id>:      Set Client ID for service <name> to <id>\n"
39                 "  --get-client-id <name>:           Connect and get Client ID for service <name>\n"
40                 "                                    (implies --keep-client-id)\n"
41                 wds_helptext
42                 dms_helptext
43                 "\n", progname);
44         return 1;
45 }
46
47 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
48 {
49         QmiService svc = qmi_service_get_by_name(optarg);
50         if (svc < 0) {
51                 fprintf(stderr, "Invalid service %s\n", optarg);
52                 exit(1);
53         }
54         qmi_service_get_client_id(qmi, svc);
55 }
56
57 int main(int argc, char **argv)
58 {
59         static struct qmi_dev dev;
60         int ch;
61
62         while ((ch = getopt_long(argc, argv, "d:k:", uqmi_getopt, NULL)) != -1) {
63                 int cmd_opt = CMD_OPT(ch);
64
65                 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
66                         uqmi_add_command(optarg, cmd_opt);
67                         continue;
68                 }
69
70                 switch(ch) {
71                 case 'k':
72                         keep_client_id(&dev, optarg);
73                         break;
74                 case 'd':
75                         device = optarg;
76                         break;
77                 default:
78                         return usage(argv[0]);
79                 }
80         }
81
82         if (!device) {
83                 fprintf(stderr, "No device given\n");
84                 return usage(argv[0]);
85         }
86
87         if (qmi_device_open(&dev, device)) {
88                 fprintf(stderr, "Failed to open device\n");
89                 return 2;
90         }
91
92         uqmi_run_commands(&dev);
93
94         qmi_device_close(&dev);
95
96         return 0;
97 }