WPILibC++ 2024.1.1-beta-4
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/**
380 * Default publish/subscribe options.
381 */
383
384/**
385 * @defgroup ntcore_instance_func Instance Functions
386 * @{
387 */
388
389/**
390 * Get default instance.
391 * This is the instance used by non-handle-taking functions.
392 *
393 * @return Instance handle
394 */
396
397/**
398 * Create an instance.
399 *
400 * @return Instance handle
401 */
403
404/**
405 * Reset the internals of an instance. Every handle previously associated
406 * with this instance will no longer be valid, except for the instance
407 * handle.
408 */
410
411/**
412 * Destroy an instance.
413 * The default instance cannot be destroyed.
414 *
415 * @param inst Instance handle
416 */
418
419/**
420 * Get instance handle from another handle.
421 *
422 * @param handle entry/instance/etc. handle
423 * @return Instance handle
424 */
426
427/** @} */
428
429/**
430 * @defgroup ntcore_table_func Table Functions
431 * @{
432 */
433
434/**
435 * Get Entry Handle.
436 *
437 * @param inst instance handle
438 * @param name entry name (UTF-8 string)
439 * @return entry handle
440 */
442
443/**
444 * Gets the name of the specified entry.
445 * Returns an empty string if the handle is invalid.
446 *
447 * @param entry entry handle
448 * @return Entry name
449 */
450std::string GetEntryName(NT_Entry entry);
451
452/**
453 * Gets the type for the specified entry, or unassigned if non existent.
454 *
455 * @param entry entry handle
456 * @return Entry type
457 */
459
460/**
461 * Gets the last time the entry was changed.
462 * Returns 0 if the handle is invalid.
463 *
464 * @param subentry subscriber or entry handle
465 * @return Entry last change time
466 */
468
469/**
470 * Get Entry Value.
471 *
472 * Returns copy of current entry value.
473 * Note that one of the type options is "unassigned".
474 *
475 * @param subentry subscriber or entry handle
476 * @return entry value
477 */
479
480/**
481 * Set Default Entry Value
482 *
483 * Returns copy of current entry value if it exists.
484 * Otherwise, sets passed in value, and returns set value.
485 * Note that one of the type options is "unassigned".
486 *
487 * @param entry entry handle
488 * @param value value to be set if name does not exist
489 * @return False on error (value not set), True on success
490 */
491bool SetDefaultEntryValue(NT_Entry entry, const Value& value);
492
493/**
494 * Set Entry Value.
495 *
496 * Sets new entry value. If type of new value differs from the type of the
497 * currently stored entry, returns error and does not update value.
498 *
499 * @param entry entry handle
500 * @param value new entry value
501 * @return False on error (type mismatch), True on success
502 */
503bool SetEntryValue(NT_Entry entry, const Value& value);
504
505/**
506 * Set Entry Flags.
507 *
508 * @param entry entry handle
509 * @param flags flags value (bitmask of NT_EntryFlags)
510 */
511void SetEntryFlags(NT_Entry entry, unsigned int flags);
512
513/**
514 * Get Entry Flags.
515 *
516 * @param entry entry handle
517 * @return Flags value (bitmask of NT_EntryFlags)
518 */
519unsigned int GetEntryFlags(NT_Entry entry);
520
521/**
522 * Read Entry Queue.
523 *
524 * Returns new entry values since last call.
525 *
526 * @param subentry subscriber or entry handle
527 * @return entry value array
528 */
529std::vector<Value> ReadQueueValue(NT_Handle subentry);
530
531/** @} */
532
533/**
534 * @defgroup ntcore_topic_func Topic Functions
535 * @{
536 */
537
538/**
539 * Get Published Topics.
540 *
541 * Returns an array of topic handles. The results are optionally filtered by
542 * string prefix and type to only return a subset of all topics.
543 *
544 * @param inst instance handle
545 * @param prefix name required prefix; only topics whose name
546 * starts with this string are returned
547 * @param types bitmask of NT_Type values; 0 is treated specially
548 * as a "don't care"
549 * @return Array of topic handles.
550 */
551std::vector<NT_Topic> GetTopics(NT_Inst inst, std::string_view prefix,
552 unsigned int types);
553
554/**
555 * Get Published Topics.
556 *
557 * Returns an array of topic handles. The results are optionally filtered by
558 * string prefix and type to only return a subset of all topics.
559 *
560 * @param inst instance handle
561 * @param prefix name required prefix; only topics whose name
562 * starts with this string are returned
563 * @param types array of type strings
564 * @return Array of topic handles.
565 */
566std::vector<NT_Topic> GetTopics(NT_Inst inst, std::string_view prefix,
567 std::span<const std::string_view> types);
568
569/**
570 * Get Topic Information about multiple topics.
571 *
572 * Returns an array of topic information (handle, name, type, and properties).
573 * The results are optionally filtered by string prefix and type to only
574 * return a subset of all topics.
575 *
576 * @param inst instance handle
577 * @param prefix name required prefix; only topics whose name
578 * starts with this string are returned
579 * @param types bitmask of NT_Type values; 0 is treated specially
580 * as a "don't care"
581 * @return Array of topic information.
582 */
583std::vector<TopicInfo> GetTopicInfo(NT_Inst inst, std::string_view prefix,
584 unsigned int types);
585
586/**
587 * Get Topic Information about multiple topics.
588 *
589 * Returns an array of topic information (handle, name, type, and properties).
590 * The results are optionally filtered by string prefix and type to only
591 * return a subset of all topics.
592 *
593 * @param inst instance handle
594 * @param prefix name required prefix; only topics whose name
595 * starts with this string are returned
596 * @param types array of type strings
597 * @return Array of topic information.
598 */
599std::vector<TopicInfo> GetTopicInfo(NT_Inst inst, std::string_view prefix,
600 std::span<const std::string_view> types);
601
602/**
603 * Gets Topic Information.
604 *
605 * Returns information about a topic (name, type, and properties).
606 *
607 * @param topic handle
608 * @return Topic information.
609 */
611
612/**
613 * Gets Topic Handle.
614 *
615 * Returns topic handle.
616 *
617 * @param inst instance handle
618 * @param name topic name
619 * @return Topic handle.
620 */
622
623/**
624 * Gets the name of the specified topic.
625 * Returns an empty string if the handle is invalid.
626 *
627 * @param topic topic handle
628 * @return Topic name
629 */
630std::string GetTopicName(NT_Topic topic);
631
632/**
633 * Gets the type for the specified topic, or unassigned if non existent.
634 *
635 * @param topic topic handle
636 * @return Topic type
637 */
639
640/**
641 * Gets the type string for the specified topic, or empty string if non
642 * existent. This may have more information than the numeric type (especially
643 * for raw values).
644 *
645 * @param topic topic handle
646 * @return Topic type string
647 */
648std::string GetTopicTypeString(NT_Topic topic);
649
650/**
651 * Sets the persistent property of a topic. If true, the stored value is
652 * persistent through server restarts.
653 *
654 * @param topic topic handle
655 * @param value True for persistent, false for not persistent.
656 */
657void SetTopicPersistent(NT_Topic topic, bool value);
658
659/**
660 * Gets the persistent property of a topic. If true, the server retains the
661 * topic even when there are no publishers.
662 *
663 * @param topic topic handle
664 * @return persistent property value
665 */
667
668/**
669 * Sets the retained property of a topic.
670 *
671 * @param topic topic handle
672 * @param value new retained property value
673 */
674void SetTopicRetained(NT_Topic topic, bool value);
675
676/**
677 * Gets the retained property of a topic.
678 *
679 * @param topic topic handle
680 * @return retained property value
681 */
683
684/**
685 * Sets the cached property of a topic. If true, the server and clients will
686 * store the latest value, allowing the value to be read (and not just accessed
687 * through event queues and listeners).
688 *
689 * @param topic topic handle
690 * @param value True for cached, false for not cached
691 */
692void SetTopicCached(NT_Topic topic, bool value);
693
694/**
695 * Gets the cached property of a topic.
696 *
697 * @param topic topic handle
698 * @return cached property value
699 */
701
702/**
703 * Determine if topic exists (e.g. has at least one publisher).
704 *
705 * @param handle Topic, entry, or subscriber handle.
706 * @return True if topic exists.
707 */
709
710/**
711 * Gets the current value of a property (as a JSON object).
712 *
713 * @param topic topic handle
714 * @param name property name
715 * @return JSON object; null object if the property does not exist.
716 */
718
719/**
720 * Sets a property value.
721 *
722 * @param topic topic handle
723 * @param name property name
724 * @param value property value
725 */
727 const wpi::json& value);
728
729/**
730 * Deletes a property. Has no effect if the property does not exist.
731 *
732 * @param topic topic handle
733 * @param name property name
734 */
736
737/**
738 * Gets all topic properties as a JSON object. Each key in the object
739 * is the property name, and the corresponding value is the property value.
740 *
741 * @param topic topic handle
742 * @return JSON object
743 */
745
746/**
747 * Updates multiple topic properties. Each key in the passed-in object is
748 * the name of the property to add/update, and the corresponding value is the
749 * property value to set for that property. Null values result in deletion
750 * of the corresponding property.
751 *
752 * @param topic topic handle
753 * @param update JSON object with keys to add/update/delete
754 * @return False if update is not a JSON object
755 */
756bool SetTopicProperties(NT_Topic topic, const wpi::json& update);
757
758/**
759 * Creates a new subscriber to value changes on a topic.
760 *
761 * @param topic topic handle
762 * @param type expected type
763 * @param typeStr expected type string
764 * @param options subscription options
765 * @return Subscriber handle
766 */
768 const PubSubOptions& options = kDefaultPubSubOptions);
769
770/**
771 * Stops subscriber.
772 *
773 * @param sub subscriber handle
774 */
776
777/**
778 * Creates a new publisher to a topic.
779 *
780 * @param topic topic handle
781 * @param type type
782 * @param typeStr type string
783 * @param options publish options
784 * @return Publisher handle
785 */
787 const PubSubOptions& options = kDefaultPubSubOptions);
788
789/**
790 * Creates a new publisher to a topic.
791 *
792 * @param topic topic handle
793 * @param type type
794 * @param typeStr type string
795 * @param properties initial properties
796 * @param options publish options
797 * @return Publisher handle
798 */
800 const wpi::json& properties,
801 const PubSubOptions& options = kDefaultPubSubOptions);
802
803/**
804 * Stops publisher.
805 *
806 * @param pubentry publisher/entry handle
807 */
808void Unpublish(NT_Handle pubentry);
809
810/**
811 * @brief Creates a new entry (subscriber and weak publisher) to a topic.
812 *
813 * @param topic topic handle
814 * @param type type
815 * @param typeStr type string
816 * @param options publish options
817 * @return Entry handle
818 */
820 const PubSubOptions& options = kDefaultPubSubOptions);
821
822/**
823 * Stops entry subscriber/publisher.
824 *
825 * @param entry entry handle
826 */
828
829/**
830 * Stops entry/subscriber/publisher.
831 *
832 * @param pubsubentry entry/subscriber/publisher handle
833 */
834void Release(NT_Handle pubsubentry);
835
836/**
837 * Gets the topic handle from an entry/subscriber/publisher handle.
838 *
839 * @param pubsubentry entry/subscriber/publisher handle
840 * @return Topic handle
841 */
843
844/** @} */
845
846/**
847 * @defgroup ntcore_advancedsub_func Advanced Subscriber Functions
848 * @{
849 */
850
851/**
852 * Subscribes to multiple topics based on one or more topic name prefixes. Can
853 * be used in combination with a Value Listener or ReadQueueValue() to get value
854 * changes across all matching topics.
855 *
856 * @param inst instance handle
857 * @param prefixes topic name prefixes
858 * @param options subscriber options
859 * @return subscriber handle
860 */
862 NT_Inst inst, std::span<const std::string_view> prefixes,
863 const PubSubOptions& options = kDefaultPubSubOptions);
864
865/**
866 * Unsubscribes a multi-subscriber.
867 *
868 * @param sub multi-subscriber handle
869 */
871
872/** @} */
873
874/**
875 * @defgroup ntcore_listener_func Listener Functions
876 * @{
877 */
878
879using ListenerCallback = std::function<void(const Event&)>;
880
881/**
882 * Creates a listener poller.
883 *
884 * A poller provides a single queue of poll events. Events linked to this
885 * poller (using AddPolledListener()) will be stored in the queue and
886 * must be collected by calling ReadListenerQueue().
887 * The returned handle must be destroyed with DestroyListenerPoller().
888 *
889 * @param inst instance handle
890 * @return poller handle
891 */
893
894/**
895 * Destroys a listener poller. This will abort any blocked polling
896 * call and prevent additional events from being generated for this poller.
897 *
898 * @param poller poller handle
899 */
901
902/**
903 * Read notifications.
904 *
905 * @param poller poller handle
906 * @return Array of events. Returns empty array if no events since last call.
907 */
908std::vector<Event> ReadListenerQueue(NT_ListenerPoller poller);
909
910/**
911 * Removes a listener.
912 *
913 * @param listener Listener handle to remove
914 */
916
917/**
918 * Wait for the listener queue to be empty. This is primarily useful
919 * for deterministic testing. This blocks until either the listener
920 * queue is empty (e.g. there are no more events that need to be passed along to
921 * callbacks or poll queues) or the timeout expires.
922 *
923 * @param handle handle
924 * @param timeout timeout, in seconds. Set to 0 for non-blocking behavior, or a
925 * negative value to block indefinitely
926 * @return False if timed out, otherwise true.
927 */
928bool WaitForListenerQueue(NT_Handle handle, double timeout);
929
930/**
931 * Create a listener for changes to topics with names that start with any of
932 * the given prefixes. This creates a corresponding internal subscriber with the
933 * lifetime of the listener.
934 *
935 * @param inst Instance handle
936 * @param prefixes Topic name string prefixes
937 * @param mask Bitmask of NT_EventFlags values (only topic and value events will
938 * be generated)
939 * @param callback Listener function
940 */
942 std::span<const std::string_view> prefixes,
943 unsigned int mask, ListenerCallback callback);
944
945/**
946 * Create a listener.
947 *
948 * Some combinations of handle and mask have no effect:
949 * - connection and log message events are only generated on instances
950 * - topic and value events are only generated on non-instances
951 *
952 * Adding value and topic events on a topic will create a corresponding internal
953 * subscriber with the lifetime of the listener.
954 *
955 * Adding a log message listener through this function will only result in
956 * events at NT_LOG_INFO or higher; for more customized settings, use
957 * AddLogger().
958 *
959 * @param handle Instance, topic, subscriber, multi-subscriber, or entry handle
960 * @param mask Bitmask of NT_EventFlags values
961 * @param callback Listener function
962 */
963NT_Listener AddListener(NT_Handle handle, unsigned int mask,
964 ListenerCallback callback);
965
966/**
967 * Creates a polled listener. This creates a corresponding internal subscriber
968 * with the lifetime of the listener.
969 * The caller is responsible for calling ReadListenerQueue() to poll.
970 *
971 * @param poller poller handle
972 * @param prefixes array of UTF-8 string prefixes
973 * @param mask Bitmask of NT_EventFlags values (only topic and value events will
974 * be generated)
975 * @return Listener handle
976 */
978 std::span<const std::string_view> prefixes,
979 unsigned int mask);
980
981/**
982 * Creates a polled listener.
983 * The caller is responsible for calling ReadListenerQueue() to poll.
984 *
985 * Some combinations of handle and mask have no effect:
986 * - connection and log message events are only generated on instances
987 * - topic and value events are only generated on non-instances
988 *
989 * Adding value and topic events on a topic will create a corresponding internal
990 * subscriber with the lifetime of the listener.
991 *
992 * Adding a log message listener through this function will only result in
993 * events at NT_LOG_INFO or higher; for more customized settings, use
994 * AddPolledLogger().
995 *
996 * @param poller poller handle
997 * @param handle instance, topic, subscriber, multi-subscriber, or entry handle
998 * @param mask NT_EventFlags bitmask
999 * @return Listener handle
1000 */
1002 unsigned int mask);
1003
1004/** @} */
1005
1006/**
1007 * @defgroup ntcore_network_func Client/Server Functions
1008 * @{
1009 */
1010
1011/**
1012 * Get the current network mode.
1013 *
1014 * @param inst instance handle
1015 * @return Bitmask of NT_NetworkMode.
1016 */
1017unsigned int GetNetworkMode(NT_Inst inst);
1018
1019/**
1020 * Starts local-only operation. Prevents calls to StartServer or StartClient
1021 * from taking effect. Has no effect if StartServer or StartClient
1022 * has already been called.
1023 */
1025
1026/**
1027 * Stops local-only operation. StartServer or StartClient can be called after
1028 * this call to start a server or client.
1029 */
1031
1032/**
1033 * Starts a server using the specified filename, listening address, and port.
1034 *
1035 * @param inst instance handle
1036 * @param persist_filename the name of the persist file to use (UTF-8 string,
1037 * null terminated)
1038 * @param listen_address the address to listen on, or null to listen on any
1039 * address. (UTF-8 string, null terminated)
1040 * @param port3 port to communicate over (NT3)
1041 * @param port4 port to communicate over (NT4)
1042 */
1043void StartServer(NT_Inst inst, std::string_view persist_filename,
1044 const char* listen_address, unsigned int port3,
1045 unsigned int port4);
1046
1047/**
1048 * Stops the server if it is running.
1049 *
1050 * @param inst instance handle
1051 */
1053
1054/**
1055 * Starts a NT3 client. Use SetServer or SetServerTeam to set the server name
1056 * and port.
1057 *
1058 * @param inst instance handle
1059 * @param identity network identity to advertise (cannot be empty string)
1060 */
1062
1063/**
1064 * Starts a NT4 client. Use SetServer or SetServerTeam to set the server name
1065 * and port.
1066 *
1067 * @param inst instance handle
1068 * @param identity network identity to advertise (cannot be empty string)
1069 */
1071
1072/**
1073 * Stops the client if it is running.
1074 *
1075 * @param inst instance handle
1076 */
1078
1079/**
1080 * Sets server address and port for client (without restarting client).
1081 *
1082 * @param inst instance handle
1083 * @param server_name server name (UTF-8 string, null terminated)
1084 * @param port port to communicate over
1085 */
1086void SetServer(NT_Inst inst, const char* server_name, unsigned int port);
1087
1088/**
1089 * Sets server addresses for client (without restarting client).
1090 * The client will attempt to connect to each server in round robin fashion.
1091 *
1092 * @param inst instance handle
1093 * @param servers array of server name and port pairs
1094 */
1096 NT_Inst inst,
1097 std::span<const std::pair<std::string_view, unsigned int>> servers);
1098
1099/**
1100 * Sets server addresses and port for client (without restarting client).
1101 * Connects using commonly known robot addresses for the specified team.
1102 *
1103 * @param inst instance handle
1104 * @param team team number
1105 * @param port port to communicate over
1106 */
1107void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port);
1108
1109/**
1110 * Disconnects the client if it's running and connected. This will automatically
1111 * start reconnection attempts to the current server list.
1112 *
1113 * @param inst instance handle
1114 */
1116
1117/**
1118 * Starts requesting server address from Driver Station.
1119 * This connects to the Driver Station running on localhost to obtain the
1120 * server IP address.
1121 *
1122 * @param inst instance handle
1123 * @param port server port to use in combination with IP from DS
1124 */
1125void StartDSClient(NT_Inst inst, unsigned int port);
1126
1127/**
1128 * Stops requesting server address from Driver Station.
1129 *
1130 * @param inst instance handle
1131 */
1133
1134/**
1135 * Flush local updates.
1136 *
1137 * Forces an immediate flush of all local changes to the client/server.
1138 * This does not flush to the network.
1139 *
1140 * Normally this is done on a regularly scheduled interval.
1141 *
1142 * @param inst instance handle
1143 */
1145
1146/**
1147 * Flush to network.
1148 *
1149 * Forces an immediate flush of all local entry changes to network.
1150 * Normally this is done on a regularly scheduled interval (set
1151 * by update rates on individual publishers).
1152 *
1153 * Note: flushes are rate limited to avoid excessive network traffic. If
1154 * the time between calls is too short, the flush will occur after the minimum
1155 * time elapses (rather than immediately).
1156 *
1157 * @param inst instance handle
1158 */
1159void Flush(NT_Inst inst);
1160
1161/**
1162 * Get information on the currently established network connections.
1163 * If operating as a client, this will return either zero or one values.
1164 *
1165 * @param inst instance handle
1166 * @return array of connection information
1167 */
1168std::vector<ConnectionInfo> GetConnections(NT_Inst inst);
1169
1170/**
1171 * Return whether or not the instance is connected to another node.
1172 *
1173 * @param inst instance handle
1174 * @return True if connected.
1175 */
1177
1178/**
1179 * Get the time offset between server time and local time. Add this value to
1180 * local time to get the estimated equivalent server time. In server mode, this
1181 * always returns 0. In client mode, this returns the time offset only if the
1182 * client and server are connected and have exchanged synchronization messages.
1183 * Note the time offset may change over time as it is periodically updated; to
1184 * receive updates as events, add a listener to the "time sync" event.
1185 *
1186 * @param inst instance handle
1187 * @return Time offset in microseconds (optional)
1188 */
1189std::optional<int64_t> GetServerTimeOffset(NT_Inst inst);
1190
1191/** @} */
1192
1193/**
1194 * @defgroup ntcore_utility_func Utility Functions
1195 * @{
1196 */
1197
1198/**
1199 * Returns monotonic current time in 1 us increments.
1200 * This is the same time base used for value and connection timestamps.
1201 * This function by default simply wraps wpi::Now(), but if SetNow() is
1202 * called, this function instead returns the value passed to SetNow();
1203 * this can be used to reduce overhead.
1204 *
1205 * @return Timestamp
1206 */
1207int64_t Now();
1208
1209/**
1210 * Sets the current timestamp used for timestamping values that do not
1211 * provide a timestamp (e.g. a value of 0 is passed). For consistency,
1212 * it also results in Now() returning the set value. This should generally
1213 * be used only if the overhead of calling wpi::Now() is a concern.
1214 * If used, it should be called periodically with the value of wpi::Now().
1215 *
1216 * @param timestamp timestamp (1 us increments)
1217 */
1218void SetNow(int64_t timestamp);
1219
1220/**
1221 * Turns a type string into a type enum value.
1222 *
1223 * @param typeString type string
1224 * @return Type value
1225 */
1227
1228/**
1229 * Turns a type enum value into a type string.
1230 *
1231 * @param type type enum
1232 * @return Type string
1233 */
1235
1236/** @} */
1237
1238/**
1239 * @defgroup ntcore_data_logger_func Data Logger Functions
1240 * @{
1241 */
1242
1243/**
1244 * Starts logging entry changes to a DataLog.
1245 *
1246 * @param inst instance handle
1247 * @param log data log object; lifetime must extend until StopEntryDataLog is
1248 * called or the instance is destroyed
1249 * @param prefix only store entries with names that start with this prefix;
1250 * the prefix is not included in the data log entry name
1251 * @param logPrefix prefix to add to data log entry names
1252 * @return Data logger handle
1253 */
1255 std::string_view prefix,
1256 std::string_view logPrefix);
1257
1258/**
1259 * Stops logging entry changes to a DataLog.
1260 *
1261 * @param logger data logger handle
1262 */
1264
1265/**
1266 * Starts logging connection changes to a DataLog.
1267 *
1268 * @param inst instance handle
1269 * @param log data log object; lifetime must extend until StopConnectionDataLog
1270 * is called or the instance is destroyed
1271 * @param name data log entry name
1272 * @return Data logger handle
1273 */
1276 std::string_view name);
1277
1278/**
1279 * Stops logging connection changes to a DataLog.
1280 *
1281 * @param logger data logger handle
1282 */
1284
1285/** @} */
1286
1287/**
1288 * @defgroup ntcore_logger_func Logger Functions
1289 * @{
1290 */
1291
1292/**
1293 * Add logger callback function. By default, log messages are sent to stderr;
1294 * this function sends log messages to the provided callback function instead.
1295 * The callback function will only be called for log messages with level
1296 * greater than or equal to min_level and less than or equal to max_level;
1297 * messages outside this range will be silently ignored.
1298 *
1299 * @param inst instance handle
1300 * @param min_level minimum log level
1301 * @param max_level maximum log level
1302 * @param func listener callback function
1303 * @return Listener handle
1304 */
1305NT_Listener AddLogger(NT_Inst inst, unsigned int min_level,
1306 unsigned int max_level, ListenerCallback func);
1307
1308/**
1309 * Set the log level for a log poller. Events will only be generated for
1310 * log messages with level greater than or equal to min_level and less than or
1311 * equal to max_level; messages outside this range will be silently ignored.
1312 *
1313 * @param poller poller handle
1314 * @param min_level minimum log level
1315 * @param max_level maximum log level
1316 * @return Logger handle
1317 */
1318NT_Listener AddPolledLogger(NT_ListenerPoller poller, unsigned int min_level,
1319 unsigned int max_level);
1320
1321/** @} */
1322
1323/**
1324 * @defgroup ntcore_schema_func Schema Functions
1325 * @{
1326 */
1327
1328/**
1329 * Returns whether there is a data schema already registered with the given
1330 * name. This does NOT perform a check as to whether the schema has already
1331 * been published by another node on the network.
1332 *
1333 * @param inst instance
1334 * @param name Name (the string passed as the data type for topics using this
1335 * schema)
1336 * @return True if schema already registered
1337 */
1339
1340/**
1341 * Registers a data schema. Data schemas provide information for how a
1342 * certain data type string can be decoded. The type string of a data schema
1343 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
1344 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
1345 * published just like normal topics, with the name being generated from the
1346 * provided name: "/.schema/<name>". Duplicate calls to this function with
1347 * the same name are silently ignored.
1348 *
1349 * @param inst instance
1350 * @param name Name (the string passed as the data type for topics using this
1351 * schema)
1352 * @param type Type of schema (e.g. "protobuf", "struct", etc)
1353 * @param schema Schema data
1354 */
1356 std::span<const uint8_t> schema);
1357
1358/**
1359 * Registers a data schema. Data schemas provide information for how a
1360 * certain data type string can be decoded. The type string of a data schema
1361 * indicates the type of the schema itself (e.g. "protobuf" for protobuf
1362 * schemas, "struct" for struct schemas, etc). In NetworkTables, schemas are
1363 * published just like normal topics, with the name being generated from the
1364 * provided name: "/.schema/<name>". Duplicate calls to this function with
1365 * the same name are silently ignored.
1366 *
1367 * @param inst instance
1368 * @param name Name (the string passed as the data type for topics using this
1369 * schema)
1370 * @param type Type of schema (e.g. "protobuf", "struct", etc)
1371 * @param schema Schema data
1372 */
1373inline void AddSchema(NT_Inst inst, std::string_view name,
1375 AddSchema(
1376 inst, name, type,
1377 std::span<const uint8_t>{reinterpret_cast<const uint8_t*>(schema.data()),
1378 schema.size()});
1379}
1380
1381/** @} */
1382/** @} */
1383
1384/**
1385 * NetworkTables meta-topic decoding functions.
1386 */
1387namespace meta {
1388
1389/**
1390 * @defgroup ntcore_cpp_meta_api ntcore C++ meta-topic API
1391 *
1392 * Meta-topic decoders for C++.
1393 *
1394 * @{
1395 */
1396
1397/**
1398 * Subscriber options. Different from PubSubOptions in this reflects only
1399 * options that are sent over the network.
1400 */
1402 double periodic = 0.1;
1403 bool topicsOnly = false;
1404 bool sendAll = false;
1405 bool prefixMatch = false;
1406 // std::string otherStr;
1407};
1408
1409/**
1410 * Topic publisher (as published via `$pub$<topic>`).
1411 */
1413 std::string client;
1414 uint64_t pubuid = 0;
1415};
1416
1417/**
1418 * Topic subscriber (as published via `$sub$<topic>`).
1419 */
1421 std::string client;
1422 uint64_t subuid = 0;
1424};
1425
1426/**
1427 * Client publisher (as published via `$clientpub$<client>` or `$serverpub`).
1428 */
1430 int64_t uid = -1;
1431 std::string topic;
1432};
1433
1434/**
1435 * Client subscriber (as published via `$clientsub$<client>` or `$serversub`).
1436 */
1438 int64_t uid = -1;
1439 std::vector<std::string> topics;
1441};
1442
1443/**
1444 * Client (as published via `$clients`).
1445 */
1446struct Client {
1447 std::string id;
1448 std::string conn;
1449 uint16_t version = 0;
1450};
1451
1452/**
1453 * Decodes `$pub$<topic>` meta-topic data.
1454 *
1455 * @param data data contents
1456 * @return Vector of TopicPublishers, or empty optional on decoding error.
1457 */
1458std::optional<std::vector<TopicPublisher>> DecodeTopicPublishers(
1459 std::span<const uint8_t> data);
1460
1461/**
1462 * Decodes `$sub$<topic>` meta-topic data.
1463 *
1464 * @param data data contents
1465 * @return Vector of TopicSubscribers, or empty optional on decoding error.
1466 */
1467std::optional<std::vector<TopicSubscriber>> DecodeTopicSubscribers(
1468 std::span<const uint8_t> data);
1469
1470/**
1471 * Decodes `$clientpub$<topic>` meta-topic data.
1472 *
1473 * @param data data contents
1474 * @return Vector of ClientPublishers, or empty optional on decoding error.
1475 */
1476std::optional<std::vector<ClientPublisher>> DecodeClientPublishers(
1477 std::span<const uint8_t> data);
1478
1479/**
1480 * Decodes `$clientsub$<topic>` meta-topic data.
1481 *
1482 * @param data data contents
1483 * @return Vector of ClientSubscribers, or empty optional on decoding error.
1484 */
1485std::optional<std::vector<ClientSubscriber>> DecodeClientSubscribers(
1486 std::span<const uint8_t> data);
1487
1488/**
1489 * Decodes `$clients` meta-topic data.
1490 *
1491 * @param data data contents
1492 * @return Vector of Clients, or empty optional on decoding error.
1493 */
1494std::optional<std::vector<Client>> DecodeClients(std::span<const uint8_t> data);
1495
1496/** @} */
1497
1498} // namespace meta
1499} // 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:89
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:382
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:879
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: MultiSubscriber.h:13
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
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:1446
std::string id
Definition: ntcore_cpp.h:1447
std::string conn
Definition: ntcore_cpp.h:1448
uint16_t version
Definition: ntcore_cpp.h:1449
Client publisher (as published via $clientpub$<client> or $serverpub).
Definition: ntcore_cpp.h:1429
int64_t uid
Definition: ntcore_cpp.h:1430
std::string topic
Definition: ntcore_cpp.h:1431
Client subscriber (as published via $clientsub$<client> or $serversub).
Definition: ntcore_cpp.h:1437
int64_t uid
Definition: ntcore_cpp.h:1438
std::vector< std::string > topics
Definition: ntcore_cpp.h:1439
SubscriberOptions options
Definition: ntcore_cpp.h:1440
Subscriber options.
Definition: ntcore_cpp.h:1401
bool topicsOnly
Definition: ntcore_cpp.h:1403
bool sendAll
Definition: ntcore_cpp.h:1404
double periodic
Definition: ntcore_cpp.h:1402
bool prefixMatch
Definition: ntcore_cpp.h:1405
Topic publisher (as published via $pub$<topic>).
Definition: ntcore_cpp.h:1412
uint64_t pubuid
Definition: ntcore_cpp.h:1414
std::string client
Definition: ntcore_cpp.h:1413
Topic subscriber (as published via $sub$<topic>).
Definition: ntcore_cpp.h:1420
SubscriberOptions options
Definition: ntcore_cpp.h:1423
std::string client
Definition: ntcore_cpp.h:1421
uint64_t subuid
Definition: ntcore_cpp.h:1422