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