GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 14.3% 1 / 0 / 7
Functions: 20.0% 1 / 0 / 5
Branches: -% 0 / 0 / 0
Decisions: -% 0 / - / 0

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