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