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 ret |= EncodeCallData(dataParcel, PARAM_TYPE_COMM_DATA, (const uint8_t *)(returnData), strlen(returnData) + 1);
804 if (ret != HC_SUCCESS) {
805 UnLockCallbackList();
806 HcFree((void *)dataParcel);
807 LOGE("build trans data failed");
808 return;
809 }
810 ActCallback(node->proxyId, CB_ID_ON_FINISH, (uintptr_t)(node->cbCtx.devAuth.onFinish), dataParcel, NULL);
811 /* delete request id */
812 DelIpcCallBackByReqId(requestId, type, false);
813 UnLockCallbackList();
814 HcFree((void *)dataParcel);
815 LOGI("process done, request id: %lld", requestId);
816 return;
817 }
818
IpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)819 static void IpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
820 {
821 GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_DEV_AUTH);
822 return;
823 }
824
TmpIpcGaCbOnFinish(int64_t requestId,int32_t operationCode,const char * returnData)825 static void TmpIpcGaCbOnFinish(int64_t requestId, int32_t operationCode, const char *returnData)
826 {
827 GaCbOnFinishWithType(requestId, operationCode, returnData, CB_TYPE_TMP_DEV_AUTH);
828 return;
829 }
830
GaCbOnErrorWithType(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn,int32_t type)831 static void GaCbOnErrorWithType(int64_t requestId, int32_t operationCode,
832 int32_t errorCode, const char *errorReturn, int32_t type)
833 {
834 uint32_t ret;
835 IpcIo *dataParcel = NULL;
836 IpcCallBackNode *node = NULL;
837
838 LOGI("starting ... request id: %lld, type %d", requestId, type);
839 LockCallbackList();
840 node = GetIpcCallBackByReqId(requestId, type);
841 if (node == NULL) {
842 UnLockCallbackList();
843 LOGE("onError hook is null, request id %lld", requestId);
844 return;
845 }
846 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
847 if (dataParcel == NULL) {
848 UnLockCallbackList();
849 return;
850 }
851 ret = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
852 ret |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE, (uint8_t *)(&operationCode), sizeof(operationCode));
853 ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERRCODE, (uint8_t *)(&errorCode), sizeof(errorCode));
854 if (errorReturn != NULL) {
855 ret |= EncodeCallData(dataParcel, PARAM_TYPE_ERR_INFO, (const uint8_t *)(errorReturn), strlen(errorReturn) + 1);
856 }
857 if (ret != HC_SUCCESS) {
858 UnLockCallbackList();
859 HcFree((void *)dataParcel);
860 LOGE("build trans data failed");
861 return;
862 }
863 ActCallback(node->proxyId, CB_ID_ON_ERROR, (uintptr_t)(node->cbCtx.devAuth.onError), dataParcel, NULL);
864 /* delete request id */
865 DelIpcCallBackByReqId(requestId, type, false);
866 UnLockCallbackList();
867 HcFree((void *)dataParcel);
868 LOGI("process done, request id: %lld", requestId);
869 return;
870 }
871
IpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)872 static void IpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
873 {
874 GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_DEV_AUTH);
875 return;
876 }
877
TmpIpcGaCbOnError(int64_t requestId,int32_t operationCode,int32_t errorCode,const char * errorReturn)878 static void TmpIpcGaCbOnError(int64_t requestId, int32_t operationCode, int32_t errorCode, const char *errorReturn)
879 {
880 GaCbOnErrorWithType(requestId, operationCode, errorCode, errorReturn, CB_TYPE_TMP_DEV_AUTH);
881 return;
882 }
883
GaCbOnRequestWithType(int64_t requestId,int32_t operationCode,const char * reqParams,int32_t type)884 static char *GaCbOnRequestWithType(int64_t requestId, int32_t operationCode, const char *reqParams, int32_t type)
885 {
886 int32_t ret;
887 uint32_t uRet;
888 IpcIo *dataParcel = NULL;
889 IpcIo reply;
890 uint8_t dataBuf[IPC_STACK_BUFF_SZ] = { 0 };
891 const char *dPtr = NULL;
892 IpcCallBackNode *node = NULL;
893
894 LOGI("starting ... request id: %lld, type %d", requestId, type);
895 IpcIoInit(&reply, (void *)dataBuf, sizeof(dataBuf), 0);
896 LockCallbackList();
897 node = GetIpcCallBackByReqId(requestId, type);
898 if (node == NULL) {
899 UnLockCallbackList();
900 LOGE("onRequest hook is null, request id %lld", requestId);
901 return NULL;
902 }
903 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
904 if (dataParcel == NULL) {
905 UnLockCallbackList();
906 return NULL;
907 }
908 uRet = EncodeCallData(dataParcel, PARAM_TYPE_REQID, (uint8_t *)(&requestId), sizeof(requestId));
909 uRet |= EncodeCallData(dataParcel, PARAM_TYPE_OPCODE, (uint8_t *)(&operationCode), sizeof(operationCode));
910 if (reqParams != NULL) {
911 uRet |= EncodeCallData(dataParcel, PARAM_TYPE_REQ_INFO, (const uint8_t *)(reqParams), strlen(reqParams) + 1);
912 }
913 if (uRet != HC_SUCCESS) {
914 UnLockCallbackList();
915 HcFree((void *)dataParcel);
916 LOGE("build trans data failed");
917 return NULL;
918 }
919
920 ActCallback(node->proxyId, CB_ID_ON_REQUEST, (uintptr_t)(node->cbCtx.devAuth.onRequest), dataParcel, &reply);
921 UnLockCallbackList();
922 HcFree((void *)dataParcel);
923 ReadInt32(&reply, &ret);
924 if (ret == HC_SUCCESS) {
925 dPtr = (const char *)ReadString(&reply, NULL);
926 }
927 LOGI("process done, request id: %lld, %s result", requestId, (dPtr != NULL) ? "valid" : "invalid");
928 return (dPtr != NULL) ? strdup(dPtr) : NULL;
929 }
930
CanFindCbByReqId(int64_t requestId)931 static bool CanFindCbByReqId(int64_t requestId)
932 {
933 LockCallbackList();
934 IpcCallBackNode *node = GetIpcCallBackByReqId(requestId, CB_TYPE_DEV_AUTH);
935 UnLockCallbackList();
936 return (node != NULL) ? true : false;
937 }
938
IpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)939 static char *IpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
940 {
941 if (!CanFindCbByReqId(requestId)) {
942 CJson *reqParamsJson = CreateJsonFromString(reqParams);
943 if (reqParamsJson == NULL) {
944 LOGE("failed to create json from string!");
945 return NULL;
946 }
947 const char *callerAppId = GetStringFromJson(reqParamsJson, FIELD_APP_ID);
948 if (callerAppId == NULL) {
949 LOGE("failed to get appId from json object!");
950 FreeJson(reqParamsJson);
951 return NULL;
952 }
953 int32_t ret = AddReqIdByAppId(callerAppId, requestId);
954 FreeJson(reqParamsJson);
955 if (ret != HC_SUCCESS) {
956 return NULL;
957 }
958 }
959 return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_DEV_AUTH);
960 }
961
TmpIpcGaCbOnRequest(int64_t requestId,int32_t operationCode,const char * reqParams)962 static char *TmpIpcGaCbOnRequest(int64_t requestId, int32_t operationCode, const char *reqParams)
963 {
964 return GaCbOnRequestWithType(requestId, operationCode, reqParams, CB_TYPE_TMP_DEV_AUTH);
965 }
966
IpcOnGroupCreated(const char * groupInfo)967 void IpcOnGroupCreated(const char *groupInfo)
968 {
969 int32_t i;
970 uint32_t ret;
971 IpcIo *dataParcel = NULL;
972 DataChangeListener *listener = NULL;
973
974 if (groupInfo == NULL) {
975 LOGE("IpcOnGroupCreated, params error");
976 return;
977 }
978
979 LockCallbackList();
980 if (g_ipcCallBackList.ctx == NULL) {
981 UnLockCallbackList();
982 LOGE("IpcCallBackList un-initialized");
983 return;
984 }
985 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
986 if (dataParcel == NULL) {
987 UnLockCallbackList();
988 return;
989 }
990
991 ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), strlen(groupInfo) + 1);
992 if (ret != HC_SUCCESS) {
993 UnLockCallbackList();
994 HcFree((void *)dataParcel);
995 LOGE("IpcGaCbOnRequest, build trans data failed");
996 return;
997 }
998
999 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1000 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1001 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1002 if (listener->onGroupCreated == NULL) {
1003 continue;
1004 }
1005 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_CREATED,
1006 (uintptr_t)(listener->onGroupCreated), dataParcel, NULL);
1007 }
1008 }
1009 UnLockCallbackList();
1010 HcFree((void *)dataParcel);
1011 return;
1012 }
1013
IpcOnGroupDeleted(const char * groupInfo)1014 void IpcOnGroupDeleted(const char *groupInfo)
1015 {
1016 int32_t i;
1017 uint32_t ret;
1018 IpcIo *dataParcel = NULL;
1019 DataChangeListener *listener = NULL;
1020
1021 if (groupInfo == NULL) {
1022 LOGE("IpcOnGroupDeleted, params error");
1023 return;
1024 }
1025
1026 LockCallbackList();
1027 if (g_ipcCallBackList.ctx == NULL) {
1028 UnLockCallbackList();
1029 LOGE("IpcCallBackList un-initialized");
1030 return;
1031 }
1032 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1033 if (dataParcel == NULL) {
1034 UnLockCallbackList();
1035 return;
1036 }
1037
1038 ret = EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), strlen(groupInfo) + 1);
1039 if (ret != HC_SUCCESS) {
1040 UnLockCallbackList();
1041 HcFree((void *)dataParcel);
1042 LOGE("IpcGaCbOnRequest, build trans data failed");
1043 return;
1044 }
1045
1046 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1047 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1048 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1049 if (listener->onGroupDeleted == NULL) {
1050 continue;
1051 }
1052 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_GROUP_DELETED,
1053 (uintptr_t)(listener->onGroupDeleted), dataParcel, NULL);
1054 }
1055 }
1056 UnLockCallbackList();
1057 HcFree((void *)dataParcel);
1058 return;
1059 }
1060
IpcOnDeviceBound(const char * peerUdid,const char * groupInfo)1061 void IpcOnDeviceBound(const char *peerUdid, const char *groupInfo)
1062 {
1063 int32_t i;
1064 uint32_t ret;
1065 IpcIo *dataParcel = NULL;
1066 DataChangeListener *listener = NULL;
1067
1068 if ((peerUdid == NULL) || (groupInfo == NULL)) {
1069 LOGE("params error");
1070 return;
1071 }
1072
1073 LockCallbackList();
1074 if (g_ipcCallBackList.ctx == NULL) {
1075 UnLockCallbackList();
1076 LOGE("IpcCallBackList un-initialized");
1077 return;
1078 }
1079 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1080 if (dataParcel == NULL) {
1081 UnLockCallbackList();
1082 return;
1083 }
1084
1085 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), strlen(peerUdid) + 1);
1086 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), strlen(groupInfo) + 1);
1087 if (ret != HC_SUCCESS) {
1088 UnLockCallbackList();
1089 HcFree((void *)dataParcel);
1090 LOGE("build trans data failed");
1091 return;
1092 }
1093
1094 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1095 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1096 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1097 if (listener->onDeviceBound == NULL) {
1098 continue;
1099 }
1100 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_BOUND,
1101 (uintptr_t)(listener->onDeviceBound), dataParcel, NULL);
1102 }
1103 }
1104 UnLockCallbackList();
1105 HcFree((void *)dataParcel);
1106 return;
1107 }
1108
IpcOnDeviceUnBound(const char * peerUdid,const char * groupInfo)1109 void IpcOnDeviceUnBound(const char *peerUdid, const char *groupInfo)
1110 {
1111 int32_t i;
1112 uint32_t ret;
1113 IpcIo *dataParcel = NULL;
1114 DataChangeListener *listener = NULL;
1115
1116 if ((peerUdid == NULL) || (groupInfo == NULL)) {
1117 LOGE("params error");
1118 return;
1119 }
1120
1121 LockCallbackList();
1122 if (g_ipcCallBackList.ctx == NULL) {
1123 UnLockCallbackList();
1124 LOGE("IpcCallBackList un-initialized");
1125 return;
1126 }
1127 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1128 if (dataParcel == NULL) {
1129 UnLockCallbackList();
1130 return;
1131 }
1132
1133 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), strlen(peerUdid) + 1);
1134 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_INFO, (const uint8_t *)(groupInfo), strlen(groupInfo) + 1);
1135 if (ret != HC_SUCCESS) {
1136 UnLockCallbackList();
1137 HcFree((void *)dataParcel);
1138 LOGE("build trans data failed");
1139 return;
1140 }
1141
1142 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1143 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1144 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1145 if (listener->onDeviceUnBound == NULL) {
1146 continue;
1147 }
1148 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNBOUND,
1149 (uintptr_t)(listener->onDeviceUnBound), dataParcel, NULL);
1150 }
1151 }
1152 UnLockCallbackList();
1153 HcFree((void *)dataParcel);
1154 return;
1155 }
1156
IpcOnDeviceNotTrusted(const char * peerUdid)1157 void IpcOnDeviceNotTrusted(const char *peerUdid)
1158 {
1159 int32_t i;
1160 uint32_t ret;
1161 IpcIo *dataParcel = NULL;
1162 DataChangeListener *listener = NULL;
1163
1164 if (peerUdid == NULL) {
1165 LOGE("params error");
1166 return;
1167 }
1168
1169 LockCallbackList();
1170 if (g_ipcCallBackList.ctx == NULL) {
1171 UnLockCallbackList();
1172 LOGE("IpcCallBackList un-initialized");
1173 return;
1174 }
1175 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1176 if (dataParcel == NULL) {
1177 UnLockCallbackList();
1178 return;
1179 }
1180
1181 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), strlen(peerUdid) + 1);
1182 if (ret != HC_SUCCESS) {
1183 UnLockCallbackList();
1184 HcFree((void *)dataParcel);
1185 LOGE("build trans data failed");
1186 return;
1187 }
1188
1189 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1190 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1191 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1192 if (listener->onDeviceNotTrusted == NULL) {
1193 continue;
1194 }
1195 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_DEV_UNTRUSTED,
1196 (uintptr_t)(listener->onDeviceNotTrusted), dataParcel, NULL);
1197 }
1198 }
1199 UnLockCallbackList();
1200 HcFree((void *)dataParcel);
1201 return;
1202 }
1203
IpcOnLastGroupDeleted(const char * peerUdid,int32_t groupType)1204 void IpcOnLastGroupDeleted(const char *peerUdid, int32_t groupType)
1205 {
1206 int32_t i;
1207 uint32_t ret;
1208 IpcIo *dataParcel = NULL;
1209 DataChangeListener *listener = NULL;
1210
1211 if (peerUdid == NULL) {
1212 LOGE("params error");
1213 return;
1214 }
1215
1216 LockCallbackList();
1217 if (g_ipcCallBackList.ctx == NULL) {
1218 UnLockCallbackList();
1219 LOGE("IpcCallBackList un-initialized");
1220 return;
1221 }
1222 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1223 if (dataParcel == NULL) {
1224 UnLockCallbackList();
1225 return;
1226 }
1227
1228 ret = EncodeCallData(dataParcel, PARAM_TYPE_UDID, (const uint8_t *)(peerUdid), strlen(peerUdid) + 1);
1229 ret |= EncodeCallData(dataParcel, PARAM_TYPE_GROUP_TYPE, (const uint8_t *)(&groupType), sizeof(groupType));
1230 if (ret != HC_SUCCESS) {
1231 UnLockCallbackList();
1232 HcFree((void *)dataParcel);
1233 LOGE("build trans data failed");
1234 return;
1235 }
1236
1237 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1238 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1239 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1240 if (listener->onLastGroupDeleted == NULL) {
1241 continue;
1242 }
1243 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_LAST_GROUP_DELETED,
1244 (uintptr_t)(listener->onLastGroupDeleted), dataParcel, NULL);
1245 }
1246 }
1247 UnLockCallbackList();
1248 HcFree((void *)dataParcel);
1249 return;
1250 }
1251
IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)1252 void IpcOnTrustedDeviceNumChanged(int32_t curTrustedDeviceNum)
1253 {
1254 int32_t i;
1255 uint32_t ret;
1256 IpcIo *dataParcel = NULL;
1257 DataChangeListener *listener = NULL;
1258
1259 LockCallbackList();
1260 if (g_ipcCallBackList.ctx == NULL) {
1261 UnLockCallbackList();
1262 LOGE("IpcCallBackList un-initialized");
1263 return;
1264 }
1265
1266 dataParcel = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1267 if (dataParcel == NULL) {
1268 UnLockCallbackList();
1269 return;
1270 }
1271 ret = EncodeCallData(dataParcel, PARAM_TYPE_DATA_NUM,
1272 (const uint8_t *)(&curTrustedDeviceNum), sizeof(curTrustedDeviceNum));
1273 if (ret != HC_SUCCESS) {
1274 UnLockCallbackList();
1275 HcFree((void *)dataParcel);
1276 LOGE("IpcOnTrustedDeviceNumChanged, build trans data failed");
1277 return;
1278 }
1279
1280 for (i = 0; i < IPC_CALL_BACK_MAX_NODES; i++) {
1281 if (g_ipcCallBackList.ctx[i].cbType == CB_TYPE_LISTENER) {
1282 listener = &(g_ipcCallBackList.ctx[i].cbCtx.listener);
1283 if (listener->onTrustedDeviceNumChanged == NULL) {
1284 continue;
1285 }
1286 ActCallback(g_ipcCallBackList.ctx[i].proxyId, CB_ID_ON_TRUST_DEV_NUM_CHANGED,
1287 (uintptr_t)(listener->onTrustedDeviceNumChanged), dataParcel, NULL);
1288 }
1289 }
1290 UnLockCallbackList();
1291 HcFree((void *)dataParcel);
1292 return;
1293 }
1294
InitDeviceAuthCbCtx(DeviceAuthCallback * ctx,int32_t type)1295 void InitDeviceAuthCbCtx(DeviceAuthCallback *ctx, int32_t type)
1296 {
1297 if (ctx == NULL) {
1298 return;
1299 }
1300 if (type == CB_TYPE_DEV_AUTH) {
1301 ctx->onTransmit = IpcGaCbOnTransmit;
1302 ctx->onSessionKeyReturned = IpcGaCbOnSessionKeyReturned;
1303 ctx->onFinish = IpcGaCbOnFinish;
1304 ctx->onError = IpcGaCbOnError;
1305 ctx->onRequest = IpcGaCbOnRequest;
1306 }
1307 if (type == CB_TYPE_TMP_DEV_AUTH) {
1308 ctx->onTransmit = TmpIpcGaCbOnTransmit;
1309 ctx->onSessionKeyReturned = TmpIpcGaCbOnSessionKeyReturned;
1310 ctx->onFinish = TmpIpcGaCbOnFinish;
1311 ctx->onError = TmpIpcGaCbOnError;
1312 ctx->onRequest = TmpIpcGaCbOnRequest;
1313 }
1314 return;
1315 }
1316
InitDevAuthListenerCbCtx(DataChangeListener * ctx)1317 void InitDevAuthListenerCbCtx(DataChangeListener *ctx)
1318 {
1319 if (ctx == NULL) {
1320 return;
1321 }
1322 ctx->onGroupCreated = IpcOnGroupCreated;
1323 ctx->onGroupDeleted = IpcOnGroupDeleted;
1324 ctx->onDeviceBound = IpcOnDeviceBound;
1325 ctx->onDeviceUnBound = IpcOnDeviceUnBound;
1326 ctx->onDeviceNotTrusted = IpcOnDeviceNotTrusted;
1327 ctx->onLastGroupDeleted = IpcOnLastGroupDeleted;
1328 ctx->onTrustedDeviceNumChanged = IpcOnTrustedDeviceNumChanged;
1329 return;
1330 }
1331
1332 /* ipc client process adapter */
CreateCallCtx(uintptr_t * callCtx,uintptr_t * cbCtx)1333 int32_t CreateCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
1334 {
1335 ProxyDevAuthData *dataCache = NULL;
1336
1337 (void)cbCtx;
1338 if (callCtx == NULL) {
1339 return HC_ERR_INVALID_PARAMS;
1340 }
1341
1342 dataCache = (ProxyDevAuthData *)HcMalloc(sizeof(ProxyDevAuthData), 0);
1343 if (dataCache == NULL) {
1344 LOGE("call context alloc failed");
1345 return HC_ERR_ALLOC_MEMORY;
1346 }
1347 dataCache->data = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1348 dataCache->tmpData = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1349 dataCache->reply = InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
1350 if ((dataCache->data == NULL) || (dataCache->tmpData == NULL) || (dataCache->reply == NULL)) {
1351 DestroyCallCtx((uintptr_t *)(dataCache), NULL);
1352 return HC_ERROR;
1353 }
1354 /* linux lite, ipc io init with : token + SvcIdentity */
1355 dataCache->ioBuffOffset = IpcIoBufferOffset();
1356 *callCtx = (uintptr_t)(dataCache);
1357 return HC_SUCCESS;
1358 }
1359
DestroyCallCtx(uintptr_t * callCtx,uintptr_t * cbCtx)1360 void DestroyCallCtx(uintptr_t *callCtx, uintptr_t *cbCtx)
1361 {
1362 ProxyDevAuthData *dataCache = NULL;
1363
1364 (void)cbCtx;
1365 if ((callCtx != NULL) && (*callCtx != 0)) {
1366 dataCache = (ProxyDevAuthData *)(*callCtx);
1367 if (dataCache->data != NULL) {
1368 HcFree((void *)dataCache->data);
1369 dataCache->data = NULL;
1370 }
1371 if (dataCache->tmpData != NULL) {
1372 HcFree((void *)dataCache->tmpData);
1373 dataCache->tmpData = NULL;
1374 }
1375 if (dataCache->reply != NULL) {
1376 HcFree((void *)dataCache->reply);
1377 dataCache->reply = NULL;
1378 }
1379 HcFree((void *)dataCache);
1380 *callCtx = 0;
1381 }
1382 return;
1383 }
1384
SetCbCtxToDataCtx(uintptr_t callCtx,int32_t cbIdx)1385 void SetCbCtxToDataCtx(uintptr_t callCtx, int32_t cbIdx)
1386 {
1387 ProxyDevAuthData *dataCache = NULL;
1388 const SvcIdentity *stubInfo = &g_sdkCbStub.stubIdentity;
1389 (void)cbIdx;
1390 if (!g_sdkCbStub.registered) {
1391 LOGW("SDK callback stub un-registered");
1392 return;
1393 }
1394 ShowIpcSvcInfo(stubInfo);
1395 dataCache = (ProxyDevAuthData *)(callCtx);
1396 SetCallbackStub(dataCache, stubInfo);
1397 return;
1398 }
1399
SetCallRequestParamInfo(uintptr_t callCtx,int32_t type,const uint8_t * param,int32_t paramSz)1400 int32_t SetCallRequestParamInfo(uintptr_t callCtx, int32_t type, const uint8_t *param, int32_t paramSz)
1401 {
1402 ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
1403
1404 return EncodeCallRequest(dataCache, type, param, paramSz);
1405 }
1406
DoBinderCall(uintptr_t callCtx,int32_t methodId,bool withSync)1407 int32_t DoBinderCall(uintptr_t callCtx, int32_t methodId, bool withSync)
1408 {
1409 int32_t ret;
1410 ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
1411
1412 LOGI("proc method %d", methodId);
1413 ret = FinalCallRequest(dataCache, methodId);
1414 if (ret != HC_SUCCESS) {
1415 return ret;
1416 }
1417 return ActCall(g_proxyInstance, dataCache);
1418 }
1419
1420 /* ipc service process adapter */
SetIpcCallMap(uintptr_t ipcInstance,IpcServiceCall method,int32_t methodId)1421 uint32_t SetIpcCallMap(uintptr_t ipcInstance, IpcServiceCall method, int32_t methodId)
1422 {
1423 if ((method == NULL) || (methodId <= 0)) {
1424 return HC_ERR_INVALID_PARAMS;
1425 }
1426
1427 return (uint32_t)SetCallMap(method, methodId);
1428 }
1429
CreateServiceInstance(uintptr_t * ipcInstance)1430 int32_t CreateServiceInstance(uintptr_t *ipcInstance)
1431 {
1432 *ipcInstance = 0x0;
1433 return HC_SUCCESS;
1434 }
1435
DestroyServiceInstance(uintptr_t * ipcInstance)1436 void DestroyServiceInstance(uintptr_t *ipcInstance)
1437 {
1438 *ipcInstance = 0x0;
1439 return;
1440 }
1441
AddDevAuthServiceToManager(uintptr_t * serviceCtx)1442 int32_t AddDevAuthServiceToManager(uintptr_t *serviceCtx)
1443 {
1444 (void)CreateServiceInstance(serviceCtx);
1445 SAMGR_Bootstrap();
1446 InitCbStubTable();
1447 LOGI("AddSystemAbility to SA manager success");
1448 return HC_SUCCESS;
1449 }
1450
IpcEncodeCallReplay(uintptr_t replayCache,int32_t type,const uint8_t * result,int32_t resultSz)1451 int32_t IpcEncodeCallReplay(uintptr_t replayCache, int32_t type, const uint8_t *result, int32_t resultSz)
1452 {
1453 int32_t ret = HC_SUCCESS;
1454 IpcIo *replyParcel = NULL;
1455 unsigned long valZero = 0ul;
1456
1457 replyParcel = (IpcIo *)(replayCache);
1458 WriteInt32(replyParcel, type);
1459 bool value;
1460 if ((result != NULL) && (resultSz > 0)) {
1461 WriteUint32(replyParcel, (uint32_t)resultSz);
1462 value = WriteBuffer(replyParcel, (const void *)result, (uint32_t)resultSz);
1463 } else {
1464 WriteUint32(replyParcel, sizeof(valZero));
1465 value = WriteBuffer(replyParcel, (const void *)(&valZero), sizeof(valZero));
1466 }
1467 if (!value) {
1468 return HC_FALSE;
1469 }
1470 LOGI("reply type %d, %s", type, (ret == HC_SUCCESS) ? "success" : "failed");
1471 return ret;
1472 }
1473
DecodeIpcData(uintptr_t data,int32_t * type,uint8_t ** val,int32_t * valSz)1474 int32_t DecodeIpcData(uintptr_t data, int32_t *type, uint8_t **val, int32_t *valSz)
1475 {
1476 IpcIo *dataPtr = NULL;
1477
1478 dataPtr = (IpcIo *)(data);
1479 if (dataPtr->bufferLeft <= 0) {
1480 return HC_SUCCESS;
1481 }
1482 ReadInt32(dataPtr, type);
1483 ReadUint32(dataPtr, (uint32_t *)valSz);
1484 *val = (uint8_t *)ReadBuffer(dataPtr, *valSz);
1485 return HC_SUCCESS;
1486 }
1487
DecodeCallReply(uintptr_t callCtx,IpcDataInfo * replyCache,int32_t cacheNum)1488 void DecodeCallReply(uintptr_t callCtx, IpcDataInfo *replyCache, int32_t cacheNum)
1489 {
1490 int32_t i;
1491 int32_t ret;
1492 uint32_t replyLen;
1493
1494 ProxyDevAuthData *dataCache = (ProxyDevAuthData *)(callCtx);
1495 ReadUint32(dataCache->reply, &replyLen);
1496 LOGI("to decode data length %u", replyLen);
1497 if (replyLen == 0) {
1498 return;
1499 }
1500
1501 for (i = 0; i < cacheNum; i++) {
1502 ret = DecodeIpcData((uintptr_t)(dataCache->reply),
1503 &(replyCache[i].type), &(replyCache[i].val), &(replyCache[i].valSz));
1504 if (ret != HC_SUCCESS) {
1505 return;
1506 }
1507 LOGI("decode success, type %d", replyCache[i].type);
1508 }
1509 return;
1510 }
1511
IsTypeForSettingPtr(int32_t type)1512 static bool IsTypeForSettingPtr(int32_t type)
1513 {
1514 int32_t typeList[] = {
1515 PARAM_TYPE_APPID, PARAM_TYPE_DEV_AUTH_CB, PARAM_TYPE_LISTERNER, PARAM_TYPE_CREATE_PARAMS,
1516 PARAM_TYPE_GROUPID, PARAM_TYPE_UDID, PARAM_TYPE_ADD_PARAMS, PARAM_TYPE_DEL_PARAMS,
1517 PARAM_TYPE_BIND, PARAM_TYPE_UNBIND, PARAM_TYPE_MGR_APPID, PARAM_TYPE_FRIEND_APPID,
1518 PARAM_TYPE_QUERY_PARAMS, PARAM_TYPE_COMM_DATA, PARAM_TYPE_REQ_CFM, PARAM_TYPE_SESS_KEY,
1519 PARAM_TYPE_REQ_INFO, PARAM_TYPE_GROUP_INFO, PARAM_TYPE_AUTH_PARAMS, PARAM_TYPE_REQ_JSON
1520 };
1521 int32_t i;
1522 int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1523 for (i = 0; i < n; i++) {
1524 if (typeList[i] == type) {
1525 return true;
1526 }
1527 }
1528 return false;
1529 }
1530
IsTypeForCpyData(int32_t type)1531 static bool IsTypeForCpyData(int32_t type)
1532 {
1533 int32_t typeList[] = {
1534 PARAM_TYPE_REQID, PARAM_TYPE_GROUP_TYPE, PARAM_TYPE_OPCODE, PARAM_TYPE_ERRCODE, PARAM_TYPE_OS_ACCOUNT_ID
1535 };
1536 int32_t i;
1537 int32_t n = sizeof(typeList) / sizeof(typeList[0]);
1538 for (i = 0; i < n; i++) {
1539 if (typeList[i] == type) {
1540 return true;
1541 }
1542 }
1543 return false;
1544 }
1545
GetIpcRequestParamByType(const IpcDataInfo * ipcParams,int32_t paramNum,int32_t type,uint8_t * paramCache,int32_t * cacheLen)1546 int32_t GetIpcRequestParamByType(const IpcDataInfo *ipcParams, int32_t paramNum,
1547 int32_t type, uint8_t *paramCache, int32_t *cacheLen)
1548 {
1549 int32_t i;
1550 int32_t ret = HC_ERR_IPC_BAD_MSG_TYPE;
1551 errno_t eno;
1552
1553 for (i = 0; i < paramNum; i++) {
1554 if (ipcParams[i].type != type) {
1555 continue;
1556 }
1557 ret = HC_SUCCESS;
1558 if (IsTypeForSettingPtr(type)) {
1559 *(uint8_t **)paramCache = ipcParams[i].val;
1560 if (cacheLen != NULL) {
1561 *cacheLen = ipcParams[i].valSz;
1562 }
1563 break;
1564 }
1565 if (IsTypeForCpyData(type)) {
1566 if ((ipcParams[i].val == NULL) || (ipcParams[i].valSz <= 0)) {
1567 ret = HC_ERR_INVALID_PARAMS;
1568 break;
1569 }
1570 eno = memcpy_s(paramCache, *cacheLen, ipcParams[i].val, ipcParams[i].valSz);
1571 if (eno != EOK) {
1572 ret = HC_ERR_MEMORY_COPY;
1573 }
1574 *cacheLen = ipcParams[i].valSz;
1575 break;
1576 }
1577 if ((type == PARAM_TYPE_CB_OBJECT) && (*(uint32_t *)cacheLen >= sizeof(ipcParams[i].idx))) {
1578 *(int32_t *)paramCache = ipcParams[i].idx;
1579 }
1580 break;
1581 }
1582 LOGI("type %d, result 0x%x", type, ret);
1583 return ret;
1584 }
1585
IsCallbackMethod(int32_t methodId)1586 bool IsCallbackMethod(int32_t methodId)
1587 {
1588 if ((methodId == IPC_CALL_ID_REG_CB) || (methodId == IPC_CALL_ID_REG_LISTENER) ||
1589 (methodId == IPC_CALL_ID_GA_PROC_DATA) || (methodId == IPC_CALL_ID_AUTH_DEVICE)) {
1590 return true;
1591 }
1592 return false;
1593 }
1594
IsServiceRunning(void)1595 bool IsServiceRunning(void)
1596 {
1597 LOGI("service activity check");
1598 return g_proxyInstance != NULL ? true : false;
1599 }
1600
InitIpcDataCache(uint32_t buffSz)1601 IpcIo *InitIpcDataCache(uint32_t buffSz)
1602 {
1603 IpcIo *ioPtr = NULL;
1604 uint8_t *buf = NULL;
1605 uint32_t len;
1606
1607 if (buffSz == 0) {
1608 LOGE("invalid param");
1609 return NULL;
1610 }
1611 len = sizeof(IpcIo) + buffSz;
1612 ioPtr = (IpcIo *)HcMalloc(len, 0);
1613 if (ioPtr == NULL) {
1614 LOGE("alloc memory failed");
1615 return NULL;
1616 }
1617 buf = (uint8_t *)ioPtr + sizeof(IpcIo);
1618 /* ipcio inited with 4 svc objects */
1619 IpcIoInit(ioPtr, (void *)buf, buffSz, 4);
1620 return ioPtr;
1621 }
1622
GetIpcIoDataLength(const IpcIo * io)1623 int32_t GetIpcIoDataLength(const IpcIo *io)
1624 {
1625 uintptr_t beginPos;
1626 uintptr_t endPos;
1627
1628 if (io == NULL) {
1629 return 0;
1630 }
1631 beginPos = (uintptr_t)(io->bufferBase + IpcIoBufferOffset());
1632 endPos = (uintptr_t)(io->bufferCur);
1633 return (endPos <= beginPos) ? 0 : (int32_t)(endPos - beginPos);
1634 }
1635
ShowIpcSvcInfo(const SvcIdentity * svc)1636 void ShowIpcSvcInfo(const SvcIdentity *svc)
1637 {
1638 LOGI("svc information - handle(%u), token(%u), cookie(%u)",
1639 svc->handle, svc->token, svc->cookie);
1640 }
1641
InitProxyAdapt(void)1642 int32_t InitProxyAdapt(void)
1643 {
1644 if (g_proxyInstance == NULL) {
1645 g_proxyInstance = (IClientProxy *)GetProxyInstance(DEV_AUTH_SERVICE_NAME);
1646 if (g_proxyInstance == NULL) {
1647 LOGE("get proxy instance failed");
1648 return HC_ERR_IPC_INIT;
1649 }
1650 LOGI("get proxy instance success");
1651 }
1652
1653 if (!g_sdkCbStub.registered) {
1654 g_objectStub.func = CbStubOnRemoteRequest;
1655 g_objectStub.args = NULL;
1656 g_objectStub.isRemote = false;
1657 g_sdkCbStub.stubIdentity.handle = IPC_INVALID_HANDLE;
1658 g_sdkCbStub.stubIdentity.token = SERVICE_TYPE_ANONYMOUS;
1659 g_sdkCbStub.stubIdentity.cookie = (uintptr_t)&g_objectStub;
1660
1661 ShowIpcSvcInfo(&(g_sdkCbStub.stubIdentity));
1662 LOGI("register ipc cb success");
1663 g_sdkCbStub.registered = true;
1664 }
1665 return HC_SUCCESS;
1666 }
1667
UnInitProxyAdapt(void)1668 void UnInitProxyAdapt(void)
1669 {
1670 g_proxyInstance = NULL;
1671 g_sdkCbStub.registered = false;
1672 return;
1673 }
1674
IpcIoBufferOffset(void)1675 int32_t IpcIoBufferOffset(void)
1676 {
1677 int8_t buf[64]; /* 64 buffer size */
1678 IpcIo ioCache;
1679
1680 IpcIoInit(&ioCache, (void *)buf, sizeof(buf), 0);
1681 if (ioCache.bufferCur <= ioCache.bufferBase) {
1682 return 0;
1683 }
1684 return (int32_t)((uintptr_t)(ioCache.bufferCur) - (uintptr_t)(ioCache.bufferBase));
1685 }
1686
1687 #ifdef __cplusplus
1688 }
1689 #endif
1690
1691