|
FERS 1.0.0
The Flexible Extensible Radar Simulator
|
Implementation of the C-style FFI for the libfers core library. More...
#include <core/logging.h>#include <core/parameters.h>#include <cstring>#include <functional>#include <libfers/api.h>#include <math/path.h>#include <math/rotation_path.h>#include <nlohmann/json.hpp>#include <string>#include "core/fers_context.h"#include "core/sim_threading.h"#include "core/thread_pool.h"#include "serial/json_serializer.h"#include "serial/kml_generator.h"#include "serial/xml_parser.h"#include "serial/xml_serializer.h"#include "simulation/channel_model.h"
Include dependency graph for api.cpp:Go to the source code of this file.
Classes | |
| struct | fers_context |
Functions | |
| static void | handle_api_exception (const std::exception &e, const std::string &function_name) |
| Centralized exception handler for the C-API boundary. | |
| fers_context_t * | fers_context_create () |
| Creates a new FERS simulation context. | |
| void | fers_context_destroy (fers_context_t *context) |
| Destroys a FERS simulation context and releases all associated memory. | |
| static logging::Level | map_level (fers_log_level_t level) |
| int | fers_configure_logging (fers_log_level_t level, const char *log_file_path) |
| Configures the internal logger. | |
| void | fers_log (fers_log_level_t level, const char *message) |
| Submits a log message to the library's unified logging system. | |
| int | fers_set_thread_count (unsigned num_threads) |
| Sets the number of worker threads for the simulation. | |
| int | fers_load_scenario_from_xml_file (fers_context_t *context, const char *xml_filepath, const int validate) |
| Loads a scenario into the context from a FERS XML file. | |
| int | fers_load_scenario_from_xml_string (fers_context_t *context, const char *xml_content, const int validate) |
| Loads a scenario into the context from a FERS XML string. | |
| char * | fers_get_scenario_as_json (fers_context_t *context) |
| Serializes the current simulation scenario into a JSON string. | |
| char * | fers_get_scenario_as_xml (fers_context_t *context) |
| Serializes the current simulation scenario into a FERS XML string. | |
| int | fers_update_scenario_from_json (fers_context_t *context, const char *scenario_json) |
| Updates the simulation scenario from a JSON string. | |
| char * | fers_get_last_error_message () |
| Retrieves the last error message that occurred on the current thread. | |
| void | fers_free_string (char *str) |
| Frees a string that was allocated and returned by the libfers API. | |
| int | fers_run_simulation (fers_context_t *context, fers_progress_callback_t callback, void *user_data) |
| Runs the simulation defined in the provided context. | |
| 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. | |
| math::Path::InterpType | to_cpp_interp_type (const fers_interp_type_t type) |
| math::RotationPath::InterpType | to_cpp_rot_interp_type (const fers_interp_type_t type) |
| fers_interpolated_path_t * | fers_get_interpolated_motion_path (const fers_motion_waypoint_t *waypoints, const size_t waypoint_count, const fers_interp_type_t interp_type, const size_t num_points) |
| Calculates an interpolated motion path from a set of waypoints. | |
| void | fers_free_interpolated_motion_path (fers_interpolated_path_t *path) |
| Frees the memory allocated for an interpolated motion path. | |
| fers_interpolated_rotation_path_t * | fers_get_interpolated_rotation_path (const fers_rotation_waypoint_t *waypoints, const size_t waypoint_count, const fers_interp_type_t interp_type, const size_t num_points) |
| Calculates an interpolated rotation path from a set of waypoints. | |
| void | fers_free_interpolated_rotation_path (fers_interpolated_rotation_path_t *path) |
| Frees the memory allocated for an interpolated rotation path. | |
| fers_antenna_pattern_data_t * | fers_get_antenna_pattern (const fers_context_t *context, const char *antenna_name, const size_t az_samples, const size_t el_samples, const double frequency_hz) |
| Samples the gain pattern of a specified antenna and provides the data. | |
| void | fers_free_antenna_pattern_data (fers_antenna_pattern_data_t *data) |
| Frees the memory allocated for an antenna pattern data structure. | |
| fers_visual_link_list_t * | fers_calculate_preview_links (const fers_context_t *context, const double time) |
| Calculates visual links for a specific simulation time. | |
| void | fers_free_preview_links (fers_visual_link_list_t *list) |
| Frees the memory allocated for a preview link list. | |
Variables | |
| thread_local std::string | last_error_message |
Implementation of the C-style FFI for the libfers core library.
This file provides the C implementations for the functions declared in api.h. It acts as the bridge between the C ABI and the C++ core, handling object creation/destruction, exception catching, error reporting, and type casting.
Definition in file api.cpp.
| fers_visual_link_list_t * fers_calculate_preview_links | ( | const fers_context_t * | context, |
| double | time | ||
| ) |
Calculates visual links for a specific simulation time.
| context | The simulation context. |
| time | The simulation time in seconds. |
Definition at line 725 of file api.cpp.
References simulation::BistaticTgtRx, simulation::BistaticTxTgt, simulation::calculatePreviewLinks(), simulation::DirectTxRx, logging::ERROR, FERS_LINK_BISTATIC_TGT_RX, FERS_LINK_BISTATIC_TX_TGT, FERS_LINK_DIRECT_TX_RX, FERS_LINK_MONOSTATIC, FERS_LINK_STRONG, FERS_LINK_WEAK, handle_api_exception(), last_error_message, LOG, simulation::Monostatic, simulation::Strong, and fers_visual_link_t::type.
Here is the call graph for this function:| int fers_configure_logging | ( | fers_log_level_t | level, |
| const char * | log_file_path | ||
| ) |
Configures the internal logger.
| level | The minimum severity level to log. |
| log_file_path | Optional path to a log file. Pass NULL to disable file logging. |
Definition at line 112 of file api.cpp.
References handle_api_exception(), last_error_message, logging::logger, logging::Logger::logToFile(), map_level(), and logging::Logger::setLevel().
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| fers_context_t * fers_context_create | ( | ) |
Creates a new FERS simulation context.
Allocates and initializes a new, empty simulation context in memory. This context serves as the container for a scenario loaded via one of the fers_load_... or fers_update_... functions.
fers_context_destroy() to prevent resource leaks.Definition at line 60 of file api.cpp.
References handle_api_exception(), and last_error_message.
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| void fers_context_destroy | ( | fers_context_t * | context | ) |
Destroys a FERS simulation context and releases all associated memory.
This function must be called for every context created by fers_context_create() to ensure proper cleanup of the underlying C++ objects. Accessing the context handle after calling this function results in undefined behavior.
| context | A valid pointer to a fers_context_t handle. If context is NULL, the function performs no action for safety. |
Definition at line 79 of file api.cpp.
References last_error_message, LOG, and logging::WARNING.
Referenced by main().
Here is the caller graph for this function:| void fers_free_antenna_pattern_data | ( | fers_antenna_pattern_data_t * | data | ) |
Frees the memory allocated for an antenna pattern data structure.
| data | A pointer to the fers_antenna_pattern_data_t struct to free. |
Definition at line 714 of file api.cpp.
References fers_antenna_pattern_data_t::gains.
| void fers_free_interpolated_motion_path | ( | fers_interpolated_path_t * | path | ) |
Frees the memory allocated for an interpolated motion path.
| path | A pointer to the fers_interpolated_path_t struct to free. |
Definition at line 524 of file api.cpp.
References fers_interpolated_path_t::points.
| void fers_free_interpolated_rotation_path | ( | fers_interpolated_rotation_path_t * | path | ) |
Frees the memory allocated for an interpolated rotation path.
| path | A pointer to the fers_interpolated_rotation_path_t struct to free. |
Definition at line 617 of file api.cpp.
References fers_interpolated_rotation_path_t::points.
| void fers_free_preview_links | ( | fers_visual_link_list_t * | list | ) |
Frees the memory allocated for a preview link list.
| list | The list to free. |
Definition at line 799 of file api.cpp.
References fers_visual_link_list_t::links.
| void fers_free_string | ( | char * | str | ) |
Frees a string that was allocated and returned by the libfers API.
This function must be used to release memory for any string returned by functions like fers_get_scenario_as_json or fers_get_last_error_message. It exists to ensure that the memory deallocation mechanism (free) matches the allocation mechanism (strdup/malloc) used within the C++ library, preventing potential crashes from mismatched allocators across language boundaries.
| str | A pointer to the string to be freed. If str is NULL, no action is taken. |
Definition at line 346 of file api.cpp.
Referenced by main().
Here is the caller graph for this function:| 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.
This utility exists to provide a simple, out-of-the-box method for users to validate and visualize the geographic layout and motion paths of their scenarios in common external tools like Google Earth.
| context | A valid fers_context_t handle containing a loaded scenario. |
| output_kml_filepath | A null-terminated UTF-8 string for the output KML file path. |
fers_get_last_error_message() to retrieve error details. Definition at line 390 of file api.cpp.
References logging::ERROR, serial::KmlGenerator::generateKml(), handle_api_exception(), last_error_message, and LOG.
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| 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.
This function calculates the antenna's far-field gain at a specified resolution over the full sphere of directions (azimuth and elevation). The resulting gain values are linear (not in dB) and normalized relative to the pattern's peak gain. This is a stateless utility useful for UI previews and analysis.
| context | A valid fers_context_t handle containing a loaded scenario with the antenna. |
| antenna_name | The name of the antenna asset to sample. |
| az_samples | The desired number of sample points along the azimuth axis. |
| el_samples | The desired number of sample points along the elevation axis. |
| frequency_hz | The frequency in Hz to use for gain calculation (affects aperture antennas). |
fers_antenna_pattern_data_t struct containing the results. Returns NULL on failure (e.g., antenna not found). The caller owns the returned struct and must free it with fers_free_antenna_pattern_data. Definition at line 628 of file api.cpp.
References params::c(), logging::ERROR, antenna::Antenna::getGain(), handle_api_exception(), last_error_message, LOG, and PI.
Here is the call graph for this function:| 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.
This function is a stateless utility that computes the path without needing a full simulation context. It is useful for UI previews.
| waypoints | An array of fers_motion_waypoint_t structs. |
| waypoint_count | The number of waypoints in the array. |
| interp_type | The interpolation algorithm to use. |
| num_points | The desired number of points in the output interpolated path. |
fers_interpolated_path_t struct containing the results. Returns NULL on failure. The caller owns the returned struct and must free it with fers_free_interpolated_motion_path. Definition at line 450 of file api.cpp.
References math::Path::addCoord(), logging::ERROR, FERS_INTERP_CUBIC, math::Path::finalize(), math::Path::getPosition(), math::Path::getVelocity(), handle_api_exception(), last_error_message, LOG, math::Path::setInterp(), fers_motion_waypoint_t::time, to_cpp_interp_type(), fers_motion_waypoint_t::x, math::Vec3::x, fers_motion_waypoint_t::y, math::Vec3::y, fers_motion_waypoint_t::z, and math::Vec3::z.
Here is the call graph for this function:| 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.
This function is a stateless utility for UI previews.
| waypoints | An array of fers_rotation_waypoint_t structs. |
| waypoint_count | The number of waypoints in the array. |
| interp_type | The interpolation algorithm to use (STATIC, LINEAR, CUBIC). |
| num_points | The desired number of points in the output interpolated path. |
fers_interpolated_rotation_path_t struct containing the results. Returns NULL on failure. The caller owns the returned struct and must free it with fers_free_interpolated_rotation_path. Definition at line 533 of file api.cpp.
References math::RotationPath::addCoord(), math::SVec3::azimuth, fers_rotation_waypoint_t::azimuth_deg, math::SVec3::elevation, fers_rotation_waypoint_t::elevation_deg, logging::ERROR, FERS_INTERP_CUBIC, math::RotationPath::finalize(), math::RotationPath::getPosition(), handle_api_exception(), last_error_message, LOG, PI, math::RotationPath::setInterp(), fers_rotation_waypoint_t::time, and to_cpp_rot_interp_type().
Here is the call graph for this function:| char * fers_get_last_error_message | ( | ) |
Retrieves the last error message that occurred on the current thread.
Because C++ exceptions cannot safely propagate across the FFI boundary into other languages, this function provides the standard C-style error reporting mechanism. The error state is stored in a thread-local variable to ensure that concurrent API calls from different threads do not overwrite each other's error messages. The error is cleared at the start of each fallible API call.
fers_free_string() to prevent memory leaks.Definition at line 334 of file api.cpp.
References last_error_message.
Referenced by main().
Here is the caller graph for this function:| char * fers_get_scenario_as_json | ( | fers_context_t * | context | ) |
Serializes the current simulation scenario into a JSON string.
This function is the primary method for the UI to retrieve the full state of the simulation. JSON is used as the interchange format because it is lightweight, human-readable, and natively supported by web technologies, making it trivial to parse and use in the React/TypeScript frontend.
fers_free_string() to prevent memory leaks.| context | A valid fers_context_t handle. |
Definition at line 243 of file api.cpp.
References logging::ERROR, handle_api_exception(), last_error_message, LOG, and serial::world_to_json().
Here is the call graph for this function:| char * fers_get_scenario_as_xml | ( | fers_context_t * | context | ) |
Serializes the current simulation scenario into a FERS XML string.
This function enables exporting the in-memory state back into the standard FERS XML file format. This is essential for interoperability with legacy tools and for allowing a user to save a scenario that was created or modified in the UI.
fers_free_string() to prevent memory leaks.| context | A valid fers_context_t handle. |
Definition at line 270 of file api.cpp.
References logging::ERROR, handle_api_exception(), last_error_message, LOG, and serial::world_to_xml_string().
Here is the call graph for this function:| 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.
This is the standard method for initializing a simulation context from a file on disk. It is essential for interoperability with the CLI and legacy workflows that rely on the FERS XML format.
| context | A valid fers_context_t handle. |
| xml_filepath | A null-terminated UTF-8 string for the input XML file path. |
| validate | A boolean (0 or 1) indicating whether to validate the XML against the embedded FERS schema. Validation is recommended to ensure scenario correctness. |
fers_get_last_error_message() to retrieve error details. Definition at line 162 of file api.cpp.
References logging::ERROR, handle_api_exception(), logging::INFO, last_error_message, LOG, params::params, serial::parseSimulation(), and params::Parameters::random_seed.
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| 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.
This function provides a way to load a scenario from an in-memory string, avoiding file I/O. It is useful for test harnesses or for UIs that manage scenarios as text content before parsing.
| context | A valid fers_context_t handle. |
| xml_content | A null-terminated UTF-8 string containing the FERS scenario in XML format. |
| validate | A boolean (0 or 1) indicating whether to validate the XML against the embedded FERS schema. |
fers_get_last_error_message() to retrieve error details. Definition at line 202 of file api.cpp.
References logging::ERROR, handle_api_exception(), logging::INFO, last_error_message, LOG, params::params, serial::parseSimulationFromString(), and params::Parameters::random_seed.
Here is the call graph for this function:| void fers_log | ( | fers_log_level_t | level, |
| const char * | message | ||
| ) |
Submits a log message to the library's unified logging system.
This ensures CLI messages match the format (timestamps, alignment) of library messages.
Definition at line 136 of file api.cpp.
References logging::Logger::log(), logging::logger, and map_level().
Here is the call graph for this function:| int fers_run_simulation | ( | fers_context_t * | context, |
| fers_progress_callback_t | callback, | ||
| void * | user_data | ||
| ) |
Runs the simulation defined in the provided context.
This function is synchronous and will block the calling thread until the simulation is complete. This design keeps the API simple. For use in a responsive UI, it is the responsibility of the caller (e.g., the Tauri backend) to invoke this function on a separate worker thread to avoid freezing the user interface.
| context | A valid fers_context_t handle containing a loaded scenario. |
| callback | A function pointer to a progress callback. Can be NULL. |
| user_data | An opaque pointer passed to the callback function. |
fers_get_last_error_message() to retrieve error details. Definition at line 354 of file api.cpp.
References logging::ERROR, handle_api_exception(), last_error_message, LOG, params::renderThreads(), and core::runEventDrivenSim().
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| int fers_set_thread_count | ( | unsigned | num_threads | ) |
Sets the number of worker threads for the simulation.
| num_threads | The number of threads to use. |
Definition at line 144 of file api.cpp.
References handle_api_exception(), last_error_message, and params::setThreads().
Referenced by main().
Here is the call graph for this function:
Here is the caller graph for this function:| int fers_update_scenario_from_json | ( | fers_context_t * | context, |
| const char * | scenario_json | ||
| ) |
Updates the simulation scenario from a JSON string.
This is the primary method for the UI to push its state back to the C++ core. It performs a full replacement of the existing scenario.
| context | A valid fers_context_t handle. |
| scenario_json | A null-terminated UTF-8 string containing the FERS scenario in JSON format. |
fers_get_last_error_message() to retrieve error details. Definition at line 300 of file api.cpp.
References logging::ERROR, handle_api_exception(), serial::json_to_world(), last_error_message, and LOG.
Here is the call graph for this function:
|
static |
Centralized exception handler for the C-API boundary.
This function catches standard C++ exceptions, records their what() message into the thread-local error storage, and logs the error. This prevents C++ exceptions from propagating across the FFI boundary, which would be undefined behavior.
| e | The exception that was caught. |
| function_name | The name of the API function where the error occurred. |
Definition at line 52 of file api.cpp.
References logging::ERROR, last_error_message, and LOG.
Referenced by fers_calculate_preview_links(), fers_configure_logging(), fers_context_create(), fers_generate_kml(), fers_get_antenna_pattern(), fers_get_interpolated_motion_path(), fers_get_interpolated_rotation_path(), fers_get_scenario_as_json(), fers_get_scenario_as_xml(), fers_load_scenario_from_xml_file(), fers_load_scenario_from_xml_string(), fers_run_simulation(), fers_set_thread_count(), and fers_update_scenario_from_json().
Here is the caller graph for this function:
|
static |
Definition at line 91 of file api.cpp.
References logging::DEBUG, logging::ERROR, logging::FATAL, FERS_LOG_DEBUG, FERS_LOG_ERROR, FERS_LOG_FATAL, FERS_LOG_INFO, FERS_LOG_TRACE, FERS_LOG_WARNING, logging::INFO, logging::TRACE, and logging::WARNING.
Referenced by fers_configure_logging(), and fers_log().
Here is the caller graph for this function:| math::Path::InterpType to_cpp_interp_type | ( | const fers_interp_type_t | type | ) |
Definition at line 421 of file api.cpp.
References FERS_INTERP_CUBIC, FERS_INTERP_LINEAR, FERS_INTERP_STATIC, math::Path::INTERP_CUBIC, math::Path::INTERP_LINEAR, and math::Path::INTERP_STATIC.
Referenced by fers_get_interpolated_motion_path().
Here is the caller graph for this function:| math::RotationPath::InterpType to_cpp_rot_interp_type | ( | const fers_interp_type_t | type | ) |
Definition at line 435 of file api.cpp.
References FERS_INTERP_CUBIC, FERS_INTERP_LINEAR, FERS_INTERP_STATIC, math::RotationPath::INTERP_CUBIC, math::RotationPath::INTERP_LINEAR, and math::RotationPath::INTERP_STATIC.
Referenced by fers_get_interpolated_rotation_path().
Here is the caller graph for this function:| thread_local std::string last_error_message |
Definition at line 41 of file api.cpp.
Referenced by fers_calculate_preview_links(), fers_configure_logging(), fers_context_create(), fers_context_destroy(), fers_generate_kml(), fers_get_antenna_pattern(), fers_get_interpolated_motion_path(), fers_get_interpolated_rotation_path(), fers_get_last_error_message(), fers_get_scenario_as_json(), fers_get_scenario_as_xml(), fers_load_scenario_from_xml_file(), fers_load_scenario_from_xml_string(), fers_run_simulation(), fers_set_thread_count(), fers_update_scenario_from_json(), and handle_api_exception().