This topic describes the sample code for a gateway application that is provided in the IoT Platform software development kit (SDK) V1.6.0.
You can download the IoT Platform SDK. For more information, see Get the SDK.
For the SDK that includes AliOS Things, the gateway sample application is located in the Products/example/linkkit_gateway/ directory. The code files are as follows.
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.hFor the SDK that does not include AliOS Things, the gateway sample application is located in the examples/linkkit/gateway/ directory. The code files are as follows.
gateway_entry.c gateway_entry.h gateway_main.c gateway_main.h gateway_ut.c gateway_ut.h gateway_api.c gateway_api.hNoteCompared with the SDK version that includes AliOS Things, this version does not include the gateway_cmds.c or gateway_cmds.h files. The other code files are the same.
The following sections describe the files in the gateway sample application.
gateway_entry.c
gateway_entry.c is the main entry file for the application.
In this topic, the four-tuple refers to the ProductKey, ProductSecret, DeviceName, and DeviceSecret.
int application_start(int argc, char **argv)
{
... ... ... ... ... ... ... ... ...
// Set the default log level
#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
// Load the four-tuple information. You must implement this function.
load_device_meta_info();
... ... ... ... ... ... ... ... ...
// Register a callback function for a successful cloud connection to perform OTA initialization.
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
// Register serial port commands. The code is in gateway_cmds.c.
#ifdef CONFIG_AOS_CLI
gateway_register_cmds();
#endif
... ... ... ... ... ... ... ... ...
// The loop of the main function.
aos_loop_run();
return 0;
}gateway_main.c
Because some sub-device network connection operations are synchronous and blocking, this file creates the following two threads:
user_dispatch_yield thread: Executes the Link Kit SDK code and the registered user callback functions.
gateway_main main thread: Calls Link Kit SDK interfaces and handles communication with sub-devices.
gateway_cmds.c
This file contains code for command-line features that facilitate testing and debugging.
gateway_ut.c
This file contains user test code. For example, you can use this code to simulate sub-device information using key-value pairs, bring a sub-device online after you retrieve the topology list, and simulate adding a sub-device when the gateway permits new sub-devices to join.
gateway_api.c
This file encapsulates SDK interface functions for sub-device operations. It supports features such as adding, deleting, and resetting sub-devices, and querying sub-device IDs. You can use these interface functions directly.
Add a sub-device
To add a sub-device, you must make three SDK interface calls.
Call
IOT_Linkkit_Open: Adds the sub-device to the local data structure of the gateway.Call
IOT_Linkkit_Connect: Registers the sub-device and adds its topology relationship to the cloud.Call
IOT_Linkkit_Report: Notifies the cloud that the sub-device is online.
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); ... ... ... ... ... ... ... ... ... }Add sub-devices in a batch
The
IOT_Linkkit_Report( ... ITM_MSG_CONNECT_SUBDEV ...)function supports adding up to five sub-devices in a single batch.If the sub-devices are successfully added in a batch, call
IOT_Linkkit_Report(subdev_id, ITM_MSG_LOGIN, NULL, 0)to bring the corresponding sub-devices online.
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) { // Bring sub-devices online in a batch 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; }Delete a sub-device
Call the
IOT_Linkkit_Report(subdev_id, ITM_MSG_DELETE_TOPO, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t))function to delete the topology relationship between a sub-device and the gateway. This operation also releases the resources of the sub-device on the gateway.NoteWhen you delete a sub-device, the system first searches for the sub-device by its ProductKey and DeviceName. If the sub-device is found, it is deleted. If the sub-device is not found, the system continues the search using the first parameter, subdev_id.
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)); ... ... ... ... ... ... ... ... ... }Reset a sub-device
Call the
IOT_Linkkit_Report(subdev_id, ITM_MSG_SUBDEV_RESET, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t))function to reset a sub-device. This operation deletes the topology relationship of the sub-device with the gateway, unbinds the sub-device from the user account, and releases the resources of the sub-device on the gateway.NoteWhen you reset a sub-device, the system first searches for the sub-device by its ProductKey and DeviceName. If the sub-device is found, it is reset. If the sub-device is not found, the system continues the search using the first parameter, subdev_id.
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)); ... ... ... ... ... ... ... ... ... }Query the DeviceID of a sub-device
Call the
IOT_Linkkit_Query(master_devid, ITM_MSG_QUERY_SUBDEV_ID, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));function to query the DeviceID of a sub-device based on its ProductKey and DeviceName. The DeviceID is a positive integer that serves as a data structure index.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)); ... ... ... ... ... ... ... ... ... }Bring sub-devices online in batches
This interface function splits the input array of sub-devices into groups of five for batch login.
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; }Log out of sub-devices in a batch
This interface function splits the input array of sub-devices into groups of five for batch logout.
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; }