List of usage examples for io.netty.buffer ByteBuf writeShortLE
public abstract ByteBuf writeShortLE(int value);
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, SocketAddress remoteAddress, ByteBuf id, short type) { if (channel != null) { int length = 2 + 2 + id.readableBytes() + 2 + 4 + 8 + 2 + 2; ByteBuf response = Unpooled.buffer(length); response.writeByte('@'); response.writeByte('@'); response.writeShortLE(length); response.writeBytes(id);/*from ww w . j a va2 s . c om*/ response.writeShort(type); response.writeIntLE(0); for (int i = 0; i < 8; i++) { response.writeByte(0xff); } response.writeShortLE( Checksum.crc16(Checksum.CRC16_X25, response.nioBuffer(0, response.writerIndex()))); response.writeByte(0x0D); response.writeByte(0x0A); channel.writeAndFlush(new NetworkMessage(response, remoteAddress)); } }
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private Object decodeSc(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id, short type, DeviceSession deviceSession) { if (type == MSG_SC_HEARTBEAT) { sendResponse(channel, remoteAddress, version, id, MSG_SC_HEARTBEAT_RESPONSE, null); } else if (type == MSG_SC_LOGIN || type == MSG_SC_LOGOUT || type == MSG_SC_GPS || type == MSG_SC_ALARM || type == MSG_SC_CURRENT_LOCATION || type == MSG_SC_FUEL) { if (type == MSG_SC_LOGIN) { ByteBuf response = Unpooled.buffer(10); response.writeIntLE(0xFFFFFFFF); response.writeShortLE(0); response.writeIntLE((int) (System.currentTimeMillis() / 1000)); sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response); }/*from w w w . ja v a 2 s .co m*/ if (type == MSG_SC_GPS) { buf.readUnsignedByte(); // historical } else if (type == MSG_SC_ALARM) { buf.readUnsignedIntLE(); // alarm } else if (type == MSG_SC_CURRENT_LOCATION) { buf.readUnsignedShortLE(); } buf.readUnsignedIntLE(); // ACC ON time buf.readUnsignedIntLE(); // UTC time long odometer = buf.readUnsignedIntLE(); long tripOdometer = buf.readUnsignedIntLE(); long fuelConsumption = buf.readUnsignedIntLE(); buf.readUnsignedShortLE(); // current fuel consumption long status = buf.readUnsignedIntLE(); buf.skipBytes(8); int count = buf.readUnsignedByte(); List<Position> positions = new LinkedList<>(); for (int i = 0; i < count; i++) { Position position = readPosition(deviceSession, buf); position.set(Position.KEY_ODOMETER, odometer); position.set(Position.KEY_ODOMETER_TRIP, tripOdometer); position.set(Position.KEY_FUEL_CONSUMPTION, fuelConsumption); position.set(Position.KEY_STATUS, status); positions.add(position); } if (type == MSG_SC_ALARM) { int alarmCount = buf.readUnsignedByte(); for (int i = 0; i < alarmCount; i++) { if (buf.readUnsignedByte() != 0) { int alarm = buf.readUnsignedByte(); for (Position position : positions) { decodeAlarm(position, alarm); } buf.readUnsignedShortLE(); // description buf.readUnsignedShortLE(); // threshold } } } else if (type == MSG_SC_FUEL) { for (Position position : positions) { position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); } } if (!positions.isEmpty()) { return positions; } } else if (type == MSG_SC_GPS_SLEEP) { buf.readUnsignedIntLE(); // device time return readPosition(deviceSession, buf); } else if (type == MSG_SC_AGPS_REQUEST) { return readPosition(deviceSession, buf); } else if (type == MSG_SC_PID_DATA) { Position position = createPosition(deviceSession); decodeStat(position, buf); buf.readUnsignedShortLE(); // sample rate decodeObd(position, buf, true); return position; } else if (type == MSG_SC_DTCS_PASSENGER) { Position position = createPosition(deviceSession); decodeStat(position, buf); buf.readUnsignedByte(); // flag position.add(ObdDecoder.decodeCodes(ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())))); return position; } else if (type == MSG_SC_OBD_DATA) { Position position = createPosition(deviceSession); decodeStat(position, buf); buf.readUnsignedByte(); // flag decodeObd(position, buf, false); return position; } else if (type == MSG_SC_CELL) { Position position = createPosition(deviceSession); decodeStat(position, buf); position.setNetwork( new Network(CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE()))); return position; } else if (type == MSG_SC_QUERY_RESPONSE) { Position position = createPosition(deviceSession); buf.readUnsignedShortLE(); // index buf.readUnsignedByte(); // response count buf.readUnsignedByte(); // response index int failureCount = buf.readUnsignedByte(); for (int i = 0; i < failureCount; i++) { buf.readUnsignedShortLE(); // tag } int successCount = buf.readUnsignedByte(); for (int i = 0; i < successCount; i++) { buf.readUnsignedShortLE(); // tag position.set(Position.KEY_RESULT, buf.readSlice(buf.readUnsignedShortLE()).toString(StandardCharsets.US_ASCII)); } return position; } return null; }
From source file:org.traccar.protocol.CastelProtocolEncoder.java
License:Apache License
private ByteBuf encodeContent(long deviceId, short type, ByteBuf content) { ByteBuf buf = Unpooled.buffer(0); String uniqueId = Context.getIdentityManager().getById(deviceId).getUniqueId(); buf.writeByte('@'); buf.writeByte('@'); buf.writeShortLE(2 + 2 + 1 + 20 + 2 + content.readableBytes() + 2 + 2); // length buf.writeByte(1); // protocol version buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); buf.writeZero(20 - uniqueId.length()); buf.writeShort(type);//from ww w.ja v a2 s . c o m buf.writeBytes(content); buf.writeShortLE(Checksum.crc16(Checksum.CRC16_X25, buf.nioBuffer())); buf.writeByte('\r'); buf.writeByte('\n'); return buf; }
From source file:org.traccar.protocol.DmtProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, int type, ByteBuf content) { if (channel != null) { ByteBuf response = Unpooled.buffer(); response.writeByte(0x02);//from w w w . java2 s . com response.writeByte(0x55); // header response.writeByte(type); response.writeShortLE(content != null ? content.readableBytes() : 0); if (content != null) { response.writeBytes(content); content.release(); } channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } }
From source file:org.traccar.protocol.EgtsProtocolDecoder.java
License:Apache License
private void sendResponse(Channel channel, int packetType, int index, int serviceType, int type, ByteBuf content) {/* ww w .ja v a 2 s . co m*/ if (channel != null) { ByteBuf data = Unpooled.buffer(); data.writeByte(type); data.writeShortLE(content.readableBytes()); data.writeBytes(content); content.release(); ByteBuf record = Unpooled.buffer(); if (packetType == PT_RESPONSE) { record.writeShortLE(index); record.writeByte(0); // success } record.writeShortLE(data.readableBytes()); record.writeShortLE(0); record.writeByte(0); // flags (possibly 1 << 6) record.writeByte(serviceType); record.writeByte(serviceType); record.writeBytes(data); data.release(); int recordChecksum = Checksum.crc16(Checksum.CRC16_CCITT_FALSE, record.nioBuffer()); ByteBuf response = Unpooled.buffer(); response.writeByte(1); // protocol version response.writeByte(0); // security key id response.writeByte(0); // flags response.writeByte(5 + 2 + 2 + 2); // header length response.writeByte(0); // encoding response.writeShortLE(record.readableBytes()); response.writeShortLE(packetId++); response.writeByte(packetType); response.writeByte(Checksum.crc8(Checksum.CRC8_EGTS, response.nioBuffer())); response.writeBytes(record); record.release(); response.writeShortLE(recordChecksum); channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); } }
From source file:org.traccar.protocol.EgtsProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg;//from ww w . j a va 2s. c o m int index = buf.getUnsignedShort(buf.readerIndex() + 5 + 2); buf.skipBytes(buf.getUnsignedByte(buf.readerIndex() + 3)); List<Position> positions = new LinkedList<>(); while (buf.readableBytes() > 2) { int length = buf.readUnsignedShortLE(); int recordIndex = buf.readUnsignedShortLE(); int recordFlags = buf.readUnsignedByte(); if (BitUtil.check(recordFlags, 0)) { buf.readUnsignedIntLE(); // object id } if (BitUtil.check(recordFlags, 1)) { buf.readUnsignedIntLE(); // event id } if (BitUtil.check(recordFlags, 2)) { buf.readUnsignedIntLE(); // time } int serviceType = buf.readUnsignedByte(); buf.readUnsignedByte(); // recipient service type int recordEnd = buf.readerIndex() + length; Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession != null) { position.setDeviceId(deviceSession.getDeviceId()); } ByteBuf response = Unpooled.buffer(); response.writeShortLE(recordIndex); response.writeByte(0); // success sendResponse(channel, PT_RESPONSE, index, serviceType, MSG_RECORD_RESPONSE, response); while (buf.readerIndex() < recordEnd) { int type = buf.readUnsignedByte(); int end = buf.readUnsignedShortLE() + buf.readerIndex(); if (type == MSG_TERM_IDENTITY) { buf.readUnsignedIntLE(); // object id int flags = buf.readUnsignedByte(); if (BitUtil.check(flags, 0)) { buf.readUnsignedShortLE(); // home dispatcher identifier } if (BitUtil.check(flags, 1)) { getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim()); } if (BitUtil.check(flags, 2)) { getDeviceSession(channel, remoteAddress, buf.readSlice(16).toString(StandardCharsets.US_ASCII).trim()); } if (BitUtil.check(flags, 3)) { buf.skipBytes(3); // language identifier } if (BitUtil.check(flags, 5)) { buf.skipBytes(3); // network identifier } if (BitUtil.check(flags, 6)) { buf.readUnsignedShortLE(); // buffer size } if (BitUtil.check(flags, 7)) { getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII).trim()); } response = Unpooled.buffer(); response.writeByte(0); // success sendResponse(channel, PT_APPDATA, 0, serviceType, MSG_RESULT_CODE, response); } else if (type == MSG_POS_DATA) { position.setTime(new Date((buf.readUnsignedIntLE() + 1262304000) * 1000)); // since 2010-01-01 position.setLatitude(buf.readUnsignedIntLE() * 90.0 / 0xFFFFFFFFL); position.setLongitude(buf.readUnsignedIntLE() * 180.0 / 0xFFFFFFFFL); int flags = buf.readUnsignedByte(); position.setValid(BitUtil.check(flags, 0)); if (BitUtil.check(flags, 5)) { position.setLatitude(-position.getLatitude()); } if (BitUtil.check(flags, 6)) { position.setLongitude(-position.getLongitude()); } int speed = buf.readUnsignedShortLE(); position.setSpeed(UnitsConverter.knotsFromKph(BitUtil.to(speed, 14) * 0.1)); position.setCourse(buf.readUnsignedByte() + (BitUtil.check(speed, 15) ? 0x100 : 0)); position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE() * 100); position.set(Position.KEY_INPUT, buf.readUnsignedByte()); position.set(Position.KEY_EVENT, buf.readUnsignedByte()); if (BitUtil.check(flags, 7)) { position.setAltitude(buf.readMediumLE()); } } else if (type == MSG_EXT_POS_DATA) { int flags = buf.readUnsignedByte(); if (BitUtil.check(flags, 0)) { position.set(Position.KEY_VDOP, buf.readUnsignedShortLE()); } if (BitUtil.check(flags, 1)) { position.set(Position.KEY_HDOP, buf.readUnsignedShortLE()); } if (BitUtil.check(flags, 2)) { position.set(Position.KEY_PDOP, buf.readUnsignedShortLE()); } if (BitUtil.check(flags, 3)) { position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); } } else if (type == MSG_AD_SENSORS_DATA) { buf.readUnsignedByte(); // inputs flags position.set(Position.KEY_OUTPUT, buf.readUnsignedByte()); buf.readUnsignedByte(); // adc flags } buf.readerIndex(end); } if (serviceType == SERVICE_TELEDATA && deviceSession != null) { positions.add(position); } } return positions.isEmpty() ? null : positions; }
From source file:org.traccar.protocol.GalileoProtocolDecoder.java
License:Apache License
private void sendReply(Channel channel, int header, int checksum) { if (channel != null) { ByteBuf reply = Unpooled.buffer(3); reply.writeByte(header);//from w w w . j av a 2 s . c o m reply.writeShortLE((short) checksum); channel.writeAndFlush(new NetworkMessage(reply, channel.remoteAddress())); } }
From source file:org.traccar.protocol.GalileoProtocolEncoder.java
License:Apache License
private ByteBuf encodeText(String uniqueId, String text) { ByteBuf buf = Unpooled.buffer(256); buf.writeByte(0x01);/*from w ww .j av a2 s. c om*/ buf.writeShortLE(uniqueId.length() + text.length() + 11); buf.writeByte(0x03); // imei tag buf.writeBytes(uniqueId.getBytes(StandardCharsets.US_ASCII)); buf.writeByte(0x04); // device id tag buf.writeShortLE(0); // not needed if imei provided buf.writeByte(0xE0); // index tag buf.writeIntLE(0); // index buf.writeByte(0xE1); // command text tag buf.writeByte(text.length()); buf.writeBytes(text.getBytes(StandardCharsets.US_ASCII)); buf.writeShortLE(Checksum.crc16(Checksum.CRC16_MODBUS, buf.nioBuffer(0, buf.writerIndex()))); return buf; }
From source file:org.traccar.protocol.GranitProtocolDecoder.java
License:Apache License
private static void sendResponseCurrent(Channel channel, int deviceId, long time) { ByteBuf response = Unpooled.buffer(); response.writeBytes("BB+UGRC~".getBytes(StandardCharsets.US_ASCII)); response.writeShortLE(6); // length response.writeInt((int) time); response.writeShortLE(deviceId);/* w w w .ja v a 2 s.c o m*/ appendChecksum(response, 16); channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); }
From source file:org.traccar.protocol.GranitProtocolDecoder.java
License:Apache License
private static void sendResponseArchive(Channel channel, int deviceId, int packNum) { ByteBuf response = Unpooled.buffer(); response.writeBytes("BB+ARCF~".getBytes(StandardCharsets.US_ASCII)); response.writeShortLE(4); // length response.writeShortLE(packNum);/*from www. j a v a 2 s . c o m*/ response.writeShortLE(deviceId); appendChecksum(response, 14); channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress())); }