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