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 @@ -987,12 +987,6 @@ else if(isKnownGetCommand(mCurrentCommand)){
}

}
/* Firmware without the event channel NACKs its probe. That NACK
* is an answer, not a failure, so it is taken here before the
* general refusal below. */
else if((byte)byteBuffer[0]==NACK_COMMAND_PROCESSED && isEventChannelProbeInFlight()) {
processEventChannelProbeNack();
}
/* The Shimmer refused the command. Without this branch the NACK
* was discarded, mWaitForAck stayed true and the instruction stack
* stayed locked until the ACK timer expired and tore the
Expand Down Expand Up @@ -2423,12 +2417,16 @@ else if(responseCommand==EVENT_CHANNEL_STATUS_RESPONSE) {
* <p>A refused log start is also taken for a log that did not start
* ({@link #processLogStartRefused(byte)}).
*
* <p>A NACK of the event channel's probe ({@link #readEventChannelSupport()})
* is an answer, not a failure: the firmware has no event channel. It is
* unwound the same way, but the application is not told that a command
* was refused.
*
* <p>Protected because ShimmerAndroidAPI's Shimmer has its own streaming
* parser, which needs to fail the command in flight the same way.
*/
protected void processNackFromCommand() {
stopTimerCheckForAckOrResp(); //cancel the ack timer
printLogDataForDebugging("NACK Received for Command: \t\t" + btCommandToString(mCurrentCommand));

byte refusedCommand = mCurrentCommand;

Expand All @@ -2438,9 +2436,18 @@ protected void processNackFromCommand() {
* skip that command. */
boolean instructionStillQueued = mWaitForAck;

//Asked before the instruction is removed, since it looks at it
boolean eventChannelProbe = instructionStillQueued && isEventChannelProbeInFlight();
printLogDataForDebugging("NACK Received for Command: \t\t" + btCommandToString(mCurrentCommand)
+ (eventChannelProbe ? ", no event channel" : ""));

mWaitForAck=false;
mWaitForResponse=false;

if(eventChannelProbe){
setEventChannelSupported(false);
}

/* Mirror the ACK path's progress report so that an operation containing a
* refused command can still reach its end value.
* BluetoothProgressReportPerDevice.updateProgress() derives the counter
Expand Down Expand Up @@ -2471,7 +2478,9 @@ protected void processNackFromCommand() {
if(refusedCommand==START_LOGGING_ONLY_COMMAND || refusedCommand==START_SDBT_COMMAND){
processLogStartRefused(refusedCommand);
}
eventCommandRefused(refusedCommand);
if(!eventChannelProbe){
eventCommandRefused(refusedCommand);
}
}

/**
Expand Down Expand Up @@ -5321,12 +5330,28 @@ public void writeConfigBytes(){
public void writeConfigBytes(boolean readBackAfter){
if(this.getFirmwareVersionCode()>=6){
byte[] configBytes = generateConfigBytesForWritingToShimmer();
int idxButtonLogMode = (mConfigByteLayout instanceof ConfigByteLayoutShimmer3) ? ((ConfigByteLayoutShimmer3) mConfigByteLayout).idxButtonLogMode : 0;
if(idxButtonLogMode>0 && !isButtonLogModeKnown()){
// Byte 232 (DEV-971) was never read from this Shimmer nor set,
// as when a configuration is written on connect before it is
// read. Skip it, so that the Shimmer keeps its own.
writeMem(SET_INFOMEM_COMMAND, mConfigByteLayout.MSP430_5XX_INFOMEM_D_ADDRESS, configBytes, mConfigByteLayout.MSP430_5XX_INFOMEM_LAST_ADDRESS, idxButtonLogMode);
// Bytes the driver does not have from this Shimmer, as when a
// configuration is written on connect before it is read. They
// are skipped, so that the Shimmer keeps its own. Only on
// firmware that can have byte 232: older firmware is still
// written in whole chunks.
List<Integer> skipIndexes = new ArrayList<Integer>();
if(mConfigByteLayout instanceof ConfigByteLayoutShimmer3){
ConfigByteLayoutShimmer3 configByteLayoutCast = (ConfigByteLayoutShimmer3) mConfigByteLayout;
if(configByteLayoutCast.idxButtonLogMode>0){
// Byte 232 (DEV-971), neither read nor set
if(!isButtonLogModeKnown()){
skipIndexes.add(configByteLayoutCast.idxButtonLogMode);
}
// Byte 233 (DEV-99, DEV-1413) on a Shimmer3R, not read
// and the sound level not turned on
if(configByteLayoutCast.idxAudioConfig>0 && !isAudioConfigKnown()){
skipIndexes.add(configByteLayoutCast.idxAudioConfig);
}
}
}
if(!skipIndexes.isEmpty()){
writeMem(SET_INFOMEM_COMMAND, mConfigByteLayout.MSP430_5XX_INFOMEM_D_ADDRESS, configBytes, mConfigByteLayout.MSP430_5XX_INFOMEM_LAST_ADDRESS, skipIndexes);
if(readBackAfter){
readConfigBytes();
}
Expand Down Expand Up @@ -5354,19 +5379,19 @@ public void writeConfigBytes(int startAddress, byte[] buf, int maxMemAddress, bo
}

public void writeMem(int command, int startAddress, byte[] buf, int maxMemAddress){
writeMem(command, startAddress, buf, maxMemAddress, -1);
writeMem(command, startAddress, buf, maxMemAddress, Collections.<Integer>emptyList());
}

/**
* Writes buf in chunks of up to 128 bytes, as {@link #writeMem(int, int, byte[], int)}
* does, but leaves out the byte at skipIndex: the chunk holding it goes as
* the bytes before it and the bytes after it. Every other chunk starts
* where it would have, which matters to the firmware for the chunks at 0
* and 128.
* does, but leaves out the bytes at skipIndexes: a chunk holding any goes
* as the runs of bytes between them, each at its own address. Every other
* chunk starts where it would have, which matters to the firmware for the
* chunks at 0 and 128.
*
* @param skipIndex the index into buf of the byte not to write, or -1 to write them all
* @param skipIndexes the indexes into buf of the bytes not to write, empty to write them all
*/
private void writeMem(int command, int startAddress, byte[] buf, int maxMemAddress, int skipIndex){
private void writeMem(int command, int startAddress, byte[] buf, int maxMemAddress, List<Integer> skipIndexes){
this.mNumOfMemSetCmds = 0;

if(this.getFirmwareVersionCode()>=6){
Expand All @@ -5390,23 +5415,18 @@ private void writeMem(int command, int startAddress, byte[] buf, int maxMemAddre
currentPacketNumBytes = numBytesRemaining;
}

// The chunk, or its runs between the bytes skipped
int chunkEnd = currentBytePointer + currentPacketNumBytes;
if(skipIndex>=currentBytePointer && skipIndex<chunkEnd){
if(skipIndex>currentBytePointer){
writeMemCommand(command, currentStartAddr, Arrays.copyOfRange(buf, currentBytePointer, skipIndex));
mNumOfMemSetCmds += 1;
}
if(skipIndex+1<chunkEnd){
writeMemCommand(command, currentStartAddr + (skipIndex+1-currentBytePointer), Arrays.copyOfRange(buf, skipIndex+1, chunkEnd));
mNumOfMemSetCmds += 1;
int runStart = currentBytePointer;
for(int i=currentBytePointer;i<=chunkEnd;i++){
if(i==chunkEnd || skipIndexes.contains(i)){
if(i>runStart){
writeMemCommand(command, currentStartAddr + (runStart-currentBytePointer), Arrays.copyOfRange(buf, runStart, i));
mNumOfMemSetCmds += 1;
}
runStart = i+1;
}
}
else {
byte[] infoSegBuf = Arrays.copyOfRange(buf, currentBytePointer, chunkEnd);

writeMemCommand(command, currentStartAddr, infoSegBuf);
mNumOfMemSetCmds += 1;
}

currentStartAddr += currentPacketNumBytes;
numBytesRemaining -= currentPacketNumBytes;
Expand Down Expand Up @@ -5494,27 +5514,6 @@ private boolean isEventChannelProbeInFlight() {
&& instruction[1]==(byte) SHIMMER_FEATURE.EVENT_STREAM.ordinal();
}

/**
* The firmware NACKed FEATURE_EVENT_STREAM, so it has no event channel.
* Completes the command as an ACK does, the progress report included, so
* that the connection stays up and the operation it is part of finishes.
*/
private void processEventChannelProbeNack() {
stopTimerCheckForAckOrResp(); //cancel the ack timer
mWaitForAck=false;
printLogDataForDebugging("NACK Received for Command: \t\t" + btCommandToString(mCurrentCommand) + ", no event channel");

setEventChannelSupported(false);

if(mOperationUnderway){
sendProgressReport(new BluetoothProgressReportPerCmd(mCurrentCommand, getListofInstructions().size(), mMyBluetoothAddress, getComPort()));
}
removeInstruction(0);

mTransactionCompleted = true;
setInstructionStackLock(false);
}

/**
* Opts this connection in to, or out of, the event channel's records
* (DEV-971): SET_FEATURE with FEATURE_EVENT_STREAM. Once in, every button and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6774,6 +6774,16 @@ public boolean isButtonLogModeKnown() {
return mIsButtonLogModeKnown;
}

/**
* Whether the driver has the Shimmer3R's own byte 233, the microphone's
* configuration ({@link SensorSoundLevel#isAudioConfigKnown()}). True
* where there is no such byte.
*/
public boolean isAudioConfigKnown() {
AbstractSensor abstractSensor = getSensorClass(AbstractSensor.SENSORS.SOUND_LEVEL);
return !(abstractSensor instanceof SensorSoundLevel) || ((SensorSoundLevel) abstractSensor).isAudioConfigKnown();
}

/**
* Whether byte 232 can be taken at its word: the Shimmer answered the event
* channel probe, or the byte holds a value the firmware keeps. Firmware
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public class SensorSoundLevel extends AbstractSensor {

/** Byte 233's audio mode and gain preset as read (bits 0-3, DEV-99), written back as they are */
private int mAudioModeAndGain = 0;
/** Whether byte 233 has been read from the Shimmer, and so mAudioModeAndGain is its own */
private boolean mIsAudioConfigRead = false;

public class GuiLabelSensors{
public static final String SOUND_LEVEL = "Sound Level";
Expand Down Expand Up @@ -241,6 +243,7 @@ public void configBytesParse(ShimmerDevice shimmerDevice, byte[] configBytes, CO
value = 0;
}
mAudioModeAndGain = value & configByteLayoutCast.maskAudioModeAndGain;
mIsAudioConfigRead = true;

SensorDetails sensorDetails = mSensorMap.get(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL);
if(sensorDetails!=null){
Expand All @@ -264,6 +267,18 @@ public int getAudioModeAndGain() {
return mAudioModeAndGain;
}

/**
* Whether a configuration write has a byte 233 to give: it was read from
* the Shimmer, or the sound level is on, the one part of it set here.
* Before a read, bits 0-3 are not known, and writing them as 0 would turn
* the Shimmer's audio mode off. With the sound level turned on before a
* read they are written as 0 all the same, since one bit cannot be written
* on its own.
*/
public boolean isAudioConfigKnown() {
return mIsAudioConfigRead || isSensorEnabled(Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL);
}

@Override
public Object setConfigValueUsingConfigLabel(Integer sensorId, String configLabel, Object valueToSet) {
return super.setConfigValueUsingConfigLabelCommon(sensorId, configLabel, valueToSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*
* @author Mark Nolan
*/
public class API_00036_ButtonLogModeTest {
public class API_00045_ButtonLogModeTest {

/** What a press does, in the table's words */
private enum PRESS {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@ public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutExce
private static class WatchedShimmer extends ShimmerPC {
private static final long serialVersionUID = 1L;
final List<Byte> progressReports = Collections.synchronizedList(new ArrayList<Byte>());
final List<Byte> commandsRefused = Collections.synchronizedList(new ArrayList<Byte>());
volatile boolean connectionLost = false;

WatchedShimmer() {
super("COM99");
}

@Override
protected void eventCommandRefused(byte command) {
commandsRefused.add(command);
super.eventCommandRefused(command);
}

@Override
protected void sendProgressReport(BluetoothProgressReportPerCmd pRPC) {
progressReports.add((byte) pRPC.mCommandCompleted);
Expand Down Expand Up @@ -180,6 +187,15 @@ public void aNackMeansItIsNotAndTheConnectionStaysUp() throws Exception {
assertFalse(mDevice.connectionLost);
}

@Test
public void theProbesNackIsAnAnswerNotARefusal() throws Exception {
connectedShimmer(S3R_V1_1_25, ShimmerObject.NACK_COMMAND_PROCESSED);
mDevice.readEventChannelSupport();
waitForInstructionsDone(2000);
assertEquals(Boolean.FALSE, mDevice.isEventChannelSupported());
assertTrue("the application is not told a command was refused", mDevice.commandsRefused.isEmpty());
}

@Test
public void aNackStillCountsTowardsTheOperation() throws Exception {
connectedShimmer(S3R_V1_1_25, ShimmerObject.NACK_COMMAND_PROCESSED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
*
* @author Mark Nolan
*/
public class API_00037_ButtonLogModeConfigWriteTest {
public class API_00046_ButtonLogModeConfigWriteTest {

private static final int IDX_BUTTON_LOG_MODE = 232;
private static final int IDX_TRIAL_CONFIG0 = 217;
Expand Down Expand Up @@ -282,13 +282,12 @@ private static int[][] allChunks(ShimmerBluetooth shimmer) {
return chunks;
}

/** The same, with the chunk at 128 sent either side of byte 232 */
private static int[][] chunksWithout232(ShimmerBluetooth shimmer) {
/** The same, with the chunk at 128 sent as the parts given */
private static int[][] chunk128As(ShimmerBluetooth shimmer, int[]... parts) {
List<int[]> chunks = new ArrayList<int[]>();
for(int[] chunk:allChunks(shimmer)){
if(chunk[0]==128){
chunks.add(new int[] {128, 104});
chunks.add(new int[] {233, 23});
chunks.addAll(Arrays.asList(parts));
}
else {
chunks.add(chunk);
Expand All @@ -297,14 +296,30 @@ private static int[][] chunksWithout232(ShimmerBluetooth shimmer) {
return chunks.toArray(new int[0][]);
}

/** Either side of byte 232 */
private static int[][] chunksWithout232(ShimmerBluetooth shimmer) {
return chunk128As(shimmer, new int[] {128, 104}, new int[] {233, 23});
}

/** Either side of bytes 232 and 233: a Shimmer3R's, before a read (API_00047) */
private static int[][] chunksWithout232And233(ShimmerBluetooth shimmer) {
return chunk128As(shimmer, new int[] {128, 104}, new int[] {234, 22});
}

/** Either side of byte 233 */
private static int[][] chunksWithout233(ShimmerBluetooth shimmer) {
return chunk128As(shimmer, new int[] {128, 105}, new int[] {234, 22});
}

@Test
public void beforeAReadTheBluetoothWriteLeavesTheShimmersByte() {
ProbedShimmer shimmer = shimmer(S3R_EVENT_CHANNEL_CAPABLE);
assertFalse(shimmer.isButtonLogModeKnown());
shimmer.writeConfigBytes(false);
assertChunks("a configuration written on connect, before the read", chunksWithout232(shimmer), shimmer);
assertChunks("a configuration written on connect, before the read; a Shimmer3R's byte 233 is left out too",
chunksWithout232And233(shimmer), shimmer);

// The bytes either side of 232 are the image's
// The bytes either side of 232 and 233 are the image's
byte[] image = shimmer.generateConfigBytesForWritingToShimmer();
for(byte[] instruction:shimmer.getListofInstructions()){
if(instruction!=null && instruction[0]==ShimmerObject.SET_INFOMEM_COMMAND){
Expand All @@ -328,7 +343,7 @@ public void onceReadOrSetTheBluetoothWriteHasTheByte() {
ProbedShimmer set = shimmer(S3R_EVENT_CHANNEL_CAPABLE);
set.setButtonMode(ButtonLogMode.BUTTON_MODE_DISABLED);
set.writeConfigBytes(false);
assertChunks("set", allChunks(set), set);
assertChunks("set, with byte 233 still unread", chunksWithout233(set), set);
byte[] chunk = infoMemChunkAt(set, 128);
assertEquals("set, before anything was read", 0x01, chunk[4 + IDX_BUTTON_LOG_MODE - 128] & 0xFF);
}
Expand Down
Loading