Search in sources :

Example 1 with EcuCodeItem

use of com.fr3ts0n.ecu.EcuCodeItem 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)

Example 2 with EcuCodeItem

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

the class Kw1281Prot method handleTelegram.

/**
 * handle incoming protocol telegram
 * default implementaion only checks telegram and notifies listeners with
 * protocol payload
 *
 * @param buffer - telegram buffer
 * @return number of listeners notified
 */
@Override
public int handleTelegram(char[] buffer) {
    int cnt = 0;
    log.fine("RX:" + ProtUtils.hexDumpBuffer(buffer));
    // if telegram is OK
    if (checkTelegram(buffer)) {
        // get command
        int cmd = getParamInt(FLD_ID_CMD, buffer);
        // update block counter
        blockCounter = (char) getParamInt(FLD_ID_BLKCNT, buffer).intValue();
        // get payload
        char[] payLoad = getPayLoad(buffer);
        switch(cmd) {
            case ID_GRPINFO_HEAD:
                // field number within group
                int fldId = 0;
                // loop through all data items within telegram
                for (int i = 0; i < payLoad.length; ) {
                    // get data id from group reading
                    int dId = (int) payLoad[i];
                    // get the corresponding data items for svc/pid
                    Vector<EcuDataItem> pidItems = itms.getPidDataItems(ID_GRPINFO_DATA, dId);
                    // if item was found ...
                    if (pidItems != null && pidItems.size() > 0) {
                        EcuDataItem currItm;
                        // if item already exists
                        if (currGrpItems.size() > fldId) {
                            // re-use existing item
                            currItm = currGrpItems.get(fldId);
                        } else {
                            // otherwise clone a new one
                            currItm = (EcuDataItem) pidItems.get(0).clone();
                            // set specific values for this item
                            currItm.pid = (int) getCurrDataGroup();
                            currItm.ofs += fldId;
                            currItm.pv.put(EcuDataPv.FID_PID, Integer.valueOf(currItm.pid));
                            currItm.pv.put(EcuDataPv.FID_OFS, Integer.valueOf(currItm.ofs));
                            // ensure there are enough elements in list
                            while (currGrpItems.size() <= fldId) currGrpItems.add(null);
                            // add item to list of group data items
                            currGrpItems.set(fldId, currItm);
                        }
                        // update metadata
                        int numTblChrs = payLoad[i + 2];
                        // if conversion is a VagConversion, we may adjust meta parameters
                        if (currItm.cnv[0] instanceof VagConversion) {
                            // set meta parameter for conversion
                            ((VagConversion) currItm.cnv[0]).setMetaNw(payLoad[i + 1]);
                            // any following table data available?
                            if (numTblChrs != 0) {
                                // set table values for conversion
                                ((VagConversion) currItm.cnv[0]).setMetaTblValues(String.valueOf(payLoad, i + 3, numTblChrs).toCharArray());
                            }
                        }
                        // increment byte index to next entry
                        i += 3 + numTblChrs;
                        // increment field number
                        fldId++;
                    }
                }
                showGroupItems(getCurrDataGroup());
                // now request again to get real data
                requestGroupData(getCurrDataGroup());
                break;
            case ID_GRPINFO_DATA:
                if (currGrpItems == null || currGrpItems.size() < BLK_NUM_ITEMS) {
                    log.severe(String.format("Missing/Incomplete Metadata for GRP:%d", (int) getCurrDataGroup()));
                }
                {
                    for (int i = 0; i < Math.min(payLoad.length, currGrpItems.size()); i++) {
                        EcuDataItem currItm = currGrpItems.get(i);
                        if (currItm != null) {
                            // and update corresponding process var
                            currItm.updatePvFomBuffer(payLoad);
                        } else {
                            log.severe(String.format("Data w/o meta GRP:%d, ITM:%d", (int) getCurrDataGroup(), i));
                        }
                    }
                }
                // if we do group reading and desired group has not changed ...
                if (service == SVC_READ_DATA_GRP && getCurrDataGroup() == getSelectedDataGroup()) {
                    // request same group again
                    requestGroupData(getCurrDataGroup());
                } else {
                    // otherwise close this group by  requesting ACK
                    requestACK();
                }
                break;
            case ID_GRPREAD_DATA:
                // loop through all 4 entries of group
                for (int i = 0; i < payLoad.length; i += 3) {
                    // get data it from group reading
                    int dId = (int) payLoad[i];
                    // get the corresponding data items for svc/pid
                    Vector<EcuDataItem> pidItems = itms.getPidDataItems(cmd, dId);
                    // if item was found ...
                    if (pidItems != null) {
                        // loop through all data items for this dId
                        Iterator<EcuDataItem> it = pidItems.iterator();
                        while (it.hasNext()) {
                            EcuDataItem currItm = (EcuDataItem) it.next().clone();
                            // set specific values for this item
                            currItm.pid = (int) getCurrDataGroup();
                            currItm.ofs += i / 3;
                            currItm.pv.put(EcuDataPv.FID_PID, Integer.valueOf(currItm.pid));
                            currItm.pv.put(EcuDataPv.FID_OFS, Integer.valueOf(currItm.ofs));
                            // ensure there are enough elements in list
                            while (currGrpItems.size() < (i / 3)) currGrpItems.add(null);
                            // add item to list of group data items
                            currGrpItems.set(i / 3, currItm);
                            // and update corresponding process var
                            currItm.updatePvFomBuffer(Arrays.copyOfRange(payLoad, i + 1, i + 3));
                        }
                    } else {
                        log.warning("Unknown data ID:" + dId);
                    }
                }
                // if we do group reading and desired group has not changed ...
                if (service == SVC_READ_DATA_GRP && getCurrDataGroup() == getSelectedDataGroup()) {
                    // request same group again
                    requestGroupData(getCurrDataGroup());
                } else {
                    // otherwise close this group by  requesting ACK
                    requestACK();
                }
                break;
            case CMD_ACK:
                switch(service) {
                    case SVC_READ_DATA_ALL:
                        // request next available data group
                        requestGroupData(getNextDataGroup());
                        break;
                    case SVC_READ_DATA_GRP:
                        // ensure new selection of group is updated
                        setCurrDataGroup(getSelectedDataGroup());
                        // request same group again
                        requestGroupData(getCurrDataGroup());
                        break;
                    case SVC_READ_DFCS:
                        // clear fault code list
                        tCodes.clear();
                        setNumCodes(0);
                        // request failure codes
                        requestDFCs();
                        break;
                    case SVC_CLEAR_DFCS:
                        // Request clearing DFCs ...
                        requestClearDFCs();
                        // ... and continue with Reading DFCc
                        setService(SVC_READ_DFCS);
                        break;
                    case SVC_SHUTDOWN:
                        requestEndComm();
                        // now set to Service NONE
                        setService(SVC_FINISHED);
                        break;
                    case SVC_NONE:
                    default:
                        requestACK();
                        break;
                }
                break;
            case ID_NODATA:
                log.info(String.format("NODATA: Group:%d -> remove", (int) getCurrDataGroup()));
                // since data is not available, remove group from map ...
                knownGrpItems.remove(Integer.valueOf(getCurrDataGroup()));
                // if we finished the initial turaround, save preset data
                if (currDataGroup == 0) {
                    savePreset();
                }
                // request ACK
                requestACK();
                break;
            case ID_DFC_DATA:
                int nCodes = numCodes;
                // loop through all codes reported
                for (int i = 0; i < payLoad.length; i += 3) {
                    // get code number and status
                    int dfcNum = getParamInt(i, 2, payLoad);
                    int dfcStat = getParamInt(i + 2, 1, payLoad);
                    // enter code into code list for visualisation
                    EcuCodeItem code = knownCodes.get(dfcNum);
                    // if code is not known ...
                    if (code == null) {
                        // create new one and add it to list of known codes
                        code = new EcuCodeItem(dfcNum, "Unknown Fault code");
                    }
                    code.put(EcuCodeItem.FID_STATUS, Integer.valueOf(dfcStat));
                    tCodes.put(dfcNum, code);
                    if (dfcNum != 0xFFFF) {
                        nCodes++;
                        // set the MIL status based on code status
                        nCodes |= dfcStat & 0x80;
                    }
                }
                // set number of codes
                setNumCodes(nCodes);
                // finish service since we received corresponding answer
                setService(SVC_NONE);
                // further DFCs may still come after ACK ...
                requestACK();
                break;
            case ID_ASCII_DATA:
                // save vehicle ID data
                EcuDataPv pv = new EcuDataPv();
                pv.put(EcuDataPv.FID_DESCRIPT, "ID");
                pv.put(EcuDataPv.FID_VALUE, String.valueOf(payLoad));
                VidPvs.put(VidPvs.size(), pv);
                // if this is the first VID dataset, try to load corresponding preset
                if (VidPvs.size() == 1)
                    loadPreset();
                // send ACK
                requestACK();
                break;
            default:
                requestACK();
                break;
        }
        cnt++;
    }
    return (cnt);
}
Also used : VagConversion(com.fr3ts0n.ecu.VagConversion) EcuDataPv(com.fr3ts0n.ecu.EcuDataPv) EcuCodeItem(com.fr3ts0n.ecu.EcuCodeItem) EcuDataItem(com.fr3ts0n.ecu.EcuDataItem)

