Example usage for io.netty.buffer ByteBuf writeIntLE

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

Introduction

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

Prototype

public abstract ByteBuf writeIntLE(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex in the Little Endian Byte Order and increases the writerIndex by 4 in this buffer.

Usage

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);//w  w w .  j a v a2s. co  m
            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.CellocatorProtocolDecoder.java

License:Apache License

private void sendReply(Channel channel, SocketAddress remoteAddress, long deviceId, byte packetNumber) {
    if (channel != null) {
        ByteBuf reply = Unpooled.buffer(28);
        reply.writeByte('M');
        reply.writeByte('C');
        reply.writeByte('G');
        reply.writeByte('P');
        reply.writeByte(MSG_SERVER_ACKNOWLEDGE);
        reply.writeIntLE((int) deviceId);
        reply.writeByte(commandCount++);
        reply.writeIntLE(0); // authentication code
        reply.writeByte(0);/*from ww w  .  j ava2  s  .  c  o m*/
        reply.writeByte(packetNumber);
        reply.writeZero(11);

        byte checksum = 0;
        for (int i = 4; i < 27; i++) {
            checksum += reply.getByte(i);
        }
        reply.writeByte(checksum);

        channel.writeAndFlush(new NetworkMessage(reply, remoteAddress));
    }
}

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

License:Apache License

private ByteBuf encodeContent(long deviceId, int command, int data1, int data2) {

    ByteBuf buf = Unpooled.buffer(0);
    buf.writeByte('M');
    buf.writeByte('C');
    buf.writeByte('G');
    buf.writeByte('P');
    buf.writeByte(0);/*from   w  w w  . j a va2  s  . c o  m*/
    buf.writeIntLE(Integer.parseInt(getUniqueId(deviceId)));
    buf.writeByte(0); // command numerator
    buf.writeIntLE(0); // authentication code
    buf.writeByte(command);
    buf.writeByte(command);
    buf.writeByte(data1);
    buf.writeByte(data1);
    buf.writeByte(data2);
    buf.writeByte(data2);
    buf.writeIntLE(0); // command specific data

    byte checksum = 0;
    for (int i = 4; i < buf.writerIndex(); i++) {
        checksum += buf.getByte(i);
    }
    buf.writeByte(checksum);

    return buf;
}

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;/*from w  ww .j  ava  2 s  .c om*/

    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 {
            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.GalileoProtocolEncoder.java

License:Apache License

private ByteBuf encodeText(String uniqueId, String text) {

    ByteBuf buf = Unpooled.buffer(256);

    buf.writeByte(0x01);//from   www .  ja va 2  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.MxtProtocolDecoder.java

License:Apache License

private static void sendResponse(Channel channel, int device, long id, int crc) {
    if (channel != null) {
        ByteBuf response = Unpooled.buffer();
        response.writeByte(device);/*from w ww .  j a va  2s . c o m*/
        response.writeByte(MSG_ACK);
        response.writeIntLE((int) id);
        response.writeShortLE(crc);
        response.writeShortLE(Checksum.crc16(Checksum.CRC16_XMODEM, response.nioBuffer()));

        ByteBuf encoded = Unpooled.buffer();
        encoded.writeByte(0x01); // header
        while (response.isReadable()) {
            int b = response.readByte();
            if (b == 0x01 || b == 0x04 || b == 0x10 || b == 0x11 || b == 0x13) {
                encoded.writeByte(0x10);
                b += 0x20;
            }
            encoded.writeByte(b);
        }
        response.release();
        encoded.writeByte(0x04); // ending
        channel.writeAndFlush(new NetworkMessage(encoded, channel.remoteAddress()));
    }
}

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

License:Apache License

private void sendAcknowledgment(Channel channel, int sequenceNumber) {
    ByteBuf data = Unpooled.buffer(4);//from   w w w  .ja  v  a  2  s .  c  o m
    data.writeShortLE(sequenceNumber);
    data.writeShortLE(0); // OK

    ByteBuf header = Unpooled.buffer(20);
    header.writeByte(1);
    header.writeByte(0);
    header.writeShortLE(senderSequenceNumber++);
    header.writeShortLE(MSG_ACKNOWLEDGEMENT);
    header.writeShortLE(header.capacity() + data.capacity());
    header.writeShortLE(0);
    header.writeShortLE(Checksum.crc16(Checksum.CRC16_CCITT_FALSE, data.nioBuffer()));
    header.writeIntLE(0);
    header.writeIntLE((int) (System.currentTimeMillis() / 1000) + LEAP_SECONDS_DELTA);

    if (channel != null) {
        channel.writeAndFlush(
                new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}

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

License:Apache License

private Object processNtcbSingle(DeviceSession deviceSession, Channel channel, ByteBuf buf) {
    Position position = parseNtcbPosition(deviceSession, buf);

    ByteBuf response = Unpooled.buffer(7);
    response.writeCharSequence("*<T", StandardCharsets.US_ASCII);
    response.writeIntLE((int) position.getLong(Position.KEY_INDEX));
    sendNtcbReply(channel, response);//  w  ww  .j  av a  2s .c o m

    return position.getFixTime() != null ? position : null;
}

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

License:Apache License

private Object processFlexSingle(FlexPositionParser parser, String flexHeader, DeviceSession deviceSession,
        Channel channel, ByteBuf buf) {

    if (!flexHeader.equals("~C")) {
        buf.readUnsignedInt(); // event index
    }/* ww  w  .  j a  va2 s.  c  om*/

    Position position = parser.parsePosition(deviceSession, buf);

    ByteBuf response = Unpooled.buffer();
    response.writeCharSequence(flexHeader, StandardCharsets.US_ASCII);
    response.writeIntLE((int) position.getLong(Position.KEY_INDEX));
    sendFlexReply(channel, response);

    return position.getFixTime() != null ? position : null;
}

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

License:Apache License

private void sendNtcbReply(Channel channel, ByteBuf data) {
    if (channel != null) {
        ByteBuf header = Unpooled.buffer(16);
        header.writeCharSequence(prefix, StandardCharsets.US_ASCII);
        header.writeIntLE((int) deviceUniqueId);
        header.writeIntLE((int) serverId);
        header.writeShortLE(data.readableBytes());
        header.writeByte(Checksum.xor(data.nioBuffer()));
        header.writeByte(Checksum.xor(header.nioBuffer()));

        channel.writeAndFlush(//from ww  w. j  a  v  a 2s  .  c  o  m
                new NetworkMessage(Unpooled.wrappedBuffer(header, data), channel.remoteAddress()));
    }
}