List of usage examples for io.netty.buffer ByteBuf readSlice
public abstract ByteBuf readSlice(int length);
From source file:org.traccar.protocol.Tk102ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(1); // header int type = buf.readUnsignedByte(); ByteBuf dataSequence = buf.readSlice(10); int length = buf.readUnsignedByte(); if (type == MSG_LOGIN_REQUEST || type == MSG_LOGIN_REQUEST_2) { ByteBuf data = buf.readSlice(length); String id;/* ww w .ja v a2 s.c o m*/ if (type == MSG_LOGIN_REQUEST) { id = data.toString(StandardCharsets.US_ASCII); } else { id = data.copy(1, 15).toString(StandardCharsets.US_ASCII); } if (getDeviceSession(channel, remoteAddress, id) != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(MODE_GPRS); response.writeBytes(data); sendResponse(channel, MSG_LOGIN_RESPONSE, dataSequence, response); } } else if (type == MSG_HEARTBEAT_REQUEST) { sendResponse(channel, MSG_HEARTBEAT_RESPONSE, dataSequence, buf.readRetainedSlice(length)); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } Parser parser = new Parser(PATTERN, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); if (!parser.matches()) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setTime(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setValid(parser.next().equals("A")); position.setLatitude(parser.nextCoordinate()); position.setLongitude(parser.nextCoordinate()); position.setSpeed(parser.nextDouble(0)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; } return null; }
From source file:org.traccar.protocol.TlvProtocolDecoder.java
License:Apache License
private String readArgument(ByteBuf buf) { return buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII); }
From source file:org.traccar.protocol.TlvProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; String type = buf.readSlice(2).toString(StandardCharsets.US_ASCII); if (channel != null) { switch (type) { case "0A": case "0C": sendResponse(channel, remoteAddress, type); break; case "0B": sendResponse(channel, remoteAddress, type, "1482202689", "10", "20", "15"); break; case "0E": case "0F": sendResponse(channel, remoteAddress, type, "30", "Unknown"); break; default:/*from ww w .ja v a2 s.c om*/ break; } } if (type.equals("0E")) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readArgument(buf)); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setValid(true); position.setTime(new Date(Long.parseLong(readArgument(buf)) * 1000)); readArgument(buf); // location identifier position.setLongitude(Double.parseDouble(readArgument(buf))); position.setLatitude(Double.parseDouble(readArgument(buf))); position.setSpeed(UnitsConverter.knotsFromKph(Double.parseDouble(readArgument(buf)))); position.setCourse(Double.parseDouble(readArgument(buf))); position.set(Position.KEY_SATELLITES, Integer.parseInt(readArgument(buf))); return position; } return null; }
From source file:org.traccar.protocol.TytanProtocolDecoder.java
License:Apache License
private void decodeExtraData(Position position, ByteBuf buf, int end) { while (buf.readerIndex() < end) { int type = buf.readUnsignedByte(); int length = buf.readUnsignedByte(); if (length == 255) { length += buf.readUnsignedByte(); }/*ww w. ja va 2 s .co m*/ int n; switch (type) { case 2: position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedMedium()); break; case 5: position.set(Position.KEY_INPUT, buf.readUnsignedByte()); break; case 6: n = buf.readUnsignedByte() >> 4; if (n < 2) { position.set(Position.PREFIX_ADC + n, buf.readFloat()); } else { position.set("di" + (n - 2), buf.readFloat()); } break; case 7: int alarm = buf.readUnsignedByte(); buf.readUnsignedByte(); if (BitUtil.check(alarm, 5)) { position.set(Position.KEY_ALARM, Position.ALARM_GENERAL); } break; case 8: position.set("antihijack", buf.readUnsignedByte()); break; case 9: position.set("unauthorized", ByteBufUtil.hexDump(buf.readSlice(8))); break; case 10: position.set("authorized", ByteBufUtil.hexDump(buf.readSlice(8))); break; case 24: for (int i = 0; i < length / 2; i++) { position.set(Position.PREFIX_TEMP + buf.readUnsignedByte(), buf.readByte()); } break; case 28: position.set(Position.KEY_AXLE_WEIGHT, buf.readUnsignedShort()); buf.readUnsignedByte(); break; case 90: position.set(Position.KEY_POWER, buf.readFloat()); break; case 101: position.set(Position.KEY_OBD_SPEED, buf.readUnsignedByte()); break; case 102: position.set(Position.KEY_RPM, buf.readUnsignedByte() * 50); break; case 107: int fuel = buf.readUnsignedShort(); int fuelFormat = fuel >> 14; if (fuelFormat == 1) { position.set("fuelValue", (fuel & 0x3fff) * 0.4 + "%"); } else if (fuelFormat == 2) { position.set("fuelValue", (fuel & 0x3fff) * 0.5 + " l"); } else if (fuelFormat == 3) { position.set("fuelValue", (fuel & 0x3fff) * -0.5 + " l"); } break; case 108: position.set(Position.KEY_OBD_ODOMETER, buf.readUnsignedInt() * 5); break; case 150: position.set(Position.KEY_DOOR, buf.readUnsignedByte()); break; default: buf.skipBytes(length); break; } } }
From source file:org.traccar.protocol.TzoneProtocolDecoder.java
License:Apache License
private void decodeCards(Position position, ByteBuf buf) { int index = 1; for (int i = 0; i < 4; i++) { int blockLength = buf.readUnsignedShort(); int blockEnd = buf.readerIndex() + blockLength; if (blockLength > 0) { int count = buf.readUnsignedByte(); for (int j = 0; j < count; j++) { int length = buf.readUnsignedByte(); boolean odd = length % 2 != 0; if (odd) { length += 1;/*from w w w .j a v a 2 s. co m*/ } String num = ByteBufUtil.hexDump(buf.readSlice(length / 2)); if (odd) { num = num.substring(1); } position.set("card" + index, num); } } buf.readerIndex(blockEnd); } }
From source file:org.traccar.protocol.TzoneProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header buf.readUnsignedShort(); // length if (buf.readUnsignedShort() != 0x2424) { return null; }/*from ww w . j a v a 2 s. c o m*/ int hardware = buf.readUnsignedShort(); long firmware = buf.readUnsignedInt(); String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_HW, hardware); position.set(Position.KEY_VERSION_FW, firmware); position.setDeviceTime( new DateBuilder().setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()).getDate()); // GPS info if (hardware == 0x406 || !decodeGps(position, buf, hardware)) { getLastLocation(position, position.getDeviceTime()); } // LBS info int blockLength = buf.readUnsignedShort(); int blockEnd = buf.readerIndex() + blockLength; if (blockLength > 0 && (hardware == 0x10A || hardware == 0x10B || hardware == 0x406)) { position.setNetwork( new Network(CellTower.fromLacCid(buf.readUnsignedShort(), buf.readUnsignedShort()))); } buf.readerIndex(blockEnd); // Status info blockLength = buf.readUnsignedShort(); blockEnd = buf.readerIndex() + blockLength; if (blockLength >= 13) { position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte())); position.set("terminalInfo", buf.readUnsignedByte()); int status = buf.readUnsignedByte(); position.set(Position.PREFIX_OUT + 1, BitUtil.check(status, 0)); position.set(Position.PREFIX_OUT + 2, BitUtil.check(status, 1)); status = buf.readUnsignedByte(); position.set(Position.PREFIX_IN + 1, BitUtil.check(status, 4)); if (BitUtil.check(status, 0)) { position.set(Position.KEY_ALARM, Position.ALARM_SOS); } position.set(Position.KEY_RSSI, buf.readUnsignedByte()); position.set("gsmStatus", buf.readUnsignedByte()); position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); position.set(Position.KEY_POWER, buf.readUnsignedShort()); position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShort()); position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShort()); } if (blockLength >= 15) { position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedShort()); } buf.readerIndex(blockEnd); if (hardware == 0x10B) { decodeCards(position, buf); buf.skipBytes(buf.readUnsignedShort()); // temperature buf.skipBytes(buf.readUnsignedShort()); // lock decodePassengers(position, buf); } if (hardware == 0x406) { decodeTags(position, buf); } return position; }
From source file:org.traccar.protocol.UlbotechProtocolDecoder.java
License:Apache License
private void decodeObd(Position position, ByteBuf buf, int length) { int end = buf.readerIndex() + length; while (buf.readerIndex() < end) { int parameterLength = buf.getUnsignedByte(buf.readerIndex()) >> 4; int mode = buf.readUnsignedByte() & 0x0F; position.add(ObdDecoder.decode(mode, ByteBufUtil.hexDump(buf.readSlice(parameterLength - 1)))); }//from ww w . ja v a 2 s. co m }
From source file:org.traccar.protocol.UlbotechProtocolDecoder.java
License:Apache License
private void decodeJ1708(Position position, ByteBuf buf, int length) { int end = buf.readerIndex() + length; while (buf.readerIndex() < end) { int mark = buf.readUnsignedByte(); int len = BitUtil.between(mark, 0, 6); int type = BitUtil.between(mark, 6, 8); int id = buf.readUnsignedByte(); if (type == 3) { id += 256;//from w w w . j a v a 2 s. c o m } String value = ByteBufUtil.hexDump(buf.readSlice(len - 1)); if (type == 2 || type == 3) { position.set("pid" + id, value); } } }
From source file:org.traccar.protocol.UlbotechProtocolDecoder.java
License:Apache License
private Object decodeBinary(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { buf.readUnsignedByte(); // header buf.readUnsignedByte(); // version buf.readUnsignedByte(); // type String imei = ByteBufUtil.hexDump(buf.readSlice(8)).substring(1); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; }/*ww w .ja v a 2 s.co m*/ if (deviceSession.getTimeZone() == null) { deviceSession.setTimeZone(getTimeZone(deviceSession.getDeviceId())); } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); long seconds = buf.readUnsignedInt() & 0x7fffffffL; seconds += 946684800L; // 2000-01-01 00:00 seconds -= deviceSession.getTimeZone().getRawOffset() / 1000; Date time = new Date(seconds * 1000); boolean hasLocation = false; while (buf.readableBytes() > 3) { int type = buf.readUnsignedByte(); int length = type == DATA_CANBUS ? buf.readUnsignedShort() : buf.readUnsignedByte(); switch (type) { case DATA_GPS: hasLocation = true; position.setValid(true); position.setLatitude(buf.readInt() / 1000000.0); position.setLongitude(buf.readInt() / 1000000.0); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.setCourse(buf.readUnsignedShort()); position.set(Position.KEY_HDOP, buf.readUnsignedShort()); break; case DATA_LBS: if (length == 11) { position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedInt(), -buf.readUnsignedByte()))); } else { position.setNetwork(new Network(CellTower.from(buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), buf.readUnsignedShort(), -buf.readUnsignedByte()))); } if (length > 9 && length != 11) { buf.skipBytes(length - 9); } break; case DATA_STATUS: int status = buf.readUnsignedShort(); position.set(Position.KEY_IGNITION, BitUtil.check(status, 9)); position.set(Position.KEY_STATUS, status); position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedShort())); break; case DATA_ODOMETER: position.set(Position.KEY_ODOMETER, buf.readUnsignedInt()); break; case DATA_ADC: decodeAdc(position, buf, length); break; case DATA_GEOFENCE: position.set("geofenceIn", buf.readUnsignedInt()); position.set("geofenceAlarm", buf.readUnsignedInt()); break; case DATA_OBD2: decodeObd(position, buf, length); break; case DATA_FUEL: position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedInt() / 10000.0); break; case DATA_OBD2_ALARM: decodeObd(position, buf, length); break; case DATA_HARSH_DRIVER: decodeDriverBehavior(position, buf); break; case DATA_CANBUS: position.set("can", ByteBufUtil.hexDump(buf.readSlice(length))); break; case DATA_J1708: decodeJ1708(position, buf, length); break; case DATA_VIN: position.set(Position.KEY_VIN, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); break; case DATA_RFID: position.set(Position.KEY_DRIVER_UNIQUE_ID, buf.readSlice(length - 1).toString(StandardCharsets.US_ASCII)); position.set("authorized", buf.readUnsignedByte() != 0); break; case DATA_EVENT: position.set(Position.KEY_EVENT, buf.readUnsignedByte()); if (length > 1) { position.set("eventMask", buf.readUnsignedInt()); } break; default: buf.skipBytes(length); break; } } if (!hasLocation) { getLastLocation(position, time); } else { position.setTime(time); } return position; }
From source file:org.traccar.protocol.UproProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; if (buf.getByte(buf.readerIndex()) != '*') { return null; }/*from w w w . j av a2s .c om*/ int headerIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); if (headerIndex < 0) { headerIndex = buf.writerIndex(); } String header = buf.readSlice(headerIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN_HEADER, header); if (!parser.matches()) { return null; } String head = parser.next(); boolean reply = parser.next().equals("1"); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, parser.next()); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); String type = parser.next(); String subtype = parser.next(); if (reply && channel != null) { channel.writeAndFlush(new NetworkMessage("*" + head + "Y" + type + subtype + "#", remoteAddress)); } while (buf.isReadable()) { buf.readByte(); // skip delimiter byte dataType = buf.readByte(); int delimiterIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); if (delimiterIndex < 0) { delimiterIndex = buf.writerIndex(); } ByteBuf data = buf.readSlice(delimiterIndex - buf.readerIndex()); switch (dataType) { case 'A': decodeLocation(position, data.toString(StandardCharsets.US_ASCII)); break; case 'B': position.set(Position.KEY_STATUS, data.toString(StandardCharsets.US_ASCII)); break; case 'C': long odometer = 0; while (data.isReadable()) { odometer <<= 4; odometer += data.readByte() - (byte) '0'; } position.set(Position.KEY_ODOMETER, odometer * 2 * 1852 / 3600); break; case 'F': position.setSpeed(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1); break; case 'K': position.set("statusExtended", data.toString(StandardCharsets.US_ASCII)); break; case 'P': if (data.readableBytes() >= 16) { position.setNetwork(new Network( CellTower.from(Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16), Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII), 16)))); } break; case 'Q': position.set("obdPid", ByteBufUtil.hexDump(data)); break; case 'R': if (head.startsWith("HQ")) { position.set(Position.KEY_RSSI, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); position.set(Position.KEY_SATELLITES, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); } else { position.set("odbTravel", ByteBufUtil.hexDump(data)); } break; case 'S': position.set("obdTraffic", ByteBufUtil.hexDump(data)); break; case 'T': position.set(Position.KEY_BATTERY_LEVEL, Integer.parseInt(data.readSlice(2).toString(StandardCharsets.US_ASCII))); break; case 'V': position.set(Position.KEY_POWER, Integer.parseInt(data.readSlice(4).toString(StandardCharsets.US_ASCII)) * 0.1); break; default: break; } } if (position.getLatitude() != 0 && position.getLongitude() != 0) { return position; } return null; }