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 #ifndef HDI_DAUDIO_CONTROL_INTERNAL_H
17 #define HDI_DAUDIO_CONTROL_INTERNAL_H
18
19 #include <cstdint>
20
21 #include "audio_types.h"
22
23 #include "daudio_errorcode.h"
24
25 #undef DH_LOG_TAG
26 #define DH_LOG_TAG "AudioControlInternal"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 template<typename T>
31 class AudioControlInternal final {
32 public:
33 static int32_t Start(AudioHandle handle);
34 static int32_t Stop(AudioHandle handle);
35 static int32_t Pause(AudioHandle handle);
36 static int32_t Resume(AudioHandle handle);
37 static int32_t Flush(AudioHandle handle);
38 static int32_t TurnStandbyMode(AudioHandle handle);
39 static int32_t AudioDevDump(AudioHandle handle, int32_t range, int32_t fd);
40 };
41
42 template<typename T>
Start(AudioHandle handle)43 int32_t AudioControlInternal<T>::Start(AudioHandle handle)
44 {
45 if (handle == nullptr) {
46 DHLOGE("The parameter is empty.");
47 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
48 }
49
50 T *context = reinterpret_cast<T *>(handle);
51 return (context == nullptr || context->proxy_ == nullptr) ?
52 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->Start();
53 }
54
55 template<typename T>
Stop(AudioHandle handle)56 int32_t AudioControlInternal<T>::Stop(AudioHandle handle)
57 {
58 if (handle == nullptr) {
59 DHLOGE("The parameter is empty.");
60 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
61 }
62
63 T *context = reinterpret_cast<T *>(handle);
64 return (context == nullptr || context->proxy_ == nullptr) ?
65 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->Stop();
66 }
67
68 template<typename T>
Pause(AudioHandle handle)69 int32_t AudioControlInternal<T>::Pause(AudioHandle handle)
70 {
71 if (handle == nullptr) {
72 DHLOGE("The parameter is empty.");
73 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
74 }
75
76 T *context = reinterpret_cast<T *>(handle);
77 return (context == nullptr || context->proxy_ == nullptr) ?
78 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->Pause();
79 }
80
81 template<typename T>
Resume(AudioHandle handle)82 int32_t AudioControlInternal<T>::Resume(AudioHandle handle)
83 {
84 if (handle == nullptr) {
85 DHLOGE("The parameter is empty.");
86 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
87 }
88
89 T *context = reinterpret_cast<T *>(handle);
90 return (context == nullptr || context->proxy_ == nullptr) ?
91 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->Resume();
92 }
93
94 template<typename T>
Flush(AudioHandle handle)95 int32_t AudioControlInternal<T>::Flush(AudioHandle handle)
96 {
97 if (handle == nullptr) {
98 DHLOGE("The parameter is empty.");
99 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
100 }
101
102 T *context = reinterpret_cast<T *>(handle);
103 return (context == nullptr || context->proxy_ == nullptr) ?
104 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->Flush();
105 }
106
107 template<typename T>
TurnStandbyMode(AudioHandle handle)108 int32_t AudioControlInternal<T>::TurnStandbyMode(AudioHandle handle)
109 {
110 if (handle == nullptr) {
111 DHLOGE("The parameter is empty.");
112 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
113 }
114
115 T *context = reinterpret_cast<T *>(handle);
116 return (context == nullptr || context->proxy_ == nullptr) ?
117 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->TurnStandbyMode();
118 }
119
120 template<typename T>
AudioDevDump(AudioHandle handle,int32_t range,int32_t fd)121 int32_t AudioControlInternal<T>::AudioDevDump(AudioHandle handle, int32_t range, int32_t fd)
122 {
123 if (handle == nullptr) {
124 DHLOGE("The parameter is empty.");
125 return ERR_DH_AUDIO_HDI_INVALID_PARAM;
126 }
127
128 T *context = reinterpret_cast<T *>(handle);
129 return (context == nullptr || context->proxy_ == nullptr) ?
130 ERR_DH_AUDIO_HDI_INVALID_PARAM : context->proxy_->AudioDevDump(range, fd);
131 }
132 } // DistributedHardware
133 } // OHOS
134 #endif // HDI_DAUDIO_CONTROL_INTERNAL_H