libe3 0.6.1
C++ library for E3AP RAN vendor integration
Loading...
Searching...
No Matches
sm_interface.hpp
Go to the documentation of this file.
1/**
2 * @file sm_interface.hpp
3 * @brief Service Model Interface - Extension point for RAN vendors
4 *
5 * Defines the interface that Service Models must implement to integrate
6 * with the E3 agent. E3SM logic is treated as opaque to E3AP - this
7 * interface provides extension points without embedding SM-specific logic.
8 *
9 * SPDX-FileCopyrightText: Copyright (c) 2026 Northeastern University
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef LIBE3_SM_INTERFACE_HPP
14#define LIBE3_SM_INTERFACE_HPP
15
16#include "types.hpp"
17#include <memory>
18#include <optional>
19#include <functional>
20#include <vector>
21#include <string>
22#include <atomic>
23#include <mutex>
24
25namespace libe3 {
26
27// Forward declarations
28class ServiceModel;
29
30/**
31 * @brief Abstract Service Model interface
32 *
33 * This is the extension point for RAN vendors to implement their
34 * Service Models. The E3 agent treats E3SM as opaque - all SM-specific
35 * encoding/decoding happens in the implementations of this interface.
36 *
37 * RAN vendors should:
38 * 1. Derive from this class for each supported SM
39 * 2. Implement the required virtual methods
40 * 3. Register their SMs with the E3Agent
41 */
43public:
44 virtual ~ServiceModel() = default;
45
46 /**
47 * @brief Get SM name
48 * @return The name of the SM
49 */
50 virtual std::string name() const = 0;
51
52 /**
53 * @brief Get SM version
54 * @return The version number of the SM
55 */
56 virtual uint32_t version() const = 0;
57
58 /**
59 * @brief Get RAN function ID for this SM
60 * @return The RAN function this SM serves
61 */
62 virtual uint32_t ran_function_id() const = 0;
63
64 /**
65 * @brief Get telemetry IDs supported by this SM
66 * @return The telemetry identifiers the SM supports
67 */
68 virtual std::vector<uint32_t> telemetry_ids() const = 0;
69
70 /**
71 * @brief Get control IDs supported by this SM
72 * @return The control identifiers the SM supports
73 */
74 virtual std::vector<uint32_t> control_ids() const = 0;
75
76 /**
77 * @brief RAN-function-specific opaque data (required, 1..32768 bytes)
78 *
79 * Returns the byte vector advertised for this RAN function in the
80 * SetupResponse (ranFunctionList->ranFunctionData). E3AP declares the field
81 * mandatory and non-empty -- `OCTET STRING (SIZE (1..262144))`, limited to
82 * 32768 bytes -- so every SM
83 * must override this: even the most minimal SM carries at least its own
84 * name. An SM with nothing to advertise is not advertisable at all, since
85 * ranFunctionList itself is optional but its entries are not.
86 *
87 * Registration fails with ErrorCode::INVALID_PARAM if the returned vector is
88 * empty or longer than MAX_PROTOCOL_DATA_SIZE. The base implementation
89 * returns an empty vector so that failure is a loud diagnostic rather than a
90 * compile error in every existing SM; it is not a usable default.
91 *
92 * @return The bytes advertised for this RAN function; registration fails if
93 * the vector is empty or oversized.
94 */
95 virtual std::vector<uint8_t> ran_function_data() const { return {}; }
96
97 /**
98 * @brief The least time between two indications that this SM keeps for a subscription
99 *
100 * Sent to the dApp as the granted periodicity of its SubscriptionResponse.
101 * Override it only when the SM itself keeps its indications at least that
102 * far apart. The base implementation reports nothing, so the response then
103 * carries no periodicity ("not reported").
104 *
105 * @param requested_us Periodicity the dApp asked for, in microseconds. 0 when
106 * it asked for none or for one indication per event.
107 * @return Microseconds between two indications. std::nullopt or 0 when the SM
108 * keeps no interval or does not say; the response then has none.
109 */
110 virtual std::optional<uint32_t> granted_periodicity(uint32_t requested_us) const {
111 (void)requested_us;
112 return std::nullopt;
113 }
114
115 /**
116 * @brief Initialize the SM
117 *
118 * Called when the SM is first registered.
119 * @return ErrorCode::SUCCESS on success
120 */
121 virtual ErrorCode init() = 0;
122
123 /**
124 * @brief Destroy the SM and release resources
125 *
126 * Called when the SM is being unregistered.
127 */
128 virtual void destroy() = 0;
129
130 /**
131 * @brief Start the SM processing
132 *
133 * Called when the first dApp subscribes to this SM's RAN function.
134 * @return ErrorCode::SUCCESS on success
135 */
136 virtual ErrorCode start() = 0;
137
138 /**
139 * @brief Stop the SM processing
140 *
141 * Called when the last dApp unsubscribes from this SM's RAN function.
142 */
143 virtual void stop() = 0;
144
145 /**
146 * @brief Check if SM is currently running
147 * @return True while the SM is processing
148 */
149 virtual bool is_running() const = 0;
150
151 /**
152 * @brief Process a control action from a dApp
153 *
154 * Called by the E3 interface when a DAppControlAction is received for this
155 * service model. Implementations answer it with respond_to_control() and can emit
156 * outbound PDUs (e.g. IndicationMessage) via emit_outbound().
157 * @param request_message_id Message identifier of the DAppControlAction
158 * @param action The control action
159 * @return ErrorCode::SUCCESS if the SM took the action
160 */
162 uint32_t request_message_id,
163 const DAppControlAction& action
164 ) = 0;
165
166protected:
167 ServiceModel() = default;
168
169 /**
170 * @brief Build a MessageAck PDU for a control/request message.
171 *
172 * DEPRECATED TO BE REMOVED. A Service Model answers a control with
173 * respond_to_control(). This helper only builds the PDU.
174 * @param request_message_id Message identifier of the message acknowledged
175 * @param response_code Result to report
176 * @return The PDU
177 */
179 uint32_t request_message_id,
180 ResponseCode response_code
181 ) {
183 MessageAck ack;
184 ack.request_id = request_message_id;
185 ack.response_code = response_code;
186 pdu.choice = ack;
187 return pdu;
188 }
189
190 /**
191 * @brief Answer a dApp control
192 *
193 * Call it once per control, from any thread, when the outcome is known: from
194 * handle_control_action() or later, as an SM that applies the control
195 * asynchronously does. It sends a ControlResponse only when the dApp asked for
196 * one (`request_ack`), so an SM can call it for every control. The RAN function
197 * and the control identifier are filled in from the control. An SM that never
198 * answers sends nothing: the library does not invent an answer for a control
199 * that may still be applied.
200 * @param dapp_id dApp that sent the control (`DAppControlAction::dapp_identifier`)
201 * @param request_message_id Message identifier of the control, as passed to handle_control_action()
202 * @param result Whether the control was applied, why not, and what the SM reports about it
203 * @return ErrorCode::SUCCESS if the answer was queued; ErrorCode::NOT_FOUND if none is owed (the
204 * dApp did not ask, or the control was answered already); ErrorCode::INVALID_PARAM if the
205 * outcome is over MAX_CONTROL_OUTCOME_SIZE; ErrorCode::NOT_INITIALIZED if no agent runs the SM
206 */
208 uint32_t dapp_id,
209 uint32_t request_message_id,
210 const ControlResult& result
211 ) {
212 if (!control_responder_) {
213 return ErrorCode::NOT_INITIALIZED;
214 }
215 return control_responder_(dapp_id, request_message_id, result);
216 }
217
218 /**
219 * @brief Build an IndicationMessage PDU.
220 *
221 * This helper only builds the PDU. Implementations can emit it with
222 * emit_outbound() when appropriate.
223 * @param dapp_id dApp the indication is for
224 * @param ran_function_id RAN function the indication comes from
225 * @param protocol_data Opaque payload of the SM
226 * @return The PDU
227 */
229 uint32_t dapp_id,
230 uint32_t ran_function_id,
231 std::vector<uint8_t> protocol_data
232 ) {
235 msg.dapp_identifier = dapp_id;
237 msg.protocol_data = std::move(protocol_data);
238 pdu.choice = std::move(msg);
239 return pdu;
240 }
241
242 /**
243 * @brief Emit an outbound PDU produced by the SM.
244 * @param pdu The PDU to send
245 * @return ErrorCode::SUCCESS if it was queued, ErrorCode::NOT_INITIALIZED if no emitter is set
246 */
248 if (!outbound_emitter_) {
249 return ErrorCode::NOT_INITIALIZED;
250 }
251 return outbound_emitter_(std::move(pdu));
252 }
253
254 /**
255 * @brief Get currently subscribed dApps for this SM's RAN function.
256 * @return The identifiers of the subscribed dApps, empty when no provider is set
257 */
258 std::vector<uint32_t> get_subscribers() const {
259 if (!subscribers_provider_) {
260 return {};
261 }
262 return subscribers_provider_();
263 }
264
265 /**
266 * @brief Set the outbound PDU emitter callback (used by E3Interface).
267 * @param emitter Callback that queues a PDU
268 */
269 void set_outbound_emitter(std::function<ErrorCode(Pdu&&)> emitter) {
270 outbound_emitter_ = std::move(emitter);
271 }
272
273 /**
274 * @brief Set the control responder callback (used by E3Interface).
275 * @param responder Callback that answers a dApp control
276 */
277 void set_control_responder(std::function<ErrorCode(uint32_t, uint32_t, const ControlResult&)> responder) {
278 control_responder_ = std::move(responder);
279 }
280
281 /**
282 * @brief Set subscriber provider callback (used by E3Interface).
283 * @param provider Callback that lists the subscribed dApps
284 */
285 void set_subscribers_provider(std::function<std::vector<uint32_t>()> provider) {
286 subscribers_provider_ = std::move(provider);
287 }
288
289private:
290 std::function<ErrorCode(Pdu&&)> outbound_emitter_;
291 std::function<ErrorCode(uint32_t, uint32_t, const ControlResult&)> control_responder_;
292 std::function<std::vector<uint32_t>()> subscribers_provider_;
293
294 friend class E3Interface;
295};
296
297/**
298 * @brief Factory function type for creating SM instances
299 */
300using SmFactory = std::function<std::unique_ptr<ServiceModel>()>;
301
302/**
303 * @brief SM Registry for managing registered Service Models
304 *
305 * This class provides a central registry for Service Models.
306 * It's used by the E3Agent to find and manage SMs.
307 */
308class SmRegistry {
309public:
310 /**
311 * @brief Get the singleton instance
312 * @return The registry
313 */
314 static SmRegistry& instance();
315
316 /**
317 * @brief Register a Service Model
318 *
319 * @param sm Service Model to register
320 * @return ErrorCode::SUCCESS on success
321 * @return ErrorCode::SM_ALREADY_REGISTERED if SM for this RAN function exists
322 * @return ErrorCode::INVALID_PARAM if the SM lists more than 256 telemetry or
323 * control ids, or 64 RAN functions are already registered
324 */
325 ErrorCode register_sm(std::unique_ptr<ServiceModel> sm);
326
327 /**
328 * @brief Register a Service Model factory
329 *
330 * Use this to defer SM creation until needed.
331 * @param ran_function_id RAN function the factory serves
332 * @param factory Creates the SM
333 * @return ErrorCode::SUCCESS on success
334 */
335 ErrorCode register_sm_factory(uint32_t ran_function_id, SmFactory factory);
336
337 /**
338 * @brief Unregister a Service Model by RAN function ID
339 * @param ran_function_id RAN function of the SM to remove
340 * @return ErrorCode::SUCCESS on success, an error code otherwise
341 */
342 ErrorCode unregister_sm(uint32_t ran_function_id);
343
344 /**
345 * @brief Get SM by RAN function ID
346 *
347 * @param ran_function_id RAN function ID to look up
348 * @return Pointer to SM, nullptr if not found
349 */
350 ServiceModel* get_by_ran_function(uint32_t ran_function_id);
351
352 /**
353 * @brief Get all available RAN function IDs
354 * @return The RAN functions that have an SM or a factory
355 */
356 std::vector<uint32_t> get_available_ran_functions() const;
357
358 /**
359 * @brief Start SM for a RAN function
360 * @param ran_function_id RAN function whose SM is started
361 * @return ErrorCode::SUCCESS on success, an error code otherwise
362 */
363 ErrorCode start_sm(uint32_t ran_function_id);
364
365 /**
366 * @brief Stop SM for a RAN function
367 * @param ran_function_id RAN function whose SM is stopped
368 * @return ErrorCode::SUCCESS on success, an error code otherwise
369 */
370 ErrorCode stop_sm(uint32_t ran_function_id);
371
372 /**
373 * @brief Check if SM is running
374 * @param ran_function_id RAN function whose SM is asked about
375 * @return True if its SM is running
376 */
377 bool is_sm_running(uint32_t ran_function_id) const;
378
379 /**
380 * @brief Clear all registered SMs
381 */
382 void clear();
383
384private:
385 SmRegistry() = default;
386 ~SmRegistry() = default;
387 SmRegistry(const SmRegistry&) = delete;
388 SmRegistry& operator=(const SmRegistry&) = delete;
389
390 mutable std::mutex mutex_;
391 std::unordered_map<uint32_t, std::unique_ptr<ServiceModel>> sms_;
392 std::unordered_map<uint32_t, SmFactory> factories_;
393};
394
395} // namespace libe3
396
397#endif // LIBE3_SM_INTERFACE_HPP
Abstract Service Model interface.
virtual std::string name() const =0
Get SM name.
ErrorCode respond_to_control(uint32_t dapp_id, uint32_t request_message_id, const ControlResult &result)
Answer a dApp control.
virtual uint32_t ran_function_id() const =0
Get RAN function ID for this SM.
virtual std::vector< uint32_t > telemetry_ids() const =0
Get telemetry IDs supported by this SM.
static Pdu make_message_ack_pdu(uint32_t request_message_id, ResponseCode response_code)
Build a MessageAck PDU for a control/request message.
std::vector< uint32_t > get_subscribers() const
Get currently subscribed dApps for this SM's RAN function.
virtual uint32_t version() const =0
Get SM version.
static Pdu make_indication_pdu(uint32_t dapp_id, uint32_t ran_function_id, std::vector< uint8_t > protocol_data)
Build an IndicationMessage PDU.
virtual std::optional< uint32_t > granted_periodicity(uint32_t requested_us) const
The least time between two indications that this SM keeps for a subscription.
void set_subscribers_provider(std::function< std::vector< uint32_t >()> provider)
Set subscriber provider callback (used by E3Interface).
void set_control_responder(std::function< ErrorCode(uint32_t, uint32_t, const ControlResult &)> responder)
Set the control responder callback (used by E3Interface).
virtual ~ServiceModel()=default
virtual void destroy()=0
Destroy the SM and release resources.
void set_outbound_emitter(std::function< ErrorCode(Pdu &&)> emitter)
Set the outbound PDU emitter callback (used by E3Interface).
ServiceModel()=default
ErrorCode emit_outbound(Pdu &&pdu)
Emit an outbound PDU produced by the SM.
virtual ErrorCode handle_control_action(uint32_t request_message_id, const DAppControlAction &action)=0
Process a control action from a dApp.
virtual ErrorCode start()=0
Start the SM processing.
friend class E3Interface
virtual void stop()=0
Stop the SM processing.
virtual std::vector< uint8_t > ran_function_data() const
RAN-function-specific opaque data (required, 1..32768 bytes).
virtual std::vector< uint32_t > control_ids() const =0
Get control IDs supported by this SM.
virtual bool is_running() const =0
Check if SM is currently running.
virtual ErrorCode init()=0
Initialize the SM.
ErrorCode start_sm(uint32_t ran_function_id)
Start SM for a RAN function.
static SmRegistry & instance()
Get the singleton instance.
ServiceModel * get_by_ran_function(uint32_t ran_function_id)
Get SM by RAN function ID.
bool is_sm_running(uint32_t ran_function_id) const
Check if SM is running.
ErrorCode unregister_sm(uint32_t ran_function_id)
Unregister a Service Model by RAN function ID.
ErrorCode register_sm(std::unique_ptr< ServiceModel > sm)
Register a Service Model.
ErrorCode stop_sm(uint32_t ran_function_id)
Stop SM for a RAN function.
ErrorCode register_sm_factory(uint32_t ran_function_id, SmFactory factory)
Register a Service Model factory.
std::vector< uint32_t > get_available_ran_functions() const
Get all available RAN function IDs.
void clear()
Clear all registered SMs.
ErrorCode
Error codes returned by libe3 operations Use shared error-code list (see libe3/error_codes....
Definition types.hpp:177
ResponseCode
E3AP response codes.
Definition types.hpp:103
@ MESSAGE_ACK
DEPRECATED TO BE REMOVED: libe3 no longer sends it, see CONTROL_RESPONSE.
Definition types.hpp:155
std::function< std::unique_ptr< ServiceModel >()> SmFactory
Factory function type for creating SM instances.
What a Service Model answers to a control (see ServiceModel::respond_to_control).
Definition types.hpp:382
E3AP dApp Control Action structure.
Definition types.hpp:332
E3AP Indication Message structure.
Definition types.hpp:323
uint32_t ran_function_identifier
RAN function identifier.
Definition types.hpp:325
std::vector< uint8_t > protocol_data
Opaque payload of the service model.
Definition types.hpp:326
uint32_t dapp_identifier
dApp the indication is for
Definition types.hpp:324
E3AP Message Acknowledgment structure.
Definition types.hpp:351
ResponseCode response_code
Response code (positive/negative).
Definition types.hpp:353
uint32_t request_id
ID of the request being acknowledged.
Definition types.hpp:352
Generic E3AP PDU structure.
Definition types.hpp:448
PduChoice choice
The message itself.
Definition types.hpp:450
E3AP Protocol Types - Vendor-neutral type definitions.