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