RDMnet  HEAD (unstable)
Implementation of ANSI E1.33 (RDMnet)
View other versions:
llrp_manager.h
1 /******************************************************************************
2  * Copyright 2020 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 RDMnet. For more information, go to:
17  * https://github.com/ETCLabs/RDMnet
18  *****************************************************************************/
19 
20 #ifndef RDMNET_CPP_LLRP_MANAGER_H_
21 #define RDMNET_CPP_LLRP_MANAGER_H_
22 
23 #include <cstdint>
24 #include "etcpal/common.h"
25 #include "etcpal/cpp/error.h"
26 #include "etcpal/cpp/inet.h"
27 #include "etcpal/cpp/opaque_id.h"
28 #include "etcpal/cpp/uuid.h"
29 #include "rdmnet/llrp_manager.h"
30 #include "rdmnet/cpp/common.h"
31 #include "rdmnet/cpp/message.h"
32 
33 namespace rdmnet
34 {
35 
36 namespace detail
37 {
39 {
40 };
41 }; // namespace detail
42 
44 namespace llrp
45 {
52 
56 {
57 public:
58  constexpr DestinationAddr(const etcpal::Uuid& cid, const rdm::Uid& uid, uint16_t subdevice = 0);
59 
60  constexpr const LlrpDestinationAddr& get() const noexcept;
61 
62 private:
63  LlrpDestinationAddr addr_;
64 };
65 
70 constexpr DestinationAddr::DestinationAddr(const etcpal::Uuid& cid, const rdm::Uid& uid, uint16_t subdevice)
71  : addr_{cid.get(), uid.get(), subdevice}
72 {
73 }
74 
77 constexpr const LlrpDestinationAddr& DestinationAddr::get() const noexcept
78 {
79  return addr_;
80 }
81 
85 {
87  DiscoveredTarget() = default;
88  constexpr DiscoveredTarget(const LlrpDiscoveredTarget& c_target) noexcept;
89  DiscoveredTarget& operator=(const LlrpDiscoveredTarget& c_target) noexcept;
90 
91  constexpr DestinationAddr address(uint16_t subdevice = 0) const noexcept;
92 
93  const char* ComponentTypeToCString() const noexcept;
94  std::string ComponentTypeToString() const;
95 
96  etcpal::Uuid cid;
97  rdm::Uid uid;
100 };
101 
103 constexpr DiscoveredTarget::DiscoveredTarget(const LlrpDiscoveredTarget& c_target) noexcept
104  : cid(c_target.cid)
105  , uid(c_target.uid)
107  , component_type(c_target.component_type)
108 {
109 }
110 
113 {
114  cid = c_target.cid;
115  uid = c_target.uid;
116  hardware_address = c_target.hardware_address;
117  component_type = c_target.component_type;
118  return *this;
119 }
120 
122 constexpr DestinationAddr DiscoveredTarget::address(uint16_t subdevice) const noexcept
123 {
124  return DestinationAddr(cid, uid, subdevice);
125 }
126 
128 inline const char* DiscoveredTarget::ComponentTypeToCString() const noexcept
129 {
131 }
132 
135 {
137 }
138 
143 class Manager
144 {
145 public:
148 
154  {
155  public:
156  virtual ~NotifyHandler() = default;
157 
161  virtual void HandleLlrpTargetDiscovered(Handle handle, const DiscoveredTarget& target) = 0;
162 
166  virtual void HandleLlrpRdmResponse(Handle handle, const RdmResponse& resp) = 0;
167 
170  virtual void HandleLlrpDiscoveryFinished(Handle handle) { ETCPAL_UNUSED_ARG(handle); }
171  };
172 
173  Manager() = default;
174  Manager(const Manager& other) = delete;
175  Manager& operator=(const Manager& other) = delete;
176  Manager(Manager&& other) = default;
177  Manager& operator=(Manager&& other) = default;
178 
179  etcpal::Error Startup(NotifyHandler& notify_handler,
180  uint16_t manufacturer_id,
181  unsigned int netint_index,
184  void Shutdown();
185 
186  etcpal::Error StartDiscovery(uint16_t filter = 0);
187  etcpal::Error StopDiscovery();
188  etcpal::Expected<uint32_t> SendRdmCommand(const DestinationAddr& destination,
189  rdmnet_command_class_t command_class,
190  uint16_t param_id,
191  const uint8_t* data = nullptr,
192  uint8_t data_len = 0);
193  etcpal::Expected<uint32_t> SendGetCommand(const DestinationAddr& destination,
194  uint16_t param_id,
195  const uint8_t* data = nullptr,
196  uint8_t data_len = 0);
197  etcpal::Expected<uint32_t> SendSetCommand(const DestinationAddr& destination,
198  uint16_t param_id,
199  const uint8_t* data = nullptr,
200  uint8_t data_len = 0);
201 
202  constexpr Handle handle() const;
203  constexpr NotifyHandler* notify_handler() const;
204 
205 private:
206  Handle handle_;
207  NotifyHandler* notify_{nullptr};
208 };
209 
212 
213 namespace internal
214 {
215 extern "C" inline void LlrpManagerLibCbTargetDiscovered(llrp_manager_t handle,
216  const LlrpDiscoveredTarget* target,
217  void* context)
218 {
219  if (target && context)
220  {
221  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpTargetDiscovered(Manager::Handle(handle), *target);
222  }
223 }
224 
225 extern "C" inline void LlrpManagerLibCbRdmResponseReceived(llrp_manager_t handle,
226  const LlrpRdmResponse* resp,
227  void* context)
228 {
229  if (resp && context)
230  {
231  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpRdmResponse(Manager::Handle(handle), *resp);
232  }
233 }
234 
235 extern "C" inline void LlrpManagerLibCbDiscoveryFinished(llrp_manager_t handle, void* context)
236 {
237  if (context)
238  {
239  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpDiscoveryFinished(Manager::Handle(handle));
240  }
241 }
242 }; // namespace internal
243 
245 
255  uint16_t manufacturer_id,
256  unsigned int netint_index,
257  etcpal_iptype_t ip_type,
258  const etcpal::Uuid& cid)
259 {
260  notify_ = &notify_handler;
261 
262  // clang-format off
263  LlrpManagerConfig config = {
264  cid.get(),
265  {
266  ip_type,
267  netint_index
268  },
269  manufacturer_id,
270  {
271  internal::LlrpManagerLibCbTargetDiscovered,
272  internal::LlrpManagerLibCbRdmResponseReceived,
273  internal::LlrpManagerLibCbDiscoveryFinished,
274  &notify_handler
275  }
276  };
277  // clang-format on
278 
280  etcpal::Error result = llrp_manager_create(&config, &c_handle);
281 
282  handle_.SetValue(c_handle);
283 
284  return result;
285 }
286 
288 inline void Manager::Shutdown()
289 {
290  llrp_manager_destroy(handle_.value());
291  handle_.Clear();
292 }
293 
303 inline etcpal::Error Manager::StartDiscovery(uint16_t filter)
304 {
305  return llrp_manager_start_discovery(handle_.value(), filter);
306 }
307 
315 {
316  return llrp_manager_stop_discovery(handle_.value());
317 }
318 
331  rdmnet_command_class_t command_class,
332  uint16_t param_id,
333  const uint8_t* data,
334  uint8_t data_len)
335 {
336  uint32_t seq_num;
337  etcpal_error_t res = llrp_manager_send_rdm_command(handle_.value(), &destination.get(), command_class, param_id, data,
338  data_len, &seq_num);
339  return (res == kEtcPalErrOk ? seq_num : res);
340 }
341 
353  uint16_t param_id,
354  const uint8_t* data,
355  uint8_t data_len)
356 {
357  uint32_t seq_num;
358  etcpal_error_t res =
359  llrp_manager_send_get_command(handle_.value(), &destination.get(), param_id, data, data_len, &seq_num);
360  return (res == kEtcPalErrOk ? seq_num : res);
361 }
362 
374  uint16_t param_id,
375  const uint8_t* data,
376  uint8_t data_len)
377 {
378  uint32_t seq_num;
379  etcpal_error_t res =
380  llrp_manager_send_set_command(handle_.value(), &destination.get(), param_id, data, data_len, &seq_num);
381  return (res == kEtcPalErrOk ? seq_num : res);
382 }
383 
386 {
387  return handle_;
388 }
389 
392 {
393  return notify_;
394 }
395 
396 }; // namespace llrp
397 }; // namespace rdmnet
398 
399 #endif // RDMNET_CPP_LLRP_MANAGER_H_
constexpr const EtcPalUuid & get() const noexcept
static Uuid OsPreferred() noexcept
Definition: llrp_manager.h:39
A destination address for an LLRP RDM command.
Definition: llrp_manager.h:56
constexpr const LlrpDestinationAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: llrp_manager.h:77
constexpr DestinationAddr(const etcpal::Uuid &cid, const rdm::Uid &uid, uint16_t subdevice=0)
Definition: llrp_manager.h:70
A class that receives notification callbacks from an LLRP manager.
Definition: llrp_manager.h:154
virtual void HandleLlrpTargetDiscovered(Handle handle, const DiscoveredTarget &target)=0
An LLRP target has been discovered.
virtual void HandleLlrpRdmResponse(Handle handle, const RdmResponse &resp)=0
An RDM response has been received from an LLRP target.
virtual void HandleLlrpDiscoveryFinished(Handle handle)
The previously-started LLRP discovery process has finished.
Definition: llrp_manager.h:170
An instance of LLRP manager functionality.
Definition: llrp_manager.h:144
etcpal::Error StopDiscovery()
Stop LLRP discovery.
Definition: llrp_manager.h:314
etcpal::Error StartDiscovery(uint16_t filter=0)
Start LLRP discovery.
Definition: llrp_manager.h:303
Manager(Manager &&other)=default
Move a manager instance.
Manager & operator=(Manager &&other)=default
Move a manager instance.
etcpal::Expected< uint32_t > SendGetCommand(const DestinationAddr &destination, uint16_t param_id, const uint8_t *data=nullptr, uint8_t data_len=0)
Send an RDM GET command from an LLRP manager.
Definition: llrp_manager.h:352
etcpal::Error Startup(NotifyHandler &notify_handler, uint16_t manufacturer_id, unsigned int netint_index, etcpal_iptype_t ip_type=kEtcPalIpTypeV4, const etcpal::Uuid &cid=etcpal::Uuid::OsPreferred())
Allocate resources and startup this LLRP manager with the given configuration.
Definition: llrp_manager.h:254
etcpal::OpaqueId< detail::LlrpManagerHandleType, llrp_manager_t, LLRP_MANAGER_INVALID > Handle
A handle type used by the RDMnet library to identify LLRP manager instances.
Definition: llrp_manager.h:147
etcpal::Expected< uint32_t > SendRdmCommand(const DestinationAddr &destination, rdmnet_command_class_t command_class, uint16_t param_id, const uint8_t *data=nullptr, uint8_t data_len=0)
Send an RDM command from an LLRP manager.
Definition: llrp_manager.h:330
constexpr Handle handle() const
Retrieve the handle of an LLRP manager instance.
Definition: llrp_manager.h:385
etcpal::Expected< uint32_t > SendSetCommand(const DestinationAddr &destination, uint16_t param_id, const uint8_t *data=nullptr, uint8_t data_len=0)
Send an RDM SET command from an LLRP manager.
Definition: llrp_manager.h:373
void Shutdown()
Shut down this LLRP manager and deallocate resources.
Definition: llrp_manager.h:288
constexpr NotifyHandler * notify_handler() const
Retrieve the NotifyHandler reference that this LLRP manager was configured with.
Definition: llrp_manager.h:391
An RDM response received over LLRP and delivered to an RDMnet callback function.
Definition: llrp_rdm_response.h:45
C++ wrapper for the RDMnet init/deinit functions.
RDMnet C++ message type definitions.
etcpal_error_t
kEtcPalErrOk
etcpal_iptype_t
kEtcPalIpTypeV4
etcpal_error_t llrp_manager_send_set_command(llrp_manager_t handle, const LlrpDestinationAddr *destination, uint16_t param_id, const uint8_t *data, uint8_t data_len, uint32_t *seq_num)
Send an RDM SET command from an LLRP manager.
Definition: llrp_manager.c:342
int llrp_manager_t
Definition: llrp_manager.h:51
#define LLRP_MANAGER_INVALID
Definition: llrp_manager.h:53
etcpal_error_t llrp_manager_destroy(llrp_manager_t handle)
Destroy an LLRP manager instance.
Definition: llrp_manager.c:168
etcpal_error_t llrp_manager_send_get_command(llrp_manager_t handle, const LlrpDestinationAddr *destination, uint16_t param_id, const uint8_t *data, uint8_t data_len, uint32_t *seq_num)
Send an RDM GET command from an LLRP manager.
Definition: llrp_manager.c:301
etcpal_error_t llrp_manager_send_rdm_command(llrp_manager_t handle, const LlrpDestinationAddr *destination, rdmnet_command_class_t command_class, uint16_t param_id, const uint8_t *data, uint8_t data_len, uint32_t *seq_num)
Send an RDM command from an LLRP manager.
Definition: llrp_manager.c:259
etcpal_error_t llrp_manager_stop_discovery(llrp_manager_t handle)
Stop discovery on an LLRP manager.
Definition: llrp_manager.c:226
etcpal_error_t llrp_manager_create(const LlrpManagerConfig *config, llrp_manager_t *handle)
Create a new LLRP manager instance.
Definition: llrp_manager.c:133
etcpal_error_t llrp_manager_start_discovery(llrp_manager_t handle, uint16_t filter)
Start discovery on an LLRP manager.
Definition: llrp_manager.c:200
rdmnet_command_class_t
An RDM command class, for RDMnet purposes.
Definition: common.h:371
const char * llrp_component_type_to_string(llrp_component_t type)
Get a string description of an LLRP component type.
Definition: llrp.c:46
llrp_component_t
Definition: llrp.h:59
Functions for implementing LLRP Manager functionality.
A namespace which contains all C++ language definitions in the RDMnet library.
Definition: broker.h:45
A destination address for an RDM command in LLRP.
Definition: llrp.h:48
Definition: llrp.h:72
Definition: llrp_manager.h:93
Definition: message.h:480
Represents an LLRP target discovered by a manager.
Definition: llrp_manager.h:85
etcpal::MacAddr hardware_address
The lowest hardware address of the machine the target is operating on.
Definition: llrp_manager.h:98
rdm::Uid uid
The target's RDM UID.
Definition: llrp_manager.h:97
DiscoveredTarget & operator=(const LlrpDiscoveredTarget &c_target) noexcept
Assign an instance of the C LlrpDiscoveredTarget type to an instance of this class.
Definition: llrp_manager.h:112
const char * ComponentTypeToCString() const noexcept
Convert the target's component type to a string representation.
Definition: llrp_manager.h:128
llrp_component_t component_type
The LLRP component type of the target.
Definition: llrp_manager.h:99
constexpr DestinationAddr address(uint16_t subdevice=0) const noexcept
Get the target's LLRP addressing information.
Definition: llrp_manager.h:122
etcpal::Uuid cid
The target's CID.
Definition: llrp_manager.h:96
DiscoveredTarget()=default
Construct a target with null/empty values by default.
std::string ComponentTypeToString() const
Convert the target's component type to a string representation.
Definition: llrp_manager.h:134