st/prober: Refactor JSON config loading so that we print less errors when we don't need the config

This commit is contained in:
Jakob Bornecrantz 2020-05-01 18:11:27 +01:00 committed by Jakob Bornecrantz
parent 1744715a5d
commit e620fa9063
4 changed files with 63 additions and 40 deletions

View file

@ -43,30 +43,48 @@ read_content(FILE *file)
return buffer; return buffer;
} }
cJSON * void
p_json_open_or_create_main_file(void) p_json_open_or_create_main_file(struct prober *p)
{ {
char tmp[1024];
ssize_t ret =
u_file_get_path_in_config_dir("config_v0.json", tmp, sizeof(tmp));
if (ret <= 0) {
fprintf(stderr,
"ERROR:Could not load or create config file no $HOME "
"or $XDG_CONFIG_HOME env variables defined\n");
return;
}
FILE *file = u_file_open_file_in_config_dir("config_v0.json", "r"); FILE *file = u_file_open_file_in_config_dir("config_v0.json", "r");
if (file == NULL) { if (file == NULL) {
fprintf(stderr, "Could not open the file!\n"); return;
return NULL;
} }
p->json.file_loaded = true;
char *str = read_content(file); char *str = read_content(file);
fclose(file); fclose(file);
if (str == NULL) { if (str == NULL) {
fprintf(stderr, "Could not read the contents of the file!\n"); fprintf(stderr, "ERROR: Could not read the contents of '%s'!\n",
return NULL; tmp);
return;
} }
cJSON *ret = cJSON_Parse(str); // No config created, ignore.
if (ret == NULL) { if (strlen(str) == 0) {
fprintf(stderr, "Failed to parse JSON:\n%s\n#######\n", str); free(str);
return;
}
p->json.root = cJSON_Parse(str);
if (p->json.root == NULL) {
fprintf(stderr, "Failed to parse JSON in '%s':\n%s\n#######\n",
tmp, str);
fprintf(stderr, "'%s'\n", cJSON_GetErrorPtr()); fprintf(stderr, "'%s'\n", cJSON_GetErrorPtr());
} }
free(str); free(str);
return ret;
} }
static cJSON * static cJSON *
@ -128,15 +146,20 @@ get_obj_str(cJSON *json, const char *name, char *array, size_t array_size)
} }
bool bool
p_json_get_tracking_settings(cJSON *root, struct xrt_settings_tracking *s) p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s)
{ {
if (root == NULL) { if (p->json.root == NULL) {
if (p->json.file_loaded) {
fprintf(stderr, "JSON not parsed!\n");
} else {
fprintf(stderr, "No config file!\n");
}
return false; return false;
} }
cJSON *t = cJSON_GetObjectItemCaseSensitive(root, "tracking"); cJSON *t = cJSON_GetObjectItemCaseSensitive(p->json.root, "tracking");
if (t == NULL) { if (t == NULL) {
fprintf(stderr, "No tracking node!\n"); fprintf(stderr, "No tracking node\n");
return false; return false;
} }

View file

@ -328,7 +328,7 @@ initialize(struct prober *p, struct xrt_prober_entry_lists *lists)
int ret; int ret;
p->json.root = p_json_open_or_create_main_file(); p_json_open_or_create_main_file(p);
ret = collect_entries(p); ret = collect_entries(p);
if (ret != 0) { if (ret != 0) {

View file

@ -135,6 +135,9 @@ struct prober
struct struct
{ {
//! For error reporting, was it loaded but not parsed?
bool file_loaded;
cJSON *root; cJSON *root;
} json; } json;
@ -178,14 +181,14 @@ struct prober
/*! /*!
* Load the JSON config file. * Load the JSON config file.
*/ */
cJSON * void
p_json_open_or_create_main_file(void); p_json_open_or_create_main_file(struct prober *p);
/*! /*!
* Extract tracking settings from the JSON. * Extract tracking settings from the JSON.
*/ */
bool bool
p_json_get_tracking_settings(cJSON *root, struct xrt_settings_tracking *s); p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s);
/*! /*!
* Dump the given device to stdout. * Dump the given device to stdout.

View file

@ -40,8 +40,8 @@ struct p_factory
// Owning prober. // Owning prober.
struct prober *p; struct prober *p;
// Has the settings be loaded. // Have we tried to load the settings.
bool setting_ok; bool tried_settings;
// Settings for this tracking system. // Settings for this tracking system.
struct xrt_settings_tracking settings; struct xrt_settings_tracking settings;
@ -112,13 +112,23 @@ on_video_device(struct xrt_prober *xp,
static void static void
p_factory_ensure_frameserver(struct p_factory *fact) p_factory_ensure_frameserver(struct p_factory *fact)
{ {
// No settings loaded. // Already created.
if (!fact->setting_ok) { if (fact->xfs != NULL) {
return; return;
} }
// Already created. // We have already tried to load the settings.
if (fact->xfs != NULL) { if (fact->tried_settings) {
return;
}
// We have no tried the settings.
fact->tried_settings = true;
if (!p_json_get_tracking_settings(fact->p, &fact->settings)) {
fprintf(stderr,
"ERROR: Could not setup PSVR and/or PSMV tracking, see "
"above.\n");
return; return;
} }
@ -217,23 +227,21 @@ p_factory_create_tracked_psmv(struct xrt_tracking_factory *xfact,
{ {
struct p_factory *fact = p_factory(xfact); struct p_factory *fact = p_factory(xfact);
if (!fact->setting_ok) {
return -1;
}
#ifdef XRT_HAVE_OPENCV #ifdef XRT_HAVE_OPENCV
struct xrt_tracked_psmv *xtmv = NULL; struct xrt_tracked_psmv *xtmv = NULL;
p_factory_ensure_frameserver(fact); p_factory_ensure_frameserver(fact);
if (fact->num_xtmv < ARRAY_SIZE(fact->xtmv)) { if (fact->num_xtmv < ARRAY_SIZE(fact->xtmv)) {
xtmv = fact->xtmv[fact->num_xtmv++]; xtmv = fact->xtmv[fact->num_xtmv];
} }
if (xtmv == NULL) { if (xtmv == NULL) {
return -1; return -1;
} }
fact->num_xtmv++;
t_psmv_start(xtmv); t_psmv_start(xtmv);
*out_xtmv = xtmv; *out_xtmv = xtmv;
@ -250,10 +258,6 @@ p_factory_create_tracked_psvr(struct xrt_tracking_factory *xfact,
{ {
struct p_factory *fact = p_factory(xfact); struct p_factory *fact = p_factory(xfact);
if (!fact->setting_ok) {
return -1;
}
#ifdef XRT_HAVE_OPENCV #ifdef XRT_HAVE_OPENCV
struct xrt_tracked_psvr *xtvr = NULL; struct xrt_tracked_psvr *xtvr = NULL;
@ -306,13 +310,6 @@ p_tracking_init(struct prober *p)
// Finally set us as the tracking factory. // Finally set us as the tracking factory.
p->base.tracking = &fact->base; p->base.tracking = &fact->base;
fact->setting_ok =
p_json_get_tracking_settings(p->json.root, &fact->settings);
if (!fact->setting_ok) {
fprintf(stderr, "Failed to load settings!\n");
}
return 0; return 0;
} }