Browse Source

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

alienwalker 3 years ago
parent
commit
07df77ee8d

+ 1 - 0
bsp/emulator/include/luat_remotem.h

@@ -36,6 +36,7 @@ int luat_remotem_ready(void);
 int luat_remotem_up(cJSON* top);
 // from string ext
 void luat_str_tohex(char* str, size_t len, char* buff);
+void luat_str_tohexwithsep(char* str, size_t len, char* separator, size_t len_j, char* buff);
 
 
 void luat_remotem_typeopt_init(cJSON* top, cJSON* data);

+ 50 - 1
bsp/win32/module_test/004crypto.lua

@@ -2,18 +2,67 @@ local md5 = crypto.md5("abc")
 log.info("md5 result",md5)
 assert(md5:upper() == "900150983CD24FB0D6963F7D28E17F72","md5 error")
 
+local hmac_md5 = crypto.hmac_md5("abc", "1234567890")
+log.info("hmac_md5 result",hmac_md5)
+assert(hmac_md5:upper() == "416478FC0ACE1C4AB37F85F4F86A16B1","hmac_md5 error")
+
 local sha1 = crypto.sha1("abc")
 log.info("sha1 result",sha1)
 assert(sha1:upper() == "A9993E364706816ABA3E25717850C26C9CD0D89D","sha1 error")
 
+local hmac_sha1 = crypto.hmac_sha1("abc", "1234567890")
+log.info("hmac_sha1 result",hmac_sha1)
+assert(hmac_sha1:upper() == "DAE54822C0DAF6C115C97B0AD62C7BCBE9D5E6FC","hmac_md5 error")
+
 local sha256 = crypto.sha256("abc")
 log.info("sha256 result",sha256)
 assert(sha256:upper() == "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD","sha256 error")
 
+local hmac_sha256 = crypto.hmac_sha256("abc", "1234567890")
+log.info("hmac_sha256 result",hmac_sha256)
+assert(hmac_sha256:upper() == "86055184805B4A466A7BE398FF4A7159F9055EA7EEF339FC94DCEC6F165898BA","hmac_sha256 error")
+
 local sha512 = crypto.sha512("abc")
 log.info("sha512 result",sha512)
 assert(sha512:upper() == "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F","sha512 error")
 
+local hmac_sha512 = crypto.hmac_sha512("abc", "1234567890")
+log.info("hmac_sha512 result",hmac_sha512)
+assert(hmac_sha512:upper() == "0F92B9AC88949E0BF7C9F1E6F9901BAB8EDFDC9E561DFDE428BC4339961A0569AD01B44343AA56E439949655D15C4D28492D459E75015489920243F3C9986F2A","hmac_sha512 error")
+
+local data_encrypt = crypto.cipher_encrypt("AES-128-ECB", "PKCS7", "12345678901234 > 123456", "1234567890123456")
+log.info("AES", "aes-128-ecb encrypt", data_encrypt:toHex())
+assert(data_encrypt:toHex():upper() == "A37DE67837A1A3006E47A7BC25AA0ECC030B4E058E1972FE5B257FD8C3436142", "AES aes-128-ecb encrypt error")
+
+local data_decrypt = crypto.cipher_decrypt("AES-128-ECB", "PKCS7", data_encrypt, "1234567890123456")
+log.info("AES", "aes-128-ecb decrypt", data_decrypt)
+assert(data_decrypt:upper() == "12345678901234 > 123456", "AES aes-128-ecb decrypt error")
+
+local data2_encrypt = crypto.cipher_encrypt("AES-128-CBC", "PKCS7", "12345678901234 > 123456", "1234567890123456", "1234567890666666")
+log.info("AES", "aes-128-cbc encrypt", data2_encrypt:toHex())
+assert(data2_encrypt:toHex():upper() == "26D98EA512AE92BC487536B83F2BE99B467649A9700338F4B4FF75AA2654DD2C", "AES aes-128-cbc encrypt error")
+
+local data2_decrypt = crypto.cipher_decrypt("AES-128-CBC", "PKCS7", data2_encrypt, "1234567890123456", "1234567890666666")
+log.info("AES", "aes-128-cbc decrypt", data2_decrypt)
+assert(data2_decrypt:upper() == "12345678901234 > 123456", "AES aes-128-cbc decrypt error")
+
+local crc16 = crypto.crc16("IBM", "1234567890")
+log.info("crc16", crc16)
+assert(crc16 == 50554, "crc16 error")
+
+local crc16_modbus = crypto.crc16_modbus("1234567890")
+log.info("crc16_modbus", crc16_modbus)
+assert(crc16_modbus == 49674, "crc16_modbus error")
+
+local crc32 = crypto.crc32("1234567890")
+log.info("crc32", crc32)
+assert(crc32 == 639479525, "crc32 error")
 
