Commit 1462d00e authored by Terry Ellison's avatar Terry Ellison
Browse files

Merge pull request #842 from jmattsson/module-ltr-cleanup

Cleanup: LTR module registration
parents 4708b2ac 9003d3e8
......@@ -86,8 +86,13 @@ COMPONENTS_eagle.app.v6 = \
crypto/libcrypto.a \
dhtlib/libdhtlib.a \
tsl2561/tsl2561lib.a \
modules/libmodules.a
modules/libmodules.a \
# Inspect the modules library and work out which modules need to be linked.
# For each enabled module, a symbol name of the form XYZ_module_selected is
# returned. At link time those names are declared undefined, so those (and
# only those) modules are pulled in.
SELECTED_MODULE_SYMS=$(filter %_module_selected %module_selected1,$(shell $(NM) modules/.output/$(TARGET)/$(FLAVOR)/lib/libmodules.a))
LINKFLAGS_eagle.app.v6 = \
-Wl,--gc-sections \
......@@ -98,6 +103,7 @@ LINKFLAGS_eagle.app.v6 = \
-Wl,--no-check-sections \
-Wl,--wrap=_xtos_set_exception_handler \
-Wl,-static \
$(addprefix -u , $(SELECTED_MODULE_SYMS)) \
-Wl,--start-group \
-lc \
-lgcc \
......@@ -134,9 +140,11 @@ DEPENDS_eagle.app.v6 = \
# -DWLAN_CONFIG_CCX
CONFIGURATION_DEFINES = -D__ets__ \
-DICACHE_FLASH \
-DLUA_OPTIMIZE_MEMORY=2 \
-DMIN_OPT_LEVEL=2 \
-DLWIP_OPEN_SRC \
-DPBUF_RSV_FOR_WLAN \
-DEBUF_LWIP
-DEBUF_LWIP \
DEFINES += \
$(UNIVERSAL_TARGET_DEFINES) \
......
#ifndef __MODULE_H__
#define __MODULE_H__
#include "user_modules.h"
#include "lrodefs.h"
/* Registering a module within NodeMCU is really easy these days!
*
* Most of the work is done by a combination of pre-processor, compiler
* and linker "magic". Gone are the days of needing to update 4+ separate
* files just to register a module!
*
* You will need:
* - to include this header
* - a name for the module
* - a LUA_REG_TYPE module map
* - optionally, an init function
*
* Then simply put a line like this at the bottom of your module file:
*
* NODEMCU_MODULE(MYNAME, "myname", myname_map, luaopen_myname);
*
* or perhaps
*
* NODEMCU_MODULE(MYNAME, "myname", myname_map, NULL);
*
* if you don't need an init function.
*
* When you've done this, the module can be enabled in user_modules.h with:
*
* #define LUA_USE_MODULES_MYNAME
*
* and within NodeMCU you access it with myname.foo(), assuming you have
* a foo function in your module.
*/
#define MODULE_EXPAND_(x) x
#define MODULE_PASTE_(x,y) x##y
#define MODULE_EXPAND_PASTE_(x,y) MODULE_PASTE_(x,y)
#define LOCK_IN_SECTION(s) __attribute__((used,unused,section(s)))
/* For the ROM table, we name the variable according to ( | denotes concat):
* cfgname | _module_selected | LUA_USE_MODULES_##cfgname
* where the LUA_USE_MODULES_XYZ macro is first expanded to yield either
* an empty string (or 1) if the module has been enabled, or the literal
* LUA_USE_MOUDLE_XYZ in the case it hasn't. Thus, the name of the variable
* ends up looking either like XYZ_module_enabled, or if not enabled,
* XYZ_module_enabledLUA_USE_MODULES_XYZ. This forms the basis for
* letting the build system detect automatically (via nm) which modules need
* to be linked in.
*/
#define NODEMCU_MODULE(cfgname, luaname, map, initfunc) \
const LOCK_IN_SECTION(".lua_libs") \
luaL_Reg MODULE_PASTE_(lua_lib_,cfgname) = { luaname, initfunc }; \
const LOCK_IN_SECTION(".lua_rotable") \
luaR_table MODULE_EXPAND_PASTE_(cfgname,MODULE_EXPAND_PASTE_(_module_selected,MODULE_PASTE_(LUA_USE_MODULES_,cfgname))) \
= { luaname, map }
/* System module registration support, not using LUA_USE_MODULES_XYZ. */
#define BUILTIN_LIB_INIT(name, luaname, initfunc) \
const LOCK_IN_SECTION(".lua_libs") \
luaL_Reg MODULE_PASTE_(lua_lib_,name) = { luaname, initfunc }
#define BUILTIN_LIB(name, luaname, map) \
const LOCK_IN_SECTION(".lua_rotable") \
luaR_table MODULE_PASTE_(lua_rotable_,name) = { luaname, map }
#if !(MIN_OPT_LEVEL==2 && LUA_OPTIMIZE_MEMORY==2)
# error "NodeMCU modules must be built with LTR enabled (MIN_OPT_LEVEL=2 and LUA_OPTIMIZE_MEMORY=2)"
#endif
#endif
......@@ -12,9 +12,7 @@
#define LUA_USE_BUILTIN_DEBUG_MINIMAL // for debug.getregistry() and debug.traceback()
#ifndef LUA_CROSS_COMPILER
#define LUA_USE_MODULES
#ifdef LUA_USE_MODULES
#define LUA_USE_MODULES_ADC
#define LUA_USE_MODULES_BIT
//#define LUA_USE_MODULES_BMP085
......@@ -47,7 +45,6 @@
//#define LUA_USE_MODULES_WS2801
#define LUA_USE_MODULES_WS2812
#endif /* LUA_USE_MODULES */
#endif
#endif /* LUA_CROSS_COMPILER */
#endif /* __USER_MODULES_H__ */
......@@ -489,6 +489,7 @@ static int luaB_newproxy (lua_State *L) {
{LSTRKEY("xpcall"), LFUNCVAL(luaB_xpcall)}
#if LUA_OPTIMIZE_MEMORY == 2
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE base_funcs_list[] = {
......
......@@ -383,6 +383,7 @@ static int db_errorfb (lua_State *L) {
return 1;
}
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE dblib[] = {
......
......@@ -538,6 +538,7 @@ static int f_flush (lua_State *L) {
return pushresult(L, fs_flush(tofile(L)) == 0, NULL);
}
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
#if LUA_OPTIMIZE_MEMORY == 2
......
......@@ -311,6 +311,7 @@ static int math_randomseed (lua_State *L) {
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE math_map[] = {
......
......@@ -646,6 +646,7 @@ static const lua_CFunction loaders[] =
{loader_preload, loader_Lua, loader_C, loader_Croot, NULL};
#if LUA_OPTIMIZE_MEMORY > 0
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE lmt[] = {
......
......@@ -16,7 +16,7 @@
#undef LREGISTER
#if (MIN_OPT_LEVEL > 0) && (LUA_OPTIMIZE_MEMORY >= MIN_OPT_LEVEL)
#define LUA_REG_TYPE luaR_entry ICACHE_RODATA_ATTR
#define LUA_REG_TYPE luaR_entry
#define LSTRKEY LRO_STRKEY
#define LNUMKEY LRO_NUMKEY
#define LNILKEY LRO_NILKEY
......
......@@ -825,6 +825,7 @@ static int str_format (lua_State *L) {
return 1;
}
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE strlib[] = {
......
......@@ -266,6 +266,7 @@ static int sort (lua_State *L) {
/* }====================================================== */
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE tab_funcs[] = {
......
......@@ -221,6 +221,7 @@ static int os_exit (lua_State *L) {
c_exit(luaL_optint(L, 1, EXIT_SUCCESS));
}
#undef MIN_OPT_LEVEL
#define MIN_OPT_LEVEL 1
#include "lrodefs.h"
const LUA_REG_TYPE syslib[] = {
......
// Module for interfacing with adc
//#include "lua.h"
#include "lualib.h"
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_types.h"
#include "user_interface.h"
......@@ -28,26 +25,10 @@ static int adc_readvdd33( lua_State* L )
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE adc_map[] =
{
{ LSTRKEY( "read" ), LFUNCVAL( adc_sample ) },
static const LUA_REG_TYPE adc_map[] = {
{ LSTRKEY( "read" ), LFUNCVAL( adc_sample ) },
{ LSTRKEY( "readvdd33" ), LFUNCVAL( adc_readvdd33) },
#if LUA_OPTIMIZE_MEMORY > 0
#endif
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_adc( lua_State *L )
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register( L, AUXLIB_ADC, adc_map );
// Add constants
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
NODEMCU_MODULE(ADC, "adc", adc_map, NULL);
// Auxiliary Lua modules. All of them are declared here, then each platform
// decides what module(s) to register in the src/platform/xxxxx/platform_conf.h file
// FIXME: no longer platform_conf.h - either CPU header file, or board file
#ifndef __AUXMODS_H__
#define __AUXMODS_H__
#include "lua.h"
#define AUXLIB_GPIO "gpio"
LUALIB_API int ( luaopen_gpio )( lua_State *L );
#define AUXLIB_SPI "spi"
LUALIB_API int ( luaopen_spi )( lua_State *L );
#define AUXLIB_CAN "can"
LUALIB_API int ( luaopen_can )( lua_State *L );
#define AUXLIB_TMR "tmr"
LUALIB_API int ( luaopen_tmr )( lua_State *L );
#define AUXLIB_PD "pd"
LUALIB_API int ( luaopen_pd )( lua_State *L );
#define AUXLIB_UART "uart"
LUALIB_API int ( luaopen_uart )( lua_State *L );
#define AUXLIB_TERM "term"
LUALIB_API int ( luaopen_term )( lua_State *L );
#define AUXLIB_PWM "pwm"
LUALIB_API int ( luaopen_pwm )( lua_State *L );
#define AUXLIB_PACK "pack"
LUALIB_API int ( luaopen_pack )( lua_State *L );
#define AUXLIB_BIT "bit"
LUALIB_API int ( luaopen_bit )( lua_State *L );
#define AUXLIB_NET "net"
LUALIB_API int ( luaopen_net )( lua_State *L );
#define AUXLIB_CPU "cpu"
LUALIB_API int ( luaopen_cpu )( lua_State* L );
#define AUXLIB_ADC "adc"
LUALIB_API int ( luaopen_adc )( lua_State *L );
#define AUXLIB_RPC "rpc"
LUALIB_API int ( luaopen_rpc )( lua_State *L );
#define AUXLIB_BITARRAY "bitarray"
LUALIB_API int ( luaopen_bitarray )( lua_State *L );
#define AUXLIB_ELUA "elua"
LUALIB_API int ( luaopen_elua )( lua_State *L );
#define AUXLIB_I2C "i2c"
LUALIB_API int ( luaopen_i2c )( lua_State *L );
#define AUXLIB_WIFI "wifi"
LUALIB_API int ( luaopen_wifi )( lua_State *L );
#define AUXLIB_COAP "coap"
LUALIB_API int ( luaopen_coap )( lua_State *L );
#define AUXLIB_MQTT "mqtt"
LUALIB_API int ( luaopen_mqtt )( lua_State *L );
#define AUXLIB_U8G "u8g"
LUALIB_API int ( luaopen_u8g )( lua_State *L );
#define AUXLIB_UCG "ucg"
LUALIB_API int ( luaopen_ucg )( lua_State *L );
#define AUXLIB_NODE "node"
LUALIB_API int ( luaopen_node )( lua_State *L );
#define AUXLIB_FILE "file"
LUALIB_API int ( luaopen_file )( lua_State *L );
#define AUXLIB_OW "ow"
LUALIB_API int ( luaopen_ow )( lua_State *L );
#define AUXLIB_CJSON "cjson"
LUALIB_API int ( luaopen_cjson )( lua_State *L );
#define AUXLIB_CRYPTO "crypto"
LUALIB_API int ( luaopen_crypto )( lua_State *L );
#define AUXLIB_RC "rc"
LUALIB_API int ( luaopen_rc )( lua_State *L );
#define AUXLIB_DHT "dht"
LUALIB_API int ( luaopen_dht )( lua_State *L );
// Helper macros
#define MOD_CHECK_ID( mod, id )\
if( !platform_ ## mod ## _exists( id ) )\
return luaL_error( L, #mod" %d does not exist", ( unsigned )id )
#define MOD_CHECK_TIMER( id )\
if( id == PLATFORM_TIMER_SYS_ID && !platform_timer_sys_available() )\
return luaL_error( L, "the system timer is not available on this platform" );\
if( !platform_timer_exists( id ) )\
return luaL_error( L, "timer %d does not exist", ( unsigned )id )\
#define MOD_CHECK_RES_ID( mod, id, resmod, resid )\
if( !platform_ ## mod ## _check_ ## resmod ## _id( id, resid ) )\
return luaL_error( L, #resmod" %d not valid with " #mod " %d", ( unsigned )resid, ( unsigned )id )
#define MOD_REG_NUMBER( L, name, val )\
lua_pushnumber( L, val );\
lua_setfield( L, -2, name )
#define MOD_REG_LUDATA( L, name, val )\
lua_pushlightuserdata( L, val );\
lua_setfield( L, -2, name )
#endif
......@@ -5,13 +5,10 @@
// Modified by BogdanM for eLua
#include "module.h"
#include "c_limits.h"
//#include "lua.h"
#include "lauxlib.h"
#include "auxmods.h"
// #include "type.h"
#include "lrotable.h"
/* FIXME: Assume size_t is an unsigned lua_Integer */
typedef size_t lua_UInteger;
......@@ -122,9 +119,7 @@ static int bit_clear( lua_State* L )
return 1;
}
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE bit_map[] = {
static const LUA_REG_TYPE bit_map[] = {
{ LSTRKEY( "bnot" ), LFUNCVAL( bit_bnot ) },
{ LSTRKEY( "band" ), LFUNCVAL( bit_band ) },
{ LSTRKEY( "bor" ), LFUNCVAL( bit_bor ) },
......@@ -140,6 +135,4 @@ const LUA_REG_TYPE bit_map[] = {
{ LNILKEY, LNILVAL}
};
LUALIB_API int luaopen_bit (lua_State *L) {
LREGISTER( L, "bit", bit_map );
}
NODEMCU_MODULE(BIT, "bit", bit_map, NULL);
#include "lualib.h"
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_stdlib.h"
#include "c_string.h"
......@@ -185,19 +183,12 @@ static int ICACHE_FLASH_ATTR bmp085_lua_pressure(lua_State* L) {
return 1;
}
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE bmp085_map[] =
{
{ LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )},
{ LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )},
static const LUA_REG_TYPE bmp085_map[] = {
{ LSTRKEY( "temperature" ), LFUNCVAL( bmp085_lua_temperature )},
{ LSTRKEY( "pressure" ), LFUNCVAL( bmp085_lua_pressure )},
{ LSTRKEY( "pressure_raw" ), LFUNCVAL( bmp085_lua_pressure_raw )},
{ LSTRKEY( "init" ), LFUNCVAL( bmp085_init )},
{ LSTRKEY( "init" ), LFUNCVAL( bmp085_init )},
{ LNILKEY, LNILVAL}
};
LUALIB_API int luaopen_bmp085(lua_State *L) {
LREGISTER(L, "bmp085", bmp085_map);
return 1;
}
NODEMCU_MODULE(BMP085, "bmp085", bmp085_map, NULL);
......@@ -37,10 +37,10 @@
*/
// #include <assert.h>
#include "module.h"
#include "c_string.h"
#include "c_math.h"
#include "c_limits.h"
#include "lua.h"
#include "lauxlib.h"
#include "flash_api.h"
......@@ -1528,53 +1528,7 @@ static int json_protect_conversion(lua_State *l)
* errors are memory related */
return luaL_error(l, "Memory allocation error in CJSON protected call");
}
#endif
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE cjson_map[] =
{
{ LSTRKEY( "encode" ), LFUNCVAL( json_encode ) },
{ LSTRKEY( "decode" ), LFUNCVAL( json_decode ) },
// { LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) },
// { LSTRKEY( "encode_max_depth" ), LFUNCVAL( json_cfg_encode_max_depth ) },
// { LSTRKEY( "decode_max_depth" ), LFUNCVAL( json_cfg_decode_max_depth ) },
// { LSTRKEY( "encode_number_precision" ), LFUNCVAL( json_cfg_encode_number_precision ) },
// { LSTRKEY( "encode_keep_buffer" ), LFUNCVAL( json_cfg_encode_keep_buffer ) },
// { LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) },
// { LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) },
// { LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) },
#if LUA_OPTIMIZE_MEMORY > 0
#endif
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_cjson( lua_State *L )
{
cjson_mem_setlua (L);
/* Initialise number conversions */
// fpconv_init(); // not needed for a specific cpu.
if(-1==cfg_init(&_cfg)){
return luaL_error(L, "BUG: Unable to init config for cjson");;
}
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register( L, AUXLIB_CJSON, cjson_map );
// Add constants
/* Set cjson.null */
lua_pushlightuserdata(l, NULL);
lua_setfield(l, -2, "null");
/* Return cjson table */
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
#if 0
/* Return cjson module table */
static int lua_cjson_new(lua_State *l)
{
......@@ -1644,5 +1598,34 @@ int luaopen_cjson_safe(lua_State *l)
return 1;
}
#endif
// Module function map
static const LUA_REG_TYPE cjson_map[] = {
{ LSTRKEY( "encode" ), LFUNCVAL( json_encode ) },
{ LSTRKEY( "decode" ), LFUNCVAL( json_decode ) },
//{ LSTRKEY( "encode_sparse_array" ), LFUNCVAL( json_cfg_encode_sparse_array ) },
//{ LSTRKEY( "encode_max_depth" ), LFUNCVAL( json_cfg_encode_max_depth ) },
//{ LSTRKEY( "decode_max_depth" ), LFUNCVAL( json_cfg_decode_max_depth ) },
//{ LSTRKEY( "encode_number_precision" ), LFUNCVAL( json_cfg_encode_number_precision ) },
//{ LSTRKEY( "encode_keep_buffer" ), LFUNCVAL( json_cfg_encode_keep_buffer ) },
//{ LSTRKEY( "encode_invalid_numbers" ), LFUNCVAL( json_cfg_encode_invalid_numbers ) },
//{ LSTRKEY( "decode_invalid_numbers" ), LFUNCVAL( json_cfg_decode_invalid_numbers ) },
//{ LSTRKEY( "new" ), LFUNCVAL( lua_cjson_new ) },
{ LNILKEY, LNILVAL }
};
int luaopen_cjson( lua_State *L )
{
cjson_mem_setlua (L);
/* Initialise number conversions */
// fpconv_init(); // not needed for a specific cpu.
if(-1==cfg_init(&_cfg)){
return luaL_error(L, "BUG: Unable to init config for cjson");;
}
return 0;
}
NODEMCU_MODULE(CJSON, "cjson", cjson_map, luaopen_cjson);
/* vi:ai et sw=4 ts=4:
*/
// Module for coapwork
//#include "lua.h"
#include "lualib.h"
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_string.h"
#include "c_stdlib.h"
......@@ -561,99 +558,48 @@ static int coap_client_delete( lua_State* L )
}
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
static const LUA_REG_TYPE coap_server_map[] =
{
{ LSTRKEY( "listen" ), LFUNCVAL ( coap_server_listen ) },
{ LSTRKEY( "close" ), LFUNCVAL ( coap_server_close ) },
{ LSTRKEY( "var" ), LFUNCVAL ( coap_server_var ) },
{ LSTRKEY( "func" ), LFUNCVAL ( coap_server_func ) },
{ LSTRKEY( "__gc" ), LFUNCVAL ( coap_server_delete ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "__index" ), LROVAL ( coap_server_map ) },
#endif
static const LUA_REG_TYPE coap_server_map[] = {
{ LSTRKEY( "listen" ), LFUNCVAL( coap_server_listen ) },
{ LSTRKEY( "close" ), LFUNCVAL( coap_server_close ) },
{ LSTRKEY( "var" ), LFUNCVAL( coap_server_var ) },
{ LSTRKEY( "func" ), LFUNCVAL( coap_server_func ) },
{ LSTRKEY( "__gc" ), LFUNCVAL( coap_server_delete ) },
{ LSTRKEY( "__index" ), LROVAL( coap_server_map ) },
{ LNILKEY, LNILVAL }
};
static const LUA_REG_TYPE coap_client_map[] =
{
{ LSTRKEY( "get" ), LFUNCVAL ( coap_client_get ) },
{ LSTRKEY( "post" ), LFUNCVAL ( coap_client_post ) },
{ LSTRKEY( "put" ), LFUNCVAL ( coap_client_put ) },
{ LSTRKEY( "delete" ), LFUNCVAL ( coap_client_delete ) },
{ LSTRKEY( "__gc" ), LFUNCVAL ( coap_client_gcdelete ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "__index" ), LROVAL ( coap_client_map ) },
#endif
static const LUA_REG_TYPE coap_client_map[] = {
{ LSTRKEY( "get" ), LFUNCVAL( coap_client_get ) },
{ LSTRKEY( "post" ), LFUNCVAL( coap_client_post ) },
{ LSTRKEY( "put" ), LFUNCVAL( coap_client_put ) },
{ LSTRKEY( "delete" ), LFUNCVAL( coap_client_delete ) },
{ LSTRKEY( "__gc" ), LFUNCVAL( coap_client_gcdelete ) },
{ LSTRKEY( "__index" ), LROVAL( coap_client_map ) },
{ LNILKEY, LNILVAL }
};
const LUA_REG_TYPE coap_map[] =
static const LUA_REG_TYPE coap_map[] =
{
{ LSTRKEY( "Server" ), LFUNCVAL ( coap_createServer ) },
{ LSTRKEY( "Client" ), LFUNCVAL ( coap_createClient ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) },
{ LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) },
{ LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) },
{ LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) },
{ LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) },
{ LSTRKEY( "Server" ), LFUNCVAL( coap_createServer ) },
{ LSTRKEY( "Client" ), LFUNCVAL( coap_createClient ) },
{ LSTRKEY( "CON" ), LNUMVAL( COAP_TYPE_CON ) },
{ LSTRKEY( "NON" ), LNUMVAL( COAP_TYPE_NONCON ) },
{ LSTRKEY( "TEXT_PLAIN"), LNUMVAL( COAP_CONTENTTYPE_TEXT_PLAIN ) },
{ LSTRKEY( "LINKFORMAT"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_LINKFORMAT ) },
{ LSTRKEY( "XML"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_XML ) },
{ LSTRKEY( "OCTET_STREAM"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM ) },
{ LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) },
{ LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) },
{ LSTRKEY( "EXI"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_EXI ) },
{ LSTRKEY( "JSON"), LNUMVAL( COAP_CONTENTTYPE_APPLICATION_JSON) },
{ LSTRKEY( "__metatable" ), LROVAL( coap_map ) },
#endif
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_coap( lua_State *L )
int luaopen_coap( lua_State *L )
{
endpoint_setup();
#if LUA_OPTIMIZE_MEMORY > 0
luaL_rometatable(L, "coap_server", (void *)coap_server_map); // create metatable for coap_server
luaL_rometatable(L, "coap_client", (void *)coap_client_map); // create metatable for coap_client
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
int n;
luaL_register( L, AUXLIB_COAP, coap_map );
// Set it as its own metatable
lua_pushvalue( L, -1 );
lua_setmetatable( L, -2 );
// Module constants
MOD_REG_NUMBER( L, "CON", COAP_TYPE_CON );
MOD_REG_NUMBER( L, "NON", COAP_TYPE_NONCON );
MOD_REG_NUMBER( L, "TEXT_PLAIN", COAP_CONTENTTYPE_TEXT_PLAIN );
MOD_REG_NUMBER( L, "LINKFORMAT", COAP_CONTENTTYPE_APPLICATION_LINKFORMAT );
MOD_REG_NUMBER( L, "XML", COAP_CONTENTTYPE_APPLICATION_XML);
MOD_REG_NUMBER( L, "OCTET_STREAM", COAP_CONTENTTYPE_APPLICATION_OCTET_STREAM);
MOD_REG_NUMBER( L, "EXI", COAP_CONTENTTYPE_APPLICATION_EXI);
MOD_REG_NUMBER( L, "JSON", COAP_CONTENTTYPE_APPLICATION_JSON);
n = lua_gettop(L);
// create metatable
luaL_newmetatable(L, "coap_server");
// metatable.__index = metatable
lua_pushliteral(L, "__index");
lua_pushvalue(L,-2);
lua_rawset(L,-3);
// Setup the methods inside metatable
luaL_register( L, NULL, coap_server_map );
lua_settop(L, n);
// create metatable
luaL_newmetatable(L, "coap_client");
// metatable.__index = metatable
lua_pushliteral(L, "__index");
lua_pushvalue(L,-2);
lua_rawset(L,-3);
// Setup the methods inside metatable
luaL_register( L, NULL, coap_client_map );
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
NODEMCU_MODULE(COAP, "coap", coap_map, luaopen_coap);
// Module for cryptography
//#include "lua.h"
#include "lualib.h"
#include "module.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include "lrotable.h"
#include "c_types.h"
#include "c_stdlib.h"
#include "../crypto/digests.h"
......@@ -153,31 +150,14 @@ static int crypto_lhmac (lua_State *L)
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE crypto_map[] =
{
{ LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) },
static const LUA_REG_TYPE crypto_map[] = {
{ LSTRKEY( "sha1" ), LFUNCVAL( crypto_sha1 ) },
{ LSTRKEY( "toBase64" ), LFUNCVAL( crypto_base64_encode ) },
{ LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) },
{ LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) },
{ LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) },
{ LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) },
#if LUA_OPTIMIZE_MEMORY > 0
#endif
{ LSTRKEY( "toHex" ), LFUNCVAL( crypto_hex_encode ) },
{ LSTRKEY( "mask" ), LFUNCVAL( crypto_mask ) },
{ LSTRKEY( "hash" ), LFUNCVAL( crypto_lhash ) },
{ LSTRKEY( "hmac" ), LFUNCVAL( crypto_lhmac ) },
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_crypto( lua_State *L )
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register( L, AUXLIB_CRYPTO, crypto_map );
// Add constants
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
NODEMCU_MODULE(CRYPTO, "crypto", crypto_map, NULL);
// Module for interfacing with the DHTxx sensors (xx = 11-21-22-33-44).
#include "lualib.h"
#include "module.h"
#include "lauxlib.h"
#include "auxmods.h"
#include "lrotable.h"
#include "platform.h"
#include "cpu_esp8266.h"
#include "dht.h"
......@@ -100,30 +99,14 @@ static int dht_lapi_readxx( lua_State *L )
// }
// Module function map
#define MIN_OPT_LEVEL 2
#include "lrodefs.h"
const LUA_REG_TYPE dht_map[] =
{
{ LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) },
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
{ LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) },
#if LUA_OPTIMIZE_MEMORY > 0
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
static const LUA_REG_TYPE dht_map[] = {
{ LSTRKEY( "read" ), LFUNCVAL( dht_lapi_read ) },
{ LSTRKEY( "read11" ), LFUNCVAL( dht_lapi_read11 ) },
{ LSTRKEY( "readxx" ), LFUNCVAL( dht_lapi_readxx ) },
{ LSTRKEY( "OK" ), LNUMVAL( DHTLIB_OK ) },
{ LSTRKEY( "ERROR_CHECKSUM" ), LNUMVAL( DHTLIB_ERROR_CHECKSUM ) },
{ LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) },
#endif
{ LSTRKEY( "ERROR_TIMEOUT" ), LNUMVAL( DHTLIB_ERROR_TIMEOUT ) },
{ LNILKEY, LNILVAL }
};
LUALIB_API int luaopen_dht( lua_State *L )
{
#if LUA_OPTIMIZE_MEMORY > 0
return 0;
#else // #if LUA_OPTIMIZE_MEMORY > 0
luaL_register( L, AUXLIB_DHT, dht_map );
// Add the constants
return 1;
#endif // #if LUA_OPTIMIZE_MEMORY > 0
}
NODEMCU_MODULE(DHT, "dht", dht_map, NULL);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment