libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
e3_dapp_session.hpp
Go to the documentation of this file.
1/**
2 * @file e3_dapp_session.hpp
3 * @brief Python-facing dApp session over libe3 — batched, low-latency seam.
4 *
5 * @section why Why this class exists
6 * libe3's core @ref libe3::E3Agent drives the dApp role with `std::function`
7 * handlers (@ref libe3::E3Agent::set_indication_handler et al.). SWIG cannot
8 * bridge those callbacks to Python across libe3's inbound worker threads
9 * without either running Python under the GIL *on the hot thread* (directors —
10 * deadlock-prone, serialises dispatch) or paying a GIL round-trip per message.
11 * Both are unacceptable at the E3AP target rate (real-time, sub-millisecond
12 * cadence, very high message throughput).
13 *
14 * @ref libe3::py::DAppSession is the answer: it owns an @ref libe3::E3Agent in
15 * DAPP role, registers the C++ handlers itself, and funnels every inbound
16 * message into a **bounded lock-free ring** (@ref libe3::LockFreeQueue). Python
17 * drains that ring in **batches** via @ref libe3::py::DAppSession::poll_events "poll_events", so one GIL acquire and
18 * one Python↔C++ boundary crossing are amortised across many messages. The
19 * SWIG wrapper releases the GIL around the blocking calls (see swig/libe3.i,
20 * `%module(threads="1")`), so libe3's C++ threads never stall on Python.
21 *
22 * The E3SM (service-model) payloads stay opaque here (`std::vector<uint8_t>`);
23 * dApp-library encodes/decodes them in Python. This class is E3AP only, mirroring
24 * the OAI split where libe3 owns E3AP and the SM owns encode/decode.
25 *
26 * @section lifecycle Lifecycle
27 * ctor (registers handlers) → @ref libe3::py::DAppSession::start "start" → @ref libe3::py::DAppSession::wait_for_setup "wait_for_setup" →
28 * (read setup via @ref libe3::py::DAppSession::dapp_id "dapp_id" / setup accessors) → @ref libe3::py::DAppSession::subscribe "subscribe" →
29 * loop: @ref libe3::py::DAppSession::poll_events "poll_events" + @ref libe3::py::DAppSession::send_control "send_control" / @ref libe3::py::DAppSession::send_report "send_report" →
30 * @ref libe3::py::DAppSession::release "release" → @ref libe3::py::DAppSession::stop "stop".
31 *
32 * @section rejected Rejected alternatives (auditable)
33 * - SWIG directors: run the Python callback on libe3's inbound thread under the
34 * GIL — serialises dispatch, deadlock-prone if Python re-enters libe3.
35 * - per-message poll: one GIL round-trip + one boundary crossing per message
36 * caps throughput; batching is strictly better with no added latency (the
37 * ring still wakes on the first event).
38 *
39 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
40 * SPDX-License-Identifier: Apache-2.0
41 */
42
43#ifndef LIBE3_PY_E3_DAPP_SESSION_HPP
44#define LIBE3_PY_E3_DAPP_SESSION_HPP
45
46#include <libe3/types.hpp>
47
48#include <cstdint>
49#include <memory>
50#include <string>
51#include <vector>
52
53namespace libe3 {
54namespace py {
55
56/**
57 * @brief Discriminator for @ref libe3::py::E3Event.
58 */
60 E3_EVENT_NONE = 0, ///< placeholder (never enqueued)
61 E3_EVENT_INDICATION = 1, ///< RAN → dApp IndicationMessage
62 E3_EVENT_XAPP_CONTROL = 2, ///< xApp → dApp XAppControlAction
63 E3_EVENT_SUBSCRIPTION_RESPONSE = 3, ///< RAN → dApp SubscriptionResponse
64 E3_EVENT_SETUP_RESPONSE = 4, ///< RAN → dApp SetupResponse
65 E3_EVENT_MESSAGE_ACK = 5, ///< RAN → dApp MessageAck. DEPRECATED TO BE REMOVED: libe3 does not send one
66 E3_EVENT_DISCONNECT = 6, ///< the RAN is gone; see E3Event::disconnect_reason
67 E3_EVENT_CONTROL_RESPONSE = 7 ///< RAN → dApp ControlResponse
68};
69
70/**
71 * @brief One inbound E3AP event, POD-flat for a clean SWIG crossing.
72 *
73 * `payload` carries the opaque E3SM bytes for indications
74 * (protocol_data) and xApp controls (xapp_control_data); it is empty for
75 * subscription/setup responses. Fields not relevant to a given
76 * `kind` are left zero, except where noted.
77 *
78 * A subscription response also carries the grant: `ran_function_id` (0 = not
79 * reported), `periodicity`, and the id lists, valid only when their has_* flag is set.
80 *
81 * A negative subscription response, setup response or message ack also carries why:
82 * `cause` (a @ref libe3::Cause value, -1 when the RAN gave none, as Aerial does) and
83 * `detail` (the RAN's text, empty when there is none). A dApp reads them to decide what
84 * to do, for example to drop a control the RAN function does not offer and subscribe again.
85 */
86struct E3Event {
87 int kind{E3_EVENT_NONE}; ///< one of @ref E3EventKind
88 uint32_t dapp_id{0}; ///< dApp identifier the message targets
89 uint32_t ran_function_id{0}; ///< RAN function (indication / xApp control / subscription response)
90 uint32_t subscription_id{0}; ///< subscription id (subscription response)
91 uint32_t request_id{0}; ///< request/message id (subscription response / control response / xApp control); for a control response, the id `send_control` returned
92 uint32_t sequence_id{0}; ///< loop correlation id (xApp control); echo it on the control you re-issue
93 uint32_t control_id{0}; ///< control identifier (control response)
94 bool request_ack{false}; ///< xApp control: the RAN wants a control response; answer it with send_control_response
95 bool has_outcome{false}; ///< control response: `payload` is the Service Model's outcome
96 int response_code{-1}; ///< 0=positive, 1=negative, -1=n/a
97 int disconnect_reason{-1}; ///< disconnect: 0=released by RAN, 1=connection lost, -1=n/a
98 std::vector<uint8_t> payload; ///< opaque E3SM bytes (indication / xApp control)
99 long periodicity{-1}; ///< least microseconds the RAN keeps between two indications (subscription response), -1 = not reported
100 int cause{-1}; ///< why a negative response (setup, subscription or control) was refused, a libe3::Cause value 0 to 14; -1 = none given
101 std::string detail; ///< the RAN's text for the refusal; empty = none given
102 bool has_telemetry_granted{false}; ///< telemetry_granted was reported
103 bool has_control_granted{false}; ///< control_granted was reported
104 std::vector<uint32_t> telemetry_granted; ///< granted telemetry ids
105 std::vector<uint32_t> control_granted; ///< granted control ids
106 uint64_t trace_seq{0}; ///< set when queued; keys the [latrec] LQ-stage records
107 // NOTE: in Python (libe3py) the payload is read via ev.get_payload(), which
108 // returns native `bytes`. SWIG member getters return a wrapped vector, so the
109 // binding exposes the payload as a by-value method instead (see swig/libe3.i);
110 // likewise the granted lists, via get_telemetry_granted() / get_control_granted().
111};
112
113/**
114 * @brief dApp-role E3AP session with a batched, lock-free inbound ring.
115 *
116 * Thread model: two producers push into the ring — the setup-response handler
117 * (libe3 setup thread) and the other handlers (libe3 inbound thread), which run
118 * concurrently at startup; the Python drainer thread is the single consumer via
119 * @ref poll_events. The ring is MPMC, so this is safe (do not "optimize" it to
120 * SPSC). All outbound verbs are thread-safe (delegated to @ref libe3::E3Agent).
121 */
123public:
124 /**
125 * @brief Construct the session and register all inbound handlers.
126 * @param config Agent config; `role` is forced to @ref E3Role::DAPP.
127 * @param queue_capacity Inbound ring capacity (rounded up to a power of
128 * two). Sized for high-throughput headroom; overflow increments the
129 * drop counter (@ref dropped_events) rather than blocking libe3.
130 */
131 explicit DAppSession(libe3::E3Config config, std::size_t queue_capacity = 8192);
132 ~DAppSession();
133
134 DAppSession(const DAppSession&) = delete;
136
137 /** @brief Start the agent, re-arming the inbound ring so a stop()->start()
138 * cycle works again. @return ErrorCode as int. */
139 int start();
140 /** @brief Block until the setup handshake completes. @param timeout_ms longest wait, in milliseconds. @return ErrorCode as int. */
141 int wait_for_setup(int timeout_ms);
142 /** @brief Send a ReleaseMessage (session keeps running until @ref stop). @return ErrorCode as int. */
143 int release();
144 /** @brief Stop the agent and shut down the ring so a blocked @ref poll_events
145 * returns an empty batch (i.e. empty-after-stop means "done"). Restartable
146 * via @ref start. */
147 void stop();
148
149 /** @brief dApp id assigned at setup. @return the id, or -1 if not yet assigned. */
150 long dapp_id() const;
151 /** @brief RAN identifier from the SetupResponse. @return the identifier, or "" if none. */
152 std::string ran_identifier() const;
153
154 /** @brief SetupResponse code. @return 0=positive, 1=negative, -1=not received. */
155 int setup_response_code() const;
156 /** @brief Number of RAN functions advertised in the SetupResponse. @return the count. */
157 std::size_t setup_ran_function_count() const;
158 /** @brief RAN function id of an advertised entry. @param i index of the entry. @return the id, 0 if out of range. */
159 uint32_t setup_ran_function_id(std::size_t i) const;
160 /** @brief Telemetry ids of an advertised entry. @param i index of the entry. @return the ids. */
161 std::vector<uint32_t> setup_ran_function_telemetry(std::size_t i) const;
162 /** @brief Control ids of an advertised entry. @param i index of the entry. @return the ids. */
163 std::vector<uint32_t> setup_ran_function_control(std::size_t i) const;
164 /** @brief Opaque ranFunctionData bytes of an advertised entry. @param i index of the entry. @return the bytes. */
165 std::vector<uint8_t> setup_ran_function_data(std::size_t i) const;
166
167 /**
168 * @brief Subscribe to a RAN function (after a positive setup).
169 * @param ran_function_id RAN function to subscribe to
170 * @param telemetry_ids telemetry identifiers requested
171 * @param control_ids control identifiers requested
172 * @param sub_time_ms subscription time in seconds (the name is historical), or
173 * -1 for unset. 0 is no limit. The RAN ends the subscription when it runs out.
174 * @param periodicity microseconds between indications, 0 = every event, or -1 for
175 * unset. Over ASN.1 the range is 0 to 60000000. An OCUDU RAN reads the
176 * same number as milliseconds.
177 * @return the assigned request id (positive, 1..4294967295) on success, so
178 * the caller can correlate the SubscriptionResponse by its
179 * request_id; a negative ErrorCode on failure.
180 */
181 int64_t subscribe(uint32_t ran_function_id,
182 std::vector<uint32_t> telemetry_ids,
183 std::vector<uint32_t> control_ids,
184 int sub_time_ms,
185 int periodicity);
186 /** @brief Delete a previously created subscription. @param ran_function_id RAN function it was for. @return ErrorCode as int. */
187 int unsubscribe(uint32_t ran_function_id);
188
189 /**
190 * @brief Send a dApp control action to the RAN.
191 * @param ran_function_id RAN function the control is for
192 * @param control_id control identifier granted by the RAN
193 * @param action_data opaque E3SM control payload
194 * @param sequence_id correlation id of the xApp procedure this control
195 * re-issues; 0 when the dApp decided on its own.
196 * @param request_ack true to ask the RAN for a control response (an
197 * E3_EVENT_CONTROL_RESPONSE event) once it has dealt with the control.
198 * The RAN answers a control it refuses itself either way.
199 * @return the assigned message id (positive, 1..4294967295) on success, so
200 * the caller can correlate a later response by id; a negative
201 * ErrorCode on failure. Returned as int64_t because a message id
202 * now spans the full uint32 range and would alias onto the negative
203 * error codes if narrowed to int.
204 */
205 int64_t send_control(uint32_t ran_function_id, uint32_t control_id,
206 std::vector<uint8_t> action_data,
207 uint32_t sequence_id = 0,
208 bool request_ack = false);
209 /**
210 * @brief Send a dApp report to the RAN.
211 * @param ran_function_id RAN function the report is for
212 * @param report_data opaque E3SM report payload
213 * @param sequence_id correlation id this dApp assigns to the detection;
214 * mandatory on the wire, 1..4294967295 (0 is refused with
215 * INVALID_PARAM).
216 * @return the assigned message id (positive, 1..4294967295) on success, so
217 * the caller can correlate a later response by id; a negative
218 * ErrorCode on failure.
219 */
220 int64_t send_report(uint32_t ran_function_id, std::vector<uint8_t> report_data,
221 uint32_t sequence_id);
222 /** @brief Acknowledge a request. DEPRECATED TO BE REMOVED: answer an xApp control with send_control_response. @param request_id request being acknowledged. @param response_code 0=positive,1=negative. @return ErrorCode as int. */
223 int send_message_ack(uint32_t request_id, int response_code);
224
225 /**
226 * @brief Answer an xApp control that has `request_ack` (E3_EVENT_XAPP_CONTROL).
227 * @param request_id the event's `request_id`
228 * @param ran_function_id the event's `ran_function_id`
229 * @param control_id the control the dApp applied, from the list the RAN advertised for the function
230 * @param response_code 0=applied (positive), 1=not applied (negative)
231 * @param cause why not, a libe3 Cause value 0 to 14, or -1 for none
232 * @param detail text for the cause, up to 512 bytes, empty for none
233 * @param outcome what the dApp reports about the control, up to 32768 bytes, empty for none
234 * @return ErrorCode as int.
235 */
236 int send_control_response(uint32_t request_id, uint32_t ran_function_id, uint32_t control_id,
237 int response_code, int cause, std::string detail,
238 std::vector<uint8_t> outcome);
239
240 /**
241 * @brief Drain up to @p max_batch inbound events, waiting up to
242 * @p timeout_ms for the first.
243 *
244 * Blocks on the ring for the first event (latency ≈ ring wakeup), then
245 * sweeps up any further already-queued events without blocking.
246 * @p timeout_ms <= 0 polls non-blocking (does not busy-wait). Returns an
247 * empty vector on a quiet tick or after @ref stop. The SWIG wrapper releases
248 * the GIL for the whole call so libe3's threads run freely while Python waits.
249 * @param max_batch most events to return
250 * @param timeout_ms longest wait for the first event, in milliseconds
251 * @return the events, empty on a quiet tick or after stop
252 */
253 std::vector<E3Event> poll_events(std::size_t max_batch, int timeout_ms);
254
255 /** @brief Total inbound events dropped due to ring overflow (backpressure). @return the count. */
256 unsigned long long dropped_events() const;
257
258private:
259 struct Impl;
260 std::unique_ptr<Impl> impl_;
261};
262
263} // namespace py
264} // namespace libe3
265
266#endif // LIBE3_PY_E3_DAPP_SESSION_HPP
uint32_t setup_ran_function_id(std::size_t i) const
RAN function id of an advertised entry.
int send_control_response(uint32_t request_id, uint32_t ran_function_id, uint32_t control_id, int response_code, int cause, std::string detail, std::vector< uint8_t > outcome)
Answer an xApp control that has request_ack (E3_EVENT_XAPP_CONTROL).
int setup_response_code() const
SetupResponse code.
int unsubscribe(uint32_t ran_function_id)
Delete a previously created subscription.
std::vector< E3Event > poll_events(std::size_t max_batch, int timeout_ms)
Drain up to max_batch inbound events, waiting up to timeout_ms for the first.
std::vector< uint8_t > setup_ran_function_data(std::size_t i) const
Opaque ranFunctionData bytes of an advertised entry.
std::vector< uint32_t > setup_ran_function_telemetry(std::size_t i) const
Telemetry ids of an advertised entry.
std::string ran_identifier() const
RAN identifier from the SetupResponse.
unsigned long long dropped_events() const
Total inbound events dropped due to ring overflow (backpressure).
void stop()
Stop the agent and shut down the ring so a blocked poll_events returns an empty batch (i....
int release()
Send a ReleaseMessage (session keeps running until stop).
DAppSession & operator=(const DAppSession &)=delete
DAppSession(const DAppSession &)=delete
int send_message_ack(uint32_t request_id, int response_code)
Acknowledge a request. DEPRECATED TO BE REMOVED: answer an xApp control with send_control_response.
int start()
Start the agent, re-arming the inbound ring so a stop()->start() cycle works again.
int wait_for_setup(int timeout_ms)
Block until the setup handshake completes.
~DAppSession()
Stops the agent and drops the queue.
int64_t send_report(uint32_t ran_function_id, std::vector< uint8_t > report_data, uint32_t sequence_id)
Send a dApp report to the RAN.
int64_t send_control(uint32_t ran_function_id, uint32_t control_id, std::vector< uint8_t > action_data, uint32_t sequence_id=0, bool request_ack=false)
Send a dApp control action to the RAN.
long dapp_id() const
dApp id assigned at setup.
int64_t subscribe(uint32_t ran_function_id, std::vector< uint32_t > telemetry_ids, std::vector< uint32_t > control_ids, int sub_time_ms, int periodicity)
Subscribe to a RAN function (after a positive setup).
std::size_t setup_ran_function_count() const
Number of RAN functions advertised in the SetupResponse.
DAppSession(libe3::E3Config config, std::size_t queue_capacity=8192)
Construct the session and register all inbound handlers.
std::vector< uint32_t > setup_ran_function_control(std::size_t i) const
Control ids of an advertised entry.
E3EventKind
Discriminator for libe3::py::E3Event.
@ E3_EVENT_NONE
placeholder (never enqueued)
@ E3_EVENT_SUBSCRIPTION_RESPONSE
RAN → dApp SubscriptionResponse.
@ E3_EVENT_SETUP_RESPONSE
RAN → dApp SetupResponse.
@ E3_EVENT_INDICATION
RAN → dApp IndicationMessage.
@ E3_EVENT_DISCONNECT
the RAN is gone; see E3Event::disconnect_reason
@ E3_EVENT_XAPP_CONTROL
xApp → dApp XAppControlAction
@ E3_EVENT_CONTROL_RESPONSE
RAN → dApp ControlResponse.
@ E3_EVENT_MESSAGE_ACK
RAN → dApp MessageAck. DEPRECATED TO BE REMOVED: libe3 does not send one.
Configuration for E3Agent.
Definition types.hpp:506
What a DAppSession holds: the agent, the event queue and the setup snapshot.
One inbound E3AP event, POD-flat for a clean SWIG crossing.
uint32_t ran_function_id
RAN function (indication / xApp control / subscription response).
uint32_t sequence_id
loop correlation id (xApp control); echo it on the control you re-issue
int kind
one of E3EventKind
uint32_t dapp_id
dApp identifier the message targets
std::vector< uint32_t > control_granted
granted control ids
bool has_outcome
control response: payload is the Service Model's outcome
std::vector< uint32_t > telemetry_granted
granted telemetry ids
uint32_t request_id
request/message id (subscription response / control response / xApp control); for a control response,...
int disconnect_reason
disconnect: 0=released by RAN, 1=connection lost, -1=n/a
long periodicity
least microseconds the RAN keeps between two indications (subscription response), -1 = not reported
uint32_t subscription_id
subscription id (subscription response)
int response_code
0=positive, 1=negative, -1=n/a
bool has_control_granted
control_granted was reported
uint64_t trace_seq
set when queued; keys the [latrec] LQ-stage records
std::vector< uint8_t > payload
opaque E3SM bytes (indication / xApp control)
int cause
why a negative response (setup, subscription or control) was refused, a libe3::Cause value 0 to 14; -...
bool request_ack
xApp control: the RAN wants a control response; answer it with send_control_response
bool has_telemetry_granted
telemetry_granted was reported
uint32_t control_id
control identifier (control response)
std::string detail
the RAN's text for the refusal; empty = none given
E3AP Protocol Types - Vendor-neutral type definitions.