Wendal Chen 2 лет назад
Родитель
Сommit
21f40d2950

+ 89 - 0
components/lpmem/luat_lib_lpmem.c

@@ -0,0 +1,89 @@
+
+/*
+@module  lpmem
+@summary 操作低功耗不掉电内存块
+@version V0002
+@date    2020.07.10
+*/
+
+#include "luat_base.h"
+#include "luat_malloc.h"
+#include "luat_msgbus.h"
+#include "luat_timer.h"
+#include "luat_lpmem.h"
+
+/*
+读取内存块
+@api    lpmem.read(offset, size)
+@int 内存偏移量
+@int 读取大小,单位字节
+@return string 读取成功返回字符串,否则返回nil
+@usage  
+-- 读取1kb的内存
+local data = lpmem.read(0, 1024)
+*/
+static int l_lpmem_read(lua_State *L) {
+    size_t offset = luaL_checkinteger(L, 1);
+    size_t size = luaL_checkinteger(L, 2);
+    void* buff = luat_heap_malloc(size);
+    int ret = luat_lpmem_read(offset, size, buff);
+    if (ret == 0) {
+        lua_pushlstring(L, (const char*)buff, size);
+    }
+    else {
+        lua_pushliteral(L, "");
+    }
+    luat_heap_free(buff);
+    return 1;
+}
+
+/*
+写入内存块
+@api    lpmem.write(offset, str)
+@int 内存偏移量
+@string 待写入的数据
+@return boolean 成功返回true,否则返回false
+@usage  
+-- 往偏移量为512字节的位置, 写入数据
+lpmem.write(512, data)
+*/
+static int l_lpmem_write(lua_State *L) {
+    size_t size;
+    size_t offset = luaL_checkinteger(L, 1);
+    const char* str = luaL_checklstring(L, 2, &size);
+    if (size > 0) {
+        int ret = luat_lpmem_write(offset, size, (void*)str);
+        if (ret == 0) {
+            lua_pushboolean(L, 1);
+            return 1;
+        }
+    }
+    lua_pushboolean(L, 0);
+    return 1;
+}
+
+/*
+获取内存块的总大小
+@api    lpmem.size()
+@return int 内存块的大小
+@usage  
+lpmem.size()
+*/
+static int l_lpmem_size(lua_State *L) {
+    lua_pushinteger(L, luat_lpmem_size());
+    return 1;
+}
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_lpmem[] =
+{
+    { "read" ,         ROREG_FUNC(l_lpmem_read)},
+    { "write" ,        ROREG_FUNC(l_lpmem_write)},
+    { "size",          ROREG_FUNC(l_lpmem_size)},
+	{ NULL,            ROREG_INT(0) }
+};
+
+LUAMOD_API int luaopen_lpmem( lua_State *L ) {
+    luat_newlib2(L, reg_lpmem);
+    return 1;
+}

+ 10 - 0
components/lpmem/luat_lpmem.h

@@ -0,0 +1,10 @@
+
+#ifndef LUAT_LPMEM_H
+#define LUAT_LPMEM_H
+#include "luat_base.h"
+
+int luat_lpmem_read(size_t offset, size_t size, void*buff);
+int luat_lpmem_write(size_t offset, size_t size, void*buff);
+int luat_lpmem_size(void);
+
+#endif

+ 84 - 0
components/network/ctiot/luat_ctiot.h

