Updated on 2022-02-24 GMT+08:00

Binding a Device

API Function

A device must be bound to the IoT platform before accessing the IoT platform for the first time. The upper-layer application calls this API to transfer the device serial number, MAC address, or other device information to bind a device to the IoT platform.

Before binding a device, developers must call Configuring Service Parameters carrying the mandatory parameters EN_IOTA_CFG_IOCM_ADDR and EN_IOTA_CFG_IOCM_PORT to set the IP address and port number of the server to be bound. (For the IoCM server, the default port 8943 is configured on the AgentLite.)

NOTE:

Before a directly connected device accesses the IoT platform for the first time, developers must register the device on the IoT platform and then initiate a binding request on the device. If the device is not registered on the IoT platform, the binding fails. The AgentLite waits for a while and tries again.

API Description

1
HW_INT IOTA_Bind(const HW_CHAR *pcVerifyCode, const ST_IOTA_DEVICE_INFO *pstInfo);

Parameter Description

Parameter

Mandatory or Optional

Type

Description

pcVerifyCode

Mandatory

String

Specifies a device binding verification code. The value must end with \0.

If the device is registered on the SP portal, set pcVerifyCode to the preSecret used during device registration.

pstInfo

Mandatory

ST_IOTA_DEVICE_INFO structure

Specifies the device information. The value must end with \0.

Return Value

For details, see 4.2 Function Return Values.

NOTE:
  • Return values only show API calling results. For example, return value 0 indicates that the API is called successfully but does not indicate that the connection is successful. The connection is successful only after the IOTA_TOPIC_BIND_RSP broadcast is received.
  • If the binding fails, the API automatically binds the device after 30 seconds. If the retry fails for consecutive five times (the total number of attempts is six), the API returns a message indicating that the binding fails and stops the binding. To enable the API to retry again, the user needs to restart the device.

Output

Broadcast Name

Broadcast Parameter

Member

Description

IOTA_TOPIC_BIND_RSP

HW_MSG object

EN_IOTA_BIND_IE_TYPE

Indicates the binding result. For details, see EN_IOTA_BIND_IE_TYPE enumeration description.

Example

Bind the device by calling IOTA_Bind().

1
2
3
4
5
6
7
8
9
//Developers call this API to bind a device.
ST_HW_DEVICE_INFO stDeviceInfo 
stDeviceInfo.pcNodeId = "SN Number";
stDeviceInfo.pcManufacturerId = "Huawei";
stDeviceInfo.pcDeviceType = "Gateway";
stDeviceInfo.pcModel = "HW_GW101";
stDeviceInfo.pcProtocolType = "HuaweiM2M";

IOTA_Bind("SN Number", &stDeviceInfo);

After a device is bound, the AgentLite returns the parameters shown in the following output. Developers must store and configure these parameters on the device before connecting the device to the IoT platform.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Register the broadcast receive processing function.
HW_BroadCastReg("IOTA_TOPIC_BIND_RSP", Device_RegResultHandler);
//Developers call this API to register the binding result returned by the function.
HW_iNT Device_RegResultHandler(HW_UiNT uiCookie, HW_MSG pstMsg) 
{
    HW_cHAR *pcDeviceId;
    HW_cHAR *pcDeviceSecret;
    HW_cHAR *pcAppId;
    HW_cHAR *pcIoCMServerAddr;
    HW_UiNT uiIoCMServerPort;
    HW_cHAR *pcMqttServerAddr;
    HW_UiNT uiMqttServerPort;
    If (HW_SUCCESS != HW_MsgGetUint(pstMsg, EN_IOTA_BIND_IE_RESULT, 0)) 
    {
        Return 0;
    }
    pcDeviceId = HW_MsgGetStr(pstMsg, EN_IOTA_BIND_IE_DEVICEID);
    pcDeviceSecret = HW_MsgGetStr(pstMsg, EN_IOTA_BIND_IE_DEVICESECRET);
    pcAppId = HW_MsgGetStr(pstMsg, EN_IOTA_BIND_IE_APPID);
    pcIoCMServerAddr = HW_MsgGetStr(pstMsg, EN_IOTA_ BIND_IE_IOCM_ADDR );
    uiIoCMServerPort = HW_MsgGetUint(pstMsg, EN_IOTA_BIND_IE_IOCM_PORT, 0);
    pcMqttServerAddr = HW_MsgGetStr(pstMsg, EN_IOTA_ BIND_IE_IOCM_ADDR );
    uiMqttServerPort = HW_MsgGetUint(pstMsg, EN_IOTA_BIND_IE_IOCM_PORT, 0);
Config_save("DeviceId", pcDeviceId);
Config_save("DeviceSecret", pcDeviceSecret);
Config_save("AppId", pcAppId);
Config_save("IoCMAddr", pcIoCMServerAddr);
Config_save("IoCMPort", pcIoCMServerPort);
Config_save("MqttAddr", pcMqttServerAddr);
Config_save("MqttPort", pcMqttServerPort);
    return 0;
}