Debian Codegraph @

src/osmo-bts-lc15/misc/lc15bts_util.c

raw symbols SCIP
1 /* lc15bts-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 #include <osmocom/core/talloc.h>
32 #include <osmocom/core/msgb.h>
33
34 #include "lc15bts_par.h"
35
36 void *tall_util_ctx;
37
38 enum act {
39 ACT_GET,
40 ACT_SET,
41 };
42
43 static enum act action;
44 static char *write_arg;
45 static int void_warranty;
46
47 static void print_help()
48 {
49 const struct value_string *par = lc15bts_par_names;
50
51 printf("lc15bts-util [--void-warranty -r | -w value] param_name\n");
52 printf("Possible param names:\n");
53
54 for (; par->str != NULL; par += 1) {
55 if (!lc15bts_par_is_int(par->value))
56 continue;
57 printf(" %s\n", par->str);
58 }
59 }
60
61 static int parse_options(int argc, char **argv)
62 {
63 while (1) {
64 int option_idx = 0, c;
65 static const struct option long_options[] = {
66 { "help", 0, 0, 'h' },
67 { "read", 0, 0, 'r' },
68 { "void-warranty", 0, 0, 1000},
69 { "write", 1, 0, 'w' },
70 { 0, 0, 0, 0 }
71 };
72
73 c = getopt_long(argc, argv, "rw:h",
74 long_options, &option_idx);
75 if (c == -1)
76 break;
77 switch (c) {
78 case 'r':
79 action = ACT_GET;
80 break;
81 case 'w':
82 action = ACT_SET;
83 write_arg = optarg;
84 break;
85 case 'h':
86 print_help();
87 return -1;
88 break;
89 case 1000:
90 printf("Will void warranty on write.\n");
91 void_warranty = 1;
92 break;
93 default:
94 return -1;
95 }
96 }
97
98 return 0;
99 }
100
101 int main(int argc, char **argv)
102 {
103 const char *parname;
104 enum lc15bts_par par;
105 int rc, val;
106
107 tall_util_ctx = talloc_named_const(NULL, 1, "lc15 utils");
108 msgb_talloc_ctx_init(tall_util_ctx, 0);
109
110 rc = parse_options(argc, argv);
111 if (rc < 0)
112 exit(2);
113
114 if (optind >= argc) {
115 fprintf(stderr, "You must specify the parameter name\n");
116 exit(2);
117 }
118 parname = argv[optind];
119
120 rc = get_string_value(lc15bts_par_names, parname);
121 if (rc < 0) {
122 fprintf(stderr, "`%s' is not a valid parameter\n", parname);
123 exit(2);
124 } else
125 par = rc;
126
127 switch (action) {
128 case ACT_GET:
129 rc = lc15bts_par_get_int(tall_util_ctx, par, &val);
130 if (rc < 0) {
131 fprintf(stderr, "Error %d\n", rc);
132 goto err;
133 }
134 printf("%d\n", val);
135 break;
136 case ACT_SET:
137 rc = lc15bts_par_get_int(tall_util_ctx, par, &val);
138 if (rc < 0) {
139 fprintf(stderr, "Error %d\n", rc);
140 goto err;
141 }
142 if (val != 0xFFFF && val != 0xFF && val != 0xFFFFFFFF && !void_warranty) {
143 fprintf(stderr, "Parameter is already set!\r\n");
144 goto err;
145 }
146 rc = lc15bts_par_set_int(tall_util_ctx, par, atoi(write_arg));
147 if (rc < 0) {
148 fprintf(stderr, "Error %d\n", rc);
149 goto err;
150 }
151 printf("Success setting %s=%d\n", parname,
152 atoi(write_arg));
153 break;
154 default:
155 fprintf(stderr, "Unsupported action\n");
156 goto err;
157 }
158
159 exit(0);
160
161 err:
162 exit(1);
163 }
164