Huasheng Device HS-5000C protocol error

roneskinder2 years ago

Getting error while getting position message from vehicle while using new device from Huasheng, protocol version 2023 attached here. Protocol_V1.3.1_230209. Protocol_V1.3.1_230209

Can you update the Huasheng protocol for Traccar latest version 5.6?

2023-03-06 15:50:17  INFO: [T98393baf] connected
2023-03-06 15:50:17  INFO: [T98393baf: huasheng < 152.231.33.164] c0000000a7aa020000000000010001001447315f48312e305f56312e39345f53410002001533474e41584b4556354c5336333634323000030013383639373331303531333933393038000400144c342d56374c673979497a7a2d724a6d00050005010006000a7469676f6e690007000b7469676f6e69200008000500000900183839353035333033313035353337343133363446000a0014696e7465726e65742e7469676f2e6e69c0
2023-03-06 15:50:17  INFO: [T98393baf: huasheng > 152.231.33.164] c00100000dff0300000000000100c0
2023-03-06 15:50:18  INFO: [T98393baf: huasheng < 152.231.33.164] c0000000b5aa0000000000001480000000323330333036313535303039ff7c775d001268ce0000000000cd0000000100186000000000000000000004d300000064060ab1000005000a100d000002980009001533474e41584b4556354c53363336343230000f00133836393733313035313339333930380010001034303534332e31303135363200110008000000000014000bf800005300001900150006000000200016373130403330304034313038344032373931c0
2023-03-06 15:50:18  INFO: [T98393baf] error - minimumReadableBytes : -4 (expected: >= 0) - IllegalArgumentException (... < HuaShengProtocolDecoder:291 < *:135 < ExtendedObjectDecoder:72 < ... < WrapperContext:102 < ...)
2023-03-06 15:50:18  INFO: [T98393baf] disconnected
2023-03-06 15:51:05  INFO: [T0d09dd0a] connected
2023-03-06 15:51:05  INFO: [T0d09dd0a: huasheng < 152.231.33.164] c0000000a7aa020000000000010001001447315f48312e305f56312e39345f53410002001533474e41584b4556354c5336333634323000030013383639373331303531333933393038000400144c342d56374c673979497a7a2d724a6d00050005010006000a7469676f6e690007000b7469676f6e69200008000500000900183839353035333033313035353337343133363446000a0014696e7465726e65742e7469676f2e6e69c0
2023-03-06 15:51:05  INFO: [T0d09dd0a: huasheng > 152.231.33.164] c00100000dff0300000000000100c0
2023-03-06 15:51:06  INFO: [T0d09dd0a: huasheng < 152.231.33.164] c0000000baaa000000000000178000000232333033303631353530353700000000000000000000000000cd00000001001800000000000000000000000000000064060ab100000400090264060ab10005000a1001000002c90009001533474e41584b4556354c53363336343230000f00133836393733313035313339333930380010000c302e30303030303000110008000000000014000bf800002800000000150006000000200016373130403330304034313038344032373931c0
2023-03-06 15:51:06  INFO: [T0d09dd0a] error - minimumReadableBytes : -4 (expected: >= 0) - IllegalArgumentException (... < HuaShengProtocolDecoder:291 < *:135 < ExtendedObjectDecoder:72 < ... < WrapperContext:102 < ...)
Anton Tananaev2 years ago

Are you interested in sponsoring the work?

roneskinder2 years ago

Manufacturer sent this parse example:

