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