Skip to content

Commit db56074

Browse files
Merge pull request #213 from ShimmerEngineering/DEV-221
DEV-221 [aarch64] SwiftAPI BLE gRPC Server & Java Examples Integration
2 parents 68f1fa4 + fe77bda commit db56074

10 files changed

Lines changed: 134 additions & 54 deletions

File tree

ShimmerBluetoothManager/src/main/java/com/shimmerresearch/managers/bluetoothManager/ShimmerBluetoothManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public abstract class ShimmerBluetoothManager{
5555
protected static final boolean USE_INFOMEM_CONFIG_METHOD = true;
5656
public static final long SLEEP_BETWEEN_GROUP_ACTIONS_MS = 50;
5757
public static final String COMPORT_PREFIX = "COM";
58-
public static final String COMPORT_PREFIX_MAC = "/dev/";
58+
// public static final String COMPORT_PREFIX_MAC = "/dev/"; //Commented-out as MacOS is relying on BT device name rather than COM PORT prefix
59+
public static final String COMPORT_PREFIX_MAC = "Shimmer";
5960
protected int mSyncTrainingIntervalInSeconds = 15;
6061
protected int msDelayBetweenSetCommands = 0;
6162
protected BluetoothProgressReportAll mProgressReportAll;
@@ -836,7 +837,8 @@ else if(deviceTypeDetected==DEVICE_TYPE.ARDUINO){
836837
if (shimmerDevice!=null && !(shimmerDevice instanceof ShimmerShell)){
837838
printMessage("Connecting to " + shimmerDevice.getClass().getSimpleName() + " with connection handle = " + (connectThroughComPort? comPort:bluetoothAddress));
838839
if(connectThroughComPort){
839-
if (!comPort.contains(COMPORT_PREFIX) && !comPort.contains(COMPORT_PREFIX_MAC)) {
840+
if (!comPort.contains(COMPORT_PREFIX)) {
841+
//Besides Windows, this is used on MacOS to connect Shimmer over BLE, as MacOS does not use BT Classic
840842
connectShimmer3BleGrpc(bluetoothDetails);
841843
}else {
842844
connectExistingShimmer(shimmerDevice, comPort, bluetoothAddress);

ShimmerDriver/build.gradle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ dependencies {
9393
api group: 'com.google.protobuf', name: 'protobuf-java', version: '3.4.0'
9494

9595
// https://mvnrepository.com/artifact/io.grpc/grpc-all
96-
api group: 'io.grpc', name: 'grpc-all', version: '1.32.1'
96+
api group: 'io.grpc', name: 'grpc-all', version: '1.71.0'
9797

9898
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
9999
api group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.6'
@@ -114,7 +114,7 @@ dependencies {
114114

115115
// https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
116116
api("javax.annotation:javax.annotation-api:1.3.2")
117-
117+
118118
// Declare the dependency for your favourite test framework you want to use in your tests.
119119
// TestNG is also supported by the Gradle Test task. Just change the
120120
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
@@ -128,5 +128,11 @@ dependencies {
128128
testImplementation group: 'junit', name: 'junit', version: '4.+'
129129

130130
api 'com.parse.bolts:bolts-tasks:1.4.0'
131+
132+
// https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on
133+
implementation("org.bouncycastle:bcpkix-jdk15on:1.61")
134+
135+
// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
136+
implementation("org.bouncycastle:bcprov-jdk15on:1.61")
131137
}
132138

ShimmerDriver/src/main/java/com/shimmerresearch/driverUtilities/BluetoothDeviceDetails.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ public BluetoothDeviceDetails(String comPort, String macId, String friendlyName)
3333
}
3434

3535
public void setMacId(String mac){
36-
37-
mac = mac.replace("-", "");
38-
39-
if(mac.length()>=12) {
40-
mShimmerMacId = mac.toUpperCase();
41-
mac = mac.replace(":", "").toUpperCase();
42-
mShimmerMacIdParsed = mac.substring(8);
36+
if(mac.startsWith("Shimmer") && mac.contains("-")) {
37+
//If using device name (e.g. Shimmer3-6813), then leave as is
38+
mShimmerMacId = mac;
39+
mShimmerMacId = mac;
40+
} else {
41+
mac = mac.replace("-", "");
42+
43+
if(mac.length()>=12) {
44+
mShimmerMacId = mac.toUpperCase();
45+
mac = mac.replace(":", "").toUpperCase();
46+
mShimmerMacIdParsed = mac.substring(8);
47+
}
4348
}
4449
}
4550

ShimmerDriver/src/main/java/com/shimmerresearch/driverUtilities/UtilShimmer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,5 +1368,10 @@ public static int getTimeZoneOffset(){
13681368
// System.err.println(milliSeconds);
13691369
// System.err.println(UtilShimmer.bytesToHexStringWithSpacesFormatted(convertMilliSecondsToShimmerRtcDataBytesLSB(milliSeconds)));
13701370
// }
1371+
1372+
public static boolean isOsMac() {
1373+
return System.getProperty("os.name").toLowerCase().contains("mac");
1374+
}
1375+
13711376

13721377
}

ShimmerDriver/src/main/java/com/shimmerresearch/grpc/GrpcBLERadioByteCommunication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void connect() throws ShimmerException {
135135
@Override
136136
public void onNext(StateStatus value) {
137137
// TODO Auto-generated method stub
138-
System.out.println(value.getMessage() + " " + value.getState().toString());
138+
// System.out.println(value.getMessage() + " " + value.getState().toString());
139139
if (value.getState().equals(BluetoothState.Connected)) {
140140
mConnectTask.setResult(true);
141141

ShimmerDriver/src/main/java/com/shimmerresearch/grpc/GrpcBLERadioByteTools.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
11
package com.shimmerresearch.grpc;
22

33
import java.io.BufferedReader;
4+
import java.io.File;
45
import java.io.IOException;
56
import java.io.InputStreamReader;
67
import java.net.ServerSocket;
78
import java.util.ArrayList;
89
import java.util.List;
910

1011
import javax.swing.JFrame;
12+
13+
import com.shimmerresearch.driverUtilities.UtilShimmer;
14+
1115
import javax.swing.JButton;
1216
import java.awt.event.ActionListener;
1317
import java.awt.event.ActionEvent;
1418

1519
public class GrpcBLERadioByteTools {
1620

1721
private Process runningProcess;
18-
String mExeName = "ShimmerBLEGrpc.exe";
19-
String mExePath = "C:\\Github\\Shimmer-C-API\\ShimmerAPI\\ShimmerBLEGrpc\\bin\\Debug\\" + mExeName; // Replace with the path to your .exe file
22+
String mExeNameWindows = "ShimmerBLEGrpc.exe";
23+
String mExePathWindows = "C:\\Github\\Shimmer-C-API\\ShimmerAPI\\ShimmerBLEGrpc\\bin\\Debug\\" + mExeNameWindows; // Replace with the path to your .exe file
2024
//String exePath = "C:\\Users\\JC\\Desktop\\testgrpc\\ShimmerBLEGrpc.exe"; // Replace with the path to your .exe file
2125

26+
//Below used by Consensys MacOS
27+
String mExeNameMac = "ShimmerBLEGrpc";
28+
String mExePathMac = System.getProperty("user.dir") + "/libs/ShimmerBLEGrpc/Products/usr/local/bin/" + mExeNameMac;
29+
2230
public GrpcBLERadioByteTools() {
2331

2432
}
2533

2634
public GrpcBLERadioByteTools(String exeName, String exePath) {
27-
mExeName = exeName;
28-
mExePath = exePath;
35+
mExeNameWindows = exeName;
36+
mExeNameMac = exeName;
37+
mExePathWindows = exePath;
38+
mExePathMac = exePath;
2939
}
3040

3141

@@ -76,16 +86,22 @@ public int startServer() throws Exception {
7686
List<String> command = new ArrayList<>();
7787

7888
// Add the command itself
79-
command.add(mExePath);
89+
if(UtilShimmer.isOsMac()) {
90+
command.add(mExePathMac);
91+
command.add("--port");
92+
} else {
93+
command.add(mExePathWindows);
94+
}
8095
command.add(Integer.toString(port));
96+
8197
ProcessBuilder processBuilder = new ProcessBuilder(command);
8298
processBuilder.redirectErrorStream(true); // Redirect standard error to the input stream
8399
runningProcess = processBuilder.start();
84100
Thread processThread = new Thread(() -> {
85101
try (BufferedReader reader = new BufferedReader(new InputStreamReader(runningProcess.getInputStream()))) {
86102
String line;
87103
while ((line = reader.readLine()) != null) {
88-
System.out.println(line);
104+
System.out.println("[BLEGrpcServer] " + line);
89105
}
90106
} catch (IOException e) {
91107
// TODO Auto-generated catch block
@@ -142,7 +158,7 @@ public void actionPerformed(ActionEvent e) {
142158
JButton btnCheck = new JButton("checkServerApp");
143159
btnCheck.addActionListener(new ActionListener() {
144160
public void actionPerformed(ActionEvent e) {
145-
if (grpcTools.isExeRunning(grpcTools.mExeName)) {
161+
if (grpcTools.isExeRunning(grpcTools.mExeNameWindows)) {
146162
System.out.println("EXE RUNNING");
147163
} else {
148164
System.out.println("EXE NOT RUNNING");

ShimmerDriverPC/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,10 @@ dependencies {
9595
// 'test.useTestNG()' to your build script.
9696
testImplementation 'junit:junit:4.12'
9797
implementation 'com.parse.bolts:bolts-tasks:1.4.0'
98+
99+
// https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on
100+
implementation("org.bouncycastle:bcpkix-jdk15on:1.61")
101+
102+
// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
103+
implementation("org.bouncycastle:bcprov-jdk15on:1.61")
98104
}

ShimmerDriverPC/src/main/java/com/shimmerresearch/pcDriver/ShimmerGRPC.java

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.shimmerresearch.driverUtilities.AssembleShimmerConfig;
2323
import com.shimmerresearch.driverUtilities.SensorDetails;
2424
import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID;
25+
import com.shimmerresearch.driverUtilities.UtilShimmer;
2526
import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_TYPE;
2627
import com.shimmerresearch.grpc.ShimmerBLEByteServerGrpc;
2728
import com.shimmerresearch.grpc.ShimmerBLEGRPC.BluetoothState;
@@ -36,6 +37,7 @@
3637

3738
import io.grpc.ManagedChannel;
3839
import io.grpc.ManagedChannelBuilder;
40+
import io.grpc.StatusRuntimeException;
3941
import io.grpc.stub.StreamObserver;
4042

4143
import com.shimmerresearch.driver.BasicProcessWithCallBack;
@@ -64,31 +66,36 @@ public class ShimmerGRPC extends ShimmerBluetooth implements Serializable{
6466
*
6567
*/
6668
private static final long serialVersionUID = 5029128107276324956L;
67-
69+
6870
public ShimmerGRPC(String macAddress, String serverHost, int serverPort) {
6971
super();
7072
mServerHost = serverHost;
7173
mServerPort = serverPort;
7274
mMacAddress = macAddress;
75+
mMyBluetoothAddress = "";
7376
mUseProcessingThread = true;
7477
if (channel==null) {
7578
InitializeProcess();
7679
}
7780
}
78-
81+
82+
/**
83+
* This constructor is used for MacOS for BLE where the mFriendlyName is the BT device name (e.g. Shimmer3-6813).
84+
* BT device name is used in place of COM Port which is Classic BT only
85+
*/
86+
public ShimmerGRPC(String macAddress, String mFriendlyName, String serverHost, int serverPort) {
87+
this(mFriendlyName, serverHost, serverPort);
88+
mMyBluetoothAddress = macAddress;
89+
mComPort = mFriendlyName;
90+
}
91+
7992
public void InitializeProcess() {
80-
// Define the server host and port
81-
82-
// Create a channel to connect to the server
83-
channel = ManagedChannelBuilder.forAddress(mServerHost, mServerPort)
93+
channel = ManagedChannelBuilder.forTarget(mServerHost + ":" + mServerPort)
8494
.usePlaintext() // Use plaintext communication (insecure for testing)
8595
.build();
96+
8697
// Create a gRPC client stub
8798
blockingStub = ShimmerBLEByteServerGrpc.newBlockingStub(channel);
88-
89-
90-
91-
9299
}
93100

94101

@@ -98,8 +105,10 @@ public static void main(String[] args) {
98105

99106
JButton btnNewButton = new JButton("Connect");
100107

101-
final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","localhost",50052);
102-
108+
final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","localhost",50052);
109+
//Use constructor below instead for MacOS, which uses the BT device name to connect, e.g. Shimmer3-6813
110+
//final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","Shimmer3-3E36","localhost",50052);
111+
103112
SensorDataReceived sdr = shimmer.new SensorDataReceived();
104113
sdr.setWaitForData(shimmer);
105114

@@ -271,11 +280,18 @@ protected void writeBytes(byte[] data) {
271280
// Create a request message
272281
WriteBytes request = WriteBytes.newBuilder().setAddress(mMacAddress).setByteToWrite(ByteString.copyFrom(data)).build();
273282

274-
// Call the remote gRPC service method
275-
Reply response = blockingStub.writeBytesShimmer(request);
276-
277-
// Process the response
278-
System.out.println("Received: " + response.getMessage());
283+
try {
284+
// Call the remote gRPC service method
285+
Reply response = blockingStub.writeBytesShimmer(request);
286+
287+
// Process the response
288+
System.out.println("Received: " + response.getMessage());
289+
} catch(StatusRuntimeException sre) {
290+
// Remote gRPC service is not available, so disconnect Shimmer device
291+
sre.printStackTrace();
292+
System.out.println("ERROR: Lost connection to GRPC Server");
293+
connectionLost();
294+
}
279295
}
280296

281297
@Override
@@ -510,11 +526,15 @@ public void disconnect() throws ShimmerException {
510526
// Create a request message
511527
Request request = Request.newBuilder().setName(mMacAddress).build();
512528

513-
// Call the remote gRPC service method
514-
Reply response = blockingStub.disconnectShimmer(request);
529+
try {
530+
// Call the remote gRPC service method
531+
Reply response = blockingStub.disconnectShimmer(request);
532+
// Process the response
533+
System.out.println("Received: " + response.getMessage());
534+
} catch(Exception e) {
535+
e.printStackTrace();
536+
}
515537

516-
// Process the response
517-
System.out.println("Received: " + response.getMessage());
518538
closeConnection();
519539
setBluetoothRadioState(BT_STATE.DISCONNECTED);
520540
}

ShimmerDriverPC/src/main/java/com/shimmerresearch/tools/bluetooth/BasicShimmerBluetoothManagerPc.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE;
2020
import com.shimmerresearch.driver.shimmer4sdk.Shimmer4sdk;
2121
import com.shimmerresearch.driverUtilities.BluetoothDeviceDetails;
22+
import com.shimmerresearch.driverUtilities.UtilShimmer;
2223
import com.shimmerresearch.exceptions.ConnectionExceptionListener;
2324
import com.shimmerresearch.exceptions.ShimmerException;
2425
import com.shimmerresearch.grpc.GrpcBLERadioByteCommunication;
@@ -46,8 +47,8 @@ public BasicShimmerBluetoothManagerPc() {
4647
startGrpc();
4748
}
4849

49-
public BasicShimmerBluetoothManagerPc(boolean isInternalUse) {
50-
if(isInternalUse) {
50+
public BasicShimmerBluetoothManagerPc(boolean startGrpc) {
51+
if(startGrpc) {
5152
startGrpc();
5253
}
5354
}
@@ -191,19 +192,25 @@ public void connectVerisenseDevice(BluetoothDeviceDetails bdd) {
191192
@Override
192193
public void connectShimmer3BleGrpc(BluetoothDeviceDetails bdd) {
193194
ShimmerGRPC shimmer;
195+
String macId = bdd.mShimmerMacId.replaceAll(":", "");
194196

195-
if(!shimmer3BleMacIdList.contains(bdd.mShimmerMacId)) {
196-
197-
shimmer = new ShimmerGRPC(bdd.mShimmerMacId.replace(":", ""),"localhost",mGRPCPort);
197+
if(!shimmer3BleMacIdList.contains(macId)) {
198+
if(UtilShimmer.isOsMac()) {
199+
//Use the mFriendlyName (e.g. Shimmer3-6813), because MacOS doesn't use BT MacID
200+
shimmer = new ShimmerGRPC(macId, bdd.mFriendlyName, "localhost", mGRPCPort);
201+
} else {
202+
shimmer = new ShimmerGRPC(macId, "localhost", mGRPCPort);
203+
}
198204
shimmer.setShimmerUserAssignedName(bdd.mFriendlyName);
199-
shimmer.setMacIdFromUart(bdd.mShimmerMacId);
205+
shimmer.setMacIdFromUart(macId);
200206
initializeNewShimmerCommon(shimmer);
201207

202208
shimmer3BleDeviceList.add(shimmer);
203-
shimmer3BleMacIdList.add(bdd.mShimmerMacId);
209+
shimmer3BleMacIdList.add(macId);
204210
}
205211
else {
206-
shimmer = shimmer3BleDeviceList.get(shimmer3BleMacIdList.indexOf(bdd.mShimmerMacId));
212+
shimmer = shimmer3BleDeviceList.get(shimmer3BleMacIdList.indexOf(macId));
213+
initializeNewShimmerCommon(shimmer);
207214
}
208215

209216
try {
@@ -215,6 +222,7 @@ public void connectShimmer3BleGrpc(BluetoothDeviceDetails bdd) {
215222
e.printStackTrace();
216223
}
217224
}
225+
218226
@Override
219227
public void connectShimmerThroughCommPort(String comPort){
220228
directConnectUnknownShimmer=true;

0 commit comments

Comments
 (0)