rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
ts_can_channel.cpp
Go to the documentation of this file.
1/**
2 * @file This file implements CAN-to-TS bridge.
3 *
4 * @date Apr 24, 2021
5 * @author andreika <prometheus.pcb@gmail.com>
6 * @author Andrey Belomutskiy, (c) 2012-2021
7 */
8
9#include "pch.h"
10#include "tunerstudio.h"
11#include "tunerstudio_io.h"
12
13#if EFI_CAN_SERIAL
14#include "serial_can.h"
15#include "can_hw.h"
16
17#if !EFI_CAN_SUPPORT
18#error "EFI_CAN_SERIAL requires EFI_CAN_SUPPORT"
19#endif
20
21
22class CanTsChannel final : public TsChannelBase {
23public:
24 CanTsChannel() : TsChannelBase("CAN") {
25
26 }
27 void start();
28
29 // TsChannelBase implementation
30 void write(const uint8_t* buffer, size_t size, bool) override;
31 size_t readTimeout(uint8_t* buffer, size_t size, int timeout) override;
32 void flush() override;
33 bool isReady() const override;
34 void stop() override;
35
36 // Special override for writeCrcPacket for small packets
37 void writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets = false) override;
38};
39
40
41void CanTsChannel::start() {
42 if (!getIsCanEnabled()) {
44 return;
45 }
46
48 warning(ObdCode::CUSTOM_ERR_CAN_CONFIGURATION, "CAN read or write not enabled");
49 }
50}
51
52void CanTsChannel::stop() {
53}
54
55void CanTsChannel::writeCrcPacket(uint8_t responseCode, const uint8_t* buf, size_t size, bool allowLongPackets) {
56#ifdef TS_CAN_DEVICE_SHORT_PACKETS_IN_ONE_FRAME
57 // a special case for short packets: we can send them in 1 frame, without CRC & size,
58 // because the CAN protocol is already protected by its own checksum.
59 if ((size + 1) <= 7) {
60 write(&responseCode, 1, false); // header without size
61 if (size > 0) {
62 write(buf, size, false); // body
63 }
64 flush();
65 return;
66 }
67#endif /* TS_CAN_DEVICE_SHORT_PACKETS_IN_ONE_FRAME */
68
69 // Packet too large, use default implementation
70 TsChannelBase::writeCrcPacket(responseCode, buf, size, allowLongPackets);
71}
72
73void CanTsChannel::write(const uint8_t* buffer, size_t size, bool) {
74 canStreamAddToTxTimeout(&size, buffer, BINARY_IO_TIMEOUT);
75}
76
77size_t CanTsChannel::readTimeout(uint8_t* buffer, size_t size, int timeout) {
79 return size;
80}
81
82void CanTsChannel::flush() {
83 canStreamFlushTx(BINARY_IO_TIMEOUT);
84}
85
86bool CanTsChannel::isReady() const {
87 // this channel is always ready
88 return true;
89}
90
91static CanTsChannel canChannel;
92
93struct CanTsThread : public TunerstudioThread {
94 CanTsThread() : TunerstudioThread("CAN TS Channel") { }
95
96 TsChannelBase* setupChannel() override {
97 canChannel.start();
98 return &canChannel;
99 }
100};
101
102static CanTsThread canTsThread;
103
105 canTsThread.start();
107}
108
109#endif // EFI_CAN_SERIAL
bool getIsCanEnabled(void)
Definition can_hw.cpp:223
virtual void stop()
virtual bool isReady() const
virtual void flush()
virtual void writeCrcPacket(uint8_t responseCode, const uint8_t *buf, size_t size, bool allowLongPackets=false)
virtual void write(const uint8_t *buffer, size_t size, bool isEndOfPacket=false)=0
virtual size_t readTimeout(uint8_t *buffer, size_t size, int timeout)=0
virtual TsChannelBase * setupChannel()=0
static bool write(void *, uint32_t, const uint8_t *, uint32_t)
static constexpr engine_configuration_s * engineConfiguration
bool warning(ObdCode code, const char *fmt,...)
@ CUSTOM_ERR_CAN_CONFIGURATION
msg_t canStreamReceiveTimeout(size_t *np, uint8_t *rxbuf, sysinterval_t timeout)
msg_t canStreamAddToTxTimeout(size_t *np, const uint8_t *txbuf, sysinterval_t timeout)
msg_t canStreamFlushTx(sysinterval_t timeout)
void canStreamInit(void)
composite packet size
static BigBufferHandle buffer
static CanTsThread canTsThread
void startCanConsole()
static CanTsChannel canChannel