-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Ganglion v3 firmware detection
- Loading branch information
1 parent
b2a8839
commit f444bfe
Showing
10 changed files
with
166 additions
and
111 deletions.
There are no files selected for viewing
134 changes: 67 additions & 67 deletions
134
java-package/openbci_gui_helpers/src/main/java/openbci_gui_helpers/GUIHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,128 @@ | ||
package openbci_gui_helpers; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.ArrayList; | ||
|
||
import org.apache.commons.lang3.SystemUtils; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.sun.jna.Library; | ||
import com.sun.jna.Native; | ||
|
||
public class GUIHelper | ||
{ | ||
private interface DllInterface extends Library | ||
{ | ||
int scan_for_ganglions (String serial_port, int timeout_sec, byte[] output, int[] output_len); | ||
public class GUIHelper { | ||
public class GanglionDevice { | ||
public int firmware_version; | ||
public String identifier; | ||
public String mac_address; | ||
} | ||
|
||
private interface DllNativeInterface extends Library | ||
{ | ||
int scan_for_ganglions (int timeout_sec, byte[] output, int[] output_len); | ||
private interface DllInterface extends Library { | ||
int scan_for_ganglions(String serial_port, int timeout_sec, byte[] output, int[] output_len); | ||
} | ||
|
||
private interface DllNativeInterface extends Library { | ||
int scan_for_ganglions(int timeout_sec, byte[] output, int[] output_len); | ||
} | ||
|
||
private static DllInterface instance; | ||
private static DllNativeInterface instance_native; | ||
private static final String VERSION = "2.0.1"; | ||
private static final String VERSION = "3.0.0"; | ||
|
||
static | ||
{ | ||
static { | ||
System.out.println("OpenBCI_GUI_Helpers Version: " + VERSION); | ||
|
||
String lib_name = "libGanglionScan.so"; | ||
String lib_native_name = "libGanglionNativeScan.so"; | ||
if (SystemUtils.IS_OS_WINDOWS) | ||
{ | ||
if (SystemUtils.IS_OS_WINDOWS) { | ||
lib_name = "GanglionScan.dll"; | ||
lib_native_name = "GanglionNativeScan.dll"; | ||
|
||
} else if (SystemUtils.IS_OS_MAC) | ||
{ | ||
} else if (SystemUtils.IS_OS_MAC) { | ||
lib_name = "libGanglionScan.dylib"; | ||
lib_native_name = "libGanglionNativeScan.dylib"; | ||
} | ||
|
||
// need to extract libraries from jar | ||
String lib_path = unpack_from_jar (lib_name); | ||
String lib_native_path = unpack_from_jar (lib_native_name); | ||
String lib_path = unpack_from_jar(lib_name); | ||
String lib_native_path = unpack_from_jar(lib_native_name); | ||
|
||
instance = (DllInterface) Native.load (lib_path, DllInterface.class); | ||
instance_native = (DllNativeInterface) Native.load (lib_native_path, DllNativeInterface.class); | ||
instance = (DllInterface) Native.load(lib_path, DllInterface.class); | ||
instance_native = (DllNativeInterface) Native.load(lib_native_path, DllNativeInterface.class); | ||
} | ||
|
||
private static String unpack_from_jar (String lib_name) | ||
{ | ||
private static String unpack_from_jar(String lib_name) { | ||
File file = null; | ||
InputStream link = null; | ||
try | ||
{ | ||
file = new File (lib_name); | ||
if (file.exists ()) | ||
file.delete (); | ||
link = (GUIHelper.class.getResourceAsStream (lib_name)); | ||
try { | ||
file = new File(lib_name); | ||
if (file.exists()) | ||
file.delete(); | ||
link = (GUIHelper.class.getResourceAsStream(lib_name)); | ||
if (SystemUtils.IS_OS_MAC) { | ||
String jarPath = GUIHelper.class.getProtectionDomain().getCodeSource().getLocation().getPath(); | ||
File jarFile = new File(jarPath); | ||
String libPathString = jarFile.getParentFile().getAbsolutePath() + File.separator + lib_name; | ||
return libPathString; | ||
} else { | ||
Files.copy (link, file.getAbsoluteFile ().toPath ()); | ||
Files.copy(link, file.getAbsoluteFile().toPath()); | ||
} | ||
return file.getAbsolutePath(); | ||
} catch (Exception io) | ||
{ | ||
io.printStackTrace (); | ||
} catch (Exception io) { | ||
io.printStackTrace(); | ||
System.err.println("native library: " + lib_name + " is not found in jar file"); | ||
System.err.println("file absolute to path: " + file.getAbsoluteFile().toPath()); | ||
System.err.println("file get absolute path: " + file.getAbsolutePath()); | ||
return ""; | ||
} | ||
} | ||
|
||
public static Map<String, String> scan_for_ganglions (String port_name, int timeout_sec) throws GanglionError | ||
{ | ||
public static GanglionDevice[] scan_for_ganglions(String port_name, int timeout_sec) throws GanglionError { | ||
int[] len = new int[1]; | ||
byte[] output_json = new byte[10240]; | ||
int ec = instance.scan_for_ganglions (port_name, timeout_sec, output_json, len); | ||
if (ec != GanglionExitCodes.STATUS_OK.get_code ()) | ||
{ | ||
throw new GanglionError ("Error in scan for ganglions", ec); | ||
|
||
int ec = instance.scan_for_ganglions(port_name, timeout_sec, output_json, len); | ||
if (ec != GanglionExitCodes.STATUS_OK.get_code()) { | ||
throw new GanglionError("Error in scan for ganglions", ec); | ||
} | ||
String json = new String (output_json, 0, len[0]); | ||
Gson gson = new Gson (); | ||
Type type = new TypeToken<Map<String, String>> () | ||
{ | ||
}.getType (); | ||
Map<String, String> map = gson.fromJson (json, type); | ||
return map; | ||
|
||
String json = new String(output_json, 0, len[0]); | ||
Gson gson = new Gson(); | ||
|
||
GanglionDevice[] devices = gson.fromJson(json, GanglionDevice[].class); | ||
|
||
ArrayList<GanglionDevice> uniqueDevices = new ArrayList<GanglionDevice>(); | ||
for (GanglionDevice device : devices) { | ||
if (!uniqueDevices.stream().anyMatch(entry -> entry.mac_address.equals(device.mac_address))) { | ||
uniqueDevices.add(device); | ||
} | ||
} | ||
|
||
return uniqueDevices.toArray(new GanglionDevice[uniqueDevices.size()]); | ||
} | ||
|
||
public static Map<String, String> scan_for_ganglions (int timeout_sec) throws GanglionError | ||
{ | ||
public static GanglionDevice[] scan_for_ganglions(int timeout_sec) throws GanglionError { | ||
int[] len = new int[1]; | ||
byte[] output_json = new byte[10240]; | ||
int ec = instance_native.scan_for_ganglions (timeout_sec, output_json, len); | ||
if (ec != GanglionExitCodes.STATUS_OK.get_code ()) | ||
{ | ||
throw new GanglionError ("Error in scan for ganglions", ec); | ||
|
||
int ec = instance_native.scan_for_ganglions(timeout_sec, output_json, len); | ||
if (ec != GanglionExitCodes.STATUS_OK.get_code()) { | ||
throw new GanglionError("Error in scan for ganglions", ec); | ||
} | ||
String json = new String (output_json, 0, len[0]); | ||
Gson gson = new Gson (); | ||
Type type = new TypeToken<Map<String, String>> () | ||
{ | ||
}.getType (); | ||
Map<String, String> map = gson.fromJson (json, type); | ||
return map; | ||
} | ||
|
||
String json = new String(output_json, 0, len[0]); | ||
Gson gson = new Gson(); | ||
|
||
GanglionDevice[] devices = gson.fromJson(json, GanglionDevice[].class); | ||
|
||
ArrayList<GanglionDevice> uniqueDevices = new ArrayList<GanglionDevice>(); | ||
for (GanglionDevice device : devices) { | ||
if (!uniqueDevices.stream().anyMatch(entry -> entry.mac_address.equals(device.mac_address))) { | ||
uniqueDevices.add(device); | ||
} | ||
} | ||
|
||
return uniqueDevices.toArray(new GanglionDevice[uniqueDevices.size()]); | ||
} | ||
} |
17 changes: 7 additions & 10 deletions
17
...package/openbci_gui_helpers/src/main/java/openbci_gui_helpers/examples/TestDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
package openbci_gui_helpers.examples; | ||
|
||
import java.util.Map; | ||
|
||
import openbci_gui_helpers.GUIHelper; | ||
import openbci_gui_helpers.GanglionError; | ||
import openbci_gui_helpers.GUIHelper.GanglionDevice; | ||
|
||
public class TestDiscovery | ||
{ | ||
public class TestDiscovery { | ||
|
||
public static void main (String[] args) throws GanglionError | ||
{ | ||
Map<String, String> map = GUIHelper.scan_for_ganglions (args[0], 3); | ||
for (Map.Entry<String, String> entry : map.entrySet ()) | ||
{ | ||
System.out.println ("Key = " + entry.getKey () + ", Value = " + entry.getValue ()); | ||
public static void main(String[] args) throws GanglionError { | ||
GanglionDevice[] devices = GUIHelper.scan_for_ganglions(args[0], 3); | ||
for (GanglionDevice device : devices) { | ||
System.out.println("Identifier = " + device.identifier + ", Mac Address = " + device.mac_address | ||
+ ", Firmware = " + device.firmware_version); | ||
} | ||
} | ||
} |
19 changes: 8 additions & 11 deletions
19
...e/openbci_gui_helpers/src/main/java/openbci_gui_helpers/examples/TestNativeDiscovery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,17 @@ | ||
package openbci_gui_helpers.examples; | ||
|
||
import java.util.Map; | ||
|
||
import openbci_gui_helpers.GUIHelper; | ||
import openbci_gui_helpers.GUIHelper.GanglionDevice; | ||
import openbci_gui_helpers.GanglionError; | ||
|
||
public class TestNativeDiscovery | ||
{ | ||
public class TestNativeDiscovery { | ||
|
||
public static void main (String[] args) throws GanglionError | ||
{ | ||
Map<String, String> map = GUIHelper.scan_for_ganglions (3); | ||
for (Map.Entry<String, String> entry : map.entrySet ()) | ||
{ | ||
System.out.println ("Key = " + entry.getKey () + ", Value = " + entry.getValue ()); | ||
public static void main(String[] args) throws GanglionError { | ||
GanglionDevice[] devices = GUIHelper.scan_for_ganglions(3); | ||
for (GanglionDevice device : devices) { | ||
System.out.println("Identifier = " + device.identifier + ", Mac Address = " + device.mac_address | ||
+ ", Firmware = " + device.firmware_version); | ||
} | ||
System.out.println ("Completed"); | ||
System.out.println("Completed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include "json.hpp" | ||
|
||
#include <cstdint> | ||
#include <string> | ||
|
||
typedef struct GanglionDevice | ||
{ | ||
std::string identifier; | ||
std::string mac_address; | ||
uint8_t firmware_version; | ||
} GanglionDevice; | ||
|
||
void to_json (nlohmann::json &result, const GanglionDevice &device); | ||
void from_json (const nlohmann::json &string, GanglionDevice &device); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "serialization.h" | ||
|
||
void to_json (nlohmann::json &result, const GanglionDevice &device) | ||
{ | ||
result = nlohmann::json {{"identifier", device.identifier}, {"mac_address", device.mac_address}, | ||
{"firmware_version", std::to_string (device.firmware_version)}}; | ||
} | ||
|
||
void from_json (const nlohmann::json &string, GanglionDevice &device) | ||
{ | ||
string.at ("identifier").get_to (device.identifier); | ||
string.at ("mac_address").get_to (device.mac_address); | ||
string.at ("firmware_version").get_to (device.firmware_version); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.