1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <hdf_base.h> 17094332d3Sopenharmony_ci#include <hdf_device_desc.h> 18094332d3Sopenharmony_ci#include <hdf_device_object.h> 19094332d3Sopenharmony_ci#include <hdf_remote_service.h> 20094332d3Sopenharmony_ci#include <osal_mem.h> 21094332d3Sopenharmony_ci#include <stub_collector.h> 22094332d3Sopenharmony_ci#include "v4_0/iaudio_manager.h" 23094332d3Sopenharmony_ci#include "audio_uhdf_log.h" 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_ci#define HDF_LOG_TAG HDF_AUDIO_PRIMARY_DRV 26094332d3Sopenharmony_ci 27094332d3Sopenharmony_cistruct HdfAudioManagerHost { 28094332d3Sopenharmony_ci struct IDeviceIoService ioService; 29094332d3Sopenharmony_ci struct IAudioManager *service; 30094332d3Sopenharmony_ci struct HdfRemoteService **stubObject; 31094332d3Sopenharmony_ci}; 32094332d3Sopenharmony_ci 33094332d3Sopenharmony_cistatic int32_t AudioManagerDriverDispatch( 34094332d3Sopenharmony_ci struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) 35094332d3Sopenharmony_ci{ 36094332d3Sopenharmony_ci if (client == NULL || client->device == NULL || client->device->service == NULL) { 37094332d3Sopenharmony_ci HDF_LOGE("%{public}s:Param is NULL!", __func__); 38094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 39094332d3Sopenharmony_ci } 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_ci struct HdfAudioManagerHost *audiomanagerHost = 42094332d3Sopenharmony_ci CONTAINER_OF(client->device->service, struct HdfAudioManagerHost, ioService); 43094332d3Sopenharmony_ci if (audiomanagerHost->service == NULL || audiomanagerHost->stubObject == NULL) { 44094332d3Sopenharmony_ci HDF_LOGE("%{public}s:invalid service obj", __func__); 45094332d3Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 46094332d3Sopenharmony_ci } 47094332d3Sopenharmony_ci 48094332d3Sopenharmony_ci struct HdfRemoteService *stubObj = *audiomanagerHost->stubObject; 49094332d3Sopenharmony_ci if (stubObj == NULL || stubObj->dispatcher == NULL || stubObj->dispatcher->Dispatch == NULL) { 50094332d3Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 51094332d3Sopenharmony_ci } 52094332d3Sopenharmony_ci 53094332d3Sopenharmony_ci return stubObj->dispatcher->Dispatch((struct HdfRemoteService *)stubObj->target, cmdId, data, reply); 54094332d3Sopenharmony_ci} 55094332d3Sopenharmony_ci 56094332d3Sopenharmony_cistatic int32_t HdfAudioManagerDriverInit(struct HdfDeviceObject *deviceObject) 57094332d3Sopenharmony_ci{ 58094332d3Sopenharmony_ci HDF_LOGI("%{public}s: driver init enter", __func__); 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ci if (deviceObject == NULL) { 61094332d3Sopenharmony_ci HDF_LOGE("%{public}s:deviceObject is null!", __func__); 62094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 63094332d3Sopenharmony_ci } 64094332d3Sopenharmony_ci 65094332d3Sopenharmony_ci if (!HdfDeviceSetClass(deviceObject, DEVICE_CLASS_AUDIO)) { 66094332d3Sopenharmony_ci HDF_LOGE("%{public}s:set primary DEVICE_CLASS_AUDIO fail!", __func__); 67094332d3Sopenharmony_ci return HDF_ERR_INVALID_OBJECT; 68094332d3Sopenharmony_ci } 69094332d3Sopenharmony_ci 70094332d3Sopenharmony_ci HDF_LOGI("%{public}s:driver init success", __func__); 71094332d3Sopenharmony_ci return HDF_SUCCESS; 72094332d3Sopenharmony_ci} 73094332d3Sopenharmony_ci 74094332d3Sopenharmony_cistatic int32_t HdfAudioManagerDriverBind(struct HdfDeviceObject *deviceObject) 75094332d3Sopenharmony_ci{ 76094332d3Sopenharmony_ci HDF_LOGI("%{public}s:bind enter", __func__); 77094332d3Sopenharmony_ci if (deviceObject == NULL) { 78094332d3Sopenharmony_ci HDF_LOGE("%{public}s:Param is NULL!", __func__); 79094332d3Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 80094332d3Sopenharmony_ci } 81094332d3Sopenharmony_ci 82094332d3Sopenharmony_ci int32_t ret = HdfDeviceObjectSetInterfaceDesc(deviceObject, IAUDIOMANAGER_INTERFACE_DESC); 83094332d3Sopenharmony_ci if (ret != HDF_SUCCESS) { 84094332d3Sopenharmony_ci HDF_LOGI("%{public}s:failed to set interface descriptor object! ret = %{public}d", __func__, ret); 85094332d3Sopenharmony_ci return HDF_FAILURE; 86094332d3Sopenharmony_ci } 87094332d3Sopenharmony_ci 88094332d3Sopenharmony_ci struct HdfAudioManagerHost *audiomanagerHost = 89094332d3Sopenharmony_ci (struct HdfAudioManagerHost *)OsalMemCalloc(sizeof(struct HdfAudioManagerHost)); 90094332d3Sopenharmony_ci if (audiomanagerHost == NULL) { 91094332d3Sopenharmony_ci HDF_LOGE("%{public}s:alloc HdfAudioManagerHost failed!", __func__); 92094332d3Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 93094332d3Sopenharmony_ci } 94094332d3Sopenharmony_ci 95094332d3Sopenharmony_ci struct IAudioManager *serviceImpl = IAudioManagerGet(true); 96094332d3Sopenharmony_ci if (serviceImpl == NULL) { 97094332d3Sopenharmony_ci HDF_LOGE("%{public}s:create serviceImpl failed!", __func__); 98094332d3Sopenharmony_ci OsalMemFree(audiomanagerHost); 99094332d3Sopenharmony_ci return HDF_FAILURE; 100094332d3Sopenharmony_ci } 101094332d3Sopenharmony_ci 102094332d3Sopenharmony_ci struct HdfRemoteService **stubObj = StubCollectorGetOrNewObject(IAUDIOMANAGER_INTERFACE_DESC, serviceImpl); 103094332d3Sopenharmony_ci if (stubObj == NULL) { 104094332d3Sopenharmony_ci HDF_LOGE("%{public}s:failed to get stub object", __func__); 105094332d3Sopenharmony_ci OsalMemFree(audiomanagerHost); 106094332d3Sopenharmony_ci IAudioManagerRelease(serviceImpl, true); 107094332d3Sopenharmony_ci return HDF_FAILURE; 108094332d3Sopenharmony_ci } 109094332d3Sopenharmony_ci 110094332d3Sopenharmony_ci audiomanagerHost->ioService.Dispatch = AudioManagerDriverDispatch; 111094332d3Sopenharmony_ci audiomanagerHost->ioService.Open = NULL; 112094332d3Sopenharmony_ci audiomanagerHost->ioService.Release = NULL; 113094332d3Sopenharmony_ci audiomanagerHost->service = serviceImpl; 114094332d3Sopenharmony_ci audiomanagerHost->stubObject = stubObj; 115094332d3Sopenharmony_ci deviceObject->service = &audiomanagerHost->ioService; 116094332d3Sopenharmony_ci HDF_LOGI("%{public}s:bind success", __func__); 117094332d3Sopenharmony_ci return HDF_SUCCESS; 118094332d3Sopenharmony_ci} 119094332d3Sopenharmony_ci 120094332d3Sopenharmony_cistatic void HdfAudioManagerDriverRelease(struct HdfDeviceObject *deviceObject) 121094332d3Sopenharmony_ci{ 122094332d3Sopenharmony_ci HDF_LOGI("%{public}s:driver release enter", __func__); 123094332d3Sopenharmony_ci if (deviceObject == NULL) { 124094332d3Sopenharmony_ci HDF_LOGE("%{public}s:Param is NULL!", __func__); 125094332d3Sopenharmony_ci return; 126094332d3Sopenharmony_ci } 127094332d3Sopenharmony_ci 128094332d3Sopenharmony_ci struct HdfAudioManagerHost *audiomanagerHost = 129094332d3Sopenharmony_ci CONTAINER_OF(deviceObject->service, struct HdfAudioManagerHost, ioService); 130094332d3Sopenharmony_ci if (audiomanagerHost == NULL) { 131094332d3Sopenharmony_ci HDF_LOGE("%{public}s:HdfAudioManagerHost is NULL!", __func__); 132094332d3Sopenharmony_ci return; 133094332d3Sopenharmony_ci } 134094332d3Sopenharmony_ci 135094332d3Sopenharmony_ci StubCollectorRemoveObject(IAUDIOMANAGER_INTERFACE_DESC, audiomanagerHost->service); 136094332d3Sopenharmony_ci IAudioManagerRelease(audiomanagerHost->service, true); 137094332d3Sopenharmony_ci OsalMemFree(audiomanagerHost); 138094332d3Sopenharmony_ci HDF_LOGI("%{public}s:release success", __func__); 139094332d3Sopenharmony_ci} 140094332d3Sopenharmony_ci 141094332d3Sopenharmony_cistatic struct HdfDriverEntry g_audiomanagerDriverEntry = { 142094332d3Sopenharmony_ci .moduleVersion = 1, 143094332d3Sopenharmony_ci .moduleName = "audio_primary_driver", 144094332d3Sopenharmony_ci .Bind = HdfAudioManagerDriverBind, 145094332d3Sopenharmony_ci .Init = HdfAudioManagerDriverInit, 146094332d3Sopenharmony_ci .Release = HdfAudioManagerDriverRelease, 147094332d3Sopenharmony_ci}; 148094332d3Sopenharmony_ci 149094332d3Sopenharmony_ciHDF_INIT(g_audiomanagerDriverEntry);