LCOV - code coverage report
Current view: top level - firmware/console/binary - tunerstudio_io.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 4 25.0 %
Date: 2024-04-25 02:23:43 Functions: 1 4 25.0 %

          Line data    Source code
       1             : /**
       2             :  * @file        tunerstudio_io.h
       3             :  * @file TS protocol commands and methods are here
       4             :  *
       5             :  * @date Mar 8, 2015
       6             :  * @author Andrey Belomutskiy, (c) 2012-2020
       7             :  */
       8             : 
       9             : #pragma once
      10             : #include "global.h"
      11             : #include "tunerstudio_impl.h"
      12             : 
      13             : #if EFI_USB_SERIAL
      14             : #include "usbconsole.h"
      15             : #endif // EFI_USB_SERIAL
      16             : 
      17             : #if EFI_PROD_CODE
      18             : #include "pin_repository.h"
      19             : #endif
      20             : 
      21             : #ifndef USART_CR2_STOP1_BITS
      22             : // todo: acticulate why exactly does prometheus_469 as for this hack
      23             : #define USART_CR2_STOP1_BITS 0
      24             : #endif
      25             : 
      26             : #define SCRATCH_BUFFER_PREFIX_SIZE 3
      27             : 
      28             : class TsChannelBase {
      29             : public:
      30             :         TsChannelBase(const char *name);
      31             :         // Virtual functions - implement these for your underlying transport
      32             :         virtual void write(const uint8_t* buffer, size_t size, bool isEndOfPacket = false) = 0;
      33             :         virtual size_t readTimeout(uint8_t* buffer, size_t size, int timeout) = 0;
      34             : 
      35             :         // These functions are optional to implement, not all channels need them
      36           4 :         virtual void flush() { }
      37           0 :         virtual bool isConfigured() const { return true; }
      38           0 :         virtual bool isReady() const { return true; }
      39           0 :         virtual void stop() { }
      40             : 
      41             :         // Base functions that use the above virtual implementation
      42             :         size_t read(uint8_t* buffer, size_t size);
      43             : 
      44             :         int bytesIn = 0;
      45             :         int bytesOut = 0;
      46             : 
      47             : #ifdef EFI_CAN_SERIAL
      48             :         virtual // CAN device needs this function to be virtual for small-packet optimization
      49             : #endif
      50             :         void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false);
      51             :         void sendResponse(ts_response_format_e mode, const uint8_t * buffer, int size, bool allowLongPackets = false);
      52             : 
      53             :         /**
      54             :          * See 'blockingFactor' in rusefi.ini
      55             :          */
      56             :         char scratchBuffer[BLOCKING_FACTOR + 30];
      57             :         const char *name;
      58             : 
      59             :         void assertPacketSize(size_t size, bool allowLongPackets);
      60             :         uint32_t writePacketHeader(const uint8_t responseCode, const size_t size);
      61             :         void crcAndWriteBuffer(const uint8_t responseCode, const size_t size);
      62             :         void copyAndWriteSmallCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size);
      63             : 
      64             :         /* When TsChannel is in "not in sync" state tsProcessOne will silently try to find
      65             :          * begining of packet.
      66             :          * As soon as tsProcessOne was able to receive valid packet with valid size and crc
      67             :          * TsChannel becomes "in sync". That means it will react on any futher errors: it will
      68             :          * emit packet with error code and switch back to "not in sync" mode.
      69             :          * This insures that RE will send only one error message after lost of synchronization
      70             :          * with TS.
      71             :          * Also while in "not in sync" state - tsProcessOne will not try to receive whole packet
      72             :          * by one read. Instead after getting packet size it will try to receive one byte of
      73             :          * command and check if it is supported. */
      74             :         bool in_sync = false;
      75             : 
      76             : private:
      77             :         void writeCrcPacketLarge(uint8_t responseCode, const uint8_t* buf, size_t size);
      78             : };
      79             : 
      80             : // This class represents a channel for a physical async serial poart
      81             : class SerialTsChannelBase : public TsChannelBase {
      82             : public:
      83             :         SerialTsChannelBase(const char *p_name) : TsChannelBase(p_name) {};
      84             :         // Open the serial port with the specified baud rate
      85             :         virtual void start(uint32_t baud) = 0;
      86             : };
      87             : 
      88             : #if HAL_USE_SERIAL
      89             : // This class implements a ChibiOS Serial Driver
      90             : class SerialTsChannel final : public SerialTsChannelBase {
      91             : public:
      92             :         SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { }
      93             : 
      94             :         void start(uint32_t baud) override;
      95             :         void stop() override;
      96             : 
      97             :         void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
      98             :         size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
      99             : 
     100             : private:
     101             :         SerialDriver* const m_driver;
     102             : };
     103             : #endif // HAL_USE_SERIAL
     104             : 
     105             : #if HAL_USE_UART
     106             : // This class implements a ChibiOS UART Driver
     107             : class UartTsChannel : public SerialTsChannelBase {
     108             : public:
     109             :         UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { }
     110             : 
     111             :         void start(uint32_t baud) override;
     112             :         void stop() override;
     113             : 
     114             :         void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override;
     115             :         size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
     116             : 
     117             : protected:
     118             :         UARTDriver* const m_driver;
     119             :         UARTConfig m_config;
     120             : };
     121             : #endif // HAL_USE_UART
     122             : 
     123             : #define CRC_VALUE_SIZE 4
     124             : 
     125             : // that's 1 second
     126             : #define BINARY_IO_TIMEOUT TIME_MS2I(1000)
     127             : 
     128             : // that's 1 second
     129             : #define SR5_READ_TIMEOUT TIME_MS2I(1000)
     130             : 
     131             : void startSerialChannels();
     132             : SerialTsChannelBase* getBluetoothChannel();
     133             : 
     134             : void startCanConsole();
     135             : 
     136             : void sendOkResponse(TsChannelBase *tsChannel, ts_response_format_e mode);

Generated by: LCOV version 1.14