blob: 5ebc4098de96cfebeb869a5ed43b57acf1186007 [file] [log] [blame]
Brian Silvermanf7bd1c22015-12-24 16:07:11 -08001/*----------------------------------------------------------------------------*/
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
22namespace nt {
23
24using llvm::ArrayRef;
25using llvm::StringRef;
26
27/** NetworkTables Entry Information */
28struct EntryInfo {
29 /** Entry name */
30 std::string name;
31
32 /** Entry type */
33 NT_Type type;
34
35 /** Entry flags */
36 unsigned int flags;
37
38 /** Timestamp of last change to entry (type or value). */
39 unsigned long long last_change;
40};
41
42/** NetworkTables Connection Information */
43struct ConnectionInfo {
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
51/** NetworkTables RPC Parameter Definition */
52struct 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
61/** NetworkTables RPC Result Definition */
62struct 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
70/** NetworkTables RPC Definition */
71struct RpcDefinition {
72 unsigned int version;
73 std::string name;
74 std::vector<RpcParamDef> params;
75 std::vector<RpcResultDef> results;
76};
77
78/** NetworkTables RPC Call Data */
79struct 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
90/** Get Entry Value.
91 * Returns copy of current entry value.
92 * Note that one of the type options is "unassigned".
93 *
94 * @param name entry name (UTF-8 string)
95 * @return entry value
96 */
97std::shared_ptr<Value> GetEntryValue(StringRef name);
98
99/** Set Entry Value.
100 * Sets new entry value. If type of new value differs from the type of the
101 * currently stored entry, returns error and does not update value.
102 *
103 * @param name entry name (UTF-8 string)
104 * @param value new entry value
105 * @return False on error (type mismatch), True on success
106 */
107bool SetEntryValue(StringRef name, std::shared_ptr<Value> value);
108
109/** Set Entry Type and Value.
110 * Sets new entry value. If type of new value differs from the type of the
111 * currently stored entry, the currently stored entry type is overridden
112 * (generally this will generate an Entry Assignment message).
113 *
114 * This is NOT the preferred method to update a value; generally
115 * SetEntryValue() should be used instead, with appropriate error handling.
116 *
117 * @param name entry name (UTF-8 string)
118 * @param value new entry value
119 */
120void SetEntryTypeValue(StringRef name, std::shared_ptr<Value> value);
121
122/** Set Entry Flags.
123 */
124void SetEntryFlags(StringRef name, unsigned int flags);
125
126/** Get Entry Flags.
127 */
128unsigned int GetEntryFlags(StringRef name);
129
130/** Delete Entry.
131 * Deletes an entry. This is a new feature in version 3.0 of the protocol,
132 * so this may not have an effect if any other node in the network is not
133 * version 3.0 or newer.
134 *
135 * Note: GetConnections() can be used to determine the protocol version
136 * of direct remote connection(s), but this is not sufficient to determine
137 * if all nodes in the network are version 3.0 or newer.
138 *
139 * @param name entry name (UTF-8 string)
140 */
141void DeleteEntry(StringRef name);
142
143/** Delete All Entries.
144 * Deletes ALL table entries. This is a new feature in version 3.0 of the
145 * so this may not have an effect if any other node in the network is not
146 * version 3.0 or newer.
147 *
148 * Note: GetConnections() can be used to determine the protocol version
149 * of direct remote connection(s), but this is not sufficient to determine
150 * if all nodes in the network are version 3.0 or newer.
151 */
152void DeleteAllEntries();
153
154/** Get Entry Information.
155 * Returns an array of entry information (name, entry type,
156 * and timestamp of last change to type/value). The results are optionally
157 * filtered by string prefix and entry type to only return a subset of all
158 * entries.
159 *
160 * @param prefix entry name required prefix; only entries whose name
161 * starts with this string are returned
162 * @param types bitmask of NT_Type values; 0 is treated specially
163 * as a "don't care"
164 * @return Array of entry information.
165 */
166std::vector<EntryInfo> GetEntryInfo(StringRef prefix, unsigned int types);
167
168/** Flush Entries.
169 * Forces an immediate flush of all local entry changes to network.
170 * Normally this is done on a regularly scheduled interval (see
171 * NT_SetUpdateRate()).
172 *
173 * Note: flushes are rate limited to avoid excessive network traffic. If
174 * the time between calls is too short, the flush will occur after the minimum
175 * time elapses (rather than immediately).
176 */
177void Flush();
178
179/*
180 * Callback Creation Functions
181 */
182
183void SetListenerOnStart(std::function<void()> on_start);
184void SetListenerOnExit(std::function<void()> on_exit);
185
186typedef std::function<void(unsigned int uid, StringRef name,
187 std::shared_ptr<Value> value,
188 unsigned int flags)> EntryListenerCallback;
189
190typedef std::function<void(unsigned int uid, bool connected,
191 const ConnectionInfo& conn)>
192 ConnectionListenerCallback;
193
194unsigned int AddEntryListener(StringRef prefix, EntryListenerCallback callback,
195 unsigned int flags);
196void RemoveEntryListener(unsigned int entry_listener_uid);
197unsigned int AddConnectionListener(ConnectionListenerCallback callback,
198 bool immediate_notify);
199void RemoveConnectionListener(unsigned int conn_listener_uid);
200
201bool NotifierDestroyed();
202
203/*
204 * Remote Procedure Call Functions
205 */
206
207typedef std::function<std::string(StringRef name, StringRef params)>
208 RpcCallback;
209
210void CreateRpc(StringRef name, StringRef def, RpcCallback callback);
211void CreatePolledRpc(StringRef name, StringRef def);
212
213bool PollRpc(bool blocking, RpcCallInfo* call_info);
214void PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
215 StringRef result);
216
217unsigned int CallRpc(StringRef name, StringRef params);
218bool GetRpcResult(bool blocking, unsigned int call_uid, std::string* result);
219
220std::string PackRpcDefinition(const RpcDefinition& def);
221bool UnpackRpcDefinition(StringRef packed, RpcDefinition *def);
222std::string PackRpcValues(ArrayRef<std::shared_ptr<Value>> values);
223std::vector<std::shared_ptr<Value>> UnpackRpcValues(StringRef packed,
224 ArrayRef<NT_Type> types);
225
226/*
227 * Client/Server Functions
228 */
229void SetNetworkIdentity(StringRef name);
230void StartServer(StringRef persist_filename, const char* listen_address,
231 unsigned int port);
232void StopServer();
233void StartClient(const char* server_name, unsigned int port);
234void StopClient();
235void StopRpcServer();
236void StopNotifier();
237void SetUpdateRate(double interval);
238std::vector<ConnectionInfo> GetConnections();
239
240/*
241 * Persistent Functions
242 */
243/* return error string, or nullptr if successful */
244const char* SavePersistent(StringRef filename);
245const char* LoadPersistent(
246 StringRef filename, std::function<void(size_t line, const char* msg)> warn);
247
248/*
249 * Utility Functions
250 */
251
252/* timestamp */
253unsigned long long Now();
254
255/* logging */
256typedef std::function<void(unsigned int level, const char* file,
257 unsigned int line, const char* msg)> LogFunc;
258void SetLogger(LogFunc func, unsigned int min_level);
259
260} // namespace nt
261
262#endif /* NTCORE_CPP_H_ */