1 /*
2  * Copyright (C) 2021-2022 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_stub.h"
17 #include "common_defs.h"
18 #include "hc_log.h"
19 #include "ipc_adapt.h"
20 #include "securec.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
StubDevAuthCb()24 StubDevAuthCb::StubDevAuthCb()
25 {}
26 
~StubDevAuthCb()27 StubDevAuthCb::~StubDevAuthCb()
28 {}
29 
DoCallBack(int32_t callbackId,uintptr_t cbHook,MessageParcel & dataParcel,MessageParcel & reply,MessageOption & option)30 void StubDevAuthCb::DoCallBack(int32_t callbackId, uintptr_t cbHook,
31     MessageParcel &dataParcel, MessageParcel &reply, MessageOption &option)
32 {
33     int32_t ret;
34     int32_t i;
35     MessageParcel retParcel;
36     IpcDataInfo cbDataCache[MAX_REQUEST_PARAMS_NUM] = { { 0 } };
37 
38     if (cbHook == 0x0) {
39         LOGE("Invalid call back hook");
40         return;
41     }
42 
43     for (i = 0; i < MAX_REQUEST_PARAMS_NUM; i++) {
44         ret = DecodeIpcData(reinterpret_cast<uintptr_t>(&dataParcel), &(cbDataCache[i].type),
45             &(cbDataCache[i].val), &(cbDataCache[i].valSz));
46         if (ret != HC_SUCCESS) {
47             LOGE("decode failed, ret %d", ret);
48             return;
49         }
50     }
51     ProcCbHook(callbackId, cbHook, cbDataCache, MAX_REQUEST_PARAMS_NUM, reinterpret_cast<uintptr_t>(&reply));
52     return;
53 }
54 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int32_t StubDevAuthCb::OnRemoteRequest(uint32_t code,
56     MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58     if (data.ReadInterfaceToken() != GetDescriptor()) {
59         LOGE("[IPC][S->C]: The client interface token is invalid!");
60         return -1;
61     }
62     int32_t callbackId;
63     uintptr_t cbHook = 0x0;
64 
65     switch (code) {
66         case static_cast<uint32_t>(DevAuthCbInterfaceCode::DEV_AUTH_CALLBACK_REQUEST):
67             if (data.GetReadableBytes() < sizeof(int32_t)) {
68                 LOGE("Insufficient data available in IPC container. [Data]: callbackId");
69                 return -1;
70             }
71             callbackId = data.ReadInt32();
72             cbHook = data.ReadPointer();
73             StubDevAuthCb::DoCallBack(callbackId, cbHook, data, reply, option);
74             break;
75         default:
76             LOGE("Invoke call back cmd id error, %u", code);
77             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
78     }
79     return 0;
80 }
81 }
82