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 170 of file lua_can_rx.cpp.

170 {
172 int counter = 0;
173 // While it processed a frame, continue checking
174 while (doOneLuaCanRx(ls)) {
175 counter++;
176 }
177 return counter;
178}
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 142 of file lua_can_rx.cpp.

142 {
144 CanFrameData* data;
145
146 msg_t msg = filledBuffers.fetch(&data, TIME_IMMEDIATE);
147
148 if (msg == MSG_TIMEOUT) {
149 // No new CAN messages rx'd, nothing more to do.
150 return false;
151 }
152
153 if (msg != MSG_OK) {
154 // Message was otherwise not OK
155 // TODO: what do here?
156 return false;
157 }
158
159 // We've accepted the frame, process it in Lua.
160 handleCanFrame(ls, data);
161
162 // We're done, return this frame to the free list
163 msg = freeBuffers.post(data, TIME_IMMEDIATE);
164 efiAssert(ObdCode::OBD_PCM_Processor_Fault, msg == MSG_OK, "lua can post to free buffer fail", false);
165
166 // We processed a frame so we should check again
167 return true;
168}
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 187 of file lua_can_rx.cpp.

187 {
188 return dropRxCount;
189}
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 lua_getglobal(ls, "global_can_data");
118 } else {
119 // Build table for data, custom implementation without explicit GC but still garbage
120 lua_createtable_noGC(ls, dlc);
121 }
122 for (size_t i = 0; i < dlc; i++) {
123 lua_pushinteger(ls, data->Frame.data8[i]);
124
125 // index is i+1 because Lua "arrays" (tables) are 1-indexed
126 lua_rawseti(ls, -2, i + 1);
127 }
128
129 // Perform the actual function call
130 int status = lua_pcall(ls, 4, 0, 0);
131
132 if (0 != status) {
133 // error calling CAN rx hook function
134 auto errMsg = lua_tostring(ls, -1);
135 efiPrintf("LUA CAN RX error %s", errMsg);
136 lua_pop(ls, 1);
137 }
138
139 lua_settop(ls, 0);
140}
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 180 of file lua_can_rx.cpp.

180 {
181 // Push all CAN frames in to the free buffer
182 for (size_t i = 0; i < LUA_canFrameCount; i++) {
183 freeBuffers.post(&canFrames[i], TIME_INFINITE);
184 }
185}
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.