init
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "defines.h"
|
||||
#include <cstdlib> // For Unix Compatibility
|
||||
|
||||
namespace Socket {
|
||||
class Socket_API IConnection
|
||||
{
|
||||
protected:
|
||||
|
||||
virtual bool open() = 0;
|
||||
|
||||
public:
|
||||
|
||||
virtual bool close() = 0;
|
||||
|
||||
virtual bool isReadyToSend(const size_t timeOut = 0) const = 0;
|
||||
virtual bool isReadyToReceive(const size_t timeOut = 0) const = 0;
|
||||
|
||||
virtual size_t sendBuffer(const void* buffer, const size_t size) = 0;
|
||||
virtual size_t receiveBuffer(void* buffer, const size_t size) = 0;
|
||||
|
||||
virtual bool sendBufferBlocking(const void* buffer, const size_t size) = 0;
|
||||
virtual bool receiveBufferBlocking(void* buffer, const size_t size) = 0;
|
||||
|
||||
virtual bool isConnected() const = 0;
|
||||
|
||||
virtual void release() = 0;
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~IConnection() = default;
|
||||
};
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
|
||||
namespace Socket {
|
||||
class Socket_API IConnectionBluetooth : public IConnection
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* \brief Connect to the bluetooth device.
|
||||
* \param[in] address the MAC address of the Bluetooth device.
|
||||
* \return If the function succeeds, the return value is true, else false.
|
||||
*/
|
||||
virtual bool connect(const uint64_t address) = 0;
|
||||
|
||||
/**
|
||||
* \brief Return the input serial pending byte count.
|
||||
* \return The number of pending byte.
|
||||
*/
|
||||
virtual size_t getPendingByteCount() = 0;
|
||||
|
||||
/**
|
||||
* \brief Flush the input serial buffer.
|
||||
* \return If the function succeeds, the return value is true, else false.
|
||||
*/
|
||||
virtual const char* getLastError() const = 0;
|
||||
|
||||
/**
|
||||
* \brief Clear the last error registered.
|
||||
*/
|
||||
virtual void clearError() = 0;
|
||||
|
||||
/**
|
||||
* \brief List the paired bluetooth devices: Name and Bluetooth MAC address.
|
||||
*
|
||||
* \param[out] nPairedDevices the Bluetooth devices count.
|
||||
* \param[out] names an array of Bluetooth names.
|
||||
* \param[out] addresses an array of Bluetooth addresses.
|
||||
*
|
||||
* \return If the function succeeds, the return value is true, else false.
|
||||
*/
|
||||
virtual bool listPairedBluetoothDevices(size_t* nPairedDevices, char** names, uint64_t** addresses) = 0;
|
||||
|
||||
/**
|
||||
* \brief Convert string MAC Bluetooth address to hexadecimal.
|
||||
* \param[out] straddr string MAC Bluetooth address.
|
||||
* \param[out] btaddr hexadecimal MAC Bluetooth address.
|
||||
* \retval true in case of success.
|
||||
* \retval false in case of failure: if the address does not match: %02x:%02x:%02x:%02x:%02x:%02x
|
||||
*/
|
||||
static bool string2BluetoothAddress(const char* straddr, uint64_t* btaddr)
|
||||
{
|
||||
std::array<uint32_t, 6> aaddr = { 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
const int value = sscanf(straddr, "%02x:%02x:%02x:%02x:%02x:%02x", &aaddr[0], &aaddr[1], &aaddr[2], &aaddr[3], &aaddr[4], &aaddr[5]);
|
||||
|
||||
if (value != 6) { return false; }
|
||||
|
||||
*btaddr = 0;
|
||||
|
||||
for (size_t i = 0; i < 6; ++i)
|
||||
{
|
||||
const uint64_t tmpaddr = static_cast<uint64_t>(aaddr[i] & 0xff);
|
||||
*btaddr = (*btaddr << 8) + tmpaddr;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
#if defined TARGET_OS_Windows
|
||||
|
||||
static const unsigned char WIN_SOCKET_MAJOR_VERSION = 2; // Winsock major version to use
|
||||
static const unsigned char WIN_SOCKET_MINOR_VERSION = 2; // Winsock minor version to use
|
||||
|
||||
static bool m_isWinsockInitialized;
|
||||
|
||||
#elif defined TARGET_OS_Linux || defined TARGET_OS_MacOS
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionBluetooth* createConnectionBluetooth();
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
|
||||
namespace Socket {
|
||||
class Socket_API IConnectionClient : public IConnection
|
||||
{
|
||||
public:
|
||||
|
||||
virtual bool connect(const char* serverName, const size_t serverPort, const size_t timeOut = 0xffffffff) = 0;
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionClient* createConnectionClient();
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
#include <string>
|
||||
|
||||
#if defined TARGET_OS_Windows
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
namespace Socket {
|
||||
/**
|
||||
* \brief The IConnectionParallel class provides the possibility to communicate with a parallel port.
|
||||
* On Windows, you must have TVicPort library installed (available for free: http://entechtaiwan.com/dev/port/index.shtm).
|
||||
*/
|
||||
class Socket_API IConnectionParallel : public IConnection
|
||||
{
|
||||
public:
|
||||
|
||||
virtual bool connect(const unsigned short port) = 0;
|
||||
|
||||
virtual std::string getLastError() = 0;
|
||||
|
||||
protected:
|
||||
|
||||
#if defined TARGET_OS_Windows
|
||||
typedef bool (CALLBACK * port_open_t)();
|
||||
typedef void (CALLBACK * port_close_t)();
|
||||
typedef bool (CALLBACK * port_is_driver_opened_t)();
|
||||
typedef bool (CALLBACK * port_write_t)(unsigned short, unsigned char);
|
||||
|
||||
HMODULE m_port = nullptr;
|
||||
|
||||
port_open_t m_portOpen = nullptr;
|
||||
port_close_t m_portClose = nullptr;
|
||||
port_is_driver_opened_t m_isDriverOpened = nullptr;
|
||||
port_write_t m_portWrite = nullptr;
|
||||
#endif
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionParallel* createConnectionParallel();
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
#include <string>
|
||||
|
||||
namespace Socket {
|
||||
class Socket_API IConnectionSerial : public IConnection
|
||||
{
|
||||
public:
|
||||
|
||||
virtual bool connect(const char* url, const size_t baudRate) = 0;
|
||||
|
||||
/**
|
||||
* \brief Return the input serial pending byte count.
|
||||
* \return The number of pending byte.
|
||||
*/
|
||||
virtual size_t getPendingByteCount() = 0;
|
||||
|
||||
/**
|
||||
* \brief Flush the input serial buffer.
|
||||
* \return If the function succeeds, the return value is true, else false.
|
||||
*/
|
||||
virtual bool flush() = 0;
|
||||
|
||||
/**
|
||||
* \brief Flush the input serial buffer.
|
||||
* \return If the function succeeds, the return value is true, else false.
|
||||
*/
|
||||
virtual const char* getLastError() = 0;
|
||||
|
||||
/**
|
||||
* \brief Checks if an error raised.
|
||||
* \return True if the m_LastError is not empty, else false.
|
||||
*/
|
||||
virtual bool isErrorRaised() = 0;
|
||||
|
||||
/**
|
||||
* \brief Clear the last error registered.
|
||||
*/
|
||||
virtual void clearError() = 0;
|
||||
|
||||
/**
|
||||
* \brief Set tiemouts for read and write function.
|
||||
* \param timeout [in] Timeout in deciseconds.
|
||||
* \return True if succeed, else false.
|
||||
*/
|
||||
virtual bool setTimeouts(const size_t timeout) = 0;
|
||||
|
||||
protected:
|
||||
void saveLastError();
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionSerial* createConnectionSerial();
|
||||
} // namespace Socket
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "TConnectionDelegate.h"
|
||||
#include "IConnectionSerial.h"
|
||||
|
||||
namespace Socket {
|
||||
struct Socket_API SConnectionSerialDelegate
|
||||
{
|
||||
SConnectionDelegate connectionDelegate;
|
||||
bool (*fpConnect)(void*, const char*, const size_t);
|
||||
size_t (*fpGetPendingByteCount)(void*);
|
||||
bool (*fpFlush)(void*);
|
||||
const char* (*fpGetLastError)(void*);
|
||||
void (*fpSaveLastError)(void*);
|
||||
// TODO for Android compatibility
|
||||
//bool(*fpIsErrorRaised)(void*);
|
||||
//void(*fpClearError)(void*);
|
||||
//bool(*fSetTimeouts)(void*, const size_t decisecondsTimeout);
|
||||
};
|
||||
|
||||
class Socket_API IConnectionSerialDelegate : public TConnectionDelegate<IConnectionSerial>
|
||||
{
|
||||
public:
|
||||
IConnectionSerialDelegate(const SConnectionSerialDelegate connectionSerialDelegate)
|
||||
: TConnectionDelegate<IConnectionSerial>(connectionSerialDelegate.connectionDelegate) { }
|
||||
|
||||
~IConnectionSerialDelegate() override { }
|
||||
|
||||
protected:
|
||||
SConnectionSerialDelegate m_connectionSerialDelegate;
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionSerialDelegate* createConnectionSerialDelegate(SConnectionSerialDelegate connectionSerialDelegate);
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
|
||||
namespace Socket {
|
||||
class Socket_API IConnectionServer : public IConnection
|
||||
{
|
||||
public:
|
||||
|
||||
/*
|
||||
* \brief Places a socket in a listening state on the specified port.
|
||||
* \param port [in]: port on the one the socket should listen.
|
||||
* when set to '0', the socket wil start on
|
||||
* an available port.
|
||||
*/
|
||||
virtual bool listen(const size_t port) = 0;
|
||||
|
||||
virtual IConnection* accept() = 0;
|
||||
|
||||
/*
|
||||
* \brief Returns the port on the one the server is listening.
|
||||
* This is useful if you set the port to '0'.
|
||||
* \param port [out]: port on the one the server is listening
|
||||
*/
|
||||
virtual bool getSocketPort(size_t& port) = 0;
|
||||
};
|
||||
|
||||
extern Socket_API IConnectionServer* createConnectionServer();
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include "IConnection.h"
|
||||
|
||||
namespace Socket {
|
||||
struct SConnectionDelegate
|
||||
{
|
||||
void* data;
|
||||
bool (*fpOpen)(void*);
|
||||
bool (*fpClose)(void*);
|
||||
bool (*fpIsReadyToSend)(void*, size_t);
|
||||
bool (*fpIsReadyToReceive)(void*, size_t);
|
||||
size_t (*fpSendBuffer)(void*, const void*, size_t);
|
||||
size_t (*fpReceiveBuffer)(void*, void*, size_t);
|
||||
bool (*fpSendBufferBlocking)(void*, const void*, size_t);
|
||||
bool (*fpReceiveBufferBlocking)(void*, void*, size_t);
|
||||
bool (*fpIsConnected)(void*);
|
||||
bool (*fpRelease)(void*);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Socket_API TConnectionDelegate : public T
|
||||
{
|
||||
public:
|
||||
TConnectionDelegate(const SConnectionDelegate connectionDelegate) : m_connectionDelegate(connectionDelegate) { }
|
||||
|
||||
virtual bool close() { return m_connectionDelegate.fpClose(m_connectionDelegate.data); }
|
||||
|
||||
virtual bool isReadyToSend(const size_t timeOut) const { return m_connectionDelegate.fpIsReadyToSend(m_connectionDelegate.data, timeOut); }
|
||||
virtual bool isReadyToReceive(const size_t timeOut) const { return m_connectionDelegate.fpIsReadyToReceive(m_connectionDelegate.data, timeOut); }
|
||||
|
||||
virtual size_t sendBuffer(const void* buffer, const size_t size) { return m_connectionDelegate.fpSendBuffer(m_connectionDelegate.data, buffer, size); }
|
||||
virtual size_t receiveBuffer(void* buffer, const size_t size) { return m_connectionDelegate.fpReceiveBuffer(m_connectionDelegate.data, buffer, size); }
|
||||
|
||||
virtual bool sendBufferBlocking(const void* buffer, const size_t size)
|
||||
{
|
||||
return m_connectionDelegate.fpSendBufferBlocking(m_connectionDelegate.data, buffer, size);
|
||||
}
|
||||
virtual bool receiveBufferBlocking(void* buffer, const size_t bufferSize)
|
||||
{
|
||||
return m_connectionDelegate.fpReceiveBufferBlocking(m_connectionDelegate.data, buffer, bufferSize);
|
||||
}
|
||||
|
||||
virtual bool isConnected() const { return m_connectionDelegate.fpIsConnected(m_connectionDelegate.data); }
|
||||
|
||||
virtual void release() { m_connectionDelegate.fpRelease(m_connectionDelegate.data); }
|
||||
|
||||
virtual ~TConnectionDelegate() { }
|
||||
protected:
|
||||
virtual bool open() { return m_connectionDelegate.fpOpen(m_connectionDelegate.data); }
|
||||
|
||||
SConnectionDelegate m_connectionDelegate;
|
||||
};
|
||||
} // namespace Socket
|
||||
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <ov_common_defines.h>
|
||||
|
||||
// Taken from
|
||||
// - http://people.redhat.com/drepper/dsohowto.pdf
|
||||
// - http://www.nedprod.com/programs/gccvisibility.html
|
||||
#if defined Socket_Shared
|
||||
# if defined TARGET_OS_Windows
|
||||
# define Socket_API_Export __declspec(dllexport)
|
||||
# define Socket_API_Import __declspec(dllimport)
|
||||
# elif defined TARGET_OS_Linux || defined TARGET_OS_MacOS
|
||||
# define Socket_API_Export __attribute__((visibility("default")))
|
||||
# define Socket_API_Import __attribute__((visibility("default")))
|
||||
# else
|
||||
# define Socket_API_Export
|
||||
# define Socket_API_Import
|
||||
# endif
|
||||
#else
|
||||
# define Socket_API_Export
|
||||
# define Socket_API_Import
|
||||
#endif
|
||||
|
||||
#if defined Socket_Exports
|
||||
# define Socket_API Socket_API_Export
|
||||
#else
|
||||
# define Socket_API Socket_API_Import
|
||||
#endif
|
||||
Reference in New Issue
Block a user