Example 3 with EcuCodeItem

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

the class MainActivity method onItemLongClick.

/**
 * Handle long licks on OBD data list items
 */
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent;
    switch(CommService.elm.getService()) {
        /* if we are in OBD data mode:
		     * ->Long click on an item starts the single item dashboard activity
		     */
        case ObdProt.OBD_SVC_DATA:
            EcuDataPv pv = (EcuDataPv) getListAdapter().getItem(position);
            /* only numeric values may be shown as graph/dashboard */
            if (pv.get(EcuDataPv.FID_VALUE) instanceof Number) {
                DashBoardActivity.setAdapter(getListAdapter());
                intent = new Intent(this, DashBoardActivity.class);
                intent.putExtra(DashBoardActivity.POSITIONS, new int[] { position });
                startActivity(intent);
            }
            break;
        /* If we are in DFC mode of any kind
			 * -> Long click leads to a web search for selected DFC
			 */
        case ObdProt.OBD_SVC_READ_CODES:
        case ObdProt.OBD_SVC_PERMACODES:
        case ObdProt.OBD_SVC_PENDINGCODES:
            try {
                intent = new Intent(Intent.ACTION_WEB_SEARCH);
                EcuCodeItem dfc = (EcuCodeItem) getListAdapter().getItem(position);
                intent.putExtra(SearchManager.QUERY, "OBD " + String.valueOf(dfc.get(EcuCodeItem.FID_CODE)));
                startActivity(intent);
            } catch (Exception e) {
                log.log(Level.SEVERE, "WebSearch DFC", e);
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
            }
            break;
    }
    return true;
}
Also used : EcuDataPv(com.fr3ts0n.ecu.EcuDataPv) Intent(android.content.Intent) EcuCodeItem(com.fr3ts0n.ecu.EcuCodeItem) IOException(java.io.IOException)

