Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
b6842e6
DEV-982: handle NACK from a Shimmer3/3R instead of dropping the conne…
marknolan Sep 3, 2026
648897b
DEV-982: only drop the queued instruction when it is still queued (Co…
marknolan Sep 3, 2026
8bd5b94
DEV-982: complete the operation when a command is refused
marknolan Sep 21, 2026
586c58d
DEV-982: add a bench test for the non-streaming NACK path
marknolan Sep 21, 2026
d2733d8
Merge remote-tracking branch 'origin/master' into DEV-982_nack_handling
MAzalya Oct 6, 2026
8a8a771
Merge remote-tracking branch 'origin/master' into DEV-982_nack_handling
marknolan Nov 10, 2026
4561b05
DEV-1431: a log start the device refuses (NACK) ends at once, idle or…
marknolan Nov 10, 2026
7351b7e
DEV-982: TestNackNotStreaming gets its NACK from 0x30, not SD sync
marknolan Nov 10, 2026
0ef5ddd
DEV-1413: Shimmer3R sound level channel (0x38), and keep byte 233 on …
marknolan Nov 11, 2026
e1804c0
DEV-1413: name v1.01.026, the first firmware release with the sound l…
marknolan Nov 11, 2026
1dd2599
Merge pull request #293 from ShimmerResearch/DEV-982_nack_handling
JongChern Nov 13, 2026
85b7f7c
Merge pull request #345 from ShimmerResearch/DEV-1431_nack_refused_lo…
JongChern Nov 13, 2026
a09a0d4
DEV-1413: offer the sound level only from LogAndStream_Shimmer3R v1.0…
JongChern Nov 13, 2026
fb5d1f3
Merge pull request #346 from ShimmerResearch/DEV-1413_sound_level
JongChern Nov 13, 2026
fafcd5b
Merge master into DEV-971_event_channel
JongChern Nov 13, 2026
1893a0e
DEV-971: read event files, take live event records, ask the event cha…
JongChern Nov 13, 2026
a8f771c
DEV-971: Event markers window in SensorMapsExample, to bench the Java…
JongChern Nov 13, 2026
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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ public class InstructionsGet{
protected String mDirectoryName;
protected int numBytesToReadFromExpBoard=0;
private static final int MAX_CALIB_DUMP_MAX = 4096;
/* The generated LiteProtocolInstructionSet has no NACK entry, so the value
* is declared here rather than read from InstructionsSet. Adding it to
* grpcprotosrc/src/LiteProtocolInstructionSet.proto and regenerating would
* be tidier, but that rewrites a large generated file committed in two
* places for the sake of one constant. */
private static final int NACK_COMMAND_PROCESSED_VALUE = 0xFE;


/**
Expand Down Expand Up @@ -678,6 +684,12 @@ else if(isKnownGetCommand(mCurrentCommand)){
}

}
/* The Shimmer refused the command. Without this branch the NACK
* was discarded and the transaction stalled until the ACK timer
* expired, which is treated as a lost connection. */
else if((((int)byteBuffer[0])&0xFF)==NACK_COMMAND_PROCESSED_VALUE) {
processNackFromCommand();
}
}
}
}
Expand Down Expand Up @@ -712,8 +724,16 @@ else if(bytesAvailableToBeRead()){
byteBuffer=readBytes(1);
mIamAlive = true;

/* As in the ACK-wait path above, and matching ShimmerBluetooth: a
* NACK can land here instead if the firmware only discovers it
* cannot serve a GET while building the response. Checked before
* isKnownResponseByte() so it is handled as a refusal rather than
* falling through to the ACK/response timeout. */
if((((int)byteBuffer[0])&0xFF)==NACK_COMMAND_PROCESSED_VALUE){
processNackFromCommand();
}
//Check to see whether it is a response byte
if(isKnownResponseByte(byteBuffer[0])){
else if(isKnownResponseByte(byteBuffer[0])){
byte responseCommand = byteBuffer[0];

if(mUseShimmerBluetoothApproach){
Expand Down Expand Up @@ -1367,6 +1387,62 @@ private int availableBytes() throws ShimmerException {
public void eventLogAndStreamStatusChanged(int currentCommand){
mProtocolListener.eventLogAndStreamStatusChangedCallback(currentCommand);
}

/**
* Unwinds the in-flight transaction after the Shimmer answered a command
* with NACK (0xFE) instead of ACK. The refused instruction is dropped so the
* stack advances, and the connection is left up - a refusal means the device
* declined this command, not that the link is gone.
*/
private void processNackFromCommand() {
stopTimerCheckForAckOrResp(); //cancel the ack timer
printLogDataForDebugging("NACK Received for Command: \t\t" + btCommandToString(mCurrentCommand));

int refusedCommand = ((int)mCurrentCommand)&0xFF;

/* Only the ACK-wait state still has the refused instruction queued: a
* GET's instruction is removed as soon as its ACK arrives, so removing
* one here as well would drop whatever was queued behind it. */
boolean instructionStillQueued = mWaitForAck;

mWaitForAck=false;
mWaitForResponse=false;

/* Mirror the ACK path's progress report so that an operation containing a
* refused command can still reach its end value, otherwise the progress
* counter stalls and the operation never completes. Sent before the
* instruction is removed and only while it is still queued, because the
* counter is derived from the remaining stack size. */
if(instructionStillQueued
&& mCurrentCommand!=InstructionsGet.GET_STATUS_COMMAND_VALUE
&& mCurrentCommand!=InstructionsSet.TEST_CONNECTION_COMMAND_VALUE
&& mCurrentCommand!=InstructionsSet.SET_BLINK_LED_VALUE
&& mOperationUnderway){
sendProgressReport(new BluetoothProgressReportPerCmd(mCurrentCommand, getListofInstructions().size(), mMyBluetoothAddress, mComPort));
}

if(instructionStillQueued && getListofInstructions().size()>0){
getListofInstructions().remove(0);
}
getListofInstructions().removeAll(Collections.singleton(null));

mTransactionCompleted=true;
setInstructionStackLock(false);

eventCommandRefused(refusedCommand);
}

/**
* Called when the Shimmer refused a command with a NACK. The transaction has
* already been unwound and the connection is still up. Concrete rather than
* a new ProtocolListener callback so that existing implementers keep
* compiling; override to surface the refusal to the application.
*
* @param command the command value that was refused
*/
protected void eventCommandRefused(int command) {
//Default: no application-level notification, the log line above stands
}

private void isNowStreaming() {
mProtocolListener.isNowStreaming();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
import com.shimmerresearch.sensors.adxl371.SensorADXL371;
import com.shimmerresearch.sensors.lsm6dsv.SensorLSM6DSV;
import com.shimmerresearch.sensors.lis2dw12.SensorLIS2DW12;
import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel;
import com.shimmerresearch.verisense.sensors.SensorLSM6DS3;
//import com.shimmerresearch.verisense.sensors.SensorLIS2DW12;
import com.shimmerresearch.verisense.sensors.SensorBattVoltageVerisense;
Expand Down Expand Up @@ -158,6 +159,8 @@ public static class CHANNEL_UNITS{
public static final String GRAVITY = "g";
public static final String RPM = "rpm";
public static final String POWER = "dB";
/** A-weighted sound pressure level, the Shimmer3R sound level channel's unit (DEV-1413) */
public static final String DECIBELS_A = "dB(A)";
public static final String SCORE = "Score";

public static final String ACCEL_CAL_UNIT = METER_PER_SECOND_SQUARE;
Expand Down Expand Up @@ -735,6 +738,8 @@ public static class SENSOR_ID{
public static final int SHIMMER_LIS3MDL_MAG_ALT = 41;
public static final int SHIMMER_BMP390_PRESSURE = 43;
public static final int SHIMMER_BMP581_PRESSURE = 44;
/** Shimmer3R microphone sound level, channel 0x38 (DEV-1413): enabled through configuration byte 233 bit 4, not a sensor bitmap bit */
public static final int SHIMMER_SOUND_LEVEL = 45;

// public static final int SHIMMER_EXG1_24BIT = 3;
// public static final int SHIMMER_EXG2_24BIT = 4;
Expand Down Expand Up @@ -949,7 +954,8 @@ public enum LABEL_SENSOR_TILE{
MPU_OTHER(SensorMPU9X50.LABEL_SENSOR_TILE.MPU_OTHER),
GPS("GPS"),
STC3100_MONITORING (SensorSTC3100.LABEL_SENSOR_TILE.STC3100_MONITORING),
SWEATCH_ADC ("Sweatch ADC");
SWEATCH_ADC ("Sweatch ADC"),
SOUND_LEVEL(SensorSoundLevel.LABEL_SENSOR_TILE.SOUND_LEVEL);

private String tileText = "";
LABEL_SENSOR_TILE(String text){
Expand Down Expand Up @@ -1337,6 +1343,8 @@ public static class CompatibilityInfoForMaps{
private static final ShimmerVerObject svoShimmer3RImuAnyExpBrdLogAndStream = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,0,1,0,ShimmerVerDetails.ANY_VERSION, ShimmerVerDetails.ANY_VERSION, NEW_IMU_EXP_REV.IMU);

public static final ShimmerVerObject svoShimmer3RLogAndStream = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION);
/** LogAndStream_Shimmer3R v1.01.026 and later: the first release with the sound level channel (DEV-1413) */
public static final ShimmerVerObject svoShimmer3RLogAndStreamSoundLevel = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,1,1,26,ShimmerVerDetails.ANY_VERSION);
public static final ShimmerVerObject svoShimmer3LogAndStreamWithSDLogSyncSupport = new ShimmerVerObject(HW_ID.SHIMMER_3,FW_ID.LOGANDSTREAM,0,16,11,ShimmerVerDetails.ANY_VERSION);
public static final ShimmerVerObject svoShimmer3RLogAndStreamWithSDLogSyncSupport = new ShimmerVerObject(HW_ID.SHIMMER_3R,FW_ID.LOGANDSTREAM,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION,ShimmerVerDetails.ANY_VERSION);

Expand Down Expand Up @@ -1544,6 +1552,13 @@ public static class CompatibilityInfoForMaps{
svoShimmer3RBrAmpUnifiedLogAndStream,
svoShimmer3RProto3DeluxeLogAndStream);

//Shimmer3r sound level (DEV-1413), from LogAndStream_Shimmer3R v1.01.026,
//the first release with the channel. Earlier releases store
//configuration byte 233 untouched, so bit 4 would read back set there
//with no channel behind it: the tile is not offered on them.
public static final List<ShimmerVerObject> listOfCompatibleVersionInfoSoundLevel = Arrays.asList(
svoShimmer3RLogAndStreamSoundLevel);

//TODO need to update this list and remove "any"
public static final List<ShimmerVerObject> listOfCompatibleVersionInfoKionixKXRB52042 = Arrays.asList(
svoAnyIntExpBoardAndSdlog, svoAnyIntExpBoardAndBtStream, svoAnyIntExpBoardAndLogAndStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import com.shimmerresearch.sensors.shimmer2.SensorMMA736x;
import com.shimmerresearch.sensors.shimmer2.SensorShimmer2Gyro;
import com.shimmerresearch.sensors.shimmer2.SensorShimmer2Mag;
import com.shimmerresearch.sensors.soundlevel.SensorSoundLevel;
import com.shimmerresearch.algorithms.orientation.GradDes3DOrientation;
import com.shimmerresearch.algorithms.orientation.GradDes3DOrientation9DoF;
import com.shimmerresearch.algorithms.orientation.Orientation3DObject;
Expand Down Expand Up @@ -505,6 +506,11 @@ public static final class PRESSURE_SENSOR_ID {
public static final byte SET_TEST = (byte) 0xA8;

public static final byte SET_FEATURE = (byte) 0xB7;
/** Event channel status (DEV-971): [0xB9] answered by [ACK][0xBA][status x 9] */
public static final byte GET_EVENT_CHANNEL_STATUS_COMMAND = (byte) 0xB9;
public static final byte EVENT_CHANNEL_STATUS_RESPONSE = (byte) 0xBA;
/** An event channel record, [0x8A][0xBB][len][record][CRC-16], sent only to a host that opted in (DEV-971) */
public static final byte EVENT_RECORD_RESPONSE = (byte) 0xBB;

public static final int MAX_NUMBER_OF_SIGNALS = 77;//50; //used to be 11 but now 13 because of the SR30 + 8 for 3d orientation
public static final int MAX_INQUIRY_PACKET_SIZE = 47;
Expand Down Expand Up @@ -2387,6 +2393,23 @@ else if (isEXGUsingDefaultThreeUnipolarConfiguration()){
calibratedDataUnits[sigIndex]=CHANNEL_UNITS.BEATS_PER_MINUTE;
}

// Sound level (DEV-1413, Shimmer3R). Enabled through configuration
// byte 233 bit 4 rather than a sensor bitmap bit, so it is decoded
// whenever the channel list carries it. 0x8000 (no level) is NaN.
int iSoundLevel = getSignalIndex(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL);
if (iSoundLevel!=-1){
double soundLevelRaw = (double)newPacketInt[iSoundLevel];
objectCluster.addDataToMap(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL,CHANNEL_TYPE.UNCAL.toString(),CHANNEL_UNITS.NO_UNITS,soundLevelRaw);
uncalibratedData[iSoundLevel]=soundLevelRaw;
uncalibratedDataUnits[iSoundLevel]=CHANNEL_UNITS.NO_UNITS;
if (mEnableCalibration){
double soundLevelDbA = SensorSoundLevel.calibrateSoundLevel(soundLevelRaw);
objectCluster.addDataToMap(SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL,CHANNEL_TYPE.CAL.toString(),CHANNEL_UNITS.DECIBELS_A,soundLevelDbA);
calibratedData[iSoundLevel]=soundLevelDbA;
calibratedDataUnits[iSoundLevel]=CHANNEL_UNITS.DECIBELS_A;
}
}

//Additional Channels Offset
int additionalChannelsOffset = calibratedData.length-numAdditionalChannels;
if(fwType == COMMUNICATION_TYPE.BLUETOOTH) {
Expand Down Expand Up @@ -3640,6 +3663,14 @@ else if ((byte)signalId[i-iTS]==(byte)0x28){
enabledSensors |= SENSOR_BRIDGE_AMP;
}
}
else if ((byte)signalId[i-iTS]==SensorSoundLevel.CHANNEL_ID && getHardwareVersion()==HW_ID.SHIMMER_3R){
// Sound level (DEV-1413): a signed 16-bit little-endian count of
// 0.01 dB(A). A Shimmer3 never sends 0x38 and keeps the unknown
// channel handling below.
signalNameArray[i+1]=SensorSoundLevel.ObjectClusterSensorName.SOUND_LEVEL;
signalDataTypeArray[i+1] = "i16";
packetSize=packetSize+2;
}
else{
signalNameArray[i+1]=Byte.toString(signalId[i-iTS]);
signalDataTypeArray[i+1] = "u12";
Expand Down Expand Up @@ -5634,6 +5665,10 @@ else if(isSupportedNewImuSensors()){

mSensorLIS2MDL = new SensorLIS2MDL(this);
addSensorClass(mSensorLIS2MDL);

// Every Shimmer3R has the microphone; whether its firmware has the
// channel is learnt from configuration byte 233 (DEV-1413)
addSensorClass(new SensorSoundLevel(this));
}
}

Expand Down Expand Up @@ -5887,6 +5922,11 @@ else if(sensorId == Configuration.Shimmer3.SENSOR_ID.HOST_SHIMMER_STREAMING_PROP
mSensorMap.get(sensorId).setIsEnabled(true);
return true;
}
// The sound level (DEV-1413) has no sensor bitmap bit, so the bitmap
// would always turn it off: its state comes from configuration byte 233
else if(sensorId == Configuration.Shimmer3.SENSOR_ID.SHIMMER_SOUND_LEVEL){
return true;
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ public class ConfigByteLayoutShimmer3 extends ConfigByteLayout implements Serial
public int maskLIS2MDLMagRateMSB = 0x07;
public int bitShiftLIS2MDLMagRateMSB = 3;

//Config Byte 233, Shimmer3R only (0 where there is none): the microphone.
//Bits 0-1 are the audio mode and bits 2-3 the gain preset (DEV-99), kept as
//read on a configuration write. Bit 4 turns on the sound level channel,
//0x38 (DEV-1413). The firmware clears bits 5-6, and reads the whole byte
//as 0 when bit 7 is set, as it is in erased flash (0xFF).
public int idxAudioConfig = 0;
public int maskAudioModeAndGain = 0x0F;
public int bitShiftSoundLevel = 4;
public int maskSoundLevel = 0x01;
public int maskAudioConfigInvalid = 0x80;

// Derived Channels - used by SW not FW
public int maskDerivedChannelsByte = 0xFF;
public int byteShiftDerivedSensors0 = 8*0;
Expand Down Expand Up @@ -397,6 +408,12 @@ public ConfigByteLayoutShimmer3(int firmwareIdentifier, int firmwareVersionMajor
maskShowErrorLedsSd = 0x01;
}

// Any Shimmer3R firmware: older builds store the byte untouched, so
// writing back what was read is safe on them too
if(mShimmerVerObject.mHardwareVersion == HW_ID.SHIMMER_3R) {
idxAudioConfig = 128+105;
}

if(mShimmerVerObject.mHardwareVersion == HW_ID.SHIMMER_3R
|| compareVersions(FW_ID.LOGANDSTREAM,0,8,1)) {
idxBtFactoryReset = 128+103;
Expand Down Expand Up @@ -547,6 +564,9 @@ public HashMap<Integer, String> getMapOfByteDescriptions(){
mapOfByteDescriptions.put(idxMaxExpLengthLsb, "idxMaxExpLengthLsb");
mapOfByteDescriptions.put(idxMacAddress, "idxMacAddress");
mapOfByteDescriptions.put(idxSDConfigDelayFlag, "idxSDConfigDelayFlag");
if(idxAudioConfig>0){
mapOfByteDescriptions.put(idxAudioConfig, "idxAudioConfig");
}

if(idxBtFactoryReset>0){
mapOfByteDescriptions.put(idxBtFactoryReset, "idxBtFactoryReset");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.shimmerresearch.driver.shimmer2r3;

import java.io.Serializable;

/**
* The answer to GET_EVENT_CHANNEL_STATUS_COMMAND (0xB9), which is
* {@code [ACK][0xBA][status x 9]} (DEV-971; log-and-stream-common
* {@code docs/SHIMMER3_BT_COMMUNICATION_PROTOCOL.md} §7.15).
*
* <pre>
* 0 inputs the configuration enables: bit 0 button markers, bit 1 TTL
* 1 bit 0 the event stream is on for this connection, bit 1 an event file
* is open, bit 2 the event file was stopped by an error, bit 3 the TTL
* input is armed, bit 4 the TTL input is off for a second after a flood
* 2 each input's state now: bit 0 button, bit 1 TTL
* 3..4 the link's next sequence number
* 5..6 edges lost this session, the event file's dropped count
* 7..8 records the link lost since the opt-in, because its queue was full
* </pre>
*/
public class EventChannelStatus implements Serializable {

private static final long serialVersionUID = 1L;

/** Length of the status after the 0xBA opcode */
public static final int LENGTH = 9;

private final int mInputsEnabled;
private final int mFlags;
private final int mState;
private final int mNextSequenceNumber;
private final int mSessionDropped;
private final int mLinkLost;

private EventChannelStatus(byte[] b, int at) {
mInputsEnabled = b[at]&0xFF;
mFlags = b[at+1]&0xFF;
mState = b[at+2]&0xFF;
mNextSequenceNumber = EventRecord.uint16(b, at+3);
mSessionDropped = EventRecord.uint16(b, at+5);
mLinkLost = EventRecord.uint16(b, at+7);
}

/**
* Decodes the 9 status bytes at {@code bytes[offset]}.
*
* @throws IllegalArgumentException when fewer than 9 remain
*/
public static EventChannelStatus parse(byte[] bytes, int offset) {
if(bytes==null || offset<0 || bytes.length<offset+LENGTH) {
throw new IllegalArgumentException("Event channel status needs 9 bytes.");
}
return new EventChannelStatus(bytes, offset);
}

/** {@link EventRecord#INPUT_BUTTON}, {@link EventRecord#INPUT_TTL} */
public int getInputsEnabled() {
return mInputsEnabled;
}

/** The stream is on for this connection, as asked; the firmware applies it moments later */
public boolean isStreaming() {
return (mFlags&0x01)!=0;
}

public boolean isFileOpen() {
return (mFlags&0x02)!=0;
}

/** The event file was stopped by an error, this session or the last */
public boolean isFileError() {
return (mFlags&0x04)!=0;
}

public boolean isTtlArmed() {
return (mFlags&0x08)!=0;
}

/** The TTL input is switched off for a second after a flood */
public boolean isTtlThrottled() {
return (mFlags&0x10)!=0;
}

/** Each input's state now: {@link EventRecord#INPUT_BUTTON}, {@link EventRecord#INPUT_TTL} */
public int getState() {
return mState;
}

public int getNextSequenceNumber() {
return mNextSequenceNumber;
}

public int getSessionDropped() {
return mSessionDropped;
}

public int getLinkLost() {
return mLinkLost;
}

@Override
public String toString() {
return "EventChannelStatus[inputs=" + mInputsEnabled
+ ", streaming=" + isStreaming() + ", fileOpen=" + isFileOpen() + ", fileError=" + isFileError()
+ ", ttlArmed=" + isTtlArmed() + ", ttlThrottled=" + isTtlThrottled()
+ ", state=" + mState + ", nextSeq=" + mNextSequenceNumber
+ ", sessionDropped=" + mSessionDropped + ", linkLost=" + mLinkLost + "]";
}

}
Loading