WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
ntcore_cpp.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NTCORE_CPP_H_
9 #define NTCORE_CPP_H_
10 
11 #include <cassert>
12 #include <functional>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 #include "llvm/ArrayRef.h"
18 #include "llvm/StringRef.h"
19 
20 #include "nt_Value.h"
21 
22 namespace nt {
23 
24 using llvm::ArrayRef;
25 using llvm::StringRef;
26 
28 struct EntryInfo {
30  std::string name;
31 
33  NT_Type type;
34 
36  unsigned int flags;
37 
39  unsigned long long last_change;
40 };
41 
44  std::string remote_id;
45  std::string remote_ip;
46  unsigned int remote_port;
47  unsigned long long last_update;
48  unsigned int protocol_version;
49 };
50 
52 struct RpcParamDef {
53  RpcParamDef() = default;
54  RpcParamDef(StringRef name_, std::shared_ptr<Value> def_value_)
55  : name(name_), def_value(def_value_) {}
56 
57  std::string name;
58  std::shared_ptr<Value> def_value;
59 };
60 
62 struct RpcResultDef {
63  RpcResultDef() = default;
64  RpcResultDef(StringRef name_, NT_Type type_) : name(name_), type(type_) {}
65 
66  std::string name;
67  NT_Type type;
68 };
69 
71 struct RpcDefinition {
72  unsigned int version;
73  std::string name;
74  std::vector<RpcParamDef> params;
75  std::vector<RpcResultDef> results;
76 };
77 
79 struct RpcCallInfo {
80  unsigned int rpc_id;
81  unsigned int call_uid;
82  std::string name;
83  std::string params;
84 };
85 
86 /*
87  * Table Functions
88  */
89 
97 std::shared_ptr<Value> GetEntryValue(StringRef name);
98 
108 bool SetDefaultEntryValue(StringRef name, std::shared_ptr<Value> value);
109 
118 bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
119 
131 void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
132 
135 void SetEntryFlags(StringRef name, unsigned int flags);
136 
139 unsigned int GetEntryFlags(StringRef name);
140 
152 void DeleteEntry(StringRef name);
153 
163 void DeleteAllEntries();
164 
177 std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
178 
188 void Flush();
189 
190 /*
191  * Callback Creation Functions
192  */
193 
194 void SetListenerOnStart(std::function<void()> on_start);
195 void SetListenerOnExit(std::function<void()> on_exit);
196 
197 typedef std::function<void(unsigned int uid, StringRef name,
198  std::shared_ptr<Value> value, unsigned int flags)>
199  EntryListenerCallback;
200 
201 typedef std::function<void(unsigned int uid, bool connected,
202  const ConnectionInfo& conn)>
203  ConnectionListenerCallback;
204 
205 unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
206  unsigned int flags);
207 void RemoveEntryListener(unsigned int entry_listener_uid);
208 unsigned int AddConnectionListener(ConnectionListenerCallback callback,
209  bool immediate_notify);
210 void RemoveConnectionListener(unsigned int conn_listener_uid);
211 
212 bool NotifierDestroyed();
213 
214 /*
215  * Remote Procedure Call Functions
216  */
217 
218 #if defined(_MSC_VER) && _MSC_VER < 1900
219 const double kTimeout_Indefinite = -1;
220 #else
221 constexpr double kTimeout_Indefinite = -1;
222 #endif
223 
224 void SetRpcServerOnStart(std::function<void()> on_start);
225 void SetRpcServerOnExit(std::function<void()> on_exit);
226 
227 typedef std::function<std::string(StringRef name, StringRef params,
228  const ConnectionInfo& conn_info)>
229  RpcCallback;
230 
231 void CreateRpc(StringRef name, StringRef def, RpcCallback callback);
232 void CreatePolledRpc(StringRef name, StringRef def);
233 
234 bool PollRpc(bool blocking, RpcCallInfo* call_info);
235 bool PollRpc(bool blocking, double time_out, RpcCallInfo* call_info);
236 void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
237  StringRef result);
238 
239 unsigned int CallRpc(StringRef name, StringRef params);
240 bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result);
241 bool GetRpcResult(bool blocking, unsigned int call_uid, double time_out,
242  std::string* result);
243 void CancelBlockingRpcResult(unsigned int call_uid);
244 
245 std::string PackRpcDefinition(const RpcDefinition& def);
246 bool UnpackRpcDefinition(StringRef packed, RpcDefinition* def);
247 std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values);
248 std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
249  ArrayRef<NT_Type> types);
250 
251 /*
252  * Client/Server Functions
253  */
254 void SetNetworkIdentity(StringRef name);
255 unsigned int GetNetworkMode();
256 void StartServer(StringRef persist_filename, const char* listen_address,
257  unsigned int port);
258 void StopServer();
259 void StartClient();
260 void StartClient(const char* server_name, unsigned int port);
261 void StartClient(ArrayRef<std::pair<StringRef, unsigned int>> servers);
262 void StopClient();
263 void SetServer(const char* server_name, unsigned int port);
264 void SetServer(ArrayRef<std::pair<StringRef, unsigned int>> servers);
265 void StartDSClient(unsigned int port);
266 void StopDSClient();
267 void StopRpcServer();
268 void StopNotifier();
269 void SetUpdateRate(double interval);
270 std::vector<ConnectionInfo> GetConnections();
271 
272 /*
273  * Persistent Functions
274  */
275 /* return error string, or nullptr if successful */
276 const char* SavePersistent(StringRef filename);
277 const char* LoadPersistent(
278  StringRef filename, std::function<void(size_t line, const char* msg)> warn);
279 
280 /*
281  * Utility Functions
282  */
283 
284 /* timestamp */
285 unsigned long long Now();
286 
287 /* logging */
288 typedef std::function<void(unsigned int level, const char* file,
289  unsigned int line, const char* msg)>
290  LogFunc;
291 void SetLogger(LogFunc func, unsigned int min_level);
292 
293 } // namespace nt
294 
295 #endif /* NTCORE_CPP_H_ */
NetworkTables Connection Information.
Definition: ntcore_cpp.h:43
NetworkTables RPC Call Data.
Definition: ntcore_cpp.h:79
NetworkTables RPC Definition.
Definition: ntcore_cpp.h:71
unsigned long long last_change
Timestamp of last change to entry (type or value).
Definition: ntcore_cpp.h:39
NT_Type type
Entry type.
Definition: ntcore_cpp.h:33
NetworkTables RPC Parameter Definition.
Definition: ntcore_cpp.h:52
NetworkTables Entry Information.
Definition: ntcore_cpp.h:28
std::string name
Entry name.
Definition: ntcore_cpp.h:30
unsigned int flags
Entry flags.
Definition: ntcore_cpp.h:36
NetworkTables RPC Result Definition.
Definition: ntcore_cpp.h:62