"\n""C000000095AA020000000000010001001347315F48312E305F56312E39325F45000200154D414B474D363639484A4E33303138373900030013383632323035303535333839383632000400144C342D56374C673979497A7A2D724A6D000500050100060008434152440007000943415244310008000500000900183839393130323735313032303034343037313939000A0007777777C0""\n"{
      "data1": {
            "header": {
                  "flag_version": "00",
                  "reserved": "00",
                  "packet_length": "0095",
                  "command_id": "AA02",
                  "serial_no": "000000000001"
            }
      },
      "data2": {
            "tlr": {
                  "0005": "01",
                  "0008": "00",
                  "Firmware version number": "G1_H1.0_V1.92_E",
                  "VIN": "MAKGM669HJN301879",
                  "IMEI": "862205055389862",
                  "Reserved": "L4-V7Lg9yIzz-rJm",
                  "APN username": "CARD",
                  "APN password": "CARD1",
                  "ICCID": "89910275102004407199",
                  "APN": "www"
            }
      }
}"\n""C0000000BDAA0000000000061D480000083233303132333039323634330000000000000000000000A600140000000100187E02DE0A00290372000005951600260000004A0000040009080000004A0005000A0D0000000AD0000900154D414B474D363639484A4E333031383739000F00133836323230353035353338393836320010001031333231322E30303030303000110008000000000014000BF851084F000018001500060000002000153430344030354035363532403130363332C0""\n"{
      "data1": {
            "header": {
                  "flag_version": "00",
                  "reserved": "00",
                  "packet_length": "00BD",
                  "command_id": "AA00",
                  "serial_no": "00000000061D"
            },
            "pos": {
                  "Location status ": "located",
                  "ACC status ": "ACC OFF",
                  "GNS module ": "normal",
                  "G-sensor": "abnormal",
                  "OBD bus": "not connected",
                  "Buffer data": "not buffer data",
                  "Reserved": "reserved",
                  "8": "harsh acceleration",
                  "Date Time": "23/01/23 09:26:43",
                  "longitude": "0.0",
                  "latitude": "0.0",
                  "GPS speed": "0",
                  "Direction": "166",
                  "Altitude": "20",
                  "Odometer speed": "0",
                  "0001": {
                        "coolant_temperature": 86,
                        "RPM": 734,
                        "Average speed": "10",
                        "Fuel consumption": "0.0041",
                        "Fuel consumption per 100km": "8.82",
                        "Interval mileage": "0",
                        "Battery voltage": "14.29",
                        "Fuel level": "8.8",
                        "Fuel level 2": "38",
                        "Trip ID": "74",
                        "Adblue level": "0.0"
                  },
                  "0004": {
                        "Event Trip ID": "74"
                  },
                  "0005": {
                        "GSM signal": 13,
                        "GPS signal": 0,
                        "Run time": "2768"
                  },
                  "0009": "Vin = MAKGM669HJN301879",
                  "000F": "Imei no = 862205055389862",
                  "0010": "odometer mileage = 13212.000000",
                  "0014": {
                        "F8": "all data is valid",
                        "Engine Load": "31.76470588235294",
                        "Timing Advance": "4.0",
                        "Intake Air Temperature": "39",
                        "Air Flow Rate": "0.01",
                        "Absolute Throttle Position": "3.1372549019607843"
                  },
                  "0020": "cell info = 404@05@5652@10632"
            }
      },
      "data2": {
            "tlr": {
                  "0005": "01",
                  "0008": "00",
                  "Firmware version number": "G1_H1.0_V1.92_E",
                  "VIN": "MAKGM669HJN301879",
                  "IMEI": "862205055389862",
                  "Reserved": "L4-V7Lg9yIzz-rJm",
                  "APN username": "CARD",
                  "APN password": "CARD1",
                  "ICCID": "89910275102004407199",
                  "APN": "www"
            }
      }
}
roneskinder2 years ago
HS-5000G example data string: 

C0000000AEAA00000000000005CC000001313937303031303130303032006F41DF001F2030005400120013005F0001001879087A5902A335D102730576000000FFFFFFFF7E0005000A1F01000000A8000900154D414A3258584D524A32444A3631353039000F00133836323230353035353338393836320010000F363535332E36303030393800110008000000000014000BF800004F130A4B00150006000000200016343034403035403040323036333132343438C0

