WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
ntcore_c.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_C_H_
9 #define NTCORE_C_H_
10 
11 #include <stddef.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
18 #define NT_DEFAULT_PORT 1735
19 
21 enum NT_Type {
22  NT_UNASSIGNED = 0,
23  NT_BOOLEAN = 0x01,
24  NT_DOUBLE = 0x02,
25  NT_STRING = 0x04,
26  NT_RAW = 0x08,
27  NT_BOOLEAN_ARRAY = 0x10,
28  NT_DOUBLE_ARRAY = 0x20,
29  NT_STRING_ARRAY = 0x40,
30  NT_RPC = 0x80
31 };
32 
34 enum NT_EntryFlags { NT_PERSISTENT = 0x01 };
35 
37 enum NT_LogLevel {
38  NT_LOG_CRITICAL = 50,
39  NT_LOG_ERROR = 40,
40  NT_LOG_WARNING = 30,
41  NT_LOG_INFO = 20,
42  NT_LOG_DEBUG = 10,
43  NT_LOG_DEBUG1 = 9,
44  NT_LOG_DEBUG2 = 8,
45  NT_LOG_DEBUG3 = 7,
46  NT_LOG_DEBUG4 = 6
47 };
48 
50 enum NT_NotifyKind {
51  NT_NOTIFY_NONE = 0,
52  NT_NOTIFY_IMMEDIATE = 0x01, /* initial listener addition */
53  NT_NOTIFY_LOCAL = 0x02, /* changed locally */
54  NT_NOTIFY_NEW = 0x04, /* newly created entry */
55  NT_NOTIFY_DELETE = 0x08, /* deleted */
56  NT_NOTIFY_UPDATE = 0x10, /* value changed */
57  NT_NOTIFY_FLAGS = 0x20 /* flags changed */
58 };
59 
60 /*
61  * Structures
62  */
63 
65 struct NT_String {
71  char* str;
72 
76  size_t len;
77 };
78 
80 struct NT_Value {
81  enum NT_Type type;
82  unsigned long long last_change;
83  union {
84  int v_boolean;
85  double v_double;
86  struct NT_String v_string;
87  struct NT_String v_raw;
88  struct {
89  int* arr;
90  size_t size;
91  } arr_boolean;
92  struct {
93  double* arr;
94  size_t size;
95  } arr_double;
96  struct {
97  struct NT_String* arr;
98  size_t size;
99  } arr_string;
100  } data;
101 };
102 
104 struct NT_EntryInfo {
106  struct NT_String name;
107 
109  enum NT_Type type;
110 
112  unsigned int flags;
113 
115  unsigned long long last_change;
116 };
117 
120  struct NT_String remote_id;
121  struct NT_String remote_ip;
122  unsigned int remote_port;
123  unsigned long long last_update;
124  unsigned int protocol_version;
125 };
126 
129  struct NT_String name;
130  struct NT_Value def_value;
131 };
132 
135  struct NT_String name;
136  enum NT_Type type;
137 };
138 
141  unsigned int version;
142  struct NT_String name;
143  size_t num_params;
144  NT_RpcParamDef* params;
145  size_t num_results;
146  NT_RpcResultDef* results;
147 };
148 
151  unsigned int rpc_id;
152  unsigned int call_uid;
153  struct NT_String name;
154  struct NT_String params;
155 };
156 
157 /*
158  * Table Functions
159  */
160 
173 void NT_GetEntryValue(const char* name, size_t name_len,
174  struct NT_Value* value);
175 
186 int NT_SetDefaultEntryValue(const char* name, size_t name_len,
187  const struct NT_Value* default_value);
188 
198 int NT_SetEntryValue(const char* name, size_t name_len,
199  const struct NT_Value* value);
200 
213 void NT_SetEntryTypeValue(const char* name, size_t name_len,
214  const struct NT_Value* value);
215 
218 void NT_SetEntryFlags(const char* name, size_t name_len, unsigned int flags);
219 
222 unsigned int NT_GetEntryFlags(const char* name, size_t name_len);
223 
236 void NT_DeleteEntry(const char* name, size_t name_len);
237 
247 void NT_DeleteAllEntries(void);
248 
263 struct NT_EntryInfo* NT_GetEntryInfo(const char* prefix, size_t prefix_len,
264  unsigned int types, size_t* count);
265 
275 void NT_Flush(void);
276 
277 /*
278  * Callback Creation Functions
279  */
280 
281 void NT_SetListenerOnStart(void (*on_start)(void* data), void* data);
282 void NT_SetListenerOnExit(void (*on_exit)(void* data), void* data);
283 
284 typedef void (*NT_EntryListenerCallback)(unsigned int uid, void* data,
285  const char* name, size_t name_len,
286  const struct NT_Value* value,
287  unsigned int flags);
288 
289 typedef void (*NT_ConnectionListenerCallback)(
290  unsigned int uid, void* data, int connected,
291  const struct NT_ConnectionInfo* conn);
292 
293 unsigned int NT_AddEntryListener(const char* prefix, size_t prefix_len,
294  void* data, NT_EntryListenerCallback callback,
295  unsigned int flags);
296 void NT_RemoveEntryListener(unsigned int entry_listener_uid);
297 unsigned int NT_AddConnectionListener(void* data,
298  NT_ConnectionListenerCallback callback,
299  int immediate_notify);
300 void NT_RemoveConnectionListener(unsigned int conn_listener_uid);
301 
302 int NT_NotifierDestroyed();
303 
304 /*
305  * Remote Procedure Call Functions
306  */
307 
308 void NT_SetRpcServerOnStart(void (*on_start)(void* data), void* data);
309 void NT_SetRpcServerOnExit(void (*on_exit)(void* data), void* data);
310 
311 typedef char* (*NT_RpcCallback)(void* data, const char* name, size_t name_len,
312  const char* params, size_t params_len,
313  size_t* results_len,
314  const struct NT_ConnectionInfo* conn_info);
315 
316 void NT_CreateRpc(const char* name, size_t name_len, const char* def,
317  size_t def_len, void* data, NT_RpcCallback callback);
318 void NT_CreatePolledRpc(const char* name, size_t name_len, const char* def,
319  size_t def_len);
320 
321 int NT_PollRpc(int blocking, struct NT_RpcCallInfo* call_info);
322 int NT_PollRpcTimeout(int blocking, double time_out,
323  struct NT_RpcCallInfo* call_info);
324 void NT_PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
325  const char* result, size_t result_len);
326 
327 unsigned int NT_CallRpc(const char* name, size_t name_len, const char* params,
328  size_t params_len);
329 char* NT_GetRpcResult(int blocking, unsigned int call_uid, size_t* result_len);
330 char* NT_GetRpcResultTimeout(int blocking, unsigned int call_uid,
331  double time_out, size_t* result_len);
332 void NT_CancelBlockingRpcResult(unsigned int call_uid);
333 
334 char* NT_PackRpcDefinition(const struct NT_RpcDefinition* def,
335  size_t* packed_len);
336 int NT_UnpackRpcDefinition(const char* packed, size_t packed_len,
337  struct NT_RpcDefinition* def);
338 char* NT_PackRpcValues(const struct NT_Value** values, size_t values_len,
339  size_t* packed_len);
340 struct NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len,
341  const NT_Type* types, size_t types_len);
342 
343 /*
344  * Client/Server Functions
345  */
346 void NT_SetNetworkIdentity(const char* name, size_t name_len);
347 
357 void NT_StartServer(const char* persist_filename, const char* listen_address,
358  unsigned int port);
359 
363 void NT_StopServer(void);
364 
368 void NT_StartClientNone(void);
369 
377 void NT_StartClient(const char* server_name, unsigned int port);
378 
389 void NT_StartClientMulti(size_t count, const char** server_names,
390  const unsigned int* ports);
391 
395 void NT_StopClient(void);
396 
403 void NT_SetServer(const char* server_name, unsigned int port);
404 
414 void NT_SetServerMulti(size_t count, const char** server_names,
415  const unsigned int* ports);
416 
423 void NT_StartDSClient(unsigned int port);
424 
426 void NT_StopDSClient(void);
427 
431 void NT_StopRpcServer(void);
432 
436 void NT_StopNotifier(void);
437 
444 void NT_SetUpdateRate(double interval);
445 
455 struct NT_ConnectionInfo* NT_GetConnections(size_t* count);
456 
457 /*
458  * Persistent Functions
459  */
460 /* return error string, or NULL if successful */
461 const char* NT_SavePersistent(const char* filename);
462 const char* NT_LoadPersistent(const char* filename,
463  void (*warn)(size_t line, const char* msg));
464 
465 /*
466  * Utility Functions
467  */
468 
469 /* frees value memory */
470 void NT_DisposeValue(struct NT_Value* value);
471 
472 /* sets type to unassigned and clears rest of struct */
473 void NT_InitValue(struct NT_Value* value);
474 
475 /* frees string memory */
476 void NT_DisposeString(struct NT_String* str);
477 
478 /* sets length to zero and pointer to null */
479 void NT_InitString(struct NT_String* str);
480 
481 /* Gets the type for the specified key, or unassigned if non existent. */
482 enum NT_Type NT_GetType(const char* name, size_t name_len);
483 
491 void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo* arr, size_t count);
492 
500 void NT_DisposeEntryInfoArray(struct NT_EntryInfo* arr, size_t count);
501 
508 void NT_DisposeRpcDefinition(struct NT_RpcDefinition* def);
509 
516 void NT_DisposeRpcCallInfo(struct NT_RpcCallInfo* call_info);
517 
518 /* timestamp */
519 unsigned long long NT_Now(void);
520 
521 /* logging */
522 typedef void (*NT_LogFunc)(unsigned int level, const char* file,
523  unsigned int line, const char* msg);
524 void NT_SetLogger(NT_LogFunc func, unsigned int min_level);
525 
526 /*
527  * Interop Utility Functions
528  */
529 
530 /* Memory Allocators */
531 
543 char* NT_AllocateCharArray(size_t size);
544 
556 int* NT_AllocateBooleanArray(size_t size);
557 
569 double* NT_AllocateDoubleArray(size_t size);
570 
582 struct NT_String* NT_AllocateStringArray(size_t size);
583 
589 void NT_FreeCharArray(char* v_char);
590 
596 void NT_FreeDoubleArray(double* v_double);
597 
603 void NT_FreeBooleanArray(int* v_boolean);
604 
615 void NT_FreeStringArray(struct NT_String* v_string, size_t arr_size);
616 
624 enum NT_Type NT_GetValueType(const struct NT_Value* value);
625 
635 int NT_GetValueBoolean(const struct NT_Value* value,
636  unsigned long long* last_change, int* v_boolean);
637 
647 int NT_GetValueDouble(const struct NT_Value* value,
648  unsigned long long* last_change, double* v_double);
649 
664 char* NT_GetValueString(const struct NT_Value* value,
665  unsigned long long* last_change, size_t* str_len);
666 
681 char* NT_GetValueRaw(const struct NT_Value* value,
682  unsigned long long* last_change, size_t* raw_len);
683 
698 int* NT_GetValueBooleanArray(const struct NT_Value* value,
699  unsigned long long* last_change, size_t* arr_size);
700 
715 double* NT_GetValueDoubleArray(const struct NT_Value* value,
716  unsigned long long* last_change,
717  size_t* arr_size);
718 
735 NT_String* NT_GetValueStringArray(const struct NT_Value* value,
736  unsigned long long* last_change,
737  size_t* arr_size);
738 
751 int NT_GetEntryBoolean(const char* name, size_t name_len,
752  unsigned long long* last_change, int* v_boolean);
753 
766 int NT_GetEntryDouble(const char* name, size_t name_len,
767  unsigned long long* last_change, double* v_double);
768 
783 char* NT_GetEntryString(const char* name, size_t name_len,
784  unsigned long long* last_change, size_t* str_len);
785 
800 char* NT_GetEntryRaw(const char* name, size_t name_len,
801  unsigned long long* last_change, size_t* raw_len);
802 
817 int* NT_GetEntryBooleanArray(const char* name, size_t name_len,
818  unsigned long long* last_change, size_t* arr_size);
819 
834 double* NT_GetEntryDoubleArray(const char* name, size_t name_len,
835  unsigned long long* last_change,
836  size_t* arr_size);
837 
855 NT_String* NT_GetEntryStringArray(const char* name, size_t name_len,
856  unsigned long long* last_change,
857  size_t* arr_size);
858 
859 /* Set Default Values */
860 
871 int NT_SetDefaultEntryBoolean(const char* name, size_t name_len,
872  int default_boolean);
873 
884 int NT_SetDefaultEntryDouble(const char* name, size_t name_len,
885  double default_double);
886 
898 int NT_SetDefaultEntryString(const char* name, size_t name_len,
899  const char* default_value, size_t default_len);
900 
912 int NT_SetDefaultEntryRaw(const char* name, size_t name_len,
913  const char* default_value, size_t default_len);
914 
926 int NT_SetDefaultEntryBooleanArray(const char* name, size_t name_len,
927  const int* default_value,
928  size_t default_size);
929 
941 int NT_SetDefaultEntryDoubleArray(const char* name, size_t name_len,
942  const double* default_value,
943  size_t default_size);
944 
956 int NT_SetDefaultEntryStringArray(const char* name, size_t name_len,
957  const struct NT_String* default_value,
958  size_t default_size);
959 
960 /* Entry Value Setters */
961 
972 int NT_SetEntryBoolean(const char* name, size_t name_len, int v_boolean,
973  int force);
974 
985 int NT_SetEntryDouble(const char* name, size_t name_len, double v_double,
986  int force);
987 
999 int NT_SetEntryString(const char* name, size_t name_len, const char* str,
1000  size_t str_len, int force);
1001 
1013 int NT_SetEntryRaw(const char* name, size_t name_len, const char* raw,
1014  size_t raw_len, int force);
1015 
1027 int NT_SetEntryBooleanArray(const char* name, size_t name_len, const int* arr,
1028  size_t size, int force);
1029 
1041 int NT_SetEntryDoubleArray(const char* name, size_t name_len, const double* arr,
1042  size_t size, int force);
1043 
1055 int NT_SetEntryStringArray(const char* name, size_t name_len,
1056  const struct NT_String* arr, size_t size, int force);
1057 
1058 #ifdef __cplusplus
1059 }
1060 #endif
1061 
1062 #endif /* NTCORE_C_H_ */
NetworkTables RPC Call Data.
Definition: ntcore_c.h:150
NetworkTables RPC Result Definition.
Definition: ntcore_c.h:134
NetworkTables Connection Information.
Definition: ntcore_c.h:119
NetworkTables Entry Value.
Definition: ntcore_c.h:80
NetworkTables Entry Information.
Definition: ntcore_c.h:104
unsigned int flags
Entry flags.
Definition: ntcore_c.h:112
unsigned long long last_change
Timestamp of last change to entry (type or value).
Definition: ntcore_c.h:115
struct NT_String name
Entry name.
Definition: ntcore_c.h:106
A NetworkTables string.
Definition: ntcore_c.h:65
NetworkTables RPC Definition.
Definition: ntcore_c.h:140
NetworkTables RPC Parameter Definition.
Definition: ntcore_c.h:128
char * str
String contents (UTF-8).
Definition: ntcore_c.h:71
size_t len
Length of the string in bytes.
Definition: ntcore_c.h:76
enum NT_Type type
Entry type.
Definition: ntcore_c.h:109