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 {
35  NT_PERSISTENT = 0x01
36 };
37 
39 enum NT_LogLevel {
40  NT_LOG_CRITICAL = 50,
41  NT_LOG_ERROR = 40,
42  NT_LOG_WARNING = 30,
43  NT_LOG_INFO = 20,
44  NT_LOG_DEBUG = 10,
45  NT_LOG_DEBUG1 = 9,
46  NT_LOG_DEBUG2 = 8,
47  NT_LOG_DEBUG3 = 7,
48  NT_LOG_DEBUG4 = 6
49 };
50 
52 enum NT_NotifyKind {
53  NT_NOTIFY_NONE = 0,
54  NT_NOTIFY_IMMEDIATE = 0x01, /* initial listener addition */
55  NT_NOTIFY_LOCAL = 0x02, /* changed locally */
56  NT_NOTIFY_NEW = 0x04, /* newly created entry */
57  NT_NOTIFY_DELETE = 0x08, /* deleted */
58  NT_NOTIFY_UPDATE = 0x10, /* value changed */
59  NT_NOTIFY_FLAGS = 0x20 /* flags changed */
60 };
61 
62 /*
63  * Structures
64  */
65 
67 struct NT_String {
73  char *str;
74 
78  size_t len;
79 };
80 
82 struct NT_Value {
83  enum NT_Type type;
84  unsigned long long last_change;
85  union {
86  int v_boolean;
87  double v_double;
88  struct NT_String v_string;
89  struct NT_String v_raw;
90  struct {
91  int *arr;
92  size_t size;
93  } arr_boolean;
94  struct {
95  double *arr;
96  size_t size;
97  } arr_double;
98  struct {
99  struct NT_String *arr;
100  size_t size;
101  } arr_string;
102  } data;
103 };
104 
106 struct NT_EntryInfo {
108  struct NT_String name;
109 
111  enum NT_Type type;
112 
114  unsigned int flags;
115 
117  unsigned long long last_change;
118 };
119 
122  struct NT_String remote_id;
123  char *remote_name;
124  unsigned int remote_port;
125  unsigned long long last_update;
126  unsigned int protocol_version;
127 };
128 
131  struct NT_String name;
132  struct NT_Value def_value;
133 };
134 
137  struct NT_String name;
138  enum NT_Type type;
139 };
140 
143  unsigned int version;
144  struct NT_String name;
145  size_t num_params;
146  NT_RpcParamDef *params;
147  size_t num_results;
148  NT_RpcResultDef *results;
149 };
150 
153  unsigned int rpc_id;
154  unsigned int call_uid;
155  struct NT_String name;
156  struct NT_String params;
157 };
158 
159 /*
160  * Table Functions
161  */
162 
175 void NT_GetEntryValue(const char *name, size_t name_len,
176  struct NT_Value *value);
177 
187 int NT_SetEntryValue(const char *name, size_t name_len,
188  const struct NT_Value *value);
189 
202 void NT_SetEntryTypeValue(const char *name, size_t name_len,
203  const struct NT_Value *value);
204 
207 void NT_SetEntryFlags(const char *name, size_t name_len, unsigned int flags);
208 
211 unsigned int NT_GetEntryFlags(const char *name, size_t name_len);
212 
225 void NT_DeleteEntry(const char *name, size_t name_len);
226 
236 void NT_DeleteAllEntries(void);
237 
252 struct NT_EntryInfo *NT_GetEntryInfo(const char *prefix, size_t prefix_len,
253  unsigned int types, size_t *count);
254 
264 void NT_Flush(void);
265 
266 /*
267  * Callback Creation Functions
268  */
269 
270 void NT_SetListenerOnStart(void (*on_start)(void *data), void *data);
271 void NT_SetListenerOnExit(void (*on_exit)(void *data), void *data);
272 
273 typedef void (*NT_EntryListenerCallback)(
274  unsigned int uid, void *data, const char *name, size_t name_len,
275  const struct NT_Value *value, unsigned int flags);
276 
277 typedef void (*NT_ConnectionListenerCallback)(
278  unsigned int uid, void *data, int connected,
279  const struct NT_ConnectionInfo *conn);
280 
281 unsigned int NT_AddEntryListener(const char *prefix, size_t prefix_len,
282  void *data, NT_EntryListenerCallback callback,
283  unsigned int flags);
284 void NT_RemoveEntryListener(unsigned int entry_listener_uid);
285 unsigned int NT_AddConnectionListener(void *data,
286  NT_ConnectionListenerCallback callback,
287  int immediate_notify);
288 void NT_RemoveConnectionListener(unsigned int conn_listener_uid);
289 
290 int NT_NotifierDestroyed();
291 
292 /*
293  * Remote Procedure Call Functions
294  */
295 
296 void NT_SetRpcServerOnStart(void (*on_start)(void *data), void *data);
297 void NT_SetRpcServerOnExit(void (*on_exit)(void *data), void *data);
298 
299 typedef char *(*NT_RpcCallback)(void *data, const char *name, size_t name_len,
300  const char *params, size_t params_len,
301  size_t *results_len);
302 
303 void NT_CreateRpc(const char *name, size_t name_len, const char *def,
304  size_t def_len, void *data, NT_RpcCallback callback);
305 void NT_CreatePolledRpc(const char *name, size_t name_len, const char *def,
306  size_t def_len);
307 
308 int NT_PollRpc(int blocking, struct NT_RpcCallInfo* call_info);
309 void NT_PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
310  const char *result, size_t result_len);
311 
312 unsigned int NT_CallRpc(const char *name, size_t name_len, const char *params,
313  size_t params_len);
314 char *NT_GetRpcResult(int blocking, unsigned int call_uid, size_t *result_len);
315 
316 char *NT_PackRpcDefinition(const struct NT_RpcDefinition *def,
317  size_t *packed_len);
318 int NT_UnpackRpcDefinition(const char *packed, size_t packed_len,
319  struct NT_RpcDefinition *def);
320 char *NT_PackRpcValues(const struct NT_Value **values, size_t values_len,
321  size_t *packed_len);
322 struct NT_Value **NT_UnpackRpcValues(const char *packed, size_t packed_len,
323  const NT_Type *types, size_t types_len);
324 
325 /*
326  * Client/Server Functions
327  */
328 void NT_SetNetworkIdentity(const char *name, size_t name_len);
329 
339 void NT_StartServer(const char *persist_filename, const char *listen_address,
340  unsigned int port);
341 
345 void NT_StopServer(void);
346 
354 void NT_StartClient(const char *server_name, unsigned int port);
355 
359 void NT_StopClient(void);
360 
364 void NT_StopRpcServer(void);
365 
369 void NT_StopNotifier(void);
370 
377 void NT_SetUpdateRate(double interval);
378 
388 struct NT_ConnectionInfo *NT_GetConnections(size_t *count);
389 
390 /*
391  * Persistent Functions
392  */
393 /* return error string, or NULL if successful */
394 const char *NT_SavePersistent(const char *filename);
395 const char *NT_LoadPersistent(const char *filename,
396  void (*warn)(size_t line, const char *msg));
397 
398 /*
399  * Utility Functions
400  */
401 
402 /* frees value memory */
403 void NT_DisposeValue(struct NT_Value *value);
404 
405 /* sets type to unassigned and clears rest of struct */
406 void NT_InitValue(struct NT_Value *value);
407 
408 /* frees string memory */
409 void NT_DisposeString(struct NT_String *str);
410 
411 /* sets length to zero and pointer to null */
412 void NT_InitString(struct NT_String *str);
413 
414 /* Gets the type for the specified key, or unassigned if non existent. */
415 enum NT_Type NT_GetType(const char *name, size_t name_len);
416 
424 void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo *arr, size_t count);
425 
433 void NT_DisposeEntryInfoArray(struct NT_EntryInfo *arr, size_t count);
434 
441 void NT_DisposeRpcDefinition(struct NT_RpcDefinition *def);
442 
449 void NT_DisposeRpcCallInfo(struct NT_RpcCallInfo *call_info);
450 
451 /* timestamp */
452 unsigned long long NT_Now(void);
453 
454 /* logging */
455 typedef void (*NT_LogFunc)(unsigned int level, const char *file,
456  unsigned int line, const char *msg);
457 void NT_SetLogger(NT_LogFunc func, unsigned int min_level);
458 
459 /*
460  * Interop Utility Functions
461  */
462 
463 /* Memory Allocators */
464 
476 char *NT_AllocateCharArray(size_t size);
477 
489 int *NT_AllocateBooleanArray(size_t size);
490 
502 double *NT_AllocateDoubleArray(size_t size);
503 
515 struct NT_String *NT_AllocateStringArray(size_t size);
516 
522 void NT_FreeCharArray(char *v_char);
523 
529 void NT_FreeDoubleArray(double *v_double);
530 
536 void NT_FreeBooleanArray(int *v_boolean);
537 
548 void NT_FreeStringArray(struct NT_String *v_string, size_t arr_size);
549 
557 enum NT_Type NT_GetValueType(const struct NT_Value *value);
558 
568 int NT_GetValueBoolean(const struct NT_Value *value,
569  unsigned long long *last_change, int *v_boolean);
570 
580 int NT_GetValueDouble(const struct NT_Value *value,
581  unsigned long long *last_change, double *v_double);
582 
597 char *NT_GetValueString(const struct NT_Value *value,
598  unsigned long long *last_change, size_t *str_len);
599 
614 char *NT_GetValueRaw(const struct NT_Value *value,
615  unsigned long long *last_change, size_t *raw_len);
616 
631 int *NT_GetValueBooleanArray(const struct NT_Value *value,
632  unsigned long long *last_change, size_t *arr_size);
633 
648 double *NT_GetValueDoubleArray(const struct NT_Value *value,
649  unsigned long long *last_change,
650  size_t *arr_size);
651 
668 NT_String *NT_GetValueStringArray(const struct NT_Value *value,
669  unsigned long long *last_change,
670  size_t *arr_size);
671 
684 int NT_GetEntryBoolean(const char *name, size_t name_len,
685  unsigned long long *last_change, int *v_boolean);
686 
699 int NT_GetEntryDouble(const char *name, size_t name_len,
700  unsigned long long *last_change, double *v_double);
701 
716 char *NT_GetEntryString(const char *name, size_t name_len,
717  unsigned long long *last_change, size_t *str_len);
718 
733 char *NT_GetEntryRaw(const char *name, size_t name_len,
734  unsigned long long *last_change, size_t *raw_len);
735 
750 int *NT_GetEntryBooleanArray(const char *name, size_t name_len,
751  unsigned long long *last_change, size_t *arr_size);
752 
767 double *NT_GetEntryDoubleArray(const char *name, size_t name_len,
768  unsigned long long *last_change,
769  size_t *arr_size);
770 
788 NT_String *NT_GetEntryStringArray(const char *name, size_t name_len,
789  unsigned long long *last_change,
790  size_t *arr_size);
791 
792 /* Entry Value Setters */
793 
804 int NT_SetEntryBoolean(const char *name, size_t name_len, int v_boolean,
805  int force);
806 
817 int NT_SetEntryDouble(const char *name, size_t name_len, double v_double,
818  int force);
819 
831 int NT_SetEntryString(const char *name, size_t name_len, const char *str,
832  size_t str_len, int force);
833 
845 int NT_SetEntryRaw(const char *name, size_t name_len, const char *raw,
846  size_t raw_len, int force);
847 
859 int NT_SetEntryBooleanArray(const char *name, size_t name_len, const int *arr,
860  size_t size, int force);
861 
873 int NT_SetEntryDoubleArray(const char *name, size_t name_len, const double *arr,
874  size_t size, int force);
875 
887 int NT_SetEntryStringArray(const char *name, size_t name_len,
888  const struct NT_String *arr, size_t size, int force);
889 
890 #ifdef __cplusplus
891 }
892 #endif
893 
894 #endif /* NTCORE_C_H_ */
NetworkTables RPC Call Data.
Definition: ntcore_c.h:152
NetworkTables RPC Result Definition.
Definition: ntcore_c.h:136
NetworkTables Connection Information.
Definition: ntcore_c.h:121
NetworkTables Entry Value.
Definition: ntcore_c.h:82
NetworkTables Entry Information.
Definition: ntcore_c.h:106
unsigned int flags
Entry flags.
Definition: ntcore_c.h:114
unsigned long long last_change
Timestamp of last change to entry (type or value).
Definition: ntcore_c.h:117
struct NT_String name
Entry name.
Definition: ntcore_c.h:108
A NetworkTables string.
Definition: ntcore_c.h:67
NetworkTables RPC Definition.
Definition: ntcore_c.h:142
NetworkTables RPC Parameter Definition.
Definition: ntcore_c.h:130
char * str
String contents (UTF-8).
Definition: ntcore_c.h:73
size_t len
Length of the string in bytes.
Definition: ntcore_c.h:78
enum NT_Type type
Entry type.
Definition: ntcore_c.h:111