128 lines
3.3 KiB
C++
128 lines
3.3 KiB
C++
/*
|
|
* TFConnection.h
|
|
*
|
|
* Created on: 10.06.2014
|
|
* Author: matzman
|
|
*/
|
|
|
|
#ifndef TFCONNECTION_H_
|
|
#define TFCONNECTION_H_
|
|
|
|
#include <tfcppsimple/TfTypes.h>
|
|
#include <tfcppsimple/ObservableTemplate.h>
|
|
#include <tfcppsimple/TfDevice.h>
|
|
#include <log4cpp/Category.hh>
|
|
#include <ip_connection.h>
|
|
#include <string>
|
|
#include <mutex>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
|
|
namespace tfcppsimple {
|
|
|
|
|
|
class TfDevice;
|
|
class TfConnection;
|
|
|
|
enum class TfConnectionState : int {
|
|
NOTCONFIGURED = 0,
|
|
PENDING = 1,
|
|
CONNECTED = 2,
|
|
CONNECTION_LOST = 3,
|
|
DISCONNECTED = 4,
|
|
ERROR = 5
|
|
};
|
|
|
|
|
|
class TfConnectionListener {
|
|
public:
|
|
virtual ~TfConnectionListener() {}
|
|
|
|
virtual void stateChangeCallback(TfConnection& conn, TfConnectionState oldstate, TfConnectionState newstate) {}
|
|
virtual void connectCallback(TfConnection& conn, TfCallbackConnectReason reason) {}
|
|
virtual void disconnectCallback(TfConnection& conn, TfCallbackDisconnectReason reason) {}
|
|
virtual void newDeviceCallback(TfConnection& conn, TfDevice& device) {}
|
|
virtual void shutdownCallback(TfConnection& conn) {}
|
|
};
|
|
|
|
|
|
class TfConnection : public ObservableTemplate<TfConnectionListener> {
|
|
public:
|
|
|
|
private:
|
|
static log4cpp::Category& logger;
|
|
|
|
static void __enumerateCallback( const char *uid,
|
|
const char *connected_uid,
|
|
char position,
|
|
uint8_t hardware_version[3],
|
|
uint8_t firmware_version[3],
|
|
uint16_t device_identifier,
|
|
uint8_t enumeration_type,
|
|
void *user_data);
|
|
static void __connectedCallback(uint8_t connect_reason, void *user_data);
|
|
static void __disconnectedCallback(uint8_t disconnect_reason, void *user_data);
|
|
|
|
void enumerateCallback(
|
|
const char *uid,
|
|
const char *connected_uid,
|
|
char position,
|
|
uint8_t hardware_version[3],
|
|
uint8_t firmware_version[3],
|
|
uint16_t device_identifier,
|
|
TfCallbackEnumerationType enumeration_type);
|
|
void connectedCallback(TfCallbackConnectReason connect_reason);
|
|
void disconnectedCallback(TfCallbackDisconnectReason disconnect_reason);
|
|
|
|
bool stateChange(TfConnectionState newstate);
|
|
|
|
|
|
std::mutex objLock;
|
|
IPConnection tfIpConnection;
|
|
std::string host;
|
|
uint16_t port = 0;
|
|
bool okFlag = false;
|
|
TfConnectionState state = TfConnectionState::NOTCONFIGURED;
|
|
std::map<std::string, TfDevice*> devices;
|
|
std::vector<TfDevice*> rootDevices;
|
|
|
|
|
|
public:
|
|
TfConnection();
|
|
virtual ~TfConnection();
|
|
|
|
bool connect(std::string host = "localhost", uint16_t port = 4223,
|
|
bool autoReconnect = true, uint32_t timeout = 2500);
|
|
bool disconnect();
|
|
|
|
IPConnection& getTfIpConnection() { return tfIpConnection; }
|
|
|
|
const std::map<std::string, TfDevice*>& getDeviceMap() { return devices; }
|
|
const std::vector<TfDevice*>& getRootDevices() { return rootDevices; }
|
|
|
|
|
|
bool isOk() { return okFlag; }
|
|
|
|
bool isAutoReconnect() { return ipcon_get_auto_reconnect(&tfIpConnection); }
|
|
void setAutoReconnect(bool flag) { return ipcon_set_auto_reconnect(&tfIpConnection, flag); }
|
|
|
|
uint32_t getResponseTimeout() { return ipcon_get_timeout(&tfIpConnection); }
|
|
void setResponseTimeout(uint32_t timeout) { ipcon_set_timeout(&tfIpConnection, timeout); }
|
|
|
|
TfConnectionState getStatus() { return state; }
|
|
|
|
void startDeviceQuery(uint32_t usec);
|
|
|
|
void wait() { ipcon_wait(&tfIpConnection); }
|
|
void unwait() { ipcon_unwait(&tfIpConnection); }
|
|
|
|
const std::string& getHost() { return host; }
|
|
|
|
uint16_t getPort() { return port; }
|
|
};
|
|
|
|
} /* namespace growcontrol */
|
|
|
|
#endif /* TFCONNECTION_H_ */
|