mirror of
https://github.com/nodemcu/nodemcu-firmware.git
synced 2025-01-30 21:12:55 +08:00
Update modules to be Lua coroutine compatible (#1423)
This commit is contained in:
parent
ff46abe61f
commit
a499716cc1
@ -2,19 +2,14 @@
|
|||||||
#include "../lua/lauxlib.h"
|
#include "../lua/lauxlib.h"
|
||||||
#include <c_stdlib.h>
|
#include <c_stdlib.h>
|
||||||
|
|
||||||
static lua_State *gL;
|
|
||||||
static const char errfmt[] = "cjson %salloc: out of mem (%d bytes)";
|
static const char errfmt[] = "cjson %salloc: out of mem (%d bytes)";
|
||||||
|
|
||||||
void cjson_mem_setlua (lua_State *L)
|
|
||||||
{
|
|
||||||
gL = L;
|
|
||||||
}
|
|
||||||
|
|
||||||
void *cjson_mem_malloc (uint32_t sz)
|
void *cjson_mem_malloc (uint32_t sz)
|
||||||
{
|
{
|
||||||
void *p = (void*)c_malloc (sz);
|
void *p = (void*)c_malloc (sz);
|
||||||
if (!p && gL)
|
lua_State *L = lua_getstate();
|
||||||
luaL_error (gL, errfmt, "m", sz);
|
if (!p)
|
||||||
|
luaL_error (L, errfmt, "m", sz);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -22,7 +17,8 @@ void *cjson_mem_malloc (uint32_t sz)
|
|||||||
void *cjson_mem_realloc (void *o, uint32_t sz)
|
void *cjson_mem_realloc (void *o, uint32_t sz)
|
||||||
{
|
{
|
||||||
void *p = (void*)c_realloc (o, sz);
|
void *p = (void*)c_realloc (o, sz);
|
||||||
if (!p && gL)
|
lua_State *L = lua_getstate();
|
||||||
luaL_error (gL, errfmt, "re", sz);
|
if (!p)
|
||||||
|
luaL_error (L, errfmt, "re", sz);
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
|
|
||||||
#include "../lua/lua.h"
|
#include "../lua/lua.h"
|
||||||
|
|
||||||
void cjson_mem_setlua (lua_State *L);
|
|
||||||
|
|
||||||
void *cjson_mem_malloc (uint32_t sz);
|
void *cjson_mem_malloc (uint32_t sz);
|
||||||
void *cjson_mem_realloc (void *p, uint32_t sz);
|
void *cjson_mem_realloc (void *p, uint32_t sz);
|
||||||
|
|
||||||
|
@ -155,7 +155,6 @@ typedef struct
|
|||||||
typedef struct coap_luser_entry coap_luser_entry;
|
typedef struct coap_luser_entry coap_luser_entry;
|
||||||
|
|
||||||
struct coap_luser_entry{
|
struct coap_luser_entry{
|
||||||
lua_State *L;
|
|
||||||
// int ref;
|
// int ref;
|
||||||
// char name[MAX_SEGMENTS_SIZE+1]; // +1 for string '\0'
|
// char name[MAX_SEGMENTS_SIZE+1]; // +1 for string '\0'
|
||||||
const char *name;
|
const char *name;
|
||||||
|
@ -37,6 +37,7 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
|
|||||||
const coap_option_t *opt;
|
const coap_option_t *opt;
|
||||||
uint8_t count;
|
uint8_t count;
|
||||||
int n;
|
int n;
|
||||||
|
lua_State *L = lua_getstate();
|
||||||
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
|
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
|
||||||
{
|
{
|
||||||
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
|
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
|
||||||
@ -58,19 +59,17 @@ static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scra
|
|||||||
NODE_DBG("/v1/v/");
|
NODE_DBG("/v1/v/");
|
||||||
NODE_DBG((char *)h->name);
|
NODE_DBG((char *)h->name);
|
||||||
NODE_DBG(" match.\n");
|
NODE_DBG(" match.\n");
|
||||||
if(h->L == NULL)
|
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
|
||||||
if(c_strlen(h->name))
|
if(c_strlen(h->name))
|
||||||
{
|
{
|
||||||
n = lua_gettop(h->L);
|
n = lua_gettop(L);
|
||||||
lua_getglobal(h->L, h->name);
|
lua_getglobal(L, h->name);
|
||||||
if (!lua_isnumber(h->L, -1) && !lua_isstring(h->L, -1)) {
|
if (!lua_isnumber(L, -1) && !lua_isstring(L, -1)) {
|
||||||
NODE_DBG ("should be a number or string.\n");
|
NODE_DBG ("should be a number or string.\n");
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
||||||
} else {
|
} else {
|
||||||
const char *res = lua_tostring(h->L,-1);
|
const char *res = lua_tostring(L,-1);
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, h->content_type);
|
return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, h->content_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,6 +93,7 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
|
|||||||
const coap_option_t *opt;
|
const coap_option_t *opt;
|
||||||
uint8_t count;
|
uint8_t count;
|
||||||
int n;
|
int n;
|
||||||
|
lua_State *L = lua_getstate();
|
||||||
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
|
if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
|
||||||
{
|
{
|
||||||
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
|
if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
|
||||||
@ -116,37 +116,34 @@ static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scr
|
|||||||
NODE_DBG((char *)h->name);
|
NODE_DBG((char *)h->name);
|
||||||
NODE_DBG(" match.\n");
|
NODE_DBG(" match.\n");
|
||||||
|
|
||||||
if(h->L == NULL)
|
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
|
||||||
|
|
||||||
if(c_strlen(h->name))
|
if(c_strlen(h->name))
|
||||||
{
|
{
|
||||||
n = lua_gettop(h->L);
|
n = lua_gettop(L);
|
||||||
lua_getglobal(h->L, h->name);
|
lua_getglobal(L, h->name);
|
||||||
if (lua_type(h->L, -1) != LUA_TFUNCTION) {
|
if (lua_type(L, -1) != LUA_TFUNCTION) {
|
||||||
NODE_DBG ("should be a function\n");
|
NODE_DBG ("should be a function\n");
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
||||||
} else {
|
} else {
|
||||||
lua_pushlstring(h->L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
|
lua_pushlstring(L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
|
||||||
lua_call(h->L, 1, 1);
|
lua_call(L, 1, 1);
|
||||||
if (!lua_isnil(h->L, -1)){ /* get return? */
|
if (!lua_isnil(L, -1)){ /* get return? */
|
||||||
if( lua_isstring(h->L, -1) ) // deal with the return string
|
if( lua_isstring(L, -1) ) // deal with the return string
|
||||||
{
|
{
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
const char *ret = luaL_checklstring( h->L, -1, &len );
|
const char *ret = luaL_checklstring( L, -1, &len );
|
||||||
if(len > MAX_PAYLOAD_SIZE){
|
if(len > MAX_PAYLOAD_SIZE){
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
luaL_error( h->L, "return string:<MAX_PAYLOAD_SIZE" );
|
luaL_error( L, "return string:<MAX_PAYLOAD_SIZE" );
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
|
||||||
}
|
}
|
||||||
NODE_DBG((char *)ret);
|
NODE_DBG((char *)ret);
|
||||||
NODE_DBG("\n");
|
NODE_DBG("\n");
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lua_settop(h->L, n);
|
lua_settop(L, n);
|
||||||
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,10 +195,10 @@ static int handle_get_id(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, c
|
|||||||
return coap_make_response(scratch, outpkt, (const uint8_t *)(&id), sizeof(uint32_t), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
return coap_make_response(scratch, outpkt, (const uint8_t *)(&id), sizeof(uint32_t), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
coap_luser_entry var_head = {NULL,NULL,NULL,0};
|
coap_luser_entry var_head = {NULL,NULL,0};
|
||||||
coap_luser_entry *variable_entry = &var_head;
|
coap_luser_entry *variable_entry = &var_head;
|
||||||
|
|
||||||
coap_luser_entry func_head = {NULL,NULL,NULL,0};
|
coap_luser_entry func_head = {NULL,NULL,0};
|
||||||
coap_luser_entry *function_entry = &func_head;
|
coap_luser_entry *function_entry = &func_head;
|
||||||
|
|
||||||
const coap_endpoint_t endpoints[] =
|
const coap_endpoint_t endpoints[] =
|
||||||
|
@ -304,7 +304,7 @@ static int bme280_lua_init(lua_State* L) {
|
|||||||
static void bme280_readoutdone (void *arg)
|
static void bme280_readoutdone (void *arg)
|
||||||
{
|
{
|
||||||
NODE_DBG("timer out\n");
|
NODE_DBG("timer out\n");
|
||||||
lua_State *L = arg;
|
lua_State *L = lua_getstate();
|
||||||
lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
|
lua_rawgeti (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
|
||||||
lua_call (L, 0, 0);
|
lua_call (L, 0, 0);
|
||||||
luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
|
luaL_unref (L, LUA_REGISTRYINDEX, lua_connected_readout_ref);
|
||||||
|
@ -1617,8 +1617,6 @@ static const LUA_REG_TYPE cjson_map[] = {
|
|||||||
|
|
||||||
int luaopen_cjson( lua_State *L )
|
int luaopen_cjson( lua_State *L )
|
||||||
{
|
{
|
||||||
cjson_mem_setlua (L);
|
|
||||||
|
|
||||||
/* Initialise number conversions */
|
/* Initialise number conversions */
|
||||||
// fpconv_init(); // not needed for a specific cpu.
|
// fpconv_init(); // not needed for a specific cpu.
|
||||||
if(-1==cfg_init(&_cfg)){
|
if(-1==cfg_init(&_cfg)){
|
||||||
|
@ -24,7 +24,6 @@ coap_queue_t *gQueue = NULL;
|
|||||||
|
|
||||||
typedef struct lcoap_userdata
|
typedef struct lcoap_userdata
|
||||||
{
|
{
|
||||||
lua_State *L;
|
|
||||||
struct espconn *pesp_conn;
|
struct espconn *pesp_conn;
|
||||||
int self_ref;
|
int self_ref;
|
||||||
}lcoap_userdata;
|
}lcoap_userdata;
|
||||||
@ -103,7 +102,6 @@ static int coap_create( lua_State* L, const char* mt )
|
|||||||
pesp_conn->state = ESPCONN_NONE;
|
pesp_conn->state = ESPCONN_NONE;
|
||||||
NODE_DBG("UDP server/client is set.\n");
|
NODE_DBG("UDP server/client is set.\n");
|
||||||
|
|
||||||
cud->L = L;
|
|
||||||
pesp_conn->reverse = cud;
|
pesp_conn->reverse = cud;
|
||||||
|
|
||||||
NODE_DBG("coap_create is called.\n");
|
NODE_DBG("coap_create is called.\n");
|
||||||
@ -129,7 +127,6 @@ static int coap_delete( lua_State* L, const char* mt )
|
|||||||
cud->self_ref = LUA_NOREF;
|
cud->self_ref = LUA_NOREF;
|
||||||
}
|
}
|
||||||
|
|
||||||
cud->L = NULL;
|
|
||||||
if(cud->pesp_conn)
|
if(cud->pesp_conn)
|
||||||
{
|
{
|
||||||
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
|
if(cud->pesp_conn->proto.udp->remote_port || cud->pesp_conn->proto.udp->local_port)
|
||||||
@ -462,10 +459,8 @@ static int coap_regist( lua_State* L, const char* mt, int isvar )
|
|||||||
return luaL_error(L, "not enough memory");
|
return luaL_error(L, "not enough memory");
|
||||||
h->next = NULL;
|
h->next = NULL;
|
||||||
h->name = NULL;
|
h->name = NULL;
|
||||||
h->L = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
h->L = L;
|
|
||||||
h->name = name;
|
h->name = name;
|
||||||
h->content_type = content_type;
|
h->content_type = content_type;
|
||||||
|
|
||||||
|
@ -34,7 +34,6 @@ static int expose_array(lua_State* L, char *array, unsigned short len);
|
|||||||
#define MAX_SOCKET 5
|
#define MAX_SOCKET 5
|
||||||
static int socket_num = 0;
|
static int socket_num = 0;
|
||||||
static int socket[MAX_SOCKET];
|
static int socket[MAX_SOCKET];
|
||||||
static lua_State *gL = NULL;
|
|
||||||
static int tcpserver_cb_connect_ref = LUA_NOREF; // for tcp server connected callback
|
static int tcpserver_cb_connect_ref = LUA_NOREF; // for tcp server connected callback
|
||||||
static uint16_t tcp_server_timeover = 30;
|
static uint16_t tcp_server_timeover = 30;
|
||||||
|
|
||||||
@ -65,8 +64,7 @@ static void net_server_disconnected(void *arg) // for tcp server only
|
|||||||
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
||||||
if(nud == NULL)
|
if(nud == NULL)
|
||||||
return;
|
return;
|
||||||
if(gL == NULL)
|
lua_State *L = lua_getstate();
|
||||||
return;
|
|
||||||
#if 0
|
#if 0
|
||||||
char temp[20] = {0};
|
char temp[20] = {0};
|
||||||
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
|
c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
|
||||||
@ -78,25 +76,25 @@ static void net_server_disconnected(void *arg) // for tcp server only
|
|||||||
#endif
|
#endif
|
||||||
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
|
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
|
||||||
{
|
{
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
||||||
lua_call(gL, 1, 0);
|
lua_call(L, 1, 0);
|
||||||
}
|
}
|
||||||
int i;
|
int i;
|
||||||
lua_gc(gL, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
for(i=0;i<MAX_SOCKET;i++){
|
for(i=0;i<MAX_SOCKET;i++){
|
||||||
if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){
|
if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){
|
||||||
// found the saved client
|
// found the saved client
|
||||||
nud->pesp_conn->reverse = NULL;
|
nud->pesp_conn->reverse = NULL;
|
||||||
nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it.
|
nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it.
|
||||||
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
||||||
luaL_unref(gL, LUA_REGISTRYINDEX, socket[i]);
|
luaL_unref(L, LUA_REGISTRYINDEX, socket[i]);
|
||||||
socket[i] = LUA_NOREF;
|
socket[i] = LUA_NOREF;
|
||||||
socket_num--;
|
socket_num--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void net_socket_disconnected(void *arg) // tcp only
|
static void net_socket_disconnected(void *arg) // tcp only
|
||||||
@ -108,11 +106,12 @@ static void net_socket_disconnected(void *arg) // tcp only
|
|||||||
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
||||||
if(nud == NULL)
|
if(nud == NULL)
|
||||||
return;
|
return;
|
||||||
|
lua_State *L = lua_getstate();
|
||||||
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
|
if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
|
||||||
{
|
{
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
||||||
lua_call(gL, 1, 0);
|
lua_call(L, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(pesp_conn->proto.tcp)
|
if(pesp_conn->proto.tcp)
|
||||||
@ -121,12 +120,12 @@ static void net_socket_disconnected(void *arg) // tcp only
|
|||||||
if(nud->pesp_conn)
|
if(nud->pesp_conn)
|
||||||
c_free(nud->pesp_conn);
|
c_free(nud->pesp_conn);
|
||||||
nud->pesp_conn = NULL; // espconn is already disconnected
|
nud->pesp_conn = NULL; // espconn is already disconnected
|
||||||
lua_gc(gL, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
if(nud->self_ref != LUA_NOREF){
|
if(nud->self_ref != LUA_NOREF){
|
||||||
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
|
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
|
||||||
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void net_server_reconnected(void *arg, sint8_t err)
|
static void net_server_reconnected(void *arg, sint8_t err)
|
||||||
@ -154,15 +153,16 @@ static void net_socket_received(void *arg, char *pdata, unsigned short len)
|
|||||||
return;
|
return;
|
||||||
if(nud->self_ref == LUA_NOREF)
|
if(nud->self_ref == LUA_NOREF)
|
||||||
return;
|
return;
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_receive_ref);
|
lua_State *L = lua_getstate();
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
|
||||||
// expose_array(gL, pdata, len);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
|
||||||
|
// expose_array(L, pdata, len);
|
||||||
// *(pdata+len) = 0;
|
// *(pdata+len) = 0;
|
||||||
// NODE_DBG(pdata);
|
// NODE_DBG(pdata);
|
||||||
// NODE_DBG("\n");
|
// NODE_DBG("\n");
|
||||||
lua_pushlstring(gL, pdata, len);
|
lua_pushlstring(L, pdata, len);
|
||||||
// lua_pushinteger(gL, len);
|
// lua_pushinteger(L, len);
|
||||||
lua_call(gL, 2, 0);
|
lua_call(L, 2, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void net_socket_sent(void *arg)
|
static void net_socket_sent(void *arg)
|
||||||
@ -178,9 +178,10 @@ static void net_socket_sent(void *arg)
|
|||||||
return;
|
return;
|
||||||
if(nud->self_ref == LUA_NOREF)
|
if(nud->self_ref == LUA_NOREF)
|
||||||
return;
|
return;
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_send_ref);
|
lua_State *L = lua_getstate();
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
|
||||||
lua_call(gL, 1, 0);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
|
||||||
|
lua_call(L, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
||||||
@ -205,34 +206,16 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
|||||||
NODE_DBG("self_ref null.\n");
|
NODE_DBG("self_ref null.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* original
|
|
||||||
if(ipaddr == NULL)
|
|
||||||
{
|
|
||||||
NODE_ERR( "DNS Fail!\n" );
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
// ipaddr->addr is a uint32_t ip
|
|
||||||
char ip_str[20];
|
|
||||||
c_memset(ip_str, 0, sizeof(ip_str));
|
|
||||||
if(ipaddr->addr != 0)
|
|
||||||
{
|
|
||||||
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
|
lua_State *L = lua_getstate();
|
||||||
//lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
|
|
||||||
lua_pushstring(gL, ip_str); // the ip para
|
|
||||||
*/
|
|
||||||
|
|
||||||
// "enhanced"
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
|
||||||
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
|
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
|
|
||||||
|
|
||||||
if(ipaddr == NULL)
|
if(ipaddr == NULL)
|
||||||
{
|
{
|
||||||
NODE_DBG( "DNS Fail!\n" );
|
NODE_DBG( "DNS Fail!\n" );
|
||||||
lua_pushnil(gL);
|
lua_pushnil(L);
|
||||||
}else{
|
}else{
|
||||||
// ipaddr->addr is a uint32_t ip
|
// ipaddr->addr is a uint32_t ip
|
||||||
char ip_str[20];
|
char ip_str[20];
|
||||||
@ -241,21 +224,21 @@ static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
|||||||
{
|
{
|
||||||
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
|
c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
|
||||||
}
|
}
|
||||||
lua_pushstring(gL, ip_str); // the ip para
|
lua_pushstring(L, ip_str); // the ip para
|
||||||
}
|
}
|
||||||
// "enhanced" end
|
// "enhanced" end
|
||||||
|
|
||||||
lua_call(gL, 2, 0);
|
lua_call(L, 2, 0);
|
||||||
|
|
||||||
end:
|
end:
|
||||||
if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0)
|
if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0)
|
||||||
|| (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){
|
|| (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){
|
||||||
lua_gc(gL, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
if(nud->self_ref != LUA_NOREF){
|
if(nud->self_ref != LUA_NOREF){
|
||||||
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
|
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
|
||||||
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -295,25 +278,24 @@ static void net_server_connected(void *arg) // for tcp only
|
|||||||
|
|
||||||
if(tcpserver_cb_connect_ref == LUA_NOREF)
|
if(tcpserver_cb_connect_ref == LUA_NOREF)
|
||||||
return;
|
return;
|
||||||
if(!gL)
|
lua_State *L = lua_getstate();
|
||||||
return;
|
|
||||||
|
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
|
lua_rawgeti(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
|
||||||
// create a new client object
|
// create a new client object
|
||||||
skt = (lnet_userdata *)lua_newuserdata(gL, sizeof(lnet_userdata));
|
skt = (lnet_userdata *)lua_newuserdata(L, sizeof(lnet_userdata));
|
||||||
|
|
||||||
if(!skt){
|
if(!skt){
|
||||||
NODE_ERR("can't newudata\n");
|
NODE_ERR("can't newudata\n");
|
||||||
lua_pop(gL, 1);
|
lua_pop(L, 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// set its metatable
|
// set its metatable
|
||||||
luaL_getmetatable(gL, "net.socket");
|
luaL_getmetatable(L, "net.socket");
|
||||||
lua_setmetatable(gL, -2);
|
lua_setmetatable(L, -2);
|
||||||
// pre-initialize it, in case of errors
|
// pre-initialize it, in case of errors
|
||||||
skt->self_ref = LUA_NOREF;
|
skt->self_ref = LUA_NOREF;
|
||||||
lua_pushvalue(gL, -1); // copy the top of stack
|
lua_pushvalue(L, -1); // copy the top of stack
|
||||||
skt->self_ref = luaL_ref(gL, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
|
skt->self_ref = luaL_ref(L, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
|
||||||
socket[i] = skt->self_ref; // save to socket array
|
socket[i] = skt->self_ref; // save to socket array
|
||||||
socket_num++;
|
socket_num++;
|
||||||
skt->cb_connect_ref = LUA_NOREF; // this socket already connected
|
skt->cb_connect_ref = LUA_NOREF; // this socket already connected
|
||||||
@ -337,7 +319,7 @@ static void net_server_connected(void *arg) // for tcp only
|
|||||||
espconn_regist_reconcb(pesp_conn, net_server_reconnected);
|
espconn_regist_reconcb(pesp_conn, net_server_reconnected);
|
||||||
|
|
||||||
// now socket[i] has the client ref, and stack top has the userdata
|
// now socket[i] has the client ref, and stack top has the userdata
|
||||||
lua_call(gL, 1, 0); // function(conn)
|
lua_call(L, 1, 0); // function(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
static void net_socket_connected(void *arg)
|
static void net_socket_connected(void *arg)
|
||||||
@ -358,9 +340,10 @@ static void net_socket_connected(void *arg)
|
|||||||
return;
|
return;
|
||||||
if(nud->self_ref == LUA_NOREF)
|
if(nud->self_ref == LUA_NOREF)
|
||||||
return;
|
return;
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_connect_ref);
|
lua_State *L = lua_getstate();
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
|
||||||
lua_call(gL, 1, 0);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
|
||||||
|
lua_call(L, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lua: s = net.create(type, secure/timeout, function(conn))
|
// Lua: s = net.create(type, secure/timeout, function(conn))
|
||||||
@ -492,8 +475,6 @@ static int net_create( lua_State* L, const char* mt )
|
|||||||
pUdpServer = pesp_conn;
|
pUdpServer = pesp_conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
gL = L; // global L for net module.
|
|
||||||
|
|
||||||
// if call back function is specified, call it with para userdata
|
// if call back function is specified, call it with para userdata
|
||||||
// luaL_checkanyfunction(L, 2);
|
// luaL_checkanyfunction(L, 2);
|
||||||
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
|
if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
|
||||||
@ -575,12 +556,12 @@ static int net_delete( lua_State* L, const char* mt )
|
|||||||
luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
|
luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
|
||||||
nud->cb_dns_found_ref = LUA_NOREF;
|
nud->cb_dns_found_ref = LUA_NOREF;
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
if(LUA_NOREF!=nud->self_ref){
|
if(LUA_NOREF!=nud->self_ref){
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
|
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
|
||||||
nud->self_ref = LUA_NOREF;
|
nud->self_ref = LUA_NOREF;
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -625,19 +606,18 @@ static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
|||||||
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
|
||||||
if(nud == NULL)
|
if(nud == NULL)
|
||||||
return;
|
return;
|
||||||
if(gL == NULL)
|
lua_State *L = lua_getstate();
|
||||||
return;
|
|
||||||
if(ipaddr == NULL)
|
if(ipaddr == NULL)
|
||||||
{
|
{
|
||||||
dns_reconn_count++;
|
dns_reconn_count++;
|
||||||
if( dns_reconn_count >= 5 ){
|
if( dns_reconn_count >= 5 ){
|
||||||
NODE_ERR( "DNS Fail!\n" );
|
NODE_ERR( "DNS Fail!\n" );
|
||||||
lua_gc(gL, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
if(nud->self_ref != LUA_NOREF){
|
if(nud->self_ref != LUA_NOREF){
|
||||||
luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
|
luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
|
||||||
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
|
||||||
}
|
}
|
||||||
lua_gc(gL, LUA_GCRESTART, 0);
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
|
NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
|
||||||
@ -1175,8 +1155,8 @@ static int net_dns_static( lua_State* L )
|
|||||||
lua_pushinteger(L, UDP); // we are going to create a new dummy UDP socket
|
lua_pushinteger(L, UDP); // we are going to create a new dummy UDP socket
|
||||||
lua_call(L,1,1);// after this the stack should have a socket
|
lua_call(L,1,1);// after this the stack should have a socket
|
||||||
|
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, rdom); // load domain back to the stack
|
lua_rawgeti(L, LUA_REGISTRYINDEX, rdom); // load domain back to the stack
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack
|
lua_rawgeti(L, LUA_REGISTRYINDEX, rfunc); // load the callback function back to the stack
|
||||||
|
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, rdom); //free reference
|
luaL_unref(L, LUA_REGISTRYINDEX, rdom); //free reference
|
||||||
luaL_unref(L, LUA_REGISTRYINDEX, rfunc); //free reference
|
luaL_unref(L, LUA_REGISTRYINDEX, rfunc); //free reference
|
||||||
|
@ -333,7 +333,7 @@ void output_redirect(const char *str) {
|
|||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (output_redir_ref == LUA_NOREF || !L) {
|
if (output_redir_ref == LUA_NOREF) {
|
||||||
uart0_sendStr(str);
|
uart0_sendStr(str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -204,7 +204,7 @@ static void on_recv (void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_a
|
|||||||
(void)port;
|
(void)port;
|
||||||
sntp_dbg("sntp: on_recv\n");
|
sntp_dbg("sntp: on_recv\n");
|
||||||
|
|
||||||
lua_State *L = arg;
|
lua_State *L = lua_getstate();
|
||||||
|
|
||||||
if (!state || state->pcb != pcb)
|
if (!state || state->pcb != pcb)
|
||||||
{
|
{
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include "c_string.h"
|
#include "c_string.h"
|
||||||
#include "rom.h"
|
#include "rom.h"
|
||||||
|
|
||||||
static lua_State *gL = NULL;
|
|
||||||
static int uart_receive_rf = LUA_NOREF;
|
static int uart_receive_rf = LUA_NOREF;
|
||||||
bool run_input = true;
|
bool run_input = true;
|
||||||
bool uart_on_data_cb(const char *buf, size_t len){
|
bool uart_on_data_cb(const char *buf, size_t len){
|
||||||
@ -16,11 +15,12 @@ bool uart_on_data_cb(const char *buf, size_t len){
|
|||||||
return false;
|
return false;
|
||||||
if(uart_receive_rf == LUA_NOREF)
|
if(uart_receive_rf == LUA_NOREF)
|
||||||
return false;
|
return false;
|
||||||
if(!gL)
|
lua_State *L = lua_getstate();
|
||||||
|
if(!L)
|
||||||
return false;
|
return false;
|
||||||
lua_rawgeti(gL, LUA_REGISTRYINDEX, uart_receive_rf);
|
lua_rawgeti(L, LUA_REGISTRYINDEX, uart_receive_rf);
|
||||||
lua_pushlstring(gL, buf, len);
|
lua_pushlstring(L, buf, len);
|
||||||
lua_call(gL, 1, 0);
|
lua_call(L, 1, 0);
|
||||||
return !run_input;
|
return !run_input;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,7 +75,6 @@ static int uart_on( lua_State* L )
|
|||||||
}
|
}
|
||||||
if(!lua_isnil(L, -1)){
|
if(!lua_isnil(L, -1)){
|
||||||
uart_receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
|
uart_receive_rf = luaL_ref(L, LUA_REGISTRYINDEX);
|
||||||
gL = L;
|
|
||||||
if(run==0)
|
if(run==0)
|
||||||
run_input = false;
|
run_input = false;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user