RDMnet  0.3.0
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/uuid.h"
30 #include "rdm/cpp/uid.h"
31 #include "rdmnet/client.h"
32 
33 namespace rdmnet
34 {
39 {
40 public:
41  SourceAddr() = default;
42  constexpr SourceAddr(uint16_t source_endpoint, const rdm::Uid& rdm_source_uid, uint16_t subdevice = 0) noexcept;
43 
44  constexpr const RdmnetSourceAddr& get() const noexcept;
45 
46 private:
47  RdmnetSourceAddr addr_{};
48 };
49 
52 constexpr const RdmnetSourceAddr& SourceAddr::get() const noexcept
53 {
54  return addr_;
55 }
56 
61 constexpr SourceAddr::SourceAddr(uint16_t source_endpoint, const rdm::Uid& rdm_source_uid, uint16_t subdevice) noexcept
62  : addr_{source_endpoint, rdm_source_uid.get(), subdevice}
63 {
64 }
65 
70 {
71 public:
72  DestinationAddr() = default;
73 
74  static constexpr DestinationAddr ToDefaultResponder(const rdm::Uid& rdmnet_uid, uint16_t subdevice = 0);
75  static constexpr DestinationAddr ToDefaultResponder(uint16_t manufacturer_id,
76  uint32_t device_id,
77  uint16_t subdevice = 0);
78  static constexpr DestinationAddr ToSubResponder(const rdm::Uid& rdmnet_uid,
79  uint16_t endpoint,
80  const rdm::Uid& rdm_uid,
81  uint16_t subdevice = 0);
82 
83  constexpr const RdmnetDestinationAddr& get() const noexcept;
84 
85 private:
86  constexpr DestinationAddr(const RdmUid& rdmnet_uid, uint16_t endpoint, const RdmUid& rdm_uid, uint16_t subdevice);
87 
88  RdmnetDestinationAddr addr_{};
89 };
90 
94 constexpr DestinationAddr DestinationAddr::ToDefaultResponder(const rdm::Uid& rdmnet_uid, uint16_t subdevice)
95 {
96  return DestinationAddr(rdmnet_uid.get(), E133_NULL_ENDPOINT, rdmnet_uid.get(), subdevice);
97 }
98 
103 constexpr DestinationAddr DestinationAddr::ToDefaultResponder(uint16_t manufacturer_id,
104  uint32_t device_id,
105  uint16_t subdevice)
106 {
107  return DestinationAddr({manufacturer_id, device_id}, E133_NULL_ENDPOINT, {manufacturer_id, device_id}, subdevice);
108 }
109 
120 constexpr DestinationAddr DestinationAddr::ToSubResponder(const rdm::Uid& rdmnet_uid,
121  uint16_t endpoint,
122  const rdm::Uid& rdm_uid,
123  uint16_t subdevice)
124 {
125  return DestinationAddr(rdmnet_uid.get(), endpoint, rdm_uid.get(), subdevice);
126 }
127 
130 constexpr const RdmnetDestinationAddr& DestinationAddr::get() const noexcept
131 {
132  return addr_;
133 }
134 
135 constexpr DestinationAddr::DestinationAddr(const RdmUid& rdmnet_uid,
136  uint16_t endpoint,
137  const RdmUid& rdm_uid,
138  uint16_t subdevice)
139  : addr_{rdmnet_uid, endpoint, rdm_uid, subdevice}
140 {
141 }
142 
149 {
150 public:
154  ClientConnectedInfo(const ClientConnectedInfo& other) = delete;
157 
158  constexpr ClientConnectedInfo(const RdmnetClientConnectedInfo& c_info) noexcept;
159 
160  constexpr etcpal::SockAddr broker_addr() const noexcept;
161  std::string broker_name() const;
162  constexpr const char* broker_name_c_str() const noexcept;
163  constexpr etcpal::Uuid broker_cid() const noexcept;
164  constexpr rdm::Uid broker_uid() const noexcept;
165 
166  constexpr const RdmnetClientConnectedInfo& get() const noexcept;
167 
168 private:
169  const RdmnetClientConnectedInfo& info_;
170 };
171 
173 constexpr ClientConnectedInfo::ClientConnectedInfo(const RdmnetClientConnectedInfo& c_info) noexcept : info_(c_info)
174 {
175 }
176 
179 {
180  return info_.broker_addr;
181 }
182 
185 {
186  return info_.broker_name;
187 }
188 
190 constexpr const char* ClientConnectedInfo::broker_name_c_str() const noexcept
191 {
192  return info_.broker_name;
193 }
194 
197 {
198  return info_.broker_cid;
199 }
200 
202 constexpr rdm::Uid ClientConnectedInfo::broker_uid() const noexcept
203 {
204  return info_.broker_uid;
205 }
206 
208 constexpr const RdmnetClientConnectedInfo& ClientConnectedInfo::get() const noexcept
209 {
210  return info_;
211 }
212 
219 {
220 public:
227 
228  constexpr ClientConnectFailedInfo(const RdmnetClientConnectFailedInfo& c_info) noexcept;
229 
230  constexpr rdmnet_connect_fail_event_t event() const noexcept;
231  constexpr etcpal::Error socket_err() const noexcept;
232  constexpr rdmnet_connect_status_t rdmnet_reason() const noexcept;
233  constexpr bool will_retry() const noexcept;
234 
235  constexpr bool HasSocketErr() const noexcept;
236  constexpr bool HasRdmnetReason() const noexcept;
237 
238  const char* EventToCString() const noexcept;
239  std::string EventToString() const;
240  const char* RdmnetReasonToCString() const noexcept;
241  std::string RdmnetReasonToString() const;
242 
243  constexpr const RdmnetClientConnectFailedInfo& get() const noexcept;
244 
245 private:
246  const RdmnetClientConnectFailedInfo& info_;
247 };
248 
251  : info_(c_info)
252 {
253 }
254 
257 {
258  return info_.event;
259 }
260 
264 {
265  return info_.socket_err;
266 }
267 
271 {
272  return info_.rdmnet_reason;
273 }
274 
284 constexpr bool ClientConnectFailedInfo::will_retry() const noexcept
285 {
286  return info_.will_retry;
287 }
288 
290 constexpr bool ClientConnectFailedInfo::HasSocketErr() const noexcept
291 {
292  return (info_.event == kRdmnetConnectFailSocketFailure || info_.event == kRdmnetConnectFailTcpLevel);
293 }
294 
296 constexpr bool ClientConnectFailedInfo::HasRdmnetReason() const noexcept
297 {
298  return (info_.event == kRdmnetConnectFailRejected);
299 }
300 
302 inline const char* ClientConnectFailedInfo::EventToCString() const noexcept
303 {
304  return rdmnet_connect_fail_event_to_string(info_.event);
305 }
306 
309 {
310  return rdmnet_connect_fail_event_to_string(info_.event);
311 }
312 
314 inline const char* ClientConnectFailedInfo::RdmnetReasonToCString() const noexcept
315 {
316  return rdmnet_connect_status_to_string(info_.rdmnet_reason);
317 }
318 
321 {
322  return rdmnet_connect_status_to_string(info_.rdmnet_reason);
323 }
324 
327 {
328  return info_;
329 }
330 
337 {
338 public:
345 
346  constexpr ClientDisconnectedInfo(const RdmnetClientDisconnectedInfo& c_info) noexcept;
347 
348  constexpr rdmnet_disconnect_event_t event() const noexcept;
349  constexpr etcpal::Error socket_err() const noexcept;
350  constexpr rdmnet_disconnect_reason_t rdmnet_reason() const noexcept;
351  constexpr bool will_retry() const noexcept;
352 
353  constexpr bool HasSocketErr() const noexcept;
354  constexpr bool HasRdmnetReason() const noexcept;
355 
356  const char* EventToCString() const noexcept;
357  std::string EventToString() const;
358  const char* RdmnetReasonToCString() const noexcept;
359  std::string RdmnetReasonToString() const;
360 
361  constexpr const RdmnetClientDisconnectedInfo& get() const noexcept;
362 
363 private:
364  const RdmnetClientDisconnectedInfo& info_;
365 };
366 
369  : info_(c_info)
370 {
371 }
372 
375 {
376  return info_.event;
377 }
378 
382 {
383  return info_.socket_err;
384 }
385 
389 {
390  return info_.rdmnet_reason;
391 }
392 
398 constexpr bool ClientDisconnectedInfo::will_retry() const noexcept
399 {
400  return info_.will_retry;
401 }
402 
404 constexpr bool ClientDisconnectedInfo::HasSocketErr() const noexcept
405 {
406  return (info_.event == kRdmnetDisconnectAbruptClose);
407 }
408 
410 constexpr bool ClientDisconnectedInfo::HasRdmnetReason() const noexcept
411 {
412  return (info_.event == kRdmnetDisconnectGracefulRemoteInitiated);
413 }
414 
416 inline const char* ClientDisconnectedInfo::EventToCString() const noexcept
417 {
418  return rdmnet_disconnect_event_to_string(info_.event);
419 }
420 
423 {
424  return rdmnet_disconnect_event_to_string(info_.event);
425 }
426 
428 inline const char* ClientDisconnectedInfo::RdmnetReasonToCString() const noexcept
429 {
430  return rdmnet_disconnect_reason_to_string(info_.rdmnet_reason);
431 }
432 
435 {
436  return rdmnet_disconnect_reason_to_string(info_.rdmnet_reason);
437 }
438 
441 {
442  return info_;
443 }
444 
448 
451 constexpr uint16_t kNullEndpoint = E133_NULL_ENDPOINT;
452 
459 class Scope
460 {
461 public:
462  Scope() = default;
463  Scope(const std::string& scope_str, const etcpal::SockAddr& static_broker_addr = etcpal::SockAddr{});
464  Scope(const RdmnetScopeConfig& scope_config);
465 
466  bool IsStatic() const noexcept;
467  bool IsDefault() const noexcept;
468  const std::string& id_string() const noexcept;
469  const etcpal::SockAddr& static_broker_addr() const noexcept;
470 
471  void SetIdString(const std::string& id);
472  void SetIdString(const char* id);
473  void SetStaticBrokerAddr(const etcpal::SockAddr& static_broker_addr);
474 
475 private:
476  std::string id_{E133_DEFAULT_SCOPE};
477  etcpal::SockAddr static_broker_addr_{};
478 };
479 
481 inline Scope::Scope(const std::string& scope_str, const etcpal::SockAddr& static_broker_addr)
482  : id_(scope_str.substr(0, E133_SCOPE_STRING_PADDED_LENGTH - 1)), static_broker_addr_(static_broker_addr)
483 {
484 }
485 
487 inline Scope::Scope(const RdmnetScopeConfig& scope_config) : id_(scope_config.scope)
488 {
489  if (!ETCPAL_IP_IS_INVALID(&scope_config.static_broker_addr.ip))
490  static_broker_addr_ = scope_config.static_broker_addr;
491 }
492 
494 inline bool Scope::IsStatic() const noexcept
495 {
496  return static_broker_addr_.IsValid();
497 }
498 
500 inline bool Scope::IsDefault() const noexcept
501 {
502  return id_ == E133_DEFAULT_SCOPE;
503 }
504 
506 inline const std::string& Scope::id_string() const noexcept
507 {
508  return id_;
509 }
510 
515 inline const etcpal::SockAddr& Scope::static_broker_addr() const noexcept
516 {
517  return static_broker_addr_;
518 }
519 
521 inline void Scope::SetIdString(const std::string& id)
522 {
523  id_ = id;
524 }
525 
528 inline void Scope::SetIdString(const char* id)
529 {
530  if (id)
531  {
532  id_ = id;
533  }
534 }
535 
537 inline void Scope::SetStaticBrokerAddr(const etcpal::SockAddr& static_broker_addr)
538 {
539  static_broker_addr_ = static_broker_addr;
540 }
541 
542 }; // namespace rdmnet
543 
544 #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:219
const char * EventToCString() const noexcept
Get the high-level reason that this connection failed as a C-style string.
Definition: client.h:302
constexpr const RdmnetClientConnectFailedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:326
constexpr bool HasRdmnetReason() const noexcept
Whether the value returned from rdmnet_reason() is valid.
Definition: client.h:296
constexpr bool will_retry() const noexcept
Whether the connection will be retried automatically.
Definition: client.h:284
std::string RdmnetReasonToString() const
Get the reason given in the RDMnet-level connection refuse message as a string.
Definition: client.h:320
const char * RdmnetReasonToCString() const noexcept
Get the reason given in the RDMnet-level connection refuse message as a C-style string.
Definition: client.h:314
constexpr rdmnet_connect_status_t rdmnet_reason() const noexcept
Get the reason given in the RDMnet-level connection refuse message.
Definition: client.h:270
ClientConnectFailedInfo(const ClientConnectFailedInfo &other)=delete
Not copyable.
constexpr rdmnet_connect_fail_event_t event() const noexcept
Get the high-level reason that this connection failed.
Definition: client.h:256
constexpr etcpal::Error socket_err() const noexcept
Get the system error code associated with the failure.
Definition: client.h:263
ClientConnectFailedInfo()=delete
Not default-constructible.
ClientConnectFailedInfo & operator=(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:308
constexpr bool HasSocketErr() const noexcept
Whether the value returned from socket_err() is valid.
Definition: client.h:290
Information about a successful connection to a broker delivered to an RDMnet callback function.
Definition: client.h:149
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:202
constexpr const RdmnetClientConnectedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:208
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:184
constexpr etcpal::Uuid broker_cid() const noexcept
Get the CID of the connected broker.
Definition: client.h:196
ClientConnectedInfo & operator=(const ClientConnectedInfo &other)=delete
Not copyable.
ClientConnectedInfo()=delete
Not default-constructible.
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:190
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:178
Information about a disconnect event from a broker delivered to an RDMnet callback function.
Definition: client.h:337
constexpr bool will_retry() const noexcept
Whether the connection will be retried automatically.
Definition: client.h:398
constexpr etcpal::Error socket_err() const noexcept
Get the system error code associated with the disconnect.
Definition: client.h:381
const char * EventToCString() const noexcept
Get the high-level reason for this disconnect as a C-style string.
Definition: client.h:416
constexpr rdmnet_disconnect_event_t event() const noexcept
Get the high-level reason for this disconnect.
Definition: client.h:374
std::string RdmnetReasonToString() const
Get the reason given in the RDMnet-level disconnect message as a string.
Definition: client.h:434
constexpr const RdmnetClientDisconnectedInfo & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:440
constexpr rdmnet_disconnect_reason_t rdmnet_reason() const noexcept
Get the reason given in the RDMnet-level disconnect message.
Definition: client.h:388
ClientDisconnectedInfo()=delete
Not default-constructible.
constexpr bool HasRdmnetReason() const noexcept
Whether the value returned from rdmnet_reason() is valid.
Definition: client.h:410
const char * RdmnetReasonToCString() const noexcept
Get the reason given in the RDMnet-level disconnect message as a C-style string.
Definition: client.h:428
std::string EventToString() const
Get the high-level reason for this disconnect as a string.
Definition: client.h:422
ClientDisconnectedInfo(const ClientDisconnectedInfo &other)=delete
Not copyable.
ClientDisconnectedInfo & operator=(const ClientDisconnectedInfo &other)=delete
Not copyable.
constexpr bool HasSocketErr() const noexcept
Whether the value returned from socket_err() is valid.
Definition: client.h:404
A destination address for an RDM command in RDMnet's RPT protocol.
Definition: client.h:70
constexpr const RdmnetDestinationAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:130
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:120
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:94
An RDMnet scope configuration.
Definition: client.h:460
const etcpal::SockAddr & static_broker_addr() const noexcept
The static broker address associated with 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:494
bool IsDefault() const noexcept
Whether this scope represents the default RDMnet scope.
Definition: client.h:500
const std::string & id_string() const noexcept
The ID string of this scope.
Definition: client.h:506
void SetStaticBrokerAddr(const etcpal::SockAddr &static_broker_addr)
Set a new static broker IP address and port for this scope.
Definition: client.h:537
void SetIdString(const std::string &id)
Set a new ID string for this scope.
Definition: client.h:521
The source address for an unsolicited RDM response generated by a local component.
Definition: client.h:39
constexpr const RdmnetSourceAddr & get() const noexcept
Get a const reference to the underlying C type.
Definition: client.h:52
API definitions used by RDMnet clients (controllers and devices)
#define ETCPAL_IP_IS_INVALID(etcpal_ip_ptr)
rdmnet_disconnect_reason_t
Disconnect reason defines for the BrokerDisconnectMsg.
Definition: common.h:85
rdmnet_connect_fail_event_t
A high-level reason for RDMnet connection failure.
Definition: common.h:149
rdmnet_disconnect_event_t
A high-level reason for RDMnet connection to be disconnected after successful connection.
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:276
const char * rdmnet_disconnect_event_to_string(rdmnet_disconnect_event_t event)
Get a string description of an RDMnet disconnect event.
Definition: common.c:304
int rdmnet_client_scope_t
A handle to a scope that an RDMnet client participates in.
Definition: client.h:41
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:364
rdmnet_connect_status_t
Connect status defines for the BrokerConnectReplyMsg.
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:333
@ kRdmnetConnectFailRejected
The remote broker rejected the connection at the RDMnet protocol level.
Definition: common.h:169
@ kRdmnetConnectFailSocketFailure
The connection was unable to be started because of an error returned from the system during a lower-l...
Definition: common.h:154
@ kRdmnetConnectFailTcpLevel
The connection started but the TCP connection was never established.
Definition: common.h:159
@ kRdmnetDisconnectAbruptClose
The TCP connection was closed without an RDMnet disconnect message being sent.
Definition: common.h:176
@ kRdmnetDisconnectGracefulRemoteInitiated
The remote component sent an RDMnet disconnect message with a reason code.
Definition: common.h:185
constexpr uint16_t kNullEndpoint
Identifies the NULL_ENDPOINT, the endpoint of the RDMnet default responder.
Definition: client.h:451
rdmnet_client_scope_t ScopeHandle
A handle to a scope that an RDMnet client participates in.
Definition: client.h:447
A namespace which contains all C++ language definitions in the RDMnet library.
Definition: broker.h:45
EtcPalIpAddr ip
Information provided by the library about an unsuccessful RDMnet client connection.
Definition: client.h:164
Information provided by the library about a successful RDMnet client connection.
Definition: client.h:151
const char * broker_name
The DNS name of the broker, if it was discovered using DNS-SD; otherwise an empty string.
Definition: client.h:155
EtcPalSockAddr broker_addr
The IP address and port of the remote broker to which we have connected.
Definition: client.h:153
RdmUid broker_uid
The RDM UID of the connected broker.
Definition: client.h:159
EtcPalUuid broker_cid
The CID of the connected broker.
Definition: client.h:157
Information provided by the library about an RDMnet client connection that disconnected after a succe...
Definition: client.h:196
A destination address for an RDM command in RDMnet's RPT protocol.
Definition: client.h:84
A set of configuration information for a single scope in which an RDMnet client is participating.
Definition: client.h:224
EtcPalSockAddr static_broker_addr
The broker address to which to connect, if a static broker has been configured.
Definition: client.h:235
The source address for an unsolicited RDM response generated by a local component.
Definition: client.h:66