• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "account_lifecycle_plugin_proxy.h"
17 #include "device_auth_defines.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "task_manager.h"
21 #include "account_task_manager.h"
22 #include "alg_loader.h"
23 
24 static AccountLifecyleExtPlug *g_accountLifeCyclePlugin = NULL;
25 static AccountLifecyleExtPlugCtx *g_accountPluginCtx = NULL;
26 
27 typedef struct {
28     HcTaskBase base;
29     ExtWorkerTask *extTask;
30     int32_t taskId;
31 } WorkerTask;
32 
DoWorkerTask(HcTaskBase * task)33 static void DoWorkerTask(HcTaskBase *task)
34 {
35     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task begin.");
36     if (task == NULL) {
37         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL, cannot do task!");
38         return;
39     }
40     WorkerTask *workerTask = (WorkerTask *)task;
41     if (workerTask->extTask == NULL) {
42         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
43         return;
44     }
45     if (workerTask->extTask->execute == NULL) {
46         LOGE("[ACCOUNT_LIFE_PLUGIN]: The ext func is NULL, cannot do task!");
47         return;
48     }
49     workerTask->extTask->execute(workerTask->extTask);
50     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task end.");
51 }
52 
DestroyExtWorkerTask(ExtWorkerTask * task)53 static void DestroyExtWorkerTask(ExtWorkerTask *task)
54 {
55     if (task == NULL || task->destroy == NULL) {
56         LOGI("[ACCOUNT_LIFE_PLUGIN]: The destroy func is NULL, cannot destroy task!");
57         return;
58     }
59     task->destroy(task);
60 }
61 
DestroyWorkerTask(HcTaskBase * workerTask)62 static void DestroyWorkerTask(HcTaskBase *workerTask)
63 {
64     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task begin.");
65     if (workerTask == NULL) {
66         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
67         return;
68     }
69     DestroyExtWorkerTask(((WorkerTask *)workerTask)->extTask);
70     RemoveAccountTaskRecordAndUnload(((WorkerTask *)workerTask)->taskId);
71     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task end.");
72 }
73 
ExecuteWorkerTask(struct ExtWorkerTask * extTask)74 static int32_t ExecuteWorkerTask(struct ExtWorkerTask *extTask)
75 {
76     if (extTask == NULL) {
77         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL.");
78         return HC_ERR_INVALID_PARAMS;
79     }
80     WorkerTask *baseTask = (WorkerTask *)HcMalloc(sizeof(WorkerTask), 0);
81     if (baseTask == NULL) {
82         LOGE("[ACCOUNT_LIFE_PLUGIN]: Failed to allocate task memory!");
83         DestroyExtWorkerTask(extTask);
84         return HC_ERR_ALLOC_MEMORY;
85     }
86     baseTask->extTask = extTask;
87     baseTask->base.doAction = DoWorkerTask;
88     baseTask->base.destroy = DestroyWorkerTask;
89     Uint8Buff taskIdBuff = { (uint8_t *)(&baseTask->taskId), sizeof(int32_t) };
90     int32_t res = GetLoaderInstance()->generateRandom(&taskIdBuff);
91     if (res != HC_SUCCESS) {
92         LOGE("Generate taskId failed, res: %" LOG_PUB "d.", res);
93         DestroyExtWorkerTask(extTask);
94         HcFree(baseTask);
95         return res;
96     }
97     res = LoadAccountAndAddTaskRecord(baseTask->taskId);
98     if (res != HC_SUCCESS) {
99         LOGE("Add account task record failed, res: %" LOG_PUB "d.", res);
100         DestroyExtWorkerTask(extTask);
101         HcFree(baseTask);
102         return res;
103     }
104     if (PushTask((HcTaskBase *)baseTask) != HC_SUCCESS) {
105         LOGE("[ACCOUNT_LIFE_PLUGIN]: Push worker task fail.");
106         RemoveAccountTaskRecordAndUnload(baseTask->taskId);
107         DestroyExtWorkerTask(extTask);
108         HcFree(baseTask);
109         return HC_ERR_INIT_TASK_FAIL;
110     }
111     return HC_SUCCESS;
112 }
113 
114 
InitAccountLifecyclePluginCtx(void)115 static int32_t InitAccountLifecyclePluginCtx(void)
116 {
117     g_accountPluginCtx = (AccountLifecyleExtPlugCtx *)HcMalloc(sizeof(AccountLifecyleExtPlugCtx), 0);
118     if (g_accountPluginCtx == NULL) {
119         LOGE("[ACCOUNT_LIFE_PLUGIN]: Malloc memory failed.");
120         return HC_ERROR;
121     }
122     const DeviceGroupManager *gmInstace = GetGmInstance();
123     if (gmInstace == NULL) {
124         LOGE("[ACCOUNT_LIFE_PLUGIN]: Gm instance is null.");
125         HcFree(g_accountPluginCtx);
126         g_accountPluginCtx = NULL;
127         return HC_ERR_INVALID_PARAMS;
128     }
129 #ifdef DEV_AUTH_IS_ENABLE
130     const CredManager *cmInstace = GetCredMgrInstance();
131     if (cmInstace == NULL) {
132         LOGE("[ACCOUNT_LIFE_PLUGIN]: Cm instance is null.");
133         HcFree(g_accountPluginCtx);
134         g_accountPluginCtx = NULL;
135         return HC_ERR_INVALID_PARAMS;
136     }
137     g_accountPluginCtx->addCredential = cmInstace->addCredential;
138     g_accountPluginCtx->exportCredential = cmInstace->exportCredential;
139     g_accountPluginCtx->deleteCredential = cmInstace->deleteCredential;
140     g_accountPluginCtx->updateCredInfo = cmInstace->updateCredInfo;
141     g_accountPluginCtx->queryCredInfoByCredId = cmInstace->queryCredInfoByCredId;
142     g_accountPluginCtx->queryCredentialByParams = cmInstace->queryCredentialByParams;
143     g_accountPluginCtx->destroyInfo = cmInstace->destroyInfo;
144 #endif
145     g_accountPluginCtx->createGroup = gmInstace->createGroup;
146     g_accountPluginCtx->deleteGroup = gmInstace->deleteGroup;
147     g_accountPluginCtx->getGroupInfo = gmInstace->getGroupInfo;
148     g_accountPluginCtx->getRegisterInfo = gmInstace->getRegisterInfo;
149     g_accountPluginCtx->regCallback = gmInstace->regCallback;
150     g_accountPluginCtx->unRegCallback = gmInstace->unRegCallback;
151     g_accountPluginCtx->executeWorkerTask = ExecuteWorkerTask;
152     g_accountPluginCtx->notifyAsyncTaskStart = NotifyAsyncTaskStart;
153     g_accountPluginCtx->notifyAsyncTaskStop = NotifyAsyncTaskStop;
154     return HC_SUCCESS;
155 }
156 
SetAccountLifecyclePlugin(const CJson * inputParams,AccountLifecyleExtPlug * accountLifeCyclePlugin)157 int32_t SetAccountLifecyclePlugin(const CJson *inputParams, AccountLifecyleExtPlug *accountLifeCyclePlugin)
158 {
159     g_accountLifeCyclePlugin = accountLifeCyclePlugin;
160     if (g_accountLifeCyclePlugin == NULL || g_accountLifeCyclePlugin->base.init == NULL) {
161         LOGE("[ACCOUNT_LIFE_PLUGIN]: Input params are invalid.");
162         return HC_ERR_INVALID_PARAMS;
163     }
164     int32_t res = InitAccountLifecyclePluginCtx();
165     if (res != HC_SUCCESS) {
166         LOGE("[ACCOUNT_LIFE_PLUGIN]: Get account life ctx failed.");
167         return HC_ERROR;
168     }
169     return g_accountLifeCyclePlugin->base.init(&g_accountLifeCyclePlugin->base,
170         inputParams, (const ExtPluginCtx *)g_accountPluginCtx);
171 }
172 
DestoryAccountLifecyclePlugin(void)173 void DestoryAccountLifecyclePlugin(void)
174 {
175     if (g_accountLifeCyclePlugin != NULL && g_accountLifeCyclePlugin->base.destroy != NULL) {
176         g_accountLifeCyclePlugin->base.destroy(&g_accountLifeCyclePlugin->base);
177         g_accountLifeCyclePlugin = NULL;
178     }
179     if (g_accountPluginCtx != NULL) {
180         HcFree(g_accountPluginCtx);
181         g_accountPluginCtx = NULL;
182     }
183 }