Example usage for io.netty.buffer ByteBuf readShortLE

List of usage examples for io.netty.buffer ByteBuf readShortLE

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf readShortLE.

Prototype

public abstract short readShortLE();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex in the Little Endian Byte Order and increases the readerIndex by 2 in this buffer.

Usage

From source file:com.ibasco.agql.protocols.valve.source.query.handlers.SourceQueryPacketAssembler.java

License:Open Source License

/**
 * Process split-packet data// w w  w. j av a2  s .  c  o  m
 *
 * @param data
 *         The {@link ByteBuf} containing the split-packet data
 * @param allocator
 *         The {@link ByteBufAllocator} used to create/allocate pooled buffers
 *
 * @return Returns a non-null {@link ByteBuf} if the split-packets have been assembled. Null if the
 *
 * @throws Exception
 */
private ByteBuf processSplitPackets(ByteBuf data, ByteBufAllocator allocator, InetSocketAddress senderAddress)
        throws Exception {
    int packetCount, packetNumber, requestId, splitSize, packetChecksum = 0;
    boolean isCompressed;

    //Start processing
    requestId = data.readIntLE();
    //read the most significant bit is set
    isCompressed = ((requestId & 0x80000000) != 0);
    //The total number of packets in the response.
    packetCount = data.readByte();
    //The number of the packet. Starts at 0.
    packetNumber = data.readByte();

    //Create our key for this request (request id + sender ip)
    final SplitPacketKey key = new SplitPacketKey(requestId, senderAddress);

    log.debug("Processing split packet {}", key);

    log.debug(
            "Split Packet Received = (AbstractRequest {}, Packet Number {}, Packet Count {}, Is Compressed: {})",
            requestId, packetNumber, packetCount, isCompressed);

    //Try to retrieve the split packet container for this request (if existing)
    //If request is not yet on the map, create and retrieve
    SplitPacketContainer splitPackets = this.requestMap.computeIfAbsent(key,
            k -> new SplitPacketContainer(packetCount));

    //As per protocol specs, the size is only present in the first packet of the response and only if the response is being compressed.
    //split size = Maximum size of packet before packet switching occurs. The default value is 1248 bytes (0x04E0
    if (isCompressed) {
        splitSize = data.readIntLE();
        packetChecksum = data.readIntLE();
    } else {
        splitSize = data.readShortLE();
    }

    //TODO: Handle compressed split packets
    int bufferSize = Math.min(splitSize, data.readableBytes());
    byte[] splitPacket = new byte[bufferSize];
    data.readBytes(splitPacket); //transfer the split data into this buffer

    //Add the split packet to the container
    splitPackets.addPacket(packetNumber, splitPacket);

    //Have we received all packets for this request?
    if (splitPackets.isComplete()) {
        log.debug(
                "Split Packets have all been successfully received from AbstractRequest {}. Re-assembling packets.",
                requestId);

        //Retrieve total split packets received based on their length
        int packetSize = splitPackets.getPacketSize();
        //Allocate a new buffer to store the re-assembled packets
        final ByteBuf packetBuffer = allocator.buffer(packetSize);
        boolean done = false;
        try {
            //Start re-assembling split-packets from the container
            done = reassembleSplitPackets(splitPackets, packetBuffer, isCompressed, splitSize, packetChecksum);
        } catch (Exception e) {
            //If an error occurs during re-assembly, make sure we release the allocated buffer
            packetBuffer.release();
            throw e;
        } finally {
            if (done)
                requestMap.remove(key);
        }

        return packetBuffer;
    }

    //Return null, indicating that we still don't have a complete packet
    return null;
}

From source file:com.ibasco.agql.protocols.valve.source.query.packets.response.SourceInfoResponsePacket.java

License:Open Source License

