10 -- Copyright (C) 2015-2023 Ant Group Holding Limited
11 -- 本模块由蚂蚁链提供实现并开放给用户使用
12 -- 具体用法请查阅demo,并联系蚂蚁链获取技术支持
14 #include "luat_base.h"
16 #include "luat_zbuff.h"
17 #include "luat_lib_antbot.h"
20 #define LUAT_LOG_TAG "antbot"
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",
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
;
51 @return int 0:成功 其他值为失败
57 static int luat_bot_init(lua_State
*L
)
59 bot_msg_notify_callback_register(bot_msg_notify_cb
, NULL
);
61 lua_pushinteger(L
, ret
);
67 @api antbot.app_sta_get()
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
);
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();
89 lua_pushstring(L
, version
);
91 //LLOGD("version: %s", version);
97 @api antbot.asset_register(asset_id, asset_type, asset_dataver)
99 @string asset_type 资源类型
100 @string asset_dataver 资源数据版本
101 @return int 0:成功 其他值为失败
103 static int luat_bot_asset_register(lua_State
*L
)
108 int num_args
= lua_gettop(L
);
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.");
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
);
124 lua_pushinteger(L
, ret
);
130 @api antbot.asset_data_publish(data)
132 @return int 0:成功 其他值为失败
134 static int luat_bot_data_publish(lua_State
*L
)
140 data
= luaL_checklstring(L
, 1, &data_len
);
141 ret
= bot_data_publish(data
, data_len
);
143 lua_pushinteger(L
, ret
);
149 @api antbot.device_status_get()
152 static int luat_bot_device_status_get(lua_State
*L
)
154 lua_pushinteger(L
, bot_device_status_get());
160 @api antbot.asset_status_get(asset_id)
161 @string asset_id 资源ID
164 static int luat_bot_asset_status_get(lua_State
*L
)
167 if (!lua_isstring(L
, 1)) {
168 LLOGE("asset_status_get parameter is invalid type, shoule be string.");
173 const char *asset_id
= luaL_checklstring(L
, 1, &size
);
174 ret
= bot_asset_status_get(asset_id
);
177 lua_pushinteger(L
, ret
);
183 @api antbot.channel_switch(cmd)
185 @return int 0:成功 其他值为失败
187 static int luat_bot_channel_switch(lua_State
*L
)
190 if (!lua_isinteger(L
, 1)) {
191 LLOGE("channel_switch parameter is invalid type, shoule be int.");
196 int cmd
= luaL_checkinteger(L
, 1);
197 ret
= bot_channel_switch(cmd
);
200 lua_pushinteger(L
, ret
);
206 @api antbot.config_set(config)
208 @return int 0:成功 其他值为失败
210 static int luat_bot_config_set(lua_State
*L
)
213 if (!lua_isstring(L
, 1)) {
214 LLOGE("config_set parameter is invalid type, shoule be string.");
219 const char *config
= luaL_checklstring(L
, 1, &size
);
220 ret
= bot_config_set(config
);
223 lua_pushinteger(L
, ret
);
229 @api antbot.config_get()
232 static int luat_bot_config_get(lua_State
*L
)
236 if (!lua_isinteger(L
, 1)) {
237 LLOGE("config_get parameter are invalid type!");
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");
247 char *config
= luat_heap_malloc(len
);
249 LLOGE("config_get out of memory");
253 ret
= bot_config_get(config
, len
);
254 lua_pushinteger(L
, ret
);
255 lua_pushlstring(L
, config
, len
);
256 luat_heap_free(config
);
260 lua_pushinteger(L
, ret
);
264 static int luat_bot_data_pac(lua_State
*L
)
267 int num_args
= lua_gettop(L
);
272 if (!lua_isinteger(L
, 1) || !lua_isstring(L
, 2)) {
273 LLOGE("data_pac parameters are invalid type!");
277 int format
= luaL_checkinteger(L
, 1);
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
284 LLOGE("data_pack out of memory");
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
);
296 lua_pushinteger(L
, ret
);
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
);