sACN  2.0.2
Implementation of ANSI E1.31 (Streaming ACN)
View other versions:
source_detector.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************
16  * This file is a part of sACN. For more information, go to:
17  * https://github.com/ETCLabs/sACN
18  *****************************************************************************/
19 
20 #ifndef SACN_CPP_SOURCE_DETECTOR_H_
21 #define SACN_CPP_SOURCE_DETECTOR_H_
22 
28 #include "sacn/cpp/common.h"
29 
30 #include <vector>
31 
32 #include "sacn/source_detector.h"
33 #include "etcpal/cpp/inet.h"
34 #include "etcpal/cpp/uuid.h"
35 
42 namespace sacn
43 {
122 {
123 public:
129  {
130  public:
131  virtual ~NotifyHandler() = default;
132 
148  virtual void HandleSourceUpdated(RemoteSourceHandle handle, const etcpal::Uuid& cid, const std::string& name,
149  const std::vector<uint16_t>& sourced_universes) = 0;
150 
158  virtual void HandleSourceExpired(RemoteSourceHandle handle, const etcpal::Uuid& cid, const std::string& name) = 0;
159 
177  virtual void HandleMemoryLimitExceeded() {}
178  };
179 
184  struct Settings
185  {
186  /********* Required values **********/
187 
188  /********* Optional values **********/
189 
194 
199 
202 
204  Settings() = default;
205  };
206 
207  SourceDetector() = delete;
208 
209  static etcpal::Error Startup(NotifyHandler& notify_handler);
210  static etcpal::Error Startup(NotifyHandler& notify_handler, std::vector<SacnMcastInterface>& netints);
211  static etcpal::Error Startup(const Settings& settings, NotifyHandler& notify_handler);
212  static etcpal::Error Startup(const Settings& settings, NotifyHandler& notify_handler,
214  static void Shutdown();
218 
219 private:
220  static SacnSourceDetectorConfig TranslateConfig(const Settings& settings, NotifyHandler& notify_handler);
221 };
222 
227 namespace internal
228 {
229 extern "C" inline void SourceDetectorCbSourceUpdated(sacn_remote_source_t handle, const EtcPalUuid* cid,
230  const char* name, const uint16_t* sourced_universes,
231  size_t num_sourced_universes, void* context)
232 {
233  if (context && cid && name)
234  {
235  std::vector<uint16_t> sourced_vec;
236  if (sourced_universes && (num_sourced_universes > 0))
237  sourced_vec.assign(sourced_universes, sourced_universes + num_sourced_universes);
238  static_cast<SourceDetector::NotifyHandler*>(context)->HandleSourceUpdated(handle, *cid, name, sourced_vec);
239  }
240 }
241 
242 extern "C" inline void SourceDetectorCbSourceExpired(sacn_remote_source_t handle, const EtcPalUuid* cid,
243  const char* name, void* context)
244 {
245  if (context && cid && name)
246  {
247  static_cast<SourceDetector::NotifyHandler*>(context)->HandleSourceExpired(handle, *cid, name);
248  }
249 }
250 
251 extern "C" inline void SourceDetectorCbMemoryLimitExceeded(void* context)
252 {
253  if (context)
254  {
255  static_cast<SourceDetector::NotifyHandler*>(context)->HandleMemoryLimitExceeded();
256  }
257 }
258 
259 }; // namespace internal
260 
282 {
284  return Startup(Settings(), notify_handler, netints);
285 }
286 
307 {
308  return Startup(Settings(), notify_handler, netints);
309 }
310 
328 inline etcpal::Error SourceDetector::Startup(const Settings& settings, NotifyHandler& notify_handler)
329 {
331  return Startup(settings, notify_handler, netints);
332 }
333 
351 inline etcpal::Error SourceDetector::Startup(const Settings& settings, NotifyHandler& notify_handler,
353 {
354  SacnSourceDetectorConfig config = TranslateConfig(settings, notify_handler);
355 
356  if (netints.empty())
357  return sacn_source_detector_create(&config, NULL);
358 
359  SacnNetintConfig netint_config = {netints.data(), netints.size()};
360  return sacn_source_detector_create(&config, &netint_config);
361 }
362 
372 {
374 }
375 
400 {
402  return ResetNetworking(netints);
403 }
404 
430 {
431  if (sys_netints.empty())
432  {
434  }
435  else
436  {
437  SacnNetintConfig netint_config = {sys_netints.data(), sys_netints.size()};
438  return sacn_source_detector_reset_networking(&netint_config);
439  }
440 }
441 
448 {
449  // This uses a guessing algorithm with a while loop to avoid race conditions.
451  size_t size_guess = 4u;
452  size_t num_netints = 0u;
453 
454  do
455  {
456  netints.resize(size_guess);
457  num_netints = sacn_source_detector_get_network_interfaces(netints.data(), netints.size());
458  size_guess = num_netints + 4u;
459  } while (num_netints > netints.size());
460 
461  netints.resize(num_netints);
462  return netints;
463 }
464 
465 inline SacnSourceDetectorConfig SourceDetector::TranslateConfig(const Settings& settings, NotifyHandler& notify_handler)
466 {
467  // clang-format off
468  SacnSourceDetectorConfig config = {
469  {
470  internal::SourceDetectorCbSourceUpdated,
471  internal::SourceDetectorCbSourceExpired,
472  internal::SourceDetectorCbMemoryLimitExceeded,
473  &notify_handler
474  },
475  settings.source_count_max,
476  settings.universes_per_source_max,
477  settings.ip_supported
478  };
479  // clang-format on
480 
481  return config;
482 }
483 
484 }; // namespace sacn
485 
486 #endif // SACN_CPP_SOURCE_DETECTOR_H_
T assign(T... args)
A base class for a class that receives notification callbacks from a sACN Source Detector.
Definition: source_detector.h:129
virtual void HandleSourceExpired(RemoteSourceHandle handle, const etcpal::Uuid &cid, const std::string &name)=0
Notify that a source is no longer transmitting Universe Discovery messages.
virtual void HandleMemoryLimitExceeded()
Notify that the module has run out of memory to track universes or sources.
Definition: source_detector.h:177
virtual void HandleSourceUpdated(RemoteSourceHandle handle, const etcpal::Uuid &cid, const std::string &name, const std::vector< uint16_t > &sourced_universes)=0
Notify that a source is new or has changed.
An instance of sACN Source Detector functionality.
Definition: source_detector.h:122
static etcpal::Error ResetNetworking()
Resets the underlying network sockets and packet receipt state for the sACN Source Detector.
Definition: source_detector.h:399
static std::vector< EtcPalMcastNetintId > GetNetworkInterfaces()
Obtain the source detector's network interfaces.
Definition: source_detector.h:447
static void Shutdown()
Destroy the sACN Source Detector.
Definition: source_detector.h:371
static etcpal::Error Startup(NotifyHandler &notify_handler)
Start the sACN Source Detector with default settings.
Definition: source_detector.h:281
C++ wrapper for the sACN init/deinit functions.
T data(T... args)
T empty(T... args)
sacn_ip_support_t
Definition: common.h:71
uint16_t sacn_remote_source_t
Definition: common.h:58
@ kSacnIpV4AndIpV6
Definition: common.h:77
#define SACN_SOURCE_DETECTOR_INFINITE
Constant for "infinite" when listening for sources or universes on a source.
Definition: source_detector.h:146
void sacn_source_detector_destroy()
Destroy the sACN Source Detector.
Definition: source_detector.c:126
etcpal_error_t sacn_source_detector_reset_networking(const SacnNetintConfig *sys_netint_config)
Updates the source detector system network interfaces. Also resets the underlying network sockets for...
Definition: source_detector.c:167
etcpal_error_t sacn_source_detector_create(const SacnSourceDetectorConfig *config, const SacnNetintConfig *netint_config)
Create the sACN Source Detector.
Definition: source_detector.c:85
size_t sacn_source_detector_get_network_interfaces(EtcPalMcastNetintId *netints, size_t netints_size)
Obtain the source detector's network interfaces.
Definition: source_detector.c:214
A namespace which contains all C++ language definitions in the sACN library.
Definition: common.h:50
sacn_remote_source_t RemoteSourceHandle
Definition: common.h:52
T resize(T... args)
T size(T... args)
sACN Source Detector API definitions
Definition: common.h:102
Definition: source_detector.h:210
int source_count_max
Definition: source_detector.h:219
A set of configuration settings that a source detector needs to initialize.
Definition: source_detector.h:185
int universes_per_source_max
Definition: source_detector.h:198
int source_count_max
Definition: source_detector.h:193
sacn_ip_support_t ip_supported
Definition: source_detector.h:201