1# Managing Assets in a Group (C/C++) 2 3Before managing assets in a group, ensure that you are familiar with the following operations: 4 5- [Adding an Asset (C/C++)](asset-native-add.md) 6- [Removing Assets (C/C++)](asset-native-remove.md) 7- [Updating an Asset (C/C++)](asset-native-update.md) 8- [Querying Assets (C/C++)](asset-native-query.md) 9 10## Prerequisites 11 12Set the group ID **demo_group_id** in the **app.json5** file. 13 14```json 15{ 16 "app": { 17 // Other configuration items are omitted here. 18 "assetAccessGroups": [ 19 "demo_group_id" 20 ] 21 } 22} 23``` 24 25## Adding an Asset to a Group 26 27Add an asset to the group, with the password **demo_pwd**, alias **demo_alias**, and additional attribute **demo_label**. The asset can be accessed after the device is unlocked for the first time. 28 29```c 30#include <string.h> 31 32#include "asset/asset_api.h" 33 34void AddAsset() { 35 static const char *SECRET = "demo_pwd"; 36 static const char *ALIAS = "demo_alias"; 37 static const char *LABEL = "demo_label"; 38 static const char *GROUP_ID = "demo_group_id"; 39 40 Asset_Blob secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 41 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 42 Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 43 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 44 Asset_Attr attr[] = { 45 { .tag = ASSET_TAG_ACCESSIBILITY, .value.u32 = ASSET_ACCESSIBILITY_DEVICE_FIRST_UNLOCKED }, 46 { .tag = ASSET_TAG_SECRET, .value.blob = secret }, 47 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 48 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label }, 49 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 50 }; 51 52 int32_t ret = OH_Asset_Add(attr, sizeof(attr) / sizeof(attr[0])); 53 if (ret == ASSET_SUCCESS) { 54 // Asset added to the group successfully. 55 } else { 56 // Failed to add Asset to the group. 57 } 58} 59``` 60 61## Removing an Asset from a Group 62 63Remove asset **demo_alias** from group **demo_group_id**. 64 65```c 66#include <string.h> 67 68#include "asset/asset_api.h" 69 70void RemoveAsset() { 71 static const char *ALIAS = "demo_alias"; 72 static const char *GROUP_ID = "demo_group_id"; 73 74 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 75 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 76 Asset_Attr attr[] = { 77 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // Specify the asset alias to remove a single asset. To remove all assets, leave the alias unspecified. 78 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 79 }; 80 81 int32_t ret = OH_Asset_Remove(attr, sizeof(attr) / sizeof(attr[0])); 82 if (ret == ASSET_SUCCESS) { 83 // Asset removed from the group successfully. 84 } else { 85 // Failed to remove Asset from the group. 86 } 87} 88``` 89 90## Updating an Asset in a Group 91 92Update asset **demo_alias** in group **demo_group_id** as follows: change the asset plaintext to **demo_pwd_new** and the additional attribute to **demo_label_new**. 93 94```c 95#include <string.h> 96 97#include "asset/asset_api.h" 98 99void UpdateAsset() { 100 static const char *ALIAS = "demo_alias"; 101 static const char *SECRET = "demo_pwd_new"; 102 static const char *LABEL = "demo_label_new"; 103 static const char *GROUP_ID = "demo_group_id"; 104 105 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 106 Asset_Blob new_secret = { (uint32_t)(strlen(SECRET)), (uint8_t *)SECRET }; 107 Asset_Blob new_label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL }; 108 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 109 Asset_Attr query[] = { 110 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, 111 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 112 }; 113 Asset_Attr attributesToUpdate[] = { 114 { .tag = ASSET_TAG_SECRET, .value.blob = new_secret }, 115 { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = new_label }, 116 }; 117 118 int32_t ret = OH_Asset_Update(query, sizeof(query) / sizeof(query[0]), attributesToUpdate, 119 sizeof(attributesToUpdate) / sizeof(attributesToUpdate[0])); 120 if (ret == ASSET_SUCCESS) { 121 // Asset updated in the group successfully. 122 } else { 123 // Failed to update Asset in the group. 124 } 125} 126``` 127 128## Querying the Plaintext of an Asset in a Group 129 130Query the plaintext of asset **demo_alias** in group **demo_group_id**. 131 132```c 133#include <string.h> 134 135#include "asset/asset_api.h" 136 137void QueryAsset() { 138 static const char *ALIAS = "demo_alias"; 139 static const char *GROUP_ID = "demo_group_id"; 140 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 141 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 142 Asset_Attr attr[] = { 143 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // Specify the alias of the asset to query. 144 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // Return all asset information, including the attributes and asset plaintext, in the group. 145 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 146 }; 147 148 Asset_ResultSet resultSet = {0}; 149 int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 150 if (ret == ASSET_SUCCESS) { 151 // Parse the resultSet. 152 for (uint32_t i = 0; i < resultSet.count; i++) { 153 // Parse the secret: the data is secret->blob.data, the size is secret->blob.size. 154 Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET); 155 } 156 } 157 OH_Asset_FreeResultSet(&resultSet); 158} 159``` 160 161## Querying the Attributes of an Asset in a Group 162 163Query attributes of asset **demo_alias**. 164 165```c 166#include <string.h> 167 168#include "asset/asset_api.h" 169 170void QueryAttributes() { 171 static const char *ALIAS = "demo_alias"; 172 static const char *GROUP_ID = "demo_group_id"; 173 Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS }; 174 Asset_Blob group_id = { (uint32_t)(strlen(GROUP_ID)), (uint8_t *)GROUP_ID }; 175 Asset_Attr attr[] = { 176 { .tag = ASSET_TAG_ALIAS, .value.blob = alias }, // Specify the alias of the asset to query. 177 { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // Return only the asset attributes of the asset in the group, that is, the result does not include the asset plaintext. 178 { .tag = ASSET_TAG_GROUP_ID, .value.blob = group_id }, 179 }; 180 181 Asset_ResultSet resultSet = {0}; 182 int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet); 183 if (ret == ASSET_SUCCESS) { 184 // Parse the result. 185 for (uint32_t i = 0; i < resultSet.count; i++) { 186 // Parse the data label: the data is label->blob.data, the size is label->blob.size. 187 Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1); 188 } 189 } 190 OH_Asset_FreeResultSet(&resultSet); 191} 192``` 193