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