List of usage examples for io.netty.buffer ByteBuf readUnsignedShortLE
public abstract int readUnsignedShortLE();
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private void decodeObd(Position position, ByteBuf buf, boolean groups) { int count = buf.readUnsignedByte(); int[] pids = new int[count]; for (int i = 0; i < count; i++) { pids[i] = buf.readUnsignedShortLE() & 0xff; }/*from w w w . j ava2 s .com*/ if (groups) { buf.readUnsignedByte(); // group count buf.readUnsignedByte(); // group size } for (int i = 0; i < count; i++) { int value; switch (PID_LENGTH_MAP.get(pids[i])) { case 1: value = buf.readUnsignedByte(); break; case 2: value = buf.readUnsignedShortLE(); break; case 4: value = buf.readIntLE(); break; default: value = 0; break; } position.add(ObdDecoder.decodeData(pids[i], value, false)); } }
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
private void decodeStat(Position position, ByteBuf buf) { buf.readUnsignedIntLE(); // ACC ON time buf.readUnsignedIntLE(); // UTC time position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedIntLE()); position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedIntLE()); buf.readUnsignedShortLE(); // current fuel consumption position.set(Position.KEY_STATUS, buf.readUnsignedIntLE()); buf.skipBytes(8);/*from w ww . j a v a 2 s . c o m*/ }
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);//from ww w. j a v a2 s . c om response.writeIntLE((int) (System.currentTimeMillis() / 1000)); sendResponse(channel, remoteAddress, version, id, MSG_SC_LOGIN_RESPONSE, response); } 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.CastelProtocolDecoder.java
License:Apache License
private Object decodeCc(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int version, ByteBuf id, short type, DeviceSession deviceSession) { if (type == MSG_CC_HEARTBEAT) { sendResponse(channel, remoteAddress, version, id, MSG_CC_HEARTBEAT_RESPONSE, null); buf.readUnsignedByte(); // 0x01 for history 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_STATUS, buf.readUnsignedIntLE()); position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); buf.readUnsignedByte(); // geo-fencing id buf.readUnsignedByte(); // geo-fencing flags buf.readUnsignedByte(); // additional flags position.setNetwork(/* w w w . j a va 2 s . com*/ new Network(CellTower.fromLacCid(buf.readUnsignedShortLE(), buf.readUnsignedShortLE()))); positions.add(position); } return positions; } else if (type == MSG_CC_LOGIN) { sendResponse(channel, remoteAddress, version, id, MSG_CC_LOGIN_RESPONSE, null); Position position = readPosition(deviceSession, buf); position.set(Position.KEY_STATUS, buf.readUnsignedIntLE()); position.set(Position.KEY_BATTERY, buf.readUnsignedByte()); position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE()); buf.readUnsignedByte(); // geo-fencing id buf.readUnsignedByte(); // geo-fencing flags buf.readUnsignedByte(); // additional flags // GSM_CELL_CODE // STR_Z - firmware version // STR_Z - hardware version return position; } return null; }
From source file:org.traccar.protocol.CastelProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; int header = buf.readUnsignedShortLE(); buf.readUnsignedShortLE(); // length int version = -1; if (header == 0x4040) { version = buf.readUnsignedByte(); }// w w w . ja va 2 s . c om ByteBuf id = buf.readSlice(20); short type = buf.readShort(); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, id.toString(StandardCharsets.US_ASCII).trim()); if (deviceSession == null) { return null; } switch (version) { case -1: return decodeMpip(channel, remoteAddress, buf, version, id, type, deviceSession); case 3: case 4: return decodeSc(channel, remoteAddress, buf, version, id, type, deviceSession); default: return decodeCc(channel, remoteAddress, buf, version, id, type, deviceSession); } }
From source file:org.traccar.protocol.CellocatorProtocolDecoder.java
License:Apache License
@Override protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception { ByteBuf buf = (ByteBuf) msg; boolean alternative = buf.getByte(buf.readerIndex() + 3) != 'P'; buf.skipBytes(4); // system code int type = buf.readUnsignedByte(); long deviceUniqueId = buf.readUnsignedIntLE(); if (type != MSG_CLIENT_SERIAL) { buf.readUnsignedShortLE(); // communication control }// www . j a v a 2s . c o m byte packetNumber = buf.readByte(); sendReply(channel, remoteAddress, deviceUniqueId, packetNumber); if (type == MSG_CLIENT_STATUS) { Position position = new Position(getProtocolName()); DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, String.valueOf(deviceUniqueId)); if (deviceSession == null) { return null; } position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte()); position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte()); buf.readUnsignedByte(); // protocol version position.set(Position.KEY_STATUS, buf.readUnsignedByte() & 0x0f); buf.readUnsignedByte(); // operator / configuration flags buf.readUnsignedByte(); // reason data position.set(Position.KEY_ALARM, decodeAlarm(buf.readUnsignedByte())); position.set("mode", buf.readUnsignedByte()); position.set(Position.KEY_INPUT, buf.readUnsignedIntLE()); if (alternative) { buf.readUnsignedByte(); // input position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE()); position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE()); } else { buf.readUnsignedByte(); // operator position.set(Position.PREFIX_ADC + 1, buf.readUnsignedIntLE()); } position.set(Position.KEY_ODOMETER, buf.readUnsignedMediumLE()); buf.skipBytes(6); // multi-purpose data buf.readUnsignedShortLE(); // fix time buf.readUnsignedByte(); // location status buf.readUnsignedByte(); // mode 1 buf.readUnsignedByte(); // mode 2 position.set(Position.KEY_SATELLITES, buf.readUnsignedByte()); position.setValid(true); if (alternative) { position.setLongitude(buf.readIntLE() / 10000000.0); position.setLatitude(buf.readIntLE() / 10000000.0); } else { position.setLongitude(buf.readIntLE() / Math.PI * 180 / 100000000); position.setLatitude(buf.readIntLE() / Math.PI * 180 / 100000000); } position.setAltitude(buf.readIntLE() * 0.01); if (alternative) { position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedIntLE())); position.setCourse(buf.readUnsignedShortLE() / 1000.0); } else { position.setSpeed(UnitsConverter.knotsFromMps(buf.readUnsignedIntLE() * 0.01)); position.setCourse(buf.readUnsignedShortLE() / Math.PI * 180.0 / 1000.0); } DateBuilder dateBuilder = new DateBuilder() .setTimeReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte()) .setDateReverse(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedShortLE()); position.setTime(dateBuilder.getDate()); return position; } return null; }
From source file:org.traccar.protocol.DmtProtocolDecoder.java
License:Apache License
private List<Position> decodeFixed64(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; }/*www . j a v a2 s . c om*/ List<Position> positions = new LinkedList<>(); while (buf.readableBytes() >= 64) { Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); buf.readByte(); // type position.set(Position.KEY_INDEX, buf.readUnsignedIntLE()); long time = buf.readUnsignedIntLE(); position.setTime( new DateBuilder().setYear((int) (2000 + (time & 0x3F))).setMonth((int) (time >> 6) & 0xF) .setDay((int) (time >> 10) & 0x1F).setHour((int) (time >> 15) & 0x1F) .setMinute((int) (time >> 20) & 0x3F).setSecond((int) (time >> 26) & 0x3F).getDate()); position.setLongitude(buf.readIntLE() * 0.0000001); position.setLatitude(buf.readIntLE() * 0.0000001); position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE())); position.setCourse(buf.readUnsignedByte() * 2); position.setAltitude(buf.readShortLE()); buf.readUnsignedShortLE(); // position accuracy buf.readUnsignedByte(); // speed accuracy position.set(Position.KEY_EVENT, buf.readUnsignedByte()); position.setValid(BitUtil.check(buf.readByte(), 0)); position.set(Position.KEY_INPUT, buf.readUnsignedIntLE()); position.set(Position.KEY_OUTPUT, buf.readUnsignedShortLE()); for (int i = 1; i <= 5; i++) { position.set(Position.PREFIX_ADC + i, buf.readShortLE()); } position.set(Position.KEY_DEVICE_TEMP, buf.readByte()); buf.readShortLE(); // accelerometer x buf.readShortLE(); // accelerometer y buf.readShortLE(); // accelerometer z buf.skipBytes(8); // device id position.set(Position.KEY_PDOP, buf.readUnsignedShortLE() * 0.01); buf.skipBytes(2); // reserved buf.readUnsignedShortLE(); // checksum positions.add(position); } return positions; }
From source file:org.traccar.protocol.DmtProtocolDecoder.java
License:Apache License
private List<Position> decodeStandard(Channel channel, SocketAddress remoteAddress, ByteBuf buf) { DeviceSession deviceSession = getDeviceSession(channel, remoteAddress); if (deviceSession == null) { return null; }/* w w w.j av a2s. co m*/ List<Position> positions = new LinkedList<>(); while (buf.isReadable()) { int recordEnd = buf.readerIndex() + buf.readUnsignedShortLE(); Position position = new Position(getProtocolName()); position.setDeviceId(deviceSession.getDeviceId()); position.set(Position.KEY_INDEX, buf.readUnsignedIntLE()); position.setDeviceTime(new Date(1356998400000L + buf.readUnsignedIntLE() * 1000)); // since 1 Jan 2013 position.set(Position.KEY_EVENT, buf.readUnsignedByte()); while (buf.readerIndex() < recordEnd) { int fieldId = buf.readUnsignedByte(); int fieldLength = buf.readUnsignedByte(); int fieldEnd = buf.readerIndex() + (fieldLength == 255 ? buf.readUnsignedShortLE() : fieldLength); if (fieldId == 0) { position.setFixTime(new Date(1356998400000L + buf.readUnsignedIntLE() * 1000)); position.setLatitude(buf.readIntLE() * 0.0000001); position.setLongitude(buf.readIntLE() * 0.0000001); position.setAltitude(buf.readShortLE()); position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedShortLE())); buf.readUnsignedByte(); // speed accuracy position.setCourse(buf.readUnsignedByte() * 2); position.set(Position.KEY_PDOP, buf.readUnsignedByte() * 0.1); position.setAccuracy(buf.readUnsignedByte()); position.setValid(buf.readUnsignedByte() != 0); } else if (fieldId == 2) { int input = buf.readIntLE(); int output = buf.readUnsignedShortLE(); int status = buf.readUnsignedShortLE(); position.set(Position.KEY_IGNITION, BitUtil.check(input, 0)); position.set(Position.KEY_INPUT, input); position.set(Position.KEY_OUTPUT, output); position.set(Position.KEY_STATUS, status); } else if (fieldId == 6) { while (buf.readerIndex() < fieldEnd) { switch (buf.readUnsignedByte()) { case 1: position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001); break; case 2: position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.01); break; case 3: position.set(Position.KEY_DEVICE_TEMP, buf.readShortLE() * 0.01); break; case 4: position.set(Position.KEY_RSSI, buf.readUnsignedShortLE()); break; case 5: position.set("solarPower", buf.readUnsignedShortLE() * 0.001); break; default: break; } } } buf.readerIndex(fieldEnd); } if (position.getFixTime() == null) { getLastLocation(position, position.getDeviceTime()); } positions.add(position); } return positions; }
From source file:org.traccar.protocol.DmtProtocolDecoder.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(); int length = buf.readUnsignedShortLE(); if (type == MSG_HELLO) { buf.readUnsignedIntLE(); // device serial number DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII)); ByteBuf response = Unpooled.buffer(); if (length == 51) { response.writeByte(0); // reserved response.writeIntLE(0); // reserved } else {/*from w w w .j a v a 2 s . c om*/ response.writeIntLE((int) ((System.currentTimeMillis() - 1356998400000L) / 1000)); response.writeIntLE(deviceSession != null ? 0 : 1); // flags } sendResponse(channel, MSG_HELLO_RESPONSE, response); } else if (type == MSG_COMMIT) { ByteBuf response = Unpooled.buffer(0); response.writeByte(1); // flags (success) sendResponse(channel, MSG_COMMIT_RESPONSE, response); } else if (type == MSG_CANNED_REQUEST_1) { ByteBuf response = Unpooled.buffer(0); response.writeBytes(new byte[12]); sendResponse(channel, MSG_CANNED_RESPONSE_1, response); } else if (type == MSG_CANNED_REQUEST_2) { sendResponse(channel, MSG_CANNED_RESPONSE_2, null); } else if (type == MSG_DATA_RECORD_64) { return decodeFixed64(channel, remoteAddress, buf); } else if (type == MSG_DATA_RECORD) { return decodeStandard(channel, remoteAddress, buf); } return null; }
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; 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 }/*from w w w .j av a 2 s . c o m*/ 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; }