Use Link SDK for C to submit or delete device tags on IoT Platform. This example uses the ./demos/devinfo_posix_demo.c sample file.
Background information
Step 1: Initialization
-
Add header files.
…… …… #include "aiot_devinfo_api.h" Configure underlying dependencies and log output.
aiot_sysdep_set_portfile(&g_aiot_sysdep_portfile); aiot_state_set_logcb(demo_state_logcb);-
Call the aiot_devinfo_init operation to create a
Devinfoclient instance and initialize default parameters.devinfo_handle = aiot_devinfo_init(); if (devinfo_handle == NULL) { demo_mqtt_stop(&mqtt_handle); printf("aiot_devinfo_init failed\n"); return -1; }
Step 2: Configure features
Call the aiot_devinfo_setopt operation to configure the following settings:
-
Associate with an MQTT connection handle.
ImportantBefore you configure device tag-specific parameters, make sure that device verification information is specified. For more information, see Usage example.
-
Sample code:
aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_MQTT_HANDLE, mqtt_handle); -
Parameters:
Parameter
Example
Description
AIOT_DEVINFOOPT_MQTT_HANDLE
mqtt_handle
The MQTT connection handle for device tags.
-
-
Configure a message callback function.
-
Sample code:
aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_RECV_HANDLER, (void *)demo_devinfo_recv_handler); -
Parameters:
Parameter
Example
Description
AIOT_DEVINFOOPT_RECV_HANDLER
demo_devinfo_recv_handler
The callback function that is invoked when a device tag message is received.
-
-
Configure status monitoring.
-
Define a callback function to monitor status.
When you define the callback function, take note of the following items:
-
If tag submission fails, one of the following event types is returned:
Event type
Description
AIOT_DEVINFOEVT_INVALID_DEVINFO
The device information in the response is invalid. The ProductKey and DeviceName values cannot be obtained.
AIOT_DEVINFOEVT_INVALID_RESPONSE
A field in the response is invalid.
AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT
A field in the response has an invalid format.
-
In this example, the response is printed. You can customize the processing logic based on your business requirements.
void demo_devinfo_event_handler(void *handle, const aiot_devinfo_event_t *event, void *userdata) { switch (event->type) { case AIOT_DEVINFOEVT_INVALID_DEVINFO: { printf("AIOT_DEVINFOEVT_INVALID_DEVINFO\n"); } break; case AIOT_DEVINFOEVT_INVALID_RESPONSE: { printf("AIOT_DEVINFOEVT_INVALID_RESPONSE\n"); } break; case AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT: { printf("AIOT_DEVINFOEVT_INVALID_RESPONSE_FORMAT\n"); } break; default: { } } } -
-
Specify the callback function to monitor status.
-
Sample code:
aiot_devinfo_setopt(devinfo_handle, AIOT_DEVINFOOPT_EVENT_HANDLER, (void *)demo_devinfo_event_handler); -
Parameters:
Parameter
Example
Description
AIOT_DEVINFOOPT_EVENT_HANDLER
demo_devinfo_event_handler
When the device connection status changes, the corresponding processing logic defined in this callback function is executed.
-
-
Step 3: Update tags
-
Call the aiot_devinfo_send function to send a request to IoT Platform. For more information about the parameters in the request, see the previous step.
When you send the request, take note of the following items:
-
aiot_devinfo_msg_t is an input parameter of the
aiot_devinfo_send()function that specifies the data format. -
AIOT_DEVINFO_MSG_UPDATE specifies the message type.
-
Sample message:
Request message
Alink format
Description
[ { "attrKey": "testKey", "attrValue": "testValue" } ]{ "id": "123", "version": "1.0", "sys":{ "ack":0 }, "params": [ { "attrKey": "testKey", "attrValue": "testValue" } ], "method": "thing.deviceinfo.update" }The message body is in JSON format. The content corresponds to the value of the params parameter in the Alink data. For more information, see Report tag information.
In this example, the specified tag is
testKey: testValue.
{ aiot_devinfo_msg_t devinfo_update; char *update = "[{\"attrKey\":\"testKey\",\"attrValue\":\"testValue\"}]"; memset(&devinfo_update, 0, sizeof(aiot_devinfo_msg_t)); devinfo_update.product_key = product_key; devinfo_update.device_name = device_name; devinfo_update.type = AIOT_DEVINFO_MSG_UPDATE; devinfo_update.data.update.params = update; res = aiot_devinfo_send(devinfo_handle, &devinfo_update); if (res < STATE_SUCCESS) { aiot_devinfo_deinit(&devinfo_handle); demo_mqtt_stop(&mqtt_handle); return -1; } printf("aiot_devinfo_send update msg id: %d\n", res); } -
-
After IoT Platform receives the request, it updates the device tag and returns a response.
-
After the device receives the response, the
demo_devinfo_recv_handlercallback function is called.When you define the callback function, take note of the following items:
-
aiot_devinfo_recv_t is an input parameter of the callback function that specifies the data format.
-
AIOT_DEVINFORECV_GENERIC_REPLY specifies the message type.
-
In this example, the message is printed. You can customize the processing logic based on your business requirements.
void demo_devinfo_recv_handler(void *handle, const aiot_devinfo_recv_t *packet, void *userdata) { switch (packet->type) { /* The response message from IoT Platform. */ case AIOT_DEVINFORECV_GENERIC_REPLY: { printf("pk: %s, dn: %s, code: %d, msg id: %d, data: %.*s, message: %.*s\n", packet->product_key, packet->device_name, packet->data.generic_reply.code, packet->data.generic_reply.msg_id, packet->data.generic_reply.data_len, packet->data.generic_reply.data, packet->data.generic_reply.message_len, packet->data.generic_reply.message); } break; default: { } } } -
(Optional) Step 4: Delete tags
-
After device tags are submitted to IoT Platform, you can call the aiot_devinfo_send operation to delete tags.
When you send the request, take note of the following items:
-
aiot_devinfo_msg_t is an input parameter of the
aiot_devinfo_send()function that specifies the data format. -
AIOT_DEVINFO_MSG_DELETE specifies the message type.
-
Sample message:
Request message
Alink format
Description
{ "attrKey": "testKey" }{ "id": "123", "version": "1.0", "sys":{ "ack":0 }, "params": [ { "attrKey": "testKey", } ], "method": "thing.deviceinfo.update" }The message body is in JSON format. The content corresponds to the value of the params parameter in the Alink data. For more information, see Delete tag information.
In this example, the specified tag is
testKey.{ aiot_devinfo_msg_t devinfo_delete; char *delete = "[{\"attrKey\":\"testKey\"}]"; memset(&devinfo_delete, 0, sizeof(aiot_devinfo_msg_t)); devinfo_delete.product_key = product_key; devinfo_delete.device_name = device_name; devinfo_delete.type = AIOT_DEVINFO_MSG_DELETE; devinfo_delete.data.delete.params = delete; res = aiot_devinfo_send(devinfo_handle, &devinfo_delete); if (res < STATE_SUCCESS) { aiot_devinfo_deinit(&devinfo_handle); demo_mqtt_stop(&mqtt_handle); return -1; } printf("aiot_devinfo_send delete msg id: %d\n", res); }
-
-
After IoT Platform receives the request, it deletes the tag and returns a response.
-
After the device receives the response, the demo_devinfo_recv_handler callback function is called.
For more information, see Process response messages.
Step 5: Exit the program
Call the aiot_devinfo_deinit function to delete the Devinfo client instance and release resources.
res = aiot_devinfo_deinit(&devinfo_handle);
if (res < STATE_SUCCESS) {
demo_mqtt_stop(&mqtt_handle);
printf("aiot_devinfo_deinit failed: -0x%04X\n", -res);
return -1;
}
What to do next
-
After you configure the sample code file, compile it to generate an executable file. In this example, the ./demos/devinfo-posix-demo executable file is generated.
For more information, see Compile and run.
-
For more information about the execution results, see Operational logs.