Просмотр исходного кода

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

alienwalker 2 лет назад
Родитель
Сommit
57d2fd005d

+ 58 - 0
components/crypto/luat_crypto_mbedtls.c

@@ -257,6 +257,64 @@ int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t k
     return ret;
 }
 
+int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream) {
+    const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
+    if (info == NULL) {
+        return -1;
+    }
+    stream->ctx = luat_heap_malloc(sizeof(mbedtls_md_context_t));
+    mbedtls_md_init(stream->ctx);
+    mbedtls_md_setup(stream->ctx, info, stream->key_len > 0 ? 1 : 0);
+    if (stream->key_len > 0){
+        mbedtls_md_hmac_starts(stream->ctx, (const unsigned char*)key, stream->key_len);
+    }
+    else {
+        mbedtls_md_starts(stream->ctx);
+    }
+    return 0;
+}
+
+int luat_crypto_md_update(const char * md, const char* str, size_t str_size, luat_crypt_stream_t *stream) {
+    const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
+    if (info == NULL) {
+        return -1;
+    }
+    if (stream->key_len > 0){
+        mbedtls_md_hmac_update(stream->ctx, (const unsigned char*)str, str_size);
+    }
+    else {
+        mbedtls_md_update(stream->ctx, (const unsigned char*)str, str_size);
+    }
+    return 0;
+}
+
+int luat_crypto_md_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream) {
+    const mbedtls_md_info_t * info = mbedtls_md_info_from_string(md);
+    if (info == NULL) {
+        return -1;
+    }
+    int ret = 0;
+
+    if (stream->key_len > 0) {
+        ret = mbedtls_md_hmac_finish(stream->ctx, out_ptr);
+    }
+    else {
+        ret = mbedtls_md_finish(stream->ctx, out_ptr);
+    }
+    if (ret == 0) {
+        unsigned char size = mbedtls_md_get_size(info);
+        mbedtls_md_free(stream->ctx);
+        luat_heap_free(stream->ctx);
+        stream->ctx = NULL;
+        return size;
+    }
+    mbedtls_md_free(stream->ctx);
+    luat_heap_free(stream->ctx);
+    stream->ctx = NULL;
+    LLOGI("md finish ret %d", ret);
+    return ret;
+}
+
 int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr) {
     return luat_crypto_md("MD5", str, str_size, out_ptr, NULL, 0);
 }

+ 7 - 4
components/network/libemqtt/luat_mqtt_client.c

