1 /*
2 * Copyright (C) 2021-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 #include "group_operation.h"
17
18 #include "alg_defs.h"
19 #include "broadcast_manager.h"
20 #include "callback_manager.h"
21 #include "common_defs.h"
22 #include "cred_manager.h"
23 #include "data_manager.h"
24 #include "dev_auth_module_manager.h"
25 #include "device_auth_defines.h"
26 #include "group_manager_common.h"
27 #include "group_operation_common.h"
28 #include "hc_dev_info.h"
29 #include "hc_log.h"
30 #include "hisysevent_adapter.h"
31 #include "hitrace_adapter.h"
32 #include "os_account_adapter.h"
33 #include "task_manager.h"
34
35 #include "across_account_group.h"
36 #include "identical_account_group.h"
37 #include "peer_to_peer_group.h"
38
IsGroupTypeSupported(int groupType)39 static bool IsGroupTypeSupported(int groupType)
40 {
41 if (((groupType == PEER_TO_PEER_GROUP) && (IsPeerToPeerGroupSupported())) ||
42 ((groupType == IDENTICAL_ACCOUNT_GROUP) && (IsIdenticalAccountGroupSupported())) ||
43 ((groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) && (IsAcrossAccountGroupSupported()))) {
44 return true;
45 }
46 LOGE("The group type is not supported! [GroupType]: %d", groupType);
47 return false;
48 }
49
RemoveNoPermissionGroup(int32_t osAccountId,GroupEntryVec * groupEntryVec,const char * appId)50 static void RemoveNoPermissionGroup(int32_t osAccountId, GroupEntryVec *groupEntryVec, const char *appId)
51 {
52 uint32_t index = 0;
53 TrustedGroupEntry **groupEntryPtr = NULL;
54 while (index < groupEntryVec->size(groupEntryVec)) {
55 groupEntryPtr = groupEntryVec->getp(groupEntryVec, index);
56 if (CheckGroupAccessible(osAccountId, StringGet(&(*groupEntryPtr)->id), appId) == HC_SUCCESS) {
57 index++;
58 continue;
59 }
60 LOGI("Remove a group without permission!");
61 TrustedGroupEntry *tempEntry = NULL;
62 HC_VECTOR_POPELEMENT(groupEntryVec, &tempEntry, index);
63 DestroyGroupEntry(tempEntry);
64 }
65 }
66
GenerateReturnEmptyArrayStr(char ** returnVec)67 static int32_t GenerateReturnEmptyArrayStr(char **returnVec)
68 {
69 CJson *json = CreateJsonArray();
70 if (json == NULL) {
71 LOGE("Failed to allocate json memory!");
72 return HC_ERR_JSON_FAIL;
73 }
74 *returnVec = PackJsonToString(json);
75 FreeJson(json);
76 if (*returnVec == NULL) {
77 LOGE("Failed to convert json to string!");
78 return HC_ERR_JSON_FAIL;
79 }
80 return HC_SUCCESS;
81 }
82
GenerateReturnGroupVec(GroupEntryVec * groupInfoVec,char ** returnGroupVec,uint32_t * groupNum)83 static int32_t GenerateReturnGroupVec(GroupEntryVec *groupInfoVec, char **returnGroupVec, uint32_t *groupNum)
84 {
85 if (HC_VECTOR_SIZE(groupInfoVec) == 0) {
86 LOGI("No group is found based on the query parameters!");
87 *groupNum = 0;
88 return GenerateReturnEmptyArrayStr(returnGroupVec);
89 }
90
91 CJson *json = CreateJsonArray();
92 if (json == NULL) {
93 LOGE("Failed to allocate json memory!");
94 return HC_ERR_JSON_FAIL;
95 }
96 uint32_t groupCount = 0;
97 uint32_t index;
98 TrustedGroupEntry **groupInfoPtr = NULL;
99 FOR_EACH_HC_VECTOR(*groupInfoVec, index, groupInfoPtr) {
100 TrustedGroupEntry *groupInfo = *groupInfoPtr;
101 CJson *groupInfoJson = CreateJson();
102 if (groupInfoJson == NULL) {
103 LOGE("Failed to allocate groupInfoJson memory!");
104 FreeJson(json);
105 return HC_ERR_ALLOC_MEMORY;
106 }
107 int32_t result = GenerateReturnGroupInfo(groupInfo, groupInfoJson);
108 if (result != HC_SUCCESS) {
109 FreeJson(groupInfoJson);
110 FreeJson(json);
111 return result;
112 }
113 if (AddObjToArray(json, groupInfoJson) != HC_SUCCESS) {
114 LOGE("Failed to add groupInfoStr to returnGroupVec!");
115 FreeJson(groupInfoJson);
116 FreeJson(json);
117 return HC_ERR_JSON_FAIL;
118 }
119 ++groupCount;
120 }
121 *returnGroupVec = PackJsonToString(json);
122 FreeJson(json);
123 if ((*returnGroupVec) == NULL) {
124 LOGE("Failed to convert json to string!");
125 return HC_ERR_JSON_FAIL;
126 }
127 *groupNum = groupCount;
128 return HC_SUCCESS;
129 }
130
GenerateReturnDeviceVec(DeviceEntryVec * devInfoVec,char ** returnDevInfoVec,uint32_t * deviceNum)131 static int32_t GenerateReturnDeviceVec(DeviceEntryVec *devInfoVec, char **returnDevInfoVec, uint32_t *deviceNum)
132 {
133 CJson *json = CreateJsonArray();
134 if (json == NULL) {
135 LOGE("Failed to allocate json memory!");
136 return HC_ERR_JSON_FAIL;
137 }
138 uint32_t devCount = 0;
139 uint32_t index;
140 TrustedDeviceEntry **devInfoPtr = NULL;
141 FOR_EACH_HC_VECTOR(*devInfoVec, index, devInfoPtr) {
142 TrustedDeviceEntry *devInfo = (TrustedDeviceEntry*)(*devInfoPtr);
143 CJson *devInfoJson = CreateJson();
144 if (devInfoJson == NULL) {
145 LOGE("Failed to allocate devInfoJson memory!");
146 FreeJson(json);
147 return HC_ERR_ALLOC_MEMORY;
148 }
149 int32_t result = GenerateReturnDevInfo(devInfo, devInfoJson);
150 if (result != HC_SUCCESS) {
151 FreeJson(devInfoJson);
152 FreeJson(json);
153 return result;
154 }
155 if (AddObjToArray(json, devInfoJson) != HC_SUCCESS) {
156 LOGE("Failed to add devInfoStr to returnGroupVec!");
157 FreeJson(devInfoJson);
158 FreeJson(json);
159 return HC_ERR_JSON_FAIL;
160 }
161 ++devCount;
162 }
163 *returnDevInfoVec = PackJsonToString(json);
164 FreeJson(json);
165 if ((*returnDevInfoVec) == NULL) {
166 LOGE("Failed to convert json to string!");
167 return HC_ERR_JSON_FAIL;
168 }
169 *deviceNum = devCount;
170 return HC_SUCCESS;
171 }
172
IsQueryParamsValid(int groupType,const char * groupId,const char * groupName,const char * groupOwner)173 static bool IsQueryParamsValid(int groupType, const char *groupId, const char *groupName, const char *groupOwner)
174 {
175 if ((groupType == ALL_GROUP) && (groupId == NULL) && (groupName == NULL) && (groupOwner == NULL)) {
176 return false;
177 } else {
178 return true;
179 }
180 }
181
QueryRelatedGroupsForGetPk(int32_t osAccountId,const char * udid,GroupEntryVec * returnGroupEntryVec)182 static int32_t QueryRelatedGroupsForGetPk(int32_t osAccountId, const char *udid, GroupEntryVec *returnGroupEntryVec)
183 {
184 DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
185 QueryDeviceParams params = InitQueryDeviceParams();
186 params.udid = udid;
187 int32_t result = QueryDevices(osAccountId, ¶ms, &deviceEntryVec);
188 if (result != HC_SUCCESS) {
189 LOGE("Failed to query trusted devices!");
190 ClearDeviceEntryVec(&deviceEntryVec);
191 return result;
192 }
193 uint32_t index;
194 TrustedDeviceEntry **entry = NULL;
195 FOR_EACH_HC_VECTOR(deviceEntryVec, index, entry) {
196 /* In order to improve availability, even if there is an error, it does not terminate. */
197 TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, StringGet(&(*entry)->groupId));
198 if (groupEntry == NULL) {
199 LOGW("An exception occurred! Device found, but group not found. There may be dirty data.");
200 continue;
201 }
202 if (groupEntry->visibility != GROUP_VISIBILITY_PUBLIC) {
203 DestroyGroupEntry(groupEntry);
204 continue;
205 }
206 if (returnGroupEntryVec->pushBackT(returnGroupEntryVec, groupEntry) == NULL) {
207 LOGW("An exception occurred! Failed to push groupEntry to returnGroupEntryVec!");
208 DestroyGroupEntry(groupEntry);
209 }
210 }
211 ClearDeviceEntryVec(&deviceEntryVec);
212 return HC_SUCCESS;
213 }
214
GetPkByParams(const char * groupId,const TrustedDeviceEntry * deviceEntry,char * returnPkHexStr,int32_t returnPkHexStrLen)215 static int32_t GetPkByParams(const char *groupId, const TrustedDeviceEntry *deviceEntry,
216 char *returnPkHexStr, int32_t returnPkHexStrLen)
217 {
218 /* Use the DeviceGroupManager package name. */
219 const char *appId = GROUP_MANAGER_PACKAGE_NAME;
220 int userType = deviceEntry->devType;
221 const char *authId = StringGet(&deviceEntry->authId);
222 Uint8Buff authIdBuff = { 0, 0 };
223 authIdBuff.length = HcStrlen(authId);
224 authIdBuff.val = (uint8_t *)HcMalloc(authIdBuff.length, 0);
225 if (authIdBuff.val == NULL) {
226 LOGE("Failed to allocate authIdBuff memory!");
227 return HC_ERR_ALLOC_MEMORY;
228 }
229 if (memcpy_s(authIdBuff.val, authIdBuff.length, authId, authIdBuff.length) != HC_SUCCESS) {
230 LOGE("Failed to copy authId!");
231 HcFree(authIdBuff.val);
232 return HC_ERR_MEMORY_COPY;
233 }
234 uint8_t returnPkBytes[PUBLIC_KEY_MAX_LENGTH] = { 0 };
235 Uint8Buff returnPkBuff = { 0, 0 };
236 returnPkBuff.length = PUBLIC_KEY_MAX_LENGTH;
237 returnPkBuff.val = returnPkBytes;
238 AuthModuleParams authParams = {
239 .pkgName = appId,
240 .serviceType = groupId,
241 .authId = &authIdBuff,
242 .userType = userType
243 };
244 int32_t res = GetPublicKey(DAS_MODULE, &authParams, &returnPkBuff);
245 HcFree(authIdBuff.val);
246 if (res != HC_SUCCESS) {
247 return res;
248 }
249 res = GetHashResult(returnPkBuff.val, returnPkBuff.length, returnPkHexStr, returnPkHexStrLen);
250 if (res != HC_SUCCESS) {
251 LOGE("Failed to get hash for pk!");
252 return HC_ERR_HASH_FAIL;
253 }
254 return HC_SUCCESS;
255 }
256
GeneratePkInfo(int32_t osAccountId,const char * queryUdid,const char * groupId,CJson * pkInfo)257 static int32_t GeneratePkInfo(int32_t osAccountId, const char *queryUdid, const char *groupId, CJson *pkInfo)
258 {
259 TrustedDeviceEntry *deviceEntry = GetTrustedDeviceEntryById(osAccountId, queryUdid, true, groupId);
260 if (deviceEntry == NULL) {
261 LOGE("The trusted device is not found!");
262 return HC_ERR_DEVICE_NOT_EXIST;
263 }
264 char returnPkHexStr[SHA256_LEN * BYTE_TO_HEX_OPER_LENGTH + 1] = { 0 };
265 int32_t result = GetPkByParams(groupId, deviceEntry, returnPkHexStr, sizeof(returnPkHexStr));
266 DestroyDeviceEntry(deviceEntry);
267 if (result != HC_SUCCESS) {
268 return result;
269 }
270 if (AddStringToJson(pkInfo, FIELD_GROUP_ID, groupId) != HC_SUCCESS) {
271 LOGE("Failed to add groupId to pkInfo!");
272 return HC_ERR_JSON_ADD;
273 }
274 if (AddStringToJson(pkInfo, FIELD_PUBLIC_KEY, returnPkHexStr) != HC_SUCCESS) {
275 LOGE("Failed to add publicKey to pkInfo!");
276 return HC_ERR_JSON_ADD;
277 }
278 return HC_SUCCESS;
279 }
280
AddAllPkInfoToList(int32_t osAccountId,const char * queryUdid,const GroupEntryVec * groupEntryVec,CJson * pkInfoList)281 static void AddAllPkInfoToList(int32_t osAccountId, const char *queryUdid, const GroupEntryVec *groupEntryVec,
282 CJson *pkInfoList)
283 {
284 uint32_t index;
285 TrustedGroupEntry **entry = NULL;
286 FOR_EACH_HC_VECTOR(*groupEntryVec, index, entry) {
287 /* Account related group cannot export public key. */
288 if (IsAccountRelatedGroup((*entry)->type)) {
289 continue;
290 }
291 const char *groupId = StringGet(&((*entry)->id));
292 CJson *pkInfo = CreateJson();
293 if (pkInfo == NULL) {
294 LOGE("Failed to create json!");
295 continue;
296 }
297 int32_t res = GeneratePkInfo(osAccountId, queryUdid, groupId, pkInfo);
298 if (res != HC_SUCCESS) {
299 FreeJson(pkInfo);
300 continue;
301 }
302 if (AddObjToArray(pkInfoList, pkInfo) != HC_SUCCESS) {
303 LOGE("Failed to add pkInfo to pkInfoList!");
304 FreeJson(pkInfo);
305 }
306 }
307 }
308
IsOnlyAccountRelatedGroups(const GroupEntryVec * groupEntryVec)309 static bool IsOnlyAccountRelatedGroups(const GroupEntryVec *groupEntryVec)
310 {
311 if (groupEntryVec->size(groupEntryVec) == 0) {
312 LOGW("No groups available.");
313 return false;
314 }
315 uint32_t index;
316 TrustedGroupEntry **entry = NULL;
317 FOR_EACH_HC_VECTOR(*groupEntryVec, index, entry) {
318 if (!IsAccountRelatedGroup((*entry)->type)) {
319 return false;
320 }
321 }
322 return true;
323 }
324
GeneratePkInfoList(int32_t osAccountId,const CJson * params,CJson * pkInfoList)325 static int32_t GeneratePkInfoList(int32_t osAccountId, const CJson *params, CJson *pkInfoList)
326 {
327 const char *udid = GetStringFromJson(params, FIELD_UDID);
328 if (udid == NULL) {
329 LOGE("Failed to get udid from params!");
330 return HC_ERR_JSON_GET;
331 }
332 bool isSelfPk = false;
333 if (GetBoolFromJson(params, FIELD_IS_SELF_PK, &isSelfPk) != HC_SUCCESS) {
334 LOGE("Failed to get isSelfPk from json!");
335 return HC_ERR_JSON_GET;
336 }
337 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
338 int32_t res = QueryRelatedGroupsForGetPk(osAccountId, udid, &groupEntryVec);
339 if (res != HC_SUCCESS) {
340 ClearGroupEntryVec(&groupEntryVec);
341 return res;
342 }
343 /**
344 * Specification requirements:
345 * when there are only account related groups in the group list of public key information to be queried,
346 * a special error code needs to be returned.
347 */
348 if (IsOnlyAccountRelatedGroups(&groupEntryVec)) {
349 LOGE("There are only account related groups in the group list.");
350 ClearGroupEntryVec(&groupEntryVec);
351 return HC_ERR_ONLY_ACCOUNT_RELATED;
352 }
353 const char *queryUdid = NULL;
354 char selfUdid[INPUT_UDID_LEN] = { 0 };
355 if (isSelfPk) {
356 res = HcGetUdid((uint8_t *)selfUdid, INPUT_UDID_LEN);
357 if (res != HC_SUCCESS) {
358 LOGE("Failed to get local udid! res: %d", res);
359 ClearGroupEntryVec(&groupEntryVec);
360 return HC_ERR_DB;
361 }
362 queryUdid = selfUdid;
363 } else {
364 queryUdid = udid;
365 }
366 AddAllPkInfoToList(osAccountId, queryUdid, &groupEntryVec, pkInfoList);
367 ClearGroupEntryVec(&groupEntryVec);
368 return HC_SUCCESS;
369 }
370
GetGroupInstance(int32_t groupType)371 static BaseGroup *GetGroupInstance(int32_t groupType)
372 {
373 if (!IsGroupTypeSupported(groupType)) {
374 return NULL;
375 }
376 BaseGroup *instance = NULL;
377 if (groupType == PEER_TO_PEER_GROUP) {
378 instance = GetPeerToPeerGroupInstance();
379 } else if (groupType == IDENTICAL_ACCOUNT_GROUP) {
380 instance = GetIdenticalAccountGroupInstance();
381 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
382 instance = GetAcrossAccountGroupInstance();
383 }
384 return instance;
385 }
386
CreateGroup(int32_t osAccountId,CJson * jsonParams,char ** returnJsonStr)387 static int32_t CreateGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
388 {
389 int32_t groupType = PEER_TO_PEER_GROUP;
390 if (GetIntFromJson(jsonParams, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
391 LOGE("Failed to get groupType from jsonParams!");
392 return HC_ERR_JSON_GET;
393 }
394 BaseGroup *instance = GetGroupInstance(groupType);
395 if (instance == NULL) {
396 LOGE("The group instance is NULL or its function ptr is NULL!");
397 return HC_ERR_NULL_PTR;
398 }
399 return instance->createGroup(osAccountId, jsonParams, returnJsonStr);
400 }
401
DeleteGroup(int32_t osAccountId,CJson * jsonParams,char ** returnJsonStr)402 static int32_t DeleteGroup(int32_t osAccountId, CJson *jsonParams, char **returnJsonStr)
403 {
404 int32_t result;
405 const char *groupId = NULL;
406 const char *appId = NULL;
407 int32_t groupType = PEER_TO_PEER_GROUP;
408 if (((result = GetGroupIdFromJson(jsonParams, &groupId)) != HC_SUCCESS) ||
409 ((result = GetAppIdFromJson(jsonParams, &appId)) != HC_SUCCESS) ||
410 ((result = CheckGroupExist(osAccountId, groupId)) != HC_SUCCESS) ||
411 ((result = GetGroupTypeFromDb(osAccountId, groupId, &groupType)) != HC_SUCCESS) ||
412 ((result = CheckPermForGroup(osAccountId, GROUP_DISBAND, appId, groupId)) != HC_SUCCESS)) {
413 return result;
414 }
415 BaseGroup *instance = GetGroupInstance(groupType);
416 return instance->deleteGroup(osAccountId, jsonParams, returnJsonStr);
417 }
418
DeleteMemberFromPeerToPeerGroup(int32_t osAccountId,int64_t requestId,CJson * jsonParams,const DeviceAuthCallback * callback)419 static int32_t DeleteMemberFromPeerToPeerGroup(int32_t osAccountId, int64_t requestId, CJson *jsonParams,
420 const DeviceAuthCallback *callback)
421 {
422 if (!IsPeerToPeerGroupSupported()) {
423 LOGE("Peer to peer group is not supported!");
424 return HC_ERR_NOT_SUPPORT;
425 }
426 PeerToPeerGroup *instance = (PeerToPeerGroup *)GetPeerToPeerGroupInstance();
427 return instance->deleteMember(osAccountId, requestId, jsonParams, callback);
428 }
429
DoCreateGroup(HcTaskBase * baseTask)430 static void DoCreateGroup(HcTaskBase *baseTask)
431 {
432 GroupManagerTask *task = (GroupManagerTask *)baseTask;
433 SET_LOG_MODE(TRACE_MODE);
434 SET_TRACE_ID(task->reqId);
435 LOGI("[Start]: DoCreateGroup! [ReqId]: %" PRId64, task->reqId);
436 char *returnJsonStr = NULL;
437 int32_t result = CreateGroup(task->osAccountId, task->params, &returnJsonStr);
438 if (result != HC_SUCCESS) {
439 ProcessErrorCallback(task->reqId, GROUP_CREATE, result, NULL, task->cb);
440 } else {
441 ProcessFinishCallback(task->reqId, GROUP_CREATE, returnJsonStr, task->cb);
442 FreeJsonString(returnJsonStr);
443 }
444 }
445
DoDeleteGroup(HcTaskBase * baseTask)446 static void DoDeleteGroup(HcTaskBase *baseTask)
447 {
448 GroupManagerTask *task = (GroupManagerTask *)baseTask;
449 SET_LOG_MODE(TRACE_MODE);
450 SET_TRACE_ID(task->reqId);
451 LOGI("[Start]: DoDeleteGroup! [ReqId]: %" PRId64, task->reqId);
452 char *returnJsonStr = NULL;
453 int32_t result = DeleteGroup(task->osAccountId, task->params, &returnJsonStr);
454 if (result != HC_SUCCESS) {
455 ProcessErrorCallback(task->reqId, GROUP_DISBAND, result, NULL, task->cb);
456 } else {
457 ProcessFinishCallback(task->reqId, GROUP_DISBAND, returnJsonStr, task->cb);
458 FreeJsonString(returnJsonStr);
459 }
460 }
461
DoDeleteMember(HcTaskBase * baseTask)462 static void DoDeleteMember(HcTaskBase *baseTask)
463 {
464 GroupManagerTask *task = (GroupManagerTask *)baseTask;
465 SET_LOG_MODE(TRACE_MODE);
466 SET_TRACE_ID(task->reqId);
467 LOGI("[Start]: DoDeleteMember! [ReqId]: %" PRId64, task->reqId);
468 (void)DeleteMemberFromPeerToPeerGroup(task->osAccountId, task->reqId, task->params, task->cb);
469 }
470
RequestCreateGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * createParams)471 static int32_t RequestCreateGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *createParams)
472 {
473 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
474 if ((appId == NULL) || (createParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
475 LOGE("Invalid input parameters!");
476 return HC_ERR_INVALID_PARAMS;
477 }
478 LOGI("[Start]: RequestCreateGroup! [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
479 DEV_AUTH_REPORT_CALL_EVENT(CREATE_GROUP_EVENT, osAccountId, requestId, appId);
480 CJson *params = CreateJsonFromString(createParams);
481 if (params == NULL) {
482 LOGE("Failed to create json from string!");
483 return HC_ERR_JSON_FAIL;
484 }
485 int32_t result = AddBindParamsToJson(GROUP_CREATE, requestId, appId, params);
486 if (result != HC_SUCCESS) {
487 FreeJson(params);
488 return result;
489 }
490 if (InitAndPushGMTask(osAccountId, GROUP_CREATE, requestId, params, DoCreateGroup) != HC_SUCCESS) {
491 FreeJson(params);
492 return HC_ERR_INIT_TASK_FAIL;
493 }
494 LOGI("[End]: RequestCreateGroup!");
495 return HC_SUCCESS;
496 }
497
RequestDeleteGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * disbandParams)498 static int32_t RequestDeleteGroup(int32_t osAccountId, int64_t requestId, const char *appId, const char *disbandParams)
499 {
500 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
501 if ((appId == NULL) || (disbandParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
502 LOGE("Invalid input parameters!");
503 return HC_ERR_INVALID_PARAMS;
504 }
505 LOGI("[Start]: RequestDeleteGroup! [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
506 DEV_AUTH_REPORT_CALL_EVENT(DELETE_GROUP_EVENT, osAccountId, requestId, appId);
507 CJson *params = CreateJsonFromString(disbandParams);
508 if (params == NULL) {
509 LOGE("Failed to create json from string!");
510 return HC_ERR_JSON_FAIL;
511 }
512 int32_t result = AddBindParamsToJson(GROUP_DISBAND, requestId, appId, params);
513 if (result != HC_SUCCESS) {
514 FreeJson(params);
515 return result;
516 }
517 if (InitAndPushGMTask(osAccountId, GROUP_DISBAND, requestId, params, DoDeleteGroup) != HC_SUCCESS) {
518 FreeJson(params);
519 return HC_ERR_INIT_TASK_FAIL;
520 }
521 LOGI("[End]: RequestDeleteGroup!");
522 return HC_SUCCESS;
523 }
524
RequestDeleteMemberFromGroup(int32_t osAccountId,int64_t requestId,const char * appId,const char * deleteParams)525 static int32_t RequestDeleteMemberFromGroup(int32_t osAccountId, int64_t requestId, const char *appId,
526 const char *deleteParams)
527 {
528 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
529 if ((appId == NULL) || (deleteParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
530 LOGE("Invalid input parameters!");
531 return HC_ERR_INVALID_PARAMS;
532 }
533 LOGI("[Start]: RequestDeleteMemberFromGroup! [AppId]: %s, [ReqId]: %" PRId64, appId, requestId);
534 DEV_AUTH_REPORT_CALL_EVENT(DEL_MEMBER_EVENT, osAccountId, requestId, appId);
535 CJson *params = CreateJsonFromString(deleteParams);
536 if (params == NULL) {
537 LOGE("Failed to create json from string!");
538 return HC_ERR_JSON_FAIL;
539 }
540 int32_t result = AddBindParamsToJson(MEMBER_DELETE, requestId, appId, params);
541 if (result != HC_SUCCESS) {
542 FreeJson(params);
543 return result;
544 }
545 if (InitAndPushGMTask(osAccountId, MEMBER_DELETE, requestId, params, DoDeleteMember) != HC_SUCCESS) {
546 FreeJson(params);
547 return HC_ERR_INIT_TASK_FAIL;
548 }
549 LOGI("[End]: RequestDeleteMemberFromGroup!");
550 return HC_SUCCESS;
551 }
552
RequestAddMultiMembersToGroup(int32_t osAccountId,const char * appId,const char * addParams)553 static int32_t RequestAddMultiMembersToGroup(int32_t osAccountId, const char *appId, const char *addParams)
554 {
555 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
556 if ((appId == NULL) || (addParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
557 LOGE("Invalid input parameters!");
558 return HC_ERR_INVALID_PARAMS;
559 }
560 LOGI("[Start]: RequestAddMultiMembersToGroup! [AppId]: %s", appId);
561 DEV_AUTH_REPORT_CALL_EVENT(ADD_MULTI_MEMBER_EVENT, osAccountId, DEFAULT_REQUEST_ID, appId);
562 CJson *params = CreateJsonFromString(addParams);
563 if (params == NULL) {
564 LOGE("Failed to create json from string!");
565 return HC_ERR_JSON_CREATE;
566 }
567 int32_t groupType = GROUP_TYPE_INVALID;
568 if (GetIntFromJson(params, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
569 LOGE("Failed to get groupType from json!");
570 FreeJson(params);
571 return HC_ERR_JSON_GET;
572 }
573 if (!IsGroupTypeSupported(groupType)) {
574 FreeJson(params);
575 return HC_ERR_NOT_SUPPORT;
576 }
577 int32_t res;
578 if (groupType == IDENTICAL_ACCOUNT_GROUP) {
579 IdenticalAccountGroup *instance = (IdenticalAccountGroup *)GetIdenticalAccountGroupInstance();
580 res = instance->addMultiMembersToGroup(osAccountId, appId, params);
581 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
582 AcrossAccountGroup *instance = (AcrossAccountGroup *)GetAcrossAccountGroupInstance();
583 res = instance->addMultiMembersToGroup(osAccountId, appId, params);
584 } else {
585 LOGE("The input groupType is invalid! [GroupType]: %d", groupType);
586 res = HC_ERR_INVALID_PARAMS;
587 }
588 FreeJson(params);
589 LOGI("[End]: RequestAddMultiMembersToGroup!");
590 return res;
591 }
592
RequestDelMultiMembersFromGroup(int32_t osAccountId,const char * appId,const char * deleteParams)593 static int32_t RequestDelMultiMembersFromGroup(int32_t osAccountId, const char *appId, const char *deleteParams)
594 {
595 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
596 if ((appId == NULL) || (deleteParams == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
597 LOGE("Invalid input parameters!");
598 return HC_ERR_INVALID_PARAMS;
599 }
600 LOGI("[Start]: RequestDelMultiMembersFromGroup! [AppId]: %s", appId);
601 DEV_AUTH_REPORT_CALL_EVENT(DEL_MULTI_MEMBER_EVENT, osAccountId, DEFAULT_REQUEST_ID, appId);
602 CJson *params = CreateJsonFromString(deleteParams);
603 if (params == NULL) {
604 LOGE("Failed to create json from string!");
605 return HC_ERR_JSON_CREATE;
606 }
607 int32_t groupType = GROUP_TYPE_INVALID;
608 if (GetIntFromJson(params, FIELD_GROUP_TYPE, &groupType) != HC_SUCCESS) {
609 LOGE("Failed to get groupType from json!");
610 FreeJson(params);
611 return HC_ERR_JSON_GET;
612 }
613 if (!IsGroupTypeSupported(groupType)) {
614 FreeJson(params);
615 return HC_ERR_NOT_SUPPORT;
616 }
617 int32_t res;
618 if (groupType == IDENTICAL_ACCOUNT_GROUP) {
619 IdenticalAccountGroup *instance = (IdenticalAccountGroup *)GetIdenticalAccountGroupInstance();
620 res = instance->delMultiMembersFromGroup(osAccountId, appId, params);
621 } else if (groupType == ACROSS_ACCOUNT_AUTHORIZE_GROUP) {
622 AcrossAccountGroup *instance = (AcrossAccountGroup *)GetAcrossAccountGroupInstance();
623 res = instance->delMultiMembersFromGroup(osAccountId, appId, params);
624 } else {
625 LOGE("The input groupType is invalid! [GroupType]: %d", groupType);
626 res = HC_ERR_INVALID_PARAMS;
627 }
628 FreeJson(params);
629 LOGI("[End]: RequestDelMultiMembersFromGroup!");
630 return res;
631 }
632
RegListener(const char * appId,const DataChangeListener * listener)633 static int32_t RegListener(const char *appId, const DataChangeListener *listener)
634 {
635 if ((appId == NULL) || (listener == NULL)) {
636 LOGE("The input parameter contains NULL value!");
637 return HC_ERR_INVALID_PARAMS;
638 }
639 if (!IsBroadcastSupported()) {
640 LOGE("Broadcast is not supported!");
641 return HC_ERR_NOT_SUPPORT;
642 }
643 return AddListener(appId, listener);
644 }
645
UnRegListener(const char * appId)646 static int32_t UnRegListener(const char *appId)
647 {
648 if (appId == NULL) {
649 LOGE("The input appId is NULL!");
650 return HC_ERR_INVALID_PARAMS;
651 }
652 if (!IsBroadcastSupported()) {
653 LOGE("Broadcast is not supported!");
654 return HC_ERR_NOT_SUPPORT;
655 }
656 return RemoveListener(appId);
657 }
658
GetRegisterInfo(const char * reqJsonStr,char ** returnRegisterInfo)659 static int32_t GetRegisterInfo(const char *reqJsonStr, char **returnRegisterInfo)
660 {
661 if ((reqJsonStr == NULL) || (returnRegisterInfo == NULL)) {
662 LOGE("The input param is NULL!");
663 return HC_ERR_INVALID_PARAMS;
664 }
665 CJson *requestJson = CreateJsonFromString(reqJsonStr);
666 if (requestJson == NULL) {
667 LOGE("Failed to create request json!");
668 return HC_ERR_JSON_CREATE;
669 }
670 if (AddIntToJson(requestJson, FIELD_CREDENTIAL_TYPE, ASYMMETRIC_CRED) != HC_SUCCESS) {
671 LOGE("Failed to add credentialType to input json!");
672 FreeJson(requestJson);
673 return HC_ERR_JSON_GET;
674 }
675 CJson *registerInfo = CreateJson();
676 if (registerInfo == NULL) {
677 LOGE("Failed to allocate registerInfo memory!");
678 FreeJson(requestJson);
679 return HC_ERR_JSON_CREATE;
680 }
681 int32_t result = ProcCred(ACCOUNT_RELATED_PLUGIN, 0, REQUEST_SIGNATURE, requestJson, registerInfo);
682 FreeJson(requestJson);
683 if (result != HC_SUCCESS) {
684 LOGE("Failed to get register info!");
685 FreeJson(registerInfo);
686 return result;
687 }
688 *returnRegisterInfo = PackJsonToString(registerInfo);
689 FreeJson(registerInfo);
690 if (*returnRegisterInfo == NULL) {
691 LOGE("Failed to convert json to string!");
692 return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
693 }
694 return HC_SUCCESS;
695 }
696
CheckAccessToGroup(int32_t osAccountId,const char * appId,const char * groupId)697 static int32_t CheckAccessToGroup(int32_t osAccountId, const char *appId, const char *groupId)
698 {
699 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
700 if ((appId == NULL) || (groupId == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
701 LOGE("Invalid input parameters!");
702 return HC_ERR_INVALID_PARAMS;
703 }
704 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
705 LOGE("You do not have the permission to query the group information!");
706 return HC_ERR_ACCESS_DENIED;
707 }
708 return HC_SUCCESS;
709 }
710
GetAccessibleGroupInfoById(int32_t osAccountId,const char * appId,const char * groupId,char ** returnGroupInfo)711 static int32_t GetAccessibleGroupInfoById(int32_t osAccountId, const char *appId, const char *groupId,
712 char **returnGroupInfo)
713 {
714 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
715 if ((appId == NULL) || (groupId == NULL) || (returnGroupInfo == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
716 LOGE("Invalid input parameters!");
717 return HC_ERR_INVALID_PARAMS;
718 }
719 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
720 LOGE("No group is found based on the query parameters!");
721 return HC_ERR_GROUP_NOT_EXIST;
722 }
723 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
724 LOGE("You do not have the permission to query the group information!");
725 return HC_ERR_ACCESS_DENIED;
726 }
727 TrustedGroupEntry *groupEntry = GetGroupEntryById(osAccountId, groupId);
728 if (groupEntry == NULL) {
729 LOGE("Failed to get groupEntry from db!");
730 return HC_ERR_DB;
731 }
732 CJson *groupInfoJson = CreateJson();
733 if (groupInfoJson == NULL) {
734 LOGE("Failed to allocate groupInfoJson memory!");
735 DestroyGroupEntry(groupEntry);
736 return HC_ERR_JSON_FAIL;
737 }
738 int32_t result = GenerateReturnGroupInfo(groupEntry, groupInfoJson);
739 DestroyGroupEntry(groupEntry);
740 if (result != HC_SUCCESS) {
741 FreeJson(groupInfoJson);
742 return result;
743 }
744 *returnGroupInfo = PackJsonToString(groupInfoJson);
745 FreeJson(groupInfoJson);
746 if (*returnGroupInfo == NULL) {
747 LOGE("Failed to convert json to string!");
748 return HC_ERR_JSON_FAIL;
749 }
750 return HC_SUCCESS;
751 }
752
GetAccessibleGroupInfo(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnGroupVec,uint32_t * groupNum)753 static int32_t GetAccessibleGroupInfo(int32_t osAccountId, const char *appId, const char *queryParams,
754 char **returnGroupVec, uint32_t *groupNum)
755 {
756 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
757 if ((appId == NULL) || (queryParams == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) ||
758 (osAccountId == INVALID_OS_ACCOUNT)) {
759 LOGE("Invalid input parameters!");
760 return HC_ERR_INVALID_PARAMS;
761 }
762 CJson *queryParamsJson = CreateJsonFromString(queryParams);
763 if (queryParamsJson == NULL) {
764 LOGE("Failed to create queryParamsJson from string!");
765 return HC_ERR_JSON_FAIL;
766 }
767 int32_t groupType = ALL_GROUP;
768 (void)GetIntFromJson(queryParamsJson, FIELD_GROUP_TYPE, &groupType);
769 if ((groupType != ALL_GROUP) && (!IsGroupTypeSupported(groupType))) {
770 LOGE("Invalid group type!");
771 FreeJson(queryParamsJson);
772 return HC_ERR_INVALID_PARAMS;
773 }
774 const char *groupId = GetStringFromJson(queryParamsJson, FIELD_GROUP_ID);
775 const char *groupName = GetStringFromJson(queryParamsJson, FIELD_GROUP_NAME);
776 const char *groupOwner = GetStringFromJson(queryParamsJson, FIELD_GROUP_OWNER);
777 if (!IsQueryParamsValid(groupType, groupId, groupName, groupOwner)) {
778 LOGE("The query parameters cannot be all null!");
779 FreeJson(queryParamsJson);
780 return HC_ERR_INVALID_PARAMS;
781 }
782 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
783 int32_t result = GetGroupInfo(osAccountId, groupType, groupId, groupName, groupOwner, &groupEntryVec);
784 FreeJson(queryParamsJson);
785 if (result != HC_SUCCESS) {
786 ClearGroupEntryVec(&groupEntryVec);
787 return result;
788 }
789 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
790 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
791 ClearGroupEntryVec(&groupEntryVec);
792 return result;
793 }
794
GetAccessibleJoinedGroups(int32_t osAccountId,const char * appId,int groupType,char ** returnGroupVec,uint32_t * groupNum)795 static int32_t GetAccessibleJoinedGroups(int32_t osAccountId, const char *appId, int groupType,
796 char **returnGroupVec, uint32_t *groupNum)
797 {
798 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
799 if ((appId == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
800 LOGE("Invalid input parameters!");
801 return HC_ERR_INVALID_PARAMS;
802 }
803 if (!IsGroupTypeSupported(groupType)) {
804 LOGE("Invalid group type!");
805 return HC_ERR_INVALID_PARAMS;
806 }
807 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
808 int32_t result = GetJoinedGroups(osAccountId, groupType, &groupEntryVec);
809 if (result != HC_SUCCESS) {
810 ClearGroupEntryVec(&groupEntryVec);
811 return result;
812 }
813 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
814 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
815 ClearGroupEntryVec(&groupEntryVec);
816 return result;
817 }
818
GetAccessibleRelatedGroups(int32_t osAccountId,const char * appId,const char * peerDeviceId,bool isUdid,char ** returnGroupVec,uint32_t * groupNum)819 static int32_t GetAccessibleRelatedGroups(int32_t osAccountId, const char *appId, const char *peerDeviceId, bool isUdid,
820 char **returnGroupVec, uint32_t *groupNum)
821 {
822 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
823 if ((appId == NULL) || (peerDeviceId == NULL) || (returnGroupVec == NULL) || (groupNum == NULL) ||
824 (osAccountId == INVALID_OS_ACCOUNT)) {
825 LOGE("Invalid input parameters!");
826 return HC_ERR_INVALID_PARAMS;
827 }
828 LOGI("Start to get related groups! [AppId]: %s", appId);
829 GroupEntryVec groupEntryVec = CreateGroupEntryVec();
830 int32_t result = GetRelatedGroups(osAccountId, peerDeviceId, isUdid, &groupEntryVec);
831 if (result != HC_SUCCESS) {
832 ClearGroupEntryVec(&groupEntryVec);
833 return result;
834 }
835 RemoveNoPermissionGroup(osAccountId, &groupEntryVec, appId);
836 result = GenerateReturnGroupVec(&groupEntryVec, returnGroupVec, groupNum);
837 ClearGroupEntryVec(&groupEntryVec);
838 return result;
839 }
840
GetAccessibleDeviceInfoById(int32_t osAccountId,const char * appId,const char * deviceId,bool isUdid,const char * groupId,char ** returnDeviceInfo)841 static int32_t GetAccessibleDeviceInfoById(int32_t osAccountId, const char *appId, const char *deviceId, bool isUdid,
842 const char *groupId, char **returnDeviceInfo)
843 {
844 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
845 if ((appId == NULL) || (deviceId == NULL) || (groupId == NULL) || (returnDeviceInfo == NULL) ||
846 (osAccountId == INVALID_OS_ACCOUNT)) {
847 LOGE("Invalid input parameters!");
848 return HC_ERR_INVALID_PARAMS;
849 }
850 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
851 LOGE("No group is found based on the query parameters!");
852 return HC_ERR_GROUP_NOT_EXIST;
853 }
854 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
855 LOGE("You do not have the permission to query the group information!");
856 return HC_ERR_ACCESS_DENIED;
857 }
858 TrustedDeviceEntry *deviceEntry = CreateDeviceEntry();
859 if (deviceEntry == NULL) {
860 LOGE("Failed to allocate deviceEntry memory!");
861 return HC_ERR_ALLOC_MEMORY;
862 }
863 if (GetTrustedDevInfoById(osAccountId, deviceId, isUdid, groupId, deviceEntry) != HC_SUCCESS) {
864 LOGE("No device is found based on the query parameters!");
865 DestroyDeviceEntry(deviceEntry);
866 return HC_ERR_DEVICE_NOT_EXIST;
867 }
868 CJson *devInfoJson = CreateJson();
869 if (devInfoJson == NULL) {
870 LOGE("Failed to allocate devInfoJson memory!");
871 DestroyDeviceEntry(deviceEntry);
872 return HC_ERR_JSON_FAIL;
873 }
874 int32_t result = GenerateReturnDevInfo(deviceEntry, devInfoJson);
875 DestroyDeviceEntry(deviceEntry);
876 if (result != HC_SUCCESS) {
877 FreeJson(devInfoJson);
878 return result;
879 }
880 *returnDeviceInfo = PackJsonToString(devInfoJson);
881 FreeJson(devInfoJson);
882 if (*returnDeviceInfo == NULL) {
883 LOGE("Failed to convert json to string!");
884 return HC_ERR_JSON_FAIL;
885 }
886 return HC_SUCCESS;
887 }
888
GetAccessibleTrustedDevices(int32_t osAccountId,const char * appId,const char * groupId,char ** returnDevInfoVec,uint32_t * deviceNum)889 static int32_t GetAccessibleTrustedDevices(int32_t osAccountId, const char *appId, const char *groupId,
890 char **returnDevInfoVec, uint32_t *deviceNum)
891 {
892 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
893 if ((appId == NULL) || (groupId == NULL) || (returnDevInfoVec == NULL) || (deviceNum == NULL) ||
894 (osAccountId == INVALID_OS_ACCOUNT)) {
895 LOGE("Invalid input parameters!");
896 return HC_ERR_INVALID_PARAMS;
897 }
898 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
899 LOGE("No group is found based on the query parameters!");
900 return HC_ERR_GROUP_NOT_EXIST;
901 }
902 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
903 LOGE("You do not have the permission to query the group information!");
904 return HC_ERR_ACCESS_DENIED;
905 }
906 DeviceEntryVec deviceEntryVec = CreateDeviceEntryVec();
907 int32_t result = GetTrustedDevices(osAccountId, groupId, &deviceEntryVec);
908 if (result != HC_SUCCESS) {
909 ClearDeviceEntryVec(&deviceEntryVec);
910 return result;
911 }
912 result = GenerateReturnDeviceVec(&deviceEntryVec, returnDevInfoVec, deviceNum);
913 ClearDeviceEntryVec(&deviceEntryVec);
914 return result;
915 }
916
IsDeviceInAccessibleGroup(int32_t osAccountId,const char * appId,const char * groupId,const char * deviceId,bool isUdid)917 static bool IsDeviceInAccessibleGroup(int32_t osAccountId, const char *appId, const char *groupId,
918 const char *deviceId, bool isUdid)
919 {
920 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
921 if ((appId == NULL) || (groupId == NULL) || (deviceId == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
922 LOGE("Invalid input parameters!");
923 return false;
924 }
925 if (!IsGroupExistByGroupId(osAccountId, groupId)) {
926 LOGE("No group is found based on the query parameters!");
927 return false;
928 }
929 if (CheckGroupAccessible(osAccountId, groupId, appId) != HC_SUCCESS) {
930 LOGE("You do not have the permission to query the group information!");
931 return false;
932 }
933 return IsTrustedDeviceInGroup(osAccountId, groupId, deviceId, isUdid);
934 }
935
GetPkInfoList(int32_t osAccountId,const char * appId,const char * queryParams,char ** returnInfoList,uint32_t * returnInfoNum)936 static int32_t GetPkInfoList(int32_t osAccountId, const char *appId, const char *queryParams,
937 char **returnInfoList, uint32_t *returnInfoNum)
938 {
939 LOGI("[Start]: start to get pk list!");
940 osAccountId = DevAuthGetRealOsAccountLocalId(osAccountId);
941 if ((appId == NULL) || (queryParams == NULL) || (returnInfoList == NULL) ||
942 (returnInfoNum == NULL) || (osAccountId == INVALID_OS_ACCOUNT)) {
943 LOGE("Invalid input parameters!");
944 return HC_ERR_INVALID_PARAMS;
945 }
946 CJson *params = CreateJsonFromString(queryParams);
947 if (params == NULL) {
948 LOGE("Failed to create json from string!");
949 return HC_ERR_JSON_CREATE;
950 }
951 CJson *pkInfoList = CreateJsonArray();
952 if (pkInfoList == NULL) {
953 LOGE("Failed to create json array!");
954 FreeJson(params);
955 return HC_ERR_JSON_CREATE;
956 }
957 int32_t res = GeneratePkInfoList(osAccountId, params, pkInfoList);
958 FreeJson(params);
959 if (res != HC_SUCCESS) {
960 FreeJson(pkInfoList);
961 return res;
962 }
963 int32_t pkInfoNum = GetItemNum(pkInfoList);
964 char *pkInfoListStr = PackJsonToString(pkInfoList);
965 FreeJson(pkInfoList);
966 if (pkInfoListStr == NULL) {
967 LOGE("Failed to convert json to string!");
968 return HC_ERR_PACKAGE_JSON_TO_STRING_FAIL;
969 }
970 *returnInfoList = pkInfoListStr;
971 *returnInfoNum = pkInfoNum;
972 LOGI("[End]: Get pk list successfully! [PkInfoNum]: %" PRId32, pkInfoNum);
973 return HC_SUCCESS;
974 }
975
DestroyInfo(char ** returnInfo)976 static void DestroyInfo(char **returnInfo)
977 {
978 if ((returnInfo == NULL) || (*returnInfo == NULL)) {
979 return;
980 }
981 FreeJsonString(*returnInfo);
982 *returnInfo = NULL;
983 }
984
985 static const GroupImpl GROUP_IMPL_INSTANCE = {
986 .createGroup = RequestCreateGroup,
987 .deleteGroup = RequestDeleteGroup,
988 .deleteMember = RequestDeleteMemberFromGroup,
989 .addMultiMembers = RequestAddMultiMembersToGroup,
990 .delMultiMembers = RequestDelMultiMembersFromGroup,
991 .regListener = RegListener,
992 .unRegListener = UnRegListener,
993 .getRegisterInfo = GetRegisterInfo,
994 .checkAccessToGroup = CheckAccessToGroup,
995 .getAccessibleGroupInfoById = GetAccessibleGroupInfoById,
996 .getAccessibleGroupInfo = GetAccessibleGroupInfo,
997 .getAccessibleJoinedGroups = GetAccessibleJoinedGroups,
998 .getAccessibleRelatedGroups = GetAccessibleRelatedGroups,
999 .getAccessibleDeviceInfoById = GetAccessibleDeviceInfoById,
1000 .getAccessibleTrustedDevices = GetAccessibleTrustedDevices,
1001 .isDeviceInAccessibleGroup = IsDeviceInAccessibleGroup,
1002 .getPkInfoList = GetPkInfoList,
1003 .destroyInfo = DestroyInfo
1004 };
1005
InitGroupRelatedModule(void)1006 int32_t InitGroupRelatedModule(void)
1007 {
1008 if (IsBroadcastSupported()) {
1009 if (InitBroadcastManager() != HC_SUCCESS) {
1010 LOGE("[End]: [Service]: Failed to init broadcast manage module!");
1011 return HC_ERR_SERVICE_NEED_RESTART;
1012 }
1013 }
1014 return HC_SUCCESS;
1015 }
1016
DestroyGroupRelatedModule(void)1017 void DestroyGroupRelatedModule(void)
1018 {
1019 DestroyBroadcastManager();
1020 }
1021
GetGroupImplInstance(void)1022 const GroupImpl *GetGroupImplInstance(void)
1023 {
1024 return &GROUP_IMPL_INSTANCE;
1025 }
1026
IsGroupSupport(void)1027 bool IsGroupSupport(void)
1028 {
1029 return true;
1030 }