aux/util: Move json config parsing to aux/util

This commit is contained in:
Christoph Haag 2021-02-17 03:51:21 +01:00
parent aa9298f70d
commit 5e4c62c8aa
10 changed files with 145 additions and 102 deletions

View file

@ -143,6 +143,8 @@ set(UTIL_SOURCE_FILES
util/u_timing_render.h util/u_timing_render.h
util/u_var.cpp util/u_var.cpp
util/u_var.h util/u_var.h
util/u_config_json.c
util/u_config_json.h
) )
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/util/u_git_tag.c.in" "${CMAKE_CURRENT_BINARY_DIR}/u_git_tag.c" @ONLY) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/util/u_git_tag.c.in" "${CMAKE_CURRENT_BINARY_DIR}/u_git_tag.c" @ONLY)

View file

@ -58,6 +58,8 @@ lib_aux_util = static_library(
'util/u_timing_render.h', 'util/u_timing_render.h',
'util/u_var.cpp', 'util/u_var.cpp',
'util/u_var.h', 'util/u_var.h',
'util/u_config_json.c',
'util/u_config_json.h',
) + [ ) + [
u_git_tag_c, u_git_tag_c,
], ],

View file

@ -7,11 +7,15 @@
* @ingroup st_prober * @ingroup st_prober
*/ */
#include <xrt/xrt_device.h>
#include "xrt/xrt_settings.h"
#include "xrt/xrt_config.h"
#include "util/u_file.h" #include "util/u_file.h"
#include "util/u_json.h" #include "util/u_json.h"
#include "util/u_debug.h" #include "util/u_debug.h"
#include "p_prober.h" #include "u_config_json.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -20,13 +24,15 @@
DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL) DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL)
#define CONFIG_FILE_NAME "config_v0.json"
void void
p_json_open_or_create_main_file(struct prober *p) u_config_json_open_or_create_main_file(struct u_config_json *json)
{ {
#ifdef XRT_OS_LINUX #ifdef XRT_OS_LINUX
char tmp[1024]; char tmp[1024];
ssize_t ret = u_file_get_path_in_config_dir("config_v0.json", tmp, sizeof(tmp)); ssize_t ret = u_file_get_path_in_config_dir(CONFIG_FILE_NAME, tmp, sizeof(tmp));
if (ret <= 0) { if (ret <= 0) {
U_LOG_E( U_LOG_E(
"Could not load or create config file no $HOME " "Could not load or create config file no $HOME "
@ -34,12 +40,12 @@ p_json_open_or_create_main_file(struct prober *p)
return; return;
} }
FILE *file = u_file_open_file_in_config_dir("config_v0.json", "r"); FILE *file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "r");
if (file == NULL) { if (file == NULL) {
return; return;
} }
p->json.file_loaded = true; json->file_loaded = true;
char *str = u_file_read_content(file); char *str = u_file_read_content(file);
fclose(file); fclose(file);
@ -54,8 +60,8 @@ p_json_open_or_create_main_file(struct prober *p)
return; return;
} }
p->json.root = cJSON_Parse(str); json->root = cJSON_Parse(str);
if (p->json.root == NULL) { if (json->root == NULL) {
U_LOG_E("Failed to parse JSON in '%s':\n%s\n#######", tmp, str); U_LOG_E("Failed to parse JSON in '%s':\n%s\n#######", tmp, str);
U_LOG_E("'%s'", cJSON_GetErrorPtr()); U_LOG_E("'%s'", cJSON_GetErrorPtr());
} }
@ -142,10 +148,10 @@ get_obj_str(cJSON *json, const char *name, char *array, size_t array_size)
} }
static bool static bool
is_json_ok(struct prober *p) is_json_ok(struct u_config_json *json)
{ {
if (p->json.root == NULL) { if (json->root == NULL) {
if (p->json.file_loaded) { if (json->file_loaded) {
U_LOG_E("JSON not parsed!"); U_LOG_E("JSON not parsed!");
} else { } else {
U_LOG_W("No config file!"); U_LOG_W("No config file!");
@ -157,17 +163,17 @@ is_json_ok(struct prober *p)
} }
static bool static bool
parse_active(const char *str, const char *from, enum p_active_config *out_active) parse_active(const char *str, const char *from, enum u_config_json_active_config *out_active)
{ {
if (strcmp(str, "none") == 0) { if (strcmp(str, "none") == 0) {
*out_active = P_ACTIVE_CONFIG_NONE; *out_active = U_ACTIVE_CONFIG_NONE;
} else if (strcmp(str, "tracking") == 0) { } else if (strcmp(str, "tracking") == 0) {
*out_active = P_ACTIVE_CONFIG_TRACKING; *out_active = U_ACTIVE_CONFIG_TRACKING;
} else if (strcmp(str, "remote") == 0) { } else if (strcmp(str, "remote") == 0) {
*out_active = P_ACTIVE_CONFIG_REMOTE; *out_active = U_ACTIVE_CONFIG_REMOTE;
} else { } else {
U_LOG_E("Unknown active config '%s' from %s.", str, from); U_LOG_E("Unknown active config '%s' from %s.", str, from);
*out_active = P_ACTIVE_CONFIG_NONE; *out_active = U_ACTIVE_CONFIG_NONE;
return false; return false;
} }
@ -175,7 +181,7 @@ parse_active(const char *str, const char *from, enum p_active_config *out_active
} }
void void
p_json_get_active(struct prober *p, enum p_active_config *out_active) u_config_json_get_active(struct u_config_json *json, enum u_config_json_active_config *out_active)
{ {
const char *str = debug_get_option_active_config(); const char *str = debug_get_option_active_config();
if (str != NULL && parse_active(str, "environment", out_active)) { if (str != NULL && parse_active(str, "environment", out_active)) {
@ -183,8 +189,8 @@ p_json_get_active(struct prober *p, enum p_active_config *out_active)
} }
char tmp[256]; char tmp[256];
if (!is_json_ok(p) || !get_obj_str(p->json.root, "active", tmp, sizeof(tmp))) { if (!is_json_ok(json) || !get_obj_str(json->root, "active", tmp, sizeof(tmp))) {
*out_active = P_ACTIVE_CONFIG_NONE; *out_active = U_ACTIVE_CONFIG_NONE;
return; return;
} }
@ -192,9 +198,9 @@ p_json_get_active(struct prober *p, enum p_active_config *out_active)
} }
bool bool
p_json_get_remote_port(struct prober *p, int *out_port) u_config_json_get_remote_port(struct u_config_json *json, int *out_port)
{ {
cJSON *t = cJSON_GetObjectItemCaseSensitive(p->json.root, "remote"); cJSON *t = cJSON_GetObjectItemCaseSensitive(json->root, "remote");
if (t == NULL) { if (t == NULL) {
U_LOG_E("No remote node"); U_LOG_E("No remote node");
return false; return false;
@ -221,10 +227,10 @@ p_json_get_remote_port(struct prober *p, int *out_port)
} }
static cJSON * static cJSON *
open_tracking_settings(struct prober *p) open_tracking_settings(struct u_config_json *json)
{ {
if (p->json.root == NULL) { if (json->root == NULL) {
if (p->json.file_loaded) { if (json->file_loaded) {
U_LOG_E("JSON not parsed!"); U_LOG_E("JSON not parsed!");
} else { } else {
U_LOG_W("No config file!"); U_LOG_W("No config file!");
@ -232,7 +238,7 @@ open_tracking_settings(struct prober *p)
return NULL; return NULL;
} }
cJSON *t = cJSON_GetObjectItemCaseSensitive(p->json.root, "tracking"); cJSON *t = cJSON_GetObjectItemCaseSensitive(json->root, "tracking");
if (t == NULL) { if (t == NULL) {
U_LOG_E("No tracking node"); U_LOG_E("No tracking node");
return NULL; return NULL;
@ -251,9 +257,11 @@ open_tracking_settings(struct prober *p)
} }
bool bool
p_json_get_tracking_overrides(struct prober *p, struct xrt_tracking_override *out_overrides, size_t *out_num_overrides) u_config_json_get_tracking_overrides(struct u_config_json *json,
struct xrt_tracking_override *out_overrides,
size_t *out_num_overrides)
{ {
cJSON *t = open_tracking_settings(p); cJSON *t = open_tracking_settings(json);
if (t == NULL) { if (t == NULL) {
U_LOG_E("No tracking node"); U_LOG_E("No tracking node");
return false; return false;
@ -301,9 +309,9 @@ p_json_get_tracking_overrides(struct prober *p, struct xrt_tracking_override *ou
} }
bool bool
p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s) u_config_json_get_tracking_settings(struct u_config_json *json, struct xrt_settings_tracking *s)
{ {
cJSON *t = open_tracking_settings(p); cJSON *t = open_tracking_settings(json);
if (t == NULL) { if (t == NULL) {
U_LOG_E("No tracking node"); U_LOG_E("No tracking node");
return false; return false;

View file

@ -0,0 +1,86 @@
// Copyright 2021, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Code to manage the settings file.
* @author Christoph Haag <christoph.haag@collabora.com>
* @ingroup aux_util
*/
#pragma once
#include "util/u_json.h"
#include "xrt/xrt_settings.h"
#ifdef __cplusplus
extern "C" {
#endif
/*!
* What config is currently active in the config file.
*/
enum u_config_json_active_config
{
U_ACTIVE_CONFIG_NONE = 0,
U_ACTIVE_CONFIG_TRACKING = 1,
U_ACTIVE_CONFIG_REMOTE = 2,
};
struct u_config_json
{
//! For error reporting, was it loaded but not parsed?
bool file_loaded;
cJSON *root;
};
/*!
* Load the JSON config file.
*
* @ingroup aux_util
*/
void
u_config_json_open_or_create_main_file(struct u_config_json *json);
/*!
* Read from the JSON loaded json config file and returns the active config,
* can be overridden by `P_OVERRIDE_ACTIVE_CONFIG` envirmental variable.
*
* @ingroup aux_util
*/
void
u_config_json_get_active(struct u_config_json *json, enum u_config_json_active_config *out_active);
/*!
* Extract tracking settings from the JSON.
*
* @ingroup aux_util
* @relatesalso xrt_settings_tracking
*/
bool
u_config_json_get_tracking_settings(struct u_config_json *json, struct xrt_settings_tracking *s);
/*!
* Extract tracking override settings from the JSON.
*
* Caller allocates an array of XRT_MAX_TRACKING_OVERRIDES tracking_override.
*
* @ingroup aux_util
* @relatesalso xrt_settings_tracking
*/
bool
u_config_json_get_tracking_overrides(struct u_config_json *json,
struct xrt_tracking_override *out_overrides,
size_t *out_num_overrides);
/*!
* Extract remote settings from the JSON.
*
* @ingroup aux_util
*/
bool
u_config_json_get_remote_port(struct u_config_json *json, int *out_port);
#ifdef __cplusplus
}
#endif

View file

@ -12,6 +12,9 @@
#include "xrt/xrt_compiler.h" #include "xrt/xrt_compiler.h"
// XRT_DEVICE_NAME_LEN
#include "xrt/xrt_device.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif

View file

@ -6,7 +6,6 @@ set(PROBER_INCLUDES)
set(PROBER_SOURCE_FILES set(PROBER_SOURCE_FILES
p_documentation.h p_documentation.h
p_dump.c p_dump.c
p_json.c
p_prober.c p_prober.c
p_prober.h p_prober.h
p_tracking.c p_tracking.c

View file

@ -4,7 +4,6 @@
prober_sources = [ prober_sources = [
'p_documentation.h', 'p_documentation.h',
'p_dump.c', 'p_dump.c',
'p_json.c',
'p_prober.c', 'p_prober.c',
'p_prober.h', 'p_prober.h',
'p_tracking.c', 'p_tracking.c',

View file

@ -11,7 +11,7 @@
#include "util/u_var.h" #include "util/u_var.h"
#include "util/u_misc.h" #include "util/u_misc.h"
#include "util/u_json.h" #include "util/u_config_json.h"
#include "util/u_debug.h" #include "util/u_debug.h"
#include "os/os_hid.h" #include "os/os_hid.h"
#include "p_prober.h" #include "p_prober.h"
@ -403,12 +403,15 @@ initialize(struct prober *p, struct xrt_prober_entry_lists *lists)
p->lists = lists; p->lists = lists;
p->ll = debug_get_log_option_prober_log(); p->ll = debug_get_log_option_prober_log();
p->json.file_loaded = false;
p->json.root = NULL;
u_var_add_root((void *)p, "Prober", true); u_var_add_root((void *)p, "Prober", true);
u_var_add_ro_u32(p, &p->ll, "Log Level"); u_var_add_ro_u32(p, &p->ll, "Log Level");
int ret; int ret;
p_json_open_or_create_main_file(p); u_config_json_open_or_create_main_file(&p->json);
ret = collect_entries(p); ret = collect_entries(p);
if (ret != 0) { if (ret != 0) {
@ -762,7 +765,7 @@ add_from_remote(struct prober *p, struct xrt_device **xdevs, size_t num_xdevs, b
#ifdef XRT_BUILD_DRIVER_REMOTE #ifdef XRT_BUILD_DRIVER_REMOTE
int port = 4242; int port = 4242;
if (!p_json_get_remote_port(p, &port)) { if (!u_config_json_get_remote_port(&p->json, &port)) {
port = 4242; port = 4242;
} }
@ -820,18 +823,18 @@ static int
select_device(struct xrt_prober *xp, struct xrt_device **xdevs, size_t num_xdevs) select_device(struct xrt_prober *xp, struct xrt_device **xdevs, size_t num_xdevs)
{ {
struct prober *p = (struct prober *)xp; struct prober *p = (struct prober *)xp;
enum p_active_config active; enum u_config_json_active_config active;
bool have_hmd = false; bool have_hmd = false;
p_json_get_active(p, &active); u_config_json_get_active(&p->json, &active);
switch (active) { switch (active) {
case P_ACTIVE_CONFIG_NONE: case U_ACTIVE_CONFIG_NONE:
case P_ACTIVE_CONFIG_TRACKING: case U_ACTIVE_CONFIG_TRACKING:
add_from_devices(p, xdevs, num_xdevs, &have_hmd); add_from_devices(p, xdevs, num_xdevs, &have_hmd);
add_from_auto_probers(p, xdevs, num_xdevs, &have_hmd); add_from_auto_probers(p, xdevs, num_xdevs, &have_hmd);
break; break;
case P_ACTIVE_CONFIG_REMOTE: add_from_remote(p, xdevs, num_xdevs, &have_hmd); break; case U_ACTIVE_CONFIG_REMOTE: add_from_remote(p, xdevs, num_xdevs, &have_hmd); break;
default: assert(false); default: assert(false);
} }
@ -856,7 +859,7 @@ select_device(struct xrt_prober *xp, struct xrt_device **xdevs, size_t num_xdevs
struct xrt_tracking_override overrides[XRT_MAX_TRACKING_OVERRIDES]; struct xrt_tracking_override overrides[XRT_MAX_TRACKING_OVERRIDES];
size_t num_overrides = 0; size_t num_overrides = 0;
if (p_json_get_tracking_overrides(p, overrides, &num_overrides)) { if (u_config_json_get_tracking_overrides(&p->json, overrides, &num_overrides)) {
for (size_t i = 0; i < num_overrides; i++) { for (size_t i = 0; i < num_overrides; i++) {
struct xrt_tracking_override *o = &overrides[i]; struct xrt_tracking_override *o = &overrides[i];
apply_tracking_override(p, xdevs, num_xdevs, o); apply_tracking_override(p, xdevs, num_xdevs, o);

View file

@ -16,6 +16,7 @@
#include "xrt/xrt_settings.h" #include "xrt/xrt_settings.h"
#include "util/u_logging.h" #include "util/u_logging.h"
#include "util/u_config_json.h"
#ifdef XRT_HAVE_LIBUSB #ifdef XRT_HAVE_LIBUSB
#include <libusb.h> #include <libusb.h>
@ -41,16 +42,6 @@
#define P_WARN(d, ...) U_LOG_IFL_W(d->ll, __VA_ARGS__) #define P_WARN(d, ...) U_LOG_IFL_W(d->ll, __VA_ARGS__)
#define P_ERROR(d, ...) U_LOG_IFL_E(d->ll, __VA_ARGS__) #define P_ERROR(d, ...) U_LOG_IFL_E(d->ll, __VA_ARGS__)
/*!
* What config is currently active in the config file.
*/
enum p_active_config
{
P_ACTIVE_CONFIG_NONE = 0,
P_ACTIVE_CONFIG_TRACKING = 1,
P_ACTIVE_CONFIG_REMOTE = 2,
};
#ifdef XRT_OS_LINUX #ifdef XRT_OS_LINUX
/*! /*!
* A hidraw interface that a @ref prober_device exposes. * A hidraw interface that a @ref prober_device exposes.
@ -131,13 +122,7 @@ struct prober
struct xrt_prober_entry_lists *lists; struct xrt_prober_entry_lists *lists;
struct struct u_config_json json;
{
//! For error reporting, was it loaded but not parsed?
bool file_loaded;
cJSON *root;
} json;
#ifdef XRT_HAVE_LIBUSB #ifdef XRT_HAVE_LIBUSB
struct struct
@ -179,51 +164,6 @@ struct prober
* *
*/ */
/*!
* Load the JSON config file.
*
* @public @memberof prober
*/
void
p_json_open_or_create_main_file(struct prober *p);
/*!
* Read from the JSON loaded json config file and returns the active config,
* can be overridden by `P_OVERRIDE_ACTIVE_CONFIG` envirmental variable.
*
* @public @memberof prober
*/
void
p_json_get_active(struct prober *p, enum p_active_config *out_active);
/*!
* Extract tracking settings from the JSON.
*
* @public @memberof prober
* @relatesalso xrt_settings_tracking
*/
bool
p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s);
/*!
* Extract tracking override settings from the JSON.
*
* Caller allocates an array of XRT_MAX_TRACKING_OVERRIDES tracking_override.
*
* @public @memberof prober
* @relatesalso xrt_settings_tracking
*/
bool
p_json_get_tracking_overrides(struct prober *p, struct xrt_tracking_override *out_overrides, size_t *out_num_overrides);
/*!
* Extract remote settings from the JSON.
*
* @public @memberof prober
*/
bool
p_json_get_remote_port(struct prober *p, int *out_port);
/*! /*!
* Dump the given device to stdout. * Dump the given device to stdout.
* *

View file

@ -20,6 +20,7 @@
#include "util/u_var.h" #include "util/u_var.h"
#include "util/u_misc.h" #include "util/u_misc.h"
#include "util/u_sink.h" #include "util/u_sink.h"
#include "util/u_config_json.h"
#include "p_prober.h" #include "p_prober.h"
#include <stdio.h> #include <stdio.h>
@ -134,7 +135,7 @@ p_factory_ensure_frameserver(struct p_factory *fact)
// We have no tried the settings. // We have no tried the settings.
fact->tried_settings = true; fact->tried_settings = true;
if (!p_json_get_tracking_settings(fact->p, &fact->settings)) { if (!u_config_json_get_tracking_settings(&fact->p->json, &fact->settings)) {
U_LOG_E( U_LOG_E(
"Could not setup PSVR and/or PSMV tracking, " "Could not setup PSVR and/or PSMV tracking, "
"see above."); "see above.");