+local crc8 = crypto.crc8("1234567890", 0x31, 0xff, false)
+log.info("crc8", crc8)
+assert(crc8 == 208, "crc8 error")
 
---todo
+local ts = 1646796576
+local otp = crypto.totp("VK54ZXPO74ISEM2E", ts)
+log.info("totp", otp)
+assert(otp == 522113, "totp error")

+ 12 - 12
components/lcd/luat_lib_lcd.c

@@ -1432,24 +1432,24 @@ RGB565颜色生成
 @usage
 -- 本API支持多种模式, 参数数量分别是 1, 2, 3, 4
 -- 1. 单参数形式, 24bit RGB值, swap = true, 推荐
-local red =   lcd.rgb(0xFF0000)
-local green = lcd.rgb(0x00FF00)
-local blue =  lcd.rgb(0x0000FF)
+local red =   lcd.rgb565(0xFF0000)
+local green = lcd.rgb565(0x00FF00)
+local blue =  lcd.rgb565(0x0000FF)
 
 -- 2. 两参数形式, 24bit RGB值, 增加swap的设置
-local red =   lcd.rgb(0xFF0000, true)
-local green = lcd.rgb(0x00FF00, true)
-local blue =  lcd.rgb(0x0000FF, true)
+local red =   lcd.rgb565(0xFF0000, true)
+local green = lcd.rgb565(0x00FF00, true)
+local blue =  lcd.rgb565(0x0000FF, true)
 
 -- 3. 三参数形式, 红/绿/蓝, 各8bit 
-local red = lcd.rgb(0xFF, 0x00, 0x00)
-local green = lcd.rgb(0x00, 0xFF, 0x00)
-local blue = lcd.rgb(0x00, 0x00, 0xFF)
+local red = lcd.rgb565(0xFF, 0x00, 0x00)
+local green = lcd.rgb565(0x00, 0xFF, 0x00)
+local blue = lcd.rgb565(0x00, 0x00, 0xFF)
 
 -- 4. 四参数形式, 红/绿/蓝, 各8bit, 增加swap的设置
