FERS 1.0.0
The Flexible Extensible Radar Simulator
Loading...
Searching...
No Matches
api.h
Go to the documentation of this file.
1#ifndef FERS_API_H
2#define FERS_API_H
3
4#include <stddef.h>
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10/**
11 * @brief An opaque handle to an in-memory FERS simulation context.
12 *
13 * This pointer represents a live simulation instance. It is an "opaque" type,
14 * meaning its internal structure is hidden from the API consumer. This design
15 * is intentional to decouple the client from the C++ implementation details of
16 * the simulator core. It ensures that the client code (e.g., in Rust) will not
17 * break even if the internal C++ `FersContext` class is changed, maintaining ABI
18 * stability across library versions.
19 *
20 * @note The client is responsible for managing the lifetime of this handle via
21 * `fers_context_create()` and `fers_context_destroy()`.
22 */
23struct fers_context;
24
26
27/**
28 * @brief A function pointer type for progress reporting callbacks.
29 *
30 * This callback can be implemented by the client to receive progress updates
31 * during long-running operations like `fers_run_simulation`.
32 *
33 * @param message A descriptive message about the current operation.
34 * @param current The current progress step.
35 * @param total The total number of steps for the operation.
36 * @param user_data An opaque pointer passed back to the caller, useful for
37 * maintaining state (e.g., a class instance or application handle).
38 */
39typedef void (*fers_progress_callback_t)(const char* message, int current, int total, void* user_data);
40
41
42// --- Context Lifecycle ---
43
44/**
45 * @brief Creates a new FERS simulation context.
46 *
47 * Allocates and initializes a new, empty simulation context in memory. This
48 * context serves as the container for a scenario loaded via one of the
49 * `fers_load_...` or `fers_update_...` functions.
50 *
51 * @note In a C API, RAII is not available. The caller is responsible for
52 * destroying the returned context using `fers_context_destroy()` to
53 * prevent resource leaks.
54 *
55 * @return A non-null opaque pointer (handle) to the simulation context on success.
56 * Returns NULL on failure (e.g., out of memory).
57 */
59
60/**
61 * @brief Destroys a FERS simulation context and releases all associated memory.
62 *
63 * This function must be called for every context created by `fers_context_create()`
64 * to ensure proper cleanup of the underlying C++ objects. Accessing the context
65 * handle after calling this function results in undefined behavior.
66 *
67 * @param context A valid pointer to a `fers_context_t` handle. If context is NULL,
68 * the function performs no action for safety.
69 */
71
72/**
73 * @brief Log levels for the FERS library.
74 */
84
85/**
86 * @brief Configures the internal logger.
87 * @param level The minimum severity level to log.
88 * @param log_file_path Optional path to a log file. Pass NULL to disable file logging.
89 * @return 0 on success, non-zero on error.
90 */
91int fers_configure_logging(fers_log_level_t level, const char* log_file_path);
92
93/**
94 * @brief Submits a log message to the library's unified logging system.
95 * This ensures CLI messages match the format (timestamps, alignment) of library messages.
96 */
97void fers_log(fers_log_level_t level, const char* message);
98
99/**
100 * @brief Sets the number of worker threads for the simulation.
101 * @param num_threads The number of threads to use.
102 * @return 0 on success, non-zero on error.
103 */
104int fers_set_thread_count(unsigned num_threads);
105
106// --- Scenario Loading & Serialization ---
107
108/**
109 * @brief Loads a scenario into the context from a FERS XML file.
110 *
111 * This is the standard method for initializing a simulation context from a file on
112 * disk. It is essential for interoperability with the CLI and legacy workflows that
113 * rely on the FERS XML format.
114 *
115 * @param context A valid `fers_context_t` handle.
116 * @param xml_filepath A null-terminated UTF-8 string for the input XML file path.
117 * @param validate A boolean (0 or 1) indicating whether to validate the XML
118 * against the embedded FERS schema. Validation is recommended to
119 * ensure scenario correctness.
120 * @return 0 on success, a non-zero error code on failure. Use
121 * `fers_get_last_error_message()` to retrieve error details.
122 */
123int fers_load_scenario_from_xml_file(fers_context_t* context, const char* xml_filepath, int validate);
124
125/**
126 * @brief Loads a scenario into the context from a FERS XML string.
127 *
128 * This function provides a way to load a scenario from an in-memory string,
129 * avoiding file I/O. It is useful for test harnesses or for UIs that manage
130 * scenarios as text content before parsing.
131 *
132 * @param context A valid `fers_context_t` handle.
133 * @param xml_content A null-terminated UTF-8 string containing the FERS scenario in XML format.
134 * @param validate A boolean (0 or 1) indicating whether to validate the XML
135 * against the embedded FERS schema.
136 * @return 0 on success, a non-zero error code on failure. Use
137 * `fers_get_last_error_message()` to retrieve error details.
138 */
139int fers_load_scenario_from_xml_string(fers_context_t* context, const char* xml_content, int validate);
140
141/**
142 * @brief Serializes the current simulation scenario into a JSON string.
143 *
144 * This function is the primary method for the UI to retrieve the full state of
145 * the simulation. JSON is used as the interchange format because it is lightweight,
146 * human-readable, and natively supported by web technologies, making it trivial
147 * to parse and use in the React/TypeScript frontend.
148 *
149 * @note Memory Management: The returned string is allocated by this library and
150 * its ownership is transferred to the caller. It is crucial to free this
151 * string using `fers_free_string()` to prevent memory leaks.
152 *
153 * @param context A valid `fers_context_t` handle.
154 * @return A dynamically allocated, null-terminated C-string containing the
155 * JSON representation of the scenario. Returns NULL on failure.
156 */
158
159/**
160 * @brief Serializes the current simulation scenario into a FERS XML string.
161 *
162 * This function enables exporting the in-memory state back into the standard FERS
163 * XML file format. This is essential for interoperability with legacy tools and
164 * for allowing a user to save a scenario that was created or modified in the UI.
165 *
166 * @note Memory Management: The returned string is dynamically allocated and
167 * its ownership is transferred to the caller. It must be freed using
168 * `fers_free_string()` to prevent memory leaks.
169 *
170 * @param context A valid `fers_context_t` handle.
171 * @return A dynamically allocated, null-terminated C-string containing the
172 * XML representation of the scenario. Returns NULL on failure.
173 */
175
176/**
177 * @brief Updates the simulation scenario from a JSON string.
178 *
179 * This is the primary method for the UI to push its state back to the C++
180 * core. It performs a full replacement of the existing scenario.
181 *
182 * @param context A valid `fers_context_t` handle.
183 * @param scenario_json A null-terminated UTF-8 string containing the FERS scenario in JSON format.
184 * @return 0 on success.
185 * 1 on generic logic error.
186 * 2 on JSON parsing/schema validation error.
187 * Use `fers_get_last_error_message()` to retrieve error details.
188 */
189int fers_update_scenario_from_json(fers_context_t* context, const char* scenario_json);
190
191
192// --- Error Handling ---
193
194/**
195 * @brief Retrieves the last error message that occurred on the current thread.
196 *
197 * Because C++ exceptions cannot safely propagate across the FFI boundary into
198 * other languages, this function provides the standard C-style error reporting
199 * mechanism. The error state is stored in a thread-local variable to ensure that
200 * concurrent API calls from different threads do not overwrite each other's error
201 * messages. The error is cleared at the start of each fallible API call.
202 *
203 * @note Memory Management: The returned string's ownership is transferred to the
204 * caller. It MUST be freed using `fers_free_string()` to prevent memory leaks.
205 *
206 * @return A dynamically allocated, null-terminated C-string containing the last
207 * error message, or NULL if no error has occurred.
208 */
210
211/**
212 * @brief Frees a string that was allocated and returned by the libfers API.
213 *
214 * This function must be used to release memory for any string returned by
215 * functions like `fers_get_scenario_as_json` or `fers_get_last_error_message`.
216 * It exists to ensure that the memory deallocation mechanism (`free`) matches the
217 * allocation mechanism (`strdup`/`malloc`) used within the C++ library, preventing
218 * potential crashes from mismatched allocators across language boundaries.
219 *
220 * @param str A pointer to the string to be freed. If str is NULL, no action is taken.
221 */
222void fers_free_string(char* str);
223
224
225// --- Simulation Execution ---
226
227/**
228 * @brief Runs the simulation defined in the provided context.
229 *
230 * This function is synchronous and will block the calling thread until the
231 * simulation is complete. This design keeps the API simple. For use in a
232 * responsive UI, it is the responsibility of the caller (e.g., the Tauri
233 * backend) to invoke this function on a separate worker thread to avoid
234 * freezing the user interface.
235 *
236 * @param context A valid `fers_context_t` handle containing a loaded scenario.
237 * @param callback A function pointer to a progress callback. Can be NULL.
238 * @param user_data An opaque pointer passed to the callback function.
239 * @return 0 on success, a non-zero error code on failure. Use
240 * `fers_get_last_error_message()` to retrieve error details.
241 */
242int fers_run_simulation(fers_context_t* context, fers_progress_callback_t callback, void* user_data);
243
244
245// --- Utility Functions ---
246
247/**
248 * @brief Generates a KML file for visualizing the scenario in the context.
249 *
250 * This utility exists to provide a simple, out-of-the-box method for users to
251 * validate and visualize the geographic layout and motion paths of their
252 * scenarios in common external tools like Google Earth.
253 *
254 * @param context A valid `fers_context_t` handle containing a loaded scenario.
255 * @param output_kml_filepath A null-terminated UTF-8 string for the output KML file path.
256 * @return 0 on success, a non-zero error code on failure. Use
257 * `fers_get_last_error_message()` to retrieve error details.
258 */
259int fers_generate_kml(const fers_context_t* context, const char* output_kml_filepath);
260
261// --- Antenna Pattern Utilities ---
262
263/**
264 * @brief Represents a sampled 2D antenna gain pattern.
265 * Gains are in linear scale (not dB), normalized to the antenna's peak gain.
266 * The data is structured as a flat array in row-major order (elevation rows, then azimuth columns).
267 * @note The `gains` array must be freed using `fers_free_antenna_pattern_data`.
268 */
269typedef struct
270{
271 double* gains; // Flat array of gain values [el_count * az_count]
272 size_t az_count; // Number of samples along azimuth (-180 to +180 deg)
273 size_t el_count; // Number of samples along elevation (-90 to +90 deg)
274 double max_gain; // The peak gain found in the pattern (linear scale)
276
277
278/**
279 * @brief Samples the gain pattern of a specified antenna and provides the data.
280 *
281 * This function calculates the antenna's far-field gain at a specified resolution
282 * over the full sphere of directions (azimuth and elevation). The resulting gain
283 * values are linear (not in dB) and normalized relative to the pattern's peak gain.
284 * This is a stateless utility useful for UI previews and analysis.
285 *
286 * @param context A valid `fers_context_t` handle containing a loaded scenario with the antenna.
287 * @param antenna_name The name of the antenna asset to sample.
288 * @param az_samples The desired number of sample points along the azimuth axis.
289 * @param el_samples The desired number of sample points along the elevation axis.
290 * @param frequency_hz The frequency in Hz to use for gain calculation (affects aperture antennas).
291 * @return A pointer to a `fers_antenna_pattern_data_t` struct containing the results.
292 * Returns NULL on failure (e.g., antenna not found). The caller owns the
293 * returned struct and must free it with `fers_free_antenna_pattern_data`.
294 */
295fers_antenna_pattern_data_t* fers_get_antenna_pattern(const fers_context_t* context, const char* antenna_name,
296 size_t az_samples, size_t el_samples, double frequency_hz);
297
298/**
299 * @brief Frees the memory allocated for an antenna pattern data structure.
300 * @param data A pointer to the `fers_antenna_pattern_data_t` struct to free.
301 */
303
304
305// --- Path Interpolation Utilities ---
306
307/**
308 * @brief Defines the interpolation methods available for path generation.
309 * This enum provides a language-agnostic way to specify the desired
310 * interpolation algorithm when calling the path generation functions.
311 */
318
319/**
320 * @brief Represents a single waypoint for a motion path.
321 * Coordinates are in the scenario's defined coordinate system (e.g., ENU meters).
322 */
323typedef struct
324{
325 double time; /**< Time in seconds. */
326 double x; /**< X coordinate in meters (East in ENU). */
327 double y; /**< Y coordinate in meters (North in ENU). */
328 double z; /**< Z coordinate in meters (Up/Altitude in ENU). */
330
331/**
332 * @brief Represents a single waypoint for a rotation path.
333 * Angles are in compass degrees (CW from North).
334 */
335typedef struct
336{
337 double time; /**< Time in seconds. */
338 double azimuth_deg; /**< Azimuth angle in compass degrees (0=North, 90=East). */
339 double elevation_deg; /**< Elevation angle in degrees (positive up). */
341
342/**
343 * @brief Represents a single interpolated point on a motion path.
344 * Includes position and velocity in the scenario's coordinate frame.
345 */
346typedef struct
347{
348 double x; /**< X position in meters. */
349 double y; /**< Y position in meters. */
350 double z; /**< Z position in meters. */
351 double vx; /**< X velocity in m/s. */
352 double vy; /**< Y velocity in m/s. */
353 double vz; /**< Z velocity in m/s. */
355
356/**
357 * @brief Represents a single interpolated point on a rotation path.
358 * Angles are in compass degrees (CW from North).
359 */
360typedef struct
361{
362 double azimuth_deg; /**< Azimuth angle in compass degrees. */
363 double elevation_deg; /**< Elevation angle in degrees. */
365
366
367/**
368 * @brief A container for an array of interpolated motion path points.
369 * @note The `points` array must be freed using `fers_free_interpolated_motion_path`.
370 */
376
377/**
378 * @brief A container for an array of interpolated rotation path points.
379 * @note The `points` array must be freed using `fers_free_interpolated_rotation_path`.
380 */
386
387
388/**
389 * @brief Calculates an interpolated motion path from a set of waypoints.
390 * This function is a stateless utility that computes the path without needing a
391 * full simulation context. It is useful for UI previews.
392 *
393 * @param waypoints An array of `fers_motion_waypoint_t` structs.
394 * @param waypoint_count The number of waypoints in the array.
395 * @param interp_type The interpolation algorithm to use.
396 * @param num_points The desired number of points in the output interpolated path.
397 * @return A pointer to a `fers_interpolated_path_t` struct containing the results.
398 * Returns NULL on failure. The caller owns the returned struct and must
399 * free it with `fers_free_interpolated_motion_path`.
400 */
402 size_t waypoint_count, fers_interp_type_t interp_type,
403 size_t num_points);
404
405/**
406 * @brief Frees the memory allocated for an interpolated motion path.
407 * @param path A pointer to the `fers_interpolated_path_t` struct to free.
408 */
410
411/**
412 * @brief Calculates an interpolated rotation path from a set of waypoints.
413 * This function is a stateless utility for UI previews.
414 *
415 * @param waypoints An array of `fers_rotation_waypoint_t` structs.
416 * @param waypoint_count The number of waypoints in the array.
417 * @param interp_type The interpolation algorithm to use (STATIC, LINEAR, CUBIC).
418 * @param num_points The desired number of points in the output interpolated path.
419 * @return A pointer to a `fers_interpolated_rotation_path_t` struct containing the results.
420 * Returns NULL on failure. The caller owns the returned struct and must
421 * free it with `fers_free_interpolated_rotation_path`.
422 */
424 size_t waypoint_count,
425 fers_interp_type_t interp_type,
426 size_t num_points);
427
428/**
429 * @brief Frees the memory allocated for an interpolated rotation path.
430 * @param path A pointer to the `fers_interpolated_rotation_path_t` struct to free.
431 */
433
434// --- Preview Link Calculation ---
435
436/**
437 * @brief Quality of the radio link based on SNR.
438 */
439typedef enum
440{
441 FERS_LINK_STRONG, // SNR > 0 dB
442 FERS_LINK_WEAK // SNR < 0 dB (Geometric possibility but sub-noise)
444
445/**
446 * @brief Type of visual link to render.
447 */
448typedef enum
449{
450 FERS_LINK_MONOSTATIC, // Combined Tx/Rx path
451 FERS_LINK_BISTATIC_TX_TGT, // Illuminator path
452 FERS_LINK_BISTATIC_TGT_RX, // Scattered path
453 FERS_LINK_DIRECT_TX_RX // Interference path
455
456/**
457 * @brief Represents a single renderable line segment metadata.
458 * Geometry is resolved client-side.
459 */
460typedef struct
461{
462 fers_link_type_t type; /**< Type of the link (Monostatic, Bistatic, etc.). */
463 fers_link_quality_t quality; /**< Signal quality (Strong/Weak). */
464 char label[128]; /**< Pre-formatted label (e.g., "-95 dBm"). */
465 char source_name[64]; /**< Name of the source component (e.g. Transmitter). */
466 char dest_name[64]; /**< Name of the destination component (e.g. Receiver/Target). */
467 char origin_name[64]; /**< Name of the originating Transmitter (for scattered paths). */
469
470/**
471 * @brief A container for a list of visual links.
472 * @note The `links` array is owned by this struct and must be freed using `fers_free_preview_links`.
473 */
479
480/**
481 * @brief Calculates visual links for a specific simulation time.
482 * @param context The simulation context.
483 * @param time The simulation time in seconds.
484 * @return A pointer to a link list. Caller must free with fers_free_preview_links.
485 */
487
488/**
489 * @brief Frees the memory allocated for a preview link list.
490 * @param list The list to free.
491 */
493
494#ifdef __cplusplus
495}
496#endif
497
498#endif
char * fers_get_scenario_as_xml(fers_context_t *context)
Serializes the current simulation scenario into a FERS XML string.
Definition api.cpp:270
void fers_free_antenna_pattern_data(fers_antenna_pattern_data_t *data)
Frees the memory allocated for an antenna pattern data structure.
Definition api.cpp:714
void(* fers_progress_callback_t)(const char *message, int current, int total, void *user_data)
A function pointer type for progress reporting callbacks.
Definition api.h:39
void fers_log(fers_log_level_t level, const char *message)
Submits a log message to the library's unified logging system.
Definition api.cpp:136
int fers_load_scenario_from_xml_file(fers_context_t *context, const char *xml_filepath, int validate)
Loads a scenario into the context from a FERS XML file.
Definition api.cpp:162
fers_interpolated_rotation_path_t * fers_get_interpolated_rotation_path(const fers_rotation_waypoint_t *waypoints, size_t waypoint_count, fers_interp_type_t interp_type, size_t num_points)
Calculates an interpolated rotation path from a set of waypoints.
Definition api.cpp:533
char * fers_get_last_error_message()
Retrieves the last error message that occurred on the current thread.
Definition api.cpp:334
void fers_free_interpolated_motion_path(fers_interpolated_path_t *path)
Frees the memory allocated for an interpolated motion path.
Definition api.cpp:524
int fers_generate_kml(const fers_context_t *context, const char *output_kml_filepath)
Generates a KML file for visualizing the scenario in the context.
Definition api.cpp:390
fers_log_level_t
Log levels for the FERS library.
Definition api.h:76
@ FERS_LOG_FATAL
Definition api.h:82
@ FERS_LOG_DEBUG
Definition api.h:78
@ FERS_LOG_ERROR
Definition api.h:81
@ FERS_LOG_INFO
Definition api.h:79
@ FERS_LOG_TRACE
Definition api.h:77
@ FERS_LOG_WARNING
Definition api.h:80
void fers_context_destroy(fers_context_t *context)
Destroys a FERS simulation context and releases all associated memory.
Definition api.cpp:79
fers_context_t * fers_context_create()
Creates a new FERS simulation context.
Definition api.cpp:60
void fers_free_preview_links(fers_visual_link_list_t *list)
Frees the memory allocated for a preview link list.
Definition api.cpp:799
fers_interpolated_path_t * fers_get_interpolated_motion_path(const fers_motion_waypoint_t *waypoints, size_t waypoint_count, fers_interp_type_t interp_type, size_t num_points)
Calculates an interpolated motion path from a set of waypoints.
Definition api.cpp:450
int fers_run_simulation(fers_context_t *context, fers_progress_callback_t callback, void *user_data)
Runs the simulation defined in the provided context.
Definition api.cpp:354
void fers_free_string(char *str)
Frees a string that was allocated and returned by the libfers API.
Definition api.cpp:346
fers_antenna_pattern_data_t * fers_get_antenna_pattern(const fers_context_t *context, const char *antenna_name, size_t az_samples, size_t el_samples, double frequency_hz)
Samples the gain pattern of a specified antenna and provides the data.
Definition api.cpp:628
fers_visual_link_list_t * fers_calculate_preview_links(const fers_context_t *context, double time)
Calculates visual links for a specific simulation time.
Definition api.cpp:725
fers_interp_type_t
Defines the interpolation methods available for path generation.
Definition api.h:313
@ FERS_INTERP_CUBIC
Definition api.h:316
@ FERS_INTERP_STATIC
Definition api.h:314
@ FERS_INTERP_LINEAR
Definition api.h:315
int fers_configure_logging(fers_log_level_t level, const char *log_file_path)
Configures the internal logger.
Definition api.cpp:112
void fers_free_interpolated_rotation_path(fers_interpolated_rotation_path_t *path)
Frees the memory allocated for an interpolated rotation path.
Definition api.cpp:617
char * fers_get_scenario_as_json(fers_context_t *context)
Serializes the current simulation scenario into a JSON string.
Definition api.cpp:243
fers_link_type_t
Type of visual link to render.
Definition api.h:449
@ FERS_LINK_BISTATIC_TX_TGT
Definition api.h:451
@ FERS_LINK_MONOSTATIC
Definition api.h:450
@ FERS_LINK_BISTATIC_TGT_RX
Definition api.h:452
@ FERS_LINK_DIRECT_TX_RX
Definition api.h:453
int fers_load_scenario_from_xml_string(fers_context_t *context, const char *xml_content, int validate)
Loads a scenario into the context from a FERS XML string.
Definition api.cpp:202
int fers_update_scenario_from_json(fers_context_t *context, const char *scenario_json)
Updates the simulation scenario from a JSON string.
Definition api.cpp:300
int fers_set_thread_count(unsigned num_threads)
Sets the number of worker threads for the simulation.
Definition api.cpp:144
fers_link_quality_t
Quality of the radio link based on SNR.
Definition api.h:440
@ FERS_LINK_WEAK
Definition api.h:442
@ FERS_LINK_STRONG
Definition api.h:441
Represents a sampled 2D antenna gain pattern.
Definition api.h:270
A container for an array of interpolated motion path points.
Definition api.h:372
fers_interpolated_point_t * points
Definition api.h:373
Represents a single interpolated point on a motion path.
Definition api.h:347
double vz
Z velocity in m/s.
Definition api.h:353
double x
X position in meters.
Definition api.h:348
double y
Y position in meters.
Definition api.h:349
double z
Z position in meters.
Definition api.h:350
double vx
X velocity in m/s.
Definition api.h:351
double vy
Y velocity in m/s.
Definition api.h:352
A container for an array of interpolated rotation path points.
Definition api.h:382
fers_interpolated_rotation_point_t * points
Definition api.h:383
Represents a single interpolated point on a rotation path.
Definition api.h:361
double elevation_deg
Elevation angle in degrees.
Definition api.h:363
double azimuth_deg
Azimuth angle in compass degrees.
Definition api.h:362
Represents a single waypoint for a motion path.
Definition api.h:324
double x
X coordinate in meters (East in ENU).
Definition api.h:326
double time
Time in seconds.
Definition api.h:325
double y
Y coordinate in meters (North in ENU).
Definition api.h:327
double z
Z coordinate in meters (Up/Altitude in ENU).
Definition api.h:328
Represents a single waypoint for a rotation path.
Definition api.h:336
double time
Time in seconds.
Definition api.h:337
double elevation_deg
Elevation angle in degrees (positive up).
Definition api.h:339
double azimuth_deg
Azimuth angle in compass degrees (0=North, 90=East).
Definition api.h:338