libvisiontransfer  10.0.0
parametertransfer.h
1 /*******************************************************************************
2  * Copyright (c) 2022 Nerian Vision 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 <memory>
25 #include <thread>
26 #include <condition_variable>
27 #include <functional>
28 
29 namespace visiontransfer {
30 namespace internal {
31 
46 public:
54  ParameterTransfer(const char* address, const char* service = "7683");
56 
67  int readIntParameter(const char* id);
68 
80  double readDoubleParameter(const char* id);
81 
92  bool readBoolParameter(const char* id);
93 
104  void writeIntParameter(const char* id, int value);
105 
116  void writeDoubleParameter(const char* id, double value);
117 
128  void writeBoolParameter(const char* id, bool value);
129 
140  template<typename T>
141  void writeParameter(const char* id, const T& value);
142 
146  std::map<std::string, ParameterInfo> getAllParameters();
147 
152 
153 private:
154  static constexpr int SOCKET_TIMEOUT_MS = 500;
155 
156  // Message types
157  static constexpr unsigned char MESSAGE_READ_INT = 0x01;
158  static constexpr unsigned char MESSAGE_READ_DOUBLE = 0x02;
159  static constexpr unsigned char MESSAGE_READ_BOOL = 0x03;
160  static constexpr unsigned char MESSAGE_WRITE_INT = 0x04;
161  static constexpr unsigned char MESSAGE_WRITE_DOUBLE = 0x05;
162  static constexpr unsigned char MESSAGE_WRITE_BOOL = 0x06;
163  static constexpr unsigned char MESSAGE_ENUMERATE_PARAMS = 0x07;
164 
165  SOCKET socket;
166 
167  static constexpr unsigned int RECV_BUF_SIZE = 1024*1024;
168  char recvBuf[RECV_BUF_SIZE];
169  unsigned int recvBufBytes;
170  unsigned int pollDelay;
171  bool networkError; // background thread exception flag
172  std::string networkErrorString;
173  bool networkReady; // after protocol check and initial update
174 
175  bool threadRunning;
176  std::shared_ptr<std::thread> receiverThread;
177 
178  Tokenizer tabTokenizer;
179 
180  param::ParameterSet paramSet;
181 
183  std::mutex readyMutex;
185  std::condition_variable readyCond;
187  std::mutex mapMutex;
189  std::map<int, std::condition_variable> waitConds;
191  std::map<int, std::mutex> waitCondMutexes;
193  std::map<int, std::pair<bool, std::string> > lastSetRequestResult;
194 
196  void waitNetworkReady();
197 
199  int getThreadId();
200 
202  void blockingCallThisThread(std::function<void()>, int waitMaxMilliseconds=1000);
203 
204  void receiverRoutine();
205  void process();
206 
207  void readParameter(unsigned char messageType, const char* id, unsigned char* dest, int length);
208  void recvData(unsigned char* dest, int length);
209 
210  std::map<std::string, ParameterInfo> recvEnumeration();
211 };
212 
213 }} // namespace
214 
215 #endif
void writeIntParameter(const char *id, int value)
Writes an integer value to a parameter of the parameter server.
Allows a configuration of device parameters over the network.
void writeParameter(const char *id, const T &value)
Writes a scalar value to a parameter of the parameter server.
ParameterTransfer(const char *address, const char *service="7683")
Creates an object and connects to the given server.
bool readBoolParameter(const char *id)
Reads a boolean value from the parameter server.
std::map< std::string, ParameterInfo > getAllParameters()
Enumerates all parameters as reported by the device.
param::ParameterSet & getParameterSet()
Returns a reference to the internal parameter set (once the network handshake is complete) ...
double readDoubleParameter(const char *id)
Reads a double precision floating point value from the parameter server.
void writeBoolParameter(const char *id, bool value)
Writes a boolean value to a parameter of the parameter server.
int readIntParameter(const char *id)
Reads an integer value from the parameter server.
void writeDoubleParameter(const char *id, double value)
Writes a double precision floating point value to a parameter of the parameter server.
Nerian Vision Technologies