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
|
2018-01-20 17:55:31 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2018
|
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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-09-16 19:25:17 -07:00
|
|
|
#include "global.h"
|
2019-07-04 00:57:21 -07:00
|
|
|
#include "os_access.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-05-27 15:58:43 -07:00
|
|
|
#include "allsensors.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "tunerstudio.h"
|
|
|
|
|
|
|
|
#include "main_trigger_callback.h"
|
|
|
|
#include "flash_main.h"
|
|
|
|
|
|
|
|
#include "tunerstudio_io.h"
|
|
|
|
#include "tunerstudio_configuration.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"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include "engine_configuration.h"
|
2016-03-14 20:01:43 -07:00
|
|
|
#include "injector_central.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "svnversion.h"
|
|
|
|
#include "loggingcentral.h"
|
|
|
|
#include "status_loop.h"
|
2016-05-06 21:01:23 -07:00
|
|
|
#include "mmc_card.h"
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "rusEfiFunctionalTest.h"
|
|
|
|
#endif
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
EXTERN_ENGINE
|
|
|
|
;
|
|
|
|
|
2016-08-30 19:02:21 -07:00
|
|
|
extern persistent_config_container_s persistentState;
|
2016-06-01 16:02:00 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
extern short currentPageId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* note the use-case where text console port is switched into
|
|
|
|
* binary port
|
|
|
|
*/
|
|
|
|
LoggingWithStorage tsLogger("binary");
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if !defined(EFI_NO_CONFIG_WORKING_COPY)
|
2016-07-05 17:02:56 -07:00
|
|
|
/**
|
|
|
|
* this is a local copy of the configuration. Any changes to this copy
|
|
|
|
* have no effect until this copy is explicitly propagated to the main working copy
|
|
|
|
*/
|
|
|
|
persistent_config_s configWorkingCopy;
|
|
|
|
|
2019-03-30 14:41:46 -07:00
|
|
|
#endif /* EFI_NO_CONFIG_WORKING_COPY */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static efitimems_t previousWriteReportMs = 0;
|
|
|
|
|
2017-05-23 10:10:43 -07:00
|
|
|
static ts_channel_s tsChannel;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
// this thread wants a bit extra stack
|
|
|
|
static THD_WORKING_AREA(tsThreadStack, 3 * UTILITY_THREAD_STACK_SIZE);
|
|
|
|
|
|
|
|
extern TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
|
|
|
|
extern tunerstudio_counters_s tsState;
|
|
|
|
|
|
|
|
static void resetTs(void) {
|
|
|
|
memset(&tsState, 0, sizeof(tsState));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printErrorCounters(void) {
|
|
|
|
scheduleMsg(&tsLogger, "TunerStudio size=%d / total=%d / errors=%d / H=%d / O=%d / P=%d / B=%d",
|
2017-03-26 14:19:08 -07:00
|
|
|
sizeof(tsOutputChannels), tsState.totalCounter, tsState.errorCounter, tsState.queryCommandCounter,
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.outputChannelsCommandCounter, tsState.readPageCommandsCounter, tsState.burnCommandCounter);
|
|
|
|
scheduleMsg(&tsLogger, "TunerStudio W=%d / C=%d / P=%d / page=%d", tsState.writeValueCommandCounter,
|
|
|
|
tsState.writeChunkCommandCounter, tsState.pageCommandCounter, currentPageId);
|
|
|
|
// scheduleMsg(&tsLogger, "page size=%d", getTunerStudioPageSize(currentPageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
void printTsStats(void) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_PROD_CODE
|
2019-04-01 08:39:25 -07:00
|
|
|
if (false) {
|
|
|
|
// todo: is this code needed somewhere else?
|
2015-11-09 16:03:32 -08:00
|
|
|
scheduleMsg(&tsLogger, "TS RX on %s", hwPortname(engineConfiguration->binarySerialRxPin));
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "TS TX on %s @%d", hwPortname(engineConfiguration->binarySerialTxPin),
|
2019-01-09 19:16:30 -08:00
|
|
|
CONFIGB(tunerStudioSerialSpeed));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
|
|
|
|
printErrorCounters();
|
|
|
|
|
|
|
|
// scheduleMsg(logger, "analogChartFrequency %d",
|
|
|
|
// (int) (&engineConfiguration->analogChartFrequency) - (int) engineConfiguration);
|
|
|
|
//
|
|
|
|
// int fuelMapOffset = (int) (&engineConfiguration->fuelTable) - (int) engineConfiguration;
|
|
|
|
// scheduleMsg(logger, "fuelTable %d", fuelMapOffset);
|
|
|
|
//
|
2019-01-09 19:16:30 -08:00
|
|
|
// int offset = (int) (&CONFIGB(hip9011Gain)) - (int) engineConfiguration;
|
2015-07-10 06:01:56 -07:00
|
|
|
// scheduleMsg(&tsLogger, "hip9011Gain %d", offset);
|
|
|
|
//
|
|
|
|
// offset = (int) (&engineConfiguration->crankingCycleBins) - (int) engineConfiguration;
|
|
|
|
// scheduleMsg(&tsLogger, "crankingCycleBins %d", offset);
|
|
|
|
//
|
|
|
|
// offset = (int) (&engineConfiguration->engineCycle) - (int) engineConfiguration;
|
|
|
|
// scheduleMsg(&tsLogger, "engineCycle %d", offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setTsSpeed(int value) {
|
2019-01-09 19:16:30 -08:00
|
|
|
CONFIGB(tunerStudioSerialSpeed) = value;
|
2015-07-10 06:01:56 -07:00
|
|
|
printTsStats();
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_BLUETOOTH_SETUP
|
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) {
|
|
|
|
bluetoothStart(&tsChannel, BLUETOOTH_HC_05, baudRate, name, pinCode);
|
|
|
|
}
|
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) {
|
|
|
|
bluetoothStart(&tsChannel, BLUETOOTH_HC_06, baudRate, name, pinCode);
|
|
|
|
}
|
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) {
|
|
|
|
bluetoothStart(&tsChannel, BLUETOOTH_SPP, baudRate, name, pinCode);
|
|
|
|
}
|
2017-06-04 05:50:31 -07:00
|
|
|
#endif /* EFI_BLUETOOTH_SETUP */
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void tunerStudioDebug(const char *msg) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&tsLogger, "%s", msg);
|
2019-06-17 09:18:55 -07:00
|
|
|
#endif /* EFI_TUNER_STUDIO_VERBOSE */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
char *getWorkingPageAddr(int pageIndex) {
|
|
|
|
switch (pageIndex) {
|
|
|
|
case 0:
|
2019-07-05 15:16:32 -07:00
|
|
|
#ifndef EFI_NO_CONFIG_WORKING_COPY
|
2015-07-10 06:01:56 -07:00
|
|
|
return (char*) &configWorkingCopy.engineConfiguration;
|
2019-03-30 14:41:46 -07:00
|
|
|
#else
|
2019-05-05 08:06:27 -07:00
|
|
|
return (char*) engineConfiguration;
|
2019-03-30 14:41:46 -07:00
|
|
|
#endif /* EFI_NO_CONFIG_WORKING_COPY */
|
2019-07-05 15:16:32 -07:00
|
|
|
default:
|
|
|
|
return nullptr;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int getTunerStudioPageSize(int pageIndex) {
|
2019-07-05 15:16:32 -07:00
|
|
|
return pageIndex ? 0 : TOTAL_CONFIG_SIZE;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-07-24 19:26:41 -07:00
|
|
|
static void sendOkResponse(ts_channel_s *tsChannel, ts_response_format_e mode) {
|
|
|
|
sr5SendResponse(tsChannel, mode, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void handlePageSelectCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId) {
|
|
|
|
tsState.pageCommandCounter++;
|
|
|
|
|
|
|
|
currentPageId = pageId;
|
|
|
|
scheduleMsg(&tsLogger, "PAGE %d", currentPageId);
|
2017-07-24 19:26:41 -07:00
|
|
|
sendOkResponse(tsChannel, mode);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-11-19 13:56:54 -08:00
|
|
|
/**
|
2019-04-08 08:57:16 -07:00
|
|
|
* Copy specified amount of bytes from specified offset from communication layer working copy into real configuration
|
|
|
|
*
|
2017-11-19 13:56:54 -08:00
|
|
|
* Some changes like changing VE table or timing table are applied right away, meaning
|
|
|
|
* that the values are copied from communication copy into actual engine control copy right away.
|
|
|
|
* We call these parameters 'soft parameters'
|
|
|
|
*
|
|
|
|
* This is needed to support TS online auto-tune.
|
|
|
|
*
|
|
|
|
* On the contrary, 'hard parameters' are waiting for the Burn button to be clicked and configuration version
|
|
|
|
* would be increased and much more complicated logic would be executed.
|
|
|
|
*/
|
2019-04-08 08:57:16 -07:00
|
|
|
static void onlineApplyWorkingCopyBytes(int currentPageId, uint32_t offset, int count) {
|
2017-05-23 10:10:43 -07:00
|
|
|
UNUSED(currentPageId);
|
2019-07-05 15:16:32 -07:00
|
|
|
if (offset >= sizeof(engine_configuration_s)) {
|
2016-10-03 19:03:22 -07:00
|
|
|
int maxSize = sizeof(persistent_config_s) - offset;
|
2016-08-30 19:02:21 -07:00
|
|
|
if (count > maxSize) {
|
2016-10-03 19:03:22 -07:00
|
|
|
warning(CUSTOM_TS_OVERFLOW, "TS overflow %d %d", offset, count);
|
2016-08-30 19:02:21 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&tsLogger, "applying soft change from %d length %d", offset, count);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if !defined(EFI_NO_CONFIG_WORKING_COPY)
|
2016-08-30 19:02:21 -07:00
|
|
|
memcpy(((char*) &persistentState.persistentConfiguration) + offset, ((char*) &configWorkingCopy) + offset,
|
2015-07-10 06:01:56 -07:00
|
|
|
count);
|
2019-03-30 14:41:46 -07:00
|
|
|
#endif /* EFI_NO_CONFIG_WORKING_COPY */
|
2015-07-10 06:01:56 -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
|
|
|
|
*/
|
|
|
|
static void handleGetStructContent(ts_channel_s *tsChannel, int structId, int size) {
|
|
|
|
tsState.readPageCommandsCounter++;
|
|
|
|
|
|
|
|
const void *addr = NULL;
|
|
|
|
if (structId == LDS_CLT_INDEX) {
|
|
|
|
addr = static_cast<thermistor_state_s*>(&engine->engineState.cltCurve);
|
|
|
|
} else if (structId == LDS_IAT_INDEX) {
|
|
|
|
addr = static_cast<thermistor_state_s*>(&engine->engineState.iatCurve);
|
2019-06-17 18:37:11 -07:00
|
|
|
} else if (structId == LDS_ENGINE_STATE_INDEX) {
|
|
|
|
addr = static_cast<engine_state2_s*>(&engine->engineState);
|
2019-06-17 09:18:55 -07:00
|
|
|
}
|
|
|
|
if (addr == NULL) {
|
|
|
|
// todo: add warning code - unexpected structId
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sr5SendResponse(tsChannel, TS_CRC, (const uint8_t *)addr, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-06 20:02:23 -07:00
|
|
|
/**
|
|
|
|
* read log file content for rusEfi console
|
|
|
|
*/
|
|
|
|
static void handleReadFileContent(ts_channel_s *tsChannel, short fileId, short offset, short length) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_FILE_LOGGING
|
2016-05-06 21:01:23 -07:00
|
|
|
readLogFileContent(tsChannel->crcReadBuffer, fileId, offset, length);
|
2018-01-23 18:17:30 -08:00
|
|
|
#endif /* EFI_FILE_LOGGING */
|
2016-05-06 20:02:23 -07:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* This command is needed to make the whole transfer a bit faster
|
|
|
|
* @note See also handleWriteValueCommand
|
|
|
|
*/
|
|
|
|
void handleWriteChunkCommand(ts_channel_s *tsChannel, ts_response_format_e mode, short offset, short count,
|
|
|
|
void *content) {
|
|
|
|
tsState.writeChunkCommandCounter++;
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "WRITE CHUNK mode=%d p=%d o=%d s=%d", mode, currentPageId, offset, count);
|
|
|
|
|
|
|
|
if (offset > getTunerStudioPageSize(currentPageId)) {
|
|
|
|
scheduleMsg(&tsLogger, "ERROR invalid offset %d", offset);
|
|
|
|
tunerStudioError("ERROR: out of range");
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > getTunerStudioPageSize(currentPageId)) {
|
|
|
|
tunerStudioError("ERROR: unexpected count");
|
|
|
|
scheduleMsg(&tsLogger, "ERROR unexpected count %d", count);
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t * addr = (uint8_t *) (getWorkingPageAddr(currentPageId) + offset);
|
|
|
|
memcpy(addr, content, count);
|
2019-04-08 08:57:16 -07:00
|
|
|
onlineApplyWorkingCopyBytes(currentPageId, offset, 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
|
|
|
}
|
|
|
|
|
|
|
|
void handleCrc32Check(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId, uint16_t offset,
|
|
|
|
uint16_t count) {
|
|
|
|
tsState.crc32CheckCommandCounter++;
|
|
|
|
|
|
|
|
count = SWAP_UINT16(count);
|
|
|
|
|
|
|
|
count = getTunerStudioPageSize(pageId);
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "CRC32 request: pageId %d offset %d size %d", pageId, offset, count);
|
|
|
|
|
|
|
|
uint32_t crc = SWAP_UINT32(crc32((void * ) getWorkingPageAddr(0), count));
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "CRC32 response: %x", crc);
|
|
|
|
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5SendResponse(tsChannel, 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
|
|
|
|
*/
|
|
|
|
void handleWriteValueCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t page, 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
|
|
|
tsState.writeValueCommandCounter++;
|
|
|
|
|
|
|
|
currentPageId = page;
|
|
|
|
|
|
|
|
tunerStudioDebug("got W (Write)"); // we can get a lot of these
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
// scheduleMsg(logger, "Page number %d\r\n", pageId); // we can get a lot of these
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// int size = sizeof(TunerStudioWriteValueRequest);
|
|
|
|
// scheduleMsg(logger, "Reading %d\r\n", size);
|
|
|
|
|
|
|
|
if (offset > getTunerStudioPageSize(currentPageId)) {
|
|
|
|
tunerStudioError("ERROR: out of range2");
|
|
|
|
scheduleMsg(&tsLogger, "ERROR offset %d", offset);
|
|
|
|
offset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
efitimems_t nowMs = currentTimeMillis();
|
|
|
|
if (nowMs - previousWriteReportMs > 5) {
|
|
|
|
previousWriteReportMs = nowMs;
|
|
|
|
scheduleMsg(&tsLogger, "page %d offset %d: value=%d", currentPageId, offset, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
getWorkingPageAddr(currentPageId)[offset] = value;
|
|
|
|
|
2019-04-08 08:57:16 -07:00
|
|
|
onlineApplyWorkingCopyBytes(currentPageId, offset, 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
// scheduleMsg(logger, "va=%d", configWorkingCopy.boardConfiguration.idleValvePin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sendErrorCode(ts_channel_s *tsChannel) {
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteCrcPacket(tsChannel, TS_RESPONSE_CRC_FAILURE, NULL, 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void handlePageReadCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t pageId, uint16_t offset,
|
|
|
|
uint16_t count) {
|
|
|
|
tsState.readPageCommandsCounter++;
|
|
|
|
currentPageId = pageId;
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&tsLogger, "READ mode=%d page=%d offset=%d size=%d", mode, (int) currentPageId, offset, count);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (currentPageId >= PAGE_COUNT) {
|
|
|
|
// something is not right here
|
|
|
|
currentPageId = 0;
|
|
|
|
tunerStudioError("ERROR: invalid page number");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int size = getTunerStudioPageSize(currentPageId);
|
|
|
|
|
|
|
|
if (size < offset + count) {
|
|
|
|
scheduleMsg(&tsLogger, "invalid offset/count %d/%d", offset, count);
|
|
|
|
sendErrorCode(tsChannel);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *addr = (const uint8_t *) (getWorkingPageAddr(currentPageId) + offset);
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5SendResponse(tsChannel, mode, addr, count);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
// scheduleMsg(&tsLogger, "Sending %d done", count);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void requestBurn(void) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_INTERNAL_FLASH
|
2015-07-10 06:01:56 -07:00
|
|
|
setNeedToWriteConfiguration();
|
|
|
|
#endif
|
2017-05-15 20:33:22 -07:00
|
|
|
incrementGlobalConfigurationVersion(PASS_ENGINE_PARAMETER_SIGNATURE);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void sendResponseCode(ts_response_format_e mode, ts_channel_s *tsChannel, const uint8_t responseCode) {
|
|
|
|
if (mode == TS_CRC) {
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteCrcPacket(tsChannel, responseCode, NULL, 0);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'Burn' command is a command to commit the changes
|
|
|
|
*/
|
|
|
|
void handleBurnCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t page) {
|
|
|
|
efitimems_t nowMs = currentTimeMillis();
|
|
|
|
tsState.burnCommandCounter++;
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "got B (Burn) %s", mode == TS_PLAIN ? "plain" : "CRC");
|
|
|
|
|
|
|
|
currentPageId = page;
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
// pointless since we only have one page now
|
|
|
|
// scheduleMsg(logger, "Page number %d", currentPageId);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// todo: how about some multi-threading?
|
2019-04-12 19:10:57 -07:00
|
|
|
#if !defined(EFI_NO_CONFIG_WORKING_COPY)
|
2015-07-10 06:01:56 -07:00
|
|
|
memcpy(&persistentState.persistentConfiguration, &configWorkingCopy, sizeof(persistent_config_s));
|
2019-03-30 14:41:46 -07:00
|
|
|
#endif /* EFI_NO_CONFIG_WORKING_COPY */
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
requestBurn();
|
|
|
|
sendResponseCode(mode, tsChannel, TS_RESPONSE_BURN_OK);
|
|
|
|
scheduleMsg(&tsLogger, "BURN in %dms", currentTimeMillis() - nowMs);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2016-05-06 20:02:23 -07:00
|
|
|
|| command == TS_GET_FILE_RANGE
|
2019-07-06 17:48:58 -07:00
|
|
|
|| command == TS_SET_LOGGER_MODE
|
|
|
|
|| command == TS_GET_LOGGER_BUFFER
|
|
|
|
|| command == TS_GET_TEXT
|
|
|
|
|| command == TS_CRC_CHECK_COMMAND
|
2017-01-28 15:03:41 -08:00
|
|
|
|| command == TS_GET_FIRMWARE_VERSION;
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-01 09:11:57 -07:00
|
|
|
// this function runs indefinitely
|
2019-04-01 11:18:21 -07:00
|
|
|
void runBinaryProtocolLoop(ts_channel_s *tsChannel) {
|
2015-07-10 06:01:56 -07:00
|
|
|
int wasReady = false;
|
2017-03-15 19:20:53 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
while (true) {
|
2019-04-01 11:18:21 -07:00
|
|
|
int isReady = sr5IsReady(tsChannel);
|
2015-07-10 06:01:56 -07:00
|
|
|
if (!isReady) {
|
|
|
|
chThdSleepMilliseconds(10);
|
|
|
|
wasReady = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!wasReady) {
|
|
|
|
wasReady = true;
|
|
|
|
// scheduleSimpleMsg(&logger, "ts channel is now ready ", hTimeNow());
|
|
|
|
}
|
|
|
|
|
2017-03-26 14:19:08 -07:00
|
|
|
tsState.totalCounter++;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-03-15 19:20:53 -07:00
|
|
|
uint8_t firstByte;
|
2017-05-23 11:37:03 -07:00
|
|
|
int received = sr5ReadData(tsChannel, &firstByte, 1);
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2017-05-23 11:14:46 -07:00
|
|
|
logMsg("received %d\r\n", received);
|
2015-07-10 06:01:56 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-05-23 11:14:46 -07: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
|
2017-06-04 05:50:31 -07:00
|
|
|
// assume there's connection loss and notify the bluetooth init code
|
|
|
|
bluetoothSoftwareDisconnectNotify();
|
|
|
|
#endif /* EFI_BLUETOOTH_SETUP */
|
2015-07-10 06:01:56 -07:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-13 18:02:14 -08:00
|
|
|
onDataArrived();
|
2017-03-15 19:20:53 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
// scheduleMsg(logger, "Got first=%x=[%c]", firstByte, firstByte);
|
|
|
|
if (handlePlainCommand(tsChannel, firstByte))
|
|
|
|
continue;
|
|
|
|
|
2017-03-15 19:20:53 -07:00
|
|
|
uint8_t secondByte;
|
2017-05-23 11:37:03 -07:00
|
|
|
received = sr5ReadData(tsChannel, &secondByte, 1);
|
2017-05-23 11:14:46 -07:00
|
|
|
if (received != 1) {
|
2016-01-09 05:01:27 -08:00
|
|
|
tunerStudioError("TS: ERROR: no second byte");
|
2015-07-10 06:01:56 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// scheduleMsg(logger, "Got secondByte=%x=[%c]", secondByte, secondByte);
|
|
|
|
|
2019-01-05 04:55:21 -08:00
|
|
|
uint16_t incomingPacketSize = firstByte << 8 | secondByte;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
if (incomingPacketSize == 0 || incomingPacketSize > (sizeof(tsChannel->crcReadBuffer) - CRC_WRAPPING_SIZE)) {
|
|
|
|
scheduleMsg(&tsLogger, "TunerStudio: invalid size: %d", incomingPacketSize);
|
|
|
|
tunerStudioError("ERROR: CRC header size");
|
|
|
|
sendErrorCode(tsChannel);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-23 11:37:03 -07:00
|
|
|
received = sr5ReadData(tsChannel, (uint8_t* )tsChannel->crcReadBuffer, 1);
|
2017-05-23 11:14:46 -07:00
|
|
|
if (received != 1) {
|
2015-07-10 06:01:56 -07:00
|
|
|
tunerStudioError("ERROR: did not receive command");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char command = tsChannel->crcReadBuffer[0];
|
|
|
|
if (!isKnownCommand(command)) {
|
|
|
|
scheduleMsg(&tsLogger, "unexpected command %x", command);
|
|
|
|
sendErrorCode(tsChannel);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_SIMULATOR
|
2015-07-10 06:01:56 -07:00
|
|
|
logMsg("command %c\r\n", command);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
// scheduleMsg(logger, "TunerStudio: reading %d+4 bytes(s)", incomingPacketSize);
|
|
|
|
|
2017-05-23 11:37:03 -07:00
|
|
|
received = sr5ReadData(tsChannel, (uint8_t * ) (tsChannel->crcReadBuffer + 1),
|
2017-05-23 10:10:43 -07:00
|
|
|
incomingPacketSize + CRC_VALUE_SIZE - 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
int expectedSize = incomingPacketSize + CRC_VALUE_SIZE - 1;
|
2017-05-23 11:14:46 -07:00
|
|
|
if (received != expectedSize) {
|
|
|
|
scheduleMsg(&tsLogger, "Got only %d bytes while expecting %d for command %c", received,
|
2015-07-10 06:01:56 -07:00
|
|
|
expectedSize, command);
|
|
|
|
tunerStudioError("ERROR: not enough bytes in stream");
|
|
|
|
sendResponseCode(TS_CRC, tsChannel, TS_RESPONSE_UNDERRUN);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t expectedCrc = *(uint32_t*) (tsChannel->crcReadBuffer + incomingPacketSize);
|
|
|
|
|
|
|
|
expectedCrc = SWAP_UINT32(expectedCrc);
|
|
|
|
|
|
|
|
uint32_t actualCrc = crc32(tsChannel->crcReadBuffer, incomingPacketSize);
|
|
|
|
if (actualCrc != expectedCrc) {
|
|
|
|
scheduleMsg(&tsLogger, "TunerStudio: CRC %x %x %x %x", tsChannel->crcReadBuffer[incomingPacketSize + 0],
|
|
|
|
tsChannel->crcReadBuffer[incomingPacketSize + 1], tsChannel->crcReadBuffer[incomingPacketSize + 2],
|
|
|
|
tsChannel->crcReadBuffer[incomingPacketSize + 3]);
|
|
|
|
|
|
|
|
scheduleMsg(&tsLogger, "TunerStudio: command %c actual CRC %x/expected %x", tsChannel->crcReadBuffer[0],
|
|
|
|
actualCrc, expectedCrc);
|
|
|
|
tunerStudioError("ERROR: CRC issue");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scheduleMsg(logger, "TunerStudio: P00-07 %x %x %x %x %x %x %x %x", crcIoBuffer[0], crcIoBuffer[1],
|
|
|
|
// crcIoBuffer[2], crcIoBuffer[3], crcIoBuffer[4], crcIoBuffer[5], crcIoBuffer[6], crcIoBuffer[7]);
|
|
|
|
|
|
|
|
int success = tunerStudioHandleCrcCommand(tsChannel, tsChannel->crcReadBuffer, incomingPacketSize);
|
|
|
|
if (!success)
|
|
|
|
print("got unexpected TunerStudio command %x:%c\r\n", command, command);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 10:01:47 -08:00
|
|
|
static THD_FUNCTION(tsThreadEntryPoint, arg) {
|
2015-07-10 06:01:56 -07:00
|
|
|
(void) arg;
|
|
|
|
chRegSetThreadName("tunerstudio thread");
|
|
|
|
|
2017-05-23 10:10:43 -07:00
|
|
|
startTsPort(&tsChannel);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2019-04-01 11:18:21 -07:00
|
|
|
runBinaryProtocolLoop(&tsChannel);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2019-04-08 08:57:16 -07:00
|
|
|
/**
|
|
|
|
* Copy real configuration into the communications layer working copy
|
|
|
|
*/
|
2015-07-10 06:01:56 -07:00
|
|
|
void syncTunerStudioCopy(void) {
|
2019-04-12 19:10:57 -07:00
|
|
|
#if !defined(EFI_NO_CONFIG_WORKING_COPY)
|
2015-07-10 06:01:56 -07:00
|
|
|
memcpy(&configWorkingCopy, &persistentState.persistentConfiguration, sizeof(persistent_config_s));
|
2019-03-30 14:41:46 -07:00
|
|
|
#endif /* EFI_NO_CONFIG_WORKING_COPY */
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
tunerstudio_counters_s tsState;
|
|
|
|
TunerStudioOutputChannels tsOutputChannels;
|
|
|
|
|
|
|
|
short currentPageId;
|
|
|
|
|
|
|
|
void tunerStudioError(const char *msg) {
|
|
|
|
tunerStudioDebug(msg);
|
|
|
|
printErrorCounters();
|
|
|
|
tsState.errorCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Query with CRC takes place while re-establishing connection
|
|
|
|
* Query without CRC takes place on TunerStudio startup
|
|
|
|
*/
|
|
|
|
void handleQueryCommand(ts_channel_s *tsChannel, ts_response_format_e mode) {
|
|
|
|
tsState.queryCommandCounter++;
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_TUNER_STUDIO_VERBOSE
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&tsLogger, "got S/H (queryCommand) mode=%d", mode);
|
|
|
|
printTsStats();
|
|
|
|
#endif
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5SendResponse(tsChannel, mode, (const uint8_t *) TS_SIGNATURE, strlen(TS_SIGNATURE) + 1);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief 'Output' command sends out a snapshot of current values
|
|
|
|
*/
|
2017-05-27 06:00:18 -07:00
|
|
|
void handleOutputChannelsCommand(ts_channel_s *tsChannel, ts_response_format_e mode, uint16_t offset, uint16_t count) {
|
2017-05-27 06:05:08 -07:00
|
|
|
if (sizeof(TunerStudioOutputChannels) < offset + count) {
|
|
|
|
scheduleMsg(&tsLogger, "invalid offset/count %d/%d", offset, count);
|
|
|
|
sendErrorCode(tsChannel);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-27 06:00:18 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
tsState.outputChannelsCommandCounter++;
|
|
|
|
prepareTunerStudioOutputs();
|
|
|
|
// this method is invoked too often to print any debug information
|
2017-05-27 06:00:18 -07:00
|
|
|
sr5SendResponse(tsChannel, mode, ((const uint8_t *) &tsOutputChannels) + offset, count);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
2017-03-05 11:13:47 -08:00
|
|
|
#define TEST_RESPONSE_TAG " ts_p_alive\r\n"
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
void handleTestCommand(ts_channel_s *tsChannel) {
|
2017-05-09 09:07:52 -07:00
|
|
|
tsState.testCommandCounter++;
|
|
|
|
static char testOutputBuffer[12];
|
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
|
|
|
|
*/
|
|
|
|
tunerStudioDebug("got T (Test)");
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteData(tsChannel, (const uint8_t *) VCS_VERSION, sizeof(VCS_VERSION));
|
2019-01-11 06:58:48 -08:00
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " %d %d", engine->engineState.warnings.lastErrorCode, tsState.testCommandCounter);
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteData(tsChannel, (const uint8_t *) testOutputBuffer, strlen(testOutputBuffer));
|
2019-04-25 19:28:46 -07:00
|
|
|
chsnprintf(testOutputBuffer, sizeof(testOutputBuffer), " uptime=%ds", getTimeNowSeconds());
|
|
|
|
sr5WriteData(tsChannel, (const uint8_t *) testOutputBuffer, strlen(testOutputBuffer));
|
2015-07-10 06:01:56 -07:00
|
|
|
/**
|
2019-05-02 14:52:48 -07:00
|
|
|
* Please note that this response is a magic constant used by rusEfi console for protocol detection
|
2015-07-10 06:01:56 -07:00
|
|
|
* @see EngineState#TS_PROTOCOL_TAG
|
|
|
|
*/
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteData(tsChannel, (const uint8_t *) TEST_RESPONSE_TAG, sizeof(TEST_RESPONSE_TAG));
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
extern CommandHandler console_line_callback;
|
|
|
|
|
2017-05-06 19:37:43 -07:00
|
|
|
static void handleGetVersion(ts_channel_s *tsChannel, ts_response_format_e mode) {
|
2017-05-07 05:20:23 -07:00
|
|
|
static char versionBuffer[32];
|
|
|
|
chsnprintf(versionBuffer, sizeof(versionBuffer), "rusEFI v%d@%s", getRusEfiVersion(), VCS_VERSION);
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5SendResponse(tsChannel, mode, (const uint8_t *) versionBuffer, strlen(versionBuffer) + 1);
|
2017-01-28 15:03:41 -08:00
|
|
|
}
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
static void handleGetText(ts_channel_s *tsChannel) {
|
2017-03-26 14:19:08 -07:00
|
|
|
tsState.textCommandCounter++;
|
|
|
|
|
2018-04-01 20:49:57 -07:00
|
|
|
printOverallStatus(getTimeNowSeconds());
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
int outputSize;
|
|
|
|
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
|
|
|
|
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, 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
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleExecuteCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) {
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteCrcPacket(tsChannel, TS_RESPONSE_COMMAND_OK, NULL, 0);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true if legacy command was processed, false otherwise
|
|
|
|
*/
|
|
|
|
bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) {
|
2019-01-05 04:55:21 -08:00
|
|
|
// Bail fast if guaranteed not to be a plain command
|
|
|
|
if(command == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (command == TS_HELLO_COMMAND) {
|
2015-07-10 06:01:56 -07:00
|
|
|
scheduleMsg(&tsLogger, "Got naked Query command");
|
|
|
|
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."
|
|
|
|
*/
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
tunerStudioDebug("not ignoring F");
|
2017-05-23 11:37:03 -07:00
|
|
|
sr5WriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL));
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 20:01:42 -08:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
switch(command)
|
|
|
|
{
|
|
|
|
case TS_OUTPUT_COMMAND:
|
|
|
|
handleOutputChannelsCommand(tsChannel, TS_CRC, data16[0], data16[1]);
|
|
|
|
break;
|
|
|
|
case TS_HELLO_COMMAND:
|
2015-07-10 06:01:56 -07:00
|
|
|
tunerStudioDebug("got Query command");
|
|
|
|
handleQueryCommand(tsChannel, TS_CRC);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_GET_FIRMWARE_VERSION:
|
2017-05-06 19:37:43 -07:00
|
|
|
handleGetVersion(tsChannel, TS_CRC);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_GET_TEXT:
|
2015-07-10 06:01:56 -07:00
|
|
|
handleGetText(tsChannel);
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
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:
|
|
|
|
handlePageSelectCommand(tsChannel, TS_CRC, data16[0]);
|
|
|
|
break;
|
|
|
|
case TS_GET_STRUCT:
|
|
|
|
handleGetStructContent(tsChannel, data16[0], data16[1]);
|
|
|
|
break;
|
|
|
|
case TS_GET_FILE_RANGE:
|
|
|
|
handleReadFileContent(tsChannel, data16[0], data16[1], data16[2]);
|
|
|
|
break;
|
|
|
|
case TS_CHUNK_WRITE_COMMAND:
|
|
|
|
currentPageId = data16[0];
|
|
|
|
handleWriteChunkCommand(tsChannel, TS_CRC, data16[1], data16[2], data + sizeof(TunerStudioWriteChunkRequest));
|
|
|
|
break;
|
|
|
|
case TS_SINGLE_WRITE_COMMAND:
|
|
|
|
{
|
|
|
|
uint8_t value = data[4];
|
|
|
|
handleWriteValueCommand(tsChannel, TS_CRC, data16[0], data16[1], value);
|
2017-07-24 19:26:41 -07:00
|
|
|
}
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
|
|
|
case TS_CRC_CHECK_COMMAND:
|
|
|
|
handleCrc32Check(tsChannel, TS_CRC, data16[0], data16[1], data16[2]);
|
|
|
|
break;
|
|
|
|
case TS_BURN_COMMAND:
|
|
|
|
handleBurnCommand(tsChannel, TS_CRC, data16[0]);
|
|
|
|
break;
|
|
|
|
case TS_READ_COMMAND:
|
|
|
|
handlePageReadCommand(tsChannel, TS_CRC, data16[0], data16[1], data16[2]);
|
|
|
|
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) {
|
|
|
|
tsOutputChannels.debugIntField1++;
|
|
|
|
tsOutputChannels.debugIntField2 = subsystem;
|
|
|
|
tsOutputChannels.debugIntField3 = index;
|
|
|
|
}
|
2017-07-24 19:26:41 -07:00
|
|
|
|
2019-04-12 19:10:57 -07:00
|
|
|
#if EFI_PROD_CODE
|
2019-07-05 15:16:32 -07:00
|
|
|
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
|
2019-07-07 16:59:48 -07:00
|
|
|
case TS_SET_LOGGER_MODE:
|
2019-07-06 17:48:58 -07:00
|
|
|
switch(data[0]) {
|
|
|
|
case 0x01:
|
|
|
|
EnableToothLogger();
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
DisableToothLogger();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// dunno what that was, send NAK
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendOkResponse(tsChannel, TS_CRC);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case TS_GET_LOGGER_BUFFER:
|
|
|
|
{
|
|
|
|
auto toothBuffer = GetToothLoggerBuffer();
|
|
|
|
sr5SendResponse(tsChannel, TS_CRC, toothBuffer.Buffer, toothBuffer.Length);
|
|
|
|
}
|
|
|
|
|
2019-07-05 15:16:32 -07:00
|
|
|
break;
|
2019-07-07 12:22:46 -07:00
|
|
|
#endif /* EFI_TOOTH_LOGGER */
|
2019-07-05 15:16:32 -07:00
|
|
|
default:
|
2015-07-10 06:01:56 -07:00
|
|
|
tunerStudioError("ERROR: ignoring unexpected command");
|
|
|
|
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");
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
memset(&tsState, 0, sizeof(tsState));
|
|
|
|
syncTunerStudioCopy();
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-02-04 10:01:47 -08:00
|
|
|
chThdCreateStatic(tsThreadStack, sizeof(tsThreadStack), NORMALPRIO, (tfunc_t)tsThreadEntryPoint, NULL);
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|