• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ipc_callback_proxy.h"
17 #include "hc_log.h"
18 #include "hc_types.h"
19 #include "ipc_adapt.h"
20 #include "liteipc_adapter.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 (!IpcIoAvailable((IpcIo *)replySrc)) {
32         LOGE("reply context is not available");
33         *(int32_t *)(replyDst->bufferCur) = -1;
34         replyDst->bufferLeft = sizeof(int32_t);
35         return;
36     }
37     if (reqRetVal != 0) {
38         *(int32_t *)(replyDst->bufferCur) = reqRetVal;
39         replyDst->bufferLeft = sizeof(int32_t);
40         return;
41     }
42 
43     LOGI("with reply data, length(%zu), flag(%u)", replySrc->bufferLeft, replySrc->flag);
44     eno = memcpy_s(replyDst->bufferCur, replyDst->bufferLeft, replySrc->bufferCur, replySrc->bufferLeft);
45     if (eno != EOK) {
46         replyDst->flag = 0;
47         LOGE("memory copy reply data failed");
48         return;
49     }
50     replyDst->bufferLeft = replySrc->bufferLeft;
51     LOGI("out reply data, length(%zu)", replyDst->bufferLeft);
52     return;
53 }
54 
CbProxySendRequest(SvcIdentity sid,int32_t callbackId,uintptr_t cbHook,IpcIo * data,IpcIo * reply)55 void CbProxySendRequest(SvcIdentity sid, int32_t callbackId, uintptr_t cbHook, IpcIo *data, IpcIo *reply)
56 {
57     int32_t ret;
58     IpcIo *reqData = NULL;
59     int32_t dataSz;
60     uintptr_t outMsg = 0x0;
61     IpcIo replyTmp;
62 
63     ShowIpcSvcInfo(&(sid));
64     reqData = (IpcIo *)InitIpcDataCache(IPC_DATA_BUFF_MAX_SZ);
65     if (reqData == NULL) {
66         return;
67     }
68     IpcIoPushInt32(reqData, callbackId);
69     IpcIoPushUintptr(reqData, cbHook);
70     dataSz = GetIpcIoDataLength((const IpcIo *)data);
71     LOGI("to form callback params data length(%d)", dataSz);
72     if (dataSz > 0) {
73         IpcIoPushFlatObj(reqData, data->bufferBase + IpcIoBufferOffset(), dataSz);
74     }
75     if (!IpcIoAvailable(reqData)) {
76         LOGE("form send data failed");
77         HcFree((void *)reqData);
78         return;
79     }
80     /* callFlag: ipc mode is blocking or non blocking. */
81     int32_t callFlag = ((reply != NULL) ? LITEIPC_FLAG_DEFAULT : LITEIPC_FLAG_ONEWAY);
82     ret = SendRequest(NULL, sid, DEV_AUTH_CALLBACK_REQUEST, reqData, &replyTmp, callFlag, &outMsg);
83     LOGI("SendRequest(%d) done, return(%d)", callFlag, ret);
84     HcFree((void *)reqData);
85     if (reply == NULL) {
86         FreeBuffer(NULL, (void *)outMsg);
87         return;
88     }
89     CbProxyFormReplyData(ret, reply, &replyTmp);
90     FreeBuffer(NULL, (void *)outMsg);
91     return;
92 }
93 
94 #ifdef __cplusplus
95 }
96 #endif
97