ソースを参照

add: pm模块的代码

Wendal Chen 5 年 前
コミット
419cf2937e

+ 4 - 1
lua/src/ldebug.c

@@ -624,6 +624,7 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
 }
 
 
+char * btoa(char *dst, uint32_t value, int base);
 /* add src:line information to 'msg' */
 const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
                                         int line) {
@@ -633,7 +634,9 @@ const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
   else {  /* no source available; use "?" instead */
     buff[0] = '?'; buff[1] = '\0';
   }
-  return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
+  char buff2[40];
+  btoa(buff2, line, 10);
+  return luaO_pushfstring(L, "%s:%s: %s", buff, buff2, msg);
 }
 
 

+ 116 - 116
lua/src/loadlib.c

@@ -562,71 +562,71 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) {
 }
 
 
-static int searcher_C (lua_State *L) {
-  const char *name = luaL_checkstring(L, 1);
-  const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
-  if (filename == NULL) return 1;  /* module not found in this path */
-  return checkload(L, (loadfunc(L, filename, name) == 0), filename);
-}
-
-
-static int searcher_Croot (lua_State *L) {
-  const char *filename;
-  const char *name = luaL_checkstring(L, 1);
-  const char *p = strchr(name, '.');
-  int stat;
-  if (p == NULL) return 0;  /* is root */
-  lua_pushlstring(L, name, p - name);
-  filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
-  if (filename == NULL) return 1;  /* root not found */
-  if ((stat = loadfunc(L, filename, name)) != 0) {
-    if (stat != ERRFUNC)
-      return checkload(L, 0, filename);  /* real error */
-    else {  /* open function not found */
-      lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
-      return 1;
-    }
-  }
-  lua_pushstring(L, filename);  /* will be 2nd argument to module */
-  return 2;
-}
-
-
-static int searcher_preload (lua_State *L) {
-  const char *name = luaL_checkstring(L, 1);
-  lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
-  if (lua_getfield(L, -1, name) == LUA_TNIL)  /* not found? */
-    lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
-  return 1;
-}
-
-
-static void findloader (lua_State *L, const char *name) {
-  int i;
-  luaL_Buffer msg;  /* to build error message */
-  luaL_buffinit(L, &msg);
-  /* push 'package.searchers' to index 3 in the stack */
-  if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
-    luaL_error(L, "'package.searchers' must be a table");
-  /*  iterate over available searchers to find a loader */
-  for (i = 1; ; i++) {
-    if (lua_rawgeti(L, 3, i) == LUA_TNIL) {  /* no more searchers? */
-      lua_pop(L, 1);  /* remove nil */
-      luaL_pushresult(&msg);  /* create error message */
-      luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
-    }
-    lua_pushstring(L, name);
-    lua_call(L, 1, 2);  /* call it */
-    if (lua_isfunction(L, -2))  /* did it find a loader? */
-      return;  /* module loader found */
-    else if (lua_isstring(L, -2)) {  /* searcher returned error message? */
-      lua_pop(L, 1);  /* remove extra return */
-      luaL_addvalue(&msg);  /* concatenate error message */
-    }
-    else
-      lua_pop(L, 2);  /* remove both returns */
-  }
-}
+// static int searcher_C (lua_State *L) {
+//   const char *name = luaL_checkstring(L, 1);
+//   const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP);
+//   if (filename == NULL) return 1;  /* module not found in this path */
+//   return checkload(L, (loadfunc(L, filename, name) == 0), filename);
+// }
+
+
+// static int searcher_Croot (lua_State *L) {
+//   const char *filename;
+//   const char *name = luaL_checkstring(L, 1);
+//   const char *p = strchr(name, '.');
+//   int stat;
+//   if (p == NULL) return 0;  /* is root */
+//   lua_pushlstring(L, name, p - name);
+//   filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP);
+//   if (filename == NULL) return 1;  /* root not found */
+//   if ((stat = loadfunc(L, filename, name)) != 0) {
+//     if (stat != ERRFUNC)
+//       return checkload(L, 0, filename);  /* real error */
+//     else {  /* open function not found */
+//       lua_pushfstring(L, "\n\tno module '%s' in file '%s'", name, filename);
+//       return 1;
+//     }
+//   }
+//   lua_pushstring(L, filename);  /* will be 2nd argument to module */
+//   return 2;
+// }
+
+
+// static int searcher_preload (lua_State *L) {
+//   const char *name = luaL_checkstring(L, 1);
+//   lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
+//   if (lua_getfield(L, -1, name) == LUA_TNIL)  /* not found? */
+//     lua_pushfstring(L, "\n\tno field package.preload['%s']", name);
+//   return 1;
+// }
+
+
+// static void findloader (lua_State *L, const char *name) {
+//   int i;
+//   luaL_Buffer msg;  /* to build error message */
+//   luaL_buffinit(L, &msg);
+//   /* push 'package.searchers' to index 3 in the stack */
+//   if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE)
+//     luaL_error(L, "'package.searchers' must be a table");
+//   /*  iterate over available searchers to find a loader */
+//   for (i = 1; ; i++) {
+//     if (lua_rawgeti(L, 3, i) == LUA_TNIL) {  /* no more searchers? */
+//       lua_pop(L, 1);  /* remove nil */
+//       luaL_pushresult(&msg);  /* create error message */
+//       luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1));
+//     }
+//     lua_pushstring(L, name);
+//     lua_call(L, 1, 2);  /* call it */
+//     if (lua_isfunction(L, -2))  /* did it find a loader? */
+//       return;  /* module loader found */
+//     else if (lua_isstring(L, -2)) {  /* searcher returned error message? */
+//       lua_pop(L, 1);  /* remove extra return */
+//       luaL_addvalue(&msg);  /* concatenate error message */
+//     }
+//     else
+//       lua_pop(L, 2);  /* remove both returns */
+//   }
+// }
 
 
 static int ll_require (lua_State *L) {
@@ -750,63 +750,63 @@ static int ll_seeall (lua_State *L) {
 
 
 
-static const luaL_Reg pk_funcs[] = {
-  {"loadlib", ll_loadlib},
-  {"searchpath", ll_searchpath},
-#if defined(LUA_COMPAT_MODULE)
-  {"seeall", ll_seeall},
-#endif
-  /* placeholders */
-  {"preload", NULL},
-  {"cpath", NULL},
-  {"path", NULL},
-  {"searchers", NULL},
-  {"loaded", NULL},
-  {NULL, NULL}
-};
-
-
-static const luaL_Reg ll_funcs[] = {
-#if defined(LUA_COMPAT_MODULE)
-  {"module", ll_module},
-#endif
-  {"require", ll_require},
-  {NULL, NULL}
-};
-
-
-static void createsearcherstable (lua_State *L) {
-  static const lua_CFunction searchers[] =
-    {searcher_Lua, NULL};
-  int i;
-  /* create 'searchers' table */
-  lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
-  /* fill it with predefined searchers */
-  for (i=0; searchers[i] != NULL; i++) {
-    lua_pushvalue(L, -2);  /* set 'package' as upvalue for all searchers */
-    lua_pushcclosure(L, searchers[i], 1);
-    lua_rawseti(L, -2, i+1);
-  }
-#if defined(LUA_COMPAT_LOADERS)
-  lua_pushvalue(L, -1);  /* make a copy of 'searchers' table */
-  lua_setfield(L, -3, "loaders");  /* put it in field 'loaders' */
-#endif
-  lua_setfield(L, -2, "searchers");  /* put it in field 'searchers' */
-}
+// static const luaL_Reg pk_funcs[] = {
+//   {"loadlib", ll_loadlib},
+//   {"searchpath", ll_searchpath},
+// #if defined(LUA_COMPAT_MODULE)
+//   {"seeall", ll_seeall},
+// #endif
+//   /* placeholders */
+//   {"preload", NULL},
+//   {"cpath", NULL},
+//   {"path", NULL},
+//   {"searchers", NULL},
+//   {"loaded", NULL},
+//   {NULL, NULL}
+// };
+
+
+// static const luaL_Reg ll_funcs[] = {
+// #if defined(LUA_COMPAT_MODULE)
+//   {"module", ll_module},
+// #endif
+//   {"require", ll_require},
+//   {NULL, NULL}
+// };
+
+
+// static void createsearcherstable (lua_State *L) {
+//   static const lua_CFunction searchers[] =
+//     {searcher_Lua, NULL};
+//   int i;
+//   /* create 'searchers' table */
+//   lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0);
+//   /* fill it with predefined searchers */
+//   for (i=0; searchers[i] != NULL; i++) {
+//     lua_pushvalue(L, -2);  /* set 'package' as upvalue for all searchers */
+//     lua_pushcclosure(L, searchers[i], 1);
+//     lua_rawseti(L, -2, i+1);
+//   }
+// #if defined(LUA_COMPAT_LOADERS)
+//   lua_pushvalue(L, -1);  /* make a copy of 'searchers' table */
+//   lua_setfield(L, -3, "loaders");  /* put it in field 'loaders' */
+// #endif
+//   lua_setfield(L, -2, "searchers");  /* put it in field 'searchers' */
+// }
 
 
 /*
 ** create table CLIBS to keep track of loaded C libraries,
 ** setting a finalizer to close all libraries when closing state.
 */
-static void createclibstable (lua_State *L) {
-  lua_newtable(L);  /* create CLIBS table */
-  lua_createtable(L, 0, 1);  /* create metatable for CLIBS */
-  lua_pushcfunction(L, gctm);
-  lua_setfield(L, -2, "__gc");  /* set finalizer for CLIBS table */
-  lua_setmetatable(L, -2);
-  lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS);  /* set CLIBS table in registry */
-}
+// static void createclibstable (lua_State *L) {
+//   lua_newtable(L);  /* create CLIBS table */
+//   lua_createtable(L, 0, 1);  /* create metatable for CLIBS */
+//   lua_pushcfunction(L, gctm);
+//   lua_setfield(L, -2, "__gc");  /* set finalizer for CLIBS table */
+//   lua_setmetatable(L, -2);
+//   lua_rawsetp(L, LUA_REGISTRYINDEX, &CLIBS);  /* set CLIBS table in registry */
+// }
 
 LUAMOD_API int luaopen_package (lua_State *L) {
   lua_pushcfunction(L, ll_require);

+ 4 - 0
luat/include/luat_base.h

@@ -86,6 +86,10 @@ LUAMOD_API int luaopen_pack( lua_State *L );
 LUAMOD_API int luaopen_mqttcore( lua_State *L );
 /** 加载crypto库, 可选*/
 LUAMOD_API int luaopen_crypto( lua_State *L );
+LUAMOD_API int luaopen_pm( lua_State *L);
+LUAMOD_API int luaopen_m2m( lua_State *L);
+LUAMOD_API int luaopen_coap( lua_State *L);
+LUAMOD_API int luaopen_lpmem( lua_State *L);
 
 /** sprintf需要支持longlong值的打印, 提供平台无关的实现*/
 int l_sprintf(char *buf, size_t size, const char *fmt, ...);

+ 21 - 0
luat/include/luat_pm.h

@@ -0,0 +1,21 @@
+
+#include "luat_base.h"
+
+#define LUAT_PM_SLEEP_MODE_NONE     0	//系统处于活跃状态,未采取任何的降低功耗状态
+#define LUAT_PM_SLEEP_MODE_IDLE     1	//空闲模式,该模式在系统空闲时停止 CPU 和部分时钟,任意事件或中断均可以唤醒
+#define LUAT_PM_SLEEP_MODE_LIGHT    2	//轻度睡眠模式,CPU 停止,多数时钟和外设停止,唤醒后需要进行时间补偿
+#define LUAT_PM_SLEEP_MODE_DEEP     3	//深度睡眠模式,CPU 停止,仅少数低功耗外设工作,可被特殊中断唤醒
+#define LUAT_PM_SLEEP_MODE_STANDBY	4	//待机模式,CPU 停止,设备上下文丢失(可保存至特殊外设),唤醒后通常复位
+//#define LUAT_PM_SLEEP_MODE_SHUTDOWN	5	//关断模式,比 Standby 模式功耗更低, 上下文通常不可恢复, 唤醒后复位
+
+int luat_pm_request(int mode);
+
+int luat_pm_release(int mode);
+
+int luat_pm_dtimer_start(int id, size_t timeout);
+
+int luat_pm_dtimer_stop(int id);
+
+void luat_pm_cb(int event, int arg, void* args);
+
+int luat_pm_last_state(void);

+ 108 - 0
luat/modules/luat_lib_pm.c

@@ -0,0 +1,108 @@
+
+#include "lua.h"
+#include "lauxlib.h"
+#include "luat_base.h"
+#include "luat_pm.h"
+#include "luat_msgbus.h"
+
+static int lua_event_cb = 0;
+
+static int l_pm_request(lua_State *L) {
+    int mode = luaL_checkinteger(L, 1);
+    if (luat_pm_request(mode) == 0)
+        lua_pushboolean(L, 1);
+    else
+        lua_pushboolean(L, 0);
+    return 1;
+}
+
+static int l_pm_release(lua_State *L) {
+    int mode = luaL_checkinteger(L, 1);
+    if (luat_pm_release(mode) == 0)
+        lua_pushboolean(L, 1);
+    else
+        lua_pushboolean(L, 0);
+    return 1;
+}
+
+static int l_pm_dtimer_start(lua_State *L) {
+    int dtimer_id = luaL_checkinteger(L, 1);
+    int timeout = luaL_checkinteger(L, 2);
+    if (luat_pm_dtimer_start(dtimer_id, timeout)) {
+        lua_pushboolean(L, 0);
+    }
+    else {
+        lua_pushboolean(L, 1);
+    }
+    return 1;
+}
+
+static int l_pm_dtimer_stop(lua_State *L) {
+    int dtimer_id = luaL_checkinteger(L, 1);
+    luat_pm_dtimer_stop(dtimer_id);
+    return 0;
+}
+
+static int l_pm_on(lua_State *L) {
+    if (lua_isfunction(L, 1)) {
+        if (lua_event_cb != 0) {
+            luaL_unref(L, LUA_REGISTRYINDEX, lua_event_cb);
+        }
+        lua_event_cb = luaL_ref(L, LUA_REGISTRYINDEX);
+    }
+    else if (lua_event_cb != 0) {
+        luaL_unref(L, LUA_REGISTRYINDEX, lua_event_cb);
+    }
+    return 0;
+}
+
+static int l_pm_last_reson(lua_State *L) {
+    lua_pushinteger(L, luat_pm_last_state());
+    return 1;
+}
+
+static int luat_pm_msg_handler(lua_State *L, void* ptr) {
+    rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
+    if (lua_event_cb == 0) {
+        return 0;
+    }
+    lua_geti(L, LUA_REGISTRYINDEX, lua_event_cb);
+    if (lua_isfunction(L, -1)) {
+        lua_pushinteger(L, msg->arg1);
+        lua_pushinteger(L, msg->arg2);
+        lua_call(L, 2, 0);
+    }
+    return 0;
+}
+
+void luat_pm_cb(int event, int arg, void* args) {
+    if (lua_event_cb != 0) {
+        rtos_msg_t msg;
+        msg.handler = luat_pm_msg_handler;
+        msg.arg1 = event;
+        msg.arg2 = arg;
+        msg.ptr = NULL;
+        luat_msgbus_put(&msg, 0);
+    }
+}
+
+#include "rotable.h"
+static const rotable_Reg reg_pm[] =
+{
+    { "request" ,       l_pm_request , 0},
+    { "release" ,       l_pm_release,  0},
+    { "dtimerStart",    l_pm_dtimer_start,0},
+    { "dtimerStop" ,    l_pm_dtimer_stop, 0},
+    { "on",             l_pm_on,   0},
+    { "lastReson",      l_pm_last_reson, 0},
+    { "IDLE",           NULL, LUAT_PM_SLEEP_MODE_IDLE},
+    { "LIGHT",          NULL, LUAT_PM_SLEEP_MODE_LIGHT},
+    { "DEEP",           NULL, LUAT_PM_SLEEP_MODE_DEEP},
+    { "HIB",            NULL, LUAT_PM_SLEEP_MODE_STANDBY},
+	{ NULL,          NULL ,       0}
+};
+
+LUAMOD_API int luaopen_pm( lua_State *L ) {
+    rotable_newlib(L, reg_pm);
+    return 1;
+}

+ 4 - 1
luat/modules/luat_lib_socket.c

@@ -88,8 +88,11 @@ static int luat_lib_netc_msg_handler(lua_State* L, void* ptr) {
         goto exit;
     }
     luat_log_debug("luat.socket", "luat_lib_netc_msg_handler event=%ld lua_ref=%ld", ent->event, ent->lua_ref);
+    if (ent->lua_ref == 0) {
+        goto exit;
+    }
     if (lua_rawgeti(L, LUA_REGISTRYINDEX, ent->lua_ref) != LUA_TFUNCTION) {
-        //LOG_W("netc[%ld] callback isn't a function", ent->netc_id);
+        luat_log_warn("luat.netc", "netc[%ld] callback isn't a function", ent->netc_id);
         goto exit;
     };
     lua_pushinteger(L, ent->netc_id);