• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include <hdf_log.h>
19 #include <hdf_sbuf_ipc.h>
20 
21 #include "v1_0/memory_tracker_interface_stub.h"
22 
23 using namespace OHOS::HDI::Memorytracker::V1_0;
24 
25 struct HdfMemoryTrackerInterfaceHost {
26     struct IDeviceIoService ioService;
27     OHOS::sptr<OHOS::IRemoteObject> stub;
28 };
29 
MemoryTrackerInterfaceDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)30 static int32_t MemoryTrackerInterfaceDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
31     struct HdfSBuf *reply)
32 {
33     auto *hdfMemoryTrackerInterfaceHost = CONTAINER_OF(client->device->service, struct HdfMemoryTrackerInterfaceHost,
34         ioService);
35 
36     OHOS::MessageParcel *dataParcel = nullptr;
37     OHOS::MessageParcel *replyParcel = nullptr;
38     OHOS::MessageOption option;
39 
40     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
41         HDF_LOGE("%{public}s:invalid data sbuf object to dispatch", __func__);
42         return HDF_ERR_INVALID_PARAM;
43     }
44     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
45         HDF_LOGE("%{public}s:invalid reply sbuf object to dispatch", __func__);
46         return HDF_ERR_INVALID_PARAM;
47     }
48 
49     return hdfMemoryTrackerInterfaceHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
50 }
51 
HdfMemoryTrackerInterfaceDriverInit(struct HdfDeviceObject * deviceObject)52 static int HdfMemoryTrackerInterfaceDriverInit(struct HdfDeviceObject *deviceObject)
53 {
54     HDF_LOGI("HdfMemoryTrackerInterfaceDriverInit enter");
55     return HDF_SUCCESS;
56 }
57 
HdfMemoryTrackerInterfaceDriverBind(struct HdfDeviceObject * deviceObject)58 static int HdfMemoryTrackerInterfaceDriverBind(struct HdfDeviceObject *deviceObject)
59 {
60     HDF_LOGI("HdfMemoryTrackerInterfaceDriverBind enter");
61 
62     auto *hdfMemoryTrackerInterfaceHost = new (std::nothrow) HdfMemoryTrackerInterfaceHost;
63     if (hdfMemoryTrackerInterfaceHost == nullptr) {
64         HDF_LOGE("%{public}s: failed to create create HdfMemoryTrackerInterfaceHost object", __func__);
65         return HDF_FAILURE;
66     }
67 
68     hdfMemoryTrackerInterfaceHost->ioService.Dispatch = MemoryTrackerInterfaceDriverDispatch;
69     hdfMemoryTrackerInterfaceHost->ioService.Open = NULL;
70     hdfMemoryTrackerInterfaceHost->ioService.Release = NULL;
71 
72     auto serviceImpl = IMemoryTrackerInterface::Get(true);
73     if (serviceImpl == nullptr) {
74         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
75         delete hdfMemoryTrackerInterfaceHost;
76         return HDF_FAILURE;
77     }
78 
79     hdfMemoryTrackerInterfaceHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
80         IMemoryTrackerInterface::GetDescriptor());
81     if (hdfMemoryTrackerInterfaceHost->stub == nullptr) {
82         HDF_LOGE("%{public}s: failed to get stub object", __func__);
83         delete hdfMemoryTrackerInterfaceHost;
84         return HDF_FAILURE;
85     }
86 
87     deviceObject->service = &hdfMemoryTrackerInterfaceHost->ioService;
88     return HDF_SUCCESS;
89 }
90 
HdfMemoryTrackerInterfaceDriverRelease(struct HdfDeviceObject * deviceObject)91 static void HdfMemoryTrackerInterfaceDriverRelease(struct HdfDeviceObject *deviceObject)
92 {
93     HDF_LOGI("HdfMemoryTrackerInterfaceDriverRelease enter");
94     auto *hdfMemoryTrackerInterfaceHost = CONTAINER_OF(deviceObject->service, struct HdfMemoryTrackerInterfaceHost,
95         ioService);
96     delete hdfMemoryTrackerInterfaceHost;
97 }
98 
99 static struct HdfDriverEntry g_memorytrackerinterfaceDriverEntry = {
100     .moduleVersion = 1,
101     .moduleName = "memory_tracker_interface_service",
102     .Bind = HdfMemoryTrackerInterfaceDriverBind,
103     .Init = HdfMemoryTrackerInterfaceDriverInit,
104     .Release = HdfMemoryTrackerInterfaceDriverRelease,
105 };
106 
107 #ifdef __cplusplus
108 extern "C" {
109 #endif /* __cplusplus */
110 HDF_INIT(g_memorytrackerinterfaceDriverEntry);
111 #ifdef __cplusplus
112 }
113 #endif /* __cplusplus */
114