@Override
public SourceServer toObject() {
    ByteBuf data = getPayloadBuffer();

    SourceServer server = new SourceServer();

    //Start Decoding
    server.setNetworkVersion(data.readByte());
    server.setName(readString(data));/*from  w  w w.  j  a va 2  s . com*/
    server.setMapName(readString(data));
    server.setGameDirectory(readString(data));
    server.setGameDescription(readString(data));
    server.setAppId(data.readShortLE());
    server.setNumOfPlayers(data.readByte());
    server.setMaxPlayers(data.readByte());
    server.setNumOfBots(data.readByte());
    server.setDedicated(data.readByte() == 1);
    server.setOperatingSystem((char) data.readByte());
    server.setPasswordProtected(data.readByte() == 1);
    server.setSecure(data.readByte() == 1);
    server.setGameVersion(readString(data));

    if (data.readableBytes() > 0) {
        byte extraDataFlag = data.readByte();

        if ((extraDataFlag & EDF_GAME_PORT) != 0) {
            data.readShortLE(); //discard, we already know which port based on the sender address
        }

        if ((extraDataFlag & EDF_SERVER_ID) != 0) {
            //this.info.put("serverId", Long.reverseBytes((this.contentData.getInt() << 32) | this.contentData.getInt()));
            server.setServerId(data.readLongLE());
        }

        if ((extraDataFlag & EDF_SOURCE_TV) != 0) {
            server.setTvPort(data.readShortLE());
            server.setTvName(readString(data));
        }

        if ((extraDataFlag & EDF_SERVER_TAGS) != 0) {
            server.setServerTags(readString(data));
        }

        if ((extraDataFlag & EDF_GAME_ID) != 0) {
            server.setGameId(data.readLongLE());
            //this.info.put("gameId", Long.reverseBytes((this.contentData.getInt() << 32) | this.contentData.getInt()));
        }
    }

    return server;
}

From source file:com.ibasco.agql.protocols.valve.source.query.packets.response.SourceRulesResponsePacket.java

License:Open Source License

@Override
public Map<String, String> toObject() {
    ByteBuf data = getPayloadBuffer();
    Map<String, String> ruleList = new HashMap<>();
    short numOfRules = data.readShortLE();
    for (int i = 0; i < numOfRules; i++) {
        String name = readString(data);
        String value = readString(data);
        ruleList.put(name, value);/*from   w w w.j a va  2 s.c om*/
    }
    return ruleList;
}

From source file:org.traccar.protocol.AdmProtocolDecoder.java

License:Apache License

private Position decodeData(Channel channel, SocketAddress remoteAddress, ByteBuf buf, int type) {

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }//  w  w w. jav a  2 s  .co m

    if (BitUtil.to(type, 2) == 0) {
        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());

        int status = buf.readUnsignedShortLE();
        position.set(Position.KEY_STATUS, status);
        position.setValid(!BitUtil.check(status, 5));
        position.setLatitude(buf.readFloatLE());
        position.setLongitude(buf.readFloatLE());
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));

        position.set(Position.KEY_ACCELERATION, buf.readUnsignedByte() * 0.1);
        position.setAltitude(buf.readShortLE());
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        position.set(Position.KEY_SATELLITES, buf.readUnsignedByte() & 0x0f);

        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));

        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() * 0.001);
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() * 0.001);

        if (BitUtil.check(type, 2)) {
            buf.readUnsignedByte(); // vib
            buf.readUnsignedByte(); // vib_count

            int out = buf.readUnsignedByte();
            for (int i = 0; i <= 3; i++) {
                position.set(Position.PREFIX_OUT + (i + 1), BitUtil.check(out, i) ? 1 : 0);
            }

            buf.readUnsignedByte(); // in_alarm
        }

        if (BitUtil.check(type, 3)) {
            for (int i = 1; i <= 6; i++) {
                position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE() * 0.001);
            }
        }

        if (BitUtil.check(type, 4)) {
            for (int i = 1; i <= 2; i++) {
                position.set(Position.PREFIX_COUNT + i, buf.readUnsignedIntLE());
            }
        }

        if (BitUtil.check(type, 5)) {
            for (int i = 1; i <= 3; i++) {
                buf.readUnsignedShortLE(); // fuel level
            }
            for (int i = 1; i <= 3; i++) {
                position.set(Position.PREFIX_TEMP + i, buf.readUnsignedByte());
            }
        }

        if (BitUtil.check(type, 6)) {
            int endIndex = buf.readerIndex() + buf.readUnsignedByte();
            while (buf.readerIndex() < endIndex) {
                int mask = buf.readUnsignedByte();
                long value;
                switch (BitUtil.from(mask, 6)) {
                case 3:
                    value = buf.readLongLE();
                    break;
                case 2:
                    value = buf.readUnsignedIntLE();
                    break;
                case 1:
                    value = buf.readUnsignedShortLE();
                    break;
                default:
                    value = buf.readUnsignedByte();
                    break;
                }
                int index = BitUtil.to(mask, 6);
                switch (index) {
                case 1:
                    position.set(Position.PREFIX_TEMP + 1, value);
                    break;
                case 2:
                    position.set("humidity", value);
                    break;
                case 3:
                    position.set("illumination", value);
                    break;
                case 4:
                    position.set(Position.KEY_BATTERY, value);
                    break;
                default:
                    position.set("can" + index, value);
                    break;
                }
            }
        }

        if (BitUtil.check(type, 7)) {
            position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        }

        return position;
    }

    return null;
}

