15 #ifndef VISIONTRANSFER_PARAMETERTRANSFER_H
16 #define VISIONTRANSFER_PARAMETERTRANSFER_H
18 #include "visiontransfer/common.h"
19 #include "visiontransfer/types.h"
20 #include "visiontransfer/parameterinfo.h"
21 #include "visiontransfer/parameterset.h"
22 #include "visiontransfer/internal/tokenizer.h"
23 #include "visiontransfer/internal/networking.h"
29 #include <condition_variable>
32 namespace visiontransfer {
48 class ParameterTransfer {
150 void writeParameter(
const char*
id,
const T& value,
bool synchronous=
true);
192 param::Parameter
getParameter(
const std::string& uid)
const;
198 param::Parameter
pollParameter(
const std::string& uid,
bool blockingCall);
210 void setParameterUpdateCallback(std::function<
void(
const std::string& uid)> callback,
bool threaded);
228 void persistParameters(
const std::vector<std::string>& uids,
bool synchronous=
true);
236 static constexpr
int SOCKET_TIMEOUT_MS = 500;
237 static constexpr
int SOCKET_RECONNECT_INTERVAL_MS = 2000;
240 static constexpr
unsigned char MESSAGE_READ_INT = 0x01;
241 static constexpr
unsigned char MESSAGE_READ_DOUBLE = 0x02;
242 static constexpr
unsigned char MESSAGE_READ_BOOL = 0x03;
243 static constexpr
unsigned char MESSAGE_WRITE_INT = 0x04;
244 static constexpr
unsigned char MESSAGE_WRITE_DOUBLE = 0x05;
245 static constexpr
unsigned char MESSAGE_WRITE_BOOL = 0x06;
246 static constexpr
unsigned char MESSAGE_ENUMERATE_PARAMS = 0x07;
253 static constexpr
unsigned int RECV_BUF_SIZE = 1024*1024;
254 char recvBuf[RECV_BUF_SIZE];
255 unsigned int recvBufBytes;
256 unsigned int pollDelay;
258 std::string networkErrorString;
262 bool featureDisabledTransactions;
265 std::shared_ptr<std::thread> receiverThread;
267 Tokenizer tabTokenizer;
268 Tokenizer spaceTokenizer;
269 Tokenizer slashTokenizer;
271 param::ParameterSet paramSet;
274 mutable std::mutex readyMutex;
276 mutable std::condition_variable readyCond;
278 mutable std::mutex mapMutex;
280 std::mutex socketModificationMutex;
282 std::mutex callbackMutex;
284 std::map<int, std::condition_variable> waitConds;
286 std::map<int, std::mutex> waitCondMutexes;
288 std::map<int, std::pair<bool, std::string> > lastSetRequestResult;
290 std::map<int, std::string> waitCondClasses;
293 std::function<void(
const std::string&)> parameterUpdateCallback;
294 bool parameterUpdateCallbackThreaded;
296 thread_local
static bool transactionInProgress;
297 thread_local
static std::vector<std::pair<std::string, std::string> > transactionQueuedWrites;
299 thread_local
static bool writingProhibited;
302 std::function<void(visiontransfer::ConnectionState)> connectionStateChangeCallback;
305 void attemptConnection();
308 void waitNetworkReady()
const;
314 void blockingCallThisThread(std::function<
void()>,
int waitMaxMilliseconds=1000,
const std::string& waitClass=
"");
316 void receiverRoutine();
319 void readParameter(
unsigned char messageType,
const char*
id,
unsigned char* dest,
int length);
320 void recvData(
unsigned char* dest,
int length);
322 std::map<std::string, ParameterInfo> recvEnumeration();
325 void writeParameterTransactionGuardedImpl(
const char*
id,
const T& value);
328 void writeParameterTransactionUnguardedImpl(
const char*
id,
const T& value);
330 void sendNetworkCommand(
const std::string& cmdline,
const std::string& diagStr);