2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file tunerstudio_io.h
|
2020-05-25 09:55:26 -07:00
|
|
|
* @file TS protocol commands and methods are here
|
2015-07-10 06:01:56 -07:00
|
|
|
*
|
|
|
|
* @date Mar 8, 2015
|
2020-01-13 18:57:43 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2020
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
2020-04-01 16:00:56 -07:00
|
|
|
#pragma once
|
2018-09-16 19:25:17 -07:00
|
|
|
#include "global.h"
|
2022-04-13 22:06:44 -07:00
|
|
|
#include "tunerstudio_impl.h"
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-11-19 21:39:08 -08:00
|
|
|
#if (!TS_NO_PRIMARY && defined(TS_PRIMARY_PORT))
|
2021-07-26 06:08:05 -07:00
|
|
|
#define HAS_PRIMARY true
|
|
|
|
#else
|
|
|
|
#define HAS_PRIMARY false
|
|
|
|
#endif
|
|
|
|
|
2021-11-19 21:39:08 -08:00
|
|
|
#if (!TS_NO_SECONDARY && defined(TS_SECONDARY_PORT))
|
2021-07-26 06:08:05 -07:00
|
|
|
#define HAS_SECONDARY true
|
|
|
|
#else
|
|
|
|
#define HAS_SECONDARY false
|
|
|
|
#endif
|
|
|
|
|
2020-09-23 11:06:22 -07:00
|
|
|
#if EFI_USB_SERIAL
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "usbconsole.h"
|
2020-09-23 11:06:22 -07:00
|
|
|
#endif // EFI_USB_SERIAL
|
2020-09-09 11:35:01 -07:00
|
|
|
|
|
|
|
#if EFI_PROD_CODE
|
2015-07-10 06:01:56 -07:00
|
|
|
#include "pin_repository.h"
|
|
|
|
#endif
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
class TsChannelBase {
|
|
|
|
public:
|
2021-09-18 12:33:14 -07:00
|
|
|
TsChannelBase(const char *name);
|
2021-02-19 23:11:39 -08:00
|
|
|
// Virtual functions - implement these for your underlying transport
|
2021-10-18 16:59:08 -07:00
|
|
|
virtual void write(const uint8_t* buffer, size_t size, bool isEndOfPacket = false) = 0;
|
2021-02-19 23:11:39 -08:00
|
|
|
virtual size_t readTimeout(uint8_t* buffer, size_t size, int timeout) = 0;
|
2021-03-17 05:39:13 -07:00
|
|
|
|
|
|
|
// These functions are optional to implement, not all channels need them
|
|
|
|
virtual void flush() { }
|
2021-03-18 11:07:22 -07:00
|
|
|
virtual bool isConfigured() const { return true; }
|
|
|
|
virtual bool isReady() const { return true; }
|
2021-03-17 05:39:13 -07:00
|
|
|
virtual void stop() { }
|
2021-02-19 23:11:39 -08:00
|
|
|
|
|
|
|
// Base functions that use the above virtual implementation
|
2021-02-19 04:40:59 -08:00
|
|
|
size_t read(uint8_t* buffer, size_t size);
|
2021-02-19 17:48:21 -08:00
|
|
|
|
2021-12-01 08:13:36 -08:00
|
|
|
#ifdef EFI_CAN_SERIAL
|
2021-03-17 05:39:13 -07:00
|
|
|
virtual // CAN device needs this function to be virtual for small-packet optimization
|
|
|
|
#endif
|
2022-02-11 13:03:20 -08:00
|
|
|
void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false);
|
|
|
|
void sendResponse(ts_response_format_e mode, const uint8_t * buffer, int size, bool allowLongPackets = false);
|
2021-02-19 04:40:59 -08:00
|
|
|
|
2015-08-22 10:01:35 -07:00
|
|
|
/**
|
2017-06-20 21:16:19 -07:00
|
|
|
* See 'blockingFactor' in rusefi.ini
|
2015-08-22 10:01:35 -07:00
|
|
|
*/
|
2020-11-14 16:21:29 -08:00
|
|
|
char scratchBuffer[BLOCKING_FACTOR + 30];
|
2021-09-18 12:33:14 -07:00
|
|
|
const char *name;
|
2020-06-21 13:37:33 -07:00
|
|
|
|
2022-04-13 23:01:34 -07:00
|
|
|
void assertPacketSize(size_t size, bool allowLongPackets);
|
2022-04-14 19:41:46 -07:00
|
|
|
void crcAndWriteBuffer(uint8_t responseCode, size_t size);
|
|
|
|
void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size);
|
2022-04-13 23:01:34 -07:00
|
|
|
|
2022-07-20 14:54:10 -07:00
|
|
|
/* When TsChannel is in "not in sync" state tsProcessOne will silently try to find
|
|
|
|
* begining of packet.
|
|
|
|
* As soon as tsProcessOne was able to receive valid packet with valid size and crc
|
|
|
|
* TsChannel becomes "in sync". That means it will react on any futher errors: it will
|
|
|
|
* emit packet with error code and switch back to "not in sync" mode.
|
|
|
|
* This insures that RE will send only one error message after lost of syncronization
|
|
|
|
* with TS.
|
|
|
|
* Also while in "not in sync" state - tsProcessOne will not try to receive whole packet
|
|
|
|
* by one read. Instead after getting packet size it will try to receive one byte of
|
|
|
|
* command and check if it is supported. */
|
|
|
|
bool in_sync = false;
|
|
|
|
|
2022-04-13 23:01:34 -07:00
|
|
|
private:
|
2021-02-19 17:48:21 -08:00
|
|
|
void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size);
|
2020-06-21 13:37:33 -07:00
|
|
|
};
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2021-03-19 14:05:04 -07:00
|
|
|
// This class represents a channel for a physical async serial poart
|
|
|
|
class SerialTsChannelBase : public TsChannelBase {
|
2021-03-18 11:07:22 -07:00
|
|
|
public:
|
2021-09-18 12:33:14 -07:00
|
|
|
SerialTsChannelBase(const char *name) : TsChannelBase(name) {};
|
2021-03-19 14:05:04 -07:00
|
|
|
// Open the serial port with the specified baud rate
|
|
|
|
virtual void start(uint32_t baud) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
#if HAL_USE_SERIAL
|
|
|
|
// This class implements a ChibiOS Serial Driver
|
|
|
|
class SerialTsChannel : public SerialTsChannelBase {
|
|
|
|
public:
|
2021-09-18 12:33:14 -07:00
|
|
|
SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { }
|
2021-03-19 14:05:04 -07:00
|
|
|
|
|
|
|
void start(uint32_t baud) override;
|
|
|
|
void stop() override;
|
|
|
|
|
2021-10-18 16:59:08 -07:00
|
|
|
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
|
2021-03-19 14:05:04 -07:00
|
|
|
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
SerialDriver* const m_driver;
|
|
|
|
};
|
|
|
|
#endif // HAL_USE_SERIAL
|
|
|
|
|
|
|
|
#if HAL_USE_UART
|
|
|
|
// This class implements a ChibiOS UART Driver
|
|
|
|
class UartTsChannel : public SerialTsChannelBase {
|
2021-03-28 06:06:36 -07:00
|
|
|
public:
|
2021-09-18 12:33:14 -07:00
|
|
|
UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { }
|
2021-03-19 14:05:04 -07:00
|
|
|
|
|
|
|
void start(uint32_t baud) override;
|
|
|
|
void stop() override;
|
2021-03-18 11:07:22 -07:00
|
|
|
|
2021-10-18 16:59:08 -07:00
|
|
|
void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
|
2021-03-18 11:07:22 -07:00
|
|
|
size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
|
|
|
|
|
2021-03-28 06:06:36 -07:00
|
|
|
protected:
|
2021-03-19 14:05:04 -07:00
|
|
|
UARTDriver* const m_driver;
|
|
|
|
UARTConfig m_config;
|
2021-03-18 11:07:22 -07:00
|
|
|
};
|
2021-03-19 14:05:04 -07:00
|
|
|
#endif // HAL_USE_UART
|
2021-03-18 11:07:22 -07:00
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
#define CRC_VALUE_SIZE 4
|
|
|
|
// todo: double-check this
|
|
|
|
#define CRC_WRAPPING_SIZE (CRC_VALUE_SIZE + 3)
|
|
|
|
|
2017-05-23 10:10:43 -07:00
|
|
|
// that's 1 second
|
2019-01-28 17:00:17 -08:00
|
|
|
#define BINARY_IO_TIMEOUT TIME_MS2I(1000)
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2017-05-23 11:54:40 -07:00
|
|
|
// that's 1 second
|
2019-01-28 17:00:17 -08:00
|
|
|
#define SR5_READ_TIMEOUT TIME_MS2I(1000)
|
2017-05-23 11:37:03 -07:00
|
|
|
|
2021-03-28 06:06:36 -07:00
|
|
|
void startSerialChannels();
|
|
|
|
SerialTsChannelBase* getBluetoothChannel();
|
|
|
|
|
2021-12-01 08:13:36 -08:00
|
|
|
void startCanConsole();
|
|
|
|
|
2021-02-19 23:11:39 -08:00
|
|
|
void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode);
|