@@ -0,0 +1,84 @@
+#ifndef __LUAT_CTIOT_H__
+#define __LUAT_CTIOT_H__
+#include "luat_base.h"
+#include "stdbool.h"
+enum
+{
+	CTIOT_EVENT_REG = 0,
+	CTIOT_EVENT_UPDATE,
+	CTIOT_EVENT_DEREG,
+	CTIOT_EVENT_STATE,
+	CTIOT_EVENT_OB19,
+	CTIOT_EVENT_TX,
+	CTIOT_EVENT_RX,
+	CTIOT_EVENT_NOTIFY,
+	CTIOT_EVENT_SUSPEND,
+	CTIOT_EVENT_FOTA,
+	CTIOT_EVENT_AIR,
+	CTIOT_EVENT_DBG,
+
+	CTIOT_REG_OK = 0,
+	CTIOT_REG_TIMEOUT,
+	CTIOT_REG_SERVER_REJECT,
+	CTIOT_REG_VERSION_ERROR,
+	CTIOT_REG_EP_ERROR,
+	CTIOT_REG_TX_ERROR,
+	CTIOT_REG_STACK_ERROR,
+	CTIOT_UPDATE_OK = 0,
+	CTIOT_UPDATE_NOT_LOGIN,
+	CTIOT_UPDATE_TIMEOUT,
+	CTIOT_UPDATE_PARAM_ERROR,
+	CTIOT_UPDATE_SERVER_REJECT,
+	CTIOT_UPDATE_TX_ERROR,
+	CTIOT_DEREG_DONE = 0,
+	CTIOT_STATE_TAU_WAKEUP = 0,
+	CTIOT_STATE_SS_ERROR,
+	CTIOT_STATE_SS_FAIL,
+	CTIOT_STATE_SS_STACK_ERROR,
+	CTIOT_STATE_TAU_NOTIFY,
+	CTIOT_OB19_ON = 0,
+	CTIOT_OB19_OFF,
+	CTIOT_TX_ACK = 0,
+	CTIOT_TX_DONE,
+	CTIOT_TX_TIMEOUT,
+	CTIOT_TX_SERVER_RST,
+	CTIOT_TX_FAIL,
+	CTIOT_TX_OTHER_ERROR,
+	CTIOT_TX_ERROR,
+	CTIOT_NOTIFY_ACK = 0,
+	CTIOT_NOTIFY_DONE,
+	CTIOT_NOTIFY_TIMEOUT,
+	CTIOT_NOTIFY_SERVER_RST,
+	CTIOT_NOTIFY_FAIL,
+	CTIOT_NOTIFY_OTHER_ERROR,
+	CTIOT_NOTIFY_ERROR,
+	CTIOT_SUSPEND_QUIT = 0,
+	CTIOT_SUSPEND_READY,
+	CTIOT_SUSPEND_START,
+	CTIOT_FOTA_DOWNLOADING = 0,
+	CTIOT_FOTA_DOWNLOAD_FAILED,
+	CTIOT_FOTA_DOWNLOAD_SUCCESS,
+	CTIOT_FOTA_UPDATING,
+	CTIOT_FOTA_UPDATE_SUCCESS,
+	CTIOT_FOTA_UPDATE_FAILED,
+	CTIOT_FOTA_UPDATE_OVER,
+	CTIOT_FOTA_6,
+	CTIOT_FOTA_7,
+	CTIOT_AIR_NON_SEND_START=0,
+	CTIOT_AIR_CON_OK,
+	CTIOT_AIR_NON_SEND,
+};
+extern void luat_ctiot_callback(uint8_t type, uint8_t code, void *buf, uint32_t len);
+extern void luat_ctiot_init(void);
+extern uint16_t luat_ctiot_get_ep(char *userEp);
+extern uint16_t luat_ctiot_set_ep(char *userEp);
+extern uint16_t luat_ctiot_set_para(char* serverIP,uint16_t port,uint32_t lifeTime,char* objectInstanceList/*,char* pskId,char* psk*/);
+extern uint16_t luat_ctiot_set_mod(uint8_t securityMode,uint8_t idAuthMode,uint8_t autoTAUUpdate,uint8_t onUQMode,uint8_t onCELevel2Policy,uint8_t autoHeartBeat,uint8_t wakeupNotify, uint8_t protocolMode);
+extern uint16_t luat_ctiot_get_para(char* serverIP,uint16_t* port,uint32_t* lifeTime,char* objectInstanceList/*,char* pskId,char* psk*/);
+extern uint16_t luat_ctiot_get_mod(uint8_t* securityMode,uint8_t* idAuthMode,uint8_t* autoTAUUpdate,uint8_t* onUQMode,uint8_t* onCELevel2Policy,uint8_t* autoHeartBeat,uint8_t* wakeupNotify, uint8_t* protocolMode);
+extern uint16_t luat_ctiot_reg(void);
+extern uint16_t luat_ctiot_dereg(void);
+extern uint16_t luat_ctiot_update_reg(uint16_t*msgId,bool withObjects);
+extern uint16_t luat_ctiot_send(const uint8_t* data,uint32_t datalen, uint8_t sendMode, uint8_t seqNum);
+extern uint16_t luat_ctiot_check_ready(void);
+#endif

