diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index befd46761..32c08f967 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -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 @@ -2423,12 +2417,16 @@ else if(responseCommand==EVENT_CHANNEL_STATUS_RESPONSE) { *

A refused log start is also taken for a log that did not start * ({@link #processLogStartRefused(byte)}). * + *

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. + * *

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; @@ -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 @@ -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); + } } /** @@ -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 skipIndexes = new ArrayList(); + 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(); } @@ -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.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 skipIndexes){ this.mNumOfMemSetCmds = 0; if(this.getFirmwareVersionCode()>=6){ @@ -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 && skipIndexcurrentBytePointer){ - writeMemCommand(command, currentStartAddr, Arrays.copyOfRange(buf, currentBytePointer, skipIndex)); - mNumOfMemSetCmds += 1; - } - if(skipIndex+1runStart){ + 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; @@ -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 diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java index 52f084551..dcf10063e 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java @@ -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 diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java index 8365b00c3..092877676 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/soundlevel/SensorSoundLevel.java @@ -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"; @@ -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){ @@ -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); diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00036_ButtonLogModeTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00045_ButtonLogModeTest.java similarity index 99% rename from ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00036_ButtonLogModeTest.java rename to ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00045_ButtonLogModeTest.java index ec895edf9..647205381 100644 --- a/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00036_ButtonLogModeTest.java +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00045_ButtonLogModeTest.java @@ -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 { diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00038_EventChannelProbeTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00038_EventChannelProbeTest.java index 58e7a74c7..2990a817f 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00038_EventChannelProbeTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00038_EventChannelProbeTest.java @@ -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 progressReports = Collections.synchronizedList(new ArrayList()); + final List commandsRefused = Collections.synchronizedList(new ArrayList()); 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); @@ -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); diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00037_ButtonLogModeConfigWriteTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00046_ButtonLogModeConfigWriteTest.java similarity index 93% rename from ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00037_ButtonLogModeConfigWriteTest.java rename to ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00046_ButtonLogModeConfigWriteTest.java index 1ad040e28..a974f217e 100644 --- a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00037_ButtonLogModeConfigWriteTest.java +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00046_ButtonLogModeConfigWriteTest.java @@ -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; @@ -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 chunks = new ArrayList(); 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); @@ -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){ @@ -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); } diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java new file mode 100644 index 000000000..caa774473 --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00047_AudioConfigWriteTest.java @@ -0,0 +1,150 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import com.shimmerresearch.driver.Configuration; +import com.shimmerresearch.driver.Configuration.COMMUNICATION_TYPE; +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.pcDriver.ShimmerPC; + +/** + * Configuration byte 233 on a Shimmer3R (DEV-99 audio mode and gain in bits + * 0-3, DEV-1413 sound level in bit 4) through a Bluetooth configuration write. + * The host can set bit 4 but never bits 0-3, which it only gives back as read. + * So before a read, the write leaves the byte out, as it does byte 232 + * (API_00046), and the Shimmer keeps its own. The one exception is the sound + * level turned on before a read: the byte is then written with bits 0-3 at 0, + * since a SET_INFOMEM cannot write one bit. + *

+ * No hardware. It lives in this package because the instruction list is a + * protected member of ShimmerBluetooth. + */ +public class API_00047_AudioConfigWriteTest { + + private static final int IDX_BUTTON_LOG_MODE = 232; + private static final int IDX_AUDIO_CONFIG = 233; + private static final int SOUND_LEVEL_BIT = 0x10; + + /** v1.01.026, the first release with the sound level */ + private static final ShimmerVerObject S3R = new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 26); + + private static ShimmerPC shimmer() { + ShimmerPC shimmer = new ShimmerPC("COM99"); + shimmer.setShimmerVersionObjectAndCreateSensorMap(S3R); + return shimmer; + } + + private static ShimmerPC readFrom(int buttonLogMode, int audioConfig) { + byte[] config = shimmer().generateConfigBytesForWritingToShimmer().clone(); + config[IDX_BUTTON_LOG_MODE] = (byte) buttonLogMode; + config[IDX_AUDIO_CONFIG] = (byte) audioConfig; + ShimmerPC shimmer = shimmer(); + shimmer.configBytesParse(config, COMMUNICATION_TYPE.BLUETOOTH); + return shimmer; + } + + /** Each SET_INFOMEM queued: [0x8C, length, address LSB, address MSB, data...] */ + private static List infoMemWrites(ShimmerBluetooth shimmer) { + List writes = new ArrayList(); + for(byte[] instruction:shimmer.getListofInstructions()){ + if(instruction!=null && instruction[0]==ShimmerObject.SET_INFOMEM_COMMAND){ + writes.add(instruction); + } + } + return writes; + } + + private static int address(byte[] write) { + return (write[2]&0xFF) | ((write[3]&0xFF)<<8); + } + + private static int length(byte[] write) { + return write[1]&0xFF; + } + + /** The value written at idx, or -1 if no SET_INFOMEM covers it */ + private static int writtenAt(ShimmerBluetooth shimmer, int idx) { + for(byte[] write:infoMemWrites(shimmer)){ + if(idx>=address(write) && idx chunks(ShimmerBluetooth shimmer) { + List chunks = new ArrayList(); + for(byte[] write:infoMemWrites(shimmer)){ + chunks.add(Arrays.toString(new int[] {address(write), length(write)})); + } + return chunks; + } + + @Test + public void beforeAReadTheBluetoothWriteLeavesTheShimmersAudioByte() { + ShimmerPC shimmer = shimmer(); + shimmer.writeConfigBytes(false); + assertEquals("a configuration written on connect, before the read, would turn the Shimmer's audio mode and sound level off", + -1, writtenAt(shimmer, IDX_AUDIO_CONFIG)); + } + + @Test + public void beforeAReadBytes232And233AreBothLeftOut() { + ShimmerPC shimmer = shimmer(); + shimmer.writeConfigBytes(false); + int length = shimmer.generateConfigBytesForWritingToShimmer().length; + List wanted = new ArrayList(); + for(int address=0;address