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 @@ -1107,8 +1107,17 @@ else if(byteBuffer[0]==ACK_COMMAND_PROCESSED) {
}
}
}

clearSerialBuffer();

/* With the event channel on, records can be queued behind
* anything read here: behind a status push (when a press starts
* logging, say), or behind the link CRC a bare ACK leaves (the
* opt-in's ACK, whose first record follows at once). So only the
* bytes just read are dropped, and the rest are left for the next
* pass; clearing the buffer would wipe the records with them
* (DEV-971). */
if(!mEventFramesPossible) {
clearSerialBuffer();
}
}
}

Expand Down Expand Up @@ -1170,7 +1179,32 @@ else if(bufferTemp[0]==DATA_PACKET
//Then handle the ACK from the last SET command
processAckWhileStreaming();
}


//The event channel status, [ACK][0xBA][9], which is not wrapped in 0x8A:
//readEventChannelStatus() is not sent while streaming, but one sent just
//before a stream started is answered once packets flow (DEV-971)
else if(bufferTemp[getPacketSizeWithCrc()+2]==EVENT_CHANNEL_STATUS_RESPONSE
&& mWaitForAck && mCurrentCommand==GET_EVENT_CHANNEL_STATUS_COMMAND){
byte[] responseData = readBytes(EventChannelStatus.LENGTH, EVENT_CHANNEL_STATUS_RESPONSE);
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForAck=false;
mWaitForResponse=false;
if(responseData!=null){
if(mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF){
clearCrcBytesFromBuffer(EVENT_CHANNEL_STATUS_RESPONSE);
}
takeEventChannelStatus(responseData);
}
processDataPacket(bufferTemp);
if(getListofInstructions().size()>0){
removeInstruction(0);
}
mTransactionCompleted=true;
setInstructionStackLock(false);
clearBuffers();
return;
}

