13#include "libe3/version.hpp"
18constexpr const char* LOG_TAG =
"E3Agent";
48 : impl_(std::make_unique<
Impl>(std::move(
config)))
50 E3_LOG_INFO(LOG_TAG) <<
"E3Agent created (version " << LIBE3_VERSION_STRING <<
")";
66 if (impl_->interface) {
67 E3_LOG_WARN(LOG_TAG) <<
"Agent already initialized";
68 return ErrorCode::ALREADY_INITIALIZED;
73 impl_->interface = std::make_unique<E3Interface>(impl_->config);
75 ErrorCode result = impl_->interface->init();
76 if (result != ErrorCode::SUCCESS) {
77 impl_->interface.reset();
78 E3_LOG_ERROR(LOG_TAG) <<
"Failed to initialize interface: "
83 if (impl_->dapp_report_handler) {
84 impl_->interface->set_dapp_report_handler(impl_->dapp_report_handler);
87 if (impl_->dapp_status_changed_handler) {
88 impl_->interface->set_dapp_status_changed_handler(impl_->dapp_status_changed_handler);
91 if (impl_->setup_response_handler) {
92 impl_->interface->set_setup_response_handler(impl_->setup_response_handler);
94 if (impl_->subscription_response_handler) {
95 impl_->interface->set_subscription_response_handler(impl_->subscription_response_handler);
97 if (impl_->indication_handler) {
98 impl_->interface->set_indication_handler(impl_->indication_handler);
100 if (impl_->xapp_control_handler) {
101 impl_->interface->set_xapp_control_handler(impl_->xapp_control_handler);
103 if (impl_->message_ack_handler) {
104 impl_->interface->set_message_ack_handler(impl_->message_ack_handler);
106 if (impl_->control_response_handler) {
107 impl_->interface->set_control_response_handler(impl_->control_response_handler);
109 if (impl_->disconnect_handler) {
110 impl_->interface->set_disconnect_handler(impl_->disconnect_handler);
113 E3_LOG_INFO(LOG_TAG) <<
"E3Agent initialized successfully";
114 return ErrorCode::SUCCESS;
119 if (!impl_->interface) {
121 if (init_result != ErrorCode::SUCCESS) {
128 ErrorCode result = impl_->interface->start();
129 if (result != ErrorCode::SUCCESS) {
135 E3_LOG_INFO(LOG_TAG) <<
"E3Agent started successfully";
136 return ErrorCode::SUCCESS;
140 if (!impl_ || !impl_->interface) {
145 impl_->interface->stop();
150 if (!impl_ || !impl_->interface) {
153 return impl_->interface->state();
157 return impl_ && impl_->interface && impl_->interface->is_running();
166 return ErrorCode::INVALID_PARAM;
169 E3_LOG_ERROR(LOG_TAG) <<
"register_sm() is only valid on RAN role";
170 return ErrorCode::STATE_ERROR;
174 if (!impl_->interface) {
176 if (init_result != ErrorCode::SUCCESS) {
181 return impl_->interface->register_sm(std::move(sm));
185 if (!impl_->interface) {
188 return impl_->interface->get_available_ran_functions();
192 impl_->dapp_report_handler = std::move(handler);
193 if (impl_->interface && impl_->dapp_report_handler) {
194 impl_->interface->set_dapp_report_handler(impl_->dapp_report_handler);
199 impl_->dapp_status_changed_handler = std::move(handler);
200 if (impl_->interface && impl_->dapp_status_changed_handler) {
201 impl_->interface->set_dapp_status_changed_handler(impl_->dapp_status_changed_handler);
211 uint32_t ran_function_id,
212 const std::vector<uint8_t>& data
215 return ErrorCode::STATE_ERROR;
217 if (!impl_->interface || !impl_->interface->is_running()) {
218 return ErrorCode::NOT_INITIALIZED;
227 uint32_t mid = impl_->interface->generate_message_id();
235 return impl_->interface->queue_outbound(std::move(pdu));
240 uint32_t ran_function_id,
241 const std::vector<uint8_t>& control_data,
242 uint32_t sequence_id,
243 uint32_t* out_message_id,
247 return ErrorCode::STATE_ERROR;
249 if (!impl_->interface || !impl_->interface->is_running()) {
250 return ErrorCode::NOT_INITIALIZED;
253 pdu.
message_id = impl_->interface->generate_message_id();
262 ErrorCode rc = impl_->interface->queue_outbound(std::move(pdu));
263 if (rc == ErrorCode::SUCCESS && out_message_id) {
264 *out_message_id = message_id;
270 if (!impl_->interface || !impl_->interface->is_running()) {
271 return ErrorCode::NOT_INITIALIZED;
274 pdu.
message_id = impl_->interface->generate_message_id();
279 return impl_->interface->queue_outbound(std::move(pdu));
283 if (!impl_->interface || impl_->config.role !=
E3Role::RAN) {
286 return impl_->interface->subscription_manager().get_registered_dapps();
290 if (!impl_->interface || impl_->config.role !=
E3Role::RAN) {
293 return impl_->interface->subscription_manager().get_dapp_subscriptions(
dapp_id);
297 if (!impl_->interface || impl_->config.role !=
E3Role::RAN) {
300 return impl_->interface->subscription_manager().get_subscribed_dapps(ran_function_id);
304 if (!impl_->interface || impl_->config.role !=
E3Role::RAN) {
307 return impl_->interface->subscription_manager().get_active_ran_functions();
311 if (!impl_->interface) {
314 const auto details = impl_->interface->subscription_manager()
315 .get_subscription_details(
dapp_id, ran_function_id);
316 return details ? details->periodicity_us : 0;
320 if (!impl_->interface) {
323 const auto details = impl_->interface->subscription_manager()
324 .get_subscription_details(
dapp_id, ran_function_id);
325 return details ? details->telemetry_ids : std::vector<uint32_t>{};
329 if (!impl_->interface) {
332 const auto details = impl_->interface->subscription_manager()
333 .get_subscription_details(
dapp_id, ran_function_id);
334 return details ? details->control_ids : std::vector<uint32_t>{};
342 return impl_->config;
350 if (!impl_->interface) {
354 return impl_->interface->subscription_manager().dapp_count();
358 return impl_->interface->dapp_id().has_value() ? 1u : 0u;
362 if (!impl_->interface) {
366 return impl_->interface->subscription_manager().subscription_count();
368 return impl_->interface->active_subscription_ids().size();
376 impl_->setup_response_handler = std::move(handler);
377 if (impl_->interface && impl_->setup_response_handler) {
378 impl_->interface->set_setup_response_handler(impl_->setup_response_handler);
383 impl_->subscription_response_handler = std::move(handler);
384 if (impl_->interface && impl_->subscription_response_handler) {
385 impl_->interface->set_subscription_response_handler(impl_->subscription_response_handler);
390 impl_->indication_handler = std::move(handler);
391 if (impl_->interface && impl_->indication_handler) {
392 impl_->interface->set_indication_handler(impl_->indication_handler);
397 impl_->xapp_control_handler = std::move(handler);
398 if (impl_->interface && impl_->xapp_control_handler) {
399 impl_->interface->set_xapp_control_handler(impl_->xapp_control_handler);
404 impl_->message_ack_handler = std::move(handler);
405 if (impl_->interface && impl_->message_ack_handler) {
406 impl_->interface->set_message_ack_handler(impl_->message_ack_handler);
411 impl_->control_response_handler = std::move(handler);
412 if (impl_->interface && impl_->control_response_handler) {
413 impl_->interface->set_control_response_handler(impl_->control_response_handler);
418 impl_->disconnect_handler = std::move(handler);
419 if (impl_->interface && impl_->disconnect_handler) {
420 impl_->interface->set_disconnect_handler(impl_->disconnect_handler);
425 if (!impl_->interface || impl_->config.role !=
E3Role::DAPP)
return std::nullopt;
426 return impl_->interface->dapp_id();
430 if (!impl_->interface || impl_->config.role !=
E3Role::DAPP)
return {};
431 return impl_->interface->subscribed_ran_functions();
435 if (!impl_->interface || impl_->config.role !=
E3Role::DAPP)
return {};
436 return impl_->interface->active_subscription_ids();
440 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::INVALID_PARAM;
441 if (!impl_->interface)
return ErrorCode::NOT_INITIALIZED;
442 return impl_->interface->wait_for_setup(timeout);
446 std::vector<uint32_t> telemetry_ids,
447 std::vector<uint32_t> control_ids,
448 std::optional<uint32_t> sub_time,
449 std::optional<uint32_t> periodicity,
450 uint32_t* out_request_id) {
451 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
452 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
453 return impl_->interface->queue_subscription_request(
454 ran_function_id, std::move(telemetry_ids), std::move(control_ids), sub_time, periodicity,
459 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
460 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
461 return impl_->interface->queue_subscription_delete(ran_function_id);
466 std::vector<uint8_t> action_data,
467 uint32_t sequence_id,
468 uint32_t* out_message_id,
470 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
471 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
472 return impl_->interface->queue_dapp_control_action(
473 ran_function_id, control_id, std::move(action_data), sequence_id, out_message_id, request_ack);
477 uint32_t ran_function_id,
480 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
481 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
482 return impl_->interface->queue_control_response(request_id, ran_function_id, control_id, result);
486 std::vector<uint8_t> report_data,
487 uint32_t sequence_id,
488 uint32_t* out_message_id) {
489 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
490 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
491 return impl_->interface->queue_dapp_report(
492 ran_function_id, std::move(report_data), sequence_id, out_message_id);
496 if (impl_->config.role !=
E3Role::DAPP)
return ErrorCode::STATE_ERROR;
497 if (!impl_->interface || !impl_->interface->is_running())
return ErrorCode::NOT_INITIALIZED;
498 return impl_->interface->queue_release_message();
E3Agent - Main façade for RAN vendor integration.
std::vector< uint32_t > get_registered_dapps() const
Get list of registered dApp IDs.
void set_disconnect_handler(DisconnectHandler handler)
void stop()
Stop the agent.
ErrorCode subscribe(uint32_t ran_function_id, std::vector< uint32_t > telemetry_ids, std::vector< uint32_t > control_ids, std::optional< uint32_t > sub_time=std::nullopt, std::optional< uint32_t > periodicity=std::nullopt, uint32_t *out_request_id=nullptr)
Subscribe to a RAN function. Must be called after a positive setup.
std::vector< uint32_t > get_subscription_telemetry_ids(uint32_t dapp_id, uint32_t ran_function_id) const
Get the telemetry IDs a dApp requested for a RAN function.
void set_xapp_control_handler(XAppControlHandler handler)
void set_setup_response_handler(SetupResponseHandler handler)
ErrorCode wait_for_setup(std::chrono::milliseconds timeout)
Block until the dApp setup handshake completes.
bool is_running() const noexcept
Check if agent is running.
std::vector< uint32_t > subscribed_ran_functions() const
void set_dapp_status_changed_handler(DAppStatusChangedHandler handler)
Set callback for dApp status changes. Called when a dApp connects, disconnects, subscribes,...
std::optional< uint32_t > dapp_id() const noexcept
dApp identifier assigned by the RAN in SetupResponse.
std::vector< uint32_t > get_available_ran_functions() const
Get available RAN function IDs.
std::vector< uint32_t > get_dapp_subscriptions(uint32_t dapp_id) const
Get subscriptions for a dApp.
E3Agent(E3Config config=E3Config{})
Construct an E3Agent with configuration.
std::vector< uint32_t > active_subscription_ids() const
std::vector< uint32_t > get_ran_function_subscribers(uint32_t ran_function_id) const
Get subscribers for a RAN function.
uint32_t get_subscription_periodicity(uint32_t dapp_id, uint32_t ran_function_id) const
Get the reporting periodicity a dApp requested for a RAN function.
ErrorCode register_sm(std::unique_ptr< ServiceModel > sm)
Register a Service Model.
ErrorCode unsubscribe(uint32_t ran_function_id)
size_t dapp_count() const
Get number of registered dApps.
void set_subscription_response_handler(SubscriptionResponseHandler handler)
ErrorCode send_xapp_control(uint32_t dapp_id, uint32_t ran_function_id, const std::vector< uint8_t > &control_data, uint32_t sequence_id, uint32_t *out_message_id=nullptr, bool request_ack=false)
Send an xApp control action to a specific dApp coming from the E2SM-DAPP.
~E3Agent()
Destructor - ensures clean shutdown.
const E3Config & config() const noexcept
Get current configuration.
std::vector< uint32_t > get_subscription_control_ids(uint32_t dapp_id, uint32_t ran_function_id) const
Get the control IDs a dApp requested for a RAN function.
ErrorCode send_report(uint32_t ran_function_id, std::vector< uint8_t > report_data, uint32_t sequence_id, uint32_t *out_message_id=nullptr)
Send a dApp report to the RAN.
ErrorCode send_control(uint32_t ran_function_id, uint32_t control_id, std::vector< uint8_t > action_data, uint32_t sequence_id=0, uint32_t *out_message_id=nullptr, bool request_ack=false)
Send a dApp control action to the RAN.
ErrorCode send_indication(uint32_t dapp_id, uint32_t ran_function_id, const std::vector< uint8_t > &data)
Send an indication message to a specific dApp.
std::vector< uint32_t > get_active_ran_functions() const
Get the RAN functions that currently have at least one subscriber.
ErrorCode send_control_response(uint32_t request_id, uint32_t ran_function_id, uint32_t control_id, const ControlResult &result)
Answer an xApp control that asked for an answer (XAppControlAction::request_ack).
void set_indication_handler(IndicationHandler handler)
AgentState state() const noexcept
Get current agent state.
void set_dapp_report_handler(DAppReportHandler handler)
Set callback for incoming dApp reports (dApp → RAN). Call before start(). When a dApp sends a DApp re...
ErrorCode init()
Initialize the agent.
void set_message_ack_handler(MessageAckHandler handler)
size_t subscription_count() const
Get total number of active subscriptions.
ErrorCode start()
Start the agent.
void set_control_response_handler(ControlResponseHandler handler)
ErrorCode send_message_ack(uint32_t request_id, ResponseCode response_code)
Send a message acknowledgment.
E3Agent - Main façade class for RAN vendors.
E3Interface - Internal protocol coordination layer.
Lock-free per-thread latency stamp recorder (header-only, C99).
static void latrec_tstamp(uint64_t seq, uint8_t stage, uint64_t aux, uint64_t aux2)
static uint64_t latrec_ctx(void)
uint64_t latrec_seq_next(void)
Logging facility for libe3.
#define E3_LOG_INFO(component)
#define E3_LOG_ERROR(component)
std::function< void(const SubscriptionResponse &)> SubscriptionResponseHandler
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
std::function< void(const XAppControlAction &)> XAppControlHandler
AgentState
Agent state machine states.
@ UNINITIALIZED
Agent not yet initialized.
std::function< void()> DAppStatusChangedHandler
@ RAN
RAN function (binds sockets; today's only behaviour).
@ DAPP
dApp (connects to a RAN agent)
ResponseCode
E3AP response codes.
std::function< void(const DAppReport &)> DAppReportHandler
@ MESSAGE_ACK
DEPRECATED TO BE REMOVED: libe3 no longer sends it, see CONTROL_RESPONSE.
std::function< void(const SetupResponse &)> SetupResponseHandler
std::function< void(const ControlResponse &)> ControlResponseHandler
Called for a ControlResponse: on a dApp, the RAN's answer to its control; on a RAN,...
std::function< void(const MessageAck &)> MessageAckHandler
Called for a MessageAck. DEPRECATED TO BE REMOVED: libe3 does not send one, it reads one from another...
std::function< void(DisconnectReason)> DisconnectHandler
const char * error_code_to_string(ErrorCode code) noexcept
Convert ErrorCode to string representation.
std::function< void(const IndicationMessage &)> IndicationHandler
What a Service Model answers to a control (see ServiceModel::respond_to_control).
E3Agent implementation details.
XAppControlHandler xapp_control_handler
Called with each xApp control action.
Impl(E3Config cfg)
Keep the configuration.
MessageAckHandler message_ack_handler
Called with each message ack (DEPRECATED TO BE REMOVED).
IndicationHandler indication_handler
Called with each indication.
SubscriptionResponseHandler subscription_response_handler
Called with each subscription response.
SetupResponseHandler setup_response_handler
Called with each setup response.
DAppReportHandler dapp_report_handler
Called with each dApp report (RAN role).
E3Config config
The configuration the agent was created with.
DisconnectHandler disconnect_handler
Called when the session with the RAN ends.
DAppStatusChangedHandler dapp_status_changed_handler
Called when a dApp's status changes (RAN role).
std::unique_ptr< E3Interface > interface
The protocol engine.
ControlResponseHandler control_response_handler
Called with each control response.
Configuration for E3Agent.
E3AP Indication Message structure.
uint32_t ran_function_identifier
RAN function identifier.
std::vector< uint8_t > protocol_data
Opaque payload of the service model.
uint32_t dapp_identifier
dApp the indication is for
E3AP Message Acknowledgment structure.
ResponseCode response_code
Response code (positive/negative).
uint32_t request_id
ID of the request being acknowledged.
Generic E3AP PDU structure.
uint64_t enqueue_seq
Set by queue_outbound; keys the [latrec] L-stage records.
PduChoice choice
The message itself.
uint32_t message_id
Unique message identifier.
E3AP xApp Control Action structure.
std::vector< uint8_t > xapp_control_data
uint32_t dapp_identifier
dApp the control is for
uint32_t ran_function_identifier
RAN function the control is for.