libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
e3_interface.hpp
Go to the documentation of this file.
1/**
2 * @file e3_interface.hpp
3 * @brief E3Interface - Internal protocol coordination layer
4 *
5 * This class manages the E3AP lifecycle and coordinates between
6 * the E3Agent facade and the protocol handling components. It is
7 * NOT exposed to library users directly.
8 *
9 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef LIBE3_E3_INTERFACE_HPP
14#define LIBE3_E3_INTERFACE_HPP
15
16#include "types.hpp"
17#include "e3_connector.hpp"
18#include "e3_encoder.hpp"
20#include "lockfree_queue.hpp"
21#include "sm_interface.hpp"
23#include <memory>
24#include <thread>
25#include <atomic>
26#include <functional>
27#include <optional>
28#include <mutex>
29#include <condition_variable>
30#include <chrono>
31#include <unordered_map>
32
33namespace libe3 {
34
35// Forward declaration
36class E3Agent;
37
38/**
39 * @brief Callback types for E3Interface events
40 */
42/// Called for a SubscriptionRequest; the returned code is the answer. @param request The request @return POSITIVE to grant it
44using DAppReportHandler = std::function<void(const DAppReport&)>;
45using DAppStatusChangedHandler = std::function<void()>;
46
47// dApp-side handlers (RAN -> dApp PDU dispatch)
48using SetupResponseHandler = std::function<void(const SetupResponse&)>;
49using SubscriptionResponseHandler = std::function<void(const SubscriptionResponse&)>;
50using IndicationHandler = std::function<void(const IndicationMessage&)>;
51using XAppControlHandler = std::function<void(const XAppControlAction&)>;
52/// Called for a MessageAck. DEPRECATED TO BE REMOVED: libe3 does not send one, it reads one from another RAN.
53using MessageAckHandler = std::function<void(const MessageAck&)>;
54/// Called for a ControlResponse: on a dApp, the RAN's answer to its control; on a RAN, the dApp's answer to an xApp control.
55using ControlResponseHandler = std::function<void(const ControlResponse&)>;
56using DisconnectHandler = std::function<void(DisconnectReason)>;
57
58/**
59 * @brief E3Interface - Internal protocol coordination
60 *
61 * This class is the internal coordination layer that:
62 * - Manages E3AP lifecycle (Setup, Subscription, Control, Release)
63 * - Owns inbound/outbound processing threads
64 * - Coordinates between connector, encoder, and subscription manager
65 * - Supports simulation mode for testing without real RAN
66 *
67 * This class is NOT directly exposed to library users. Users interact
68 * with the E3Agent facade which owns an E3Interface instance.
69 */
71public:
72 /**
73 * @brief Construct with configuration
74 * @param config Role, transport, encoding and tuning for this interface
75 */
76 explicit E3Interface(const E3Config& config);
77
78 /**
79 * @brief Destructor - ensures clean shutdown
80 */
82
83 // Non-copyable, non-movable
84 E3Interface(const E3Interface&) = delete;
88
89 /**
90 * @brief Initialize the interface
91 *
92 * Creates connector, encoder, and initializes internal state.
93 * @return ErrorCode::SUCCESS on success
94 */
96
97 /**
98 * @brief Start the interface processing loops
99 *
100 * Spawns the setup, inbound, and outbound threads, plus the report worker on the RAN role.
101 * @return ErrorCode::SUCCESS on success
102 */
104
105 /**
106 * @brief Stop the interface
107 *
108 * Signals all threads to stop and waits for them to finish. It also ends
109 * the dApp's reconnecting, so the dApp stays down. Not to be called from
110 * the disconnect handler, which runs on a thread stop() joins.
111 */
112 void stop();
113
114 /**
115 * @brief Get current state
116 * @return The lifecycle state of the interface
117 */
118 AgentState state() const noexcept { return state_.load(); }
119
120 /**
121 * @brief Check if interface is running
122 * @return True while the state is RUNNING
123 */
124 bool is_running() const noexcept {
125 return state_.load() == AgentState::RUNNING;
126 }
127
128 /**
129 * @brief Get the subscription manager
130 * @return The manager that holds the dApps and their subscriptions
131 */
133 return *subscription_manager_;
134 }
135
136 /**
137 * @brief Get the response queue for outbound messages
138 * @return The queue the outbound loop drains
139 */
141 return *response_queue_;
142 }
143
144 /**
145 * @brief Queue a PDU for outbound transmission
146 * @param pdu PDU to send; it is moved into the queue
147 * @return SUCCESS once queued; the queueing error otherwise
148 */
150
151 /**
152 * @brief Get available RAN functions
153 * @return The identifiers of the registered Service Models, sorted
154 */
155 std::vector<uint32_t> get_available_ran_functions() const;
156
157 /**
158 * @brief Register a Service Model
159 * @param sm Service Model to register; ownership moves to the registry
160 * @return SUCCESS, or the registry's error
161 */
162 ErrorCode register_sm(std::unique_ptr<ServiceModel> sm);
163
164 // =========================================================================
165 // Event Handlers - Set by E3Agent
166 // =========================================================================
167
168 /// Set the callback for a dApp report that reaches the RAN. @param handler Callback to call
170 dapp_report_handler_ = std::move(handler);
171 }
172
173 /// Set the callback for a dApp that connects, subscribes, unsubscribes or leaves. @param handler Callback to call
175 dapp_status_changed_handler_ = std::move(handler);
176 }
177
178 // dApp-side handlers (set before start() when role==DAPP)
179 /// Set the callback for a SetupResponse. @param handler Callback to call
181 setup_response_handler_ = std::move(handler);
182 }
183 /// Set the callback for a SubscriptionResponse. @param handler Callback to call
185 subscription_response_handler_ = std::move(handler);
186 }
187 /// Set the callback for an IndicationMessage. @param handler Callback to call
189 indication_handler_ = std::move(handler);
190 }
191 /// Set the callback for an XAppControlAction. @param handler Callback to call
193 xapp_control_handler_ = std::move(handler);
194 }
195 /// Set the callback for a MessageAck. DEPRECATED TO BE REMOVED. @param handler Callback to call
197 message_ack_handler_ = std::move(handler);
198 }
199 /// Set the callback for a ControlResponse (either role). @param handler Callback to call
201 control_response_handler_ = std::move(handler);
202 }
203 /// Called once when the dApp's session with the RAN ends (see E3Agent::set_disconnect_handler).
204 /// @param handler Callback to call
206 disconnect_handler_ = std::move(handler);
207 }
208
209 // dApp-side accessors
210 /// @return The dApp identifier the RAN assigned, or nothing before setup completes
211 std::optional<uint32_t> dapp_id() const noexcept;
212 /// @return The subscription identifiers the RAN granted and that have not ended
213 std::vector<uint32_t> active_subscription_ids() const;
214 /// @return The RAN functions this dApp holds a subscription to
215 std::vector<uint32_t> subscribed_ran_functions() const;
216 /// @return The RAN functions the RAN advertised in its setup response
217 std::vector<RanFunctionDef> remote_ran_functions() const;
218
219 // dApp-side outbound helpers (queue PDUs that the outbound loop will encode + send)
220 /**
221 * @brief Queue a SubscriptionRequest for the outbound loop to send.
222 * @param ran_function_id RAN function to subscribe to
223 * @param telemetry_ids telemetry identifiers requested
224 * @param control_ids control identifiers requested
225 * @param sub_time how long to keep the subscription, in seconds; unset leaves it to the RAN
226 * @param periodicity microseconds between indications, 0 = every event
227 * @param out_request_id when non-null, receives the assigned E3-MessageID on
228 * success, so the caller can correlate the eventual SubscriptionResponse
229 * (which echoes request_id) back to this request.
230 * @return SUCCESS once queued; STATE_ERROR on a RAN-role agent; NOT_INITIALIZED
231 * before setup has assigned a dApp id; otherwise the queueing error.
232 */
233 ErrorCode queue_subscription_request(uint32_t ran_function_id,
234 std::vector<uint32_t> telemetry_ids,
235 std::vector<uint32_t> control_ids,
236 std::optional<uint32_t> sub_time = std::nullopt,
237 std::optional<uint32_t> periodicity = std::nullopt,
238 uint32_t* out_request_id = nullptr);
239 /// Queue a SubscriptionDelete for the outbound loop to send.
240 /// @param ran_function_id RAN function whose subscription ends
241 /// @return SUCCESS once queued; STATE_ERROR on a RAN-role agent; NOT_INITIALIZED before setup has assigned a dApp id;
242 /// SUBSCRIPTION_NOT_FOUND when the dApp holds none for it; otherwise the queueing error
243 ErrorCode queue_subscription_delete(uint32_t ran_function_id);
244 /// Queue a dApp control action for the outbound loop to send.
245 /// @param ran_function_id RAN function the control is for
246 /// @param control_id control identifier granted by the RAN
247 /// @param action_data opaque payload of the Service Model
248 /// @param sequence_id correlation id of the xApp procedure this control re-issues; 0 for none
249 /// @param out_message_id when non-null, receives the assigned E3-MessageID on
250 /// success, so the caller can correlate a later response back to this send
251 /// @param request_ack true to ask the RAN for a ControlResponse
252 /// @return SUCCESS once queued; otherwise the queueing error
253 ErrorCode queue_dapp_control_action(uint32_t ran_function_id,
254 uint32_t control_id,
255 std::vector<uint8_t> action_data,
256 uint32_t sequence_id = 0,
257 uint32_t* out_message_id = nullptr,
258 bool request_ack = false);
259 /// Queue a ControlResponse that answers an xApp control (dApp role).
260 /// @param request_id message id of the xApp control this answers
261 /// @param ran_function_id RAN function of the control
262 /// @param control_id control the answer is about
263 /// @param result code, reason and outcome to send
264 /// @return SUCCESS once queued; STATE_ERROR on a RAN-role agent; NOT_INITIALIZED before setup has
265 /// assigned a dApp id; INVALID_PARAM for an outcome over MAX_CONTROL_OUTCOME_SIZE; otherwise the queueing error
266 ErrorCode queue_control_response(uint32_t request_id,
267 uint32_t ran_function_id,
268 uint32_t control_id,
269 const ControlResult& result);
270 /// Answer a dApp control the RAN holds an answer for (RAN role, called by Service Models).
271 /// @param dapp_id dApp whose control is answered
272 /// @param request_id message id of the dApp control
273 /// @param result code, reason and outcome to send
274 /// @return SUCCESS once queued; NOT_FOUND when no answer is owed: the dApp did not ask for one, or
275 /// the control was answered already; INVALID_PARAM for an outcome over MAX_CONTROL_OUTCOME_SIZE
276 ErrorCode respond_to_control(uint32_t dapp_id, uint32_t request_id, const ControlResult& result);
277 /// Queue a dApp report for the outbound loop to send.
278 /// @param ran_function_id RAN function the report is for
279 /// @param report_data opaque payload of the Service Model
280 /// @param sequence_id correlation id the dApp assigns to this detection; mandatory, not 0
281 /// @param out_message_id when non-null, receives the assigned E3-MessageID on success
282 /// @return SUCCESS once queued; INVALID_PARAM for a sequence id of 0; otherwise the queueing error
283 ErrorCode queue_dapp_report(uint32_t ran_function_id,
284 std::vector<uint8_t> report_data,
285 uint32_t sequence_id,
286 uint32_t* out_message_id = nullptr);
287 /// Queue a ReleaseMessage that ends this dApp's session.
288 /// @return SUCCESS once queued; otherwise the queueing error
290
291 /// Block until the setup handshake completes (or times out / fails).
292 /// Only meaningful on dApp role.
293 /// @param timeout How long to wait
294 /// @return SUCCESS when setup completed; TIMEOUT when it did not complete in time; CONNECTION_FAILED when it failed;
295 /// INVALID_PARAM on a RAN-role interface
296 ErrorCode wait_for_setup(std::chrono::milliseconds timeout);
297
298 /// Tell the status handler that a dApp connected, subscribed, unsubscribed or left.
300
301private:
302 // Configuration
303 E3Config config_;
304
305 // State
306 std::atomic<AgentState> state_{AgentState::UNINITIALIZED};
307 std::atomic<bool> should_stop_{false};
308 bool started_before_{false};
309
310 // Monotonic source for E3-MessageID (1..4294967295). A counter gives
311 // unique ids, unlike the former random draw which could collide (birthday
312 // hazard) and defeat request/response correlation.
313 std::atomic<uint32_t> next_message_id_{0};
314
315 // Core components
316 std::unique_ptr<E3Connector> connector_;
317 std::unique_ptr<E3Encoder> encoder_;
318 // RAN-only state (nullptr when role==DAPP).
319 std::unique_ptr<SubscriptionManager> subscription_manager_;
320 // dApp-only state (nullptr when role==RAN).
321 std::unique_ptr<DAppSubscriptionState> dapp_state_;
322
323 // dApp-only: subscription requests awaiting their SubscriptionResponse.
324 // E3-SubscriptionResponse echoes requestId but not ranFunctionIdentifier,
325 // so the RAN function a granted subscription belongs to is only knowable
326 // from the request that asked for it. Keyed by the E3-MessageID we sent;
327 // entries are erased when the response arrives.
328 struct PendingSubscriptionOp {
329 uint32_t ran_function_id{0};
330 bool is_delete{false};
331 // subscriptionTime asked for, in seconds. The dApp's deadline starts when
332 // the response arrives, never before the RAN's own.
333 std::optional<uint32_t> subscription_time_s;
334 };
335 std::mutex pending_subscriptions_mutex_;
336 std::unordered_map<uint32_t, PendingSubscriptionOp> pending_subscriptions_;
337 std::unique_ptr<LockFreeQueue<Pdu>> response_queue_;
338
339 // dApp-report queue: subscriber/inbound thread hands reports off to the
340 // report worker thread so downstream (OAI / iApp) work never blocks reads.
341 std::unique_ptr<LockFreeQueue<DAppReport>> report_queue_;
342
343 // Threads. setup_thread_ runs the setup loop (RAN: serves; dApp: drives once).
344 // inbound_thread_ / outbound_thread_ replace the old subscriber_thread_ /
345 // publisher_thread_ names since on the dApp side what flows in is not a
346 // subscription.
347 std::unique_ptr<std::thread> setup_thread_;
348 std::unique_ptr<std::thread> inbound_thread_;
349 std::unique_ptr<std::thread> outbound_thread_;
350 std::unique_ptr<std::thread> report_worker_thread_;
351 std::unique_ptr<std::thread> expiry_thread_;
352 std::mutex expiry_mu_;
353 std::condition_variable expiry_cv_; // woken by stop() so the thread exits at once
354
355 // dApp reconnect. reconnect_thread_ restarts the interface (stop_locked, then
356 // start_locked) when the RAN is gone or setup failed. lifecycle_mu_ keeps
357 // that from overlapping a start() or stop() of the application;
358 // reconnect_mu_ guards the two flags below and is never held across either.
359 std::mutex lifecycle_mu_;
360 std::mutex reconnect_mu_;
361 std::condition_variable reconnect_cv_;
362 bool reconnect_pending_{false}; ///< a restart has been asked for
363 bool user_stopped_{false}; ///< stop() was called and start() has not been since
364 std::unique_ptr<std::thread> reconnect_thread_;
365#ifdef LIBE3_ENABLE_LATREC
366 // ~1 Hz slow-lane sampler stamping LATREC_CONTEXT (involuntary context
367 // switches, current CPU frequency), to explain p99 tails. Exists only in
368 // a latrec-enabled build: a normal build spawns no such thread at all.
369 std::unique_ptr<std::thread> context_thread_;
370#endif
371
372 // RAN-side handlers
373 DAppReportHandler dapp_report_handler_;
374 DAppStatusChangedHandler dapp_status_changed_handler_;
375
376 // dApp-side handlers
377 SetupResponseHandler setup_response_handler_;
378 SubscriptionResponseHandler subscription_response_handler_;
379 IndicationHandler indication_handler_;
380 XAppControlHandler xapp_control_handler_;
381 MessageAckHandler message_ack_handler_; // DEPRECATED TO BE REMOVED
382 ControlResponseHandler control_response_handler_;
383 DisconnectHandler disconnect_handler_;
384
385 /// A dApp control that asked for an answer and has none yet: where the RAN function and the
386 /// control are found when a Service Model answers. At most MAX_PENDING_CONTROLS; an entry
387 /// goes when it is answered or when its dApp leaves.
388 struct PendingControl {
389 uint32_t ran_function_id{0}; ///< RAN function of the control
390 uint32_t control_id{0}; ///< Control identifier of the control
391 };
392 std::mutex pending_controls_mu_;
393 std::unordered_map<uint64_t, PendingControl> pending_controls_; ///< Keyed by dApp id and request id
394
395 // dApp-only: the RAN is gone (set once per session) / we sent our own release.
396 std::atomic<bool> ran_gone_{false};
397 std::atomic<bool> release_sent_{false};
398
399 // dApp-side setup-complete signal (notified by setup_loop_dapp)
400 mutable std::mutex setup_complete_mu_;
401 std::condition_variable setup_complete_cv_;
402 bool setup_complete_{false};
403 bool setup_succeeded_{false};
404
405 // =========================================================================
406 // Thread Entry Points
407 // =========================================================================
408
409 /**
410 * @brief RAN-role main setup loop - handles E3 Setup requests
411 */
412 void setup_loop_ran();
413
414 /**
415 * @brief dApp-role setup loop - send SetupRequest, recv SetupResponse, idle.
416 */
417 void setup_loop_dapp();
418
419 /**
420 * @brief RAN-role inbound loop - receives subscribe/control/report/release.
421 */
422 void inbound_loop_ran();
423
424 /**
425 * @brief dApp-role inbound loop - receives subscribe-response/indication/
426 * xapp-control/message-ack.
427 */
428 void inbound_loop_dapp();
429
430 /**
431 * @brief RAN-role outbound loop - sends indications, subscription
432 * responses and acks back to dApps.
433 */
434 void outbound_loop_ran();
435
436 /**
437 * @brief dApp-role outbound loop - sends subscribe/delete/control/
438 * report/release back to the RAN.
439 */
440 void outbound_loop_dapp();
441
442 /**
443 * @brief Report worker thread - drains report_queue_ and invokes
444 * handle_dapp_report() off the subscriber thread so ZMQ reads
445 * are never blocked by downstream (OAI / iApp) work.
446 */
447 void report_worker_loop();
448
449 /**
450 * @brief Thread that ends subscriptions whose subscriptionTime has run out.
451 * The RAN removes them; the dApp forgets them at the same moment.
452 * Neither tells the other: E3-ReleaseMessage ends the whole dApp
453 * session, not one subscription.
454 */
455 void subscription_expiry_loop();
456
457#ifdef LIBE3_ENABLE_LATREC
458 /**
459 * @brief ~1 Hz slow-lane sampler stamping LATREC_CONTEXT.
460 */
461 void context_monitor_loop();
462#endif
463
464 // =========================================================================
465 // Message Handlers
466 // =========================================================================
467
468 // RAN-role handlers
469 void handle_setup_request(const SetupRequest& request, uint32_t request_message_id);
470 void handle_subscription_request(const SubscriptionRequest& request, uint32_t request_message_id);
471 void handle_subscription_delete(const SubscriptionDelete& del, uint32_t request_message_id);
472 void handle_control_action(const DAppControlAction& action, uint32_t request_message_id,
473 uint64_t latrec_seq);
474 void handle_dapp_report(const DAppReport& report);
475 void handle_release_message(const ReleaseMessage &release);
476 void handle_dapp_disconnection(uint32_t dapp_id);
477 void handle_control_response_from_dapp(const ControlResponse& response);
478 /// Queue a negative ControlResponse for a dApp control the RAN refuses itself.
479 void refuse_control(const DAppControlAction& action, uint32_t request_message_id,
480 Cause cause, std::string detail);
481
482 /**
483 * @brief Release and remove every dApp that has been quiet for dapp_idle_timeout_s
484 *
485 * Runs on the expiry thread. Each dApp gets a ReleaseMessage first, then the
486 * same removal a release from the dApp gets.
487 */
488 void remove_idle_dapps();
489
490 /// start() and stop() without the lifecycle lock or the reconnect thread.
491 ErrorCode start_locked();
492 void stop_locked();
493
494 /// Ask the reconnect thread for a restart, unless it is off or stop() was called.
495 void request_reconnect();
496
497 /// Body of reconnect_thread_: wait for a request, pause, restart, repeat.
498 void reconnect_loop();
499
500 /// stop_locked() then start_locked() under the lifecycle lock, unless stop() was called.
501 void restart_session();
502
503 /**
504 * @brief Which connection each dApp id belongs to, on a RAN that can tell its peers apart.
505 *
506 * A dApp id belongs to the connection that sent its setup request, for as long as
507 * that registration lasts. Each entry carries the generation of the registration
508 * (see SubscriptionManager::dapp_generation), because an id is handed out again
509 * after its dApp is removed. The setup thread adds entries and the inbound thread
510 * reads and removes them, under bindings_mu_.
511 */
512 struct PeerBindings {
513 /// Connection token, then the dApps bound to it, each with its registration's generation.
514 std::unordered_map<uint64_t, std::unordered_map<uint32_t, uint64_t>> by_peer;
515 /// dApp id, then its connection token and its registration's generation.
516 std::unordered_map<uint32_t, std::pair<uint64_t, uint64_t>> owner;
517 };
518
519 /// Guards peer_bindings_.
520 std::mutex bindings_mu_;
521 /// The connection of each registered dApp, on a connector that names its peers.
522 PeerBindings peer_bindings_;
523
524 /**
525 * @brief Bind a dApp that has just registered to the connection that sent its setup request
526 *
527 * @param dapp_id The identifier the dApp was given
528 * @param peer Token of the connection, 0 if the connector cannot tell its peers apart (then nothing is bound)
529 */
530 void bind_dapp_to_connection(uint32_t dapp_id, uint64_t peer);
531
532 /**
533 * @brief The connection a dApp is bound to
534 *
535 * @param dapp_id The dApp
536 * @return The connection's token, or 0 if the dApp is not bound
537 */
538 uint64_t connection_of(uint32_t dapp_id);
539
540 /**
541 * @brief Decide whether a message that names a dApp may be acted on
542 *
543 * A dApp is bound to the connection of its setup request. A message that names it
544 * must come on that connection: the E3AP messages carry no authentication, so
545 * without this any dApp could release or control another. A message that names a
546 * dApp that is not registered, or one that is not bound, is let through.
547 *
548 * @param peer Token of the connection the message came on, 0 if the connector cannot tell
549 * @param dapp_id The dApp the message names
550 * @return false if the dApp is bound to another connection, so the message is to be dropped
551 */
552 bool admit_dapp_message(uint64_t peer, uint32_t dapp_id);
553
554 /**
555 * @brief Remove every dApp that was bound to a connection that has since closed
556 *
557 * No release is sent, there is nobody to send it to. A dApp whose id was
558 * removed and handed out again since it was bound is left alone.
559 */
560 void remove_dapps_of_closed_peers();
561
562 /**
563 * @brief Reply to a setup request that cannot be answered positively.
564 *
565 * The RAN side of the setup channel is a REQ/REP exchange: exactly one
566 * reply must be sent per received request before the next one can be
567 * received (a ZMQ REP socket enforces this in its state machine).
568 * Bailing out without replying (undecodable bytes, wrong PDU type)
569 * wedges the setup channel for every subsequent dApp until the agent
570 * restarts. The encoding is fixed and known a priori by both peers, so
571 * a negative SetupResponse can always be produced; the peer gets an
572 * explicit rejection instead of a silent timeout.
573 *
574 * @param request_id Message ID of the offending request, or any id that
575 * cannot be echoed (0 when the request bytes never
576 * decoded, out of E3-MessageID range otherwise) — a
577 * fresh id is then substituted on the wire (dApps do
578 * not correlate setup replies by ID).
579 */
580 void send_negative_setup_reply(uint32_t request_id);
581
582 // dApp-role handlers
583 void handle_setup_response(const SetupResponse& response);
584 void handle_subscription_response(const SubscriptionResponse& response);
585 void handle_indication(const IndicationMessage& msg, uint64_t latrec_seq);
586 void handle_xapp_control_action(const XAppControlAction& action, uint64_t latrec_seq);
587 void handle_message_ack(const MessageAck& ack); // DEPRECATED TO BE REMOVED
588 void handle_control_response_from_ran(const ControlResponse& response, uint64_t latrec_seq);
589 void handle_release_from_ran(const ReleaseMessage& release);
590
591 /// End the dApp session: clear its state and tell the application, once.
592 void on_ran_gone(DisconnectReason reason);
593
594 /// Build the transport connector from config_ (init, and again on restart).
595 ErrorCode create_connector_from_config();
596
597 // =========================================================================
598 // SM Lifecycle Management
599 // =========================================================================
600
601 /**
602 * @brief Callback for SM lifecycle changes
603 */
604 void on_sm_lifecycle_change(uint32_t ran_function_id, bool should_start);
605
606 /**
607 * @brief Record a subscribe/unsubscribe request awaiting its response.
608 *
609 * @param request_id E3-MessageID the request was sent with
610 * @param ran_function_id RAN function the request concerns
611 * @param is_delete true for a SubscriptionDelete, false for a request
612 * @param subscription_time_s subscriptionTime the request asked for, if any
613 */
614 void remember_subscription_op(
615 uint32_t request_id, uint32_t ran_function_id, bool is_delete,
616 std::optional<uint32_t> subscription_time_s = std::nullopt);
617
618 /// Drop a pending record, e.g. when the request could not be queued.
619 void forget_subscription_op(uint32_t request_id);
620
621public:
622 /**
623 * @brief Generate the next message ID, monotonic within 1..4294967295.
624 *
625 * Wraps a lock-free counter into the ASN.1 E3-MessageID range, skipping 0.
626 * Monotonic (not random) so an id is unique, letting a caller correlate a
627 * response by the id it was assigned.
628 *
629 * @return The next message id
630 */
631 uint32_t generate_message_id();
632
633 /**
634 * @brief Return a request id that can be echoed into a response.
635 *
636 * The decoders do not enforce integer range constraints, so a
637 * peer-supplied message id of 0, outside E3-MessageID's 1..4294967295,
638 * can reach the handlers; echoing it into a response would fail the
639 * encode. Every non-zero id passes through unchanged, 0 is replaced
640 * with a fresh generated id.
641 *
642 * @param request_id Message id a peer sent
643 * @return The same id when it is not 0, a fresh one otherwise
644 */
645 uint32_t sanitize_request_message_id(uint32_t request_id);
646};
647
648} // namespace libe3
649
650#endif // LIBE3_E3_INTERFACE_HPP
E3Agent - Main façade for RAN vendor integration.
Definition e3_agent.hpp:94
ErrorCode queue_dapp_report(uint32_t ran_function_id, std::vector< uint8_t > report_data, uint32_t sequence_id, uint32_t *out_message_id=nullptr)
void notify_dapp_status_changed()
Tell the status handler that a dApp connected, subscribed, unsubscribed or left.
ErrorCode init()
Initialize the interface.
std::vector< uint32_t > subscribed_ran_functions() const
LockFreeQueue< Pdu > & response_queue() noexcept
Get the response queue for outbound messages.
ErrorCode respond_to_control(uint32_t dapp_id, uint32_t request_id, const ControlResult &result)
ErrorCode queue_control_response(uint32_t request_id, uint32_t ran_function_id, uint32_t control_id, const ControlResult &result)
ErrorCode queue_outbound(Pdu pdu)
Queue a PDU for outbound transmission.
ErrorCode queue_subscription_delete(uint32_t ran_function_id)
E3Interface & operator=(E3Interface &&)=delete
SubscriptionManager & subscription_manager() noexcept
Get the subscription manager.
void set_dapp_status_changed_handler(DAppStatusChangedHandler handler)
Set the callback for a dApp that connects, subscribes, unsubscribes or leaves.
~E3Interface()
Destructor - ensures clean shutdown.
void set_indication_handler(IndicationHandler handler)
Set the callback for an IndicationMessage.
void set_disconnect_handler(DisconnectHandler handler)
bool is_running() const noexcept
Check if interface is running.
ErrorCode queue_subscription_request(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)
Queue a SubscriptionRequest for the outbound loop to send.
std::vector< uint32_t > get_available_ran_functions() const
Get available RAN functions.
uint32_t generate_message_id()
Generate the next message ID, monotonic within 1..4294967295.
ErrorCode start()
Start the interface processing loops.
E3Interface(const E3Interface &)=delete
AgentState state() const noexcept
Get current state.
void set_control_response_handler(ControlResponseHandler handler)
Set the callback for a ControlResponse (either role).
ErrorCode wait_for_setup(std::chrono::milliseconds timeout)
void set_subscription_response_handler(SubscriptionResponseHandler handler)
Set the callback for a SubscriptionResponse.
ErrorCode queue_release_message()
E3Interface(const E3Config &config)
Construct with configuration.
E3Interface & operator=(const E3Interface &)=delete
void set_xapp_control_handler(XAppControlHandler handler)
Set the callback for an XAppControlAction.
void set_message_ack_handler(MessageAckHandler handler)
Set the callback for a MessageAck. DEPRECATED TO BE REMOVED.
void stop()
Stop the interface.
void set_setup_response_handler(SetupResponseHandler handler)
Set the callback for a SetupResponse.
std::vector< uint32_t > active_subscription_ids() const
E3Interface(E3Interface &&)=delete
ErrorCode queue_dapp_control_action(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)
std::vector< RanFunctionDef > remote_ran_functions() const
std::optional< uint32_t > dapp_id() const noexcept
void set_dapp_report_handler(DAppReportHandler handler)
Set the callback for a dApp report that reaches the RAN.
uint32_t sanitize_request_message_id(uint32_t request_id)
Return a request id that can be echoed into a response.
ErrorCode register_sm(std::unique_ptr< ServiceModel > sm)
Register a Service Model.
Lock-free bounded blocking queue.
Per-instance state for the dApp role.
Abstract E3 Connector interface for transport layer.
Abstract E3 Encoder interface for PDU encoding/decoding.
Lock-free bounded blocking queue for the E3AP hot paths.
std::function< void(const SubscriptionResponse &)> SubscriptionResponseHandler
Definition e3_agent.hpp:39
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
std::function< void(const XAppControlAction &)> XAppControlHandler
Definition e3_agent.hpp:43
AgentState
Agent state machine states.
Definition types.hpp:162
@ RUNNING
Main processing loop active.
Definition types.hpp:167
@ UNINITIALIZED
Agent not yet initialized.
Definition types.hpp:163
std::function< void()> DAppStatusChangedHandler
Definition e3_agent.hpp:33
ResponseCode
E3AP response codes.
Definition types.hpp:103
std::function< void(const DAppReport &)> DAppReportHandler
Definition e3_agent.hpp:30
std::function< void(const SetupResponse &)> SetupResponseHandler
Definition e3_agent.hpp:37
std::function< void(const ControlResponse &)> ControlResponseHandler
Called for a ControlResponse: on a dApp, the RAN's answer to its control; on a RAN,...
Definition e3_agent.hpp:47
std::function< void(const MessageAck &)> MessageAckHandler
Called for a MessageAck. DEPRECATED TO BE REMOVED: libe3 does not send one, it reads one from another...
Definition e3_agent.hpp:45
std::function< ResponseCode(const SubscriptionRequest &)> SubscriptionRequestHandler
Called for a SubscriptionRequest; the returned code is the answer.
Cause
Why a RAN refused a request (E3-Cause).
Definition types.hpp:115
DisconnectReason
Why a dApp's session with the RAN ended (see DisconnectHandler).
Definition types.hpp:136
std::function< ResponseCode(const SetupRequest &, SetupResponse &)> SetupRequestHandler
Callback types for E3Interface events.
std::function< void(DisconnectReason)> DisconnectHandler
Definition e3_agent.hpp:49
std::function< void(const IndicationMessage &)> IndicationHandler
Definition e3_agent.hpp:41
Service Model Interface - Extension point for RAN vendors.
E3AP Control Response structure.
Definition types.hpp:366
What a Service Model answers to a control (see ServiceModel::respond_to_control).
Definition types.hpp:382
E3AP dApp Report structure.
Definition types.hpp:392
Configuration for E3Agent.
Definition types.hpp:506
E3AP Indication Message structure.
Definition types.hpp:323
E3AP Message Acknowledgment structure.
Definition types.hpp:351
Generic E3AP PDU structure.
Definition types.hpp:448
RAN Function Definition for Setup Response.
Definition types.hpp:263
E3AP Setup Request structure.
Definition types.hpp:253
E3AP Setup Response structure.
Definition types.hpp:273
E3AP Subscription Request structure.
Definition types.hpp:287
E3AP Subscription Response structure.
Definition types.hpp:307
E3AP xApp Control Action structure.
Definition types.hpp:405
E3 Subscription Manager for tracking dApp registrations.
E3AP Protocol Types - Vendor-neutral type definitions.