From fd5a1c5b4bccaf5354b86283eedfe9255409841f Mon Sep 17 00:00:00 2001 From: nic Date: Wed, 11 May 2011 11:34:47 +0200 Subject: [PATCH] - integrating comments from code review proposals http://code.google.com/r/sandordornbush-patches/source/detail?r=3dfdbcf78de082edb66a8994ffb604c84945df0d --- .../services/sensors/SensorUtils.java | 24 +++++++++++-------- .../services/sensors/ZephyrMessageParser.java | 19 ++++++++++----- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java index 45c2364a9..7424b727c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/SensorUtils.java @@ -30,7 +30,7 @@ public class SensorUtils { private SensorUtils() { } - + /** * Extract one unsigned short from a big endian byte array. * @@ -66,10 +66,10 @@ public class SensorUtils { * @param end the position in the byte array where the payload ends * @return CRC8 value */ - public static int getCrc8(byte[] buffer, int start, int end) { - int crc = 0x0; + public static byte getCrc8(byte[] buffer, int start, int length) { + byte crc = 0x0; - for (int i = start; i < (end + 1); i++) { + for (int i = start; i < (start + length); i++) { crc = crc8PushByte(crc, buffer[i]); } return crc; @@ -81,14 +81,18 @@ public class SensorUtils { * @param crc int of crc value * @param add the next byte to add to the CRC8 calculation */ - private static int crc8PushByte(int crc, byte add) { - int addInt = (add & 0x000000FF); - crc = crc ^ addInt; + private static byte crc8PushByte(byte crc, byte add) { + crc = (byte) (crc ^ add); + for (int i = 0; i < 8; i++) { - if ((crc & 0x00000001) != 0x00000000) { - crc = (crc >> 1) ^ 0x0000008C; + if ((crc & 0x1) != 0x0) { + // we have to do the OxFF mask because the right shift is + // supposed to shift in a 0 + // as crc gets implicitly casted to a + // signed int by >> it may be filled with 1s + crc = (byte) (((crc & 0xFF) >> 1) ^ 0x8C); } else { - crc = (crc >> 1); + crc = (byte) ((crc & 0xFF) >> 1); } } return crc; diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java index 0808c73ca..1b86e6822 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/sensors/ZephyrMessageParser.java @@ -27,6 +27,10 @@ import com.google.android.apps.mytracks.content.Sensor; */ public class ZephyrMessageParser implements MessageParser { + public static final int ZEPHYR_HXM_BYTE_STX = 0; + public static final int ZEPHYR_HXM_BYTE_CRC = 58; + public static final int ZEPHYR_HXM_BYTE_ETX = 59; + @Override public Sensor.SensorDataSet parseBuffer(byte[] buffer) { StringBuilder sb = new StringBuilder(); @@ -36,11 +40,11 @@ public class ZephyrMessageParser implements MessageParser { Log.w(Constants.TAG, "Got zephyr data: " + sb); // Heart Rate Sensor.SensorData.Builder heartrate = Sensor.SensorData.newBuilder() - .setValue(buffer[12] & 0xFF) + .setValue(buffer[12]) .setState(Sensor.SensorState.SENDING); // Changes Nico Laum (Power and Cadence) Sensor.SensorData.Builder power = Sensor.SensorData.newBuilder() - .setValue(buffer[11] & 0xFF) + .setValue(buffer[11]) .setState(Sensor.SensorState.SENDING); Sensor.SensorData.Builder cadence = Sensor.SensorData.newBuilder() .setValue(SensorUtils.unsignedShortToIntLittleEndian(buffer, 56) / 16) @@ -59,10 +63,13 @@ public class ZephyrMessageParser implements MessageParser { @Override public boolean isValid(byte[] buffer) { - // Check STX, ETX and CRC Checksum - return buffer[0] == 0x02 - && buffer[59] == 0x03 - && SensorUtils.getCrc8(buffer, 3, 57) == (int) (buffer[58] & 0xFF); + if (buffer.length > ZEPHYR_HXM_BYTE_ETX) { + // Check STX (Start of Text), ETX (End of Text) and CRC Checksum + return buffer[ZEPHYR_HXM_BYTE_STX] == 0x02 + && buffer[ZEPHYR_HXM_BYTE_ETX] == 0x03 + && SensorUtils.getCrc8(buffer, 3, 55) == buffer[ZEPHYR_HXM_BYTE_CRC]; + } else + return false; } @Override