1 /*
2  * Copyright (C) 2022 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 DATA_MANAGER_H
17 #define DATA_MANAGER_H
18 
19 #include <stdbool.h>
20 #include "hc_string.h"
21 #include "hc_string_vector.h"
22 #include "hc_tlv_parser.h"
23 #include "hc_vector.h"
24 #include "json_utils.h"
25 
26 #define MAX_STRING_LEN 256
27 #define MAX_EXPIRE_TIME 90
28 #define HC_TRUST_DEV_ENTRY_MAX_NUM 101
29 #define HC_TRUST_GROUP_ENTRY_MAX_NUM 100
30 
31 typedef struct {
32     HcString name; /* group name */
33     HcString id; /* group id */
34     uint32_t type; /* including identical account group(1), peer to peer group(256), across account group(1282) */
35     int32_t visibility; /* visibility of the group */
36     int32_t expireTime; /* the time of group expired, unit day, user config */
37     HcString userId; /* the user account id */
38     HcString sharedUserId; /* the shared user account id */
39     StringVector managers; /* group manager vector, group manager can add and delete members, index 0 is the owner */
40     StringVector friends; /* group friend vector, group friend can query group information */
41     uint8_t upgradeFlag;
42 } TrustedGroupEntry;
43 DECLARE_HC_VECTOR(GroupEntryVec, TrustedGroupEntry*)
44 
45 typedef struct {
46     TrustedGroupEntry *groupEntry;
47     HcString groupId;
48     HcString udid; /* unique device id */
49     HcString authId; /* id by service defined for authentication */
50     HcString userId; /* the user account id */
51     HcString serviceType; /* compatible with previous versions, the value is the same as groupId */
52     HcParcel ext; /* for caching extern data, user data */
53     uint8_t credential; /* 1 - asymmetrical, 2 - symmetrical */
54     uint8_t devType; /* 0 - accessory, 1 - controller, 2 - proxy */
55     uint8_t source; /* the tursted relationship source. 0: self-created, 1: imported from the cloud */
56     uint64_t lastTm; /* accessed time of the device of the auth information, absolute time */
57     uint8_t upgradeFlag;
58 } TrustedDeviceEntry;
59 DECLARE_HC_VECTOR(DeviceEntryVec, TrustedDeviceEntry*)
60 
61 typedef struct {
62     const char *groupId;
63     const char *groupName;
64     const char *ownerName;
65     const char *userId;
66     const char *sharedUserId;
67     uint32_t groupType;
68     int32_t groupVisibility;
69 } QueryGroupParams;
70 
71 typedef struct {
72     const char *groupId;
73     const char *udid;
74     const char *authId;
75     const char *userId;
76 } QueryDeviceParams;
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 int32_t InitDatabase(void);
83 void DestroyDatabase(void);
84 
85 void ReloadOsAccountDb(int32_t osAccountId);
86 int32_t AddGroup(int32_t osAccountId, const TrustedGroupEntry *groupEntry);
87 int32_t DelGroup(int32_t osAccountId, const QueryGroupParams *params);
88 int32_t AddTrustedDevice(int32_t osAccountId, const TrustedDeviceEntry *deviceEntry);
89 int32_t DelTrustedDevice(int32_t osAccountId, const QueryDeviceParams *params);
90 int32_t QueryGroups(int32_t osAccountId, const QueryGroupParams *params, GroupEntryVec *vec);
91 int32_t QueryDevices(int32_t osAccountId, const QueryDeviceParams *params, DeviceEntryVec *vec);
92 int32_t SaveOsAccountDb(int32_t osAccountId);
93 bool GenerateGroupEntryFromEntry(const TrustedGroupEntry *entry, TrustedGroupEntry *returnEntry);
94 bool GenerateDeviceEntryFromEntry(const TrustedDeviceEntry *entry, TrustedDeviceEntry *returnEntry);
95 
96 TrustedGroupEntry *DeepCopyGroupEntry(const TrustedGroupEntry *entry);
97 TrustedDeviceEntry *DeepCopyDeviceEntry(const TrustedDeviceEntry *entry);
98 
99 QueryGroupParams InitQueryGroupParams(void);
100 QueryDeviceParams InitQueryDeviceParams(void);
101 
102 int32_t GenerateReturnGroupInfo(const TrustedGroupEntry *groupEntry, CJson *returnJson);
103 int32_t GenerateReturnDevInfo(const TrustedDeviceEntry *deviceEntry, CJson *returnJson);
104 
105 TrustedGroupEntry *CreateGroupEntry(void);
106 TrustedDeviceEntry *CreateDeviceEntry(void);
107 void DestroyGroupEntry(TrustedGroupEntry *groupEntry);
108 void DestroyDeviceEntry(TrustedDeviceEntry *deviceEntry);
109 GroupEntryVec CreateGroupEntryVec(void);
110 DeviceEntryVec CreateDeviceEntryVec(void);
111 void ClearGroupEntryVec(GroupEntryVec *vec);
112 void ClearDeviceEntryVec(DeviceEntryVec *vec);
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 #endif
118