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_name;
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 
107 bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
108 
120 void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
121 
124 void SetEntryFlags(StringRef name, unsigned int flags);
125 
128 unsigned int GetEntryFlags(StringRef name);
129 
141 void DeleteEntry(StringRef name);
142 
152 void DeleteAllEntries();
153 
166 std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
167 
177 void Flush();
178 
179 /*
180  * Callback Creation Functions
181  */
182 
183 void SetListenerOnStart(std::function<void()> on_start);
184 void SetListenerOnExit(std::function<void()> on_exit);
185 
186 typedef std::function<void(unsigned int uid, StringRef name,
187  std::shared_ptr<Value> value,
188  unsigned int flags)> EntryListenerCallback;
189 
190 typedef std::function<void(unsigned int uid, bool connected,
191  const ConnectionInfo& conn)>
192  ConnectionListenerCallback;
193 
194 unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
195  unsigned int flags);
196 void RemoveEntryListener(unsigned int entry_listener_uid);
197 unsigned int AddConnectionListener(ConnectionListenerCallback callback,
198  bool immediate_notify);
199 void RemoveConnectionListener(unsigned int conn_listener_uid);
200 
201 bool NotifierDestroyed();
202 
203 /*
204  * Remote Procedure Call Functions
205  */
206 
207 void SetRpcServerOnStart(std::function<void()> on_start);
208 void SetRpcServerOnExit(std::function<void()> on_exit);
209 
210 typedef std::function<std::string(StringRef name, StringRef params)>
211  RpcCallback;
212 
213 void CreateRpc(StringRef name, StringRef def, RpcCallback callback);
214 void CreatePolledRpc(StringRef name, StringRef def);
215 
216 bool PollRpc(bool blocking, RpcCallInfo* call_info);
217 void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
218  StringRef result);
219 
220 unsigned int CallRpc(StringRef name, StringRef params);
221 bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result);
222 
223 std::string PackRpcDefinition(const RpcDefinition& def);
224 bool UnpackRpcDefinition(StringRef packed, RpcDefinition *def);
225 std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values);
226 std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
227  ArrayRef<NT_Type> types);
228 
229 /*
230  * Client/Server Functions
231  */
232 void SetNetworkIdentity(StringRef name);
233 void StartServer(StringRef persist_filename, const char* listen_address,
234  unsigned int port);
235 void StopServer();
236 void StartClient(const char* server_name, unsigned int port);
237 void StopClient();
238 void StopRpcServer();
239 void StopNotifier();
240 void SetUpdateRate(double interval);
241 std::vector<ConnectionInfo> GetConnections();
242 
243 /*
244  * Persistent Functions
245  */
246 /* return error string, or nullptr if successful */
247 const char* SavePersistent(StringRef filename);
248 const char* LoadPersistent(
249  StringRef filename, std::function<void(size_t line, const char* msg)> warn);
250 
251 /*
252  * Utility Functions
253  */
254 
255 /* timestamp */
256 unsigned long long Now();
257 
258 /* logging */
259 typedef std::function<void(unsigned int level, const char* file,
260  unsigned int line, const char* msg)> LogFunc;
261 void SetLogger(LogFunc func, unsigned int min_level);
262 
263 } // namespace nt
264 
265 #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
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:54
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
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39