int tunerStudioHandleCrcCommand(ts_channel_s *tsChannel, char *data, int incomingPacketSize) { char command = data[0]; data++; if (command == TS_HELLO_COMMAND || command == TS_HELLO_COMMAND_DEPRECATED) { tunerStudioDebug("got Query command"); handleQueryCommand(tsChannel, TS_CRC); } else if (command == TS_GET_TEXT) { handleGetText(tsChannel); } else if (command == TS_EXECUTE) { handleExecuteCommand(tsChannel, data, incomingPacketSize - 1); } else if (command == TS_OUTPUT_COMMAND) { handleOutputChannelsCommand(tsChannel, TS_CRC); } else if (command == TS_PAGE_COMMAND) { uint16_t page = *(uint16_t *) data; handlePageSelectCommand(tsChannel, TS_CRC, page); } else if (command == TS_CHUNK_WRITE_COMMAND) { currentPageId = *(uint16_t *) data; uint16_t offset = *(uint16_t *) (data + 2); uint16_t count = *(uint16_t *) (data + 4); handleWriteChunkCommand(tsChannel, TS_CRC, offset, count, data + sizeof(TunerStudioWriteChunkRequest)); } else if (command == TS_SINGLE_WRITE_COMMAND) { uint16_t page = *(uint16_t *) data; uint16_t offset = *(uint16_t *) (data + 2); uint8_t value = data[4]; handleWriteValueCommand(tsChannel, TS_CRC, page, offset, value); } else if (command == TS_CRC_CHECK_COMMAND) { uint16_t page = *(uint16_t *) data; uint16_t offset = *(uint16_t *) (data + 2); uint16_t count = *(uint16_t *) (data + 4); handleCrc32Check(tsChannel, TS_CRC, page, offset, count); } else if (command == TS_BURN_COMMAND) { uint16_t page = *(uint16_t *) data; handleBurnCommand(tsChannel, TS_CRC, page); } else if (command == TS_READ_COMMAND) { uint16_t page = *(uint16_t *) data; uint16_t offset = *(uint16_t *) (data + 2); uint16_t count = *(uint16_t *) (data + 4); handlePageReadCommand(tsChannel, TS_CRC, page, offset, count); } else if (command == 't' || command == 'T') { handleTestCommand(tsChannel); } else if (command == TS_LEGACY_HELLO_COMMAND) { /** * 'Q' is the query command used for compatibility with older ECUs */ tunerStudioDebug("ignoring Q"); } else if (command == TS_COMMAND_F) { tunerStudioDebug("ignoring F"); /** * 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." */ } else { tunerStudioError("ERROR: ignoring unexpected command"); return false; } return true; }
int tunerStudioHandleCommand(short command) { if (command == 'H') { handleQueryCommand(); } else if (command == 'O') { handleOutputChannelsCommand(); } else if (command == 'W') { handleValueWriteCommand(); } else if (command == 'B') { handleBurnCommand(); } else if (command == 'C') { handlePageReadCommand(); } else if (command == 't' || command == 'T') { handleTestCommand(); } else if (command == 'F') { tunerStudioDebug("ignoring F"); /** * 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." */ } else { #if EFI_TUNER_STUDIO_OVER_USB /** * With TTL there is a real chance of corrupted messages. * With serial-over-USB we are not expecting communication errors */ // fatal("unexpected TunerStudio command in USB mode"); #endif /* EFI_TUNER_STUDIO_OVER_USB */ tsState.errorCounter++; return FALSE; } return TRUE; }
/** * '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) { tsState.writeValueCommandCounter++; currentPageId = page; tunerStudioDebug("got W (Write)"); // we can get a lot of these #if EFI_TUNER_STUDIO_VERBOSE // 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; yellowMagic(currentPageId, offset, 1); // scheduleMsg(logger, "va=%d", configWorkingCopy.boardConfiguration.idleValvePin); }
static bool handlePlainCommand(uint8_t command) { if (command == TS_HELLO_COMMAND) { scheduleMsg(&logger, "Got naked Query command"); handleQueryCommand(TS_PLAIN); return true; } else if (command == 't' || command == 'T') { handleTestCommand(); return true; } else if (command == TS_PAGE_COMMAND) { int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&pageIn, sizeof(pageIn)); // todo: validate 'recieved' value handlePageSelectCommand(TS_PLAIN, pageIn); return true; } else if (command == TS_READ_COMMAND) { //scheduleMsg(&logger, "Got naked READ PAGE???"); int recieved = chSequentialStreamRead(getTsSerialDevice(), (uint8_t *)&readRequest, sizeof(readRequest)); if (recieved != sizeof(readRequest)) { // todo: handler error return true; } handlePageReadCommand(TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count); return true; } else if (command == TS_OUTPUT_COMMAND) { //scheduleMsg(&logger, "Got naked Channels???"); handleOutputChannelsCommand(TS_PLAIN); return true; } else if (command == 'F') { tunerStudioDebug("not ignoring F"); tunerStudioWriteData((const uint8_t *) PROTOCOL, strlen(PROTOCOL)); return true; } else { return false; } }
void handlePageReadCommand(ts_response_format_e mode, uint16_t pageId, uint16_t offset, uint16_t count) { tsState.readPageCommandsCounter++; tunerStudioDebug("got R (Read page)"); tsState.currentPageId = pageId; #if EFI_TUNER_STUDIO_VERBOSE scheduleMsg(&logger, "Page requested: page %d offset=%d count=%d", (int)tsState.currentPageId, offset, count); #endif if (tsState.currentPageId > MAX_PAGE_ID) { scheduleMsg(&logger, "invalid Page number %x", tsState.currentPageId); // something is not right here tsState.currentPageId = 0; tunerStudioError("ERROR: invalid page"); return; } int size = getTunerStudioPageSize(tsState.currentPageId); if (size < offset + count) { scheduleMsg(&logger, "invalid offset/count %d/%d", offset, count); sendErrorCode(); return; } const uint8_t *addr = (const uint8_t *) (getWorkingPageAddr(tsState.currentPageId) + offset); tsSendResponse(mode, addr, count); #if EFI_TUNER_STUDIO_VERBOSE scheduleMsg(&logger, "Sending %d done", count); #endif }
void handleTestCommand(void) { /** * this is NOT a standard TunerStudio command, this is my own * extension of the protocol to simplify troubleshooting */ tunerStudioDebug("got T (Test)\r\n"); tunerStudioWriteData((const uint8_t *) "alive\r\n", 7); }
void handleTestCommand(ts_channel_s *tsChannel) { /** * this is NOT a standard TunerStudio command, this is my own * extension of the protocol to simplify troubleshooting */ tunerStudioDebug("got T (Test)"); tunerStudioWriteData(tsChannel, (const uint8_t *) VCS_VERSION, sizeof(VCS_VERSION)); /** * Please note that this response is a magic constant used by dev console for protocol detection * @see EngineState#TS_PROTOCOL_TAG */ tunerStudioWriteData(tsChannel, (const uint8_t *) " ts_p_alive\r\n", 8); }
/** * '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++; tunerStudioDebug("got B (Burn)"); currentPageId = page; #if EFI_TUNER_STUDIO_VERBOSE // pointless since we only have one page now // scheduleMsg(logger, "Page number %d", currentPageId); #endif // todo: how about some multi-threading? memcpy(&persistentState.persistentConfiguration, &configWorkingCopy, sizeof(persistent_config_s)); requestBurn(); tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_BURN_OK, NULL, 0); scheduleMsg(&tsLogger, "BURN in %dms", currentTimeMillis() - nowMs); }
/** * 'Burn' command is a command to commit the changes */ void handleBurnCommand(ts_response_format_e mode, uint16_t page) { efitimems_t nowMs = currentTimeMillis(); tsState.burnCommandCounter++; tunerStudioDebug("got B (Burn)"); tsState.currentPageId = page; #if EFI_TUNER_STUDIO_VERBOSE // pointless since we only have one page now // scheduleMsg(&logger, "Page number %d", tsState.currentPageId); #endif // todo: how about some multi-threading? memcpy(&persistentState.persistentConfiguration, &configWorkingCopy, sizeof(persistent_config_s)); #if EFI_INTERNAL_FLASH setNeedToWriteConfiguration(); #endif incrementGlobalConfigurationVersion(); tunerStudioWriteCrcPacket(TS_RESPONSE_BURN_OK, NULL, 0); scheduleMsg(&logger, "burned in (ms): %d", currentTimeMillis() - nowMs); }
/** * @return true if legacy command was processed, false otherwise */ bool handlePlainCommand(ts_channel_s *tsChannel, uint8_t command) { if (command == TS_HELLO_COMMAND || command == TS_HELLO_COMMAND_DEPRECATED) { scheduleMsg(&tsLogger, "Got naked Query command"); handleQueryCommand(tsChannel, TS_PLAIN); return true; } else if (command == 't' || command == 'T') { handleTestCommand(tsChannel); return true; } else if (command == TS_PAGE_COMMAND) { int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&pageIn, sizeof(pageIn), TS_READ_TIMEOUT); if (recieved != sizeof(pageIn)) { tunerStudioError("ERROR: not enough for PAGE"); return true; } handlePageSelectCommand(tsChannel, TS_PLAIN, pageIn); return true; } else if (command == TS_BURN_COMMAND) { scheduleMsg(&tsLogger, "Got naked BURN"); uint16_t page; int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&page, sizeof(page), TS_READ_TIMEOUT); if (recieved != sizeof(page)) { tunerStudioError("ERROR: Not enough for plain burn"); return true; } handleBurnCommand(tsChannel, TS_PLAIN, page); return true; } else if (command == TS_CHUNK_WRITE_COMMAND) { scheduleMsg(&tsLogger, "Got naked CHUNK_WRITE"); int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&writeChunkRequest, sizeof(writeChunkRequest), TS_READ_TIMEOUT); if (recieved != sizeof(writeChunkRequest)) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write header: %d", recieved); tsState.errorCounter++; return true; } recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&tsChannel->crcReadBuffer, writeChunkRequest.count, TS_READ_TIMEOUT); if (recieved != writeChunkRequest.count) { scheduleMsg(&tsLogger, "ERROR: Not enough for plain chunk write content: %d while expecting %d", recieved, writeChunkRequest.count); tsState.errorCounter++; return true; } currentPageId = writeChunkRequest.page; handleWriteChunkCommand(tsChannel, TS_PLAIN, writeChunkRequest.offset, writeChunkRequest.count, (uint8_t *) &tsChannel->crcReadBuffer); return true; } else if (command == TS_READ_COMMAND) { //scheduleMsg(logger, "Got naked READ PAGE???"); int recieved = chnReadTimeout(tsChannel->channel, (uint8_t * )&readRequest, sizeof(readRequest), TS_READ_TIMEOUT); if (recieved != sizeof(readRequest)) { tunerStudioError("Not enough for plain read header"); return true; } handlePageReadCommand(tsChannel, TS_PLAIN, readRequest.page, readRequest.offset, readRequest.count); return true; } else if (command == TS_OUTPUT_COMMAND) { //scheduleMsg(logger, "Got naked Channels???"); handleOutputChannelsCommand(tsChannel, TS_PLAIN); return true; } else if (command == TS_LEGACY_HELLO_COMMAND) { tunerStudioDebug("ignoring LEGACY_HELLO_COMMAND"); return true; } else if (command == TS_COMMAND_F) { tunerStudioDebug("not ignoring F"); tunerStudioWriteData(tsChannel, (const uint8_t *) PROTOCOL, strlen(PROTOCOL)); return true; } else { return false; } }
void tunerStudioError(const char *msg) { tunerStudioDebug(msg); tsState.errorCounter++; }
void handleQueryCommand(void) { tsState.queryCommandCounter++; tunerStudioDebug("got H (queryCommand)"); tunerStudioWriteData((const uint8_t *) TS_SIGNATURE, strlen(TS_SIGNATURE) + 1); }