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