libvisiontransfer  10.6.0
parametertransfer.h
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_PARAMETERTRANSFER_H
16 #define VISIONTRANSFER_PARAMETERTRANSFER_H
17 
18 #include "visiontransfer/networking.h"
19 #include "visiontransfer/parameterinfo.h"
20 #include "visiontransfer/tokenizer.h"
21 #include "visiontransfer/parameterset.h"
22 
23 #include <map>
24 #include <set>
25 #include <memory>
26 #include <thread>
27 #include <condition_variable>
28 #include <functional>
29 
30 namespace visiontransfer {
31 namespace internal {
32 
46 class ParameterTransfer {
47 public:
55  ParameterTransfer(const char* address, const char* service = "7683");
57 
68  int readIntParameter(const char* id);
69 
81  double readDoubleParameter(const char* id);
82 
93  bool readBoolParameter(const char* id);
94 
105  void writeIntParameter(const char* id, int value);
106 
117  void writeDoubleParameter(const char* id, double value);
118 
129  void writeBoolParameter(const char* id, bool value);
130 
141  template<typename T>
142  void writeParameter(const char* id, const T& value, bool synchronous=true);
143 
154  template<typename T>
155  void writeParameterTransactionGuarded(const char* id, const T& value);
156 
167  template<typename T>
168  void writeParameterTransactionUnguarded(const char* id, const T& value);
169 
173  std::map<std::string, ParameterInfo> getAllParameters();
174 
178  param::ParameterSet& getParameterSet();
179 
183  param::ParameterSet const& getParameterSet() const;
184 
185  void setParameterUpdateCallback(std::function<void(const std::string& uid)> callback);
186 
192  void transactionStartQueue();
193 
198  void transactionCommitQueue();
199 
200 private:
201  static constexpr int SOCKET_TIMEOUT_MS = 500;
202  static constexpr int SOCKET_RECONNECT_INTERVAL_MS = 2000;
203 
204  // Message types
205  static constexpr unsigned char MESSAGE_READ_INT = 0x01;
206  static constexpr unsigned char MESSAGE_READ_DOUBLE = 0x02;
207  static constexpr unsigned char MESSAGE_READ_BOOL = 0x03;
208  static constexpr unsigned char MESSAGE_WRITE_INT = 0x04;
209  static constexpr unsigned char MESSAGE_WRITE_DOUBLE = 0x05;
210  static constexpr unsigned char MESSAGE_WRITE_BOOL = 0x06;
211  static constexpr unsigned char MESSAGE_ENUMERATE_PARAMS = 0x07;
212 
213  SOCKET socket;
214 
215  std::string address; // for reconnection
216  std::string service;
217 
218  static constexpr unsigned int RECV_BUF_SIZE = 1024*1024;
219  char recvBuf[RECV_BUF_SIZE];
220  unsigned int recvBufBytes;
221  unsigned int pollDelay;
222  bool networkError; // background thread exception flag
223  std::string networkErrorString;
224  bool networkReady; // after protocol check and initial update
225 
226  // Fallback handling for connecting to out-of-date firmware versions
227  bool featureDisabledTransactions;
228 
229  bool threadRunning;
230  std::shared_ptr<std::thread> receiverThread;
231 
232  Tokenizer tabTokenizer;
233 
234  param::ParameterSet paramSet;
235 
237  mutable std::mutex readyMutex;
239  mutable std::condition_variable readyCond;
241  std::mutex mapMutex;
243  std::mutex socketModificationMutex;
245  std::map<int, std::condition_variable> waitConds;
247  std::map<int, std::mutex> waitCondMutexes;
249  std::map<int, std::pair<bool, std::string> > lastSetRequestResult;
250 
252  std::function<void(const std::string&)> parameterUpdateCallback;
253 
254  thread_local static bool transactionInProgress;
255  thread_local static std::vector<std::pair<std::string, std::string> > transactionQueuedWrites;
256 
258  void attemptConnection();
259 
261  void waitNetworkReady() const;
262 
264  int getThreadId();
265 
267  void blockingCallThisThread(std::function<void()>, int waitMaxMilliseconds=1000);
268 
269  void receiverRoutine();
270  void process();
271 
272  void readParameter(unsigned char messageType, const char* id, unsigned char* dest, int length);
273  void recvData(unsigned char* dest, int length);
274 
275  std::map<std::string, ParameterInfo> recvEnumeration();
276 
277  template<typename T>
278  void writeParameterTransactionGuardedImpl(const char* id, const T& value);
279 
280  template<typename T>
281  void writeParameterTransactionUnguardedImpl(const char* id, const T& value);
282 
283  void sendNetworkCommand(const std::string& cmdline);
284 };
285 
286 }} // namespace
287 
288 #endif
visiontransfer::internal::ParameterTransfer::writeDoubleParameter
void writeDoubleParameter(const char *id, double value)
Writes a double precision floating point value to a parameter of the parameter server.
Definition: parametertransfer.cpp:290
visiontransfer::internal::ParameterTransfer::transactionCommitQueue
void transactionCommitQueue()
Complete the started parameter transaction.
Definition: parametertransfer.cpp:611
visiontransfer::internal::ParameterTransfer::writeIntParameter
void writeIntParameter(const char *id, int value)
Writes an integer value to a parameter of the parameter server.
Definition: parametertransfer.cpp:286
visiontransfer::internal::ParameterTransfer::writeParameter
void writeParameter(const char *id, const T &value, bool synchronous=true)
Writes a scalar value to a parameter of the parameter server.
Definition: parametertransfer.cpp:123
visiontransfer::internal::ParameterTransfer::getParameterSet
param::ParameterSet & getParameterSet()
Returns a reference to the internal parameter set (once the network handshake is complete)
Definition: parametertransfer.cpp:578
visiontransfer::internal::ParameterTransfer::ParameterTransfer
ParameterTransfer(const char *address, const char *service="7683")
Creates an object and connects to the given server.
Definition: parametertransfer.cpp:42
visiontransfer::internal::ParameterTransfer::writeParameterTransactionUnguarded
void writeParameterTransactionUnguarded(const char *id, const T &value)
Writes a scalar value to a parameter of the parameter server, using 'fire-and-forget' for real-time c...
visiontransfer::internal::ParameterTransfer::getAllParameters
std::map< std::string, ParameterInfo > getAllParameters()
Enumerates all parameters as reported by the device.
Definition: parametertransfer.cpp:298
visiontransfer::internal::ParameterTransfer::readBoolParameter
bool readBoolParameter(const char *id)
Reads a boolean value from the parameter server.
Definition: parametertransfer.cpp:274
visiontransfer::internal::ParameterTransfer::transactionStartQueue
void transactionStartQueue()
Start batch parameter transaction.
Definition: parametertransfer.cpp:600
visiontransfer::internal::ParameterTransfer::writeParameterTransactionGuarded
void writeParameterTransactionGuarded(const char *id, const T &value)
Writes a scalar value to a parameter of the parameter server, transparently deferring for a batch upd...
visiontransfer::internal::ParameterTransfer::readDoubleParameter
double readDoubleParameter(const char *id)
Reads a double precision floating point value from the parameter server.
Definition: parametertransfer.cpp:262
visiontransfer::internal::ParameterTransfer::writeBoolParameter
void writeBoolParameter(const char *id, bool value)
Writes a boolean value to a parameter of the parameter server.
Definition: parametertransfer.cpp:294
visiontransfer::internal::ParameterTransfer::readIntParameter
int readIntParameter(const char *id)
Reads an integer value from the parameter server.
Definition: parametertransfer.cpp:250
Allied Vision