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
soda.cpp
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/**
19 * @file soda.cpp
20 * @brief Small validated command-line dispatch point for
21 * post-capture analysis handlers. */
22#include <unistd.h>
23
24#include <cerrno>
25#include <cstdint>
26#include <cstdlib>
27#include <cstring>
28#include <fstream>
29#include <iostream>
30#include <limits>
31#include <string>
32#include <vector>
33
35#include "image_lens.h"
36#include "version.h"
37
38namespace {
39void print_usage(const char* program) {
40 std::cout
41 << SODA_BUILD_VERSION << '\n'
42 << "Usage: " << program << " [OPTIONS] IMAGE [10 shot integers]\n"
43 << " --local Development-PC mode; no hardware or remote "
44 "connection.\n"
45 << " --aicam Select the AI camera handler.\n"
46 << " --chdkcam Select the CHDK camera handler.\n"
47 << " --lwircam Select the thermal camera handler.\n"
48 << " --earcam Select the acoustic camera handler.\n"
49 << " --lens-profile FILE Emulate a lens before AIcam analysis "
50 "(requires --local --aicam).\n"
51 << " --lens-cache DIR Optional zoom-18 XYZ cache (pprzgcs legacy path GOOGLE/18) "
52 "coverage.\n"
53 << " --version Show application name, version, and build Git "
54 "revision.\n"
55 << " --help Show this help.\n"
56 << " -- End option parsing.\n"
57 << "Select at most one camera; otherwise use the generic handler.\n"
58 << "IMAGE must be readable and nonempty. Optional int32 shot values:\n"
59 << "number latitude longitude altitude roll pitch yaw speed course "
60 "groundalt\n";
61}
62
63void process_aicam() {
64 std::cout << "Now I can do nifty stuff for aicam" << std::endl;
65}
66
67void process_chdkcam() {
68 std::cout << "Now I can do nifty stuff for chdkcam" << std::endl;
69}
70
71void process_lwircam() {
72 std::cout << "Now I can do nifty stuff for lwircam" << std::endl;
73}
74
75void process_earcam() {
76 std::cout << "Now I can do nifty stuff for earcam" << std::endl;
77}
78
79struct CameraHandler {
80 const char* option;
81 void (*process)();
82};
83
84const CameraHandler handlers[] = {{"--aicam", process_aicam},
85 {"--chdkcam", process_chdkcam},
86 {"--lwircam", process_lwircam},
87 {"--earcam", process_earcam}};
88
89bool is_shot_value(const char* text) {
90 const char* digit = text;
91 if (*digit == '-' || *digit == '+') ++digit;
92 if (*digit == '\0') return false;
93 for (; *digit != '\0'; ++digit) {
94 if (*digit < '0' || *digit > '9') return false;
95 }
96 errno = 0;
97 char* end = nullptr;
98 const long long value = std::strtoll(text, &end, 10);
99 return errno != ERANGE && *end == '\0' &&
100 value >= std::numeric_limits<int32_t>::min() &&
101 value <= std::numeric_limits<int32_t>::max();
102}
103
104bool emulate_lens(const char* input, const char* profile, const char* cache,
105 std::string& output) {
106 char executable[4096];
107 ssize_t length =
108 readlink("/proc/self/exe", executable, sizeof(executable) - 1);
109 if (length <= 0 || static_cast<size_t>(length) >= sizeof(executable) - 1)
110 return false;
111 executable[length] = '\0';
112 std::string directory(executable);
113 directory.resize(directory.find_last_of('/'));
114 const std::string executable_path = directory + "/camera_lens";
115 if (access(executable_path.c_str(), X_OK) != 0) {
116 std::cerr << "SODA: native lens executable missing; run make camera-lens "
117 "in the USHER directory\n";
118 return false;
119 }
120 char derivative[4096];
121 if (!image_lens_emulate(executable_path.c_str(), input, profile, cache,
122 derivative, sizeof(derivative), nullptr))
123 return false;
124 output = derivative;
125 return true;
126}
127} // namespace
128
129/** @brief Select and run the postprocessor for one saved camera image.
130 * @param argc Argument count including the program name.
131 * @param argv Command-line argument vector.
132 * @return Process status for argument handling and the selected processing path.
133 * @details Resolve camera and optional lens settings before dispatching;
134 * local-camera derivatives are explicitly selected, not inferred from a live shot. */
135int main(int argc, char* argv[]) {
136 const CameraHandler* selected = nullptr;
137 const char* lens_profile = nullptr;
138 const char* lens_cache = nullptr;
139 std::vector<const char*> positional;
140 bool options = true;
141 bool local = false;
142 bool help = false;
143 bool version = false;
144 for (int argument = 1; argument < argc; ++argument) {
145 const char* value = argv[argument];
146 if (options && (std::strcmp(value, "--lens-profile") == 0 ||
147 std::strcmp(value, "--lens-cache") == 0)) {
148 const char** destination = std::strcmp(value, "--lens-profile") == 0
149 ? &lens_profile
150 : &lens_cache;
151 if (*destination != nullptr || argument + 1 >= argc ||
152 argv[argument + 1][0] == '\0') {
153 std::cerr << "SODA: missing or duplicate lens option\n";
154 return 2;
155 }
156 *destination = argv[++argument];
157 continue;
158 }
159 if (options && std::strcmp(value, "--") == 0) {
160 options = false;
161 continue;
162 }
163 if (options && std::strcmp(value, "--local") == 0) {
164 if (local) {
165 std::cerr << "SODA: duplicate --local option" << std::endl;
166 return 2;
167 }
168 local = true;
169 continue;
170 }
171 if (options && std::strcmp(value, "--help") == 0) {
172 help = true;
173 continue;
174 }
175 if (options && std::strcmp(value, "--version") == 0) {
176 version = true;
177 continue;
178 }
179 const CameraHandler* match = nullptr;
180 if (options) {
181 for (const CameraHandler& handler : handlers) {
182 if (std::strcmp(value, handler.option) == 0) match = &handler;
183 }
184 }
185 if (match != nullptr) {
186 if (selected != nullptr) {
187 std::cerr << "SODA: specify only one camera selector" << std::endl;
188 return 2;
189 }
190 selected = match;
191 } else if (options && value[0] == '-' && !is_shot_value(value)) {
192 std::cerr << "SODA: unknown option " << value << std::endl;
193 return 2;
194 } else {
195 positional.push_back(value);
196 }
197 }
198 if (help) {
199 print_usage(argv[0]);
200 return 0;
201 }
202 if (version) {
203 std::cout << SODA_BUILD_VERSION << std::endl;
204 return 0;
205 }
206 if (positional.empty()) {
207 std::cerr << "SODA: no image was provided" << std::endl;
208 return 1;
209 }
210 if ((lens_cache != nullptr && lens_profile == nullptr) ||
211 (lens_profile != nullptr && (!local || selected != &handlers[0]))) {
212 std::cerr << "SODA: lens emulation requires --local --aicam --lens-profile "
213 "FILE\n";
214 return 2;
215 }
216 if (positional.size() != 1 && positional.size() != 11) {
217 std::cerr << "SODA: expected IMAGE optionally followed by 10 shot integers"
218 << std::endl;
219 return 2;
220 }
221 for (size_t index = 1; index < positional.size(); ++index) {
222 if (!is_shot_value(positional[index])) {
223 std::cerr << "SODA: invalid shot integer " << positional[index]
224 << std::endl;
225 return 2;
226 }
227 }
228
229 std::ifstream image(positional[0], std::ios::binary | std::ios::ate);
230 if (!image || image.tellg() <= 0) {
231 std::cerr << "SODA: could not inspect image " << positional[0] << std::endl;
232 return 1;
233 }
234
235 if (local) std::cerr << "SODA: development-PC mode (--local)" << std::endl;
236 std::string lens_output;
237 const char* analysis_image = positional[0];
238 if (lens_profile != nullptr) {
239 if (!emulate_lens(positional[0], lens_profile, lens_cache, lens_output)) {
240 std::cerr << "SODA: lens emulation failed; clean image retained, "
241 "analysis skipped\n";
242 return 1;
243 }
244 analysis_image = lens_output.c_str();
245 std::cout << "SODA: lens-emulated analysis input " << analysis_image
246 << '\n';
247 }
248 // Every backend's image is checked here, independent of `selected` -- usher's
249 // own
250 // "--aicam" selector looks identical whether or not --spottercam produced
251 // this image, so the only reliable signal is whatever this image's own EXIF
252 // says.
253 if (!soda_process_object_detection(analysis_image)) {
254 // A partial output failure must reach USHER's bounded retry policy.
255 // Returning success here would silently abandon missing crops/CSV while
256 // claiming completion.
257 std::cerr << "SODA: object post-processing incomplete; original retained\n";
258 return 1;
259 }
260 if (selected != nullptr) {
261 selected->process();
262 } else {
263 std::cout << "SODA: I looked at the image. Cool, right?" << std::endl;
264 }
265 return 0;
266}
static void print_usage(FILE *stream, const char *program)
Print command-line help or version-led usage text.
Definition earcam.c:314
bool image_lens_emulate(const char *application, const char *input, const char *profile, const char *cache, char *output, size_t capacity, const volatile sig_atomic_t *running)
Definition image_lens.c:29
static struct usher_camera_profile profile
static FILE * output
Definition pose_log.c:77
int main(int argc, char *argv[])
Select and run the postprocessor for one saved camera image.
Definition soda.cpp:135
bool soda_process_object_detection(const char *image_path)
Post-process a captured image for an object-detection hit, if it has one.
EXIF-driven object-hit post-processing for SODA.
Compile-time build identity strings for USHER executables.
#define SODA_BUILD_VERSION
Definition version.h:73
Validated capture and reporting policy, fixed before the sample loop.
Definition earcam.c:137
struct captured_image image
Definition usher.c:371