Example 4 with EcuCodeItem

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

the class VagTestFrame method cbAddressActionPerformed.

// GEN-LAST:event_tabMainStateChanged
private // GEN-FIRST:event_cbAddressActionPerformed
void cbAddressActionPerformed(// GEN-FIRST:event_cbAddressActionPerformed
java.awt.event.ActionEvent evt) {
    // GEN-HEADEREND:event_cbAddressActionPerformed
    EcuCodeItem itm = (EcuCodeItem) cbAddress.getSelectedItem();
    int newAddress = Integer.parseInt(itm.get(EcuCodeItem.FID_CODE).toString(), 16);
    if (newAddress > 0) {
        setControllerAddress(newAddress);
    }
}
Also used : EcuCodeItem(com.fr3ts0n.ecu.EcuCodeItem)

Aggregations

EcuCodeItem (com.fr3ts0n.ecu.EcuCodeItem)4 EcuDataPv (com.fr3ts0n.ecu.EcuDataPv)2 Intent (android.content.Intent)1 EcuDataItem (com.fr3ts0n.ecu.EcuDataItem)1 ObdCodeItem (com.fr3ts0n.ecu.ObdCodeItem)1 VagConversion (com.fr3ts0n.ecu.VagConversion)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 IOException (java.io.IOException)1