This topic provides sample signature code for you to develop your devices, so that your devices can communicate with IoT Platform over MQTT without using the device SDK provided by Alibaba Cloud.
Description
We recommend that you use a device SDK provided by Alibaba Cloud. If a device SDK in any language is used, you do not need to configure your own signature mechanism. To view the SDK download path, access Download device SDKs.
If you use other methods to connect your devices to IoT Platform, note the following information:
- You must take the responsibility to ensure connection stability and maintain the keepalive and reconnection mechanisms for MQTT connections.
- Alibaba Cloud will not provide related technical support for any possible connection issues.
- If you want to use IoT Platform features, such as OTA, TSL models, and unique-certificate-per-product authentication, you must compile your own code to implement these features. This may consume a lot of development time and bug fixing time.
Sample code for signature calculation
If you do not use the device SDKs provided by Alibaba Cloud IoT Platform, click the following links to access the corresponding sample code pages.
- sign_mqtt.c: the sample code used to implement the signature function.
- sign_api.h: the sample code that defines the data structure used by the signature function.
- sign_sha256.c: the sample code that defines the algorithm implementations that can be used by the signature function. If you have HMACSHA256 implementation on your own platform, you do not need to compile this code file. However, you must provide the
utils_hmac_sha256()
function that can be called by the APIs defined in sign_mqtt.c. - sign_test.c: the sample code used to test the signature function.
Description of the signature function
Function prototype | int32_t IOT_Sign_MQTT(iotx_mqtt_region_types_t region, iotx_dev_meta_info_t *meta, iotx_sign_mqtt_t *signout); |
Description | Outputs the information required to connect to Alibaba Cloud IoT Platform based on the input information about the IoT device authentication. The connection information contains the domain name, MQTT client ID, MQTT username, and MQTT password. Then, you can provide the information to the MQTT client to connect to Alibaba Cloud IoT Platform. |
Input parameters | The following input parameters are included:
|
Output parameters | signout: the output data, which is used to establish an MQTT connection. Sample code: The parameters in this example are described as follows:
|
Return values |
|
Example on how to use the signature function
In this example, the test code in sign_test.c is used.
#include <stdio.h>
#include <string.h>
#include "sign_api.h" //Defines all data structures that are used in the signature function.
//The following macros are used to define the Alibaba Cloud authentication information of the device: ProductKey, ProductSecret, DeviceName, and DeviceSecret.
//In actual product development, the device authentication information must be encrypted by the device manufacturer and stored in the flash of the device or a file.
//These macros are read and used after the device is powered on.
#define EXAMPLE_PRODUCT_KEY "a1X2bEn****"
#define EXAMPLE_PRODUCT_SECRET "7jluWm1zql7b****"
#define EXAMPLE_DEVICE_NAME "example1"
#define EXAMPLE_DEVICE_SECRET "ga7XA6KdlEeiPXQPpRbAjOZXwG8y****"
int main(int argc, char *argv[])
{
iotx_dev_meta_info_t meta_info;
iotx_sign_mqtt_t sign_mqtt;
memset(&meta_info, 0, sizeof(iotx_dev_meta_info_t));
//Use the following code to copy the previously defined device identity information to meta_info.
memcpy(meta_info.product_key, EXAMPLE_PRODUCT_KEY, strlen(EXAMPLE_PRODUCT_KEY));
memcpy(meta_info.product_secret, EXAMPLE_PRODUCT_SECRET, strlen(EXAMPLE_PRODUCT_SECRET));
memcpy(meta_info.device_name, EXAMPLE_DEVICE_NAME, strlen(EXAMPLE_DEVICE_NAME));
memcpy(meta_info.device_secret, EXAMPLE_DEVICE_SECRET, strlen(EXAMPLE_DEVICE_SECRET));
//Call the signature function to generate various data that is required to establish an MQTT connection.
IOT_Sign_MQTT(IOTX_CLOUD_REGION_SHANGHAI, &meta_info, &sign_mqtt);
...
}