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 
22 static AccountLifecyleExtPlug *g_accountLifeCyclePlugin = NULL;
23 static AccountLifecyleExtPlugCtx *g_accountPluginCtx = NULL;
24 
25 typedef struct {
26     HcTaskBase base;
27     ExtWorkerTask *extTask;
28 } WorkerTask;
29 
DoWorkerTask(HcTaskBase * task)30 static void DoWorkerTask(HcTaskBase *task)
31 {
32     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task begin.");
33     if (task == NULL) {
34         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL, cannot do task!");
35         return;
36     }
37     WorkerTask *workerTask = (WorkerTask *)task;
38     if (workerTask->extTask == NULL) {
39         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
40         return;
41     }
42     if (workerTask->extTask->execute == NULL) {
43         LOGE("[ACCOUNT_LIFE_PLUGIN]: The ext func is NULL, cannot do task!");
44         return;
45     }
46     workerTask->extTask->execute(workerTask->extTask);
47     LOGD("[ACCOUNT_LIFE_PLUGIN]: Do worker task end.");
48 }
49 
DestroyExtWorkerTask(ExtWorkerTask * task)50 static void DestroyExtWorkerTask(ExtWorkerTask *task)
51 {
52     if (task == NULL || task->destroy == NULL) {
53         LOGI("[ACCOUNT_LIFE_PLUGIN]: The destroy func is NULL, cannot destroy task!");
54         return;
55     }
56     task->destroy(task);
57 }
58 
DestroyWorkerTask(HcTaskBase * workerTask)59 static void DestroyWorkerTask(HcTaskBase *workerTask)
60 {
61     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task begin.");
62     if (workerTask == NULL) {
63         LOGE("[ACCOUNT_LIFE_PLUGIN]: The inner task is NULL, cannot do task!");
64         return;
65     }
66     DestroyExtWorkerTask(((WorkerTask *)workerTask)->extTask);
67     LOGD("[ACCOUNT_LIFE_PLUGIN]: Destroy worker task end.");
68 }
69 
ExecuteWorkerTask(struct ExtWorkerTask * extTask)70 static int32_t ExecuteWorkerTask(struct ExtWorkerTask *extTask)
71 {
72     if (extTask == NULL) {
73         LOGE("[ACCOUNT_LIFE_PLUGIN]: The input task is NULL.");
74         return HC_ERR_INVALID_PARAMS;
75     }
76     WorkerTask *baseTask = (WorkerTask *)HcMalloc(sizeof(WorkerTask), 0);
77     if (baseTask == NULL) {
78         LOGE("[ACCOUNT_LIFE_PLUGIN]: Failed to allocate task memory!");
79         DestroyExtWorkerTask(extTask);
80         return HC_ERR_ALLOC_MEMORY;
81     }
82     baseTask->extTask = extTask;
83     baseTask->base.doAction = DoWorkerTask;
84     baseTask->base.destroy = DestroyWorkerTask;
85     if (PushTask((HcTaskBase *)baseTask) != HC_SUCCESS) {
86         LOGE("[ACCOUNT_LIFE_PLUGIN]: Push worker task fail.");
87         DestroyExtWorkerTask(extTask);
88         HcFree(baseTask);
89         return HC_ERR_INIT_TASK_FAIL;
90     }
91     return HC_SUCCESS;
92 }
93 
94 
InitAccountLifecyclePluginCtx(void)95 static int32_t InitAccountLifecyclePluginCtx(void)
96 {
97     g_accountPluginCtx = (AccountLifecyleExtPlugCtx *)HcMalloc(sizeof(AccountLifecyleExtPlugCtx), 0);
98     if (g_accountPluginCtx == NULL) {
99         LOGE("[ACCOUNT_LIFE_PLUGIN]: Malloc memory failed.");
100         return HC_ERROR;
101     }
102     const DeviceGroupManager *gmInstace = GetGmInstance();
103     if (gmInstace == NULL) {
104         LOGE("[ACCOUNT_LIFE_PLUGIN]: Gm instance is null.");
105         HcFree(g_accountPluginCtx);
106         g_accountPluginCtx = NULL;
107         return HC_ERR_INVALID_PARAMS;
108     }
109     g_accountPluginCtx->createGroup = gmInstace->createGroup;
110     g_accountPluginCtx->deleteGroup = gmInstace->deleteGroup;
111     g_accountPluginCtx->getGroupInfo = gmInstace->getGroupInfo;
112     g_accountPluginCtx->getRegisterInfo = gmInstace->getRegisterInfo;
113     g_accountPluginCtx->regCallback = gmInstace->regCallback;
114     g_accountPluginCtx->unRegCallback = gmInstace->unRegCallback;
115     g_accountPluginCtx->executeWorkerTask = ExecuteWorkerTask;
116     return HC_SUCCESS;
117 }
118 
SetAccountLifecyclePlugin(const CJson * inputParams,AccountLifecyleExtPlug * accountLifeCyclePlugin)119 int32_t SetAccountLifecyclePlugin(const CJson *inputParams, AccountLifecyleExtPlug *accountLifeCyclePlugin)
120 {
121     g_accountLifeCyclePlugin = accountLifeCyclePlugin;
122     if (g_accountLifeCyclePlugin == NULL || g_accountLifeCyclePlugin->base.init == NULL) {
123         LOGE("[ACCOUNT_LIFE_PLUGIN]: Input params are invalid.");
124         return HC_ERR_INVALID_PARAMS;
125     }
126     int32_t res = InitAccountLifecyclePluginCtx();
127     if (res != HC_SUCCESS) {
128         LOGE("[ACCOUNT_LIFE_PLUGIN]: Get account life ctx failed.");
129         return HC_ERROR;
130     }
131     return g_accountLifeCyclePlugin->base.init(&g_accountLifeCyclePlugin->base,
132         inputParams, (const ExtPluginCtx *)g_accountPluginCtx);
133 }
134 
DestoryAccountLifecyclePlugin(void)135 void DestoryAccountLifecyclePlugin(void)
136 {
137     if (g_accountLifeCyclePlugin != NULL && g_accountLifeCyclePlugin->base.destroy != NULL) {
138         g_accountLifeCyclePlugin->base.destroy(&g_accountLifeCyclePlugin->base);
139         g_accountLifeCyclePlugin = NULL;
140     }
141     if (g_accountPluginCtx != NULL) {
142         HcFree(g_accountPluginCtx);
143         g_accountPluginCtx = NULL;
144     }
145 }