rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
lua_can_rx.cpp File Reference

Functions

void processLuaCan (const size_t busIndex, const CANRxFrame &frame)
 
static void lua_createtable_noGC (lua_State *L, int narray)
 
static void handleCanFrame (LuaHandle &ls, CanFrameData *data)
 
static bool doOneLuaCanRx (LuaHandle &ls)
 
int doLuaCanRx (LuaHandle &ls)
 
void initLuaCanRx ()
 
size_t getLuaCanRxDropped ()
 

Variables

static CanFrameData canFrames [LUA_canFrameCount]
 
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > freeBuffers
 
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > filledBuffers
 
static size_t dropRxCount = 0
 

Function Documentation

◆ doLuaCanRx()

int doLuaCanRx ( LuaHandle ls)

Definition at line 172 of file lua_can_rx.cpp.

172 {
174 int counter = 0;
175 // While it processed a frame, continue checking
176 while (doOneLuaCanRx(ls)) {
177 counter++;
178 }
179 return counter;
180}
static bool doOneLuaCanRx(LuaHandle &ls)
@ LuaAllCanRxFunction

Referenced by runOneLua().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ doOneLuaCanRx()

static bool doOneLuaCanRx ( LuaHandle ls)
static

Definition at line 144 of file lua_can_rx.cpp.

144 {
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}
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > freeBuffers
static void handleCanFrame(LuaHandle &ls, CanFrameData *data)
static chibios_rt::Mailbox< CanFrameData *, LUA_canFrameCount > filledBuffers
@ OBD_PCM_Processor_Fault
@ LuaOneCanRxFunction

Referenced by doLuaCanRx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getLuaCanRxDropped()

size_t getLuaCanRxDropped ( )

Definition at line 189 of file lua_can_rx.cpp.

189 {
190 return dropRxCount;
191}
static size_t dropRxCount

Referenced by configureRusefiLuaHooks(), and startLua().

Here is the caller graph for this function:

◆ handleCanFrame()

static void handleCanFrame ( LuaHandle ls,
CanFrameData *  data 
)
static

Definition at line 89 of file lua_can_rx.cpp.

89 {
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}
static constexpr engine_configuration_s * engineConfiguration
static void lua_createtable_noGC(lua_State *L, int narray)
@ LuaOneCanRxCallback

Referenced by doOneLuaCanRx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ initLuaCanRx()

void initLuaCanRx ( )

Definition at line 182 of file lua_can_rx.cpp.

182 {
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}
static CanFrameData canFrames[LUA_canFrameCount]

Referenced by startLua().

Here is the caller graph for this function:

◆ lua_createtable_noGC()

static void lua_createtable_noGC ( lua_State *  L,
int  narray 
)
static

Definition at line 74 of file lua_can_rx.cpp.

74 {
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}

Referenced by handleCanFrame().

Here is the caller graph for this function:

◆ processLuaCan()

void processLuaCan ( const size_t  busIndex,
const CANRxFrame frame 
)

Definition at line 38 of file lua_can_rx.cpp.

38 {
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}
CanFilter * getFilterForId(size_t busIndex, int Id)

Referenced by processCanRxMessage().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ canFrames

CanFrameData canFrames[LUA_canFrameCount]
static

Definition at line 30 of file lua_can_rx.cpp.

Referenced by initLuaCanRx().

◆ dropRxCount

size_t dropRxCount = 0
static

Definition at line 36 of file lua_can_rx.cpp.

Referenced by getLuaCanRxDropped(), and processLuaCan().

◆ filledBuffers

chibios_rt::Mailbox<CanFrameData*, LUA_canFrameCount> filledBuffers
static

Definition at line 34 of file lua_can_rx.cpp.

Referenced by doOneLuaCanRx(), GetToothLoggerBufferImpl(), and processLuaCan().

◆ freeBuffers

chibios_rt::Mailbox<CanFrameData*, LUA_canFrameCount> freeBuffers
static

Go to the source code of this file.