luat_base_mini.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_fs.h"
  4. #include "luat_timer.h"
  5. #include "luat_malloc.h"
  6. #include <stdlib.h>
  7. #include "luat_mock.h"
  8. #include "luat_libs.h"
  9. #define LUAT_LOG_TAG "main"
  10. #include "luat_log.h"
  11. static const luaL_Reg loadedlibs[] = {
  12. {"_G", luaopen_base}, // _G
  13. {LUA_LOADLIBNAME, luaopen_package}, // require
  14. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  15. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  16. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  17. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  18. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  19. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  20. {LUA_UTF8LIBNAME, luaopen_utf8},
  21. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  22. #if defined(LUA_COMPAT_BITLIB)
  23. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  24. #endif
  25. // 外设类
  26. #ifdef LUAT_USE_UART
  27. {"uart", luaopen_uart}, // 串口操作
  28. #endif
  29. #ifdef LUAT_USE_GPIO
  30. {"gpio", luaopen_gpio}, // GPIO脚的操作
  31. #endif
  32. #ifdef LUAT_USE_I2C
  33. {"i2c", luaopen_i2c}, // I2C操作
  34. #endif
  35. #ifdef LUAT_USE_SPI
  36. {"spi", luaopen_spi}, // SPI操作
  37. #endif
  38. #ifdef LUAT_USE_I2S
  39. {"i2s", luaopen_i2s}, // I2S操作
  40. #endif
  41. #ifdef LUAT_USE_ADC
  42. {"adc", luaopen_adc}, // ADC模块
  43. #endif
  44. #ifdef LUAT_USE_PWM
  45. {"pwm", luaopen_pwm}, // PWM模块
  46. #endif
  47. #ifdef LUAT_USE_WDT
  48. {"wdt", luaopen_wdt}, // watchdog模块
  49. #endif
  50. #ifdef LUAT_USE_PM
  51. {"pm", luaopen_pm}, // 电源管理模块
  52. #endif
  53. #ifdef LUAT_USE_MCU
  54. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  55. #endif
  56. #ifdef LUAT_USE_RTC
  57. {"rtc", luaopen_rtc}, // 实时时钟
  58. #endif
  59. #ifdef LUAT_USE_OTP
  60. {"otp", luaopen_otp}, // OTP
  61. #endif
  62. //-----------------------------------------------------------------
  63. {"rtos", luaopen_rtos}, // rtos底层库, 核心功能是队列和定时器
  64. {"log", luaopen_log}, // 日志库
  65. {"timer", luaopen_timer}, // 延时库
  66. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  67. {"json", luaopen_cjson}, // json
  68. {"zbuff", luaopen_zbuff}, //
  69. {"crypto", luaopen_crypto},
  70. {"hmeta", luaopen_hmeta}, // 硬件信息库
  71. #ifdef LUAT_USE_RSA
  72. {"rsa", luaopen_rsa},
  73. #endif
  74. #ifdef LUAT_USE_MINIZ
  75. {"miniz", luaopen_miniz},
  76. #endif
  77. #ifdef LUAT_USE_PROTOBUF
  78. {"protobuf", luaopen_protobuf},
  79. #endif
  80. #ifdef LUAT_USE_IOTAUTH
  81. {"iotauth", luaopen_iotauth},
  82. #endif
  83. #ifdef LUAT_USE_ICONV
  84. {"iconv", luaopen_iconv},
  85. #endif
  86. #ifdef LUAT_USE_BIT64
  87. {"bit64", luaopen_bit64},
  88. #endif
  89. #ifdef LUAT_USE_GMSSL
  90. {"gmssl", luaopen_gmssl},
  91. #endif
  92. #ifdef LUAT_USE_MCU
  93. {"mcu", luaopen_mcu},
  94. #endif
  95. #ifdef LUAT_USE_LIBGNSS
  96. {"libgnss", luaopen_libgnss}, // 处理GNSS定位数据
  97. #endif
  98. #ifdef LUAT_USE_FFT
  99. {"fft", luaopen_fft}, // FFT 库
  100. #endif
  101. #ifdef LUAT_USE_FS
  102. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  103. #endif
  104. #ifdef LUAT_USE_FSKV
  105. {"fskv", luaopen_fskv},
  106. #endif
  107. #ifdef LUAT_USE_MQTTCORE
  108. {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  109. #endif
  110. #ifdef LUAT_USE_LIBCOAP
  111. {"libcoap", luaopen_libcoap}, // 处理COAP消息
  112. #endif
  113. #ifdef LUAT_USE_YMODEM
  114. {"ymodem", luaopen_ymodem},
  115. #endif
  116. #ifdef LUAT_USE_FASTLZ
  117. {"fastlz", luaopen_fastlz},
  118. #endif
  119. #ifdef LUAT_USE_COREMARK
  120. {"coremark", luaopen_coremark},
  121. #endif
  122. #ifdef LUAT_USE_NETWORK
  123. {"socket", luaopen_socket_adapter},
  124. {"http", luaopen_http},
  125. {"mqtt", luaopen_mqtt},
  126. {"websocket", luaopen_websocket},
  127. // {"ftp", luaopen_ftp},
  128. {"errDump", luaopen_errdump},
  129. #endif
  130. #ifdef LUAT_USE_ERCOAP
  131. {"ercoap", luaopen_ercoap},
  132. #endif
  133. // UI类
  134. #ifdef LUAT_USE_FONTS
  135. {"fonts", luaopen_fonts},
  136. #endif
  137. #ifdef LUAT_USE_DISP
  138. {"disp", luaopen_disp}, // OLED显示模块
  139. #endif
  140. #ifdef LUAT_USE_U8G2
  141. {"u8g2", luaopen_u8g2}, // u8g2
  142. #endif
  143. #ifdef LUAT_USE_EINK
  144. {"eink", luaopen_eink}, // 电子墨水屏
  145. #endif
  146. #ifdef LUAT_USE_FATFS
  147. {"fatfs", luaopen_fatfs}, // SD卡/tf卡
  148. #endif
  149. #ifdef LUAT_USE_LVGL
  150. {"lvgl", luaopen_lvgl},
  151. #endif
  152. #ifdef LUAT_USE_EASYLVGL
  153. {"easylvgl", luaopen_easylvgl},
  154. #endif
  155. #ifdef LUAT_USE_LCD
  156. {"lcd", luaopen_lcd},
  157. #endif
  158. #ifdef LUAT_USE_PINYIN
  159. {"pinyin", luaopen_pinyin},
  160. #endif
  161. #ifdef LUAT_USE_GTFONT
  162. {"gtfont", luaopen_gtfont},
  163. #endif
  164. #ifdef LUAT_USE_HZFONT
  165. {"hzfont", luaopen_hzfont},
  166. #endif
  167. #ifdef LUAT_USE_TP
  168. {"tp", luaopen_tp},
  169. #endif
  170. #ifdef LUAT_USE_AUDIO
  171. {"audio", luaopen_multimedia_audio},
  172. #endif
  173. #ifdef LUAT_USE_SQLITE3
  174. {"sqlite3", luaopen_sqlite3},
  175. #endif
  176. #ifdef LUAT_USE_WS2812
  177. {"ws2812", luaopen_ws2812},
  178. #endif
  179. #ifdef LUAT_USE_ONEWIRE
  180. {"onewire", luaopen_onewire},
  181. #endif
  182. #ifdef LUAT_USE_XXTEA
  183. {"xxtea", luaopen_xxtea},
  184. #endif
  185. #ifdef LUAT_USE_ULWIP
  186. {"ulwip", luaopen_ulwip},
  187. #endif
  188. #ifdef LUAT_USE_PROFILER
  189. {"profiler", luaopen_profiler},
  190. #endif
  191. #ifdef LUAT_USE_VTOOL
  192. {"vtool", luaopen_vtool},
  193. #endif
  194. #ifdef LUAT_USE_AIRUI
  195. {"airui", luaopen_airui},
  196. #endif
  197. #ifdef LUAT_USE_CAN
  198. {"can", luaopen_can},
  199. #endif
  200. #ifdef LUAT_USE_OTP
  201. {"otp", luaopen_otp},
  202. #endif
  203. #ifdef LUAT_USE_MOBILE
  204. {"mobile", luaopen_mobile},
  205. #endif
  206. {NULL, NULL}
  207. };
  208. // 按不同的rtconfig加载不同的库函数
  209. void luat_openlibs(lua_State *L) {
  210. // 初始化队列服务
  211. luat_msgbus_init();
  212. //print_list_mem("done>luat_msgbus_init");
  213. // 加载系统库
  214. const luaL_Reg *lib;
  215. /* "require" functions from 'loadedlibs' and set results to global table */
  216. for (lib = loadedlibs; lib->func; lib++) {
  217. luaL_requiref(L, lib->name, lib->func, 1);
  218. lua_pop(L, 1); /* remove lib */
  219. //extern void print_list_mem(const char* name);
  220. //print_list_mem(lib->name);
  221. }
  222. }
  223. void luat_os_reboot(int code) {
  224. exit(code);
  225. }
  226. char bsp_name[64];
  227. const char* luat_os_bsp(void) {
  228. int ret = 0;
  229. #ifdef LUAT_USE_MOCKAPI
  230. luat_mock_ctx_t ctx = {0};
  231. memcpy(ctx.key, "rtos.bsp.get", strlen("rtos.bsp.get"));
  232. ret = luat_mock_call(&ctx);
  233. if (ret == 0 && ctx.resp_len > 0 && ctx.resp_len < 64) {
  234. memcpy(bsp_name, ctx.resp_data, ctx.resp_len);
  235. bsp_name[ctx.resp_len] = 0x00;
  236. return bsp_name;
  237. }
  238. #endif
  239. return "PC";
  240. }
  241. /** 设备进入待机模式 */
  242. void luat_os_standy(int timeout) {
  243. (void)timeout;
  244. return; // nop
  245. }
  246. void luat_ota_reboot(int timeout_ms) {
  247. if (timeout_ms > 0)
  248. luat_timer_mdelay(timeout_ms);
  249. exit(0);
  250. }
  251. ///------------------------------------
  252. #include "uv.h"
  253. static void on_free(uv_handle_t* ptr) {
  254. luat_heap_free(ptr);
  255. }
  256. void free_uv_handle(void* ptr) {
  257. if (ptr == NULL)
  258. return;
  259. uv_close((uv_handle_t*)ptr, on_free);
  260. }
  261. void luat_rtos_task_suspend_all(void) {}
  262. void luat_rtos_task_resume_all(void) {}