C0 - start
000000AEAA00000000000005 - packet header
CC00 - status bits
0001 - break point data
313937303031303130303032 - UTC time
006F41DF - longitude: 72.91359
001F2030 - latitude: 20.39856
0054 - GPS speed
0012 - direction
0013 - altitude
005F - Odometer speed
0001001879087A5902A335D102730576000000FFFFFFFF7E - OBD info
0005000A1F01000000A8 - State Info
000900154D414A3258584D524A32444A3631353039 - VIN
0010000F363535332E363030303938 - Odometer mileage
0011000800000000 - Engine hour
0014000BF800004F130A4B - OBD data 2
001500060000- EV data
00200016343034403035403040323036333132343438  - Cell Info
C0 - end


ATTN: If the longitude & latitude is 0,0, it means GPS location is invalid, server needs to ignore invalid position & keep the last valid position, so the tracking route will not point to 0,0. 


Example login request: 
C000000095AA020000000000010001001347315F48312E305F56312E38395F45000200154D414B474D363639484A4E33303138373900030013383632323035303535333839383632000400144C342D56374C673979497A7A2D724A6D000500050100060008434152440007000943415244310008000500000900183839393130323735313032303034343037313939000A0007777777C0

Example login answer (simple answer without UTC time):
C00100000DFF0300000000000100C0

Example login answer (with UTC time, which is neccessary):
C00100001BFF03000000000001000015000E31363736353333343430C0
roneskinder2 years ago

Added the missing ALARMS and updated decodePosition().

