RDMnet  HEAD (unstable)
Implementation of ANSI E1.33 (RDMnet)
View other versions:
client.h
Go to the documentation of this file.
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 
22 
23 #ifndef RDMNET_CPP_CLIENT_H_
24 #define RDMNET_CPP_CLIENT_H_
25 
26 #include <cstdint>
27 #include "etcpal/cpp/error.h"
28 #include "etcpal/cpp/inet.h"
29 #include "etcpal/cpp/opaque_id.h"
30 #include "etcpal/cpp/uuid.h"
31 #include "rdm/cpp/uid.h"
32 #include "rdmnet/client.h"
33 
34 namespace rdmnet
35 {
36 
37 namespace detail
38 {
40 {
41 };
42 }; // namespace detail
43 
48 {
49 public:
50  SourceAddr() = default;
51  constexpr SourceAddr(uint16_t source_endpoint, const rdm::Uid& rdm_source_uid, uint16_t subdevice = 0) noexcept;
52 
53  constexpr const RdmnetSourceAddr& get() const noexcept;
54 
55 private:
56  RdmnetSourceAddr addr_{};
57 };
58 
61 constexpr const RdmnetSourceAddr& SourceAddr::get() const noexcept
62 {
63  return addr_;
64 }
65 
70 constexpr SourceAddr::SourceAddr(uint16_t source_endpoint, const rdm::Uid& rdm_source_uid, uint16_t subdevice) noexcept
71  : addr_{source_endpoint, rdm_source_uid.get(), subdevice}
72 {
73 }
74 
79 {
80 public:
81  DestinationAddr() = default;
82 
83  static constexpr DestinationAddr ToDefaultResponder(const rdm::Uid& rdmnet_uid, uint16_t subdevice = 0);
84  static constexpr DestinationAddr ToDefaultResponder(uint16_t manufacturer_id,
85  uint32_t device_id,
86  uint16_t subdevice = 0);
87  static constexpr DestinationAddr ToSubResponder(const rdm::Uid& rdmnet_uid,
88  uint16_t endpoint,
89  const rdm::Uid& rdm_uid,
90  uint16_t subdevice = 0);
91 
92  constexpr const RdmnetDestinationAddr& get() const noexcept;
93 
94 private:
95  constexpr DestinationAddr(const RdmUid& rdmnet_uid, uint16_t endpoint, const RdmUid& rdm_uid, uint16_t subdevice);
96 
97  RdmnetDestinationAddr addr_{};
98 };
99 
103 constexpr DestinationAddr DestinationAddr::ToDefaultResponder(const rdm::Uid& rdmnet_uid, uint16_t subdevice)
104 {
105  return DestinationAddr(rdmnet_uid.get(), E133_NULL_ENDPOINT, rdmnet_uid.get(), subdevice);
106 }
107 
112 constexpr DestinationAddr DestinationAddr::ToDefaultResponder(uint16_t manufacturer_id,
113  uint32_t device_id,
114  uint16_t subdevice)
115 {
116  return DestinationAddr({manufacturer_id, device_id}, E133_NULL_ENDPOINT, {manufacturer_id, device_id}, subdevice);
117 }
118 
129 constexpr DestinationAddr DestinationAddr::ToSubResponder(const rdm::Uid& rdmnet_uid,
130  uint16_t endpoint,
131  const rdm::Uid& rdm_uid,
132  uint16_t subdevice)
133 {
134  return DestinationAddr(rdmnet_uid.get(), endpoint, rdm_uid.get(), subdevice);
135 }
136 
139 constexpr const RdmnetDestinationAddr& DestinationAddr::get() const noexcept
140 {
141  return addr_;
142 }
143 
144 constexpr DestinationAddr::DestinationAddr(const RdmUid& rdmnet_uid,
145  uint16_t endpoint,
146  const RdmUid& rdm_uid,
147  uint16_t subdevice)
148  : addr_{rdmnet_uid, endpoint, rdm_uid, subdevice}
149 {
150 }
151 
158 {
159 public:
163  ClientConnectedInfo(const ClientConnectedInfo& other) = delete;
166 
167  constexpr ClientConnectedInfo(const RdmnetClientConnectedInfo& c_info) noexcept;
168 
169  constexpr etcpal::SockAddr broker_addr() const noexcept;
170  std::string broker_name() const;
171  constexpr const char* broker_name_c_str() const noexcept;
172  constexpr etcpal::Uuid broker_cid() const noexcept;
173  constexpr rdm::Uid broker_uid() const noexcept;
174 
175  constexpr const RdmnetClientConnectedInfo& get() const noexcept;
176 
177 private:
178  const RdmnetClientConnectedInfo& info_;
179 };
180 
182 constexpr ClientConnectedInfo::ClientConnectedInfo(const RdmnetClientConnectedInfo& c_info) noexcept : info_(c_info)
183 {
184 }
185 
188 {
189  return info_.broker_addr;
190 }
191 
194 {
195  return info_.broker_name;
196 }
197 
199 constexpr const char* ClientConnectedInfo::broker_name_c_str() const noexcept
200 {
201  return info_.broker_name;
202 }
203 
206 {
207  return info_.broker_cid;
208 }
209 
211 constexpr rdm::Uid ClientConnectedInfo::broker_uid() const noexcept
212 {
213  return info_.broker_uid;
214 }
215 
217 constexpr const RdmnetClientConnectedInfo& ClientConnectedInfo::get() const noexcept
218 {
219  return info_;
220 }
221 
228 {
229 public:
236 
237  constexpr ClientConnectFailedInfo(const RdmnetClientConnectFailedInfo& c_info) noexcept;
238 
239  constexpr rdmnet_connect_fail_event_t event() const noexcept;
240  constexpr etcpal::Error socket_err() const noexcept;
241  constexpr rdmnet_connect_status_t rdmnet_reason() const noexcept;
242  constexpr bool will_retry() const noexcept;
243 
244  constexpr bool HasSocketErr() const noexcept;
245  constexpr bool HasRdmnetReason() const noexcept;
246 
247  const char* EventToCString() const noexcept;
248  std::string EventToString() const;
249  const char* RdmnetReasonToCString() const noexcept;
250  std::string RdmnetReasonToString() const;
251 
252  constexpr const RdmnetClientConnectFailedInfo& get() const noexcept;
253 
254 private:
255  const RdmnetClientConnectFailedInfo& info_;
256 };
257 
260  : info_(c_info)
261 {
262 }
263 
266 {
267  return info_.event;
268 }
269 
273 {
274  return info_.socket_err;
275 }
276 
280 {
281  return info_.rdmnet_reason;
282 }
283 
293 constexpr bool ClientConnectFailedInfo::will_retry() const noexcept
294 {
295  return info_.will_retry;
296 }
297 
299 constexpr bool ClientConnectFailedInfo::HasSocketErr() const noexcept
300 {
301  return (info_.event == kRdmnetConnectFailSocketFailure || info_.event == kRdmnetConnectFailTcpLevel);
302 }
303 
305 constexpr bool ClientConnectFailedInfo::HasRdmnetReason() const noexcept
306 {
307  return (info_.event == kRdmnetConnectFailRejected);
308 }
309 
311 inline const char* ClientConnectFailedInfo::EventToCString() const noexcept
312 {
313  return rdmnet_connect_fail_event_to_string(info_.event);
314 }
315 
318 {
319  return rdmnet_connect_fail_event_to_string(info_.event);
320 }
321 
323 inline const char* ClientConnectFailedInfo::RdmnetReasonToCString() const noexcept
324 {
325  return rdmnet_connect_status_to_string(info_.rdmnet_reason);
326 }
327 
330 {
331  return rdmnet_connect_status_to_string(info_.rdmnet_reason);
332 }
333 
336 {
337  return info_;
338 }
339 
346 {
347 public:
354 
355  constexpr ClientDisconnectedInfo(const RdmnetClientDisconnectedInfo& c_info) noexcept;
356 
357  constexpr rdmnet_disconnect_event_t event() const noexcept;
358  constexpr etcpal::Error socket_err() const noexcept;
359  constexpr rdmnet_disconnect_reason_t rdmnet_reason() const noexcept;
360  constexpr bool will_retry() const noexcept;
361 
362  constexpr bool HasSocketErr() const noexcept;
363  constexpr bool HasRdmnetReason() const noexcept;
364 
365  const char* EventToCString() const noexcept;
366  std::string EventToString() const;
367  const char* RdmnetReasonToCString() const noexcept;
368  std::string RdmnetReasonToString() const;
369 
370  constexpr const RdmnetClientDisconnectedInfo& get() const noexcept;
371 
372 private:
373  const RdmnetClientDisconnectedInfo& info_;
374 };
375 
378  : info_(c_info)
379 {
380 }
381 
384 {
385  return info_.event;
386 }
387 
391 {
392  return info_.socket_err;
393 }
394 
398 {
399  return info_.rdmnet_reason;
400 }
401 
407 constexpr bool ClientDisconnectedInfo::will_retry() const noexcept
408 {
409  return info_.will_retry;
410 }
411 
413 constexpr bool ClientDisconnectedInfo::HasSocketErr() const noexcept
414 {
415  return (info_.event == kRdmnetDisconnectAbruptClose);
416 }
417 
419 constexpr bool ClientDisconnectedInfo::HasRdmnetReason() const noexcept
420 {
421  return (info_.event == kRdmnetDisconnectGracefulRemoteInitiated);
422 }
423 
425 inline const char* ClientDisconnectedInfo::EventToCString() const noexcept
426 {
427  return rdmnet_disconnect_event_to_string(info_.event);
428 }
429 
432 {
433  return rdmnet_disconnect_event_to_string(info_.event);
434 }
435 
437 inline const char* ClientDisconnectedInfo::RdmnetReasonToCString() const noexcept
438 {
439  return rdmnet_disconnect_reason_to_string(info_.rdmnet_reason);
440 }
441 
444 {
445  return rdmnet_disconnect_reason_to_string(info_.rdmnet_reason);
446 }
447 
450 {
451  return info_;
452 }
453 
457 
460 constexpr uint16_t kNullEndpoint = E133_NULL_ENDPOINT;
461 
468 class Scope
469 {
470 public:
471  Scope() = default;
472  Scope(const std::string& scope_str, const etcpal::SockAddr& static_broker_addr = etcpal::SockAddr{});
473  Scope(const RdmnetScopeConfig& scope_config);
474 
475  bool IsStatic() const noexcept;
476  bool IsDefault() const noexcept;
477  const std::string& id_string() const noexcept;
478  const etcpal::SockAddr& static_broker_addr() const noexcept;
479 
480  void SetIdString(const std::string& id);
481  void SetIdString(const char* id);
482  void SetStaticBrokerAddr(const etcpal::SockAddr& static_broker_addr);
483 
484 private:
485  std::string id_{E133_DEFAULT_SCOPE};
486  etcpal::SockAddr static_broker_addr_{};
487 };
488 
490 inline Scope::Scope(const std::string& scope_str, const etcpal::SockAddr& static_broker_addr)
491  : id_(scope_str.substr(0, E133_SCOPE_STRING_PADDED_LENGTH - 1)), static_broker_addr_(static_broker_addr)
492 {
493 }
494 
496 inline Scope::Scope(const RdmnetScopeConfig& scope_config) : id_(scope_config.scope)
497 {
498  if (!ETCPAL_IP_IS_INVALID(&scope_config.static_broker_addr.ip))
499  static_broker_addr_ = scope_config.static_broker_addr;
500 }
501 
503 inline bool Scope::IsStatic() const noexcept
504 {
505  return static_broker_addr_.IsValid();
506 }
507 
509 inline bool Scope::IsDefault() const noexcept
510 {
511  return id_ == E133_DEFAULT_SCOPE;
512 }
513 
515 inline const std::string& Scope::id_string() const noexcept
516 {
517  return id_;
518 }
519 
524 inline const etcpal::SockAddr& Scope::static_broker_addr() const noexcept
525 {
526  return static_broker_addr_;
527 }
528 
530 inline void Scope::SetIdString(const std::string& id)
531 {
532  id_ = id;
533 }
534 
537 inline void Scope::SetIdString(const char* id)
538 {
539  if (id)
540  {
541  id_ = id;
542  }
543 }
544 
546 inline void Scope::SetStaticBrokerAddr(const etcpal::SockAddr& static_broker_addr)
547 {
548  static_broker_addr_ = static_broker_addr;
549 }
550 
551 }; // namespace rdmnet
552 
553 #endif // RDMNET_CPP_CLIENT_H_
constexpr bool IsValid() const noexcept
Information about a failed connection to a broker delivered to an RDMnet callback function.
Definition: client.h:228
const char * EventToCString() const noexcept
Get the high-level reason that this connection failed as a C-style string.
Definition: client.h:311
const char * RdmnetReasonToCString() const noexcept
Get the reason given in the RDMnet-level connection refuse message as a C-style string.
Definition: client.h:323
constexpr rdmnet_connect_status_t rdmnet_reason() const noexcept
Get the reason given in the RDMnet-level connection refuse message.
Definition: client.h:279
constexpr bool will_retry() const noexcept
Whether the connection will be retried automatically.
Definition: client.h:293
constexpr bool HasSocketErr() const noexcept
Whether the value returned from socket_err() is valid.
Definition: client.h:299
ClientConnectFailedInfo(const ClientConnectFailedInfo &other)=delete
Not copyable.
std::string EventToString() const
Get the high-level reason that this connection failed as a string.
Definition: client.h:317
constexpr etcpal::Error socket_err() const noexcept
Get the system error code associated with the failure.
Definition: client.h:272
std::string RdmnetReasonToString() const
Get the reason given in the RDMnet-level connection refuse message as a string.
Definition: client.h:329
constexpr bool HasRdmnetReason() const noexcept
Whether the value returned from rdmnet_reason() is valid.
Definition: client.h:305
ClientConnectFailedInfo & operator=(const ClientConnectFailedInfo &other)=delete
Not copyable.
ClientConnectFailedInfo()=delete
Not default-constructible.
constexpr rdmnet_connect_fail_event_t event() const noexcept
Get the high-level reason that this connection failed.
Definition: client.h:265
constexpr const RdmnetClientConnectFailedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:335
Information about a successful connection to a broker delivered to an RDMnet callback function.
Definition: client.h:158
std::string broker_name() const
Get the DNS name of the broker (if it was discovered via DNS-SD; otherwise this will be an empty stri...
Definition: client.h:193
ClientConnectedInfo(const ClientConnectedInfo &other)=delete
Not copyable.
constexpr rdm::Uid broker_uid() const noexcept
Get the RDM UID of the connected broker.
Definition: client.h:211
constexpr etcpal::Uuid broker_cid() const noexcept
Get the CID of the connected broker.
Definition: client.h:205
constexpr const char * broker_name_c_str() const noexcept
Get the DNS name of the broker (if it was discovered via DNS-SD; otherwise this will be an empty stri...
Definition: client.h:199
ClientConnectedInfo & operator=(const ClientConnectedInfo &other)=delete
Not copyable.
ClientConnectedInfo()=delete
Not default-constructible.
constexpr const RdmnetClientConnectedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:217
constexpr etcpal::SockAddr broker_addr() const noexcept
Get the IP address and port of the remote broker to which we have connected.
Definition: client.h:187
Information about a disconnect event from a broker delivered to an RDMnet callback function.
Definition: client.h:346
ClientDisconnectedInfo & operator=(const ClientDisconnectedInfo &other)=delete
Not copyable.
const char * EventToCString() const noexcept
Get the high-level reason for this disconnect as a C-style string.
Definition: client.h:425
constexpr rdmnet_disconnect_event_t event() const noexcept
Get the high-level reason for this disconnect.
Definition: client.h:383
const char * RdmnetReasonToCString() const noexcept
Get the reason given in the RDMnet-level disconnect message as a C-style string.
Definition: client.h:437
constexpr bool will_retry() const noexcept
Whether the connection will be retried automatically.
Definition: client.h:407
ClientDisconnectedInfo()=delete
Not default-constructible.
constexpr bool HasSocketErr() const noexcept
Whether the value returned from socket_err() is valid.
Definition: client.h:413
constexpr rdmnet_disconnect_reason_t rdmnet_reason() const noexcept
Get the reason given in the RDMnet-level disconnect message.
Definition: client.h:397
std::string EventToString() const
Get the high-level reason for this disconnect as a string.
Definition: client.h:431
constexpr etcpal::Error socket_err() const noexcept
Get the system error code associated with the disconnect.
Definition: client.h:390
std::string RdmnetReasonToString() const
Get the reason given in the RDMnet-level disconnect message as a string.
Definition: client.h:443
constexpr bool HasRdmnetReason() const noexcept
Whether the value returned from rdmnet_reason() is valid.
Definition: client.h:419
ClientDisconnectedInfo(const ClientDisconnectedInfo &other)=delete
Not copyable.
constexpr const RdmnetClientDisconnectedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:449
A destination address for an RDM command in RDMnet's RPT protocol.
Definition: client.h:79
static constexpr DestinationAddr ToDefaultResponder(const rdm::Uid &rdmnet_uid, uint16_t subdevice=0)
Get a DestinationAddr representing a message addressed to a component's default responder.
Definition: client.h:103
static constexpr DestinationAddr ToSubResponder(const rdm::Uid &rdmnet_uid, uint16_t endpoint, const rdm::Uid &rdm_uid, uint16_t subdevice=0)
Get a DestinationAddr representing a message addressed to a sub-responder on a component.
Definition: client.h:129
constexpr const RdmnetDestinationAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:139
An RDMnet scope configuration.
Definition: client.h:469
const etcpal::SockAddr & static_broker_addr() const noexcept
The static broker address associated with this scope.
Definition: client.h:524
bool IsDefault() const noexcept
Whether this scope represents the default RDMnet scope.
Definition: client.h:509
void SetIdString(const std::string &id)
Set a new ID string for this scope.
Definition: client.h:530
const std::string & id_string() const noexcept
The ID string of this scope.
Definition: client.h:515
bool IsStatic() const noexcept
Whether this scope has been configured with a static IP address and port for a broker.
Definition: client.h:503
void SetStaticBrokerAddr(const etcpal::SockAddr &static_broker_addr)
Set a new static broker IP address and port for this scope.
Definition: client.h:546
The source address for an unsolicited RDM response generated by a local component.
Definition: client.h:48
constexpr const RdmnetSourceAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:61
Definition: client.h:40
API definitions used by RDMnet clients (controllers and devices)
#define ETCPAL_IP_IS_INVALID(etcpal_ip_ptr)
rdmnet_disconnect_reason_t
Definition: common.h:85
rdmnet_connect_fail_event_t
Definition: common.h:149
rdmnet_disconnect_event_t
Definition: common.h:174
const char * rdmnet_connect_fail_event_to_string(rdmnet_connect_fail_event_t event)
Get a string description of an RDMnet connection failure event.
Definition: common.c:350
const char * rdmnet_disconnect_event_to_string(rdmnet_disconnect_event_t event)
Get a string description of an RDMnet disconnect event.
Definition: common.c:378
const char * rdmnet_disconnect_reason_to_string(rdmnet_disconnect_reason_t code)
Get a string description of an RDMnet disconnect reason code.
Definition: common.c:438
rdmnet_connect_status_t
Definition: common.h:117
const char * rdmnet_connect_status_to_string(rdmnet_connect_status_t code)
Get a string description of an RDMnet connect status code.
Definition: common.c:407
@ kRdmnetConnectFailRejected
Definition: common.h:169
@ kRdmnetConnectFailSocketFailure
Definition: common.h:154
@ kRdmnetConnectFailTcpLevel
Definition: common.h:159
@ kRdmnetDisconnectAbruptClose
Definition: common.h:176
@ kRdmnetDisconnectGracefulRemoteInitiated
Definition: common.h:185
constexpr uint16_t kNullEndpoint
Identifies the NULL_ENDPOINT, the endpoint of the RDMnet default responder.
Definition: client.h:460
A namespace which contains all C++ language definitions in the RDMnet library.
Definition: broker.h:45
EtcPalIpAddr ip
Definition: client.h:164
Definition: client.h:151
EtcPalSockAddr broker_addr
Definition: client.h:153
EtcPalUuid broker_cid
Definition: client.h:157
const char * broker_name
Definition: client.h:155
RdmUid broker_uid
Definition: client.h:159
Definition: client.h:196
A destination address for an RDM command in RDMnet's RPT protocol.
Definition: client.h:84
Definition: client.h:224
EtcPalSockAddr static_broker_addr
Definition: client.h:235
The source address for an unsolicited RDM response generated by a local component.
Definition: client.h:66