Skip to content

Commit

Permalink
feat: support load message from lua when create broadcast packet
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Nov 15, 2024
1 parent 12b2368 commit fedb73d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
14 changes: 9 additions & 5 deletions example.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
local M = {}

--params: string, response from broadcaster
--return: string or none
M.on_message = function (params)
-- params: string, response from broadcaster
-- return: string or none
M.on_message = function(params)
-- do something with params
return 'response from broadcaster:'..params
return 'response from broadcaster:' .. params
end

-- create a message to send to broadcaster
M.load_message = function()
return '{"type": "discovery", "params": {"mac": "7C:27:3C:08:57:94"}}'
end

return M
return M
46 changes: 43 additions & 3 deletions finder.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,48 @@ static void udp_cb(struct mg_connection *c, int ev, void *ev_data, void *fn_data
}
}

static void load_message(void *handle, cJSON *payload) {
struct finder_private *priv = (struct finder_private *)handle;
const char *ret = NULL;
lua_State *L = luaL_newstate();
luaL_openlibs(L);

if ( luaL_dofile(L, priv->cfg.opts->callback_lua) ) {
MG_ERROR(("lua dofile failed"));
goto done;
}

lua_getfield(L, -1, "load_message");
if (!lua_isfunction(L, -1)) {
MG_ERROR(("method load_message is not a function"));
goto done;
}

if (lua_pcall(L, 0, 1, 0)) {//0 param, one return values, zero error func
MG_ERROR(("callback failed"));
goto done;
}

ret = lua_tostring(L, -1);
if (!ret) {
MG_ERROR(("lua call no ret"));
goto done;
}

MG_INFO(("ret: %s", ret));

cJSON_SetValuestring(payload, ret);

done:
if (L)
lua_close(L);
}

static void do_broadcast(void *arg, void *address) {
struct mg_mgr *mgr = (struct mg_mgr *)arg;
struct finder_private *priv = (struct finder_private *)mgr->userdata;
struct mg_connection *c;
cJSON *root = NULL;
cJSON *root = NULL, *payload = NULL;
char *printed = NULL;
int flag = 1;

Expand All @@ -162,14 +199,17 @@ static void do_broadcast(void *arg, void *address) {

root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "service", priv->cfg.opts->service);
cJSON_AddStringToObject(root, "payload", priv->cfg.opts->payload);

payload = cJSON_AddStringToObject(root, "payload", priv->cfg.opts->payload); //default payload
load_message(priv, payload); //maybe change payload by lua script

cJSON_AddStringToObject(root, "address", address);
cJSON_AddNumberToObject(root, "nonce", nonce);

//add sign, sha1(service + payload + address + nonce + key)
unsigned char digest[20] = {0};
char sign_str[41] = {0};
char *data = mg_mprintf("%s%s%s%d%s", priv->cfg.opts->service, priv->cfg.opts->payload, address, nonce, priv->cfg.opts->key);
char *data = mg_mprintf("%s%s%s%d%s", priv->cfg.opts->service, cJSON_GetStringValue(payload), address, nonce, priv->cfg.opts->key);
//MG_DEBUG(("data: %s", data));
mg_sha1_ctx ctx;
mg_sha1_init(&ctx);
Expand Down

0 comments on commit fedb73d

Please sign in to comment.