1 /*
2 * Copyright (C) 2021 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 "das_module.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "hc_vector.h"
21 #include "das_task_main.h"
22
23 #define DAS_CLIENT_STEP_MASK 0xF00F
24 #define DAS_CLIENT_FIRST_MESSAGE 0x0001
25
26 DECLARE_HC_VECTOR(TaskInModuleVec, void *);
27 IMPLEMENT_HC_VECTOR(TaskInModuleVec, void *, 1)
28
29 TaskInModuleVec g_taskInModuleVec;
30 DasAuthModule g_dasModule = { 0 };
31
RegisterDasLocalIdentity(const char * pkgName,const char * serviceType,Uint8Buff * authId,int userType)32 static int32_t RegisterDasLocalIdentity(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType)
33 {
34 return RegisterLocalIdentityInTask(pkgName, serviceType, authId, userType);
35 }
36
UnregisterDasLocalIdentity(const char * pkgName,const char * serviceType,Uint8Buff * authId,int userType)37 static int32_t UnregisterDasLocalIdentity(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType)
38 {
39 return UnregisterLocalIdentityInTask(pkgName, serviceType, authId, userType);
40 }
41
DeleteDasPeerAuthInfo(const char * pkgName,const char * serviceType,Uint8Buff * authId,int userType)42 static int32_t DeleteDasPeerAuthInfo(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType)
43 {
44 return DeletePeerAuthInfoInTask(pkgName, serviceType, authId, userType);
45 }
46
GetDasPublicKey(const char * pkgName,const char * serviceType,Uint8Buff * authId,int userType,Uint8Buff * returnPk)47 static int32_t GetDasPublicKey(const char *pkgName, const char *serviceType, Uint8Buff *authId, int userType,
48 Uint8Buff *returnPk)
49 {
50 return GetPublicKeyInTask(pkgName, serviceType, authId, userType, returnPk);
51 }
52
IsDasMsgNeedIgnore(const CJson * in)53 bool IsDasMsgNeedIgnore(const CJson *in)
54 {
55 int32_t message = 0;
56 if (GetIntFromJson(in, FIELD_MESSAGE, &message) != HC_SUCCESS) {
57 LOGD("There is no message code."); // There is no message code in the client's createTask request params
58 return false;
59 }
60 if ((message & DAS_CLIENT_STEP_MASK) == DAS_CLIENT_FIRST_MESSAGE) {
61 return false;
62 }
63
64 LOGI("The message needs to ignore, message: %d.", message);
65 return true;
66 }
67
CreateDasTask(int32_t * taskId,const CJson * in,CJson * out)68 static int CreateDasTask(int32_t *taskId, const CJson *in, CJson *out)
69 {
70 if (taskId == NULL || in == NULL || out == NULL) {
71 LOGE("Params is null.");
72 return HC_ERR_NULL_PTR;
73 }
74 if (IsDasMsgNeedIgnore(in)) {
75 return HC_ERR_IGNORE_MSG;
76 }
77 Task *task = CreateTaskT(taskId, in, out);
78 if (task == NULL) {
79 LOGE("Create das task failed.");
80 return HC_ERR_ALLOC_MEMORY;
81 }
82
83 g_taskInModuleVec.pushBackT(&g_taskInModuleVec, (void *)task);
84 return HC_SUCCESS;
85 }
86
DestroyDasModule(AuthModuleBase * module)87 static void DestroyDasModule(AuthModuleBase *module)
88 {
89 uint32_t index;
90 void **ptr = NULL;
91 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
92 if ((ptr != NULL) && (*ptr != NULL)) {
93 ((Task *)(*ptr))->destroyTask((Task *)(*ptr));
94 }
95 }
96 DESTROY_HC_VECTOR(TaskInModuleVec, &g_taskInModuleVec);
97 DestroyDasProtocolEntities();
98 if (module != NULL) {
99 (void)memset_s(module, sizeof(DasAuthModule), 0, sizeof(DasAuthModule));
100 }
101 }
102
ProcessDasTask(int32_t taskId,const CJson * in,CJson * out,int32_t * status)103 static int ProcessDasTask(int32_t taskId, const CJson* in, CJson* out, int32_t *status)
104 {
105 if (status == NULL || in == NULL || out == NULL) {
106 LOGE("Params is null.");
107 return HC_ERR_NULL_PTR;
108 }
109 uint32_t index;
110 void **ptr = NULL;
111 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
112 if ((ptr != NULL) && (*ptr != NULL)) {
113 Task *temp = (Task *)*ptr;
114 if (taskId == temp->taskId) {
115 return temp->processTask(temp, in, out, status);
116 }
117 }
118 }
119
120 LOGE("Task doesn't exist, taskId: %d.", taskId);
121 return HC_ERR_TASK_ID_IS_NOT_MATCH;
122 }
123
DestroyDasTask(int taskId)124 static void DestroyDasTask(int taskId)
125 {
126 uint32_t index;
127 void **ptr = NULL;
128 FOR_EACH_HC_VECTOR(g_taskInModuleVec, index, ptr) {
129 if ((ptr != NULL) && (*ptr != NULL)) {
130 Task *temp = (Task *)(*ptr);
131 if (taskId == temp->taskId) {
132 temp->destroyTask(temp);
133 void *tempPtr = NULL;
134 HC_VECTOR_POPELEMENT(&g_taskInModuleVec, &tempPtr, index);
135 return;
136 }
137 }
138 }
139 }
140
IsDasSupported(void)141 bool IsDasSupported(void)
142 {
143 return true;
144 }
145
CreateDasModule(void)146 AuthModuleBase *CreateDasModule(void)
147 {
148 g_dasModule.moduleBase.moduleType = DAS_MODULE;
149 g_dasModule.moduleBase.createTask = CreateDasTask;
150 g_dasModule.moduleBase.processTask = ProcessDasTask;
151 g_dasModule.moduleBase.destroyTask = DestroyDasTask;
152 g_dasModule.moduleBase.destroyModule = DestroyDasModule;
153 g_dasModule.registerLocalIdentity = RegisterDasLocalIdentity;
154 g_dasModule.unregisterLocalIdentity = UnregisterDasLocalIdentity;
155 g_dasModule.deletePeerAuthInfo = DeleteDasPeerAuthInfo;
156 g_dasModule.getPublicKey = GetDasPublicKey;
157 g_taskInModuleVec = CREATE_HC_VECTOR(TaskInModuleVec);
158 if (InitDasProtocolEntities() != HC_SUCCESS) {
159 LOGE("Init das protocol entities failed.");
160 DestroyDasModule((AuthModuleBase *)&g_dasModule);
161 return NULL;
162 }
163 return (AuthModuleBase *)&g_dasModule;
164 }
165