Move parameter file opening into separate function

* use talloc for file path allocation
* print detailed error on failures

This simplifies troubleshooting of lc15bts-mgr failures.

Change-Id: I86c93a2a4f080e8ac1517be93f58f6ffd00d248c
Related: SYS#3686
This commit is contained in:
Max 2017-06-15 15:34:41 +02:00
parent aa5edff477
commit d5971d0681
5 changed files with 52 additions and 61 deletions

View File

@ -116,7 +116,7 @@ static void respond_to(struct sockaddr_in *src, struct osmo_fd *fd,
}
/* fetch the serial number */
lc15bts_par_get_int(LC15BTS_PAR_SERNR, &serno);
lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_PAR_SERNR, &serno);
snprintf(ser_str, sizeof(ser_str), "%d", serno);
/* fetch the model and trx number */

View File

@ -114,12 +114,13 @@ void lc15bts_check_temp(int no_rom_write)
for (i = 0; i < ARRAY_SIZE(temp_data); i++) {
int ret;
rc = lc15bts_par_get_int(temp_data[i].ee_par, &ret);
rc = lc15bts_par_get_int(tall_mgr_ctx, temp_data[i].ee_par, &ret);
temp_old[i] = ret * 1000;
temp_cur[i] = lc15bts_temp_get(temp_data[i].sensor);
if (temp_cur[i] < 0 && temp_cur[i] > -1000) {
LOGP(DTEMP, LOGL_ERROR, "Error reading temperature (%d)\n", temp_data[i].sensor);
LOGP(DTEMP, LOGL_ERROR, "Error reading temperature (%d): unexpected value %d\n",
temp_data[i].sensor, temp_cur[i]);
continue;
}
@ -132,8 +133,7 @@ void lc15bts_check_temp(int no_rom_write)
temp_cur[i]/1000, temp_old[i]%1000);
if (!no_rom_write) {
rc = lc15bts_par_set_int(temp_data[i].ee_par,
temp_cur[i]/1000);
rc = lc15bts_par_set_int(tall_mgr_ctx, temp_data[i].ee_par, temp_cur[i]/1000);
if (rc < 0)
LOGP(DTEMP, LOGL_ERROR, "error writing new %s "
"max temp %d (%s)\n", temp_data[i].name,
@ -157,7 +157,7 @@ int lc15bts_update_hours(int no_rom_write)
if (last_update == 0) {
last_update = now;
rc = lc15bts_par_get_int(LC15BTS_PAR_HOURS, &op_hrs);
rc = lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_PAR_HOURS, &op_hrs);
if (rc < 0) {
LOGP(DTEMP, LOGL_ERROR, "Unable to read "
"operational hours: %d (%s)\n", rc,
@ -172,7 +172,7 @@ int lc15bts_update_hours(int no_rom_write)
}
if (now >= last_update + 3600) {
rc = lc15bts_par_get_int(LC15BTS_PAR_HOURS, &op_hrs);
rc = lc15bts_par_get_int(tall_mgr_ctx, LC15BTS_PAR_HOURS, &op_hrs);
if (rc < 0) {
LOGP(DTEMP, LOGL_ERROR, "Unable to read "
"operational hours: %d (%s)\n", rc,
@ -187,7 +187,7 @@ int lc15bts_update_hours(int no_rom_write)
op_hrs);
if (!no_rom_write) {
rc = lc15bts_par_set_int(LC15BTS_PAR_HOURS, op_hrs);
rc = lc15bts_par_set_int(tall_mgr_ctx, LC15BTS_PAR_HOURS, op_hrs);
if (rc < 0)
return rc;
}

View File

@ -35,13 +35,10 @@
#include <sys/stat.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/talloc.h>
#include "lc15bts_par.h"
#define FACTORY_ROM_PATH "/mnt/rom/factory"
#define USER_ROM_PATH "/mnt/rom/user"
const struct value_string lc15bts_par_names[_NUM_LC15BTS_PAR+1] = {
{ LC15BTS_PAR_TEMP_SUPPLY_MAX, "temp-supply-max" },
{ LC15BTS_PAR_TEMP_SOC_MAX, "temp-soc-max" },
@ -80,19 +77,32 @@ int lc15bts_par_is_int(enum lc15bts_par par)
}
}
int lc15bts_par_get_int(enum lc15bts_par par, int *ret)
FILE *lc15bts_par_get_path(void *ctx, enum lc15bts_par par, const char* mode)
{
char fpath[PATH_MAX];
char *fpath;
FILE *fp;
int rc;
if (par >= _NUM_LC15BTS_PAR)
return -ENODEV;
return NULL;
snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
fpath[sizeof(fpath)-1] = '\0';
fpath = talloc_asprintf(ctx, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
if (!fpath)
return NULL;
fp = fopen(fpath, mode);
if (!fp)
fprintf(stderr, "Failed to open %s due to '%s' error\n", fpath, strerror(errno));
talloc_free(fpath);
return fp;
}
int lc15bts_par_get_int(void *ctx, enum lc15bts_par par, int *ret)
{
FILE *fp = lc15bts_par_get_path(ctx, par, "r");
int rc;
fp = fopen(fpath, "r");
if (fp == NULL) {
return -errno;
}
@ -106,19 +116,11 @@ int lc15bts_par_get_int(enum lc15bts_par par, int *ret)
return 0;
}
int lc15bts_par_set_int(enum lc15bts_par par, int val)
int lc15bts_par_set_int(void *ctx, enum lc15bts_par par, int val)
{
char fpath[PATH_MAX];
FILE *fp;
FILE *fp = lc15bts_par_get_path(ctx, par, "w");
int rc;
if (par >= _NUM_LC15BTS_PAR)
return -ENODEV;
snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
fpath[sizeof(fpath)-1] = '\0';
fp = fopen(fpath, "w");
if (fp == NULL) {
return -errno;
}
@ -132,20 +134,11 @@ int lc15bts_par_set_int(enum lc15bts_par par, int val)
return 0;
}
int lc15bts_par_get_buf(enum lc15bts_par par, uint8_t *buf,
unsigned int size)
int lc15bts_par_get_buf(void *ctx, enum lc15bts_par par, uint8_t *buf, unsigned int size)
{
char fpath[PATH_MAX];
FILE *fp;
FILE *fp = lc15bts_par_get_path(ctx, par, "rb");
int rc;
if (par >= _NUM_LC15BTS_PAR)
return -ENODEV;
snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
fpath[sizeof(fpath)-1] = '\0';
fp = fopen(fpath, "rb");
if (fp == NULL) {
return -errno;
}
@ -157,20 +150,11 @@ int lc15bts_par_get_buf(enum lc15bts_par par, uint8_t *buf,
return rc;
}
int lc15bts_par_set_buf(enum lc15bts_par par, const uint8_t *buf,
unsigned int size)
int lc15bts_par_set_buf(void *ctx, enum lc15bts_par par, const uint8_t *buf, unsigned int size)
{
char fpath[PATH_MAX];
FILE *fp;
FILE *fp = lc15bts_par_get_path(ctx, par, "wb");
int rc;
if (par >= _NUM_LC15BTS_PAR)
return -ENODEV;
snprintf(fpath, sizeof(fpath)-1, "%s/%s", USER_ROM_PATH, get_value_string(lc15bts_par_names, par));
fpath[sizeof(fpath)-1] = '\0';
fp = fopen(fpath, "wb");
if (fp == NULL) {
return -errno;
}

View File

@ -3,6 +3,9 @@
#include <osmocom/core/utils.h>
#define FACTORY_ROM_PATH "/mnt/rom/factory"
#define USER_ROM_PATH "/mnt/rom/user"
enum lc15bts_par {
LC15BTS_PAR_TEMP_SUPPLY_MAX,
LC15BTS_PAR_TEMP_SOC_MAX,
@ -22,12 +25,10 @@ enum lc15bts_par {
extern const struct value_string lc15bts_par_names[_NUM_LC15BTS_PAR+1];
int lc15bts_par_get_int(enum lc15bts_par par, int *ret);
int lc15bts_par_set_int(enum lc15bts_par par, int val);
int lc15bts_par_get_buf(enum lc15bts_par par, uint8_t *buf,
unsigned int size);
int lc15bts_par_set_buf(enum lc15bts_par par, const uint8_t *buf,
unsigned int size);
int lc15bts_par_get_int(void *ctx, enum lc15bts_par par, int *ret);
int lc15bts_par_set_int(void *ctx, enum lc15bts_par par, int val);
int lc15bts_par_get_buf(void *ctx, enum lc15bts_par par, uint8_t *buf, unsigned int size);
int lc15bts_par_set_buf(void *ctx, enum lc15bts_par par, const uint8_t *buf, unsigned int size);
int lc15bts_par_is_int(enum lc15bts_par par);

View File

@ -28,10 +28,13 @@
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/msgb.h>
#include "lc15bts_par.h"
void *tall_util_ctx;
enum act {
ACT_GET,
ACT_SET,
@ -101,6 +104,9 @@ int main(int argc, char **argv)
enum lc15bts_par par;
int rc, val;
tall_util_ctx = talloc_named_const(NULL, 1, "lc15 utils");
msgb_talloc_ctx_init(tall_util_ctx, 0);
rc = parse_options(argc, argv);
if (rc < 0)
exit(2);
@ -120,7 +126,7 @@ int main(int argc, char **argv)
switch (action) {
case ACT_GET:
rc = lc15bts_par_get_int(par, &val);
rc = lc15bts_par_get_int(tall_util_ctx, par, &val);
if (rc < 0) {
fprintf(stderr, "Error %d\n", rc);
goto err;
@ -128,7 +134,7 @@ int main(int argc, char **argv)
printf("%d\n", val);
break;
case ACT_SET:
rc = lc15bts_par_get_int(par, &val);
rc = lc15bts_par_get_int(tall_util_ctx, par, &val);
if (rc < 0) {
fprintf(stderr, "Error %d\n", rc);
goto err;
@ -137,7 +143,7 @@ int main(int argc, char **argv)
fprintf(stderr, "Parameter is already set!\r\n");
goto err;
}
rc = lc15bts_par_set_int(par, atoi(write_arg));
rc = lc15bts_par_set_int(tall_util_ctx, par, atoi(write_arg));
if (rc < 0) {
fprintf(stderr, "Error %d\n", rc);
goto err;