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 #include <shared_mutex>
16 #include <hdf_base.h>
17 #include <hdf_device_desc.h>
18 #include "intell_voice_log.h"
19 #include <hdf_sbuf_ipc.h>
20 #include "v1_2/intell_voice_engine_manager_stub.h"
21 
22 #undef HDF_LOG_TAG
23 #define HDF_LOG_TAG "IntellVoiceEngineDriver"
24 
25 namespace {
26     static std::shared_mutex g_engineMgrMutex;
27 }
28 
29 struct HdfIntellVoiceEngineManagerHost {
30     struct IDeviceIoService ioService;
31     OHOS::sptr<OHOS::IRemoteObject> stub;
32 };
33 
IntellVoiceEngineManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)34 static int32_t IntellVoiceEngineManagerDriverDispatch(struct HdfDeviceIoClient *client, int cmdId,
35     struct HdfSBuf *data, struct HdfSBuf *reply)
36 {
37     OHOS::MessageParcel *dataParcel = nullptr;
38     OHOS::MessageParcel *replyParcel = nullptr;
39     OHOS::MessageOption option;
40 
41     if (SbufToParcel(data, &dataParcel) != HDF_SUCCESS) {
42         INTELLIGENT_VOICE_LOGE("invalid data sbuf object to dispatch");
43         return HDF_ERR_INVALID_PARAM;
44     }
45     if (SbufToParcel(reply, &replyParcel) != HDF_SUCCESS) {
46         INTELLIGENT_VOICE_LOGE("invalid reply sbuf object to dispatch");
47         return HDF_ERR_INVALID_PARAM;
48     }
49 
50     std::shared_lock lock(g_engineMgrMutex);
51     if ((client == nullptr) || (client->device == nullptr) || (client->device->service == nullptr)) {
52         INTELLIGENT_VOICE_LOGE("client or device or service is nullptr");
53         return HDF_FAILURE;
54     }
55 
56     auto *hdfIntellVoiceEngineManagerHost = CONTAINER_OF(client->device->service,
57         struct HdfIntellVoiceEngineManagerHost, ioService);
58     if ((hdfIntellVoiceEngineManagerHost == nullptr) || (hdfIntellVoiceEngineManagerHost->stub == nullptr)) {
59         INTELLIGENT_VOICE_LOGE("invalid hdfIntellVoiceEngineManagerHost");
60         return HDF_FAILURE;
61     }
62 
63     return hdfIntellVoiceEngineManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
64 }
65 
HdfIntellVoiceEngineManagerDriverInit(struct HdfDeviceObject * deviceObject)66 static int HdfIntellVoiceEngineManagerDriverInit(struct HdfDeviceObject *deviceObject)
67 {
68     INTELLIGENT_VOICE_LOGD("driver init start");
69     return HDF_SUCCESS;
70 }
71 
HdfIntellVoiceEngineManagerDriverBind(struct HdfDeviceObject * deviceObject)72 static int HdfIntellVoiceEngineManagerDriverBind(struct HdfDeviceObject *deviceObject)
73 {
74     INTELLIGENT_VOICE_LOGI("enter");
75     if (deviceObject == nullptr) {
76         INTELLIGENT_VOICE_LOGE("deviceObject is nullptr");
77         return HDF_FAILURE;
78     }
79 
80     auto *hdfIntellVoiceEngineManagerHost = new (std::nothrow) HdfIntellVoiceEngineManagerHost;
81     if (hdfIntellVoiceEngineManagerHost == nullptr) {
82         INTELLIGENT_VOICE_LOGE("failed to create create HdfIntellVoiceEngineManagerHost object");
83         return HDF_FAILURE;
84     }
85 
86     hdfIntellVoiceEngineManagerHost->ioService.Dispatch = IntellVoiceEngineManagerDriverDispatch;
87     hdfIntellVoiceEngineManagerHost->ioService.Open = NULL;
88     hdfIntellVoiceEngineManagerHost->ioService.Release = NULL;
89 
90     auto serviceImpl = OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager::Get(true);
91     if (serviceImpl == nullptr) {
92         INTELLIGENT_VOICE_LOGE("failed to get of implement service");
93         delete hdfIntellVoiceEngineManagerHost;
94         return HDF_FAILURE;
95     }
96 
97     hdfIntellVoiceEngineManagerHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
98         OHOS::HDI::IntelligentVoice::Engine::V1_2::IIntellVoiceEngineManager::GetDescriptor());
99     if (hdfIntellVoiceEngineManagerHost->stub == nullptr) {
100         INTELLIGENT_VOICE_LOGE("failed to get stub object");
101         delete hdfIntellVoiceEngineManagerHost;
102         return HDF_FAILURE;
103     }
104 
105     deviceObject->service = &hdfIntellVoiceEngineManagerHost->ioService;
106     return HDF_SUCCESS;
107 }
108 
HdfIntellVoiceEngineManagerDriverRelease(struct HdfDeviceObject * deviceObject)109 static void HdfIntellVoiceEngineManagerDriverRelease(struct HdfDeviceObject *deviceObject)
110 {
111     INTELLIGENT_VOICE_LOGI("enter");
112     if ((deviceObject == nullptr) || (deviceObject->service == nullptr)) {
113         INTELLIGENT_VOICE_LOGE("deviceObject is nullptr or service is nullptr");
114         return;
115     }
116 
117     std::unique_lock lock(g_engineMgrMutex);
118     auto *hdfIntellVoiceEngineManagerHost = CONTAINER_OF(deviceObject->service,
119         struct HdfIntellVoiceEngineManagerHost, ioService);
120     if (hdfIntellVoiceEngineManagerHost != nullptr) {
121         hdfIntellVoiceEngineManagerHost->stub = nullptr;
122         delete hdfIntellVoiceEngineManagerHost;
123     }
124     deviceObject->service = nullptr;
125 }
126 
127 static struct HdfDriverEntry g_intellvoiceEngineManagerDriverEntry = {
128     .moduleVersion = 1,
129     .moduleName = "intell_voice_engine_service",
130     .Bind = HdfIntellVoiceEngineManagerDriverBind,
131     .Init = HdfIntellVoiceEngineManagerDriverInit,
132     .Release = HdfIntellVoiceEngineManagerDriverRelease,
133 };
134 
135 #ifdef __cplusplus
136 extern "C" {
137 #endif /* __cplusplus */
138 HDF_INIT(g_intellvoiceEngineManagerDriverEntry);
139 #ifdef __cplusplus
140 }
141 #endif /* __cplusplus */
142