-local red = lcd.rgb(0xFF, 0x00, 0x00, true)
-local green = lcd.rgb(0x00, 0xFF, 0x00, true)
-local blue = lcd.rgb(0x00, 0x00, 0xFF, true)
+local red = lcd.rgb565(0xFF, 0x00, 0x00, true)
+local green = lcd.rgb565(0x00, 0xFF, 0x00, true)
+local blue = lcd.rgb565(0x00, 0x00, 0xFF, true)
 */
 static int l_lcd_rgb565(lua_State* L) {
   uint8_t r =0,g =0,b = 0;

+ 7 - 5
components/sfud/luat_lib_sfud.c

@@ -27,7 +27,7 @@ luat_sfud_flash_t luat_sfud;
 --spi
 log.info("sfud.init",sfud.init(0,20,20 * 1000 * 1000))
 --spi_device
-local spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,1)
+local spi_device = spi.deviceSetup(0,17,0,0,8,2000000,spi.MSB,1,0)
 log.info("sfud.init",sfud.init(spi_device))
 */
 static int luat_sfud_init(lua_State *L){
@@ -43,7 +43,7 @@ static int luat_sfud_init(lua_State *L){
         sfud_spi_flash.dataw = 8; // 8bit
         sfud_spi_flash.bit_dict = 1; // MSB=1, LSB=0
         sfud_spi_flash.master = 1; // master=1,slave=0
-        sfud_spi_flash.mode = 1; // FULL=1, half=0
+        sfud_spi_flash.mode = 0; // FULL=1, half=0
         luat_spi_setup(&sfud_spi_flash);
         luat_sfud.user_data = &sfud_spi_flash;
     }else if (lua_type(L, 1) == LUA_TUSERDATA){
@@ -113,12 +113,14 @@ static int luat_sfud_chip_erase(lua_State *L){
 }
 
 /*
-擦除 Flash 全部数据
-@api  sfud.erase(flash)
+擦除 Flash 指定地址指定大小
+@api  sfud.erase(flash,add,size)
 @userdata flash Flash 设备对象 sfud.get_device_table()返回的数据结构
+@number add 擦除地址
+@number size 擦除大小
 @return int 成功返回0
 @usage
-sfud.erase(flash)
+sfud.erase(flash,add,size)
 */
 static int luat_sfud_erase(lua_State *L){
     const sfud_flash *flash = lua_touserdata(L, 1);

+ 19 - 7
lua/src/lstrlib_exts.c

@@ -13,14 +13,21 @@
 /* }====================================================== */
 
 static unsigned char hexchars[] = "0123456789ABCDEF";
-void luat_str_tohex(char* str, size_t len, char* buff) {
+void luat_str_tohexwithsep(char* str, size_t len, char* separator, size_t len_j, char* buff) {
   for (size_t i = 0; i < len; i++)
   {
     char ch = *(str+i);
-    buff[i*2] = hexchars[(unsigned char)ch >> 4];
-    buff[i*2+1] = hexchars[(unsigned char)ch & 0xF];
+    buff[i*(2+len_j)] = hexchars[(unsigned char)ch >> 4];
+    buff[i*(2+len_j)+1] = hexchars[(unsigned char)ch & 0xF];
+    for (size_t j = 0; j < len_j; j++){
+      buff[i*(2+len_j)+2+j] = separator[j];
+    }
   }
 }
+
+void luat_str_tohex(char* str, size_t len, char* buff) {
+  luat_str_tohexwithsep(str, len, NULL, 0, buff);
+}
 void luat_str_fromhex(char* str, size_t len, char* buff) {
   for (size_t i = 0; i < len/2; i++)
   {
@@ -35,25 +42,30 @@ void luat_str_fromhex(char* str, size_t len, char* buff) {
 }
 /*
 将字符串转成HEX
-@api string.toHex(str)
+@api string.toHex(str, separator)
 @string 需要转换的字符串
+@string 分隔符, 默认为""
 @return string HEX字符串
 @return number HEX字符串的长度
 @usage
 string.toHex("\1\2\3") --> "010203" 6
 string.toHex("123abc") --> "313233616263" 12
+string.toHex("123abc", " ") --> "31 32 33 61 62 63 " 12
 */
 int l_str_toHex (lua_State *L) {
   size_t len;
   const char *str = luaL_checklstring(L, 1, &len);
+  size_t len_j;
+  const char *separator = luaL_optlstring(L, 2, "", &len_j);
   luaL_Buffer buff;
-  luaL_buffinitsize(L, &buff, 2*len);
-  luat_str_tohex((char*)str, len, buff.b);
-  buff.n = len * 2;
+  luaL_buffinitsize(L, &buff, (2+len_j)*len);
+  luat_str_tohexwithsep((char*)str, len, (char*)separator, len_j, buff.b);
+  buff.n = len * (2 + len_j);
   luaL_pushresult(&buff);
   lua_pushinteger(L, len*2);
   return 2;
 }
+
 /*
 将HEX转成字符串
 @api string.fromHex(hex)

+ 1 - 0
luat/include/luat_str.h

@@ -3,6 +3,7 @@
 #define LUAT_STR_H
 #include "string.h"
 
+void luat_str_tohexwithsep(char* str, size_t len, char* separator, size_t len_j, char* buff);
 void luat_str_tohex(char* str, size_t len, char* buff);
 void luat_str_fromhex(char* str, size_t len, char* buff);
 

+ 3 - 3
luat/modules/luat_lib_dac.c

@@ -23,7 +23,7 @@
 @int 模式,默认为0,预留
 @return true 成功返回true,否则返回false
 @return int 底层返回值,调试用
-
+@usage
 if dac.open(0, 44000) then
     log.info("dac", "dac ch0 is opened")
 end
@@ -46,7 +46,7 @@ static int l_dac_open(lua_State *L) {
 @string 若输出固定值,可以填数值, 若输出波形,填string
 @return true 成功返回true,否则返回false
 @return int 底层返回值,调试用
-
+@usage
 if dac.open(0, 44000) then
     log.info("dac", "dac ch0 is opened")
     dac.write(0, string.fromHex("ABCDABCD"))
@@ -137,7 +137,7 @@ static int l_dac_write(lua_State *L) {
 @int 通道编号,例如0
 @return true 成功返回true,否则返回false
 @return int 底层返回值,调试用
-
+@usage
 if dac.open(0, 44000) then
     log.info("dac", "dac ch0 is opened")
     dac.write(0, string.fromHex("ABCDABCD"))

+ 99 - 0
script/turnkey/eink-calendar/main.lua

@@ -0,0 +1,99 @@
+PROJECT = "wifidemo"
+VERSION = "1.0.0"
+
+--测试支持硬件:ESP32C3
+--测试固件版本:LuatOS-SoC_V0003_ESP32C3[_USB].soc
+
+local sys = require "sys"
+
+--需要自行填写的东西
+--wifi信息
+local wifiName,wifiPassword = "wifi","password"
+--地区id,请前往https://api.luatos.org/luatos-calendar/v1/check-city/ 查询自己所在位置的id
+local location = "101020100"
+--天气接口信息,需要自己申请,具体参数请参考https://api.luatos.org/ 页面上的描述
+local appid,appsecret = "27548549","rEi9nRak"
+
+local function connectWifi()
+    log.info("wlan", "wlan_init:", wlan.init())
+
+    wlan.setMode(wlan.STATION)
+    wlan.connect(wifiName,wifiPassword)
+
+    -- 等待连上路由,此时还没获取到ip
+    result, _ = sys.waitUntil("WLAN_STA_CONNECTED")
+    log.info("wlan", "WLAN_STA_CONNECTED", result)
+    -- 等到成功获取ip就代表连上局域网了
+    result, data = sys.waitUntil("IP_READY")
+    log.info("wlan", "IP_READY", result, data)
+end
+
+local function requestHttp()
+    local rd = {}
+    local httpc = esphttp.init(esphttp.GET, "http://apicn.luatos.org:23328/luatos-calendar/v1?mac=111&battery=10&location="..location.."&appid="..appid.."&appsecret="..appsecret)
+    if httpc then
+        local ok, err = esphttp.perform(httpc, true)
+        if ok then
+            while 1 do
+                local result, c, ret, data = sys.waitUntil("ESPHTTP_EVT", 20000)
+                --log.info("httpc", result, c, ret)
+                if c == httpc then
+                    if esphttp.is_done(httpc, ret) then
+                        break
+                    end
+                    if ret == esphttp.EVENT_ON_DATA and esphttp.status_code(httpc) == 200 then
+                        table.insert(rd,data)
+                    end
+                end
+            end
+        else
+            log.warn("esphttp", "bad perform", err)
+        end
+        esphttp.cleanup(httpc)
+        if ok then
+            return table.concat(rd)
+        end
+    end
+end
+
+function refresh()
+    log.info("refresh","start!")
+    local data
+    for i=1,5 do--重试最多五次
+        data = requestHttp()
+        if #data > 100 then
+            break
+        end
+        log.info("load fail","retry!")
+    end
+    if #data < 100 then
+        log.info("load fail","exit!")
+        return
+    end
+    eink.model(eink.MODEL_1in54)
+    log.info("eink.setup",eink.setup(0, 2,11,10,6,7))
+    eink.setWin(200, 200, 2)
+    eink.clear(1)
+    log.info("eink", "end setup")
+    eink.drawXbm(0, 0, 200, 200, data)
+    -- 刷屏幕
+    eink.show()
+    eink.sleep()
+    log.info("refresh","done")
+end
+
+
+sys.taskInit(function()
+    --先连wifi
+    connectWifi()
+    while true do
+        refresh()
+        sys.wait(3600*1000)--一小时刷新一次吧
+    end
+end)
+
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!

+ 124 - 0
script/turnkey/remote_pc_poweron/main.lua

@@ -0,0 +1,124 @@
+PROJECT = "poweron_tool"
+VERSION = "1.0.0"
+
+--测试支持硬件:ESP32C3
+--测试固件版本:LuatOS-SoC_V0003_ESP32C3[_USB].soc
+
+sys = require "sys"
+
+---------------------------------------------------------------------------
+--这些东西改成自己用的
+local wifiName,wifiPassword = "wifi", "password"--wifi账号密码
+local pcIp =  "255.255.255.255"--目标pc ip,广播就255.255.255.255
+local pcMac = "112233445566" --写mac的hex值就行
+--服务器使用介绍详见https://api.luatos.org/#poweron
+local mqttAddr = "mqtt://apicn.luatos.org:1883"--这是公共服务器,只允许订阅和推送poweron/request/+、poweron/reply/+两个主题
+local mqttUser,mqttPassword = "13xxxxxxxxx","888888"--你的erp账号和密码,连接mqtt服务器用,默认八个8 erp.openluat.com
+local subscribeTopic,subscribePayload = "poweron/request/chenxuuu","poweron"
+local replyTopic,replyPayload = "poweron/reply/chenxuuu","ok"
+---------------------------------------------------------------------------
+
+connected = false
+
+-- 开发板上的2个LED
+local LED_D4 = gpio.setup(12, 0)
+local LED_D5 = gpio.setup(13, 0)
+
+sys.taskInit(function()
+    while true do
+        if connected then
+            LED_D4(1)
+            sys.wait(1000)
+        else
+            LED_D4(0)
+            sys.wait(200)
+            LED_D4(1)
+            sys.wait(200)
+        end
+    end
+end)
+
+
+function wakeUp(mac)
+    log.info("socket", "begin socket")
+    local sock = socket.create(socket.UDP) -- udp
+    log.info("socket.bind", socket.bind(sock, "0.0.0.0", 23333)) --udp必须绑定端口
+    local err = socket.connect(sock, pcIp, 7)--你电脑ip
+    if err ~= 0 then log.info("socket", err) return end
+
+    mac = mac:fromHex()
+    local msg = string.rep(string.char(0xff),6)..string.rep(mac,16)
+    local len = socket.send(sock, msg)
+    log.info("socket", "sendlen", len)
+    socket.close(sock)
+    return len == #msg, len
+end
+
+sys.taskInit(function()
+    log.info("wlan", "wlan_init:",  wlan.init())
+    wlan.setMode(wlan.STATION)
+    wlan.connect(wifiName,wifiPassword)
+    -- 等到成功获取ip就代表连上局域网了
+    local result, data = sys.waitUntil("IP_READY")
+    log.info("wlan", "IP_READY", result, data)
+
+
+    local mqttc = espmqtt.init({
+        uri = mqttAddr,
+        client_id = (esp32.getmac():toHex()),
+        username = mqttUser,
+        password = mqttPassword,
+    })
+    log.info("mqttc", mqttc)
+    if mqttc then
+        log.info("mqttc", "what happen")
+        local ok, err = espmqtt.start(mqttc)
+        log.info("mqttc", "start", ok, err)
+        if ok then
+            connected = true
+            while 1 do
+                log.info("mqttc", "wait ESPMQTT_EVT 30s")
+                local result, c, ret, topic, data = sys.waitUntil("ESPMQTT_EVT", 30000)
+                log.info("mqttc", result, c, ret)
+                if result == false then
+                    -- 没消息, 没动静
+                    log.info("mqttc", "wait timeout")
+                elseif ret == espmqtt.EVENT_DISCONNECTED then--断线了
+                    log.info("mqttc", "disconnected!!!")
+                    break
+                elseif c == mqttc then
+                    -- 是当前mqtt客户端的消息, 处理之
+                    if ret == espmqtt.EVENT_CONNECTED then
+                        -- 连接成功, 通常就是定义一些topic
+                        espmqtt.subscribe(mqttc, subscribeTopic)
+                    elseif ret == espmqtt.EVENT_DATA then
+                        -- 服务器来消息了, 如果data很长(超过4kb), 可能会分多次接收, 导致topic为空字符串
+                        log.info("mqttc", topic, data)
+                        if data == subscribePayload then--收到payload信息是开机
+                            LED_D5(1)
+                            log.info("poweron","发送开机请求啦!")
+                            wakeUp(pcMac)
+                            espmqtt.publish(mqttc, replyTopic, replyPayload)--回一条
+                            LED_D5(0)
+                        end
+                    else
+                        -- qos > 0 的订阅信息有响应, 会进这个分支
+                        -- 默认情况下mqtt是自动重连的, 不需要用户关心
+                        log.info("mqttc", "event", ret)
+                    end
+                else
+                    log.info("mqttc", "not this mqttc")
+                end
+            end
+            connected = false
+        else
+            log.warn("mqttc", "bad start", err)
+        end
+        espmqtt.destroy(mqttc)
+        log.warn("reboot", "device will reboot")
+        rtos.reboot()
+    end
+end)
+
+
+sys.run()