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 #include "data_manager.h"
17
18 #include "broadcast_manager.h"
19 #include "common_defs.h"
20 #include "device_auth.h"
21 #include "device_auth_defines.h"
22 #include "hc_dev_info.h"
23 #include "hc_file.h"
24 #include "hc_log.h"
25 #include "hc_mutex.h"
26 #include "hc_string_vector.h"
27 #include "hc_types.h"
28 #include "securec.h"
29 #include "hidump_adapter.h"
30
31 typedef struct {
32 DECLARE_TLV_STRUCT(9)
33 TlvString name;
34 TlvString id;
35 TlvUint32 type;
36 TlvInt32 visibility;
37 TlvInt32 expireTime;
38 TlvString userId;
39 TlvString sharedUserId;
40 TlvBuffer managers;
41 TlvBuffer friends;
42 } TlvGroupElement;
43 DECLEAR_INIT_FUNC(TlvGroupElement)
44 DECLARE_TLV_VECTOR(TlvGroupVec, TlvGroupElement)
45
46 typedef struct {
47 uint8_t credential;
48 uint8_t devType;
49 uint8_t source;
50 int64_t userId;
51 uint64_t lastTm;
52 } DevAuthFixedLenInfo;
53 DECLARE_TLV_FIX_LENGTH_TYPE(TlvDevAuthFixedLenInfo, DevAuthFixedLenInfo)
54 DECLEAR_INIT_FUNC(TlvDevAuthFixedLenInfo)
55
56 typedef struct {
57 DECLARE_TLV_STRUCT(7)
58 TlvString groupId;
59 TlvString udid;
60 TlvString authId;
61 TlvString userId;
62 TlvString serviceType;
63 TlvBuffer ext;
64 TlvDevAuthFixedLenInfo info;
65 } TlvDeviceElement;
66 DECLEAR_INIT_FUNC(TlvDeviceElement)
67 DECLARE_TLV_VECTOR(TlvDeviceVec, TlvDeviceElement)
68
69 typedef struct {
70 DECLARE_TLV_STRUCT(3)
71 TlvInt32 version;
72 TlvGroupVec groups;
73 TlvDeviceVec devices;
74 } HCDataBaseV1;
75 DECLEAR_INIT_FUNC(HCDataBaseV1)
76
77 DEFINE_TLV_FIX_LENGTH_TYPE(TlvDevAuthFixedLenInfo, NO_REVERT)
78
79 BEGIN_TLV_STRUCT_DEFINE(TlvGroupElement, 0x0001)
80 TLV_MEMBER(TlvString, name, 0x4001)
81 TLV_MEMBER(TlvString, id, 0x4002)
82 TLV_MEMBER(TlvUint32, type, 0x4003)
83 TLV_MEMBER(TlvInt32, visibility, 0x4004)
84 TLV_MEMBER(TlvInt32, expireTime, 0x4005)
85 TLV_MEMBER(TlvString, userId, 0x4006)
86 TLV_MEMBER(TlvString, sharedUserId, 0x4007)
87 TLV_MEMBER(TlvBuffer, managers, 0x4008)
88 TLV_MEMBER(TlvBuffer, friends, 0x4009)
89 END_TLV_STRUCT_DEFINE()
90 IMPLEMENT_TLV_VECTOR(TlvGroupVec, TlvGroupElement, 1)
91
92 BEGIN_TLV_STRUCT_DEFINE(TlvDeviceElement, 0x0002)
93 TLV_MEMBER(TlvString, groupId, 0x4101)
94 TLV_MEMBER(TlvString, udid, 0x4102)
95 TLV_MEMBER(TlvString, authId, 0x4103)
96 TLV_MEMBER(TlvString, userId, 0x4107)
97 TLV_MEMBER(TlvString, serviceType, 0x4104)
98 TLV_MEMBER(TlvBuffer, ext, 0x4105)
99 TLV_MEMBER(TlvDevAuthFixedLenInfo, info, 0x4106)
100 END_TLV_STRUCT_DEFINE()
101 IMPLEMENT_TLV_VECTOR(TlvDeviceVec, TlvDeviceElement, 1)
102
103 BEGIN_TLV_STRUCT_DEFINE(HCDataBaseV1, 0x0001)
104 TLV_MEMBER(TlvInt32, version, 0x6001)
105 TLV_MEMBER(TlvGroupVec, groups, 0x6002)
106 TLV_MEMBER(TlvDeviceVec, devices, 0x6003)
107 END_TLV_STRUCT_DEFINE()
108
109 IMPLEMENT_HC_VECTOR(GroupEntryVec, TrustedGroupEntry*, 1)
110 IMPLEMENT_HC_VECTOR(DeviceEntryVec, TrustedDeviceEntry*, 1)
111
112 typedef struct {
113 int32_t osAccountId;
114 GroupEntryVec groups;
115 DeviceEntryVec devices;
116 } OsAccountTrustedInfo;
117
118 DECLARE_HC_VECTOR(DeviceAuthDb, OsAccountTrustedInfo)
119 IMPLEMENT_HC_VECTOR(DeviceAuthDb, OsAccountTrustedInfo, 1)
120
121 #define MAX_DB_PATH_LEN 256
122
123 static HcMutex *g_databaseMutex = NULL;
124 static DeviceAuthDb g_deviceauthDb;
125
EndWithZero(HcParcel * parcel)126 static bool EndWithZero(HcParcel *parcel)
127 {
128 const char *p = GetParcelLastChar(parcel);
129 if (p == NULL) {
130 return false;
131 }
132 return (*p == '\0');
133 }
134
LoadStringVectorFromParcel(StringVector * vec,HcParcel * parcel)135 static bool LoadStringVectorFromParcel(StringVector *vec, HcParcel *parcel)
136 {
137 uint32_t strLen = 0;
138 do {
139 if (!ParcelReadUint32(parcel, &strLen)) {
140 return true;
141 }
142 if ((strLen == 0) || (strLen > MAX_STRING_LEN)) {
143 return false;
144 }
145 HcString str = CreateString();
146 ClearParcel(&str.parcel);
147 if (!ParcelReadParcel(parcel, &str.parcel, strLen, false) ||
148 !EndWithZero(&str.parcel)) {
149 DeleteString(&str);
150 return false;
151 } else {
152 if (vec->pushBack(vec, &str) == NULL) {
153 DeleteString(&str);
154 return false;
155 }
156 }
157 } while (1);
158 }
159
SaveStringVectorToParcel(const StringVector * vec,HcParcel * parcel)160 static bool SaveStringVectorToParcel(const StringVector *vec, HcParcel *parcel)
161 {
162 uint32_t index;
163 HcString *str = NULL;
164 FOR_EACH_HC_VECTOR(*vec, index, str) {
165 uint32_t len = StringLength(str) + sizeof(char);
166 if (!ParcelWriteUint32(parcel, len)) {
167 return false;
168 }
169 if (!ParcelWrite(parcel, GetParcelData(&str->parcel), GetParcelDataSize(&str->parcel))) {
170 return false;
171 }
172 }
173 return true;
174 }
175
GetTrustedInfoByOsAccountId(int32_t osAccountId)176 static OsAccountTrustedInfo *GetTrustedInfoByOsAccountId(int32_t osAccountId)
177 {
178 uint32_t index = 0;
179 OsAccountTrustedInfo *info = NULL;
180 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
181 if ((info != NULL) && (info->osAccountId == osAccountId)) {
182 return info;
183 }
184 }
185 LOGI("[DB]: Create a new os account database cache! [Id]: %d", osAccountId);
186 OsAccountTrustedInfo newInfo;
187 newInfo.osAccountId = osAccountId;
188 newInfo.groups = CreateGroupEntryVec();
189 newInfo.devices = CreateDeviceEntryVec();
190 OsAccountTrustedInfo *returnInfo = g_deviceauthDb.pushBackT(&g_deviceauthDb, newInfo);
191 if (returnInfo == NULL) {
192 LOGE("[DB]: Failed to push osAccountInfo to database!");
193 DestroyGroupEntryVec(&newInfo.groups);
194 DestroyDeviceEntryVec(&newInfo.devices);
195 }
196 return returnInfo;
197 }
198
GetOsAccountInfoPath(int32_t osAccountId,char * infoPath,uint32_t pathBufferLen)199 static bool GetOsAccountInfoPath(int32_t osAccountId, char *infoPath, uint32_t pathBufferLen)
200 {
201 const char *beginPath = GetStorageDirPath();
202 if (beginPath == NULL) {
203 LOGE("[DB]: Failed to get the storage path dir!");
204 return false;
205 }
206 int32_t writeByteNum;
207 if (osAccountId == DEFAULT_OS_ACCOUNT) {
208 writeByteNum = sprintf_s(infoPath, pathBufferLen, "%s/hcgroup.dat", beginPath);
209 } else {
210 writeByteNum = sprintf_s(infoPath, pathBufferLen, "%s/hcgroup%d.dat", beginPath, osAccountId);
211 }
212 if (writeByteNum <= 0) {
213 LOGE("[DB]: sprintf_s fail!");
214 return false;
215 }
216 return true;
217 }
218
GenerateGroupEntryFromEntry(const TrustedGroupEntry * entry,TrustedGroupEntry * returnEntry)219 bool GenerateGroupEntryFromEntry(const TrustedGroupEntry *entry, TrustedGroupEntry *returnEntry)
220 {
221 if (HC_VECTOR_SIZE(&entry->managers) <= 0) {
222 LOGE("[DB]: The group owner is lost!");
223 return false;
224 }
225 HcString entryOwner = HC_VECTOR_GET(&entry->managers, 0);
226 if (!StringSet(&returnEntry->name, entry->name)) {
227 LOGE("[DB]: Failed to copy groupName!");
228 return false;
229 }
230 if (!StringSet(&returnEntry->id, entry->id)) {
231 LOGE("[DB]: Failed to copy groupId!");
232 return false;
233 }
234 if (!StringSet(&returnEntry->userId, entry->userId)) {
235 LOGE("[DB]: Failed to copy userId!");
236 return false;
237 }
238 if (!StringSet(&returnEntry->sharedUserId, entry->sharedUserId)) {
239 LOGE("[DB]: Failed to copy sharedUserId!");
240 return false;
241 }
242 returnEntry->type = entry->type;
243 returnEntry->visibility = entry->visibility;
244 returnEntry->expireTime = entry->expireTime;
245 HcString ownerName = CreateString();
246 if (!StringSet(&ownerName, entryOwner)) {
247 LOGE("[DB]: Failed to copy groupOwner!");
248 DeleteString(&ownerName);
249 return false;
250 }
251 if (returnEntry->managers.pushBack(&returnEntry->managers, &ownerName) == NULL) {
252 LOGE("[DB]: Failed to push groupOwner to managers!");
253 DeleteString(&ownerName);
254 return false;
255 }
256 return true;
257 }
258
GenerateDeviceEntryFromEntry(const TrustedDeviceEntry * entry,TrustedDeviceEntry * returnEntry)259 bool GenerateDeviceEntryFromEntry(const TrustedDeviceEntry *entry, TrustedDeviceEntry *returnEntry)
260 {
261 returnEntry->groupEntry = NULL;
262 if (!StringSet(&returnEntry->groupId, entry->groupId)) {
263 LOGE("[DB]: Failed to copy udid!");
264 return false;
265 }
266 if (!StringSet(&returnEntry->udid, entry->udid)) {
267 LOGE("[DB]: Failed to copy udid!");
268 return false;
269 }
270 if (!StringSet(&returnEntry->authId, entry->authId)) {
271 LOGE("[DB]: Failed to copy authId!");
272 return false;
273 }
274 if (!StringSet(&returnEntry->userId, entry->userId)) {
275 LOGE("[DB]: Failed to copy userId!");
276 return false;
277 }
278 if (!StringSet(&returnEntry->serviceType, entry->serviceType)) {
279 LOGE("[DB]: Failed to copy serviceType!");
280 return false;
281 }
282 returnEntry->credential = entry->credential;
283 returnEntry->devType = entry->devType;
284 returnEntry->source = entry->source;
285 returnEntry->lastTm = entry->lastTm;
286 return true;
287 }
288
GenerateGroupEntryFromTlv(TlvGroupElement * group,TrustedGroupEntry * entry)289 static bool GenerateGroupEntryFromTlv(TlvGroupElement *group, TrustedGroupEntry *entry)
290 {
291 if (!StringSet(&entry->name, group->name.data)) {
292 LOGE("[DB]: Failed to load groupName from tlv!");
293 return false;
294 }
295 if (!StringSet(&entry->id, group->id.data)) {
296 LOGE("[DB]: Failed to load groupId from tlv!");
297 return false;
298 }
299 if (!StringSet(&entry->userId, group->userId.data)) {
300 LOGE("[DB]: Failed to load userId from tlv!");
301 return false;
302 }
303 if (!StringSet(&entry->sharedUserId, group->sharedUserId.data)) {
304 LOGE("[DB]: Failed to load sharedUserId from tlv!");
305 return false;
306 }
307 if (!LoadStringVectorFromParcel(&entry->managers, &group->managers.data)) {
308 LOGE("[DB]: Failed to load managers from tlv!");
309 return false;
310 }
311 if (!LoadStringVectorFromParcel(&entry->friends, &group->friends.data)) {
312 LOGE("[DB]: Failed to load friends from tlv!");
313 return false;
314 }
315 entry->type = group->type.data;
316 entry->visibility = group->visibility.data;
317 entry->expireTime = group->expireTime.data;
318 return true;
319 }
320
GenerateDeviceEntryFromTlv(TlvDeviceElement * device,TrustedDeviceEntry * deviceEntry)321 static bool GenerateDeviceEntryFromTlv(TlvDeviceElement *device, TrustedDeviceEntry *deviceEntry)
322 {
323 deviceEntry->groupEntry = NULL;
324 if (!StringSet(&deviceEntry->groupId, device->groupId.data)) {
325 LOGE("[DB]: Failed to load groupId from tlv!");
326 return false;
327 }
328 if (!StringSet(&deviceEntry->udid, device->udid.data)) {
329 LOGE("[DB]: Failed to load udid from tlv!");
330 return false;
331 }
332 if (!StringSet(&deviceEntry->authId, device->authId.data)) {
333 LOGE("[DB]: Failed to load authId from tlv!");
334 return false;
335 }
336 if (!StringSet(&deviceEntry->userId, device->userId.data)) {
337 LOGE("[DB]: Failed to load userId from tlv!");
338 return false;
339 }
340 if (!StringSet(&deviceEntry->serviceType, device->serviceType.data)) {
341 LOGE("[DB]: Failed to load serviceType from tlv!");
342 return false;
343 }
344 if (!ParcelCopy(&device->ext.data, &deviceEntry->ext)) {
345 LOGE("[DB]: Failed to load external data from tlv!");
346 return false;
347 }
348 deviceEntry->credential = device->info.data.credential;
349 deviceEntry->devType = device->info.data.devType;
350 deviceEntry->source = device->info.data.source;
351 deviceEntry->lastTm = device->info.data.lastTm;
352 return true;
353 }
354
LoadGroups(HCDataBaseV1 * db,GroupEntryVec * vec)355 static bool LoadGroups(HCDataBaseV1 *db, GroupEntryVec *vec)
356 {
357 uint32_t index;
358 TlvGroupElement *group = NULL;
359 FOR_EACH_HC_VECTOR(db->groups.data, index, group) {
360 TrustedGroupEntry *entry = CreateGroupEntry();
361 if (entry == NULL) {
362 LOGE("[DB]: Failed to allocate entry memory!");
363 ClearGroupEntryVec(vec);
364 return false;
365 }
366 if (!GenerateGroupEntryFromTlv(group, entry)) {
367 DestroyGroupEntry(entry);
368 ClearGroupEntryVec(vec);
369 return false;
370 }
371 if (vec->pushBackT(vec, entry) == NULL) {
372 LOGE("[DB]: Failed to push entry to vec!");
373 DestroyGroupEntry(entry);
374 ClearGroupEntryVec(vec);
375 return false;
376 }
377 }
378 return true;
379 }
380
LoadDevices(HCDataBaseV1 * db,DeviceEntryVec * vec)381 static bool LoadDevices(HCDataBaseV1 *db, DeviceEntryVec *vec)
382 {
383 uint32_t index;
384 TlvDeviceElement *device = NULL;
385 FOR_EACH_HC_VECTOR(db->devices.data, index, device) {
386 TrustedDeviceEntry *entry = CreateDeviceEntry();
387 if (entry == NULL) {
388 LOGE("[DB]: Failed to allocate entry memory!");
389 ClearDeviceEntryVec(vec);
390 return false;
391 }
392 if (!GenerateDeviceEntryFromTlv(device, entry)) {
393 DestroyDeviceEntry(entry);
394 ClearDeviceEntryVec(vec);
395 return false;
396 }
397 if (vec->pushBackT(vec, entry) == NULL) {
398 LOGE("[DB]: Failed to push entry to vec!");
399 DestroyDeviceEntry(entry);
400 ClearDeviceEntryVec(vec);
401 return false;
402 }
403 }
404 return true;
405 }
406
ReadInfoFromParcel(HcParcel * parcel,OsAccountTrustedInfo * info)407 static bool ReadInfoFromParcel(HcParcel *parcel, OsAccountTrustedInfo *info)
408 {
409 bool ret = false;
410 HCDataBaseV1 dbv1;
411 TLV_INIT(HCDataBaseV1, &dbv1)
412 if (DecodeTlvMessage((TlvBase *)&dbv1, parcel, false)) {
413 if (!LoadGroups(&dbv1, &info->groups)) {
414 TLV_DEINIT(dbv1)
415 return false;
416 }
417 if (!LoadDevices(&dbv1, &info->devices)) {
418 ClearGroupEntryVec(&info->groups);
419 TLV_DEINIT(dbv1)
420 return false;
421 }
422 ret = true;
423 } else {
424 LOGE("[DB]: Decode Tlv Message Failed!");
425 }
426 TLV_DEINIT(dbv1)
427 return ret;
428 }
429
ReadParcelFromFile(int32_t osAccountId,HcParcel * parcel)430 static bool ReadParcelFromFile(int32_t osAccountId, HcParcel *parcel)
431 {
432 char infoPath[MAX_DB_PATH_LEN] = { 0 };
433 if (!GetOsAccountInfoPath(osAccountId, infoPath, MAX_DB_PATH_LEN)) {
434 return false;
435 }
436 FileHandle file;
437 int ret = HcFileOpen(infoPath, MODE_FILE_READ, &file);
438 if (ret != 0) {
439 LOGE("[DB]: Failed to open database file!");
440 return false;
441 }
442 int fileSize = HcFileSize(file);
443 if (fileSize <= 0) {
444 LOGE("[DB]: The database file size is invalid!");
445 HcFileClose(file);
446 return false;
447 }
448 char *fileData = (char *)HcMalloc(fileSize, 0);
449 if (fileData == NULL) {
450 LOGE("[DB]: Failed to allocate fileData memory!");
451 HcFileClose(file);
452 return false;
453 }
454 if (HcFileRead(file, fileData, fileSize) != fileSize) {
455 LOGE("[DB]: Read file error!");
456 HcFileClose(file);
457 HcFree(fileData);
458 return false;
459 }
460 HcFileClose(file);
461 if (!ParcelWrite(parcel, fileData, fileSize)) {
462 LOGE("[DB]: parcel write error!");
463 HcFree(fileData);
464 return false;
465 }
466 HcFree(fileData);
467 return true;
468 }
469
LoadOsAccountDb(int32_t osAccountId)470 static void LoadOsAccountDb(int32_t osAccountId)
471 {
472 HcParcel parcel = CreateParcel(0, 0);
473 if (!ReadParcelFromFile(osAccountId, &parcel)) {
474 DeleteParcel(&parcel);
475 return;
476 }
477 OsAccountTrustedInfo info;
478 info.osAccountId = osAccountId;
479 info.groups = CreateGroupEntryVec();
480 info.devices = CreateDeviceEntryVec();
481 if (!ReadInfoFromParcel(&parcel, &info)) {
482 DestroyGroupEntryVec(&info.groups);
483 DestroyDeviceEntryVec(&info.devices);
484 DeleteParcel(&parcel);
485 return;
486 }
487 DeleteParcel(&parcel);
488 if (g_deviceauthDb.pushBackT(&g_deviceauthDb, info) == NULL) {
489 LOGE("[DB]: Failed to push osAccountInfo to database!");
490 ClearGroupEntryVec(&info.groups);
491 ClearDeviceEntryVec(&info.devices);
492 }
493 LOGI("[DB]: Load os account db successfully! [Id]: %d", osAccountId);
494 }
495
LoadDeviceAuthDb(void)496 static void LoadDeviceAuthDb(void)
497 {
498 StringVector osAccountDbNameVec = CreateStrVector();
499 HcFileGetSubFileName(GetStorageDirPath(), &osAccountDbNameVec);
500 uint32_t index;
501 HcString *dbName;
502 FOR_EACH_HC_VECTOR(osAccountDbNameVec, index, dbName) {
503 int32_t osAccountId;
504 const char *osAccountIdStr = StringGet(dbName);
505 if (osAccountIdStr == NULL) {
506 continue;
507 }
508 if (strcmp(osAccountIdStr, "hcgroup.dat") == 0) {
509 LoadOsAccountDb(DEFAULT_OS_ACCOUNT);
510 } else if (sscanf_s(osAccountIdStr, "hcgroup%d.dat", &osAccountId) == 1) {
511 LoadOsAccountDb(osAccountId);
512 }
513 }
514 DestroyStrVector(&osAccountDbNameVec);
515 }
516
SetGroupElement(TlvGroupElement * element,TrustedGroupEntry * entry)517 static bool SetGroupElement(TlvGroupElement *element, TrustedGroupEntry *entry)
518 {
519 if (!StringSet(&element->name.data, entry->name)) {
520 LOGE("[DB]: Failed to copy groupName!");
521 return false;
522 }
523 if (!StringSet(&element->id.data, entry->id)) {
524 LOGE("[DB]: Failed to copy groupId!");
525 return false;
526 }
527 if (!StringSet(&element->userId.data, entry->userId)) {
528 LOGE("[DB]: Failed to copy userId!");
529 return false;
530 }
531 if (!StringSet(&element->sharedUserId.data, entry->sharedUserId)) {
532 LOGE("[DB]: Failed to copy sharedUserId!");
533 return false;
534 }
535 element->type.data = entry->type;
536 element->visibility.data = entry->visibility;
537 element->expireTime.data = entry->expireTime;
538 if (!SaveStringVectorToParcel(&entry->managers, &element->managers.data)) {
539 LOGE("[DB]: Failed to copy managers!");
540 return false;
541 }
542 if (!SaveStringVectorToParcel(&entry->friends, &element->friends.data)) {
543 LOGE("[DB]: Failed to copy friends!");
544 return false;
545 }
546 return true;
547 }
548
SetDeviceElement(TlvDeviceElement * element,TrustedDeviceEntry * entry)549 static bool SetDeviceElement(TlvDeviceElement *element, TrustedDeviceEntry *entry)
550 {
551 if (!StringSet(&element->groupId.data, entry->groupId)) {
552 LOGE("[DB]: Failed to copy groupId!");
553 return false;
554 }
555 if (!StringSet(&element->udid.data, entry->udid)) {
556 LOGE("[DB]: Failed to copy udid!");
557 return false;
558 }
559 if (!StringSet(&element->authId.data, entry->authId)) {
560 LOGE("[DB]: Failed to copy authId!");
561 return false;
562 }
563 if (!StringSet(&element->userId.data, entry->userId)) {
564 LOGE("[DB]: Failed to copy userId!");
565 return false;
566 }
567 if (!StringSet(&element->serviceType.data, entry->serviceType)) {
568 LOGE("[DB]: Failed to copy serviceType!");
569 return false;
570 }
571 if (!ParcelCopy(&element->ext.data, &entry->ext)) {
572 LOGE("[DB]: Failed to copy external data!");
573 return false;
574 }
575 element->info.data.credential = entry->credential;
576 element->info.data.devType = entry->devType;
577 element->info.data.source = entry->source;
578 element->info.data.lastTm = entry->lastTm;
579 return true;
580 }
581
SaveGroups(const GroupEntryVec * vec,HCDataBaseV1 * db)582 static bool SaveGroups(const GroupEntryVec *vec, HCDataBaseV1 *db)
583 {
584 uint32_t index;
585 TrustedGroupEntry **entry;
586 FOR_EACH_HC_VECTOR(*vec, index, entry) {
587 TlvGroupElement tmp;
588 TlvGroupElement *element = db->groups.data.pushBack(&db->groups.data, &tmp);
589 if (element == NULL) {
590 return false;
591 }
592 TLV_INIT(TlvGroupElement, element);
593 if (!SetGroupElement(element, *entry)) {
594 TLV_DEINIT((*element));
595 return false;
596 }
597 }
598 return true;
599 }
600
SaveDevices(const DeviceEntryVec * vec,HCDataBaseV1 * db)601 static bool SaveDevices(const DeviceEntryVec *vec, HCDataBaseV1 *db)
602 {
603 uint32_t index;
604 TrustedDeviceEntry **entry;
605 FOR_EACH_HC_VECTOR(*vec, index, entry) {
606 TlvDeviceElement tmp;
607 TlvDeviceElement *element = db->devices.data.pushBack(&db->devices.data, &tmp);
608 if (element == NULL) {
609 return false;
610 }
611 TLV_INIT(TlvDeviceElement, element);
612 if (!SetDeviceElement(element, *entry)) {
613 TLV_DEINIT((*element));
614 return false;
615 }
616 }
617 return true;
618 }
619
SaveInfoToParcel(const OsAccountTrustedInfo * info,HcParcel * parcel)620 static bool SaveInfoToParcel(const OsAccountTrustedInfo *info, HcParcel *parcel)
621 {
622 int32_t ret = false;
623 HCDataBaseV1 dbv1;
624 TLV_INIT(HCDataBaseV1, &dbv1)
625 dbv1.version.data = 1;
626 do {
627 if (!SaveGroups(&info->groups, &dbv1)) {
628 break;
629 }
630 if (!SaveDevices(&info->devices, &dbv1)) {
631 break;
632 }
633 if (!EncodeTlvMessage((TlvBase *)&dbv1, parcel)) {
634 LOGE("[DB]: Encode Tlv Message failed!");
635 break;
636 }
637 ret = true;
638 } while (0);
639 TLV_DEINIT(dbv1)
640 return ret;
641 }
642
SaveParcelToFile(const OsAccountTrustedInfo * info,HcParcel * parcel)643 static bool SaveParcelToFile(const OsAccountTrustedInfo *info, HcParcel *parcel)
644 {
645 char infoPath[MAX_DB_PATH_LEN] = { 0 };
646 if (!GetOsAccountInfoPath(info->osAccountId, infoPath, MAX_DB_PATH_LEN)) {
647 return false;
648 }
649 FileHandle file;
650 int ret = HcFileOpen(infoPath, MODE_FILE_WRITE, &file);
651 if (ret != HC_SUCCESS) {
652 LOGE("[DB]: Failed to open database file!");
653 return false;
654 }
655 int fileSize = (int)GetParcelDataSize(parcel);
656 const char *fileData = GetParcelData(parcel);
657 int writeSize = HcFileWrite(file, fileData, fileSize);
658 HcFileClose(file);
659 if (writeSize == fileSize) {
660 return true;
661 } else {
662 LOGE("[DB]: write file error!");
663 return false;
664 }
665 }
666
CompareQueryGroupParams(const QueryGroupParams * params,const TrustedGroupEntry * entry)667 static bool CompareQueryGroupParams(const QueryGroupParams *params, const TrustedGroupEntry *entry)
668 {
669 if ((params->groupId != NULL) && (strcmp(params->groupId, StringGet(&entry->id)) != 0)) {
670 return false;
671 }
672 if ((params->groupName != NULL) && (strcmp(params->groupName, StringGet(&entry->name)) != 0)) {
673 return false;
674 }
675 if ((params->userId != NULL) && (strcmp(params->userId, StringGet(&entry->userId)) != 0)) {
676 return false;
677 }
678 if ((params->sharedUserId != NULL) && (strcmp(params->sharedUserId, StringGet(&entry->sharedUserId)) != 0)) {
679 return false;
680 }
681 if ((params->groupType != ALL_GROUP) && (params->groupType != entry->type)) {
682 return false;
683 }
684 if ((params->groupVisibility != ALL_GROUP_VISIBILITY) && (params->groupVisibility != entry->visibility)) {
685 return false;
686 }
687 if (params->ownerName != NULL) {
688 HcString entryOwner = HC_VECTOR_GET(&entry->managers, 0);
689 if (strcmp(params->ownerName, StringGet(&entryOwner)) != 0) {
690 return false;
691 }
692 }
693 return true;
694 }
695
CompareQueryDeviceParams(const QueryDeviceParams * params,const TrustedDeviceEntry * entry)696 static bool CompareQueryDeviceParams(const QueryDeviceParams *params, const TrustedDeviceEntry *entry)
697 {
698 if ((params->groupId != NULL) && (strcmp(params->groupId, StringGet(&entry->groupId)) != 0)) {
699 return false;
700 }
701 if ((params->udid != NULL) && (strcmp(params->udid, StringGet(&entry->udid)) != 0)) {
702 return false;
703 }
704 if ((params->authId != NULL) && (strcmp(params->authId, StringGet(&entry->authId)) != 0)) {
705 return false;
706 }
707 if ((params->userId != NULL) && (strcmp(params->userId, StringGet(&entry->userId)) != 0)) {
708 return false;
709 }
710 return true;
711 }
712
QueryGroupEntryPtrIfMatch(const GroupEntryVec * vec,const QueryGroupParams * params)713 static TrustedGroupEntry **QueryGroupEntryPtrIfMatch(const GroupEntryVec *vec, const QueryGroupParams *params)
714 {
715 uint32_t index;
716 TrustedGroupEntry **entry;
717 FOR_EACH_HC_VECTOR(*vec, index, entry) {
718 if ((entry != NULL) && (*entry != NULL) && (CompareQueryGroupParams(params, *entry))) {
719 return entry;
720 }
721 }
722 return NULL;
723 }
724
QueryDeviceEntryPtrIfMatch(const DeviceEntryVec * vec,const QueryDeviceParams * params)725 static TrustedDeviceEntry **QueryDeviceEntryPtrIfMatch(const DeviceEntryVec *vec, const QueryDeviceParams *params)
726 {
727 uint32_t index;
728 TrustedDeviceEntry **entry;
729 FOR_EACH_HC_VECTOR(*vec, index, entry) {
730 if ((entry != NULL) && (*entry != NULL) && (CompareQueryDeviceParams(params, *entry))) {
731 return entry;
732 }
733 }
734 return NULL;
735 }
736
PostGroupCreatedMsg(const TrustedGroupEntry * groupEntry)737 static void PostGroupCreatedMsg(const TrustedGroupEntry *groupEntry)
738 {
739 if (!IsBroadcastSupported()) {
740 return;
741 }
742 GetBroadcaster()->postOnGroupCreated(groupEntry);
743 }
744
PostGroupDeletedMsg(const TrustedGroupEntry * groupEntry)745 static void PostGroupDeletedMsg(const TrustedGroupEntry *groupEntry)
746 {
747 if (!IsBroadcastSupported()) {
748 return;
749 }
750 GetBroadcaster()->postOnGroupDeleted(groupEntry);
751 }
752
PostDeviceBoundMsg(OsAccountTrustedInfo * info,const TrustedDeviceEntry * deviceEntry)753 static void PostDeviceBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry)
754 {
755 if (!IsBroadcastSupported()) {
756 return;
757 }
758 QueryGroupParams groupParams = InitQueryGroupParams();
759 groupParams.groupId = StringGet(&deviceEntry->groupId);
760 TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams);
761 if (groupEntryPtr != NULL) {
762 GetBroadcaster()->postOnDeviceBound(StringGet(&deviceEntry->udid), *groupEntryPtr);
763 }
764 }
765
PostDeviceUnBoundMsg(OsAccountTrustedInfo * info,const TrustedDeviceEntry * deviceEntry)766 static void PostDeviceUnBoundMsg(OsAccountTrustedInfo *info, const TrustedDeviceEntry *deviceEntry)
767 {
768 if (!IsBroadcastSupported()) {
769 return;
770 }
771 const char *groupId = StringGet(&deviceEntry->groupId);
772 const char *udid = StringGet(&deviceEntry->udid);
773 QueryGroupParams groupParams = InitQueryGroupParams();
774 groupParams.groupId = groupId;
775 TrustedGroupEntry **groupEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, &groupParams);
776 if (groupEntryPtr != NULL) {
777 GetBroadcaster()->postOnDeviceUnBound(udid, *groupEntryPtr);
778 }
779 QueryDeviceParams deviceParams = InitQueryDeviceParams();
780 deviceParams.udid = udid;
781 if (QueryDeviceEntryPtrIfMatch(&info->devices, &deviceParams) == NULL) {
782 GetBroadcaster()->postOnDeviceNotTrusted(udid);
783 }
784 }
785
InitQueryGroupParams(void)786 QueryGroupParams InitQueryGroupParams(void)
787 {
788 QueryGroupParams params = {
789 .groupId = NULL,
790 .groupName = NULL,
791 .ownerName = NULL,
792 .userId = NULL,
793 .groupType = ALL_GROUP,
794 .groupVisibility = ALL_GROUP_VISIBILITY
795 };
796 return params;
797 }
798
InitQueryDeviceParams(void)799 QueryDeviceParams InitQueryDeviceParams(void)
800 {
801 QueryDeviceParams params = {
802 .groupId = NULL,
803 .udid = NULL,
804 .authId = NULL,
805 .userId = NULL
806 };
807 return params;
808 }
809
CreateGroupEntry(void)810 TrustedGroupEntry *CreateGroupEntry(void)
811 {
812 TrustedGroupEntry *ptr = (TrustedGroupEntry *)HcMalloc(sizeof(TrustedGroupEntry), 0);
813 if (ptr == NULL) {
814 LOGE("[DB]: Failed to allocate groupEntry memory!");
815 return NULL;
816 }
817 ptr->name = CreateString();
818 ptr->id = CreateString();
819 ptr->userId = CreateString();
820 ptr->sharedUserId = CreateString();
821 ptr->managers = CreateStrVector();
822 ptr->friends = CreateStrVector();
823 return ptr;
824 }
825
DestroyGroupEntry(TrustedGroupEntry * groupEntry)826 void DestroyGroupEntry(TrustedGroupEntry *groupEntry)
827 {
828 DeleteString(&groupEntry->name);
829 DeleteString(&groupEntry->id);
830 DeleteString(&groupEntry->userId);
831 DeleteString(&groupEntry->sharedUserId);
832 DestroyStrVector(&groupEntry->managers);
833 DestroyStrVector(&groupEntry->friends);
834 HcFree(groupEntry);
835 }
836
DeepCopyGroupEntry(const TrustedGroupEntry * entry)837 TrustedGroupEntry *DeepCopyGroupEntry(const TrustedGroupEntry *entry)
838 {
839 TrustedGroupEntry *returnEntry = CreateGroupEntry();
840 if (returnEntry == NULL) {
841 return NULL;
842 }
843 if (!GenerateGroupEntryFromEntry(entry, returnEntry)) {
844 DestroyGroupEntry(returnEntry);
845 return NULL;
846 }
847 return returnEntry;
848 }
849
CreateDeviceEntry(void)850 TrustedDeviceEntry *CreateDeviceEntry(void)
851 {
852 TrustedDeviceEntry *ptr = (TrustedDeviceEntry *)HcMalloc(sizeof(TrustedDeviceEntry), 0);
853 if (ptr == NULL) {
854 LOGE("[DB]: Failed to allocate deviceEntry memory!");
855 return NULL;
856 }
857 ptr->groupId = CreateString();
858 ptr->udid = CreateString();
859 ptr->authId = CreateString();
860 ptr->userId = CreateString();
861 ptr->serviceType = CreateString();
862 ptr->ext = CreateParcel(0, 0);
863 return ptr;
864 }
865
DestroyDeviceEntry(TrustedDeviceEntry * deviceEntry)866 void DestroyDeviceEntry(TrustedDeviceEntry *deviceEntry)
867 {
868 DeleteString(&deviceEntry->groupId);
869 DeleteString(&deviceEntry->udid);
870 DeleteString(&deviceEntry->authId);
871 DeleteString(&deviceEntry->userId);
872 DeleteString(&deviceEntry->serviceType);
873 DeleteParcel(&deviceEntry->ext);
874 HcFree(deviceEntry);
875 }
876
DeepCopyDeviceEntry(const TrustedDeviceEntry * entry)877 TrustedDeviceEntry *DeepCopyDeviceEntry(const TrustedDeviceEntry *entry)
878 {
879 TrustedDeviceEntry *returnEntry = CreateDeviceEntry();
880 if (returnEntry == NULL) {
881 return NULL;
882 }
883 if (!GenerateDeviceEntryFromEntry(entry, returnEntry)) {
884 DestroyDeviceEntry(returnEntry);
885 return NULL;
886 }
887 return returnEntry;
888 }
889
ClearGroupEntryVec(GroupEntryVec * vec)890 void ClearGroupEntryVec(GroupEntryVec *vec)
891 {
892 uint32_t index;
893 TrustedGroupEntry **entry;
894 FOR_EACH_HC_VECTOR(*vec, index, entry) {
895 DestroyGroupEntry(*entry);
896 }
897 DESTROY_HC_VECTOR(GroupEntryVec, vec);
898 }
899
ClearDeviceEntryVec(DeviceEntryVec * vec)900 void ClearDeviceEntryVec(DeviceEntryVec *vec)
901 {
902 uint32_t index;
903 TrustedDeviceEntry **entry;
904 FOR_EACH_HC_VECTOR(*vec, index, entry) {
905 DestroyDeviceEntry(*entry);
906 }
907 DESTROY_HC_VECTOR(DeviceEntryVec, vec);
908 }
909
AddGroup(int32_t osAccountId,const TrustedGroupEntry * groupEntry)910 int32_t AddGroup(int32_t osAccountId, const TrustedGroupEntry *groupEntry)
911 {
912 LOGI("[DB]: Start to add a group to database!");
913 if (groupEntry == NULL) {
914 LOGE("[DB]: The input groupEntry is NULL!");
915 return HC_ERR_NULL_PTR;
916 }
917 g_databaseMutex->lock(g_databaseMutex);
918 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
919 if (info == NULL) {
920 g_databaseMutex->unlock(g_databaseMutex);
921 return HC_ERR_INVALID_PARAMS;
922 }
923 TrustedGroupEntry *newEntry = DeepCopyGroupEntry(groupEntry);
924 if (newEntry == NULL) {
925 g_databaseMutex->unlock(g_databaseMutex);
926 return HC_ERR_MEMORY_COPY;
927 }
928 QueryGroupParams params = InitQueryGroupParams();
929 params.groupId = StringGet(&groupEntry->id);
930 TrustedGroupEntry **oldEntryPtr = QueryGroupEntryPtrIfMatch(&info->groups, ¶ms);
931 if (oldEntryPtr != NULL) {
932 DestroyGroupEntry(*oldEntryPtr);
933 *oldEntryPtr = newEntry;
934 PostGroupCreatedMsg(newEntry);
935 g_databaseMutex->unlock(g_databaseMutex);
936 LOGI("[DB]: Replace an old group successfully! [GroupType]: %d", groupEntry->type);
937 return HC_SUCCESS;
938 }
939 if (info->groups.pushBackT(&info->groups, newEntry) == NULL) {
940 DestroyGroupEntry(newEntry);
941 g_databaseMutex->unlock(g_databaseMutex);
942 LOGE("[DB]: Failed to push groupEntry to vec!");
943 return HC_ERR_MEMORY_COPY;
944 }
945 PostGroupCreatedMsg(newEntry);
946 g_databaseMutex->unlock(g_databaseMutex);
947 LOGI("[DB]: Add a group to database successfully! [GroupType]: %d", groupEntry->type);
948 return HC_SUCCESS;
949 }
950
AddTrustedDevice(int32_t osAccountId,const TrustedDeviceEntry * deviceEntry)951 int32_t AddTrustedDevice(int32_t osAccountId, const TrustedDeviceEntry *deviceEntry)
952 {
953 LOGI("[DB]: Start to add a trusted device to database!");
954 if (deviceEntry == NULL) {
955 LOGE("[DB]: The input deviceEntry is NULL!");
956 return HC_ERR_NULL_PTR;
957 }
958 g_databaseMutex->lock(g_databaseMutex);
959 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
960 if (info == NULL) {
961 g_databaseMutex->unlock(g_databaseMutex);
962 return HC_ERR_INVALID_PARAMS;
963 }
964 TrustedDeviceEntry *newEntry = DeepCopyDeviceEntry(deviceEntry);
965 if (newEntry == NULL) {
966 g_databaseMutex->unlock(g_databaseMutex);
967 return HC_ERR_MEMORY_COPY;
968 }
969 QueryDeviceParams params = InitQueryDeviceParams();
970 params.udid = StringGet(&deviceEntry->udid);
971 params.groupId = StringGet(&deviceEntry->groupId);
972 TrustedDeviceEntry **oldEntryPtr = QueryDeviceEntryPtrIfMatch(&info->devices, ¶ms);
973 if (oldEntryPtr != NULL) {
974 DestroyDeviceEntry(*oldEntryPtr);
975 *oldEntryPtr = newEntry;
976 PostDeviceBoundMsg(info, newEntry);
977 g_databaseMutex->unlock(g_databaseMutex);
978 LOGI("[DB]: Replace an old trusted device successfully!");
979 return HC_SUCCESS;
980 }
981 if (info->devices.pushBackT(&info->devices, newEntry) == NULL) {
982 DestroyDeviceEntry(newEntry);
983 g_databaseMutex->unlock(g_databaseMutex);
984 LOGE("[DB]: Failed to push deviceEntry to vec!");
985 return HC_ERR_MEMORY_COPY;
986 }
987 PostDeviceBoundMsg(info, newEntry);
988 g_databaseMutex->unlock(g_databaseMutex);
989 LOGI("[DB]: Add a trusted device to database successfully!");
990 return HC_SUCCESS;
991 }
992
DelGroup(int32_t osAccountId,const QueryGroupParams * params)993 int32_t DelGroup(int32_t osAccountId, const QueryGroupParams *params)
994 {
995 LOGI("[DB]: Start to delete groups from database!");
996 if (params == NULL) {
997 LOGE("[DB]: The input params is NULL!");
998 return HC_ERR_NULL_PTR;
999 }
1000 g_databaseMutex->lock(g_databaseMutex);
1001 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1002 if (info == NULL) {
1003 g_databaseMutex->unlock(g_databaseMutex);
1004 return HC_ERR_INVALID_PARAMS;
1005 }
1006 int32_t count = 0;
1007 uint32_t index = 0;
1008 TrustedGroupEntry **entry = NULL;
1009 while (index < HC_VECTOR_SIZE(&info->groups)) {
1010 entry = info->groups.getp(&info->groups, index);
1011 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryGroupParams(params, *entry))) {
1012 index++;
1013 continue;
1014 }
1015 TrustedGroupEntry *popEntry;
1016 HC_VECTOR_POPELEMENT(&info->groups, &popEntry, index);
1017 PostGroupDeletedMsg(popEntry);
1018 LOGI("[DB]: Delete a group from database successfully! [GroupType]: %d", popEntry->type);
1019 DestroyGroupEntry(popEntry);
1020 count++;
1021 }
1022 g_databaseMutex->unlock(g_databaseMutex);
1023 LOGI("[DB]: Number of groups deleted: %d", count);
1024 return HC_SUCCESS;
1025 }
1026
DelTrustedDevice(int32_t osAccountId,const QueryDeviceParams * params)1027 int32_t DelTrustedDevice(int32_t osAccountId, const QueryDeviceParams *params)
1028 {
1029 LOGI("[DB]: Start to delete devices from database!");
1030 if (params == NULL) {
1031 LOGE("[DB]: The input params is NULL!");
1032 return HC_ERR_NULL_PTR;
1033 }
1034 g_databaseMutex->lock(g_databaseMutex);
1035 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1036 if (info == NULL) {
1037 g_databaseMutex->unlock(g_databaseMutex);
1038 return HC_ERR_INVALID_PARAMS;
1039 }
1040 int32_t count = 0;
1041 uint32_t index = 0;
1042 TrustedDeviceEntry **entry = NULL;
1043 while (index < HC_VECTOR_SIZE(&info->devices)) {
1044 entry = info->devices.getp(&info->devices, index);
1045 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryDeviceParams(params, *entry))) {
1046 index++;
1047 continue;
1048 }
1049 TrustedDeviceEntry *popEntry;
1050 HC_VECTOR_POPELEMENT(&info->devices, &popEntry, index);
1051 PostDeviceUnBoundMsg(info, popEntry);
1052 LOGI("[DB]: Delete a trusted device from database successfully!");
1053 DestroyDeviceEntry(popEntry);
1054 count++;
1055 }
1056 g_databaseMutex->unlock(g_databaseMutex);
1057 LOGI("[DB]: Number of trusted devices deleted: %d", count);
1058 return HC_SUCCESS;
1059 }
1060
QueryGroups(int32_t osAccountId,const QueryGroupParams * params,GroupEntryVec * vec)1061 int32_t QueryGroups(int32_t osAccountId, const QueryGroupParams *params, GroupEntryVec *vec)
1062 {
1063 if ((params == NULL) || (vec == NULL)) {
1064 LOGE("[DB]: The input params or vec is NULL!");
1065 return HC_ERR_NULL_PTR;
1066 }
1067 g_databaseMutex->lock(g_databaseMutex);
1068 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1069 if (info == NULL) {
1070 g_databaseMutex->unlock(g_databaseMutex);
1071 return HC_ERR_INVALID_PARAMS;
1072 }
1073 uint32_t index;
1074 TrustedGroupEntry **entry;
1075 FOR_EACH_HC_VECTOR(info->groups, index, entry) {
1076 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryGroupParams(params, *entry))) {
1077 continue;
1078 }
1079 TrustedGroupEntry *newEntry = DeepCopyGroupEntry(*entry);
1080 if (newEntry == NULL) {
1081 continue;
1082 }
1083 if (vec->pushBackT(vec, newEntry) == NULL) {
1084 LOGE("[DB]: Failed to push entry to vec!");
1085 DestroyGroupEntry(newEntry);
1086 }
1087 }
1088 g_databaseMutex->unlock(g_databaseMutex);
1089 LOGI("[DB]: Number of groups queried: %d", vec->size(vec));
1090 return HC_SUCCESS;
1091 }
1092
QueryDevices(int32_t osAccountId,const QueryDeviceParams * params,DeviceEntryVec * vec)1093 int32_t QueryDevices(int32_t osAccountId, const QueryDeviceParams *params, DeviceEntryVec *vec)
1094 {
1095 if ((params == NULL) || (vec == NULL)) {
1096 LOGE("[DB]: The input params or vec is NULL!");
1097 return HC_ERR_NULL_PTR;
1098 }
1099 g_databaseMutex->lock(g_databaseMutex);
1100 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1101 if (info == NULL) {
1102 g_databaseMutex->unlock(g_databaseMutex);
1103 return HC_ERR_INVALID_PARAMS;
1104 }
1105 uint32_t index;
1106 TrustedDeviceEntry **entry;
1107 FOR_EACH_HC_VECTOR(info->devices, index, entry) {
1108 if ((entry == NULL) || (*entry == NULL) || (!CompareQueryDeviceParams(params, *entry))) {
1109 continue;
1110 }
1111 TrustedDeviceEntry *newEntry = DeepCopyDeviceEntry(*entry);
1112 if (newEntry == NULL) {
1113 continue;
1114 }
1115 if (vec->pushBackT(vec, newEntry) == NULL) {
1116 LOGE("[DB]: Failed to push entry to vec!");
1117 DestroyDeviceEntry(newEntry);
1118 }
1119 }
1120 g_databaseMutex->unlock(g_databaseMutex);
1121 LOGI("[DB]: Number of trusted devices queried: %d", vec->size(vec));
1122 return HC_SUCCESS;
1123 }
1124
SaveOsAccountDb(int32_t osAccountId)1125 int32_t SaveOsAccountDb(int32_t osAccountId)
1126 {
1127 g_databaseMutex->lock(g_databaseMutex);
1128 OsAccountTrustedInfo *info = GetTrustedInfoByOsAccountId(osAccountId);
1129 if (info == NULL) {
1130 g_databaseMutex->unlock(g_databaseMutex);
1131 return HC_ERR_INVALID_PARAMS;
1132 }
1133 HcParcel parcel = CreateParcel(0, 0);
1134 if (!SaveInfoToParcel(info, &parcel)) {
1135 DeleteParcel(&parcel);
1136 g_databaseMutex->unlock(g_databaseMutex);
1137 return HC_ERR_MEMORY_COPY;
1138 }
1139 if (!SaveParcelToFile(info, &parcel)) {
1140 DeleteParcel(&parcel);
1141 g_databaseMutex->unlock(g_databaseMutex);
1142 return HC_ERR_MEMORY_COPY;
1143 }
1144 DeleteParcel(&parcel);
1145 g_databaseMutex->unlock(g_databaseMutex);
1146 LOGI("[DB]: Save an os account database successfully! [Id]: %d", osAccountId);
1147 return HC_SUCCESS;
1148 }
1149
1150 #ifndef DISABLE_HIVIEW
DumpGroups(int fd,GroupEntryVec * vec)1151 static void DumpGroups(int fd, GroupEntryVec *vec)
1152 {
1153 dprintf(fd, "---- GROUP INFO ----\n");
1154 (void)vec;
1155 }
1156
DumpDevices(int fd,DeviceEntryVec * vec)1157 static void DumpDevices(int fd, DeviceEntryVec *vec)
1158 {
1159 dprintf(fd, "---- DEVICE INFO ----\n");
1160 (void)vec;
1161 }
1162
DevAuthDataBaseDump(int fd)1163 static void DevAuthDataBaseDump(int fd)
1164 {
1165 if (g_databaseMutex == NULL) {
1166 LOGE("[DB]: Init mutex failed");
1167 return;
1168 }
1169 g_databaseMutex->lock(g_databaseMutex);
1170
1171 uint32_t index;
1172 OsAccountTrustedInfo *info;
1173 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
1174 DumpGroups(fd, &info->groups);
1175 DumpDevices(fd, &info->devices);
1176 }
1177
1178 g_databaseMutex->unlock(g_databaseMutex);
1179 }
1180 #endif
InitDatabase(void)1181 int32_t InitDatabase(void)
1182 {
1183 if (g_databaseMutex == NULL) {
1184 g_databaseMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
1185 if (g_databaseMutex == NULL) {
1186 LOGE("[DB]: Alloc databaseMutex failed");
1187 return HC_ERR_ALLOC_MEMORY;
1188 }
1189 if (InitHcMutex(g_databaseMutex) != HC_SUCCESS) {
1190 LOGE("[DB]: Init mutex failed");
1191 HcFree(g_databaseMutex);
1192 g_databaseMutex = NULL;
1193 return HC_ERROR;
1194 }
1195 }
1196 g_deviceauthDb = CREATE_HC_VECTOR(DeviceAuthDb);
1197 LoadDeviceAuthDb();
1198 #ifndef DISABLE_HIVIEW
1199 RegisterDumpFunc(DevAuthDataBaseDump);
1200 #endif
1201 return HC_SUCCESS;
1202 }
1203
DestroyDatabase(void)1204 void DestroyDatabase(void)
1205 {
1206 g_databaseMutex->lock(g_databaseMutex);
1207 uint32_t index;
1208 OsAccountTrustedInfo *info;
1209 FOR_EACH_HC_VECTOR(g_deviceauthDb, index, info) {
1210 ClearGroupEntryVec(&info->groups);
1211 ClearDeviceEntryVec(&info->devices);
1212 }
1213 DESTROY_HC_VECTOR(DeviceAuthDb, &g_deviceauthDb);
1214 g_databaseMutex->unlock(g_databaseMutex);
1215 if (g_databaseMutex != NULL) {
1216 DestroyHcMutex(g_databaseMutex);
1217 HcFree(g_databaseMutex);
1218 g_databaseMutex = NULL;
1219 }
1220 }