1 /*
2 * Copyright (c) 2022 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_audio_pnp_server.h"
17 #include "audio_uhdf_log.h"
18 #include "hdf_audio_input_event.h"
19 #include "hdf_audio_pnp_uevent.h"
20 #include "hdf_audio_server.h"
21 #include "hdf_device_desc.h"
22 #include "hdf_device_object.h"
23 #include "hdf_io_service_if.h"
24 #include "hdf_sbuf.h"
25 #include "hdf_service_status.h"
26 #include "securec.h"
27 #include "servmgr_hdi.h"
28
29 #define HDF_LOG_TAG HDF_AUDIO_HOST
30 #define AUDIO_HDI_SERVICE_NAME "audio_hdi_usb_service"
31 #define AUDIO_TOKEN_SERVER_NAME "ohos.hdi.audio_service"
32 #define AUDIO_PNP_SEND_USB_CMD 8
33 #define AUDIO_PNP_INFO_LEN_MAX 256
34
35 static struct HdfDeviceObject *g_audioPnpDevice = NULL;
36
AudioPnpStatusSend(const char * serverName,const char * tokenServerName,const char * pnpInfo,const int cmd)37 int32_t AudioPnpStatusSend(const char *serverName, const char *tokenServerName, const char *pnpInfo, const int cmd)
38 {
39 if (serverName == NULL || tokenServerName == NULL || pnpInfo == NULL) {
40 AUDIO_FUNC_LOGE("serverName is null!");
41 return HDF_ERR_INVALID_PARAM;
42 }
43
44 struct HDIServiceManager *servmgr = HDIServiceManagerGet();
45 if (servmgr == NULL) {
46 AUDIO_FUNC_LOGE("get all service failed!");
47 return HDF_FAILURE;
48 }
49
50 struct HdfRemoteService *hdiAudioService = servmgr->GetService(servmgr, serverName);
51 HDIServiceManagerRelease(servmgr);
52 if (hdiAudioService == NULL || hdiAudioService->dispatcher == NULL) {
53 AUDIO_FUNC_LOGE("get %{public}s not exist!", serverName);
54 return HDF_FAILURE;
55 }
56
57 if (!HdfRemoteServiceSetInterfaceDesc(hdiAudioService, tokenServerName)) {
58 AUDIO_FUNC_LOGE("SetInterfaceDesc %{public}s failed! ", tokenServerName);
59 HdfRemoteServiceRecycle(hdiAudioService);
60 return HDF_FAILURE;
61 }
62
63 struct HdfSBuf *data = HdfSbufTypedObtain(SBUF_IPC);
64 if (data == NULL) {
65 AUDIO_FUNC_LOGE("sbuf data malloc failed!");
66 return HDF_FAILURE;
67 }
68
69 if (!HdfRemoteServiceWriteInterfaceToken(hdiAudioService, data)) {
70 AUDIO_FUNC_LOGE("write token failed!");
71 HdfSbufRecycle(data);
72 return HDF_FAILURE;
73 }
74
75 if (!HdfSbufWriteString(data, pnpInfo)) {
76 HdfSbufRecycle(data);
77 AUDIO_FUNC_LOGE("sbuf write failed!");
78 return HDF_FAILURE;
79 }
80
81 int ret = hdiAudioService->dispatcher->Dispatch(hdiAudioService, cmd, data, NULL);
82 HdfSbufRecycle(data);
83 if (ret != HDF_SUCCESS) {
84 AUDIO_FUNC_LOGE("%{public}s cmd(%{public}d) dispatch failed! ret = %{public}d", serverName, cmd, ret);
85 return HDF_FAILURE;
86 }
87
88 return HDF_SUCCESS;
89 }
90
AudioPnpUpdateInfo(const char * statusInfo)91 int32_t AudioPnpUpdateInfo(const char *statusInfo)
92 {
93 if (g_audioPnpDevice == NULL) {
94 AUDIO_FUNC_LOGE("g_audioPnpDevice is null!");
95 return HDF_ERR_INVALID_PARAM;
96 }
97 if (statusInfo == NULL) {
98 AUDIO_FUNC_LOGE("statusInfo is null!");
99 return HDF_ERR_INVALID_PARAM;
100 }
101
102 if (HdfDeviceObjectSetServInfo(g_audioPnpDevice, statusInfo) != HDF_SUCCESS) {
103 AUDIO_FUNC_LOGE("set audio new status info failed!");
104 return HDF_FAILURE;
105 }
106 if (HdfDeviceObjectUpdate(g_audioPnpDevice) != HDF_SUCCESS) {
107 AUDIO_FUNC_LOGE("update audio status info failed!");
108 return HDF_FAILURE;
109 }
110
111 return HDF_SUCCESS;
112 }
113
AudioPnpUpdateInfoOnly(struct AudioEvent audioEvent)114 int32_t AudioPnpUpdateInfoOnly(struct AudioEvent audioEvent)
115 {
116 int32_t ret;
117 char pnpInfo[AUDIO_PNP_INFO_LEN_MAX] = {0};
118
119 ret = snprintf_s(pnpInfo, AUDIO_PNP_INFO_LEN_MAX, AUDIO_PNP_INFO_LEN_MAX - 1, "EVENT_TYPE=%u;DEVICE_TYPE=%u",
120 audioEvent.eventType, audioEvent.deviceType);
121 if (ret < 0) {
122 AUDIO_FUNC_LOGE("snprintf_s failed!");
123 return HDF_FAILURE;
124 }
125
126 ret = AudioPnpUpdateInfo(pnpInfo);
127 if (ret != HDF_SUCCESS) {
128 AUDIO_FUNC_LOGE("Update info failed: ret = %{public}d", ret);
129 return HDF_FAILURE;
130 }
131 AUDIO_FUNC_LOGD("Audio uevent:%{public}s", pnpInfo);
132
133 return HDF_SUCCESS;
134 }
135
AudioPnpUpdateAndSend(struct AudioEvent audioEvent)136 int32_t AudioPnpUpdateAndSend(struct AudioEvent audioEvent)
137 {
138 int32_t ret;
139 char pnpInfo[AUDIO_PNP_INFO_LEN_MAX] = {0};
140
141 ret = snprintf_s(pnpInfo, AUDIO_PNP_INFO_LEN_MAX, AUDIO_PNP_INFO_LEN_MAX - 1, "EVENT_TYPE=%u;DEVICE_TYPE=%u",
142 audioEvent.eventType, audioEvent.deviceType);
143 if (ret < 0) {
144 AUDIO_FUNC_LOGE("snprintf_s fail!");
145 return HDF_FAILURE;
146 }
147
148 ret = AudioPnpUpdateInfo(pnpInfo);
149 if (ret != HDF_SUCCESS) {
150 AUDIO_FUNC_LOGE("update info fail! ret = %{public}d", ret);
151 return HDF_FAILURE;
152 }
153
154 ret = AudioPnpStatusSend(AUDIO_HDI_SERVICE_NAME, AUDIO_TOKEN_SERVER_NAME, pnpInfo, AUDIO_HDI_PNP_DEV_STATUS);
155 if (ret != HDF_SUCCESS) {
156 return HDF_FAILURE;
157 }
158 AUDIO_FUNC_LOGD("Audio uevent:%{public}s", pnpInfo);
159
160 return HDF_SUCCESS;
161 }
162
HdfAudioPnpBind(struct HdfDeviceObject * device)163 static int32_t HdfAudioPnpBind(struct HdfDeviceObject *device)
164 {
165 AUDIO_FUNC_LOGI("enter.");
166 if (device == NULL) {
167 AUDIO_FUNC_LOGE("device is null!");
168 return HDF_ERR_INVALID_PARAM;
169 }
170 AUDIO_FUNC_LOGI("end.");
171
172 return HDF_SUCCESS;
173 }
174
HdfAudioPnpInit(struct HdfDeviceObject * device)175 static int32_t HdfAudioPnpInit(struct HdfDeviceObject *device)
176 {
177 AUDIO_FUNC_LOGI("enter.");
178 if (device == NULL) {
179 AUDIO_FUNC_LOGE("device is null!");
180 return HDF_ERR_INVALID_PARAM;
181 }
182
183 if (!HdfDeviceSetClass(device, DEVICE_CLASS_AUDIO)) {
184 AUDIO_FUNC_LOGE("set audio class failed!");
185 return HDF_FAILURE;
186 }
187 g_audioPnpDevice = device;
188 AudioPnpUeventStartThread();
189 AudioPnpInputStartThread();
190
191 AUDIO_FUNC_LOGI("end.");
192 return HDF_SUCCESS;
193 }
194
HdfAudioPnpRelease(struct HdfDeviceObject * device)195 static void HdfAudioPnpRelease(struct HdfDeviceObject *device)
196 {
197 AUDIO_FUNC_LOGI("enter.");
198 if (device == NULL) {
199 AUDIO_FUNC_LOGE("device is null!");
200 return;
201 }
202
203 AudioPnpUeventStopThread();
204 AudioPnpInputEndThread();
205 device->service = NULL;
206
207 AUDIO_FUNC_LOGI("end.");
208 return;
209 }
210
211 struct HdfDriverEntry g_hdiAudioPnpEntry = {
212 .moduleVersion = 1,
213 .moduleName = "hdi_audio_pnp_server",
214 .Bind = HdfAudioPnpBind,
215 .Init = HdfAudioPnpInit,
216 .Release = HdfAudioPnpRelease,
217 };
218
219 HDF_INIT(g_hdiAudioPnpEntry);
220