Matter Connectivity

Last Updated on : 2023-12-27 03:32:10download

This topic describes how to develop a Matter gateway with TuyaOS Gateway Development Framework.

Matter features

This section describes how to enable Matter features with TuyaOS Gateway Development Framework and implement Matter gateway capabilities with Tuya’s ZS3L module. A Matter gateway can connect to Thread sub-devices and bridge Zigbee sub-devices to interoperate with Matter networks.

  • To connect to Thread sub-devices, enable the Thread feature in the configuration file and start OpenThread Border Router (OTBR). Set the thread field in the gateway configuration file. Here is an example:

    "matter":{
        "thread": {
            "netif_name":"wpan0"
        }
    }
    
  • To disable Thread connectivity, delete the thread field from the configuration file. Here is an example:

    "matter":{
    }
    
  • To bridge Zigbee sub-devices, enable the Zigbee service and implement the Zigbee connectivity.

    To enable Thread connectivity and bridging Zigbee sub-devices into Matter, you need to write a DP engine file and upload it to the Tuya IoT Development Platform. For more information about the DP engine file, see Integration by Sub-Device Config Files.

Initialize and start Matter

The following interfaces are used to enable Matter features.

  • Initialize Matter
  • Start Matter
  • Import Matter production testing data

The interfaces for initializing and starting Matter have the same parameters in JSON, but differ in configurations.

typedef struct {
    CHAR_T *matterDacCrt;
    UINT_T matterDacCrtLen;
    CHAR_T *matterPaiCrt;
    UINT_T matterPaiCrtLen;
    CHAR_T *matterResource;
    UINT_T matterResourceLen;
    CHAR_T *matterPrivateKey;
    UINT_T matterPrivateKeyLen;
    CHAR_T *matterPubKey;
    UINT_T matterPubKeyLen;
} MATTER_MANUFACTYORY_DATA_T;

Implement reading the Matter production testing data from the struct MATTER_MANUFACTYORY_DATA_T that is stored in the gateway’s non-volatile memory. The SDK will use this data. A callback is provided to facilitate implementation.

Example

int main(int argc, char **argv)
{
    ...
     /* init tuya service */
    ty_cJSON *js_tuya_cfg = ty_cJSON_GetObjectItem(cfg, "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_zigbee_svc_init(js_zb_cfg);
        }
        // built-in matter service
        ty_cJSON *js_matter_cfg = ty_cJSON_GetObjectItem(js_tuya_cfg, "matter");
        if (js_matter_cfg != NULL) {
            matter_svc_init(js_matter_cfg);
        }
    }
    ...

    // start tuya service
    if (js_tuya_cfg != NULL) {
        ty_cJSON *js_matter_cfg = ty_cJSON_GetObjectItem(js_tuya_cfg, "matter");
        if (js_matter_cfg != NULL) {
            matter_manufactory_data_set(user_prod_test_matter_get);
            matter_svc_start(js_matter_cfg);
        }
    }
    ...
    return 0;
}

The user_prod_test_matter_get function is used to get the certificate that is configured in Matter protection testing.

Unbind

When the gateway is unbound, call this function to instruct the SDK to delete the binding data.

/**
  * @brief: the unbind of matter gateway. The reset type is GW_REMOTE_UNACTIVE or GW_LOCAL_UNACTIVE.
  *
  * @param[in] VOID:
  * @return OPRT_OK on success. Others on error
  */
OPERATE_RET matter_unactive(VOID);

Clear data

When the gateway is reset, call this function to erase the data from the device.

/**
  * @brief: clear data when resetting. The reset type is GW_RESET_DATA_FACTORY
  *
  * @param[in] VOID: .
  * @return OPRT_OK on success. Others on error
  */
OPERATE_RET matter_clear_data(VOID);

Factory reset

When the gateway is reset, call this function to instruct the SDK to execute the factory reset process.

/**
  * @brief: the reset of Matter gateway. The reset type is GW_REMOTE_RESET_FACTORY or GW_LOCAL_RESET_FACTORY
  *
  * @param[in] VOID:
  * @return OPRT_OK on success. Others on error
  */
OPERATE_RET matter_factory_reset(VOID);

Use case

STATIC VOID __gw_reset_cb(GW_RESET_TYPE_E type)
{
    PR_DEBUG("gw reset callback, type: %d", type);

    if ((GW_REMOTE_RESET_FACTORY == type) || (GW_LOCAL_RESET_FACTORY == type)) {
        matter_factory_reset();
    }
    else if ((GW_REMOTE_UNACTIVE == type) || (GW_LOCAL_UNACTIVE == type)){
        matter_unactive();
    }
    else if(GW_RESET_DATA_FACTORY == type) {
        matter_clear_data();
    }
    else{
        PR_DEBUG("NOT SUPPORTED, type: %d", type);
    }

    if (type != GW_RESET_DATA_FACTORY) {
        sleep(3);
       /* restart GW */
        system("/tmp/tuya/tuya_start.sh /tmp/tuya &");
    }
}

int main(int argc, char **argv)
{
    (VOID_T) argc;
    (VOID_T) argv;
    OPERATE_RET rt = OPRT_OK;

    TY_GW_INFRA_CBS_S gw_cbs = {
        .gw_reset_cb       = __gw_reset_cb,
        .gw_reboot_cb      = __gw_reboot_cb,
        .gw_active_stat_cb = __gw_active_cb,
        .gw_upgrade_cb     = __gw_upgrade_cb,
    };

    TUYA_CALL_ERR_RETURN(user_svc_init((VOID *)&gw_cbs));
    ...
    while (1) {
        tal_system_sleep(10*1000);
    }
    return 0;