From source file:org.traccar.protocol.ApelProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;
    int type = buf.readUnsignedShortLE();
    boolean alarm = (type & 0x8000) != 0;
    type = type & 0x7FFF;//w w  w .j  a  va  2s.co m
    buf.readUnsignedShortLE(); // length

    if (alarm) {
        sendSimpleMessage(channel, MSG_ACK_ALARM);
    }

    if (type == MSG_TRACKER_ID) {
        return null; // unsupported authentication type
    }

    if (type == MSG_TRACKER_ID_EXT) {

        buf.readUnsignedIntLE(); // id
        int length = buf.readUnsignedShortLE();
        buf.skipBytes(length);
        length = buf.readUnsignedShortLE();
        getDeviceSession(channel, remoteAddress, buf.readSlice(length).toString(StandardCharsets.US_ASCII));

    } else if (type == MSG_LAST_LOG_INDEX) {

        long index = buf.readUnsignedIntLE();
        if (index > 0) {
            newIndex = index;
            requestArchive(channel);
        }

    } else if (type == MSG_CURRENT_GPS_DATA || type == MSG_STATE_FULL_INFO_T104 || type == MSG_LOG_RECORDS) {

        DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
        if (deviceSession == null) {
            return null;
        }

        List<Position> positions = new LinkedList<>();

        int recordCount = 1;
        if (type == MSG_LOG_RECORDS) {
            recordCount = buf.readUnsignedShortLE();
        }

        for (int j = 0; j < recordCount; j++) {
            Position position = new Position(getProtocolName());
            position.setDeviceId(deviceSession.getDeviceId());

            int subtype = type;
            if (type == MSG_LOG_RECORDS) {
                position.set(Position.KEY_ARCHIVE, true);
                lastIndex = buf.readUnsignedIntLE() + 1;
                position.set(Position.KEY_INDEX, lastIndex);

                subtype = buf.readUnsignedShortLE();
                if (subtype != MSG_CURRENT_GPS_DATA && subtype != MSG_STATE_FULL_INFO_T104) {
                    buf.skipBytes(buf.readUnsignedShortLE());
                    continue;
                }
                buf.readUnsignedShortLE(); // length
            }

            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            position.setLatitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);
            position.setLongitude(buf.readIntLE() * 180.0 / 0x7FFFFFFF);

            if (subtype == MSG_STATE_FULL_INFO_T104) {
                int speed = buf.readUnsignedByte();
                position.setValid(speed != 255);
                position.setSpeed(UnitsConverter.knotsFromKph(speed));
                position.set(Position.KEY_HDOP, buf.readByte());
            } else {
                int speed = buf.readShortLE();
                position.setValid(speed != -1);
                position.setSpeed(UnitsConverter.knotsFromKph(speed * 0.01));
            }

            position.setCourse(buf.readShortLE() * 0.01);
            position.setAltitude(buf.readShortLE());

            if (subtype == MSG_STATE_FULL_INFO_T104) {

                position.set(Position.KEY_SATELLITES, buf.readUnsignedByte());
                position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                position.set(Position.KEY_EVENT, buf.readUnsignedShortLE());
                position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
                position.set(Position.KEY_INPUT, buf.readUnsignedByte());
                position.set(Position.KEY_OUTPUT, buf.readUnsignedByte());

                for (int i = 1; i <= 8; i++) {
                    position.set(Position.PREFIX_ADC + i, buf.readUnsignedShortLE());
                }

                position.set(Position.PREFIX_COUNT + 1, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 2, buf.readUnsignedIntLE());
                position.set(Position.PREFIX_COUNT + 3, buf.readUnsignedIntLE());
            }

            positions.add(position);
        }

        buf.readUnsignedIntLE(); // crc

        if (type == MSG_LOG_RECORDS) {
            requestArchive(channel);
        } else {
            sendSimpleMessage(channel, MSG_REQUEST_LAST_LOG_INDEX);
        }

        return positions;
    }

    return null;
}

From source file:org.traccar.protocol.AplicomProtocolDecoder.java

License:Apache License

