ED247 Library  VA2.3.0
Implementation of ED247-A standard
ed247_stream.h
1 /* -*- mode: c++; c-basic-offset: 2 -*- */
2 /******************************************************************************
3  * The MIT Licence
4  *
5  * Copyright (c) 2021 Airbus Operations S.A.S
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23  * OTHER DEALINGS IN THE SOFTWARE.
24  *****************************************************************************/
25 #ifndef _ED247_STREAM_H_
26 #define _ED247_STREAM_H_
27 #include "ed247_xml.h"
28 #include "ed247_signal.h"
29 #include "ed247_sample.h"
30 
31 
32 // base structures for C API
37 
38 namespace ed247
39 {
40  class Context;
41  class StreamAssistant;
42 
44  {
45  public:
46 
47  Stream(Context* context,
48  const xml::Stream* configuration,
49  ed247_internal_channel_t* ed247_api_channel, // Parent for ed247_stream_get_channel()
50  uint32_t sample_size_size); // Size of the sample size header field (that depend on stream type)
51 
52  virtual ~Stream();
53 
54  // No implicit copy.
55  Stream(const Stream & other) = delete;
56  Stream& operator = (const Stream & other) = delete;
57 
58 
59  // configuration accessors
60  const std::string& get_name() const { return _configuration->_name; }
61  const std::string& get_icd() const { return _configuration->_icd; }
62  const std::string& get_comment() const { return _configuration->_comment; }
63  ed247_uid_t get_uid() const { return _configuration->_uid; }
64  ed247_stream_type_t get_type() const { return _configuration->_type; }
65  ed247_direction_t get_direction() const { return _configuration->_direction; }
66  uint32_t get_sample_max_size_bytes() const { return _configuration->_sample_max_size_bytes; }
67  uint32_t get_sample_max_number() const { return _configuration->_sample_max_number; }
68  uint32_t get_max_size() const { return _max_size; }
69 
70 
71  // implementation of API method ed247_stream_get_channel()
72  ed247_internal_channel_t* get_api_channel() { return _ed247_api_channel; }
73 
74 
75  // Handle user-data
76  void set_user_data(void *user_data) { _user_data = user_data; }
77  void get_user_data(void **user_data) { *user_data = _user_data; }
78 
79 
80  // Signals specific part
81  bool is_signal_based() const { return _configuration->is_signal_based(); }
82  virtual uint32_t get_sampling_period_us() { return 0; }
83  StreamAssistant* get_assistant() { return _assistant.get(); }
84  signal_list_t& get_signals() { return _signals; }
85  ed247_internal_signal_list_t* get_client_signals() { return _client_signals.get(); }
86  signal_list_t find_signals(std::string str_regex);
87  signal_ptr_t get_signal(std::string str_name);
88 
89 
90  // Handing samples
91  uint32_t get_incoming_sample_number() { return _recv_stack.size(); }
92  uint32_t get_outgoing_sample_number() { return _send_stack.size(); }
93 
94  // Append sample_data to send stack
95  // If send stack is full, the oldest pushed sample will be silently dropped. This is not an error.
96  // If data_timestamp provided, sample will be dated if enabled by configuration
97  // If full provided, will be set to true if send stack is full after this appending
98  // Return false on fatal error. (stack full is not an error)
99  bool push_sample(const void* sample_data, uint32_t sample_size,
100  const ed247_timestamp_t* data_timestamp,
101  bool* full);
102 
103  // Return the oldest received sample and mark it as removed.
104  // Set empty to true if incoming stack is empty after the pop.
105  // If stack is empty before the pop, an arbitrary, but valid, sample will be returned. (see get_incoming_sample_number())
106  StreamSample& pop_sample(bool* empty);
107 
108  // Encode each pushed sample of the stream in frame.
109  // Return encoded length.
110  uint32_t encode(char* frame, uint32_t frame_size);
111 
112  // Decode the given frame and fill internal samples.
113  // Return false on error (the rest of the frame cannot be decoded)
114  bool decode(const char* frame, uint32_t frame_size, const ed247_sample_details_t& frame_details);
115 
116  // Callback managment (Can we remove this ugly API ?)
117  ed247_status_t register_callback(ed247_context_t context, ed247_stream_recv_callback_t callback);
118  ed247_status_t unregister_callback(ed247_context_t context, ed247_stream_recv_callback_t callback);
119  bool run_callbacks();
120 
121 
122  protected:
123  Context* _context;
124  const xml::Stream* _configuration;
125  uint32_t _sample_size_size;
126  uint32_t _sample_first_header_size;
127  uint32_t _sample_next_header_size;
128  uint32_t _max_size;
129  signal_list_t _signals;
130  std::unique_ptr<ed247_internal_signal_list_t> _client_signals;
131  std::unique_ptr<StreamAssistant> _assistant;
132  StreamSampleRingBuffer _recv_stack;
133  StreamSampleRingBuffer _send_stack;
134 
135  private:
136  ed247_internal_channel_t* _ed247_api_channel;
137  void* _user_data;
138 
139  // Callback managment (Can we remove this ugly API ?)
140  struct CallbackData {
141  ed247_context_t context;
143  };
144  std::vector<CallbackData> _callbacks;
145 
146  ED247_FRIEND_TEST();
147  };
148 
149 
150  //
151  // Streams with signals
152  //
153  struct StreamSignals: public Stream {
154  StreamSignals(Context* context, const xml::Stream* configuration,
155  ed247_internal_channel_t* ed247_api_channel, uint32_t sample_size_size);
156  uint32_t get_sampling_period_us() override { return ((xml::StreamSignals*)_configuration)->_sampling_period_us; }
157 
158  };
159 
160  typedef std::shared_ptr<Stream> stream_ptr_t;
161  typedef std::vector<stream_ptr_t> stream_list_t;
162  typedef std::unordered_map<std::string, stream_ptr_t> stream_map_t;
163 
164 
165  //
166  // Global list of streams
167  //
168  class StreamSet
169  {
170  public:
171  StreamSet(Context* context);
172  ~StreamSet();
173 
174  StreamSet& operator=(const StreamSet &) = delete;
175  StreamSet& operator=(StreamSet &&) = delete;
176 
177  stream_ptr_t create(const xml::Stream* configuration, ed247_internal_channel_t* ed247_api_channel);
178 
179  stream_ptr_t get(std::string name);
180  stream_list_t find(std::string regex);
181 
182  stream_map_t& streams() { return _streams; }
183  uint32_t size() const { return _streams.size(); }
184 
185  stream_list_t& get_streams_signals_output() { return _streams_signals_output; }
186  stream_list_t& get_streams_signals_input() { return _streams_signals_input; }
187 
188  private:
189  stream_map_t _streams;
190  stream_list_t _streams_signals_output;
191  stream_list_t _streams_signals_input;
192  Context* _context;
193  };
194 }
195 
196 #endif
Definition: ed247_stream.h:153
ed247_direction_t
Stream direction.
Definition: ed247.h:153
Definition: ed247_stream.h:168
Definition: ed247_channel.h:34
Timestamp structure, seconds from EPOCH (January 1st 1970) and nanoseconds offset with reference to p...
Definition: ed247.h:209
uint16_t ed247_uid_t
Unique identifier type.
Definition: ed247.h:115
Definition: ed247_sample.h:107
Definition: ed247_stream.h:34
Definition: ed247_context.h:36
Definition: ed247_sample.h:56
Definition: ed247_stream.h:43
Definition: ed247_stream.h:33
Definition: ed247_xml.h:147
Definition: ed247_signal.h:37
ed247_status_t
Status codes.
Definition: ed247.h:67
Definition: ed247_channel.cpp:37
ed247_stream_type_t
Stream types.
Definition: ed247.h:132
ed247_status_t(* ed247_stream_recv_callback_t)(ed247_context_t context, ed247_stream_t stream)
Stream receive callback function pointer.
Definition: ed247.h:615
Definition: ed247_stream_assistant.h:11
Definition: ed247_stream_assistant.h:20
Definition: ed247_xml.h:173
Sample Details.
Definition: ed247.h:909
Definition: ed247_context.h:31