Demo

Last Updated on : 2023-12-27 01:55:38download

This topic describes how to initialize and use the Tuya Bridge SDK. The code example in this topic is taken from the demo routine located in the apps directory of the SDK. It is recommended to review this documentation alongside the demo for better comprehension.

API usage

The following code is located in apps/tuyaos_demo_bridge. This section focuses on how to use the APIs specific to Matter bridge features.

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 pre-initialization*/
    tuya_iot_sdk_pre_init(TRUE);

    /* Private service initialization */
    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 Matter 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 initialization */
#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 startup */
    TUYA_CALL_ERR_RETURN(tuya_iot_sdk_start());

    /* Private service startup */
    TUYA_CALL_ERR_RETURN(user_zigbee_svc_start());

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

    return 0;
}

The Matter features enabled in the configuration file will be started up during initialization.

Matter service

    matter_svc_init(js_matter_cfg);// First, call Matter service initialization.
    matter_manufactory_data_set(user_prod_test_matter_get);// Write the environment parameters for running Matter.
    matter_svc_start(js_matter_cfg);// Start the Matter service module.

Bridge sub-service

The bridge feature is a sub-service that operates based on the capabilities of the Matter service. Therefore, you must first call the Matter service API for initialization to prepare the Matter environment. Then, call the bridge API to activate the sub-service.

tuya_matter_bridge_correlation_register is used to present the content you implement, which is invoked in the SDK runtime through callbacks.

If you are using protocols provided by Tuya, you can leave these callbacks empty. Otherwise, populate them as required.

        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);

Configuration file

{
    "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
        }
    }
}

Field description

Field Required Description
pid Yes The ID of the gateway product you create on the Tuya IoT Development Platform.
uuid Yes The authorization information. A universally unique identifier (UUID) uniquely identifies a device and is paired with an authkey. Be sure to replace the uuid with your value.
authkey Yes The authorization information. An authentication key is assigned uniquely to a device and paired with a uuid. Be sure to replace the authkey with your value.
log_level No The log level, defaulting to the debug level.
storage_path Yes The storage path that must be readable and ended with /.
matter Yes The flag for initializing Matter service. The child elements contain the required information to run Matter, including the certificate, public and private keys, and product information.
dp_engine Yes The DP engine. This field is mandatory if you implement connectivity to Tuya-enabled devices.