Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public abstract class ShimmerBluetoothManager{
protected static final boolean USE_INFOMEM_CONFIG_METHOD = true;
public static final long SLEEP_BETWEEN_GROUP_ACTIONS_MS = 50;
public static final String COMPORT_PREFIX = "COM";
public static final String COMPORT_PREFIX_MAC = "/dev/";
// public static final String COMPORT_PREFIX_MAC = "/dev/"; //Commented-out as MacOS is relying on BT device name rather than COM PORT prefix
public static final String COMPORT_PREFIX_MAC = "Shimmer";
protected int mSyncTrainingIntervalInSeconds = 15;
protected int msDelayBetweenSetCommands = 0;
protected BluetoothProgressReportAll mProgressReportAll;
Expand Down Expand Up @@ -836,7 +837,8 @@ else if(deviceTypeDetected==DEVICE_TYPE.ARDUINO){
if (shimmerDevice!=null && !(shimmerDevice instanceof ShimmerShell)){
printMessage("Connecting to " + shimmerDevice.getClass().getSimpleName() + " with connection handle = " + (connectThroughComPort? comPort:bluetoothAddress));
if(connectThroughComPort){
if (!comPort.contains(COMPORT_PREFIX) && !comPort.contains(COMPORT_PREFIX_MAC)) {
if (!comPort.contains(COMPORT_PREFIX)) {
//Besides Windows, this is used on MacOS to connect Shimmer over BLE, as MacOS does not use BT Classic
connectShimmer3BleGrpc(bluetoothDetails);
}else {
connectExistingShimmer(shimmerDevice, comPort, bluetoothAddress);
Expand Down
10 changes: 8 additions & 2 deletions ShimmerDriver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ dependencies {
api group: 'com.google.protobuf', name: 'protobuf-java', version: '3.4.0'

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

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

// https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
api("javax.annotation:javax.annotation-api:1.3.2")

// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
Expand All @@ -128,5 +128,11 @@ dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.+'

api 'com.parse.bolts:bolts-tasks:1.4.0'

// https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on
implementation("org.bouncycastle:bcpkix-jdk15on:1.61")

// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
implementation("org.bouncycastle:bcprov-jdk15on:1.61")
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ public BluetoothDeviceDetails(String comPort, String macId, String friendlyName)
}

public void setMacId(String mac){

mac = mac.replace("-", "");

if(mac.length()>=12) {
mShimmerMacId = mac.toUpperCase();
mac = mac.replace(":", "").toUpperCase();
mShimmerMacIdParsed = mac.substring(8);
if(mac.startsWith("Shimmer") && mac.contains("-")) {
//If using device name (e.g. Shimmer3-6813), then leave as is
mShimmerMacId = mac;
mShimmerMacId = mac;
} else {
mac = mac.replace("-", "");

if(mac.length()>=12) {
mShimmerMacId = mac.toUpperCase();
mac = mac.replace(":", "").toUpperCase();
mShimmerMacIdParsed = mac.substring(8);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1368,5 +1368,10 @@ public static int getTimeZoneOffset(){
// System.err.println(milliSeconds);
// System.err.println(UtilShimmer.bytesToHexStringWithSpacesFormatted(convertMilliSecondsToShimmerRtcDataBytesLSB(milliSeconds)));
// }

public static boolean isOsMac() {
return System.getProperty("os.name").toLowerCase().contains("mac");
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void connect() throws ShimmerException {
@Override
public void onNext(StateStatus value) {
// TODO Auto-generated method stub
System.out.println(value.getMessage() + " " + value.getState().toString());
// System.out.println(value.getMessage() + " " + value.getState().toString());
if (value.getState().equals(BluetoothState.Connected)) {
mConnectTask.setResult(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
package com.shimmerresearch.grpc;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import com.shimmerresearch.driverUtilities.UtilShimmer;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GrpcBLERadioByteTools {

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

//Below used by Consensys MacOS
String mExeNameMac = "ShimmerBLEGrpc";
String mExePathMac = System.getProperty("user.dir") + "/libs/ShimmerBLEGrpc/Products/usr/local/bin/" + mExeNameMac;

public GrpcBLERadioByteTools() {

}

public GrpcBLERadioByteTools(String exeName, String exePath) {
mExeName = exeName;
mExePath = exePath;
mExeNameWindows = exeName;
mExeNameMac = exeName;
mExePathWindows = exePath;
mExePathMac = exePath;
}


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

// Add the command itself
command.add(mExePath);
if(UtilShimmer.isOsMac()) {
command.add(mExePathMac);
command.add("--port");
} else {
command.add(mExePathWindows);
}
command.add(Integer.toString(port));

ProcessBuilder processBuilder = new ProcessBuilder(command);
processBuilder.redirectErrorStream(true); // Redirect standard error to the input stream
runningProcess = processBuilder.start();
Thread processThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(runningProcess.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
System.out.println("[BLEGrpcServer] " + line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
Expand Down Expand Up @@ -142,7 +158,7 @@ public void actionPerformed(ActionEvent e) {
JButton btnCheck = new JButton("checkServerApp");
btnCheck.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (grpcTools.isExeRunning(grpcTools.mExeName)) {
if (grpcTools.isExeRunning(grpcTools.mExeNameWindows)) {
System.out.println("EXE RUNNING");
} else {
System.out.println("EXE NOT RUNNING");
Expand Down
6 changes: 6 additions & 0 deletions ShimmerDriverPC/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ dependencies {
// 'test.useTestNG()' to your build script.
testImplementation 'junit:junit:4.12'
implementation 'com.parse.bolts:bolts-tasks:1.4.0'

// https://mvnrepository.com/artifact/org.bouncycastle/bcpkix-jdk15on
implementation("org.bouncycastle:bcpkix-jdk15on:1.61")

// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15on
implementation("org.bouncycastle:bcprov-jdk15on:1.61")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.shimmerresearch.driverUtilities.AssembleShimmerConfig;
import com.shimmerresearch.driverUtilities.SensorDetails;
import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID;
import com.shimmerresearch.driverUtilities.UtilShimmer;
import com.shimmerresearch.driverUtilities.ChannelDetails.CHANNEL_TYPE;
import com.shimmerresearch.grpc.ShimmerBLEByteServerGrpc;
import com.shimmerresearch.grpc.ShimmerBLEGRPC.BluetoothState;
Expand All @@ -36,6 +37,7 @@

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import io.grpc.stub.StreamObserver;

import com.shimmerresearch.driver.BasicProcessWithCallBack;
Expand Down Expand Up @@ -64,31 +66,36 @@ public class ShimmerGRPC extends ShimmerBluetooth implements Serializable{
*
*/
private static final long serialVersionUID = 5029128107276324956L;

public ShimmerGRPC(String macAddress, String serverHost, int serverPort) {
super();
mServerHost = serverHost;
mServerPort = serverPort;
mMacAddress = macAddress;
mMyBluetoothAddress = "";
mUseProcessingThread = true;
if (channel==null) {
InitializeProcess();
}
}


/**
* This constructor is used for MacOS for BLE where the mFriendlyName is the BT device name (e.g. Shimmer3-6813).
* BT device name is used in place of COM Port which is Classic BT only
*/
public ShimmerGRPC(String macAddress, String mFriendlyName, String serverHost, int serverPort) {
this(mFriendlyName, serverHost, serverPort);
mMyBluetoothAddress = macAddress;
mComPort = mFriendlyName;
}

public void InitializeProcess() {
// Define the server host and port

// Create a channel to connect to the server
channel = ManagedChannelBuilder.forAddress(mServerHost, mServerPort)
channel = ManagedChannelBuilder.forTarget(mServerHost + ":" + mServerPort)
.usePlaintext() // Use plaintext communication (insecure for testing)
.build();

// Create a gRPC client stub
blockingStub = ShimmerBLEByteServerGrpc.newBlockingStub(channel);




}


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

JButton btnNewButton = new JButton("Connect");

final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","localhost",50052);

final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","localhost",50052);
//Use constructor below instead for MacOS, which uses the BT device name to connect, e.g. Shimmer3-6813
//final ShimmerGRPC shimmer = new ShimmerGRPC("E8EB1B713E36","Shimmer3-3E36","localhost",50052);

SensorDataReceived sdr = shimmer.new SensorDataReceived();
sdr.setWaitForData(shimmer);

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

// Call the remote gRPC service method
Reply response = blockingStub.writeBytesShimmer(request);

// Process the response
System.out.println("Received: " + response.getMessage());
try {
// Call the remote gRPC service method
Reply response = blockingStub.writeBytesShimmer(request);

// Process the response
System.out.println("Received: " + response.getMessage());
} catch(StatusRuntimeException sre) {
// Remote gRPC service is not available, so disconnect Shimmer device
sre.printStackTrace();
System.out.println("ERROR: Lost connection to GRPC Server");
connectionLost();
}
}

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

// Call the remote gRPC service method
Reply response = blockingStub.disconnectShimmer(request);
try {
// Call the remote gRPC service method
Reply response = blockingStub.disconnectShimmer(request);
// Process the response
System.out.println("Received: " + response.getMessage());
} catch(Exception e) {
e.printStackTrace();
}

// Process the response
System.out.println("Received: " + response.getMessage());
closeConnection();
setBluetoothRadioState(BT_STATE.DISCONNECTED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE;
import com.shimmerresearch.driver.shimmer4sdk.Shimmer4sdk;
import com.shimmerresearch.driverUtilities.BluetoothDeviceDetails;
import com.shimmerresearch.driverUtilities.UtilShimmer;
import com.shimmerresearch.exceptions.ConnectionExceptionListener;
import com.shimmerresearch.exceptions.ShimmerException;
import com.shimmerresearch.grpc.GrpcBLERadioByteCommunication;
Expand Down Expand Up @@ -46,8 +47,8 @@ public BasicShimmerBluetoothManagerPc() {
startGrpc();
}

public BasicShimmerBluetoothManagerPc(boolean isInternalUse) {
if(isInternalUse) {
public BasicShimmerBluetoothManagerPc(boolean startGrpc) {
if(startGrpc) {
startGrpc();
}
}
Expand Down Expand Up @@ -191,19 +192,25 @@ public void connectVerisenseDevice(BluetoothDeviceDetails bdd) {
@Override
public void connectShimmer3BleGrpc(BluetoothDeviceDetails bdd) {
ShimmerGRPC shimmer;
String macId = bdd.mShimmerMacId.replaceAll(":", "");

if(!shimmer3BleMacIdList.contains(bdd.mShimmerMacId)) {

shimmer = new ShimmerGRPC(bdd.mShimmerMacId.replace(":", ""),"localhost",mGRPCPort);
if(!shimmer3BleMacIdList.contains(macId)) {
if(UtilShimmer.isOsMac()) {
//Use the mFriendlyName (e.g. Shimmer3-6813), because MacOS doesn't use BT MacID
shimmer = new ShimmerGRPC(macId, bdd.mFriendlyName, "localhost", mGRPCPort);
} else {
shimmer = new ShimmerGRPC(macId, "localhost", mGRPCPort);
}
shimmer.setShimmerUserAssignedName(bdd.mFriendlyName);
shimmer.setMacIdFromUart(bdd.mShimmerMacId);
shimmer.setMacIdFromUart(macId);
initializeNewShimmerCommon(shimmer);

shimmer3BleDeviceList.add(shimmer);
shimmer3BleMacIdList.add(bdd.mShimmerMacId);
shimmer3BleMacIdList.add(macId);
}
else {
shimmer = shimmer3BleDeviceList.get(shimmer3BleMacIdList.indexOf(bdd.mShimmerMacId));
shimmer = shimmer3BleDeviceList.get(shimmer3BleMacIdList.indexOf(macId));
initializeNewShimmerCommon(shimmer);
}

try {
Expand All @@ -215,6 +222,7 @@ public void connectShimmer3BleGrpc(BluetoothDeviceDetails bdd) {
e.printStackTrace();
}
}

@Override
public void connectShimmerThroughCommPort(String comPort){
directConnectUnknownShimmer=true;
Expand Down
Loading
Loading