API Documentation
Loading...
Searching...
No Matches
Connection.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Hardware
28File: Connection
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include "DLLInfo.h"
34#include <NDEVR/SoftwareService.h>
35#include <NDEVR/Device.h>
36#include <NDEVR/String.h>
37#include <NDEVR/TimeSpan.h>
38#include <QObject>
39class QAbstractSocket;
40class QTcpSocket;
41class QUdpSocket;
42class QSerialPortInfo;
43class QSerialPort;
44class QBluetoothSocket;
45class QBluetoothServiceInfo;
46class QBluetoothDeviceInfo;
47class QBluetoothAddress;
48class QIODevice;
49namespace NDEVR
50{
51 class Log;
52 class SocketIODriver;
53 /**--------------------------------------------------------------------------------------------------
54 \brief Describes the state of the connection.
55 **/
72 /**--------------------------------------------------------------------------------------------------
73 \brief Describes how to open a connection
74 **/
76 {
77 e_not_open = 0x0000,
78 e_read_only = 0x0001,
79 e_write_only = 0x0002,
81 e_append = 0x0004,
82 e_truncate = 0x0008,
83 e_text = 0x0010,
84 e_unbuffered = 0x0020,
85 e_new_only = 0x0040,
86 e_existing_only = 0x0080
87 };
93 /**--------------------------------------------------------------------------------------------------
94 \brief A structure designed to store information about a specific Connection
95 **/
97 {
103 ProgressInfo* log = nullptr;
105
106 String id() const
107 {
108 return type + "|" + address;
109 }
111 {
112 name = TranslatedString(id());
113 }
114 HARDWARE_API void setID(const String& id);
115 bool operator!=(const ConnectionInfo& other) const
116 {
117 return type != other.type
118 || address != other.address
119 || max_timeout != other.max_timeout
120 || connect_mode != other.connect_mode;
121 }
122 };
123 /**--------------------------------------------------------------------------------------------------
124 \brief A category represents the type of connection. For example Bluetooth, Serial Port, TCP, etc
125 **/
132 /**--------------------------------------------------------------------------------------------------
133 \brief Describes settings for performing a Connection search.
134 **/
148 /**--------------------------------------------------------------------------------------------------
149 \brief Provides the unseen backend logic for setting up, tearing down, and sending data over a specific
150 type of connection.
151 **/
152 class HARDWARE_API ConnectionBackend : public QObject
153 {
154 Q_OBJECT
155 public:
157 virtual uint04 peek(char* s, uint04 size = Constant<uint04>::Max) const = 0;
158 virtual uint04 rx(char* s, uint04 max_size = Constant<uint04>::Max) = 0;
159 virtual void tx(const String& command) = 0;
160 virtual bool open(const ConnectionInfo& request) = 0;
161 virtual void close() = 0;
162 virtual bool isOpen() const = 0;
163 virtual bool waitForReadyRead(const TimeSpan& span) = 0;
164 virtual TranslatedString lastError() const = 0;
165 virtual uint04 bytesAvailable() const = 0;
166 virtual String threadID() const { return String(); };
167 signals:
170 protected:
172 };
173 /**--------------------------------------------------------------------------------------------------
174 \brief Stores factories that can be registered for creating Connections of various types.
175 **/
192
193 /**--------------------------------------------------------------------------------------------------
194 \brief A standard interface for all types of connections that allow transmitting and recieving of
195 data between two devices or services.
196 **/
198 {
199 Q_OBJECT
200 public:
201 Connection(const Model& model, ProgressInfo* raw_network_log = nullptr, ProgressInfo* command_log = nullptr, QObject* parent = nullptr);
202 Connection(const ConnectionInfo& connection, const Model& model, ProgressInfo* raw_network_log = nullptr, ProgressInfo* command_log = nullptr, QObject* parent = nullptr);
203 virtual ~Connection();
204 //if size == nan, use m_default_rx_buffer_size
206 [[nodiscard]] Buffer<String> rx(char deliminator, uint04 size = Constant<uint04>::Invalid);
207 [[nodiscard]] Buffer<String> rx(char deliminator, const String& end_sequence, uint04 size = Constant<uint04>::Invalid);
210 [[nodiscard]] const String& rxLine(TimeSpan wait_time, char delimeter = '\n', uint04 size = Constant<uint04>::Invalid);
211 virtual void tx(const String& data);
212 virtual bool open();
213 virtual bool open(const ConnectionInfo& request);
214 virtual void close();
215 virtual void setConnectionInfo(const ConnectionInfo& request);
216 virtual void clearConnectionInfo();
217 [[nodiscard]] virtual bool isOpen() const;
218 [[nodiscard]] virtual const String& address() const { return m_connection_info.address; }
219 [[nodiscard]] virtual const String& type() const { return m_connection_info.type; }
220 void setRawDataLog(ProgressInfo* raw_network_log);
222 [[nodiscard]] virtual String threadID() const;
223 [[nodiscard]] virtual bool canReadLine(char deliminator = '\n') const;
224 virtual void setTXDelay(const TimeSpan& tx_delay) { m_tx_delay = tx_delay; }
225 [[nodiscard]] ProgressInfo* rawNetworkLog() { return m_raw_data_log; }
226 [[nodiscard]] Time lastRxTime() const { return m_last_rx; }
227 [[nodiscard]] Time lastTxTime() const { return m_last_tx; }
228 [[nodiscard]] const ProgressInfo* rawNetworkLog() const { return m_raw_data_log; }
229 [[nodiscard]] const ConnectionInfo& connectionInfo() const { return m_connection_info; }
230 [[nodiscard]] ConnectionState connectionState() const;
231 [[nodiscard]] ConnectionBackend* ioDevice() const;
232 virtual bool waitForReadyRead(const TimeSpan& span);
233 virtual void setDefaultRxBufferSize(uint04 default_rx_buffer_size);
234 void setRawLoggingMode(RawConnectionLoggingMode mode) { m_raw_logging_mode = mode; }
235 [[nodiscard]] virtual bool ping() const;
236 public:
237 [[nodiscard]] static bool Ping(const String& address, ProgressInfo* log = nullptr);
238 [[nodiscard]] static bool Ping(const String& address, String& response, ProgressInfo* log = nullptr);
239 static bool LogNetworkInfo(ProgressInfo* log);
240 static bool LogIPInfo(ProgressInfo* log);
241 protected:
242 virtual void setBackend(ConnectionBackend* device);
245 bool openBackend(ConnectionBackend* io_device, const ConnectionInfo& request);
246 public slots:
249 signals:
254 protected:
256 Buffer<std::function<void(const String&)>> m_tx_callback;
257 Buffer<std::function<void(const String&)>> m_rx_callback;
259 Time m_last_rx = Time(0);
260 Time m_last_tx = Time(0);
261 TimeSpan m_tx_delay = TimeSpan(0);
262 ConnectionBackend* m_io_device = nullptr;
263 ProgressInfo* m_raw_data_log = nullptr;
264 uint04 m_total_tx = 0;
265 uint04 m_total_rx = 0;
266 uint04 m_default_rx_buffer_size = 2046;
267 ConnectionState m_connection_state = ConnectionState::e_no_connection;
268 RawConnectionLoggingMode m_raw_logging_mode = RawConnectionLoggingMode::e_ascii_logging;
269 bool m_log_tx = true;
270 bool m_log_rx = true;
271 bool m_show_errors = false;
272 };
273}
#define HARDWARE_API
Definition DLLInfo.h:56
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
Provides the unseen backend logic for setting up, tearing down, and sending data over a specific type...
Definition Connection.h:153
void connectionStateChangedSignal(ConnectionState state)
virtual bool waitForReadyRead(const TimeSpan &span)=0
virtual String threadID() const
Definition Connection.h:166
virtual bool open(const ConnectionInfo &request)=0
virtual uint04 bytesAvailable() const =0
ConnectionBackend(ProgressInfo *log)
virtual void tx(const String &command)=0
ProgressInfo * m_log
Definition Connection.h:171
virtual uint04 rx(char *s, uint04 max_size=Constant< uint04 >::Max)=0
virtual uint04 peek(char *s, uint04 size=Constant< uint04 >::Max) const =0
virtual bool isOpen() const =0
virtual void close()=0
virtual TranslatedString lastError() const =0
Stores factories that can be registered for creating Connections of various types.
Definition Connection.h:177
static Buffer< ConnectionFactory * > s_registered_factories
Definition Connection.h:190
static Buffer< ConnectionCategory > AvailableConnectionCatagories()
virtual bool handlesType(const String &request) const =0
static void RegisterConnectionFactory(ConnectionFactory *factory)
static ConnectionBackend * CreateConnection(const ConnectionInfo &request)
virtual ConnectionBackend * createConnection(const ConnectionInfo &request)=0
static Buffer< ConnectionInfo > AvailableConnections(const ConnectionSearchParamaters &params)
virtual Buffer< ConnectionInfo > availableConnections(const ConnectionSearchParamaters &params)=0
virtual Buffer< ConnectionCategory > availableConnectionCatagories()=0
virtual bool canConnectTo(const ConnectionInfo &request) const
A standard interface for all types of connections that allow transmitting and recieving of data betwe...
Definition Connection.h:198
void connectionStateChangedSignal(ConnectionState state)
virtual String threadID() const
virtual const String & address() const
Definition Connection.h:218
virtual void setBackend(ConnectionBackend *device)
String m_connection_rx_buffer
Definition Connection.h:258
void dataAvailableSignal()
ProgressInfo * rawNetworkLog()
Definition Connection.h:225
virtual bool open()
const ProgressInfo * rawNetworkLog() const
Definition Connection.h:228
void setRawDataLog(ProgressInfo *raw_network_log)
virtual bool waitForReadyRead(const TimeSpan &span)
virtual void close()
virtual void clearConnectionInfo()
virtual void tx(const String &data)
virtual void setConnectionInfo(const ConnectionInfo &request)
virtual void setDefaultRxBufferSize(uint04 default_rx_buffer_size)
void handleErrorString(const TranslatedString &error)
const String & peek(uint04 size=Constant< uint04 >::Invalid) const
Connection(const ConnectionInfo &connection, const Model &model, ProgressInfo *raw_network_log=nullptr, ProgressInfo *command_log=nullptr, QObject *parent=nullptr)
Connection(const Model &model, ProgressInfo *raw_network_log=nullptr, ProgressInfo *command_log=nullptr, QObject *parent=nullptr)
Buffer< std::function< void(const String &)> > m_rx_callback
Definition Connection.h:257
Buffer< String > rx(char deliminator, uint04 size=Constant< uint04 >::Invalid)
virtual void setConnectionState(ConnectionState state)
ConnectionState connectionState() const
virtual void setTXDelay(const TimeSpan &tx_delay)
Definition Connection.h:224
virtual bool open(const ConnectionInfo &request)
void setRawLoggingMode(RawConnectionLoggingMode mode)
Definition Connection.h:234
const ConnectionInfo & connectionInfo() const
Definition Connection.h:229
const String & rx(uint04 size=Constant< uint04 >::Invalid)
virtual bool canReadLine(char deliminator='\n') const
const String & rxLine(TimeSpan wait_time, char delimeter='\n', uint04 size=Constant< uint04 >::Invalid)
ConnectionInfo m_connection_info
Definition Connection.h:255
Buffer< std::function< void(const String &)> > m_tx_callback
Definition Connection.h:256
bool openBackend(ConnectionBackend *io_device, const ConnectionInfo &request)
const String & rx(TimeSpan wait_time, uint04 size=Constant< uint04 >::Invalid)
virtual bool isOpen() const
virtual bool ping() const
virtual ~Connection()
Buffer< String > rx(char deliminator, const String &end_sequence, uint04 size=Constant< uint04 >::Invalid)
ConnectionBackend * ioDevice() const
virtual const String & type() const
Definition Connection.h:219
Time lastRxTime() const
Definition Connection.h:226
Time lastTxTime() const
Definition Connection.h:227
static bool Ping(const String &address, String &response, ProgressInfo *log=nullptr)
static bool Ping(const String &address, ProgressInfo *log=nullptr)
static bool LogNetworkInfo(ProgressInfo *log)
static bool LogIPInfo(ProgressInfo *log)
void writeConnectionLogMessage(const TranslatedString &message)
A root class which describes some physical object, such as a Motor, Sensor, or Connection....
Definition Device.h:52
A core class that represents a node on model heirarchy. This node may contain a Geometry or one or mo...
Definition Model.h:58
A light-weight base class for Log that allows processes to update, without the need for additional in...
Definition ProgressInfo.hpp:48
Software Services provide an interface for adding to or changing the software behavior via functional...
Definition SoftwareService.h:9
Software Service Managers take a Software service to modify the behavior of the software.
Definition SoftwareService.h:15
The core String class for the NDEVR API.
Definition String.h:69
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
Stores a time span, or difference between two times, with an optional start time.
Definition TimeSpan.h:46
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
Definition TranslatedString.h:13
Definition ACIColor.h:37
RawConnectionLoggingMode
Definition Connection.h:89
ConnectionState
Describes the state of the connection.
Definition Connection.h:57
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
@ e_initializing
Definition OrientationSensor.h:47
ConnectionOpenType
Describes how to open a connection.
Definition Connection.h:76
A category represents the type of connection. For example Bluetooth, Serial Port, TCP,...
Definition Connection.h:127
String icon
Definition Connection.h:130
TranslatedString name
Definition Connection.h:128
String id
Definition Connection.h:129
A structure designed to store information about a specific Connection.
Definition Connection.h:97
void setID(const String &id)
String type
Definition Connection.h:99
bool operator!=(const ConnectionInfo &other) const
Definition Connection.h:115
String thread_id
Definition Connection.h:104
ProgressInfo * log
Definition Connection.h:103
void autoSetName()
Definition Connection.h:110
TranslatedString name
Definition Connection.h:98
ConnectionOpenType connect_mode
Definition Connection.h:101
String address
Definition Connection.h:100
String id() const
Definition Connection.h:106
TimeSpan max_timeout
Definition Connection.h:102
Describes settings for performing a Connection search.
Definition Connection.h:136
std::function< SearchReturn(const ConnectionInfo &)> on_service_found
Definition Connection.h:144
std::function< SearchReturn(const ConnectionInfo &)> on_device_found
Definition Connection.h:143
ProgressInfo * log
Definition Connection.h:145
SearchReturn
Definition Connection.h:138
@ e_continue_searching
Definition Connection.h:139
@ e_stop_searching
Definition Connection.h:140
Buffer< ConnectionCategory > allowed_categories
Definition Connection.h:142
TimeSpan timeout
Definition Connection.h:146
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233