34#include <NDEVR/SoftwareService.h>
35#include <NDEVR/Device.h>
36#include <NDEVR/String.h>
37#include <NDEVR/TimeSpan.h>
91 ConnectionInfo() =
default;
133 HARDWARE_API
bool operator!=(
const ConnectionInfo& other)
const;
418 [[nodiscard]]
virtual bool isOpen()
const;
448 [[nodiscard]]
virtual bool canReadLine(
char deliminator =
'\n')
const;
509 [[nodiscard]]
virtual bool ping()
const;
The equivelent of std::vector but with a bit more control.
Provides the unseen backend logic for setting up, tearing down, and sending data over a specific type...
virtual void tx(const StringView &command)=0
Transmits a string command over the connection.
virtual bool waitForReadyRead(const TimeSpan &span)=0
Blocks until data is available to read or the timeout expires.
ConnectionBackend(LogPtr log)
Constructs a ConnectionBackend with the given log.
void connectionStateChangedSignal(ConnectionState state)
Emitted when the connection state changes.
virtual void close()=0
Closes the connection.
void dataAvailableSignal()
Emitted when data is available to be read from the connection.
virtual bool open(const ConnectionInfo &request)=0
Opens the connection using the provided connection info.
virtual bool isOpen() const =0
Checks whether the connection is currently open.
virtual TranslatedString lastError() const =0
Returns the last error message from this backend.
virtual uint04 bytesAvailable() const =0
Returns the number of bytes available to read.
LogPtr m_log
Log for diagnostic messages.
virtual uint04 rx(char *s, uint04 max_size=Constant< uint04 >::Max)=0
Reads and consumes data from the receive buffer.
virtual uint04 peek(char *s, uint04 size=Constant< uint04 >::Max) const =0
Reads data from the receive buffer without consuming it.
virtual String threadID() const
Returns the thread ID this backend must execute on, if any.
Stores factories that can be registered for creating Connections of various types by instantiating a ...
static Buffer< ConnectionCategory > AvailableConnectionCategories()
Returns all connection categories from all registered factories.
virtual bool handlesType(const StringView &request) const =0
Checks whether this factory handles the given connection type.
static void RegisterConnectionFactory(ConnectionFactory *factory)
Registers a ConnectionFactory so it can be used to create connections.
static Buffer< ConnectionInfo > AvailableConnections(const ConnectionSearchParameters ¶ms)
Discovers available connections across all registered factories.
virtual bool canConnectTo(const ConnectionInfo &request) const
Checks whether this factory can establish a connection with the given info.
virtual Buffer< ConnectionCategory > availableConnectionCategories()=0
Returns the connection categories this factory supports.
virtual Buffer< ConnectionInfo > availableConnections(const ConnectionSearchParameters ¶ms)=0
Discovers available connections matching the given search parameters.
virtual ConnectionBackend * createConnection(const ConnectionInfo &request)=0
Creates a new ConnectionBackend for the given connection info.
static ConnectionBackend * CreateConnection(const ConnectionInfo &request)
Creates a ConnectionBackend by searching all registered factories for one that handles the request.
static Buffer< ConnectionFactory * > s_registered_factories
All registered connection factories.
virtual void setConnectionState(ConnectionState state)
Sets the connection state and emits the state changed signal.
virtual void close()
Closes the connection.
Buffer< std::function< void(const StringView &)> > m_tx_callback
Callbacks invoked on each transmit operation.
ConnectionState connectionState() const
Returns the current connection state.
Time lastRxTime() const
Returns the timestamp of the last received data.
static bool Ping(const StringView &address, TimeSpan timeout, String &response, LogPtr log=nullptr)
Pings an address with a custom timeout and captures the response.
ConnectionState m_connection_state
Current state of the connection.
const ConnectionInfo & connectionInfo() const
Returns the current connection info.
virtual bool waitForReadyRead(const TimeSpan &span)
Blocks until data is available to read or the timeout expires.
virtual bool canReadLine(char deliminator='\n') const
Checks whether a complete line is available in the receive buffer.
virtual const String & address() const
Returns the address of the current connection.
virtual bool open(const ConnectionInfo &request)
Opens the connection using the provided connection info.
void setRawDataLog(LogPtr raw_network_log)
Sets the log used for raw network data.
bool m_show_errors
Whether to display errors to the user.
Time m_last_tx
Timestamp of the last transmit operation.
uint04 m_total_tx
Total number of bytes transmitted.
ConnectionBackend * m_io_device
The underlying backend IO device.
Connection(const Model &model, LogPtr raw_network_log=nullptr, LogPtr command_log=nullptr, QObject *parent=nullptr)
Constructs a Connection with the given model and optional logs.
bool m_log_rx
Whether to log received data.
virtual void tx(const uint01 *data, uint04 size)
Transmits raw binary data over the connection.
virtual void clearConnectionInfo()
Clears the stored connection info.
virtual void setTXDelay(const TimeSpan &tx_delay)
Sets the delay to insert between successive transmit operations.
Buffer< std::function< void(const StringView &)> > m_rx_callback
Callbacks invoked on each receive operation.
void dataAvailableSignal()
Emitted when data is available to be read.
static bool LogIPInfo(LogPtr log)
Logs IP address information to the given log.
String m_connection_rx_buffer
Internal buffer for received data.
void initConnection()
Initializes internal connection state and signal/slot wiring.
Connection(const ConnectionInfo &connection, const Model &model, LogPtr raw_network_log=nullptr, LogPtr command_log=nullptr, QObject *parent=nullptr)
Constructs a Connection with pre-configured connection info.
bool m_log_tx
Whether to log transmitted data.
ConnectionBackend * ioDevice() const
Returns the underlying backend IO device.
virtual String threadID() const
Returns the thread ID this connection must execute on, if any.
const LogPtr rawNetworkLog() const
Returns the raw data log (const version).
uint04 m_total_rx
Total number of bytes received.
static bool Ping(const StringView &address, TimeSpan timeout, LogPtr log=nullptr)
Pings an address with a custom timeout.
virtual void setConnectionInfo(const ConnectionInfo &request)
Sets the connection info for this connection.
LogPtr m_raw_data_log
Log for raw network data.
void connectSignal()
Emitted when the connection is opened.
Time m_last_rx
Timestamp of the last receive operation.
void disconnectSignal()
Emitted when the connection is closed.
RawConnectionLoggingMode m_raw_logging_mode
Current raw data logging mode.
ConnectionInfo m_connection_info
Stored connection parameters.
const String & rx(TimeSpan wait_time, uint04 size=Constant< uint04 >::Invalid)
Receives data, waiting up to the specified time for data to arrive.
virtual bool ping() const
Sends a ping to the remote endpoint to check connectivity.
virtual void setBackend(ConnectionBackend *device)
Sets the backend IO device for this connection.
virtual const String & type() const
Returns the type of the current connection.
virtual bool isOpen() const
Checks whether the connection is currently open.
Buffer< String > rx(char deliminator, uint04 size=Constant< uint04 >::Invalid)
Receives data split by a delimiter character.
Buffer< String > rx(char deliminator, const StringView &end_sequence, uint04 size=Constant< uint04 >::Invalid)
Receives data split by a delimiter, stopping at an end sequence.
const String & rx(uint04 size=Constant< uint04 >::Invalid)
Receives raw data up to the specified size.
void handleError()
Handles the last error from the backend by logging and updating state.
const String & rxLine(TimeSpan wait_time, char delimeter='\n', uint04 size=Constant< uint04 >::Invalid)
Receives a single line of data, delimited by the given character.
void connectionStateChangedSignal(ConnectionState state)
Emitted when the connection state changes.
virtual bool open()
Opens the connection using the currently stored connection info.
virtual void setDefaultRxBufferSize(uint04 default_rx_buffer_size)
Sets the default receive buffer size used when no explicit size is given.
const String & peek(uint04 size=Constant< uint04 >::Invalid) const
Reads data from the receive buffer without consuming it.
void setRawLoggingMode(RawConnectionLoggingMode mode)
Sets the logging mode for raw connection data.
static bool LogNetworkInfo(LogPtr log)
Logs network interface information to the given log.
TimeSpan m_tx_delay
Delay inserted between successive transmissions.
uint04 m_default_rx_buffer_size
Default receive buffer size in bytes.
void writeConnectionLogMessage(const TranslatedString &message)
Writes a message to the connection log.
LogPtr rawNetworkLog()
Returns the raw data log.
bool openBackend(ConnectionBackend *io_device, const ConnectionInfo &request)
Opens a backend connection with the given parameters.
static bool Ping(const StringView &address, LogPtr log=nullptr)
Pings an address with default timeout to check reachability.
Time lastTxTime() const
Returns the timestamp of the last transmitted data.
virtual void tx(const StringView &data)
Transmits a string over the connection.
void handleErrorString(const TranslatedString &error)
Handles an error string by logging it and updating the connection state.
Device(const Model &model, LogPtr log, QObject *parent=nullptr)
Constructs a Device from a Model with the given log and optional Qt parent.
A light-weight wrapper that will be a no-op if there is not a valid log reference,...
A core class that represents a node on model hierarchy.
Vector< 3, fltp08 > size() const
Returns the size (extents) of the model's bounding box.
Software Service Managers take a Software service to modify the behavior of the software.
Base interface for services that extend or modify software behavior through modules.
The core String View class for the NDEVR API.
The core String class for the NDEVR API.
Stores a time span, or difference between two times, with an optional start time.
Represents a timestamp with utilities for manipulation and conversion.
Any text displayed to the user should be defined as a TranslatedString which allows the program to lo...
static TranslatedString DirectString(const StringView &sub_string)
If a string does not have a translation (EG: reading from an already translated string) this operatio...
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
RawConnectionLoggingMode
Connection can write to the log either as raw ascii bytes, or by converting those bytes into a binary...
@ e_binary_logging
Raw data is converted to hex for logging.
@ e_no_logging
Logging is disabled.
@ e_ascii_logging
Raw data is logged as ASCII text.
@ e_append
Appends the specified value(s) to a multiString registry key.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
ConnectionState
Describes the state of the connection.
@ e_attempting_connection
Currently attempting to establish a connection.
@ e_network_error
A general network error occurred.
@ e_no_response
No response was received within the expected time.
@ e_invalid_connection
The connection is in an invalid state.
@ e_connection_timeout
The connection attempt timed out.
@ e_invalid_response
A response was received but was not valid.
@ e_access_to_port_denied
Access to the requested port was denied.
@ e_resetting
The connection is being reset.
@ e_valid_connection
A valid, active connection is established.
@ e_awaiting_response
Waiting for a response from the remote endpoint.
@ e_no_services
No services are available on the remote endpoint.
@ e_no_connection
No connection has been established.
@ e_initializing
The gyroscope is currently initializing.
ConnectionOpenType
Describes how to open a connection.
@ e_write_only
Open for writing only.
@ e_text
Open in text mode.
@ e_new_only
Only open if the connection is new.
@ e_truncate
Truncate existing data on open.
@ e_existing_only
Only open if the connection already exists.
@ e_unbuffered
Disable buffering.
@ e_read_only
Open for reading only.
@ e_read_write
Open for both reading and writing.
@ e_not_open
Connection is not open.
A category represents the type of connection.
String icon
Icon resource name associated with this category.
TranslatedString name
Human-readable display name of the connection category.
String id
Unique identifier for this connection category.
A structure designed to store information about a specific Connection.
ConnectionInfo(const StringView &id)
Constructs a ConnectionInfo by parsing a combined type|address identifier.
LogPtr log
Log used for connection diagnostics.
TranslatedString name
Human-readable display name for the connection.
String thread_id
If set, connection must be executed on a specific thread.
bool operator!=(const ConnectionInfo &other) const
Inequality comparison operator.
void setID(const StringView &id)
Parses a combined "type|address" string and sets the type and address fields.
TimeSpan max_timeout
Maximum time to wait for a connection before timing out.
void autoSetName()
Automatically sets the display name from the connection identifier.
String id() const
Returns a unique identifier string combining the type and address.
ConnectionOpenType connect_mode
The mode in which to open the connection.
String type
The connection type (e.g., "serial", "tcp", "bluetooth").
String address
The address or port of the connection.
Describes settings for performing a Connection search.
LogPtr log
Log for search diagnostics.
TimeSpan timeout
Maximum duration to search before stopping.
SearchReturn
The return value from a search callback, indicating whether to continue or stop.
@ e_stop_searching
Stop the search immediately.
@ e_continue_searching
Continue scanning for more connections.
Buffer< ConnectionCategory > allowed_categories
Categories to include in the search. Empty means all categories.
std::function< SearchReturn(const ConnectionInfo &)> on_service_found
Callback invoked when a service is discovered.
std::function< SearchReturn(const ConnectionInfo &)> on_device_found
Callback invoked when a device is discovered.