private void decodeCanData(ByteBuf buf, Position position) {

    buf.readUnsignedMedium(); // packet identifier
    position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
    int count = buf.readUnsignedByte();
    buf.readUnsignedByte(); // batch count
    buf.readUnsignedShort(); // selector bit
    buf.readUnsignedInt(); // timestamp

    buf.skipBytes(8);/*from  w w w  .  jav  a 2s  .co  m*/

    ArrayList<ByteBuf> values = new ArrayList<>(count);

    for (int i = 0; i < count; i++) {
        values.add(buf.readSlice(8));
    }

    for (int i = 0; i < count; i++) {
        ByteBuf value = values.get(i);
        switch (buf.readInt()) {
        case 0x20D:
            position.set(Position.KEY_RPM, value.readShortLE());
            position.set("dieselTemperature", value.readShortLE() * 0.1);
            position.set("batteryVoltage", value.readShortLE() * 0.01);
            position.set("supplyAirTempDep1", value.readShortLE() * 0.1);
            break;
        case 0x30D:
            position.set("activeAlarm", ByteBufUtil.hexDump(value));
            break;
        case 0x40C:
            position.set("airTempDep1", value.readShortLE() * 0.1);
            position.set("airTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x40D:
            position.set("coldUnitState", ByteBufUtil.hexDump(value));
            break;
        case 0x50C:
            position.set("defrostTempDep1", value.readShortLE() * 0.1);
            position.set("defrostTempDep2", value.readShortLE() * 0.1);
            break;
        case 0x50D:
            position.set("condenserPressure", value.readShortLE() * 0.1);
            position.set("suctionPressure", value.readShortLE() * 0.1);
            break;
        case 0x58C:
            value.readByte();
            value.readShort(); // index
            switch (value.readByte()) {
            case 0x01:
                position.set("setpointZone1", value.readIntLE() * 0.1);
                break;
            case 0x02:
                position.set("setpointZone2", value.readIntLE() * 0.1);
                break;
            case 0x05:
                position.set("unitType", value.readIntLE());
                break;
            case 0x13:
                position.set("dieselHours", value.readIntLE() / 60 / 60);
                break;
            case 0x14:
                position.set("electricHours", value.readIntLE() / 60 / 60);
                break;
            case 0x17:
                position.set("serviceIndicator", value.readIntLE());
                break;
            case 0x18:
                position.set("softwareVersion", value.readIntLE() * 0.01);
                break;
            default:
                break;
            }
            break;
        default:
            LOGGER.warn("Aplicom CAN decoding error", new UnsupportedOperationException());
            break;
        }
    }
}

From source file:org.traccar.protocol.BlackKiteProtocolDecoder.java

License:Apache License

@Override
protected Object decode(Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {

    ByteBuf buf = (ByteBuf) msg;

    buf.readUnsignedByte(); // header
    int length = (buf.readUnsignedShortLE() & 0x7fff) + 3;

    List<Position> positions = new LinkedList<>();
    Set<Integer> tags = new HashSet<>();
    boolean hasLocation = false;
    Position position = new Position(getProtocolName());

    while (buf.readerIndex() < length) {

        // Check if new message started
        int tag = buf.readUnsignedByte();
        if (tags.contains(tag)) {
            if (hasLocation && position.getFixTime() != null) {
                positions.add(position);
            }/*w w  w . jav  a 2 s . c om*/
            tags.clear();
            hasLocation = false;
            position = new Position(getProtocolName());
        }
        tags.add(tag);

        switch (tag) {

        case TAG_IMEI:
            getDeviceSession(channel, remoteAddress, buf.readSlice(15).toString(StandardCharsets.US_ASCII));
            break;

        case TAG_DATE:
            position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
            break;

        case TAG_COORDINATES:
            hasLocation = true;
            position.setValid((buf.readUnsignedByte() & 0xf0) == 0x00);
            position.setLatitude(buf.readIntLE() / 1000000.0);
            position.setLongitude(buf.readIntLE() / 1000000.0);
            break;

        case TAG_SPEED_COURSE:
            position.setSpeed(buf.readUnsignedShortLE() * 0.0539957);
            position.setCourse(buf.readUnsignedShortLE() * 0.1);
            break;

        case TAG_ALTITUDE:
            position.setAltitude(buf.readShortLE());
            break;

        case TAG_STATUS:
            int status = buf.readUnsignedShortLE();
            position.set(Position.KEY_IGNITION, BitUtil.check(status, 9));
            if (BitUtil.check(status, 15)) {
                position.set(Position.KEY_ALARM, Position.ALARM_GENERAL);
            }
            position.set(Position.KEY_CHARGE, BitUtil.check(status, 2));
            break;

        case TAG_DIGITAL_INPUTS:
            int input = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 1), BitUtil.check(input, i));
            }
            break;

        case TAG_DIGITAL_OUTPUTS:
            int output = buf.readUnsignedShortLE();
            for (int i = 0; i < 16; i++) {
                position.set(Position.PREFIX_IO + (i + 17), BitUtil.check(output, i));
            }
            break;

        case TAG_INPUT_VOLTAGE1:
            position.set(Position.PREFIX_ADC + 1, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE2:
            position.set(Position.PREFIX_ADC + 2, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE3:
            position.set(Position.PREFIX_ADC + 3, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_INPUT_VOLTAGE4:
            position.set(Position.PREFIX_ADC + 4, buf.readUnsignedShortLE() / 1000.0);
            break;

        case TAG_XT1:
        case TAG_XT2:
        case TAG_XT3:
            buf.skipBytes(16);
            break;

        default:
            break;

        }
    }

    if (hasLocation && position.getFixTime() != null) {
        positions.add(position);
    }

    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress);
    if (deviceSession == null) {
        return null;
    }

    sendReply(channel, buf.readUnsignedShortLE());

    for (Position p : positions) {
        p.setDeviceId(deviceSession.getDeviceId());
    }

    if (positions.isEmpty()) {
        return null;
    }

    return positions;
}

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;
    }/*from  w ww .ja v a2s.  c  o  m*/

    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;
    }/*from  ww w.jav a  2s  . c  om*/

    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.GalileoProtocolDecoder.java

