• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <hdf_base.h>
16 #include <hdf_device_desc.h>
17 #include "intell_voice_log.h"
18 #include <hdf_sbuf_ipc.h>
19 #include "v1_0/intell_voice_engine_manager_stub.h"
20 
21 #undef HDF_LOG_TAG
22 #define HDF_LOG_TAG "IntellVoiceEngineDriver"
23 
24 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
25 
26 struct HdfIntellVoiceEngineManagerHost {
27     struct IDeviceIoService ioService;
28     OHOS::sptr<OHOS::IRemoteObject> stub;
29 };
30 
IntellVoiceEngineManagerDriverDispatch(struct HdfDeviceIoClient * client,int cmdId,struct HdfSBuf * data,struct HdfSBuf * reply)31 static int32_t IntellVoiceEngineManagerDriverDispatch(struct HdfDeviceIoClient *client, int cmdId,
32     struct HdfSBuf *data, struct HdfSBuf *reply)
33 {
34     auto *hdfIntellVoiceEngineManagerHost = CONTAINER_OF(client->device->service,
35         struct HdfIntellVoiceEngineManagerHost, ioService);
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     return hdfIntellVoiceEngineManagerHost->stub->SendRequest(cmdId, *dataParcel, *replyParcel, option);
51 }
52 
HdfIntellVoiceEngineManagerDriverInit(struct HdfDeviceObject * deviceObject)53 static int HdfIntellVoiceEngineManagerDriverInit(struct HdfDeviceObject *deviceObject)
54 {
55     INTELLIGENT_VOICE_LOGD("driver init start");
56     return HDF_SUCCESS;
57 }
58 
HdfIntellVoiceEngineManagerDriverBind(struct HdfDeviceObject * deviceObject)59 static int HdfIntellVoiceEngineManagerDriverBind(struct HdfDeviceObject *deviceObject)
60 {
61     INTELLIGENT_VOICE_LOGD("driver bind start");
62     auto *hdfIntellVoiceEngineManagerHost = new (std::nothrow) HdfIntellVoiceEngineManagerHost;
63     if (hdfIntellVoiceEngineManagerHost == nullptr) {
64         INTELLIGENT_VOICE_LOGE("failed to create create HdfIntellVoiceEngineManagerHost object");
65         return HDF_FAILURE;
66     }
67 
68     hdfIntellVoiceEngineManagerHost->ioService.Dispatch = IntellVoiceEngineManagerDriverDispatch;
69     hdfIntellVoiceEngineManagerHost->ioService.Open = NULL;
70     hdfIntellVoiceEngineManagerHost->ioService.Release = NULL;
71 
72     auto serviceImpl = IIntellVoiceEngineManager::Get(true);
73     if (serviceImpl == nullptr) {
74         INTELLIGENT_VOICE_LOGE("failed to get of implement service");
75         delete hdfIntellVoiceEngineManagerHost;
76         return HDF_FAILURE;
77     }
78 
79     hdfIntellVoiceEngineManagerHost->stub = OHOS::HDI::ObjectCollector::GetInstance().GetOrNewObject(serviceImpl,
80         IIntellVoiceEngineManager::GetDescriptor());
81     if (hdfIntellVoiceEngineManagerHost->stub == nullptr) {
82         INTELLIGENT_VOICE_LOGE("failed to get stub object");
83         delete hdfIntellVoiceEngineManagerHost;
84         return HDF_FAILURE;
85     }
86 
87     deviceObject->service = &hdfIntellVoiceEngineManagerHost->ioService;
88     return HDF_SUCCESS;
89 }
90 
HdfIntellVoiceEngineManagerDriverRelease(struct HdfDeviceObject * deviceObject)91 static void HdfIntellVoiceEngineManagerDriverRelease(struct HdfDeviceObject *deviceObject)
92 {
93     INTELLIGENT_VOICE_LOGD("driver release start");
94     if (deviceObject->service == nullptr) {
95         return;
96     }
97 
98     auto *hdfIntellVoiceEngineManagerHost = CONTAINER_OF(deviceObject->service,
99         struct HdfIntellVoiceEngineManagerHost, ioService);
100     if (hdfIntellVoiceEngineManagerHost != nullptr) {
101         delete hdfIntellVoiceEngineManagerHost;
102     }
103 }
104 
105 static struct HdfDriverEntry g_intellvoiceEngineManagerDriverEntry = {
106     .moduleVersion = 1,
107     .moduleName = "intell_voice_engine_service",
108     .Bind = HdfIntellVoiceEngineManagerDriverBind,
109     .Init = HdfIntellVoiceEngineManagerDriverInit,
110     .Release = HdfIntellVoiceEngineManagerDriverRelease,
111 };
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif /* __cplusplus */
116 HDF_INIT(g_intellvoiceEngineManagerDriverEntry);
117 #ifdef __cplusplus
118 }
119 #endif /* __cplusplus */
120