main_mini.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <stdio.h>
  2. #include "luat_base.h"
  3. #include "luat_malloc.h"
  4. #include "luat_msgbus.h"
  5. #include "luat_fs.h"
  6. #include <stdlib.h>
  7. #include "luat_pcconf.h"
  8. #include "bget.h"
  9. #define LUAT_LOG_TAG "main"
  10. #include "luat_log.h"
  11. #include "uv.h"
  12. #include "luat_mem.h"
  13. #ifdef LUAT_USE_LVGL
  14. uv_timer_t lvgl_timer;
  15. #include "lvgl.h"
  16. #endif
  17. extern char *luadb_ptr;
  18. extern const uint8_t luadb_mod[];
  19. // 如果模拟器luadb_mod 的前32bytes为luatdb_secret,说明可能被luatools修改过
  20. // 这串数据实际上是"FFFFEEEEDDDDCCCCBBBBAAAA999988887777666655554444333322221111"的sha256值
  21. const uint8_t luatdb_secret[] = {0xa8, 0xe4, 0x9c, 0x1a, 0x57, 0x4b, 0x00, 0x2f, 0x4c, 0xc4, 0x74, 0xb8, 0x69, 0x1d, 0x90, 0xc1, 0x84, 0x24, 0x16, 0x11, 0x79, 0xa2, 0xd0, 0x4b, 0xfc, 0xf5, 0x14, 0x5d, 0xdd, 0x54, 0xdd, 0x55};
  22. #define LUAT_HEAP_SIZE (2*1024*1024)
  23. uint8_t luavm_heap[LUAT_HEAP_SIZE] = {0};
  24. int cmdline_argc;
  25. char** cmdline_argv;
  26. // uv_timespec64_t boot_ts;
  27. extern uint64_t uv_startup_ns;
  28. extern void luat_lwip_init(void);
  29. int lua_main (int argc, char** argv);
  30. void luat_log_init_win32(void);
  31. void luat_uart_initial_win32(void);
  32. void luat_network_init(void);
  33. uv_loop_t *main_loop;
  34. uv_mutex_t timer_lock;
  35. int luat_cmd_parse(int argc, char** argv);
  36. static int luat_lvg_handler(lua_State* L, void* ptr);
  37. static void lvgl_timer_cb(uv_timer_t* lvgl_timer);
  38. int32_t luatos_pc_climode;
  39. void uv_luat_main(void* args) {
  40. (void)args;
  41. // printf("cmdline_argc %d\n", cmdline_argc);
  42. if (cmdline_argc == 1) {
  43. luatos_pc_climode = 1;
  44. #ifdef LUAT_CONF_VM_64bit
  45. LLOGI("LuatOS@%s %s, Build: " __DATE__ " " __TIME__ " 64bit", "PC", LUAT_VERSION);
  46. #else
  47. LLOGI("LuatOS@%s %s, Build: " __DATE__ " " __TIME__ " 32bit", "PC", LUAT_VERSION);
  48. #endif
  49. lua_main(cmdline_argc, cmdline_argv);
  50. }
  51. else {
  52. luat_main();
  53. }
  54. }
  55. static void timer_nop(uv_timer_t *handle) {
  56. // 不需要操作东西
  57. (void)handle;
  58. }
  59. static void timer_lwip(uv_timer_t *handle);
  60. // boot
  61. int main(int argc, char** argv) {
  62. cmdline_argc = argc;
  63. cmdline_argv = argv;
  64. luat_heap_opt_init(LUAT_HEAP_SRAM);
  65. #ifdef LUAT_USE_WINDOWS
  66. extern void InitCrashDump();
  67. InitCrashDump();
  68. // Windows平台下自动设置控制台编码
  69. extern void luat_console_auto_encoding(void);
  70. luat_console_auto_encoding();
  71. #endif
  72. main_loop = malloc(sizeof(uv_loop_t));
  73. // uv_replace_allocator(luat_heap_malloc, luat_heap_realloc, luat_heap_calloc, luat_heap_free);
  74. uv_loop_init(main_loop);
  75. // uv_clock_gettime(UV_CLOCK_MONOTONIC, &boot_ts);
  76. uv_startup_ns = uv_hrtime();
  77. uv_mutex_init(&timer_lock);
  78. luat_pcconf_init();
  79. luat_log_init_win32();
  80. bpool(luavm_heap, LUAT_HEAP_SIZE);
  81. luat_heap_opt_init(LUAT_HEAP_PSRAM);
  82. #ifdef LUAT_USE_LVGL
  83. lv_init();
  84. #endif
  85. luat_fs_init();
  86. luat_network_init();
  87. // 如果luadb_mod被修改过,那么直接以luadb_mod偏移32bytes作为luadb
  88. if(!memcmp(luatdb_secret, luadb_mod, 32))
  89. {
  90. LLOGI("luadb mod init");
  91. luadb_ptr = luadb_mod + 32;
  92. cmdline_argc = 2;
  93. }
  94. else
  95. {
  96. int ret = luat_cmd_parse(cmdline_argc, cmdline_argv);
  97. if (ret) {
  98. return ret;
  99. }
  100. }
  101. #ifdef LUAT_USE_LVGL
  102. uv_timer_init(main_loop, &lvgl_timer);
  103. uv_timer_start(&lvgl_timer, lvgl_timer_cb, 25, 25);
  104. #endif
  105. #ifdef LUAT_USE_LWIP
  106. //LLOGD("初始化lwip");
  107. luat_lwip_init();
  108. #endif
  109. // uv_thread_t l_main;
  110. // 加一个NOP的timer,防止uv_run 立即退出
  111. uv_timer_t t;
  112. uv_timer_init(main_loop, &t);
  113. #if defined(LUAT_USE_LWIP)
  114. uv_timer_start(&t, timer_lwip, 5, 5);
  115. #else
  116. uv_timer_start(&t, timer_nop, 1000, 1000);
  117. #endif
  118. uv_luat_main(NULL);
  119. uv_loop_close(main_loop);
  120. free(main_loop);
  121. return 0;
  122. }
  123. // UI相关
  124. #ifdef LUAT_USE_LVGL
  125. static int luat_lvg_handler(lua_State* L, void* ptr) {
  126. (void)L;
  127. (void)ptr;
  128. lv_tick_inc(25);
  129. lv_task_handler();
  130. return 0;
  131. }
  132. static void lvgl_timer_cb(uv_timer_t* lvgl_timer) {
  133. rtos_msg_t msg = {
  134. .handler = luat_lvg_handler
  135. };
  136. luat_msgbus_put(&msg, 0);
  137. }
  138. #endif
  139. void sys_check_timeouts(void);
  140. static void timer_lwip(uv_timer_t *handle) {
  141. (void)handle;
  142. sys_check_timeouts();
  143. }