WPILibC++ 2024.3.2
ntcore_cpp.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <cassert>
10#include <functional>
11#include <memory>
12#include <optional>
13#include <span>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <variant>
18#include <vector>
19
20#include <wpi/json_fwd.h>
21
23#include "ntcore_c.h"
24#include "ntcore_cpp_types.h"
25
26namespace wpi {
27template <typename T>
28class SmallVectorImpl;
29} // namespace wpi
30
31namespace wpi::log {
32class DataLog;
33} // namespace wpi::log
34
35/** NetworkTables (ntcore) namespace */
36namespace nt {
37
38/**
39 * @defgroup ntcore_cpp_handle_api ntcore C++ API
40 *
41 * Handle-based interface for C++.
42 *
43 * @{
44 */
45
46/**
47 * Event notification flags.
48 *
49 * The flags are a bitmask and must be OR'ed together to indicate the
50 * combination of events desired to be received.
51 *
52 */
53struct EventFlags {
54 EventFlags() = delete;
55
56 static constexpr unsigned int kNone = NT_EVENT_NONE;
57 /**
58 * Initial listener addition.
59 * Set this flag to receive immediate notification of matches to the
60 * flag criteria.
61 */
62 static constexpr unsigned int kImmediate = NT_EVENT_IMMEDIATE;
63 /** Client connected (on server, any client connected). */
64 static constexpr unsigned int kConnected = NT_EVENT_CONNECTED;
65 /** Client disconnected (on server, any client disconnected). */
66 static constexpr unsigned int kDisconnected = NT_EVENT_DISCONNECTED;
67 /** Any connection event (connect or disconnect). */
68 static constexpr unsigned int kConnection = kConnected | kDisconnected;
69 /** New topic published. */
70 static constexpr unsigned int kPublish = NT_EVENT_PUBLISH;
71 /** Topic unpublished. */
72 static constexpr unsigned int kUnpublish = NT_EVENT_UNPUBLISH;
73 /** Topic properties changed. */
74 static constexpr unsigned int kProperties = NT_EVENT_PROPERTIES;
75 /** Any topic event (publish, unpublish, or properties changed). */
76 static constexpr unsigned int kTopic = kPublish | kUnpublish | kProperties;
77 /** Topic value updated (via network). */
78 static constexpr unsigned int kValueRemote = NT_EVENT_VALUE_REMOTE;
79 /** Topic value updated (local). */
80 static constexpr unsigned int kValueLocal = NT_EVENT_VALUE_LOCAL;
81 /** Topic value updated (network or local). */
82 static constexpr unsigned int kValueAll = kValueRemote | kValueLocal;
83 /** Log message. */
84 static constexpr unsigned int kLogMessage = NT_EVENT_LOGMESSAGE;
85 /** Time synchronized with server. */
86 static constexpr unsigned int kTimeSync = NT_EVENT_TIMESYNC;
87};
88
89/** NetworkTables Topic Information */
90struct TopicInfo {
91 /** Topic handle */
93
94 /** Topic name */
95 std::string name;
96
97 /** Topic type */
99
100 /** Topic type string */
101 std::string type_str;
102
103 /** Topic properties JSON string */
104 std::string properties;
105
106 /** Get topic properties as a JSON object. */
107 wpi::json GetProperties() const;
108
109 friend void swap(TopicInfo& first, TopicInfo& second) {
110 using std::swap;
111 swap(first.topic, second.topic);
112 swap(first.name, second.name);
113 swap(first.type, second.type);
114 swap(first.type_str, second.type_str);
115 swap(first.properties, second.properties);
116 }
117};
118
119/** NetworkTables Connection Information */
121 /**
122 * The remote identifier (as set on the remote node by
123 * NetworkTableInstance::StartClient4() or nt::StartClient4()).
124 */
125 std::string remote_id;
126
127 /** The IP address of the remote node. */
128 std::string remote_ip;
129
130 /** The port number of the remote node. */
131 unsigned int remote_port{0};
132
133 /**
134 * The last time any update was received from the remote node (same scale as
135 * returned by nt::Now()).
136 */
137 int64_t last_update{0};
138
139 /**
140 * The protocol version being used for this connection. This in protocol
141 * layer format, so 0x0200 = 2.0, 0x0300 = 3.0).
142 */
143 unsigned int protocol_version{0};
144
145 friend void swap(ConnectionInfo& first, ConnectionInfo& second) {
146 using std::swap;
147 swap(first.remote_id, second.remote_id);
148 swap(first.remote_ip, second.remote_ip);
149 swap(first.remote_port, second.remote_port);
150 swap(first.last_update, second.last_update);
151 swap(first.protocol_version, second.protocol_version);
152 }
153};
154
155/** NetworkTables Value Event Data */
157 public:
158 ValueEventData() = default;
160 : topic{topic}, subentry{subentry}, value{std::move(value)} {}
161
162 /** Topic handle. */
164
165 /** Subscriber/entry handle. */
167
168 /** The new value. */
170};
171
172/** NetworkTables log message. */
174 public:
175 LogMessage() = default;
176 LogMessage(unsigned int level, std::string_view filename, unsigned int line,
179
180 /** Log level of the message. See NT_LogLevel. */
181 unsigned int level{0};
182
183 /** The filename of the source file that generated the message. */
184 std::string filename;
185
186 /** The line number in the source file that generated the message. */
187 unsigned int line{0};
188
189 /** The message. */
190 std::string message;
191};
192
193/** NetworkTables time sync event data. */
195 public:
196 TimeSyncEventData() = default;
199
200 /**
201 * Offset between local time and server time, in microseconds. Add this value
202 * to local time to get the estimated equivalent server time.
203 */
205
206 /** Measured round trip time divided by 2, in microseconds. */
207 int64_t rtt2;
208
209 /**
210 * If serverTimeOffset and RTT are valid. An event with this set to false is
211 * sent when the client disconnects.
212 */
213 bool valid;
214};
215
216/** NetworkTables event */
217class Event {
218 public:
219 Event() = default;
221 : listener{listener}, flags{flags}, data{std::move(info)} {}
223 : listener{listener}, flags{flags}, data{std::move(info)} {}
225 : listener{listener}, flags{flags}, data{std::move(data)} {}
227 : listener{listener}, flags{flags}, data{std::move(msg)} {}
229 NT_Handle subentry, Value value)
231 flags{flags},
232 data{ValueEventData{topic, subentry, std::move(value)}} {}
233 Event(NT_Listener listener, unsigned int flags, unsigned int level,
234 std::string_view filename, unsigned int line, std::string_view message)
236 flags{flags},
237 data{LogMessage{level, filename, line, message}} {}
238 Event(NT_Listener listener, unsigned int flags, int64_t serverTimeOffset,
239 int64_t rtt2, bool valid)
241 flags{flags},
242 data{TimeSyncEventData{serverTimeOffset, rtt2, valid}} {}
243
244 /** Listener that triggered this event. */
246
247 /**
248 * Event flags (NT_EventFlags). Also indicates the data included with the
249 * event:
250 * - NT_EVENT_CONNECTED or NT_EVENT_DISCONNECTED: GetConnectionInfo()
251 * - NT_EVENT_PUBLISH, NT_EVENT_UNPUBLISH, or NT_EVENT_PROPERTIES:
252 * GetTopicInfo()
253 * - NT_EVENT_VALUE, NT_EVENT_VALUE_LOCAL: GetValueData()
254 * - NT_EVENT_LOGMESSAGE: GetLogMessage()
255 * - NT_EVENT_TIMESYNC: GetTimeSyncEventData()
256 */
257 unsigned int flags{0};
258
259 /**
260 * Test event flags.
261 *
262 * @param kind event flag(s) to test
263 * @return True if flags matches kind
264 */
265 bool Is(unsigned int kind) const { return (flags & kind) != 0; }
266
267 /** Event data; content depends on flags. */
271
273 return std::get_if<ConnectionInfo>(&data);
274 }
276 return std::get_if<ConnectionInfo>(&data);
277 }
278
279 const TopicInfo* GetTopicInfo() const {
280 return std::get_if<TopicInfo>(&data);
281 }
282 TopicInfo* GetTopicInfo() { return std::get_if<TopicInfo>(&data); }
283
285 return std::get_if<ValueEventData>(&data);
286 }
288 return std::get_if<ValueEventData>(&data);
289 }
290
291 const LogMessage* GetLogMessage() const {
292 return std::get_if<LogMessage>(&data);
293 }
294 LogMessage* GetLogMessage() { return std::get_if<LogMessage>(&data); }
295
297 return std::get_if<TimeSyncEventData>(&data);
298 }
300 return std::get_if<TimeSyncEventData>(&data);
301 }
302};
303
304/** NetworkTables publish/subscribe options. */
306 /**
307 * Default value of periodic.
308 */
309 static constexpr double kDefaultPeriodic = 0.1;
310
311 /**
312 * Structure size. Must be set to sizeof(PubSubOptions).
313 */
314 unsigned int structSize = sizeof(PubSubOptions);
315
316 /**
317 * Polling storage size for a subscription. Specifies the maximum number of
318 * updates NetworkTables should store between calls to the subscriber's
319 * ReadQueue() function. If zero, defaults to 1 if sendAll is false, 20 if
320 * sendAll is true.
321 */
322 unsigned int pollStorage = 0;
323
324 /**
325 * How frequently changes will be sent over the network, in seconds.
326 * NetworkTables may send more frequently than this (e.g. use a combined
327 * minimum period for all values) or apply a restricted range to this value.
328 * The default is 100 ms.
329 */
331
332 /**
333 * For subscriptions, if non-zero, value updates for ReadQueue() are not
334 * queued for this publisher.
335 */
337
338 /**
339 * Send all value changes over the network.
340 */
341 bool sendAll = false;
342
343 /**
344 * For subscriptions, don't ask for value changes (only topic announcements).
345 */
346 bool topicsOnly = false;
347
348 /**
349 * Preserve duplicate value changes (rather than ignoring them).
350 */
351 bool keepDuplicates = false;
352
353 /**
354 * Perform prefix match on subscriber topic names. Is ignored/overridden by
355 * Subscribe() functions; only present in struct for the purposes of getting
356 * information about subscriptions.
357 */
358 bool prefixMatch = false;
359
360 /**
361 * For subscriptions, if remote value updates should not be queued for
362 * ReadQueue(). See also disableLocal.
363 */
364 bool disableRemote = false;
365
366 /**
367 * For subscriptions, if local value updates should not be queued for
368 * ReadQueue(). See also disableRemote.
369 */
370 bool disableLocal = false;
371
372 /**
373 * For entries, don't queue (for ReadQueue) value updates for the entry's
374 * internal publisher.
375 */
376 bool excludeSelf = false;
377
378 /**
379 * For subscriptions, don't share the existence of the subscription with the
380 * network. Note this means updates will not be received from the network
381 * unless another subscription overlaps with this one, and the subscription
382 * will not appear in metatopics.
383 */
384 bool hidden = false;
385};
386
387/**
388 * Default publish/subscribe options.
389 */
391
392/**
393 * @defgroup ntcore_instance_func Instance Functions
394 * @{
395 */
396
397/**
398 * Get default instance.
399 * This is the instance used by non-handle-taking functions.
400 *
401 * @return Instance handle
402 */
404
405/**
406 * Create an instance.
407 *
408 * @return Instance handle
409 */
411
412/**
413 * Reset the internals of an instance. Every handle previously associated
414 * with this instance will no longer be valid, except for the instance
415 * handle.
416 */
418
419/**
420 * Destroy an instance.
421 * The default instance cannot be destroyed.
422 *
423 * @param inst Instance handle
424 */
426
427/**
428 * Get instance handle from another handle.
429 *
430 * @param handle entry/instance/etc. handle
431 * @return Instance handle
432 */
434
435/** @} */
436
437/**
438 * @defgroup ntcore_table_func Table Functions
439 * @{
440 */
441
442/**
443 * Get Entry Handle.
444 *
445 * @param inst instance handle
446 * @param name entry name (UTF-8 string)
447 * @return entry handle
448 */
450
451/**
452 * Gets the name of the specified entry.
453 * Returns an empty string if the handle is invalid.
454 *
455 * @param entry entry handle
456 * @return Entry name
457 */
458std::string GetEntryName(NT_Entry entry);
459
460/**
461 * Gets the type for the specified entry, or unassigned if non existent.
462 *
463 * @param entry entry handle
464 * @return Entry type
465 */
467
468/**
469 * Gets the last time the entry was changed.
470 * Returns 0 if the handle is invalid.
471 *
472 * @param subentry subscriber or entry handle
473 * @return Entry last change time
474 */
476
477/**
478 * Get Entry Value.
479 *
480 * Returns copy of current entry value.
481 * Note that one of the type options is "unassigned".
482 *
483 * @param subentry subscriber or entry handle
484 * @return entry value
485 */
487
488/**
489 * Set Default Entry Value
490 *
491 * Returns copy of current entry value if it exists.
492 * Otherwise, sets passed in value, and returns set value.
493 * Note that one of the type options is "unassigned".
494 *
495 * @param entry entry handle
496 * @param value value to be set if name does not exist
497 * @return False on error (value not set), True on success
498 */
499bool SetDefaultEntryValue(NT_Entry entry, const Value& value);
500
501/**
502 * Set Entry Value.
503 *
504 * Sets new entry value. If type of new value differs from the type of the
505 * currently stored entry, returns error and does not update value.
506 *
507 * @param entry entry handle
508 * @param value new entry value
509 * @return False on error (type mismatch), True on success
510 */
511bool SetEntryValue(NT_Entry entry, const Value& value);
512
513/**
514 * Set Entry Flags.
515 *
516 * @param entry entry handle
517 * @param flags flags value (bitmask of NT_EntryFlags)
518 */
519void SetEntryFlags(NT_Entry entry, unsigned int flags);
520
521/**
522 * Get Entry Flags.
523 *
524 * @param entry entry handle
525 * @return Flags value (bitmask of NT_EntryFlags)
526 */
527unsigned int GetEntryFlags(NT_Entry entry);
528
529/**
530 * Read Entry Queue.
531 *
532 * Returns new entry values since last call.
533 *
534 * @param subentry subscriber or entry handle
535 * @return entry value array
536 */
537std::vector<Value> ReadQueueValue(NT_Handle subentry);
538
539/**
540 * Read Entry Queue.
541 *
542 * Returns new entry values since last call.
543 *
544 * @param subentry subscriber or entry handle
545 * @param types bitmask of NT_Type values; 0 is treated specially
546 * as a "don't care"
547 * @return entry value array
548 */
549std::vector<Value> ReadQueueValue(NT_Handle subentry, unsigned int types);
550
551/** @} */
552
553/**
554 * @defgroup ntcore_topic_func Topic Functions
555 * @{
556 */
557
558/**
559 * Get Published Topics.
560 *
561 * Returns an array of topic handles. The results are optionally filtered by
562 * string prefix and type to only return a subset of all topics.
563 *
564 * @param inst instance handle
565 * @param prefix name required prefix; only topics whose name
566 * starts with this string are returned
567 * @param types bitmask of NT_Type values; 0 is treated specially
568 * as a "don't care"
569 * @return Array of topic handles.
570 */
571std::vector<NT_Topic> GetTopics(NT_Inst inst, std::string_view prefix,
572 unsigned int types);
573
574/**
575 * Get Published Topics.
576 *
577 * Returns an array of topic handles. The results are optionally filtered by
578 * string prefix and type to only return a subset of all topics.
579 *
580 * @param inst instance handle
581 * @param prefix name required prefix; only topics whose name
582 * starts with this string are returned
583 * @param types array of type strings
584 * @return Array of topic handles.
585 */
586std::vector<NT_Topic> GetTopics(NT_Inst inst, std::string_view prefix,
587 std::span<const std::string_view> types);
588
589/**
590 * Get Topic Information about multiple topics.
591 *
592 * Returns an array of topic information (handle, name, type, and properties).
593 * The results are optionally filtered by string prefix and type to only
594 * return a subset of all topics.
595 *
596 * @param inst instance handle
597 * @param prefix name required prefix; only topics whose name
598 * starts with this string are returned
599 * @param types bitmask of NT_Type values; 0 is treated specially
600 * as a "don't care"
601 * @return Array of topic information.
602 */
603std::vector<TopicInfo> GetTopicInfo(NT_Inst inst, std::string_view prefix,
604 unsigned int types);
605
606/**
607 * Get Topic Information about multiple topics.
608 *
609 * Returns an array of topic information (handle, name, type, and properties).
610 * The results are optionally filtered by string prefix and type to only
611 * return a subset of all topics.
612 *
613 * @param inst instance handle
614 * @param prefix name required prefix; only topics whose name
615 * starts with this string are returned
616 * @param types array of type strings
617 * @return Array of topic information.
618 */
619std::vector<TopicInfo> GetTopicInfo(NT_Inst inst, std::string_view prefix,
620 std::span<const std::string_view> types);
621
622/**
623 * Gets Topic Information.
624 *
625 * Returns information about a topic (name, type, and properties).
626 *
627 * @param topic handle
628 * @return Topic information.
629 */
631
632/**
633 * Gets Topic Handle.
634 *
635 * Returns topic handle.
636 *
637 * @param inst instance handle
638 * @param name topic name
639 * @return Topic handle.
640 */
642
643/**
644 * Gets the name of the specified topic.
645 * Returns an empty string if the handle is invalid.
646 *
647 * @param topic topic handle
648 * @return Topic name
649 */
650std::string GetTopicName(NT_Topic topic);
651
652/**
653 * Gets the type for the specified topic, or unassigned if non existent.
654 *
655 * @param topic topic handle
656 * @return Topic type
657 */
659
660/**
661 * Gets the type string for the specified topic, or empty string if non
662 * existent. This may have more information than the numeric type (especially
663 * for raw values).
664 *
665 * @param topic topic handle
666 * @return Topic type string
667 */
668std::string GetTopicTypeString(NT_Topic topic);
669
670/**
671 * Sets the persistent property of a topic. If true, the stored value is
672 * persistent through server restarts.
673 *
674 * @param topic topic handle
675 * @param value True for persistent, false for not persistent.
676 */
677void SetTopicPersistent(NT_Topic topic, bool value);
678
679/**
680 * Gets the persistent property of a topic. If true, the server retains the
681 * topic even when there are no publishers.
682 *
683 * @param topic topic handle
684 * @return persistent property value
685 */
687
688/**
689 * Sets the retained property of a topic.
690 *
691 * @param topic topic handle
692 * @param value new retained property value
693 */
694void SetTopicRetained(NT_Topic topic, bool value);
695
696/**
697 * Gets the retained property of a topic.
698 *
699 * @param topic topic handle
700 * @return retained property value
701 */
703
704/**
705 * Sets the cached property of a topic. If true, the server and clients will
706 * store the latest value, allowing the value to be read (and not just accessed
707 * through event queues and listeners).
708 *
709 * @param topic topic handle
710 * @param value True for cached, false for not cached
711 */
712void SetTopicCached(NT_Topic topic, bool value);
713
714/**
715 * Gets the cached property of a topic.
716 *
717 * @param topic topic handle
718 * @return cached property value
719 */
721
722/**
723 * Determine if topic exists (e.g. has at least one publisher).
724 *
725 * @param handle Topic, entry, or subscriber handle.
726 * @return True if topic exists.
727 */
729
730/**
731 * Gets the current value of a property (as a JSON object).
732 *
733 * @param topic topic handle
734 * @param name property name
735 * @return JSON object; null object if the property does not exist.
736 */
738
739/**
740 * Sets a property value.
741 *
742 * @param topic topic handle
743 * @param name property name
744 * @param value property value
745 */
747 const wpi::json& value);
748
749/**
750 * Deletes a property. Has no effect if the property does not exist.
751 *
752 * @param topic topic handle
753 * @param name property name
754 */
756
757/**
758 * Gets all topic properties as a JSON object. Each key in the object
759 * is the property name, and the corresponding value is the property value.
760 *
761 * @param topic topic handle
762 * @return JSON object
763 */
765
766/**
767 * Updates multiple topic properties. Each key in the passed-in object is
768 * the name of the property to add/update, and the corresponding value is the
769 * property value to set for that property. Null values result in deletion
770 * of the corresponding property.
771 *
772 * @param topic topic handle
773 * @param update JSON object with keys to add/update/delete
774 * @return False if update is not a JSON object
775 */
776bool SetTopicProperties(NT_Topic topic, const wpi::json& update);
777
778/**
779 * Creates a new subscriber to value changes on a topic.
780 *
781 * @param topic topic handle
782 * @param type expected type
783 * @param typeStr expected type string
784 * @param options subscription options
785 * @return Subscriber handle
786 */
788 const PubSubOptions& options = kDefaultPubSubOptions);
789
790/**
791 * Stops subscriber.
792 *
793 * @param sub subscriber handle
794 */
796
797/**
798 * Creates a new publisher to a topic.
799 *
800 * @param topic topic handle
801 * @param type type
802 * @param typeStr type string
803 * @param options publish options
804 * @return Publisher handle
805 */
807 const PubSubOptions& options = kDefaultPubSubOptions);
808
809/**
810 * Creates a new publisher to a topic.
811 *
812 * @param topic topic handle
813 * @param type type
814 * @param typeStr type string
815 * @param properties initial properties
816 * @param options publish options
817 * @return Publisher handle
818 */
820 const wpi::json& properties,
821 const PubSubOptions& options = kDefaultPubSubOptions);
822
823/**
824 * Stops publisher.
825 *
826 * @param pubentry publisher/entry handle
827 */
828void Unpublish(NT_Handle pubentry);
829
830/**
831 * @brief Creates a new entry (subscriber and weak publisher) to a topic.
832 *
833 * @param topic topic handle
834 * @param type type
835 * @param typeStr type string
836 * @param options publish options
837 * @return Entry handle
838 */
840 const PubSubOptions& options = kDefaultPubSubOptions);
841
842/**
843 * Stops entry subscriber/publisher.
844 *
845 * @param entry entry handle
846 */
848
849/**
850 * Stops entry/subscriber/publisher.
851 *
852 * @param pubsubentry entry/subscriber/publisher handle
853 */
854void Release(NT_Handle pubsubentry);
855
856/**
857 * Gets the topic handle from an entry/subscriber/publisher handle.
858 *
859 * @param pubsubentry entry/subscriber/publisher handle
860 * @return Topic handle
861 */
863
864/** @} */
865
866/**
867 * @defgroup ntcore_advancedsub_func Advanced Subscriber Functions
868 * @{
869 */
870
871/**
872 * Subscribes to multiple topics based on one or more topic name prefixes. Can
873 * be used in combination with a Value Listener or ReadQueueValue() to get value
874 * changes across all matching topics.
875 *
876 * @param inst instance handle
877 * @param prefixes topic name prefixes
878 * @param options subscriber options
879 * @return subscriber handle
880 */
882 NT_Inst inst, std::span<const std::string_view> prefixes,
883 const PubSubOptions& options = kDefaultPubSubOptions);
884
885/**
886 * Unsubscribes a multi-subscriber.
887 *
888 * @param sub multi-subscriber handle
889 */
891
892/** @} */
893
894/**
895 * @defgroup ntcore_listener_func Listener Functions
896 * @{
897 */
898
899using ListenerCallback = std::function<void(const Event&)>;
900
901/**
902 * Creates a listener poller.
903 *
904 * A poller provides a single queue of poll events. Events linked to this
905 * poller (using AddPolledListener()) will be stored in the queue and
906 * must be collected by calling ReadListenerQueue().
907 * The returned handle must be destroyed with DestroyListenerPoller().
908 *
909 * @param inst instance handle
910 * @return poller handle
911 */
913
914/**
915 * Destroys a listener poller. This will abort any blocked polling
916 * call and prevent additional events from being generated for this poller.
917 *
918 * @param poller poller handle
919 */
921
922/**
923 * Read notifications.
924 *
925 * @param poller poller handle
926 * @return Array of events. Returns empty array if no events since last call.
927 */
928std::vector<Event> ReadListenerQueue(NT_ListenerPoller poller);
929
930/**
931 * Removes a listener.
932 *
933 * @param listener Listener handle to remove
934 */
936
937/**
938 * Wait for the listener queue to be empty. This is primarily useful
939 * for deterministic testing. This blocks until either the listener
940 * queue is empty (e.g. there are no more events that need to be passed along to
941 * callbacks or poll queues) or the timeout expires.
942 *
943 * @param handle handle
944 * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
945 * negative value to block indefinitely
946 * @return False if timed out, otherwise true.
947 */
948bool WaitForListenerQueue(NT_Handle handle, double timeout);
949
950/**
951 * Create a listener for changes to topics with names that start with any of
952 * the given prefixes. This creates a corresponding internal subscriber with the
953 * lifetime of the listener.
954 *
955 * @param inst Instance handle
956 * @param prefixes Topic name string prefixes
957 * @param mask Bitmask of NT_EventFlags values (only topic and value events will
958 * be generated)
959 * @param callback Listener function
960 */
962 std::span<const std::string_view> prefixes,
963 unsigned int mask, ListenerCallback callback);
964
965/**
966 * Create a listener.
967 *
968 * Some combinations of handle and mask have no effect:
969 * - connection and log message events are only generated on instances
970 * - topic and value events are only generated on non-instances
971 *
972 * Adding value and topic events on a topic will create a corresponding internal
973 * subscriber with the lifetime of the listener.
974 *
975 * Adding a log message listener through this function will only result in
976 * events at NT_LOG_INFO or higher; for more customized settings, use
977 * AddLogger().
978 *
979 * @param handle Instance, topic, subscriber, multi-subscriber, or entry handle
980 * @param mask Bitmask of NT_EventFlags values
981 * @param callback Listener function
982 */
983NT_Listener AddListener(NT_Handle handle, unsigned int mask,
984 ListenerCallback callback);
985
986/**
987 * Creates a polled listener. This creates a corresponding internal subscriber
988 * with the lifetime of the listener.
989 * The caller is responsible for calling ReadListenerQueue() to poll.
990 *
991 * @param poller poller handle
992 * @param prefixes array of UTF-8 string prefixes
993 * @param mask Bitmask of NT_EventFlags values (only topic and value events will
994 * be generated)
995 * @return Listener handle
996 */
998 std::span<const std::string_view> prefixes,
999 unsigned int mask);
1000
1001/**
1002 * Creates a polled listener.
1003 * The caller is responsible for calling ReadListenerQueue() to poll.
1004 *
1005 * Some combinations of handle and mask have no effect:
1006 * - connection and log message events are only generated on instances
1007 * - topic and value events are only generated on non-instances
1008 *
1009 * Adding value and topic events on a topic will create a corresponding internal
1010 * subscriber with the lifetime of the listener.
1011 *
1012 * Adding a log message listener through this function will only result in
1013 * events at NT_LOG_INFO or higher; for more customized settings, use
1014 * AddPolledLogger().
1015 *
1016 * @param poller poller handle
1017 * @param handle instance, topic, subscriber, multi-subscriber, or entry handle
1018 * @param mask NT_EventFlags bitmask
1019 * @return Listener handle
1020 */
1022 unsigned int mask);
1023
1024/** @} */
1025
1026/**
1027 * @defgroup ntcore_network_func Client/Server Functions
1028 * @{
1029 */
1030
1031/**
1032 * Get the current network mode.
1033 *
1034 * @param inst instance handle
1035 * @return Bitmask of NT_NetworkMode.
1036 */
1037unsigned int GetNetworkMode(NT_Inst inst);
1038
1039/**
1040 * Starts local-only operation. Prevents calls to StartServer or StartClient
1041 * from taking effect. Has no effect if StartServer or StartClient
1042 * has already been called.
1043 */
1045
1046/**
1047 * Stops local-only operation. StartServer or StartClient can be called after
1048 * this call to start a server or client.
1049 */
1051
1052/**
1053 * Starts a server using the specified filename, listening address, and port.
1054 *
1055 * @param inst instance handle
1056 * @param persist_filename the name of the persist file to use (UTF-8 string,
1057 * null terminated)
1058 * @param listen_address the address to listen on, or null to listen on any
1059 * address. (UTF-8 string, null terminated)
1060 * @param port3 port to communicate over (NT3)
1061 * @param port4 port to communicate over (NT4)
1062 */
1063void StartServer(NT_Inst inst, std::string_view persist_filename,
1064 const char* listen_address, unsigned int port3,
1065 unsigned int port4);
1066
1067/**
1068 * Stops the server if it is running.
1069 *
1070 * @param inst instance handle
1071 */
1073
1074/**
1075 * Starts a NT3 client. Use SetServer or SetServerTeam to set the server name
1076 * and port.
1077 *
1078 * @param inst instance handle
1079 * @param identity network identity to advertise (cannot be empty string)
1080 */
1082
1083/**
1084 * Starts a NT4 client. Use SetServer or SetServerTeam to set the server name
1085 * and port.
1086 *
1087 * @param inst instance handle
1088 * @param identity network identity to advertise (cannot be empty string)
1089 */
1091
1092/**
1093 * Stops the client if it is running.
1094 *
1095 * @param inst instance handle
1096 */
1098
1099/**
1100 * Sets server address and port for client (without restarting client).
1101 *
1102 * @param inst instance handle
1103 * @param server_name server name (UTF-8 string, null terminated)
1104 * @param port port to communicate over
1105 */
1106void SetServer(NT_Inst inst, const char* server_name, unsigned int port);
1107
1108/**
1109 * Sets server addresses for client (without restarting client).
1110 * The client will attempt to connect to each server in round robin fashion.
1111 *
1112 * @param inst instance handle
1113 * @param servers array of server name and port pairs
1114 */
1116 NT_Inst inst,
1117 std::span<const std::pair<std::string_view, unsigned int>> servers);
1118
1119/**
1120 * Sets server addresses and port for client (without restarting client).
1121 * Connects using commonly known robot addresses for the specified team.
1122 *
1123 * @param inst instance handle
1124 * @param team team number
1125 * @param port port to communicate over
1126 */
1127void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port);
1128
1129/**
1130 * Disconnects the client if it's running and connected. This will automatically
1131 * start reconnection attempts to the current server list.
1132 *
1133 * @param inst instance handle
1134 */
1136
1137/**
1138 * Starts requesting server address from Driver Station.
1139 * This connects to the Driver Station running on localhost to obtain the
1140 * server IP address.
1141 *
1142 * @param inst instance handle
1143 * @param port server port to use in combination with IP from DS
1144 */
1145void StartDSClient(NT_Inst inst, unsigned int port);
1146
1147/**
1148 * Stops requesting server address from Driver Station.
1149 *
1150 * @param inst instance handle
1151 */
1153
1154/**
1155 * Flush local updates.
1156 *
1157 * Forces an immediate flush of all local changes to the client/server.
1158 * This does not flush to the network.
1159 *
1160 * Normally this is done on a regularly scheduled interval.
1161 *
1162 * @param inst instance handle
1163 */
1165
1166/**
1167 * Flush to network.
1168 *
1169 * Forces an immediate flush of all local entry changes to network.
1170 * Normally this is done on a regularly scheduled interval (set
1171 * by update rates on individual publishers).
1172 *
1173 * Note: flushes are rate limited to avoid excessive network traffic. If
1174 * the time between calls is too short, the flush will occur after the minimum
1175 * time elapses (rather than immediately).
1176 *
1177 * @param inst instance handle
1178 */
1179void Flush(NT_Inst inst);
1180
1181/**
1182 * Get information on the currently established network connections.
1183 * If operating as a client, this will return either zero or one values.
1184 *
1185 * @param inst instance handle
1186 * @return array of connection information
1187 */
1188std::vector<ConnectionInfo> GetConnections(NT_Inst inst);
1189
1190/**
1191 * Return whether or not the instance is connected to another node.
1192 *
1193 * @param inst instance handle
1194 * @return True if connected.
1195 */
1197
1198/**
1199 * Get the time offset between server time and local time. Add this value to
1200 * local time to get the estimated equivalent server time. In server mode, this
1201 * always returns 0. In client mode, this returns the time offset only if the
1202 * client and server are connected and have exchanged synchronization messages.
1203 * Note the time offset may change over time as it is periodically updated; to
1204 * receive updates as events, add a listener to the "time sync" event.
1205 *
1206 * @param inst instance handle
1207 * @return Time offset in microseconds (optional)
1208 */
1209std::optional<int64_t> GetServerTimeOffset(NT_Inst inst);
1210
1211/** @} */
1212
1213/**
1214 * @defgroup ntcore_utility_func Utility Functions
1215 * @{
1216 */
1217
1218/**
1219 * Returns monotonic current time in 1 us increments.
1220 * This is the same time base used for value and connection timestamps.
1221 * This function by default simply wraps wpi::Now(), but if SetNow() is
1222 * called, this function instead returns the value passed to SetNow();
1223 * this can be used to reduce overhead.
1224 *
1225 * @return Timestamp
1226 */
1227int64_t Now();
1228
1229/**
1230 * Sets the current timestamp used for timestamping values that do not
1231 * provide a timestamp (e.g. a value of 0 is passed). For consistency,
1232 * it also results in Now() returning the set value. This should generally
1233 * be used only if the overhead of calling wpi::Now() is a concern.
1234 * If used, it should be called periodically with the value of wpi::Now().
1235 *
1236 * @param timestamp timestamp (1 us increments)
1237 */
1238void SetNow(int64_t timestamp);
1239
1240/**
1241 * Turns a type string into a type enum value.
1242 *
1243 * @param typeString type string
1244 * @return Type value
1245 */
1247
1248/**
1249 * Turns a type enum value into a type string.
1250 *
1251 * @param type type enum
1252 * @return Type string
1253 */
1255
1256/** @} */
1257
1258/**
1259 * @defgroup ntcore_data_logger_func Data Logger Functions
1260 * @{
1261 */
1262
1263/**
1264 * Starts logging entry changes to a DataLog.
1265 *
1266 * @param inst instance handle
1267 * @param log data log object; lifetime must extend until StopEntryDataLog is
1268 * called or the instance is destroyed
1269 * @param prefix only store entries with names that start with this prefix;
1270 * the prefix is not included in the data log entry name
1271 * @param logPrefix prefix to add to data log entry names
1272 * @return Data logger handle
1273 */
1275 std::string_view prefix,
1276 std::string_view logPrefix);
1277
1278/**
1279 * Stops logging entry changes to a DataLog.
1280 *
1281 * @param logger data logger handle
1282 */
1284
1285/**
1286 * Starts logging connection changes to a DataLog.
1287 *
1288 * @param inst instance handle
1289 * @param log data log object; lifetime must extend until StopConnectionDataLog
1290 * is called or the instance is destroyed
1291 * @param name data log entry name
1292 * @return Data logger handle
1293 */
1296 std::string_view name);
1297
1298/**
1299 * Stops logging connection changes to a DataLog.
1300 *
1301 * @param logger data logger handle
1302 */
1304
1305/** @} */
1306
1307/**
1308 * @defgroup ntcore_logger_func Logger Functions
1309 * @{
1310 */
1311
1312/**
1313 * Add logger callback function. By default, log messages are sent to stderr;
1314 * this function sends log messages to the provided callback function instead.
1315 * The callback function will only be called for log messages with level
1316 * greater than or equal to min_level and less than or equal to max_level;
1317 * messages outside this range will be silently ignored.
1318 *
1319 * @param inst instance handle
1320 * @param min_level minimum log level
1321 * @param max_level maximum log level
1322 * @param func listener callback function
1323 * @return Listener handle
1324 */
1325NT_Listener AddLogger(NT_Inst inst, unsigned int min_level,
1326 unsigned int max_level, ListenerCallback func);
1327
1328/**
1329 * Set the log level for a log poller. Events will only be generated for
1330 * log messages with level greater than or equal to min_level and less than or
1331 * equal to max_level; messages outside this range will be silently ignored.
1332 *
1333 * @param poller poller handle
1334 * @param min_level minimum log level
1335 * @param max_level maximum log level
1336 * @return Logger handle
1337 */
1338NT_Listener AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level,
1339 unsigned int max_level);
1340
1341/** @} */
1342
1343/**
1344 * @defgroup ntcore_schema_func Schema Functions
1345 * @{
1346 */
1347
1348/**
1349 * Returns whether there is a data schema already registered with the given
1350 * name. This does NOT perform a check as to whether the schema has already
1351 * been published by another node on the network.
1352 *
1353 * @param inst instance
1354 * @param name Name (the string passed as the data type for topics using this
1355 * schema)
1356 * @return True if schema already registered
1357 */
1359
1360/**
1361 * Registers a data schema. Data schemas provide information for how a
1362 * certain data type string can be decoded. The type string of a data schema
1363 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
1364 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
1365 * published just like normal topics, with the name being generated from the
1366 * provided name: "/.schema/<name>". Duplicate calls to this function with
1367 * the same name are silently ignored.
1368 *
1369 * @param inst instance
1370 * @param name Name (the string passed as the data type for topics using this
1371 * schema)
1372 * @param type Type of schema (e.g. "protobuf", "struct", etc)
1373 * @param schema Schema data
1374 */
1376 std::span<const uint8_t> schema);
1377
1378/**
1379 * Registers a data schema. Data schemas provide information for how a
1380 * certain data type string can be decoded. The type string of a data schema
1381 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
1382 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
1383 * published just like normal topics, with the name being generated from the
1384 * provided name: "/.schema/<name>". Duplicate calls to this function with
1385 * the same name are silently ignored.
1386 *
1387 * @param inst instance
1388 * @param name Name (the string passed as the data type for topics using this
1389 * schema)
1390 * @param type Type of schema (e.g. "protobuf", "struct", etc)
1391 * @param schema Schema data
1392 */
1393inline void AddSchema(NT_Inst inst, std::string_view name,
1395 AddSchema(
1396 inst, name, type,
1397 std::span<const uint8_t>{reinterpret_cast<const uint8_t*>(schema.data()),
1398 schema.size()});
1399}
1400
1401/** @} */
1402/** @} */
1403
1404/**
1405 * NetworkTables meta-topic decoding functions.
1406 */
1407namespace meta {
1408
1409/**
1410 * @defgroup ntcore_cpp_meta_api ntcore C++ meta-topic API
1411 *
1412 * Meta-topic decoders for C++.
1413 *
1414 * @{
1415 */
1416
1417/**
1418 * Subscriber options. Different from PubSubOptions in this reflects only
1419 * options that are sent over the network.
1420 */
1422 double periodic = 0.1;
1423 bool topicsOnly = false;
1424 bool sendAll = false;
1425 bool prefixMatch = false;
1426 // std::string otherStr;
1427};
1428
1429/**
1430 * Topic publisher (as published via `$pub$<topic>`).
1431 */
1433 std::string client;
1434 uint64_t pubuid = 0;
1435};
1436
1437/**
1438 * Topic subscriber (as published via `$sub$<topic>`).
1439 */
1441 std::string client;
1442 uint64_t subuid = 0;
1444};
1445
1446/**
1447 * Client publisher (as published via `$clientpub$<client>` or `$serverpub`).
1448 */
1450 int64_t uid = -1;
1451 std::string topic;
1452};
1453
1454/**
1455 * Client subscriber (as published via `$clientsub$<client>` or `$serversub`).
1456 */
1458 int64_t uid = -1;
1459 std::vector<std::string> topics;
1461};
1462
1463/**
1464 * Client (as published via `$clients`).
1465 */
1466struct Client {
1467 std::string id;
1468 std::string conn;
1469 uint16_t version = 0;
1470};
1471
1472/**
1473 * Decodes `$pub$<topic>` meta-topic data.
1474 *
1475 * @param data data contents
1476 * @return Vector of TopicPublishers, or empty optional on decoding error.
1477 */
1478std::optional<std::vector<TopicPublisher>> DecodeTopicPublishers(
1479 std::span<const uint8_t> data);
1480
1481/**
1482 * Decodes `$sub$<topic>` meta-topic data.
1483 *
1484 * @param data data contents
1485 * @return Vector of TopicSubscribers, or empty optional on decoding error.
1486 */
1487std::optional<std::vector<TopicSubscriber>> DecodeTopicSubscribers(
1488 std::span<const uint8_t> data);
1489
1490/**
1491 * Decodes `$clientpub$<topic>` meta-topic data.
1492 *
1493 * @param data data contents
1494 * @return Vector of ClientPublishers, or empty optional on decoding error.
1495 */
1496std::optional<std::vector<ClientPublisher>> DecodeClientPublishers(
1497 std::span<const uint8_t> data);
1498
1499/**
1500 * Decodes `$clientsub$<topic>` meta-topic data.
1501 *
1502 * @param data data contents
1503 * @return Vector of ClientSubscribers, or empty optional on decoding error.
1504 */
1505std::optional<std::vector<ClientSubscriber>> DecodeClientSubscribers(
1506 std::span<const uint8_t> data);
1507
1508/**
1509 * Decodes `$clients` meta-topic data.
1510 *
1511 * @param data data contents
1512 * @return Vector of Clients, or empty optional on decoding error.
1513 */
1514std::optional<std::vector<Client>> DecodeClients(std::span<const uint8_t> data);
1515
1516/** @} */
1517
1518} // namespace meta
1519} // namespace nt
NetworkTables event.
Definition: ntcore_cpp.h:217
Event(NT_Listener listener, unsigned int flags, ValueEventData data)
Definition: ntcore_cpp.h:224
Event(NT_Listener listener, unsigned int flags, ConnectionInfo info)
Definition: ntcore_cpp.h:220
const LogMessage * GetLogMessage() const
Definition: ntcore_cpp.h:291
const TimeSyncEventData * GetTimeSyncEventData() const
Definition: ntcore_cpp.h:296
Event(NT_Listener listener, unsigned int flags, int64_t serverTimeOffset, int64_t rtt2, bool valid)
Definition: ntcore_cpp.h:238
TopicInfo * GetTopicInfo()
Definition: ntcore_cpp.h:282
ValueEventData * GetValueEventData()
Definition: ntcore_cpp.h:287
const ValueEventData * GetValueEventData() const
Definition: ntcore_cpp.h:284
LogMessage * GetLogMessage()
Definition: ntcore_cpp.h:294
Event(NT_Listener listener, unsigned int flags, unsigned int level, std::string_view filename, unsigned int line, std::string_view message)
Definition: ntcore_cpp.h:233
std::variant< ConnectionInfo, TopicInfo, ValueEventData, LogMessage, TimeSyncEventData > data
Event data; content depends on flags.
Definition: ntcore_cpp.h:270
bool Is(unsigned int kind) const
Test event flags.
Definition: ntcore_cpp.h:265
Event(NT_Listener listener, unsigned int flags, NT_Topic topic, NT_Handle subentry, Value value)
Definition: ntcore_cpp.h:228
unsigned int flags
Event flags (NT_EventFlags).
Definition: ntcore_cpp.h:257
const ConnectionInfo * GetConnectionInfo() const
Definition: ntcore_cpp.h:272
ConnectionInfo * GetConnectionInfo()
Definition: ntcore_cpp.h:275
Event()=default
Event(NT_Listener listener, unsigned int flags, TopicInfo info)
Definition: ntcore_cpp.h:222
NT_Listener listener
Listener that triggered this event.
Definition: ntcore_cpp.h:245
TimeSyncEventData * GetTimeSyncEventData()
Definition: ntcore_cpp.h:299
Event(NT_Listener listener, unsigned int flags, LogMessage msg)
Definition: ntcore_cpp.h:226
const TopicInfo * GetTopicInfo() const
Definition: ntcore_cpp.h:279
NetworkTables log message.
Definition: ntcore_cpp.h:173
unsigned int line
The line number in the source file that generated the message.
Definition: ntcore_cpp.h:187
LogMessage()=default
LogMessage(unsigned int level, std::string_view filename, unsigned int line, std::string_view message)
Definition: ntcore_cpp.h:176
unsigned int level
Log level of the message.
Definition: ntcore_cpp.h:181
std::string message
The message.
Definition: ntcore_cpp.h:190
std::string filename
The filename of the source file that generated the message.
Definition: ntcore_cpp.h:184
NetworkTables time sync event data.
Definition: ntcore_cpp.h:194
int64_t rtt2
Measured round trip time divided by 2, in microseconds.
Definition: ntcore_cpp.h:207
TimeSyncEventData()=default
int64_t serverTimeOffset
Offset between local time and server time, in microseconds.
Definition: ntcore_cpp.h:204
bool valid
If serverTimeOffset and RTT are valid.
Definition: ntcore_cpp.h:213
TimeSyncEventData(int64_t serverTimeOffset, int64_t rtt2, bool valid)
Definition: ntcore_cpp.h:197
NetworkTables Value Event Data.
Definition: ntcore_cpp.h:156
NT_Handle subentry
Subscriber/entry handle.
Definition: ntcore_cpp.h:166
ValueEventData()=default
ValueEventData(NT_Topic topic, NT_Handle subentry, Value value)
Definition: ntcore_cpp.h:159
NT_Topic topic
Topic handle.
Definition: ntcore_cpp.h:163
Value value
The new value.
Definition: ntcore_cpp.h:169
A network table entry value.
Definition: NetworkTableValue.h:32
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:579
A data log.
Definition: DataLog.h:90
basic_string_view< char > string_view
Definition: core.h:501
dimensionless::scalar_t log(const ScalarUnit x) noexcept
Compute natural logarithm.
Definition: math.h:349
NT_MultiSubscriber SubscribeMultiple(NT_Inst inst, std::span< const std::string_view > prefixes, const PubSubOptions &options=kDefaultPubSubOptions)
Subscribes to multiple topics based on one or more topic name prefixes.
void UnsubscribeMultiple(NT_MultiSubscriber sub)
Unsubscribes a multi-subscriber.
NT_Handle NT_Topic
Definition: ntcore_c.h:40
NT_Handle NT_ConnectionDataLogger
Definition: ntcore_c.h:33
NT_Handle NT_Listener
Definition: ntcore_c.h:37
NT_Handle NT_Subscriber
Definition: ntcore_c.h:41
unsigned int NT_Handle
Definition: ntcore_c.h:32
NT_Type
NetworkTables data types.
Definition: ntcore_c.h:51
NT_Handle NT_Inst
Definition: ntcore_c.h:36
NT_Handle NT_Publisher
Definition: ntcore_c.h:42
NT_Handle NT_ListenerPoller
Definition: ntcore_c.h:38
NT_Handle NT_MultiSubscriber
Definition: ntcore_c.h:39
NT_Handle NT_Entry
Definition: ntcore_c.h:35
NT_Handle NT_DataLogger
Definition: ntcore_c.h:34
@ NT_UNASSIGNED
Definition: ntcore_c.h:52
@ NT_EVENT_LOGMESSAGE
Log message.
Definition: ntcore_c.h:123
@ NT_EVENT_NONE
Definition: ntcore_c.h:99
@ NT_EVENT_UNPUBLISH
Topic unpublished.
Definition: ntcore_c.h:111
@ NT_EVENT_PROPERTIES
Topic properties changed.
Definition: ntcore_c.h:113
@ NT_EVENT_CONNECTED
Client connected (on server, any client connected).
Definition: ntcore_c.h:103
@ NT_EVENT_TIMESYNC
Time synchronized with server.
Definition: ntcore_c.h:125
@ NT_EVENT_VALUE_REMOTE
Topic value updated (via network).
Definition: ntcore_c.h:117
@ NT_EVENT_PUBLISH
New topic published.
Definition: ntcore_c.h:109
@ NT_EVENT_DISCONNECTED
Client disconnected (on server, any client disconnected).
Definition: ntcore_c.h:105
@ NT_EVENT_IMMEDIATE
Initial listener addition.
Definition: ntcore_c.h:101
@ NT_EVENT_VALUE_LOCAL
Topic value updated (local).
Definition: ntcore_c.h:119
constexpr PubSubOptions kDefaultPubSubOptions
Default publish/subscribe options.
Definition: ntcore_cpp.h:390
std::optional< std::vector< ClientSubscriber > > DecodeClientSubscribers(std::span< const uint8_t > data)
Decodes $clientsub$<topic> meta-topic data.
std::optional< std::vector< TopicSubscriber > > DecodeTopicSubscribers(std::span< const uint8_t > data)
Decodes $sub$<topic> meta-topic data.
std::optional< std::vector< ClientPublisher > > DecodeClientPublishers(std::span< const uint8_t > data)
Decodes $clientpub$<topic> meta-topic data.
std::optional< std::vector< TopicPublisher > > DecodeTopicPublishers(std::span< const uint8_t > data)
Decodes $pub$<topic> meta-topic data.
std::optional< std::vector< Client > > DecodeClients(std::span< const uint8_t > data)
Decodes $clients meta-topic data.
NT_ConnectionDataLogger StartConnectionDataLog(NT_Inst inst, wpi::log::DataLog &log, std::string_view name)
Starts logging connection changes to a DataLog.
void StopConnectionDataLog(NT_ConnectionDataLogger logger)
Stops logging connection changes to a DataLog.
NT_DataLogger StartEntryDataLog(NT_Inst inst, wpi::log::DataLog &log, std::string_view prefix, std::string_view logPrefix)
Starts logging entry changes to a DataLog.
void StopEntryDataLog(NT_DataLogger logger)
Stops logging entry changes to a DataLog.
NT_Inst GetInstanceFromHandle(NT_Handle handle)
Get instance handle from another handle.
void ResetInstance(NT_Inst inst)
Reset the internals of an instance.
NT_Inst CreateInstance()
Create an instance.
NT_Inst GetDefaultInstance()
Get default instance.
void DestroyInstance(NT_Inst inst)
Destroy an instance.
std::function< void(const Event &)> ListenerCallback
Definition: ntcore_cpp.h:899
NT_Listener AddPolledListener(NT_ListenerPoller poller, std::span< const std::string_view > prefixes, unsigned int mask)
Creates a polled listener.
NT_ListenerPoller CreateListenerPoller(NT_Inst inst)
Creates a listener poller.
bool WaitForListenerQueue(NT_Handle handle, double timeout)
Wait for the listener queue to be empty.
NT_Listener AddListener(NT_Inst inst, std::span< const std::string_view > prefixes, unsigned int mask, ListenerCallback callback)
Create a listener for changes to topics with names that start with any of the given prefixes.
void DestroyListenerPoller(NT_ListenerPoller poller)
Destroys a listener poller.
void RemoveListener(NT_Listener listener)
Removes a listener.
std::vector< Event > ReadListenerQueue(NT_ListenerPoller poller)
Read notifications.
NT_Listener AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level, unsigned int max_level)
Set the log level for a log poller.
NT_Listener AddLogger(NT_Inst inst, unsigned int min_level, unsigned int max_level, ListenerCallback func)
Add logger callback function.
void StartClient4(NT_Inst inst, std::string_view identity)
Starts a NT4 client.
void Disconnect(NT_Inst inst)
Disconnects the client if it's running and connected.
std::optional< int64_t > GetServerTimeOffset(NT_Inst inst)
Get the time offset between server time and local time.
unsigned int GetNetworkMode(NT_Inst inst)
Get the current network mode.
void StopLocal(NT_Inst inst)
Stops local-only operation.
void StopDSClient(NT_Inst inst)
Stops requesting server address from Driver Station.
bool IsConnected(NT_Inst inst)
Return whether or not the instance is connected to another node.
void StopServer(NT_Inst inst)
Stops the server if it is running.
void StartLocal(NT_Inst inst)
Starts local-only operation.
void Flush(NT_Inst inst)
Flush to network.
void SetServer(NT_Inst inst, const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
void StartClient3(NT_Inst inst, std::string_view identity)
Starts a NT3 client.
void StartDSClient(NT_Inst inst, unsigned int port)
Starts requesting server address from Driver Station.
void StopClient(NT_Inst inst)
Stops the client if it is running.
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
void StartServer(NT_Inst inst, std::string_view persist_filename, const char *listen_address, unsigned int port3, unsigned int port4)
Starts a server using the specified filename, listening address, and port.
std::vector< ConnectionInfo > GetConnections(NT_Inst inst)
Get information on the currently established network connections.
void FlushLocal(NT_Inst inst)
Flush local updates.
bool HasSchema(NT_Inst inst, std::string_view name)
Returns whether there is a data schema already registered with the given name.
void AddSchema(NT_Inst inst, std::string_view name, std::string_view type, std::span< const uint8_t > schema)
Registers a data schema.
NT_Type GetEntryType(NT_Entry entry)
Gets the type for the specified entry, or unassigned if non existent.
Value GetEntryValue(NT_Handle subentry)
Get Entry Value.
void SetEntryFlags(NT_Entry entry, unsigned int flags)
Set Entry Flags.
NT_Entry GetEntry(NT_Inst inst, std::string_view name)
Get Entry Handle.
int64_t GetEntryLastChange(NT_Handle subentry)
Gets the last time the entry was changed.
unsigned int GetEntryFlags(NT_Entry entry)
Get Entry Flags.
std::vector< Value > ReadQueueValue(NT_Handle subentry)
Read Entry Queue.
std::string GetEntryName(NT_Entry entry)
Gets the name of the specified entry.
bool SetEntryValue(NT_Entry entry, const Value &value)
Set Entry Value.
bool SetDefaultEntryValue(NT_Entry entry, const Value &value)
Set Default Entry Value.
NT_Topic GetTopicFromHandle(NT_Handle pubsubentry)
Gets the topic handle from an entry/subscriber/publisher handle.
void Unsubscribe(NT_Subscriber sub)
Stops subscriber.
void Release(NT_Handle pubsubentry)
Stops entry/subscriber/publisher.
bool GetTopicRetained(NT_Topic topic)
Gets the retained property of a topic.
bool GetTopicExists(NT_Handle handle)
Determine if topic exists (e.g.
wpi::json GetTopicProperties(NT_Topic topic)
Gets all topic properties as a JSON object.
NT_Type GetTopicType(NT_Topic topic)
Gets the type for the specified topic, or unassigned if non existent.
bool GetTopicCached(NT_Topic topic)
Gets the cached property of a topic.
std::string GetTopicTypeString(NT_Topic topic)
Gets the type string for the specified topic, or empty string if non existent.
std::string GetTopicName(NT_Topic topic)
Gets the name of the specified topic.
NT_Topic GetTopic(NT_Inst inst, std::string_view name)
Gets Topic Handle.
void SetTopicProperty(NT_Topic topic, std::string_view name, const wpi::json &value)
Sets a property value.
void SetTopicCached(NT_Topic topic, bool value)
Sets the cached property of a topic.
NT_Publisher PublishEx(NT_Topic topic, NT_Type type, std::string_view typeStr, const wpi::json &properties, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
void SetTopicRetained(NT_Topic topic, bool value)
Sets the retained property of a topic.
bool SetTopicProperties(NT_Topic topic, const wpi::json &update)
Updates multiple topic properties.
NT_Publisher Publish(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new publisher to a topic.
std::vector< NT_Topic > GetTopics(NT_Inst inst, std::string_view prefix, unsigned int types)
Get Published Topics.
std::vector< TopicInfo > GetTopicInfo(NT_Inst inst, std::string_view prefix, unsigned int types)
Get Topic Information about multiple topics.
void SetTopicPersistent(NT_Topic topic, bool value)
Sets the persistent property of a topic.
wpi::json GetTopicProperty(NT_Topic topic, std::string_view name)
Gets the current value of a property (as a JSON object).
void ReleaseEntry(NT_Entry entry)
Stops entry subscriber/publisher.
void Unpublish(NT_Handle pubentry)
Stops publisher.
NT_Subscriber Subscribe(NT_Topic topic, NT_Type type, std::string_view typeStr, const PubSubOptions &options=kDefaultPubSubOptions)
Creates a new subscriber to value changes on a topic.
void DeleteTopicProperty(NT_Topic topic, std::string_view name)
Deletes a property.
bool GetTopicPersistent(NT_Topic topic)
Gets the persistent property of a topic.
NT_Type GetTypeFromString(std::string_view typeString)
Turns a type string into a type enum value.
int64_t Now()
Returns monotonic current time in 1 us increments.
void SetNow(int64_t timestamp)
Sets the current timestamp used for timestamping values that do not provide a timestamp (e....
std::string_view GetStringFromType(NT_Type type)
Turns a type enum value into a type string.
const T & first(const T &value, const Tail &...)
Definition: compile.h:60
type
Definition: core.h:556
NetworkTables (ntcore) namespace.
Definition: ntcore_cpp.h:36
Definition: array.h:89
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
Definition: ntcore_cpp.h:31
Definition: ntcore_cpp.h:26
NetworkTables Connection Information.
Definition: ntcore_cpp.h:120
unsigned int protocol_version
The protocol version being used for this connection.
Definition: ntcore_cpp.h:143
unsigned int remote_port
The port number of the remote node.
Definition: ntcore_cpp.h:131
friend void swap(ConnectionInfo &first, ConnectionInfo &second)
Definition: ntcore_cpp.h:145
int64_t last_update
The last time any update was received from the remote node (same scale as returned by nt::Now()).
Definition: ntcore_cpp.h:137
std::string remote_id
The remote identifier (as set on the remote node by NetworkTableInstance::StartClient4() or nt::Start...
Definition: ntcore_cpp.h:125
std::string remote_ip
The IP address of the remote node.
Definition: ntcore_cpp.h:128
Event notification flags.
Definition: ntcore_cpp.h:53
static constexpr unsigned int kValueRemote
Topic value updated (via network).
Definition: ntcore_cpp.h:78
static constexpr unsigned int kValueAll
Topic value updated (network or local).
Definition: ntcore_cpp.h:82
static constexpr unsigned int kTopic
Any topic event (publish, unpublish, or properties changed).
Definition: ntcore_cpp.h:76
static constexpr unsigned int kNone
Definition: ntcore_cpp.h:56
static constexpr unsigned int kLogMessage
Log message.
Definition: ntcore_cpp.h:84
static constexpr unsigned int kUnpublish
Topic unpublished.
Definition: ntcore_cpp.h:72
static constexpr unsigned int kImmediate
Initial listener addition.
Definition: ntcore_cpp.h:62
static constexpr unsigned int kConnection
Any connection event (connect or disconnect).
Definition: ntcore_cpp.h:68
static constexpr unsigned int kTimeSync
Time synchronized with server.
Definition: ntcore_cpp.h:86
static constexpr unsigned int kPublish
New topic published.
Definition: ntcore_cpp.h:70
static constexpr unsigned int kProperties
Topic properties changed.
Definition: ntcore_cpp.h:74
static constexpr unsigned int kValueLocal
Topic value updated (local).
Definition: ntcore_cpp.h:80
static constexpr unsigned int kConnected
Client connected (on server, any client connected).
Definition: ntcore_cpp.h:64
EventFlags()=delete
static constexpr unsigned int kDisconnected
Client disconnected (on server, any client disconnected).
Definition: ntcore_cpp.h:66
NetworkTables publish/subscribe options.
Definition: ntcore_cpp.h:305
bool topicsOnly
For subscriptions, don't ask for value changes (only topic announcements).
Definition: ntcore_cpp.h:346
NT_Publisher excludePublisher
For subscriptions, if non-zero, value updates for ReadQueue() are not queued for this publisher.
Definition: ntcore_cpp.h:336
bool excludeSelf
For entries, don't queue (for ReadQueue) value updates for the entry's internal publisher.
Definition: ntcore_cpp.h:376
bool sendAll
Send all value changes over the network.
Definition: ntcore_cpp.h:341
static constexpr double kDefaultPeriodic
Default value of periodic.
Definition: ntcore_cpp.h:309
bool keepDuplicates
Preserve duplicate value changes (rather than ignoring them).
Definition: ntcore_cpp.h:351
bool disableLocal
For subscriptions, if local value updates should not be queued for ReadQueue().
Definition: ntcore_cpp.h:370
bool disableRemote
For subscriptions, if remote value updates should not be queued for ReadQueue().
Definition: ntcore_cpp.h:364
bool prefixMatch
Perform prefix match on subscriber topic names.
Definition: ntcore_cpp.h:358
unsigned int structSize
Structure size.
Definition: ntcore_cpp.h:314
bool hidden
For subscriptions, don't share the existence of the subscription with the network.
Definition: ntcore_cpp.h:384
double periodic
How frequently changes will be sent over the network, in seconds.
Definition: ntcore_cpp.h:330
unsigned int pollStorage
Polling storage size for a subscription.
Definition: ntcore_cpp.h:322
NetworkTables Topic Information.
Definition: ntcore_cpp.h:90
wpi::json GetProperties() const
Get topic properties as a JSON object.
std::string name
Topic name.
Definition: ntcore_cpp.h:95
NT_Topic topic
Topic handle.
Definition: ntcore_cpp.h:92
std::string type_str
Topic type string.
Definition: ntcore_cpp.h:101
std::string properties
Topic properties JSON string.
Definition: ntcore_cpp.h:104
friend void swap(TopicInfo &first, TopicInfo &second)
Definition: ntcore_cpp.h:109
NT_Type type
Topic type.
Definition: ntcore_cpp.h:98
Client (as published via $clients).
Definition: ntcore_cpp.h:1466
std::string id
Definition: ntcore_cpp.h:1467
std::string conn
Definition: ntcore_cpp.h:1468
uint16_t version
Definition: ntcore_cpp.h:1469
Client publisher (as published via $clientpub$<client> or $serverpub).
Definition: ntcore_cpp.h:1449
int64_t uid
Definition: ntcore_cpp.h:1450
std::string topic
Definition: ntcore_cpp.h:1451
Client subscriber (as published via $clientsub$<client> or $serversub).
Definition: ntcore_cpp.h:1457
int64_t uid
Definition: ntcore_cpp.h:1458
std::vector< std::string > topics
Definition: ntcore_cpp.h:1459
SubscriberOptions options
Definition: ntcore_cpp.h:1460
Subscriber options.
Definition: ntcore_cpp.h:1421
bool topicsOnly
Definition: ntcore_cpp.h:1423
bool sendAll
Definition: ntcore_cpp.h:1424
double periodic
Definition: ntcore_cpp.h:1422
bool prefixMatch
Definition: ntcore_cpp.h:1425
Topic publisher (as published via $pub$<topic>).
Definition: ntcore_cpp.h:1432
uint64_t pubuid
Definition: ntcore_cpp.h:1434
std::string client
Definition: ntcore_cpp.h:1433
Topic subscriber (as published via $sub$<topic>).
Definition: ntcore_cpp.h:1440
SubscriberOptions options
Definition: ntcore_cpp.h:1443
std::string client
Definition: ntcore_cpp.h:1441
uint64_t subuid
Definition: ntcore_cpp.h:1442