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 "allocator_service_stub.h"
17 #include "buffer_handle_parcel.h"
18 #include "buffer_handle_utils.h"
19 #include "hdf_base.h"
20 #include "hdf_log.h"
21 #include "hdf_sbuf_ipc.h"
22 #include "parcel_utils.h"
23 
24 #define HDF_LOG_TAG HDI_DISP_STUB
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Display {
29 namespace V1_0 {
AllocaltorStubAllocMem(MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t AllocatorServiceStub::AllocaltorStubAllocMem(MessageParcel &data, MessageParcel &reply, MessageOption &option)
31 {
32     if (data.ReadInterfaceToken() != AllocatorServiceStub::GetDescriptor()) {
33         HDF_LOGE("AllocMem: failed to check interface token");
34         return HDF_ERR_INVALID_PARAM;
35     }
36     AllocInfo info;
37     if (ParcelUtils::UnpackAllocInfo(data, &info) != HDF_SUCCESS) {
38         HDF_LOGE("%{public}s: UnpackAllocInfo failed", __func__);
39         return HDF_ERR_INVALID_PARAM;
40     }
41     BufferHandle *buffer = nullptr;
42     int32_t errCode = AllocMem(info, buffer);
43     if (!reply.WriteInt32(errCode)) {
44         if (buffer != nullptr) {
45             (void)FreeMem(*buffer);
46         }
47         HDF_LOGE("AllocMem: write reply failed");
48         return HDF_FAILURE;
49     }
50 
51     if (errCode != HDF_SUCCESS) {
52         HDF_LOGE("%{public}s:  call failed", __func__);
53         return errCode;
54     }
55 
56     if (WriteBufferHandle(reply, *buffer) != true) {
57         (void)FreeMem(*buffer);
58         HDF_LOGE("%{public}s: WriteBufferHandle failed", __func__);
59         return HDF_ERR_INVALID_PARAM;
60     }
61 
62     (void)FreeMem(*buffer);
63     return HDF_SUCCESS;
64 }
65 
OnRemoteRequest(uint32_t cmdId,MessageParcel & data,MessageParcel & reply,MessageOption & option)66 int32_t AllocatorServiceStub::OnRemoteRequest(
67     uint32_t cmdId, MessageParcel &data, MessageParcel &reply, MessageOption &option)
68 {
69     if (cmdId == CMD_ALLOCATOR_ALLOCMEM) {
70         return AllocaltorStubAllocMem(data, reply, option);
71     }
72     HDF_LOGE("%{public}s: not support cmd", __func__);
73     return HDF_ERR_INVALID_PARAM;
74 }
75 } // namespace V1_0
76 } // namespace Display
77 } // namespace HDI
78 } // namespace OHOS
79