List of usage examples for java.nio ByteBuffer order
Endianness order
To view the source code for java.nio ByteBuffer order.
Click Source Link
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodePhoneVersion2x(byte os) { final short LENGTH_PHONEVERSION = 17; ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort(LENGTH_PHONEVERSION);/*from www. j av a 2 s .co m*/ buf.putShort(ENDPOINT_PHONEVERSION); buf.put((byte) 0x01); buf.putInt(-1); //0xffffffff if (os == PHONEVERSION_REMOTE_OS_ANDROID) { buf.putInt(PHONEVERSION_SESSION_CAPS_GAMMARAY); } else { buf.putInt(0); } buf.putInt(PHONEVERSION_REMOTE_CAPS_SMS | PHONEVERSION_REMOTE_CAPS_TELEPHONY | os); buf.put(PHONEVERSION_APPVERSION_MAGIC); buf.put(PHONEVERSION_APPVERSION_MAJOR); buf.put(PHONEVERSION_APPVERSION_MINOR); buf.put(PHONEVERSION_APPVERSION_PATCH); return buf.array(); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodePhoneVersion3x(byte os) { final short LENGTH_PHONEVERSION3X = 25; ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_PHONEVERSION3X); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort(LENGTH_PHONEVERSION3X); buf.putShort(ENDPOINT_PHONEVERSION); buf.put((byte) 0x01); buf.putInt(-1); //0xffffffff buf.putInt(0);// w w w. j a v a 2 s . co m buf.putInt(os); buf.put(PHONEVERSION_APPVERSION_MAGIC); buf.put((byte) 4); // major buf.put((byte) 1); // minor buf.put((byte) 1); // patch buf.order(ByteOrder.LITTLE_ENDIAN); buf.putLong(0x00000000000029af); //flags return buf.array(); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodeWeatherForecast(WeatherSpec weatherSpec) { final short WEATHER_FORECAST_LENGTH = 20; String[] parts = { weatherSpec.location, weatherSpec.currentCondition }; // Calculate length first short attributes_length = 0; if (parts != null) { for (String s : parts) { if (s == null || s.equals("")) { continue; }//from w ww .j a va 2 s. c o m attributes_length += (2 + s.getBytes().length); } } short pin_length = (short) (WEATHER_FORECAST_LENGTH + attributes_length); ByteBuffer buf = ByteBuffer.allocate(pin_length); buf.order(ByteOrder.LITTLE_ENDIAN); buf.put((byte) 3); // unknown, always 3? buf.putShort((short) (weatherSpec.currentTemp - 273)); buf.put(Weather.mapToPebbleCondition(weatherSpec.currentConditionCode)); buf.putShort((short) (weatherSpec.todayMaxTemp - 273)); buf.putShort((short) (weatherSpec.todayMinTemp - 273)); buf.put(Weather.mapToPebbleCondition(weatherSpec.tomorrowConditionCode)); buf.putShort((short) (weatherSpec.tomorrowMaxTemp - 273)); buf.putShort((short) (weatherSpec.tomorrowMinTemp - 273)); buf.putInt(weatherSpec.timestamp); buf.put((byte) 0); // automatic location 0=manual 1=auto buf.putShort(attributes_length); // Encode Pascal-Style Strings if (parts != null) { for (String s : parts) { if (s == null || s.equals("")) { continue; } int partlength = s.getBytes().length; if (partlength > 512) partlength = 512; buf.putShort((short) partlength); buf.put(s.getBytes(), 0, partlength); } } return encodeBlobdb(UUID_LOCATION, BLOBDB_INSERT, BLOBDB_WEATHER, buf.array()); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
@Override public byte[] encodeAppDelete(UUID uuid) { if (mFwMajor >= 3) { if (UUID_PEBBLE_HEALTH.equals(uuid)) { return encodeActivateHealth(false); }//from www. j ava 2 s .c o m if (UUID_WORKOUT.equals(uuid)) { return encodeActivateHRM(false); } if (UUID_WEATHER.equals(uuid)) { //TODO: probably it wasn't present in firmware 3 return encodeActivateWeather(false); } return encodeBlobdb(uuid, BLOBDB_DELETE, BLOBDB_APP, null); } else { final short LENGTH_REMOVEAPP_2X = 17; ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_REMOVEAPP_2X); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort(LENGTH_REMOVEAPP_2X); buf.putShort(ENDPOINT_APPMANAGER); buf.put(APPMANAGER_REMOVEAPP); buf.putLong(uuid.getMostSignificantBits()); buf.putLong(uuid.getLeastSignificantBits()); return buf.array(); } }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
@Override public byte[] encodeSetCannedMessages(CannedMessagesSpec cannedMessagesSpec) { if (cannedMessagesSpec.cannedMessages == null || cannedMessagesSpec.cannedMessages.length == 0) { return null; }//w w w. ja v a 2 s . c om String blobDBKey; switch (cannedMessagesSpec.type) { case CannedMessagesSpec.TYPE_MISSEDCALLS: blobDBKey = "com.pebble.android.phone"; break; case CannedMessagesSpec.TYPE_NEWSMS: blobDBKey = "com.pebble.sendText"; break; default: return null; } int replies_length = -1; for (String reply : cannedMessagesSpec.cannedMessages) { replies_length += reply.getBytes().length + 1; } ByteBuffer buf = ByteBuffer.allocate(12 + replies_length); buf.order(ByteOrder.LITTLE_ENDIAN); buf.putInt(0x00000000); // unknown buf.put((byte) 0x00); // attributes count? buf.put((byte) 0x01); // actions count? // action buf.put((byte) 0x00); // action id buf.put((byte) 0x03); // action type = reply buf.put((byte) 0x01); // attributes count buf.put((byte) 0x08); // canned messages buf.putShort((short) replies_length); for (int i = 0; i < cannedMessagesSpec.cannedMessages.length - 1; i++) { buf.put(cannedMessagesSpec.cannedMessages[i].getBytes()); buf.put((byte) 0x00); } // last one must not be zero terminated, else we get an additional empty reply buf.put(cannedMessagesSpec.cannedMessages[cannedMessagesSpec.cannedMessages.length - 1].getBytes()); return encodeBlobdb(blobDBKey, BLOBDB_INSERT, BLOBDB_CANNED_MESSAGES, buf.array()); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private GBDeviceEvent[] decodeAction(ByteBuffer buf) { buf.order(ByteOrder.LITTLE_ENDIAN); byte command = buf.get(); if (command == NOTIFICATIONACTION_INVOKE) { int id;//from w w w . j av a 2s . c om UUID uuid = new UUID(0, 0); if (mFwMajor >= 3) { uuid = getUUID(buf); id = (int) (uuid.getLeastSignificantBits() & 0xffffffffL); } else { id = buf.getInt(); } byte action = buf.get(); if (action >= 0x00 && action <= 0x05) { GBDeviceEventNotificationControl devEvtNotificationControl = new GBDeviceEventNotificationControl(); devEvtNotificationControl.handle = id; String caption = "undefined"; int icon_id = 1; boolean needsAck2x = true; switch (action) { case 0x01: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.OPEN; caption = "Opened"; icon_id = PebbleIconID.DURING_PHONE_CALL; break; case 0x02: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS; caption = "Dismissed"; icon_id = PebbleIconID.RESULT_DISMISSED; needsAck2x = false; break; case 0x03: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.DISMISS_ALL; caption = "All dismissed"; icon_id = PebbleIconID.RESULT_DISMISSED; needsAck2x = false; break; case 0x04: devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.MUTE; caption = "Muted"; icon_id = PebbleIconID.RESULT_MUTE; break; case 0x05: case 0x00: boolean failed = true; byte attribute_count = buf.get(); if (attribute_count > 0) { byte attribute = buf.get(); if (attribute == 0x01) { // reply string is in attribute 0x01 short length = buf.getShort(); if (length > 64) length = 64; byte[] reply = new byte[length]; buf.get(reply); devEvtNotificationControl.phoneNumber = null; if (buf.remaining() > 1 && buf.get() == 0x0c) { short phoneNumberLength = buf.getShort(); byte[] phoneNumberBytes = new byte[phoneNumberLength]; buf.get(phoneNumberBytes); devEvtNotificationControl.phoneNumber = new String(phoneNumberBytes); } devEvtNotificationControl.event = GBDeviceEventNotificationControl.Event.REPLY; devEvtNotificationControl.reply = new String(reply); caption = "SENT"; icon_id = PebbleIconID.RESULT_SENT; failed = false; } } if (failed) { caption = "FAILED"; icon_id = PebbleIconID.RESULT_FAILED; devEvtNotificationControl = null; // error } break; } GBDeviceEventSendBytes sendBytesAck = null; if (mFwMajor >= 3 || needsAck2x) { sendBytesAck = new GBDeviceEventSendBytes(); if (mFwMajor >= 3) { sendBytesAck.encodedBytes = encodeActionResponse(uuid, icon_id, caption); } else { sendBytesAck.encodedBytes = encodeActionResponse2x(id, action, 6, caption); } } return new GBDeviceEvent[] { sendBytesAck, devEvtNotificationControl }; } LOG.info("unexpected action: " + action); } return null; }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private GBDeviceEvent[] decodeDictToJSONAppMessage(UUID uuid, ByteBuffer buf) throws JSONException { buf.order(ByteOrder.LITTLE_ENDIAN); byte dictSize = buf.get(); if (dictSize == 0) { LOG.info("dict size is 0, ignoring"); return null; }//from w ww . j a va 2s.c om JSONArray jsonArray = new JSONArray(); while (dictSize-- > 0) { JSONObject jsonObject = new JSONObject(); Integer key = buf.getInt(); byte type = buf.get(); short length = buf.getShort(); jsonObject.put("key", key); if (type == TYPE_CSTRING) { length--; } jsonObject.put("length", length); switch (type) { case TYPE_UINT: jsonObject.put("type", "uint"); if (length == 1) { jsonObject.put("value", buf.get() & 0xff); } else if (length == 2) { jsonObject.put("value", buf.getShort() & 0xffff); } else { jsonObject.put("value", buf.getInt() & 0xffffffffL); } break; case TYPE_INT: jsonObject.put("type", "int"); if (length == 1) { jsonObject.put("value", buf.get()); } else if (length == 2) { jsonObject.put("value", buf.getShort()); } else { jsonObject.put("value", buf.getInt()); } break; case TYPE_BYTEARRAY: case TYPE_CSTRING: byte[] bytes = new byte[length]; buf.get(bytes); if (type == TYPE_BYTEARRAY) { jsonObject.put("type", "bytes"); jsonObject.put("value", new String(Base64.encode(bytes, Base64.NO_WRAP))); } else { jsonObject.put("type", "string"); jsonObject.put("value", new String(bytes)); buf.get(); // skip null-termination; } break; default: LOG.info("unknown type in appmessage, ignoring"); return null; } jsonArray.put(jsonObject); } GBDeviceEventSendBytes sendBytesAck = null; if (mAlwaysACKPebbleKit) { // this is a hack we send an ack to the Pebble immediately because somebody said it helps some PebbleKit apps :P sendBytesAck = new GBDeviceEventSendBytes(); sendBytesAck.encodedBytes = encodeApplicationMessageAck(uuid, last_id); } GBDeviceEventAppMessage appMessage = new GBDeviceEventAppMessage(); appMessage.appUUID = uuid; appMessage.id = last_id & 0xff; appMessage.message = jsonArray.toString(); return new GBDeviceEvent[] { appMessage, sendBytesAck }; }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodeBlobdb(Object key, byte command, byte db, byte[] blob) { int length = 5; int key_length; if (key instanceof UUID) { key_length = LENGTH_UUID; } else if (key instanceof String) { key_length = ((String) key).getBytes().length; } else {/*from w w w.ja v a 2 s .c om*/ LOG.warn("unknown key type"); return null; } if (key_length > 255) { LOG.warn("key is too long"); return null; } length += key_length; if (blob != null) { length += blob.length + 2; } ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort((short) length); buf.putShort(ENDPOINT_BLOBDB); buf.order(ByteOrder.LITTLE_ENDIAN); buf.put(command); buf.putShort((short) mRandom.nextInt()); // token buf.put(db); buf.put((byte) key_length); if (key instanceof UUID) { UUID uuid = (UUID) key; buf.order(ByteOrder.BIG_ENDIAN); buf.putLong(uuid.getMostSignificantBits()); buf.putLong(uuid.getLeastSignificantBits()); buf.order(ByteOrder.LITTLE_ENDIAN); } else { buf.put(((String) key).getBytes()); } if (blob != null) { buf.putShort((short) blob.length); buf.put(blob); } return buf.array(); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private ArrayList<Pair<Integer, Object>> decodeDict(ByteBuffer buf) { ArrayList<Pair<Integer, Object>> dict = new ArrayList<>(); buf.order(ByteOrder.LITTLE_ENDIAN); byte dictSize = buf.get(); while (dictSize-- > 0) { Integer key = buf.getInt(); byte type = buf.get(); short length = buf.getShort(); switch (type) { case TYPE_INT: case TYPE_UINT: if (length == 1) { dict.add(new Pair<Integer, Object>(key, buf.get())); } else if (length == 2) { dict.add(new Pair<Integer, Object>(key, buf.getShort())); } else { dict.add(new Pair<Integer, Object>(key, buf.getInt())); }/* w w w.j a v a 2 s. c om*/ break; case TYPE_CSTRING: case TYPE_BYTEARRAY: byte[] bytes = new byte[length]; buf.get(bytes); if (type == TYPE_BYTEARRAY) { dict.add(new Pair<Integer, Object>(key, bytes)); } else { dict.add(new Pair<Integer, Object>(key, new String(bytes))); } break; default: } } return dict; }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodeTimelinePin(UUID uuid, int timestamp, short duration, int icon_id, String title, String subtitle) {//from ww w. ja v a 2 s . c om final short TIMELINE_PIN_LENGTH = 46; icon_id |= 0x80000000; byte attributes_count = 2; byte actions_count = 0; int attributes_length = 10 + title.getBytes().length; if (subtitle != null && !subtitle.isEmpty()) { attributes_length += 3 + subtitle.getBytes().length; attributes_count += 1; } int pin_length = TIMELINE_PIN_LENGTH + attributes_length; ByteBuffer buf = ByteBuffer.allocate(pin_length); // pin - 46 bytes buf.order(ByteOrder.BIG_ENDIAN); buf.putLong(uuid.getMostSignificantBits()); buf.putLong(uuid.getLeastSignificantBits()); buf.putLong(0); // parent buf.putLong(0); buf.order(ByteOrder.LITTLE_ENDIAN); buf.putInt(timestamp); // 32-bit timestamp buf.putShort(duration); buf.put((byte) 0x02); // type (0x02 = pin) buf.putShort((short) 0x0001); // flags 0x0001 = ? buf.put((byte) 0x01); // layout was (0x02 = pin?), 0x01 needed for subtitle but seems to do no harm if there isn't one buf.putShort((short) attributes_length); // total length of all attributes and actions in bytes buf.put(attributes_count); buf.put(actions_count); buf.put((byte) 4); // icon buf.putShort((short) 4); // length of int buf.putInt(icon_id); buf.put((byte) 1); // title buf.putShort((short) title.getBytes().length); buf.put(title.getBytes()); if (subtitle != null && !subtitle.isEmpty()) { buf.put((byte) 2); //subtitle buf.putShort((short) subtitle.getBytes().length); buf.put(subtitle.getBytes()); } return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_PIN, buf.array()); }