1# Querying Assets (C/C++)
2
3## Available APIs
4
5You can use [OH_Asset_Query](../../reference/apis-asset-store-kit/_asset_api.md#oh_asset_query) to query asset
6information.
7
8The following table describes the attributes for querying an asset.
9
10>**NOTE**
11>
12>In the following table, the attributes starting with **ASSET_TAG_DATA_LABEL** are custom asset attributes reserved. These attributes are not encrypted. Therefore, do not put personal data in these attributes.
13
14| Attribute Name (Asset_Tag)           | Attribute Content (Asset_Value)                                      | Mandatory| Description                                                        |
15| ------------------------------- | ------------------------------------------------------------ | -------- | ------------------------------------------------------------ |
16| ASSET_TAG_ALIAS                 | Type: uint8[]<br>Length: 1-256 bytes                              | No    | Asset alias, which uniquely identifies an asset.                      |
17| ASSET_TAG_ACCESSIBILITY         | Type: uint32_t<br>Value range: see [Asset_Accessibility](../../reference/apis-asset-store-kit/_asset_type.md#asset_accessibility)| No    | Access control based on the lock screen status.                                                |
18| ASSET_TAG_REQUIRE_PASSWORD_SET  | Type: bool                                                  | No    | Whether the asset is accessible only when a lock screen password is set.                |
19| ASSET_TAG_AUTH_TYPE             | Type: uint32_t<br>Value range: see [Asset_AuthType](../../reference/apis-asset-store-kit/_asset_type.md#asset_authtype)| No    | Type of user authentication required for accessing the asset.                              |
20| ASSET_TAG_SYNC_TYPE             | Type: uint32_t<br>Value range: see [Asset_SyncType](../../reference/apis-asset-store-kit/_asset_type.md#asset_synctype)| No    | Type of sync supported by the asset.                                      |
21| ASSET_TAG_IS_PERSISTENT         | Type: bool                                                  | No    | Whether to retain the asset when the application is uninstalled.                            |
22| ASSET_TAG_DATA_LABEL_CRITICAL_1 | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
23| ASSET_TAG_DATA_LABEL_CRITICAL_2 | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
24| ASSET_TAG_DATA_LABEL_CRITICAL_3 | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
25| ASSET_TAG_DATA_LABEL_CRITICAL_4 | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service with integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
26| ASSET_TAG_DATA_LABEL_NORMAL_1   | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
27| ASSET_TAG_DATA_LABEL_NORMAL_2   | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
28| ASSET_TAG_DATA_LABEL_NORMAL_3   | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
29| ASSET_TAG_DATA_LABEL_NORMAL_4   | Type: uint8[]<br>Length: 1-2048 bytes                          | No    | Asset attribute information customized by the service without integrity protection.<br>**NOTE**: The data length is 1 to 512 bytes before API version 12.|
30| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_1<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
31| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_2<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
32| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_3<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
33| ASSET_TAG_DATA_LABEL_NORMAL_LOCAL_4<sup>12+</sup> | Type: uint8[]<br>Length: 1-2048 bytes| No| Local attribute information about the asset. The value is assigned by the service without integrity protection and will not be synced.|
34| ASSET_TAG_RETURN_TYPE           | Type: uint32_t<br>Value range: see [Asset_ReturnType](../../reference/apis-asset-store-kit/_asset_type.md#asset_returntype)| No    | Type of the asset query result to return.                                        |
35| ASSET_TAG_RETURN_LIMIT          | Type: uint32_t                                              | No    | Maximum number of asset records to return.                                        |
36| ASSET_TAG_RETURN_OFFSET         | Type: uint32_t<br>Value range: 1-65536                           | No    | Offset of the asset query result.<br>**NOTE**: This parameter specifies the starting asset record to return in batch asset query.                 |
37| ASSET_TAG_RETURN_ORDERED_BY     | Type: uint32_t<br>Value: ASSET_TAG_DATA_LABEL_xxx          | No    | How the query results are sorted. Currently, the results can be sorted only by **ASSET_TAG_DATA_LABEL**.<br>**NOTE**: By default, assets are returned in the order in which they are added.|
38| ASSET_TAG_REQUIRE_ATTR_ENCRYPTED<sup>14+</sup> | Type: bool| No| Whether to query the customized asset attribute information that is encrypted. By default, the unencrypted, customized asset attribute information is queried.|
39
40## Constraints
41
42The assets queried are transmitted to the service through an IPC channel. Due to the limitation of the IPC buffer size, the maximum number of assets to be queried at a time cannot exceed 40.
43
44## Example
45
46### Querying the Plaintext of an Asset
47
48Query the plaintext of asset **demo_alias**.
49
501. Add the dynamic library in the CMake script.
51   ```txt
52   target_link_libraries(entry PUBLIC libasset_ndk.z.so)
53   ```
54
552. Add an asset.
56   ```c
57   #include <string.h>
58
59   #include "asset/asset_api.h"
60
61   void QueryAsset() {
62      static const char *ALIAS = "demo_alias";
63      Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS };
64      Asset_Attr attr[] = {
65         { .tag = ASSET_TAG_ALIAS, .value.blob = alias },  // Specify the alias of the asset to query.
66         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ALL }, // Return all asset information, including the attributes and asset plaintext.
67      };
68
69      Asset_ResultSet resultSet = {0};
70      int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
71      if (ret == ASSET_SUCCESS) {
72         // Parse the resultSet.
73         for (uint32_t i = 0; i < resultSet.count; i++) {
74               // Parse the secret: the data is secret->blob.data, the size is secret->blob.size.
75               Asset_Attr *secret = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_SECRET);
76         }
77      }
78      OH_Asset_FreeResultSet(&resultSet);
79   }
80   ```
81
82### Querying Attributes of an Asset
83
84Query attributes of asset **demo_alias**.
85
861. Add the dynamic library in the CMake script.
87   ```txt
88   target_link_libraries(entry PUBLIC libasset_ndk.z.so)
89   ```
90
912. Add an asset.
92   ```c
93   #include <string.h>
94
95   #include "asset/asset_api.h"
96
97   void QueryAttributes() {
98      static const char *ALIAS = "demo_alias";
99      Asset_Blob alias = { (uint32_t)(strlen(ALIAS)), (uint8_t *)ALIAS };
100      Asset_Attr attr[] = {
101         { .tag = ASSET_TAG_ALIAS, .value.blob = alias },  // Specify the alias of the asset to query.
102         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES }, // Return only the asset attributes, that is, the result does not include the asset plaintext.
103      };
104
105      Asset_ResultSet resultSet = {0};
106      int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
107      if (ret == ASSET_SUCCESS) {
108         // Parse the result.
109         for (uint32_t i = 0; i < resultSet.count; i++) {
110         // Parse the data label: the data is label->blob.data, the size is label->blob.size.
111               Asset_Attr *label = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_DATA_LABEL_NORMAL_1);
112         }
113      }
114      OH_Asset_FreeResultSet(&resultSet);
115   }
116   ```
117
118### Querying Attributes of Assets
119
120Query attributes of assets with the tag of **demo_label** and return a total of 10 records sorted by **DATA_LABEL_NORMAL_1** starting from the fifth record that matches the search criteria.
121
1221. Add the dynamic library in the CMake script.
123   ```txt
124   target_link_libraries(entry PUBLIC libasset_ndk.z.so)
125   ```
126
1272. Add an asset.
128   ```c
129   #include <string.h>
130
131   #include "asset/asset_api.h"
132
133   void BatchQuery() {
134      static const char *LABEL = "demo_label";
135      Asset_Blob label = { (uint32_t)(strlen(LABEL)), (uint8_t *)LABEL };
136
137      Asset_Attr attr[] = {
138         { .tag = ASSET_TAG_RETURN_TYPE, .value.u32 = ASSET_RETURN_ATTRIBUTES },
139         { .tag = ASSET_TAG_DATA_LABEL_NORMAL_1, .value.blob = label },
140         { .tag = ASSET_TAG_RETURN_OFFSET, .value.u32 = 5 },
141         { .tag = ASSET_TAG_RETURN_LIMIT, .value.u32 = 10 },
142         { .tag = ASSET_TAG_RETURN_ORDERED_BY, .value.u32 = ASSET_TAG_DATA_LABEL_NORMAL_1 },
143      };
144
145      Asset_ResultSet resultSet = { 0 };
146      int32_t ret = OH_Asset_Query(attr, sizeof(attr) / sizeof(attr[0]), &resultSet);
147      if (ret == ASSET_SUCCESS) {
148         // Parse the result.
149         for (uint32_t i = 0; i < resultSet.count; i++) {
150               // Parse the data alias: the data is alias->blob.data, the size is alias->blob.size.
151               Asset_Attr *alias = OH_Asset_ParseAttr(resultSet.results + i, ASSET_TAG_ALIAS);
152         }
153      }
154      OH_Asset_FreeResultSet(&resultSet);
155   }
156   ```
157