• 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_manager_common.h"
17 
18 #include "hc_log.h"
19 #include "common_defs.h"
20 #include "callback_manager.h"
21 #include "task_manager.h"
22 
InitGroupManagerTask(GroupManagerTask * task,GMTaskParams * taskParams,TaskFunc func)23 static int32_t InitGroupManagerTask(GroupManagerTask *task, GMTaskParams *taskParams, TaskFunc func)
24 {
25     task->base.doAction = func;
26     task->base.destroy = DestroyGroupManagerTask;
27     task->osAccountId = taskParams->osAccountId;
28     task->opCode = taskParams->opCode;
29     task->reqId = taskParams->reqId;
30     task->params = taskParams->params;
31     return BindCallbackToTask(task, taskParams->params);
32 }
33 
AddReqInfoToJson(int64_t requestId,const char * appId,CJson * jsonParams)34 int32_t AddReqInfoToJson(int64_t requestId, const char *appId, CJson *jsonParams)
35 {
36     if (AddInt64StringToJson(jsonParams, FIELD_REQUEST_ID, requestId) != HC_SUCCESS) {
37         LOGE("Failed to add requestId to json!");
38         return HC_ERR_JSON_FAIL;
39     }
40     if (AddStringToJson(jsonParams, FIELD_APP_ID, appId) != HC_SUCCESS) {
41         LOGE("Failed to add appId to json!");
42         return HC_ERR_JSON_FAIL;
43     }
44     return HC_SUCCESS;
45 }
46 
BindCallbackToTask(GroupManagerTask * task,const CJson * jsonParams)47 int32_t BindCallbackToTask(GroupManagerTask *task, const CJson *jsonParams)
48 {
49     const char *appId = GetStringFromJson(jsonParams, FIELD_APP_ID);
50     if (appId == NULL) {
51         LOGE("Failed to get appId from jsonParams!");
52         return HC_ERR_JSON_GET;
53     }
54     task->cb = GetGMCallbackByAppId(appId);
55     if (task->cb == NULL) {
56         LOGE("Failed to find callback by appId! [AppId]: %s", appId);
57         return HC_ERR_CALLBACK_NOT_FOUND;
58     }
59     return HC_SUCCESS;
60 }
61 
DestroyGroupManagerTask(HcTaskBase * task)62 void DestroyGroupManagerTask(HcTaskBase *task)
63 {
64     if (task == NULL) {
65         LOGE("The input task is NULL!");
66         return;
67     }
68     FreeJson(((GroupManagerTask *)task)->params);
69 }
70 
AddBindParamsToJson(int operationCode,int64_t requestId,const char * appId,CJson * jsonParams)71 int32_t AddBindParamsToJson(int operationCode, int64_t requestId, const char *appId, CJson *jsonParams)
72 {
73     if (AddIntToJson(jsonParams, FIELD_OPERATION_CODE, operationCode) != HC_SUCCESS) {
74         LOGE("Failed to add operationCode to json!");
75         return HC_ERR_JSON_FAIL;
76     }
77     return AddReqInfoToJson(requestId, appId, jsonParams);
78 }
79 
InitAndPushGMTask(int32_t osAccountId,int32_t opCode,int64_t reqId,CJson * params,TaskFunc func)80 int32_t InitAndPushGMTask(int32_t osAccountId, int32_t opCode, int64_t reqId, CJson *params, TaskFunc func)
81 {
82     GroupManagerTask *task = (GroupManagerTask *)HcMalloc(sizeof(GroupManagerTask), 0);
83     if (task == NULL) {
84         LOGE("Failed to allocate task memory!");
85         return HC_ERR_ALLOC_MEMORY;
86     }
87     GMTaskParams taskParams;
88     taskParams.osAccountId = osAccountId;
89     taskParams.opCode = opCode;
90     taskParams.reqId = reqId;
91     taskParams.params = params;
92     if (InitGroupManagerTask(task, &taskParams, func) != HC_SUCCESS) {
93         HcFree(task);
94         return HC_ERR_INIT_TASK_FAIL;
95     }
96     if (PushTask((HcTaskBase *)task) != HC_SUCCESS) {
97         HcFree(task);
98         return HC_ERR_INIT_TASK_FAIL;
99     }
100     return HC_SUCCESS;
101 }