|
| 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 | + |
| 58 | + // private static final short commandGetComVersion = 0x0100; |
| 59 | + // private static final short commandGetSupportedCommandSet = 0x0200; |
| 60 | + // private static final short commandGetSerialNumber = 0x0300; |
| 61 | + // private static final short commandActivateFactoryMode = 0x0400; |
| 62 | + // private static final short commandGetHardwareRevision = 0x0500; |
| 63 | + // private static final short commandGetFirmwareVersion = 0x0600; |
| 64 | + // private static final short commandGetBatteryLevel = 0x0700; |
| 65 | + // private static final short commandGetDeviceName = 0x0800; |
| 66 | + // private static final short commandSetDeviceName = 0x0900; |
| 67 | + // private static final short commandGetDeviceId = 0x0a00; |
| 68 | + |
| 69 | + @Override |
| 70 | + public void onServicesDiscovered(@NonNull BluetoothPeripheral peripheral) { |
| 71 | + try { |
| 72 | + // Request rangefinder service |
| 73 | + Log.i(TAG, "app -> rf: subscribe"); |
| 74 | + peripheral.setNotify(terrapinService, terrapinCharacteristic1, true); |
| 75 | + } catch (Throwable e) { |
| 76 | + Log.e(TAG, "rangefinder handshake exception", e); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void processBytes(@NonNull BluetoothPeripheral peripheral, @NonNull byte[] value) { |
| 82 | + Log.d(TAG, "rf -> app: process " + byteArrayToHex(value)); |
| 83 | + if (value[0] != 0x7e || value[value.length - 1] != 0x7e) { |
| 84 | + Log.w(TAG, "rf -> app: invalid command " + byteArrayToHex(value)); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + // Remove frame |
| 89 | + byte[] frame = Arrays.copyOfRange(value, 1, value.length - 1); |
| 90 | + // Unescape special characters 0x7e and 0x7d |
| 91 | + frame = unescape(frame); |
| 92 | + |
| 93 | + // Check checksum |
| 94 | + final int checksum = frame[frame.length - 2] + (frame[frame.length - 1] << 8); |
| 95 | + if (Crc16.crc16(frame) != checksum) { |
| 96 | + Log.w(TAG, "rf -> app: invalid checksum " + byteArrayToHex(frame) + " " + Integer.toHexString(Crc16.crc16(frame)) + " != " + Integer.toHexString(checksum)); |
| 97 | + } |
| 98 | + |
| 99 | + // Packet types |
| 100 | + if (frame[0] + (frame[1] << 8) == packetTypeCommand) { |
| 101 | + // Data length |
| 102 | + int dataLength = frame[3] + (frame[2] << 8); |
| 103 | + if (dataLength == 512) dataLength = 0; |
| 104 | + // Command |
| 105 | + final int command = frame[5] + (frame[4] << 8); |
| 106 | + final byte[] data = Arrays.copyOfRange(frame, 6, frame.length - 2); |
| 107 | + if (command == commandNewMeasurementAvailable) { |
| 108 | + Log.i(TAG, "rf -> app: new measurement available " + byteArrayToHex(data)); |
| 109 | + getLastRange(peripheral); |
| 110 | + } else { |
| 111 | + Log.w(TAG, "rf -> app: command unknown 0x" + Integer.toHexString(command) + " " + dataLength + " " + byteArrayToHex(data)); |
| 112 | + } |
| 113 | + } else if (frame[0] + (frame[1] << 8) == packetTypeData) { |
| 114 | + Log.i(TAG, "rf -> app: data " + byteArrayToHex(frame)); |
| 115 | + } else if (frame[0] + (frame[1] << 8) == packetTypeAck) { |
| 116 | + Log.i(TAG, "rf -> app: ack " + byteArrayToHex(frame)); |
| 117 | + } else if (frame[0] + (frame[1] << 8) == packetTypeNack) { |
| 118 | + Log.i(TAG, "rf -> app: nack " + byteArrayToHex(frame)); |
| 119 | + } else { |
| 120 | + Log.w(TAG, "rf -> app: unknown " + byteArrayToHex(frame)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void processMeasurement(@NonNull byte[] value) { |
| 125 | + Log.d(TAG, "rf -> app: measure " + byteArrayToHex(value)); |
| 126 | + // TODO |
| 127 | + EventBus.getDefault().post(new LaserMeasurement(0, 0)); |
| 128 | + } |
| 129 | + private void startMeasurement(@NonNull BluetoothPeripheral peripheral) { |
| 130 | + Log.i(TAG, "app -> rf: start measurement"); |
| 131 | + sendCommand(peripheral, commandStartMeasurement, null); |
| 132 | + } |
| 133 | + |
| 134 | + private void getLastRange(@NonNull BluetoothPeripheral peripheral) { |
| 135 | + Log.i(TAG, "app -> rf: get last range"); |
| 136 | + sendCommand(peripheral, commandGetLastRange, null); |
| 137 | + } |
| 138 | + |
| 139 | + private void sendCommand(@NonNull BluetoothPeripheral peripheral, short command, @Nullable byte[] data) { |
| 140 | + final int dataLength = data == null ? 0 : data.length; // TODO: / 2 ? |
| 141 | + byte[] frame = new byte[dataLength + 6]; |
| 142 | + // Packet type |
| 143 | + frame[0] = (byte) (packetTypeCommand & 0xff); |
| 144 | + frame[1] = (byte) ((packetTypeCommand >> 8) & 0xff); |
| 145 | + // Data length |
| 146 | + if (data != null) { |
| 147 | + frame[2] = (byte) (dataLength & 0xff); |
| 148 | + frame[3] = (byte) ((dataLength >> 8) & 0xff); |
| 149 | + System.arraycopy(data, 0, frame, 6, data.length); |
| 150 | + } else { |
| 151 | + frame[2] = 2; |
| 152 | + frame[3] = 0; |
| 153 | + } |
| 154 | + // Command |
| 155 | + frame[4] = (byte) (command & 0xff); |
| 156 | + frame[5] = (byte) ((command >> 8) & 0xff); |
| 157 | + // Data |
| 158 | + // Checksum |
| 159 | + final int checksum = Crc16.crc16(Arrays.copyOfRange(frame, 1, 7)); |
| 160 | + frame[frame.length - 2] = (byte) (checksum & 0xff); |
| 161 | + frame[frame.length - 1] = (byte) ((checksum >> 8) & 0xff); |
| 162 | + // Escape special characters 0x7e and 0x7d |
| 163 | + frame = escape(frame); |
| 164 | + // Wrap frame |
| 165 | + byte[] wrapped = new byte[frame.length + 2]; |
| 166 | + wrapped[0] = 0x7e; |
| 167 | + System.arraycopy(frame, 0, wrapped, 1, frame.length); |
| 168 | + wrapped[wrapped.length - 1] = 0x7e; |
| 169 | + Log.d(TAG, "app -> rf: send command " + byteArrayToHex(wrapped)); |
| 170 | + peripheral.writeCharacteristic(terrapinService, terrapinCharacteristic2, wrapped, WriteType.WITH_RESPONSE); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Return true iff a bluetooth scan result looks like a rangefinder |
| 175 | + */ |
| 176 | + @Override |
| 177 | + public boolean canParse(@NonNull BluetoothPeripheral peripheral, @Nullable ScanRecord record) { |
| 178 | + final String deviceName = peripheral.getName(); |
| 179 | + if (record != null && Arrays.equals(record.getManufacturerSpecificData(manufacturerId1), manufacturerData1)) { |
| 180 | + return true; // Manufacturer match (kenny's laser) |
| 181 | + } else if ( |
| 182 | + (record != null && hasRangefinderService(record)) |
| 183 | + || deviceName.startsWith("FastM") |
| 184 | + || deviceName.startsWith("Terrapin")) { |
| 185 | + // Send manufacturer data to firebase |
| 186 | + final String mfg = toManufacturerString(record); |
| 187 | + Exceptions.report(new BleException("Terrapin laser unknown mfg data: " + deviceName + " " + mfg)); |
| 188 | + return true; |
| 189 | + } else { |
| 190 | + return false; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + private boolean hasRangefinderService(@NonNull ScanRecord record) { |
| 195 | + final List<ParcelUuid> uuids = record.getServiceUuids(); |
| 196 | + return uuids != null && uuids.contains(new ParcelUuid(terrapinService)); |
| 197 | + } |
| 198 | + |
| 199 | + private byte[] escape(byte[] data) { |
| 200 | + final byte[] escaped = new byte[data.length * 2]; |
| 201 | + int j = 0; |
| 202 | + for (byte b : data) { |
| 203 | + if (b == 0x7e) { |
| 204 | + escaped[j++] = 0x7d; |
| 205 | + escaped[j++] = 0x5e; |
| 206 | + } else if (b == 0x7d) { |
| 207 | + escaped[j++] = 0x7d; |
| 208 | + escaped[j++] = 0x5d; |
| 209 | + } else { |
| 210 | + escaped[j++] = b; |
| 211 | + } |
| 212 | + } |
| 213 | + return Arrays.copyOf(escaped, j); |
| 214 | + } |
| 215 | + |
| 216 | + private byte[] unescape(byte[] data) { |
| 217 | + final byte[] unescaped = new byte[data.length]; |
| 218 | + int j = 0; |
| 219 | + for (int i = 0; i < data.length; i++) { |
| 220 | + if (data[i] == 0x7d) { |
| 221 | + if (data[i + 1] == 0x5e) { |
| 222 | + unescaped[j++] = 0x7e; |
| 223 | + } else if (data[i + 1] == 0x5d) { |
| 224 | + unescaped[j++] = 0x7d; |
| 225 | + } else { |
| 226 | + Log.w(TAG, "rf -> app: invalid escape sequence " + byteArrayToHex(data)); |
| 227 | + } |
| 228 | + i++; |
| 229 | + } else { |
| 230 | + unescaped[j++] = data[i]; |
| 231 | + } |
| 232 | + } |
| 233 | + return Arrays.copyOf(unescaped, j); |
| 234 | + } |
| 235 | +} |
0 commit comments