Skip to content

Commit

Permalink
Merge pull request #60 from nightscout/hotfix/v3calcrecs
Browse files Browse the repository at this point in the history
Hotfix to parse v3 Cal records
  • Loading branch information
jasoncalabrese committed Nov 13, 2014
2 parents 7763114 + 6ead47d commit 675b687
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nightscout.android"
android:versionCode="15"
android:versionName="0.1.9">
android:versionCode="16"
android:versionName="0.1.10">

<uses-feature android:name="android.hardware.usb.host" />

Expand Down
@@ -1,7 +1,7 @@
package com.nightscout.android.dexcom;

public class CRCFailRuntimeException extends RuntimeException {
CRCFailRuntimeException(String message){
public CRCFailRuntimeException(String message){
super(message);
}
}
Expand Up @@ -50,6 +50,7 @@ public class Constants {
public final static int EGV_TREND_ARROW_MASK = 15;
public final static int EGV_NOISE_MASK = 112;
public final static float MG_DL_TO_MMOL_L = 0.05556f;
public final static int CRC_LEN = 2;

public enum BATTERY_STATES {
NONE,
Expand Down
Expand Up @@ -6,6 +6,7 @@
import com.nightscout.android.dexcom.records.EGVRecord;
import com.nightscout.android.dexcom.records.GenericXMLRecord;
import com.nightscout.android.dexcom.records.MeterRecord;
import com.nightscout.android.dexcom.records.PageHeader;
import com.nightscout.android.dexcom.records.SensorRecord;

import org.w3c.dom.Element;
Expand Down Expand Up @@ -211,6 +212,7 @@ private ReadPacket read(int numOfBytes) {

private <T> T ParsePage(byte[] data, int recordType) {
int HEADER_LEN = 28;
PageHeader pageHeader=new PageHeader(data);
int NUM_REC_OFFSET = 4;
int numRec = data[NUM_REC_OFFSET];
int rec_len;
Expand Down Expand Up @@ -244,7 +246,10 @@ private <T> T ParsePage(byte[] data, int recordType) {
}
return (T) meterRecords;
case CAL_SET:
rec_len = 148;
rec_len = 249;
if (pageHeader.getRevision()<=2) {
rec_len = 148;
}
CalRecord[] calRecords = new CalRecord[numRec];
for (int i = 0; i < numRec; i++) {
int startIdx = HEADER_LEN + rec_len * i;
Expand Down
@@ -1,19 +1,21 @@
package com.nightscout.android.dexcom.records;

import android.util.Log;

import com.nightscout.android.TimeConstants;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class CalRecord extends GenericTimestampRecord {

private static final String TAG = CalRecord.class.getSimpleName();
private double slope;
private double intercept;
private double scale;
private int[] unk = new int[3];
private double decay;
private int numRecords;
private CalSubrecord[] calSubrecords = new CalSubrecord[6];
private CalSubrecord[] calSubrecords = new CalSubrecord[12];
private int SUB_LEN = 17;

public CalRecord(byte[] packet) {
Expand All @@ -29,6 +31,7 @@ public CalRecord(byte[] packet) {
long displayTimeOffset = (getDisplayTime().getTime() - getSystemTime().getTime()) / TimeConstants.SEC_TO_MS;
int start = 44;
for (int i = 0; i < numRecords; i++) {
Log.d("CalDebug","Loop #"+i);
byte[] temp = new byte[SUB_LEN];
System.arraycopy(packet, start, temp, 0, temp.length);
calSubrecords[i] = new CalSubrecord(temp, displayTimeOffset);
Expand Down
@@ -1,12 +1,15 @@
package com.nightscout.android.dexcom.records;

import android.util.Log;

import com.nightscout.android.dexcom.Utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Date;

public class CalSubrecord {
private static final String TAG = CalSubrecord.class.getSimpleName();
private Date dateEntered;
private int calBGL;
private int calRaw;
Expand Down
Expand Up @@ -8,11 +8,11 @@

public class GenericTimestampRecord {

private final int OFFSET_SYS_TIME = 0;
private final int OFFSET_DISPLAY_TIME = 4;
private Date systemTime;
private int systemTimeSeconds;
private Date displayTime;
protected final int OFFSET_SYS_TIME = 0;
protected final int OFFSET_DISPLAY_TIME = 4;
protected Date systemTime;
protected int systemTimeSeconds;
protected Date displayTime;

public GenericTimestampRecord(byte[] packet) {
systemTimeSeconds = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(OFFSET_SYS_TIME);
Expand Down
@@ -1,9 +1,7 @@
package com.nightscout.android.dexcom.records;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class MeterRecord extends GenericTimestampRecord {

Expand Down
@@ -0,0 +1,82 @@
package com.nightscout.android.dexcom.records;

import com.nightscout.android.dexcom.CRC16;
import com.nightscout.android.dexcom.CRCFailRuntimeException;
import com.nightscout.android.dexcom.Constants;
import com.nightscout.android.dexcom.Utils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class PageHeader {
protected final int HEADER_SIZE=28;
protected final int FIRSTRECORDINDEX_OFFSET=0;
protected final int NUMRECS_OFFSET=4;
protected final int RECTYPE_OFFSET=8;
protected final int REV_OFFSET=9;
protected final int PAGENUMBER_OFFSET=10;
protected final int RESERVED2_OFFSET=14;
protected final int RESERVED3_OFFSET=18;
protected final int RESERVED4_OFFSET=22;

protected int firstRecordIndex;
protected int numOfRecords;
protected Constants.RECORD_TYPES recordType;
protected byte revision;
protected int pageNumber;
protected int reserved2;
protected int reserved3;
protected int reserved4;
protected byte[] crc=new byte[2];


public PageHeader(byte[] packet) {
firstRecordIndex = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(FIRSTRECORDINDEX_OFFSET);
numOfRecords = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(NUMRECS_OFFSET);
recordType = Constants.RECORD_TYPES.values()[packet[RECTYPE_OFFSET]];
revision = packet[REV_OFFSET];
pageNumber = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(PAGENUMBER_OFFSET);
reserved2 = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(RESERVED2_OFFSET);
reserved3 = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(RESERVED3_OFFSET);
reserved4 = ByteBuffer.wrap(packet).order(ByteOrder.LITTLE_ENDIAN).getInt(RESERVED4_OFFSET);
System.arraycopy(packet,HEADER_SIZE-Constants.CRC_LEN,crc,0,Constants.CRC_LEN);
byte[] crc_calc = CRC16.calculate(packet,0,HEADER_SIZE - Constants.CRC_LEN);
if (!Arrays.equals(this.crc, crc_calc)) {
throw new CRCFailRuntimeException("CRC check failed: " + Utils.bytesToHex(this.crc) + " vs " + Utils.bytesToHex(crc_calc));
}

}

public byte getRevision() {
return revision;
}

public Constants.RECORD_TYPES getRecordType() {
return recordType;
}

public int getFirstRecordIndex() {
return firstRecordIndex;
}

public int getNumOfRecords() {
return numOfRecords;
}

public int getPageNumber() {
return pageNumber;
}

public int getReserved2() {
return reserved2;
}

public int getReserved3() {
return reserved3;
}

public int getReserved4() {
return reserved4;
}
}

0 comments on commit 675b687

Please sign in to comment.