libvisiontransfer  10.8.0
parametertransfer.h
1 /*******************************************************************************
2  * Copyright (c) 2024 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/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"
24 
25 #include <map>
26 #include <set>
27 #include <memory>
28 #include <thread>
29 #include <condition_variable>
30 #include <functional>
31 
32 namespace visiontransfer {
33 namespace internal {
34 
48 class ParameterTransfer {
49 public:
57  ParameterTransfer(const char* address, const char* service = "7683");
59 
64  bool isConnected() const;
65 
76  int readIntParameter(const char* id);
77 
89  double readDoubleParameter(const char* id);
90 
101  bool readBoolParameter(const char* id);
102 
113  void writeIntParameter(const char* id, int value);
114 
125  void writeDoubleParameter(const char* id, double value);
126 
137  void writeBoolParameter(const char* id, bool value);
138 
149  template<typename T>
150  void writeParameter(const char* id, const T& value, bool synchronous=true);
151 
162  template<typename T>
163  void writeParameterTransactionGuarded(const char* id, const T& value);
164 
175  template<typename T>
176  void writeParameterTransactionUnguarded(const char* id, const T& value);
177 
181  std::map<std::string, ParameterInfo> getAllParameters();
182 
186  bool hasParameter(const std::string& uid) const;
187 
192  param::Parameter getParameter(const std::string& uid) const;
193 
198  param::Parameter pollParameter(const std::string& uid, bool blockingCall);
199 
203  param::ParameterSet& getParameterSet();
204 
208  param::ParameterSet const& getParameterSet() const;
209 
210  void setParameterUpdateCallback(std::function<void(const std::string& uid)> callback, bool threaded);
211 
217  void transactionStartQueue();
218 
223  void transactionCommitQueue(int maxWaitMilliseconds);
224 
228  void persistParameters(const std::vector<std::string>& uids, bool synchronous=true);
229 
233  void setConnectionStateChangeCallback(std::function<void(visiontransfer::ConnectionState)> callback);
234 
235 private:
236  static constexpr int SOCKET_TIMEOUT_MS = 500;
237  static constexpr int SOCKET_RECONNECT_INTERVAL_MS = 2000;
238 
239  // Message types
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;
247 
248  SOCKET socket;
249 
250  std::string address; // for reconnection
251  std::string service;
252 
253  static constexpr unsigned int RECV_BUF_SIZE = 1024*1024;
254  char recvBuf[RECV_BUF_SIZE];
255  unsigned int recvBufBytes;
256  unsigned int pollDelay;
257  bool networkError; // background thread exception flag
258  std::string networkErrorString;
259  bool networkReady; // after protocol check and initial update
260 
261  // Fallback handling for connecting to out-of-date firmware versions
262  bool featureDisabledTransactions;
263 
264  bool threadRunning;
265  std::shared_ptr<std::thread> receiverThread;
266 
267  Tokenizer tabTokenizer;
268  Tokenizer spaceTokenizer;
269  Tokenizer slashTokenizer;
270 
271  param::ParameterSet paramSet;
272 
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;
291 
293  std::function<void(const std::string&)> parameterUpdateCallback;
294  bool parameterUpdateCallbackThreaded;
295 
296  thread_local static bool transactionInProgress;
297  thread_local static std::vector<std::pair<std::string, std::string> > transactionQueuedWrites;
298 
299  thread_local static bool writingProhibited;
300 
302  std::function<void(visiontransfer::ConnectionState)> connectionStateChangeCallback;
303 
305  void attemptConnection();
306 
308  void waitNetworkReady() const;
309 
311  int getThreadId();
312 
314  void blockingCallThisThread(std::function<void()>, int waitMaxMilliseconds=1000, const std::string& waitClass="");
315 
316  void receiverRoutine();
317  void process();
318 
319  void readParameter(unsigned char messageType, const char* id, unsigned char* dest, int length);
320  void recvData(unsigned char* dest, int length);
321 
322  std::map<std::string, ParameterInfo> recvEnumeration();
323 
324  template<typename T>
325  void writeParameterTransactionGuardedImpl(const char* id, const T& value);
326 
327  template<typename T>
328  void writeParameterTransactionUnguardedImpl(const char* id, const T& value);
329 
330  void sendNetworkCommand(const std::string& cmdline, const std::string& diagStr);
331 
332 };
333 
334 }} // namespace
335 
336 #endif
visiontransfer::internal::ParameterTransfer::isConnected
bool isConnected() const
Returns whether the background connection is currently up and running (it may be temporarily false du...
Definition: parametertransfer.cpp:65
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:328
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:324
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:133
visiontransfer::internal::ParameterTransfer::getParameterSet
param::ParameterSet & getParameterSet()
Returns a reference to the internal parameter set (once the network handshake is complete)
Definition: parametertransfer.cpp:685
visiontransfer::internal::ParameterTransfer::hasParameter
bool hasParameter(const std::string &uid) const
Returns true if the matching parameter UID is present in the parameter set.
Definition: parametertransfer.cpp:703
visiontransfer::internal::ParameterTransfer::persistParameters
void persistParameters(const std::vector< std::string > &uids, bool synchronous=true)
Requests to save the current values for the specified parameter UIDs to permanent storage.
Definition: parametertransfer.cpp:839
visiontransfer::internal::ParameterTransfer::pollParameter
param::Parameter pollParameter(const std::string &uid, bool blockingCall)
Returns a disconnected copy of the matching parameter from the parameter set after polling for its cu...
Definition: parametertransfer.cpp:887
visiontransfer::internal::ParameterTransfer::ParameterTransfer
ParameterTransfer(const char *address, const char *service="7683")
Creates an object and connects to the given server.
Definition: parametertransfer.cpp:43
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:336
visiontransfer::internal::ParameterTransfer::setConnectionStateChangeCallback
void setConnectionStateChangeCallback(std::function< void(visiontransfer::ConnectionState)> callback)
Sets the callback function to inform of background disconnection / reconnection.
Definition: parametertransfer.cpp:926
visiontransfer::internal::ParameterTransfer::readBoolParameter
bool readBoolParameter(const char *id)
Reads a boolean value from the parameter server.
Definition: parametertransfer.cpp:311
visiontransfer::internal::ParameterTransfer::transactionStartQueue
void transactionStartQueue()
Start batch parameter transaction.
Definition: parametertransfer.cpp:735
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:298
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:332
visiontransfer::internal::ParameterTransfer::readIntParameter
int readIntParameter(const char *id)
Reads an integer value from the parameter server.
Definition: parametertransfer.cpp:285
visiontransfer::internal::ParameterTransfer::transactionCommitQueue
void transactionCommitQueue(int maxWaitMilliseconds)
Complete the started parameter transaction.
Definition: parametertransfer.cpp:746
visiontransfer::internal::ParameterTransfer::getParameter
param::Parameter getParameter(const std::string &uid) const
Returns a disconnected copy of the matching parameter from the parameter set; exception if not found.
Definition: parametertransfer.cpp:714
Allied Vision