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