List of usage examples for io.netty.buffer ByteBuf getByte
public abstract byte getByte(int index);
From source file:org.traccar.protocol.KhdProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; buf.skipBytes(2); // header int type = buf.readUnsignedByte(); buf.readUnsignedShort(); // size if (type == MSG_LOGIN || type == MSG_ADMIN_NUMBER || type == MSG_SEND_TEXT || type == MSG_SMS_ALARM_SWITCH || type == MSG_POSITION_REUPLOAD) { ByteBuf response = Unpooled.buffer(); response.writeByte(0x29);// w ww . j av a 2 s. c o m response.writeByte(0x29); // header response.writeByte(MSG_CONFIRMATION); response.writeShort(5); // size response.writeByte(buf.getByte(buf.writerIndex() - 2)); response.writeByte(type); response.writeByte(buf.writerIndex() > 9 ? buf.getByte(9) : 0); // 10th byte response.writeByte(Checksum.xor(response.nioBuffer())); response.writeByte(0x0D); // ending if (channel != null) { channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } } if (type == MSG_ON_DEMAND || type == MSG_POSITION_UPLOAD || type == MSG_POSITION_REUPLOAD || type == MSG_ALARM || type == MSG_REPLY || type == MSG_PERIPHERAL) { Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, readSerialNumber(buf)); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setYear(BcdUtil.readInteger(buf, 2)) .setMonth(BcdUtil.readInteger(buf, 2)).setDay(BcdUtil.readInteger(buf, 2)) .setHour(BcdUtil.readInteger(buf, 2)).setMinute(BcdUtil.readInteger(buf, 2)) .setSecond(BcdUtil.readInteger(buf, 2)); position.setTime(dateBuilder.getDate()); position.setLatitude(BcdUtil.readCoordinate(buf)); position.setLongitude(BcdUtil.readCoordinate(buf)); position.setSpeed(UnitsConverter.knotsFromKph(BcdUtil.readInteger(buf, 4))); position.setCourse(BcdUtil.readInteger(buf, 4)); position.setValid((buf.readUnsignedByte() & 0x80) != 0); if (type != MSG_ALARM) { position.set(Position.KEY_ODOMETER, buf.readUnsignedMedium()); position.set(Position.KEY_STATUS, buf.readUnsignedInt()); position.set(Position.KEY_HDOP, buf.readUnsignedByte()); position.set(Position.KEY_VDOP, buf.readUnsignedByte()); position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); buf.skipBytes(5); // other location data if (type == MSG_PERIPHERAL) { buf.readUnsignedShort(); // data length int dataType = buf.readUnsignedByte(); buf.readUnsignedByte(); // content length switch (dataType) { case 0x01: position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 100 + buf.readUnsignedByte()); break; case 0x02: position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() * 100 + buf.readUnsignedByte()); break; default: break; } } } return position; } return null; }
From source file:org.traccar.protocol.L100FrameDecoder.java
License:Apache License
private Object decodeOld(ByteBuf buf) { int header = buf.getByte(buf.readerIndex()); boolean obd = header == 'L' || header == 'H'; int index;// w w w. j a va 2 s . c o m if (obd) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '*'); } else { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x02); if (index < 0) { index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x04); if (index < 0) { return null; } } } index += 2; // checksum if (buf.writerIndex() >= index) { if (!obd) { buf.skipBytes(2); // header } ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex() - 2); buf.skipBytes(2); // footer return frame; } return null; }
From source file:org.traccar.protocol.M2mProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; // Remove offset for (int i = 0; i < buf.readableBytes(); i++) { int b = buf.getByte(i); if (b != 0x0b) { buf.setByte(i, b - 0x20);//w w w. j ava 2 s. c o m } } if (firstPacket) { firstPacket = false; StringBuilder imei = new StringBuilder(); for (int i = 0; i < 8; i++) { int b = buf.readByte(); if (i != 0) { imei.append(b / 10); } imei.append(b % 10); } getDeviceSession(channel, remoteAddress, imei.toString()); } else { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; } Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte() & 0x3f) .setMonth(buf.readUnsignedByte() & 0x3f).setYear(buf.readUnsignedByte()) .setHour(buf.readUnsignedByte() & 0x3f).setMinute(buf.readUnsignedByte() & 0x7f) .setSecond(buf.readUnsignedByte() & 0x7f); position.setTime(dateBuilder.getDate()); int degrees = buf.readUnsignedByte(); double latitude = buf.readUnsignedByte(); latitude += buf.readUnsignedByte() / 100.0; latitude += buf.readUnsignedByte() / 10000.0; latitude /= 60; latitude += degrees; int b = buf.readUnsignedByte(); degrees = (b & 0x7f) * 100 + buf.readUnsignedByte(); double longitude = buf.readUnsignedByte(); longitude += buf.readUnsignedByte() / 100.0; longitude += buf.readUnsignedByte() / 10000.0; longitude /= 60; longitude += degrees; if ((b & 0x80) != 0) { longitude = -longitude; } if ((b & 0x40) != 0) { latitude = -latitude; } position.setValid(true); position.setLatitude(latitude); position.setLongitude(longitude); position.setSpeed(buf.readUnsignedByte()); int satellites = buf.readUnsignedByte(); if (satellites == 0) { return null; // cell information } position.set(Position.KEY_SATELLITES, satellites); // decode other data return position; } return null; }
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; }// w w w. j a va 2 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.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 }/*from w w w. ja va2 s. com*/ 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 decodeFlex(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { if (buf.getByte(buf.readerIndex()) == 0x7F) { return null; // keep alive }//from www .ja v a2 s . co 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.PiligrimProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { FullHttpRequest request = (FullHttpRequest) msg; String uri = request.uri();/* w w w. j a va2 s . co m*/ if (uri.startsWith("/config")) { sendResponse(channel, "CONFIG: OK"); } else if (uri.startsWith("/addlog")) { sendResponse(channel, "ADDLOG: OK"); } else if (uri.startsWith("/inform")) { sendResponse(channel, "INFORM: OK"); } else if (uri.startsWith("/bingps")) { sendResponse(channel, "BINGPS: OK"); QueryStringDecoder decoder = new QueryStringDecoder(request.uri()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, decoder.parameters().get("imei").get(0)); if (deviceSession == null) { return null; } List<Position> positions = new LinkedList<>(); ByteBuf buf = request.content(); while (buf.readableBytes() > 2) { buf.readUnsignedByte(); // header int type = buf.readUnsignedByte(); buf.readUnsignedByte(); // length if (type == MSG_GPS || type == MSG_GPS_SENSORS) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); DateBuilder dateBuilder = new DateBuilder().setDay(buf.readUnsignedByte()) .setMonth(buf.getByte(buf.readerIndex()) & 0x0f) .setYear(2010 + (buf.readUnsignedByte() >> 4)) .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()); position.setTime(dateBuilder.getDate()); double latitude = buf.readUnsignedByte(); latitude += buf.readUnsignedByte() / 60.0; latitude += buf.readUnsignedByte() / 6000.0; latitude += buf.readUnsignedByte() / 600000.0; double longitude = buf.readUnsignedByte(); longitude += buf.readUnsignedByte() / 60.0; longitude += buf.readUnsignedByte() / 6000.0; longitude += buf.readUnsignedByte() / 600000.0; int flags = buf.readUnsignedByte(); if (BitUtil.check(flags, 0)) { latitude = -latitude; } if (BitUtil.check(flags, 1)) { longitude = -longitude; } position.setLatitude(latitude); position.setLongitude(longitude); int satellites = buf.readUnsignedByte(); position.set(Position.KEY_SATELLITES, satellites); position.setValid(satellites >= 3); position.setSpeed(buf.readUnsignedByte()); double course = buf.readUnsignedByte() << 1; course += (flags >> 2) & 1; course += buf.readUnsignedByte() / 100.0; position.setCourse(course); if (type == MSG_GPS_SENSORS) { double power = buf.readUnsignedByte(); power += buf.readUnsignedByte() << 8; position.set(Position.KEY_POWER, power * 0.01); double battery = buf.readUnsignedByte(); battery += buf.readUnsignedByte() << 8; position.set(Position.KEY_BATTERY, battery * 0.01); buf.skipBytes(6); } positions.add(position); } else if (type == MSG_EVENTS) { buf.skipBytes(13); } } return positions; } 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; }/*from ww w . j a 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.TeltonikaProtocolDecoder.java
License:Apache License
private void decodeSerial(Channel channel, SocketAddress remoteAddress, Position position, ByteBuf buf) { getLastLocation(position, null);/*from ww w . j a va 2 s. c om*/ int type = buf.readUnsignedByte(); if (type == 0x0D) { buf.readInt(); // length int subtype = buf.readUnsignedByte(); if (subtype == 0x01) { long photoId = buf.readUnsignedInt(); ByteBuf photo = Unpooled.buffer(buf.readInt()); photos.put(photoId, photo); sendImageRequest(channel, remoteAddress, photoId, 0, Math.min(IMAGE_PACKET_MAX, photo.capacity())); } else if (subtype == 0x02) { long photoId = buf.readUnsignedInt(); buf.readInt(); // offset ByteBuf photo = photos.get(photoId); photo.writeBytes(buf, buf.readUnsignedShort()); if (photo.writableBytes() > 0) { sendImageRequest(channel, remoteAddress, photoId, photo.writerIndex(), Math.min(IMAGE_PACKET_MAX, photo.writableBytes())); } else { String uniqueId = Context.getIdentityManager().getById(position.getDeviceId()).getUniqueId(); photos.remove(photoId); try { position.set(Position.KEY_IMAGE, Context.getMediaManager().writeFile(uniqueId, photo, "jpg")); } finally { photo.release(); } } } } else { position.set(Position.KEY_TYPE, type); int length = buf.readInt(); boolean readable = true; for (int i = 0; i < length; i++) { byte b = buf.getByte(buf.readerIndex() + i); if (b < 32 && b != '\r' && b != '\n') { readable = false; break; } } if (readable) { position.set(Position.KEY_RESULT, buf.readSlice(length).toString(StandardCharsets.US_ASCII)); } else { position.set(Position.KEY_RESULT, ByteBufUtil.hexDump(buf.readSlice(length))); } } }
From source file:org.traccar.protocol.TmgFrameDecoder.java
License:Apache License
private int findHeader(ByteBuf buffer) { int guessedIndex = buffer.indexOf(buffer.readerIndex(), buffer.writerIndex(), (byte) '$'); while (guessedIndex != -1 && buffer.writerIndex() - guessedIndex >= 5) { if (buffer.getByte(guessedIndex + 4) == ',' && isLetter(buffer.getByte(guessedIndex + 1)) && isLetter(buffer.getByte(guessedIndex + 2)) && isLetter(buffer.getByte(guessedIndex + 3))) { return guessedIndex; }//from w w w . j a va 2 s . c o m guessedIndex = buffer.indexOf(guessedIndex, buffer.writerIndex(), (byte) '$'); } return -1; }