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;
}
cJSON *
p_json_open_or_create_main_file(void)
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");
if (file == NULL) {
fprintf(stderr, "Could not open the file!\n");
return NULL;
return;
}
p->json.file_loaded = true;
char *str = read_content(file);
fclose(file);
if (str == NULL) {
fprintf(stderr, "Could not read the contents of the file!\n");
return NULL;
fprintf(stderr, "ERROR: Could not read the contents of '%s'!\n",
tmp);
return;
}
cJSON *ret = cJSON_Parse(str);
if (ret == NULL) {
fprintf(stderr, "Failed to parse JSON:\n%s\n#######\n", str);
// No config created, ignore.
if (strlen(str) == 0) {
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());
}
free(str);
return ret;
}
static cJSON *
@ -128,15 +146,20 @@ get_obj_str(cJSON *json, const char *name, char *array, size_t array_size)
}
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;
}
cJSON *t = cJSON_GetObjectItemCaseSensitive(root, "tracking");
cJSON *t = cJSON_GetObjectItemCaseSensitive(p->json.root, "tracking");
if (t == NULL) {
fprintf(stderr, "No tracking node!\n");
fprintf(stderr, "No tracking node\n");
return false;
}

View file

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

View file

@ -135,6 +135,9 @@ struct prober
struct
{
//! For error reporting, was it loaded but not parsed?
bool file_loaded;
cJSON *root;
} json;
@ -178,14 +181,14 @@ struct prober
/*!
* Load the JSON config file.
*/
cJSON *
p_json_open_or_create_main_file(void);
void
p_json_open_or_create_main_file(struct prober *p);
/*!
* Extract tracking settings from the JSON.
*/
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.

View file

@ -40,8 +40,8 @@ struct p_factory
// Owning prober.
struct prober *p;
// Has the settings be loaded.
bool setting_ok;
// Have we tried to load the settings.
bool tried_settings;
// Settings for this tracking system.
struct xrt_settings_tracking settings;
@ -112,13 +112,23 @@ on_video_device(struct xrt_prober *xp,
static void
p_factory_ensure_frameserver(struct p_factory *fact)
{
// No settings loaded.
if (!fact->setting_ok) {
// Already created.
if (fact->xfs != NULL) {
return;
}
// Already created.
if (fact->xfs != NULL) {
// We have already tried to load the settings.
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;
}
@ -217,23 +227,21 @@ p_factory_create_tracked_psmv(struct xrt_tracking_factory *xfact,
{
struct p_factory *fact = p_factory(xfact);
if (!fact->setting_ok) {
return -1;
}
#ifdef XRT_HAVE_OPENCV
struct xrt_tracked_psmv *xtmv = NULL;
p_factory_ensure_frameserver(fact);
if (fact->num_xtmv < ARRAY_SIZE(fact->xtmv)) {
xtmv = fact->xtmv[fact->num_xtmv++];
xtmv = fact->xtmv[fact->num_xtmv];
}
if (xtmv == NULL) {
return -1;
}
fact->num_xtmv++;
t_psmv_start(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);
if (!fact->setting_ok) {
return -1;
}
#ifdef XRT_HAVE_OPENCV
struct xrt_tracked_psvr *xtvr = NULL;
@ -306,13 +310,6 @@ p_tracking_init(struct prober *p)
// Finally set us as the tracking factory.
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;
}