//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");
Expand All @@ -1187,7 +1221,13 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
if(mEventFramesPossible && inStreamResponseCommand!=null
&& inStreamResponseCommand[0]==EVENT_RECORD_RESPONSE){
processDataPacket(bufferTemp);
processAckWhileStreaming();
if(mWaitForAck){
processAckWhileStreaming();
}
else {
// Nothing waits for an ACK, so the SET that went last must not be acknowledged again
printLogDataForDebugging("ACK with no command awaiting it, before an event record - ignored");
}
readEventFrameRestFromPort();
clearBuffers();
return;
Expand Down Expand Up @@ -1238,12 +1278,30 @@ else if(isSupportedInStreamCmds() && bufferTemp[getPacketSizeWithCrc()+2]==INSTR
else if(mEventFramesPossible
&& bufferTemp[0]==DATA_PACKET
&& bufferTemp[getPacketSizeWithCrc()+1]==INSTREAM_CMD_RESPONSE){
if (mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF && !checkCrc(bufferTemp, getPacketSize() + 1)) {
discardBufferBytesToNextPacket();
return;
int packetEnd = getPacketSizeWithCrc()+1;
if (mBtCommsCrcModeCurrent != BT_CRC_MODE.OFF) {
//The packet's own CRC confirms it
if (!checkCrc(bufferTemp, getPacketSize() + 1)) {
discardBufferBytesToNextPacket();
return;
}
}
else {
//With no link CRC, a 0x8A where the next packet should start is
//only a frame, and the packet before it only a packet, once the
//frame's own CRC checks out. Otherwise the boundary is wrong (a
//byte lost, say), and the bytes are discarded as before.
int frameLength = EventFrame.getFrameLength(bufferTemp, packetEnd, bufferTemp.length-packetEnd);
if (frameLength==EventFrame.NEED_MORE) {
return; //wait for the rest of the frame
}
if (frameLength==EventFrame.NOT_A_FRAME) {
discardBufferBytesToNextPacket();
return;
}
}
processDataPacket(bufferTemp);
clearBytesFromStartOfBuffers(bufferTemp, getPacketSizeWithCrc()+1);
clearBytesFromStartOfBuffers(bufferTemp, packetEnd);
}
//Data packet followed by a NACK (a command refused while streaming, e.g.
//any SET blocked by the firmware because it is sensing, or a log start the
Expand Down Expand Up @@ -2164,7 +2222,14 @@ else if(responseCommand==RWC_RESPONSE) {
}
}
else if(responseCommand==INSTREAM_CMD_RESPONSE) {
processInstreamResponse(false);
byte[] inStreamResponseCommand = readBytes(1, INSTREAM_CMD_RESPONSE);
if(inStreamResponseCommand!=null){
processInstreamResponse(inStreamResponseCommand[0], false);
if(inStreamResponseCommand[0]==EVENT_RECORD_RESPONSE && mEventFramesPossible){
// An event record frame carries its own CRC and never the link CRC (DEV-971)
responseWasParsed = false;
}
}
}
else if(responseCommand==LSM303DLHC_ACCEL_LPMODE_RESPONSE) {
byte[] responseData = readBytes(1, responseCommand);
Expand Down Expand Up @@ -2327,16 +2392,7 @@ else if(responseCommand==BT_FW_VERSION_STR_RESPONSE) {
else if(responseCommand==EVENT_CHANNEL_STATUS_RESPONSE) {
byte[] responseData = readBytes(EventChannelStatus.LENGTH, responseCommand);
if(responseData!=null){
EventChannelStatus status = EventChannelStatus.parse(responseData, 0);
mEventChannelStatus = status;
printLogDataForDebugging("Event channel status:\t" + status);
for(EventChannelListener listener:getEventChannelListeners()){
try {
listener.onEventChannelStatus(this, status);
} catch (RuntimeException e) {
printLogDataForDebugging("Event channel listener threw: " + e);
}
}
takeEventChannelStatus(responseData);
}
}

Expand Down Expand Up @@ -5510,13 +5566,31 @@ public boolean isEventStreamRequested() {
* @return whether it was sent
*/
public boolean readEventChannelStatus() {
if(!Boolean.TRUE.equals(isEventChannelSupported()) || isStreaming()) {
if(!Boolean.TRUE.equals(isEventChannelSupported()) || isStreaming() || isStreamStartQueued()) {
return false;
}
writeInstruction(new byte[]{GET_EVENT_CHANNEL_STATUS_COMMAND});
return true;
}

/**
* Whether a start of streaming is queued or in flight: the instruction list
* keeps a command until it is answered, and isStreaming() turns true only
* then. A status read queued behind it would be answered between data packets.
*/
private boolean isStreamStartQueued() {
List<byte[]> instructions = getListofInstructions();
synchronized(instructions){
for(byte[] instruction:instructions){
if(instruction!=null && instruction.length>0
&& (instruction[0]==START_STREAMING_COMMAND || instruction[0]==START_SDBT_COMMAND)){
return true;
}
}
}
return false;
}

/** The last answer to {@link #readEventChannelStatus()}, or null */
public EventChannelStatus getEventChannelStatus() {
return mEventChannelStatus;
Expand Down Expand Up @@ -5626,6 +5700,20 @@ private void readEventFrameRestFromPort() {
dispatchEventRecord(record);
}

/** Keeps the event channel status's 9 bytes and hands them to each listener */
private void takeEventChannelStatus(byte[] responseData) {
EventChannelStatus status = EventChannelStatus.parse(responseData, 0);
mEventChannelStatus = status;
printLogDataForDebugging("Event channel status:\t" + status);
for(EventChannelListener listener:getEventChannelListeners()){
try {
listener.onEventChannelStatus(this, status);
} catch (RuntimeException e) {
printLogDataForDebugging("Event channel listener threw: " + e);
}
}
}

private void dispatchEventRecord(EventRecord record) {
mEventFramesReceived++;
printLogDataForDebugging("Event record:\t" + record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,73 +331,45 @@ public void framesAreNotParsedWithoutAnOptIn() {
// Not streaming, with the IOThread running
// ------------------------------------------------------------------

/** Answers as LogAndStream_Shimmer3R v1.01.026 does */
private static class AnsweringRadio implements ByteCommunication {
final ConcurrentLinkedDeque<Byte> rx = new ConcurrentLinkedDeque<Byte>();
final List<byte[]> written = Collections.synchronizedList(new ArrayList<byte[]>());
/** Answers as LogAndStream_Shimmer3R v1.01.026 does, each reply one burst */
private static class AnsweringRadio extends WaitingTestRadio {
/** Sent ahead of the next command's ACK, as a record already queued would be */
volatile byte[] beforeNextAck = null;
/** Sent between the next status command's ACK and its reply */
volatile byte[] betweenAckAndReply = null;

void queue(byte... bytes) {
for(byte b:bytes){
rx.add(b);
}
}

@Override
public boolean writeBytes(byte[] buffer) {
written.add(buffer.clone());
byte[] answer(byte[] buffer) {
java.io.ByteArrayOutputStream reply = new java.io.ByteArrayOutputStream();
byte[] early = beforeNextAck;
if(early!=null) {
beforeNextAck = null;
queue(early);
reply.write(early, 0, early.length);
}
if(buffer[0]==ShimmerObject.SET_FEATURE && buffer[1]==(byte) SHIMMER_FEATURE.EVENT_STREAM.ordinal()){
queue(ShimmerObject.ACK_COMMAND_PROCESSED);
reply.write(ShimmerObject.ACK_COMMAND_PROCESSED);
if(buffer[2]!=0) {
// The first record at the opt-in, then one already made
queue(INITIAL_STATE);
queue(MARKER_PRESS);
reply.write(INITIAL_STATE, 0, INITIAL_STATE.length);
reply.write(MARKER_PRESS, 0, MARKER_PRESS.length);
}
}
else if(buffer[0]==ShimmerObject.GET_EVENT_CHANNEL_STATUS_COMMAND){
queue(ShimmerObject.ACK_COMMAND_PROCESSED);
reply.write(ShimmerObject.ACK_COMMAND_PROCESSED);
byte[] between = betweenAckAndReply;
if(between!=null) {
betweenAckAndReply = null;
queue(between);
reply.write(between, 0, between.length);
}
queue(ShimmerObject.EVENT_CHANNEL_STATUS_RESPONSE);
queue(hex("03 0B 02 05 00 02 00 01 00"));
reply.write(ShimmerObject.EVENT_CHANNEL_STATUS_RESPONSE);
byte[] status = hex("03 0B 02 05 00 02 00 01 00");
reply.write(status, 0, status.length);
}
else {
queue(ShimmerObject.ACK_COMMAND_PROCESSED);
reply.write(ShimmerObject.ACK_COMMAND_PROCESSED);
}
return true;
return reply.toByteArray();
}

@Override
public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException {
if(rx.size()<byteCount){
throw new SerialPortTimeoutException("TEST", "readBytes", timeout);
}
byte[] out = new byte[byteCount];
for(int i=0;i<byteCount;i++){
out[i] = rx.poll();
}
return out;
}

@Override public int getInputBufferBytesCount() { return rx.size(); }
@Override public boolean isOpened() { return true; }
@Override public boolean closePort() { return true; }
@Override public boolean openPort() { return true; }
@Override public boolean setParams(int i, int j, int k, int l) { return true; }
@Override public boolean purgePort(int i) { return true; }
@Override public void setByteCommunicationListener(ByteCommunicationListener byteCommListener) { }
@Override public void removeRadioListenerList() { }
}

private ShimmerPC mIdleDevice;
Expand Down
Loading