|
| 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 = 0x00; |
| 44 | + private static final short packetTypeData = 0x03; |
| 45 | + private static final short packetTypeAck = 0x04; |
| 46 | + private static final short packetTypeNack = 0x05; |
| 47 | + |
| 48 | + // Terrapin commands |
| 49 | + private static final short commandNewMeasurementAvailable = 0x1000; |
| 50 | + private static final short commandStartMeasurement = 0x1001; |
| 51 | + private static final short commandGetLastRange = 0x1002; |
| 52 | + private static final short commandGetLastInclination = 0x1003; |
| 53 | + private static final short commandGetLastDirection = 0x1004; |
| 54 | + private static final short commandGetLastTemperature = 0x1005; |
| 55 | + private static final short commandGetLastPressure = 0x1006; |
| 56 | + private static final short commandGetLastEHR = 0x1007; |
| 57 | + private static final short commandGetLaserMode = 0x1022; |
| 58 | + private static final short commandGetDeclination = 0x1027; |
| 59 | + private static final short commandGetRangeGate = 0x1028; |
| 60 | + // private static final short commandGetLastSNR = 0xf006; |
| 61 | + |
| 62 | + // private static final short commandGetComVersion = 0x01; |
| 63 | + // private static final short commandGetSupportedCommandSet = 0x02; |
| 64 | + // private static final short commandGetSerialNumber = 0x03; |
| 65 | + // private static final short commandActivateFactoryMode = 0x04; |
| 66 | + // private static final short commandGetHardwareRevision = 0x05; |
| 67 | + // private static final short commandGetFirmwareVersion = 0x06; |
| 68 | + // private static final short commandGetBatteryLevel = 0x07; |
| 69 | + // private static final short commandGetDeviceName = 0x08; |
| 70 | + // private static final short commandSetDeviceName = 0x09; |
| 71 | + // private static final short commandGetDeviceId = 0x0a; |
| 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 sentence " + 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 | + // Data length |
| 108 | + int dataLength = getShort(frame, 2); |
| 109 | + if (dataLength == 512) dataLength = 0; |
| 110 | + // Command |
| 111 | + final int command = getShort(frame, 4); |
| 112 | + if (packetType == packetTypeCommand) { |
| 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 " + dataLength + " " + shortToHex(command) + " " + 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 - 1] = (byte) ((checksum >> 8) & 0xff); |
| 171 | + frame[frame.length - 2] = (byte) (checksum & 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] & 0xff) | (data[offset + 1] << 8)); |
| 264 | + } |
| 265 | +} |
0 commit comments