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
2 changes: 1 addition & 1 deletion ShimmerBluetoothManager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ publishing {

groupId = 'com.shimmerresearch' // Replace with your package's group/organization name
artifactId = 'shimmerbluetoothmanager' // Replace with the name of your package
version = '0.10.0_alpha' // Replace with your package version
version = '0.10.18_alpha' // Replace with your package version

// Jar publication
//(
Expand Down
2 changes: 1 addition & 1 deletion ShimmerDriver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ publishing {

groupId = 'com.shimmerresearch' // Replace with your package's group/organization name
artifactId = 'shimmerdriver' // Replace with the name of your package
version = '0.10.0_alpha' // Replace with your package version
version = '0.10.18_alpha' // Replace with your package version

// Jar publication
//(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public String toString() {
private boolean mCheckIfConnectionisAlive = false;
//endregion --------- TIMERS ---------

transient ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();
protected transient ByteArrayOutputStream mByteArrayOutputStream = new ByteArrayOutputStream();

// private boolean mVerboseMode = true;
// private String mParentClassName = "ShimmerBluetooth";
Expand Down Expand Up @@ -475,7 +475,7 @@ public int getNumCrcBytes() {

private int mNumOfMemSetCmds = 0;

private boolean mSerialPortReadTimeout;
protected boolean mSerialPortReadTimeout;

public static final int MSG_IDENTIFIER_DATA_PACKET = 2;
public static final int MSG_IDENTIFIER_DEVICE_PAIRED = 8;
Expand Down Expand Up @@ -580,7 +580,7 @@ public void run() {
//region --------- BLUETOOH STACK ---------

public class IOThread extends Thread {
byte[] byteBuffer = {0};
protected byte[] byteBuffer = {0};
public boolean stop = false;

public void run() {
Expand Down Expand Up @@ -727,112 +727,6 @@ else if((mCurrentCommand==GET_FW_VERSION_COMMAND)
}
}
}


private void processWhileStreaming() {
byteBuffer = readBytes(1);
if(byteBuffer!=null){
mByteArrayOutputStream.write(byteBuffer[0]);
//Everytime a byte is received the timestamp is taken
if(mEnablePCTimeStamps) {
mListofPCTimeStamps.add(System.currentTimeMillis());
}
}
else {
printLogDataForDebugging("readbyte null");
}

//If there is a full packet and the subsequent sequence number of following packet
if(mByteArrayOutputStream.size()>=getPacketSizeWithCrc()+2){ // +2 because there are two acks
processPacket();
}
}

private void processPacket() {
setIamAlive(true);
byte[] bufferTemp = mByteArrayOutputStream.toByteArray();

//Data packet followed by another data packet
if(bufferTemp[0]==DATA_PACKET
&& bufferTemp[getPacketSizeWithCrc()+1]==DATA_PACKET){

if (mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && !checkCrc(bufferTemp, getPacketSize() + 1)) {
discardFirstBufferByte();
return;
}

//Handle the data packet
processDataPacket(bufferTemp);
clearSingleDataPacketFromBuffers(bufferTemp, getPacketSizeWithCrc()+1);
}

//Data packet followed by an ACK (suggesting an ACK in response to a SET BT command or else a BT response command)
else if(bufferTemp[0]==DATA_PACKET
&& bufferTemp[getPacketSizeWithCrc()+1]==ACK_COMMAND_PROCESSED){
if(mByteArrayOutputStream.size()>getPacketSizeWithCrc()+2){

if(bufferTemp[getPacketSizeWithCrc()+2]==DATA_PACKET){
//Firstly handle the data packet
processDataPacket(bufferTemp);
clearSingleDataPacketFromBuffers(bufferTemp, getPacketSizeWithCrc()+2);

//Then handle the ACK from the last SET command
if(isKnownSetCommand(mCurrentCommand)){
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForAck=false;

processAckFromSetCommand(mCurrentCommand);

mTransactionCompleted = true;
setInstructionStackLock(false);
}
printLogDataForDebugging("Ack Received for Command: \t\t\t" + btCommandToString(mCurrentCommand));
}

//this is for LogAndStream support, command is transmitted and ack received
else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTREAM_CMD_RESPONSE){
printLogDataForDebugging("COMMAND TXed and ACK RECEIVED IN STREAM");
printLogDataForDebugging("INS CMD RESP");

//Firstly handle the in-stream response
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForResponse=false;
mWaitForAck=false;

processInstreamResponse();

// Need to remove here because it is an
// in-stream response while streaming so not
// handled elsewhere
if(getListofInstructions().size()>0){
removeInstruction(0);
}

mTransactionCompleted=true;
setInstructionStackLock(false);

//Then process the Data packet
processDataPacket(bufferTemp);
clearBuffers();
}
else {
printLogDataForDebugging("Unknown parsing error while streaming");
}
}
if(mByteArrayOutputStream.size()>getPacketSizeWithCrc()+2){
printLogDataForDebugging("Unknown packet error (check with JC):\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes but buffer contains " + mByteArrayOutputStream.size() + "bytes");
discardFirstBufferByte(); //throw the first byte away
}

}
//TODO: ACK in bufferTemp[0] not handled
//else if
else {
printLogDataForDebugging("Packet syncing problem:\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes. Buffer contains " + mByteArrayOutputStream.size() + "bytes"
+ "\nBuffer = " + UtilShimmer.bytesToHexStringWithSpacesFormatted(mByteArrayOutputStream.toByteArray()));
discardFirstBufferByte(); //throw the first byte away
}
}

/** Process ACK from a GET or SET command while not streaming */
private void processNotStreamingWaitForAck() {
Expand Down Expand Up @@ -1009,6 +903,112 @@ && bytesAvailableToBeRead()) {

}


protected void processPacket() {
setIamAlive(true);
byte[] bufferTemp = mByteArrayOutputStream.toByteArray();

//Data packet followed by another data packet
if(bufferTemp[0]==DATA_PACKET
&& bufferTemp[getPacketSizeWithCrc()+1]==DATA_PACKET){

if (mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && !checkCrc(bufferTemp, getPacketSize() + 1)) {
discardFirstBufferByte();
return;
}

//Handle the data packet
processDataPacket(bufferTemp);
clearSingleDataPacketFromBuffers(bufferTemp, getPacketSizeWithCrc()+1);
}

//Data packet followed by an ACK (suggesting an ACK in response to a SET BT command or else a BT response command)
else if(bufferTemp[0]==DATA_PACKET
&& bufferTemp[getPacketSizeWithCrc()+1]==ACK_COMMAND_PROCESSED){
if(mByteArrayOutputStream.size()>getPacketSizeWithCrc()+2){

if(bufferTemp[getPacketSizeWithCrc()+2]==DATA_PACKET){
//Firstly handle the data packet
processDataPacket(bufferTemp);
clearSingleDataPacketFromBuffers(bufferTemp, getPacketSizeWithCrc()+2);

//Then handle the ACK from the last SET command
if(isKnownSetCommand(mCurrentCommand)){
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForAck=false;

processAckFromSetCommand(mCurrentCommand);

mTransactionCompleted = true;
setInstructionStackLock(false);
}
printLogDataForDebugging("Ack Received for Command: \t\t\t" + btCommandToString(mCurrentCommand));
}

//this is for LogAndStream support, command is transmitted and ack received
else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTREAM_CMD_RESPONSE){
printLogDataForDebugging("COMMAND TXed and ACK RECEIVED IN STREAM");
printLogDataForDebugging("INS CMD RESP");

//Firstly handle the in-stream response
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForResponse=false;
mWaitForAck=false;

processInstreamResponse();

// Need to remove here because it is an
// in-stream response while streaming so not
// handled elsewhere
if(getListofInstructions().size()>0){
removeInstruction(0);
}

mTransactionCompleted=true;
setInstructionStackLock(false);

//Then process the Data packet
processDataPacket(bufferTemp);
clearBuffers();
}
else {
printLogDataForDebugging("Unknown parsing error while streaming");
}
}
if(mByteArrayOutputStream.size()>getPacketSizeWithCrc()+2){
printLogDataForDebugging("Unknown packet error (check with JC):\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes but buffer contains " + mByteArrayOutputStream.size() + "bytes");
discardFirstBufferByte(); //throw the first byte away
}

}
//TODO: ACK in bufferTemp[0] not handled
//else if
else {
printLogDataForDebugging("Packet syncing problem:\tExpected: " + (getPacketSizeWithCrc()+2) + "bytes. Buffer contains " + mByteArrayOutputStream.size() + "bytes"
+ "\nBuffer = " + UtilShimmer.bytesToHexStringWithSpacesFormatted(mByteArrayOutputStream.toByteArray()));
discardFirstBufferByte(); //throw the first byte away
}
}

protected void processWhileStreaming() {
byte[] byteBuffer = readBytes(1);
if(byteBuffer!=null){
mByteArrayOutputStream.write(byteBuffer[0]);
//Everytime a byte is received the timestamp is taken
if(mEnablePCTimeStamps) {
mListofPCTimeStamps.add(System.currentTimeMillis());
}
}
else {
printLogDataForDebugging("readbyte null");
}

//If there is a full packet and the subsequent sequence number of following packet
if(mByteArrayOutputStream.size()>=getPacketSizeWithCrc()+2){ // +2 because there are two acks
processPacket();
}
}

private static boolean isKnownResponse(byte response) {
return mBtResponseMap.containsKey(response);
}
Expand Down Expand Up @@ -1042,7 +1042,7 @@ private static boolean isKnownGetCommand(byte getCmd) {
return mBtGetCommandMap.containsKey(getCmd);
}

private static boolean isKnownSetCommand(byte setCmd) {
protected static boolean isKnownSetCommand(byte setCmd) {
return mBtSetCommandMap.containsKey(setCmd);
}

Expand All @@ -1064,7 +1064,7 @@ else if(currentCommand==GET_INFOMEM_COMMAND || currentCommand==GET_CALIB_DUMP_CO
}

/** process responses to in-stream response */
private void processInstreamResponse() {
protected void processInstreamResponse() {
byte[] inStreamResponseCommandBuffer = readBytes(1, INSTREAM_CMD_RESPONSE);
if(inStreamResponseCommandBuffer!=null){
byte inStreamResponseCommand = inStreamResponseCommandBuffer[0];
Expand Down Expand Up @@ -1153,7 +1153,7 @@ private void buildAndSendMsg(byte[] packet, COMMUNICATION_TYPE fwType, boolean t
/**this is to clear the buffer
*
*/
private void clearSerialBuffer() {
protected void clearSerialBuffer() {
startTimerCheckForSerialPortClear();

List<Byte> buffer = new ArrayList<Byte>();
Expand Down Expand Up @@ -1219,7 +1219,7 @@ public void processDataPacket(byte[] bufferTemp){
* @param bufferTemp
* @param packetSize
*/
private void clearSingleDataPacketFromBuffers(byte[] bufferTemp, int packetSize) {
protected void clearSingleDataPacketFromBuffers(byte[] bufferTemp, int packetSize) {
mByteArrayOutputStream.reset();
mByteArrayOutputStream.write(bufferTemp[packetSize]);
// consolePrintLn(Integer.toString(bufferTemp[mPacketSize+2]));
Expand All @@ -1238,15 +1238,15 @@ private void clearSingleDataPacketFromBuffers(byte[] bufferTemp, int packetSize)
/**
*
*/
private void clearBuffers() {
protected void clearBuffers() {
mByteArrayOutputStream.reset();
mListofPCTimeStamps.clear();
}

/**
*
*/
private void discardFirstBufferByte(){
protected void discardFirstBufferByte(){
byte[] bTemp = mByteArrayOutputStream.toByteArray();
mByteArrayOutputStream.reset();
mByteArrayOutputStream.write(bTemp, 1, bTemp.length-1); //this will throw the first byte away
Expand Down Expand Up @@ -1801,7 +1801,7 @@ else if(responseCommand==BT_FW_VERSION_STR_RESPONSE) {
/**
* @param currentCommand
*/
private void processAckFromSetCommand(byte currentCommand) {
protected void processAckFromSetCommand(byte currentCommand) {
// check for null and size were put in because if Shimmer was abruptly
// disconnected there is sometimes indexoutofboundsexceptions
if(getListofInstructions().size() > 0){
Expand Down Expand Up @@ -2315,7 +2315,7 @@ private void processShimmer2EmgCalReadBytes(){
}


private byte[] readBytes(int numBytes, byte btCommand) {
protected byte[] readBytes(int numBytes, byte btCommand) {
byte[] response = readBytes(numBytes);
if(response==null){
printLogDataForDebugging("NULL serial readBytes response for command: " + btCommandToString(btCommand));
Expand All @@ -2325,7 +2325,7 @@ private byte[] readBytes(int numBytes, byte btCommand) {
/**
* @param statusByte
*/
private void parseStatusByte(byte statusByte){
protected void parseStatusByte(byte statusByte){
Boolean savedDockedState = isDocked();

setIsDocked(((statusByte & (0x01 << 0)) > 0)? true:false);
Expand Down Expand Up @@ -2371,7 +2371,7 @@ public boolean isSupportedSdInfoInStatus() {
return isThisVerCompatibleWith(FW_ID.LOGANDSTREAM, 0, 7, 12);
}

private boolean isSupportedInStreamCmds() {
protected boolean isSupportedInStreamCmds() {
if((getFirmwareIdentifier()==FW_ID.LOGANDSTREAM
|| isThisVerCompatibleWith(FW_ID.BTSTREAM, 0, 8, 1))&& getHardwareVersion()!=HW_ID.SHIMMER_2R){
return true;
Expand Down Expand Up @@ -5476,7 +5476,7 @@ private byte[] getInstruction(){
return null;
}

private synchronized void removeInstruction(int index){
protected synchronized void removeInstruction(int index){
synchronized(mListofInstructions){
try {
mListofInstructions.remove(index);
Expand Down
Loading