1# Syncing Assets (Backup and Restore) (C/C++) 2 3 4## Adding Dependencies 5 6Link the dynamic libraries in the CMake script. 7```txt 8target_link_libraries(entry PUBLIC libasset_ndk.z.so) 9``` 10 11## Adding Assets That Support Sync 12 13Add an asset with the password **demo_pwd**, alias **demo_alias**, and additional information **demo_label**. 14 15```c 16#include <string.h> 17 18#include "asset/asset_api.h" 19 20void AddAsset() { 21 static const char *SECRET = "demo_pwd"; 22 static const char *ALIAS = "demo_alias"; 23 static const char *LABEL = "demo_label"; 24 25 Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 26 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 27 Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 28 Asset_Attr attr[] = { 29 { .tag = ASSET_TAG_SECRET, .value.blob = secret }, 30 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 31 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, 32 { .tag = ASSET_TAG_SYNC_TYPE, .value.u32 = ASSET_SYNC_TYPE_TRUSTED_DEVICE }, // You need to specify the sync type between trusted devices (for example, clone between old and new devices). 33 }; 34 35 int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); 36 if (ret == ASSET_SUCCESS) { 37 // The asset that supports sync is added successfully. 38 } else { 39 // The asset that supports sync failed to be added. 40 } 41} 42``` 43 44## Accessing the Backup and Restore Extension Capability 45 46To trigger the backup and restore of application data, see [Accessing Backup and Restore](../../file-management/app-file-backup-extension.md). 47 48## Querying the Sync Result of Assets 49 50### Available APIs 51 52You can call **OH_Asset_QuerySyncResult** to query the sync result of assets. For more information, see the API reference. 53 54The following table describes the attributes for querying an asset. 55 56| Attribute Name (Asset_Tag) | Attribute Content (Asset_Value) | Mandatory or Not| Description | 57| ------------------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ | 58| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED<sup>14+</sup> | Type: bool| Yes| Whether to query the sync result of the asset whose custom additional information is encrypted. The value **true** means to query the sync result; the value **false** means the opposite. The default value is **false**.| 59| ASSET_TAG_GROUP_ID<sup>18+</sup> | Type: Uint8[]<br>Length: 7-127 bytes| Yes| Group to which the asset to be queried belongs. By default, this parameter is not specified.| 60 61 62### Sample Code 63 64```c 65#include <string.h> 66 67#include "asset/asset_api.h" 68 69void QuerySyncResults() { 70 Asset_SyncResult syncResult = {0}; 71 int32_t ret = OH_Asset_QuerySyncResult(NULL, 0, &syncResult); 72 if (ret == ASSET_SUCCESS) { 73 // The asset sync result is successfully queried. 74 } else { 75 // The asset sync result failed to be queried. 76 } 77} 78``` 79 80## Notes and Constraints 81 82For a successful sync between trusted devices, the assets of both old and new devices must be accessible. Otherwise, the sync might fail. 83 84* For assets that are accessible only when a password is set, the sync will fail if no lock screen password is set on either the old or new device. 85 86* For assets that are accessible only when the screen is unlocked, the sync will fail if the screen of either the old or new device is locked. 87 88* For assets that are accessible only after user authentication, the sync will fail if no lock screen password is set on the old device. 89