Search in sources :

Example 1 with ObdCodeItem

use of com.fr3ts0n.ecu.ObdCodeItem in project AndrOBD by fr3ts0n.

the class ObdProt method handleTelegram.

/**
 * handle OBD response telegram
 * @param buffer - telegram buffer
 * @return number of listeners notified
 */
@Override
@SuppressWarnings("fallthrough")
public synchronized int handleTelegram(char[] buffer) {
    int result = 0;
    int msgPid;
    if (checkTelegram(buffer)) {
        try {
            msgService = (Integer) getParamValue(ID_OBD_SVC, buffer);
            // check for negative result
            if (msgService == OBD_ID_NRC) {
                // get NR service
                int svc = (Integer) getParamValue(ID_NR_SVC, buffer);
                // get NRC code
                int nrcCode = (Integer) getParamValue(ID_NR_CODE, buffer);
                // get NRC object
                NRC nrc = NRC.get(nrcCode);
                // create NRC error message
                String error = String.format(nrc.toString(svc));
                // log error
                log.severe(error);
                if (isResetOnNrc()) {
                    // perform immediate reset because NRC reception
                    reset();
                } else {
                    // otherwise just switch off any active service
                    setService(OBD_SVC_NONE, true);
                }
                // notify change listeners
                firePropertyChange(new PropertyChangeEvent(this, PROP_NRC, null, error));
                // handling finished
                return result;
            }
            // positive response -> mask service ID
            msgService &= ~0x40;
            // check service of message
            switch(msgService) {
                // OBD Data frame
                case OBD_SVC_FREEZEFRAME:
                case OBD_SVC_DATA:
                    msgPid = (Integer) getParamValue(ID_OBD_PID, buffer);
                    switch(msgPid) {
                        case 0x00:
                        case 0x20:
                        case 0x40:
                        case 0x60:
                        case 0x80:
                        case 0xA0:
                        case 0xC0:
                        case 0xE0:
                            long msgPayload = Long.valueOf(new String(getPayLoad(buffer)), 16);
                            markSupportedPids(msgService, msgPid, msgPayload, PidPvs);
                            break;
                        // OBD number of fault codes
                        case 1:
                            msgPayload = ((Integer) getParamValue(ID_NUM_CODES, NUMCODE_PARAMETERS, buffer)).longValue();
                            setNumCodes(Long.valueOf(msgPayload).intValue());
                        // no break here ...
                        default:
                            dataItems.updateDataItems(msgService, msgPid, hexToBytes(String.valueOf(getPayLoad(buffer))));
                            break;
                    }
                    break;
                // get vehicle information (mode 9)
                case OBD_SVC_VEH_INFO:
                    msgPid = (Integer) getParamValue(ID_OBD_PID, buffer);
                    switch(msgPid) {
                        case 0x00:
                        case 0x20:
                        case 0x40:
                        case 0x60:
                        case 0x80:
                        case 0xA0:
                        case 0xC0:
                        case 0xE0:
                            long msgPayload = Long.valueOf(new String(getPayLoad(buffer)), 16);
                            markSupportedPids(msgService, msgPid, msgPayload, VidPvs);
                            break;
                        default:
                            dataItems.updateDataItems(msgService, msgPid, hexToBytes(String.valueOf(getPayLoad(buffer))));
                            break;
                    }
                    break;
                // fault code response
                case OBD_SVC_READ_CODES:
                case OBD_SVC_PENDINGCODES:
                case OBD_SVC_PERMACODES:
                    int currCode;
                    Integer key;
                    EcuCodeItem code;
                    int nCodes = 0;
                    // default DTC data to start at offset 2 (Byte 1)
                    int DTCOffs = 2;
                    // If message contains optional number of codes (1 Byte) then set it ...
                    if ((buffer.length % 4) == 0) {
                        nCodes = Integer.valueOf(new String(buffer, 2, 2), 16);
                        setNumCodes(nCodes);
                        // DTC data starts at offset 4 (byte 2)
                        DTCOffs = 4;
                    }
                    // read in all trouble codes
                    for (int i = DTCOffs; i < buffer.length; i += 4) {
                        key = Integer.valueOf(new String(buffer, i, 4), 16);
                        currCode = key.intValue();
                        if (currCode != 0) {
                            if ((code = (EcuCodeItem) knownCodes.get(key)) == null) {
                                code = new ObdCodeItem(key.intValue(), Messages.getString("customer.specific.trouble.code.see.manual"));
                            }
                            log.fine(String.format("+DFC: %04x: %s", key, code.toString()));
                            tCodes.put(key, code);
                            // increment number of codes
                            nCodes++;
                        }
                    }
                    if (nCodes == 0) {
                        tCodes.put(0, new ObdCodeItem(0, Messages.getString("no.trouble.codes.set")));
                    }
                    break;
                // clear code response
                case OBD_SVC_CLEAR_CODES:
                    break;
                default:
                    log.warning("Service not (yet) supported: " + msgService);
            }
        } catch (Exception e) {
            log.warning("'" + buffer.toString() + "':" + e.getMessage());
        }
    }
    return (result);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) ObdCodeItem(com.fr3ts0n.ecu.ObdCodeItem) EcuCodeItem(com.fr3ts0n.ecu.EcuCodeItem)

Aggregations

EcuCodeItem (com.fr3ts0n.ecu.EcuCodeItem)1 ObdCodeItem (com.fr3ts0n.ecu.ObdCodeItem)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1