All Products
Search
Document Center

:lv_message_adapter

Last Updated:Mar 01, 2022

Ingests IoT Platform messages to LinkVisual SDK.

Description

int lv_message_adapter(const lv_device_auth_s *auth, const lv_message_adapter_param_s *param)

The following table describes the related parameters.

Parameter

Type

Description

auth

lv_device_auth_s

The device information for authentication.

param

const lv_message_adapter_param_s*

The message struct.

Sample code

// user_link_visual_handler is a callback function to which  IOT_RegisterCallback subscribes by using ITE_LINK_VISUAL. 
// In the callback function, all messages are ingested by using the lv_message_adapter operation.
static int user_link_visual_handler(const int devid, const char *service_id,
                                    const int service_id_len, const char *payload,
                                    const int payload_len) {
    /* Custom messages in LinkVisual SDK are all processed by LinkVisual SDK. */
    if (payload == NULL || payload_len == 0) {
        return 0;
    }

/* Device certificate information, which includes the following items:
product_key: the ProductKey of the device created in Alibaba Cloud IoT Platform. 
device_name: the DeviceName of the device, which is issued by IoT Platform. 
device_secret: the DeviceSecret of the device, which is issued by IoT Platform for device authentication and encryption. */

    char *product_key = NULL;
    char *device_name = NULL;
    char *device_secret= NULL;
    iotx_dm_get_triple_by_devid(devid, &product_key, &device_name, &device_secret);

    lv_device_auth_s auth;
    GetAuth(devid, &auth);

    lv_message_adapter_param_s in = {0};
    in.type = LV_MESSAGE_ADAPTER_TYPE_LINK_VISUAL;
    in.service_name = (char *)service_id;
    in.service_name_len = service_id_len;
    in.request = (char *)payload;
    in.request_len = payload_len;
    int ret = lv_message_adapter(&auth, &in);
    if (ret < 0) {
        printf("LinkVisual process service request failed, ret = %d\n", ret);
        return -1;
    }

    return 0;
}


// user_service_request_handler is a callback function to which IOT_RegisterCallback subscribes by using ITE_SERVICE_REQUST.
// In the callback function, lv_message_adapter is required to ingest the commands that meet specific rules.
static int user_service_request_handler(const int devid, const char *id, const int id_len,
                                            const char *serviceid, const int serviceid_len,
                                            const char *request, const int request_len,
                                            char **response, int *response_len) {
    printf("Service Request Received, Devid: %d, ID %.*s, Service ID: %.*s, Payload: %s\n",
             devid, id_len, id, serviceid_len, serviceid, request);

    /* Only part of the Thing Specification Language (TSL) service messages are processed by LinkVisual SDK, while the rest must be processed manually?  */
    int link_visual_process = 0;
    for (unsigned int i = 0; i < sizeof(link_visual_service)/sizeof(link_visual_service[0]); i++) {
        /* You must check the lengths of the serviceid and link_visual_service[i] strings. */
        if (!strncmp(serviceid, link_visual_service[i], strlen(link_visual_service[i]))) {
            link_visual_process = 1;
            break;
        }
    }

    if (link_visual_process) {
        /* ISVs send specific service messages to LinkVisual SDK for processing instead of generating a response. */
       /* Device certificate information, which includes the following items:
           product_key: the ProductKey of the device created in Alibaba Cloud IoT Platform. 
           device_name: the DeviceName of the device, which is issued by IoT Platform. 
           device_secret: the DeviceSecret of the device, which is issued by IoT Platform for device authentication and encryption.  */
        char *product_key = NULL;
        char *device_name = NULL;
        char *device_secret= NULL;
        iotx_dm_get_triple_by_devid(devid, &product_key, &device_name, &device_secret);

        lv_device_auth_s auth;
        GetAuth(devid, &auth);

        lv_message_adapter_param_s in = {0};
        in.type = LV_MESSAGE_ADAPTER_TYPE_TSL_SERVICE;
        in.msg_id = (char *)id;
        in.msg_id_len = id_len;
        in.service_name = (char *)serviceid;
        in.service_name_len = serviceid_len;
        in.request = (char *)request;
        in.request_len = request_len;
        int ret = lv_message_adapter(&auth, &in);
        if (ret < 0) {
            printf("LinkVisual process service request failed, ret = %d\n", ret);
            return -1;
        }
    } else {
        /* Messages that are not processed by LinkVisual SDK */
    }

    return 0;
}