Skip to content

Commit 0b18036

Browse files
committed
Vectronix Terrapin-X laser rangefinder protocol
1 parent 1d830c4 commit 0b18036

File tree

3 files changed

+334
-0
lines changed

3 files changed

+334
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.platypii.baseline.lasers.rangefinder;
2+
3+
class Crc16 {
4+
/**
5+
* Computes the CRC16 checksum for a given byte array.
6+
*/
7+
public static short crc16(byte[] byteArray) {
8+
int crc = 0xffff;
9+
10+
// Process bytes in pairs (LSB first)
11+
for (int i = 0; i < byteArray.length; i++) {
12+
int b = byteArray[i] & 0xFF;
13+
crc ^= b;
14+
15+
// Process each bit
16+
for (int j = 0; j < 8; j++) {
17+
if ((crc & 0x0001) != 0) {
18+
crc = (crc >> 1) ^ 0x8408; // 0x8408 is reversed polynomial
19+
} else {
20+
crc = crc >> 1;
21+
}
22+
}
23+
}
24+
25+
// XOR with 0xfff
26+
crc ^= 0xffff;
27+
28+
// Swap the bytes of the result
29+
crc = ((crc & 0xFF) << 8) | ((crc >> 8) & 0xFF);
30+
31+
return (short) crc;
32+
}
33+
34+
private static final int[] CRC16_LOOKUP_TABLE = {
35+
0, 4489, 8978, 12955, 17956, 22445, 25910, 29887,
36+
35912, 40385, 44890, 48851, 51820, 56293, 59774, 63735,
37+
4225, 264, 13203, 8730, 22181, 18220, 30135, 25662,
38+
40137, 36160, 49115, 44626, 56045, 52068, 63999, 59510,
39+
8450, 12427, 528, 5017, 26406, 30383, 17460, 21949,
40+
44362, 48323, 36440, 40913, 60270, 64231, 51324, 55797,
41+
12675, 8202, 4753, 792, 30631, 26158, 21685, 17724,
42+
48587, 44098, 40665, 36688, 64495, 60006, 55549, 51572,
43+
16900, 21389, 24854, 28831, 1056, 5545, 10034, 14011,
44+
52812, 57285, 60766, 64727, 34920, 39393, 43898, 47859,
45+
21125, 17164, 29079, 24606, 5281, 1320, 14259, 9786,
46+
57037, 53060, 64991, 60502, 39145, 35168, 48123, 43634,
47+
25350, 29327, 16404, 20893, 9506, 13483, 1584, 6073,
48+
61262, 65223, 52316, 56789, 43370, 47331, 35448, 39921,
49+
29575, 25102, 20629, 16668, 13731, 9258, 5809, 1848,
50+
65487, 60998, 56541, 52564, 47595, 43106, 39673, 35696,
51+
33800, 38273, 42778, 46739, 49708, 54181, 57662, 61623,
52+
2112, 6601, 11090, 15067, 20068, 24557, 28022, 31999,
53+
38025, 34048, 47003, 42514, 53933, 49956, 61887, 57398,
54+
6337, 2376, 15315, 10842, 24293, 20332, 32247, 27774,
55+
42250, 46211, 34328, 38801, 58158, 62119, 49212, 53685,
56+
10562, 14539, 2640, 7129, 28518, 32495, 19572, 24061,
57+
46475, 41986, 38553, 34576, 62383, 57894, 53437, 49460,
58+
14787, 10314, 6865, 2904, 32743, 28270, 23797, 19836,
59+
50700, 55173, 58654, 62615, 32808, 37281, 41786, 45747,
60+
19012, 23501, 26966, 30943, 3168, 7657, 12146, 16123,
61+
54925, 50948, 62879, 58390, 37033, 33056, 46011, 41522,
62+
23237, 19276, 31191, 26718, 7393, 3432, 16371, 11898,
63+
59150, 63111, 50204, 54677, 41258, 45219, 33336, 37809,
64+
27462, 31439, 18516, 23005, 11618, 15595, 3696, 8185,
65+
63375, 58886, 54429, 50452, 45483, 40994, 37561, 33584,
66+
31687, 27214, 22741, 18780, 15843, 11370, 7921, 3960
67+
};
68+
}

