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 "ext_part_proxy.h"
17 
18 #include <stddef.h>
19 #include "account_auth_plugin_proxy.h"
20 #include "account_lifecycle_plugin_proxy.h"
21 #include "device_auth_defines.h"
22 #include "hc_log.h"
23 #include "hc_types.h"
24 
25 static ExtPart *g_extPart = NULL;
26 
ParsePlugins(const ExtPartProxy * pluginProxy)27 static int32_t ParsePlugins(const ExtPartProxy *pluginProxy)
28 {
29     ExtPluginList pluginList = pluginProxy->getPluginListFunc(g_extPart);
30     ExtPluginNode *current =  pluginList;
31     while (current != NULL) {
32         if (current->plugin == NULL) {
33             current = current->next;
34             continue;
35         }
36         switch (current->plugin->pluginType) {
37             case EXT_PLUGIN_ACCT_LIFECYCLE:
38                 SetAccountLifecyclePlugin(NULL, (AccountLifecyleExtPlug *)(current->plugin));
39                 break;
40             case EXT_PLUGIN_ACCT_AUTH:
41                 SetAccountAuthPlugin(NULL, (AccountAuthExtPlug *)(current->plugin));
42                 break;
43             default:
44                 LOGW("Invalid plugin type %d", current->plugin->pluginType);
45                 break;
46         }
47         current = current->next;
48     }
49     return HC_SUCCESS;
50 }
51 
AddExtPlugin(const ExtPartProxy * pluginProxy)52 int32_t AddExtPlugin(const ExtPartProxy *pluginProxy)
53 {
54     if (pluginProxy == NULL || pluginProxy->initExtPartFunc == NULL || pluginProxy->getPluginListFunc == NULL ||
55         pluginProxy->destroyExtPartFunc == NULL) {
56         LOGE("[EXT_PLUGIN]: The plugin is invalid.");
57         return HC_ERR_INVALID_PARAMS;
58     }
59     g_extPart = (ExtPart *)HcMalloc(sizeof(ExtPart), 0);
60     if (g_extPart == NULL) {
61         LOGE("[EXT_PLUGIN]: Malloc memory failed.");
62         return HC_ERR_ALLOC_MEMORY;
63     }
64     int32_t res = HC_ERROR;
65     do {
66         res = pluginProxy->initExtPartFunc(NULL, g_extPart);
67         if (res != HC_SUCCESS) {
68             LOGE("[EXT_PLUGIN]: Init ext failed.");
69             break;
70         }
71         res = ParsePlugins(pluginProxy);
72         if (res != HC_SUCCESS) {
73             LOGE("[EXT_PLUGIN]: Parse ext plugin failed.");
74             break;
75         }
76         return HC_SUCCESS;
77     } while (0);
78     HcFree(g_extPart);
79     g_extPart = NULL;
80     return res;
81 }
82 
DestroyExt(ExtPartProxy * pluginProxy)83 void DestroyExt(ExtPartProxy *pluginProxy)
84 {
85     if (pluginProxy != NULL) {
86         pluginProxy->destroyExtPartFunc(g_extPart);
87     }
88     if (g_extPart != NULL) {
89         HcFree(g_extPart);
90         g_extPart = NULL;
91     }
92 }