• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ipc_adapt.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "hc_types.h"
20 #include "ipc_callback_proxy.h"
21 #include "ipc_callback_stub.h"
22 #include "ipc_dev_auth_proxy.h"
23 #include "ipc_dev_auth_stub.h"
24 #include "ipc_sdk.h"
25 #include "ipc_service.h"
26 #include "iservice_registry.h"
27 #include "securec.h"
28 #include "system_ability_definition.h"
29 
30 using namespace std;
31 using namespace OHOS;
32 namespace {
33     static const int32_t BUFF_MAX_SZ = 128;
34     static const int32_t IPC_CALL_BACK_MAX_NODES = 64;
35     static const int32_t IPC_CALL_BACK_STUB_NODES = 2;
36 }
37 
38 static sptr<StubDevAuthCb> g_sdkCbStub[IPC_CALL_BACK_STUB_NODES] = { nullptr, nullptr };
39 
40 typedef void (*CallbackStub)(uintptr_t, const IpcDataInfo *, int32_t, MessageParcel &);
41 typedef struct {
42     union {
43         DeviceAuthCallback devAuth;
44         DataChangeListener listener;
45     } cbCtx;
46     int64_t requestId;
47     char appId[BUFF_MAX_SZ];
48     int32_t cbType;
49     int32_t delOnFni;
50     int32_t methodId;
51     int32_t proxyId;
52     int32_t nodeIdx;
53 } IpcCallBackNode;
54 
55 static struct {
56     IpcCallBackNode *ctx;
57     int32_t nodeCnt;
58 } g_ipcCallBackList = {nullptr, 0};
59 static std::mutex g_cbListLock;
60 
SetIpcCallBackNodeDefault(IpcCallBackNode & node)61 static void SetIpcCallBackNodeDefault(IpcCallBackNode &node)
62 {
63     (void)memset_s(&node, sizeof(IpcCallBackNode), 0, sizeof(IpcCallBackNode));
64     node.proxyId = -1;
65     node.nodeIdx = -1;
66     return;
67 }
68 
InitIpcCallBackList(void)69 int32_t InitIpcCallBackList(void)
70 {
71     int32_t i;
72 
73     LOGI("initializing ...");
74     if (g_ipcCallBackList.ctx != nullptr) {
75         LOGI("has initialized");
76         return HC_SUCCESS;
77     }
78 
79     g_ipcCallBackList.ctx = new(std::nothrow) IpcCallBackNode[IPC_CALL_BACK_MAX_NODES];
80     if (g_ipcCallBackList.ctx == nullptr) {
81         LOGE("initialized failed");
82         return HC_ERROR;
83     }
84     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
85         SetIpcCallBackNodeDefault(g_ipcCallBackList.ctx[i]);
86     }
87     g_ipcCallBackList.nodeCnt = 0;
88     LOGI("initialized successful");
89     return HC_SUCCESS;
90 }
91 
ResetIpcCallBackNode(IpcCallBackNode & node)92 static void ResetIpcCallBackNode(IpcCallBackNode &node)
93 {
94     char errStr[] = "invalid";
95     char *appId = errStr;
96     if ((node.appId[0] != 0) && (node.appId[sizeof(node.appId) - 1] == 0)) {
97         appId = node.appId;
98     }
99     LOGI("appid is %s ", appId);
100     ServiceDevAuth::ResetRemoteObject(node.proxyId);
101     SetIpcCallBackNodeDefault(node);
102     return;
103 }
104 
DeInitIpcCallBackList(void)105 void DeInitIpcCallBackList(void)
106 {
107     int32_t i;
108 
109     std::lock_guard<std::mutex> autoLock(g_cbListLock);
110     if (g_ipcCallBackList.ctx == nullptr) {
111         return;
112     }
113     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
114         ResetIpcCallBackNode(g_ipcCallBackList.ctx[i]);
115     }
116     delete[] g_ipcCallBackList.ctx;
117     g_ipcCallBackList.ctx = nullptr;
118     return;
119 }
120 
ResetIpcCallBackNodeByNodeId(int32_t nodeIdx)121 void ResetIpcCallBackNodeByNodeId(int32_t nodeIdx)
122 {
123     LOGI("starting..., index %d", nodeIdx);
124     if ((nodeIdx < 0) || (nodeIdx >= IPC_CALL_BACK_MAX_NODES)) {
125         return;
126     }
127     std::lock_guard<std::mutex> autoLock(g_cbListLock);
128     if (g_ipcCallBackList.ctx == nullptr) {
129         return;
130     }
131     ResetIpcCallBackNode(g_ipcCallBackList.ctx[nodeIdx]);
132     g_ipcCallBackList.nodeCnt--;
133     LOGI("done, index %d", nodeIdx);
134     return;
135 }
136 
GetIpcCallBackByAppId(const char * appId,int32_t type)137 static IpcCallBackNode *GetIpcCallBackByAppId(const char *appId, int32_t type)
138 {
139     int32_t i;
140     int32_t ret;
141 
142     LOGI("appid: %s", appId);
143     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
144         if (g_ipcCallBackList.ctx[i].appId[0] == 0) {
145             continue;
146         }
147         ret = strcmp(g_ipcCallBackList.ctx[i].appId, appId);
148         if ((ret == 0) && (g_ipcCallBackList.ctx[i].cbType == type)) {
149             return &g_ipcCallBackList.ctx[i];
150         }
151     }
152     return nullptr;
153 }
154 
GetFreeIpcCallBackNode(void)155 static IpcCallBackNode *GetFreeIpcCallBackNode(void)
156 {
157     int32_t i;
158 
159     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
160         if ((g_ipcCallBackList.ctx[i].appId[0] == 0) && (g_ipcCallBackList.ctx[i].cbType == 0)) {
161             g_ipcCallBackList.ctx[i].nodeIdx = i;
162             return &g_ipcCallBackList.ctx[i];
163         }
164     }
165     return nullptr;
166 }
167 
SetCbDeathRecipient(int32_t type,int32_t objIdx,int32_t cbDataIdx)168 static void SetCbDeathRecipient(int32_t type, int32_t objIdx, int32_t cbDataIdx)
169 {
170     if ((type == CB_TYPE_DEV_AUTH) || (type == CB_TYPE_LISTENER)) {
171         ServiceDevAuth::SetCbDeathRecipient(objIdx, cbDataIdx);
172     }
173     return;
174 }
175 
AddIpcCbObjByAppId(const char * appId,int32_t objIdx,int32_t type)176 void AddIpcCbObjByAppId(const char *appId, int32_t objIdx, int32_t type)
177 {
178     IpcCallBackNode *node = nullptr;
179 
180     std::lock_guard<std::mutex> autoLock(g_cbListLock);
181     if (g_ipcCallBackList.ctx == nullptr) {
182         LOGE("list not inited");
183         return;
184     }
185 
186     if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
187         LOGE("list is full");
188         return;
189     }
190 
191     node = GetIpcCallBackByAppId(appId, type);
192     if (node != nullptr) {
193         node->proxyId = objIdx;
194         SetCbDeathRecipient(type, objIdx, node->nodeIdx);
195         LOGI("ipc object add success, appid: %s, proxyId %d", appId, node->proxyId);
196     }
197     return;
198 }
199 
AddIpcCallBackByAppId(const char * appId,const uint8_t * cbPtr,int32_t cbSz,int32_t type)200 int32_t AddIpcCallBackByAppId(const char *appId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
201 {
202     IpcCallBackNode *node = nullptr;
203     errno_t eno;
204 
205     std::lock_guard<std::mutex> autoLock(g_cbListLock);
206     if (g_ipcCallBackList.ctx == nullptr) {
207         LOGE("list not inited");
208         return HC_ERROR;
209     }
210 
211     if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
212         LOGE("list is full");
213         return HC_ERROR;
214     }
215 
216     node = GetIpcCallBackByAppId(appId, type);
217     if (node != nullptr) {
218         eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
219         if (eno != EOK) {
220             LOGE("callback context memory copy failed");
221             return HC_ERROR;
222         }
223         if (node->proxyId >= 0) {
224             ServiceDevAuth::ResetRemoteObject(node->proxyId);
225             node->proxyId = -1;
226         }
227         LOGI("callback add success, appid: %s", appId);
228         return HC_SUCCESS;
229     }
230 
231     LOGI("new callback to add, appid: %s", appId);
232     node = GetFreeIpcCallBackNode();
233     if (node == nullptr) {
234         LOGE("get free node failed");
235         return HC_ERROR;
236     }
237     node->cbType = type;
238     eno = memcpy_s(&(node->appId), sizeof(node->appId), appId, strlen(appId) + 1);
239     if (eno != EOK) {
240         ResetIpcCallBackNode(*node);
241         LOGE("appid memory copy failed");
242         return HC_ERROR;
243     }
244     eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
245     if (eno != EOK) {
246         ResetIpcCallBackNode(*node);
247         LOGE("callback context memory copy failed");
248         return HC_ERROR;
249     }
250     node->proxyId = -1;
251     g_ipcCallBackList.nodeCnt++;
252     LOGI("callback add success, appid: %s, type %d", node->appId, node->cbType);
253     return HC_SUCCESS;
254 }
255 
DelIpcCallBackByAppId(const char * appId,int32_t type)256 void DelIpcCallBackByAppId(const char *appId, int32_t type)
257 {
258     IpcCallBackNode *node = nullptr;
259 
260     std::lock_guard<std::mutex> autoLock(g_cbListLock);
261     if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == nullptr)) {
262         return;
263     }
264 
265     node = GetIpcCallBackByAppId(appId, type);
266     if (node != nullptr) {
267         ResetIpcCallBackNode(*node);
268         g_ipcCallBackList.nodeCnt--;
269     }
270     return;
271 }
272 
GetIpcCallBackByReqId(int64_t reqId,int32_t type)273 static IpcCallBackNode *GetIpcCallBackByReqId(int64_t reqId, int32_t type)
274 {
275     int32_t i;
276 
277     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
278         if ((reqId == g_ipcCallBackList.ctx[i].requestId) &&
279             (g_ipcCallBackList.ctx[i].cbType == type)) {
280             return &g_ipcCallBackList.ctx[i];
281         }
282     }
283     return nullptr;
284 }
285 
AddReqIdByAppId(const char * appId,int64_t reqId)286 int32_t AddReqIdByAppId(const char *appId, int64_t reqId)
287 {
288     IpcCallBackNode *node = nullptr;
289 
290     std::lock_guard<std::mutex> autoLock(g_cbListLock);
291     if (g_ipcCallBackList.ctx == nullptr) {
292         LOGE("ipc callback list not inited");
293         return HC_ERROR;
294     }
295 
296     node = GetIpcCallBackByAppId(appId, CB_TYPE_DEV_AUTH);
297     if (node == nullptr) {
298         LOGE("ipc callback node not found, appid: %s", appId);
299         return HC_ERROR;
300     }
301     node->requestId = reqId;
302     node->delOnFni = 0;
303     LOGI("success, appid: %s, requestId: %lld", appId, (long long)reqId);
304     return HC_SUCCESS;
305 }
306 
AddIpcCbObjByReqId(int64_t reqId,int32_t objIdx,int32_t type)307 void AddIpcCbObjByReqId(int64_t reqId, int32_t objIdx, int32_t type)
308 {
309     IpcCallBackNode *node = nullptr;
310 
311     std::lock_guard<std::mutex> autoLock(g_cbListLock);
312     if (g_ipcCallBackList.ctx == nullptr) {
313         LOGE("list not inited");
314         return;
315     }
316 
317     if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
318         LOGE("list is full");
319         return;
320     }
321 
322     node = GetIpcCallBackByReqId(reqId, type);
323     if (node != nullptr) {
324         node->proxyId = objIdx;
325         LOGI("ipc object add success, request id %lld, type %d, proxy id %d",
326             (long long)reqId, type, node->proxyId);
327     }
328     return;
329 }
330 
AddIpcCallBackByReqId(int64_t reqId,const uint8_t * cbPtr,int32_t cbSz,int32_t type)331 int32_t AddIpcCallBackByReqId(int64_t reqId, const uint8_t *cbPtr, int32_t cbSz, int32_t type)
332 {
333     IpcCallBackNode *node = nullptr;
334     errno_t eno;
335 
336     std::lock_guard<std::mutex> autoLock(g_cbListLock);
337     if (g_ipcCallBackList.ctx == nullptr) {
338         LOGE("list is full");
339         return HC_ERROR;
340     }
341 
342     if (g_ipcCallBackList.nodeCnt >= IPC_CALL_BACK_MAX_NODES) {
343         LOGE("list is full");
344         return HC_ERROR;
345     }
346 
347     node = GetIpcCallBackByReqId(reqId, type);
348     if (node != nullptr) {
349         eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
350         if (eno != EOK) {
351             LOGE("callback context memory copy failed");
352             return HC_ERROR;
353         }
354         if (node->proxyId >= 0) {
355             ServiceDevAuth::ResetRemoteObject(node->proxyId);
356             node->proxyId = -1;
357         }
358         LOGI("callback replaced success, request id %lld, type %d", (long long)reqId, type);
359         return HC_SUCCESS;
360     }
361 
362     LOGI("new callback to add, request id %lld, type %d", (long long)reqId, type);
363     node = GetFreeIpcCallBackNode();
364     if (node == nullptr) {
365         LOGE("get free node failed");
366         return HC_ERROR;
367     }
368     node->cbType = type;
369     node->requestId = reqId;
370     eno = memcpy_s(&(node->cbCtx), sizeof(node->cbCtx), cbPtr, cbSz);
371     if (eno != EOK) {
372         ResetIpcCallBackNode(*node);
373         LOGE("callback context memory copy failed");
374         return HC_ERROR;
375     }
376     node->delOnFni = 1;
377     node->proxyId = -1;
378     g_ipcCallBackList.nodeCnt++;
379     LOGI("callback added success, request id %lld, type %d", (long long)reqId, type);
380     return HC_SUCCESS;
381 }
382 
DelCallBackByReqId(int64_t reqId,int32_t type)383 static void DelCallBackByReqId(int64_t reqId, int32_t type)
384 {
385     IpcCallBackNode *node = nullptr;
386 
387     if ((g_ipcCallBackList.nodeCnt <= 0) || (g_ipcCallBackList.ctx == nullptr)) {
388         return;
389     }
390 
391     node = GetIpcCallBackByReqId(reqId, type);
392     if ((node != nullptr) && (node->delOnFni == 1)) {
393         ResetIpcCallBackNode(*node);
394         g_ipcCallBackList.nodeCnt--;
395     }
396     return;
397 }
398 
DelIpcCallBackByReqId(int64_t reqId,int32_t type,bool withLock)399 void DelIpcCallBackByReqId(int64_t reqId, int32_t type, bool withLock)
400 {
401     if (withLock) {
402         std::lock_guard<std::mutex> autoLock(g_cbListLock);
403         DelCallBackByReqId(reqId, type);
404         return;
405     }
406     DelCallBackByReqId(reqId, type);
407     return;
408 }
409 
OnTransmitStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)410 static void OnTransmitStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
411 {
412     int64_t requestId = 0;
413     int32_t inOutLen = 0;
414     uint8_t *data = nullptr;
415     uint32_t dataLen = 0u;
416     bool bRet = false;
417     bool (*onTransmitHook)(int64_t, uint8_t *, uint32_t) = nullptr;
418 
419     onTransmitHook = reinterpret_cast<bool (*)(int64_t, uint8_t *, uint32_t)>(cbHook);
420     inOutLen = sizeof(requestId);
421     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQID,
422         reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
423     (void)GetIpcRequestParamByType(cbDataCache, cacheNum,
424         PARAM_TYPE_COMM_DATA, (uint8_t *)&data, reinterpret_cast<int32_t *>(&dataLen));
425     bRet = onTransmitHook(requestId, data, dataLen);
426     (bRet == true) ? reply.WriteInt32(HC_SUCCESS) : reply.WriteInt32(HC_ERROR);
427     return;
428 }
429 
OnSessKeyStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)430 static void OnSessKeyStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
431 {
432     int64_t requestId = 0;
433     int32_t inOutLen = 0;
434     uint8_t *keyData = nullptr;
435     uint32_t dataLen = 0u;
436     void (*onSessKeyHook)(int64_t, uint8_t *, uint32_t) = nullptr;
437 
438     (void)reply;
439     onSessKeyHook = reinterpret_cast<void (*)(int64_t, uint8_t *, uint32_t)>(cbHook);
440     inOutLen = sizeof(requestId);
441     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQID,
442         reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
443     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_SESS_KEY,
444         reinterpret_cast<uint8_t *>(&keyData), reinterpret_cast<int32_t *>(&dataLen));
445     onSessKeyHook(requestId, keyData, dataLen);
446     return;
447 }
448 
OnFinishStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)449 static void OnFinishStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
450 {
451     int64_t requestId = 0;
452     int32_t opCode = 0;
453     int32_t inOutLen = 0;
454     char *data = nullptr;
455     void (*onFinishHook)(int64_t, int32_t, char *) = nullptr;
456 
457     (void)reply;
458     onFinishHook = reinterpret_cast<void (*)(int64_t, int32_t, char *)>(cbHook);
459     inOutLen = sizeof(requestId);
460     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQID,
461         reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
462     inOutLen = sizeof(opCode);
463     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_OPCODE,
464         reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
465     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_COMM_DATA,
466         reinterpret_cast<uint8_t *>(&data), nullptr);
467     onFinishHook(requestId, opCode, data);
468     return;
469 }
470 
OnErrorStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)471 static void OnErrorStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
472 {
473     int64_t requestId = 0;
474     int32_t opCode = 0;
475     int32_t errCode = 0;
476     int32_t inOutLen = 0;
477     char *errInfo = nullptr;
478     void (*onErrorHook)(int64_t, int32_t, int32_t, char *) = nullptr;
479 
480     onErrorHook = reinterpret_cast<void (*)(int64_t, int32_t, int32_t, char *)>(cbHook);
481     inOutLen = sizeof(requestId);
482     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQID,
483         reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
484     inOutLen = sizeof(opCode);
485     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_OPCODE,
486         reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
487     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_ERRCODE,
488         reinterpret_cast<uint8_t *>(&errCode), &inOutLen);
489     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_ERR_INFO,
490         reinterpret_cast<uint8_t *>(&errInfo), nullptr);
491     onErrorHook(requestId, opCode, errCode, errInfo);
492     return;
493 }
494 
OnRequestStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)495 static void OnRequestStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
496 {
497     int64_t requestId = 0;
498     int32_t opCode = 0;
499     int32_t inOutLen = 0;
500     char *reqParams = nullptr;
501     char *reqResult = nullptr;
502     char *(*onReqHook)(int64_t, int32_t, char *) = nullptr;
503 
504     (void)reply;
505     onReqHook = reinterpret_cast<char *(*)(int64_t, int32_t, char *)>(cbHook);
506     inOutLen = sizeof(requestId);
507     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQID,
508         reinterpret_cast<uint8_t *>(&requestId), &inOutLen);
509     inOutLen = sizeof(opCode);
510     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_OPCODE,
511         reinterpret_cast<uint8_t *>(&opCode), &inOutLen);
512     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_REQ_INFO,
513         reinterpret_cast<uint8_t *>(&reqParams), nullptr);
514     reqResult = onReqHook(requestId, opCode, reqParams);
515     if (reqResult == nullptr) {
516         reply.WriteInt32(HC_ERROR);
517         return;
518     }
519     reply.WriteInt32(HC_SUCCESS);
520     reply.WriteCString(const_cast<const char *>(reqResult));
521     HcFree(reqResult);
522     reqResult = nullptr;
523     return;
524 }
525 
OnGroupCreatedStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)526 static void OnGroupCreatedStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
527 {
528     const char *groupInfo = nullptr;
529     void (*onGroupCreatedHook)(const char *) = nullptr;
530 
531     (void)reply;
532     onGroupCreatedHook = reinterpret_cast<void (*)(const char *)>(cbHook);
533     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_GROUP_INFO,
534         reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
535     onGroupCreatedHook(groupInfo);
536     return;
537 }
538 
OnGroupDeletedStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)539 static void OnGroupDeletedStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
540 {
541     const char *groupInfo = nullptr;
542     void (*onDelGroupHook)(const char *) = nullptr;
543 
544     (void)reply;
545     onDelGroupHook = reinterpret_cast<void (*)(const char *)>(cbHook);
546     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_GROUP_INFO,
547         reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
548     onDelGroupHook(groupInfo);
549     return;
550 }
551 
OnDevBoundStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)552 static void OnDevBoundStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
553 {
554     const char *groupInfo = nullptr;
555     const char *udid = nullptr;
556     void (*onDevBoundHook)(const char *, const char *) = nullptr;
557 
558     (void)reply;
559     onDevBoundHook = reinterpret_cast<void (*)(const char *, const char *)>(cbHook);
560     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_UDID, reinterpret_cast<uint8_t *>(&udid), nullptr);
561     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_GROUP_INFO,
562         reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
563     onDevBoundHook(udid, groupInfo);
564     return;
565 }
566 
OnDevUnboundStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)567 static void OnDevUnboundStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
568 {
569     const char *groupInfo = nullptr;
570     const char *udid = nullptr;
571     void (*onDevUnBoundHook)(const char *, const char *) = nullptr;
572 
573     (void)reply;
574     onDevUnBoundHook = reinterpret_cast<void (*)(const char *, const char *)>(cbHook);
575     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_UDID, reinterpret_cast<uint8_t *>(&udid), nullptr);
576     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_GROUP_INFO,
577         reinterpret_cast<uint8_t *>(&groupInfo), nullptr);
578     onDevUnBoundHook(udid, groupInfo);
579     return;
580 }
581 
OnDevUnTrustStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)582 static void OnDevUnTrustStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
583 {
584     const char *udid = nullptr;
585     void (*onDevUnTrustHook)(const char *) = nullptr;
586 
587     (void)reply;
588     onDevUnTrustHook = reinterpret_cast<void (*)(const char *)>(cbHook);
589     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_UDID, reinterpret_cast<uint8_t *>(&udid), nullptr);
590     onDevUnTrustHook(udid);
591     return;
592 }
593 
OnDelLastGroupStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)594 static void OnDelLastGroupStub(uintptr_t cbHook, const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
595 {
596     const char *udid = nullptr;
597     int32_t groupType = 0;
598     int32_t inOutLen = 0;
599     void (*onDelLastGroupHook)(const char *, int32_t) = nullptr;
600 
601     (void)reply;
602     onDelLastGroupHook = reinterpret_cast<void (*)(const char *, int32_t)>(cbHook);
603     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_UDID, reinterpret_cast<uint8_t *>(&udid), nullptr);
604     inOutLen = sizeof(groupType);
605     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_GROUP_TYPE,
606         reinterpret_cast<uint8_t *>(&groupType), &inOutLen);
607     onDelLastGroupHook(udid, groupType);
608     return;
609 }
610 
OnTrustDevNumChangedStub(uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,MessageParcel & reply)611 static void OnTrustDevNumChangedStub(uintptr_t cbHook,
612     const IpcDataInfo *cbDataCache, int32_t cacheNum, MessageParcel &reply)
613 {
614     int32_t devNum = 0;
615     int32_t inOutLen = 0;
616     void (*onTrustDevNumChangedHook)(int32_t) = nullptr;
617 
618     (void)reply;
619     onTrustDevNumChangedHook = reinterpret_cast<void (*)(int32_t)>(cbHook);
620     inOutLen = sizeof(devNum);
621     (void)GetIpcRequestParamByType(cbDataCache, cacheNum, PARAM_TYPE_DATA_NUM,
622         reinterpret_cast<uint8_t *>(&devNum), &inOutLen);
623     onTrustDevNumChangedHook(devNum);
624     return;
625 }
626 
ProcCbHook(int32_t callbackId,uintptr_t cbHook,const IpcDataInfo * cbDataCache,int32_t cacheNum,uintptr_t replyCtx)627 void ProcCbHook(int32_t callbackId, uintptr_t cbHook,
628     const IpcDataInfo *cbDataCache, int32_t cacheNum, uintptr_t replyCtx)
629 {
630     CallbackStub stubTable[] = {
631         OnTransmitStub, OnSessKeyStub, OnFinishStub, OnErrorStub,
632         OnRequestStub, OnGroupCreatedStub, OnGroupDeletedStub, OnDevBoundStub,
633         OnDevUnboundStub, OnDevUnTrustStub, OnDelLastGroupStub, OnTrustDevNumChangedStub
634     };
635     MessageParcel *reply = nullptr;
636 
637     reply = reinterpret_cast<MessageParcel *>(replyCtx);
638     LOGI("Process call back hook, callback id %d", callbackId);
639     if ((callbackId < CB_ID_ON_TRANS) || (callbackId > CB_ID_ON_TRUST_DEV_NUM_CHANGED)) {
640         LOGE("Invalid call back id");
641         return;
642     }
643     if (cbHook == 0x0) {
644         LOGE("Invalid call back hook");
645         return;
646     }
647     stubTable[callbackId - 1](cbHook, cbDataCache, cacheNum, *reply);
648     LOGI("ProcCbHook done");
649     return;
650 }
651 
EncodeCallData(MessageParcel & dataParcel,int32_t type,const uint8_t * param,int32_t paramSz)652 static uint32_t EncodeCallData(MessageParcel &dataParcel, int32_t type, const uint8_t *param, int32_t paramSz)
653 {
654     const uint8_t *paramTmp = nullptr;
655     int32_t zeroVal = 0;
656 
657     paramTmp = param;
658     if ((param == nullptr) || (paramSz == 0)) {
659         paramTmp = reinterpret_cast<const uint8_t *>(&zeroVal);
660         paramSz = sizeof(zeroVal);
661     }
662     if (dataParcel.WriteInt32(type) && dataParcel.WriteInt32(paramSz) &&
663         dataParcel.WriteBuffer(reinterpret_cast<const void *>(paramTmp), static_cast<size_t>(paramSz))) {
664         return static_cast<uint32_t>(HC_SUCCESS);
665     }
666     return static_cast<uint32_t>(HC_ERROR);
667 }
668 
669 /* group auth callback adapter */
GaCbOnTransmitWithType(int64_t requestId,const uint8_t * data,uint32_t dataLen,int32_t type)670 static bool GaCbOnTransmitWithType(int64_t requestId, const uint8_t *data, uint32_t dataLen, int32_t type)
671 {
672     int32_t ret = -1;
673     uint32_t uRet;
674     MessageParcel dataParcel;
675     MessageParcel reply;
676     IpcCallBackNode *node = nullptr;
677 
678     LOGI("starting ... request id: %lld, type %d", (long long)requestId, type);
679     std::lock_guard<std::mutex> autoLock(g_cbListLock);
680     node = GetIpcCallBackByReqId(requestId, type);
681     if (node == nullptr) {
682         LOGE("onTransmit hook is null, request id %lld", (long long)requestId);
683         return false;
684     }
685     uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID,
686         reinterpret_cast<const uint8_t *>(&requestId), sizeof(requestId));
687     uRet |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA, data, dataLen);
688     if (uRet != HC_SUCCESS) {
689         LOGE("build trans data failed");
690         return false;
691     }
692     ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_TRANS, true,
693         reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onTransmit), dataParcel, reply);
694     LOGI("process done, request id: %lld", (long long)requestId);
695     if (reply.ReadInt32(ret) && (ret == HC_SUCCESS)) {
696         return true;
697     }
698     return false;
699 }
700 
IpcGaCbOnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)701 static bool IpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
702 {
703     return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_DEV_AUTH);
704 }
705 
TmpIpcGaCbOnTransmit(int64_t requestId,const uint8_t * data,uint32_t dataLen)706 static bool TmpIpcGaCbOnTransmit(int64_t requestId, const uint8_t *data, uint32_t dataLen)
707 {
708     return GaCbOnTransmitWithType(requestId, data, dataLen, CB_TYPE_TMP_DEV_AUTH);
709 }
710 
GaCbOnSessionKeyRetWithType(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen,int32_t type)711 static void GaCbOnSessionKeyRetWithType(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen, int32_t type)
712 {
713     uint32_t ret;
714     MessageParcel dataParcel;
715     MessageParcel reply;
716     IpcCallBackNode *node = nullptr;
717 
718     LOGI("starting ... request id: %lld, type %d", (long long)requestId, type);
719     std::lock_guard<std::mutex> autoLock(g_cbListLock);
720     node = GetIpcCallBackByReqId(requestId, type);
721     if (node == nullptr) {
722         LOGE("onSessionKeyReturned hook is null, request id %lld", (long long)requestId);
723         return;
724     }
725 
726     ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
727     ret |= EncodeCallData(dataParcel, PARAM_TYPE_SESS_KEY, sessKey, sessKeyLen);
728     if (ret != HC_SUCCESS) {
729         LOGE("build trans data failed");
730         return;
731     }
732     ServiceDevAuth::ActCallback(node->proxyId, CB_ID_SESS_KEY_DONE, false,
733         reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onSessionKeyReturned), dataParcel, reply);
734     LOGI("process done, request id: %lld", (long long)requestId);
735     return;
736 }
737 
IpcGaCbOnSessionKeyReturned(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen)738 static void IpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
739 {
740     GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_DEV_AUTH);
741     return;
742 }
743 
TmpIpcGaCbOnSessionKeyReturned(int64_t requestId,const uint8_t * sessKey,uint32_t sessKeyLen)744 static void TmpIpcGaCbOnSessionKeyReturned(int64_t requestId, const uint8_t *sessKey, uint32_t sessKeyLen)
745 {
746     GaCbOnSessionKeyRetWithType(requestId, sessKey, sessKeyLen, CB_TYPE_TMP_DEV_AUTH);
747     return;
748 }
749 
GaCbOnFinishWithType(int64_t requestId,int32_t operationCode,const char * returnData,int32_t type)750 static void GaCbOnFinishWithType(int64_t requestId, int32_t operationCode, const char *returnData, int32_t type)
751 {
752     uint32_t ret;
753     MessageParcel dataParcel;
754     MessageParcel reply;
755     IpcCallBackNode *node = nullptr;
756 
757     LOGI("starting ... request id: %lld, type %d", (long long)requestId, type);
758     std::lock_guard<std::mutex> autoLock(g_cbListLock);
759     node = GetIpcCallBackByReqId(requestId, type);
760     if (node == nullptr) {
761         LOGE("onFinish hook is null, request id %lld", (long long)requestId);
762         return;
763     }
764     ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
765     ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
766         reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
767     ret |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA,
768         reinterpret_cast<const uint8_t *>(returnData), strlen(returnData) + 1);
769     if (ret != HC_SUCCESS) {
770         LOGE("build trans data failed");
771         return;
772     }
773     ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_FINISH, false,
774         reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onFinish), dataParcel, reply);
775     /* delete request id */
776     DelIpcCallBackByReqId(requestId, type, false);
777     LOGI("process done, request id: %lld", (long long)requestId);
778     return;
779 }
780 
IpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)781 static void IpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
782 {
783     GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_DEV_AUTH);
784     return;
785 }
786 
TmpIpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)787 static void TmpIpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
788 {
789     GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_TMP_DEV_AUTH);
790     return;
791 }
792 
GaCbOnErrorWithType(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn,int32_t type)793 static void GaCbOnErrorWithType(int64_t requestId, int32_t operationCode,
794     int32_t errorCode, const char *errorReturn, int32_t type)
795 {
796     uint32_t ret;
797     MessageParcel dataParcel;
798     MessageParcel reply;
799     IpcCallBackNode *node = nullptr;
800 
801     LOGI("starting ... request id: %lld, type %d", (long long)requestId, type);
802     std::lock_guard<std::mutex> autoLock(g_cbListLock);
803     node = GetIpcCallBackByReqId(requestId, type);
804     if (node == nullptr) {
805         LOGE("onError hook is null, request id %lld", (long long)requestId);
806         return;
807     }
808     ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
809     ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
810         reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
811     ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERRCODE, reinterpret_cast<uint8_t *>(&errorCode), sizeof(errorCode));
812     if (errorReturn != nullptr) {
813         ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERR_INFO,
814             reinterpret_cast<const uint8_t *>(errorReturn), strlen(errorReturn) + 1);
815     }
816     if (ret != HC_SUCCESS) {
817         LOGE("build trans data failed");
818         return;
819     }
820     ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_ERROR, false,
821         reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onError), dataParcel, reply);
822     /* delete request id */
823     DelIpcCallBackByReqId(requestId, type, false);
824     LOGI("process done, request id: %lld", (long long)requestId);
825     return;
826 }
827 
IpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)828 static void IpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
829 {
830     GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_DEV_AUTH);
831     return;
832 }
833 
TmpIpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)834 static void TmpIpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
835 {
836     GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_TMP_DEV_AUTH);
837     return;
838 }
839 
GaCbOnRequestWithType(int64_t requestId,int32_t operationCode,const char * reqParams,int32_t type)840 static char *GaCbOnRequestWithType(int64_t requestId, int32_t operationCode, const char *reqParams, int32_t type)
841 {
842     int32_t ret = -1;
843     uint32_t uRet;
844     MessageParcel dataParcel;
845     MessageParcel reply;
846     const char *dPtr = nullptr;
847     IpcCallBackNode *node = nullptr;
848 
849     LOGI("starting ... request id: %lld, type %d", (long long)requestId, type);
850     std::lock_guard<std::mutex> autoLock(g_cbListLock);
851     node = GetIpcCallBackByReqId(requestId, type);
852     if (node == nullptr) {
853         LOGE("onRequest hook is null, request id %lld", (long long)requestId);
854         return nullptr;
855     }
856 
857     uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID, reinterpret_cast<uint8_t *>(&requestId), sizeof(requestId));
858     uRet |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE,
859         reinterpret_cast<uint8_t *>(&operationCode), sizeof(operationCode));
860     if (reqParams != nullptr) {
861         uRet |= EncodeCallData(dataParcel, PARAM_TYPE_REQ_INFO,
862             reinterpret_cast<const uint8_t *>(reqParams), strlen(reqParams) + 1);
863     }
864     if (uRet != HC_SUCCESS) {
865         LOGE("build trans data failed");
866         return nullptr;
867     }
868 
869     ServiceDevAuth::ActCallback(node->proxyId, CB_ID_ON_REQUEST, true,
870         reinterpret_cast<uintptr_t>(node->cbCtx.devAuth.onRequest), dataParcel, reply);
871     if (reply.ReadInt32(ret) && (ret == HC_SUCCESS)) {
872         if (reply.GetReadableBytes() == 0) {
873             LOGE("onRequest has no data, but success");
874             return nullptr;
875         }
876         dPtr = reply.ReadCString();
877         LOGI("process done, request id: %lld, %s string",
878              (long long)requestId, (dPtr != nullptr) ? "valid" : "invalid");
879         return (dPtr != nullptr) ? strdup(dPtr) : nullptr;
880     }
881     return nullptr;
882 }
883 
CanFindCbByReqId(int64_t requestId)884 static bool CanFindCbByReqId(int64_t requestId)
885 {
886     std::lock_guard<std::mutex> autoLock(g_cbListLock);
887     IpcCallBackNode *node = GetIpcCallBackByReqId(requestId, CB_TYPE_DEV_AUTH);
888     return (node != NULL) ? true : false;
889 }
890 
IpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)891 static char *IpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
892 {
893     if (!CanFindCbByReqId(requestId)) {
894         CJson *reqParamsJson = CreateJsonFromString(reqParams);
895         if (reqParamsJson == NULL) {
896             LOGE("failed to create json from string!");
897             return NULL;
898         }
899         const char *callerAppId = GetStringFromJson(reqParamsJson, FIELD_APP_ID);
900         if (callerAppId == NULL) {
901             LOGE("failed to get appId from json object!");
902             FreeJson(reqParamsJson);
903             return NULL;
904         }
905         int32_t ret = AddReqIdByAppId(callerAppId, requestId);
906         FreeJson(reqParamsJson);
907         if (ret != HC_SUCCESS) {
908             return NULL;
909         }
910     }
911     return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_DEV_AUTH);
912 }
913 
TmpIpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)914 static char *TmpIpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
915 {
916     return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_TMP_DEV_AUTH);
917 }
918 
IpcOnGroupCreated(const char * groupInfo)919 void IpcOnGroupCreated(const char *groupInfo)
920 {
921     int32_t i;
922     uint32_t ret;
923     MessageParcel dataParcel;
924     MessageParcel reply;
925     DataChangeListener *listener = nullptr;
926 
927     std::lock_guard<std::mutex> autoLock(g_cbListLock);
928     if (g_ipcCallBackList.ctx == nullptr) {
929         LOGE("IpcCallBackList un-initialized");
930         return;
931     }
932 
933     if (groupInfo == nullptr) {
934         LOGE("IpcOnGroupCreated, params error");
935         return;
936     }
937 
938     ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
939         reinterpret_cast<const uint8_t *>(groupInfo), strlen(groupInfo) + 1);
940     if (ret != HC_SUCCESS) {
941         LOGE("IpcGaCbOnRequest, build trans data failed");
942         return;
943     }
944 
945     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
946         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
947             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
948             if (listener->onGroupCreated == nullptr) {
949                 LOGE("onGroupCreated hook is invalid");
950                 continue;
951             }
952             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_CREATED,
953                 false, reinterpret_cast<uintptr_t>(listener->onGroupCreated), dataParcel, reply);
954         }
955     }
956     return;
957 }
958 
IpcOnGroupDeleted(const char * groupInfo)959 void IpcOnGroupDeleted(const char *groupInfo)
960 {
961     int32_t i;
962     uint32_t ret;
963     MessageParcel dataParcel;
964     MessageParcel reply;
965     DataChangeListener *listener = nullptr;
966 
967     std::lock_guard<std::mutex> autoLock(g_cbListLock);
968     if (g_ipcCallBackList.ctx == nullptr) {
969         LOGE("IpcCallBackList un-initialized");
970         return;
971     }
972 
973     if (groupInfo == nullptr) {
974         LOGE("IpcOnGroupDeleted, params error");
975         return;
976     }
977 
978     ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
979         reinterpret_cast<const uint8_t *>(groupInfo), strlen(groupInfo) + 1);
980     if (ret != HC_SUCCESS) {
981         LOGE("IpcGaCbOnRequest, build trans data failed");
982         return;
983     }
984 
985     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
986         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
987             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
988             if (listener->onGroupDeleted == nullptr) {
989                 LOGE("onGroupDeleted hook is null");
990                 continue;
991             }
992             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_DELETED,
993                 false, reinterpret_cast<uintptr_t>(listener->onGroupDeleted), dataParcel, reply);
994         }
995     }
996     return;
997 }
998 
IpcOnDeviceBound(const char * peerUdid,const char * groupInfo)999 void IpcOnDeviceBound(const char *peerUdid, const char *groupInfo)
1000 {
1001     int32_t i;
1002     uint32_t ret;
1003     MessageParcel dataParcel;
1004     MessageParcel reply;
1005     DataChangeListener *listener = nullptr;
1006 
1007     std::lock_guard<std::mutex> autoLock(g_cbListLock);
1008     if (g_ipcCallBackList.ctx == nullptr) {
1009         LOGE("IpcCallBackList un-initialized");
1010         return;
1011     }
1012 
1013     if ((peerUdid == nullptr) || (groupInfo == nullptr)) {
1014         LOGE("params error");
1015         return;
1016     }
1017 
1018     ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1019         reinterpret_cast<const uint8_t *>(peerUdid), strlen(peerUdid) + 1);
1020     ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1021         reinterpret_cast<const uint8_t *>(groupInfo), strlen(groupInfo) + 1);
1022     if (ret != HC_SUCCESS) {
1023         LOGE("build trans data failed");
1024         return;
1025     }
1026 
1027     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1028         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1029             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1030             if (listener->onDeviceBound == nullptr) {
1031                 LOGE("onDeviceBound hook is null");
1032                 continue;
1033             }
1034             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_BOUND,
1035                 false, reinterpret_cast<uintptr_t>(listener->onDeviceBound), dataParcel, reply);
1036         }
1037     }
1038     return;
1039 }
1040 
IpcOnDeviceUnBound(const char * peerUdid,const char * groupInfo)1041 void IpcOnDeviceUnBound(const char *peerUdid, const char *groupInfo)
1042 {
1043     int32_t i;
1044     uint32_t ret;
1045     MessageParcel dataParcel;
1046     MessageParcel reply;
1047     DataChangeListener *listener = nullptr;
1048 
1049     std::lock_guard<std::mutex> autoLock(g_cbListLock);
1050     if (g_ipcCallBackList.ctx == nullptr) {
1051         LOGE("IpcCallBackList un-initialized");
1052         return;
1053     }
1054 
1055     if ((peerUdid == nullptr) || (groupInfo == nullptr)) {
1056         LOGE("params error");
1057         return;
1058     }
1059 
1060     ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1061         reinterpret_cast<const uint8_t *>(peerUdid), strlen(peerUdid) + 1);
1062     ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO,
1063         reinterpret_cast<const uint8_t *>(groupInfo), strlen(groupInfo) + 1);
1064     if (ret != HC_SUCCESS) {
1065         LOGE("build trans data failed");
1066         return;
1067     }
1068 
1069     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1070         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1071             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1072             if (listener->onDeviceUnBound == nullptr) {
1073                 LOGE("onDeviceUnBound hook is null");
1074                 continue;
1075             }
1076             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNBOUND,
1077                 false, reinterpret_cast<uintptr_t>(listener->onDeviceUnBound), dataParcel, reply);
1078         }
1079     }
1080     return;
1081 }
1082 
IpcOnDeviceNotTrusted(const char * peerUdid)1083 void IpcOnDeviceNotTrusted(const char *peerUdid)
1084 {
1085     int32_t i;
1086     uint32_t ret;
1087     MessageParcel dataParcel;
1088     MessageParcel reply;
1089     DataChangeListener *listener = nullptr;
1090 
1091     std::lock_guard<std::mutex> autoLock(g_cbListLock);
1092     if (g_ipcCallBackList.ctx == nullptr) {
1093         LOGE("IpcCallBackList un-initialized");
1094         return;
1095     }
1096 
1097     if (peerUdid == nullptr) {
1098         LOGE("params error");
1099         return;
1100     }
1101 
1102     ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1103         reinterpret_cast<const uint8_t *>(peerUdid), strlen(peerUdid) + 1);
1104     if (ret != HC_SUCCESS) {
1105         LOGE("build trans data failed");
1106         return;
1107     }
1108 
1109     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1110         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1111             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1112             if (listener->onDeviceNotTrusted == nullptr) {
1113                 LOGE("onDeviceNotTrusted hook is null");
1114                 continue;
1115             }
1116             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNTRUSTED,
1117                 false, reinterpret_cast<uintptr_t>(listener->onDeviceNotTrusted), dataParcel, reply);
1118         }
1119     }
1120     return;
1121 }
1122 
IpcOnLastGroupDeleted(const char * peerUdid,int32_t groupType)1123 void IpcOnLastGroupDeleted(const char *peerUdid, int32_t groupType)
1124 {
1125     int32_t i;
1126     uint32_t ret;
1127     MessageParcel dataParcel;
1128     MessageParcel reply;
1129     DataChangeListener *listener = nullptr;
1130 
1131     std::lock_guard<std::mutex> autoLock(g_cbListLock);
1132     if (g_ipcCallBackList.ctx == nullptr) {
1133         LOGE("IpcCallBackList un-initialized");
1134         return;
1135     }
1136 
1137     if (peerUdid == nullptr) {
1138         LOGE("params error");
1139         return;
1140     }
1141 
1142     ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID,
1143         reinterpret_cast<const uint8_t *>(peerUdid), strlen(peerUdid) + 1);
1144     ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_TYPE,
1145         reinterpret_cast<const uint8_t *>(&groupType), sizeof(groupType));
1146     if (ret != HC_SUCCESS) {
1147         LOGE("build trans data failed");
1148         return;
1149     }
1150 
1151     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1152         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1153             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1154             if (listener->onLastGroupDeleted == nullptr) {
1155                 LOGE("onLastGroupDeleted hook is null");
1156                 continue;
1157             }
1158             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_LAST_GROUP_DELETED,
1159                 false, reinterpret_cast<uintptr_t>(listener->onLastGroupDeleted), dataParcel, reply);
1160         }
1161     }
1162     return;
1163 }
1164 
IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)1165 void IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)
1166 {
1167     int32_t i;
1168     uint32_t ret;
1169     MessageParcel dataParcel;
1170     MessageParcel reply;
1171     DataChangeListener *listener = nullptr;
1172 
1173     std::lock_guard<std::mutex> autoLock(g_cbListLock);
1174     if (g_ipcCallBackList.ctx == nullptr) {
1175         LOGE("IpcCallBackList un-initialized");
1176         return;
1177     }
1178 
1179     ret = EncodeCallData(dataParcel, PARAM_TYPE_DATA_NUM,
1180         reinterpret_cast<const uint8_t *>(&curTrustedDeviceNum), sizeof(curTrustedDeviceNum));
1181     if (ret != HC_SUCCESS) {
1182         LOGE("IpcOnTrustedDeviceNumChanged, build trans data failed");
1183         return;
1184     }
1185 
1186     for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1187         if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1188             listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1189             if (listener->onTrustedDeviceNumChanged == nullptr) {
1190                 LOGE("onTrustedDeviceNumChanged hook is null");
1191                 continue;
1192             }
1193             ServiceDevAuth::ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_TRUST_DEV_NUM_CHANGED,
1194                 false, reinterpret_cast<uintptr_t>(listener->onTrustedDeviceNumChanged), dataParcel, reply);
1195         }
1196     }
1197     return;
1198 }
1199 
InitDeviceAuthCbCtx(DeviceAuthCallback * ctx,int32_t type)1200 void InitDeviceAuthCbCtx(DeviceAuthCallback *ctx, int32_t type)
1201 {
1202     if (ctx == nullptr) {
1203         return;
1204     }
1205     if (type == CB_TYPE_DEV_AUTH) {
1206         ctx->onTransmit = IpcGaCbOnTransmit;
1207         ctx->onSessionKeyReturned = IpcGaCbOnSessionKeyReturned;
1208         ctx->onFinish = IpcGaCbOnFinish;
1209         ctx->onError = IpcGaCbOnError;
1210         ctx->onRequest = IpcGaCbOnRequest;
1211     }
1212     if (type == CB_TYPE_TMP_DEV_AUTH) {
1213         ctx->onTransmit = TmpIpcGaCbOnTransmit;
1214         ctx->onSessionKeyReturned = TmpIpcGaCbOnSessionKeyReturned;
1215         ctx->onFinish = TmpIpcGaCbOnFinish;
1216         ctx->onError = TmpIpcGaCbOnError;
1217         ctx->onRequest = TmpIpcGaCbOnRequest;
1218     }
1219     return;
1220 }
1221 
InitDevAuthListenerCbCtx(DataChangeListener * ctx)1222 void InitDevAuthListenerCbCtx(DataChangeListener *ctx)
1223 {
1224     if (ctx == nullptr) {
1225         return;
1226     }
1227     ctx->onGroupCreated = IpcOnGroupCreated;
1228     ctx->onGroupDeleted = IpcOnGroupDeleted;
1229     ctx->onDeviceBound = IpcOnDeviceBound;
1230     ctx->onDeviceUnBound = IpcOnDeviceUnBound;
1231     ctx->onDeviceNotTrusted = IpcOnDeviceNotTrusted;
1232     ctx->onLastGroupDeleted = IpcOnLastGroupDeleted;
1233     ctx->onTrustedDeviceNumChanged = IpcOnTrustedDeviceNumChanged;
1234     return;
1235 }
1236 
1237 /* ipc client process adapter */
CreateCallCtx(uintptr_t * callCtx,uintptr_t * cbCtx)1238 int32_t CreateCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
1239 {
1240     ProxyDevAuthData *dataCache = nullptr;
1241 
1242     (void)cbCtx;
1243     if (callCtx == nullptr) {
1244         return HC_ERR_INVALID_PARAMS;
1245     }
1246 
1247     dataCache = new(std::nothrow) ProxyDevAuthData();
1248     if (dataCache == nullptr) {
1249         LOGE("call context alloc failed");
1250         return HC_ERR_ALLOC_MEMORY;
1251     }
1252     *callCtx = reinterpret_cast<uintptr_t>(dataCache);
1253     return HC_SUCCESS;
1254 }
1255 
DestroyCallCtx(uintptr_t * callCtx,uintptr_t * cbCtx)1256 void DestroyCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
1257 {
1258     ProxyDevAuthData *dataCache = nullptr;
1259 
1260     (void)cbCtx;
1261     if ((callCtx != nullptr) && (*callCtx != 0)) {
1262         dataCache = reinterpret_cast<ProxyDevAuthData *>(*callCtx);
1263         delete dataCache;
1264         *callCtx = 0;
1265     }
1266     return;
1267 }
1268 
SetCbCtxToDataCtx(uintptr_t callCtx,int32_t cbIdx)1269 void SetCbCtxToDataCtx(uintptr_t callCtx, int32_t cbIdx)
1270 {
1271     ProxyDevAuthData *dataCache = nullptr;
1272     sptr<IRemoteObject> remote = g_sdkCbStub[cbIdx];
1273     dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1274     dataCache->SetCallbackStub(remote);
1275     return;
1276 }
1277 
SetCallRequestParamInfo(uintptr_t callCtx,int32_t type,const uint8_t * param,int32_t paramSz)1278 int32_t SetCallRequestParamInfo(uintptr_t callCtx, int32_t type, const uint8_t *param, int32_t paramSz)
1279 {
1280     ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1281 
1282     return dataCache->EncodeCallRequest(type, param, paramSz);
1283 }
1284 
DoBinderCall(uintptr_t callCtx,int32_t methodId,bool withSync)1285 int32_t DoBinderCall(uintptr_t callCtx, int32_t methodId, bool withSync)
1286 {
1287     int32_t ret;
1288     ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1289 
1290     LOGI("proc method %d", methodId);
1291     ret = dataCache->FinalCallRequest(methodId);
1292     if (ret != HC_SUCCESS) {
1293         return ret;
1294     }
1295     return dataCache->ActCall(withSync);
1296 }
1297 
1298 /* ipc service process adapter */
SetIpcCallMap(uintptr_t ipcInstance,IpcServiceCall method,int32_t methodId)1299 uint32_t SetIpcCallMap(uintptr_t ipcInstance, IpcServiceCall method, int32_t methodId)
1300 {
1301     sptr<ServiceDevAuth> service = nullptr;
1302     if ((method == nullptr) || (methodId <= 0)) {
1303         return static_cast<uint32_t>(HC_ERR_INVALID_PARAMS);
1304     }
1305 
1306     service = reinterpret_cast<ServiceDevAuth *>(ipcInstance);
1307     return static_cast<uint32_t>(service->SetCallMap(method, methodId));
1308 }
1309 
CreateServiceInstance(uintptr_t * ipcInstance)1310 int32_t CreateServiceInstance(uintptr_t *ipcInstance)
1311 {
1312     ServiceDevAuth *service = nullptr;
1313     service = new(std::nothrow) ServiceDevAuth();
1314     if (service == nullptr) {
1315         return HC_ERR_ALLOC_MEMORY;
1316     }
1317     *ipcInstance = reinterpret_cast<uintptr_t>(service);
1318     return HC_SUCCESS;
1319 }
1320 
DestroyServiceInstance(uintptr_t * ipcInstance)1321 void DestroyServiceInstance(uintptr_t *ipcInstance)
1322 {
1323     sptr<ServiceDevAuth> service = nullptr;
1324     if (ipcInstance == nullptr) {
1325         return;
1326     }
1327     service = reinterpret_cast<ServiceDevAuth *>(*ipcInstance);
1328     if (service == nullptr) {
1329         return;
1330     }
1331     delete service;
1332     *ipcInstance = 0x0;
1333     return;
1334 }
1335 
AddDevAuthServiceToManager(uintptr_t * serviceCtx)1336 int32_t AddDevAuthServiceToManager(uintptr_t *serviceCtx)
1337 {
1338     int32_t ret = ERR_OK;
1339     ServiceDevAuth *sPtr = nullptr;
1340 
1341     sptr<ISystemAbilityManager> sysMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1342     if (sysMgr == nullptr) {
1343         return HC_ERR_IPC_GET_SERVICE;
1344     }
1345     sPtr = new(std::nothrow) ServiceDevAuth();
1346     if (sPtr == nullptr) {
1347         return HC_ERR_ALLOC_MEMORY;
1348     }
1349     ret = sysMgr->AddSystemAbility(DEVICE_AUTH_SERVICE_ID, sPtr);
1350     if (ret != ERR_OK) {
1351         LOGE("add service failed");
1352         delete sPtr;
1353         return HC_ERROR;
1354     }
1355     *serviceCtx = reinterpret_cast<uintptr_t>(sPtr);
1356     LOGI("AddSystemAbility to SA manager success");
1357     return HC_SUCCESS;
1358 }
1359 
IpcEncodeCallReplay(uintptr_t replayCache,int32_t type,const uint8_t * result,int32_t resultSz)1360 int32_t IpcEncodeCallReplay(uintptr_t replayCache, int32_t type, const uint8_t *result, int32_t resultSz)
1361 {
1362     int32_t errCnt = 0;
1363     MessageParcel *replyParcel = nullptr;
1364     unsigned long valZero = 0ul;
1365 
1366     replyParcel = reinterpret_cast<MessageParcel *>(replayCache);
1367     errCnt += replyParcel->WriteInt32(type) ? 0 : 1;
1368     errCnt += replyParcel->WriteInt32(resultSz) ? 0 : 1;
1369     if ((result != nullptr) && (resultSz > 0)) {
1370         errCnt += replyParcel->WriteBuffer(
1371             reinterpret_cast<const void *>(result), static_cast<size_t>(resultSz)) ? 0 : 1;
1372     } else {
1373         errCnt += replyParcel->WriteBuffer(
1374             reinterpret_cast<const void *>(&valZero), sizeof(unsigned long)) ? 0 : 1;
1375     }
1376     LOGI("reply type %d, %s", type, (errCnt == 0) ? "success" : "failed");
1377     return (errCnt == 0) ? HC_SUCCESS : HC_ERROR;
1378 }
1379 
DecodeIpcData(uintptr_t data,int32_t * type,uint8_t ** val,int32_t * valSz)1380 int32_t DecodeIpcData(uintptr_t data, int32_t *type, uint8_t **val, int32_t *valSz)
1381 {
1382     MessageParcel *dataPtr = nullptr;
1383 
1384     dataPtr = reinterpret_cast<MessageParcel *>(data);
1385     if (dataPtr->GetReadableBytes() == 0) {
1386         return HC_SUCCESS;
1387     }
1388     if (sizeof(int32_t) > dataPtr->GetReadableBytes()) {
1389         return HC_ERR_IPC_BAD_MESSAGE_LENGTH;
1390     }
1391     *type = dataPtr->ReadInt32();
1392     *valSz = dataPtr->ReadInt32();
1393     if (*valSz > static_cast<int32_t>(dataPtr->GetReadableBytes())) {
1394         return HC_ERR_IPC_BAD_VAL_LENGTH;
1395     }
1396     *val = const_cast<uint8_t *>(dataPtr->ReadUnpadBuffer(*valSz));
1397     return HC_SUCCESS;
1398 }
1399 
DecodeCallReply(uintptr_t callCtx,IpcDataInfo * replyCache,int32_t cacheNum)1400 void DecodeCallReply(uintptr_t callCtx, IpcDataInfo *replyCache, int32_t cacheNum)
1401 {
1402     int32_t dataLen = 0;
1403     int32_t i;
1404     int32_t ret;
1405 
1406     ProxyDevAuthData *dataCache = reinterpret_cast<ProxyDevAuthData *>(callCtx);
1407     MessageParcel *tmpParcel = dataCache->GetReplyParcel();
1408     dataLen = tmpParcel->ReadInt32();
1409     if ((dataLen <= 0) || (dataLen != static_cast<int32_t>(tmpParcel->GetReadableBytes()))) {
1410         LOGE("decode failed, data length %d", dataLen);
1411         return;
1412     }
1413 
1414     for (i = 0; i < cacheNum; i++) {
1415         ret = DecodeIpcData(reinterpret_cast<uintptr_t>(tmpParcel),
1416             &(replyCache[i].type), &(replyCache[i].val), &(replyCache[i].valSz));
1417         if (ret != HC_SUCCESS) {
1418             return;
1419         }
1420         LOGI("decode success, type %d", replyCache[i].type);
1421     }
1422     return;
1423 }
1424 
IsTypeForSettingPtr(int32_t type)1425 static bool IsTypeForSettingPtr(int32_t type)
1426 {
1427     int32_t typeList[] = {
1428         PARAM_TYPE_APPID, PARAM_TYPE_DEV_AUTH_CB, PARAM_TYPE_LISTERNER, PARAM_TYPE_CREATE_PARAMS,
1429         PARAM_TYPE_GROUPID, PARAM_TYPE_UDID, PARAM_TYPE_ADD_PARAMS, PARAM_TYPE_DEL_PARAMS,
1430         PARAM_TYPE_BIND, PARAM_TYPE_UNBIND, PARAM_TYPE_CREDENTIAL, PARAM_TYPE_MGR_APPID,
1431         PARAM_TYPE_FRIEND_APPID, PARAM_TYPE_QUERY_PARAMS, PARAM_TYPE_COMM_DATA, PARAM_TYPE_REQ_CFM,
1432         PARAM_TYPE_SESS_KEY, PARAM_TYPE_REQ_INFO, PARAM_TYPE_GROUP_INFO, PARAM_TYPE_AUTH_PARAMS
1433     };
1434     int32_t i;
1435     int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1436     for (i = 0; i < n; i++) {
1437         if (typeList[i] == type) {
1438             return true;
1439         }
1440     }
1441     return false;
1442 }
1443 
IsTypeForCpyData(int32_t type)1444 static bool IsTypeForCpyData(int32_t type)
1445 {
1446     int32_t typeList[] = {
1447         PARAM_TYPE_REQID, PARAM_TYPE_GROUP_TYPE, PARAM_TYPE_OPCODE, PARAM_TYPE_ERRCODE, PARAM_TYPE_OS_ACCOUNT_ID
1448     };
1449     int32_t i;
1450     int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1451     for (i = 0; i < n; i++) {
1452         if (typeList[i] == type) {
1453             return true;
1454         }
1455     }
1456     return false;
1457 }
1458 
GetIpcRequestParamByType(const IpcDataInfo * ipcParams,int32_t paramNum,int32_t type,uint8_t * paramCache,int32_t * cacheLen)1459 int32_t GetIpcRequestParamByType(const IpcDataInfo *ipcParams, int32_t paramNum,
1460     int32_t type, uint8_t *paramCache, int32_t *cacheLen)
1461 {
1462     int32_t i;
1463     int32_t ret = HC_ERR_IPC_BAD_MSG_TYPE;
1464     errno_t eno;
1465 
1466     for (i = 0; i < paramNum; i++) {
1467         if (ipcParams[i].type != type) {
1468             continue;
1469         }
1470         ret = HC_SUCCESS;
1471         if (IsTypeForSettingPtr(type)) {
1472             *(uint8_t **)paramCache = ipcParams[i].val;
1473             if (cacheLen != NULL) {
1474                 *cacheLen = ipcParams[i].valSz;
1475             }
1476             break;
1477         }
1478         if (IsTypeForCpyData(type)) {
1479             if ((ipcParams[i].val == nullptr) || (ipcParams[i].valSz <= 0)) {
1480                 ret = HC_ERR_INVALID_PARAMS;
1481                 break;
1482             }
1483             eno = memcpy_s(paramCache, *cacheLen, ipcParams[i].val, ipcParams[i].valSz);
1484             if (eno != EOK) {
1485                 ret = HC_ERR_MEMORY_COPY;
1486             }
1487             *cacheLen = ipcParams[i].valSz;
1488             break;
1489         }
1490         if ((type == PARAM_TYPE_CB_OBJECT) && (*(uint32_t *)cacheLen >= sizeof(ipcParams[i].idx))) {
1491             *(int32_t *)paramCache = ipcParams[i].idx;
1492         }
1493         break;
1494     }
1495     LOGI("type %d, result 0x%x", type, ret);
1496     return ret;
1497 }
1498 
IsCallbackMethod(int32_t methodId)1499 bool IsCallbackMethod(int32_t methodId)
1500 {
1501     if ((methodId == IPC_CALL_ID_REG_CB) || (methodId == IPC_CALL_ID_REG_LISTENER) ||
1502         (methodId == IPC_CALL_ID_GA_PROC_DATA) || (methodId == IPC_CALL_ID_AUTH_DEVICE)) {
1503         return true;
1504     }
1505     return false;
1506 }
1507 
IsServiceRunning(void)1508 bool IsServiceRunning(void)
1509 {
1510     LOGI("service activity check");
1511     return ProxyDevAuth::ServiceRunning();
1512 }
1513 
UnInitProxyAdapt(void)1514 void UnInitProxyAdapt(void)
1515 {
1516     g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] = nullptr;
1517     g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID] = nullptr;
1518     return;
1519 }
1520 
InitProxyAdapt(void)1521 int32_t InitProxyAdapt(void)
1522 {
1523     g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] = new(std::nothrow) StubDevAuthCb;
1524     g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID] = new(std::nothrow) StubDevAuthCb;
1525     if (!g_sdkCbStub[IPC_CALL_BACK_STUB_AUTH_ID] || !g_sdkCbStub[IPC_CALL_BACK_STUB_BIND_ID]) {
1526         LOGE("alloc callback stub object failed");
1527         UnInitProxyAdapt();
1528         return HC_ERR_ALLOC_MEMORY;
1529     }
1530     LOGI("init callback stub object success");
1531     return HC_SUCCESS;
1532 }
1533