app/src/main/java/com/platypii/baseline/lasers/rangefinder/RangefinderService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class RangefinderService {
3030
private final BleProtocol protocols[] = {
3131
new ATNProtocol(),
3232
new SigSauerProtocol(),
33+
new TerrapinProtocol(),
3334
new UineyeProtocol()
3435
};
3536

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
package com.platypii.baseline.lasers.rangefinder;
2+
3+
import com.platypii.baseline.bluetooth.BleException;
4+
import com.platypii.baseline.bluetooth.BleProtocol;
5+
import com.platypii.baseline.lasers.LaserMeasurement;
6+
import com.platypii.baseline.util.Exceptions;
7+
8+
import android.bluetooth.le.ScanRecord;
9+
import android.os.ParcelUuid;
10+
import android.util.Log;
11+
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
13+
import com.welie.blessed.BluetoothPeripheral;
14+
import com.welie.blessed.WriteType;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
import java.util.UUID;
18+
import org.greenrobot.eventbus.EventBus;
19+
20+
import static com.platypii.baseline.bluetooth.BluetoothUtil.byteArrayToHex;
21+
import static com.platypii.baseline.bluetooth.BluetoothUtil.toManufacturerString;
22+
23+
/**
24+
* This class contains ids, commands, and decoders for Vectronix Terrapin-X laser rangefinders.
25+
*/
26+
class TerrapinProtocol extends BleProtocol {
27+
private static final String TAG = "TerrapinProtocol";
28+
29+
// Manufacturer ID
30+
private static final int manufacturerId1 = 1164;
31+
private static final byte[] manufacturerData1 = {1, -96, -1, -1, -1, -1, 0}; // 01-a0-ff-ff-ff-ff-00
32+
33+
// Terrapin service
34+
private static final UUID terrapinService = UUID.fromString("81480000-b0b7-4074-8a24-ae554e5cdbc4");
35+
// Terrapin characteristic: read, indicate
36+
private static final UUID terrapinCharacteristic1 = UUID.fromString("81480100-b0b7-4074-8a24-ae554e5cdbc4");
37+
// Terrapin characteristic: notify, write
38+
private static final UUID terrapinCharacteristic2 = UUID.fromString("81480200-b0b7-4074-8a24-ae554e5cdbc4");
39+
40+
private static final String factoryModeSecretKey = "b6987833";
41+
42+
// Terrapin packet types
43+
private static final short packetTypeCommand = 0x0000;
44+
private static final short packetTypeData = 0x0300;
45+
private static final short packetTypeAck = 0x0400;
46+
private static final short packetTypeNack = 0x0500;
47+
48+
// Terrapin commands
49+
private static final short commandNewMeasurementAvailable = 0x0010;
50+
private static final short commandStartMeasurement = 0x0110;
51+
private static final short commandGetLastRange = 0x0210;
52+
private static final short commandGetLastInclination = 0x0310;
53+
private static final short commandGetLastDirection = 0x0410;
54+
private static final short commandGetLastTemperature = 0x0510;
55+
private static final short commandGetLastPressure = 0x0610;
56+
private static final short commandGetLastEHR = 0x0710;
57+
private static final short commandGetLaserMode = 0x2210;
58+
private static final short commandGetDeclination = 0x2710;
59+
private static final short commandGetRangeGate = 0x2810;
60+
private static final short commandGetLastSNR = 0x06f0;
61+
62+
// private static final short commandGetComVersion = 0x0100;
63+
// private static final short commandGetSupportedCommandSet = 0x0200;
64+
// private static final short commandGetSerialNumber = 0x0300;
65+
// private static final short commandActivateFactoryMode = 0x0400;
66+
// private static final short commandGetHardwareRevision = 0x0500;
67+
// private static final short commandGetFirmwareVersion = 0x0600;
68+
// private static final short commandGetBatteryLevel = 0x0700;
69+
// private static final short commandGetDeviceName = 0x0800;
70+
// private static final short commandSetDeviceName = 0x0900;
71+
// private static final short commandGetDeviceId = 0x0a00;
72+
73+
@Override
74+
public void onServicesDiscovered(@NonNull BluetoothPeripheral peripheral) {
75+
try {
76+
// Request rangefinder service
77+
Log.i(TAG, "app -> rf: subscribe");
78+
peripheral.setNotify(terrapinService, terrapinCharacteristic1, true);
79+
} catch (Throwable e) {
80+
Log.e(TAG, "rangefinder handshake exception", e);
81+
}
82+
}
83+
84+
@Override
85+
public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value) {
86+
Log.d(TAG, "rf -> app: processBytes " + byteArrayToHex(value));
87+
if (value[0] != 0x7e || value[value.length - 1] != 0x7e) {
88+
Log.w(TAG, "rf -> app: invalid command " + byteArrayToHex(value));
89+
return;
90+
}
91+
92+
// Remove frame
93+
byte[] frame = Arrays.copyOfRange(value, 1, value.length - 1);
94+
// Unescape special characters 0x7e and 0x7d
95+
frame = unescape(frame);
96+
97+
// Check checksum
98+
final int checksumValue = getShort(frame, frame.length - 2);
99+
byte[] checksumContent = Arrays.copyOfRange(frame, 0, frame.length - 2);
100+
final short checksumComputed = Crc16.crc16(checksumContent);
101+
if (checksumValue != checksumComputed) {
102+
Log.w(TAG, "rf -> app: invalid checksum " + byteArrayToHex(checksumContent) + " " + shortToHex(checksumValue) + " != " + shortToHex(checksumComputed));
103+
}
104+
105+
// Packet types
106+
final short packetType = getShort(frame, 0);
107+
if (packetType == packetTypeCommand) {
108+
// Data length
109+
int dataLength = getShort(frame, 2);
110+
if (dataLength == 512) dataLength = 0;
111+
// Command
112+
final int command = getShort(frame, 4);
113+
final byte[] data = Arrays.copyOfRange(frame, 6, frame.length - 2);
114+
if (command == commandNewMeasurementAvailable) {
115+
Log.i(TAG, "rf -> app: new measurement available");
116+
getLastRange(peripheral);
117+
} else {
118+
Log.w(TAG, "rf -> app: command unknown 0x" + shortToHex(command) + " " + dataLength + " " + byteArrayToHex(data));
119+
}
120+
} else if (packetType == packetTypeData) {
121+
Log.i(TAG, "rf -> app: data " + byteArrayToHex(frame));
122+
} else if (packetType == packetTypeAck) {
123+
Log.i(TAG, "rf -> app: ack " + byteArrayToHex(frame));
124+
} else if (packetType == packetTypeNack) {
125+
Log.i(TAG, "rf -> app: nack " + byteArrayToHex(frame));
126+
} else {
127+
Log.w(TAG, "rf -> app: unknown packet type " + shortToHex(packetType) + " " + byteArrayToHex(frame));
128+
}
129+
}
130+
131+
private String shortToHex(int value) {
132+
return Integer.toHexString(value & 0xffff);
133+
}
134+
135+
private void processMeasurement(@NonNull byte[] value) {
136+
Log.d(TAG, "rf -> app: measure " + byteArrayToHex(value));
137+
// TODO
138+
EventBus.getDefault().post(new LaserMeasurement(0, 0));
139+
}
140+
private void startMeasurement(@NonNull BluetoothPeripheral peripheral) {
141+
Log.i(TAG, "app -> rf: start measurement");
142+
sendCommand(peripheral, commandStartMeasurement, null);
143+
}
144+
145+
private void getLastRange(@NonNull BluetoothPeripheral peripheral) {
146+
Log.i(TAG, "app -> rf: get last range");
147+
sendCommand(peripheral, commandGetLastRange, null);
148+
}
149+
150+
private void sendCommand(@NonNull BluetoothPeripheral peripheral, short command, @Nullable byte[] data) {
151+
final int dataLength = data == null ? 0 : data.length; // TODO: / 2 ?
152+
byte[] frame = new byte[8 + dataLength];
153+
// Packet type
154+
frame[0] = (byte) (packetTypeCommand & 0xff);
155+
frame[1] = (byte) ((packetTypeCommand >> 8) & 0xff);
156+
// Data length
157+
if (data != null) {
158+
frame[2] = (byte) (dataLength & 0xff);
159+
frame[3] = (byte) ((dataLength >> 8) & 0xff);
160+
System.arraycopy(data, 0, frame, 6, data.length);
161+
} else {
162+
frame[2] = 2;
163+
frame[3] = 0;
164+
}
165+
// Command
166+
frame[4] = (byte) (command & 0xff);
167+
frame[5] = (byte) ((command >> 8) & 0xff);
168+
// Checksum
169+
final int checksum = Crc16.crc16(Arrays.copyOfRange(frame, 0, frame.length - 2));
170+
frame[frame.length - 2] = (byte) (checksum & 0xff);
171+
frame[frame.length - 1] = (byte) ((checksum >> 8) & 0xff);
172+
// Escape special characters 0x7e and 0x7d
173+
frame = escape(frame);
174+
// Wrap frame
175+
byte[] wrapped = new byte[frame.length + 2];
176+
wrapped[0] = 0x7e;
177+
System.arraycopy(frame, 0, wrapped, 1, frame.length);
178+
wrapped[wrapped.length - 1] = 0x7e;
179+
Log.d(TAG, "app -> rf: send command " + byteArrayToHex(wrapped));
180+
peripheral.writeCharacteristic(terrapinService, terrapinCharacteristic2, wrapped, WriteType.WITH_RESPONSE);
181+
}
182+
183+
/**
184+
* Return true iff a bluetooth scan result looks like a rangefinder
185+
*/
186+
@Override
187+
public boolean canParse(@NonNull BluetoothPeripheral peripheral, @Nullable ScanRecord record) {
188+
final String deviceName = peripheral.getName();
189+
if (record != null && Arrays.equals(record.getManufacturerSpecificData(manufacturerId1), manufacturerData1)) {
190+
return true; // Manufacturer match (kenny's laser)
191+
} else if (
192+
(record != null && hasRangefinderService(record))
193+
|| deviceName.startsWith("FastM")
194+
|| deviceName.startsWith("Terrapin")) {
195+
// Send manufacturer data to firebase
196+
final String mfg = toManufacturerString(record);
197+
Exceptions.report(new BleException("Terrapin laser unknown mfg data: " + deviceName + " " + mfg));
198+
return true;
199+
} else {
200+
return false;
201+
}
202+
}
203+
204+
private boolean hasRangefinderService(@NonNull ScanRecord record) {
205+
final List<ParcelUuid> uuids = record.getServiceUuids();
206+
return uuids != null && uuids.contains(new ParcelUuid(terrapinService));
207+
}
208+
209+
private byte[] escape(byte[] data) {
210+
final byte[] escaped = new byte[data.length * 2];
211+
int j = 0;
212+
for (byte b : data) {
213+
if (b == 0x7e) {
214+
escaped[j++] = 0x7d;
215+
escaped[j++] = 0x5e;
216+
} else if (b == 0x7d) {
217+
escaped[j++] = 0x7d;
218+
escaped[j++] = 0x5d;
219+
} else {
220+
escaped[j++] = b;
221+
}
222+
}
223+
return Arrays.copyOf(escaped, j);
224+
}
225+
226+
private byte[] unescape(byte[] data) {
227+
final byte[] unescaped = new byte[data.length];
228+
int j = 0;
229+
for (int i = 0; i < data.length; i++) {
230+
if (data[i] == 0x7d) {
231+
if (data[i + 1] == 0x5e) {
232+
unescaped[j++] = 0x7e;
233+
} else if (data[i + 1] == 0x5d) {
234+
unescaped[j++] = 0x7d;
235+
} else {
236+
Log.w(TAG, "rf -> app: invalid escape sequence " + byteArrayToHex(data));
237+
}
238+
i++;
239+
} else {
240+
unescaped[j++] = data[i];
241+
}
242+
}
243+
return Arrays.copyOf(unescaped, j);
244+
}
245+
246+
@NonNull
247+
private byte[] swapEndianness(@NonNull byte[] data) {
248+
if (data.length % 2 != 0) {
249+
Log.w(TAG, "rf -> app: data length must be even " + byteArrayToHex(data));
250+
}
251+
final byte[] swapped = new byte[data.length];
252+
for (int i = 0; i < data.length; i += 2) {
253+
swapped[i] = data[i + 1];
254+
swapped[i + 1] = data[i];
255+
}
256+
return swapped;
257+
}
258+
259+
/**
260+
* Endian-swapped short from byte array
261+
*/
262+
private short getShort(@NonNull byte[] data, int offset) {
263+
return (short) ((data[offset + 1] & 0xff) | (data[offset] << 8));
264+
}
265+
}

0 commit comments

Comments
 (0)