aboutsummaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2013-05-20 22:03:59 -0500
committerDavid Barksdale <amatus.amongus@gmail.com>2013-05-20 22:03:59 -0500
commiteab6ca58cc8a8db37845a55120e1c8ebe371c359 (patch)
tree8d1ac5e7f655de0d28fc03eeb5ff32b0a62955bd /android
parentd060e56a3c578622e74af6b9be8903eb763391bc (diff)
Use checksum for message synchronization.
We've lost the framing information over bluetooth, one way to try to recover synchronization is to check message checksum.
Diffstat (limited to 'android')
-rw-r--r--android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java48
1 files changed, 40 insertions, 8 deletions
diff --git a/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java b/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java
index ba021cf..e1d2b81 100644
--- a/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java
+++ b/android/IBusIntents/src/name/amatus/ibusintents/IBusIntentsService.java
@@ -54,25 +54,57 @@ public class IBusIntentsService extends IntentService
}
BluetoothDevice device =
adapter.getRemoteDevice(intent.getStringExtra("device"));
- BluetoothSocket socket;
- InputStream stream;
try {
- socket =
+ BluetoothSocket socket =
device.createRfcommSocketToServiceRecord(UUID16ServiceClassSerialPort);
socket.connect();
- stream = socket.getInputStream();
+ BufferedInputStream stream =
+ new BufferedInputStream(socket.getInputStream());
while (true) {
- byte[] buffer = new byte[256];
- int length = stream.read(buffer);
+ stream.mark(258);
+ int source = stream.read();
+ if (-1 == source) {
+ Log.w(Name, "EOF while reading stream");
+ break;
+ }
+ int length = stream.read();
if (-1 == length) {
- Log.w(Name, "Read failed");
+ Log.w(Name, "EOF while reading stream");
break;
}
- String hex = new String();
+ if (length < 2) {
+ Log.d(Name, "Message too short");
+ stream.reset();
+ stream.skip(1);
+ continue;
+ }
+ byte[] buffer = new byte[length];
+ int count = 0;
+ for (int offset = 0; offset < length; offset += count) {
+ count = stream.read(buffer, offset, length - offset);
+ if (-1 == count) {
+ Log.w(Name, "EOF while reading stream");
+ return;
+ }
+ }
+ String hex = String.format("%02X %02X", source, length);
for (int i = 0; i < length; ++i) {
hex += String.format(" %02X", buffer[i]);
}
Log.d(Name, "I-Bus says:" + hex);
+ byte checksum = (byte)(source ^ length);
+ for (int i = 0; i < length; ++i) {
+ checksum ^= buffer[i];
+ }
+ if (checksum != 0) {
+ Log.d(Name, String.format("Invalid checksum: %02X", checksum));
+ stream.reset();
+ stream.skip(1);
+ continue;
+ }
+ Log.d(Name, "Message valid");
+ stream.reset();
+ stream.skip(1);
}
socket.close();
} catch (IOException exception) {