2 * uqmi -- tiny QMI support implementation
4 * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
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.
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.
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.
22 #include <libubox/uloop.h>
23 #include <libubox/utils.h>
35 static const char *device;
37 #define CMD_OPT(_arg) (-2 - _arg)
39 #define __uqmi_command(_name, _optname, _arg, _option) { #_optname, _arg##_argument, NULL, CMD_OPT(__UQMI_COMMAND_##_name) }
40 static const struct option uqmi_getopt[] = {
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' },
50 static int usage(const char *progname)
52 fprintf(stderr, "Usage: %s <options|actions>\n"
54 " --single, -s: Print output as a single line (for scripts)\n"
55 " --device=NAME, -d NAME: Set device name to NAME (required)\n"
56 " --keep-client-id <name>: Keep Client ID for service <name>\n"
57 " --release-client-id <name>: Release Client ID after exiting\n"
59 "Services: dms, nas, pds, wds, wms\n"
62 " --get-versions: Get service versions\n"
63 " --set-client-id <name>,<id>: Set Client ID for service <name> to <id>\n"
64 " (implies --keep-client-id)\n"
65 " --get-client-id <name>: Connect and get Client ID for service <name>\n"
66 " (implies --keep-client-id)\n"
76 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
78 QmiService svc = qmi_service_get_by_name(optarg);
80 fprintf(stderr, "Invalid service %s\n", optarg);
83 qmi_service_get_client_id(qmi, svc);
86 static void release_client_id(struct qmi_dev *qmi, const char *optarg)
88 QmiService svc = qmi_service_get_by_name(optarg);
90 fprintf(stderr, "Invalid service %s\n", optarg);
93 qmi_service_release_client_id(qmi, svc);
96 static void handle_exit_signal(int signal)
98 cancel_all_requests = true;
102 int main(int argc, char **argv)
104 static struct qmi_dev dev;
108 signal(SIGINT, handle_exit_signal);
109 signal(SIGTERM, handle_exit_signal);
111 while ((ch = getopt_long(argc, argv, "d:k:s", uqmi_getopt, NULL)) != -1) {
112 int cmd_opt = CMD_OPT(ch);
114 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
115 uqmi_add_command(optarg, cmd_opt);
121 release_client_id(&dev, optarg);
124 keep_client_id(&dev, optarg);
133 return usage(argv[0]);
138 fprintf(stderr, "No device given\n");
139 return usage(argv[0]);
142 if (qmi_device_open(&dev, device)) {
143 fprintf(stderr, "Failed to open device\n");
147 ret = uqmi_run_commands(&dev) ? 0 : -1;
149 qmi_device_close(&dev);