/*
 * Copyright 2016 - 2021 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.traccar.protocol;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.traccar.BaseProtocolDecoder;
import org.traccar.session.DeviceSession;
import org.traccar.NetworkMessage;
import org.traccar.Protocol;
import org.traccar.helper.BitUtil;
import org.traccar.helper.DateBuilder;
import org.traccar.helper.UnitsConverter;
import org.traccar.model.CellTower;
import org.traccar.model.Network;
import org.traccar.model.Position;
import org.traccar.model.WifiAccessPoint;

import java.net.SocketAddress;
import java.nio.charset.StandardCharsets;

public class HuaShengProtocolDecoder extends BaseProtocolDecoder {

    public HuaShengProtocolDecoder(Protocol protocol) {
        super(protocol);
    }

    public static final int MSG_POSITION = 0xAA00;
    public static final int MSG_POSITION_RSP = 0xFF01;
    public static final int MSG_LOGIN = 0xAA02;
    public static final int MSG_LOGIN_RSP = 0xFF03;
    public static final int MSG_UPFAULT = 0xAA12;
    public static final int MSG_UPFAULT_RSP = 0xFF13;
    public static final int MSG_HSO_REQ = 0x0002;
    public static final int MSG_HSO_RSP = 0x0003;

    private void sendResponse(Channel channel, int type, int index, ByteBuf content) {
        if (channel != null) {
            ByteBuf response = Unpooled.buffer();
            response.writeByte(0xC0);
            response.writeShort(0x0100);
            response.writeShort(12 + (content != null ? content.readableBytes() : 0));
            response.writeShort(type);
            response.writeShort(0);
            response.writeInt(index);
            if (content != null) {
                response.writeBytes(content);
                content.release();
            }
            response.writeByte(0xC0);
            channel.writeAndFlush(new NetworkMessage(response, channel.remoteAddress()));
        }
    }

    private String decodeAlarm(int event) {
        switch (event) {
            case 3:
                return Position.ALARM_OVERSPEED;
            case 4:
                return Position.ALARM_FATIGUE_DRIVING;
            case 6:
                return Position.ALARM_SOS;
            case 7:
                return Position.ALARM_BRAKING;
            case 8:
                return Position.ALARM_ACCELERATION;
            case 9:
                return Position.ALARM_CORNERING;
            case 10:
            case 11:
                return Position.ALARM_ACCIDENT;
            case 12:
                return Position.ALARM_HIGH_RPM;
            case 14:
                return Position.ALARM_IDLE;
            case 16:
                return Position.ALARM_REMOVING;
            case 20:
                return Position.ALARM_POWER_OFF;
            case 21:
                return Position.ALARM_POWER_ON;
            case 22:
                return Position.ALARM_TOW;
            case 30:
                return Position.KEY_COOLANT_TEMP;
            default:
                return null;
        }
    }

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

        ByteBuf buf = (ByteBuf) msg;

        buf.skipBytes(1); // start marker
        buf.readUnsignedByte(); // flag
        buf.readUnsignedByte(); // reserved
        buf.readUnsignedShort(); // length

        int type = buf.readUnsignedShort();

        buf.readUnsignedShort(); // checksum
        int index = buf.readInt();

        if (type == MSG_LOGIN) {

            while (buf.readableBytes() > 4) {
                int subtype = buf.readUnsignedShort();
                int length = buf.readUnsignedShort() - 4;
                if (subtype == 0x0003) {
                    String imei = buf.readCharSequence(length, StandardCharsets.US_ASCII).toString();
                    DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
                    if (deviceSession != null && channel != null) {
                        ByteBuf content = Unpooled.buffer();
                        content.writeByte(0); // success
                        sendResponse(channel, MSG_LOGIN_RSP, index, content);
                    }
                } else {
                    buf.skipBytes(length);
                }
            }

        } else if (type == MSG_HSO_REQ) {

            sendResponse(channel, MSG_HSO_RSP, index, null);

        } else if (type == MSG_UPFAULT) {

            return decodeFaultCodes(channel, remoteAddress, buf);

        } else if (type == MSG_POSITION) {

            return decodePosition(channel, remoteAddress, buf, index);

        }

        return null;
    }

    private Position decodeFaultCodes(
            Channel channel, SocketAddress remoteAddress, ByteBuf buf) {

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

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        getLastLocation(position, null);

        buf.readUnsignedShort(); // type
        buf.readUnsignedShort(); // length

        StringBuilder codes = new StringBuilder();
        while (buf.readableBytes() > 2) {
            String value = ByteBufUtil.hexDump(buf.readSlice(2));
            int digit = Integer.parseInt(value.substring(0, 1), 16);
            char prefix;
            switch (digit >> 2) {
                default:
                    prefix = 'P';
                    break;
                case 1:
                    prefix = 'C';
                    break;
                case 2:
                    prefix = 'B';
                    break;
                case 3:
                    prefix = 'U';
                    break;
            }
            codes.append(prefix).append(digit % 4).append(value.substring(1));
            if (buf.readableBytes() > 2) {
                codes.append(' ');
            }
        }

        position.set(Position.KEY_DTCS, codes.toString());

        return position;
    }

    private Position decodePosition(
            Channel channel, SocketAddress remoteAddress, ByteBuf buf, int index) {

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

        Position position = new Position(getProtocolName());
        position.setDeviceId(deviceSession.getDeviceId());

        int status = buf.readUnsignedShort();

        position.setValid(BitUtil.check(status, 15));

        position.set(Position.KEY_STATUS, status);
        position.set(Position.KEY_IGNITION, BitUtil.check(status, 14));

        int event = buf.readUnsignedShort();
        position.set(Position.KEY_ALARM, decodeAlarm(event));
        position.set(Position.KEY_EVENT, event);

        String time = buf.readCharSequence(12, StandardCharsets.US_ASCII).toString();

        DateBuilder dateBuilder = new DateBuilder()
                .setYear(Integer.parseInt(time.substring(0, 2)))
                .setMonth(Integer.parseInt(time.substring(2, 4)))
                .setDay(Integer.parseInt(time.substring(4, 6)))
                .setHour(Integer.parseInt(time.substring(6, 8)))
                .setMinute(Integer.parseInt(time.substring(8, 10)))
                .setSecond(Integer.parseInt(time.substring(10, 12)));
        position.setTime(dateBuilder.getDate());

        position.setLongitude(buf.readInt() * 0.00001);
        position.setLatitude(buf.readInt() * 0.00001);

        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedShort()));
        position.setCourse(buf.readUnsignedShort());
        position.setAltitude(buf.readUnsignedShort());

        position.set(Position.KEY_ODOMETER, buf.readUnsignedShort() * 1000);

        Network network = new Network();

        while (buf.readableBytes() > 4) {
            int subtype = buf.readUnsignedShort();
            int length = buf.readUnsignedShort() - 4;
            switch (subtype) {
                case 0x0001:
                    int coolantTemperature = buf.readUnsignedByte() - 40;
                    if (coolantTemperature <= 215) {
                        position.set(Position.KEY_COOLANT_TEMP, coolantTemperature);
                    }
                    int rpm = buf.readUnsignedShort();
                    if (rpm <= 65535) {
                        position.set(Position.KEY_RPM, rpm);
                    }
                    position.set("averageSpeed", buf.readUnsignedByte());
                    buf.readUnsignedShort(); // interval fuel consumption
                    position.set(Position.KEY_FUEL_CONSUMPTION, buf.readUnsignedShort() * 0.01);
                    position.set(Position.KEY_ODOMETER_TRIP, buf.readUnsignedShort());
                    position.set(Position.KEY_POWER, buf.readUnsignedShort() * 0.01);
                    position.set(Position.KEY_FUEL_LEVEL, buf.readUnsignedByte() * 0.4);
                    position.set(Position.KEY_FUEL_USED, buf.readUnsignedByte());
                    buf.readUnsignedInt(); // trip id
                    buf.readUnsignedByte(); //1 byte ADblue level, unit: 0.4%
                    break;
                case 0x0005:
                    position.set(Position.KEY_RSSI, buf.readUnsignedByte());
                    position.set(Position.KEY_HDOP, buf.readUnsignedByte());
                    buf.readUnsignedInt(); // run time
                    break;
                case 0x0009:
                    position.set(
                            Position.KEY_VIN, buf.readCharSequence(length, StandardCharsets.US_ASCII).toString());
                    break;
                case 0x0010:
                    position.set(
                            Position.KEY_OBD_ODOMETER, buf.readCharSequence(length, StandardCharsets.US_ASCII).toString());
                    break;
                case 0x0011:
                    position.set(Position.KEY_HOURS, buf.readUnsignedInt() * 0.05);
                    break;
                case 0x0014:
                    position.set(Position.KEY_ENGINE_LOAD, buf.readUnsignedByte() / 255.0);
                    position.set("timingAdvance", buf.readUnsignedByte() * 0.5);
                    position.set("airTemp", buf.readUnsignedByte() - 40);
                    position.set("airFlow", buf.readUnsignedShort() * 0.01);
                    position.set(Position.KEY_THROTTLE, buf.readUnsignedByte() / 255.0);
                    break;
                case 0x0020:
                    String[] cells = buf.readCharSequence(
                            length, StandardCharsets.US_ASCII).toString().split("\\+");
                    for (String cell : cells) {
                        String[] values = cell.split("@");
                        network.addCellTower(CellTower.from(
                                Integer.parseInt(values[0]), Integer.parseInt(values[1]),
                                Integer.parseInt(values[2], 16), Integer.parseInt(values[3], 16)));
                    }
                    break;
                case 0x0021:
                    String[] points = buf.readCharSequence(
                            length, StandardCharsets.US_ASCII).toString().split("\\+");
                    for (String point : points) {
                        String[] values = point.split("@");
                        network.addWifiAccessPoint(WifiAccessPoint.from(values[0], Integer.parseInt(values[1])));
                    }
                    break;
                default:
                    buf.skipBytes(length);
                    break;
            }
        }

        if (network.getCellTowers() != null || network.getWifiAccessPoints() != null) {
            position.setNetwork(network);
        }

        sendResponse(channel, MSG_POSITION_RSP, index, null);

        return position;
    }

}
roneskinder2 years ago

Didnt work, im getting this error now, what am i missing?

INFO: [Ta4990112] error - readerIndex(72) + length(1276) exceeds writerIndex(183): UnpooledByteBufAllocator$InstrumentedUnpooledHeapByteBuf(ridx: 72, widx: 183, cap: 183) - IndexOutOfBoundsException (... < HuaShengProtocolDecoder:312 < *:150 < ExtendedObjectDecoder:72 < ... < WrapperContext:102 < ...)