License:Apache License

private void decodeTagOther(Position position, ByteBuf buf, int tag) {
    switch (tag) {
    case 0x01://from   ww w  . j  a v  a 2s.co  m
        position.set(Position.KEY_VERSION_HW, buf.readUnsignedByte());
        break;
    case 0x02:
        position.set(Position.KEY_VERSION_FW, buf.readUnsignedByte());
        break;
    case 0x04:
        position.set("deviceId", buf.readUnsignedShortLE());
        break;
    case 0x10:
        position.set(Position.KEY_INDEX, buf.readUnsignedShortLE());
        break;
    case 0x20:
        position.setTime(new Date(buf.readUnsignedIntLE() * 1000));
        break;
    case 0x33:
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShortLE() * 0.1));
        position.setCourse(buf.readUnsignedShortLE() * 0.1);
        break;
    case 0x34:
        position.setAltitude(buf.readShortLE());
        break;
    case 0x35:
        position.set(Position.KEY_HDOP, buf.readUnsignedByte() * 0.1);
        break;
    case 0x40:
        position.set(Position.KEY_STATUS, buf.readUnsignedShortLE());
        break;
    case 0x41:
        position.set(Position.KEY_POWER, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x42:
        position.set(Position.KEY_BATTERY, buf.readUnsignedShortLE() / 1000.0);
        break;
    case 0x43:
        position.set(Position.KEY_DEVICE_TEMP, buf.readByte());
        break;
    case 0x44:
        position.set(Position.KEY_ACCELERATION, buf.readUnsignedIntLE());
        break;
    case 0x45:
        position.set(Position.KEY_OUTPUT, buf.readUnsignedShortLE());
        break;
    case 0x46:
        position.set(Position.KEY_INPUT, buf.readUnsignedShortLE());
        break;
    case 0x48:
        position.set("statusExtended", buf.readUnsignedShortLE());
        break;
    case 0x58:
        position.set("rs2320", buf.readUnsignedShortLE());
        break;
    case 0x59:
        position.set("rs2321", buf.readUnsignedShortLE());
        break;
    case 0x90:
        position.set(Position.KEY_DRIVER_UNIQUE_ID, String.valueOf(buf.readUnsignedIntLE()));
        break;
    case 0xc0:
        position.set("fuelTotal", buf.readUnsignedIntLE() * 0.5);
        break;
    case 0xc1:
        position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
        position.set(Position.PREFIX_TEMP + 1, buf.readUnsignedByte() - 40);
        position.set(Position.KEY_RPM, buf.readUnsignedShortLE() * 0.125);
        break;
    case 0xc2:
        position.set("canB0", buf.readUnsignedIntLE());
        break;
    case 0xc3:
        position.set("canB1", buf.readUnsignedIntLE());
        break;
    case 0xd4:
        position.set(Position.KEY_ODOMETER, buf.readUnsignedIntLE());
        break;
    case 0xe0:
        position.set(Position.KEY_INDEX, buf.readUnsignedIntLE());
        break;
    case 0xe1:
        position.set(Position.KEY_RESULT,
                buf.readSlice(buf.readUnsignedByte()).toString(StandardCharsets.US_ASCII));
        break;
    case 0xea:
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        position.set("userDataArray", ByteBufUtil.hexDump(buf.readSlice(buf.readUnsignedByte())));
        break;
    default:
        buf.skipBytes(getTagLength(tag));
        break;
    }
}