Debian Codegraph @

src/osmo-bts-oc2g/misc/oc2gbts_util.c

raw
1 /* oc2gbts-util - access to hardware related parameters */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_misc.c
7 * (C) 2012-2013 by Harald Welte <[email protected]>
8 *
9 * All Rights Reserved
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Affero General Public License for more details.
20 *
21 * You should have received a copy of the GNU Affero General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <getopt.h>
31
32
33 #include "oc2gbts_par.h"
34
35 enum act {
36 ACT_GET,
37 ACT_SET,
38 };
39
40 static enum act action;
41 static char *write_arg;
42 static int void_warranty;
43
44 static void print_help()
45 {
46 const struct value_string *par = oc2gbts_par_names;
47
48 printf("oc2gbts-util [--void-warranty -r | -w value] param_name\n");
49 printf("Possible param names:\n");
50
51 for (; par->str != NULL; par += 1) {
52 if (!oc2gbts_par_is_int(par->value))
53 continue;
54 printf(" %s\n", par->str);
55 }
56 }
57
58 static int parse_options(int argc, char **argv)
59 {
60 while (1) {
61 int option_idx = 0, c;
62 static const struct option long_options[] = {
63 { "help", 0, 0, 'h' },
64 { "read", 0, 0, 'r' },
65 { "void-warranty", 0, 0, 1000},
66 { "write", 1, 0, 'w' },
67 { 0, 0, 0, 0 }
68 };
69
70 c = getopt_long(argc, argv, "rw:h",
71 long_options, &option_idx);
72 if (c == -1)
73 break;
74 switch (c) {
75 case 'r':
76 action = ACT_GET;
77 break;
78 case 'w':
79 action = ACT_SET;
80 write_arg = optarg;
81 break;
82 case 'h':
83 print_help();
84 return -1;
85 break;
86 case 1000:
87 printf("Will void warranty on write.\n");
88 void_warranty = 1;
89 break;
90 default:
91 return -1;
92 }
93 }
94
95 return 0;
96 }
97
98 int main(int argc, char **argv)
99 {
100 const char *parname;
101 enum oc2gbts_par par;
102 int rc, val;
103
104 rc = parse_options(argc, argv);
105 if (rc < 0)
106 exit(2);
107
108 if (optind >= argc) {
109 fprintf(stderr, "You must specify the parameter name\n");
110 exit(2);
111 }
112 parname = argv[optind];
113
114 rc = get_string_value(oc2gbts_par_names, parname);
115 if (rc < 0) {
116 fprintf(stderr, "`%s' is not a valid parameter\n", parname);
117 exit(2);
118 } else
119 par = rc;
120
121 switch (action) {
122 case ACT_GET:
123 rc = oc2gbts_par_get_int(par, &val);
124 if (rc < 0) {
125 fprintf(stderr, "Error %d\n", rc);
126 goto err;
127 }
128 printf("%d\n", val);
129 break;
130 case ACT_SET:
131 rc = oc2gbts_par_get_int(par, &val);
132 if (rc < 0) {
133 fprintf(stderr, "Error %d\n", rc);
134 goto err;
135 }
136 if (val != 0xFFFF && val != 0xFF && val != 0xFFFFFFFF && !void_warranty) {
137 fprintf(stderr, "Parameter is already set!\r\n");
138 goto err;
139 }
140 rc = oc2gbts_par_set_int(par, atoi(write_arg));
141 if (rc < 0) {
142 fprintf(stderr, "Error %d\n", rc);
143 goto err;
144 }
145 printf("Success setting %s=%d\n", parname,
146 atoi(write_arg));
147 break;
148 default:
149 fprintf(stderr, "Unsupported action\n");
150 goto err;
151 }
152
153 exit(0);
154
155 err:
156 exit(1);
157 }
158