fix: luat_bot_asset_status_get函数缺返回值类型
[LuatOS.git] / components / antbot / binding / luat_lib_antbot.c
blobe58675e395da5f51c25552a1bc7b0d44b92e08cc
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,并联系蚂蚁链获取技术支持
14 #include "luat_base.h"
15 #include "rotable2.h"
16 #include "luat_zbuff.h"
17 #include "luat_lib_antbot.h"
18 #include "bot_api.h"
20 #define LUAT_LOG_TAG "antbot"
21 #include "luat_log.h"
23 #define BOT_DATA_PAC_OUT_MAX_SIZE 1024
25 bot_msg_type_e g_bot_status = BOT_MSG_UNAVAILABLE;
27 static const char *bot_app_sta_notify_str[] = {
28 "BOT_MSG_UNAVAILABLE",
29 "BOT_MSG_INIT_FAILED",
30 "BOT_MSG_INIT_SUCESS",
31 "BOT_MSG_REG_FAILED",
32 "BOT_MSG_REG_SUCESS",
33 "BOT_MSG_PUB_FAILED",
34 "BOT_MSG_PUB_SUCESS",
35 "BOT_MSG_DATA_VERIFY_FAILED"
38 static void bot_msg_notify_cb(bot_msg_type_e notify_type, void *args)
40 if (g_bot_status != notify_type) {
41 LLOGI("----------------------------------------------------------------------------------");
42 LLOGI("state change: %s ---> %s", bot_app_sta_notify_str[g_bot_status], bot_app_sta_notify_str[notify_type]);
43 LLOGI("----------------------------------------------------------------------------------");
44 g_bot_status = notify_type;
49 初始化
50 @api antbot.init()
51 @return int 0:成功 其他值为失败
52 @usage
54 -- 初始化蚂蚁链的底层适配
55 antbot.init()
57 static int luat_bot_init(lua_State *L)
59 bot_msg_notify_callback_register(bot_msg_notify_cb, NULL);
60 int ret = bot_init();
61 lua_pushinteger(L, ret);
62 return 1;
66 获取客户端状态
67 @api antbot.app_sta_get()
68 @return int 状态码
70 static int luat_bot_app_sta_get(lua_State *L)
72 LLOGD("bot_app_sta_get lua: %d", g_bot_status);
73 lua_pushinteger(L, g_bot_status);
74 return 1;
78 获取SDK版本号
79 @api antbot.version_get()
80 @return string 版本号,如果获取失败返回nil
82 static int luat_bot_version_get(lua_State *L)
84 const char *version = bot_version_get();
86 if (!version)
87 return 0;
88 else
89 lua_pushstring(L, version);
91 //LLOGD("version: %s", version);
92 return 1;
96 asset资源注册
97 @api antbot.asset_register(asset_id, asset_type, asset_dataver)
98 @string asset_id 资源ID
99 @string asset_type 资源类型
100 @string asset_dataver 资源数据版本
101 @return int 0:成功 其他值为失败
103 static int luat_bot_asset_register(lua_State *L)
105 int ret = -1;
106 size_t size = 0;
108 int num_args = lua_gettop(L);
109 if (num_args != 3) {
110 goto exit;
113 if (!lua_isstring(L, 1) || !lua_isstring(L, 2) || !lua_isstring(L, 3)) {
114 LLOGE("asset_register parameters are invalid type, shoule be string.");
115 goto exit;
118 const char *asset_id = luaL_checklstring(L, 1, &size);
119 const char *asset_type = luaL_checklstring(L, 2, &size);
120 const char *asset_dataver = luaL_checklstring(L, 3, &size);
121 ret = bot_asset_register(asset_id, asset_type, asset_dataver);
123 exit:
124 lua_pushinteger(L, ret);
125 return 1;
129 asset资源发布
130 @api antbot.asset_data_publish(data)
131 @string data 资源数据
132 @return int 0:成功 其他值为失败
134 static int luat_bot_data_publish(lua_State *L)
136 int ret = -1;
137 uint8_t *data;
138 size_t data_len = 0;
140 data = luaL_checklstring(L, 1, &data_len);
141 ret = bot_data_publish(data, data_len);
143 lua_pushinteger(L, ret);
144 return 1;
148 获取设备状态
149 @api antbot.device_status_get()
150 @return int 设备状态
152 static int luat_bot_device_status_get(lua_State *L)
154 lua_pushinteger(L, bot_device_status_get());
155 return 1;
159 获取assset状态
160 @api antbot.asset_status_get(asset_id)
161 @string asset_id 资源ID
162 @return int 资源状态
164 static int luat_bot_asset_status_get(lua_State *L)
166 int ret = -1;
167 if (!lua_isstring(L, 1)) {
168 LLOGE("asset_status_get parameter is invalid type, shoule be string.");
169 goto exit;
172 size_t size = 0;
173 const char *asset_id = luaL_checklstring(L, 1, &size);
174 ret = bot_asset_status_get(asset_id);
176 exit:
177 lua_pushinteger(L, ret);
178 return 1;
182 切换channel
183 @api antbot.channel_switch(cmd)
184 @int 0 - 关闭, 1 - 开启
185 @return int 0:成功 其他值为失败
187 static int luat_bot_channel_switch(lua_State *L)
189 int ret = -1;
190 if (!lua_isinteger(L, 1)) {
191 LLOGE("channel_switch parameter is invalid type, shoule be int.");
192 goto exit;
195 size_t size = 0;
196 int cmd = luaL_checkinteger(L, 1);
197 ret = bot_channel_switch(cmd);
199 exit:
200 lua_pushinteger(L, ret);
201 return 1;
205 配置设备
206 @api antbot.config_set(config)
207 @string config 配置内容
208 @return int 0:成功 其他值为失败
210 static int luat_bot_config_set(lua_State *L)
212 int ret = -1;
213 if (!lua_isstring(L, 1)) {
214 LLOGE("config_set parameter is invalid type, shoule be string.");
215 goto exit;
218 size_t size = 0;
219 const char *config = luaL_checklstring(L, 1, &size);
220 ret = bot_config_set(config);
222 exit:
223 lua_pushinteger(L, ret);
224 return 1;
228 获取设备配置
229 @api antbot.config_get()
230 @return string 配置内容
232 static int luat_bot_config_get(lua_State *L)
234 int ret = -1;
236 if (!lua_isinteger(L, 1)) {
237 LLOGE("config_get parameter are invalid type!");
238 goto exit;
241 int len = luaL_checkinteger(L, 1);
242 if (len < BOT_CONFIG_BUF_MIN_LEN) {
243 LLOGE("config_get len must be greater than BOT_CONFIG_BUF_MIN_LEN");
244 goto exit;
247 char *config = luat_heap_malloc(len);
248 if (!config) {
249 LLOGE("config_get out of memory");
250 goto exit;
253 ret = bot_config_get(config, len);
254 lua_pushinteger(L, ret);
255 lua_pushlstring(L, config, len);
256 luat_heap_free(config);
257 return 2;
259 exit:
260 lua_pushinteger(L, ret);
261 return 1;
264 static int luat_bot_data_pac(lua_State *L)
266 int ret = -1;
267 int num_args = lua_gettop(L);
268 if (num_args != 2) {
269 goto exit;
272 if (!lua_isinteger(L, 1) || !lua_isstring(L, 2)) {
273 LLOGE("data_pac parameters are invalid type!");
274 goto exit;
277 int format = luaL_checkinteger(L, 1);
278 size_t data_len;
279 uint8_t *data_in = luaL_checklstring(L, 2, &data_len);
281 int outlen = BOT_DATA_PAC_OUT_MAX_SIZE;
282 uint8_t *data_out = luat_heap_malloc(outlen); // TODO 改成 lua_Buff
283 if (!data_out) {
284 LLOGE("data_pack out of memory");
285 goto exit;
288 ret = bot_data_pac(format, data_in, data_len, data_out, &outlen);
289 lua_pushinteger(L, ret);
290 lua_pushlstring(L, (const char *)data_out, outlen);
291 lua_pushinteger(L, outlen);
292 luat_heap_free(data_out);
293 return 3;
295 exit:
296 lua_pushinteger(L, ret);
297 return 1;
300 static const rotable_Reg_t reg_antbot[] = {
301 { "version_get", ROREG_FUNC(luat_bot_version_get)},
302 { "init", ROREG_FUNC(luat_bot_init)},
303 { "app_sta_get", ROREG_FUNC(luat_bot_app_sta_get)},
304 { "asset_register", ROREG_FUNC(luat_bot_asset_register)},
305 { "data_publish", ROREG_FUNC(luat_bot_data_publish)},
306 { "device_status_get", ROREG_FUNC(luat_bot_device_status_get)},
307 { "asset_status_get", ROREG_FUNC(luat_bot_asset_status_get)},
308 { "channel_switch", ROREG_FUNC(luat_bot_channel_switch)},
309 { "config_set", ROREG_FUNC(luat_bot_config_set)},
310 { "config_get", ROREG_FUNC(luat_bot_config_get)},
311 { "data_pac", ROREG_FUNC(luat_bot_data_pac)},
312 { NULL, ROREG_INT(0)}
315 LUAMOD_API int luaopen_antbot(lua_State *L)
317 luat_newlib2(L, reg_antbot);
318 return 1;