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