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 
61 enum NT_NetworkMode {
62  NT_NET_MODE_NONE = 0x00, /* not running */
63  NT_NET_MODE_SERVER = 0x01, /* running in server mode */
64  NT_NET_MODE_CLIENT = 0x02, /* running in client mode */
65  NT_NET_MODE_STARTING = 0x04, /* flag for starting (either client or server) */
66  NT_NET_MODE_FAILURE = 0x08, /* flag for failure (either client or server) */
67 };
68 
69 /*
70  * Structures
71  */
72 
74 struct NT_String {
80  char* str;
81 
85  size_t len;
86 };
87 
89 struct NT_Value {
90  enum NT_Type type;
91  unsigned long long last_change;
92  union {
93  int v_boolean;
94  double v_double;
95  struct NT_String v_string;
96  struct NT_String v_raw;
97  struct {
98  int* arr;
99  size_t size;
100  } arr_boolean;
101  struct {
102  double* arr;
103  size_t size;
104  } arr_double;
105  struct {
106  struct NT_String* arr;
107  size_t size;
108  } arr_string;
109  } data;
110 };
111 
113 struct NT_EntryInfo {
115  struct NT_String name;
116 
118  enum NT_Type type;
119 
121  unsigned int flags;
122 
124  unsigned long long last_change;
125 };
126 
129  struct NT_String remote_id;
130  struct NT_String remote_ip;
131  unsigned int remote_port;
132  unsigned long long last_update;
133  unsigned int protocol_version;
134 };
135 
138  struct NT_String name;
139  struct NT_Value def_value;
140 };
141 
144  struct NT_String name;
145  enum NT_Type type;
146 };
147 
150  unsigned int version;
151  struct NT_String name;
152  size_t num_params;
153  NT_RpcParamDef* params;
154  size_t num_results;
155  NT_RpcResultDef* results;
156 };
157 
160  unsigned int rpc_id;
161  unsigned int call_uid;
162  struct NT_String name;
163  struct NT_String params;
164 };
165 
166 /*
167  * Table Functions
168  */
169 
182 void NT_GetEntryValue(const char* name, size_t name_len,
183  struct NT_Value* value);
184 
195 int NT_SetDefaultEntryValue(const char* name, size_t name_len,
196  const struct NT_Value* default_value);
197 
207 int NT_SetEntryValue(const char* name, size_t name_len,
208  const struct NT_Value* value);
209 
222 void NT_SetEntryTypeValue(const char* name, size_t name_len,
223  const struct NT_Value* value);
224 
227 void NT_SetEntryFlags(const char* name, size_t name_len, unsigned int flags);
228 
231 unsigned int NT_GetEntryFlags(const char* name, size_t name_len);
232 
245 void NT_DeleteEntry(const char* name, size_t name_len);
246 
256 void NT_DeleteAllEntries(void);
257 
272 struct NT_EntryInfo* NT_GetEntryInfo(const char* prefix, size_t prefix_len,
273  unsigned int types, size_t* count);
274 
284 void NT_Flush(void);
285 
286 /*
287  * Callback Creation Functions
288  */
289 
290 void NT_SetListenerOnStart(void (*on_start)(void* data), void* data);
291 void NT_SetListenerOnExit(void (*on_exit)(void* data), void* data);
292 
293 typedef void (*NT_EntryListenerCallback)(unsigned int uid, void* data,
294  const char* name, size_t name_len,
295  const struct NT_Value* value,
296  unsigned int flags);
297 
298 typedef void (*NT_ConnectionListenerCallback)(
299  unsigned int uid, void* data, int connected,
300  const struct NT_ConnectionInfo* conn);
301 
302 unsigned int NT_AddEntryListener(const char* prefix, size_t prefix_len,
303  void* data, NT_EntryListenerCallback callback,
304  unsigned int flags);
305 void NT_RemoveEntryListener(unsigned int entry_listener_uid);
306 unsigned int NT_AddConnectionListener(void* data,
307  NT_ConnectionListenerCallback callback,
308  int immediate_notify);
309 void NT_RemoveConnectionListener(unsigned int conn_listener_uid);
310 
311 int NT_NotifierDestroyed();
312 
313 /*
314  * Remote Procedure Call Functions
315  */
316 
317 void NT_SetRpcServerOnStart(void (*on_start)(void* data), void* data);
318 void NT_SetRpcServerOnExit(void (*on_exit)(void* data), void* data);
319 
320 typedef char* (*NT_RpcCallback)(void* data, const char* name, size_t name_len,
321  const char* params, size_t params_len,
322  size_t* results_len,
323  const struct NT_ConnectionInfo* conn_info);
324 
325 void NT_CreateRpc(const char* name, size_t name_len, const char* def,
326  size_t def_len, void* data, NT_RpcCallback callback);
327 void NT_CreatePolledRpc(const char* name, size_t name_len, const char* def,
328  size_t def_len);
329 
330 int NT_PollRpc(int blocking, struct NT_RpcCallInfo* call_info);
331 int NT_PollRpcTimeout(int blocking, double time_out,
332  struct NT_RpcCallInfo* call_info);
333 void NT_PostRpcResponse(unsigned int rpc_id, unsigned int call_uid,
334  const char* result, size_t result_len);
335 
336 unsigned int NT_CallRpc(const char* name, size_t name_len, const char* params,
337  size_t params_len);
338 char* NT_GetRpcResult(int blocking, unsigned int call_uid, size_t* result_len);
339 char* NT_GetRpcResultTimeout(int blocking, unsigned int call_uid,
340  double time_out, size_t* result_len);
341 void NT_CancelBlockingRpcResult(unsigned int call_uid);
342 
343 char* NT_PackRpcDefinition(const struct NT_RpcDefinition* def,
344  size_t* packed_len);
345 int NT_UnpackRpcDefinition(const char* packed, size_t packed_len,
346  struct NT_RpcDefinition* def);
347 char* NT_PackRpcValues(const struct NT_Value** values, size_t values_len,
348  size_t* packed_len);
349 struct NT_Value** NT_UnpackRpcValues(const char* packed, size_t packed_len,
350  const NT_Type* types, size_t types_len);
351 
352 /*
353  * Client/Server Functions
354  */
355 void NT_SetNetworkIdentity(const char* name, size_t name_len);
356 
360 unsigned int NT_GetNetworkMode();
361 
371 void NT_StartServer(const char* persist_filename, const char* listen_address,
372  unsigned int port);
373 
377 void NT_StopServer(void);
378 
382 void NT_StartClientNone(void);
383 
391 void NT_StartClient(const char* server_name, unsigned int port);
392 
403 void NT_StartClientMulti(size_t count, const char** server_names,
404  const unsigned int* ports);
405 
409 void NT_StopClient(void);
410 
417 void NT_SetServer(const char* server_name, unsigned int port);
418 
428 void NT_SetServerMulti(size_t count, const char** server_names,
429  const unsigned int* ports);
430 
437 void NT_StartDSClient(unsigned int port);
438 
440 void NT_StopDSClient(void);
441 
445 void NT_StopRpcServer(void);
446 
450 void NT_StopNotifier(void);
451 
458 void NT_SetUpdateRate(double interval);
459 
469 struct NT_ConnectionInfo* NT_GetConnections(size_t* count);
470 
471 /*
472  * Persistent Functions
473  */
474 /* return error string, or NULL if successful */
475 const char* NT_SavePersistent(const char* filename);
476 const char* NT_LoadPersistent(const char* filename,
477  void (*warn)(size_t line, const char* msg));
478 
479 /*
480  * Utility Functions
481  */
482 
483 /* frees value memory */
484 void NT_DisposeValue(struct NT_Value* value);
485 
486 /* sets type to unassigned and clears rest of struct */
487 void NT_InitValue(struct NT_Value* value);
488 
489 /* frees string memory */
490 void NT_DisposeString(struct NT_String* str);
491 
492 /* sets length to zero and pointer to null */
493 void NT_InitString(struct NT_String* str);
494 
495 /* Gets the type for the specified key, or unassigned if non existent. */
496 enum NT_Type NT_GetType(const char* name, size_t name_len);
497 
505 void NT_DisposeConnectionInfoArray(struct NT_ConnectionInfo* arr, size_t count);
506 
514 void NT_DisposeEntryInfoArray(struct NT_EntryInfo* arr, size_t count);
515 
522 void NT_DisposeRpcDefinition(struct NT_RpcDefinition* def);
523 
530 void NT_DisposeRpcCallInfo(struct NT_RpcCallInfo* call_info);
531 
532 /* timestamp */
533 unsigned long long NT_Now(void);
534 
535 /* logging */
536 typedef void (*NT_LogFunc)(unsigned int level, const char* file,
537  unsigned int line, const char* msg);
538 void NT_SetLogger(NT_LogFunc func, unsigned int min_level);
539 
540 /*
541  * Interop Utility Functions
542  */
543 
544 /* Memory Allocators */
545 
557 char* NT_AllocateCharArray(size_t size);
558 
570 int* NT_AllocateBooleanArray(size_t size);
571 
583 double* NT_AllocateDoubleArray(size_t size);
584 
596 struct NT_String* NT_AllocateStringArray(size_t size);
597 
603 void NT_FreeCharArray(char* v_char);
604 
610 void NT_FreeDoubleArray(double* v_double);
611 
617 void NT_FreeBooleanArray(int* v_boolean);
618 
629 void NT_FreeStringArray(struct NT_String* v_string, size_t arr_size);
630 
638 enum NT_Type NT_GetValueType(const struct NT_Value* value);
639 
649 int NT_GetValueBoolean(const struct NT_Value* value,
650  unsigned long long* last_change, int* v_boolean);
651 
661 int NT_GetValueDouble(const struct NT_Value* value,
662  unsigned long long* last_change, double* v_double);
663 
678 char* NT_GetValueString(const struct NT_Value* value,
679  unsigned long long* last_change, size_t* str_len);
680 
695 char* NT_GetValueRaw(const struct NT_Value* value,
696  unsigned long long* last_change, size_t* raw_len);
697 
712 int* NT_GetValueBooleanArray(const struct NT_Value* value,
713  unsigned long long* last_change, size_t* arr_size);
714 
729 double* NT_GetValueDoubleArray(const struct NT_Value* value,
730  unsigned long long* last_change,
731  size_t* arr_size);
732 
749 NT_String* NT_GetValueStringArray(const struct NT_Value* value,
750  unsigned long long* last_change,
751  size_t* arr_size);
752 
765 int NT_GetEntryBoolean(const char* name, size_t name_len,
766  unsigned long long* last_change, int* v_boolean);
767 
780 int NT_GetEntryDouble(const char* name, size_t name_len,
781  unsigned long long* last_change, double* v_double);
782 
797 char* NT_GetEntryString(const char* name, size_t name_len,
798  unsigned long long* last_change, size_t* str_len);
799 
814 char* NT_GetEntryRaw(const char* name, size_t name_len,
815  unsigned long long* last_change, size_t* raw_len);
816 
831 int* NT_GetEntryBooleanArray(const char* name, size_t name_len,
832  unsigned long long* last_change, size_t* arr_size);
833 
848 double* NT_GetEntryDoubleArray(const char* name, size_t name_len,
849  unsigned long long* last_change,
850  size_t* arr_size);
851 
869 NT_String* NT_GetEntryStringArray(const char* name, size_t name_len,
870  unsigned long long* last_change,
871  size_t* arr_size);
872 
873 /* Set Default Values */
874 
885 int NT_SetDefaultEntryBoolean(const char* name, size_t name_len,
886  int default_boolean);
887 
898 int NT_SetDefaultEntryDouble(const char* name, size_t name_len,
899  double default_double);
900 
912 int NT_SetDefaultEntryString(const char* name, size_t name_len,
913  const char* default_value, size_t default_len);
914 
926 int NT_SetDefaultEntryRaw(const char* name, size_t name_len,
927  const char* default_value, size_t default_len);
928 
940 int NT_SetDefaultEntryBooleanArray(const char* name, size_t name_len,
941  const int* default_value,
942  size_t default_size);
943 
955 int NT_SetDefaultEntryDoubleArray(const char* name, size_t name_len,
956  const double* default_value,
957  size_t default_size);
958 
970 int NT_SetDefaultEntryStringArray(const char* name, size_t name_len,
971  const struct NT_String* default_value,
972  size_t default_size);
973 
974 /* Entry Value Setters */
975 
986 int NT_SetEntryBoolean(const char* name, size_t name_len, int v_boolean,
987  int force);
988 
999 int NT_SetEntryDouble(const char* name, size_t name_len, double v_double,
1000  int force);
1001 
1013 int NT_SetEntryString(const char* name, size_t name_len, const char* str,
1014  size_t str_len, int force);
1015 
1027 int NT_SetEntryRaw(const char* name, size_t name_len, const char* raw,
1028  size_t raw_len, int force);
1029 
1041 int NT_SetEntryBooleanArray(const char* name, size_t name_len, const int* arr,
1042  size_t size, int force);
1043 
1055 int NT_SetEntryDoubleArray(const char* name, size_t name_len, const double* arr,
1056  size_t size, int force);
1057 
1069 int NT_SetEntryStringArray(const char* name, size_t name_len,
1070  const struct NT_String* arr, size_t size, int force);
1071 
1072 #ifdef __cplusplus
1073 }
1074 #endif
1075 
1076 #endif /* NTCORE_C_H_ */
NetworkTables RPC Call Data.
Definition: ntcore_c.h:159
NetworkTables RPC Result Definition.
Definition: ntcore_c.h:143
NetworkTables Connection Information.
Definition: ntcore_c.h:128
NetworkTables Entry Value.
Definition: ntcore_c.h:89
NetworkTables Entry Information.
Definition: ntcore_c.h:113
unsigned int flags
Entry flags.
Definition: ntcore_c.h:121
unsigned long long last_change
Timestamp of last change to entry (type or value).
Definition: ntcore_c.h:124
struct NT_String name
Entry name.
Definition: ntcore_c.h:115
A NetworkTables string.
Definition: ntcore_c.h:74
NetworkTables RPC Definition.
Definition: ntcore_c.h:149
NetworkTables RPC Parameter Definition.
Definition: ntcore_c.h:137
char * str
String contents (UTF-8).
Definition: ntcore_c.h:80
size_t len
Length of the string in bytes.
Definition: ntcore_c.h:85
enum NT_Type type
Entry type.
Definition: ntcore_c.h:118