Develop with TSL model pass-through
This topic describes how a device uses binary data to report and receive Thing Specification Language (TSL) model data.
Background information
A Thing Specification Language (TSL) model is a data model that the Alibaba Cloud IoT Platform uses to define a product. The default data encoding format for this data model is JSON. For resource-constrained devices, encoding data in a binary format can resolve issues such as high serialization overhead and long messages that are common with the JSON format.
Prerequisites
You have downloaded the C LinkSDK Extended.
You have added a TSL model.
You have created a data parsing script for the TSL model to transform data between binary and JSON formats.
Usage flow
This section describes the basic flow for TSL model communication that uses binary data. The device application ./demos/data_model_raw_demo.c is used as an example. For more information about the APIs, see aiot_dm_api.h.

Step 1: Initialize the device
You can create a device handle and establish a device connection.
static void* demo_device_init(char *product_key, char *device_name, char *device_secret, char *host, uint16_t port)
{
int32_t res = STATE_SUCCESS;
/* Create a device. */
void *device = aiot_device_create(product_key, device_name);
if (device == NULL) {
printf("device create failed\n");
return NULL;
}
/* Set the device secret. */
aiot_device_set_device_secret(device, device_secret);
/* Initialize the connection configuration parameters. */
aiot_linkconfig_t* config = aiot_linkconfig_init(protocol);
/* Set the host and port of the server. */
aiot_linkconfig_host(config, host, port);
/* Set the device connection parameters. */
aiot_device_set_linkconfig(device, config);
/* Establish a device connection. */
res = aiot_device_connect(device);
if (res < STATE_SUCCESS) {
/* If the connection fails, destroy the MQTT instance and revoke the resources. */
aiot_linkconfig_deinit(&config);
aiot_device_delete(&device);
printf("aiot_device_connect failed: -0x%04X\n\r\n", -res);
return NULL;
}
/* If the connection is successful, return the device object. */
aiot_linkconfig_deinit(&config);
return device;
}Step 2: Report binary data
You can use this API to report properties, report events, and reply to messages in a custom data format.
/* Report binary data. */
uint8_t raw_data[] = { 0x01, 0x02 };
aiot_device_dm_raw_post(device_client, raw_data, sizeof(raw_data));Step 3: Receive and process binary data
The binary message callback handles property settings, service invocations, and replies to cloud messages. Message types include AIOT_DMRECV_RAW_REPLY for replies from the cloud and AIOT_DMRECV_RAW_DOWN for messages sent by the cloud. The example only prints the property settings. You can parse the data to perform your own business operations.
/* The callback function for TSL model messages. */
static void demo_dm_msg_callback(void *device, const aiot_dm_msg_t *dm_msg, void *userdata)
{
if(dm_msg == NULL) {
return;
}
printf("[thing-message] recv msg type %d\r\n", dm_msg->type);
switch(dm_msg->type){
case AIOT_DMRECV_RAW_REPLY:
case AIOT_DMRECV_RAW_DOWN:
demo_dm_msg_down_raw_callback(device, dm_msg, userdata);
break;
default:
break;
}
}Step 4: Deinitialize the device
/* Disconnect the device and revoke its resources. */
demo_device_deinit(device_client);