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