From 80c1ab74f98929a1408de3be32e04f6d3eedc729 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 25 Oct 2023 11:17:30 +0800 Subject: [PATCH 01/15] =?UTF-8?q?update:=20=E4=BD=BF=E7=94=A8uv=5Fhrtime?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E5=AE=9E=E7=8E=B0mcu.tick64()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_mcu_pc.c | 12 +++--------- src/main_mini.c | 8 +++++--- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/port/luat_mcu_pc.c b/port/luat_mcu_pc.c index 77ba682..e1bccf9 100644 --- a/port/luat_mcu_pc.c +++ b/port/luat_mcu_pc.c @@ -34,16 +34,10 @@ uint32_t luat_mcu_hz(void) { return 1; } +uint64_t uv_startup_ns; uint64_t luat_mcu_tick64(void) { - uv_timespec64_t ts; - int ret = uv_clock_gettime(UV_CLOCK_MONOTONIC, &ts); - if (ret) { - return 0; - } - uint64_t tmp = ts.tv_sec * 1000000L + ts.tv_nsec; - uint64_t tmp2 = boot_ts.tv_sec * 1000000L + boot_ts.tv_nsec; - //LLOGD("tick64 %llu", tmp - tmp2); - return tmp - tmp2; + uint64_t ns = uv_hrtime(); + return (ns - uv_startup_ns) / 1000; } int luat_mcu_us_period(void) { diff --git a/src/main_mini.c b/src/main_mini.c index 76834e6..964e746 100644 --- a/src/main_mini.c +++ b/src/main_mini.c @@ -21,7 +21,8 @@ uint8_t luavm_heap[LUAT_HEAP_SIZE] = {0}; int cmdline_argc; char** cmdline_argv; -uv_timespec64_t boot_ts; +// uv_timespec64_t boot_ts; +extern uint64_t uv_startup_ns; int lua_main (int argc, char **argv); @@ -55,7 +56,8 @@ int main(int argc, char** argv) { main_loop = malloc(sizeof(uv_loop_t)); // uv_replace_allocator(luat_heap_malloc, luat_heap_realloc, luat_heap_calloc, luat_heap_free); uv_loop_init(main_loop); - uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts); + // uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts); + uv_startup_ns = uv_hrtime(); uv_mutex_init(&timer_lock); luat_pcconf_init(); @@ -69,7 +71,7 @@ int main(int argc, char** argv) { uv_timer_t t; uv_timer_init(main_loop, &t); uv_timer_start(&t, timer_nop, 1000, 1000); - uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts); + // uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts); // uv_thread_create(&l_main, uv_luat_main, NULL); // uv_thread_join(&l_main); From 9012799e7811330970489ce5a7bf5ff112329d1b Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 25 Oct 2023 11:21:08 +0800 Subject: [PATCH 02/15] =?UTF-8?q?fix:=20luat=5Fmcu=5Ftick64=5Fms=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=94=99=E4=BA=86,=E5=BA=94=E8=AF=A5=E6=98=AFtick64/1?= =?UTF-8?q?000?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_mcu_pc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/port/luat_mcu_pc.c b/port/luat_mcu_pc.c index e1bccf9..921a47e 100644 --- a/port/luat_mcu_pc.c +++ b/port/luat_mcu_pc.c @@ -45,7 +45,7 @@ int luat_mcu_us_period(void) { } uint64_t luat_mcu_tick64_ms(void) { - return luat_mcu_tick64() / luat_mcu_us_period(); + return luat_mcu_tick64() / 1000; } void luat_mcu_set_clk_source(uint8_t source_main, uint8_t source_32k, uint32_t delay) { From 76ebafb9dde7cb4a532777225be820b9ba28f9d6 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 25 Oct 2023 12:05:33 +0800 Subject: [PATCH 03/15] =?UTF-8?q?update:=20=E4=BC=98=E5=8C=96log=E9=80=82?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_log_mini.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/port/luat_log_mini.c b/port/luat_log_mini.c index f56ce93..607e861 100644 --- a/port/luat_log_mini.c +++ b/port/luat_log_mini.c @@ -4,6 +4,7 @@ #include "luat_uart.h" #include "luat_malloc.h" #include "printf.h" +#include "luat_mcu.h" #include @@ -43,41 +44,47 @@ void luat_log_log(int level, const char* tag, const char* _fmt, ...) { if (luat_log_level_cur > level) return; char buff[LOGLOG_SIZE] = {0}; char *tmp = (char *)buff; + // uint64_t t = luat_mcu_tick64(); + // uint64_t sec = t / 1000 / 1000; + // uint32_t ms = (uint32_t)((t / 1000) % 1000); + // sprintf_(tmp, "[%08llu", sec); + // sprintf_(tmp + strlen(tmp), ".%03lu] ", ms); + // uint64_t s = t / 1000 / 1000; + // sprintf_(tmp, "[%08lu.%03lu] ", t / 1000, t % 1000); + // tmp += strlen(tmp); switch (level) { case LUAT_LOG_DEBUG: - buff[0] = 'D'; + tmp[0] = 'D'; break; case LUAT_LOG_INFO: - buff[0] = 'I'; + tmp[0] = 'I'; break; case LUAT_LOG_WARN: - buff[0] = 'W'; + tmp[0] = 'W'; break; case LUAT_LOG_ERROR: - buff[0] = 'E'; + tmp[0] = 'E'; break; default: - buff[0] = '?'; + tmp[0] = '?'; break; } - buff[1] = '/'; - tmp += 2; + tmp ++; + tmp[0] = '/'; + tmp ++; memcpy(tmp, tag, strlen(tag)); - buff[2+strlen(tag)] = ' '; - tmp += strlen(tag) + 1; + tmp += strlen(tag); + tmp[0] = ' '; + tmp ++; + size_t len = 0; va_list args; va_start(args, _fmt); - size_t len = vsnprintf_(tmp, LOGLOG_SIZE, _fmt, args); + len = vsnprintf_(tmp, LOGLOG_SIZE - strlen(buff), _fmt, args); va_end(args); if (len > 0) { len = strlen(buff); - // if (len > LOGLOG_SIZE - 2) - // len = LOGLOG_SIZE - 2; - // buff[len] = '\r'; - // buff[len+1] = '\n'; - // luat_nprint(buff, len+2); if (len > LOGLOG_SIZE - 1) len = LOGLOG_SIZE - 1; buff[len] = '\n'; From b5cce856d67ecc230713d1f851e886c7a7091605 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Sun, 29 Oct 2023 11:10:39 +0800 Subject: [PATCH 04/15] =?UTF-8?q?update:=20=E8=B0=83=E6=95=B4luat=5Fmsgbus?= =?UTF-8?q?=5Fget=E7=9A=84=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/rtos/luat_msgbus_pc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/port/rtos/luat_msgbus_pc.c b/port/rtos/luat_msgbus_pc.c index 9117192..f020b3e 100644 --- a/port/rtos/luat_msgbus_pc.c +++ b/port/rtos/luat_msgbus_pc.c @@ -45,7 +45,7 @@ uint32_t luat_msgbus_get(rtos_msg_t *msg, size_t timeout) int ret2 = 0; while (1) { - ret2 = uv_run(main_loop, UV_RUN_NOWAIT); + uv_mutex_lock(&m); ret = luat_queue_pop(&head, item); uv_mutex_unlock(&m); @@ -55,10 +55,7 @@ uint32_t luat_msgbus_get(rtos_msg_t *msg, size_t timeout) luat_heap_free(item); return 0; } - if (ret2 == 0) - uv_sleep(2); - else - uv_sleep(1); + ret2 = uv_run(main_loop, UV_RUN_ONCE); } return 1; } From 83ed56f3774efc39fef5200f91044fb5b1425c94 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Sun, 29 Oct 2023 12:46:38 +0800 Subject: [PATCH 05/15] =?UTF-8?q?fix:=20=E6=AD=A3=E7=A1=AE=E9=87=8A?= =?UTF-8?q?=E6=94=BEuv=5Fhandle=5Ft=E7=B3=BB=E5=88=97=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/luat_pcconf.h | 2 + port/luat_base_mini.c | 16 ++ port/luat_cmds.c | 288 ++++++++++++++++++++++ port/luat_fs_mini.c | 273 +------------------- port/network/luat_network_adapter_libuv.c | 30 ++- port/rtos/luat_rtos_timer_pc.c | 3 +- port/rtos/luat_timer_pc.c | 5 +- src/main_mini.c | 15 +- 8 files changed, 342 insertions(+), 290 deletions(-) create mode 100644 port/luat_cmds.c diff --git a/include/luat_pcconf.h b/include/luat_pcconf.h index 24377c8..6cafed8 100644 --- a/include/luat_pcconf.h +++ b/include/luat_pcconf.h @@ -26,4 +26,6 @@ void luat_pcconf_init(void); void luat_pcconf_save(void); +void free_uv_handle(void* ptr); + #endif diff --git a/port/luat_base_mini.c b/port/luat_base_mini.c index 85e0aeb..381ae09 100644 --- a/port/luat_base_mini.c +++ b/port/luat_base_mini.c @@ -2,6 +2,7 @@ #include "luat_msgbus.h" #include "luat_fs.h" #include "luat_timer.h" +#include "luat_malloc.h" #include #include @@ -156,3 +157,18 @@ void luat_ota_reboot(int timeout_ms) { luat_timer_mdelay(timeout_ms); exit(0); } + +///------------------------------------ + +#include "uv.h" + +static void on_free(uv_handle_t* ptr) { + luat_heap_free(ptr); +} + + +void free_uv_handle(void* ptr) { + if (ptr == NULL) + return; + uv_close((uv_handle_t*)ptr, on_free); +} diff --git a/port/luat_cmds.c b/port/luat_cmds.c new file mode 100644 index 0000000..0eb033b --- /dev/null +++ b/port/luat_cmds.c @@ -0,0 +1,288 @@ + +#include "luat_base.h" +#include "luat_fs.h" +#include "luat_malloc.h" + +#define LUAT_LOG_TAG "fs" +#include "luat_log.h" + +#include "dirent.h" + +extern const struct luat_vfs_filesystem vfs_fs_posix; +extern const struct luat_vfs_filesystem vfs_fs_luadb; +extern const struct luat_vfs_filesystem vfs_fs_ram; + +extern int cmdline_argc; +extern char **cmdline_argv; + + +char *luadb_ptr; +static size_t luadb_offset; +static int luadb_init(void) +{ + char *tmp = luat_heap_malloc(0x20); + size_t offset = 0; + // magic of luadb + tmp[offset + 0] = 0x01; + tmp[offset + 1] = 0x04; + tmp[offset + 2] = 0x5A; + tmp[offset + 3] = 0xA5; + tmp[offset + 4] = 0x5A; + tmp[offset + 5] = 0xA5; + offset += 6; + + // version 0x00 0x02 + tmp[offset + 0] = 0x02; + tmp[offset + 1] = 0x02; + tmp[offset + 2] = 0x02; + tmp[offset + 3] = 0x00; + offset += 4; + + // headers total size + tmp[offset + 0] = 0x03; + tmp[offset + 1] = 0x04; + tmp[offset + 2] = 0x00; + tmp[offset + 3] = 0x00; + tmp[offset + 4] = 0x00; + tmp[offset + 5] = 0x18; + offset += 6; + + // file count + tmp[offset + 0] = 0x04; + tmp[offset + 1] = 0x02; + tmp[offset + 2] = 0x00; + tmp[offset + 3] = 0x00; + offset += 4; + + // crc + tmp[offset + 0] = 0xFE; + tmp[offset + 1] = 0x02; + tmp[offset + 2] = 0x00; + tmp[offset + 3] = 0x00; + offset += 4; + + luadb_ptr = tmp; + luadb_offset = offset; + return 0; +} + +static int luadb_addfile(const char *name, char *data, size_t len) +{ + if (luadb_ptr == NULL) + { + luadb_init(); + } + if (luadb_ptr == NULL) + { + return -1; + } + size_t offset = luadb_offset; + char *tmp = luat_heap_realloc(luadb_ptr, luadb_offset + len + 512); + if (tmp == NULL) + { + return -2; + } + + // 如果是lua文件, 执行预处理 + + // 下面是文件了 + + // magic of file + tmp[offset + 0] = 0x01; + tmp[offset + 1] = 0x04; + tmp[offset + 2] = 0x5A; + tmp[offset + 3] = 0xA5; + tmp[offset + 4] = 0x5A; + tmp[offset + 5] = 0xA5; + offset += 6; + + // name of file + tmp[offset + 0] = 0x02; + tmp[offset + 1] = (uint8_t)(strlen(name) & 0xFF); + memcpy(tmp + offset + 2, name, strlen(name)); + offset += 2 + strlen(name); + + // len of file data + tmp[offset + 0] = 0x03; + tmp[offset + 1] = 0x04; + tmp[offset + 2] = (len >> 0) & 0xFF; + tmp[offset + 3] = (len >> 8) & 0xFF; + tmp[offset + 4] = (len >> 16) & 0xFF; + tmp[offset + 5] = (len >> 24) & 0xFF; + offset += 6; + + // crc + tmp[offset + 0] = 0xFE; + tmp[offset + 1] = 0x02; + tmp[offset + 2] = 0x00; + tmp[offset + 3] = 0x00; + offset += 4; + + memcpy(tmp + offset, data, len); + + offset += len; + + luadb_offset = offset; + luadb_ptr = tmp; + + // 调整文件数量, TODO 兼容256个以上的文件 + luadb_ptr[0x12]++; + + return 0; +} + +void *check_cmd_args(int index); +int luat_cmd_parse(int argc, char** argv) { + if (cmdline_argc == 1) + { + return 0; + } + for (size_t i = 1; i < (size_t)cmdline_argc; i++) + { + const char *argv = cmdline_argv[i]; + if (argv[0] == '-') { + continue; + } + check_cmd_args(i); + } + return 0; +} + +void *check_cmd_args(int index) +{ + size_t len = 0; + // int ret = 0; + void *ptr = NULL; + const char *path = cmdline_argv[index]; + if (strlen(path) < 4 || strlen(path) >= 512) + { + return NULL; + } + + if (!memcmp(path + strlen(path) - 4, ".bin", 4)) + { + FILE *f = fopen(path, "rb"); + if (!f) + { + LLOGE("无法打开luadb镜像文件 %s", path); + return NULL; + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + ptr = luat_heap_malloc(len); + if (ptr == NULL) + { + fclose(f); + LLOGE("luadb镜像文件太大,内存放不下 %s", path); + return NULL; + } + fread(ptr, len, 1, f); + fclose(f); + return ptr; + } + if (!memcmp(path + strlen(path) - 4, ".lua", 4)) + { + // LLOGD("把%s当做main.lua运行", path); + char tmpname[512] = {0}; + FILE *f = fopen(path, "rb"); + if (!f) + { + LLOGE("lua文件 %s", path); + return NULL; + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + // void* fptr = luat_heap_malloc(len); + char *tmp = luat_heap_malloc(len); + if (tmp == NULL) + { + fclose(f); + LLOGE("lua文件太大,内存放不下 %s", path); + return NULL; + } + fread(tmp, 1, len, f); + fclose(f); + for (size_t i = strlen(path); i > 0; i--) + { + if (path[i-1] == '/' || path[i-1] == '\\') { + memcpy(tmpname, path + i, strlen(path) - 1); + break; + } + } + if (tmpname[0] == 0x00) { + memcpy(tmpname, path, strlen(path)); + } + + // 开始合成luadb结构 + luadb_addfile(tmpname, tmp, len); + luat_heap_free(tmp); + return luadb_ptr; + } + // 目录模式 + if (!memcmp(path + strlen(path) - 1, "/", 1) || !memcmp(path + strlen(path) - 1, "\\", 1)) + { + DIR *dp; + struct dirent *ep; + // int index = 0; + FILE* f = NULL; + char buff[512] = {0}; + + // LLOGD("加载目录 %s", path); + #ifdef LUA_USE_WINDOWS + memcpy(buff, path, strlen(path)); + #else + memcpy(buff, path, strlen(path) - 1); + #endif; + dp = opendir(buff); + // LLOGD("目录打开 %p", dp); + if (dp != NULL) + { + // LLOGD("开始遍历目录 %s", path); + while ((ep = readdir(dp)) != NULL) + { + // LLOGD("文件/目录 %s %d", ep->d_name, ep->d_type); + if (ep->d_type != DT_REG) { + continue; + } + #ifdef LUA_USE_WINDOWS + sprintf(buff, "%s\\%s", path, ep->d_name); + #else + sprintf(buff, "%s/%s", path, ep->d_name); + #endif + // index++; + f = fopen(buff, "rb"); + if (f == NULL) { + LLOGW("打开文件失败,跳过 %s", buff); + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + char* tmp = luat_heap_malloc(len); + if (tmp == NULL) { + LLOGE("内存不足,无法加载文件 %s", buff); + } + else { + fread(tmp, 1, len, f); + } + fclose(f); + if (tmp) { + luadb_addfile(ep->d_name, tmp, len); + luat_heap_free(tmp); + } + // continue; + } + // LLOGD("遍历结束"); + (void)closedir(dp); + return luadb_ptr; + } + else + { + LLOGW("opendir file %s failed", path); + return NULL; + } + } + // LLOGD("啥模式都不是, 没法加载 %s", path); + return NULL; +} \ No newline at end of file diff --git a/port/luat_fs_mini.c b/port/luat_fs_mini.c index d3260d5..2b78462 100644 --- a/port/luat_fs_mini.c +++ b/port/luat_fs_mini.c @@ -14,6 +14,8 @@ extern const struct luat_vfs_filesystem vfs_fs_ram; extern int cmdline_argc; extern char **cmdline_argv; +extern char *luadb_ptr; + // 从命令行参数构建luadb void *build_luadb_from_cmd(void); @@ -45,7 +47,7 @@ int luat_fs_init(void) luat_fs_mount(&conf_ram); // 挂载虚拟的/luadb - void *ptr = build_luadb_from_cmd(); + void *ptr = luadb_ptr; if (ptr != NULL) { luat_fs_conf_t conf2 = { @@ -57,273 +59,4 @@ int luat_fs_init(void) luat_fs_mount(&conf2); } return 0; -} - -static char *luadb_ptr; -static size_t luadb_offset; -static int luadb_init(void) -{ - char *tmp = luat_heap_malloc(0x20); - size_t offset = 0; - // magic of luadb - tmp[offset + 0] = 0x01; - tmp[offset + 1] = 0x04; - tmp[offset + 2] = 0x5A; - tmp[offset + 3] = 0xA5; - tmp[offset + 4] = 0x5A; - tmp[offset + 5] = 0xA5; - offset += 6; - - // version 0x00 0x02 - tmp[offset + 0] = 0x02; - tmp[offset + 1] = 0x02; - tmp[offset + 2] = 0x02; - tmp[offset + 3] = 0x00; - offset += 4; - - // headers total size - tmp[offset + 0] = 0x03; - tmp[offset + 1] = 0x04; - tmp[offset + 2] = 0x00; - tmp[offset + 3] = 0x00; - tmp[offset + 4] = 0x00; - tmp[offset + 5] = 0x18; - offset += 6; - - // file count - tmp[offset + 0] = 0x04; - tmp[offset + 1] = 0x02; - tmp[offset + 2] = 0x00; - tmp[offset + 3] = 0x00; - offset += 4; - - // crc - tmp[offset + 0] = 0xFE; - tmp[offset + 1] = 0x02; - tmp[offset + 2] = 0x00; - tmp[offset + 3] = 0x00; - offset += 4; - - luadb_ptr = tmp; - luadb_offset = offset; - return 0; -} - -static int luadb_addfile(const char *name, char *data, size_t len) -{ - if (luadb_ptr == NULL) - { - luadb_init(); - } - if (luadb_ptr == NULL) - { - return -1; - } - size_t offset = luadb_offset; - char *tmp = luat_heap_realloc(luadb_ptr, luadb_offset + len + 512); - if (tmp == NULL) - { - return -2; - } - - // 下面是文件了 - - // magic of file - tmp[offset + 0] = 0x01; - tmp[offset + 1] = 0x04; - tmp[offset + 2] = 0x5A; - tmp[offset + 3] = 0xA5; - tmp[offset + 4] = 0x5A; - tmp[offset + 5] = 0xA5; - offset += 6; - - // name of file - tmp[offset + 0] = 0x02; - tmp[offset + 1] = (uint8_t)(strlen(name) & 0xFF); - memcpy(tmp + offset + 2, name, strlen(name)); - offset += 2 + strlen(name); - - // len of file data - tmp[offset + 0] = 0x03; - tmp[offset + 1] = 0x04; - tmp[offset + 2] = (len >> 0) & 0xFF; - tmp[offset + 3] = (len >> 8) & 0xFF; - tmp[offset + 4] = (len >> 16) & 0xFF; - tmp[offset + 5] = (len >> 24) & 0xFF; - offset += 6; - - // crc - tmp[offset + 0] = 0xFE; - tmp[offset + 1] = 0x02; - tmp[offset + 2] = 0x00; - tmp[offset + 3] = 0x00; - offset += 4; - - memcpy(tmp + offset, data, len); - - offset += len; - - luadb_offset = offset; - luadb_ptr = tmp; - - // 调整文件数量, TODO 兼容256个以上的文件 - luadb_ptr[0x12]++; - - return 0; -} - -void *check_cmd_args(int index); -void *build_luadb_from_cmd(void) { - if (cmdline_argc == 1) - { - return NULL; - } - for (size_t i = 1; i < (size_t)cmdline_argc; i++) - { - const char *argv = cmdline_argv[i]; - if (argv[0] == '-') { - continue; - } - check_cmd_args(i); - } - return luadb_ptr; -} - -void *check_cmd_args(int index) -{ - size_t len = 0; - // int ret = 0; - void *ptr = NULL; - const char *path = cmdline_argv[index]; - if (strlen(path) < 4 || strlen(path) >= 512) - { - return NULL; - } - - if (!memcmp(path + strlen(path) - 4, ".bin", 4)) - { - FILE *f = fopen(path, "rb"); - if (!f) - { - LLOGE("无法打开luadb镜像文件 %s", path); - return NULL; - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - ptr = luat_heap_malloc(len); - if (ptr == NULL) - { - fclose(f); - LLOGE("luadb镜像文件太大,内存放不下 %s", path); - return NULL; - } - fread(ptr, len, 1, f); - fclose(f); - return ptr; - } - if (!memcmp(path + strlen(path) - 4, ".lua", 4)) - { - // LLOGD("把%s当做main.lua运行", path); - char tmpname[512] = {0}; - FILE *f = fopen(path, "rb"); - if (!f) - { - LLOGE("lua文件 %s", path); - return NULL; - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - // void* fptr = luat_heap_malloc(len); - char *tmp = luat_heap_malloc(len); - if (tmp == NULL) - { - fclose(f); - LLOGE("lua文件太大,内存放不下 %s", path); - return NULL; - } - fread(tmp, 1, len, f); - fclose(f); - for (size_t i = strlen(path); i > 0; i--) - { - if (path[i-1] == '/' || path[i-1] == '\\') { - memcpy(tmpname, path + i, strlen(path) - 1); - break; - } - } - if (tmpname[0] == 0x00) { - memcpy(tmpname, path, strlen(path)); - } - - // 开始合成luadb结构 - luadb_addfile(tmpname, tmp, len); - luat_heap_free(tmp); - return luadb_ptr; - } - // 目录模式 - if (!memcmp(path + strlen(path) - 1, "/", 1) || !memcmp(path + strlen(path) - 1, "\\", 1)) - { - DIR *dp; - struct dirent *ep; - // int index = 0; - FILE* f = NULL; - char buff[512] = {0}; - - // LLOGD("加载目录 %s", path); - #ifdef LUA_USE_WINDOWS - memcpy(buff, path, strlen(path)); - #else - memcpy(buff, path, strlen(path) - 1); - #endif; - dp = opendir(buff); - // LLOGD("目录打开 %p", dp); - if (dp != NULL) - { - // LLOGD("开始遍历目录 %s", path); - while ((ep = readdir(dp)) != NULL) - { - // LLOGD("文件/目录 %s %d", ep->d_name, ep->d_type); - if (ep->d_type != DT_REG) { - continue; - } - #ifdef LUA_USE_WINDOWS - sprintf(buff, "%s\\%s", path, ep->d_name); - #else - sprintf(buff, "%s/%s", path, ep->d_name); - #endif - // index++; - f = fopen(buff, "rb"); - if (f == NULL) { - LLOGW("打开文件失败,跳过 %s", buff); - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - char* tmp = luat_heap_malloc(len); - if (tmp == NULL) { - LLOGE("内存不足,无法加载文件 %s", buff); - } - else { - fread(tmp, 1, len, f); - } - fclose(f); - if (tmp) { - luadb_addfile(ep->d_name, tmp, len); - luat_heap_free(tmp); - } - // continue; - } - // LLOGD("遍历结束"); - (void)closedir(dp); - return luadb_ptr; - } - else - { - LLOGW("opendir file %s failed", path); - return NULL; - } - } - // LLOGD("啥模式都不是, 没法加载 %s", path); - return NULL; } \ No newline at end of file diff --git a/port/network/luat_network_adapter_libuv.c b/port/network/luat_network_adapter_libuv.c index 1bc8ad0..caee443 100644 --- a/port/network/luat_network_adapter_libuv.c +++ b/port/network/luat_network_adapter_libuv.c @@ -6,6 +6,7 @@ #include "luat_malloc.h" #include "printf.h" #include "luat_msgbus.h" +#include "luat_pcconf.h" #include "luat_network_adapter.h" @@ -134,16 +135,17 @@ static inline int set_socket_state(int socket_id, int state) { typedef struct task_event_async { - uv_async_t async; + // uv_async_t async; OS_EVENT event; luat_network_cb_param_t param; }task_event_async_t; static void cb_nw_task_async(uv_async_t *async) { - task_event_async_t* e = (task_event_async_t*)async; + task_event_async_t* e = (task_event_async_t*)async->data; ctrl.socket_cb(&e->event, &e->param); - luat_heap_free(async); + luat_heap_free(e); + free_uv_handle(async); } static void cb_to_nw_task(uint32_t event_id, uint32_t param1, uint32_t param2, uint32_t param3) @@ -154,10 +156,13 @@ static void cb_to_nw_task(uint32_t event_id, uint32_t param1, uint32_t param2, u LLOGE("out of memory when malloc cb_to_nw_task async ctx"); return; } - ret = uv_async_init(main_loop, &e->async, cb_nw_task_async); + memset(e, 0, sizeof(task_event_async_t)); + uv_async_t* async = luat_heap_malloc(sizeof(uv_async_t));; + ret = uv_async_init(main_loop, async, cb_nw_task_async); if (ret) { LLOGE("uv_async_init cb_to_nw_task %d", ret); luat_heap_free(e); + free_uv_handle(async); return; } OS_EVENT event = {.ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3}; @@ -170,7 +175,8 @@ static void cb_to_nw_task(uint32_t event_id, uint32_t param1, uint32_t param2, u } memcpy(&e->param, ¶m, sizeof(luat_network_cb_param_t)); LLOGD("socket[%d] 发送nw_task消息 %08X %016X", param1 & 0xFF, e->event.ID, param.tag); - uv_async_send(&e->async); + async->data = e; + uv_async_send(async); } static int libuv_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data); @@ -427,7 +433,7 @@ static void udp_connect_async(uv_async_t *async) // ret = uv_udp_connect(&sockets[socket_id].udp, (const struct sockaddr *)&c->addr); // memcpy(&sockets[socket_id].remote, (const struct sockaddr *)&c->addr, sizeof(const struct sockaddr)); on_connect(&sockets[socket_id].udp, ret); - luat_heap_free(async); + free_uv_handle(async); } // 作为client绑定一个port,并连接remote_ip和remote_port对应的server @@ -540,7 +546,7 @@ static void on_shutdown(uv_shutdown_t *handle) static void udp_async_close(uv_async_t *handle) { int socket_id = (int)handle->data; - luat_heap_free(handle); + free_uv_handle(handle); on_close(&sockets[socket_id].udp); } @@ -569,7 +575,7 @@ static int close_socket(int socket_id, const char *tag) uv_async_init(main_loop, async, udp_async_close); ret = uv_async_send(async); if (ret) { - luat_heap_free(async); + free_uv_handle(async); LLOGI("socket[%d] uv_async_send %d %s", socket_id, ret, uv_err_name(ret)); set_socket_state(socket_id, CLOSED); } @@ -697,6 +703,7 @@ static void on_sent(uv_write_t *req, int status) memcpy(&len, tmp, 4); int socket_id = (int32_t)req->data; LLOGD("socket[%d] tcp sent %d %d", socket_id, status, len); + luat_heap_free(req); if (status == 0) { @@ -705,10 +712,9 @@ static void on_sent(uv_write_t *req, int status) } else { - // LLOGD("发送成功, 执行ERROR消息"); + // LLOGD("发送失败, 执行ERROR消息"); cb_to_nw_task(EV_NW_SOCKET_ERROR, socket_id, 0, sockets[socket_id].param); } - luat_heap_free(req); } static void on_sent_udp(uv_udp_send_t *req, int status) @@ -761,7 +767,7 @@ static int libuv_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, ui // LLOGD("待发送的内容 %.*s", len, buf); if (sockets[socket_id].is_tcp) { - req = luat_heap_malloc(sizeof(uv_write_t)); + req = luat_heap_malloc(sizeof(uv_write_t) + 4); memset(req, 0, sizeof(uv_write_t)); tmp = (char *)req; tmp += sizeof(uv_write_t); @@ -1051,7 +1057,7 @@ static int l_ip_ready(lua_State *L, void *ptr) static void ip_ready_timer_cb(uv_timer_t *t) { rtos_msg_t msg = {0}; - luat_heap_free(t); + free_uv_handle(t); msg.handler = l_ip_ready; msg.arg1 = 1; luat_msgbus_put(&msg, 0); diff --git a/port/rtos/luat_rtos_timer_pc.c b/port/rtos/luat_rtos_timer_pc.c index 71ed7d8..999f3f2 100644 --- a/port/rtos/luat_rtos_timer_pc.c +++ b/port/rtos/luat_rtos_timer_pc.c @@ -1,6 +1,7 @@ #include "luat_base.h" #include "luat_rtos.h" #include "luat_malloc.h" +#include "luat_pcconf.h" #include "uv.h" @@ -81,7 +82,7 @@ void luat_release_rtos_timer(void *timer) { uv_timer_t *t = (uv_timer_t *)timer; uv_timer_stop(t); luat_heap_free(t->data); - luat_heap_free(t); + free_uv_handle(t); uv_mutex_unlock(&timer_lock); } diff --git a/port/rtos/luat_timer_pc.c b/port/rtos/luat_timer_pc.c index 06ac79b..dbb3dd7 100644 --- a/port/rtos/luat_timer_pc.c +++ b/port/rtos/luat_timer_pc.c @@ -3,6 +3,7 @@ #include "luat_msgbus.h" #include "luat_malloc.h" #include "luat_timer.h" +#include "luat_pcconf.h" #include "uv.h" @@ -106,7 +107,7 @@ int luat_timer_start(luat_timer_t *timer) ret = uv_timer_init(main_loop, timer_req); if (ret) { LLOGE("uv_timer_init %d", ret); - luat_heap_free(timers[id]); + free_uv_handle(timers[id]); timers[id] = NULL; uv_mutex_unlock(&timer_lock); return -1; @@ -136,7 +137,7 @@ int luat_timer_stop(luat_timer_t *timer) ret = uv_timer_stop(timer_req); if (ret) LLOGI("uv_timer_stop %d", ret); - luat_heap_free(timer_req); + free_uv_handle(timer_req); timers[i] = NULL; uv_mutex_unlock(&timer_lock); return 0; diff --git a/src/main_mini.c b/src/main_mini.c index 964e746..6a16e39 100644 --- a/src/main_mini.c +++ b/src/main_mini.c @@ -24,7 +24,7 @@ char** cmdline_argv; // uv_timespec64_t boot_ts; extern uint64_t uv_startup_ns; -int lua_main (int argc, char **argv); +int lua_main (int argc, char** argv); void luat_log_init_win32(void); void luat_uart_initial_win32(void); @@ -33,6 +33,8 @@ void luat_network_init(void); uv_loop_t *main_loop; uv_mutex_t timer_lock; +int luat_cmd_parse(int argc, char** argv); + void uv_luat_main(void* args) { (void)args; // printf("cmdline_argc %d\n", cmdline_argc); @@ -53,6 +55,7 @@ static void timer_nop(uv_timer_t *handle) { int main(int argc, char** argv) { cmdline_argc = argc; cmdline_argv = argv; + main_loop = malloc(sizeof(uv_loop_t)); // uv_replace_allocator(luat_heap_malloc, luat_heap_realloc, luat_heap_calloc, luat_heap_free); uv_loop_init(main_loop); @@ -67,15 +70,17 @@ int main(int argc, char** argv) { luat_fs_init(); luat_network_init(); + + int ret = luat_cmd_parse(cmdline_argc, cmdline_argv); + if (ret) { + return ret; + } + // uv_thread_t l_main; uv_timer_t t; uv_timer_init(main_loop, &t); uv_timer_start(&t, timer_nop, 1000, 1000); - // uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts); - // uv_thread_create(&l_main, uv_luat_main, NULL); - // uv_thread_join(&l_main); - // uv_run(main_loop, UV_RUN_DEFAULT); uv_luat_main(NULL); uv_loop_close(main_loop); From b20e0a70c0f6a6e6e3e908cb162389cc692627f8 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Mon, 30 Oct 2023 14:57:52 +0800 Subject: [PATCH 06/15] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0GUI=E6=94=AF?= =?UTF-8?q?=E6=8C=81,=20lcd/lvgl=E5=BA=93=E5=8F=AF=E7=94=A8=E4=BA=86,=20?= =?UTF-8?q?=E5=9F=BA=E4=BA=8ESDL2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build_windows_32bit_msvc.bat | 1 + build_windows_32bit_msvc_gui.bat | 6 ++ build_windows_64bit_msvc.bat | 1 + build_windows_64bit_msvc_gui.bat | 6 ++ include/luat_conf_bsp.h | 105 +++++++++++++++++++++++++++++++ port/{ => driver}/luat_gpio_pc.c | 0 port/driver/luat_i2c_pc.c | 34 ++++++++++ port/{ => driver}/luat_mcu_pc.c | 0 port/{ => driver}/luat_pm_pc.c | 0 port/{ => driver}/luat_rtc_pc.c | 0 port/driver/luat_spi_device.c | 49 +++++++++++++++ port/driver/luat_spi_pc.c | 84 +++++++++++++++++++++++++ port/luat_base_mini.c | 20 ++++++ port/luat_pcconf_pc.c | 6 +- src/main_mini.c | 32 ++++++++++ ui/luat_u8g2_pc.c | 29 +++++++++ xmake.lua | 37 ++++++++++- 17 files changed, 407 insertions(+), 3 deletions(-) create mode 100644 build_windows_32bit_msvc_gui.bat create mode 100644 build_windows_64bit_msvc_gui.bat rename port/{ => driver}/luat_gpio_pc.c (100%) create mode 100644 port/driver/luat_i2c_pc.c rename port/{ => driver}/luat_mcu_pc.c (100%) rename port/{ => driver}/luat_pm_pc.c (100%) rename port/{ => driver}/luat_rtc_pc.c (100%) create mode 100644 port/driver/luat_spi_device.c create mode 100644 port/driver/luat_spi_pc.c create mode 100644 ui/luat_u8g2_pc.c diff --git a/build_windows_32bit_msvc.bat b/build_windows_32bit_msvc.bat index 39e1439..2283e8f 100644 --- a/build_windows_32bit_msvc.bat +++ b/build_windows_32bit_msvc.bat @@ -1,5 +1,6 @@ xmake clean -a set VM_64bit=0 +set LUAT_USE_GUI=n xmake f -a x86 -y xmake -y -w -v diff --git a/build_windows_32bit_msvc_gui.bat b/build_windows_32bit_msvc_gui.bat new file mode 100644 index 0000000..21bd16c --- /dev/null +++ b/build_windows_32bit_msvc_gui.bat @@ -0,0 +1,6 @@ + +xmake clean -a +set VM_64bit=0 +set LUAT_USE_GUI=y +xmake f -a x86 -y +xmake -y -w -v diff --git a/build_windows_64bit_msvc.bat b/build_windows_64bit_msvc.bat index cf0623c..72ce421 100644 --- a/build_windows_64bit_msvc.bat +++ b/build_windows_64bit_msvc.bat @@ -1,5 +1,6 @@ xmake clean -a set VM_64bit=1 +set LUAT_USE_GUI=n xmake f -a x86 -y xmake -y -w -v diff --git a/build_windows_64bit_msvc_gui.bat b/build_windows_64bit_msvc_gui.bat new file mode 100644 index 0000000..72ce421 --- /dev/null +++ b/build_windows_64bit_msvc_gui.bat @@ -0,0 +1,6 @@ + +xmake clean -a +set VM_64bit=1 +set LUAT_USE_GUI=n +xmake f -a x86 -y +xmake -y -w -v diff --git a/include/luat_conf_bsp.h b/include/luat_conf_bsp.h index f2b564b..440ab05 100644 --- a/include/luat_conf_bsp.h +++ b/include/luat_conf_bsp.h @@ -27,6 +27,19 @@ #define LUAT_USE_SNTP 1 #define LUAT_USE_TLS 1 +#define LV_HOR_RES_MAX (2000) +#define LV_VER_RES_MAX (2000) +#define LV_COLOR_DEPTH 16 + +#define LV_COLOR_16_SWAP 1 +#define LUAT_LCD_COLOR_DEPTH 16 +#define LUAT_USE_LVGL_SDL2 1 +#define LUAT_USE_LCD_SDL2 1 +#define LUAT_USE_LCD_CUSTOM_DRAW 1 +#define LV_MEM_CUSTOM 1 +// #define LV_USE_LOG 1 +// #define LUAT_LV_DEBUG 1 + //---------------------------- // 外设,按需启用, 最起码启用uart和wdt库 #define LUAT_USE_UART 1 @@ -90,4 +103,96 @@ #define LUAT_USE_BIT64 1 #define LUAT_USE_FASTLZ 1 +//-------------------------------------------------- +// GUI相关 +//-------------------------------------------------- +// 这里需要从环境变量里启用,不能直接修改下面的宏 +// #define LUAT_USE_GUI 1 + +#ifdef LUAT_USE_GUI +//--------------------- +// UI +// LCD 是彩屏, 若使用LVGL就必须启用LCD +#define LUAT_USE_LCD +#define LUAT_USE_TJPGD +// EINK 是墨水屏 +// #define LUAT_USE_EINK + +//--------------------- +// U8G2 +// 单色屏, 支持i2c/spi +// #define LUAT_USE_DISP +// #define LUAT_USE_U8G2 + +/**************FONT*****************/ +#define LUAT_USE_FONTS +/**********U8G2&LCD&EINK FONT*************/ +#define USE_U8G2_OPPOSANSM_ENGLISH 1 +#define USE_U8G2_OPPOSANSM8_CHINESE +#define USE_U8G2_OPPOSANSM10_CHINESE +#define USE_U8G2_OPPOSANSM12_CHINESE +#define USE_U8G2_OPPOSANSM16_CHINESE +// #define USE_U8G2_OPPOSANSM24_CHINESE +// #define USE_U8G2_OPPOSANSM32_CHINESE +// SARASA +#define USE_U8G2_SARASA_ENGLISH +#define USE_U8G2_SARASA_M8_CHINESE +#define USE_U8G2_SARASA_M10_CHINESE +#define USE_U8G2_SARASA_M12_CHINESE +#define USE_U8G2_SARASA_M14_CHINESE +#define USE_U8G2_SARASA_M16_CHINESE +// #define USE_U8G2_SARASA_M18_CHINESE +// #define USE_U8G2_SARASA_M20_CHINESE +// #define USE_U8G2_SARASA_M22_CHINESE +// #define USE_U8G2_SARASA_M24_CHINESE +// #define USE_U8G2_SARASA_M26_CHINESE +// #define USE_U8G2_SARASA_M28_CHINESE +/**********LVGL FONT*************/ +#define LV_FONT_OPPOSANS_M_8 +#define LV_FONT_OPPOSANS_M_10 +#define LV_FONT_OPPOSANS_M_12 +#define LV_FONT_OPPOSANS_M_16 + +//--------------------- +// LVGL +// 主推的UI库, 功能强大但API繁琐 +#define LUAT_USE_LVGL 1 + +#define LUAT_USE_LVGL_INDEV 1 // 输入设备 + +#define LUAT_USE_LVGL_ARC //圆弧 无依赖 +#define LUAT_USE_LVGL_BAR //进度条 无依赖 +#define LUAT_USE_LVGL_BTN //按钮 依赖容器CONT +#define LUAT_USE_LVGL_BTNMATRIX //按钮矩阵 无依赖 +#define LUAT_USE_LVGL_CALENDAR //日历 无依赖 +#define LUAT_USE_LVGL_CANVAS //画布 依赖图片IMG +#define LUAT_USE_LVGL_CHECKBOX //复选框 依赖按钮BTN 标签LABEL +#define LUAT_USE_LVGL_CHART //图表 无依赖 +#define LUAT_USE_LVGL_CONT //容器 无依赖 +#define LUAT_USE_LVGL_CPICKER //颜色选择器 无依赖 +#define LUAT_USE_LVGL_DROPDOWN //下拉列表 依赖页面PAGE 标签LABEL +#define LUAT_USE_LVGL_GAUGE //仪表 依赖进度条BAR 仪表(弧形刻度)LINEMETER +#define LUAT_USE_LVGL_IMG //图片 依赖标签LABEL +#define LUAT_USE_LVGL_IMGBTN //图片按钮 依赖按钮BTN +#define LUAT_USE_LVGL_KEYBOARD //键盘 依赖图片按钮IMGBTN +#define LUAT_USE_LVGL_LABEL //标签 无依赖 +#define LUAT_USE_LVGL_LED //LED 无依赖 +#define LUAT_USE_LVGL_LINE //线 无依赖 +#define LUAT_USE_LVGL_LIST //列表 依赖页面PAGE 按钮BTN 标签LABEL +#define LUAT_USE_LVGL_LINEMETER //仪表(弧形刻度) 无依赖 +#define LUAT_USE_LVGL_OBJMASK //对象蒙版 无依赖 +#define LUAT_USE_LVGL_MSGBOX //消息框 依赖图片按钮IMGBTN 标签LABEL +#define LUAT_USE_LVGL_PAGE //页面 依赖容器CONT +#define LUAT_USE_LVGL_SPINNER //旋转器 依赖圆弧ARC 动画ANIM +#define LUAT_USE_LVGL_ROLLER //滚筒 无依赖 +#define LUAT_USE_LVGL_SLIDER //滑杆 依赖进度条BAR +#define LUAT_USE_LVGL_SPINBOX //数字调整框 无依赖 +#define LUAT_USE_LVGL_SWITCH //开关 依赖滑杆SLIDER +#define LUAT_USE_LVGL_TEXTAREA //文本框 依赖标签LABEL 页面PAGE +#define LUAT_USE_LVGL_TABLE //表格 依赖标签LABEL +#define LUAT_USE_LVGL_TABVIEW //页签 依赖页面PAGE 图片按钮IMGBTN +#define LUAT_USE_LVGL_TILEVIEW //平铺视图 依赖页面PAGE +#define LUAT_USE_LVGL_WIN //窗口 依赖容器CONT 按钮BTN 标签LABEL 图片IMG 页面PAGE +#endif + #endif diff --git a/port/luat_gpio_pc.c b/port/driver/luat_gpio_pc.c similarity index 100% rename from port/luat_gpio_pc.c rename to port/driver/luat_gpio_pc.c diff --git a/port/driver/luat_i2c_pc.c b/port/driver/luat_i2c_pc.c new file mode 100644 index 0000000..2fc0297 --- /dev/null +++ b/port/driver/luat_i2c_pc.c @@ -0,0 +1,34 @@ + +#include "luat_base.h" +#include "luat_i2c.h" + +#define LUAT_LOG_TAG "luat.i2c" +#include "luat_log.h" + +int luat_i2c_exist(int id) { + return id == 0; +} + +int luat_i2c_setup(int id, int speed) { + return 0; +} + +int luat_i2c_close(int id) { + return 0; +} + +int luat_i2c_send(int id, int addr, void* buff, size_t len, uint8_t stop) { + return 0; +} + +int luat_i2c_recv(int id, int addr, void* buff, size_t len) { + return 0; +} + +int luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len) { + return 0; +} + +int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len, uint16_t Toms, void *CB, void *pParam) { + return 0; +} diff --git a/port/luat_mcu_pc.c b/port/driver/luat_mcu_pc.c similarity index 100% rename from port/luat_mcu_pc.c rename to port/driver/luat_mcu_pc.c diff --git a/port/luat_pm_pc.c b/port/driver/luat_pm_pc.c similarity index 100% rename from port/luat_pm_pc.c rename to port/driver/luat_pm_pc.c diff --git a/port/luat_rtc_pc.c b/port/driver/luat_rtc_pc.c similarity index 100% rename from port/luat_rtc_pc.c rename to port/driver/luat_rtc_pc.c diff --git a/port/driver/luat_spi_device.c b/port/driver/luat_spi_device.c new file mode 100644 index 0000000..dcafb97 --- /dev/null +++ b/port/driver/luat_spi_device.c @@ -0,0 +1,49 @@ +#include "luat_base.h" +#include "luat_gpio.h" +#include "luat_spi.h" + +#define LUAT_SPI_CS_SELECT 0 +#define LUAT_SPI_CS_CLEAR 1 + + +// luat_spi_device_t* 在lua层看到的是一个userdata +int luat_spi_device_setup(luat_spi_device_t* spi_dev) { + luat_spi_bus_setup(spi_dev); + luat_gpio_mode(spi_dev->spi_config.cs, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // CS + return 0; +} + +//关闭SPI设备,成功返回0 +int luat_spi_device_close(luat_spi_device_t* spi_dev) { + return luat_spi_close(spi_dev->bus_id); +} + +//收发SPI数据,返回接收字节数 +int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) { + luat_spi_device_config(spi_dev); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT); + int ret = luat_spi_transfer(spi_dev->bus_id, send_buf, send_length, recv_buf, recv_length); + // int ret = luat_spi_send(spi_dev->bus_id, send_buf, send_length); + // ret = luat_spi_recv(spi_dev->bus_id, recv_buf, recv_length); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR); + return ret; +} + +//收SPI数据,返回接收字节数 +int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length) { + luat_spi_device_config(spi_dev); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT); + int ret = luat_spi_recv(spi_dev->bus_id, recv_buf, length); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR); + return ret; +} + +//发SPI数据,返回发送字节数 +int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length) { + luat_spi_device_config(spi_dev); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT); + int ret = luat_spi_send(spi_dev->bus_id, send_buf, length); + luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR); + return ret; +} + diff --git a/port/driver/luat_spi_pc.c b/port/driver/luat_spi_pc.c new file mode 100644 index 0000000..dbb2be0 --- /dev/null +++ b/port/driver/luat_spi_pc.c @@ -0,0 +1,84 @@ + +#include "luat_base.h" +#include "luat_gpio.h" +#include "luat_msgbus.h" +#include "luat_spi.h" + +// 模拟SPI在win32下的实现 +// TODO 当需要返回数据时, 调用lua方法获取需要返回的数据 + +#define LUAT_WIN32_SPI_COUNT (3) + +typedef struct win32spi { + luat_spi_t spi; + uint8_t open; +}win32spi_t; + +win32spi_t win32spis[LUAT_WIN32_SPI_COUNT] = {0}; + +int luat_spi_device_config(luat_spi_device_t* spi_dev){ + return 0; +} + +int luat_spi_bus_setup(luat_spi_device_t* spi_dev){ + int bus_id = spi_dev->bus_id; + if (bus_id < 0 || bus_id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + memcpy(&win32spis[bus_id].spi, &(spi_dev->spi_config), sizeof(luat_spi_t)); + win32spis[bus_id].open = 1; + return 0; +} + +int luat_spi_setup(luat_spi_t* spi) { + if (spi->id < 0 || spi->id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + memcpy(&win32spis[spi->id].spi, spi, sizeof(luat_spi_t)); + win32spis[spi->id].open = 1; + return 0; +} +//关闭SPI,成功返回0 +int luat_spi_close(int spi_id) { + if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + win32spis[spi_id].open = 0; + return 0; +} +//收发SPI数据,返回接收字节数 +int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) { + if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + if (win32spis[spi_id].open == 0) + return -1; + memset(recv_buf, 0, recv_length); + return recv_length; +} +//收SPI数据,返回接收字节数 +int luat_spi_recv(int spi_id, char* recv_buf, size_t length) { + if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + if (win32spis[spi_id].open == 0) + return -1; + memset(recv_buf, 0, length); + return length; +} +//发SPI数据,返回发送字节数 +int luat_spi_send(int spi_id, const char* send_buf, size_t length) { + if (spi_id < 0 || spi_id >= LUAT_WIN32_SPI_COUNT) { + return -1; + } + if (win32spis[spi_id].open == 0) + return -1; + return length; +} + +int luat_spi_change_speed(int spi_id, uint32_t speed){ + return 0; +} + + + diff --git a/port/luat_base_mini.c b/port/luat_base_mini.c index 381ae09..b72d640 100644 --- a/port/luat_base_mini.c +++ b/port/luat_base_mini.c @@ -117,6 +117,26 @@ static const luaL_Reg loadedlibs[] = { {"websocket", luaopen_websocket}, // {"ftp", luaopen_ftp}, {"errDump", luaopen_errdump}, +#endif +// UI类 +#ifdef LUAT_USE_DISP + {"disp", luaopen_disp}, // OLED显示模块 +#endif +#ifdef LUAT_USE_U8G2 + {"u8g2", luaopen_u8g2}, // u8g2 +#endif + +#ifdef LUAT_USE_EINK + {"eink", luaopen_eink}, // 电子墨水屏 +#endif +#ifdef LUAT_USE_FATFS + {"fatfs", luaopen_fatfs}, // SD卡/tf卡 +#endif +#ifdef LUAT_USE_LVGL + {"lvgl", luaopen_lvgl}, +#endif +#ifdef LUAT_USE_LCD + {"lcd", luaopen_lcd}, #endif {NULL, NULL} }; diff --git a/port/luat_pcconf_pc.c b/port/luat_pcconf_pc.c index c54e3fb..4b4855a 100644 --- a/port/luat_pcconf_pc.c +++ b/port/luat_pcconf_pc.c @@ -13,10 +13,15 @@ #include "luat_log.h" luat_pcconf_t g_pcconf; +extern uv_loop_t *main_loop; extern const luat_uart_drv_opts_t* uart_drvs[]; extern const luat_uart_drv_opts_t uart_udp; void luat_pcconf_init(void) { + #ifdef LUAT_USE_LVGL + + #endif + memcpy(g_pcconf.mcu_unique_id, "LuatOS@PC", strlen("LuatOS@PC")); g_pcconf.mcu_unique_id_len = strlen("LuatOS@PC"); @@ -28,4 +33,3 @@ void luat_pcconf_init(void) { void luat_pcconf_save(void) { } - diff --git a/src/main_mini.c b/src/main_mini.c index 6a16e39..730071b 100644 --- a/src/main_mini.c +++ b/src/main_mini.c @@ -16,6 +16,11 @@ #include "uv.h" +#ifdef LUAT_USE_LVGL +uv_timer_t lvgl_timer; +#include "lvgl.h" +#endif + #define LUAT_HEAP_SIZE (1024*1024) uint8_t luavm_heap[LUAT_HEAP_SIZE] = {0}; @@ -34,6 +39,8 @@ uv_loop_t *main_loop; uv_mutex_t timer_lock; int luat_cmd_parse(int argc, char** argv); +static int luat_lvg_handler(lua_State* L, void* ptr); +static void lvgl_timer_cb(uv_timer_t* lvgl_timer); void uv_luat_main(void* args) { (void)args; @@ -76,6 +83,12 @@ int main(int argc, char** argv) { return ret; } + #ifdef LUAT_USE_LVGL + lv_init(); + uv_timer_init(main_loop, &lvgl_timer); + uv_timer_start(&lvgl_timer, lvgl_timer_cb, 25, 25); + #endif + // uv_thread_t l_main; uv_timer_t t; uv_timer_init(main_loop, &t); @@ -87,3 +100,22 @@ int main(int argc, char** argv) { free(main_loop); return 0; } + +// UI相关 + +#ifdef LUAT_USE_LVGL +static int luat_lvg_handler(lua_State* L, void* ptr) { + (void)L; + (void)ptr; + lv_tick_inc(25); + lv_task_handler(); + return 0; +} +static void lvgl_timer_cb(uv_timer_t* lvgl_timer) { + rtos_msg_t msg = { + .handler = luat_lvg_handler + }; + luat_msgbus_put(&msg, 0); +} +#endif + diff --git a/ui/luat_u8g2_pc.c b/ui/luat_u8g2_pc.c new file mode 100644 index 0000000..1450140 --- /dev/null +++ b/ui/luat_u8g2_pc.c @@ -0,0 +1,29 @@ +#include "luat_base.h" +#include "u8g2.h" +#include "luat_u8g2.h" + +uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); +uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); +uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); + +uint8_t u8x8_luat_gpio_and_delay_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); +uint8_t u8x8_luat_byte_hw_i2c_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); +uint8_t u8x8_luat_byte_4wire_hw_spi_default(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr); + +int luat_u8g2_setup_default(luat_u8g2_conf_t *conf); + +int luat_u8g2_setup(luat_u8g2_conf_t *conf) { + return luat_u8g2_setup_default(conf); +} + +uint8_t u8x8_luat_gpio_and_delay(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + return u8x8_luat_gpio_and_delay_default(u8x8, msg, arg_int, arg_ptr); +} + +uint8_t u8x8_luat_byte_hw_i2c(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + return u8x8_luat_byte_hw_i2c_default(u8x8, msg, arg_int, arg_ptr); +} + +uint8_t u8x8_luat_byte_4wire_hw_spi(u8x8_t *u8x8, uint8_t msg, uint8_t arg_int, void *arg_ptr) { + return u8x8_luat_byte_4wire_hw_spi_default(u8x8, msg, arg_int, arg_ptr); +} diff --git a/xmake.lua b/xmake.lua index 4ca3533..dcc622e 100644 --- a/xmake.lua +++ b/xmake.lua @@ -26,8 +26,11 @@ if os.getenv("VM_64bit") == "1" then add_defines("LUAT_CONF_VM_64bit") end --- add_requires("libsdl") --- add_packages("libsdl") +if os.getenv("LUAT_USE_GUI") == "y" then + add_defines("LUAT_USE_GUI=1") + add_requires("libsdl") + add_packages("libsdl") +end if is_host("windows") then -- add_defines("LUA_USE_WINDOWS") @@ -188,4 +191,34 @@ target("luatos-lua") add_includedirs(luatos.."components/network/errdump",{public = true}) add_files(luatos.."components/network/errdump/*.c") + if os.getenv("LUAT_USE_GUI") == "y" then + add_files("ui/*.c") + + -- sdl2 + add_includedirs(luatos.."components/ui/sdl2") + add_files(luatos.."components/ui/sdl2/*.c") + -- u8g2 + add_includedirs(luatos.."components/u8g2") + add_files(luatos.."components/u8g2/*.c") + -- lcd + add_includedirs(luatos.."components/lcd") + add_files(luatos.."components/lcd/*.c") + -- lvgl + add_includedirs(luatos.."components/lvgl") + add_includedirs(luatos.."components/lvgl/binding") + add_includedirs(luatos.."components/lvgl/gen") + add_includedirs(luatos.."components/lvgl/src") + add_includedirs(luatos.."components/lvgl/font") + add_includedirs(luatos.."components/lvgl/src/lv_font") + add_includedirs(luatos.."components/lvgl/sdl2") + add_files(luatos.."components/lvgl/**.c") + -- 默认不编译lv的demos, 节省大量的编译时间 + remove_files(luatos.."components/lvgl/lv_demos/**.c") + + -- qrcode 和 tjpgd + add_includedirs(luatos.."components/qrcode") + add_includedirs(luatos.."components/tjpgd") + add_files(luatos.."components/tjpgd/*.c") + add_files(luatos.."components/qrcode/*.c") + end target_end() From 11c22045e4001bf4ba8de9d19f730459150e4112 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Mon, 30 Oct 2023 15:22:47 +0800 Subject: [PATCH 07/15] =?UTF-8?q?update:=20=E6=9B=B4=E6=96=B0=E5=B7=B2?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=9A=84=E5=BA=93=E7=9A=84=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 472962c..adbd535 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,22 @@ * [x] 支持windows编译,运行 * [x] 支持linux编译,运行 -* [ ] 支持macos编译,运行 -- 理论上支持,没有测试环境 +* [x] 支持macos编译,运行 * [x] 交互模式 REPL * [x] 单文件模式,直接跑main.lua * [x] 目录模式,把指定目录挂载成/luadb,模拟真实设备的路径 * [ ] ZTT机制(模拟器)的设计和实现 * [ ] 跑通iRTU代码 +## 已支持的库 + +* lua基础库, io/os/table/math/bit等所有自带的库 +* luatos基础库,log/rtos/timer +* 外设库, uart/gpio/mcu/fskv +* 网络库, socket/http/mqtt/websocket/sntp,含TLS/SSL +* UI库, lcd/lvgl +* 工具库, crypto/pack/json/gmssl/iotauth/bit64/zbuff/protobuf等所有工具库 + ## 授权协议 [MIT License](LICENSE) From 77abe2790e683ad847365c5d1a9df40418f489ac Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Mon, 30 Oct 2023 17:28:16 +0800 Subject: [PATCH 08/15] =?UTF-8?q?update:=20linux=E4=B8=8B=E4=B9=9F?= =?UTF-8?q?=E8=83=BD=E8=B7=91GUI=E7=B3=BB=E5=88=97=E5=BA=93=E4=BA=86,=20?= =?UTF-8?q?=E5=B0=B1=E6=98=AF=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8=E5=A4=A7?= =?UTF-8?q?=E4=B8=80=E4=BA=9B,=E7=BC=96=E8=AF=91=E8=BF=87=E7=A8=8B?= =?UTF-8?q?=E9=BA=BB=E7=83=A6=E4=BA=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build_linux_32bit.sh | 3 ++- build_linux_64bit.sh | 1 + build_linux_64bit_gui.sh | 5 +++++ include/luat_conf_bsp.h | 1 + xmake.lua | 10 +++++----- 5 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 build_linux_64bit_gui.sh diff --git a/build_linux_32bit.sh b/build_linux_32bit.sh index a5ff799..bb83147 100644 --- a/build_linux_32bit.sh +++ b/build_linux_32bit.sh @@ -4,5 +4,6 @@ xmake clean -a export VM_64bit=0 -xmake f -p linux -a i386 -m debug -y +export LUAT_USE_GUI=n +xmake f -p linux -a i386 -m debug xmake -w -y diff --git a/build_linux_64bit.sh b/build_linux_64bit.sh index 477cbd8..f08e2a9 100644 --- a/build_linux_64bit.sh +++ b/build_linux_64bit.sh @@ -1,4 +1,5 @@ xmake clean -a export VM_64bit=1 +export LUAT_USE_GUI=n xmake f -p linux -a i386 -y xmake -w -y diff --git a/build_linux_64bit_gui.sh b/build_linux_64bit_gui.sh new file mode 100644 index 0000000..79e6c28 --- /dev/null +++ b/build_linux_64bit_gui.sh @@ -0,0 +1,5 @@ +xmake clean -a +export VM_64bit=1 +export LUAT_USE_GUI=y +xmake f -p linux -a i386 -y +xmake -w -y diff --git a/include/luat_conf_bsp.h b/include/luat_conf_bsp.h index 440ab05..af1971f 100644 --- a/include/luat_conf_bsp.h +++ b/include/luat_conf_bsp.h @@ -5,6 +5,7 @@ #include "stdint.h" #define LUAT_BSP_VERSION "V1106" +#define LUAT_CONF_USE_LIBSYS_SOURCE 1 #define LUAT_USE_CMDLINE_ARGS 1 // 启用64位虚拟机 // #define LUAT_CONF_VM_64bit diff --git a/xmake.lua b/xmake.lua index dcc622e..638022a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -33,14 +33,14 @@ if os.getenv("LUAT_USE_GUI") == "y" then end if is_host("windows") then - -- add_defines("LUA_USE_WINDOWS") + add_defines("LUA_USE_WINDOWS") add_cflags("/utf-8") add_includedirs("win32") -- add_ldflags("-static") --- elseif is_host("linux") then --- add_defines("LUA_USE_LINUX") --- elseif is_host("macos") then --- add_defines("LUA_USE_MACOSX") +elseif is_host("linux") then + add_defines("LUA_USE_LINUX") +elseif is_host("macos") then + add_defines("LUA_USE_MACOSX") end From 1fcd6a76c939c3830eb3f3a23b20bf74dca90fe7 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Tue, 31 Oct 2023 21:06:58 +0800 Subject: [PATCH 09/15] =?UTF-8?q?add:=20=E6=94=AF=E6=8C=81=E5=8A=A0?= =?UTF-8?q?=E8=BD=BDluatools=E9=A1=B9=E7=9B=AE=E6=96=87=E4=BB=B6=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E5=8A=A0=E8=BD=BD=E7=84=B6=E5=90=8E=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_cmds.c | 214 ++++++++++++++++++++++++++++++++++++----------- xmake.lua | 2 +- 2 files changed, 166 insertions(+), 50 deletions(-) diff --git a/port/luat_cmds.c b/port/luat_cmds.c index 0eb033b..b592245 100644 --- a/port/luat_cmds.c +++ b/port/luat_cmds.c @@ -15,7 +15,6 @@ extern const struct luat_vfs_filesystem vfs_fs_ram; extern int cmdline_argc; extern char **cmdline_argv; - char *luadb_ptr; static size_t luadb_offset; static int luadb_init(void) @@ -131,56 +130,166 @@ static int luadb_addfile(const char *name, char *data, size_t len) return 0; } -void *check_cmd_args(int index); -int luat_cmd_parse(int argc, char** argv) { +void *check_cmd_args(const char *path); + +static int load_luadb(const char *path); +static int load_luatools(const char *path); + +static int is_opts(const char *key, const char *arg) +{ + if (strlen(key) >= strlen(arg)) + { + return 0; + } + return memcmp(key, arg, strlen(key)) == 0; +} + +int luat_cmd_parse(int argc, char **argv) +{ if (cmdline_argc == 1) { return 0; } - for (size_t i = 1; i < (size_t)cmdline_argc; i++) + for (size_t i = 1; i < (size_t)argc; i++) { - const char *argv = cmdline_argv[i]; - if (argv[0] == '-') { + const char *arg = argv[i]; + if (is_opts("--load_luadb=", arg)) + { + if (load_luadb(arg + strlen("--load_luadb="))) + { + LLOGE("加载luadb镜像失败"); + return -1; + } + continue; + } + if (is_opts("--load_luatools=", arg)) + { + if (load_luatools(arg + strlen("--load_luatools="))) + { + LLOGE("加载luatools项目文件失败"); + return -1; + } continue; } - check_cmd_args(i); + if (arg[0] == '-') + { + continue; + } + check_cmd_args(arg); } return 0; } -void *check_cmd_args(int index) +static int load_luadb(const char *path) { - size_t len = 0; - // int ret = 0; - void *ptr = NULL; - const char *path = cmdline_argv[index]; - if (strlen(path) < 4 || strlen(path) >= 512) + long len = 0; + FILE *f = fopen(path, "rb"); + if (!f) { - return NULL; + LLOGE("无法打开luadb镜像文件 %s", path); + return -1; } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + char *ptr = luat_heap_malloc(len); + if (ptr == NULL) + { + fclose(f); + LLOGE("luadb镜像文件太大,内存放不下 %s", path); + return -1; + } + fread(ptr, len, 1, f); + fclose(f); + luadb_ptr = ptr; + luadb_offset = len; + return 0; +} + - if (!memcmp(path + strlen(path) - 4, ".bin", 4)) +static int load_luatools(const char *path) +{ + long len = 0; + FILE *f = fopen(path, "rb"); + if (!f) + { + LLOGE("无法打开luatools项目文件 %s", path); + return -1; + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + char *ptr = luat_heap_malloc(len + 1); + if (ptr == NULL) { - FILE *f = fopen(path, "rb"); - if (!f) - { - LLOGE("无法打开luadb镜像文件 %s", path); - return NULL; - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - ptr = luat_heap_malloc(len); - if (ptr == NULL) - { - fclose(f); - LLOGE("luadb镜像文件太大,内存放不下 %s", path); - return NULL; - } - fread(ptr, len, 1, f); fclose(f); - return ptr; + LLOGE("luatools项目文件太大,内存放不下 %s", path); + return -1; + } + fread(ptr, len, 1, f); + fclose(f); + + ptr[len] = 0; + + char *ret = ptr; + char dirline[512] = {0}; + char rpath[1024] = {0}; + size_t retlen = 0; + while (ptr[0] != 0x00) { + // LLOGD("ptr %c", ptr[0]); + if (ptr[0] == '\r' || ptr[0] == '\n') { + if (ret != ptr) { + ptr[0] = 0x00; + retlen = strlen(ret); + // LLOGD("检索到的行 %s", ret); + if (!strcmp("[info]", ret)) { + + } + else if (retlen > 5) { + if (ret[0] == '[' && ret[retlen - 1] == ']') { + LLOGD("目录行 %s", ret); + memcpy(dirline, ret + 1, retlen - 2); + dirline[retlen - 2] = 0x00; + } + else { + if (dirline[0]) { + for (size_t i = 0; i < strlen(ret); i++) + { + if (ret[i] == ' ' || ret[i] == '=') { + ret[i] = 0; + memset(rpath, 0, 1024); + memcpy(rpath, dirline, strlen(dirline)); + #ifdef LUA_USE_WINDOWS + rpath[strlen(dirline)] = '\\'; + #else + rpath[strlen(dirline)] = '/'; + #endif + memcpy(rpath + strlen(rpath), ret, strlen(ret)); + LLOGI("加载文件 %s", rpath); + if (check_cmd_args(rpath) == NULL ) + return -2; + break; + } + } + } + } + } + } + ret = ptr + 1; + } + ptr ++; } + return 0; +} + +void *check_cmd_args(const char *path) +{ + size_t len = 0; + if (strlen(path) < 4 || strlen(path) >= 512) + { + return NULL; + } + if (!memcmp(path + strlen(path) - 4, ".lua", 4)) { // LLOGD("把%s当做main.lua运行", path); @@ -206,12 +315,14 @@ void *check_cmd_args(int index) fclose(f); for (size_t i = strlen(path); i > 0; i--) { - if (path[i-1] == '/' || path[i-1] == '\\') { + if (path[i - 1] == '/' || path[i - 1] == '\\') + { memcpy(tmpname, path + i, strlen(path) - 1); break; } } - if (tmpname[0] == 0x00) { + if (tmpname[0] == 0x00) + { memcpy(tmpname, path, strlen(path)); } @@ -226,15 +337,15 @@ void *check_cmd_args(int index) DIR *dp; struct dirent *ep; // int index = 0; - FILE* f = NULL; + FILE *f = NULL; char buff[512] = {0}; - // LLOGD("加载目录 %s", path); - #ifdef LUA_USE_WINDOWS +// LLOGD("加载目录 %s", path); +#ifdef LUA_USE_WINDOWS memcpy(buff, path, strlen(path)); - #else +#else memcpy(buff, path, strlen(path) - 1); - #endif; +#endif; dp = opendir(buff); // LLOGD("目录打开 %p", dp); if (dp != NULL) @@ -243,31 +354,36 @@ void *check_cmd_args(int index) while ((ep = readdir(dp)) != NULL) { // LLOGD("文件/目录 %s %d", ep->d_name, ep->d_type); - if (ep->d_type != DT_REG) { + if (ep->d_type != DT_REG) + { continue; } - #ifdef LUA_USE_WINDOWS +#ifdef LUA_USE_WINDOWS sprintf(buff, "%s\\%s", path, ep->d_name); - #else +#else sprintf(buff, "%s/%s", path, ep->d_name); - #endif +#endif // index++; f = fopen(buff, "rb"); - if (f == NULL) { + if (f == NULL) + { LLOGW("打开文件失败,跳过 %s", buff); } fseek(f, 0, SEEK_END); len = ftell(f); fseek(f, 0, SEEK_SET); - char* tmp = luat_heap_malloc(len); - if (tmp == NULL) { + char *tmp = luat_heap_malloc(len); + if (tmp == NULL) + { LLOGE("内存不足,无法加载文件 %s", buff); } - else { + else + { fread(tmp, 1, len, f); } fclose(f); - if (tmp) { + if (tmp) + { luadb_addfile(ep->d_name, tmp, len); luat_heap_free(tmp); } diff --git a/xmake.lua b/xmake.lua index 638022a..3267169 100644 --- a/xmake.lua +++ b/xmake.lua @@ -33,7 +33,7 @@ if os.getenv("LUAT_USE_GUI") == "y" then end if is_host("windows") then - add_defines("LUA_USE_WINDOWS") + -- add_defines("LUA_USE_WINDOWS") add_cflags("/utf-8") add_includedirs("win32") -- add_ldflags("-static") From eaa45407804ae6ac205659f9ac80127f5ac3395f Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Tue, 31 Oct 2023 22:25:56 +0800 Subject: [PATCH 10/15] =?UTF-8?q?add:=20=E5=8A=A0=E8=BD=BDlua=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=BC=BA=E5=88=B6=E8=BD=ACluac,=E4=BB=8E=E8=80=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=AF=AD=E6=B3=95=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_cmds.c | 263 +++++++++++++++++++++++++++++++---------------- 1 file changed, 173 insertions(+), 90 deletions(-) diff --git a/port/luat_cmds.c b/port/luat_cmds.c index b592245..af937f2 100644 --- a/port/luat_cmds.c +++ b/port/luat_cmds.c @@ -2,6 +2,7 @@ #include "luat_base.h" #include "luat_fs.h" #include "luat_malloc.h" +#include "lundump.h" #define LUAT_LOG_TAG "fs" #include "luat_log.h" @@ -130,7 +131,7 @@ static int luadb_addfile(const char *name, char *data, size_t len) return 0; } -void *check_cmd_args(const char *path); +void *check_file_path(const char *path); static int load_luadb(const char *path); static int load_luatools(const char *path); @@ -175,7 +176,7 @@ int luat_cmd_parse(int argc, char **argv) { continue; } - check_cmd_args(arg); + check_file_path(arg); } return 0; } @@ -206,7 +207,6 @@ static int load_luadb(const char *path) return 0; } - static int load_luatools(const char *path) { long len = 0; @@ -235,38 +235,46 @@ static int load_luatools(const char *path) char dirline[512] = {0}; char rpath[1024] = {0}; size_t retlen = 0; - while (ptr[0] != 0x00) { + while (ptr[0] != 0x00) + { // LLOGD("ptr %c", ptr[0]); - if (ptr[0] == '\r' || ptr[0] == '\n') { - if (ret != ptr) { + if (ptr[0] == '\r' || ptr[0] == '\n') + { + if (ret != ptr) + { ptr[0] = 0x00; retlen = strlen(ret); // LLOGD("检索到的行 %s", ret); - if (!strcmp("[info]", ret)) { - + if (!strcmp("[info]", ret)) + { } - else if (retlen > 5) { - if (ret[0] == '[' && ret[retlen - 1] == ']') { + else if (retlen > 5) + { + if (ret[0] == '[' && ret[retlen - 1] == ']') + { LLOGD("目录行 %s", ret); memcpy(dirline, ret + 1, retlen - 2); dirline[retlen - 2] = 0x00; } - else { - if (dirline[0]) { + else + { + if (dirline[0]) + { for (size_t i = 0; i < strlen(ret); i++) { - if (ret[i] == ' ' || ret[i] == '=') { + if (ret[i] == ' ' || ret[i] == '=') + { ret[i] = 0; memset(rpath, 0, 1024); memcpy(rpath, dirline, strlen(dirline)); - #ifdef LUA_USE_WINDOWS +#ifdef LUA_USE_WINDOWS rpath[strlen(dirline)] = '\\'; - #else +#else rpath[strlen(dirline)] = '/'; - #endif +#endif memcpy(rpath + strlen(rpath), ret, strlen(ret)); LLOGI("加载文件 %s", rpath); - if (check_cmd_args(rpath) == NULL ) + if (check_file_path(rpath) == NULL) return -2; break; } @@ -277,59 +285,149 @@ static int load_luatools(const char *path) } ret = ptr + 1; } - ptr ++; + ptr++; + } + return 0; +} + +typedef struct luac_ctx +{ + char *ptr; + size_t len; +} luac_ctx_t; + +static int writer(lua_State *L, const void *p, size_t size, void *u) +{ + UNUSED(L); + // LLOGD("写入部分数据 %p %d", p, size); + luac_ctx_t *ctx = (luac_ctx_t *)u; + if (ctx->ptr == NULL) + { + ctx->ptr = luat_heap_malloc(size); + ctx->len = size; + memcpy(ctx->ptr, p, size); + return 0; } + char *ptr = luat_heap_realloc(ctx->ptr, ctx->len + size); + if (ptr == NULL) + { + LLOGE("内存分配失败"); + return 1; + } + memcpy(ptr + ctx->len, p, size); + ctx->ptr = ptr; + ctx->len += size; return 0; } -void *check_cmd_args(const char *path) +static int pmain(lua_State *L) { + const char *name = luaL_checkstring(L, 1); size_t len = 0; - if (strlen(path) < 4 || strlen(path) >= 512) + const char *data = luaL_checklstring(L, 2, &len); + int ret = luaL_loadbufferx(L, data, len, name, NULL); + if (ret) { - return NULL; + LLOGE("文件加载失败 %s %s", name, lua_tostring(L, -1)); + return 0; } + // LLOGD("luac转换成功,开始转buff"); + luac_ctx_t *ctx = luat_heap_malloc(sizeof(luac_ctx_t)); + memset(ctx, 0, sizeof(luac_ctx_t)); + // LLOGD("getproto "); + const Proto *f = getproto(L->top - 1); + // LLOGD("Proto %p", f); + ret = luaU_dump(L, f, writer, ctx, 0); + // LLOGD("luaU_dump 执行完成"); + if (ret == 0) + { + luadb_addfile(name, ctx->ptr, ctx->len); + } + lua_pushinteger(L, ret); + luat_heap_free(ctx->ptr); + luat_heap_free(ctx); + return 1; +} - if (!memcmp(path + strlen(path) - 4, ".lua", 4)) +static int to_luac(const char *name, char *data, size_t len) +{ + // LLOGD("检查语法并转换成luac %s", name); + lua_State *L = lua_newstate(luat_heap_alloc, NULL); + // LLOGD("创建临时luavm"); + lua_pushcfunction(L, &pmain); + lua_pushstring(L, name); + lua_pushlstring(L, data, len); + // LLOGD("准备执行luac转换"); + int ret = lua_pcall(L, 2, 1, 0); + if (ret) + { + LLOGD("lua文件加载失败 %s %d", name, ret); + lua_close(L); + return -1; + } + ret = luaL_checkinteger(L, -1); + lua_close(L); + return ret; +} + +static int add_onefile(const char *path) +{ + size_t len = 0; + int ret = 0; + // LLOGD("把%s当做main.lua运行", path); + char tmpname[512] = {0}; + FILE *f = fopen(path, "rb"); + if (!f) + { + LLOGE("文件不存在 %s", path); + return -1; + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + // void* fptr = luat_heap_malloc(len); + char *tmp = luat_heap_malloc(len); + if (tmp == NULL) { - // LLOGD("把%s当做main.lua运行", path); - char tmpname[512] = {0}; - FILE *f = fopen(path, "rb"); - if (!f) - { - LLOGE("lua文件 %s", path); - return NULL; - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - // void* fptr = luat_heap_malloc(len); - char *tmp = luat_heap_malloc(len); - if (tmp == NULL) - { - fclose(f); - LLOGE("lua文件太大,内存放不下 %s", path); - return NULL; - } - fread(tmp, 1, len, f); fclose(f); - for (size_t i = strlen(path); i > 0; i--) - { - if (path[i - 1] == '/' || path[i - 1] == '\\') - { - memcpy(tmpname, path + i, strlen(path) - 1); - break; - } - } - if (tmpname[0] == 0x00) + LLOGE("文件太大,内存放不下 %s", path); + return -2; + } + fread(tmp, 1, len, f); + fclose(f); + + for (size_t i = strlen(path); i > 0; i--) + { + if (path[i - 1] == '/' || path[i - 1] == '\\') { - memcpy(tmpname, path, strlen(path)); + memcpy(tmpname, path + i, strlen(path) - 1); + break; } + } + if (tmpname[0] == 0x00) + { + memcpy(tmpname, path, strlen(path)); + } - // 开始合成luadb结构 - luadb_addfile(tmpname, tmp, len); - luat_heap_free(tmp); - return luadb_ptr; + if (!memcmp(path + strlen(path) - 4, ".lua", 4)) + { + + ret = to_luac(tmpname, tmp, len); + } + else + { + ret = luadb_addfile(tmpname, tmp, len); + } + luat_heap_free(tmp); + return ret; +} + +void *check_file_path(const char *path) +{ + if (strlen(path) < 4 || strlen(path) >= 512) + { + LLOGD("文件长度不对劲 %d %s", strlen(path), path); + return NULL; } // 目录模式 if (!memcmp(path + strlen(path) - 1, "/", 1) || !memcmp(path + strlen(path) - 1, "\\", 1)) @@ -337,15 +435,14 @@ void *check_cmd_args(const char *path) DIR *dp; struct dirent *ep; // int index = 0; - FILE *f = NULL; char buff[512] = {0}; -// LLOGD("加载目录 %s", path); -#ifdef LUA_USE_WINDOWS + // LLOGD("加载目录 %s", path); + #ifdef LUA_USE_WINDOWS memcpy(buff, path, strlen(path)); -#else + #else memcpy(buff, path, strlen(path) - 1); -#endif; + #endif; dp = opendir(buff); // LLOGD("目录打开 %p", dp); if (dp != NULL) @@ -358,36 +455,15 @@ void *check_cmd_args(const char *path) { continue; } -#ifdef LUA_USE_WINDOWS + #ifdef LUA_USE_WINDOWS sprintf(buff, "%s\\%s", path, ep->d_name); -#else + #else sprintf(buff, "%s/%s", path, ep->d_name); -#endif - // index++; - f = fopen(buff, "rb"); - if (f == NULL) - { - LLOGW("打开文件失败,跳过 %s", buff); - } - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - char *tmp = luat_heap_malloc(len); - if (tmp == NULL) + #endif + if (add_onefile(buff)) { - LLOGE("内存不足,无法加载文件 %s", buff); + return NULL; } - else - { - fread(tmp, 1, len, f); - } - fclose(f); - if (tmp) - { - luadb_addfile(ep->d_name, tmp, len); - luat_heap_free(tmp); - } - // continue; } // LLOGD("遍历结束"); (void)closedir(dp); @@ -399,6 +475,13 @@ void *check_cmd_args(const char *path) return NULL; } } - // LLOGD("啥模式都不是, 没法加载 %s", path); - return NULL; -} \ No newline at end of file + else + { + if (add_onefile(path)) + { + return NULL; + } + return luadb_ptr; + } + // return NULL; +} From d92bf7ac29c0ac1d94dd2c68d294c71660747360 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 1 Nov 2023 09:36:09 +0800 Subject: [PATCH 11/15] =?UTF-8?q?update:=20lua=E6=96=87=E4=BB=B6=E8=AF=AD?= =?UTF-8?q?=E6=B3=95=E9=94=99=E8=AF=AF=E6=97=B6=E6=89=93=E5=8D=B0=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_cmds.c | 6 +++--- xmake.lua | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/port/luat_cmds.c b/port/luat_cmds.c index af937f2..233e685 100644 --- a/port/luat_cmds.c +++ b/port/luat_cmds.c @@ -349,7 +349,7 @@ static int pmain(lua_State *L) return 1; } -static int to_luac(const char *name, char *data, size_t len) +static int to_luac(const char *fullpath, const char *name, char *data, size_t len) { // LLOGD("检查语法并转换成luac %s", name); lua_State *L = lua_newstate(luat_heap_alloc, NULL); @@ -361,7 +361,7 @@ static int to_luac(const char *name, char *data, size_t len) int ret = lua_pcall(L, 2, 1, 0); if (ret) { - LLOGD("lua文件加载失败 %s %d", name, ret); + LLOGD("lua文件加载失败 %s %d", fullpath, ret); lua_close(L); return -1; } @@ -412,7 +412,7 @@ static int add_onefile(const char *path) if (!memcmp(path + strlen(path) - 4, ".lua", 4)) { - ret = to_luac(tmpname, tmp, len); + ret = to_luac(path, tmpname, tmp, len); } else { diff --git a/xmake.lua b/xmake.lua index 3267169..742ba3c 100644 --- a/xmake.lua +++ b/xmake.lua @@ -34,6 +34,7 @@ end if is_host("windows") then -- add_defines("LUA_USE_WINDOWS") + add_defines("_CRT_SECURE_NO_WARNINGS") add_cflags("/utf-8") add_includedirs("win32") -- add_ldflags("-static") From cbcb080c9a365e3f89bc1dadd65e283c5b00022a Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 1 Nov 2023 09:47:58 +0800 Subject: [PATCH 12/15] =?UTF-8?q?add:=20=E6=8F=90=E4=BE=9B=E7=AE=80?= =?UTF-8?q?=E5=8D=95=E7=9A=84=E8=AF=B4=E6=98=8E=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- doc/usage.md | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ port/luat_cmds.c | 22 +++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 doc/usage.md diff --git a/README.md b/README.md index adbd535..082dd79 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # 针对PC环境的LuatOS集成 -* 使用说明 +* [使用说明](doc/usage.md) * [编译说明](doc/compile.md) * [设计文档](doc/design.md) +本BSP已经替代主库的`bsp/linux`和`bsp/win32` + ## 待完成列表功能 * [x] 支持windows编译,运行 diff --git a/doc/usage.md b/doc/usage.md new file mode 100644 index 0000000..44643f6 --- /dev/null +++ b/doc/usage.md @@ -0,0 +1,57 @@ +# 用法指南 + +## 交互模式 + +windows + +```bash +luatos-pc.exe +``` + +linux + +```bash +./luatos-pc +``` + +若需退出交互模式 +```lua +os.exit() +``` + +## 指定文件或文件夹加载并启动 + +支持指定多个文件或文件夹,一起加载并启动 + + +单文件启动 +```bash +luatos-pc.exe test/001.helloworld/main.lua +``` + +多文件启动,按第一个`main.lua`作为主文件 +```bash +luatos-pc.exe test/001.helloworld/main.lua test/abc.lua test/logo.jpg +``` + +按文件夹启动, 路径需要以`/`或者`\` 结尾 +```bash +luatos-pc.exe ../LuatOS/demo/gmssl/ +``` + +文件和文件夹混合启动 +```bash +luatos-pc.exe test/001.helloworld/main.lua ../LuatOS/demo/gmssl/ +``` + +## 加载luatools项目文件直接启动 + +```bash +luatos-pc.exe --llt=D:/luatools/project/air101_gpio.ini +``` + +## 加载luadb镜像文件启动 + +```bash +luatos-pc.exe --ldb=D:/luatools/SoC量产文件/script.bin +``` diff --git a/port/luat_cmds.c b/port/luat_cmds.c index 233e685..c3421f5 100644 --- a/port/luat_cmds.c +++ b/port/luat_cmds.c @@ -154,6 +154,7 @@ int luat_cmd_parse(int argc, char **argv) for (size_t i = 1; i < (size_t)argc; i++) { const char *arg = argv[i]; + // 加载luadb文件镜像直接启动 if (is_opts("--load_luadb=", arg)) { if (load_luadb(arg + strlen("--load_luadb="))) @@ -163,6 +164,16 @@ int luat_cmd_parse(int argc, char **argv) } continue; } + if (is_opts("--ldb=", arg)) + { + if (load_luadb(arg + strlen("--ldb="))) + { + LLOGE("加载luadb镜像失败"); + return -1; + } + continue; + } + // 加载LuaTools项目文件直接启动 if (is_opts("--load_luatools=", arg)) { if (load_luatools(arg + strlen("--load_luatools="))) @@ -172,6 +183,17 @@ int luat_cmd_parse(int argc, char **argv) } continue; } + if (is_opts("--llt=", arg)) + { + if (load_luatools(arg + strlen("--llt="))) + { + LLOGE("加载luatools项目文件失败"); + return -1; + } + continue; + } + + if (arg[0] == '-') { continue; From 59e0e40fb83f81453d46aeee3a41240318c3f14a Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 1 Nov 2023 10:20:06 +0800 Subject: [PATCH 13/15] =?UTF-8?q?fix:=20luat=5Fos=5Fbsp=E5=BA=94=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E8=BF=94=E5=9B=9Epc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/luat_base_mini.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/port/luat_base_mini.c b/port/luat_base_mini.c index b72d640..571fcaa 100644 --- a/port/luat_base_mini.c +++ b/port/luat_base_mini.c @@ -162,7 +162,7 @@ void luat_os_reboot(int code) { } const char* luat_os_bsp(void) { - return "mini"; + return "pc"; // TODO 变成动态字符串 } From 54cf08626c2629094699255023ec81bf98a31bc9 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 1 Nov 2023 12:49:02 +0800 Subject: [PATCH 14/15] =?UTF-8?q?add:=20mock(=E6=A8=A1=E6=8B=9F=E5=99=A8?= =?UTF-8?q?=E6=9C=BA=E5=88=B6)=E9=80=BB=E8=BE=91=E8=B7=91=E9=80=9A,?= =?UTF-8?q?=E8=BF=98=E9=9C=80=E8=A6=81=E6=9B=B4=E6=B7=B1=E5=85=A5=E7=9A=84?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/luat_mock.h | 21 ++++ port/luat_base_mini.c | 14 ++- port/luat_cmds.c | 11 ++ port/mock/luat_mock.c | 241 +++++++++++++++++++++++++++++++++++++ test/010.mock/main.lua | 11 ++ test/010.mock/mocktest.lua | 13 ++ 6 files changed, 310 insertions(+), 1 deletion(-) create mode 100644 include/luat_mock.h create mode 100644 port/mock/luat_mock.c create mode 100644 test/010.mock/main.lua create mode 100644 test/010.mock/mocktest.lua diff --git a/include/luat_mock.h b/include/luat_mock.h new file mode 100644 index 0000000..2320357 --- /dev/null +++ b/include/luat_mock.h @@ -0,0 +1,21 @@ +#ifndef LUAT_MOCK_H +#define LUAT_MOCK_H + +int luat_mock_init(const char* path); + +#define MOCK_DISABLED (-0xFF) + +typedef struct luat_mock_ctx +{ + char key[128]; + char* req_data; + size_t req_len; + char* resp_data; + size_t resp_len; + int resp_type; + int resp_code; +}luat_mock_ctx_t; + +int luat_mock_call(luat_mock_ctx_t* ctx); + +#endif \ No newline at end of file diff --git a/port/luat_base_mini.c b/port/luat_base_mini.c index 571fcaa..4bcb6a9 100644 --- a/port/luat_base_mini.c +++ b/port/luat_base_mini.c @@ -5,6 +5,7 @@ #include "luat_malloc.h" #include #include +#include "luat_mock.h" #define LUAT_LOG_TAG "main" #include "luat_log.h" @@ -161,8 +162,19 @@ void luat_os_reboot(int code) { exit(code); } +char bsp_name[64]; + const char* luat_os_bsp(void) { - return "pc"; // TODO 变成动态字符串 + int ret = 0; + luat_mock_ctx_t ctx = {0}; + memcpy(ctx.key, "rtos.bsp.get", strlen("rtos.bsp.get")); + ret = luat_mock_call(&ctx); + if (ret == 0 && ctx.resp_len > 0 && ctx.resp_len < 64) { + memcpy(bsp_name, ctx.resp_data, ctx.resp_len); + bsp_name[ctx.resp_len] = 0x00; + return bsp_name; + } + return "pc"; } diff --git a/port/luat_cmds.c b/port/luat_cmds.c index c3421f5..4f5aa91 100644 --- a/port/luat_cmds.c +++ b/port/luat_cmds.c @@ -3,6 +3,7 @@ #include "luat_fs.h" #include "luat_malloc.h" #include "lundump.h" +#include "luat_mock.h" #define LUAT_LOG_TAG "fs" #include "luat_log.h" @@ -193,6 +194,16 @@ int luat_cmd_parse(int argc, char **argv) continue; } + // mock加载 + if (is_opts("--mlua=", arg)) + { + if (luat_mock_init(arg + strlen("--mlua="))) + { + LLOGE("加载mock功能失败"); + return -1; + } + continue; + } if (arg[0] == '-') { diff --git a/port/mock/luat_mock.c b/port/mock/luat_mock.c new file mode 100644 index 0000000..611afc3 --- /dev/null +++ b/port/mock/luat_mock.c @@ -0,0 +1,241 @@ + +#include "luat_base.h" +#include "luat_fs.h" +#include "luat_malloc.h" +#include "luat_mock.h" + +#define LUAT_LOG_TAG "mock" +#include "luat_log.h" + +static lua_State *mock_L; +static char mock_file_path[1024]; + +static int mock_main(lua_State *L); + +static const luaL_Reg loadedlibs[] = { + {"_G", luaopen_base}, // _G + {LUA_LOADLIBNAME, luaopen_package}, // require + {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库 + {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构 + {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件 + {LUA_OSLIBNAME, luaopen_os}, // os库,已精简 + {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作 + {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算 + {LUA_UTF8LIBNAME, luaopen_utf8}, + {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简 +#if defined(LUA_COMPAT_BITLIB) + {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用 +#endif +// 外设类 +#ifdef LUAT_USE_UART + {"uart", luaopen_uart}, // 串口操作 +#endif +#ifdef LUAT_USE_GPIO + {"gpio", luaopen_gpio}, // GPIO脚的操作 +#endif +#ifdef LUAT_USE_I2C + {"i2c", luaopen_i2c}, // I2C操作 +#endif +#ifdef LUAT_USE_SPI + {"spi", luaopen_spi}, // SPI操作 +#endif +#ifdef LUAT_USE_ADC + {"adc", luaopen_adc}, // ADC模块 +#endif +#ifdef LUAT_USE_PWM + {"pwm", luaopen_pwm}, // PWM模块 +#endif +#ifdef LUAT_USE_WDT + {"wdt", luaopen_wdt}, // watchdog模块 +#endif +#ifdef LUAT_USE_PM + {"pm", luaopen_pm}, // 电源管理模块 +#endif +#ifdef LUAT_USE_MCU + {"mcu", luaopen_mcu}, // MCU特有的一些操作 +#endif +#ifdef LUAT_USE_RTC + {"rtc", luaopen_rtc}, // 实时时钟 +#endif +#ifdef LUAT_USE_OTP + {"otp", luaopen_otp}, // OTP +#endif +//----------------------------------------------------------------- + {"log", luaopen_log}, // 日志库 + {"timer", luaopen_timer}, // 延时库 + {"pack", luaopen_pack}, // pack.pack/pack.unpack + {"json", luaopen_cjson}, // json + {"zbuff", luaopen_zbuff}, // + {"crypto", luaopen_crypto}, +#ifdef LUAT_USE_RSA + {"rsa", luaopen_rsa}, +#endif +#ifdef LUAT_USE_MINIZ + {"miniz", luaopen_miniz}, +#endif +#ifdef LUAT_USE_PROTOBUF + {"protobuf", luaopen_protobuf}, +#endif +#ifdef LUAT_USE_IOTAUTH + {"iotauth", luaopen_iotauth}, +#endif +#ifdef LUAT_USE_ICONV + {"iconv", luaopen_iconv}, +#endif +#ifdef LUAT_USE_BIT64 + {"bit64", luaopen_bit64}, +#endif +#ifdef LUAT_USE_FS + {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法 +#endif +#ifdef LUAT_USE_MQTTCORE + {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装 +#endif + {NULL, NULL} +}; + +int luat_mock_init(const char* path) { + mock_L = lua_newstate(luat_heap_alloc, NULL); + memcpy(mock_file_path, path, strlen(path) + 1); + LLOGI("mock脚本路径 %s", mock_file_path); + if (mock_L == NULL || mock_file_path[0] == 0x00) { + return -1; + } + const luaL_Reg *lib; + /* "require" functions from 'loadedlibs' and set results to global table */ + for (lib = loadedlibs; lib->func; lib++) { + luaL_requiref(mock_L, lib->name, lib->func, 1); + lua_pop(mock_L, 1); /* remove lib */ + //extern void print_list_mem(const char* name); + //print_list_mem(lib->name); + } + return 0; +} + +int luat_mock_call(luat_mock_ctx_t* ctx) { + if (mock_L == NULL) { + return -0xFF; + } + if (mock_file_path[0] == 0x00) { + return -2; + } + lua_pushcfunction(mock_L, &mock_main); + lua_pushstring(mock_L, ctx->key); + lua_pushlstring(mock_L, ctx->req_data, ctx->req_len); + int ret = lua_pcall(mock_L, 2, 2, 0); + if (ret) { + LLOGE("pcall %d %s", ret, lua_tostring(mock_L, -1)); + return ret; + } + else { + ctx->resp_code = lua_tointeger(mock_L, 1); + ctx->resp_type = lua_type(mock_L, 2); + switch (lua_type(mock_L, 2)) + { + case LUA_TNIL: // 空值 + /* code */ + break; + case LUA_TBOOLEAN: + ctx->resp_data = luat_heap_malloc(1); + ctx->resp_data[0] = lua_toboolean(mock_L, 2); + ctx->resp_len = 1; + break; + case LUA_TNUMBER: + ctx->resp_data = luat_heap_malloc(sizeof(lua_Integer)); + ctx->resp_len = sizeof(lua_Integer); + if (lua_isinteger(mock_L, 2)) { + lua_Integer intVal = lua_tointeger(mock_L, 2); + memcpy(ctx->resp_data, &intVal, sizeof(lua_Integer)); + } + else { + lua_Number numVal = lua_tonumber(mock_L, 2); + memcpy(ctx->resp_data, &numVal, sizeof(lua_Integer)); + } + break; + case LUA_TSTRING: + const char* resp = lua_tolstring(mock_L, 2, &ctx->resp_len); + if (ctx->resp_len > 0) { + ctx->resp_data = luat_heap_malloc(ctx->resp_len + 1); + memcpy(ctx->resp_data, resp, ctx->resp_len + 1); + } + break; + default: + break; + } + } + return ret; +} + +static int load_mock_lua_file(lua_State *L) { + const char* name = mock_file_path; + const char* path = mock_file_path; + // 加载mock.lua + //---------------------------------------------- + size_t len = 0; + int ret = 0; + // LLOGD("把%s当做main.lua运行", path); + char tmpname[512] = {0}; + FILE *f = fopen(name, "rb"); + if (!f) + { + LLOGE("文件不存在 %s", path); + return -3; + } + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + // void* fptr = luat_heap_malloc(len); + char *tmp = luat_heap_malloc(len); + if (tmp == NULL) + { + fclose(f); + LLOGE("文件太大,内存放不下 %s", path); + return -3; + } + fread(tmp, 1, len, f); + fclose(f); + + for (size_t i = strlen(path); i > 0; i--) + { + if (path[i - 1] == '/' || path[i - 1] == '\\') + { + memcpy(tmpname, name + i, strlen(path) - 1); + break; + } + } + if (tmpname[0] == 0x00) + { + memcpy(tmpname, path, strlen(path)); + } + + //---------------------------------------------- + + ret = luaL_loadbufferx(L, tmp, len, name, NULL); + if (ret) { + LLOGE("文件加载失败 %s %s", name, lua_tostring(L, -1)); + return -3; + } + ret = lua_pcall(L, 0, 1, 0); + if (ret) { + LLOGE("mock加载失败 %s %s", name, lua_tostring(L, -1)); + return -4; + } + // LLOGD("栈顶对象的类型 %d", lua_type(L, -1)); + lua_setglobal(L, "mockf"); + return 0; +} + +static int mock_main(lua_State *L) { + int ret = 0; + + ret = load_mock_lua_file(L); + if (ret) { + LLOGD("加载mock脚本失败 %d", ret); + return ret; + } + lua_getglobal(L, "mockf"); + lua_pushvalue(L, 1); + lua_pushvalue(L, 2); + lua_call(L, 2, 2); + return 2; +} diff --git a/test/010.mock/main.lua b/test/010.mock/main.lua new file mode 100644 index 0000000..a0a7a53 --- /dev/null +++ b/test/010.mock/main.lua @@ -0,0 +1,11 @@ + +_G.sys = require("sys") + +sys.taskInit(function() + while 1 do + sys.wait(1000) + log.info("bsp", rtos.bsp()) + end +end) + +sys.run() diff --git a/test/010.mock/mocktest.lua b/test/010.mock/mocktest.lua new file mode 100644 index 0000000..4b3b5ea --- /dev/null +++ b/test/010.mock/mocktest.lua @@ -0,0 +1,13 @@ + +local function mock_handle(key, data) + log.info("mock.key", key) + --print("mock", key, data or "nil") + if key == "rtos.bsp.get" then + return 0, "EC618" + end + return -1 +end + +-- print("我在这里...") + +return mock_handle From 36dcab5f1200eed9baf4a639648a228717154cc9 Mon Sep 17 00:00:00 2001 From: Wendal Chen Date: Wed, 1 Nov 2023 16:57:13 +0800 Subject: [PATCH 15/15] =?UTF-8?q?fix:=20luat=5Fmock=E4=B8=8D=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E5=9C=A8switch=E5=86=85=E9=83=A8=E5=A3=B0=E6=98=8E?= =?UTF-8?q?=E5=B1=80=E9=83=A8=E5=8F=98=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/mock/luat_mock.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/port/mock/luat_mock.c b/port/mock/luat_mock.c index 611afc3..9df62f4 100644 --- a/port/mock/luat_mock.c +++ b/port/mock/luat_mock.c @@ -119,9 +119,13 @@ int luat_mock_call(luat_mock_ctx_t* ctx) { if (mock_file_path[0] == 0x00) { return -2; } + + const char *resp; + lua_Integer intVal; + lua_Number numVal; lua_pushcfunction(mock_L, &mock_main); - lua_pushstring(mock_L, ctx->key); - lua_pushlstring(mock_L, ctx->req_data, ctx->req_len); + lua_pushstring(mock_L, ctx->key); + lua_pushlstring(mock_L, ctx->req_data, ctx->req_len); int ret = lua_pcall(mock_L, 2, 2, 0); if (ret) { LLOGE("pcall %d %s", ret, lua_tostring(mock_L, -1)); @@ -144,16 +148,16 @@ int luat_mock_call(luat_mock_ctx_t* ctx) { ctx->resp_data = luat_heap_malloc(sizeof(lua_Integer)); ctx->resp_len = sizeof(lua_Integer); if (lua_isinteger(mock_L, 2)) { - lua_Integer intVal = lua_tointeger(mock_L, 2); + intVal = lua_tointeger(mock_L, 2); memcpy(ctx->resp_data, &intVal, sizeof(lua_Integer)); } else { - lua_Number numVal = lua_tonumber(mock_L, 2); + numVal = lua_tonumber(mock_L, 2); memcpy(ctx->resp_data, &numVal, sizeof(lua_Integer)); } break; case LUA_TSTRING: - const char* resp = lua_tolstring(mock_L, 2, &ctx->resp_len); + resp = lua_tolstring(mock_L, 2, &ctx->resp_len); if (ctx->resp_len > 0) { ctx->resp_data = luat_heap_malloc(ctx->resp_len + 1); memcpy(ctx->resp_data, resp, ctx->resp_len + 1);