luat_lib_antbot.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. @module antbot
  3. @summary 蚂蚁链
  4. @catalog 扩展API
  5. @version 1.0
  6. @date 2022.12.18
  7. @demo antbot
  8. @tag LUAT_USE_ANTBOT
  9. @usage
  10. -- Copyright (C) 2015-2023 Ant Group Holding Limited
  11. -- 本模块由蚂蚁链提供实现并开放给用户使用
  12. -- 具体用法请查阅demo,并联系蚂蚁链获取技术支持
  13. */
  14. #include "luat_base.h"
  15. #ifdef LUAT_USE_ANTBOT
  16. #include "rotable2.h"
  17. #include "luat_zbuff.h"
  18. #include "luat_lib_antbot.h"
  19. #include "bot_api.h"
  20. #define LUAT_LOG_TAG "antbot"
  21. #include "luat_log.h"
  22. #define BOT_DATA_PAC_OUT_MAX_SIZE 1024
  23. bot_msg_type_e g_bot_status = BOT_MSG_UNAVAILABLE;
  24. static const char *bot_app_sta_notify_str[] = {
  25. "BOT_MSG_UNAVAILABLE",
  26. "BOT_MSG_INIT_FAILED",
  27. "BOT_MSG_INIT_SUCESS",
  28. "BOT_MSG_REG_FAILED",
  29. "BOT_MSG_REG_SUCESS",
  30. "BOT_MSG_PUB_FAILED",
  31. "BOT_MSG_PUB_SUCESS",
  32. "BOT_MSG_DATA_VERIFY_FAILED"
  33. };
  34. static void bot_msg_notify_cb(bot_msg_type_e notify_type, void *args)
  35. {
  36. if (g_bot_status != notify_type) {
  37. LLOGI("----------------------------------------------------------------------------------");
  38. LLOGI("state change: %s ---> %s", bot_app_sta_notify_str[g_bot_status], bot_app_sta_notify_str[notify_type]);
  39. LLOGI("----------------------------------------------------------------------------------");
  40. g_bot_status = notify_type;
  41. }
  42. }
  43. /*
  44. 初始化
  45. @api antbot.init()
  46. @return int 0:成功 其他值为失败
  47. @usage
  48. -- 初始化蚂蚁链的底层适配
  49. antbot.init()
  50. */
  51. static int luat_bot_init(lua_State *L)
  52. {
  53. bot_msg_notify_callback_register(bot_msg_notify_cb, NULL);
  54. int ret = bot_init();
  55. lua_pushinteger(L, ret);
  56. return 1;
  57. }
  58. /*
  59. 获取客户端状态
  60. @api antbot.app_sta_get()
  61. @return int 状态码
  62. */
  63. static int luat_bot_app_sta_get(lua_State *L)
  64. {
  65. LLOGD("bot_app_sta_get lua: %d", g_bot_status);
  66. lua_pushinteger(L, g_bot_status);
  67. return 1;
  68. }
  69. /*
  70. 获取SDK版本号
  71. @api antbot.version_get()
  72. @return string 版本号,如果获取失败返回nil
  73. */
  74. static int luat_bot_version_get(lua_State *L)
  75. {
  76. const char *version = bot_version_get();
  77. if (!version)
  78. return 0;
  79. else
  80. lua_pushstring(L, version);
  81. //LLOGD("version: %s", version);
  82. return 1;
  83. }
  84. /*
  85. asset资源注册
  86. @api antbot.asset_register(asset_id, asset_type, asset_dataver)
  87. @string asset_id 资源ID
  88. @string asset_type 资源类型
  89. @string asset_dataver 资源数据版本
  90. @return int 0:成功 其他值为失败
  91. */
  92. static int luat_bot_asset_register(lua_State *L)
  93. {
  94. int ret = -1;
  95. size_t size = 0;
  96. int num_args = lua_gettop(L);
  97. if (num_args != 3) {
  98. goto exit;
  99. }
  100. if (!lua_isstring(L, 1) || !lua_isstring(L, 2) || !lua_isstring(L, 3)) {
  101. LLOGE("asset_register parameters are invalid type, shoule be string.");
  102. goto exit;
  103. }
  104. const char *asset_id = luaL_checklstring(L, 1, &size);
  105. const char *asset_type = luaL_checklstring(L, 2, &size);
  106. const char *asset_dataver = luaL_checklstring(L, 3, &size);
  107. ret = bot_asset_register(asset_id, asset_type, asset_dataver);
  108. exit:
  109. lua_pushinteger(L, ret);
  110. return 1;
  111. }
  112. /*
  113. asset资源发布
  114. @api antbot.asset_data_publish(data)
  115. @string data 资源数据
  116. @return int 0:成功 其他值为失败
  117. */
  118. static int luat_bot_data_publish(lua_State *L)
  119. {
  120. int ret = -1;
  121. uint8_t *data;
  122. size_t data_len = 0;
  123. data = luaL_checklstring(L, 1, &data_len);
  124. ret = bot_data_publish(data, data_len);
  125. lua_pushinteger(L, ret);
  126. return 1;
  127. }
  128. /*
  129. 获取设备状态
  130. @api antbot.device_status_get()
  131. @return int 设备状态
  132. */
  133. static int luat_bot_device_status_get(lua_State *L)
  134. {
  135. lua_pushinteger(L, bot_device_status_get());
  136. return 1;
  137. }
  138. /*
  139. 获取assset状态
  140. @api antbot.asset_status_get(asset_id)
  141. @string asset_id 资源ID
  142. @return int 资源状态
  143. */
  144. static int luat_bot_asset_status_get(lua_State *L)
  145. {
  146. int ret = -1;
  147. if (!lua_isstring(L, 1)) {
  148. LLOGE("asset_status_get parameter is invalid type, shoule be string.");
  149. goto exit;
  150. }
  151. size_t size = 0;
  152. const char *asset_id = luaL_checklstring(L, 1, &size);
  153. ret = bot_asset_status_get(asset_id);
  154. exit:
  155. lua_pushinteger(L, ret);
  156. return 1;
  157. }
  158. /*
  159. 切换channel
  160. @api antbot.channel_switch(cmd)
  161. @int 0 - 关闭, 1 - 开启
  162. @return int 0:成功 其他值为失败
  163. */
  164. static int luat_bot_channel_switch(lua_State *L)
  165. {
  166. int ret = -1;
  167. if (!lua_isinteger(L, 1)) {
  168. LLOGE("channel_switch parameter is invalid type, shoule be int.");
  169. goto exit;
  170. }
  171. size_t size = 0;
  172. int cmd = luaL_checkinteger(L, 1);
  173. ret = bot_channel_switch(cmd);
  174. exit:
  175. lua_pushinteger(L, ret);
  176. return 1;
  177. }
  178. /*
  179. 配置设备
  180. @api antbot.config_set(config)
  181. @string config 配置内容
  182. @return int 0:成功 其他值为失败
  183. */
  184. static int luat_bot_config_set(lua_State *L)
  185. {
  186. int ret = -1;
  187. if (!lua_isstring(L, 1)) {
  188. LLOGE("config_set parameter is invalid type, shoule be string.");
  189. goto exit;
  190. }
  191. size_t size = 0;
  192. const char *config = luaL_checklstring(L, 1, &size);
  193. ret = bot_config_set(config);
  194. exit:
  195. lua_pushinteger(L, ret);
  196. return 1;
  197. }
  198. /*
  199. 获取设备配置
  200. @api antbot.config_get()
  201. @return string 配置内容
  202. */
  203. static int luat_bot_config_get(lua_State *L)
  204. {
  205. int ret = -1;
  206. if (!lua_isinteger(L, 1)) {
  207. LLOGE("config_get parameter are invalid type!");
  208. goto exit;
  209. }
  210. int len = luaL_checkinteger(L, 1);
  211. if (len < BOT_CONFIG_BUF_MIN_LEN) {
  212. LLOGE("config_get len must be greater than BOT_CONFIG_BUF_MIN_LEN");
  213. goto exit;
  214. }
  215. char *config = luat_heap_malloc(len);
  216. if (!config) {
  217. LLOGE("config_get out of memory");
  218. goto exit;
  219. }
  220. ret = bot_config_get(config, len);
  221. lua_pushinteger(L, ret);
  222. lua_pushlstring(L, config, len);
  223. luat_heap_free(config);
  224. return 2;
  225. exit:
  226. lua_pushinteger(L, ret);
  227. return 1;
  228. }
  229. static int luat_bot_data_pac(lua_State *L)
  230. {
  231. int ret = -1;
  232. int num_args = lua_gettop(L);
  233. if (num_args != 2) {
  234. goto exit;
  235. }
  236. if (!lua_isinteger(L, 1) || !lua_isstring(L, 2)) {
  237. LLOGE("data_pac parameters are invalid type!");
  238. goto exit;
  239. }
  240. int format = luaL_checkinteger(L, 1);
  241. size_t data_len;
  242. uint8_t *data_in = luaL_checklstring(L, 2, &data_len);
  243. int outlen = BOT_DATA_PAC_OUT_MAX_SIZE;
  244. uint8_t *data_out = luat_heap_malloc(outlen); // TODO 改成 lua_Buff
  245. if (!data_out) {
  246. LLOGE("data_pack out of memory");
  247. goto exit;
  248. }
  249. ret = bot_data_pac(format, data_in, data_len, data_out, &outlen);
  250. lua_pushinteger(L, ret);
  251. lua_pushlstring(L, (const char *)data_out, outlen);
  252. lua_pushinteger(L, outlen);
  253. luat_heap_free(data_out);
  254. return 3;
  255. exit:
  256. lua_pushinteger(L, ret);
  257. return 1;
  258. }
  259. static const rotable_Reg_t reg_antbot[] = {
  260. { "version_get", ROREG_FUNC(luat_bot_version_get)},
  261. { "init", ROREG_FUNC(luat_bot_init)},
  262. { "app_sta_get", ROREG_FUNC(luat_bot_app_sta_get)},
  263. { "asset_register", ROREG_FUNC(luat_bot_asset_register)},
  264. { "data_publish", ROREG_FUNC(luat_bot_data_publish)},
  265. { "device_status_get", ROREG_FUNC(luat_bot_device_status_get)},
  266. { "asset_status_get", ROREG_FUNC(luat_bot_asset_status_get)},
  267. { "channel_switch", ROREG_FUNC(luat_bot_channel_switch)},
  268. { "config_set", ROREG_FUNC(luat_bot_config_set)},
  269. { "config_get", ROREG_FUNC(luat_bot_config_get)},
  270. { "data_pac", ROREG_FUNC(luat_bot_data_pac)},
  271. { NULL, ROREG_INT(0)}
  272. };
  273. LUAMOD_API int luaopen_antbot(lua_State *L)
  274. {
  275. luat_newlib2(L, reg_antbot);
  276. return 1;
  277. }
  278. #endif