| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
} |