@@ -107,8 +107,8 @@ static void mqtt_reconnect(luat_mqtt_ctrl_t *mqtt_ctrl){
 }
 
 void luat_mqtt_close_socket(luat_mqtt_ctrl_t *mqtt_ctrl){
-	LLOGI("mqtt closing socket");
-	if (mqtt_ctrl->netc){
+	LLOGI("mqtt closing socket netc:%p mqtt_state:%d",mqtt_ctrl->netc,mqtt_ctrl->mqtt_state);
+	if (mqtt_ctrl->netc && mqtt_ctrl->mqtt_state){
 		l_luat_mqtt_msg_cb(mqtt_ctrl, MQTT_MSG_DISCONNECT, 0);
 		network_force_close_socket(mqtt_ctrl->netc);
 	}
@@ -239,7 +239,7 @@ further:
 		else {
 			LLOGW("mqtt_parse ret %d, closing socket");
 			luat_mqtt_close_socket(mqtt_ctrl);
-			break;
+			return -1;
 		}
 	}
 	return 0;
@@ -255,12 +255,12 @@ static int luat_mqtt_msg_cb(luat_mqtt_ctrl_t *mqtt_ctrl) {
     switch (msg_tp) {
 		case MQTT_MSG_CONNACK: {
 			LLOGD("MQTT_MSG_CONNACK");
+			mqtt_ctrl->mqtt_state = 1;
 			if(mqtt_ctrl->mqtt_packet_buffer[3] != 0x00){
 				LLOGW("CONACK 0x%02x",mqtt_ctrl->mqtt_packet_buffer[3]);
                 luat_mqtt_close_socket(mqtt_ctrl);
                 return -1;
             }
-			mqtt_ctrl->mqtt_state = 1;
             l_luat_mqtt_msg_cb(mqtt_ctrl, MQTT_MSG_CONNACK, 0);
             break;
         }
@@ -349,6 +349,9 @@ int32_t luat_mqtt_callback(void *data, void *param) {
 	}else if(event->ID == EV_NW_RESULT_EVENT){
 		if (event->Param1==0){
 			ret = luat_mqtt_read_packet(mqtt_ctrl);
+			if (ret){
+				return -1;
+			}
 			// LLOGD("luat_mqtt_read_packet ret:%d",ret);
 			// luat_stop_rtos_timer(mqtt_ctrl->ping_timer);
 			// luat_start_rtos_timer(mqtt_ctrl->ping_timer, mqtt_ctrl->keepalive*1000*0.75, 1);

+ 83 - 0
demo/crypto/main.lua

@@ -102,6 +102,89 @@ sys.taskInit(function()
         log.info("checksum", "357E", string.char(crypto.checksum("357E", 1)):toHex())
     end
 
+            -- 流式hash测试
+    if crypto.hash_init then
+        -- MD5
+        local md5_obj = crypto.hash_init("MD5")
+        crypto.hash_update(md5_obj, "1234567890")
+        crypto.hash_update(md5_obj, "1234567890")
+        crypto.hash_update(md5_obj, "1234567890")
+        crypto.hash_update(md5_obj, "1234567890")
+        local md5_result = crypto.hash_finish(md5_obj)
+        log.info("md5_stream", md5_result)
+        log.info("md5", crypto.md5("1234567890123456789012345678901234567890"))
+
+        -- HMAC_MD5
+        local hmac_md5_obj = crypto.hash_init("MD5", "1234567890")
+        crypto.hash_update(hmac_md5_obj, "1234567890")
+        crypto.hash_update(hmac_md5_obj, "1234567890")
+        crypto.hash_update(hmac_md5_obj, "1234567890")
+        crypto.hash_update(hmac_md5_obj, "1234567890")
+        local hmac_md5_result = crypto.hash_finish(hmac_md5_obj)
+        log.info("hmac_md5_stream", hmac_md5_result)
+        log.info("hmac_md5", crypto.hmac_md5("1234567890123456789012345678901234567890", "1234567890"))
+
+        -- SHA1
+        local sha1_obj = crypto.hash_init("SHA1")
+        crypto.hash_update(sha1_obj, "1234567890")
+        crypto.hash_update(sha1_obj, "1234567890")
+        crypto.hash_update(sha1_obj, "1234567890")
+        crypto.hash_update(sha1_obj, "1234567890")
+        local sha1_result = crypto.hash_finish(sha1_obj)
+        log.info("sha1_stream", sha1_result)
+        log.info("sha1", crypto.sha1("1234567890123456789012345678901234567890"))
+
+        -- HMAC_SHA1
+        local hmac_sha1_obj = crypto.hash_init("SHA1", "1234567890")
+        crypto.hash_update(hmac_sha1_obj, "1234567890")
+        crypto.hash_update(hmac_sha1_obj, "1234567890")
+        crypto.hash_update(hmac_sha1_obj, "1234567890")
+        crypto.hash_update(hmac_sha1_obj, "1234567890")
+        local hmac_sha1_result = crypto.hash_finish(hmac_sha1_obj)
+        log.info("hmac_sha1_stream", hmac_sha1_result)
+        log.info("hmac_sha1", crypto.hmac_sha1("1234567890123456789012345678901234567890", "1234567890"))
+
+        -- SHA256
+        local sha256_obj = crypto.hash_init("SHA256")
+        crypto.hash_update(sha256_obj, "1234567890")
+        crypto.hash_update(sha256_obj, "1234567890")
+        crypto.hash_update(sha256_obj, "1234567890")
+        crypto.hash_update(sha256_obj, "1234567890")
+        local sha256_result = crypto.hash_finish(sha256_obj)
+        log.info("sha256_stream", sha256_result)
+        log.info("sha256", crypto.sha256("1234567890123456789012345678901234567890"))
+
+        -- HMAC_SHA256
+        local hmac_sha256_obj = crypto.hash_init("SHA256", "1234567890")
+        crypto.hash_update(hmac_sha256_obj, "1234567890")
+        crypto.hash_update(hmac_sha256_obj, "1234567890")
+        crypto.hash_update(hmac_sha256_obj, "1234567890")
+        crypto.hash_update(hmac_sha256_obj, "1234567890")
+        local hmac_sha256_result = crypto.hash_finish(hmac_sha256_obj)
+        log.info("hmac_sha256_stream", hmac_sha256_result)
+        log.info("hmac_sha256", crypto.hmac_sha256("1234567890123456789012345678901234567890", "1234567890"))
+
+        -- SHA512
+        local sha512_obj = crypto.hash_init("SHA512")
+        crypto.hash_update(sha512_obj, "1234567890")
+        crypto.hash_update(sha512_obj, "1234567890")
+        crypto.hash_update(sha512_obj, "1234567890")
+        crypto.hash_update(sha512_obj, "1234567890")
+        local sha512_result = crypto.hash_finish(sha512_obj)
+        log.info("sha512_stream", sha512_result)
+        log.info("sha512", crypto.sha512("1234567890123456789012345678901234567890"))
+
+        -- HMAC_SHA512
+        local hmac_sha512_obj = crypto.hash_init("SHA512", "1234567890")
+        crypto.hash_update(hmac_sha512_obj, "1234567890")
+        crypto.hash_update(hmac_sha512_obj, "1234567890")
+        crypto.hash_update(hmac_sha512_obj, "1234567890")
+        crypto.hash_update(hmac_sha512_obj, "1234567890")
+        local hmac_sha512_result = crypto.hash_finish(hmac_sha512_obj)
+        log.info("hmac_sha512_stream", hmac_sha512_result)
+        log.info("hmac_sha512", crypto.hmac_sha512("1234567890123456789012345678901234567890", "1234567890"))
+    end
+
     log.info("crypto", "ALL Done")
     sys.wait(100000)
 end)

+ 48 - 0
demo/i2s/main.lua

@@ -0,0 +1,48 @@
+
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "i2sdemo"
+VERSION = "1.0.0"
+
+-- sys库是标配
+sys = require("sys")
+
+if wdt then
+    --添加硬狗防止程序卡死,在支持的设备上启用这个功能
+    wdt.init(9000)--初始化watchdog设置为9s
+    sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
+end
+
+sys.taskInit(function()
+    audio.config(0, 20, 1, 0, 20)
+    audio.setBus(0, audio.BUS_I2S)
+    audio.start(0, audio.PCM, 1, 16000, 16)
+    audio.vol(0, 10)
+
+    local file_size = fs.fsize("/luadb/test.pcm")
+    print("/luadb/test.pcm size",file_size)   
+    local f = io.open("/luadb/test.pcm", "rb")
+    if f then 
+        while 1 do
+            local data = f:read(4096)
+            print("-------------")
+            if not data or #data == 0 then
+                break
+            end
+            audio.write(0, data)
+            sys.wait(50)
+        end
+        f:close()
+    end
+end)
+
+sys.taskInit(function()
+    while 1 do
+        sys.wait(1000)
+        log.info("sys", rtos.meminfo("sys"))
+    end
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

BIN
demo/i2s/test.pcm


+ 10 - 0
demo/json/main.lua

@@ -49,6 +49,16 @@ sys.taskInit(function()
         local tmp3 = json.decode(tmp2)
         log.info("json", "tmp3", tmp3.str, tmp3.str == tmp)
         -- break
+
+        log.info("json.null", json.encode({name=json.null}))
+        log.info("json.null", json.decode("{\"abc\":null}").abc == json.null)
+        log.info("json.null", json.decode("{\"abc\":null}").abc == nil)
+
+        -- 以下代码仅64bit固件可正确运行
+        local tmp = [[{ "timestamp": 1691998307973}]]
+        local abc, err = json.decode(tmp)
+        log.info("json", abc, err)
+        log.info("json", abc and abc.timestamp)
     end
 end)
 

+ 12 - 0
luat/include/luat_crypto.h

@@ -1,6 +1,7 @@
 #ifndef LUAT_CRYPTO_H
 #define LUAT_CRYPTO_H
 #include "luat_base.h"
+#include "mbedtls/md.h"
 
 #define LUAT_CRYPTO_AES_ECB 1
 #define LUAT_CRYPTO_AES_CBC 2
@@ -12,6 +13,13 @@
 #define LUAT_CRYPTO_AES_PAD_5 2
 #define LUAT_CRYPTO_AES_PAD_7 3
 
+typedef struct
+{
+    char tp[16];
+    size_t key_len;
+	mbedtls_md_context_t *ctx;
+}luat_crypt_stream_t;
+
 int luat_crypto_trng(char* buff, size_t len);
 
 int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr);
@@ -31,4 +39,8 @@ int luat_crypto_cipher_suites(const char** list, size_t* len);
 
 int luat_crypto_md(const char* md, const char* str, size_t str_size, void* out_ptr, const char* key, size_t key_len);
 int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t key_len, const char* path);
+
+int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream);
+int luat_crypto_md_update(const char* md, const char* str, size_t str_size, luat_crypt_stream_t *stream);
+int luat_crypto_md_finish(const char* md, void* out_ptr, luat_crypt_stream_t *stream);
 #endif

+ 87 - 0
luat/modules/luat_lib_crypto.c

@@ -13,8 +13,10 @@
 #include "luat_str.h"
 #include <time.h>
 #include "luat_zbuff.h"
+#include "mbedtls/md.h"
 
 #define LUAT_LOG_TAG "crypto"
+#define LUAT_CRYPTO_TYPE "crypto"
 #include "luat_log.h"
 
 static const unsigned char hexchars[] = "0123456789ABCDEF";
@@ -662,6 +664,86 @@ static int l_crypto_md(lua_State *L) {
     return 1;
 }
 
+/*
+创建流式hash用的stream
+@api crypto.hash_init(tp)
+@string hash类型, 大写字母, 例如 "MD5" "SHA1" "SHA256"
+@string hmac值,可选
+@return userdata 成功返回一个数据结构,否则返回nil
+@usage
+-- 无hmac的hash stream
+log.info("md5", crypto.hash_init("MD5"))
+log.info("sha1", crypto.hash_init("SHA1"))
+log.info("sha256", crypto.hash_init("SHA256"))
+
+-- 带hmac的hash stream
+log.info("hmac_md5", crypto.hash_init("MD5", "123456"))
+log.info("hmac_sha1", crypto.hash_init("SHA1", "123456"))
+log.info("hmac_sha256", crypto.hash_init("SHA256", "123456"))
+local stream = crypto.hash_init()
+*/
+static int l_crypt_hash_init(lua_State *L) {
+    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)lua_newuserdata(L, sizeof(luat_crypt_stream_t));
+    if(stream == NULL) {
+        lua_pushnil(L);
+    } else {
+        memset(stream, 0x00, sizeof(luat_crypt_stream_t));
+        const char* key = NULL;
+        const char* md = luaL_checkstring(L, 1);
+        strncpy(stream->tp, md, strlen(md));
+        if(lua_type(L, 2) == LUA_TSTRING) {
+            key = luaL_checklstring(L, 2, &(stream->key_len));
+        }
+        int ret = luat_crypto_md_init(md, key, stream);
+        if (ret < 0) {
+            lua_pushnil(L);
+        } else {
+            luaL_setmetatable(L, LUAT_CRYPTO_TYPE);
+        }
+    }
+    return 1;
+}
+
+/*
+流式hash更新数据
+@api crypto.hash_update(stream, data)
+@userdata crypto.hash_init()创建的stream, 必选
+@string 待计算的数据,必选
+@return 无
+@usage
+crypto.hash_update(stream, "OK")
+*/
+static int l_crypt_hash_update(lua_State *L) {
+    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
+    size_t data_len = 0;
+    const char *data = luaL_checklstring(L, 2, &data_len);
+    luat_crypto_md_update(stream->tp, data, data_len ,stream);
+    return 0;
+}
+
+/*
+获取流式hash校验值并释放创建的stream
+@api crypto.l_crypt_hash_finish(stream)
+@userdata crypto.hash_init()创建的stream,必选
+@return string 成功返回计算得出的流式hash值的hex字符串,失败无返回
+@usage
+local hashResult = crypto.hash_finish("MD5", stream)
+*/
+static int l_crypt_hash_finish(lua_State *L) {
+    luat_crypt_stream_t *stream = (luat_crypt_stream_t *)luaL_checkudata(L, 1, LUAT_CRYPTO_TYPE);
+    char buff[128] = {0};
+    char output[64];
+    int ret = luat_crypto_md_finish(stream->tp, output, stream);
+    LLOGD("finish result %d", ret);
+    if (ret < 1) {
+        return 0;
+    }
+    fixhex(output, buff, ret);
+    lua_pushlstring(L, buff, ret * 2);
+    return 1;
+}
+
+
 /*
 计算checksum校验和
 @api crypto.checksum(data, mode)
@@ -725,12 +807,17 @@ static const rotable_Reg_t reg_crypto[] =
     { "md_file",        ROREG_FUNC(l_crypto_md_file)},
     { "md",             ROREG_FUNC(l_crypto_md)},
     { "checksum",       ROREG_FUNC(l_crypt_checksum)},
+    { "hash_init",      ROREG_FUNC(l_crypt_hash_init)},
+    { "hash_update",    ROREG_FUNC(l_crypt_hash_update)},
+    { "hash_finish",    ROREG_FUNC(l_crypt_hash_finish)},
 
 	{ NULL,             ROREG_INT(0) }
 };
 
 LUAMOD_API int luaopen_crypto( lua_State *L ) {
     luat_newlib2(L, reg_crypto);
+    luaL_newmetatable(L, LUAT_CRYPTO_TYPE);
+    lua_pop(L, 1);
     return 1;
 }
 

+ 1 - 1
script/libs/rc522.lua

@@ -554,7 +554,7 @@ end
 rc522.write_datablock(addr,data)
 ]]
 function rc522.write_datablock(addr,data)
-    if #data ~= 6 then
+    if #data ~= 16 then
         return false
     end
     local status,array_id = rc522.request(rc522.reqall)

+ 2 - 1
tools/make_doc_file.py

@@ -5,10 +5,11 @@ import requests
 #bsp.h文件列表
 bsp_header_list = [
 {"name":"Air101/Air103","url":"https://gitee.com/openLuat/luatos-soc-air101/raw/master/app/port/luat_conf_bsp.h"},
+{"name":"Air601","url":"https://gitee.com/openLuat/luatos-soc-air101/raw/master/app/port/luat_conf_bsp.h"},
 {"name":"Air105","url":"https://gitee.com/openLuat/luatos-soc-air105/raw/master/application/include/luat_conf_bsp.h"},
 {"name":"ESP32C3","url":"https://gitee.com/openLuat/luatos-soc-idf5/raw/master/luatos/include/luat_conf_bsp.h"},
 {"name":"ESP32S3","url":"https://gitee.com/openLuat/luatos-soc-idf5/raw/master/luatos/include/luat_conf_bsp.h"},
-{"name":"Air780E","url":"https://gitee.com/openLuat/luatos-soc-2022/raw/master/project/luatos/inc/luat_conf_bsp.h"},
+{"name":"Air780E/Air700E","url":"https://gitee.com/openLuat/luatos-soc-2022/raw/master/project/luatos/inc/luat_conf_bsp.h"},
 ]
 print("getting bsp.h files...")
 for bsp in bsp_header_list: