luat_lib_nimble.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_malloc.h"
  4. #include "luat_spi.h"
  5. #include "luat_nimble.h"
  6. #define LUAT_LOG_TAG "nimble"
  7. #include "luat_log.h"
  8. static int l_nimble_init(lua_State* L) {
  9. int rc = 0;
  10. char* name = NULL;
  11. if(lua_isstring(L, 1)) {
  12. name = lua_tostring(L, 1);
  13. }
  14. rc = luat_nimble_init(0xFF,name);
  15. if (rc) {
  16. lua_pushboolean(L, 0);
  17. lua_pushinteger(L, rc);
  18. return 2;
  19. }
  20. else {
  21. lua_pushboolean(L, 1);
  22. return 1;
  23. }
  24. }
  25. static int l_nimble_deinit(lua_State* L) {
  26. int rc = 0;
  27. rc = luat_nimble_deinit();
  28. if (rc) {
  29. lua_pushboolean(L, 0);
  30. lua_pushinteger(L, rc);
  31. return 2;
  32. }
  33. else {
  34. lua_pushboolean(L, 1);
  35. return 1;
  36. }
  37. }
  38. static int l_nimble_debug(lua_State* L) {
  39. int level;
  40. if (lua_gettop(L) > 0)
  41. level = luat_nimble_trace_level(luaL_checkinteger(L, 1));
  42. else
  43. level = luat_nimble_trace_level(-1);
  44. lua_pushinteger(L, level);
  45. return 1;
  46. }
  47. static int l_nimble_server_init(lua_State* L) {
  48. test_server_api_init();
  49. return 0;
  50. }
  51. static int l_nimble_server_deinit(lua_State* L) {
  52. test_server_api_deinit();
  53. return 0;
  54. }
  55. #include "wm_bt_config.h"
  56. #include "wm_params.h"
  57. #include "wm_param.h"
  58. #include "wm_ble.h"
  59. int tls_nimble_gap_adv(wm_ble_adv_type_t type, int duration);
  60. static int l_nimble_gap_adv(lua_State* L) {
  61. wm_ble_adv_type_t type = luaL_checkinteger(L, 1);
  62. int duration = luaL_optinteger(L, 2, 0);
  63. int ret = tls_nimble_gap_adv(type, duration);
  64. lua_pushboolean(L, ret == 0 ? 1 : 0);
  65. lua_pushinteger(L, ret);
  66. return 2;
  67. }
  68. static int l_nimble_send_msg(lua_State *L) {
  69. int conn_id = luaL_checkinteger(L, 1);
  70. int handle_id = luaL_checkinteger(L, 2);
  71. size_t len;
  72. const char* data = luaL_checklstring(L, 3, &len);
  73. int ret = tls_ble_server_api_send_msg(data, len);
  74. lua_pushboolean(L, ret == 0 ? 1 : 0);
  75. lua_pushinteger(L, ret);
  76. return 2;
  77. }
  78. int
  79. ble_ibeacon_set_adv_data(void *uuid128, uint16_t major,
  80. uint16_t minor, int8_t measured_power);
  81. static int l_ibeacon_set_adv_data(lua_State *L) {
  82. uint8_t buff[16];
  83. size_t len = 0;
  84. const char* data = luaL_checklstring(L, 1, &len);
  85. memset(buff, 0, 16);
  86. memcpy(buff, data, len > 16 ? 16 : len);
  87. uint16_t major = luaL_optinteger(L, 2, 2);
  88. uint16_t minor = luaL_optinteger(L, 3, 10);
  89. int8_t measured_power = luaL_optinteger(L, 4, 0);
  90. int ret = ble_ibeacon_set_adv_data(buff, major, minor, measured_power);
  91. lua_pushboolean(L, ret == 0 ? 1 : 0);
  92. lua_pushinteger(L, ret);
  93. return 2;
  94. }
  95. static int l_gap_adv_start(lua_State *L) {
  96. return 0;
  97. }
  98. static int l_gap_adv_stop(lua_State *L) {
  99. return 0;
  100. }
  101. #include "rotable.h"
  102. static const rotable_Reg reg_nimble[] =
  103. {
  104. { "init", l_nimble_init, 0},
  105. { "deinit", l_nimble_deinit, 0},
  106. { "debug", l_nimble_debug, 0},
  107. { "server_init", l_nimble_server_init, 0},
  108. { "server_deinit", l_nimble_server_deinit, 0},
  109. { "gap_adv", l_nimble_gap_adv, 0},
  110. { "send_msg", l_nimble_send_msg, 0},
  111. // for ibeacon
  112. { "ibeacon_set_adv_data", l_ibeacon_set_adv_data, 0},
  113. // { "gap_adv_start", l_gap_adv_start, 0},
  114. // { "gap_adv_stop", l_gap_adv_stop, 0},
  115. { NULL, NULL , 0}
  116. };
  117. LUAMOD_API int luaopen_nimble( lua_State *L ) {
  118. luat_newlib(L, reg_nimble);
  119. return 1;
  120. }