Debian Codegraph @

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

raw symbols SCIP
1 /* lc15bts - 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 "lc15bts_par.h"
41
42 const struct value_string lc15bts_par_names[_NUM_LC15BTS_PAR+1] = {
43 { LC15BTS_PAR_TEMP_SUPPLY_MAX, "temp-supply-max" },
44 { LC15BTS_PAR_TEMP_SOC_MAX, "temp-soc-max" },
45 { LC15BTS_PAR_TEMP_FPGA_MAX, "temp-fpga-max" },
46 { LC15BTS_PAR_TEMP_RMSDET_MAX, "temp-rmsdet-max" },
47 { LC15BTS_PAR_TEMP_OCXO_MAX, "temp-ocxo-max" },
48 { LC15BTS_PAR_TEMP_TX0_MAX, "temp-tx0-max" },
49 { LC15BTS_PAR_TEMP_TX1_MAX, "temp-tx1-max" },
50 { LC15BTS_PAR_TEMP_PA0_MAX, "temp-pa0-max" },
51 { LC15BTS_PAR_TEMP_PA1_MAX, "temp-pa1-max" },
52 { LC15BTS_PAR_VOLT_SUPPLY_MAX, "volt-supply-max" },
53 { LC15BTS_PAR_PWR_SUPPLY_MAX, "pwr-supply-max" },
54 { LC15BTS_PAR_PWR_PA0_MAX, "pwr-pa0-max" },
55 { LC15BTS_PAR_PWR_PA1_MAX, "pwr-pa1-max" },
56 { LC15BTS_PAR_VSWR_TX0_MAX, "vswr-tx0-max" },
57 { LC15BTS_PAR_VSWR_TX1_MAX, "vswr-tx1-max" },
58 { LC15BTS_PAR_GPS_FIX, "gps-fix" },
59 { LC15BTS_PAR_SERNR, "serial-nr" },
60 { LC15BTS_PAR_HOURS, "hours-running" },
61 { LC15BTS_PAR_BOOTS, "boot-count" },
62 { LC15BTS_PAR_KEY, "key" },
63 { 0, NULL }
64 };
65
66 int lc15bts_par_is_int(enum lc15bts_par par)
67 {
68 switch (par) {
69 case LC15BTS_PAR_TEMP_SUPPLY_MAX:
70 case LC15BTS_PAR_TEMP_SOC_MAX:
71 case LC15BTS_PAR_TEMP_FPGA_MAX:
72 case LC15BTS_PAR_TEMP_RMSDET_MAX:
73 case LC15BTS_PAR_TEMP_OCXO_MAX:
74 case LC15BTS_PAR_TEMP_TX0_MAX:
75 case LC15BTS_PAR_TEMP_TX1_MAX:
76 case LC15BTS_PAR_TEMP_PA0_MAX:
77 case LC15BTS_PAR_TEMP_PA1_MAX:
78 case LC15BTS_PAR_VOLT_SUPPLY_MAX:
79 case LC15BTS_PAR_VSWR_TX0_MAX:
80 case LC15BTS_PAR_VSWR_TX1_MAX:
81 case LC15BTS_PAR_SERNR:
82 case LC15BTS_PAR_HOURS:
83 case LC15BTS_PAR_BOOTS:
84 case LC15BTS_PAR_PWR_SUPPLY_MAX:
85 case LC15BTS_PAR_PWR_PA0_MAX:
86 case LC15BTS_PAR_PWR_PA1_MAX:
87 return 1;
88 default:
89 return 0;
90 }
91 }
92
93 FILE *lc15bts_par_get_path(void *ctx, enum lc15bts_par par, const char* mode)
94 {
95 char *fpath;
96 FILE *fp;
97
98 if (par >= _NUM_LC15BTS_PAR)
99 return NULL;
100
101 fpath = talloc_asprintf(ctx, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
102 if (!fpath)
103 return NULL;
104
105 fp = fopen(fpath, mode);
106 if (!fp)
107 fprintf(stderr, "Failed to open %s due to '%s' error\n", fpath, strerror(errno));
108
109 talloc_free(fpath);
110
111 return fp;
112 }
113
114 int lc15bts_par_get_int(void *ctx, enum lc15bts_par par, int *ret)
115 {
116 FILE *fp = lc15bts_par_get_path(ctx, par, "r");
117 int rc;
118
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 lc15bts_par_set_int(void *ctx, enum lc15bts_par par, int val)
133 {
134 FILE *fp = lc15bts_par_get_path(ctx, par, "w");
135 int rc;
136
137 if (fp == NULL) {
138 return -errno;
139 }
140
141 rc = fprintf(fp, "%d", val);
142 if (rc < 0) {
143 fclose(fp);
144 return -EIO;
145 }
146
147 fsync(fileno(fp));
148 fclose(fp);
149 return 0;
150 }
151
152 int lc15bts_par_get_buf(void *ctx, enum lc15bts_par par, uint8_t *buf, unsigned int size)
153 {
154 FILE *fp = lc15bts_par_get_path(ctx, par, "rb");
155 int rc;
156
157 if (fp == NULL) {
158 return -errno;
159 }
160
161 rc = fread(buf, 1, size, fp);
162
163 fclose(fp);
164
165 return rc;
166 }
167
168 int lc15bts_par_set_buf(void *ctx, enum lc15bts_par par, const uint8_t *buf, unsigned int size)
169 {
170 FILE *fp = lc15bts_par_get_path(ctx, par, "wb");
171 int rc;
172
173 if (fp == NULL) {
174 return -errno;
175 }
176
177 rc = fwrite(buf, 1, size, fp);
178
179 fsync(fileno(fp));
180 fclose(fp);
181
182 return rc;
183 }
184
185 int lc15bts_par_get_gps_fix(time_t *ret)
186 {
187 char fpath[PATH_MAX];
188 FILE *fp;
189 int rc;
190
191 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, LC15BTS_PAR_GPS_FIX));
192 fpath[sizeof(fpath)-1] = '\0';
193
194 fp = fopen(fpath, "r");
195 if (fp == NULL) {
196 return -errno;
197 }
198
199 rc = fscanf(fp, "%lld", (long long *)ret);
200 if (rc != 1) {
201 fclose(fp);
202 return -EIO;
203 }
204 fclose(fp);
205
206 return 0;
207 }
208
209 int lc15bts_par_set_gps_fix(time_t val)
210 {
211 char fpath[PATH_MAX];
212 FILE *fp;
213 int rc;
214
215 snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, LC15BTS_PAR_GPS_FIX));
216 fpath[sizeof(fpath)-1] = '\0';
217
218 fp = fopen(fpath, "w");
219 if (fp == NULL) {
220 return -errno;
221 }
222
223 rc = fprintf(fp, "%lld", (long long)val);
224 if (rc < 0) {
225 fclose(fp);
226 return -EIO;
227 }
228 fsync(fileno(fp));
229 fclose(fp);
230
231 return 0;
232 }