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