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 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_device_object.h>
19 #include <hdf_log.h>
20 #include <hdf_sbuf_ipc.h>
21 #include "v1_0/media_key_system_factory_stub.h"
22 #include "v1_0/imedia_key_system_factory.h"
23 
24 #define HDF_LOG_TAG media_key_system_factory_driver
25 
26 using namespace OHOS::HDI::Drm::V1_0;
27 
28 struct HdfMediaKeySystemFactoryHost {
29     struct IDeviceIoService ioService;
30     OHOS::sptr<OHOS::IRemoteObject> stub;
31 };
32 
MediaKeySystemFactoryDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)33 static int32_t MediaKeySystemFactoryDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data,
34     struct HdfSBuf *reply)
35 {
36     auto *hdfMediaKeySystemFactoryHost =
37         CONTAINER_OF(client->device->service, struct HdfMediaKeySystemFactoryHost, ioService);
38 
39     OHOS::MessageParcel *dataParcel = nullptr;
40     OHOS::MessageParcel *replyParcel = nullptr;
41     OHOS::MessageOption option;
42 
43     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
44         HDF_LOGE("%{public}s: invalid data sbuf object to dispatch", __func__);
45         return HDF_ERR_INVALID_PARAM;
46     }
47     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
48         HDF_LOGE("%{public}s: invalid reply sbuf object to dispatch", __func__);
49         return HDF_ERR_INVALID_PARAM;
50     }
51 
52     return hdfMediaKeySystemFactoryHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
53 }
54 
HdfMediaKeySystemFactoryDriverInit(struct HdfDeviceObject * deviceObject)55 static int HdfMediaKeySystemFactoryDriverInit(struct HdfDeviceObject *deviceObject)
56 {
57     HDF_LOGI("%{public}s: driver init start", __func__);
58     return HDF_SUCCESS;
59 }
60 
HdfMediaKeySystemFactoryDriverBind(struct HdfDeviceObject * deviceObject)61 static int HdfMediaKeySystemFactoryDriverBind(struct HdfDeviceObject *deviceObject)
62 {
63     HDF_LOGI("%{public}s: driver bind start", __func__);
64     auto *hdfMediaKeySystemFactoryHost = new (std::nothrow) HdfMediaKeySystemFactoryHost;
65     if (hdfMediaKeySystemFactoryHost == nullptr) {
66         HDF_LOGE("%{public}s: failed to create create HdfMediaKeySystemFactoryHost object", __func__);
67         return HDF_FAILURE;
68     }
69     int ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, "ohos.hdi.drm.v1_0.IMediaKeySystemFactory");
70     if (ret != HDF_SUCCESS) {
71         HDF_LOGE("%{public}s: failed to HdfDeviceObjectSetInterfaceDesc", __func__);
72     }
73 
74     hdfMediaKeySystemFactoryHost->ioService.Dispatch = MediaKeySystemFactoryDriverDispatch;
75     hdfMediaKeySystemFactoryHost->ioService.Open = NULL;
76     hdfMediaKeySystemFactoryHost->ioService.Release = NULL;
77 
78     auto serviceImpl = OHOS::HDI::Drm::V1_0::IMediaKeySystemFactory::Get("clearplay_service", true);
79     if (serviceImpl == nullptr) {
80         HDF_LOGE("%{public}s: failed to get of implement service", __func__);
81         delete hdfMediaKeySystemFactoryHost;
82         return HDF_FAILURE;
83     }
84 
85     hdfMediaKeySystemFactoryHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
86         OHOS::HDI::Drm::V1_0::IMediaKeySystemFactory::GetDescriptor());
87     if (hdfMediaKeySystemFactoryHost->stub == nullptr) {
88         HDF_LOGE("%{public}s: failed to get stub object", __func__);
89         delete hdfMediaKeySystemFactoryHost;
90         return HDF_FAILURE;
91     }
92 
93     deviceObject->service = &hdfMediaKeySystemFactoryHost->ioService;
94     return HDF_SUCCESS;
95 }
96 
HdfMediaKeySystemFactoryDriverRelease(struct HdfDeviceObject * deviceObject)97 static void HdfMediaKeySystemFactoryDriverRelease(struct HdfDeviceObject *deviceObject)
98 {
99     HDF_LOGI("%{public}s: driver release start", __func__);
100     if (deviceObject->service == nullptr) {
101         return;
102     }
103 
104     auto *hdfMediaKeySystemFactoryHost =
105         CONTAINER_OF(deviceObject->service, struct HdfMediaKeySystemFactoryHost, ioService);
106     if (hdfMediaKeySystemFactoryHost != nullptr) {
107         delete hdfMediaKeySystemFactoryHost;
108     }
109 }
110 
111 struct HdfDriverEntry g_mediakeysystemfactoryDriverEntry = {
112     .moduleVersion = 1,
113     .moduleName = "clearplay_service",
114     .Bind = HdfMediaKeySystemFactoryDriverBind,
115     .Init = HdfMediaKeySystemFactoryDriverInit,
116     .Release = HdfMediaKeySystemFactoryDriverRelease,
117 };
118 
119 #ifdef __cplusplus
120 extern "C" {
121 #endif /* __cplusplus */
122 HDF_INIT(g_mediakeysystemfactoryDriverEntry);
123 #ifdef __cplusplus
124 }
125 #endif /* __cplusplus */
126