• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/uqmi/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         { "timeout", required_argument, NULL, 't' },
 48         { NULL, 0, NULL, 0 }
 49 };
 50 #undef __uqmi_command
 51 
 52 static int usage(const char *progname)
 53 {
 54         fprintf(stderr, "Usage: %s <options|actions>\n"
 55                 "Options:\n"
 56                 "  --single, -s:                     Print output as a single line (for scripts)\n"
 57                 "  --device=NAME, -d NAME:           Set device name to NAME (required)\n"
 58                 "  --keep-client-id <name>:          Keep Client ID for service <name>\n"
 59                 "  --release-client-id <name>:       Release Client ID after exiting\n"
 60                 "  --mbim, -m                        NAME is an MBIM device with EXT_QMUX support\n"
 61                 "  --timeout, -t                     response timeout in msecs\n"
 62                 "\n"
 63                 "Services:                           dms, nas, pds, wds, wms\n"
 64                 "\n"
 65                 "Actions:\n"
 66                 "  --get-versions:                   Get service versions\n"
 67                 "  --set-client-id <name>,<id>:      Set Client ID for service <name> to <id>\n"
 68                 "                                    (implies --keep-client-id)\n"
 69                 "  --get-client-id <name>:           Connect and get Client ID for service <name>\n"
 70                 "                                    (implies --keep-client-id)\n"
 71                 "  --sync:                           Release all Client IDs\n"
 72                 wds_helptext
 73                 dms_helptext
 74                 uim_helptext
 75                 nas_helptext
 76                 wms_helptext
 77                 wda_helptext
 78                 "\n", progname);
 79         return 1;
 80 }
 81 
 82 static void keep_client_id(struct qmi_dev *qmi, const char *optarg)
 83 {
 84         QmiService svc = qmi_service_get_by_name(optarg);
 85         if (svc < 0) {
 86                 fprintf(stderr, "Invalid service %s\n", optarg);
 87                 exit(1);
 88         }
 89         qmi_service_get_client_id(qmi, svc);
 90 }
 91 
 92 static void release_client_id(struct qmi_dev *qmi, const char *optarg)
 93 {
 94         QmiService svc = qmi_service_get_by_name(optarg);
 95         if (svc < 0) {
 96                 fprintf(stderr, "Invalid service %s\n", optarg);
 97                 exit(1);
 98         }
 99         qmi_service_release_client_id(qmi, svc);
100 }
101 
102 static void handle_exit_signal(int signal)
103 {
104         cancel_all_requests = true;
105         uloop_end();
106 }
107 
108 static void _request_timeout_handler(struct uloop_timeout *timeout)
109 {
110         fprintf(stderr, "Request timed out\n");
111         handle_exit_signal(0);
112 }
113 
114 struct uloop_timeout request_timeout = { .cb = _request_timeout_handler, };
115 
116 int main(int argc, char **argv)
117 {
118         static struct qmi_dev dev;
119         int ch, ret;
120 
121         uloop_init();
122         signal(SIGINT, handle_exit_signal);
123         signal(SIGTERM, handle_exit_signal);
124 
125         while ((ch = getopt_long(argc, argv, "d:k:smt:", uqmi_getopt, NULL)) != -1) {
126                 int cmd_opt = CMD_OPT(ch);
127 
128                 if (ch < 0 && cmd_opt >= 0 && cmd_opt < __UQMI_COMMAND_LAST) {
129                         uqmi_add_command(optarg, cmd_opt);
130                         continue;
131                 }
132 
133                 switch(ch) {
134                 case 'r':
135                         release_client_id(&dev, optarg);
136                         break;
137                 case 'k':
138                         keep_client_id(&dev, optarg);
139                         break;
140                 case 'd':
141                         device = optarg;
142                         break;
143                 case 's':
144                         single_line = true;
145                         break;
146                 case 'm':
147                         dev.is_mbim = true;
148                         break;
149                 case 't':
150                         uloop_timeout_set(&request_timeout, atol(optarg));
151                         break;
152                 default:
153                         return usage(argv[0]);
154                 }
155         }
156 
157         if (!device) {
158                 fprintf(stderr, "No device given\n");
159                 return usage(argv[0]);
160         }
161 
162         if (qmi_device_open(&dev, device)) {
163                 fprintf(stderr, "Failed to open device\n");
164                 return 2;
165         }
166 
167         ret = uqmi_run_commands(&dev) ? 0 : -1;
168 
169         qmi_device_close(&dev);
170 
171         return ret;
172 }
173 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt