Debian Codegraph @

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

raw
1 /* oc2gbts - access to hardware related parameters */
2
3 /* Copyright (C) 2015 by Yves Godin <[email protected]>
4 *
5 * Based on sysmoBTS:
6 * sysmobts_par.c
7 * (C) 2012 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
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <limits.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36
37 #include <osmocom/core/utils.h>
38 #include <osmocom/core/talloc.h>
39
40 #include "oc2gbts_par.h"
41
42 const struct value_string oc2gbts_par_names[_NUM_OC2GBTS_PAR+1] = {
43 { OC2GBTS_PAR_TEMP_SUPPLY_MAX, "temp-supply-max" },
44 { OC2GBTS_PAR_TEMP_SOC_MAX, "temp-soc-max" },
45 { OC2GBTS_PAR_TEMP_FPGA_MAX, "temp-fpga-max" },
46 { OC2GBTS_PAR_TEMP_RMSDET_MAX, "temp-rmsdet-max" },
47 { OC2GBTS_PAR_TEMP_OCXO_MAX, "temp-ocxo-max" },
48 { OC2GBTS_PAR_TEMP_TX_MAX, "temp-tx-max" },
49 { OC2GBTS_PAR_TEMP_PA_MAX, "temp-pa-max" },
50 { OC2GBTS_PAR_VOLT_SUPPLY_MAX, "volt-supply-max" },
51 { OC2GBTS_PAR_PWR_SUPPLY_MAX, "pwr-supply-max" },
52 { OC2GBTS_PAR_PWR_PA_MAX, "pwr-pa-max" },
53 { OC2GBTS_PAR_VSWR_MAX, "vswr-max" },
54 { OC2GBTS_PAR_GPS_FIX, "gps-fix" },
55 { OC2GBTS_PAR_SERNR, "serial-nr" },
56 { OC2GBTS_PAR_HOURS, "hours-running" },
57 { OC2GBTS_PAR_BOOTS, "boot-count" },
58 { OC2GBTS_PAR_KEY, "key" },
59 { 0, NULL }
60 };
61
62 int oc2gbts_par_is_int(enum oc2gbts_par par)
63 {
64 switch (par) {
65 case OC2GBTS_PAR_TEMP_SUPPLY_MAX:
66 case OC2GBTS_PAR_TEMP_SOC_MAX:
67 case OC2GBTS_PAR_TEMP_FPGA_MAX:
68 case OC2GBTS_PAR_TEMP_RMSDET_MAX:
69 case OC2GBTS_PAR_TEMP_OCXO_MAX:
70 case OC2GBTS_PAR_TEMP_TX_MAX:
71 case OC2GBTS_PAR_TEMP_PA_MAX:
72 case OC2GBTS_PAR_VOLT_SUPPLY_MAX:
73 case OC2GBTS_PAR_VSWR_MAX:
74 case OC2GBTS_PAR_SERNR:
75 case OC2GBTS_PAR_HOURS:
76 case OC2GBTS_PAR_BOOTS:
77 case OC2GBTS_PAR_PWR_SUPPLY_MAX:
78 case OC2GBTS_PAR_PWR_PA_MAX:
79 return 1;
80 default:
81 return 0;
82 }
83 }
84
85 FILE *oc2gbts_par_get_path(void *ctx, enum oc2gbts_par par, const char* mode)
86 {
87 char *fpath;
88 FILE *fp;
89
90 if (par >= _NUM_OC2GBTS_PAR)
91 return NULL;
92
93 fpath = talloc_asprintf(ctx, "%s/%s", USER_ROM_PATH, get_value_string(oc2gbts_par_names, par));
94 if (!fpath)
95 return NULL;
96
97 fp = fopen(fpath, mode);
98 if (!fp)
99 fprintf(stderr, "Failed to open %s due to '%s' error\n", fpath, strerror(errno));
100
101 talloc_free(fpath);
102
103 return fp;
104 }
105
106 int oc2gbts_par_get_int(enum oc2gbts_par par, int *ret)
107 {
108 char fpath[PATH_MAX];
109 FILE *fp;
110 int rc;
111
112 if (par >= _NUM_OC2GBTS_PAR)
113 return -ENODEV;
114
115 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(oc2gbts_par_names, par));
116 fpath[sizeof(fpath)-1] = '\0';
117
118 fp = fopen(fpath, "r");
119 if (fp == NULL) {
120 return -errno;
121 }
122
123 rc = fscanf(fp, "%d", ret);
124 if (rc != 1) {
125 fclose(fp);
126 return -EIO;
127 }
128 fclose(fp);
129 return 0;
130 }
131
132 int oc2gbts_par_set_int(enum oc2gbts_par par, int val)
133 {
134 char fpath[PATH_MAX];
135 FILE *fp;
136 int rc;
137
138 if (par >= _NUM_OC2GBTS_PAR)
139 return -ENODEV;
140
141 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(oc2gbts_par_names, par));
142 fpath[sizeof(fpath)-1] = '\0';
143
144 fp = fopen(fpath, "w");
145 if (fp == NULL) {
146 return -errno;
147 }
148
149 rc = fprintf(fp, "%d", val);
150 if (rc < 0) {
151 fclose(fp);
152 return -EIO;
153 }
154 fsync(fileno(fp));
155 fclose(fp);
156 return 0;
157 }
158
159 int oc2gbts_par_get_buf(enum oc2gbts_par par, uint8_t *buf,
160 unsigned int size)
161 {
162 char fpath[PATH_MAX];
163 FILE *fp;
164 int rc;
165
166 if (par >= _NUM_OC2GBTS_PAR)
167 return -ENODEV;
168
169 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(oc2gbts_par_names, par));
170 fpath[sizeof(fpath)-1] = '\0';
171
172 fp = fopen(fpath, "rb");
173 if (fp == NULL) {
174 return -errno;
175 }
176
177 rc = fread(buf, 1, size, fp);
178
179 fclose(fp);
180
181 return rc;
182 }
183
184 int oc2gbts_par_set_buf(enum oc2gbts_par par, const uint8_t *buf,
185 unsigned int size)
186 {
187 char fpath[PATH_MAX];
188 FILE *fp;
189 int rc;
190
191 if (par >= _NUM_OC2GBTS_PAR)
192 return -ENODEV;
193
194 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(oc2gbts_par_names, par));
195 fpath[sizeof(fpath)-1] = '\0';
196
197 fp = fopen(fpath, "wb");
198 if (fp == NULL) {
199 return -errno;
200 }
201
202 rc = fwrite(buf, 1, size, fp);
203
204 fsync(fileno(fp));
205 fclose(fp);
206
207 return rc;
208 }
209
210 int oc2gbts_par_get_gps_fix(void *ctx, time_t *ret)
211 {
212 FILE *fp;
213 int rc;
214
215 fp = oc2gbts_par_get_path(ctx, OC2GBTS_PAR_GPS_FIX, "r");
216 if (fp == NULL) {
217 return -errno;
218 }
219
220 rc = fscanf(fp, "%ld", ret);
221 if (rc != 1) {
222 fclose(fp);
223 return -EIO;
224 }
225 fclose(fp);
226
227 return 0;
228 }
229
230 int oc2gbts_par_set_gps_fix(void *ctx, time_t val)
231 {
232 FILE *fp;
233 int rc;
234
235 fp = oc2gbts_par_get_path(ctx, OC2GBTS_PAR_GPS_FIX, "w");
236 if (fp == NULL) {
237 return -errno;
238 }
239
240 rc = fprintf(fp, "%ld", val);
241 if (rc < 0) {
242 fclose(fp);
243 return -EIO;
244 }
245 fsync(fileno(fp));
246 fclose(fp);
247
248 return 0;
249 }