2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file tunerstudio.cpp
|
|
|
|
* @brief Binary protocol implementation
|
|
|
|
*
|
|
|
|
* This implementation would not happen without the documentation
|
|
|
|
* provided by Jon Zeeff (jon@zeeff.com)
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @brief Integration with EFI Analytics Tuner Studio software
|
|
|
|
*
|
|
|
|
* Tuner Studio has a really simple protocol, a minimal implementation
|
|
|
|
* capable of displaying current engine state on the gauges would
|
|
|
|
* require only two commands: queryCommand and ochGetCommand
|
|
|
|
*
|
|
|
|
* queryCommand:
|
|
|
|
* Communication initialization command. TunerStudio sends a single byte H
|
|
|
|
* ECU response:
|
2018-01-24 12:08:10 -08:00
|
|
|
* One of the known ECU id strings.
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* ochGetCommand:
|
|
|
|
* Request for output channels state.TunerStudio sends a single byte O
|
|
|
|
* ECU response:
|
|
|
|
* A snapshot of output channels as described in [OutputChannels] section of the .ini file
|
|
|
|
* The length of this block is 'ochBlockSize' property of the .ini file
|
|
|
|
*
|
|
|
|
* These two commands are enough to get working gauges. In order to start configuring the ECU using
|
|
|
|
* tuner studio, three more commands should be implemented:
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @date Oct 22, 2013
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* This file is part of rusEfi - see http://rusefi.com
|
|
|
|
*
|
|
|
|
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
|
|
|
|
* the GNU General Public License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This file is part of rusEfi - see http://rusefi.com
|
|
|
|
*
|
|
|
|
* rusEfi is free software; you can redistribute it and/or modify it under the terms of
|
|
|
|
* the GNU General Public License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* rusEfi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
|
|
|
|
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-08-03 19:05:01 -07:00
|
|
|
#include "pch.h"
|
2019-07-04 00:57:21 -07:00
|
|
|
#include "os_access.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "tunerstudio.h"
|
2020-12-08 00:05:43 -08:00
|
|
|
#include "tunerstudio_impl.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#include "flash_main.h"
|
|
|
|
|
|
|
|
#include "tunerstudio_io.h"
|
|
|
|
#include "malfunction_central.h"
|
|
|
|
#include "console_io.h"
|
|
|
|
#include "crc.h"
|
2017-06-04 05:50:31 -07:00
|
|
|
#include "bluetooth.h"
|
2019-01-05 04:55:21 -08:00
|
|
|
#include "tunerstudio_io.h"
|
2019-07-06 17:48:58 -07:00
|
|
|
#include "tooth_logger.h"
|
2019-11-27 19:07:36 -08:00
|
|
|
#include "electronic_throttle.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include <string.h>
|
2020-03-26 05:03:55 -07:00
|
|
|
#include "bench_test.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "svnversion.h"
|
|
|
|
#include "status_loop.h"
|
2016-05-06 21:01:23 -07:00
|
|
|
#include "mmc_card.h"
|
2019-10-13 13:14:08 -07:00
|
|
|
|
2020-07-02 09:33:31 -07:00
|
|
|
#include "signature.h"
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "rusEfiFunctionalTest.h"
|
2020-03-28 17:14:17 -07:00
|
|
|
#endif /* EFI_SIMULATOR */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
static void printErrorCounters() {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d",
|
2021-12-07 17:18:47 -08:00
|
|
|
sizeof(engine->outputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter,
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter);
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TunerStudio W=%d / C=%d / P=%d", tsState.writeValueCommandCounter,
|
2020-01-07 04:56:45 -08:00
|
|
|
tsState.writeChunkCommandCounter, tsState.pageCommandCounter);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
|
|
|
|
/* 1S */
|
|
|
|
#define TS_COMMUNICATION_TIMEOUT TIME_MS2I(1000)
|
|
|
|
|
|
|
|
extern persistent_config_container_s persistentState;
|
|
|
|
|
|
|
|
static efitimems_t previousWriteReportMs = 0;
|
|
|
|
|
2021-11-15 04:02:34 -08:00
|
|
|
static void resetTs() {
|
2021-11-10 04:29:40 -08:00
|
|
|
memset(&tsState, 0, sizeof(tsState));
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void printTsStats(void) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_PROD_CODE
|
2021-09-18 15:03:31 -07:00
|
|
|
#ifdef EFI_CONSOLE_RX_BRAIN_PIN
|
2022-01-08 16:38:02 -08:00
|
|
|
efiPrintf("Primary UART RX", hwPortname(EFI_CONSOLE_RX_BRAIN_PIN));
|
|
|
|
efiPrintf("Primary UART TX", hwPortname(EFI_CONSOLE_TX_BRAIN_PIN));
|
2021-09-18 15:03:31 -07:00
|
|
|
#endif
|
2021-09-18 13:58:35 -07:00
|
|
|
|
2019-04-01 08:39:25 -07:00
|
|
|
if (false) {
|
|
|
|
// todo: is this code needed somewhere else?
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TS RX on %s", hwPortname(engineConfiguration->binarySerialRxPin));
|
2015-11-09 16:03:32 -08:00
|
|
|
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TS TX on %s @%d", hwPortname(engineConfiguration->binarySerialTxPin),
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->tunerStudioSerialSpeed);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
|
|
|
|
printErrorCounters();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setTsSpeed(int value) {
|
2021-11-17 00:54:21 -08:00
|
|
|
engineConfiguration->tunerStudioSerialSpeed = value;
|
2015-07-10 06:01:56 -07:00
|
|
|
printTsStats();
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_BLUETOOTH_SETUP
|
2020-05-02 22:43:39 -07:00
|
|
|
|
2017-06-04 15:07:02 -07:00
|
|
|
// Bluetooth HC-05 module initialization start (it waits for disconnect and then communicates to the module)
|
|
|
|
static void bluetoothHC05(const char *baudRate, const char *name, const char *pinCode) {
|
2021-03-28 06:06:36 -07:00
|
|
|
bluetoothStart(getBluetoothChannel(), BLUETOOTH_HC_05, baudRate, name, pinCode);
|
2017-06-04 15:07:02 -07:00
|
|
|
}
|
2020-05-02 22:43:39 -07:00
|
|
|
|
2017-06-04 05:50:31 -07:00
|
|
|
// Bluetooth HC-06 module initialization start (it waits for disconnect and then communicates to the module)
|
|
|
|
static void bluetoothHC06(const char *baudRate, const char *name, const char *pinCode) {
|
2021-03-28 06:06:36 -07:00
|
|
|
bluetoothStart(getBluetoothChannel(), BLUETOOTH_HC_06, baudRate, name, pinCode);
|
2017-06-04 05:50:31 -07:00
|
|
|
}
|
2020-05-02 22:43:39 -07:00
|
|
|
|
2017-11-02 15:10:00 -07:00
|
|
|
// Bluetooth SPP-C module initialization start (it waits for disconnect and then communicates to the module)
|
|
|
|
static void bluetoothSPP(const char *baudRate, const char *name, const char *pinCode) {
|
2021-03-28 06:06:36 -07:00
|
|
|
bluetoothStart(getBluetoothChannel(), BLUETOOTH_SPP, baudRate, name, pinCode);
|
2017-11-02 15:10:00 -07:00
|
|
|
}
|
2017-06-04 05:50:31 -07:00
|
|
|
#endif /* EFI_BLUETOOTH_SETUP */
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
|
2021-09-18 13:58:35 -07:00
|
|
|
void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2021-09-18 13:58:35 -07:00
|
|
|
efiPrintf("%s: %s", tsChannel->name, msg);
|
2019-06-17 09:18:55 -07:00
|
|
|
#endif /* EFI_TUNER_STUDIO_VERBOSE */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-11-29 13:44:45 -08:00
|
|
|
uint8_t* getWorkingPageAddr() {
|
|
|
|
return (uint8_t*)engineConfiguration;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
static constexpr size_t getTunerStudioPageSize() {
|
|
|
|
return TOTAL_CONFIG_SIZE;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode) {
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(mode, NULL, 0);
|
2017-07-24 19:26:41 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void sendErrorCode(TsChannelBase *tsChannel, uint8_t code) {
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->writeCrcPacket(code, nullptr, 0);
|
2020-01-07 04:56:45 -08:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
void TunerStudio::sendErrorCode(TsChannelBase* tsChannel, uint8_t code) {
|
2020-12-08 23:23:02 -08:00
|
|
|
::sendErrorCode(tsChannel, code);
|
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handlePageSelectCommand(TsChannelBase *tsChannel, ts_response_format_e mode) {
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.pageCommandCounter++;
|
|
|
|
|
2020-08-18 19:37:08 -07:00
|
|
|
sendOkResponse(tsChannel, mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
|
2021-10-14 15:50:03 -07:00
|
|
|
static const void * getStructAddr(live_data_e structId) {
|
2019-07-13 06:00:03 -07:00
|
|
|
switch (structId) {
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_engine_state:
|
2019-07-13 06:00:03 -07:00
|
|
|
return static_cast<engine_state2_s*>(&engine->engineState);
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_wall_fuel_state:
|
2020-07-20 00:55:45 -07:00
|
|
|
return static_cast<wall_fuel_state*>(&engine->injectionEvents.elements[0].wallFuel);
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_trigger_central:
|
2019-07-20 11:56:56 -07:00
|
|
|
return static_cast<trigger_central_s*>(&engine->triggerCentral);
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_trigger_state:
|
2019-09-03 16:30:51 -07:00
|
|
|
return static_cast<trigger_state_s*>(&engine->triggerCentral.triggerState);
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_ac_control:
|
2021-11-25 04:59:31 -08:00
|
|
|
return static_cast<ac_control_s*>(&engine->module<AcController>().unmock());
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_fuel_pump:
|
2021-11-19 19:23:12 -08:00
|
|
|
return static_cast<fuel_pump_control_s*>(&engine->module<FuelPumpController>().unmock());
|
2022-01-06 14:58:59 -08:00
|
|
|
//#if EFI_ELECTRONIC_THROTTLE_BODY
|
|
|
|
// case LDS_ETB_PID:
|
|
|
|
// return engine->etbControllers[0]->getPidState();
|
|
|
|
//#endif /* EFI_ELECTRONIC_THROTTLE_BODY */
|
|
|
|
//
|
|
|
|
//#ifndef EFI_IDLE_CONTROL
|
|
|
|
// case LDS_IDLE_PID:
|
|
|
|
// return static_cast<pid_state_s*>(getIdlePid());
|
|
|
|
//#endif /* EFI_IDLE_CONTROL */
|
|
|
|
case LDS_idle_state:
|
2022-01-10 16:48:45 -08:00
|
|
|
return static_cast<idle_state_s*>(&engine->module<IdleController>().unmock());
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_tps_accel_state:
|
2021-10-30 19:20:29 -07:00
|
|
|
return static_cast<tps_accel_state_s*>(&engine->tpsAccelEnrichment);
|
2022-01-18 09:31:12 -08:00
|
|
|
case LDS_high_pressure_fuel_pump:
|
|
|
|
return static_cast<high_pressure_fuel_pump_s*>(&engine->module<HpfpController>().unmock());
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_main_relay:
|
2021-11-24 04:47:51 -08:00
|
|
|
return static_cast<main_relay_s*>(&engine->module<MainRelayController>().unmock());
|
2021-11-24 09:59:57 -08:00
|
|
|
#if EFI_BOOST_CONTROL
|
2022-01-06 14:58:59 -08:00
|
|
|
case LDS_boost_control:
|
2021-11-24 09:59:57 -08:00
|
|
|
return static_cast<boost_control_s*>(&engine->boostController);
|
|
|
|
#endif // EFI_BOOST_CONTROL
|
2019-07-13 06:00:03 -07:00
|
|
|
default:
|
2021-10-30 19:20:29 -07:00
|
|
|
return nullptr;
|
2019-07-13 06:00:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:18:55 -07:00
|
|
|
/**
|
|
|
|
* Read internal structure for Live Doc
|
|
|
|
* This is somewhat similar to read page and somewhat similar to read outputs
|
|
|
|
* We can later consider combining this functionality
|
|
|
|
*/
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleGetStructContent(TsChannelBase* tsChannel, int structId, int size) {
|
2019-06-17 09:18:55 -07:00
|
|
|
tsState.readPageCommandsCounter++;
|
|
|
|
|
2021-10-14 15:50:03 -07:00
|
|
|
const void *addr = getStructAddr((live_data_e)structId);
|
2020-01-20 22:47:58 -08:00
|
|
|
if (addr == nullptr) {
|
2019-06-17 09:18:55 -07:00
|
|
|
// todo: add warning code - unexpected structId
|
|
|
|
return;
|
|
|
|
}
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, (const uint8_t *)addr, size);
|
2019-06-17 09:18:55 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
// Validate whether the specified offset and count would cause an overrun in the tune.
|
|
|
|
// Returns true if an overrun would occur.
|
2021-02-19 23:11:39 -08:00
|
|
|
static bool validateOffsetCount(size_t offset, size_t count, TsChannelBase* tsChannel) {
|
2020-01-07 04:56:45 -08:00
|
|
|
if (offset + count > getTunerStudioPageSize()) {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TS: Project mismatch? Too much configuration requested %d/%d", offset, count);
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "ERROR: out of range");
|
2020-08-21 05:24:31 -07:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_OUT_OF_RANGE);
|
2020-01-07 04:56:45 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-17 09:18:55 -07:00
|
|
|
|
2020-11-03 16:03:46 -08:00
|
|
|
// This is used to prevent TS from reading/writing when we have just applied a preset, to prevent TS getting confused.
|
|
|
|
// At the same time an ECU reboot is forced by triggering a fatal error, informing the user to please restart
|
|
|
|
// the ECU. Forcing a reboot will force TS to re-read the tune CRC,
|
|
|
|
bool rebootForPresetPending = false;
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This command is needed to make the whole transfer a bit faster
|
|
|
|
* @note See also handleWriteValueCommand
|
|
|
|
*/
|
2021-11-10 04:29:40 -08:00
|
|
|
void handleWriteChunkCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count,
|
2021-11-16 01:15:29 -08:00
|
|
|
void *content) {
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.writeChunkCommandCounter++;
|
|
|
|
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("WRITE CHUNK mode=%d o=%d s=%d", mode, offset, count);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
if (validateOffsetCount(offset, count, tsChannel)) {
|
|
|
|
return;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 16:03:46 -08:00
|
|
|
// Skip the write if a preset was just loaded - we don't want to overwrite it
|
|
|
|
if (!rebootForPresetPending) {
|
|
|
|
uint8_t * addr = (uint8_t *) (getWorkingPageAddr() + offset);
|
|
|
|
memcpy(addr, content, count);
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-07-24 19:26:41 -07:00
|
|
|
sendOkResponse(tsChannel, mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleCrc32Check(TsChannelBase *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) {
|
2020-01-07 04:56:45 -08:00
|
|
|
tsState.crc32CheckCommandCounter++;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-08-18 19:37:08 -07:00
|
|
|
// Ensure we are reading from in bounds
|
|
|
|
if (validateOffsetCount(offset, count, tsChannel)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-29 13:44:45 -08:00
|
|
|
const uint8_t* start = getWorkingPageAddr() + offset;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-07-04 15:03:17 -07:00
|
|
|
uint32_t crc = SWAP_UINT32(crc32(start, count));
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(mode, (const uint8_t *) &crc, 4);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Write' command receives a single value at a given offset
|
|
|
|
* @note Writing values one by one is pretty slow
|
|
|
|
*/
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleWriteValueCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint8_t value) {
|
2017-05-23 10:10:43 -07:00
|
|
|
UNUSED(tsChannel);
|
|
|
|
UNUSED(mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
tsState.writeValueCommandCounter++;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioDebug(tsChannel, "got W (Write)"); // we can get a lot of these
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
if (validateOffsetCount(offset, 1, tsChannel)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
efitimems_t nowMs = currentTimeMillis();
|
|
|
|
if (nowMs - previousWriteReportMs > 5) {
|
|
|
|
previousWriteReportMs = nowMs;
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("offset %d: value=%d", offset, value);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-11-03 16:03:46 -08:00
|
|
|
// Skip the write if a preset was just loaded - we don't want to overwrite it
|
|
|
|
if (!rebootForPresetPending) {
|
|
|
|
getWorkingPageAddr()[offset] = value;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handlePageReadCommand(TsChannelBase* tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) {
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.readPageCommandsCounter++;
|
|
|
|
|
2020-11-03 16:03:46 -08:00
|
|
|
if (rebootForPresetPending) {
|
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("READ mode=%d offset=%d size=%d", mode, offset, count);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2020-01-07 04:56:45 -08:00
|
|
|
if (validateOffsetCount(offset, count, tsChannel)) {
|
2015-07-10 06:01:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:44:45 -08:00
|
|
|
const uint8_t* addr = getWorkingPageAddr() + offset;
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(mode, addr, count);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2021-04-18 16:13:48 -07:00
|
|
|
// efiPrintf("Sending %d done", count);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void requestBurn(void) {
|
2021-11-10 04:29:40 -08:00
|
|
|
#if !EFI_UNIT_TEST
|
2021-11-16 01:15:29 -08:00
|
|
|
onBurnRequest();
|
2020-07-05 08:16:07 -07:00
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_INTERNAL_FLASH
|
2015-07-10 06:01:56 -07:00
|
|
|
setNeedToWriteConfiguration();
|
|
|
|
#endif
|
2021-11-10 04:29:40 -08:00
|
|
|
#endif // !EFI_UNIT_TEST
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void sendResponseCode(ts_response_format_e mode, TsChannelBase *tsChannel, const uint8_t responseCode) {
|
2015-07-10 06:01:56 -07:00
|
|
|
if (mode == TS_CRC) {
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->writeCrcPacket(responseCode, nullptr, 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Burn' command is a command to commit the changes
|
|
|
|
*/
|
2021-11-16 01:15:29 -08:00
|
|
|
void handleBurnCommand(TsChannelBase* tsChannel, ts_response_format_e mode) {
|
2015-07-10 06:01:56 -07:00
|
|
|
efitimems_t nowMs = currentTimeMillis();
|
|
|
|
tsState.burnCommandCounter++;
|
|
|
|
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("got B (Burn) %s", mode == TS_PLAIN ? "plain" : "CRC");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-11-03 16:03:46 -08:00
|
|
|
// Skip the burn if a preset was just loaded - we don't want to overwrite it
|
|
|
|
if (!rebootForPresetPending) {
|
|
|
|
requestBurn();
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK);
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("BURN in %dms", currentTimeMillis() - nowMs);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static bool isKnownCommand(char command) {
|
|
|
|
return command == TS_HELLO_COMMAND || command == TS_READ_COMMAND || command == TS_OUTPUT_COMMAND
|
|
|
|
|| command == TS_PAGE_COMMAND || command == TS_BURN_COMMAND || command == TS_SINGLE_WRITE_COMMAND
|
2019-01-05 04:55:21 -08:00
|
|
|
|| command == TS_CHUNK_WRITE_COMMAND || command == TS_EXECUTE
|
2016-03-04 20:01:42 -08:00
|
|
|
|| command == TS_IO_TEST_COMMAND
|
2019-06-17 09:18:55 -07:00
|
|
|
|| command == TS_GET_STRUCT
|
2020-05-25 10:12:15 -07:00
|
|
|
|| command == TS_SET_LOGGER_SWITCH
|
|
|
|
|| command == TS_GET_LOGGER_GET_BUFFER
|
2020-05-30 08:31:18 -07:00
|
|
|
|| command == TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY
|
2019-07-06 17:48:58 -07:00
|
|
|
|| command == TS_GET_TEXT
|
|
|
|
|| command == TS_CRC_CHECK_COMMAND
|
2019-11-21 20:45:16 -08:00
|
|
|
|| command == TS_GET_FIRMWARE_VERSION
|
|
|
|
|| command == TS_PERF_TRACE_BEGIN
|
2020-03-29 04:27:36 -07:00
|
|
|
|| command == TS_PERF_TRACE_GET_BUFFER
|
|
|
|
|| command == TS_GET_CONFIG_ERROR;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2021-04-18 16:13:48 -07:00
|
|
|
TunerStudio tsInstance;
|
2017-03-15 19:20:53 -07:00
|
|
|
|
2021-06-23 01:36:55 -07:00
|
|
|
static int tsProcessOne(TsChannelBase* tsChannel) {
|
2020-12-08 00:05:43 -08:00
|
|
|
validateStack("communication", STACK_USAGE_COMMUNICATION, 128);
|
2020-08-02 07:52:52 -07:00
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
if (!tsChannel->isReady()) {
|
2020-12-08 00:05:43 -08:00
|
|
|
chThdSleepMilliseconds(10);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
tsState.totalCounter++;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
uint8_t firstByte;
|
2021-06-23 01:36:55 -07:00
|
|
|
int received = tsChannel->readTimeout(&firstByte, 1, TS_COMMUNICATION_TIMEOUT);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2020-12-08 00:05:43 -08:00
|
|
|
logMsg("received %d\r\n", received);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
if (received != 1) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// tunerStudioError("ERROR: no command");
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_BLUETOOTH_SETUP
|
2021-12-08 08:52:42 -08:00
|
|
|
// no data in a whole second means time to disconnect BT
|
2020-12-08 00:05:43 -08:00
|
|
|
// assume there's connection loss and notify the bluetooth init code
|
|
|
|
bluetoothSoftwareDisconnectNotify();
|
2017-06-04 05:50:31 -07:00
|
|
|
#endif /* EFI_BLUETOOTH_SETUP */
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2017-03-15 19:20:53 -07:00
|
|
|
|
2021-06-23 01:26:32 -07:00
|
|
|
if (handlePlainCommand(tsChannel, firstByte)) {
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2021-06-23 01:26:32 -07:00
|
|
|
}
|
2020-12-08 00:05:43 -08:00
|
|
|
|
|
|
|
uint8_t secondByte;
|
2021-06-23 01:36:55 -07:00
|
|
|
received = tsChannel->readTimeout(&secondByte, 1, TS_COMMUNICATION_TIMEOUT);
|
2020-12-08 00:05:43 -08:00
|
|
|
if (received != 1) {
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "TS: ERROR: no second byte");
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
uint16_t incomingPacketSize = firstByte << 8 | secondByte;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->scratchBuffer) - CRC_WRAPPING_SIZE)) {
|
2021-12-07 12:15:06 -08:00
|
|
|
efiPrintf("process_ts: channel=%s invalid size: %d", tsChannel->name, incomingPacketSize);
|
|
|
|
tunerStudioError(tsChannel, "process_ts: ERROR: CRC header size");
|
2020-12-08 00:05:43 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-06-23 01:36:55 -07:00
|
|
|
received = tsChannel->readTimeout((uint8_t* )tsChannel->scratchBuffer, 1, TS_COMMUNICATION_TIMEOUT);
|
2020-12-08 00:05:43 -08:00
|
|
|
if (received != 1) {
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "ERROR: did not receive command");
|
2020-12-08 00:05:43 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
char command = tsChannel->scratchBuffer[0];
|
|
|
|
if (!isKnownCommand(command)) {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("unexpected command %x", command);
|
2020-12-08 00:05:43 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2020-12-08 00:05:43 -08:00
|
|
|
logMsg("command %c\r\n", command);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1;
|
2021-06-23 01:36:55 -07:00
|
|
|
received = tsChannel->readTimeout((uint8_t*)(tsChannel->scratchBuffer + 1), expectedSize, TS_COMMUNICATION_TIMEOUT);
|
2020-12-08 00:05:43 -08:00
|
|
|
if (received != expectedSize) {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("Got only %d bytes while expecting %d for command %c", received,
|
2020-12-08 00:05:43 -08:00
|
|
|
expectedSize, command);
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "ERROR: not enough bytes in stream");
|
2020-12-08 00:05:43 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNDERRUN);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
uint32_t expectedCrc = *(uint32_t*) (tsChannel->scratchBuffer + incomingPacketSize);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
expectedCrc = SWAP_UINT32(expectedCrc);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
uint32_t actualCrc = crc32(tsChannel->scratchBuffer, incomingPacketSize);
|
|
|
|
if (actualCrc != expectedCrc) {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TunerStudio: CRC %x %x %x %x", tsChannel->scratchBuffer[incomingPacketSize + 0],
|
2020-12-08 00:05:43 -08:00
|
|
|
tsChannel->scratchBuffer[incomingPacketSize + 1], tsChannel->scratchBuffer[incomingPacketSize + 2],
|
|
|
|
tsChannel->scratchBuffer[incomingPacketSize + 3]);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("TunerStudio: command %c actual CRC %x/expected %x", tsChannel->scratchBuffer[0],
|
2020-12-08 00:05:43 -08:00
|
|
|
actualCrc, expectedCrc);
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "ERROR: CRC issue");
|
2020-12-08 00:05:43 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_CRC_FAILURE);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
int success = tsInstance.handleCrcCommand(tsChannel, tsChannel->scratchBuffer, incomingPacketSize);
|
2021-03-26 14:23:26 -07:00
|
|
|
|
|
|
|
if (!success) {
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("got unexpected TunerStudio command %x:%c", command, command);
|
2021-06-23 01:36:55 -07:00
|
|
|
return -1;
|
2021-03-26 14:23:26 -07:00
|
|
|
}
|
2021-06-23 01:36:55 -07:00
|
|
|
|
|
|
|
return 0;
|
2020-12-08 00:05:43 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-03-28 06:06:36 -07:00
|
|
|
void TunerstudioThread::ThreadTask() {
|
|
|
|
auto channel = setupChannel();
|
|
|
|
|
2021-03-18 11:07:22 -07:00
|
|
|
// No channel configured for this thread, cancel.
|
2021-03-28 06:06:36 -07:00
|
|
|
if (!channel || !channel->isConfigured()) {
|
2021-03-18 11:07:22 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:05:43 -08:00
|
|
|
// Until the end of time, process incoming messages.
|
2021-12-08 08:52:42 -08:00
|
|
|
while (true) {
|
|
|
|
if (tsProcessOne(channel) == 0) {
|
2021-06-23 01:36:55 -07:00
|
|
|
onDataArrived(true);
|
2021-12-08 08:52:42 -08:00
|
|
|
} else {
|
2021-06-23 01:36:55 -07:00
|
|
|
onDataArrived(false);
|
2021-12-08 08:52:42 -08:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#endif // EFI_TUNER_STUDIO
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
tunerstudio_counters_s tsState;
|
|
|
|
|
2021-09-18 13:58:35 -07:00
|
|
|
void tunerStudioError(TsChannelBase* tsChannel, const char *msg) {
|
|
|
|
tunerStudioDebug(tsChannel, msg);
|
2015-07-10 06:01:56 -07:00
|
|
|
printErrorCounters();
|
|
|
|
tsState.errorCounter++;
|
|
|
|
}
|
|
|
|
|
2021-11-10 04:29:40 -08:00
|
|
|
#if EFI_TUNER_STUDIO
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* Query with CRC takes place while re-establishing connection
|
|
|
|
* Query without CRC takes place on TunerStudio startup
|
|
|
|
*/
|
2021-02-19 23:11:39 -08:00
|
|
|
void handleQueryCommand(TsChannelBase* tsChannel, ts_response_format_e mode) {
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.queryCommandCounter++;
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("got S/H (queryCommand) mode=%d", mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
printTsStats();
|
|
|
|
#endif
|
2020-07-02 09:33:31 -07:00
|
|
|
const char *signature = getTsSignature();
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(mode, (const uint8_t *)signature, strlen(signature) + 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2020-05-20 21:14:08 -07:00
|
|
|
/**
|
|
|
|
* rusEfi own test command
|
|
|
|
*/
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleTestCommand(TsChannelBase* tsChannel) {
|
2017-05-09 09:07:52 -07:00
|
|
|
tsState.testCommandCounter++;
|
2021-08-27 03:18:05 -07:00
|
|
|
char testOutputBuffer[64];
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* this is NOT a standard TunerStudio command, this is my own
|
|
|
|
* extension of the protocol to simplify troubleshooting
|
|
|
|
*/
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioDebug(tsChannel, "got T (Test)");
|
2021-02-19 04:40:59 -08:00
|
|
|
tsChannel->write((const uint8_t*)VCS_VERSION, sizeof(VCS_VERSION));
|
2019-08-18 09:30:59 -07:00
|
|
|
|
2019-01-11 06:58:48 -08:00
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " %d %d", engine->engineState.warnings.lastErrorCode, tsState.testCommandCounter);
|
2021-02-19 04:40:59 -08:00
|
|
|
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
|
2019-08-18 09:30:59 -07:00
|
|
|
|
2022-01-12 13:03:28 -08:00
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " uptime=%ds ", (int)getTimeNowSeconds());
|
2021-02-19 04:40:59 -08:00
|
|
|
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
|
2019-08-18 09:30:59 -07:00
|
|
|
|
2022-01-11 19:47:46 -08:00
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), __DATE__ " %s\r\n", PROTOCOL_TEST_RESPONSE_TAG);
|
2021-02-19 04:40:59 -08:00
|
|
|
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
|
2021-07-13 19:53:11 -07:00
|
|
|
|
|
|
|
if (hasFirmwareError()) {
|
|
|
|
const char* error = getFirmwareError();
|
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), "error=%s\r\n", error);
|
|
|
|
tsChannel->write((const uint8_t*)testOutputBuffer, strlen(testOutputBuffer));
|
|
|
|
}
|
2021-12-06 19:19:07 -08:00
|
|
|
tsChannel->flush();
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern CommandHandler console_line_callback;
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleGetVersion(TsChannelBase* tsChannel) {
|
2021-08-27 03:18:05 -07:00
|
|
|
char versionBuffer[32];
|
2017-05-07 05:20:23 -07:00
|
|
|
chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION);
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1);
|
2017-01-28 15:03:41 -08:00
|
|
|
}
|
|
|
|
|
2021-03-15 03:42:58 -07:00
|
|
|
#if EFI_TEXT_LOGGING
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleGetText(TsChannelBase* tsChannel) {
|
2017-03-26 14:19:08 -07:00
|
|
|
tsState.textCommandCounter++;
|
|
|
|
|
2018-04-01 20:49:57 -07:00
|
|
|
printOverallStatus(getTimeNowSeconds());
|
|
|
|
|
2021-03-15 03:42:58 -07:00
|
|
|
size_t outputSize;
|
2021-03-09 05:43:29 -08:00
|
|
|
const char* output = swapOutputBuffers(&outputSize);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
logMsg("get test sending [%d]\r\n", outputSize);
|
|
|
|
#endif
|
|
|
|
|
2021-03-09 05:43:29 -08:00
|
|
|
tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, reinterpret_cast<const uint8_t*>(output), outputSize);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
logMsg("sent [%d]\r\n", outputSize);
|
|
|
|
#endif
|
|
|
|
}
|
2021-03-15 03:42:58 -07:00
|
|
|
#endif // EFI_TEXT_LOGGING
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
static void handleExecuteCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) {
|
2015-07-10 06:01:56 -07:00
|
|
|
data[incomingPacketSize] = 0;
|
|
|
|
char *trimmed = efiTrim(data);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
logMsg("execute [%s]\r\n", trimmed);
|
|
|
|
#endif
|
|
|
|
(console_line_callback)(trimmed);
|
2020-11-17 12:24:00 -08:00
|
|
|
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->writeCrcPacket(TS_RESPONSE_COMMAND_OK, nullptr, 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if legacy command was processed, false otherwise
|
|
|
|
*/
|
2021-02-19 23:11:39 -08:00
|
|
|
bool handlePlainCommand(TsChannelBase* tsChannel, uint8_t command) {
|
2019-01-05 04:55:21 -08:00
|
|
|
// Bail fast if guaranteed not to be a plain command
|
2021-10-20 12:40:37 -07:00
|
|
|
if (command == 0) {
|
2019-01-05 04:55:21 -08:00
|
|
|
return false;
|
2021-10-20 17:50:50 -07:00
|
|
|
} else if (command == TS_HELLO_COMMAND || command == TS_QUERY_COMMAND) {
|
2021-10-20 12:40:37 -07:00
|
|
|
// We interpret 'Q' as TS_HELLO_COMMAND, since TS uses hardcoded 'Q' during ECU detection (scan all serial ports)
|
2021-04-18 16:13:48 -07:00
|
|
|
efiPrintf("Got naked Query command");
|
2015-07-10 06:01:56 -07:00
|
|
|
handleQueryCommand(tsChannel, TS_PLAIN);
|
|
|
|
return true;
|
2016-08-16 19:05:36 -07:00
|
|
|
} else if (command == TS_TEST_COMMAND || command == 'T') {
|
2015-07-10 06:01:56 -07:00
|
|
|
handleTestCommand(tsChannel);
|
|
|
|
return true;
|
|
|
|
} else if (command == TS_COMMAND_F) {
|
2019-01-05 04:55:21 -08:00
|
|
|
/**
|
|
|
|
* http://www.msextra.com/forums/viewtopic.php?f=122&t=48327
|
|
|
|
* Response from TS support: This is an optional command *
|
|
|
|
* "The F command is used to find what ini. file needs to be loaded in TunerStudio to match the controller.
|
|
|
|
* If you are able to just make your firmware ignore the command that would work.
|
|
|
|
* Currently on some firmware versions the F command is not used and is just ignored by the firmware as a unknown command."
|
|
|
|
*/
|
|
|
|
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioDebug(tsChannel, "not ignoring F");
|
2021-02-19 04:40:59 -08:00
|
|
|
tsChannel->write((const uint8_t *)TS_PROTOCOL, strlen(TS_PROTOCOL));
|
2021-12-06 19:19:07 -08:00
|
|
|
tsChannel->flush();
|
2015-07-10 06:01:56 -07:00
|
|
|
return true;
|
|
|
|
} else {
|
2019-01-05 04:55:21 -08:00
|
|
|
// This wasn't a valid command
|
2015-07-10 06:01:56 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:30:53 -07:00
|
|
|
static int transmitted = 0;
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
int TunerStudioBase::handleCrcCommand(TsChannelBase* tsChannel, char *data, int incomingPacketSize) {
|
2019-10-13 13:14:08 -07:00
|
|
|
ScopePerf perf(PE::TunerStudioHandleCrcCommand);
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
char command = data[0];
|
|
|
|
data++;
|
2019-01-05 04:55:21 -08:00
|
|
|
|
2019-07-05 15:16:32 -07:00
|
|
|
const uint16_t* data16 = reinterpret_cast<uint16_t*>(data);
|
|
|
|
|
2020-08-18 19:37:08 -07:00
|
|
|
uint16_t offset = data16[0];
|
|
|
|
uint16_t count = data16[1];
|
|
|
|
|
2019-07-05 15:16:32 -07:00
|
|
|
switch(command)
|
|
|
|
{
|
|
|
|
case TS_OUTPUT_COMMAND:
|
2020-12-08 23:23:02 -08:00
|
|
|
cmdOutputChannels(tsChannel, offset, count);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_HELLO_COMMAND:
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioDebug(tsChannel, "got Query command");
|
2015-07-10 06:01:56 -07:00
|
|
|
handleQueryCommand(tsChannel, TS_CRC);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_GET_FIRMWARE_VERSION:
|
2020-12-08 23:23:02 -08:00
|
|
|
handleGetVersion(tsChannel);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
2021-03-15 03:42:58 -07:00
|
|
|
#if EFI_TEXT_LOGGING
|
2019-07-05 15:16:32 -07:00
|
|
|
case TS_GET_TEXT:
|
2015-07-10 06:01:56 -07:00
|
|
|
handleGetText(tsChannel);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
2021-03-15 03:42:58 -07:00
|
|
|
#endif // EFI_TEXT_LOGGING
|
2019-07-05 15:16:32 -07:00
|
|
|
case TS_EXECUTE:
|
2015-07-10 06:01:56 -07:00
|
|
|
handleExecuteCommand(tsChannel, data, incomingPacketSize - 1);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_PAGE_COMMAND:
|
2020-08-18 19:37:08 -07:00
|
|
|
handlePageSelectCommand(tsChannel, TS_CRC);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_GET_STRUCT:
|
2020-08-18 19:37:08 -07:00
|
|
|
handleGetStructContent(tsChannel, offset, count);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_CHUNK_WRITE_COMMAND:
|
2020-08-18 19:37:08 -07:00
|
|
|
handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest));
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_SINGLE_WRITE_COMMAND:
|
|
|
|
{
|
|
|
|
uint8_t value = data[4];
|
2020-08-18 19:37:08 -07:00
|
|
|
handleWriteValueCommand(tsChannel, TS_CRC, offset, value);
|
2017-07-24 19:26:41 -07:00
|
|
|
}
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_CRC_CHECK_COMMAND:
|
2020-08-18 19:37:08 -07:00
|
|
|
handleCrc32Check(tsChannel, TS_CRC, offset, count);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_BURN_COMMAND:
|
2020-08-18 19:37:08 -07:00
|
|
|
handleBurnCommand(tsChannel, TS_CRC);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_READ_COMMAND:
|
2020-08-18 19:37:08 -07:00
|
|
|
handlePageReadCommand(tsChannel, TS_CRC, offset, count);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_TEST_COMMAND:
|
|
|
|
[[fallthrough]];
|
|
|
|
case 'T':
|
|
|
|
handleTestCommand(tsChannel);
|
|
|
|
break;
|
|
|
|
case TS_IO_TEST_COMMAND:
|
|
|
|
{
|
|
|
|
uint16_t subsystem = SWAP_UINT16(data16[0]);
|
|
|
|
uint16_t index = SWAP_UINT16(data16[1]);
|
|
|
|
|
|
|
|
if (engineConfiguration->debugMode == DBG_BENCH_TEST) {
|
2021-12-07 17:18:47 -08:00
|
|
|
engine->outputChannels.debugIntField1++;
|
|
|
|
engine->outputChannels.debugIntField2 = subsystem;
|
|
|
|
engine->outputChannels.debugIntField3 = index;
|
2019-07-05 15:16:32 -07:00
|
|
|
}
|
2017-07-24 19:26:41 -07:00
|
|
|
|
2020-02-26 15:16:35 -08:00
|
|
|
#if EFI_PROD_CODE && EFI_ENGINE_CONTROL
|
|
|
|
executeTSCommand(subsystem, index);
|
2017-07-24 19:26:41 -07:00
|
|
|
#endif /* EFI_PROD_CODE */
|
2019-07-05 15:16:32 -07:00
|
|
|
sendOkResponse(tsChannel, TS_CRC);
|
|
|
|
}
|
2019-07-06 17:48:58 -07:00
|
|
|
break;
|
2019-07-07 12:22:46 -07:00
|
|
|
#if EFI_TOOTH_LOGGER
|
2020-05-25 10:12:15 -07:00
|
|
|
case TS_SET_LOGGER_SWITCH:
|
2019-07-06 17:48:58 -07:00
|
|
|
switch(data[0]) {
|
2020-05-30 09:23:27 -07:00
|
|
|
case TS_COMPOSITE_ENABLE:
|
2019-07-06 17:48:58 -07:00
|
|
|
EnableToothLogger();
|
|
|
|
break;
|
2020-05-30 09:23:27 -07:00
|
|
|
case TS_COMPOSITE_DISABLE:
|
2019-07-06 17:48:58 -07:00
|
|
|
DisableToothLogger();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// dunno what that was, send NAK
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendOkResponse(tsChannel, TS_CRC);
|
|
|
|
|
|
|
|
break;
|
2020-05-26 19:30:53 -07:00
|
|
|
case TS_GET_COMPOSITE_BUFFER_DONE_DIFFERENTLY:
|
2020-05-30 08:31:18 -07:00
|
|
|
|
2020-05-26 19:30:53 -07:00
|
|
|
{
|
2020-05-30 08:31:18 -07:00
|
|
|
EnableToothLoggerIfNotEnabled();
|
2020-05-26 19:30:53 -07:00
|
|
|
const uint8_t* const buffer = GetToothLoggerBuffer().Buffer;
|
|
|
|
|
|
|
|
const uint8_t* const start = buffer + COMPOSITE_PACKET_SIZE * transmitted;
|
|
|
|
|
|
|
|
int currentEnd = getCompositeRecordCount();
|
|
|
|
|
2020-05-30 08:31:18 -07:00
|
|
|
// set debug_mode 40
|
2020-07-04 18:32:12 -07:00
|
|
|
if (engineConfiguration->debugMode == DBG_COMPOSITE_LOG) {
|
2021-12-07 17:18:47 -08:00
|
|
|
engine->outputChannels.debugIntField1 = currentEnd;
|
|
|
|
engine->outputChannels.debugIntField2 = transmitted;
|
2020-05-30 08:31:18 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:30:53 -07:00
|
|
|
if (currentEnd > transmitted) {
|
|
|
|
// more normal case - tail after head
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (currentEnd - transmitted));
|
2020-05-26 19:30:53 -07:00
|
|
|
transmitted = currentEnd;
|
2020-05-30 08:31:18 -07:00
|
|
|
} else if (currentEnd == transmitted) {
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, start, 0);
|
2020-05-26 19:30:53 -07:00
|
|
|
} else {
|
|
|
|
// we are here if tail of buffer has reached the end of buffer and re-started from the start of buffer
|
|
|
|
// sending end of the buffer, next transmission would take care of the rest
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, start, COMPOSITE_PACKET_SIZE * (COMPOSITE_PACKET_COUNT - transmitted));
|
2020-05-26 19:30:53 -07:00
|
|
|
transmitted = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-05-25 10:12:15 -07:00
|
|
|
case TS_GET_LOGGER_GET_BUFFER:
|
2019-07-06 17:48:58 -07:00
|
|
|
{
|
|
|
|
auto toothBuffer = GetToothLoggerBuffer();
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, toothBuffer.Buffer, toothBuffer.Length);
|
2019-07-06 17:48:58 -07:00
|
|
|
}
|
|
|
|
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
2019-12-02 16:18:00 -08:00
|
|
|
#endif /* EFI_TOOTH_LOGGER */
|
2019-11-19 17:36:47 -08:00
|
|
|
#if ENABLE_PERF_TRACE
|
2019-12-02 16:18:00 -08:00
|
|
|
case TS_PERF_TRACE_BEGIN:
|
2019-10-14 23:40:51 -07:00
|
|
|
perfTraceEnable();
|
2019-11-21 20:45:16 -08:00
|
|
|
sendOkResponse(tsChannel, TS_CRC);
|
2019-10-14 23:40:51 -07:00
|
|
|
break;
|
|
|
|
case TS_PERF_TRACE_GET_BUFFER:
|
|
|
|
{
|
|
|
|
auto trace = perfTraceGetBuffer();
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, trace.Buffer, trace.Size);
|
2019-10-14 23:40:51 -07:00
|
|
|
}
|
2019-12-02 16:18:00 -08:00
|
|
|
|
2019-11-19 17:36:47 -08:00
|
|
|
break;
|
2019-12-02 16:18:00 -08:00
|
|
|
#endif /* ENABLE_PERF_TRACE */
|
2020-05-20 19:15:47 -07:00
|
|
|
case TS_GET_CONFIG_ERROR: {
|
2021-03-14 06:31:11 -07:00
|
|
|
const char* configError = getFirmwareError();
|
2020-07-27 19:49:59 -07:00
|
|
|
#if HW_CHECK_MODE
|
|
|
|
// analog input errors are returned as firmware error in QC mode
|
|
|
|
if (!hasFirmwareError()) {
|
2021-03-14 06:52:27 -07:00
|
|
|
strcpy((char*)configError, "FACTORY_MODE_PLEASE_CONTACT_SUPPORT");
|
2020-07-27 19:49:59 -07:00
|
|
|
}
|
2020-05-20 19:15:47 -07:00
|
|
|
#endif // HW_CHECK_MODE
|
2021-02-19 17:48:21 -08:00
|
|
|
tsChannel->sendResponse(TS_CRC, reinterpret_cast<const uint8_t*>(configError), strlen(configError));
|
2020-03-29 04:27:36 -07:00
|
|
|
break;
|
2020-05-20 19:15:47 -07:00
|
|
|
}
|
2019-07-05 15:16:32 -07:00
|
|
|
default:
|
2021-02-14 12:03:07 -08:00
|
|
|
sendErrorCode(tsChannel, TS_RESPONSE_UNRECOGNIZED_COMMAND);
|
2021-09-18 13:58:35 -07:00
|
|
|
tunerStudioError(tsChannel, "ERROR: ignoring unexpected command");
|
2015-07-10 06:01:56 -07:00
|
|
|
return false;
|
|
|
|
}
|
2019-07-05 15:16:32 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void startTunerStudioConnectivity(void) {
|
2019-07-05 15:16:32 -07:00
|
|
|
// Assert tune & output channel struct sizes
|
|
|
|
static_assert(sizeof(persistent_config_s) == TOTAL_CONFIG_SIZE, "TS datapage size mismatch");
|
|
|
|
static_assert(sizeof(TunerStudioOutputChannels) == TS_OUTPUT_SIZE, "TS output channels size mismatch");
|
2021-06-12 10:48:37 -07:00
|
|
|
// useful trick if you need to know how far off is the static_assert
|
|
|
|
// char (*__kaboom)[sizeof(persistent_config_s)] = 1;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
memset(&tsState, 0, sizeof(tsState));
|
|
|
|
|
|
|
|
addConsoleAction("tsinfo", printTsStats);
|
|
|
|
addConsoleAction("reset_ts", resetTs);
|
|
|
|
addConsoleActionI("set_ts_speed", setTsSpeed);
|
2017-06-04 05:50:31 -07:00
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_BLUETOOTH_SETUP
|
2017-06-04 05:50:31 -07:00
|
|
|
// Usage: "bluetooth_hc06 <baud> <name> <pincode>"
|
|
|
|
// Example: "bluetooth_hc06 38400 rusefi 1234"
|
2017-06-04 15:07:02 -07:00
|
|
|
addConsoleActionSSS("bluetooth_hc05", bluetoothHC05);
|
2017-06-04 05:50:31 -07:00
|
|
|
addConsoleActionSSS("bluetooth_hc06", bluetoothHC06);
|
2017-11-02 15:10:00 -07:00
|
|
|
addConsoleActionSSS("bluetooth_spp", bluetoothSPP);
|
2017-06-04 05:50:31 -07:00
|
|
|
addConsoleAction("bluetooth_cancel", bluetoothCancel);
|
|
|
|
#endif /* EFI_BLUETOOTH_SETUP */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|