+ 546 - 0
components/network/ctiot/luat_lib_ctiot.c

@@ -0,0 +1,546 @@
+/*
+@module  ctiot
+@summary 中国电信CTIOT集成
+@version 1.0
+@date    2020.08.30
+@demo ctiot
+*/
+#include "luat_base.h"
+#include "luat_timer.h"
+#include "luat_malloc.h"
+#include "luat_msgbus.h"
+#include "luat_ctiot.h"
+#define LUAT_LOG_TAG "ctiot"
+#include "luat_log.h"
+//---------------------------
+#ifdef AIR302
+#define DBG(X,Y...) LLOGD("%s %d:"X, __FUNCTION__, __LINE__, ##Y)
+const char luat_pub_tx_tag[] = "CTIOT_TX";
+const char luat_pub_rx_tag[] = "CTIOT_RX";
+const char luat_pub_reg_tag[] = "CTIOT_REG";
+const char luat_pub_update_tag[] = "CTIOT_UPDATE";
+const char luat_pub_dereg_tag[] = "CTIOT_DEREG";
+const char luat_pub_wakeup_tag[] = "CTIOT_WAKEUP";
+const char luat_pub_other_tag[] = "CTIOT_OTHER";
+const char luat_pub_fota_tag[] = "CTIOT_FOTA";
+static int luat_ctiot_msg_handler(lua_State *L, void* ptr)
+{
+	uint8_t type;
+	uint8_t code;
+	uint32_t len;
+	uint8_t *buff = (uint8_t *)ptr;
+	char *tag;
+	LUA_INTEGER error,error_code,param;
+	type = buff[0];
+	code = buff[1];
+	memcpy(&len, buff+2, 4);
+	switch(type)
+	{
+	case CTIOT_EVENT_TX:
+		tag = (char *)luat_pub_tx_tag;
+		param = 0;
+		switch (code)
+		{
+		case CTIOT_TX_ACK:
+		case CTIOT_TX_DONE:
+			goto LUAT_CTIOT_MSG_HANDLER_DONE;
+			//break;
+		default:
+			error = 1;
+			error_code = code;
+			break;
+		}
+		break;
+	case CTIOT_EVENT_UPDATE:
+		tag = (char *)luat_pub_update_tag;
+		param = 0;
+		switch (code)
+		{
+		case CTIOT_UPDATE_OK:
+			error = 0;
+			error_code = 0;
+			param = 0;
+			break;
+		default:
+			error = 1;
+			error_code = code;
+			param = 0;
+			break;
+		}
+		break;
+	case CTIOT_EVENT_AIR:
+		tag = (char *)luat_pub_tx_tag;
+		param = 0;
+		switch (code)
+		{
+		case CTIOT_AIR_NON_SEND_START:
+			goto LUAT_CTIOT_MSG_HANDLER_DONE;
+			//break;
+		default:
+			error = 0;
+			error_code = 0;
+			param = buff[6];
+			break;
+		}
+		break;
+	case CTIOT_EVENT_RX:
+		tag = (char *)luat_pub_rx_tag;
+		break;
+	case CTIOT_EVENT_STATE:
+		switch (code)
+		{
+		case CTIOT_STATE_TAU_WAKEUP:
+		case CTIOT_STATE_TAU_NOTIFY:
+			tag = (char *)luat_pub_wakeup_tag;
+			error = 0;
+			error_code = 0;
+			param = code;
+			break;
+		default:
+			tag = (char *)luat_pub_other_tag;
+			error = 0;
+			error_code = type;
+			param = code;
+			break;
+		}
+		break;
+	case CTIOT_EVENT_REG:
+		tag = (char *)luat_pub_reg_tag;
+		switch (code)
+		{
+		case CTIOT_REG_OK:
+			error = 0;
+			error_code = 0;
+			param = 0;
+			break;
+		default:
+			error = 1;
+			error_code = code;
+			param = 0;
+			break;
+		}
+		break;
+	case CTIOT_EVENT_OB19:
+		tag = (char *)luat_pub_reg_tag;
+		switch (code)
+		{
+		case CTIOT_OB19_ON:
+			error = 0;
+			error_code = 0;
+			param = 1;
+			break;
+		default:
+			error = 1;
+			error_code = code;
+			param = 0;
+			break;
+		}
+		break;
+	case CTIOT_EVENT_DEREG:
+		tag = (char *)luat_pub_dereg_tag;
+		error = 0;
+		error_code = 0;
+		param = 0;
+		break;
+	case CTIOT_EVENT_FOTA:
+		tag = (char *)luat_pub_fota_tag;
+		error = 0;
+		error_code = 0;
+		param = code;
+		break;
+	default:
+		tag = (char *)luat_pub_other_tag;
+		error = 0;
+		error_code = type;
+		param = code;
+		break;
+	}
+	lua_getglobal(L, "sys_pub");
+	lua_pushstring(L, tag);
+/*
+@sys_pub ctiot
+CTIOT 接收回调消息
+CTIOT_RX
+@string data, CTIOT 接收数据
+@usage
+sys.subscribe("CTIOT_RX", function(data)
+    log.info("CTIOT_RX", data:toHex())
+end)
+*/
+	if (CTIOT_EVENT_RX == type)
+	{
+		lua_pushlstring(L, buff + 6, len);
+		lua_call(L, 2, 0);
+	}
+	else
+	{
+/*
+@sys_pub ctiot
+CTIOT 发送回调消息
+CTIOT_TX
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_TX", function (error, error_code, param)
+    log.info("CTIOT_TX", error, error_code, param)
+end)
+*/
+
+/*
+@sys_pub ctiot
+CTIOT REG回调消息
+CTIOT_REG
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_REG", function (error, error_code, param)
+    log.info("CTIOT_REG", error, error_code, param)
+end)
+*/
+
+/*
+@sys_pub ctiot
+CTIOT DEREG回调消息
+CTIOT_DEREG
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_DEREG", function (error, error_code, param)
+    log.info("CTIOT_DEREG", error, error_code, param)
+end)
+*/
+
+/*
+@sys_pub ctiot
+CTIOT 唤醒回调消息
+CTIOT_WAKEUP
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_WAKEUP", function (error, error_code, param)
+    log.info("CTIOT_WAKEUP", error, error_code, param)
+end)
+*/
+
+/*
+@sys_pub ctiot
+CTIOT 其他回调消息
+CTIOT_OTHER
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_OTHER", function (error, error_code, param)
+    log.info("CTIOT_OTHER", error, error_code, param)
+end)
+*/
+
+/*
+@sys_pub ctiot
+CTIOT FOTA回调消息
+CTIOT_FOTA
+@bool error, 是否成功
+@number error_code, 错误代码
+@number param, 数据
+@usage
+sys.subscribe("CTIOT_FOTA", function (error, error_code, param)
+    log.info("CTIOT_FOTA", error, error_code, param)
+end)
+*/
+
+		lua_pushboolean(L, error);
+		lua_pushinteger(L, error_code);
+		lua_pushinteger(L, param);
+		lua_call(L, 4, 0);
+	}
+LUAT_CTIOT_MSG_HANDLER_DONE:
+	luat_heap_free(ptr);
+	return 0;
+}
+
+
+void luat_ctiot_callback(uint8_t type, uint8_t code, void *buf, uint32_t len)
+{
+	rtos_msg_t msg;
+	uint8_t *buff;
+	DBG("%d,%d,0x%08x,%u",type, code, buf, len);
+	if (type >= CTIOT_EVENT_DBG)
+	{
+		return;
+	}
+	msg.handler = luat_ctiot_msg_handler;
+	if (buf)
+	{
+		msg.ptr = luat_heap_malloc(6 + len);
+	}
+	else
+	{
+		msg.ptr = luat_heap_malloc(6);
+	}
+	buff = msg.ptr;
+	if (buff)
+	{
+		buff[0] = type;
+		buff[1] = code;
+		memcpy(buff + 2, &len, 4);
+		if (buf)
+		{
+			memcpy(buff + 6, buf, len);
+		}
+	}
+	luat_msgbus_put(&msg, 0);
+
+}
+/**
+初始化ctiot,在复位开机后使用一次
+@api ctiot.init()
+@return nil 无返回值
+ */
+static int l_ctiot_init(lua_State *L)
+{
+	luat_ctiot_init();
+	return 0;
+}
+
+/**
+设置和读取ctiot相关参数,有参数输入则设置,无论是否有参数输入,均输出当前参数
+@api ctiot.param(ip, port, lifetime)
+@string 服务器ip
+@int 服务器端口
+@int 生命周期,单位秒
+@return string 服务器ip
+@return int 服务器端口
+@return int 生命周期,单位秒
+ */
+static int l_ctiot_param(lua_State *L)
+{
+	char server_ip[20];
+	const char *new_ip;
+	uint16_t port;
+	uint16_t result;
+	uint32_t lifetime;
+	size_t len;
+	uint8_t new_set = 0;
+	result = luat_ctiot_get_para(server_ip, &port, &lifetime, NULL);
+	new_ip = lua_tolstring(L, 1, &len);
+	if (len < 20 && len)
+	{
+		memset(server_ip, 0, 20);
+		memcpy(server_ip, new_ip, len);
+		new_set = 1;
+	}
+	if (lua_isinteger(L, 2))
+	{
+		port = lua_tointeger(L, 2);
+		new_set = 1;
+	}
+	if (lua_isinteger(L, 3))
+	{
+		lifetime = lua_tointeger(L, 3);
+		new_set = 1;
+	}
+	if (new_set)
+	{
+		result = luat_ctiot_set_para(server_ip, port, lifetime, NULL);
+	}
+	lua_pushstring(L, server_ip);
+	lua_pushinteger(L, port);
+	lua_pushinteger(L, lifetime);
+	return 3;
+}
+
+/**
+设置和读取自定义EP
+@api ctiot.ep(val)
+@string 自定义EP的值,默认是imei,读取的话不要填这个参数
+@return string 当前EP值
+ */
+static int l_ctiot_ep(lua_State *L)
+{
+	char userEp[40];
+	const char *new_ep;
+	size_t len;
+	uint8_t new_set = 0;
+	luat_ctiot_get_ep(userEp);
+	new_ep = lua_tolstring(L, 1, &len);
+	if (len < 40 && len)
+	{
+		memset(userEp, 0, 40);
+		memcpy(userEp, new_ep, len);
+		new_set = 1;
+	}
+	if (new_set)
+	{
+		luat_ctiot_set_ep(userEp);
+		luat_ctiot_get_ep(userEp);
+	}
+	lua_pushstring(L, userEp);
+	return 1;
+}
+
+// /**
+//设置和读取ctiot相关模式,有模式输入则设置,无论是否有模式输入,均输出当前模式
+//@api ctiot.mode()
+//@return nil 当前无返回值
+//  */
+static int l_ctiot_mode(lua_State *L)
+{
+	return 0;
+}
+
+/**
+连接CTIOT,必须在设置完参数和模式后再使用
+@api ctiot.connect()
+@return boolean 成功返回true,否则返回false
+ */
+static int l_ctiot_connect(lua_State *L)
+{
+	if (luat_ctiot_reg())
+	{
+		lua_pushboolean(L, 0);
+	}
+	else
+	{
+		lua_pushboolean(L, 1);
+	}
+	return 1;
+}
+
+/**
+断开ctiot
+@api ctiot.disconnect()
+@return nil 无返回值
+ */
+static int l_ctiot_disconnect(lua_State *L)
+{
+	luat_ctiot_dereg();
+	return 0;
+}
+
+/**
+发送数据给ctiot
+@api ctiot.write(data, mode, seq)
+@string 需要发送的数据
+@int 模式, ctiot.CON/NON/NON_REL/CON_REL
+@int 序号
+@return boolean 成功返回true,否则返回false
+@return string 成功为nil,失败返回错误描述
+ */
+static int l_ctiot_write(lua_State *L)
+{
+	const char *data;
+	uint8_t mode = 0;
+	uint8_t seq = 0;
+	size_t len = 0;
+	data = lua_tolstring(L, 1, &len);
+	if (!len)
+	{
+		lua_pushboolean(L, 0);
+		lua_pushstring(L, "no data");
+		return 2;
+	}
+	if (lua_isinteger(L, 2))
+	{
+		mode = lua_tointeger(L, 2);
+	}
+	if (lua_isinteger(L, 3))
+	{
+		seq = lua_tointeger(L, 3);
+	}
+	if (mode >= 4)
+	{
+		lua_pushboolean(L, 0);
+		lua_pushstring(L, "mode error");
+		return 2;
+	}
+	uint16_t result = luat_ctiot_send(data, len, mode, seq);
+	if (result)
+	{
+		lua_pushboolean(L, 0);
+		lua_pushfstring(L, "error code %d", result);
+	}
+	else
+	{
+		lua_pushboolean(L, 1);
+		lua_pushnil(L);
+	}
+	return 2;
+}
+
+// /**
+//读取已经接收到的数据
+//@api ctiot.read()
+//@return nil 暂无返回值
+//  */
+static int l_ctiot_read(lua_State *L)
+{
+	return 0;
+}
+
+/**
+是否已经就绪
+@api ctiot.ready()
+@return int 已经就绪返回0,否则返回错误代码
+ */
+static int l_ctiot_ready(lua_State *L)
+{
+	uint16_t result = luat_ctiot_check_ready();
+	lua_pushinteger(L, result);
+	return 1;
+}
+ /**
+发送更新注册信息给ctiot
+@api ctio.update()
+@return boolean 发送成功等待结果返回true,否则返回false
+  */
+static int l_ctiot_update(lua_State *L)
+{
+	uint16_t msgId;
+	if (luat_ctiot_update_reg(&msgId, NULL))
+	{
+		lua_pushboolean(L, 0);
+	}
+	else
+	{
+		lua_pushboolean(L, 1);
+	}
+	return 1;
+}
+#endif
+
+
+#include "rotable2.h"
+static const rotable_Reg_t reg_ctiot[] =
+{
+#ifdef AIR302
+    { "init", ROREG_FUNC(l_ctiot_init)},
+    { "param", ROREG_FUNC(l_ctiot_param)},
+	{ "ep", ROREG_FUNC(l_ctiot_ep)},
+	{ "isReady", ROREG_FUNC(l_ctiot_ready)},
+//	{ "mode", ROREG_FUNC(l_ctiot_mode)},
+	{ "connect", ROREG_FUNC(l_ctiot_connect)},
+	{ "disconnect", ROREG_FUNC(l_ctiot_disconnect)},
+	{ "write", ROREG_FUNC(l_ctiot_write)},
+//	{ "read", ROREG_FUNC(l_ctiot_read)},
+	{ "update", ROREG_FUNC(l_ctiot_update)},
+    // ----- 类型常量
+    //@const CON number CON
+	{ "CON", ROREG_INT(0)},
+    //@const NON number NON
+	{ "NON", ROREG_INT(1)},
+    //@const NON_REL number NON_REL
+	{ "NON_REL", ROREG_INT(2)},
+    //@const CON_REL number CON_REL
+	{ "CON_REL", ROREG_INT(3)},
+#endif
+	{ NULL, ROREG_INT(0) }
+};
+
+LUAMOD_API int luaopen_ctiot( lua_State *L ) {
+    luat_newlib2(L, reg_ctiot);
+    return 1;
+}