int OBD2_request_PID(unsigned char pid, int *value, size_t timeout)
{
    CAN_msg msg;
    msg.addressValue = 0x7df;
    msg.data[0] = 2;
    msg.data[1] = 1;
    msg.data[2] = pid;
    msg.data[3] = 0x55;
    msg.data[4] = 0x55;
    msg.data[5] = 0x55;
    msg.data[6] = 0x55;
    msg.data[7] = 0x55;
    msg.dataLength = 8;
    msg.isExtendedAddress = 0;

    int pid_request_success = 0;

    if (CAN_tx_msg(0, &msg, timeout)) {
        size_t start_time = xTaskGetTickCount();
        while (!isTimeoutMs(start_time, OBD2_PID_DEFAULT_TIMEOUT_MS)) {
            int result = CAN_rx_msg(0, &msg, OBD2_PID_DEFAULT_TIMEOUT_MS);
            if (result) {
                result = decode_pid(pid, &msg, value);
                if (result) {
                    pid_request_success = 1;
                    if (DEBUG_LEVEL) {
                        pr_debug("read OBD2 PID ");
                        pr_debug_int(pid);
                        pr_debug("=")
                        pr_debug_int(*value);
                        pr_debug("\r\n");
                    }
                    break;
                }
            }
        }
    }
    return pid_request_success;
}
Ejemplo n.º 2
0
static void fileWriterTask(void *params)
{
        LoggerMessage *msg = NULL;
        struct logging_status ls;
        memset(&ls, 0, sizeof(struct logging_status));

        while(1) {
                int rc = -1;

                /* Get a sample. */
                xQueueReceive(g_sampleRecordQueue, &(msg), portMAX_DELAY);

                switch (msg->type) {
                case LoggerMessageType_Sample:
                        rc = logging_sample(&ls, msg);
                        break;
                case LoggerMessageType_Start:
                        rc = logging_start(&ls);
                        break;
                case LoggerMessageType_Stop:
                        rc = logging_stop(&ls);
                        break;
                default:
                        pr_warning("Unsupported message type\r\n");
                }

                /* Turns the LED on if things are bad, off otherwise. */
                error_led(rc);
                if (rc) {
                        pr_debug("Msg type ");
                        pr_debug_int(msg->type);
                        pr_debug_int_msg(" failed with code ", rc);
                }

                flush_logfile(&ls);
        }
}