Illustrated C/C++ source sheets, symbol index, API relationship graph, compiler console and debug trace

LIVE AI / FLEXIBLE SENSING / FLIGHT CONTEXT

Sensoraia API

Live AI and sensing, connected to the flight.
See, listen, inspect and return supported results while airborne.

Loading...
Searching...
No Matches
inference_settings.c
Go to the documentation of this file.
1/*
2 * Copyright 2026 OpenUAS
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** @file inference_settings.c
19 * @brief Strict shared fields and deterministic application_settings.ini lookup.
20 * @details Module callbacks own module-specific keys; this layer never guesses a model's meaning. */
21#include "inference_settings.h"
22
23#include <stdio.h>
24#include <string.h>
25#include <sys/stat.h>
26#include <unistd.h>
27
29 enum inference_backend backend)
30{
31 if (!settings) return;
32 unsigned char *bytes = (unsigned char *)settings;
33 for (size_t index = 0; index < sizeof(*settings); ++index) bytes[index] = 0;
34 settings->backend = backend;
35 settings->threads = 1;
36 settings->timeout_ms = 2000;
37}
38
39/** @brief Reject truncation and control characters before accepting configuration text. */
40static bool text_copy(char *destination, size_t capacity, const char *source)
41{
42 if (!source || !capacity) return false;
43 const size_t length = strlen(source);
44 if (length >= capacity) return false;
45 for (size_t index = 0; index <= length; ++index) {
46 const unsigned char character = (unsigned char)source[index];
47 if ((character < 32 && character) || character == 127) return false;
48 destination[index] = source[index];
49 }
50 return true;
51}
52
54 const char *key, const char *value)
55{
56 if (!settings || !key || !value || !value[0]) return -1;
57 static const char *const keys[] = {"inference_backend", "input_source", "model", "model_descriptor",
58 "onnx_lib_path", "input", "onnx_threads", "inference_timeout_ms"};
59 size_t field = 0;
60 while (field < sizeof(keys) / sizeof(keys[0]) && strcmp(keys[field], key)) ++field;
61 if (field == sizeof(keys) / sizeof(keys[0])) return 0;
62 /* Duplicates are errors instead of making behavior depend on field order. */
63 const uint32_t bit = UINT32_C(1) << field;
64 if (settings->seen & bit) return -1;
65 settings->seen |= bit;
66 if (field == 0) {
67 if (!strcmp(value, "onnx")) settings->backend = INFERENCE_ONNX;
68 else if (!strcmp(value, "imx500")) settings->backend = INFERENCE_IMX500;
69 else if (!strcmp(value, "native")) settings->backend = INFERENCE_NATIVE;
70 else if (!strcmp(value, "auto")) settings->backend = INFERENCE_AUTO;
71 else return -1;
72 return 1;
73 }
74 if (field == 1) {
75 if (!strcmp(value, "live")) settings->source = INFERENCE_LIVE;
76 else if (!strcmp(value, "image")) settings->source = INFERENCE_IMAGE;
77 else if (!strcmp(value, "fake")) settings->source = INFERENCE_FAKE;
78 else return -1;
79 return 1;
80 }
81 if (field >= 2 && field <= 5) {
82 char *destination = field == 2 ? settings->model : field == 3 ? settings->descriptor :
83 field == 4 ? settings->library_directory : settings->input;
84 return text_copy(destination, INFERENCE_PATH_CAPACITY, value) ? 1 : -1;
85 }
86 uint64_t number = 0;
87 if (!ini_uint(value, &number) || !number || number > (field == 6 ? 32U : 120000U)) return -1;
88 if (field == 6) settings->threads = (unsigned)number;
89 else settings->timeout_ms = (unsigned)number;
90 return 1;
91}
92
93bool inference_resolve_path(const char *base_file, const char *value,
94 char *destination, size_t capacity)
95{
96 if (!base_file || !value || !value[0] || !destination || !capacity) return false;
98 if (base_file[0] == '/') {
99 if (!text_copy(absolute, sizeof(absolute), base_file)) return false;
100 } else {
101 char working[INFERENCE_PATH_CAPACITY];
102 if (!getcwd(working, sizeof(working))) return false;
103 const int count = snprintf(absolute, sizeof(absolute), "%s/%s", working, base_file);
104 if (count < 0 || (size_t)count >= sizeof(absolute)) return false;
105 }
106 char *separator = strrchr(absolute, '/');
107 if (!separator) return false;
108 separator[1] = '\0';
109 const int count = snprintf(joined, sizeof(joined), "%s%s", value[0] == '/' ? "" : absolute, value);
110 if (count < 0 || (size_t)count >= sizeof(joined)) return false;
111 size_t used = 1;
112 /* Normalize lexically: a packaged output path need not exist on the development machine. */
113 if (capacity < 2) return false;
114 destination[0] = '/';
115 const char *cursor = joined;
116 while (*cursor) {
117 while (*cursor == '/') ++cursor;
118 const char *start = cursor;
119 while (*cursor && *cursor != '/') ++cursor;
120 const size_t length = (size_t)(cursor - start);
121 if (!length || (length == 1 && start[0] == '.')) continue;
122 if (length == 2 && start[0] == '.' && start[1] == '.') {
123 if (used > 1) {
124 --used;
125 while (used > 1 && destination[used - 1] != '/') --used;
126 }
127 continue;
128 }
129 if (length > capacity - used - 1) return false;
130 for (size_t index = 0; index < length; ++index) {
131 const unsigned char character = (unsigned char)start[index];
132 if (character < 32 || character == 127) return false;
133 destination[used++] = (char)character;
134 }
135 if (used >= capacity - 1) return false;
136 destination[used++] = '/';
137 }
138 if (used > 1) --used;
139 destination[used] = '\0';
140 return true;
141}
142
143/** @brief Borrowed dispatch state scoped to one destructive INI parse. */
149
150/** @brief Delegate only unknown common keys; an invalid common key must not be accepted by a module. */
151static bool settings_field(void *context, const char *key, const char *value)
152{
153 struct settings_parser *parser = context;
154 const int common = inference_settings_field(parser->settings, key, value);
155 return common ? common > 0 : parser->extra && parser->extra(parser->context, key, value);
156}
157
158bool inference_settings_read(struct inference_settings *settings, const char *filename,
160 char *error, size_t capacity)
161{
162 if (!settings || !filename || !error || !capacity) return false;
163 error[0] = '\0';
164 char contents[8193];
165 size_t length = 0;
166 struct settings_parser parser = {.settings = settings, .extra = extra, .context = context};
167 if (!ini_read_file(filename, contents, sizeof(contents), &length) ||
168 !ini_parse(contents, length, settings_field, &parser)) {
169 snprintf(error, capacity, "Invalid inference settings: %s", filename);
170 return false;
171 }
173 for (size_t index = 0; index < sizeof(paths) / sizeof(paths[0]); ++index) {
174 if (!paths[index][0]) continue;
175 char resolved[INFERENCE_PATH_CAPACITY];
176 if (!inference_resolve_path(filename, paths[index], resolved, sizeof(resolved)) ||
177 !text_copy(paths[index], INFERENCE_PATH_CAPACITY, resolved)) {
178 snprintf(error, capacity, "Invalid inference settings path: %s", filename);
179 return false;
180 }
181 }
183 const char *suffix = strrchr(settings->model, '.');
185 else if (suffix && !strcmp(suffix, ".onnx")) {
187 if (!settings->descriptor[0]) {
188 const int count = snprintf(settings->descriptor, sizeof(settings->descriptor), "%.*s.ini",
189 (int)(suffix - settings->model), settings->model);
190 if (count < 0 || (size_t)count >= sizeof(settings->descriptor)) return false;
191 }
192 }
193 else if (suffix && !strcmp(suffix, ".rpk")) settings->backend = INFERENCE_IMX500;
194 else {
195 snprintf(error, capacity, "Automatic inference requires a .onnx or .rpk model: %s", settings->model);
196 return false;
197 }
198 }
199 return true;
200}
201
202bool inference_settings_find(const char *application, const char *module,
203 char *filename, size_t capacity)
204{
205 if (!application || !module || !filename || !capacity ||
206 strspn(application, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(application) ||
207 strspn(module, "abcdefghijklmnopqrstuvwxyz0123456789_") != strlen(module)) return false;
208 char executable[INFERENCE_PATH_CAPACITY], relative[INFERENCE_PATH_CAPACITY];
209 const ssize_t length = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
210 if (length <= 0 || (size_t)length >= sizeof(executable) - 1) return false;
211 executable[length] = '\0';
212 for (unsigned directory = 0; directory < 5; ++directory) {
213 /* Never search cwd: launching from another directory must not silently select another assignment. */
214 int count;
215 if (!directory) count = snprintf(relative, sizeof(relative), "%s_settings.ini", application);
216 else if (directory == 1) count = snprintf(relative, sizeof(relative), "../modules/%s/%s_settings.ini", module, application);
217 else if (directory == 2) count = snprintf(relative, sizeof(relative), "../share/%s/%s_settings.ini", application, application);
218 else if (directory == 3) count = snprintf(relative, sizeof(relative), "modules/%s/%s_settings.ini", module, application);
219 else count = snprintf(relative, sizeof(relative), "share/%s/%s_settings.ini", application, application);
220 if (count < 0 || (size_t)count >= sizeof(relative) ||
221 !inference_resolve_path(executable, relative, filename, capacity)) return false;
222 struct stat status;
223 if (!stat(filename, &status)) return S_ISREG(status.st_mode);
224 }
225 return false;
226}
static bool ini_uint(const char *text, uint64_t *number)
Parse digits-only uint64; overflow and signs are rejected.
Definition bounded_ini.h:52
static bool ini_read_file(const char *filename, char *buffer, size_t capacity, size_t *length)
Read a nonempty regular file smaller than capacity and append NUL.
Definition bounded_ini.h:98
bool(* ini_field_handler)(void *context, const char *key, const char *value)
Definition bounded_ini.h:35
static bool ini_parse(char *buffer, size_t length, ini_field_handler handler, void *context)
Destructively parse flat fields with blank lines and #/; comment lines.
static unsigned capacity[COVERAGE_CAMERAS]
uint8_t bytes[CHDK_CONTROL_REPLY_SIZE]
Definition chdk_pipe.c:144
static bool start(uint32_t session)
Admit a nonzero session and discard correlation from the previous one.
static bool settings_field(void *context, const char *key, const char *value)
Delegate only unknown common keys; an invalid common key must not be accepted by a module.
bool inference_settings_find(const char *application, const char *module, char *filename, size_t capacity)
Discover application_settings.ini in adjacent, development-module, then installed-share order.
bool inference_resolve_path(const char *base_file, const char *value, char *destination, size_t capacity)
Resolve an INI-relative path without requiring its destination to exist yet.
int inference_settings_field(struct inference_settings *settings, const char *key, const char *value)
Parse one common key without consuming module-specific keys.
static bool text_copy(char *destination, size_t capacity, const char *source)
Reject truncation and control characters before accepting configuration text.
bool inference_settings_read(struct inference_settings *settings, const char *filename, ini_field_handler extra, void *context, char *error, size_t capacity)
Parse one flat INI, delegate application keys and resolve paths against the selected file.
void inference_settings_init(struct inference_settings *settings, enum inference_backend backend)
Reset storage and select conservative thread/timeout defaults.
Shared application_settings.ini discovery with module-owned extra keys.
@ INFERENCE_IMAGE
@ INFERENCE_LIVE
@ INFERENCE_FAKE
#define INFERENCE_PATH_CAPACITY
inference_backend
Backend selection; AUTO resolves the model suffix, never an execution error.
@ INFERENCE_NATIVE
@ INFERENCE_AUTO
@ INFERENCE_ONNX
@ INFERENCE_IMX500
struct jpeg_error_mgr error
Definition lwir_image.c:60
static size_t count
Definition pose_log.c:79
Per-application configuration; all resolved paths belong to this caller-owned value.
enum inference_backend backend
char model[INFERENCE_PATH_CAPACITY]
char input[INFERENCE_PATH_CAPACITY]
enum inference_source source
char descriptor[INFERENCE_PATH_CAPACITY]
char library_directory[INFERENCE_PATH_CAPACITY]
Borrowed dispatch state scoped to one destructive INI parse.
ini_field_handler extra
struct inference_settings * settings