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/Device.h>
35#include <NDEVR/String.h>
36#include <NDEVR/TimeSpan.h>
37#include <QObject>
38class QAbstractSocket;
39class QTcpSocket;
40class QUdpSocket;
41class QSerialPortInfo;
42class QSerialPort;
43class QBluetoothSocket;
44class QBluetoothServiceInfo;
45class QBluetoothDeviceInfo;
46class QBluetoothAddress;
47class QIODevice;
48namespace NDEVR
49{
50 class Log;
51 class SocketIODriver;
69 {
70 e_not_open = 0x0000,
71 e_read_only = 0x0001,
72 e_write_only = 0x0002,
74 e_append = 0x0004,
75 e_truncate = 0x0008,
76 e_text = 0x0010,
77 e_unbuffered = 0x0020,
78 e_new_only = 0x0040,
79 e_existing_only = 0x0080
80 };
87 {
93 ProgressInfo* log = nullptr;
95
96 String id() const
97 {
98 return type + "|" + address;
99 }
101 {
102 name = TranslatedString(id());
103 }
104 HARDWARE_API void setID(const String& id);
105 bool operator!=(const ConnectionInfo& other) const
106 {
107 return type != other.type
108 || address != other.address
109 || max_timeout != other.max_timeout
110 || connect_mode != other.connect_mode;
111 }
112 };
132
133 class HARDWARE_API ConnectionBackend : public QObject
134 {
135 Q_OBJECT
136 public:
138 virtual uint04 peek(char* s, uint04 size = Constant<uint04>::Max) const = 0;
139 virtual uint04 rx(char* s, uint04 max_size = Constant<uint04>::Max) = 0;
140 virtual void tx(const String& command) = 0;
141 virtual bool open(const ConnectionInfo& request) = 0;
142 virtual void close() = 0;
143 virtual bool isOpen() const = 0;
144 virtual bool waitForReadyRead(const TimeSpan& span) = 0;
145 virtual TranslatedString lastError() const = 0;
146 virtual uint04 bytesAvailable() const = 0;
147 virtual String threadID() const { return String(); };
148 signals:
151 protected:
153 };
155 {
156 public:
157 virtual bool handlesType(const String& request) const = 0;
158 virtual bool canConnectTo(const ConnectionInfo& request) const;
162 public:
163 static ConnectionBackend* CreateConnection(const ConnectionInfo& request);
164 static void RegisterConnectionFactory(ConnectionFactory* factory);
165 static Buffer<ConnectionInfo> AvailableConnections(const ConnectionSearchParamaters& params);
166 static Buffer<ConnectionCategory> AvailableConnectionCatagories();
167 protected:
169 };
171 {
172 Q_OBJECT
173 public:
174 Connection(const Model& model, ProgressInfo* raw_network_log = nullptr, ProgressInfo* command_log = nullptr, QObject* parent = nullptr);
175 Connection(const ConnectionInfo& connection, const Model& model, ProgressInfo* raw_network_log = nullptr, ProgressInfo* command_log = nullptr, QObject* parent = nullptr);
176 virtual ~Connection();
177 //if size == nan, use m_default_rx_buffer_size
178 const String& peek(uint04 size = Constant<uint04>::NaN) const;
179 [[nodiscard]] Buffer<String> rx(char deliminator, uint04 size = Constant<uint04>::NaN);
180 [[nodiscard]] Buffer<String> rx(char deliminator, const String& end_sequence, uint04 size = Constant<uint04>::NaN);
181 const String& rx(uint04 size = Constant<uint04>::NaN);
183 [[nodiscard]] const String& rxLine(TimeSpan wait_time, char delimeter = '\n', uint04 size = Constant<uint04>::NaN);
184 virtual void tx(const String& data);
185 virtual bool open();
186 virtual bool open(const ConnectionInfo& request);
187 virtual void close();
188 virtual void setConnectionInfo(const ConnectionInfo& request);
189 virtual void clearConnectionInfo();
190 [[nodiscard]] virtual bool isOpen() const;
191 [[nodiscard]] virtual const String& address() const { return m_connection_info.address; }
192 [[nodiscard]] virtual const String& type() const { return m_connection_info.type; }
193 void setRawDataLog(ProgressInfo* raw_network_log);
194 void handleError();
195 [[nodiscard]] virtual String threadID() const;
196 [[nodiscard]] virtual bool canReadLine(char deliminator = '\n') const;
197 virtual void setTXDelay(const TimeSpan& tx_delay) { m_tx_delay = tx_delay; }
198 [[nodiscard]] ProgressInfo* rawNetworkLog() { return m_raw_data_log; }
199 [[nodiscard]] Time lastRxTime() const { return m_last_rx; }
200 [[nodiscard]] Time lastTxTime() const { return m_last_tx; }
201 [[nodiscard]] const ProgressInfo* rawNetworkLog() const { return m_raw_data_log; }
202 [[nodiscard]] const ConnectionInfo& connectionInfo() const { return m_connection_info; }
203 [[nodiscard]] ConnectionState connectionState() const;
204 [[nodiscard]] ConnectionBackend* ioDevice() const;
205 virtual bool waitForReadyRead(const TimeSpan& span);
206 virtual void setDefaultRxBufferSize(uint04 default_rx_buffer_size);
207 void setRawLoggingMode(RawConnectionLoggingMode mode) { m_raw_logging_mode = mode; }
208 [[nodiscard]] virtual bool ping() const;
209 public:
210 [[nodiscard]] static bool Ping(const String& address, ProgressInfo* log = nullptr);
211 [[nodiscard]] static bool Ping(const String& address, String& response, ProgressInfo* log = nullptr);
212 static bool LogNetworkInfo(ProgressInfo* log);
213 static bool LogIPInfo(ProgressInfo* log);
214 protected:
215 virtual void setBackend(ConnectionBackend* device);
216 void initConnection();
217 void handleErrorString(const TranslatedString& error);
218 bool openBackend(ConnectionBackend* io_device, const ConnectionInfo& request);
219 public slots:
220 virtual void setConnectionState(ConnectionState state);
221 void writeConnectionLogMessage(const TranslatedString& message);
222 signals:
227 protected:
229 Buffer<std::function<void(const String&)>> m_tx_callback;
230 Buffer<std::function<void(const String&)>> m_rx_callback;
232 Time m_last_rx = Time(0);
233 Time m_last_tx = Time(0);
234 TimeSpan m_tx_delay = TimeSpan(0);
235 ConnectionBackend* m_io_device = nullptr;
236 ProgressInfo* m_raw_data_log = nullptr;
237 uint04 m_total_tx = 0;
238 uint04 m_total_rx = 0;
239 uint04 m_default_rx_buffer_size = 2046;
240 ConnectionState m_connection_state = ConnectionState::e_no_connection;
241 RawConnectionLoggingMode m_raw_logging_mode = RawConnectionLoggingMode::e_ascii_logging;
242 bool m_log_tx = true;
243 bool m_log_rx = true;
244 bool m_show_errors = false;
245 };
246}
#define HARDWARE_API
Definition DLLInfo.h:74
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
Definition Connection.h:134
void connectionStateChangedSignal(ConnectionState state)
virtual bool waitForReadyRead(const TimeSpan &span)=0
virtual String threadID() const
Definition Connection.h:147
virtual bool open(const ConnectionInfo &request)=0
virtual uint04 bytesAvailable() const =0
virtual void tx(const String &command)=0
ProgressInfo * m_log
Definition Connection.h:152
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
Definition Connection.h:155
static Buffer< ConnectionFactory * > s_registered_factories
Definition Connection.h:168
virtual bool handlesType(const String &request) const =0
virtual ConnectionBackend * createConnection(const ConnectionInfo &request)=0
virtual Buffer< ConnectionInfo > availableConnections(const ConnectionSearchParamaters &params)=0
virtual Buffer< ConnectionCategory > availableConnectionCatagories()=0
Definition Connection.h:171
void connectionStateChangedSignal(ConnectionState state)
virtual const String & address() const
Definition Connection.h:191
String m_connection_rx_buffer
Definition Connection.h:231
void dataAvailableSignal()
ProgressInfo * rawNetworkLog()
Definition Connection.h:198
const ProgressInfo * rawNetworkLog() const
Definition Connection.h:201
Buffer< std::function< void(const String &)> > m_rx_callback
Definition Connection.h:230
virtual void setTXDelay(const TimeSpan &tx_delay)
Definition Connection.h:197
void setRawLoggingMode(RawConnectionLoggingMode mode)
Definition Connection.h:207
const ConnectionInfo & connectionInfo() const
Definition Connection.h:202
ConnectionInfo m_connection_info
Definition Connection.h:228
Buffer< std::function< void(const String &)> > m_tx_callback
Definition Connection.h:229
virtual const String & type() const
Definition Connection.h:192
Time lastRxTime() const
Definition Connection.h:199
Time lastTxTime() const
Definition Connection.h:200
Definition Device.h:48
Definition Model.h:54
Definition ProgressInfo.hpp:43
Definition String.h:40
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
Definition TimeSpan.h:40
Definition TranslatedString.h:9
Definition ACIColor.h:37
RawConnectionLoggingMode
Definition Connection.h:82
ConnectionState
Definition Connection.h:53
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
uint04 wait_time
Definition IPWidget.cpp:141
@ e_initializing
Definition OrientationSensor.h:43
ConnectionOpenType
Definition Connection.h:69
Definition Connection.h:114
String icon
Definition Connection.h:117
TranslatedString name
Definition Connection.h:115
String id
Definition Connection.h:116
Definition Connection.h:87
HARDWARE_API void setID(const String &id)
Definition Connection.cpp:17
String type
Definition Connection.h:89
bool operator!=(const ConnectionInfo &other) const
Definition Connection.h:105
String thread_id
Definition Connection.h:94
ProgressInfo * log
Definition Connection.h:93
void autoSetName()
Definition Connection.h:100
TranslatedString name
Definition Connection.h:88
ConnectionOpenType connect_mode
Definition Connection.h:91
String address
Definition Connection.h:90
String id() const
Definition Connection.h:96
TimeSpan max_timeout
Definition Connection.h:92
Definition Connection.h:120
std::function< SearchReturn(const ConnectionInfo &)> on_service_found
Definition Connection.h:128
std::function< SearchReturn(const ConnectionInfo &)> on_device_found
Definition Connection.h:127
ProgressInfo * log
Definition Connection.h:129
SearchReturn
Definition Connection.h:122
@ e_continue_searching
Definition Connection.h:123
@ e_stop_searching
Definition Connection.h:124
Buffer< ConnectionCategory > allowed_categories
Definition Connection.h:126
TimeSpan timeout
Definition Connection.h:130
Definition BaseValues.hpp:272