rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
lua_can_rx.cpp
Go to the documentation of this file.
1#include "pch.h"
2
3#include "can_filter.h"
4
5#if EFI_CAN_SUPPORT
6
7#include "rusefi_lua.h"
8
9extern "C" {
10 #include "lapi.h"
11 #include "ltable.h"
12 #include "lgc.h"
13}
14
15// Stores information about one received CAN frame: which bus, plus the actual frame
16struct CanFrameData {
17 uint8_t BusIndex;
18 int Callback;
19 CANRxFrame Frame;
20};
21
22#ifndef LUA_canFrameCount
23#if defined(STM32F7) || defined(STM32H7)
24#define LUA_canFrameCount 256
25#else
26#define LUA_canFrameCount 32
27#endif
28#endif
29
30static CanFrameData canFrames[LUA_canFrameCount];
31// CAN frame buffers that are not in use
32static chibios_rt::Mailbox<CanFrameData*, LUA_canFrameCount> freeBuffers;
33// CAN frame buffers that are waiting to be processed by the lua thread
34static chibios_rt::Mailbox<CanFrameData*, LUA_canFrameCount> filledBuffers;
35
36static size_t dropRxCount = 0;
37
38void processLuaCan(const size_t busIndex, const CANRxFrame& frame) {
39 auto filter = getFilterForId(busIndex, CAN_ID(frame));
40
41 // Filter the frame if we aren't listening for it
42 if (!filter) {
43 return;
44 }
45
46 CanFrameData* frameBuffer;
47 msg_t msg;
48
49 {
50 // Acquire a buffer under lock
51 chibios_rt::CriticalSectionLocker csl;
52 msg = freeBuffers.fetchI(&frameBuffer);
53 }
54
55 if (msg != MSG_OK) {
56 // all buffers are already in use, this frame will be dropped!
58 return;
59 }
60
61 // Copy the frame in to the buffer
62 frameBuffer->BusIndex = busIndex;
63 frameBuffer->Frame = frame;
64 frameBuffer->Callback = filter->Callback;
65
66 {
67 // Push the frame in to the queue under lock
68 chibios_rt::CriticalSectionLocker csl;
69 filledBuffers.postI(frameBuffer);
70 }
71}
72
73// From lapi.c:762 lua_createtable, modified slightly
74static void lua_createtable_noGC(lua_State *L, int narray) {
75 Table *t;
76 lua_lock(L);
77 t = luaH_new(L);
78 sethvalue2s(L, L->top.p, t);
79 api_incr_top(L);
80 luaH_resize(L, t, narray, 0);
81
82 // This line is commented out - no need to do a GC every time in
83 // this hot path when we'll do it shortly and have plenty of memory available.
84 // luaC_checkGC(L);
85
86 lua_unlock(L);
87}
88
89static void handleCanFrame(LuaHandle& ls, CanFrameData* data) {
91 if (data->Callback == NO_CALLBACK) {
92 // No callback, use catch-all function
93 lua_getglobal(ls, "onCanRx");
94 } else {
95 // Push the specified callback on to the stack
96 lua_rawgeti(ls, LUA_REGISTRYINDEX, data->Callback);
97 }
98
99 auto frameCanId = CAN_ID(data->Frame);
100
101 if (lua_isnil(ls, -1)) {
102 // no rx function, ignore
103 efiPrintf("LUA CAN rx missing function onCanRx ID=%ld", frameCanId);
104 lua_settop(ls, 0);
105 return;
106 }
107
108 auto dlc = data->Frame.DLC;
109
110 // Push bus, ID and DLC
111 lua_pushinteger(ls, HUMAN_OFFSET + data->BusIndex);
112 lua_pushinteger(ls, frameCanId);
113 lua_pushinteger(ls, dlc);
114
116 // todo: https://github.com/rusefi/rusefi/issues/6041
117 if (lua_getglobal(ls, "global_can_data") != LUA_TTABLE) {
118 criticalError("luaCanRxWorkaround without global_can_data");
119 }
120 } else {
121 // Build table for data, custom implementation without explicit GC but still garbage
122 lua_createtable_noGC(ls, dlc);
123 }
124 for (size_t i = 0; i < dlc; i++) {
125 lua_pushinteger(ls, data->Frame.data8[i]);
126
127 // index is i+1 because Lua "arrays" (tables) are 1-indexed
128 lua_rawseti(ls, -2, i + 1);
129 }
130
131 // Perform the actual function call
132 int status = lua_pcall(ls, 4, 0, 0);
133
134 if (0 != status) {
135 // error calling CAN rx hook function
136 auto errMsg = lua_tostring(ls, -1);
137 efiPrintf("LUA CAN RX error %s", errMsg);
138 lua_pop(ls, 1);
139 }
140
141 lua_settop(ls, 0);
142}
143
144static bool doOneLuaCanRx(LuaHandle& ls) {
146 CanFrameData* data;
147
148 msg_t msg = filledBuffers.fetch(&data, TIME_IMMEDIATE);
149
150 if (msg == MSG_TIMEOUT) {
151 // No new CAN messages rx'd, nothing more to do.
152 return false;
153 }
154
155 if (msg != MSG_OK) {
156 // Message was otherwise not OK
157 // TODO: what do here?
158 return false;
159 }
160
161 // We've accepted the frame, process it in Lua.
162 handleCanFrame(ls, data);
163
164 // We're done, return this frame to the free list
165 msg = freeBuffers.post(data, TIME_IMMEDIATE);
166 efiAssert(ObdCode::OBD_PCM_Processor_Fault, msg == MSG_OK, "lua can post to free buffer fail", false);
167
168 // We processed a frame so we should check again
169 return true;
170}
171
174 int counter = 0;
175 // While it processed a frame, continue checking
176 while (doOneLuaCanRx(ls)) {
177 counter++;
178 }
179 return counter;
180}
181
183 // Push all CAN frames in to the free buffer
184 for (size_t i = 0; i < LUA_canFrameCount; i++) {
185 freeBuffers.post(&canFrames[i], TIME_INFINITE);
186 }
187}
188
190 return dropRxCount;
191}
192
193#endif // EFI_CAN_SUPPORT
CanFilter * getFilterForId(size_t busIndex, int Id)
static constexpr engine_configuration_s * engineConfiguration
int doLuaCanRx(LuaHandle &ls)
void processLuaCan(const size_t busIndex, const CANRxFrame &frame)
size_t getLuaCanRxDropped()
static size_t dropRxCount
static CanFrameData canFrames[LUA_canFrameCount]
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > freeBuffers
static void handleCanFrame(LuaHandle &ls, CanFrameData *data)
void initLuaCanRx()
static void lua_createtable_noGC(lua_State *L, int narray)
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > filledBuffers
static bool doOneLuaCanRx(LuaHandle &ls)
@ OBD_PCM_Processor_Fault
@ LuaOneCanRxCallback
@ LuaAllCanRxFunction
@ LuaOneCanRxFunction