網(wǎng)關(guān)應(yīng)用示例說明
本文介紹生活物聯(lián)網(wǎng)平臺SDK V1.6.0版本中的網(wǎng)關(guān)產(chǎn)品示例代碼。
請您根據(jù)需求下載生活物聯(lián)網(wǎng)平臺SDK。詳細(xì)下載地址請參見獲取SDK。
包含AliOS Things的SDK,網(wǎng)關(guān)參考應(yīng)用程序位于Products/example/linkkit_gateway/,代碼文件如下。
gateway_cmds.c gateway_cmds.h gateway_entry.c gateway_entry.h gateway_main.c gateway_main.h gateway_ut.c gateway_ut.h gateway_api.c gateway_api.h
無AliOS Things的SDK,網(wǎng)關(guān)參考應(yīng)用程序位于examples/linkkit/gateway/,代碼文件如下。
gateway_entry.c gateway_entry.h gateway_main.c gateway_main.h gateway_ut.c gateway_ut.h gateway_api.c gateway_api.h
說明:此處相比于包含AliOS Things版本的SDK,源代碼少了gateway_cmds.c與gateway_cmds.h文件,其他代碼一樣。
下面詳細(xì)介紹網(wǎng)關(guān)參考應(yīng)用的文件。
gateway_entry.c
gateway_entry.c為應(yīng)用程序主入口文件。
本文中,四元組分別指的是ProductKey、ProductSecret、DeviceName和DeviceSecret。
int application_start(int argc, char **argv)
{
... ... ... ... ... ... ... ... ...
設(shè)置默認(rèn)的日志級別
#ifdef DEFAULT_LOG_LEVEL_DEBUG
IOT_SetLogLevel(IOT_LOG_DEBUG);
#else
#ifdef CSP_LINUXHOST
IOT_SetLogLevel(IOT_LOG_DEBUG);
#else
IOT_SetLogLevel(IOT_LOG_INFO);
#endif
#endif
加載四元組信息,用戶需要實(shí)現(xiàn)該函數(shù)
load_device_meta_info();
... ... ... ... ... ... ... ... ...
注冊連云成功回調(diào)函數(shù),執(zhí)行ota初始化
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
注冊串口命令,代碼在gateway_cmds.c中
#ifdef CONFIG_AOS_CLI
gateway_register_cmds();
#endif
... ... ... ... ... ... ... ... ...
main函數(shù)的loop循環(huán)
aos_loop_run();
return 0;
}
gateway_main.c
由于子設(shè)備的某些入網(wǎng)操作是同步阻塞式的,該文件里已創(chuàng)建了如下兩個(gè)線程。
user_dispatch_yield線程:執(zhí)行Link Kit SDK代碼及注冊的用戶回調(diào)函數(shù)。
gateway_main主線程:調(diào)用Link Kit SDK接口,以及處理與子設(shè)備間的通信。
gateway_cmds.c
此文件主要包括命令行相關(guān)的功能代碼,方便測試和調(diào)試操作。
gateway_ut.c
此文件為用戶測試代碼,例如kv模擬子設(shè)備信息、get topo list之后上線子設(shè)備、permit join的時(shí)候模擬添加子設(shè)備等。
gateway_api.c
此文件是對子設(shè)備操作的SDK接口函數(shù)的封裝,支持的功能有添加子設(shè)備、刪除子設(shè)備、復(fù)位子設(shè)備、查詢子設(shè)備ID等操作,您可以直接使用該接口函數(shù)。
添加一個(gè)子設(shè)備
添加一個(gè)子設(shè)備需要對SDK進(jìn)行三次接口調(diào)用。
調(diào)用
IOT_Linkkit_Open
:將子設(shè)備添加到網(wǎng)關(guān)本地的數(shù)據(jù)結(jié)構(gòu)。調(diào)用
IOT_Linkkit_Connect
:將子設(shè)備注冊并添加拓?fù)潢P(guān)系到云端。調(diào)用
IOT_Linkkit_Report
:告訴云端子設(shè)備上線了。
GATEWAY_API int gateway_add_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate) { ... ... ... ... ... ... ... ... ... devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_SLAVE, subdev_mate); ... ... ... ... ... ... ... ... ... res = IOT_Linkkit_Connect(devid); ... ... ... ... ... ... ... ... ... res = IOT_Linkkit_Report(devid, ITM_MSG_LOGIN, NULL, 0); ... ... ... ... ... ... ... ... ... }
批量添加子設(shè)備
通過
IOT_Linkkit_Report( ... ITM_MSG_CONNECT_SUBDEV ...)
支持最多一次批量添加五個(gè)子設(shè)備。如果批量添加成功就調(diào)用
IOT_Linkkit_Report(subdev_id, ITM_MSG_LOGIN, NULL, 0)
上線對應(yīng)的子設(shè)備。
GATEWAY_API int gateway_add_multi_subdev(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_list, int subdev_num) { ... ... ... ... ... ... ... ... ... connect_times = subdev_num / GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM + (((subdev_num % GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM) == 0) ? 0 : 1); for (index = 0; index < connect_times; index++) { ... ... ... ... ... ... ... ... ... res = IOT_Linkkit_Report(master_devid, ITM_MSG_CONNECT_SUBDEV, (unsigned char *)p_cur_subdev, sizeof(iotx_linkkit_dev_meta_info_t) * cur_subdev_num); if (res == SUCCESS_RETURN) { 批量上線子設(shè)備 res = IOT_Linkkit_Report(subdev_id, ITM_MSG_BATCH_LOGIN, (unsigned char *)p_cur_subdev, sizeof(iotx_linkkit_dev_meta_info_t) * cur_subdev_num); ... ... ... ... ... ... ... ... ... } } return res; }
刪除一個(gè)子設(shè)備
調(diào)用函數(shù)
IOT_Linkkit_Report(subdev_id, ITM_MSG_DELETE_TOPO, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t))
刪除一個(gè)子設(shè)備和網(wǎng)關(guān)的拓?fù)潢P(guān)系,同時(shí)會(huì)釋放網(wǎng)關(guān)中子設(shè)備的資源。說明刪除子設(shè)備時(shí),優(yōu)先根據(jù)子設(shè)備的ProductKey和DeviceName查找子設(shè)備,找到就直接刪除;如果找不到子設(shè)備,則根據(jù)第一個(gè)參數(shù)subdev_id繼續(xù)查找。
GATEWAY_API int gateway_del_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate) { ... ... ... ... ... ... ... ... ... //Here pk and dn of subdev is priorior than subdev id ret = IOT_Linkkit_Report(subdev_id, ITM_MSG_DELETE_TOPO, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t)); ... ... ... ... ... ... ... ... ... }
復(fù)位一個(gè)子設(shè)備
調(diào)用函數(shù)
IOT_Linkkit_Report(subdev_id, ITM_MSG_SUBDEV_RESET, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t))
復(fù)位一個(gè)子設(shè)備,復(fù)位子設(shè)備除了會(huì)刪除和網(wǎng)關(guān)的拓?fù)潢P(guān)系之外,還會(huì)刪除子設(shè)備和手機(jī)用戶賬號的綁定關(guān)系,同時(shí)會(huì)釋放網(wǎng)關(guān)中子設(shè)備的資源。說明復(fù)位子設(shè)備時(shí),優(yōu)先根據(jù)子設(shè)備的ProductKey和DeviceName查找子設(shè)備,找到就直接復(fù)位;如果找不到子設(shè)備,則根據(jù)第一個(gè)參數(shù)subdev_id繼續(xù)查找。
GATEWAY_API int gateway_reset_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate) { ... ... ... ... ... ... ... ... ... //Here pk and dn of subdev is priorior than subdev id ret = IOT_Linkkit_Report(subdev_id, ITM_MSG_SUBDEV_RESET, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t)); ... ... ... ... ... ... ... ... ... }
查詢一個(gè)子設(shè)備的DeviceID
調(diào)用函數(shù)
IOT_Linkkit_Query(master_devid, ITM_MSG_QUERY_SUBDEV_ID, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));
根據(jù)子設(shè)備的ProductKey和DeviceName信息查詢一個(gè)子設(shè)備的DeviceID(數(shù)據(jù)結(jié)構(gòu)索引,為大于0的整數(shù))。GATEWAY_API int gateway_query_subdev_id(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_mate) { ... ... ... ... ... ... ... ... ... subdev_id = IOT_Linkkit_Query(master_devid, ITM_MSG_QUERY_SUBDEV_ID, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t)); ... ... ... ... ... ... ... ... ... }
批量上線子設(shè)備接口
該接口函數(shù)會(huì)將輸入的子設(shè)備數(shù)組拆分成五個(gè)一組進(jìn)行批量上線。
GATEWAY_API int gateway_batch_login(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_list, int subdev_num) { ... ... ... ... ... ... ... ... ... for (index = 0; index < connect_times; index++) { ... ... ... ... ... ... ... ... ... res = IOT_Linkkit_Report(subdev_id, ITM_MSG_BATCH_LOGIN, (unsigned char *)p_cur_subdev, sizeof(iotx_linkkit_dev_meta_info_t) * cur_subdev_num); ... ... ... ... ... ... ... ... ... return res; }
批量下線子設(shè)備接口
該接口函數(shù)會(huì)將輸入的子設(shè)備數(shù)組拆分成五個(gè)一組進(jìn)行批量下線。
GATEWAY_API int gateway_batch_logout(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_list, int subdev_num) { ... ... ... ... ... ... ... ... ... for (index = 0; index < connect_times; index++) { ... ... ... ... ... ... ... ... ... res = IOT_Linkkit_Report(subdev_id, ITM_MSG_BATCH_LOGOUT, (unsigned char *)p_cur_subdev, sizeof(iotx_linkkit_dev_meta_info_t) * cur_subdev_num); ... ... ... ... ... ... ... ... ... } return res; }