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