Skip to content

Commit

Permalink
bugfix/LoRa module/callback coredump in certain circumstances
Browse files Browse the repository at this point in the history
  • Loading branch information
jolivepetrus committed Jul 19, 2019
1 parent a7b006a commit 401ad6e
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions components/lua/modules/middleware/lora.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "modules.h"
#include "error.h"
#include "hex.h"
#include "sys.h"

#include <string.h>
#include <stdlib.h>
Expand All @@ -67,16 +68,16 @@ void lora_gw_start();

#include <drivers/uart.h>

static int rx_callback = 0;
static lua_State* rx_callbackL;
static uint8_t is_gateway = 0;
static lua_callback_t *callback;

static void on_received(int port, char *payload) {
if (rx_callback != LUA_NOREF) {
lua_rawgeti(rx_callbackL, LUA_REGISTRYINDEX, rx_callback);
lua_pushinteger(rx_callbackL, port);
lua_pushlstring(rx_callbackL, payload, strlen(payload));
lua_call(rx_callbackL, 2, 0);
if (callback) {
// Push argument for the callback's function
lua_pushinteger(luaS_callback_state(callback), port);
lua_pushlstring(luaS_callback_state(callback), payload,strlen(payload));

luaS_callback_call(callback, 2);
}

free(payload);
Expand Down Expand Up @@ -453,12 +454,11 @@ static int llora_tx(lua_State* L) {
static int llora_rx(lua_State* L) {
if (is_gateway) luaL_exception_extended(L, LORA_ERR_NOT_ALLOWED, "only allowed for nodes");

luaL_checktype(L, 1, LUA_TFUNCTION);
lua_pushvalue(L, 1);

rx_callback = luaL_ref(L, LUA_REGISTRYINDEX);
callback = luaS_callback_create(L, 1);
if (callback == NULL) {
return luaL_exception_extended(L, LORA_ERR_NO_MEM, NULL);
}

rx_callbackL = L;
lora_set_rx_callback(on_received);

return 0;
Expand Down

0 comments on commit 401ad6e

Please sign in to comment.