allen 5 лет назад
Родитель
Сommit
36ff08d8b0
3 измененных файлов с 55 добавлено и 28 удалено
  1. 46 27
      bsp/air302/demo/ctiot/main.lua
  2. 2 1
      luat/include/luat_ctiot.h
  3. 7 0
      luat/modules/luat_lib_ctiot.c

+ 46 - 27
bsp/air302/demo/ctiot/main.lua

@@ -15,7 +15,9 @@
 -- 发送数据,输入参数为数据,发送模式,发送序号
 -- 发送模式为ctiot.CON, ctiot.NON, ctiot.NON_REL, ctiot.CON_REL
 -- 发送序号只是一个标识,不会影响发送的数据和模式,在发送结果回调时会返回
-
+-- ctiot.isReady()
+-- 检测当前ctiot连接状态是否可以发送数据
+-- 0可以,其他都不可以
 PROJECT = "ctiot"
 VERSION = "1.0.0"
 local TAG="ctiot"
@@ -46,30 +48,32 @@ end
 sys.subscribe("CTIOT_RX", cb_rx)
 --sys.subscribe("CTIOT_REG", cb_reg)
 --sys.subscribe("CTIOT_DEREG", cb_dereg)
-sys.subscribe("CTIOT_WAKEUP", cb_wakeup)
+--sys.subscribe("CTIOT_WAKEUP", cb_wakeup)
 sys.subscribe("CTIOT_OTHER", cb_other)
 sys.subscribe("CTIOT_FOTA", cb_fota)
 
 local function send_test()
     log.info(TAG, "test tx")
+    local result, tx_error, error_code, param
     --发送的数据请按照自己的profile修改
     --发送数据,并要求服务器应答
-    ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 33)
+    result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 33)
     --发送数据,并要求服务器应答,序号为11
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 11)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON, 11)
     --发送数据,不需要服务器应答
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 0)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 0)
     --发送数据,不需要服务器应答,序号为23
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 23)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON, 23)
     --发送数据,不需要服务器应答,发送完成后立刻释放RRC,加快进入休眠的速度
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 0)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 0)
     --发送数据,不需要服务器应答,发送完成后立刻释放RRC,加快进入休眠的速度,序号为56
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 56)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.NON_REL, 56)
     --发送数据,并要求服务器应答,接收到应答后立刻释放RRC,加快进入休眠的速度
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 0)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 0)
     --发送数据,并要求服务器应答,接收到应答后立刻释放RRC,加快进入休眠的速度,序号为255,序号最大255
-    --ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 255)
-    local result, tx_error, error_code, param = sys.waitUntilExt("CTIOT_TX", 20000)
+    --result, tx_error = ctiot.write(pack.pack("<bbA", 0x00,0x05, "hello"), ctiot.CON_REL, 255)
+    log.info(TAG, result, tx_error)
+    result, tx_error, error_code, param = sys.waitUntilExt("CTIOT_TX", 20000)
     if result then
         if not tx_error then
             log.info(TAG, "tx ok", param)
@@ -85,40 +89,55 @@ end
 local function task()
     local inSleep = false
     local isConnected = false
-    --设置自定义EP,如果不设置,则使用IMEI
-    local ep="qweasdzxcrtyfgh"
-    if ctiot.ep() ~= ep then
-        ctiot.ep(ep)
+    local result, error, error_code, param
+    if pm.lastReson() ~= 0 then
+        --唤醒的不需要处理
+        pm.request(pm.IDLE)
+        log.info(TAG, "after", ctiot.ep(), ctiot.param())
+        result, error, error_code, param = sys.waitUntilExt("CTIOT_WAKEUP", 30000)
+        if not result then
+            log.info(TAG, "wakeup but ctiot not ready")
+        else
+            while ctiot.isReady() > 0 do
+                log.info(TAG, "wakeup and wait ready")
+                sys.wait(500)
+            end
+            isConnected = true
+        end
+    else
+        --设置自定义EP,如果不设置,则使用IMEI
+        local ep="qweasdzxcrtyfgh"
+        if ctiot.ep() ~= ep then
+            ctiot.ep(ep)
+        end
+        --设置服务器IP,端口,保活时间
+        ctiot.param("180.101.147.115", 5683, 300)
+        local ip, port, keeptime = ctiot.param()
+        log.info(TAG, "set param", ip, port, keeptime)
     end
-    --设置服务器IP,端口,保活时间
-    ctiot.param("180.101.147.115", 5683, 300)
-    local ip, port, keeptime = ctiot.param()
-    log.info(TAG, "set param", ip, port, keeptime)
 	while not socket.isReady() do 
 		log.info("net", "wait for network ready")
 		sys.waitUntil("NET_READY", 1000)
     end
     -- 非普通上电/复位上电,那就是唤醒上电咯
     if pm.lastReson() ~= 0 then
-        isConnected = true
+        
     else
         --启动连接
-        pm.request(pm.IDLE)
         ctiot.connect()
-        local result, reg_error, error_code, param
         while true do
-            result, reg_error, error_code, param = sys.waitUntilExt("CTIOT_REG", 30000)
-            if not result and reg_error==nil then
+            result, error, error_code, param = sys.waitUntilExt("CTIOT_REG", 30000)
+            if not result and error==nil then
                 log.info(TAG, "reg wait timeout")
                 break
             end
-            log.info(TAG, result, reg_error, error_code, param)
-            if not reg_error and param > 0 then
+            log.info(TAG, result, error, error_code, param)
+            if not error and param > 0 then
                 log.info(TAG, "reg ok")
                 isConnected = true
                 break
             end
-            if reg_error then
+            if error then
                 log.info(TAG, "reg fail")
                 break
             end

+ 2 - 1
luat/include/luat_ctiot.h

@@ -15,6 +15,7 @@ enum
 	CTIOT_EVENT_SUSPEND,
 	CTIOT_EVENT_FOTA,
 	CTIOT_EVENT_AIR,
+	CTIOT_EVENT_DBG,
 
 	CTIOT_REG_OK = 0,
 	CTIOT_REG_TIMEOUT,
@@ -79,5 +80,5 @@ 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

+ 7 - 0
luat/modules/luat_lib_ctiot.c

@@ -368,6 +368,12 @@ static int l_ctiot_read(lua_State *L)
 	return 0;
 }
 
+static int l_ctiot_ready(lua_State *L)
+{
+	uint16_t result = luat_ctiot_check_ready();
+	lua_pushinteger(L, result);
+	return 1;
+}
 /**
  * @brief 发送更新注册信息给ctiot
  * 
@@ -388,6 +394,7 @@ static const rotable_Reg reg_ctiot[] =
     { "init", l_ctiot_init, 0},
     { "param", l_ctiot_param, 0},
 	{ "ep", l_ctiot_ep, 0},
+	{ "isReady", l_ctiot_ready, 0},
 //	{ "mode", l_ctiot_mode, 0},
 	{ "connect", l_ctiot_connect, 0},
 	{ "disconnect", l_ctiot_disconnect, 0},