Demo

更新时间:2024-06-25 03:10:25下载pdf

本文介绍 Tuya Bridge SDK 的使用和初始化流程。文中的代码均来自于 SDK 包中 apps 目录中的 Demo 例程,阅读本文时可以结合 SDK 中的 apps 中的 Demo 执行现象,便于理解。

API 使用说明

下面代码位于 SDK 目录 apps/tuyaos_demo_bridge 例程中,此处着重介绍如何使用涉及 Matter Bridge 的接口。

int main(int argc, char **argv)
{
    OPERATE_RET rt = OPRT_OK;
    CHAR_T *cfg_str = NULL;
    ty_cJSON *cfg_json = NULL;
    CHAR_T *storage_path = NULL;
    CHAR_T *pid = NULL;
    TUYA_PLATEFORM_S *cfg = NULL;
...
    /* Core: SDK 预初始化 */
    tuya_iot_sdk_pre_init(TRUE);

    /* 私有业务初始化 */
    ty_cJSON *js_tuya_cfg = ty_cJSON_GetObjectItem(cfg->input, "tuya");
    if (js_tuya_cfg != NULL) {
        // built-in zigbee service
        ty_cJSON *js_zb_cfg = ty_cJSON_GetObjectItem(js_tuya_cfg, "zigbee");
        if (js_zb_cfg != NULL) {
            TUYA_CALL_ERR_RETURN(user_zigbee_custom_init());
            TUYA_CALL_ERR_RETURN(user_zigbee_svc_init());
        }

        // built-in mattter service
        ty_cJSON *js_matter_cfg = ty_cJSON_GetObjectItem(js_tuya_cfg, "matter");
        if (js_matter_cfg != NULL) {
            user_fetch_matter_data(js_matter_cfg);
            matter_svc_init(js_matter_cfg);
            matter_manufactory_data_set(user_prod_test_matter_get);
            matter_svc_start(js_matter_cfg);

            DEVELOPER_CALLBACKS_S callbacks = {
                .ota_file_notify_cb = NULL,
                .ota_process_notify_cb = NULL,
                .commissioning_status_cb = NULL,
                //.zigbee_private_handler = &zigbee,
                .matter_data_private_process_cb = NULL,
                // .manufactory_cbs = NULL,
            };
            tuya_matter_bridge_correlation_register(BRIDGE_ZIGBEE, NULL, &callbacks);
        }
    }
    /* Core: SDK 初始化 */
#if defined(WIFI_GW) && (WIFI_GW==1)
#if defined(GW_SUPPORT_WIRED_WIFI) && (GW_SUPPORT_WIRED_WIFI==1)
    TUYA_CALL_ERR_RETURN(tuya_iot_wr_wf_sdk_init(IOT_GW_NET_WIRED_WIFI, GWCM_OLD, WF_START_AP_ONLY, cfg->product_id, USER_SW_VER, NULL, 0));
#else
    TUYA_CALL_ERR_RETURN(tuya_iot_wf_sdk_init(GWCM_OLD, WF_START_AP_ONLY, cfg->product_id, USER_SW_VER, NULL, 0));
#endif
#else
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_init(cfg->product_id, USER_SW_VER, NULL, 0));
#endif

    /* Core: SDK 启动 */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_start());

    /* 私有业务启动 */
    TUYA_CALL_ERR_RETURN(user_zigbee_svc_start());

    while (1) {
        tal_system_sleep(10*1000);
    }

    return 0;
}

配置文件中有使能 Matter 相关功能,则开始初始化启动 Matter 相关能力。

Matter 服务能力

    matter_svc_init(js_matter_cfg);//首先调用 Matter 服务的初始化
    matter_manufactory_data_set(user_prod_test_matter_get);//写入 Matter 运行的环境参数
    matter_svc_start(js_matter_cfg);//启动 Matter 服务模块

Bridge 子服务能力

Bridge 功能是 Matter 服务的一项子功能,其运行依赖 Matter 服务能力,所以有一个先后顺序。即先调用 Matter 服务相关接口初始化准备好 Matter 服务环境,其次再调用 Bridge 相关接口激活 Bridge 子功能。

Bridge 子功能的接口使用如下:

