1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-09 19:49:30 +01:00

Pebble: experimenting with more generic application message encoding

This commit is contained in:
Andreas Shimokawa 2015-05-11 23:30:38 +02:00
parent 05d22b8dd1
commit 6fab01a3c2

View File

@ -1,9 +1,11 @@
package nodomain.freeyourgadget.gadgetbridge.pebble; package nodomain.freeyourgadget.gadgetbridge.pebble;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.TimeZone; import java.util.TimeZone;
@ -429,48 +431,65 @@ public class PebbleProtocol extends GBDeviceProtocol {
} }
public byte[] encodeApplicationMessageTest() { public byte[] encodeApplicationMessageTest() {
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + 69 + LENGTH_PREFIX + 18);
// encode push message for WeatherNeat
ArrayList<Pair<Integer, Object>> pairs = new ArrayList<>();
pairs.add(new Pair<>(1, (Object) "Berlin")); // city
pairs.add(new Pair<>(2, (Object) "22 C")); // temperature
pairs.add(new Pair<>(3, (Object) "windy")); // condition
pairs.add(new Pair<>(5, (Object) 3)); // seconds for backlight on shake
byte[] testMessage = encodeApplicationMessagePush(ENDPOINT_APPLICATIONMESSAGE, WeatherNeatUUID, pairs);
ByteBuffer buf = ByteBuffer.allocate(testMessage.length + LENGTH_PREFIX + 18); // +ACK
// encode ack and put in front of push message (hack for acknowledgeing the last message)
buf.order(ByteOrder.BIG_ENDIAN); buf.order(ByteOrder.BIG_ENDIAN);
// ACK 18
buf.putShort((short) 18); buf.putShort((short) 18);
buf.putShort(ENDPOINT_APPLICATIONMESSAGE); buf.putShort(ENDPOINT_APPLICATIONMESSAGE);
buf.put(APPLICATIONMESSAGE_ACK); buf.put(APPLICATIONMESSAGE_ACK);
buf.put(last_id); buf.put(--last_id);
buf.put(WeatherNeatUUID); buf.put(WeatherNeatUUID);
buf.putShort((short) 69);
buf.putShort(ENDPOINT_APPLICATIONMESSAGE); buf.put(testMessage);
// 19
return buf.array();
}
public byte[] encodeApplicationMessagePush(short endpoint, byte[] uuid, ArrayList<Pair<Integer, Object>> pairs) {
int length = uuid.length + 3; // PUSH + id + length of dict
for (Pair<Integer, Object> pair : pairs) {
length += 7; // key + type + length
if (pair.second instanceof Integer) {
length += 4;
} else if (pair.second instanceof String) {
length += ((String) pair.second).length() + 1;
}
}
ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
buf.order(ByteOrder.BIG_ENDIAN);
buf.putShort((short) length);
buf.putShort(endpoint); // 48 or 49
buf.put(APPLICATIONMESSAGE_PUSH); buf.put(APPLICATIONMESSAGE_PUSH);
buf.put(++last_id); buf.put(++last_id);
buf.put(WeatherNeatUUID); buf.put(uuid);
buf.put((byte) 4); buf.put((byte) pairs.size());
// 14
buf.order(ByteOrder.LITTLE_ENDIAN); buf.order(ByteOrder.LITTLE_ENDIAN); // Um, yes, really
buf.putInt(0x1); // city for (Pair<Integer, Object> pair : pairs) {
buf.put((byte) 1); // string buf.putInt(pair.first);
buf.putShort((short) 7); // length of string if (pair.second instanceof Integer) {
String temp = "Berlin"; buf.put((byte) 0);
buf.put(temp.getBytes());
buf.put((byte) 0x00);
// 12
temp = "22 C";
buf.putInt(0x2); // temperature
buf.put((byte) 1); // string
buf.putShort((short) 5); // length of string
buf.put(temp.getBytes());
buf.put((byte) 0x00);
// 13
temp = "windy";
buf.putInt(0x3); // city
buf.put((byte) 1); // string
buf.putShort((short) 6); // length of string
buf.put(temp.getBytes());
buf.put((byte) 0x00);
//11
buf.putInt(0x5); // backlight timeoff on shake
buf.put((byte) 0); // int
buf.putShort((short) 4); // length of int buf.putShort((short) 4); // length of int
buf.putInt(0); // no backlight on shake buf.putInt((int) pair.second);
} else if (pair.second instanceof String) {
buf.put((byte) 1);
buf.putShort((short) (((String) pair.second).length() + 1));
buf.put(((String) pair.second).getBytes());
buf.put((byte) 0);
}
}
return buf.array(); return buf.array();
} }