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