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 "callback_manager.h"
17 #include "channel_manager.h"
18 #include "device_auth_defines.h"
19 #include "hc_log.h"
20 #include "hc_mutex.h"
21 #include "hc_types.h"
22 #include "hc_vector.h"
23 #include "securec.h"
24
25 typedef struct {
26 char *appId;
27 DeviceAuthCallback *callback;
28 } CallbackEntry;
29
30 DECLARE_HC_VECTOR(GMCallbackEntryVec, CallbackEntry);
31 IMPLEMENT_HC_VECTOR(GMCallbackEntryVec, CallbackEntry, 1)
32 static GMCallbackEntryVec g_callbackVec;
33 static HcMutex *g_callbackMutex = NULL;
34
UpdateCallbackIfExist(const char * appId,const DeviceAuthCallback * callback)35 static int32_t UpdateCallbackIfExist(const char *appId, const DeviceAuthCallback *callback)
36 {
37 uint32_t index;
38 CallbackEntry *entry = NULL;
39 g_callbackMutex->lock(g_callbackMutex);
40 FOR_EACH_HC_VECTOR(g_callbackVec, index, entry) {
41 if (strcmp(entry->appId, appId) == 0) {
42 if (memcpy_s(entry->callback, sizeof(DeviceAuthCallback),
43 callback, sizeof(DeviceAuthCallback)) != EOK) {
44 g_callbackMutex->unlock(g_callbackMutex);
45 LOGE("Failed to copy service callback!");
46 return HC_ERR_MEMORY_COPY;
47 }
48 g_callbackMutex->unlock(g_callbackMutex);
49 return HC_SUCCESS;
50 }
51 }
52 g_callbackMutex->unlock(g_callbackMutex);
53 return HC_ERR_CALLBACK_NOT_FOUND;
54 }
55
AddCallbackIfNotExist(const char * appId,const DeviceAuthCallback * callback)56 static int32_t AddCallbackIfNotExist(const char *appId, const DeviceAuthCallback *callback)
57 {
58 uint32_t appIdLen = HcStrlen(appId) + 1;
59 char *copyAppId = (char *)HcMalloc(appIdLen, 0);
60 if (copyAppId == NULL) {
61 LOGE("Failed to allocate copyAppId memory!");
62 return HC_ERR_ALLOC_MEMORY;
63 }
64 if (strcpy_s(copyAppId, appIdLen, appId) != EOK) {
65 LOGE("Failed to copy appId!");
66 HcFree(copyAppId);
67 return HC_ERR_MEMORY_COPY;
68 }
69 DeviceAuthCallback *copyCallback = (DeviceAuthCallback *)HcMalloc(sizeof(DeviceAuthCallback), 0);
70 if (copyCallback == NULL) {
71 LOGE("Failed to allocate copyCallback memory!");
72 HcFree(copyAppId);
73 return HC_ERR_ALLOC_MEMORY;
74 }
75 if (memcpy_s(copyCallback, sizeof(DeviceAuthCallback), callback, sizeof(DeviceAuthCallback)) != EOK) {
76 LOGE("Failed to copy service callback!");
77 HcFree(copyAppId);
78 HcFree(copyCallback);
79 return HC_ERR_MEMORY_COPY;
80 }
81 CallbackEntry entry;
82 entry.appId = copyAppId;
83 entry.callback = copyCallback;
84 g_callbackMutex->lock(g_callbackMutex);
85 if (g_callbackVec.pushBack(&g_callbackVec, &entry) == NULL) {
86 LOGE("Failed to push callback to vector!");
87 HcFree(copyAppId);
88 HcFree(copyCallback);
89 g_callbackMutex->unlock(g_callbackMutex);
90 return HC_ERR_MEMORY_COPY;
91 }
92 g_callbackMutex->unlock(g_callbackMutex);
93 LOGD("[End]: Add callback successfully!");
94 return HC_SUCCESS;
95 }
96
ProcessTransmitCallback(int64_t reqId,const uint8_t * data,uint32_t dataLen,const DeviceAuthCallback * callback)97 bool ProcessTransmitCallback(int64_t reqId, const uint8_t *data, uint32_t dataLen, const DeviceAuthCallback *callback)
98 {
99 if ((callback != NULL) && (callback->onTransmit != NULL)) {
100 LOGI("[Service][In]: ProcessTransmitCallback! [DataLen]: %u, [ReqId]: %" PRId64, dataLen, reqId);
101 bool res = callback->onTransmit(reqId, data, dataLen);
102 LOGI("[Service][Out]: ProcessTransmitCallback!");
103 return res;
104 }
105 LOGE("[OnTransmit]: Currently, the service callback is NULL! [ReqId]: %" PRId64, reqId);
106 return false;
107 }
108
ProcessSessionKeyCallback(int64_t reqId,const uint8_t * sessionKey,uint32_t sessionKeyLen,const DeviceAuthCallback * callback)109 void ProcessSessionKeyCallback(int64_t reqId, const uint8_t *sessionKey, uint32_t sessionKeyLen,
110 const DeviceAuthCallback *callback)
111 {
112 if ((callback != NULL) && (callback->onSessionKeyReturned != NULL)) {
113 LOGI("[Service][In]: ProcessSessionKeyCallback! [ReqId]: %" PRId64, reqId);
114 callback->onSessionKeyReturned(reqId, sessionKey, sessionKeyLen);
115 LOGI("[Service][Out]: ProcessSessionKeyCallback!");
116 return;
117 }
118 LOGE("[OnSessionKeyReturned]: Currently, the service callback is NULL! [ReqId]: %" PRId64, reqId);
119 }
120
ProcessFinishCallback(int64_t reqId,int operationCode,const char * returnData,const DeviceAuthCallback * callback)121 void ProcessFinishCallback(int64_t reqId, int operationCode, const char *returnData,
122 const DeviceAuthCallback *callback)
123 {
124 if ((callback != NULL) && (callback->onFinish != NULL)) {
125 LOGI("[Service][In]: ProcessFinishCallback! [ReqId]: %" PRId64, reqId);
126 callback->onFinish(reqId, operationCode, returnData);
127 LOGI("[Service][Out]: ProcessFinishCallback!");
128 return;
129 }
130 LOGE("[OnFinish]: Currently, the service callback is NULL! [ReqId]: %" PRId64, reqId);
131 }
132
ProcessErrorCallback(int64_t reqId,int operationCode,int errorCode,const char * errorReturn,const DeviceAuthCallback * callback)133 void ProcessErrorCallback(int64_t reqId, int operationCode, int errorCode, const char *errorReturn,
134 const DeviceAuthCallback *callback)
135 {
136 if ((callback != NULL) && (callback->onError != NULL)) {
137 LOGI("[Service][In]: ProcessErrorCallback! [ReqId]: %" PRId64, reqId);
138 callback->onError(reqId, operationCode, errorCode, errorReturn);
139 LOGI("[Service][Out]: ProcessErrorCallback!");
140 return;
141 }
142 LOGE("[OnError]: Currently, the service callback is NULL! [ReqId]: %" PRId64, reqId);
143 }
144
ProcessRequestCallback(int64_t reqId,int operationCode,const char * reqParams,const DeviceAuthCallback * callback)145 char *ProcessRequestCallback(int64_t reqId, int operationCode, const char *reqParams,
146 const DeviceAuthCallback *callback)
147 {
148 if ((callback != NULL) && (callback->onRequest != NULL)) {
149 LOGI("[Service][In]: ProcessRequestCallback! [ReqId]: %" PRId64, reqId);
150 char *returnData = callback->onRequest(reqId, operationCode, reqParams);
151 LOGI("[Service][Out]: ProcessRequestCallback!");
152 return returnData;
153 }
154 LOGE("[OnRequest]: Currently, the service callback is NULL! [ReqId]: %" PRId64, reqId);
155 return NULL;
156 }
157
GetGMCallbackByAppId(const char * appId)158 const DeviceAuthCallback *GetGMCallbackByAppId(const char *appId)
159 {
160 uint32_t index;
161 CallbackEntry *entry = NULL;
162 g_callbackMutex->lock(g_callbackMutex);
163 FOR_EACH_HC_VECTOR(g_callbackVec, index, entry) {
164 if (strcmp(entry->appId, appId) == 0) {
165 g_callbackMutex->unlock(g_callbackMutex);
166 return entry->callback;
167 }
168 }
169 g_callbackMutex->unlock(g_callbackMutex);
170 return NULL;
171 }
172
RegGroupManagerCallback(const char * appId,const DeviceAuthCallback * callback)173 int32_t RegGroupManagerCallback(const char *appId, const DeviceAuthCallback *callback)
174 {
175 SET_LOG_MODE(NORMAL_MODE);
176 if ((appId == NULL) || (callback == NULL)) {
177 LOGE("The input parameters contains NULL value!");
178 return HC_ERR_INVALID_PARAMS;
179 }
180 int32_t res = InitChannelManager();
181 if (res != HC_SUCCESS) {
182 LOGE("[End]: [Service]: Failed to init channel manage module!");
183 return res;
184 }
185 if (UpdateCallbackIfExist(appId, callback) == HC_SUCCESS) {
186 return HC_SUCCESS;
187 }
188 return AddCallbackIfNotExist(appId, callback);
189 }
190
UnRegGroupManagerCallback(const char * appId)191 int32_t UnRegGroupManagerCallback(const char *appId)
192 {
193 SET_LOG_MODE(NORMAL_MODE);
194 if (appId == NULL) {
195 LOGE("The input appId is NULL!");
196 return HC_ERR_INVALID_PARAMS;
197 }
198 uint32_t index;
199 CallbackEntry *entry = NULL;
200 g_callbackMutex->lock(g_callbackMutex);
201 FOR_EACH_HC_VECTOR(g_callbackVec, index, entry) {
202 if (strcmp(entry->appId, appId) == 0) {
203 HcFree(entry->appId);
204 HcFree(entry->callback);
205 CallbackEntry tempEntry;
206 HC_VECTOR_POPELEMENT(&g_callbackVec, &tempEntry, index);
207 g_callbackMutex->unlock(g_callbackMutex);
208 return HC_SUCCESS;
209 }
210 }
211 g_callbackMutex->unlock(g_callbackMutex);
212 return HC_SUCCESS;
213 }
214
InitCallbackManager(void)215 int32_t InitCallbackManager(void)
216 {
217 if (g_callbackMutex == NULL) {
218 g_callbackMutex = (HcMutex *)HcMalloc(sizeof(HcMutex), 0);
219 if (g_callbackMutex == NULL) {
220 LOGE("Failed to allocate broadcast mutex memory!");
221 return HC_ERR_ALLOC_MEMORY;
222 }
223 if (InitHcMutex(g_callbackMutex) != HC_SUCCESS) {
224 LOGE("Init mutex failed!");
225 HcFree(g_callbackMutex);
226 g_callbackMutex = NULL;
227 return HC_ERROR;
228 }
229 }
230 g_callbackVec = CREATE_HC_VECTOR(GMCallbackEntryVec);
231 return HC_SUCCESS;
232 }
233
DestroyCallbackManager(void)234 void DestroyCallbackManager(void)
235 {
236 uint32_t index;
237 CallbackEntry *entry = NULL;
238 g_callbackMutex->lock(g_callbackMutex);
239 FOR_EACH_HC_VECTOR(g_callbackVec, index, entry) {
240 HcFree(entry->appId);
241 HcFree(entry->callback);
242 }
243 DESTROY_HC_VECTOR(GMCallbackEntryVec, &g_callbackVec);
244 g_callbackMutex->unlock(g_callbackMutex);
245 DestroyHcMutex(g_callbackMutex);
246 HcFree(g_callbackMutex);
247 g_callbackMutex = NULL;
248 }