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