11package openbci_gui_helpers ;
22
33import java .io .File ;
4- import java .io .IOException ;
54import java .io .InputStream ;
5+ import java .lang .reflect .Type ;
66import java .nio .file .Files ;
7- import java .util .Arrays ;
8- import java .util .HashMap ;
97import java .util .Map ;
108
119import org .apache .commons .lang3 .SystemUtils ;
1210
13- import com .sun .jna .Library ;
14- import com .sun .jna .Native ;
15-
16- import java .lang .reflect .Type ;
1711import com .google .gson .Gson ;
1812import com .google .gson .reflect .TypeToken ;
13+ import com .sun .jna .Library ;
14+ import com .sun .jna .Native ;
1915
2016public class GUIHelper
2117{
@@ -24,24 +20,35 @@ private interface DllInterface extends Library
2420 int scan_for_ganglions (String serial_port , int timeout_sec , byte [] output , int [] output_len );
2521 }
2622
23+ private interface DllNativeInterface extends Library
24+ {
25+ int scan_for_ganglions (int timeout_sec , byte [] output , int [] output_len );
26+ }
27+
2728 private static DllInterface instance ;
29+ private static DllNativeInterface instance_native ;
2830
2931 static
3032 {
3133 String lib_name = "libGanglionScan.so" ;
34+ String lib_native_name = "libGanglionNativeScan.so" ;
3235 if (SystemUtils .IS_OS_WINDOWS )
3336 {
3437 lib_name = "GanglionScan.dll" ;
38+ lib_native_name = "GanglionNativeScan.dll" ;
3539
3640 } else if (SystemUtils .IS_OS_MAC )
3741 {
3842 lib_name = "libGanglionScan.dylib" ;
43+ lib_native_name = "libGanglionNativeScan.dylib" ;
3944 }
4045
4146 // need to extract libraries from jar
4247 unpack_from_jar (lib_name );
48+ unpack_from_jar (lib_native_name );
4349
4450 instance = (DllInterface ) Native .loadLibrary (lib_name , DllInterface .class );
51+ instance_native = (DllNativeInterface ) Native .loadLibrary (lib_native_name , DllNativeInterface .class );
4552 }
4653
4754 private static void unpack_from_jar (String lib_name )
@@ -77,4 +84,22 @@ public static Map<String, String> scan_for_ganglions (String port_name, int time
7784 return map ;
7885 }
7986
87+ public static Map <String , String > scan_for_ganglions (int timeout_sec ) throws GanglionError
88+ {
89+ int [] len = new int [1 ];
90+ byte [] output_json = new byte [10240 ];
91+ int ec = instance_native .scan_for_ganglions (timeout_sec , output_json , len );
92+ if (ec != GanglionExitCodes .STATUS_OK .get_code ())
93+ {
94+ throw new GanglionError ("Error in scan for ganglions" , ec );
95+ }
96+ String json = new String (output_json , 0 , len [0 ]);
97+ Gson gson = new Gson ();
98+ Type type = new TypeToken <Map <String , String >> ()
99+ {
100+ }.getType ();
101+ Map <String , String > map = gson .fromJson (json , type );
102+ return map ;
103+ }
104+
80105}
0 commit comments