文档

lv_message_adapter

更新时间:
一键部署

调用该接口向LinkVisual SDK注入物联网平台消息。

接口详情

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

接口中相关参数说明如下。

参数

类型

说明

auth

lv_device_auth_s

设备认证信息。

param

const lv_message_adapter_param_s*

消息结构体。

示例代码

//user_link_visual_handler为IOT_RegisterCallback使用ITE_LINK_VISUAL订阅的回调,
//在回调中,所有消息都使用lv_message_adapter注入
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) {
    /* LinkVisual自定义的消息,直接全交由LinkVisual来处理 */
    if (payload == NULL || payload_len == 0) {
        return 0;
    }

/* 设备证书信息,其中:
product_key:在阿里云物联网平台创建的产品ProductKey。
device_name:物联网平台为设备颁发的设备名称。
device_secret:物联网平台为设备颁发的设备密钥,用于认证加密。*/

    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为IOT_RegisterCallback使用ITE_SERVICE_REQUST订阅的回调
//在回调中,满足一定规则的命令需要使用lv_message_adapter注入
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);

    /* 部分物模型服务消息由LinkVisual处理,部分需要自行处理。 */
    int link_visual_process = 0;
    for (unsigned int i = 0; i < sizeof(link_visual_service)/sizeof(link_visual_service[0]); i++) {
        /* 这里需要根据字符串的长度来判断 */
        if (!strncmp(serviceid, link_visual_service[i], strlen(link_visual_service[i]))) {
            link_visual_process = 1;
            break;
        }
    }

    if (link_visual_process) {
        /* ISV将某些服务类消息交由LinkVisual来处理,不需要处理response */
       /* 设备证书信息,其中:
           product_key:在阿里云物联网平台创建的产品ProductKey。
           device_name:物联网平台为设备颁发的设备名称。
           device_secret:物联网平台为设备颁发的设备密钥,用于认证加密。 */
        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 {
        /* 非LinkVisual处理的消息 */
    }

    return 0;
}

  • 本页导读 (1)
文档反馈