diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java index 18bfc999c..7a7a47e10 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/bluetooth/ShimmerBluetooth.java @@ -1616,27 +1616,8 @@ else if(responseCommand==BMP280_CALIBRATION_COEFFICIENTS_RESPONSE){ printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); } } else if(responseCommand==PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE){ - byte[] pressureResoRes = null; - byte[] filteredByteArray = null; - if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP390)){ - pressureResoRes = new byte[23]; //21 bytes + 2 - pressureResoRes = readBytes(23, responseCommand); - printLogDataForDebugging("BMP390 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - }else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP280)){ - pressureResoRes = new byte[26]; //24 bytes + 2 - pressureResoRes = readBytes(26, responseCommand); - printLogDataForDebugging("BMP280 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - }else if(mSensorBMPX80.mSensorType.equals(SENSORS.BMP180)){ - pressureResoRes = new byte[24]; //22 bytes + 2 - pressureResoRes = readBytes(24, responseCommand); - printLogDataForDebugging("BMP180 CALIB Received:\t" + UtilShimmer.bytesToHexStringWithSpacesFormatted(pressureResoRes)); - } - - if(pressureResoRes!=null){ - filteredByteArray = Arrays.copyOfRange(pressureResoRes, 2, pressureResoRes.length); - retrievePressureCalibrationParametersFromPacket(filteredByteArray,CALIB_READ_SOURCE.LEGACY_BT_COMMAND); - } - } + processPressureCalibCoefficientsResponse(); + } else if(responseCommand==EXG_REGS_RESPONSE){ delayForBtResponse(300); // Wait to ensure the packet has been fully received byte[] bufferAns = readBytes(11, responseCommand); @@ -3341,14 +3322,95 @@ public void readPressureCalibrationCoefficients() { } } } else if (getHardwareVersion() == HW_ID.SHIMMER_3R) { - // BMP581 self-compensates and has no coefficients; its firmware NACKs - // GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND, so don't send it. - if (!isSupportedBmp581()) { + // LogAndStream_Shimmer3R v1.01.007+ answers with the fitted sensor's ID + // ([A6][01][03] for a BMP581), which confirms or overrides the + // SR-number rule. v1.01.006 is the one version that NACKs it on a + // BMP581, and without NACK handling an unanswered command times out + // and drops the connection - so skip it there and let the SR-number + // rule stand. The rule's firmware guard (>= v1.01.006) makes "rule + // says BMP581 and firmware < v1.01.007" exactly v1.01.006. + boolean isBmp581PerSrNumber = isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails()); + boolean isFwWithInBandPressureSensorId = getShimmerVerObject().compareVersions(FW_ID.LOGANDSTREAM, 1, 1, 7); + if (!isBmp581PerSrNumber || isFwWithInBandPressureSensorId) { writeInstruction(GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND); } } } + /** + * Parses a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, laid out as + * [0xA6][len = 1+n][sensorId][n coefficient bytes] (log-and-stream-common + * Comms/shimmer_bt_uart.c). The response byte has already been read. + *

+ * The length byte, not the sensor the driver assumed, decides how much is + * read, so the stream stays in step whatever the Shimmer sends. The length + * is then checked against the sensor ID, and a mismatch is rejected rather + * than applied. On a Shimmer3R the sensor ID is authoritative: it overrides + * the SR-number rule in either direction (see + * {@link #setPressureSensorIdInBand(int)}). On a Shimmer3 the coefficients + * are applied only if they are for the sensor class in use. + */ + protected void processPressureCalibCoefficientsResponse() { + byte[] length = readBytes(1, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE); + if(length==null){ + return; + } + int lengthToRead = length[0]&0xFF; + if(lengthToRead==0){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: zero length, no sensor ID"); + return; + } + byte[] payload = readBytes(lengthToRead, PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE); + if(payload==null){ + return; + } + + int sensorId = payload[0]&0xFF; + byte[] coefficients = Arrays.copyOfRange(payload, 1, payload.length); + String bytesReceived = UtilShimmer.bytesToHexStringWithSpacesFormatted(ArrayUtils.addAll(length, payload)); + + int expectedLength = getPressureCalibCoefficientByteLength(sensorId); + if(expectedLength<0 || coefficients.length!=expectedLength){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: sensor ID " + sensorId + " with " + coefficients.length + + " coefficient bytes, expected " + expectedLength + ":\t" + bytesReceived); + return; + } + + SENSORS sensorReported = null; + switch(sensorId){ + case PRESSURE_SENSOR_ID.BMP180: + sensorReported = SENSORS.BMP180; + break; + case PRESSURE_SENSOR_ID.BMP280: + sensorReported = SENSORS.BMP280; + break; + case PRESSURE_SENSOR_ID.BMP390: + sensorReported = SENSORS.BMP390; + break; + case PRESSURE_SENSOR_ID.BMP581: + sensorReported = SENSORS.BMP581; + break; + } + printLogDataForDebugging(sensorReported + " CALIB Received:\t" + bytesReceived); + + if(getHardwareVersion()==HW_ID.SHIMMER_3R){ + if(sensorId!=PRESSURE_SENSOR_ID.BMP390 && sensorId!=PRESSURE_SENSOR_ID.BMP581){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: a Shimmer3R carries a BMP390 or BMP581, not a " + sensorReported); + return; + } + setPressureSensorIdInBand(sensorId); + } + else if(!mSensorBMPX80.mSensorType.equals(sensorReported)){ + printLogDataForDebugging("PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE rejected: coefficients are for a " + sensorReported + " but the driver is using a " + mSensorBMPX80.mSensorType); + return; + } + + // A BMP581 self-compensates, so it has no coefficients to apply + if(coefficients.length>0){ + retrievePressureCalibrationParametersFromPacket(coefficients,CALIB_READ_SOURCE.LEGACY_BT_COMMAND); + } + } + public void readBattery(){ writeInstruction(GET_VBATT_COMMAND); } diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java index 4056fe9fb..4f153f927 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/driver/ShimmerObject.java @@ -473,7 +473,17 @@ public class BTStream { public static final byte GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND = (byte) 0xA7;//BMP390 public static final byte PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE = (byte) 0xA6;//BMP390 - + + /** The sensor ID byte in a PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, which + * is laid out as [0xA6][len = 1+n][sensorId][n coefficient bytes] + * (log-and-stream-common Comms/shimmer_bt_uart.c). */ + public static final class PRESSURE_SENSOR_ID { + public static final int BMP180 = 0; + public static final int BMP280 = 1; + public static final int BMP390 = 2; + public static final int BMP581 = 3; + } + public static final byte SET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x52; public static final byte PRESSURE_OVERSAMPLING_RATIO_RESPONSE = (byte) 0x53; public static final byte GET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x54; @@ -680,7 +690,13 @@ public String toString() { protected SensorMPU9X50 mSensorMpu9x50 = new SensorMPU9150(this); // Shimmer3 - Pressure/Temperature public SensorBMPX80 mSensorBMPX80 = new SensorBMP180(this); - + /** The pressure sensor a Shimmer3R reported in-band in its + * PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE (a {@link PRESSURE_SENSOR_ID}), + * or null if it hasn't. When set it overrides the SR-number rule in + * {@link #isSupportedBmp581()}. Cleared whenever the expansion board details + * change, so each connection starts from the SR-number rule again. */ + protected Integer mPressureSensorIdInBand = null; + // Shimmer3r - Mag private SensorLIS2MDL mSensorLIS2MDL = new SensorLIS2MDL(this); // Shimmer3r - Alt Mag @@ -10822,31 +10838,50 @@ public static boolean isSupportedBmp280(ShimmerVerObject svo, ExpansionBoardDeta * SR number/revision and the firmware version that produces the new output * format. * - * @return true if the connected Shimmer3R board revision and firmware version indicate BMP581 output support + *

+ * If the Shimmer has reported its pressure sensor in-band (the sensor ID in + * its PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, LogAndStream_Shimmer3R + * v1.01.007 onwards), that answer is used and overrides the SR-number rule + * in either direction. Otherwise, e.g. when the firmware NACKs the command + * or is too old to have it, the SR-number rule decides. + * + * @return true if the connected Shimmer3R reported a BMP581, or if its board revision and firmware version indicate BMP581 output support */ public boolean isSupportedBmp581() { + if(mPressureSensorIdInBand!=null && getHardwareVersion()==HW_ID.SHIMMER_3R){ + return mPressureSensorIdInBand==PRESSURE_SENSOR_ID.BMP581; + } return isSupportedBmp581(getShimmerVerObject(), getExpansionBoardDetails()); } + /** The SR-number rule alone, used when the Shimmer hasn't identified its + * pressure sensor in-band and for SD log files (whose header carries no + * sensor ID). It mirrors the firmware's own fallback, + * ShimBrd_isBmp581PresentPerSrNumber() in log-and-stream-common + * Boards/shimmer_boards.c, plus a firmware-version guard. + * + * @param svo the Shimmer's version details + * @param ebd the Shimmer's expansion board details + * @return true if a Shimmer3R with this board and firmware carries a BMP581 + */ public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDetails ebd) { if(svo==null || ebd==null || svo.getHardwareVersion()!=HW_ID.SHIMMER_3R){ return false; } - // SR48 (GSR+): BMP581 spans two bands - 7.2..7.x AND 8.2 and above. - // 7.0/7.1 and 8.0/8.1 are BMP390, so a single lexicographic ">=" can't - // express it (8.0/8.1 fall between the two BMP581 bands). - boolean sr48Bmp581 = - (ebd.getExpansionBoardId()==HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED - && ebd.getExpansionBoardRev()==7 - && ebd.getExpansionBoardRevSpecial()>=2) // SR48 7.2..7.x - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2); // SR48 8.2 and above - + // Fitted models (">=" applies to both rev fields): SR31-11-2, SR38-4-2, + // SR47-8-2, SR48-7-2, SR48-8-2, SR49-4-2. SR48 (GSR+) has two BMP581 + // bands, 7.2..7.x and 8.2 onwards: 8.0/8.1 went back to the BMP390, so + // the rev-7 band is ">= 7.2 and not >= 8.0". An unprogrammed card (board + // ID 0x00 or 0xFF) matches none of these IDs. boolean boardEligible = - ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2 - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2 - || sr48Bmp581 // SR48 7.2..7.x and 8.2+ - || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2 + ebd.isSrNumberGte(HW_ID_SR_CODES.SHIMMER3, 11, 2) // SR31 >= 11.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2) // SR38 >= 4.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2) // SR47 >= 8.2 + || (ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 2) + && !ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 0)) // SR48 7.2..7.x + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2) // SR48 >= 8.2 + || ebd.isSrNumberGte(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2); // SR49 >= 4.2 // Format guard: BMP581 (pre-compensated) output only exists from // LogAndStream_Shimmer3R v1.01.006 onwards. @@ -10855,6 +10890,70 @@ public static boolean isSupportedBmp581(ShimmerVerObject svo, ExpansionBoardDeta return boardEligible && fwEligible; } + /** Records the pressure sensor a Shimmer3R reported in-band and, if the + * sensor class in use is not the one it names, rebuilds the sensor classes + * around the reported sensor. The rebuild is what would have happened had + * the sensor been known when the daughter card ID arrived: the config bytes, + * which the Shimmer sends before this response while connecting, are parsed + * again so the new sensor class picks up its settings (e.g. oversampling). + * + * @param pressureSensorId the sensor ID from the response, a {@link PRESSURE_SENSOR_ID} + */ + public void setPressureSensorIdInBand(int pressureSensorId) { + mPressureSensorIdInBand = pressureSensorId; + + if(isShimmerGen3R()){ + boolean bmp581InUse = mSensorBMPX80.mSensorType==SENSORS.BMP581; + if(bmp581InUse!=isSupportedBmp581()){ + consolePrintLn("Pressure sensor reported in-band (ID " + pressureSensorId + ") differs from the SR-number rule, switching to " + (isSupportedBmp581()? "BMP581":"BMP390")); + sensorAndConfigMapsCreate(); + if(mShimmerUsingConfigFromInfoMem && ConfigByteLayout.checkConfigBytesValid(mConfigBytes)){ + configBytesParse(mConfigBytes, COMMUNICATION_TYPE.BLUETOOTH); + } + } + } + } + + /** + * @return the pressure sensor the Shimmer reported in-band (a {@link PRESSURE_SENSOR_ID}), or null if it hasn't + */ + public Integer getPressureSensorIdInBand() { + return mPressureSensorIdInBand; + } + + /** + * @param pressureSensorId a {@link PRESSURE_SENSOR_ID} + * @return the number of coefficient bytes that follow this sensor ID in a + * PRESSURE_CALIBRATION_COEFFICIENTS_RESPONSE, or -1 if the ID is unknown + */ + public static int getPressureCalibCoefficientByteLength(int pressureSensorId) { + switch(pressureSensorId){ + case PRESSURE_SENSOR_ID.BMP180: + return 22; + case PRESSURE_SENSOR_ID.BMP280: + return 24; + case PRESSURE_SENSOR_ID.BMP390: + return 21; + case PRESSURE_SENSOR_ID.BMP581: + return 0; // self-compensating, no coefficients + default: + return -1; + } + } + + @Override + public void setExpansionBoardDetails(ExpansionBoardDetails eBD){ + super.setExpansionBoardDetails(eBD); + // A different board may carry a different pressure sensor + mPressureSensorIdInBand = null; + } + + @Override + public void clearExpansionBoardDetails(){ + super.clearExpansionBoardDetails(); + mPressureSensorIdInBand = null; + } + /** Returns true if the Shimmer is using new sensors. These sensors are: *

  • Use BMP280 instead of BMP180 as barometer. *
  • Use MPU9250 instead of MPU9150 as IMU. diff --git a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/bmpX80/SensorBMP581.java b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/bmpX80/SensorBMP581.java index fb5abfcfc..ff50d5b80 100644 --- a/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/bmpX80/SensorBMP581.java +++ b/ShimmerDriver/src/main/java/com/shimmerresearch/sensors/bmpX80/SensorBMP581.java @@ -81,8 +81,12 @@ public SensorBMP581(ShimmerDevice shimmerDevice) { //--------- Constructors for this class end -------------- //--------- Bluetooth commands start -------------- - // Oversampling only. There is deliberately NO GET_PRESSURE_CALIBRATION_COEFFICIENTS - // command - the BMP581 firmware NACKs it because the sensor self-compensates. + // Oversampling only. The BMP581 self-compensates, so it has no calibration + // coefficients. GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND (0xA7) is still + // sent: LogAndStream_Shimmer3R v1.01.007+ answers it with just the sensor ID, + // [A6][01][03], which identifies the BMP581 in-band. Only v1.01.006 NACKs it on + // a BMP581, so the driver doesn't send it to that version (see + // ShimmerBluetooth.readPressureCalibrationCoefficients()). public static final byte SET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x52; public static final byte PRESSURE_OVERSAMPLING_RATIO_RESPONSE = (byte) 0x53; public static final byte GET_PRESSURE_OVERSAMPLING_RATIO_COMMAND = (byte) 0x54; diff --git a/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00020_Bmp581SrRuleTest.java b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00020_Bmp581SrRuleTest.java new file mode 100644 index 000000000..12080799c --- /dev/null +++ b/ShimmerDriver/src/test/java/com/shimmerresearch/driver/API_00020_Bmp581SrRuleTest.java @@ -0,0 +1,122 @@ +package com.shimmerresearch.driver; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.shimmerresearch.driverUtilities.ExpansionBoardDetails; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID_SR_CODES; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; + +/** + * The SR-number rule that decides whether a Shimmer3R carries a BMP581 when + * the Shimmer hasn't said so in-band (0xA7 NACKed or unanswered, and every SD + * log file). It has to match the firmware's own rule, + * ShimBrd_isBmp581PresentPerSrNumber() in log-and-stream-common + * Boards/shimmer_boards.c, so the cases below are copied from that repo's + * Test/host/test_boards.c test_bmp581_gate(). Before DEV-1110 the driver's rule + * had no SR38 (Proto3 Deluxe) entry. + * + * @author Mark Nolan + */ +public class API_00020_Bmp581SrRuleTest { + + private static final ShimmerVerObject S3R_FW_1_1_6 = new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 6); + + private static boolean rule(ShimmerVerObject svo, int srId, int rev, int revSpecial) { + return ShimmerObject.isSupportedBmp581(svo, new ExpansionBoardDetails(srId, rev, revSpecial)); + } + + private static void check(int srId, int rev, int revSpecial, boolean expected, String why) { + assertEquals("SR" + srId + "-" + rev + "-" + revSpecial + ": " + why, expected, rule(S3R_FW_1_1_6, srId, rev, revSpecial)); + } + + @Test + public void imuSr31FromEleven2() { + check(HW_ID_SR_CODES.SHIMMER3, 11, 1, false, "one minor below the line"); + check(HW_ID_SR_CODES.SHIMMER3, 11, 2, true, "the first board with it"); + check(HW_ID_SR_CODES.SHIMMER3, 11, 3, true, "a later minor keeps it"); + check(HW_ID_SR_CODES.SHIMMER3, 12, 0, true, "a later major keeps it"); + check(HW_ID_SR_CODES.SHIMMER3, 10, 9, false, "an earlier major never has it"); + } + + @Test + public void proto3DeluxeSr38FromFour2() { + check(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 1, false, "one minor below the line"); + check(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2, true, "the first board with it"); + check(HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 5, 0, true, "a later major keeps it"); + } + + @Test + public void exgSr47FromEight2() { + check(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 7, 2, false, "SR47 has no 7-2 dev build"); + check(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 1, false, "one minor below the line"); + check(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 8, 2, true, "the first board with it"); + check(HW_ID_SR_CODES.EXP_BRD_EXG_UNIFIED, 9, 0, true, "a later major keeps it"); + } + + @Test + public void bridgeAmpSr49FromFour2() { + check(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 1, false, "one minor below the line"); + check(HW_ID_SR_CODES.EXP_BRD_BR_AMP_UNIFIED, 4, 2, true, "the first board with it"); + } + + @Test + public void gsrSr48HasTwoWindows() { + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 6, 0, false, "the earliest proto"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 0, false, "below the dev build"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 1, false, "the BOOT0 ECO rev, still no BMP581"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 2, true, "the dev build that carries it"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 7, 3, true, "above the dev build, same major"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 0, false, "LATER board, but back to the BMP390"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 1, false, "still the BMP390"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2, true, "production line picks it up again"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 3, true, "and keeps it"); + check(HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 9, 0, true, "a later major keeps it"); + } + + @Test + public void boardWithNoRule() { + check(HW_ID_SR_CODES.EXP_BRD_PROTO3_MINI, 9, 9, false, "no rule for this board ID"); + } + + @Test + public void shimmer3HostNeverMatches() { + // A daughter card can be moved between hosts: the same EEPROM bytes on a + // Shimmer3 must not claim a BMP581. + ShimmerVerObject s3 = new ShimmerVerObject(HW_ID.SHIMMER_3, FW_ID.LOGANDSTREAM, 1, 1, 6); + assertFalse(rule(s3, HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2)); + assertFalse(rule(s3, HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2)); + assertFalse(rule(s3, HW_ID_SR_CODES.SHIMMER3, 11, 2)); + } + + @Test + public void unprogrammedCardNeverMatches() { + // 0xFF,0xFF,0xFF would satisfy every ">=" if the board ID weren't matched first + check(0xFF, 0xFF, 0xFF, false, "an unprogrammed card does not claim a BMP581"); + check(0x00, 0x00, 0x00, false, "nor does an empty one"); + } + + @Test + public void firmwareGuard() { + // Not in the firmware's test (the firmware knows its own version): the + // pre-compensated output only exists from LogAndStream_Shimmer3R v1.01.006. + assertFalse(rule(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 5), HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2)); + assertTrue(rule(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 6), HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2)); + assertTrue(rule(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 1, 7), HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2)); + assertTrue(rule(new ShimmerVerObject(HW_ID.SHIMMER_3R, FW_ID.LOGANDSTREAM, 1, 2, 0), HW_ID_SR_CODES.EXP_BRD_PROTO3_DELUXE, 4, 2)); + assertFalse(rule(null, HW_ID_SR_CODES.EXP_BRD_GSR_UNIFIED, 8, 2)); + assertFalse(ShimmerObject.isSupportedBmp581(S3R_FW_1_1_6, null)); + } + + @Test + public void coefficientLengthPerSensorId() { + assertEquals(22, ShimmerObject.getPressureCalibCoefficientByteLength(ShimmerObject.PRESSURE_SENSOR_ID.BMP180)); + assertEquals(24, ShimmerObject.getPressureCalibCoefficientByteLength(ShimmerObject.PRESSURE_SENSOR_ID.BMP280)); + assertEquals(21, ShimmerObject.getPressureCalibCoefficientByteLength(ShimmerObject.PRESSURE_SENSOR_ID.BMP390)); + assertEquals(0, ShimmerObject.getPressureCalibCoefficientByteLength(ShimmerObject.PRESSURE_SENSOR_ID.BMP581)); + assertEquals(-1, ShimmerObject.getPressureCalibCoefficientByteLength(4)); + } +} diff --git a/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00021_Bmp581InBandSensorIdTest.java b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00021_Bmp581InBandSensorIdTest.java new file mode 100644 index 000000000..d98b2dcaf --- /dev/null +++ b/ShimmerDriverPC/src/test/java/com/shimmerresearch/bluetooth/API_00021_Bmp581InBandSensorIdTest.java @@ -0,0 +1,276 @@ +package com.shimmerresearch.bluetooth; + +import static org.junit.Assert.*; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Deque; + +import org.junit.Test; + +import com.shimmerresearch.driver.ShimmerObject; +import com.shimmerresearch.driver.calibration.CalibDetails.CALIB_READ_SOURCE; +import com.shimmerresearch.driverUtilities.ExpansionBoardDetails; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.FW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID; +import com.shimmerresearch.driverUtilities.ShimmerVerDetails.HW_ID_SR_CODES; +import com.shimmerresearch.driverUtilities.ShimmerVerObject; +import com.shimmerresearch.pcDriver.ShimmerPC; +import com.shimmerresearch.sensors.AbstractSensor.SENSORS; +import com.shimmerresearch.sensors.bmpX80.SensorBMP390; +import com.shimmerresearch.sensors.bmpX80.SensorBMPX80; +import com.shimmerresearch.shimmer3.communication.ByteCommunication; +import com.shimmerresearch.verisense.communication.ByteCommunicationListener; + +import jssc.SerialPortTimeoutException; + +/** + * DEV-1110: the Shimmer3R's answer to GET_PRESSURE_CALIBRATION_COEFFICIENTS_COMMAND + * (0xA7) names the fitted pressure sensor, [0xA6][len = 1+n][sensorId][n bytes], + * and that answer overrides the SR-number rule in either direction. Before + * DEV-1110 the driver read a fixed length chosen from the sensor it had assumed, + * so a BMP581's 3-byte answer was read as a 23-byte BMP390 one, and it never + * sent 0xA7 to a board the SR-number rule marked as a BMP581. + *

    + * No hardware: the response handler reads from an in-memory byte queue. It lives + * in this package because the handler and the instruction stack are protected + * members of ShimmerBluetooth. + * + * @author Mark Nolan + */ +public class API_00021_Bmp581InBandSensorIdTest { + + /** Coefficients of a real BMP390 (ByteCommunicationSimulatorS3R.getPressureResoTest()) */ + private static final byte[] BMP390_COEFFICIENTS = { + (byte) 0xE7, (byte) 0x6B, (byte) 0xF0, (byte) 0x4A, (byte) 0xF9, + (byte) 0xAB, (byte) 0x1C, (byte) 0x9B, (byte) 0x15, (byte) 0x06, + (byte) 0x01, (byte) 0xD2, (byte) 0x49, (byte) 0x18, (byte) 0x5F, + (byte) 0x03, (byte) 0xFA, (byte) 0x3A, (byte) 0x0F, (byte) 0x07, + (byte) 0xF5 + }; + + /** Bytes following the 0xA6 response byte */ + private static byte[] response(int sensorId, byte[] coefficients) { + byte[] bytes = new byte[2 + coefficients.length]; + bytes[0] = (byte) (1 + coefficients.length); + bytes[1] = (byte) sensorId; + System.arraycopy(coefficients, 0, bytes, 2, coefficients.length); + return bytes; + } + + private static class QueueRadio implements ByteCommunication { + final Deque rx = new ArrayDeque(); + + void queue(byte... bytes) { + for(byte b:bytes){ + rx.add(b); + } + } + + @Override + public byte[] readBytes(int byteCount, int timeout) throws SerialPortTimeoutException { + if(rx.size()