luat_mock.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "luat_base.h"
  2. #include "luat_fs.h"
  3. #include "luat_malloc.h"
  4. #include "luat_mock.h"
  5. #define LUAT_LOG_TAG "mock"
  6. #include "luat_log.h"
  7. static lua_State *mock_L;
  8. static char mock_file_path[1024];
  9. static int mock_main(lua_State *L);
  10. static const luaL_Reg loadedlibs[] = {
  11. {"_G", luaopen_base}, // _G
  12. {LUA_LOADLIBNAME, luaopen_package}, // require
  13. {LUA_COLIBNAME, luaopen_coroutine}, // coroutine协程库
  14. {LUA_TABLIBNAME, luaopen_table}, // table库,操作table类型的数据结构
  15. {LUA_IOLIBNAME, luaopen_io}, // io库,操作文件
  16. {LUA_OSLIBNAME, luaopen_os}, // os库,已精简
  17. {LUA_STRLIBNAME, luaopen_string}, // string库,字符串操作
  18. {LUA_MATHLIBNAME, luaopen_math}, // math 数值计算
  19. {LUA_UTF8LIBNAME, luaopen_utf8},
  20. {LUA_DBLIBNAME, luaopen_debug}, // debug库,已精简
  21. #if defined(LUA_COMPAT_BITLIB)
  22. {LUA_BITLIBNAME, luaopen_bit32}, // 不太可能启用
  23. #endif
  24. // 外设类
  25. #ifdef LUAT_USE_UART
  26. {"uart", luaopen_uart}, // 串口操作
  27. #endif
  28. #ifdef LUAT_USE_GPIO
  29. {"gpio", luaopen_gpio}, // GPIO脚的操作
  30. #endif
  31. #ifdef LUAT_USE_I2C
  32. {"i2c", luaopen_i2c}, // I2C操作
  33. #endif
  34. #ifdef LUAT_USE_SPI
  35. {"spi", luaopen_spi}, // SPI操作
  36. #endif
  37. #ifdef LUAT_USE_ADC
  38. {"adc", luaopen_adc}, // ADC模块
  39. #endif
  40. #ifdef LUAT_USE_PWM
  41. {"pwm", luaopen_pwm}, // PWM模块
  42. #endif
  43. #ifdef LUAT_USE_WDT
  44. {"wdt", luaopen_wdt}, // watchdog模块
  45. #endif
  46. #ifdef LUAT_USE_PM
  47. {"pm", luaopen_pm}, // 电源管理模块
  48. #endif
  49. #ifdef LUAT_USE_MCU
  50. {"mcu", luaopen_mcu}, // MCU特有的一些操作
  51. #endif
  52. #ifdef LUAT_USE_RTC
  53. {"rtc", luaopen_rtc}, // 实时时钟
  54. #endif
  55. #ifdef LUAT_USE_OTP
  56. {"otp", luaopen_otp}, // OTP
  57. #endif
  58. //-----------------------------------------------------------------
  59. {"log", luaopen_log}, // 日志库
  60. {"timer", luaopen_timer}, // 延时库
  61. {"pack", luaopen_pack}, // pack.pack/pack.unpack
  62. {"json", luaopen_cjson}, // json
  63. {"zbuff", luaopen_zbuff}, //
  64. {"crypto", luaopen_crypto},
  65. #ifdef LUAT_USE_RSA
  66. {"rsa", luaopen_rsa},
  67. #endif
  68. #ifdef LUAT_USE_MINIZ
  69. {"miniz", luaopen_miniz},
  70. #endif
  71. #ifdef LUAT_USE_PROTOBUF
  72. {"protobuf", luaopen_protobuf},
  73. #endif
  74. #ifdef LUAT_USE_IOTAUTH
  75. {"iotauth", luaopen_iotauth},
  76. #endif
  77. #ifdef LUAT_USE_ICONV
  78. {"iconv", luaopen_iconv},
  79. #endif
  80. #ifdef LUAT_USE_BIT64
  81. {"bit64", luaopen_bit64},
  82. #endif
  83. #ifdef LUAT_USE_FS
  84. {"fs", luaopen_fs}, // 文件系统库,在io库之外再提供一些方法
  85. #endif
  86. #ifdef LUAT_USE_MQTTCORE
  87. {"mqttcore",luaopen_mqttcore}, // MQTT 协议封装
  88. #endif
  89. {NULL, NULL}
  90. };
  91. int luat_mock_init(const char* path) {
  92. mock_L = lua_newstate(luat_heap_alloc, NULL);
  93. memcpy(mock_file_path, path, strlen(path) + 1);
  94. LLOGI("mock脚本路径 %s", mock_file_path);
  95. if (mock_L == NULL || mock_file_path[0] == 0x00) {
  96. return -1;
  97. }
  98. const luaL_Reg *lib;
  99. /* "require" functions from 'loadedlibs' and set results to global table */
  100. for (lib = loadedlibs; lib->func; lib++) {
  101. luaL_requiref(mock_L, lib->name, lib->func, 1);
  102. lua_pop(mock_L, 1); /* remove lib */
  103. //extern void print_list_mem(const char* name);
  104. //print_list_mem(lib->name);
  105. }
  106. return 0;
  107. }
  108. int luat_mock_call(luat_mock_ctx_t* ctx) {
  109. if (mock_L == NULL) {
  110. return -0xFF;
  111. }
  112. if (mock_file_path[0] == 0x00) {
  113. return -2;
  114. }
  115. const char *resp;
  116. lua_Integer intVal;
  117. lua_Number numVal;
  118. lua_pushcfunction(mock_L, &mock_main);
  119. lua_pushstring(mock_L, ctx->key);
  120. lua_pushlstring(mock_L, ctx->req_data, ctx->req_len);
  121. int ret = lua_pcall(mock_L, 2, 2, 0);
  122. if (ret) {
  123. LLOGE("pcall %d %s", ret, lua_tostring(mock_L, -1));
  124. return ret;
  125. }
  126. else {
  127. ctx->resp_code = lua_tointeger(mock_L, 1);
  128. ctx->resp_type = lua_type(mock_L, 2);
  129. switch (lua_type(mock_L, 2))
  130. {
  131. case LUA_TNIL: // 空值
  132. /* code */
  133. break;
  134. case LUA_TBOOLEAN:
  135. ctx->resp_data = luat_heap_malloc(1);
  136. ctx->resp_data[0] = lua_toboolean(mock_L, 2);
  137. ctx->resp_len = 1;
  138. break;
  139. case LUA_TNUMBER:
  140. ctx->resp_data = luat_heap_malloc(sizeof(lua_Integer));
  141. ctx->resp_len = sizeof(lua_Integer);
  142. if (lua_isinteger(mock_L, 2)) {
  143. intVal = lua_tointeger(mock_L, 2);
  144. memcpy(ctx->resp_data, &intVal, sizeof(lua_Integer));
  145. }
  146. else {
  147. numVal = lua_tonumber(mock_L, 2);
  148. memcpy(ctx->resp_data, &numVal, sizeof(lua_Integer));
  149. }
  150. break;
  151. case LUA_TSTRING:
  152. resp = lua_tolstring(mock_L, 2, &ctx->resp_len);
  153. if (ctx->resp_len > 0) {
  154. ctx->resp_data = luat_heap_malloc(ctx->resp_len + 1);
  155. memcpy(ctx->resp_data, resp, ctx->resp_len + 1);
  156. }
  157. break;
  158. default:
  159. break;
  160. }
  161. }
  162. return ret;
  163. }
  164. static int load_mock_lua_file(lua_State *L) {
  165. const char* name = mock_file_path;
  166. const char* path = mock_file_path;
  167. // 加载mock.lua
  168. //----------------------------------------------
  169. size_t len = 0;
  170. int ret = 0;
  171. // LLOGD("把%s当做main.lua运行", path);
  172. char tmpname[512] = {0};
  173. FILE *f = fopen(name, "rb");
  174. if (!f)
  175. {
  176. LLOGE("文件不存在 %s", path);
  177. return -3;
  178. }
  179. fseek(f, 0, SEEK_END);
  180. len = ftell(f);
  181. fseek(f, 0, SEEK_SET);
  182. // void* fptr = luat_heap_malloc(len);
  183. char *tmp = luat_heap_malloc(len);
  184. if (tmp == NULL)
  185. {
  186. fclose(f);
  187. LLOGE("文件太大,内存放不下 %s", path);
  188. return -3;
  189. }
  190. fread(tmp, 1, len, f);
  191. fclose(f);
  192. for (size_t i = strlen(path); i > 0; i--)
  193. {
  194. if (path[i - 1] == '/' || path[i - 1] == '\\')
  195. {
  196. memcpy(tmpname, name + i, strlen(path) - 1);
  197. break;
  198. }
  199. }
  200. if (tmpname[0] == 0x00)
  201. {
  202. memcpy(tmpname, path, strlen(path));
  203. }
  204. //----------------------------------------------
  205. ret = luaL_loadbufferx(L, tmp, len, name, NULL);
  206. if (ret) {
  207. LLOGE("文件加载失败 %s %s", name, lua_tostring(L, -1));
  208. return -3;
  209. }
  210. ret = lua_pcall(L, 0, 1, 0);
  211. if (ret) {
  212. LLOGE("mock加载失败 %s %s", name, lua_tostring(L, -1));
  213. return -4;
  214. }
  215. // LLOGD("栈顶对象的类型 %d", lua_type(L, -1));
  216. lua_setglobal(L, "mockf");
  217. return 0;
  218. }
  219. static int mock_main(lua_State *L) {
  220. int ret = 0;
  221. ret = load_mock_lua_file(L);
  222. if (ret) {
  223. LLOGD("加载mock脚本失败 %d", ret);
  224. return ret;
  225. }
  226. lua_getglobal(L, "mockf");
  227. lua_pushvalue(L, 1);
  228. lua_pushvalue(L, 2);
  229. lua_call(L, 2, 2);
  230. return 2;
  231. }