1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef CLOUD_EXTENSION_TYPES_H
17 #define CLOUD_EXTENSION_TYPES_H
18 
19 #ifndef CLOUD_EXTENSION_BASIC_RUST_TYPES_H
20 #include "basic_rust_types.h"
21 #endif
22 
23 #include <stddef.h>
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 /**
32  * @brief       Type declaration of Rust cloud extension struct Value.
33  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call
34  *              `OhCloudExtValueFree` to release the memory occupied
35  */
36 typedef struct {
37     const size_t id;
38 } OhCloudExtValue;
39 
40 /**
41  * @brief       Enumeration represents inner type of contents in Value enum.
42  */
43 enum OhCloudExtValueType {
44     VALUEINNERTYPE_EMPTY,
45     VALUEINNERTYPE_INT,
46     VALUEINNERTYPE_FLOAT,
47     VALUEINNERTYPE_STRING,
48     VALUEINNERTYPE_BOOL,
49     VALUEINNERTYPE_BYTES,
50     VALUEINNERTYPE_ASSET,
51     VALUEINNERTYPE_ASSETS
52 };
53 
54 /**
55  * @brief       Create an Value instance according to ValueInnerType.
56  * @retval      != NULL, a valid pointer of OhCloudExtValue.
57  */
58 OhCloudExtValue *OhCloudExtValueNew(OhCloudExtValueType typ, void *content, unsigned int contentLen);
59 
60 /**
61  * @brief       Get content from a Value pointer.
62  * @param       src         [IN]
63  *              typ         [OUT] User should use this value to cast the output pointer.
64  *              content     [OUT] The pointer value will be updated to point to the content of the Value.
65  *              contentLen  [OUT]
66  * @attention   If the type is Asset or Assets, the OhCloudExtVector pointer returned should be freed by
67  *              OhCloudExtVectorFree.
68  */
69 int OhCloudExtValueGetContent(
70     OhCloudExtValue *src,
71     OhCloudExtValueType *typ,
72     void **content,
73     unsigned int *contentLen
74 );
75 
76 /**
77  * @brief Free a Value pointer.
78  */
79 void OhCloudExtValueFree(OhCloudExtValue *src);
80 
81 /**
82  * @brief       AssetStutus enumeration.
83  */
84 enum OhCloudExtAssetStatus {
85     ASSETSTATUS_UNKNOWN,
86     ASSETSTATUS_NORMAL,
87     ASSETSTATUS_INSERT,
88     ASSETSTATUS_UPDATE,
89     ASSETSTATUS_DELETE,
90     ASSETSTATUS_ABNORMAL,
91     ASSETSTATUS_DOWNLOADING,
92     ASSETSTATUS_BUTT,
93 };
94 
95 /**
96 * @brief       CloudAsset builder that will used to build an CloudAsset in Rust side
97 */
98 typedef struct {
99     const unsigned long long version;
100     const OhCloudExtAssetStatus status;
101     const unsigned long long expiresTime;
102     const unsigned char *id;
103     const unsigned int idLen;
104     const unsigned char *name;
105     const unsigned int nameLen;
106     const unsigned char *uri;
107     const unsigned int uriLen;
108     const unsigned char *localPath;
109     const unsigned int localPathLen;
110     const unsigned char *createTime;
111     const unsigned int createTimeLen;
112     const unsigned char *modifyTime;
113     const unsigned int modifyTimeLen;
114     const unsigned char *size;
115     const unsigned int sizeLen;
116     const unsigned char *hash;
117     const unsigned int hashLen;
118 } OhCloudExtCloudAssetBuilder;
119 
120 /**
121  * @brief       Type declaration of Rust cloud extension struct CloudAsset.
122  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call
123  *              `OhCloudExtCloudAssetFree` to release the memory occupied
124  */
125 typedef struct {
126     const size_t id;
127 } OhCloudExtCloudAsset;
128 
129 /**
130  * @brief       Initialize an CloudAsset by the AssetBuilder.
131  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call
132  *              `OhCloudExtCloudAssetFree` to release the memory occupied
133  */
134 OhCloudExtCloudAsset *OhCloudExtCloudAssetNew(const OhCloudExtCloudAssetBuilder *builder);
135 
136 /**
137  * @brief       Get id from an CloudAsset pointer.
138  */
139 int OhCloudExtCloudAssetGetId(const OhCloudExtCloudAsset *asset, unsigned char **id, unsigned int *len);
140 
141 /**
142  * @brief       Get name from an CloudAsset pointer.
143  */
144 int OhCloudExtCloudAssetGetName(const OhCloudExtCloudAsset *asset, unsigned char **name, unsigned int *len);
145 
146 /**
147  * @brief       Get uri from an CloudAsset pointer.
148  */
149 int OhCloudExtCloudAssetGetUri(const OhCloudExtCloudAsset *asset, unsigned char **uri, unsigned int *len);
150 
151 /**
152  * @brief       Get local path from an CloudAsset pointer.
153  */
154 int OhCloudExtCloudAssetGetLocalPath(const OhCloudExtCloudAsset *asset, unsigned char **localPath, unsigned int *len);
155 
156 /**
157  * @brief       Get create time from an CloudAsset pointer.
158  */
159 int OhCloudExtCloudAssetGetCreateTime(const OhCloudExtCloudAsset *asset, unsigned char **createTime, unsigned int *len);
160 
161 /**
162  * @brief       Get modified time from an CloudAsset pointer.
163  */
164 int OhCloudExtCloudAssetGetModifiedTime(
165     const OhCloudExtCloudAsset *asset, unsigned char **modifiedTime, unsigned int *len);
166 
167 /**
168  * @brief       Get size from an CloudAsset pointer.
169  */
170 int OhCloudExtCloudAssetGetSize(const OhCloudExtCloudAsset *asset, unsigned char **size, unsigned int *len);
171 
172 /**
173  * @brief       Get hash from an CloudAsset pointer.
174  */
175 int OhCloudExtCloudAssetGetHash(const OhCloudExtCloudAsset *asset, unsigned char **hash, unsigned int *len);
176 
177 /**
178  * @brief Free an CloudAsset pointer.
179  */
180 void OhCloudExtCloudAssetFree(OhCloudExtCloudAsset *src);
181 
182 /**
183  * @brief       Type declaration of Rust cloud extension struct Database. Can be obtained from Schema.
184  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call `DatabaseFree` to
185  *              release the memory occupied
186  */
187 typedef struct {
188     const size_t id;
189 } OhCloudExtDatabase;
190 
191 /**
192  * @brief       Create a Database instance.
193  * @param       tables  [IN] HashMap<String, Table>, table_name as key
194  * @retval      != NULL, a valid pointer of Database.
195  * @attention   When passed in, database will take control of the memory management of the vector tables. No more free
196  *              is needed. For the database pointer, the memory is managed by Rust. Therefore, to prevent memory leaks,
197  *              users should call `OhCloudExtDatabaseFree` to release the memory occupied
198  */
199 OhCloudExtDatabase *OhCloudExtDatabaseNew(
200     const unsigned char *name,
201     unsigned int nameLen,
202     const unsigned char *alias,
203     unsigned int aliasLen,
204     OhCloudExtHashMap *tables
205 );
206 
207 /**
208  * @brief       Get name from a Database instance.
209  * @param       db          [IN]
210  *              name        [OUT]
211  *              len         [OUT]
212  * @retval      SUCCESS
213  *              ERRNO_NULLPTR
214  * @attention   Name returned shouldn't be freed, or issues of double free is possible.
215  */
216 int OhCloudExtDatabaseGetName(const OhCloudExtDatabase *db, unsigned char **name, unsigned int *len);
217 
218 /**
219  * @brief       Get alias from a Database instance.
220  * @param       db           [IN]
221  *              alias        [OUT]
222  *              len          [OUT]
223  * @retval      SUCCESS
224  *              ERRNO_NULLPTR
225  * @attention   Alias returned shouldn't be freed, or issues of double free is possible.
226  */
227 int OhCloudExtDatabaseGetAlias(const OhCloudExtDatabase *db, unsigned char **alias, unsigned int *len);
228 
229 /**
230  * @brief       Get tables from a Database instance.
231  * @param       db          [IN]
232  *              tables      [OUT] HashMap<String, Table>, table name as key
233  * @retval      SUCCESS
234  *              ERRNO_NULLPTR
235  * @attention   The HashMap returned should be freed by OhCloudExtHashMapFree.
236  */
237 int OhCloudExtDatabaseGetTable(const OhCloudExtDatabase *db, OhCloudExtHashMap **tables);
238 
239 /**
240  * @brief       Free a Database pointer.
241  */
242 void OhCloudExtDatabaseFree(OhCloudExtDatabase *db);
243 
244 /**
245  * @brief       Type declaration of Rust cloud extension struct Table. Can be obtained from Database.
246  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call
247  *              `OhCloudExtTableFree` to release the memory occupied
248  */
249 typedef struct {
250     const size_t id;
251 } OhCloudExtTable;
252 
253 /**
254  * @brief       Create a Table instance.
255  *              fields      [IN] Vec<Field>
256  * @attention   When passed in, table will take control of the memory management of the vector field. No more free is
257  *              needed.
258  */
259 OhCloudExtTable *OhCloudExtTableNew(
260     const unsigned char *name,
261     unsigned int nameLen,
262     const unsigned char *alias,
263     unsigned int aliasLen,
264     OhCloudExtVector *fields
265 );
266 
267 /**
268  * @brief       Get name from a Table instance.
269  * @param       tb          [IN]
270  *              name        [OUT]
271  *              len         [OUT]
272  * @attention   Name returned shouldn't be freed, or issues of double free is possible.
273  */
274 int OhCloudExtTableGetName(const OhCloudExtTable *tb, unsigned char **name, unsigned int *len);
275 
276 /**
277  * @brief       Get alias from a Table instance.
278  * @param       tb          [IN]
279  *              alias       [OUT]
280  *              len         [OUT]
281  * @attention   Alias returned shouldn't be freed, or issues of double free is possible.
282  */
283 int OhCloudExtTableGetAlias(const OhCloudExtTable *tb, unsigned char **alias, unsigned int *len);
284 
285 /**
286  * @brief       Get fields from a Database instance.
287  * @param       tb          [IN]
288  *              fields      [OUT]  Vec<Field>
289  *              len         [OUT]
290  * @attention   The Vector  returned should be freed by OhCloudExtVectorFree.
291  */
292 int OhCloudExtTableGetFields(const OhCloudExtTable *tb, OhCloudExtVector **fields);
293 
294 /**
295  * @brief       Free a Table pointer.
296  */
297 void OhCloudExtTableFree(OhCloudExtTable *db);
298 
299 /**
300  * @brief       Type declaration of Rust cloud extension struct Field. Can be obtained from Table.
301  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call `FieldFree` to
302  *              release the memory occupied
303  */
304 typedef struct {
305     const size_t id;
306 } OhCloudExtField;
307 
308 /**
309  * @brief       FieldBuilder providing necessary information when create a Field.
310  */
311 typedef struct {
312     const unsigned char *colName;
313     const unsigned int colNameLen;
314     const unsigned char *alias;
315     const unsigned int aliasLen;
316     const unsigned int typ;
317     const bool primary;
318     const bool nullable;
319 } OhCloudExtFieldBuilder;
320 
321 /**
322  * @brief       Initialize a Field instance.
323  * @attention   The memory is managed by Rust. Therefore, to prevent memory leaks, users should call
324  *              `OhCloudExtFieldFree` to release the memory occupied
325  */
326 OhCloudExtField *OhCloudExtFieldNew(const OhCloudExtFieldBuilder *builder);
327 
328 /**
329  * @brief       Get name from a Field instance.
330  * @param       fd          [IN]
331  *              name        [OUT]
332  *              len         [OUT]
333  * @attention   Name returned shouldn't be freed, or issues of double free is possible.
334  */
335 int OhCloudExtFieldGetColName(const OhCloudExtField *fd, unsigned char **name, unsigned int *len);
336 
337 /**
338  * @brief       Get alias from a Field instance.
339  * @param       fd          [IN]
340  *              alias       [OUT]
341  *              len         [OUT]
342  * @attention   Alias returned shouldn't be freed, or issues of double free is possible.
343  */
344 int OhCloudExtFieldGetAlias(const OhCloudExtField *fd, unsigned char **alias, unsigned int *len);
345 
346 /**
347  * @brief       Get type of a Field instance.
348  * @param       fd          [IN]
349  *              typ         [OUT]
350  */
351 int OhCloudExtFieldGetTyp(const OhCloudExtField *fd, unsigned int *typ);
352 
353 /**
354  * @brief       Get primacy of a Field instance.
355  * @param       fd          [IN]
356  *              primary     [OUT]
357  */
358 int OhCloudExtFieldGetPrimary(const OhCloudExtField *fd, bool *primary);
359 
360 /**
361  * @brief       Get nullability of a Field instance.
362  * @param       fd          [IN]
363  *              nullable    [OUT]
364  */
365 int OhCloudExtFieldGetNullable(const OhCloudExtField *fd, bool *nullable);
366 
367 /**
368  * @brief       Free a Field pointer.
369  */
370 void OhCloudExtFieldFree(OhCloudExtField *db);
371 
372 /**
373  * @brief       Type declaration of Rust cloud extension struct CloudInfo. Can be obtained from
374  *              CloudServerGetServiceInfo.
375  * @attention   CloudInfo is obtained from CloudSync to provide relevant information to users. When done, users should
376  *              call CloudInfoFree to release memory and prevent memory leak.
377  */
378 typedef struct {
379     const size_t id;
380 } OhCloudExtCloudInfo;
381 
382 /**
383  * @brief       Get user from a CloudInfo pointer.
384  */
385 int OhCloudExtCloudInfoGetUser(const OhCloudExtCloudInfo *info, int *user);
386 
387 /**
388  * @brief       Get user from a CloudInfo pointer.
389  */
390 int OhCloudExtCloudInfoGetId(
391     const OhCloudExtCloudInfo *info,
392     unsigned char **id,
393     unsigned int *idLen
394 );
395 
396 /**
397  * @brief       Get total space from a CloudInfo pointer.
398  */
399 int OhCloudExtCloudInfoGetTotalSpace(
400     const OhCloudExtCloudInfo *info,
401     unsigned long long *totalSpace
402 );
403 
404 /**
405  * @brief       Get remain space from a CloudInfo pointer.
406  */
407 int OhCloudExtCloudInfoGetRemainSpace(
408     const OhCloudExtCloudInfo *info,
409     unsigned long long *remainSpace
410 );
411 
412 /**
413  * @brief       Check whether a CloudInfo enables cloud sync.
414  */
415 int OhCloudExtCloudInfoEnabled(
416     const OhCloudExtCloudInfo *info,
417     bool *enableCloud
418 );
419 
420 /**
421  * @brief       Get hashmap of AppInfo from a CloudInfo pointer.
422  * @param       appInfo     [OUT] HashMap<String, AppInfo>, bundle name as key
423  */
424 int OhCloudExtCloudInfoGetAppInfo(
425     const OhCloudExtCloudInfo *info,
426     OhCloudExtHashMap **appInfo
427 );
428 
429 /**
430  * @brief       Destroy an CloudInfo instance.
431  * @param       ptr     [IN]    The pointer of CloudInfo that should be destroyed.
432  */
433 void OhCloudExtCloudInfoFree(OhCloudExtCloudInfo *ptr);
434 
435 /**
436  * @brief       Type declaration of Rust cloud extension struct AppInfo.
437  * @attention   AppInfo is obtained from CloudInfo. When CloudInfo is freed, AppInfo will also be
438  *              freed.
439  */
440 typedef struct {
441     const size_t id;
442 } OhCloudExtAppInfo;
443 
444 /**
445  * @brief       Get id from an AppInfo pointer.
446  */
447 int OhCloudExtAppInfoGetAppId(
448     const OhCloudExtAppInfo *appInfo,
449     unsigned char **appId,
450     unsigned int *idLen
451 );
452 
453 /**
454  * @brief       Get bundle name from an AppInfo pointer.
455  */
456 int OhCloudExtAppInfoGetBundleName(
457     const OhCloudExtAppInfo *appInfo,
458     unsigned char **bundleName,
459     unsigned int *len
460 );
461 
462 /**
463  * @brief       Check whether an AppInfo pointer allows cloud switch.
464  */
465 int OhCloudExtAppInfoGetCloudSwitch(
466     const OhCloudExtAppInfo *appInfo,
467     bool *cloudSwitch
468 );
469 
470 /**
471  * @brief       Get instance id from an AppInfo pointer.
472  */
473 int OhCloudExtAppInfoGetInstanceId(
474     const OhCloudExtAppInfo *appInfo,
475     int *id
476 );
477 
478 /**
479  * @brief       Free a AppInfo ptr.
480  */
481 void OhCloudExtAppInfoFree(OhCloudExtAppInfo *info);
482 
483 /**
484  * @brief       Type declaration of Rust cloud extension struct SchemaMeta. Can be obtained from
485  *              CloudSyncGetServiceInfo.
486  * @attention   SchemaMeta is obtained from CloudSync to provide relevant information to users. When done, users
487  *              should call SchemaMetaFree to release memory and prevent memory leak.
488  */
489 typedef struct {
490     const size_t id;
491 } OhCloudExtSchemaMeta;
492 
493 /**
494  * @brief       Get version from a SchemaMeta pointer.
495  */
496 int OhCloudExtSchemaMetaGetVersion(const OhCloudExtSchemaMeta *schema, int *version);
497 
498 /**
499  * @brief       Get bundle name from a SchemaMeta pointer.
500  */
501 int OhCloudExtSchemaMetaGetBundleName(const OhCloudExtSchemaMeta *schema, unsigned char **name, unsigned int *len);
502 
503 /**
504  * @brief       Get databases from a SchemaMeta instance.
505  * @param       schema     [IN]
506  *              db         [IN] Vec<Database>
507  * @attention   The Vector returned should be freed by OhCloudExtVectorFree.
508  */
509 int OhCloudExtSchemaMetaGetDatabases(
510     const OhCloudExtSchemaMeta *schema,
511     OhCloudExtVector **db
512 );
513 
514 /**
515  * @brief       Destroy an SchemaMeta instance.
516  * @param       ptr     [IN]    The pointer of SchemaMeta that should be destroyed.
517  */
518 void OhCloudExtSchemaMetaFree(OhCloudExtSchemaMeta *ptr);
519 
520 /**
521  * @brief       Type declaration of Rust cloud extension struct RelationSet.
522  * @attention   When done, users should call `RelationSetFree` to release memory and prevent memory leak.
523  */
524 typedef struct {
525     const size_t id;
526 } OhCloudExtRelationSet;
527 
528 /**
529  * @brief       Create a RelationSet instance by bundle name, and relations.
530  * @param       relations  [IN] HashMap<String, String>, Database alias as key, subscription id and time generated
531  *                              by the cloud as value
532  * @attention   The hashmap passed in will be managed in the Rust side, so don't call free on this pointer again.
533  */
534 OhCloudExtRelationSet *OhCloudExtRelationSetNew(
535     const unsigned char *bundleName,
536     unsigned int nameLen,
537     int expireTime,
538     OhCloudExtHashMap *relations
539 );
540 
541 /**
542  * @brief       Get bundle name from a RelationSet pointer.
543  */
544 int OhCloudExtRelationSetGetBundleName(const OhCloudExtRelationSet *re, unsigned char **name, unsigned int *len);
545 
546 /**
547  * @brief       Get expire time from a RelationSet pointer.
548  */
549 int OhCloudExtRelationSetGetExpireTime(const OhCloudExtRelationSet *re, unsigned long long *time);
550 
551 /**
552  * @brief       Get relations from a RelationSet pointer.
553  * @param       relations  [OUT] HashMap<String, u32>, Database alias as key, subscription id and time generated
554  *                               by the cloud as value
555  * @attention   The hashmap returned should be freed by OhCloudExtHashMapFree.
556  */
557 int OhCloudExtRelationSetGetRelations(const OhCloudExtRelationSet *re, OhCloudExtHashMap **relations);
558 
559 /**
560  * @brief       Free a RelationSet pointer.
561  */
562 void OhCloudExtRelationSetFree(OhCloudExtRelationSet *ptr);
563 
564 /**
565  * @brief       Type declaration of Rust cloud extension struct CloudDbData. Can be obtained from CloudDbBatchQuery.
566  * @attention   CloudDbData is obtained from CloudDatabase to provide relevant information to users. When done, users
567  *              should call `CloudDbDataFree` to release memory and prevent memory leak.
568  */
569 typedef struct {
570     const size_t id;
571 } OhCloudExtCloudDbData;
572 
573 /**
574  * @brief       Get the next cursor from a CloudDbData pointer.
575  */
576 int OhCloudExtCloudDbDataGetNextCursor(const OhCloudExtCloudDbData *data, unsigned char **cursor, unsigned int *len);
577 
578 /**
579  * @brief       Check whether a CloudDbData has more data.
580  */
581 int OhCloudExtCloudDbDataGetHasMore(const OhCloudExtCloudDbData *data, bool *hasMore);
582 
583 /**
584  * @brief       Get vector of values from a CloudDbData pointer.
585  * @param       values  [OUT] Vec<ValueBucket>
586  * @attention   The vector returned should be freed by OhCloudExtVectorFree.
587  */
588 int OhCloudExtCloudDbDataGetValues(const OhCloudExtCloudDbData *data, OhCloudExtVector **values);
589 
590 /**
591  * @brief       Free a CloudDbData pointer.
592  */
593 void OhCloudExtCloudDbDataFree(OhCloudExtCloudDbData *ptr);
594 
595 /**
596  * @brief       Type declaration of Rust cloud extension struct KeyName.
597  */
598 typedef struct {
599     unsigned char *key;
600     unsigned int keyLen;
601 } OhCloudExtKeyName;
602 
603 /**
604  * @brief       Create a KeyName instance by key, and keyLen.
605  */
606 OhCloudExtKeyName OhCloudExtKeyNameNew(const unsigned char *key, unsigned int keyLen);
607 
608 /**
609  * @brief       Type declaration of Rust cloud extension struct ValueBucket.
610  */
611 typedef struct {
612     const size_t id;
613 } OhCloudExtValueBucket;
614 
615 /**
616  * @brief      Get ValueBucket all keys from a ValueBucket ptr.
617  */
618 int OhCloudExtValueBucketGetKeys(OhCloudExtValueBucket *info, OhCloudExtVector **keys, unsigned int *keysLen);
619 
620 /**
621  * @brief      Get ValueBucket Value from a ValueBucket prt.
622  */
623 int OhCloudExtValueBucketGetValue(OhCloudExtValueBucket *info, OhCloudExtKeyName keyName,
624                                   OhCloudExtValueType *typ, void **content, unsigned int *contentLen);
625 
626 /**
627  * @brief       Free a ValueBucket ptr.
628  */
629 void OhCloudExtValueBucketFree(OhCloudExtValueBucket *info);
630 
631 #ifdef __cplusplus
632 #if __cplusplus
633 }
634 #endif
635 #endif
636 
637 #endif // CLOUD_EXTENSION_TYPES_H
638