uqmi: print radio interfaces in serving system command
[project/uqmi.git] / main.c
1 /*
2 * uqmi -- tiny QMI support implementation
3 *
4 * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
20 */
21
22 #include <libubox/uloop.h>
23 #include <libubox/utils.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <getopt.h>
30 #include <signal.h>
31
32 #include "uqmi.h"
33 #include "commands.h"
34
35 static const char *device;
36
37 #define CMD_OPT(_arg) (-2 - _arg)
38
39 #define __uqmi_command(_name, _optname, _arg, _option) { #_optname, _arg##_argument, NULL, CMD_OPT(__UQMI_COMMAND_##_name) }
40 static const struct option uqmi_getopt[] = {
41 __uqmi_commands,
42 { "single", no_argument, NULL, 's' },
43 { "device", required_argument, NULL, 'd' },
44 { "keep-client-id", required_argument, NULL, 'k' },
45 { "release-client-id", required_argument, NULL, 'r' },
46 { "mbim", no_argument, NULL, 'm' },
47 { NULL, 0, NULL, 0 }
48 };
49 #undef __uqmi_command
50
51 static int usage(const char *progname)
52 {
53 fprintf(stderr, "Usage: %s <options|actions>\n"
54 "Options:\n"
55 " --single, -s: Print output as a single line (for scripts)\n"
56 " --device=NAME, -d NAME: Set device name to NAME (required)\n"
57 " --keep-client-id <name>: Keep Client ID for service <name>\n"
58 " --release-client-id <name>: Release Client ID after exiting\n"
59 " --mbim, -m NAME is an MBIM device with EXT_QMUX support\n"
60 "\n"
61 "Services: dms, nas, pds, wds, wms\n"
62 "\n"
63 "Actions:\n"
64 " --get-versions: Get service versions\n"
65 " --set-client-id <name>,<id>: Set Client ID for service <name> to <id>\n"
66 " (implies --keep-client-id)\n"
67 " --get-client-id <name>: Connect and get Client ID for service <name>\n"
68 " (implies --keep-client-id)\n"
69 " --sync: Release all Client IDs\n"
70 wds_helptext
71 dms_helptext
72 uim_helptext
73 nas_helptext
74 wms_helptext
75 wda_helptext
76 "\n", progname);
77 return 1;
78 }
79
80 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
81 {
82 QmiService svc = qmi_service_get_by_name(optarg);
83 if (svc < 0) {
84 fprintf(stderr, "Invalid service %s\n", optarg);
85 exit(1);
86 }
87 qmi_service_get_client_id(qmi, svc);
88 }
89
90 static void release_client_id(struct qmi_dev *qmi, const char *optarg)
91 {
92 QmiService svc = qmi_service_get_by_name(optarg);
93 if (svc < 0) {
94 fprintf(stderr, "Invalid service %s\n", optarg);
95 exit(1);
96 }
97 qmi_service_release_client_id(qmi, svc);
98 }
99
100 static void handle_exit_signal(int signal)
101 {
102 cancel_all_requests = true;
103 uloop_end();
104 }
105
106 int main(int argc, char **argv)
107 {
108 static struct qmi_dev dev;
109 int ch, ret;
110
111 uloop_init();
112 signal(SIGINT, handle_exit_signal);
113 signal(SIGTERM, handle_exit_signal);
114
115 while ((ch = getopt_long(argc, argv, "d:k:sm", uqmi_getopt, NULL)) != -1) {
116 int cmd_opt = CMD_OPT(ch);
117
118 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
119 uqmi_add_command(optarg, cmd_opt);
120 continue;
121 }
122
123 switch(ch) {
124 case 'r':
125 release_client_id(&dev, optarg);
126 break;
127 case 'k':
128 keep_client_id(&dev, optarg);
129 break;
130 case 'd':
131 device = optarg;
132 break;
133 case 's':
134 single_line = true;
135 break;
136 case 'm':
137 dev.is_mbim = true;
138 break;
139 default:
140 return usage(argv[0]);
141 }
142 }
143
144 if (!device) {
145 fprintf(stderr, "No device given\n");
146 return usage(argv[0]);
147 }
148
149 if (qmi_device_open(&dev, device)) {
150 fprintf(stderr, "Failed to open device\n");
151 return 2;
152 }
153
154 ret = uqmi_run_commands(&dev) ? 0 : -1;
155
156 qmi_device_close(&dev);
157
158 return ret;
159 }