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 005package edu.wpi.first.networktables; 006 007import edu.wpi.first.util.RuntimeLoader; 008import edu.wpi.first.util.datalog.DataLog; 009import java.io.IOException; 010import java.util.EnumSet; 011import java.util.OptionalLong; 012import java.util.concurrent.atomic.AtomicBoolean; 013 014public final class NetworkTablesJNI { 015 static boolean libraryLoaded = false; 016 static RuntimeLoader<NetworkTablesJNI> loader = null; 017 018 public static class Helper { 019 private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true); 020 021 public static boolean getExtractOnStaticLoad() { 022 return extractOnStaticLoad.get(); 023 } 024 025 public static void setExtractOnStaticLoad(boolean load) { 026 extractOnStaticLoad.set(load); 027 } 028 } 029 030 static { 031 if (Helper.getExtractOnStaticLoad()) { 032 try { 033 loader = 034 new RuntimeLoader<>( 035 "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class); 036 loader.loadLibrary(); 037 } catch (IOException ex) { 038 ex.printStackTrace(); 039 System.exit(1); 040 } 041 libraryLoaded = true; 042 } 043 } 044 045 /** 046 * Force load the library. 047 * 048 * @throws IOException if the library fails to load 049 */ 050 public static synchronized void forceLoad() throws IOException { 051 if (libraryLoaded) { 052 return; 053 } 054 loader = 055 new RuntimeLoader<>( 056 "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class); 057 loader.loadLibrary(); 058 libraryLoaded = true; 059 } 060 061 private static PubSubOptions buildOptions(PubSubOption... options) { 062 if (options.length == 0) { 063 return null; // optimize common case (JNI checks for null) 064 } 065 return new PubSubOptions(options); 066 } 067 068 public static native int getDefaultInstance(); 069 070 public static native int createInstance(); 071 072 public static native void destroyInstance(int inst); 073 074 public static native int getInstanceFromHandle(int handle); 075 076 private static native int getEntryImpl( 077 int topic, int type, String typeStr, PubSubOptions options); 078 079 public static native int getEntry(int inst, String key); 080 081 public static int getEntry( 082 int topic, int type, String typeStr, PubSubOptions options) { 083 return getEntryImpl(topic, type, typeStr, options); 084 } 085 086 public static int getEntry( 087 int topic, int type, String typeStr, PubSubOption... options) { 088 return getEntryImpl(topic, type, typeStr, buildOptions(options)); 089 } 090 091 public static native String getEntryName(int entry); 092 093 public static native long getEntryLastChange(int entry); 094 095 public static native int getType(int entry); 096 097 /* Topic functions */ 098 099 public static native int[] getTopics(int inst, String prefix, int types); 100 101 public static native int[] getTopicsStr(int inst, String prefix, String[] types); 102 103 public static native TopicInfo[] getTopicInfos( 104 NetworkTableInstance instObject, int inst, String prefix, int types); 105 106 public static native TopicInfo[] getTopicInfosStr( 107 NetworkTableInstance instObject, int inst, String prefix, String[] types); 108 109 public static native int getTopic(int inst, String name); 110 111 public static native String getTopicName(int topic); 112 113 public static native int getTopicType(int topic); 114 115 public static native void setTopicPersistent(int topic, boolean value); 116 117 public static native boolean getTopicPersistent(int topic); 118 119 public static native void setTopicRetained(int topic, boolean value); 120 121 public static native boolean getTopicRetained(int topic); 122 123 public static native String getTopicTypeString(int topic); 124 125 public static native boolean getTopicExists(int topic); 126 127 public static native String getTopicProperty(int topic, String name); 128 129 public static native void setTopicProperty(int topic, String name, String value); 130 131 public static native void deleteTopicProperty(int topic, String name); 132 133 public static native String getTopicProperties(int topic); 134 135 public static native void setTopicProperties(int topic, String properties); 136 137 public static native int subscribe( 138 int topic, int type, String typeStr, PubSubOptions options); 139 140 public static int subscribe( 141 int topic, int type, String typeStr, PubSubOption... options) { 142 return subscribe(topic, type, typeStr, buildOptions(options)); 143 } 144 145 public static native void unsubscribe(int sub); 146 147 public static native int publish( 148 int topic, int type, String typeStr, PubSubOptions options); 149 150 public static int publish( 151 int topic, int type, String typeStr, PubSubOption... options) { 152 return publish(topic, type, typeStr, buildOptions(options)); 153 } 154 155 public static native int publishEx( 156 int topic, int type, String typeStr, String properties, PubSubOptions options); 157 158 public static int publishEx( 159 int topic, int type, String typeStr, String properties, PubSubOption... options) { 160 return publishEx(topic, type, typeStr, properties, buildOptions(options)); 161 } 162 163 public static native void unpublish(int pubentry); 164 165 public static native void releaseEntry(int entry); 166 167 public static native void release(int pubsubentry); 168 169 public static native int getTopicFromHandle(int pubsubentry); 170 171 public static native int subscribeMultiple(int inst, String[] prefixes, PubSubOptions options); 172 173 public static int subscribeMultiple(int inst, String[] prefixes, PubSubOption... options) { 174 return subscribeMultiple(inst, prefixes, buildOptions(options)); 175 } 176 177 public static native void unsubscribeMultiple(int sub); 178 179 public static native TimestampedBoolean getAtomicBoolean( 180 int subentry, boolean defaultValue); 181 182 public static native TimestampedBoolean[] readQueueBoolean(int subentry); 183 184 public static native boolean[] readQueueValuesBoolean(int subentry); 185 186 public static native boolean setBoolean(int entry, long time, boolean value); 187 188 public static native boolean getBoolean(int entry, boolean defaultValue); 189 190 public static native boolean setDefaultBoolean(int entry, long time, boolean defaultValue); 191 192 public static native TimestampedInteger getAtomicInteger( 193 int subentry, long defaultValue); 194 195 public static native TimestampedInteger[] readQueueInteger(int subentry); 196 197 public static native long[] readQueueValuesInteger(int subentry); 198 199 public static native boolean setInteger(int entry, long time, long value); 200 201 public static native long getInteger(int entry, long defaultValue); 202 203 public static native boolean setDefaultInteger(int entry, long time, long defaultValue); 204 205 public static native TimestampedFloat getAtomicFloat( 206 int subentry, float defaultValue); 207 208 public static native TimestampedFloat[] readQueueFloat(int subentry); 209 210 public static native float[] readQueueValuesFloat(int subentry); 211 212 public static native boolean setFloat(int entry, long time, float value); 213 214 public static native float getFloat(int entry, float defaultValue); 215 216 public static native boolean setDefaultFloat(int entry, long time, float defaultValue); 217 218 public static native TimestampedDouble getAtomicDouble( 219 int subentry, double defaultValue); 220 221 public static native TimestampedDouble[] readQueueDouble(int subentry); 222 223 public static native double[] readQueueValuesDouble(int subentry); 224 225 public static native boolean setDouble(int entry, long time, double value); 226 227 public static native double getDouble(int entry, double defaultValue); 228 229 public static native boolean setDefaultDouble(int entry, long time, double defaultValue); 230 231 public static native TimestampedString getAtomicString( 232 int subentry, String defaultValue); 233 234 public static native TimestampedString[] readQueueString(int subentry); 235 236 public static native String[] readQueueValuesString(int subentry); 237 238 public static native boolean setString(int entry, long time, String value); 239 240 public static native String getString(int entry, String defaultValue); 241 242 public static native boolean setDefaultString(int entry, long time, String defaultValue); 243 244 public static native TimestampedRaw getAtomicRaw( 245 int subentry, byte[] defaultValue); 246 247 public static native TimestampedRaw[] readQueueRaw(int subentry); 248 249 public static native byte[][] readQueueValuesRaw(int subentry); 250 251 public static native boolean setRaw(int entry, long time, byte[] value); 252 253 public static native byte[] getRaw(int entry, byte[] defaultValue); 254 255 public static native boolean setDefaultRaw(int entry, long time, byte[] defaultValue); 256 257 public static native TimestampedBooleanArray getAtomicBooleanArray( 258 int subentry, boolean[] defaultValue); 259 260 public static native TimestampedBooleanArray[] readQueueBooleanArray(int subentry); 261 262 public static native boolean[][] readQueueValuesBooleanArray(int subentry); 263 264 public static native boolean setBooleanArray(int entry, long time, boolean[] value); 265 266 public static native boolean[] getBooleanArray(int entry, boolean[] defaultValue); 267 268 public static native boolean setDefaultBooleanArray(int entry, long time, boolean[] defaultValue); 269 270 public static native TimestampedIntegerArray getAtomicIntegerArray( 271 int subentry, long[] defaultValue); 272 273 public static native TimestampedIntegerArray[] readQueueIntegerArray(int subentry); 274 275 public static native long[][] readQueueValuesIntegerArray(int subentry); 276 277 public static native boolean setIntegerArray(int entry, long time, long[] value); 278 279 public static native long[] getIntegerArray(int entry, long[] defaultValue); 280 281 public static native boolean setDefaultIntegerArray(int entry, long time, long[] defaultValue); 282 283 public static native TimestampedFloatArray getAtomicFloatArray( 284 int subentry, float[] defaultValue); 285 286 public static native TimestampedFloatArray[] readQueueFloatArray(int subentry); 287 288 public static native float[][] readQueueValuesFloatArray(int subentry); 289 290 public static native boolean setFloatArray(int entry, long time, float[] value); 291 292 public static native float[] getFloatArray(int entry, float[] defaultValue); 293 294 public static native boolean setDefaultFloatArray(int entry, long time, float[] defaultValue); 295 296 public static native TimestampedDoubleArray getAtomicDoubleArray( 297 int subentry, double[] defaultValue); 298 299 public static native TimestampedDoubleArray[] readQueueDoubleArray(int subentry); 300 301 public static native double[][] readQueueValuesDoubleArray(int subentry); 302 303 public static native boolean setDoubleArray(int entry, long time, double[] value); 304 305 public static native double[] getDoubleArray(int entry, double[] defaultValue); 306 307 public static native boolean setDefaultDoubleArray(int entry, long time, double[] defaultValue); 308 309 public static native TimestampedStringArray getAtomicStringArray( 310 int subentry, String[] defaultValue); 311 312 public static native TimestampedStringArray[] readQueueStringArray(int subentry); 313 314 public static native String[][] readQueueValuesStringArray(int subentry); 315 316 public static native boolean setStringArray(int entry, long time, String[] value); 317 318 public static native String[] getStringArray(int entry, String[] defaultValue); 319 320 public static native boolean setDefaultStringArray(int entry, long time, String[] defaultValue); 321 322 public static native NetworkTableValue[] readQueueValue(int subentry); 323 324 public static native NetworkTableValue getValue(int entry); 325 326 public static native void setEntryFlags(int entry, int flags); 327 328 public static native int getEntryFlags(int entry); 329 330 public static native TopicInfo getTopicInfo(NetworkTableInstance inst, int topic); 331 332 public static native int createListenerPoller(int inst); 333 334 public static native void destroyListenerPoller(int poller); 335 336 private static int kindsToMask(EnumSet<NetworkTableEvent.Kind> kinds) { 337 int mask = 0; 338 for (NetworkTableEvent.Kind kind : kinds) { 339 mask |= kind.getValue(); 340 } 341 return mask; 342 } 343 344 public static int addListener(int poller, String[] prefixes, EnumSet<NetworkTableEvent.Kind> kinds) { 345 return addListener(poller, prefixes, kindsToMask(kinds)); 346 } 347 348 public static int addListener(int poller, int handle, EnumSet<NetworkTableEvent.Kind> kinds) { 349 return addListener(poller, handle, kindsToMask(kinds)); 350 } 351 352 public static native int addListener(int poller, String[] prefixes, int mask); 353 354 public static native int addListener(int poller, int handle, int mask); 355 356 public static native NetworkTableEvent[] readListenerQueue( 357 NetworkTableInstance inst, int poller); 358 359 public static native void removeListener(int listener); 360 361 public static native int getNetworkMode(int inst); 362 363 public static native void startLocal(int inst); 364 365 public static native void stopLocal(int inst); 366 367 public static native void startServer( 368 int inst, String persistFilename, String listenAddress, int port3, int port4); 369 370 public static native void stopServer(int inst); 371 372 public static native void startClient3(int inst, String identity); 373 374 public static native void startClient4(int inst, String identity); 375 376 public static native void stopClient(int inst); 377 378 public static native void setServer(int inst, String serverName, int port); 379 380 public static native void setServer(int inst, String[] serverNames, int[] ports); 381 382 public static native void setServerTeam(int inst, int team, int port); 383 384 public static native void disconnect(int inst); 385 386 public static native void startDSClient(int inst, int port); 387 388 public static native void stopDSClient(int inst); 389 390 public static native void flushLocal(int inst); 391 392 public static native void flush(int inst); 393 394 public static native ConnectionInfo[] getConnections(int inst); 395 396 public static native boolean isConnected(int inst); 397 398 public static native OptionalLong getServerTimeOffset(int inst); 399 400 public static native long now(); 401 402 private static native int startEntryDataLog(int inst, long log, String prefix, String logPrefix); 403 404 public static int startEntryDataLog(int inst, DataLog log, String prefix, String logPrefix) { 405 return startEntryDataLog(inst, log.getImpl(), prefix, logPrefix); 406 } 407 408 public static native void stopEntryDataLog(int logger); 409 410 private static native int startConnectionDataLog(int inst, long log, String name); 411 412 public static int startConnectionDataLog(int inst, DataLog log, String name) { 413 return startConnectionDataLog(inst, log.getImpl(), name); 414 } 415 416 public static native void stopConnectionDataLog(int logger); 417 418 public static native int addLogger(int poller, int minLevel, int maxLevel); 419}