List of usage examples for io.netty.buffer ByteBuf indexOf
public abstract int indexOf(int fromIndex, int toIndex, byte value);
From source file:org.traccar.protocol.MeitrackProtocolDecoder.java
License:Apache License
private List<Position> decodeBinaryE(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { List<Position> positions = new LinkedList<>(); buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',') + 1); String imei = buf.readSlice(15).toString(StandardCharsets.US_ASCII); buf.skipBytes(1 + 3 + 1);//from ww w.j av a 2s . com DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; } buf.readUnsignedIntLE(); // remaining cache int count = buf.readUnsignedShortLE(); for (int i = 0; i < count; i++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); buf.readUnsignedShortLE(); // length buf.readUnsignedShortLE(); // index int paramCount = buf.readUnsignedByte(); for (int j = 0; j < paramCount; j++) { int id = buf.readUnsignedByte(); switch (id) { case 0x01: position.set(Position.KEY_EVENT, buf.readUnsignedByte()); break; case 0x05: position.setValid(buf.readUnsignedByte() > 0); break; case 0x06: position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); break; case 0x07: position.set(Position.KEY_RSSI, buf.readUnsignedByte()); break; default: buf.readUnsignedByte(); break; } } paramCount = buf.readUnsignedByte(); for (int j = 0; j < paramCount; j++) { int id = buf.readUnsignedByte(); switch (id) { case 0x08: position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE())); break; case 0x09: position.setCourse(buf.readUnsignedShortLE()); break; case 0x0B: position.setAltitude(buf.readShortLE()); break; case 0x19: position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); break; case 0x1A: position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.01); break; default: buf.readUnsignedShortLE(); break; } } paramCount = buf.readUnsignedByte(); for (int j = 0; j < paramCount; j++) { int id = buf.readUnsignedByte(); switch (id) { case 0x02: position.setLatitude(buf.readIntLE() * 0.000001); break; case 0x03: position.setLongitude(buf.readIntLE() * 0.000001); break; case 0x04: position.setTime(new Date((946684800 + buf.readUnsignedIntLE()) * 1000)); // 2000-01-01 break; case 0x0D: position.set("runtime", buf.readUnsignedIntLE()); break; default: buf.readUnsignedIntLE(); break; } } paramCount = buf.readUnsignedByte(); for (int j = 0; j < paramCount; j++) { buf.readUnsignedByte(); // id buf.skipBytes(buf.readUnsignedByte()); // value } positions.add(position); } return positions; }
From source file:org.traccar.protocol.MeitrackProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII); index = buf.indexOf(index + 1, buf.writerIndex(), (byte) ','); String type = buf.toString(index + 1, 3, StandardCharsets.US_ASCII); switch (type) { case "D00": if (photo == null) { photo = Unpooled.buffer();/*from ww w. j a v a 2s . c om*/ } index = index + 1 + type.length() + 1; int endIndex = buf.indexOf(index, buf.writerIndex(), (byte) ','); String file = buf.toString(index, endIndex - index, StandardCharsets.US_ASCII); index = endIndex + 1; endIndex = buf.indexOf(index, buf.writerIndex(), (byte) ','); int total = Integer.parseInt(buf.toString(index, endIndex - index, StandardCharsets.US_ASCII)); index = endIndex + 1; endIndex = buf.indexOf(index, buf.writerIndex(), (byte) ','); int current = Integer.parseInt(buf.toString(index, endIndex - index, StandardCharsets.US_ASCII)); buf.readerIndex(endIndex + 1); photo.writeBytes(buf.readSlice(buf.readableBytes() - 1 - 2 - 2)); if (current == total - 1) { Position position = new Position(getProtocolName()); position.setDeviceId(getDeviceSession(channel, remoteAddress, imei).getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(imei, photo, "jpg")); photo.release(); photo = null; return position; } else { if ((current + 1) % 8 == 0) { requestPhotoPacket(channel, remoteAddress, imei, file, current + 1); } return null; } case "D03": photo = Unpooled.buffer(); requestPhotoPacket(channel, remoteAddress, imei, "camera_picture.jpg", 0); return null; case "CCC": return decodeBinaryC(channel, remoteAddress, buf); case "CCE": return decodeBinaryE(channel, remoteAddress, buf); default: return decodeRegular(channel, remoteAddress, buf); } }
From source file:org.traccar.protocol.Mta6ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest) msg; ByteBuf buf = request.content(); buf.skipBytes("id=".length()); int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '&'); String uniqueId = buf.toString(buf.readerIndex(), index - buf.readerIndex(), StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, uniqueId); if (deviceSession == null) { return null; }//from w ww.j a v a 2 s.c o m buf.skipBytes(uniqueId.length()); buf.skipBytes("&bin=".length()); short packetId = buf.readUnsignedByte(); short offset = buf.readUnsignedByte(); // dataOffset short packetCount = buf.readUnsignedByte(); buf.readUnsignedByte(); // reserved buf.readUnsignedByte(); // timezone buf.skipBytes(offset - 5); if (channel != null) { sendContinue(channel); sendResponse(channel, packetId, packetCount); } if (packetId == 0x31 || packetId == 0x32 || packetId == 0x36) { if (simple) { return parseFormatA1(deviceSession, buf); } else { return parseFormatA(deviceSession, buf); } } // else if (0x34 0x38 0x4F 0x59) return null; }
From source file:org.traccar.protocol.MxtFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 2) { return null; }//from w ww.j av a2 s. c o m int index = buf.indexOf(buf.readerIndex() + 1, buf.writerIndex(), (byte) 0x04); if (index != -1) { ByteBuf result = Unpooled.buffer(index + 1 - buf.readerIndex()); while (buf.readerIndex() <= index) { int b = buf.readUnsignedByte(); if (b == 0x10) { result.writeByte(buf.readUnsignedByte() - 0x20); } else { result.writeByte(b); } } return result; } return null; }
From source file:org.traccar.protocol.Pt502FrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }/*www.ja v a 2 s . c o m*/ if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf && buf.toString(buf.readerIndex() + BINARY_HEADER, 4, StandardCharsets.US_ASCII).equals("$PHD")) { int length = buf.getUnsignedShortLE(buf.readerIndex() + 3); if (buf.readableBytes() >= length) { buf.skipBytes(BINARY_HEADER); ByteBuf result = buf.readRetainedSlice(length - BINARY_HEADER - 2); buf.skipBytes(2); // line break return result; } } else { if (buf.getUnsignedByte(buf.readerIndex()) == 0xbf) { buf.skipBytes(BINARY_HEADER); } int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\r'); if (index < 0) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\n'); } if (index > 0) { ByteBuf result = buf.readRetainedSlice(index - buf.readerIndex()); while (buf.isReadable() && (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n')) { buf.skipBytes(1); } return result; } } return null; }
From source file:org.traccar.protocol.Pt502ProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int typeEndIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String type = buf.toString(buf.readerIndex(), typeEndIndex - buf.readerIndex(), StandardCharsets.US_ASCII); if (type.startsWith("$PHD")) { int dataIndex = buf.indexOf(typeEndIndex + 1, buf.writerIndex(), (byte) ',') + 1; buf.readerIndex(dataIndex);/* w ww . j a v a 2s . c o m*/ if (photo != null) { photo.writeBytes(buf.readSlice(buf.readableBytes())); if (photo.writableBytes() > 0) { requestPhotoFragment(channel); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()) .getUniqueId(); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); getLastLocation(position, null); position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); photo.release(); photo = null; return position; } } } else { if (type.startsWith("$PHO")) { int size = Integer.parseInt(type.split("-")[0].substring(4)); if (size > 0) { photo = Unpooled.buffer(size); requestPhotoFragment(channel); } } return decodePosition(channel, remoteAddress, buf.toString(StandardCharsets.US_ASCII)); } return null; }
From source file:org.traccar.protocol.RetranslatorProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { if (channel != null) { channel.writeAndFlush(new NetworkMessage(Unpooled.wrappedBuffer(new byte[] { 0x11 }), remoteAddress)); }//from ww w . ja va 2 s . co m ByteBuf buf = (ByteBuf) msg; buf.readUnsignedInt(); // length int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII); buf.readByte(); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); buf.readUnsignedInt(); // bit flags while (buf.isReadable()) { buf.readUnsignedShort(); // block type int blockEnd = buf.readInt() + buf.readerIndex(); buf.readUnsignedByte(); // security attribute int dataType = buf.readUnsignedByte(); int nameLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); String name = buf.readBytes(nameLength).toString(StandardCharsets.US_ASCII); buf.readByte(); if (name.equals("posinfo")) { position.setValid(true); position.setLongitude(buf.readDoubleLE()); position.setLatitude(buf.readDoubleLE()); position.setAltitude(buf.readDoubleLE()); position.setSpeed(buf.readShort()); position.setCourse(buf.readShort()); position.set(Position.KEY_SATELLITES, buf.readByte()); } else { switch (dataType) { case 1: int len = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex(); position.set(name, buf.readBytes(len).toString(StandardCharsets.US_ASCII)); buf.readByte(); break; case 3: position.set(name, buf.readInt()); break; case 4: position.set(name, buf.readDoubleLE()); break; case 5: position.set(name, buf.readLong()); break; default: break; } } buf.readerIndex(blockEnd); } if (position.getLatitude() == 0 && position.getLongitude() == 0) { getLastLocation(position, position.getDeviceTime()); } return position; }
From source file:org.traccar.protocol.RitiProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(buf.readUnsignedShort())); if (deviceSession == null) { return null; }/* w w w .j a v a 2 s .c o m*/ position.setDeviceId(deviceSession.getDeviceId()); position.set("mode", buf.readUnsignedByte()); position.set(Position.KEY_COMMAND, buf.readUnsignedByte()); position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001); buf.skipBytes(5); // status buf.readUnsignedShortLE(); // idleCount buf.readUnsignedShortLE(); // idleTime in seconds position.set(Position.KEY_DISTANCE, buf.readUnsignedIntLE()); position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedIntLE()); // Parse GPRMC int end = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); String gprmc = buf.toString(buf.readerIndex(), end - buf.readerIndex(), StandardCharsets.US_ASCII); Parser parser = new Parser(PATTERN, gprmc); if (!parser.matches()) { return null; } 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)); position.setCourse(parser.nextDouble(0)); dateBuilder.setDateReverse(parser.nextInt(0), parser.nextInt(0), parser.nextInt(0)); position.setTime(dateBuilder.getDate()); return position; }
From source file:org.traccar.protocol.SabertekFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { int beginIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); if (beginIndex >= 0) { int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x03); if (endIndex >= 0) { buf.readerIndex(beginIndex + 1); ByteBuf frame = buf.readRetainedSlice(endIndex - beginIndex - 1); buf.readerIndex(endIndex + 1); buf.skipBytes(2); // end line return frame; }/*from www.j a v a 2 s.c o m*/ } return null; }
From source file:org.traccar.protocol.SmokeyProtocolDecoder.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.readUnsignedByte(); // protocol version int type = buf.readUnsignedByte(); ByteBuf id = buf.readSlice(8);/*from w w w. ja va 2s. com*/ DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, ByteBufUtil.hexDump(id)); if (deviceSession == null) { return null; } if (type == MSG_DATE_RECORD) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_FW, buf.readUnsignedShort()); int status = buf.readUnsignedShort(); position.set(Position.KEY_STATUS, status); DateBuilder dateBuilder = new DateBuilder().setDate(2000, 1, 1).addSeconds(buf.readUnsignedInt()); getLastLocation(position, dateBuilder.getDate()); int index = buf.readUnsignedByte(); position.set(Position.KEY_INDEX, index); int report = buf.readUnsignedShort(); buf.readUnsignedShort(); // length position.set(Position.KEY_BATTERY, buf.readUnsignedShort()); Network network = new Network(); if (report != 0x0203) { int count = 1; if (report != 0x0200) { count = buf.readUnsignedByte(); } for (int i = 0; i < count; i++) { int mcc = buf.readUnsignedShort(); int mnc = buf.readUnsignedShort(); int lac = buf.readUnsignedShort(); int cid = buf.readUnsignedShort(); if (i == 0) { buf.readByte(); // timing advance } int rssi = buf.readByte(); network.addCellTower(CellTower.from(mcc, mnc, lac, cid, rssi)); } } if (report == 0x0202 || report == 0x0203) { int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { buf.readerIndex(buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0) + 1); // ssid String mac = String.format("%02x:%02x:%02x:%02x:%02x:%02x", buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); network.addWifiAccessPoint(WifiAccessPoint.from(mac, buf.readByte())); } } position.setNetwork(network); sendResponse(channel, remoteAddress, id, index, report); return position; } return null; }