RDMnet  0.3.0
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/uuid.h"
28 #include "rdmnet/llrp_manager.h"
29 #include "rdmnet/cpp/common.h"
30 #include "rdmnet/cpp/message.h"
31 
32 namespace rdmnet
33 {
35 namespace llrp
36 {
43 
47 {
48 public:
49  constexpr DestinationAddr(const etcpal::Uuid& cid, const rdm::Uid& uid, uint16_t subdevice = 0);
50 
51  constexpr const LlrpDestinationAddr& get() const noexcept;
52 
53 private:
54  LlrpDestinationAddr addr_;
55 };
56 
61 constexpr DestinationAddr::DestinationAddr(const etcpal::Uuid& cid, const rdm::Uid& uid, uint16_t subdevice)
62  : addr_{cid.get(), uid.get(), subdevice}
63 {
64 }
65 
68 constexpr const LlrpDestinationAddr& DestinationAddr::get() const noexcept
69 {
70  return addr_;
71 }
72 
76 {
78  DiscoveredTarget() = default;
79  constexpr DiscoveredTarget(const LlrpDiscoveredTarget& c_target) noexcept;
80  DiscoveredTarget& operator=(const LlrpDiscoveredTarget& c_target) noexcept;
81 
82  constexpr DestinationAddr address(uint16_t subdevice = 0) const noexcept;
83 
84  const char* ComponentTypeToCString() const noexcept;
85  std::string ComponentTypeToString() const;
86 
87  etcpal::Uuid cid;
88  rdm::Uid uid;
91 };
92 
94 constexpr DiscoveredTarget::DiscoveredTarget(const LlrpDiscoveredTarget& c_target) noexcept
95  : cid(c_target.cid)
96  , uid(c_target.uid)
98  , component_type(c_target.component_type)
99 {
100 }
101 
104 {
105  cid = c_target.cid;
106  uid = c_target.uid;
107  hardware_address = c_target.hardware_address;
108  component_type = c_target.component_type;
109  return *this;
110 }
111 
113 constexpr DestinationAddr DiscoveredTarget::address(uint16_t subdevice) const noexcept
114 {
115  return DestinationAddr(cid, uid, subdevice);
116 }
117 
119 inline const char* DiscoveredTarget::ComponentTypeToCString() const noexcept
120 {
122 }
123 
126 {
128 }
129 
134 class Manager
135 {
136 public:
140  static constexpr Handle kInvalidHandle = LLRP_MANAGER_INVALID;
141 
147  {
148  public:
149  virtual ~NotifyHandler() = default;
150 
154  virtual void HandleLlrpTargetDiscovered(Handle handle, const DiscoveredTarget& target) = 0;
155 
159  virtual void HandleLlrpRdmResponse(Handle handle, const RdmResponse& resp) = 0;
160 
163  virtual void HandleLlrpDiscoveryFinished(Handle handle) { ETCPAL_UNUSED_ARG(handle); }
164  };
165 
166  Manager() = default;
167  Manager(const Manager& other) = delete;
168  Manager& operator=(const Manager& other) = delete;
169  Manager(Manager&& other) = default;
170  Manager& operator=(Manager&& other) = default;
171 
172  etcpal::Error Startup(NotifyHandler& notify_handler,
173  uint16_t manufacturer_id,
174  unsigned int netint_index,
177  void Shutdown();
178 
179  etcpal::Error StartDiscovery(uint16_t filter = 0);
180  etcpal::Error StopDiscovery();
181  etcpal::Expected<uint32_t> SendRdmCommand(const DestinationAddr& destination,
182  rdmnet_command_class_t command_class,
183  uint16_t param_id,
184  const uint8_t* data = nullptr,
185  uint8_t data_len = 0);
186  etcpal::Expected<uint32_t> SendGetCommand(const DestinationAddr& destination,
187  uint16_t param_id,
188  const uint8_t* data = nullptr,
189  uint8_t data_len = 0);
190  etcpal::Expected<uint32_t> SendSetCommand(const DestinationAddr& destination,
191  uint16_t param_id,
192  const uint8_t* data = nullptr,
193  uint8_t data_len = 0);
194 
195  constexpr Handle handle() const;
196  constexpr NotifyHandler* notify_handler() const;
197 
198 private:
199  Handle handle_{kInvalidHandle};
200  NotifyHandler* notify_{nullptr};
201 };
202 
205 
206 namespace internal
207 {
208 extern "C" inline void LlrpManagerLibCbTargetDiscovered(llrp_manager_t handle,
209  const LlrpDiscoveredTarget* target,
210  void* context)
211 {
212  if (target && context)
213  {
214  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpTargetDiscovered(handle, *target);
215  }
216 }
217 
218 extern "C" inline void LlrpManagerLibCbRdmResponseReceived(llrp_manager_t handle,
219  const LlrpRdmResponse* resp,
220  void* context)
221 {
222  if (resp && context)
223  {
224  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpRdmResponse(handle, *resp);
225  }
226 }
227 
228 extern "C" inline void LlrpManagerLibCbDiscoveryFinished(llrp_manager_t handle, void* context)
229 {
230  if (context)
231  {
232  static_cast<Manager::NotifyHandler*>(context)->HandleLlrpDiscoveryFinished(handle);
233  }
234 }
235 }; // namespace internal
236 
238 
248  uint16_t manufacturer_id,
249  unsigned int netint_index,
250  etcpal_iptype_t ip_type,
251  const etcpal::Uuid& cid)
252 {
253  notify_ = &notify_handler;
254 
255  // clang-format off
256  LlrpManagerConfig config = {
257  cid.get(),
258  {
259  ip_type,
260  netint_index
261  },
262  manufacturer_id,
263  {
264  internal::LlrpManagerLibCbTargetDiscovered,
265  internal::LlrpManagerLibCbRdmResponseReceived,
266  internal::LlrpManagerLibCbDiscoveryFinished,
267  &notify_handler
268  }
269  };
270  // clang-format on
271 
272  return llrp_manager_create(&config, &handle_);
273 }
274 
276 inline void Manager::Shutdown()
277 {
278  llrp_manager_destroy(handle_);
279  handle_ = kInvalidHandle;
280 }
281 
291 inline etcpal::Error Manager::StartDiscovery(uint16_t filter)
292 {
293  return llrp_manager_start_discovery(handle_, filter);
294 }
295 
303 {
304  return llrp_manager_stop_discovery(handle_);
305 }
306 
319  rdmnet_command_class_t command_class,
320  uint16_t param_id,
321  const uint8_t* data,
322  uint8_t data_len)
323 {
324  uint32_t seq_num;
325  etcpal_error_t res =
326  llrp_manager_send_rdm_command(handle_, &destination.get(), command_class, param_id, data, data_len, &seq_num);
327  return (res == kEtcPalErrOk ? seq_num : res);
328 }
329 
341  uint16_t param_id,
342  const uint8_t* data,
343  uint8_t data_len)
344 {
345  uint32_t seq_num;
346  etcpal_error_t res = llrp_manager_send_get_command(handle_, &destination.get(), param_id, data, data_len, &seq_num);
347  return (res == kEtcPalErrOk ? seq_num : res);
348 }
349 
361  uint16_t param_id,
362  const uint8_t* data,
363  uint8_t data_len)
364 {
365  uint32_t seq_num;
366  etcpal_error_t res = llrp_manager_send_set_command(handle_, &destination.get(), param_id, data, data_len, &seq_num);
367  return (res == kEtcPalErrOk ? seq_num : res);
368 }
369 
372 {
373  return handle_;
374 }
375 
378 {
379  return notify_;
380 }
381 
382 }; // namespace llrp
383 }; // namespace rdmnet
384 
385 #endif // RDMNET_CPP_LLRP_MANAGER_H_
constexpr const EtcPalUuid & get() const noexcept
static Uuid OsPreferred() noexcept
A destination address for an LLRP RDM command.
Definition: llrp_manager.h:47
constexpr DestinationAddr(const etcpal::Uuid &cid, const rdm::Uid &uid, uint16_t subdevice=0)
Construct a destination address from its component parts.
Definition: llrp_manager.h:61
constexpr const LlrpDestinationAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: llrp_manager.h:68
A class that receives notification callbacks from an LLRP manager.
Definition: llrp_manager.h:147
virtual void HandleLlrpDiscoveryFinished(Handle handle)
The previously-started LLRP discovery process has finished.
Definition: llrp_manager.h:163
virtual void HandleLlrpRdmResponse(Handle handle, const RdmResponse &resp)=0
An RDM response has been received from an LLRP target.
virtual void HandleLlrpTargetDiscovered(Handle handle, const DiscoveredTarget &target)=0
An LLRP target has been discovered.
An instance of LLRP manager functionality.
Definition: llrp_manager.h:135
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:340
void Shutdown()
Shut down this LLRP manager and deallocate resources.
Definition: llrp_manager.h:276
constexpr NotifyHandler * notify_handler() const
Retrieve the NotifyHandler reference that this LLRP manager was configured with.
Definition: llrp_manager.h:377
Manager(Manager &&other)=default
Move a manager instance.
llrp_manager_t Handle
A handle type used by the RDMnet library to identify LLRP manager instances.
Definition: llrp_manager.h:138
Manager & operator=(Manager &&other)=default
Move a manager instance.
etcpal::Error StopDiscovery()
Stop LLRP discovery.
Definition: llrp_manager.h:302
constexpr Handle handle() const
Retrieve the handle of an LLRP manager instance.
Definition: llrp_manager.h:371
etcpal::Error StartDiscovery(uint16_t filter=0)
Start LLRP discovery.
Definition: llrp_manager.h:291
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:247
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:318
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:360
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:316
int llrp_manager_t
A handle for an instance of LLRP Manager functionality.
Definition: llrp_manager.h:51
#define LLRP_MANAGER_INVALID
An invalid LLRP manager handle value.
Definition: llrp_manager.h:53
etcpal_error_t llrp_manager_destroy(llrp_manager_t handle)
Destroy an LLRP manager instance.
Definition: llrp_manager.c:163
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:281
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:245
etcpal_error_t llrp_manager_stop_discovery(llrp_manager_t handle)
Stop discovery on an LLRP manager.
Definition: llrp_manager.c:215
etcpal_error_t llrp_manager_create(const LlrpManagerConfig *config, llrp_manager_t *handle)
Create a new LLRP manager instance.
Definition: llrp_manager.c:128
etcpal_error_t llrp_manager_start_discovery(llrp_manager_t handle, uint16_t filter)
Start discovery on an LLRP manager.
Definition: llrp_manager.c:192
rdmnet_command_class_t
An RDM command class, for RDMnet purposes.
Definition: common.h:350
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
Identifies the type of RPT Component with which an LLRP Target is associated.
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
A set of information associated with an LLRP Target.
Definition: llrp.h:72
A set of information that defines the startup parameters of an LLRP Manager.
Definition: llrp_manager.h:93
An RDM response received from a remote LLRP Target.
Definition: message.h:479
Represents an LLRP target discovered by a manager.
Definition: llrp_manager.h:76
etcpal::MacAddr hardware_address
The lowest hardware address of the machine the target is operating on.
Definition: llrp_manager.h:89
llrp_component_t component_type
The LLRP component type of the target.
Definition: llrp_manager.h:90
const char * ComponentTypeToCString() const noexcept
Convert the target's component type to a string representation.
Definition: llrp_manager.h:119
std::string ComponentTypeToString() const
Convert the target's component type to a string representation.
Definition: llrp_manager.h:125
etcpal::Uuid cid
The target's CID.
Definition: llrp_manager.h:87
rdm::Uid uid
The target's RDM UID.
Definition: llrp_manager.h:88
DiscoveredTarget()=default
Construct a target with null/empty values by default.
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:103
constexpr DestinationAddr address(uint16_t subdevice=0) const noexcept
Get the target's LLRP addressing information.
Definition: llrp_manager.h:113