tuya_matter_bridge_correlation_register 函数展示需要您参与的内容,以回调函数的形式参与到整个 SDK 运行。

若使用的协议接入能力是涂鸦提供的,则下面这些回调都可以为空,否则需要根据自己实际情况填写。

        DEVELOPER_CALLBACKS_S callbacks = {
            .ota_file_notify_cb = NULL,
            .ota_process_notify_cb = NULL,
            .commissioning_status_cb = NULL,
            //.zigbee_private_handler = &zigbee,
            .matter_data_private_process_cb = NULL,
            // .manufactory_cbs = NULL,
        };
        tuya_matter_bridge_correlation_register(BRIDGE_ZIGBEE, NULL, &callbacks);

配置文件

{
    "pid": "lrjugqpxezjix7pg",
    "uuid": "6f351c7092417fcb",
    "authkey": "D2WtkZ81Uu0iFRLunacVy27VSLnhgNHH",
    "storage_path": "./",
    "cache_path":"/tmp/",
    "bin_path":"/tmp/",
    "backup_path":"/tmp/",
    "log_path": "./",
    "sw_ver": "1.0.0",
    "sync_systime":0,
    "log_level": 4,
    "tuya":{
        "zigbee":{
            "dev_name":"/dev/ttyS2",
            "cts":1,
            "thread_mode":1
        },
        "matter":{
            "DacCrt":"-----BEGIN CERTIFICATE-----\nMIIB7jCCAZSgAwIBAgIRAimzT4sgm4tKGET9Ww978XswCgYIKoZIzj0EAwIwRjEY\nMBYGA1UEAwwPVHV5YSBNYXR0ZXIgUEFJMRQwEgYKKwYBBAGConwCAQwEMTI1RDEU\nMBIGCisGAQQBgqJ8AgIMBDAwNzAwIBcNMjMwODIzMDE1NzIxWhgPOTk5OTEyMzEy\nMzU5NTlaMEcxGTAXBgNVBAMMEDZmMzUxYzcwOTI0MTdmY2IxFDASBgorBgEEAYKi\nfAIBDAQxMjVEMRQwEgYKKwYBBAGConwCAgwEMDA3MDBZMBMGByqGSM49AgEGCCqG\nSM49AwEHA0IABNDmI7wzNwr85K9xrnnA8IAg0se/uD3fPBOPAhpLkL8lDwe6lxEj\n2isaiw4KOL3tY+NzLCt7fScEiVpo0hgs0GCjYDBeMAwGA1UdEwEB/wQCMAAwHwYD\nVR0jBBgwFoAUQ+cWn/Fw7Xh43Kf6fchU01w1JDYwHQYDVR0OBBYEFBXnxce7pzfr\nA6yTameiX/AvqWlvMA4GA1UdDwEB/wQEAwIHgDAKBggqhkjOPQQDAgNIADBFAiEA\nrhAIwWMLVteXe6fnAbeZua4kjKaAlYHjfCoIlkH4JZ8CIDPG+ApIw/KIEFsgAr0E\nMIrItOP6vbLXcwfeGzU7XAn+\n-----END CERTIFICATE-----\n",
            "PaiCrt":"-----BEGIN CERTIFICATE-----\nMIIB0jCCAXigAwIBAgIGAYUFcodgMAoGCCqGSM49BAMCMDAxGDAWBgNVBAMMD1R1\neWEgTWF0dGVyIFBBQTEUMBIGCisGAQQBgqJ8AgEMBDEyNUQwIBcNMjIxMjEyMDgy\nNzQ5WhgPOTk5OTEyMzEyMzU5NTlaMEYxGDAWBgNVBAMMD1R1eWEgTWF0dGVyIFBB\nSTEUMBIGCisGAQQBgqJ8AgEMBDEyNUQxFDASBgorBgEEAYKifAICDAQwMDcwMFkw\nEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE8T76NMoAkJscY6Cj28PytGLtKKx6nzke\naWH0W4Cta5R42s30hMPK1XNlah/hnP2BqqJtFWNdjXlcaJ4sTngWXqNmMGQwEgYD\nVR0TAQH/BAgwBgEB/wIBADAfBgNVHSMEGDAWgBQ1V1KjpyBYM3ce+r6lMmcMsLMd\nCTAdBgNVHQ4EFgQUQ+cWn/Fw7Xh43Kf6fchU01w1JDYwDgYDVR0PAQH/BAQDAgEG\nMAoGCCqGSM49BAMCA0gAMEUCIQDoC2kJaQm1JMPSFwNeNmcSDGeSHH6Vp2mx9HCh\neJunNwIgWzztaYJh2PBAQpmN61TPYrUhxR3xVMRYRvfuzRj8SSc=\n-----END CERTIFICATE-----\n",
            "Resource":"{\"payload\":{\"productName\":\"Smart Wired Gateway Pro\",\"partNumber\":\"THP10-Z-X\",\"vendorName\":\"Tuya\",\"hardwareVersion\":1,\"productId\":112,\"hardwareVersionString\":\"1.0\",\"customFlow\":0,\"serialNumber\":\"LKEH16MRN00006\",\"version\":0,\"vendorId\":4701,\"discoveryCapabilities\":4,\"discriminator\":2702,\"passcode\":75274938},\"spakeParam\":{\"salt\":\"r7JvfXuCkBnXia1gE3K0UNtPRuN46sVXidVknaM7ua4=\",\"iterations\":1000,\"w0\":\"t5MXtpMe5EJxeSd6X0m1pM9o9hQt4duYEgeR8kAq1V4=\",\"l\":\"BHoNFyWo9tonRHyiVgh3P2la4BkvqJIcKMIyhhzGwlOLwWVX9tEMvmiVIFuOaEG0P5rHY8ECsOFiYIl5/XDzjyY=\"},\"cd\":\"MIHmBgkqhkiG9w0BBwKggdgwgdUCAQMxDTALBglghkgBZQMEAgEwQgYJKoZIhvcNAQcBoDUEMxUkAAElAV0SNgIEcBgkAw4sBBNDU0EyMjA3MU1BVDQwMDcxLTAwJAUAJAYAJAcBJAgCGDF9MHsCAQOAFP40P5WZR3Y7Ye5FORMTOElP5n2OMAsGCWCGSAFlAwQCATAKBggqhkjOPQQDAgRHMEUCIE7abImhfNW78WkVZ37DNArIPwk96m2jmeYOlS0ekAC1AiEA85F7pl2bWkJbGjayvNY5/fYIwCjzrDx2Mk1GDnE9+7g=\"}",
            "PrivateKey":"-----BEGIN EC PRIVATE KEY-----\nMHcCAQEEIA3PtcW3kyicOeS0jjI+BrIMNT0meFYHlr2zHdP07ufyoAoGCCqGSM49\nAwEHoUQDQgAE0OYjvDM3Cvzkr3GuecDwgCDSx7+4Pd88E48CGkuQvyUPB7qXESPa\nKxqLDgo4ve1j43MsK3t9JwSJWmjSGCzQYA==\n-----END EC PRIVATE KEY-----\n",
            "PubKey":"-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE0OYjvDM3Cvzkr3GuecDwgCDSx7+4\nPd88E48CGkuQvyUPB7qXESPaKxqLDgo4ve1j43MsK3t9JwSJWmjSGCzQYA==\n-----END PUBLIC KEY-----\n"
        },
        "dp_engine":{
            "storage_path":"/tmp/",
            "file_max_count":1,
            "pidTable_max_count":1,
            "enable_zigbee_trans":1,
            "enable_matter_trans":1
        }
    }
}

字段说明

字段 必须 说明
pid 设备产品 ID,在 涂鸦开发者平台上创建网关产品时由平台生成。
uuid 授权信息,为 Universally Unique Identifier 的缩写。在涂鸦智能开发智能化产品时,UUID 是设备唯一的识别 ID,与 Authkey 为一对。必须填写自己申请的 UUID。
authkey 授权信息,涂鸦开发者平台为设备颁发的设备密钥,与 UUID 为一对。必须填写自己申请的 Authkey。
log_level 日志等级,默认是 debug。
storage_path 存储路径,需要有可读性权限,必须以 / 结束。
matter Matter 能力包初始化标志,子项是 Matter 运行的相关必要信息,如证书、公私钥、产品信息。
dp_engine DP 引擎被涂鸦设备能力接入包依赖,当使用涂鸦设备接入能力时必选。