001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005// THIS FILE WAS AUTO-GENERATED BY ./ntcore/generate_topics.py. DO NOT MODIFY
006
007package edu.wpi.first.networktables;
008
009import edu.wpi.first.util.RuntimeLoader;
010import edu.wpi.first.util.datalog.DataLog;
011import java.io.IOException;
012import java.nio.ByteBuffer;
013import java.util.EnumSet;
014import java.util.OptionalLong;
015import java.util.concurrent.atomic.AtomicBoolean;
016
017/** NetworkTables JNI. */
018public final class NetworkTablesJNI {
019  static boolean libraryLoaded = false;
020  static RuntimeLoader<NetworkTablesJNI> loader = null;
021
022  /** Sets whether JNI should be loaded in the static block. */
023  public static class Helper {
024    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
025
026    /**
027     * Returns true if the JNI should be loaded in the static block.
028     *
029     * @return True if the JNI should be loaded in the static block.
030     */
031    public static boolean getExtractOnStaticLoad() {
032      return extractOnStaticLoad.get();
033    }
034
035    /**
036     * Sets whether the JNI should be loaded in the static block.
037     *
038     * @param load Whether the JNI should be loaded in the static block.
039     */
040    public static void setExtractOnStaticLoad(boolean load) {
041      extractOnStaticLoad.set(load);
042    }
043
044    /** Utility class. */
045    private Helper() {}
046  }
047
048  static {
049    if (Helper.getExtractOnStaticLoad()) {
050      try {
051        loader =
052            new RuntimeLoader<>(
053                "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
054        loader.loadLibrary();
055      } catch (IOException ex) {
056        ex.printStackTrace();
057        System.exit(1);
058      }
059      libraryLoaded = true;
060    }
061  }
062
063  /**
064   * Force load the library.
065   *
066   * @throws IOException if the library fails to load
067   */
068  public static synchronized void forceLoad() throws IOException {
069    if (libraryLoaded) {
070      return;
071    }
072    loader =
073        new RuntimeLoader<>(
074            "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
075    loader.loadLibrary();
076    libraryLoaded = true;
077  }
078
079  private static PubSubOptions buildOptions(PubSubOption... options) {
080    if (options.length == 0) {
081      return null;  // optimize common case (JNI checks for null)
082    }
083    return new PubSubOptions(options);
084  }
085
086  /**
087   * Returns default instance handle.
088   *
089   * @return Default instance handle.
090   */
091  public static native int getDefaultInstance();
092
093  /**
094   * Creates an NT instance.
095   *
096   * @return NT instance handle.
097   */
098  public static native int createInstance();
099
100  /**
101   * Destroys an NT instance.
102   *
103   * @param inst NT instance handle.
104   */
105  public static native void destroyInstance(int inst);
106
107  /**
108   * Returns NT instance from handle.
109   *
110   * @param handle NT instance handle.
111   * @return NT instance.
112   */
113  public static native int getInstanceFromHandle(int handle);
114
115  private static native int getEntryImpl(
116      int topic, int type, String typeStr, PubSubOptions options);
117
118  /**
119   * Returns NT entry handle.
120   *
121   * @param inst NT instance handle.
122   * @param key NT entry key.
123   * @return NT entry handle.
124   */
125  public static native int getEntry(int inst, String key);
126
127  /**
128   * Returns NT entry handle.
129   *
130   * @param topic NT entry topic.
131   * @param type NT entry type.
132   * @param typeStr NT entry type as a string.
133   * @param options NT entry pubsub options.
134   * @return NT entry handle.
135   */
136  public static int getEntry(
137      int topic, int type, String typeStr, PubSubOptions options) {
138    return getEntryImpl(topic, type, typeStr, options);
139  }
140
141  /**
142   * Returns NT entry handle.
143   *
144   * @param topic NT entry topic.
145   * @param type NT entry type.
146   * @param typeStr NT entry type as a string.
147   * @param options NT entry pubsub options.
148   * @return NT entry handle.
149   */
150  public static int getEntry(
151      int topic, int type, String typeStr, PubSubOption... options) {
152    return getEntryImpl(topic, type, typeStr, buildOptions(options));
153  }
154
155  /**
156   * Returns NT entry name.
157   *
158   * @param entry NT entry handle.
159   * @return NT entry name.
160   */
161  public static native String getEntryName(int entry);
162
163  /**
164   * Returns NT entry last change time in microseconds.
165   *
166   * @param entry NT entry handle.
167   * @return NT entry last change time in microseconds.
168   */
169  public static native long getEntryLastChange(int entry);
170
171  /**
172   * Returns NT entry type.
173   *
174   * @param entry NT entry handle.
175   * @return NT entry type.
176   */
177  public static native int getType(int entry);
178
179  /* Topic functions */
180
181  /**
182   * Returns list of topic handles.
183   *
184   * @param inst NT instance handle.
185   * @param prefix Topic prefix.
186   * @param types Topic types.
187   * @return List of topic handles.
188   */
189  public static native int[] getTopics(int inst, String prefix, int types);
190
191  /**
192   * Returns list of topic handles.
193   *
194   * @param inst NT instance handle.
195   * @param prefix Topic prefix.
196   * @param types Topic types as strings.
197   * @return List of topic handles.
198   */
199  public static native int[] getTopicsStr(int inst, String prefix, String[] types);
200
201  /**
202   * Returns list of topic infos.
203   *
204   * @param instObject NT instance.
205   * @param inst NT instance handle.
206   * @param prefix Topic prefix.
207   * @param types Topic types.
208   * @return List of topic infos.
209   */
210  public static native TopicInfo[] getTopicInfos(
211      NetworkTableInstance instObject, int inst, String prefix, int types);
212
213  /**
214   * Returns list of topic infos.
215   *
216   * @param instObject NT instance.
217   * @param inst NT instance handle.
218   * @param prefix Topic prefix.
219   * @param types Topic types as strings.
220   * @return List of topic infos.
221   */
222  public static native TopicInfo[] getTopicInfosStr(
223      NetworkTableInstance instObject, int inst, String prefix, String[] types);
224
225  /**
226   * Returns Topic handle.
227   *
228   * @param inst NT instance handle.
229   * @param name Topic name.
230   * @return Topic handle.
231   */
232  public static native int getTopic(int inst, String name);
233
234  /**
235   * Returns topic name.
236   *
237   * @param topic Topic handle.
238   * @return Topic name.
239   */
240  public static native String getTopicName(int topic);
241
242  /**
243   * Returns topic type.
244   *
245   * @param topic Topic handle.
246   * @return Topic type.
247   */
248  public static native int getTopicType(int topic);
249
250  /**
251   * Sets topic persistency.
252   *
253   * @param topic Topic handle.
254   * @param value True if topic should be persistent.
255   */
256  public static native void setTopicPersistent(int topic, boolean value);
257
258  /**
259   * Returns true if topic is persistent.
260   *
261   * @param topic Topic handle.
262   * @return True if topic is persistent.
263   */
264  public static native boolean getTopicPersistent(int topic);
265
266  /**
267   * Sets whether topic is retained.
268   *
269   * @param topic Topic handle.
270   * @param value True if topic should be retained.
271   */
272  public static native void setTopicRetained(int topic, boolean value);
273
274  /**
275   * Returns true if topic is retained.
276   *
277   * @param topic Topic handle.
278   * @return True if topic is retained.
279   */
280  public static native boolean getTopicRetained(int topic);
281
282  /**
283   * Sets topic caching.
284   *
285   * @param topic Topic handle.
286   * @param value True if topic should be cached.
287   */
288  public static native void setTopicCached(int topic, boolean value);
289
290  /**
291   * Returns true if topic is cached.
292   *
293   * @param topic Topic handle.
294   * @return True if topic is cached.
295   */
296  public static native boolean getTopicCached(int topic);
297
298  /**
299   * Returns topic type as string.
300   *
301   * @param topic Topic handle.
302   * @return Topic type as string.
303   */
304  public static native String getTopicTypeString(int topic);
305
306  /**
307   * Returns true if topic exists.
308   *
309   * @param topic Topic handle.
310   * @return True if topic exists.
311   */
312  public static native boolean getTopicExists(int topic);
313
314  /**
315   * Returns topic property.
316   *
317   * @param topic Topic handle.
318   * @param name Property name.
319   * @return Topic property.
320   */
321  public static native String getTopicProperty(int topic, String name);
322
323  /**
324   * Sets topic property.
325   *
326   * @param topic Topic handle.
327   * @param name Property name.
328   * @param value Property value.
329   */
330  public static native void setTopicProperty(int topic, String name, String value);
331
332  /**
333   * Deletes topic property.
334   *
335   * @param topic Topic handle.
336   * @param name Property name.
337   */
338  public static native void deleteTopicProperty(int topic, String name);
339
340  /**
341   * Returns topic properties.
342   *
343   * @param topic Topic handle.
344   * @return Topic properties.
345   */
346  public static native String getTopicProperties(int topic);
347
348  /**
349   * Sets topic properties.
350   *
351   * @param topic Topic handle.
352   * @param properties Topic properties.
353   */
354  public static native void setTopicProperties(int topic, String properties);
355
356  /**
357   * Subscribes to topic.
358   *
359   * @param topic Topic handle.
360   * @param type Topic type.
361   * @param typeStr Topic type as a string.
362   * @param options Pubsub options.
363   * @return Subscriber handle.
364   */
365  public static native int subscribe(
366      int topic, int type, String typeStr, PubSubOptions options);
367
368  /**
369   * Subscribes to topic.
370   *
371   * @param topic Topic handle.
372   * @param type Topic type.
373   * @param typeStr Topic type as a string.
374   * @param options Pubsub options.
375   * @return Subscriber handle.
376   */
377  public static int subscribe(
378      int topic, int type, String typeStr, PubSubOption... options) {
379    return subscribe(topic, type, typeStr, buildOptions(options));
380  }
381
382  /**
383   * Unsubscribes from topic.
384   *
385   * @param sub Subscriber handle.
386   */
387  public static native void unsubscribe(int sub);
388
389  /**
390   * Publishes topic.
391   *
392   * @param topic Topic handle.
393   * @param type Topic type.
394   * @param typeStr Topic type as a string.
395   * @param options Pubsub options.
396   * @return Publish handle.
397   */
398  public static native int publish(
399      int topic, int type, String typeStr, PubSubOptions options);
400
401  /**
402   * Publishes topic.
403   *
404   * @param topic Topic handle.
405   * @param type Topic type.
406   * @param typeStr Topic type as a string.
407   * @param options Pubsub options.
408   * @return Publish handle.
409   */
410  public static int publish(
411      int topic, int type, String typeStr, PubSubOption... options) {
412    return publish(topic, type, typeStr, buildOptions(options));
413  }
414
415  /**
416   * Publishes topic.
417   *
418   * @param topic Topic handle.
419   * @param type Topic type.
420   * @param typeStr Topic type as a string.
421   * @param properties Topic properties.
422   * @param options Pubsub options.
423   * @return Publish handle.
424   */
425  public static native int publishEx(
426      int topic, int type, String typeStr, String properties, PubSubOptions options);
427
428  /**
429   * Publishes topic.
430   *
431   * @param topic Topic handle.
432   * @param type Topic type.
433   * @param typeStr Topic type as a string.
434   * @param properties Topic properties.
435   * @param options Pubsub options.
436   * @return Publish handle.
437   */
438  public static int publishEx(
439      int topic, int type, String typeStr, String properties, PubSubOption... options) {
440    return publishEx(topic, type, typeStr, properties, buildOptions(options));
441  }
442
443  /**
444   * Unpublishes topic.
445   *
446   * @param pubentry Publish entry handle.
447   */
448  public static native void unpublish(int pubentry);
449
450  /**
451   * Releases NT entry.
452   *
453   * @param entry NT entry handle.
454   */
455  public static native void releaseEntry(int entry);
456
457  /**
458   * Relesaes pubsub entry.
459   *
460   * @param pubsubentry Pubsub entry handle.
461   */
462  public static native void release(int pubsubentry);
463
464  /**
465   * Returns topic from pubsub entry handle.
466   *
467   * @param pubsubentry Pubsub entry handle.
468   * @return Topic handle.
469   */
470  public static native int getTopicFromHandle(int pubsubentry);
471
472  /**
473   * Subscribes to multiple topics.
474   *
475   * @param inst NT instance handle.
476   * @param prefixes List of topic prefixes.
477   * @param options Pubsub options.
478   * @return Subscribe handle.
479   */
480  public static native int subscribeMultiple(int inst, String[] prefixes, PubSubOptions options);
481
482  /**
483   * Subscribes to multiple topics.
484   *
485   * @param inst NT instance handle.
486   * @param prefixes List of topic prefixes.
487   * @param options Pubsub options.
488   * @return Subscribe handle.
489   */
490  public static int subscribeMultiple(int inst, String[] prefixes, PubSubOption... options) {
491    return subscribeMultiple(inst, prefixes, buildOptions(options));
492  }
493
494  /**
495   * Unsubscribes from multiple topics.
496   *
497   * @param sub Subscribe handle.
498   */
499  public static native void unsubscribeMultiple(int sub);
500
501  /**
502   * Returns timestamped topic value as an atomic Boolean.
503   *
504   * @param subentry Subentry handle.
505   * @param defaultValue Default value.
506   * @return Timestamped topic value.
507   */
508  public static native TimestampedBoolean getAtomicBoolean(
509      int subentry, boolean defaultValue);
510
511  /**
512   * Returns queued timestamped topic values.
513   *
514   * @param subentry Subentry handle.
515   * @return List of timestamped topic values.
516   */
517  public static native TimestampedBoolean[] readQueueBoolean(int subentry);
518
519  /**
520   * Returns queued topic values.
521   *
522   * @param subentry Subentry handle.
523   * @return List of topic values.
524   */
525  public static native boolean[] readQueueValuesBoolean(int subentry);
526
527  /**
528   * Sets topic value.
529   *
530   * @param entry Entry handle.
531   * @param time Time in microseconds.
532   * @param value Topic value.
533   * @return True if set succeeded.
534   */
535  public static native boolean setBoolean(int entry, long time, boolean value);
536
537  /**
538   * Returns topic value.
539   *
540   * @param entry Entry handle.
541   * @param defaultValue Default value.
542   * @return Topic value.
543   */
544  public static native boolean getBoolean(int entry, boolean defaultValue);
545
546  /**
547   * Sets default topic value.
548   *
549   * @param entry Entry handle.
550   * @param time Time in microseconds.
551   * @param defaultValue Default value.
552   * @return True if set succeeded.
553   */
554  public static native boolean setDefaultBoolean(int entry, long time, boolean defaultValue);
555
556
557  /**
558   * Returns timestamped topic value as an atomic Integer.
559   *
560   * @param subentry Subentry handle.
561   * @param defaultValue Default value.
562   * @return Timestamped topic value.
563   */
564  public static native TimestampedInteger getAtomicInteger(
565      int subentry, long defaultValue);
566
567  /**
568   * Returns queued timestamped topic values.
569   *
570   * @param subentry Subentry handle.
571   * @return List of timestamped topic values.
572   */
573  public static native TimestampedInteger[] readQueueInteger(int subentry);
574
575  /**
576   * Returns queued topic values.
577   *
578   * @param subentry Subentry handle.
579   * @return List of topic values.
580   */
581  public static native long[] readQueueValuesInteger(int subentry);
582
583  /**
584   * Sets topic value.
585   *
586   * @param entry Entry handle.
587   * @param time Time in microseconds.
588   * @param value Topic value.
589   * @return True if set succeeded.
590   */
591  public static native boolean setInteger(int entry, long time, long value);
592
593  /**
594   * Returns topic value.
595   *
596   * @param entry Entry handle.
597   * @param defaultValue Default value.
598   * @return Topic value.
599   */
600  public static native long getInteger(int entry, long defaultValue);
601
602  /**
603   * Sets default topic value.
604   *
605   * @param entry Entry handle.
606   * @param time Time in microseconds.
607   * @param defaultValue Default value.
608   * @return True if set succeeded.
609   */
610  public static native boolean setDefaultInteger(int entry, long time, long defaultValue);
611
612
613  /**
614   * Returns timestamped topic value as an atomic Float.
615   *
616   * @param subentry Subentry handle.
617   * @param defaultValue Default value.
618   * @return Timestamped topic value.
619   */
620  public static native TimestampedFloat getAtomicFloat(
621      int subentry, float defaultValue);
622
623  /**
624   * Returns queued timestamped topic values.
625   *
626   * @param subentry Subentry handle.
627   * @return List of timestamped topic values.
628   */
629  public static native TimestampedFloat[] readQueueFloat(int subentry);
630
631  /**
632   * Returns queued topic values.
633   *
634   * @param subentry Subentry handle.
635   * @return List of topic values.
636   */
637  public static native float[] readQueueValuesFloat(int subentry);
638
639  /**
640   * Sets topic value.
641   *
642   * @param entry Entry handle.
643   * @param time Time in microseconds.
644   * @param value Topic value.
645   * @return True if set succeeded.
646   */
647  public static native boolean setFloat(int entry, long time, float value);
648
649  /**
650   * Returns topic value.
651   *
652   * @param entry Entry handle.
653   * @param defaultValue Default value.
654   * @return Topic value.
655   */
656  public static native float getFloat(int entry, float defaultValue);
657
658  /**
659   * Sets default topic value.
660   *
661   * @param entry Entry handle.
662   * @param time Time in microseconds.
663   * @param defaultValue Default value.
664   * @return True if set succeeded.
665   */
666  public static native boolean setDefaultFloat(int entry, long time, float defaultValue);
667
668
669  /**
670   * Returns timestamped topic value as an atomic Double.
671   *
672   * @param subentry Subentry handle.
673   * @param defaultValue Default value.
674   * @return Timestamped topic value.
675   */
676  public static native TimestampedDouble getAtomicDouble(
677      int subentry, double defaultValue);
678
679  /**
680   * Returns queued timestamped topic values.
681   *
682   * @param subentry Subentry handle.
683   * @return List of timestamped topic values.
684   */
685  public static native TimestampedDouble[] readQueueDouble(int subentry);
686
687  /**
688   * Returns queued topic values.
689   *
690   * @param subentry Subentry handle.
691   * @return List of topic values.
692   */
693  public static native double[] readQueueValuesDouble(int subentry);
694
695  /**
696   * Sets topic value.
697   *
698   * @param entry Entry handle.
699   * @param time Time in microseconds.
700   * @param value Topic value.
701   * @return True if set succeeded.
702   */
703  public static native boolean setDouble(int entry, long time, double value);
704
705  /**
706   * Returns topic value.
707   *
708   * @param entry Entry handle.
709   * @param defaultValue Default value.
710   * @return Topic value.
711   */
712  public static native double getDouble(int entry, double defaultValue);
713
714  /**
715   * Sets default topic value.
716   *
717   * @param entry Entry handle.
718   * @param time Time in microseconds.
719   * @param defaultValue Default value.
720   * @return True if set succeeded.
721   */
722  public static native boolean setDefaultDouble(int entry, long time, double defaultValue);
723
724
725  /**
726   * Returns timestamped topic value as an atomic String.
727   *
728   * @param subentry Subentry handle.
729   * @param defaultValue Default value.
730   * @return Timestamped topic value.
731   */
732  public static native TimestampedString getAtomicString(
733      int subentry, String defaultValue);
734
735  /**
736   * Returns queued timestamped topic values.
737   *
738   * @param subentry Subentry handle.
739   * @return List of timestamped topic values.
740   */
741  public static native TimestampedString[] readQueueString(int subentry);
742
743  /**
744   * Returns queued topic values.
745   *
746   * @param subentry Subentry handle.
747   * @return List of topic values.
748   */
749  public static native String[] readQueueValuesString(int subentry);
750
751  /**
752   * Sets topic value.
753   *
754   * @param entry Entry handle.
755   * @param time Time in microseconds.
756   * @param value Topic value.
757   * @return True if set succeeded.
758   */
759  public static native boolean setString(int entry, long time, String value);
760
761  /**
762   * Returns topic value.
763   *
764   * @param entry Entry handle.
765   * @param defaultValue Default value.
766   * @return Topic value.
767   */
768  public static native String getString(int entry, String defaultValue);
769
770  /**
771   * Sets default topic value.
772   *
773   * @param entry Entry handle.
774   * @param time Time in microseconds.
775   * @param defaultValue Default value.
776   * @return True if set succeeded.
777   */
778  public static native boolean setDefaultString(int entry, long time, String defaultValue);
779
780
781  /**
782   * Returns timestamped topic value as an atomic Raw.
783   *
784   * @param subentry Subentry handle.
785   * @param defaultValue Default value.
786   * @return Timestamped topic value.
787   */
788  public static native TimestampedRaw getAtomicRaw(
789      int subentry, byte[] defaultValue);
790
791  /**
792   * Returns queued timestamped topic values.
793   *
794   * @param subentry Subentry handle.
795   * @return List of timestamped topic values.
796   */
797  public static native TimestampedRaw[] readQueueRaw(int subentry);
798
799  /**
800   * Returns queued topic values.
801   *
802   * @param subentry Subentry handle.
803   * @return List of topic values.
804   */
805  public static native byte[][] readQueueValuesRaw(int subentry);
806
807  /**
808   * Sets raw topic value.
809   *
810   * @param entry Entry handle.
811   * @param time Time in microseconds.
812   * @param value Raw value buffer.
813   * @return True if set succeeded.
814   */
815  public static boolean setRaw(int entry, long time, byte[] value) {
816    return setRaw(entry, time, value, 0, value.length);
817  }
818
819  /**
820   * Sets raw topic value.
821   *
822   * @param entry Entry handle.
823   * @param time Time in microseconds.
824   * @param value Raw value buffer.
825   * @param start Value's offset into buffer.
826   * @param len Length of value in buffer.
827   * @return True if set succeeded.
828   */
829  public static native boolean setRaw(int entry, long time, byte[] value, int start, int len);
830
831  /**
832   * Sets raw topic value.
833   *
834   * @param entry Entry handle.
835   * @param time Time in microseconds.
836   * @param value Raw value buffer.
837   * @return True if set succeeded.
838   */
839  public static boolean setRaw(int entry, long time, ByteBuffer value) {
840    int pos = value.position();
841    return setRaw(entry, time, value, pos, value.capacity() - pos);
842  }
843
844  /**
845   * Sets raw topic value.
846   *
847   * @param entry Entry handle.
848   * @param time Time in microseconds.
849   * @param value Raw value buffer.
850   * @param start Value's offset into buffer.
851   * @param len Length of value in buffer.
852   * @return True if set succeeded.
853   */
854  public static boolean setRaw(int entry, long time, ByteBuffer value, int start, int len) {
855    if (value.isDirect()) {
856      if (start < 0) {
857        throw new IndexOutOfBoundsException("start must be >= 0");
858      }
859      if (len < 0) {
860        throw new IndexOutOfBoundsException("len must be >= 0");
861      }
862      if ((start + len) > value.capacity()) {
863        throw new IndexOutOfBoundsException("start + len must be smaller than buffer capacity");
864      }
865      return setRawBuffer(entry, time, value, start, len);
866    } else if (value.hasArray()) {
867      return setRaw(entry, time, value.array(), value.arrayOffset() + start, len);
868    } else {
869      throw new UnsupportedOperationException("ByteBuffer must be direct or have a backing array");
870    }
871  }
872
873  /**
874   * Sets raw topic value buffer.
875   *
876   * @param entry Entry handle.
877   * @param time Time in microseconds.
878   * @param value Raw value buffer.
879   * @param start Value's offset into buffer.
880   * @param len Length of value in buffer.
881   * @return True if set succeeded.
882   */
883  private static native boolean setRawBuffer(int entry, long time, ByteBuffer value, int start, int len);
884
885  /**
886   * Returns topic value.
887   *
888   * @param entry Entry handle.
889   * @param defaultValue Default value.
890   * @return Topic value.
891   */
892  public static native byte[] getRaw(int entry, byte[] defaultValue);
893
894  /**
895   * Sets default raw topic value.
896   *
897   * @param entry Entry handle.
898   * @param time Time in microseconds.
899   * @param defaultValue Default value.
900   * @return True if set succeeded.
901   */
902  public static boolean setDefaultRaw(int entry, long time, byte[] defaultValue) {
903    return setDefaultRaw(entry, time, defaultValue, 0, defaultValue.length);
904  }
905
906  /**
907   * Sets default raw topic value.
908   *
909   * @param entry Entry handle.
910   * @param time Time in microseconds.
911   * @param defaultValue Default value.
912   * @param start Value's offset into buffer.
913   * @param len Length of value in buffer.
914   * @return True if set succeeded.
915   */
916  public static native boolean setDefaultRaw(int entry, long time, byte[] defaultValue, int start, int len);
917
918  /**
919   * Sets default raw topic value.
920   *
921   * @param entry Entry handle.
922   * @param time Time in microseconds.
923   * @param defaultValue Default value.
924   * @return True if set succeeded.
925   */
926  public static boolean setDefaultRaw(int entry, long time, ByteBuffer defaultValue) {
927    int pos = defaultValue.position();
928    return setDefaultRaw(entry, time, defaultValue, pos, defaultValue.limit() - pos);
929  }
930
931  /**
932   * Sets default raw topic value.
933   *
934   * @param entry Entry handle.
935   * @param time Time in microseconds.
936   * @param defaultValue Default value.
937   * @param start Value's offset into buffer.
938   * @param len Length of value in buffer.
939   * @return True if set succeeded.
940   */
941  public static boolean setDefaultRaw(int entry, long time, ByteBuffer defaultValue, int start, int len) {
942    if (defaultValue.isDirect()) {
943      if (start < 0) {
944        throw new IndexOutOfBoundsException("start must be >= 0");
945      }
946      if (len < 0) {
947        throw new IndexOutOfBoundsException("len must be >= 0");
948      }
949      if ((start + len) > defaultValue.capacity()) {
950        throw new IndexOutOfBoundsException("start + len must be smaller than buffer capacity");
951      }
952      return setDefaultRawBuffer(entry, time, defaultValue, start, len);
953    } else if (defaultValue.hasArray()) {
954      return setDefaultRaw(entry, time, defaultValue.array(), defaultValue.arrayOffset() + start, len);
955    } else {
956      throw new UnsupportedOperationException("ByteBuffer must be direct or have a backing array");
957    }
958  }
959
960  /**
961   * Sets default raw topic value buffer.
962   *
963   * @param entry Entry handle.
964   * @param time Time in microseconds.
965   * @param defaultValue Default value.
966   * @param start Value's offset into buffer.
967   * @param len Length of value in buffer.
968   * @return True if set succeeded.
969   */
970  private static native boolean setDefaultRawBuffer(int entry, long time, ByteBuffer defaultValue, int start, int len);
971
972
973  /**
974   * Returns timestamped topic value as an atomic BooleanArray.
975   *
976   * @param subentry Subentry handle.
977   * @param defaultValue Default value.
978   * @return Timestamped topic value.
979   */
980  public static native TimestampedBooleanArray getAtomicBooleanArray(
981      int subentry, boolean[] defaultValue);
982
983  /**
984   * Returns queued timestamped topic values.
985   *
986   * @param subentry Subentry handle.
987   * @return List of timestamped topic values.
988   */
989  public static native TimestampedBooleanArray[] readQueueBooleanArray(int subentry);
990
991  /**
992   * Returns queued topic values.
993   *
994   * @param subentry Subentry handle.
995   * @return List of topic values.
996   */
997  public static native boolean[][] readQueueValuesBooleanArray(int subentry);
998
999  /**
1000   * Sets topic value.
1001   *
1002   * @param entry Entry handle.
1003   * @param time Time in microseconds.
1004   * @param value Topic value.
1005   * @return True if set succeeded.
1006   */
1007  public static native boolean setBooleanArray(int entry, long time, boolean[] value);
1008
1009  /**
1010   * Returns topic value.
1011   *
1012   * @param entry Entry handle.
1013   * @param defaultValue Default value.
1014   * @return Topic value.
1015   */
1016  public static native boolean[] getBooleanArray(int entry, boolean[] defaultValue);
1017
1018  /**
1019   * Sets default topic value.
1020   *
1021   * @param entry Entry handle.
1022   * @param time Time in microseconds.
1023   * @param defaultValue Default value.
1024   * @return True if set succeeded.
1025   */
1026  public static native boolean setDefaultBooleanArray(int entry, long time, boolean[] defaultValue);
1027
1028
1029  /**
1030   * Returns timestamped topic value as an atomic IntegerArray.
1031   *
1032   * @param subentry Subentry handle.
1033   * @param defaultValue Default value.
1034   * @return Timestamped topic value.
1035   */
1036  public static native TimestampedIntegerArray getAtomicIntegerArray(
1037      int subentry, long[] defaultValue);
1038
1039  /**
1040   * Returns queued timestamped topic values.
1041   *
1042   * @param subentry Subentry handle.
1043   * @return List of timestamped topic values.
1044   */
1045  public static native TimestampedIntegerArray[] readQueueIntegerArray(int subentry);
1046
1047  /**
1048   * Returns queued topic values.
1049   *
1050   * @param subentry Subentry handle.
1051   * @return List of topic values.
1052   */
1053  public static native long[][] readQueueValuesIntegerArray(int subentry);
1054
1055  /**
1056   * Sets topic value.
1057   *
1058   * @param entry Entry handle.
1059   * @param time Time in microseconds.
1060   * @param value Topic value.
1061   * @return True if set succeeded.
1062   */
1063  public static native boolean setIntegerArray(int entry, long time, long[] value);
1064
1065  /**
1066   * Returns topic value.
1067   *
1068   * @param entry Entry handle.
1069   * @param defaultValue Default value.
1070   * @return Topic value.
1071   */
1072  public static native long[] getIntegerArray(int entry, long[] defaultValue);
1073
1074  /**
1075   * Sets default topic value.
1076   *
1077   * @param entry Entry handle.
1078   * @param time Time in microseconds.
1079   * @param defaultValue Default value.
1080   * @return True if set succeeded.
1081   */
1082  public static native boolean setDefaultIntegerArray(int entry, long time, long[] defaultValue);
1083
1084
1085  /**
1086   * Returns timestamped topic value as an atomic FloatArray.
1087   *
1088   * @param subentry Subentry handle.
1089   * @param defaultValue Default value.
1090   * @return Timestamped topic value.
1091   */
1092  public static native TimestampedFloatArray getAtomicFloatArray(
1093      int subentry, float[] defaultValue);
1094
1095  /**
1096   * Returns queued timestamped topic values.
1097   *
1098   * @param subentry Subentry handle.
1099   * @return List of timestamped topic values.
1100   */
1101  public static native TimestampedFloatArray[] readQueueFloatArray(int subentry);
1102
1103  /**
1104   * Returns queued topic values.
1105   *
1106   * @param subentry Subentry handle.
1107   * @return List of topic values.
1108   */
1109  public static native float[][] readQueueValuesFloatArray(int subentry);
1110
1111  /**
1112   * Sets topic value.
1113   *
1114   * @param entry Entry handle.
1115   * @param time Time in microseconds.
1116   * @param value Topic value.
1117   * @return True if set succeeded.
1118   */
1119  public static native boolean setFloatArray(int entry, long time, float[] value);
1120
1121  /**
1122   * Returns topic value.
1123   *
1124   * @param entry Entry handle.
1125   * @param defaultValue Default value.
1126   * @return Topic value.
1127   */
1128  public static native float[] getFloatArray(int entry, float[] defaultValue);
1129
1130  /**
1131   * Sets default topic value.
1132   *
1133   * @param entry Entry handle.
1134   * @param time Time in microseconds.
1135   * @param defaultValue Default value.
1136   * @return True if set succeeded.
1137   */
1138  public static native boolean setDefaultFloatArray(int entry, long time, float[] defaultValue);
1139
1140
1141  /**
1142   * Returns timestamped topic value as an atomic DoubleArray.
1143   *
1144   * @param subentry Subentry handle.
1145   * @param defaultValue Default value.
1146   * @return Timestamped topic value.
1147   */
1148  public static native TimestampedDoubleArray getAtomicDoubleArray(
1149      int subentry, double[] defaultValue);
1150
1151  /**
1152   * Returns queued timestamped topic values.
1153   *
1154   * @param subentry Subentry handle.
1155   * @return List of timestamped topic values.
1156   */
1157  public static native TimestampedDoubleArray[] readQueueDoubleArray(int subentry);
1158
1159  /**
1160   * Returns queued topic values.
1161   *
1162   * @param subentry Subentry handle.
1163   * @return List of topic values.
1164   */
1165  public static native double[][] readQueueValuesDoubleArray(int subentry);
1166
1167  /**
1168   * Sets topic value.
1169   *
1170   * @param entry Entry handle.
1171   * @param time Time in microseconds.
1172   * @param value Topic value.
1173   * @return True if set succeeded.
1174   */
1175  public static native boolean setDoubleArray(int entry, long time, double[] value);
1176
1177  /**
1178   * Returns topic value.
1179   *
1180   * @param entry Entry handle.
1181   * @param defaultValue Default value.
1182   * @return Topic value.
1183   */
1184  public static native double[] getDoubleArray(int entry, double[] defaultValue);
1185
1186  /**
1187   * Sets default topic value.
1188   *
1189   * @param entry Entry handle.
1190   * @param time Time in microseconds.
1191   * @param defaultValue Default value.
1192   * @return True if set succeeded.
1193   */
1194  public static native boolean setDefaultDoubleArray(int entry, long time, double[] defaultValue);
1195
1196
1197  /**
1198   * Returns timestamped topic value as an atomic StringArray.
1199   *
1200   * @param subentry Subentry handle.
1201   * @param defaultValue Default value.
1202   * @return Timestamped topic value.
1203   */
1204  public static native TimestampedStringArray getAtomicStringArray(
1205      int subentry, String[] defaultValue);
1206
1207  /**
1208   * Returns queued timestamped topic values.
1209   *
1210   * @param subentry Subentry handle.
1211   * @return List of timestamped topic values.
1212   */
1213  public static native TimestampedStringArray[] readQueueStringArray(int subentry);
1214
1215  /**
1216   * Returns queued topic values.
1217   *
1218   * @param subentry Subentry handle.
1219   * @return List of topic values.
1220   */
1221  public static native String[][] readQueueValuesStringArray(int subentry);
1222
1223  /**
1224   * Sets topic value.
1225   *
1226   * @param entry Entry handle.
1227   * @param time Time in microseconds.
1228   * @param value Topic value.
1229   * @return True if set succeeded.
1230   */
1231  public static native boolean setStringArray(int entry, long time, String[] value);
1232
1233  /**
1234   * Returns topic value.
1235   *
1236   * @param entry Entry handle.
1237   * @param defaultValue Default value.
1238   * @return Topic value.
1239   */
1240  public static native String[] getStringArray(int entry, String[] defaultValue);
1241
1242  /**
1243   * Sets default topic value.
1244   *
1245   * @param entry Entry handle.
1246   * @param time Time in microseconds.
1247   * @param defaultValue Default value.
1248   * @return True if set succeeded.
1249   */
1250  public static native boolean setDefaultStringArray(int entry, long time, String[] defaultValue);
1251
1252
1253  /**
1254   * Returns queued subentry values.
1255   *
1256   * @param subentry Subentry handle.
1257   * @return List of queued subentry values.
1258   */
1259  public static native NetworkTableValue[] readQueueValue(int subentry);
1260
1261  /**
1262   * Returns entry's NT value.
1263   *
1264   * @param entry Entry handle.
1265   * @return Entry's NT value.
1266   */
1267  public static native NetworkTableValue getValue(int entry);
1268
1269  /**
1270   * Sets entry flags.
1271   *
1272   * @param entry Entry handle.
1273   * @param flags Entry flags.
1274   */
1275  public static native void setEntryFlags(int entry, int flags);
1276
1277  /**
1278   * Returns entry flags.
1279   *
1280   * @param entry Entry handle.
1281   * @return Entry flags.
1282   */
1283  public static native int getEntryFlags(int entry);
1284
1285  /**
1286   * Returns topic info.
1287   *
1288   * @param inst NT instance handle.
1289   * @param topic Topic handle.
1290   * @return Topic info.
1291   */
1292  public static native TopicInfo getTopicInfo(NetworkTableInstance inst, int topic);
1293
1294  /**
1295   * Creates a listener poller.
1296   *
1297   * @param inst NT instance handle.
1298   * @return Listener poller handle.
1299   */
1300  public static native int createListenerPoller(int inst);
1301
1302  /**
1303   * Destroys listener poller.
1304   *
1305   * @param poller Listener poller handle.
1306   */
1307  public static native void destroyListenerPoller(int poller);
1308
1309  /**
1310   * Converts NT event kinds to mask.
1311   *
1312   * @param kinds Enum set of NT event kinds.
1313   * @return NT event mask.
1314   */
1315  private static int kindsToMask(EnumSet<NetworkTableEvent.Kind> kinds) {
1316    int mask = 0;
1317    for (NetworkTableEvent.Kind kind : kinds) {
1318      mask |= kind.getValue();
1319    }
1320    return mask;
1321  }
1322
1323  /**
1324   * Adds listener.
1325   *
1326   * @param poller Listener poller handle.
1327   * @param prefixes Topic prefixes.
1328   * @param kinds Enum set of NT event kinds.
1329   * @return Listener handle.
1330   */
1331  public static int addListener(int poller, String[] prefixes, EnumSet<NetworkTableEvent.Kind> kinds) {
1332    return addListener(poller, prefixes, kindsToMask(kinds));
1333  }
1334
1335  /**
1336   * Adds listener.
1337   *
1338   * @param poller Listener poller handle.
1339   * @param handle Topic handle.
1340   * @param kinds Enum set of NT event kinds.
1341   * @return Listener handle.
1342   */
1343  public static int addListener(int poller, int handle, EnumSet<NetworkTableEvent.Kind> kinds) {
1344    return addListener(poller, handle, kindsToMask(kinds));
1345  }
1346
1347  /**
1348   * Adds listener.
1349   *
1350   * @param poller Listener poller handle.
1351   * @param prefixes Topic prefixes.
1352   * @param mask NT event mask.
1353   * @return Listener handle.
1354   */
1355  public static native int addListener(int poller, String[] prefixes, int mask);
1356
1357  /**
1358   * Adds listener.
1359   *
1360   * @param poller Listener poller handle.
1361   * @param handle Topic handle.
1362   * @param mask NT event mask.
1363   * @return Listener handle.
1364   */
1365  public static native int addListener(int poller, int handle, int mask);
1366
1367  /**
1368   * Returns NT events from listener queue.
1369   *
1370   * @param inst NT instance handle.
1371   * @param poller Listener poller handle.
1372   * @return List of NT events.
1373   */
1374  public static native NetworkTableEvent[] readListenerQueue(
1375      NetworkTableInstance inst, int poller);
1376
1377  /**
1378   * Removes listener.
1379   *
1380   * @param listener Listener handle.
1381   */
1382  public static native void removeListener(int listener);
1383
1384  /**
1385   * Returns network mode.
1386   *
1387   * @param inst NT instance handle.
1388   * @return Network mode.
1389   */
1390  public static native int getNetworkMode(int inst);
1391
1392  /**
1393   * Starts local-only operation. Prevents calls to startServer or startClient from taking effect.
1394   * Has no effect if startServer or startClient has already been called.
1395   *
1396   * @param inst NT instance handle.
1397   */
1398  public static native void startLocal(int inst);
1399
1400  /**
1401   * Stops local-only operation. startServer or startClient can be called after this call to start
1402   * a server or client.
1403   *
1404   * @param inst NT instance handle.
1405   */
1406  public static native void stopLocal(int inst);
1407
1408  /**
1409   * Starts a server using the specified filename, listening address, and port.
1410   *
1411   * @param inst NT instance handle.
1412   * @param persistFilename the name of the persist file to use
1413   * @param listenAddress the address to listen on, or empty to listen on any address
1414   * @param port3 port to communicate over (NT3)
1415   * @param port4 port to communicate over (NT4)
1416   */
1417  public static native void startServer(
1418      int inst, String persistFilename, String listenAddress, int port3, int port4);
1419
1420  /**
1421   * Stops the server if it is running.
1422   *
1423   * @param inst NT instance handle.
1424   */
1425  public static native void stopServer(int inst);
1426
1427  /**
1428   * Starts a NT3 client. Use SetServer or SetServerTeam to set the server name and port.
1429   *
1430   * @param inst NT instance handle.
1431   * @param identity network identity to advertise (cannot be empty string)
1432   */
1433  public static native void startClient3(int inst, String identity);
1434
1435  /**
1436   * Starts a NT4 client. Use SetServer or SetServerTeam to set the server name and port.
1437   *
1438   * @param inst NT instance handle.
1439   * @param identity network identity to advertise (cannot be empty string)
1440   */
1441  public static native void startClient4(int inst, String identity);
1442
1443  /**
1444   * Stops the client if it is running.
1445   *
1446   * @param inst NT instance handle.
1447   */
1448  public static native void stopClient(int inst);
1449
1450  /**
1451   * Sets server address and port for client (without restarting client).
1452   *
1453   * @param inst NT instance handle.
1454   * @param serverName server name
1455   * @param port port to communicate over
1456   */
1457  public static native void setServer(int inst, String serverName, int port);
1458
1459  /**
1460   * Sets server addresses and ports for client (without restarting client). The client will
1461   * attempt to connect to each server in round robin fashion.
1462   *
1463   * @param inst NT instance handle.
1464   * @param serverNames array of server names
1465   * @param ports array of port numbers (0=default)
1466   */
1467  public static native void setServer(int inst, String[] serverNames, int[] ports);
1468
1469  /**
1470   * Sets server addresses and port for client (without restarting client). Connects using commonly
1471   * known robot addresses for the specified team.
1472   *
1473   * @param inst NT instance handle.
1474   * @param team team number
1475   * @param port port to communicate over
1476   */
1477  public static native void setServerTeam(int inst, int team, int port);
1478
1479  /**
1480   * Disconnects the client if it's running and connected. This will automatically start
1481   * reconnection attempts to the current server list.
1482   *
1483   * @param inst NT instance handle.
1484   */
1485  public static native void disconnect(int inst);
1486
1487  /**
1488   * Starts requesting server address from Driver Station. This connects to the Driver Station
1489   * running on localhost to obtain the server IP address.
1490   *
1491   * @param inst NT instance handle.
1492   * @param port server port to use in combination with IP from DS
1493   */
1494  public static native void startDSClient(int inst, int port);
1495
1496  /**
1497   * Stops requesting server address from Driver Station.
1498   *
1499   * @param inst NT instance handle.
1500   */
1501  public static native void stopDSClient(int inst);
1502
1503  /**
1504   * Flushes all updated values immediately to the local client/server. This does not flush to the
1505   * network.
1506   *
1507   * @param inst NT instance handle.
1508   */
1509  public static native void flushLocal(int inst);
1510
1511  /**
1512   * Flushes all updated values immediately to the network. Note: This is rate-limited to protect
1513   * the network from flooding. This is primarily useful for synchronizing network updates with
1514   * user code.
1515   *
1516   * @param inst NT instance handle.
1517   */
1518  public static native void flush(int inst);
1519
1520  /**
1521   * Gets information on the currently established network connections. If operating as a client,
1522   * this will return either zero or one values.
1523   *
1524   * @param inst NT instance handle.
1525   * @return array of connection information
1526   */
1527  public static native ConnectionInfo[] getConnections(int inst);
1528
1529  /**
1530   * Return whether or not the instance is connected to another node.
1531   *
1532   * @param inst NT instance handle.
1533   * @return True if connected.
1534   */
1535  public static native boolean isConnected(int inst);
1536
1537  /**
1538   * Get the time offset between server time and local time. Add this value to local time to get
1539   * the estimated equivalent server time. In server mode, this always returns 0. In client mode,
1540   * this returns the time offset only if the client and server are connected and have exchanged
1541   * synchronization messages. Note the time offset may change over time as it is periodically
1542   * updated; to receive updates as events, add a listener to the "time sync" event.
1543   *
1544   * @param inst NT instance handle.
1545   * @return Time offset in microseconds (optional)
1546   */
1547  public static native OptionalLong getServerTimeOffset(int inst);
1548
1549  /**
1550   * Returns the current timestamp in microseconds.
1551   *
1552   * @return The current timestsamp in microseconds.
1553   */
1554  public static native long now();
1555
1556  /**
1557   * Starts logging entry changes to a DataLog.
1558   *
1559   * @param inst NT instance handle.
1560   * @param log data log handle; lifetime must extend until StopEntryDataLog is called or the
1561   *     instance is destroyed
1562   * @param prefix only store entries with names that start with this prefix; the prefix is not
1563   *     included in the data log entry name
1564   * @param logPrefix prefix to add to data log entry names
1565   * @return Data logger handle
1566   */
1567  private static native int startEntryDataLog(int inst, long log, String prefix, String logPrefix);
1568
1569  /**
1570   * Starts logging entry changes to a DataLog.
1571   *
1572   * @param inst NT instance handle.
1573   * @param log data log object; lifetime must extend until StopEntryDataLog is called or the
1574   *     instance is destroyed
1575   * @param prefix only store entries with names that start with this prefix; the prefix is not
1576   *     included in the data log entry name
1577   * @param logPrefix prefix to add to data log entry names
1578   * @return Data logger handle
1579   */
1580  public static int startEntryDataLog(int inst, DataLog log, String prefix, String logPrefix) {
1581    return startEntryDataLog(inst, log.getImpl(), prefix, logPrefix);
1582  }
1583
1584  /**
1585   * Stops logging entry changes to a DataLog.
1586   *
1587   * @param logger data logger handle
1588   */
1589  public static native void stopEntryDataLog(int logger);
1590
1591  /**
1592   * Starts logging connection changes to a DataLog.
1593   *
1594   * @param inst NT instance handle.
1595   * @param log data log handle; lifetime must extend until StopConnectionDataLog is called or the
1596   *     instance is destroyed
1597   * @param name data log entry name
1598   * @return Data logger handle
1599   */
1600  private static native int startConnectionDataLog(int inst, long log, String name);
1601
1602  /**
1603   * Starts logging connection changes to a DataLog.
1604   *
1605   * @param inst NT instance handle.
1606   * @param log data log object; lifetime must extend until StopConnectionDataLog is called or the
1607   *     instance is destroyed
1608   * @param name data log entry name
1609   * @return Data logger handle
1610   */
1611  public static int startConnectionDataLog(int inst, DataLog log, String name) {
1612    return startConnectionDataLog(inst, log.getImpl(), name);
1613  }
1614
1615  /**
1616   * Stops logging connection changes to a DataLog.
1617   *
1618   * @param logger data logger handle
1619   */
1620  public static native void stopConnectionDataLog(int logger);
1621
1622  /**
1623   * Add logger callback function. By default, log messages are sent to stderr; this function sends
1624   * log messages with the specified levels to the provided callback function instead. The callback
1625   * function will only be called for log messages with level greater than or equal to minLevel and
1626   * less than or equal to maxLevel; messages outside this range will be silently ignored.
1627   *
1628   * @param poller Listener poller handle.
1629   * @param minLevel minimum log level
1630   * @param maxLevel maximum log level
1631   * @return Listener handle
1632   */
1633  public static native int addLogger(int poller, int minLevel, int maxLevel);
1634
1635  /** Utility class. */
1636  private NetworkTablesJNI() {}
1637}