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_callback_proxy.h"
17 #include "hc_log.h"
18 #include "hc_types.h"
19 #include "ipc_adapt.h"
20 #include "ipc_skeleton.h"
21 #include "securec.h"
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
CbProxyFormReplyData(int32_t reqRetVal,IpcIo * replyDst,const IpcIo * replySrc)27 static void CbProxyFormReplyData(int32_t reqRetVal, IpcIo *replyDst, const IpcIo *replySrc)
28 {
29 errno_t eno;
30
31 if (reqRetVal != 0) {
32 *(int32_t *)(replyDst->bufferCur) = reqRetVal;
33 replyDst->bufferLeft = sizeof(int32_t);
34 return;
35 }
36
37 LOGI("with reply data, length(%zu), flag(%u)", replySrc->bufferLeft, replySrc->flag);
38 eno = memcpy_s(replyDst->bufferCur, replyDst->bufferLeft, replySrc->bufferCur, replySrc->bufferLeft);
39 if (eno != EOK) {
40 replyDst->flag = 0;
41 LOGE("memory copy reply data failed");
42 return;
43 }
44 replyDst->bufferLeft = replySrc->bufferLeft;
45 LOGI("out reply data, length(%zu)", replyDst->bufferLeft);
46 return;
47 }
48
CbProxySendRequest(SvcIdentity sid,int32_t callbackId,uintptr_t cbHook,IpcIo * data,IpcIo * reply)49 void CbProxySendRequest(SvcIdentity sid, int32_t callbackId, uintptr_t cbHook, IpcIo *data, IpcIo *reply)
50 {
51 int32_t ret;
52 IpcIo *reqData = NULL;
53 int32_t dataSz;
54 uintptr_t outMsg = 0x0;
55 IpcIo replyTmp;
56
57 ShowIpcSvcInfo(&(sid));
58 reqData = (IpcIo *)InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
59 if (reqData == NULL) {
60 return;
61 }
62 WriteInt32(reqData, callbackId);
63 WritePointer(reqData, cbHook);
64 dataSz = GetIpcIoDataLength((const IpcIo *)data);
65 LOGI("to form callback params data length(%d)", dataSz);
66 if (dataSz > 0) {
67 WriteUint32(reqData, dataSz);
68 bool value = WriteBuffer(reqData, data->bufferBase + IpcIoBufferOffset(), dataSz);
69 if (!value) {
70 return;
71 }
72 }
73 /* callFlag: ipc mode is blocking or non blocking. */
74 MessageOption option;
75 MessageOptionInit(&option);
76 option.flags = ((reply != NULL) ? TF_OP_SYNC : TF_OP_ASYNC);
77 ret = SendRequest(sid, DEV_AUTH_CALLBACK_REQUEST, reqData, &replyTmp, option, &outMsg);
78 LOGI("SendRequest(%d) done, return(%d)", option.flags, ret);
79 HcFree((void *)reqData);
80 if (reply == NULL) {
81 return;
82 }
83 CbProxyFormReplyData(ret, reply, &replyTmp);
84 FreeBuffer((void *)outMsg);
85 return;
86 }
87
88 #ifdef __cplusplus
89 }
90 #endif
91