2020-04-08 11:27:41 +00:00
|
|
|
// Copyright 2019, Collabora, Ltd.
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief Code to manage the settings file.
|
|
|
|
* @author Jakob Bornecrantz <jakob@collabora.com>
|
|
|
|
* @ingroup st_prober
|
|
|
|
*/
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
#include <xrt/xrt_device.h>
|
|
|
|
#include "xrt/xrt_settings.h"
|
|
|
|
#include "xrt/xrt_config.h"
|
|
|
|
|
2020-04-08 11:27:41 +00:00
|
|
|
#include "util/u_file.h"
|
|
|
|
#include "util/u_json.h"
|
|
|
|
#include "util/u_debug.h"
|
2020-10-15 16:58:52 +00:00
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
#include "u_config_json.h"
|
2020-04-08 11:27:41 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2020-10-15 16:58:52 +00:00
|
|
|
DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL)
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
#define CONFIG_FILE_NAME "config_v0.json"
|
|
|
|
|
2021-02-18 02:05:39 +00:00
|
|
|
void
|
|
|
|
u_config_json_close(struct u_config_json *json)
|
|
|
|
{
|
|
|
|
if (json->root != NULL) {
|
|
|
|
cJSON_Delete(json->root);
|
|
|
|
json->root = NULL;
|
|
|
|
}
|
|
|
|
json->file_loaded = false;
|
|
|
|
}
|
2020-04-08 11:27:41 +00:00
|
|
|
|
2020-05-01 17:11:27 +00:00
|
|
|
void
|
2021-02-17 02:51:21 +00:00
|
|
|
u_config_json_open_or_create_main_file(struct u_config_json *json)
|
2020-04-08 11:27:41 +00:00
|
|
|
{
|
2020-07-16 03:11:31 +00:00
|
|
|
#ifdef XRT_OS_LINUX
|
2020-05-01 17:11:27 +00:00
|
|
|
char tmp[1024];
|
2021-02-17 02:51:21 +00:00
|
|
|
ssize_t ret = u_file_get_path_in_config_dir(CONFIG_FILE_NAME, tmp, sizeof(tmp));
|
2020-05-01 17:11:27 +00:00
|
|
|
if (ret <= 0) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E(
|
|
|
|
"Could not load or create config file no $HOME "
|
|
|
|
"or $XDG_CONFIG_HOME env variables defined");
|
2020-05-01 17:11:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
FILE *file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "r");
|
2020-04-08 11:27:41 +00:00
|
|
|
if (file == NULL) {
|
2020-05-01 17:11:27 +00:00
|
|
|
return;
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
json->file_loaded = true;
|
2020-05-01 17:11:27 +00:00
|
|
|
|
2021-02-17 00:38:44 +00:00
|
|
|
char *str = u_file_read_content(file);
|
2020-04-08 11:27:41 +00:00
|
|
|
fclose(file);
|
|
|
|
if (str == NULL) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Could not read the contents of '%s'!", tmp);
|
2020-05-01 17:11:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No config created, ignore.
|
|
|
|
if (strlen(str) == 0) {
|
|
|
|
free(str);
|
|
|
|
return;
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
json->root = cJSON_Parse(str);
|
|
|
|
if (json->root == NULL) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Failed to parse JSON in '%s':\n%s\n#######", tmp, str);
|
|
|
|
U_LOG_E("'%s'", cJSON_GetErrorPtr());
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(str);
|
2020-07-16 03:11:31 +00:00
|
|
|
#else
|
|
|
|
//! @todo implement the underlying u_file_get_path_in_config_dir
|
|
|
|
return;
|
|
|
|
#endif
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static cJSON *
|
|
|
|
get_obj(cJSON *json, const char *name)
|
|
|
|
{
|
|
|
|
cJSON *item = cJSON_GetObjectItemCaseSensitive(json, name);
|
|
|
|
if (item == NULL) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Failed to find node '%s'!", name);
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
XRT_MAYBE_UNUSED static bool
|
|
|
|
get_obj_bool(cJSON *json, const char *name, bool *out_bool)
|
|
|
|
{
|
|
|
|
cJSON *item = get_obj(json, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u_json_get_bool(item, out_bool)) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Failed to parse '%s'!", name);
|
2020-04-08 11:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
get_obj_int(cJSON *json, const char *name, int *out_int)
|
|
|
|
{
|
|
|
|
cJSON *item = get_obj(json, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u_json_get_int(item, out_int)) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Failed to parse '%s'!", name);
|
2020-04-08 11:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
static bool
|
|
|
|
get_obj_float(cJSON *json, const char *name, float *out_float)
|
|
|
|
{
|
|
|
|
cJSON *item = get_obj(json, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u_json_get_float(item, out_float)) {
|
|
|
|
U_LOG_E("Failed to parse '%s'!", name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 11:27:41 +00:00
|
|
|
static bool
|
|
|
|
get_obj_str(cJSON *json, const char *name, char *array, size_t array_size)
|
|
|
|
{
|
|
|
|
cJSON *item = get_obj(json, name);
|
|
|
|
if (item == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u_json_get_string_into_array(item, array, array_size)) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Failed to parse '%s'!", name);
|
2020-04-08 11:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:58:52 +00:00
|
|
|
static bool
|
2021-02-17 02:51:21 +00:00
|
|
|
is_json_ok(struct u_config_json *json)
|
2020-10-15 16:58:52 +00:00
|
|
|
{
|
2021-02-17 02:51:21 +00:00
|
|
|
if (json->root == NULL) {
|
|
|
|
if (json->file_loaded) {
|
2021-03-24 17:15:39 +00:00
|
|
|
U_LOG_E("Config file was loaded but JSON is not parsed!");
|
2020-10-15 16:58:52 +00:00
|
|
|
} else {
|
2021-03-24 17:14:39 +00:00
|
|
|
U_LOG_I("No config file was loaded!");
|
2020-10-15 16:58:52 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-02-17 02:51:21 +00:00
|
|
|
parse_active(const char *str, const char *from, enum u_config_json_active_config *out_active)
|
2020-10-15 16:58:52 +00:00
|
|
|
{
|
|
|
|
if (strcmp(str, "none") == 0) {
|
2021-02-17 02:51:21 +00:00
|
|
|
*out_active = U_ACTIVE_CONFIG_NONE;
|
2020-10-15 16:58:52 +00:00
|
|
|
} else if (strcmp(str, "tracking") == 0) {
|
2021-02-17 02:51:21 +00:00
|
|
|
*out_active = U_ACTIVE_CONFIG_TRACKING;
|
2020-10-15 17:11:25 +00:00
|
|
|
} else if (strcmp(str, "remote") == 0) {
|
2021-02-17 02:51:21 +00:00
|
|
|
*out_active = U_ACTIVE_CONFIG_REMOTE;
|
2020-10-15 16:58:52 +00:00
|
|
|
} else {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Unknown active config '%s' from %s.", str, from);
|
2021-02-17 02:51:21 +00:00
|
|
|
*out_active = U_ACTIVE_CONFIG_NONE;
|
2020-10-15 16:58:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-17 02:51:21 +00:00
|
|
|
u_config_json_get_active(struct u_config_json *json, enum u_config_json_active_config *out_active)
|
2020-10-15 16:58:52 +00:00
|
|
|
{
|
|
|
|
const char *str = debug_get_option_active_config();
|
|
|
|
if (str != NULL && parse_active(str, "environment", out_active)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp[256];
|
2021-02-17 02:51:21 +00:00
|
|
|
if (!is_json_ok(json) || !get_obj_str(json->root, "active", tmp, sizeof(tmp))) {
|
|
|
|
*out_active = U_ACTIVE_CONFIG_NONE;
|
2020-10-15 16:58:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parse_active(tmp, "json", out_active);
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:11:25 +00:00
|
|
|
bool
|
2021-02-17 02:51:21 +00:00
|
|
|
u_config_json_get_remote_port(struct u_config_json *json, int *out_port)
|
2020-10-15 17:11:25 +00:00
|
|
|
{
|
2021-02-17 02:51:21 +00:00
|
|
|
cJSON *t = cJSON_GetObjectItemCaseSensitive(json->root, "remote");
|
2020-10-15 17:11:25 +00:00
|
|
|
if (t == NULL) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("No remote node");
|
2020-10-15 17:11:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ver = -1;
|
|
|
|
if (!get_obj_int(t, "version", &ver)) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Missing version tag!");
|
2020-10-15 17:11:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ver >= 1) {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_E("Unknown version tag '%i'!", ver);
|
2020-10-15 17:11:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int port = 0;
|
|
|
|
if (!get_obj_int(t, "port", &port)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_port = port;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
static cJSON *
|
2021-02-17 02:51:21 +00:00
|
|
|
open_tracking_settings(struct u_config_json *json)
|
2020-04-08 11:27:41 +00:00
|
|
|
{
|
2021-03-24 17:16:04 +00:00
|
|
|
if (!is_json_ok(json)) {
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
return NULL;
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 02:51:21 +00:00
|
|
|
cJSON *t = cJSON_GetObjectItemCaseSensitive(json->root, "tracking");
|
2020-04-08 11:27:41 +00:00
|
|
|
if (t == NULL) {
|
2021-03-24 17:36:41 +00:00
|
|
|
U_LOG_I("Config file does not contain tracking config");
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
return NULL;
|
2020-04-08 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ver = -1;
|
|
|
|
bool bad = false;
|
|
|
|
|
|
|
|
bad |= !get_obj_int(t, "version", &ver);
|
|
|
|
if (bad || ver >= 1) {
|
2021-03-24 17:36:41 +00:00
|
|
|
U_LOG_E("Missing or unknown version tag '%i' in tracking config", ver);
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-02-17 02:51:21 +00:00
|
|
|
u_config_json_get_tracking_overrides(struct u_config_json *json,
|
|
|
|
struct xrt_tracking_override *out_overrides,
|
|
|
|
size_t *out_num_overrides)
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
{
|
2021-02-17 02:51:21 +00:00
|
|
|
cJSON *t = open_tracking_settings(json);
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
if (t == NULL) {
|
2020-04-08 11:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
|
|
|
|
cJSON *overrides = cJSON_GetObjectItemCaseSensitive(t, "tracking_overrides");
|
|
|
|
|
|
|
|
*out_num_overrides = 0;
|
|
|
|
|
|
|
|
cJSON *override = NULL;
|
|
|
|
cJSON_ArrayForEach(override, overrides)
|
|
|
|
{
|
|
|
|
bool bad = false;
|
|
|
|
|
|
|
|
struct xrt_tracking_override *o = &out_overrides[(*out_num_overrides)++];
|
|
|
|
bad |= !get_obj_str(override, "target_device_serial", o->target_device_serial, XRT_DEVICE_NAME_LEN);
|
|
|
|
bad |= !get_obj_str(override, "tracker_device_serial", o->tracker_device_serial, XRT_DEVICE_NAME_LEN);
|
|
|
|
|
|
|
|
cJSON *offset = cJSON_GetObjectItemCaseSensitive(override, "offset");
|
|
|
|
if (offset) {
|
|
|
|
cJSON *orientation = cJSON_GetObjectItemCaseSensitive(offset, "orientation");
|
|
|
|
bad |= !get_obj_float(orientation, "x", &o->offset.orientation.x);
|
|
|
|
bad |= !get_obj_float(orientation, "y", &o->offset.orientation.y);
|
|
|
|
bad |= !get_obj_float(orientation, "z", &o->offset.orientation.z);
|
|
|
|
bad |= !get_obj_float(orientation, "w", &o->offset.orientation.w);
|
|
|
|
|
|
|
|
cJSON *position = cJSON_GetObjectItemCaseSensitive(offset, "position");
|
|
|
|
bad |= !get_obj_float(position, "x", &o->offset.position.x);
|
|
|
|
bad |= !get_obj_float(position, "y", &o->offset.position.y);
|
|
|
|
bad |= !get_obj_float(position, "z", &o->offset.position.z);
|
|
|
|
} else {
|
|
|
|
o->offset.orientation.w = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! @todo support arbitrary tracking inputs for overrides
|
|
|
|
o->input_name = XRT_INPUT_GENERIC_TRACKER_POSE;
|
|
|
|
|
|
|
|
if (bad) {
|
|
|
|
*out_num_overrides = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-02-17 02:51:21 +00:00
|
|
|
u_config_json_get_tracking_settings(struct u_config_json *json, struct xrt_settings_tracking *s)
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
{
|
2021-02-17 02:51:21 +00:00
|
|
|
cJSON *t = open_tracking_settings(json);
|
xrt: implement multi device wrappers for tracking overrides
Example config ~/.config/monado/config_v0.json
{
"active": "tracking",
"tracking": {
"version": 0,
"tracking_overrides": [
{
"target_device_serial": "LHR-E8CC625B",
"tracker_device_serial": "LHR-1D80A098",
"offset": {
"orientation": {
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"position": {
"x": 0,
"y": 0,
"z": 0
}
}
}
]
}
}
v2: Add multi device wrapper
2021-02-09 02:19:56 +00:00
|
|
|
if (t == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp[16];
|
|
|
|
|
|
|
|
bool bad = false;
|
|
|
|
|
2021-01-14 14:13:48 +00:00
|
|
|
bad |= !get_obj_str(t, "camera_name", s->camera_name, sizeof(s->camera_name));
|
2020-04-08 11:27:41 +00:00
|
|
|
bad |= !get_obj_int(t, "camera_mode", &s->camera_mode);
|
|
|
|
bad |= !get_obj_str(t, "camera_type", tmp, sizeof(tmp));
|
2021-01-14 14:13:48 +00:00
|
|
|
bad |= !get_obj_str(t, "calibration_path", s->calibration_path, sizeof(s->calibration_path));
|
2020-04-08 11:27:41 +00:00
|
|
|
if (bad) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(tmp, "regular_mono") == 0) {
|
|
|
|
s->camera_type = XRT_SETTINGS_CAMERA_TYPE_REGULAR_MONO;
|
|
|
|
} else if (strcmp(tmp, "regular_sbs") == 0) {
|
|
|
|
s->camera_type = XRT_SETTINGS_CAMERA_TYPE_REGULAR_SBS;
|
|
|
|
} else if (strcmp(tmp, "ps4") == 0) {
|
|
|
|
s->camera_type = XRT_SETTINGS_CAMERA_TYPE_PS4;
|
|
|
|
} else if (strcmp(tmp, "leap_motion") == 0) {
|
|
|
|
s->camera_type = XRT_SETTINGS_CAMERA_TYPE_LEAP_MOTION;
|
|
|
|
} else {
|
2020-12-16 17:12:56 +00:00
|
|
|
U_LOG_W("Unknown camera type '%s'", tmp);
|
2020-04-08 11:27:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-18 02:05:39 +00:00
|
|
|
|
2021-03-24 16:33:46 +00:00
|
|
|
static void
|
|
|
|
u_config_json_make_default_root(struct u_config_json *json)
|
|
|
|
{
|
|
|
|
json->root = cJSON_CreateObject();
|
|
|
|
}
|
|
|
|
|
2021-02-18 02:05:39 +00:00
|
|
|
static void
|
|
|
|
u_config_write(struct u_config_json *json)
|
|
|
|
{
|
|
|
|
char *str = cJSON_Print(json->root);
|
|
|
|
U_LOG_D("%s", str);
|
|
|
|
|
|
|
|
FILE *config_file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "w");
|
|
|
|
fprintf(config_file, "%s\n", str);
|
|
|
|
fflush(config_file);
|
|
|
|
fclose(config_file);
|
|
|
|
config_file = NULL;
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
u_config_json_save_calibration(struct u_config_json *json, struct xrt_settings_tracking *settings)
|
|
|
|
{
|
2021-03-24 16:33:46 +00:00
|
|
|
if (!json->root) {
|
|
|
|
u_config_json_make_default_root(json);
|
|
|
|
}
|
2021-02-18 02:05:39 +00:00
|
|
|
cJSON *root = json->root;
|
|
|
|
|
|
|
|
cJSON *t = cJSON_GetObjectItem(root, "tracking");
|
|
|
|
if (!t) {
|
|
|
|
t = cJSON_AddObjectToObject(root, "tracking");
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "version");
|
|
|
|
cJSON_AddNumberToObject(t, "version", 0);
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "camera_name");
|
|
|
|
cJSON_AddStringToObject(t, "camera_name", settings->camera_name);
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "camera_mode");
|
|
|
|
cJSON_AddNumberToObject(t, "camera_mode", settings->camera_mode);
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "camera_type");
|
|
|
|
switch (settings->camera_type) {
|
|
|
|
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_MONO: cJSON_AddStringToObject(t, "camera_type", "regular_mono"); break;
|
|
|
|
case XRT_SETTINGS_CAMERA_TYPE_REGULAR_SBS: cJSON_AddStringToObject(t, "camera_type", "regular_sbs"); break;
|
|
|
|
case XRT_SETTINGS_CAMERA_TYPE_PS4: cJSON_AddStringToObject(t, "camera_type", "ps4"); break;
|
|
|
|
case XRT_SETTINGS_CAMERA_TYPE_LEAP_MOTION: cJSON_AddStringToObject(t, "camera_type", "leap_motion"); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "calibration_path");
|
|
|
|
cJSON_AddStringToObject(t, "calibration_path", settings->calibration_path);
|
|
|
|
|
|
|
|
u_config_write(json);
|
|
|
|
}
|
2021-02-18 02:06:01 +00:00
|
|
|
|
|
|
|
static cJSON *
|
|
|
|
make_pose(struct xrt_pose *pose)
|
|
|
|
{
|
|
|
|
cJSON *json = cJSON_CreateObject();
|
|
|
|
|
|
|
|
cJSON *o = cJSON_CreateObject();
|
|
|
|
cJSON_AddNumberToObject(o, "x", pose->orientation.x);
|
|
|
|
cJSON_AddNumberToObject(o, "y", pose->orientation.y);
|
|
|
|
cJSON_AddNumberToObject(o, "z", pose->orientation.z);
|
|
|
|
cJSON_AddNumberToObject(o, "w", pose->orientation.w);
|
|
|
|
cJSON_AddItemToObject(json, "orientation", o);
|
|
|
|
|
|
|
|
cJSON *p = cJSON_CreateObject();
|
|
|
|
cJSON_AddNumberToObject(p, "x", pose->position.x);
|
|
|
|
cJSON_AddNumberToObject(p, "y", pose->position.y);
|
|
|
|
cJSON_AddNumberToObject(p, "z", pose->position.z);
|
|
|
|
cJSON_AddItemToObject(json, "position", p);
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
u_config_json_save_overrides(struct u_config_json *json, struct xrt_tracking_override *overrides, size_t num_overrides)
|
|
|
|
{
|
2021-03-24 16:33:46 +00:00
|
|
|
if (!json->root) {
|
|
|
|
u_config_json_make_default_root(json);
|
|
|
|
}
|
2021-02-18 02:06:01 +00:00
|
|
|
cJSON *root = json->root;
|
|
|
|
|
|
|
|
cJSON *t = cJSON_GetObjectItem(root, "tracking");
|
|
|
|
if (!t) {
|
|
|
|
t = cJSON_AddObjectToObject(root, "tracking");
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON_DeleteItemFromObject(t, "tracking_overrides");
|
|
|
|
cJSON *o = cJSON_AddArrayToObject(t, "tracking_overrides");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < num_overrides; i++) {
|
|
|
|
cJSON *entry = cJSON_CreateObject();
|
|
|
|
|
|
|
|
cJSON_AddStringToObject(entry, "target_device_serial", overrides[i].target_device_serial);
|
|
|
|
cJSON_AddStringToObject(entry, "tracker_device_serial", overrides[i].tracker_device_serial);
|
|
|
|
cJSON_AddItemToObject(entry, "offset", make_pose(&overrides[i].offset));
|
|
|
|
|
|
|
|
cJSON_AddItemToArray(o, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
u_config_write(json);
|
|
|
|
}
|