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 java.io.IOException;
009import java.nio.ByteBuffer;
010import java.util.concurrent.atomic.AtomicBoolean;
011
012public final class NetworkTablesJNI {
013  static boolean libraryLoaded = false;
014  static RuntimeLoader<NetworkTablesJNI> loader = null;
015
016  public static class Helper {
017    private static AtomicBoolean extractOnStaticLoad = new AtomicBoolean(true);
018
019    public static boolean getExtractOnStaticLoad() {
020      return extractOnStaticLoad.get();
021    }
022
023    public static void setExtractOnStaticLoad(boolean load) {
024      extractOnStaticLoad.set(load);
025    }
026  }
027
028  static {
029    if (Helper.getExtractOnStaticLoad()) {
030      try {
031        loader =
032            new RuntimeLoader<>(
033                "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
034        loader.loadLibrary();
035      } catch (IOException ex) {
036        ex.printStackTrace();
037        System.exit(1);
038      }
039      libraryLoaded = true;
040    }
041  }
042
043  /**
044   * Force load the library.
045   *
046   * @throws IOException if the library fails to load
047   */
048  public static synchronized void forceLoad() throws IOException {
049    if (libraryLoaded) {
050      return;
051    }
052    loader =
053        new RuntimeLoader<>(
054            "ntcorejni", RuntimeLoader.getDefaultExtractionRoot(), NetworkTablesJNI.class);
055    loader.loadLibrary();
056    libraryLoaded = true;
057  }
058
059  public static native int getDefaultInstance();
060
061  public static native int createInstance();
062
063  public static native void destroyInstance(int inst);
064
065  public static native int getInstanceFromHandle(int handle);
066
067  public static native int getEntry(int inst, String key);
068
069  public static native int[] getEntries(int inst, String prefix, int types);
070
071  public static native String getEntryName(int entry);
072
073  public static native long getEntryLastChange(int entry);
074
075  public static native int getType(int entry);
076
077  public static native boolean setBoolean(int entry, long time, boolean value, boolean force);
078
079  public static native boolean setDouble(int entry, long time, double value, boolean force);
080
081  public static native boolean setString(int entry, long time, String value, boolean force);
082
083  public static native boolean setRaw(int entry, long time, byte[] value, boolean force);
084
085  public static native boolean setRaw(
086      int entry, long time, ByteBuffer value, int len, boolean force);
087
088  public static native boolean setBooleanArray(
089      int entry, long time, boolean[] value, boolean force);
090
091  public static native boolean setDoubleArray(int entry, long time, double[] value, boolean force);
092
093  public static native boolean setStringArray(int entry, long time, String[] value, boolean force);
094
095  public static native NetworkTableValue getValue(int entry);
096
097  public static native boolean getBoolean(int entry, boolean defaultValue);
098
099  public static native double getDouble(int entry, double defaultValue);
100
101  public static native String getString(int entry, String defaultValue);
102
103  public static native byte[] getRaw(int entry, byte[] defaultValue);
104
105  public static native boolean[] getBooleanArray(int entry, boolean[] defaultValue);
106
107  public static native double[] getDoubleArray(int entry, double[] defaultValue);
108
109  public static native String[] getStringArray(int entry, String[] defaultValue);
110
111  public static native boolean setDefaultBoolean(int entry, long time, boolean defaultValue);
112
113  public static native boolean setDefaultDouble(int entry, long time, double defaultValue);
114
115  public static native boolean setDefaultString(int entry, long time, String defaultValue);
116
117  public static native boolean setDefaultRaw(int entry, long time, byte[] defaultValue);
118
119  public static native boolean setDefaultBooleanArray(int entry, long time, boolean[] defaultValue);
120
121  public static native boolean setDefaultDoubleArray(int entry, long time, double[] defaultValue);
122
123  public static native boolean setDefaultStringArray(int entry, long time, String[] defaultValue);
124
125  public static native void setEntryFlags(int entry, int flags);
126
127  public static native int getEntryFlags(int entry);
128
129  public static native void deleteEntry(int entry);
130
131  public static native void deleteAllEntries(int inst);
132
133  public static native EntryInfo getEntryInfoHandle(NetworkTableInstance inst, int entry);
134
135  public static native EntryInfo[] getEntryInfo(
136      NetworkTableInstance instObject, int inst, String prefix, int types);
137
138  public static native int createEntryListenerPoller(int inst);
139
140  public static native void destroyEntryListenerPoller(int poller);
141
142  public static native int addPolledEntryListener(int poller, String prefix, int flags);
143
144  public static native int addPolledEntryListener(int poller, int entry, int flags);
145
146  public static native EntryNotification[] pollEntryListener(NetworkTableInstance inst, int poller)
147      throws InterruptedException;
148
149  public static native EntryNotification[] pollEntryListenerTimeout(
150      NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
151
152  public static native void cancelPollEntryListener(int poller);
153
154  public static native void removeEntryListener(int entryListener);
155
156  public static native boolean waitForEntryListenerQueue(int inst, double timeout);
157
158  public static native int createConnectionListenerPoller(int inst);
159
160  public static native void destroyConnectionListenerPoller(int poller);
161
162  public static native int addPolledConnectionListener(int poller, boolean immediateNotify);
163
164  public static native ConnectionNotification[] pollConnectionListener(
165      NetworkTableInstance inst, int poller) throws InterruptedException;
166
167  public static native ConnectionNotification[] pollConnectionListenerTimeout(
168      NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
169
170  public static native void cancelPollConnectionListener(int poller);
171
172  public static native void removeConnectionListener(int connListener);
173
174  public static native boolean waitForConnectionListenerQueue(int inst, double timeout);
175
176  public static native int createRpcCallPoller(int inst);
177
178  public static native void destroyRpcCallPoller(int poller);
179
180  public static native void createPolledRpc(int entry, byte[] def, int poller);
181
182  public static native RpcAnswer[] pollRpc(NetworkTableInstance inst, int poller)
183      throws InterruptedException;
184
185  public static native RpcAnswer[] pollRpcTimeout(
186      NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
187
188  public static native void cancelPollRpc(int poller);
189
190  public static native boolean waitForRpcCallQueue(int inst, double timeout);
191
192  public static native boolean postRpcResponse(int entry, int call, byte[] result);
193
194  public static native int callRpc(int entry, byte[] params);
195
196  public static native byte[] getRpcResult(int entry, int call);
197
198  public static native byte[] getRpcResult(int entry, int call, double timeout);
199
200  public static native void cancelRpcResult(int entry, int call);
201
202  public static native byte[] getRpc(int entry, byte[] defaultValue);
203
204  public static native void setNetworkIdentity(int inst, String name);
205
206  public static native int getNetworkMode(int inst);
207
208  public static native void startLocal(int inst);
209
210  public static native void stopLocal(int inst);
211
212  public static native void startServer(
213      int inst, String persistFilename, String listenAddress, int port);
214
215  public static native void stopServer(int inst);
216
217  public static native void startClient(int inst);
218
219  public static native void startClient(int inst, String serverName, int port);
220
221  public static native void startClient(int inst, String[] serverNames, int[] ports);
222
223  public static native void startClientTeam(int inst, int team, int port);
224
225  public static native void stopClient(int inst);
226
227  public static native void setServer(int inst, String serverName, int port);
228
229  public static native void setServer(int inst, String[] serverNames, int[] ports);
230
231  public static native void setServerTeam(int inst, int team, int port);
232
233  public static native void startDSClient(int inst, int port);
234
235  public static native void stopDSClient(int inst);
236
237  public static native void setUpdateRate(int inst, double interval);
238
239  public static native void flush(int inst);
240
241  public static native ConnectionInfo[] getConnections(int inst);
242
243  public static native boolean isConnected(int inst);
244
245  public static native void savePersistent(int inst, String filename) throws PersistentException;
246
247  public static native String[] loadPersistent(int inst, String filename)
248      throws PersistentException; // returns warnings
249
250  public static native void saveEntries(int inst, String filename, String prefix)
251      throws PersistentException;
252
253  public static native String[] loadEntries(int inst, String filename, String prefix)
254      throws PersistentException; // returns warnings
255
256  public static native long now();
257
258  public static native int createLoggerPoller(int inst);
259
260  public static native void destroyLoggerPoller(int poller);
261
262  public static native int addPolledLogger(int poller, int minLevel, int maxLevel);
263
264  public static native LogMessage[] pollLogger(NetworkTableInstance inst, int poller)
265      throws InterruptedException;
266
267  public static native LogMessage[] pollLoggerTimeout(
268      NetworkTableInstance inst, int poller, double timeout) throws InterruptedException;
269
270  public static native void cancelPollLogger(int poller);
271
272  public static native void removeLogger(int logger);
273
274  public static native boolean waitForLoggerQueue(int inst, double timeout);
275}