List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(int index, int length, Charset charset);
From source file:org.traccar.protocol.MegastekFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }// www . j av a2 s .c o m if (Character.isDigit(buf.getByte(buf.readerIndex()))) { int length = 4 + Integer.parseInt(buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII)); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } else { while (buf.getByte(buf.readerIndex()) == '\r' || buf.getByte(buf.readerIndex()) == '\n') { buf.skipBytes(1); } int delimiter = BufferUtil.indexOf("\r\n", buf); if (delimiter == -1) { delimiter = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '!'); } if (delimiter != -1) { ByteBuf result = buf.readRetainedSlice(delimiter - buf.readerIndex()); buf.skipBytes(1); return result; } } return null; }
From source file:org.traccar.protocol.MeiligaoProtocolDecoder.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.readShort(); // length ByteBuf id = buf.readSlice(7);/* w w w . j a va2 s . c o m*/ int command = buf.readUnsignedShort(); if (command == MSG_LOGIN) { ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 }); sendResponse(channel, remoteAddress, id, MSG_LOGIN_RESPONSE, response); return null; } else if (command == MSG_HEARTBEAT) { ByteBuf response = Unpooled.wrappedBuffer(new byte[] { 0x01 }); sendResponse(channel, remoteAddress, id, MSG_HEARTBEAT, response); return null; } else if (command == MSG_SERVER) { ByteBuf response = Unpooled.copiedBuffer(getServer(channel, ':'), StandardCharsets.US_ASCII); sendResponse(channel, remoteAddress, id, MSG_SERVER, response); return null; } else if (command == MSG_UPLOAD_PHOTO) { byte imageIndex = buf.readByte(); photos.put(imageIndex, Unpooled.buffer()); ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex }); sendResponse(channel, remoteAddress, id, MSG_UPLOAD_PHOTO_RESPONSE, response); return null; } else if (command == MSG_UPLOAD_COMPLETE) { byte imageIndex = buf.readByte(); ByteBuf response = Unpooled.copiedBuffer(new byte[] { imageIndex, 0, 0 }); sendResponse(channel, remoteAddress, id, MSG_RETRANSMISSION, response); return null; } DeviceSession deviceSession = identify(id, channel, remoteAddress); if (deviceSession == null) { return null; } if (command == MSG_DATA_PHOTO) { byte imageIndex = buf.readByte(); buf.readUnsignedShort(); // image footage buf.readUnsignedByte(); // total packets buf.readUnsignedByte(); // packet index photos.get(imageIndex).writeBytes(buf, buf.readableBytes() - 2 - 2); return null; } else if (command == MSG_RETRANSMISSION) { return decodeRetransmission(buf, deviceSession); } else { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); if (command == MSG_ALARM) { short alarmCode = buf.readUnsignedByte(); position.set(Position.KEY_ALARM, decodeAlarm(alarmCode)); if (alarmCode >= 0x02 && alarmCode <= 0x05) { position.set(Position.PREFIX_IN + alarmCode, 1); } else if (alarmCode >= 0x32 && alarmCode <= 0x35) { position.set(Position.PREFIX_IN + (alarmCode - 0x30), 0); } } else if (command == MSG_POSITION_LOGGED) { buf.skipBytes(6); } else if (command == MSG_RFID) { for (int i = 0; i < 15; i++) { long rfid = buf.readUnsignedInt(); if (rfid != 0) { String card = String.format("%010d", rfid); position.set("card" + (i + 1), card); position.set(Position.KEY_DRIVER_UNIQUE_ID, card); } } } else if (command == MSG_POSITION_IMAGE) { byte imageIndex = buf.readByte(); buf.readUnsignedByte(); // image upload type String uniqueId = Context.getIdentityManager().getById(deviceSession.getDeviceId()).getUniqueId(); ByteBuf photo = photos.remove(imageIndex); try { position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); } finally { photo.release(); } } String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 4, StandardCharsets.US_ASCII); switch (command) { case MSG_POSITION: case MSG_POSITION_LOGGED: case MSG_ALARM: case MSG_POSITION_IMAGE: return decodeRegular(position, sentence); case MSG_RFID: return decodeRfid(position, sentence); case MSG_OBD_RT: return decodeObd(position, sentence); case MSG_OBD_RTA: return decodeObdA(position, sentence); default: return null; } } }
From source file:org.traccar.protocol.MeitrackFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.readableBytes() < 10) { return null; }//from w w w .j a va 2 s. co m int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); if (index != -1) { int length = index - buf.readerIndex() + Integer.parseInt( buf.toString(buf.readerIndex() + 3, index - buf.readerIndex() - 3, StandardCharsets.US_ASCII)); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } return null; }
From source file:org.traccar.protocol.MeitrackProtocolDecoder.java
License:Apache License
private List<Position> decodeBinaryC(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { List<Position> positions = new LinkedList<>(); String flag = buf.toString(2, 1, StandardCharsets.US_ASCII); int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ','); String imei = buf.toString(index + 1, 15, StandardCharsets.US_ASCII); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei); if (deviceSession == null) { return null; }/* w ww . j a v a2 s. c om*/ buf.skipBytes(index + 1 + 15 + 1 + 3 + 1 + 2 + 2 + 4); while (buf.readableBytes() >= 0x34) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); position.setLatitude(buf.readIntLE() * 0.000001); position.setLongitude(buf.readIntLE() * 0.000001); position.setTime(new Date((946684800 + buf.readUnsignedIntLE()) * 1000)); // 946684800 = 2000-01-01 position.setValid(buf.readUnsignedByte() == 1); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); int rssi = buf.readUnsignedByte(); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE())); position.setCourse(buf.readUnsignedShortLE()); position.set(Position.KEY_HDOP, buf.readUnsignedShortLE() * 0.1); position.setAltitude(buf.readUnsignedShortLE()); position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); position.set("runtime", buf.readUnsignedIntLE()); position.setNetwork(new Network(CellTower.from(buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), buf.readUnsignedShortLE(), rssi))); position.set(Position.KEY_STATUS, buf.readUnsignedShortLE()); position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.01); position.set(Position.KEY_POWER, buf.readUnsignedShortLE()); buf.readUnsignedIntLE(); // geo-fence positions.add(position); } if (channel != null) { StringBuilder command = new StringBuilder("@@"); command.append(flag).append(27 + positions.size() / 10).append(","); command.append(imei).append(",CCC,").append(positions.size()).append("*"); int checksum = 0; for (int i = 0; i < command.length(); i += 1) { checksum += command.charAt(i); } command.append(String.format("%02x", checksum & 0xff).toUpperCase()); command.append("\r\n"); channel.writeAndFlush(new NetworkMessage(command.toString(), remoteAddress)); // delete processed data } 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 w ww . 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 w w .jav a 2s .co 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.NavisFrameDecoder.java
License:Apache License
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception { if (buf.getByte(buf.readerIndex()) == 0x7F) { return buf.readRetainedSlice(1); // keep alive }// w w w. j a v a 2 s . c o m if (ctx != null && flexDataSize == 0) { NavisProtocolDecoder protocolDecoder = BasePipelineFactory.getHandler(ctx.pipeline(), NavisProtocolDecoder.class); if (protocolDecoder != null) { flexDataSize = protocolDecoder.getFlexDataSize(); } } if (flexDataSize > 0) { if (buf.readableBytes() > FLEX_HEADER_LENGTH) { int length = 0; String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII); switch (type) { // FLEX 1.0 case "~A": length = flexDataSize * buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH) + 1 + 1; break; case "~T": length = flexDataSize + 4 + 1; break; case "~C": length = flexDataSize + 1; break; // FLEX 2.0 (Extra packages) case "~E": length++; for (int i = 0; i < buf.getByte(buf.readerIndex() + FLEX_HEADER_LENGTH); i++) { if (buf.readableBytes() > FLEX_HEADER_LENGTH + length + 1) { length += buf.getUnsignedShort(length + FLEX_HEADER_LENGTH) + 2; } else { return null; } } length++; break; case "~X": length = buf.getUnsignedShortLE(buf.readerIndex() + FLEX_HEADER_LENGTH) + 4 + 1; break; default: return null; } if (buf.readableBytes() >= FLEX_HEADER_LENGTH + length) { return buf.readRetainedSlice(buf.readableBytes()); } } } else { if (buf.readableBytes() < NTCB_HEADER_LENGTH) { return null; } int length = NTCB_HEADER_LENGTH + buf.getUnsignedShortLE(buf.readerIndex() + NTCB_LENGTH_OFFSET); if (buf.readableBytes() >= length) { return buf.readRetainedSlice(length); } } return null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object decodeNtcb(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { prefix = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII); buf.skipBytes(prefix.length()); // prefix @NTC by default serverId = buf.readUnsignedIntLE();//from www .j a v a 2 s .co m deviceUniqueId = buf.readUnsignedIntLE(); int length = buf.readUnsignedShortLE(); buf.skipBytes(2); // header and data XOR checksum if (length == 0) { return null; // keep alive message } String type = buf.toString(buf.readerIndex(), 3, StandardCharsets.US_ASCII); buf.skipBytes(type.length()); if (type.equals("*>S")) { return processHandshake(channel, remoteAddress, buf); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null) { switch (type) { case "*>A": return processNtcbArray(deviceSession, channel, buf); case "*>T": return processNtcbSingle(deviceSession, channel, buf); case "*>F": buf.skipBytes(3); return processFlexNegotiation(channel, buf); default: break; } } } return null; }
From source file:org.traccar.protocol.NavisProtocolDecoder.java
License:Apache License
private Object decodeFlex(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { if (buf.getByte(buf.readerIndex()) == 0x7F) { return null; // keep alive }/*from www. ja va 2 s. c o m*/ String type = buf.toString(buf.readerIndex(), 2, StandardCharsets.US_ASCII); buf.skipBytes(type.length()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null) { switch (type) { // FLEX 1.0 case "~A": return processFlexArray(this::parseFlexPosition, type, deviceSession, channel, buf); case "~T": case "~C": return processFlexSingle(this::parseFlexPosition, type, deviceSession, channel, buf); // FLEX 2.0 (extra packages) case "~E": return processFlexArray(this::parseFlex20Position, type, deviceSession, channel, buf); case "~X": return processFlexSingle(this::parseFlex20Position, type, deviceSession, channel, buf); default: break; } } return null; }
From source file:org.traccar.protocol.NvsProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; if (buf.getUnsignedByte(buf.readerIndex()) == 0) { buf.readUnsignedShort(); // length String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII); if (getDeviceSession(channel, remoteAddress, imei) != null) { sendResponse(channel, remoteAddress, "OK"); } else {/*from w w w . ja v a 2 s .com*/ sendResponse(channel, remoteAddress, "NO01"); } } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } List<Position> positions = new LinkedList<>(); buf.skipBytes(4); // marker buf.readUnsignedShort(); // length buf.readLong(); // imei buf.readUnsignedByte(); // codec int count = buf.readUnsignedByte(); for (int i = 0; i < count; i++) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.setTime(new Date(buf.readUnsignedInt() * 1000)); position.set("reason", buf.readUnsignedByte()); position.setLongitude(buf.readInt() / 10000000.0); position.setLatitude(buf.readInt() / 10000000.0); position.setAltitude(buf.readShort()); position.setCourse(buf.readUnsignedShort()); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort())); position.setValid(buf.readUnsignedByte() != 0); buf.readUnsignedByte(); // used systems buf.readUnsignedByte(); // cause element id // Read 1 byte data int cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedByte()); } // Read 2 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedShort()); } // Read 4 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readUnsignedInt()); } // Read 8 byte data cnt = buf.readUnsignedByte(); for (int j = 0; j < cnt; j++) { position.set(Position.PREFIX_IO + buf.readUnsignedByte(), buf.readLong()); } positions.add(position); } return positions; } return null; }