1 /*
2 * Copyright (c) 2021-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 "audio_common_napi.h"
17 #include "audio_log.h"
18 #include "audio_manager_napi.h"
19 #include "audio_info.h"
20
21 namespace OHOS {
22 namespace AudioStandard {
GetStringArgument(napi_env env,napi_value value)23 std::string AudioCommonNapi::GetStringArgument(napi_env env, napi_value value)
24 {
25 std::string strValue = "";
26 size_t bufLength = 0;
27 napi_status status = napi_get_value_string_utf8(env, value, nullptr, 0, &bufLength);
28 if (status == napi_ok && bufLength > 0 && bufLength < PATH_MAX) {
29 char *buffer = (char *)malloc((bufLength + 1) * sizeof(char));
30 CHECK_AND_RETURN_RET_LOG(buffer != nullptr, strValue, "no memory");
31 status = napi_get_value_string_utf8(env, value, buffer, bufLength + 1, &bufLength);
32 if (status == napi_ok) {
33 AUDIO_DEBUG_LOG("argument = %{public}s", buffer);
34 strValue = buffer;
35 }
36 free(buffer);
37 buffer = nullptr;
38 }
39 return strValue;
40 }
41
getMessageByCode(int32_t & code)42 std::string AudioCommonNapi::getMessageByCode(int32_t &code)
43 {
44 std::string err_message;
45 switch (code) {
46 case NAPI_ERR_INVALID_PARAM:
47 err_message = NAPI_ERR_INVALID_PARAM_INFO;
48 break;
49 case NAPI_ERR_NO_MEMORY:
50 err_message = NAPI_ERR_NO_MEMORY_INFO;
51 break;
52 case NAPI_ERR_ILLEGAL_STATE:
53 err_message = NAPI_ERR_ILLEGAL_STATE_INFO;
54 break;
55 case NAPI_ERR_UNSUPPORTED:
56 err_message = NAPI_ERR_UNSUPPORTED_INFO;
57 break;
58 case NAPI_ERR_TIMEOUT:
59 err_message = NAPI_ERR_TIMEOUT_INFO;
60 break;
61 case NAPI_ERR_STREAM_LIMIT:
62 err_message = NAPI_ERR_STREAM_LIMIT_INFO;
63 break;
64 case NAPI_ERR_SYSTEM:
65 err_message = NAPI_ERR_SYSTEM_INFO;
66 break;
67 case NAPI_ERR_INPUT_INVALID:
68 err_message = NAPI_ERR_INPUT_INVALID_INFO;
69 break;
70 default:
71 err_message = NAPI_ERR_SYSTEM_INFO;
72 code = NAPI_ERR_SYSTEM;
73 break;
74 }
75 return err_message;
76 }
77
throwError(napi_env env,int32_t code)78 void AudioCommonNapi::throwError(napi_env env, int32_t code)
79 {
80 std::string messageValue = AudioCommonNapi::getMessageByCode(code);
81 napi_throw_error(env, (std::to_string(code)).c_str(), messageValue.c_str());
82 }
83
IsLegalInputArgumentVolLevel(int32_t volLevel)84 bool AudioCommonNapi::IsLegalInputArgumentVolLevel(int32_t volLevel)
85 {
86 return (volLevel < MIN_VOLUME_LEVEL || volLevel > MAX_VOLUME_LEVEL) ? false : true;
87 }
88
IsLegalInputArgumentVolType(int32_t inputType)89 bool AudioCommonNapi::IsLegalInputArgumentVolType(int32_t inputType)
90 {
91 bool result = false;
92 switch (inputType) {
93 case AudioManagerNapi::RINGTONE:
94 case AudioManagerNapi::MEDIA:
95 case AudioManagerNapi::VOICE_CALL:
96 case AudioManagerNapi::VOICE_ASSISTANT:
97 case AudioManagerNapi::ALL:
98 result = true;
99 break;
100 default:
101 result = false;
102 break;
103 }
104 return result;
105 }
106
IsLegalInputArgumentDeviceFlag(int32_t deviceFlag)107 bool AudioCommonNapi::IsLegalInputArgumentDeviceFlag(int32_t deviceFlag)
108 {
109 bool result = false;
110 switch (deviceFlag) {
111 case DeviceFlag::NONE_DEVICES_FLAG:
112 case DeviceFlag::OUTPUT_DEVICES_FLAG:
113 case DeviceFlag::INPUT_DEVICES_FLAG:
114 case DeviceFlag::ALL_DEVICES_FLAG:
115 case DeviceFlag::DISTRIBUTED_OUTPUT_DEVICES_FLAG:
116 case DeviceFlag::DISTRIBUTED_INPUT_DEVICES_FLAG:
117 case DeviceFlag::ALL_DISTRIBUTED_DEVICES_FLAG:
118 case DeviceFlag::ALL_L_D_DEVICES_FLAG:
119 result = true;
120 break;
121 default:
122 result = false;
123 break;
124 }
125 return result;
126 }
127
IsLegalInputArgumentActiveDeviceType(int32_t activeDeviceFlag)128 bool AudioCommonNapi::IsLegalInputArgumentActiveDeviceType(int32_t activeDeviceFlag)
129 {
130 bool result = false;
131 switch (activeDeviceFlag) {
132 case ActiveDeviceType::SPEAKER:
133 case ActiveDeviceType::BLUETOOTH_SCO:
134 result = true;
135 break;
136 default:
137 result = false;
138 break;
139 }
140 return result;
141 }
142
IsLegalInputArgumentCommunicationDeviceType(int32_t communicationDeviceType)143 bool AudioCommonNapi::IsLegalInputArgumentCommunicationDeviceType(int32_t communicationDeviceType)
144 {
145 bool result = false;
146 switch (communicationDeviceType) {
147 case CommunicationDeviceType::COMMUNICATION_SPEAKER:
148 result = true;
149 break;
150 default:
151 result = false;
152 break;
153 }
154 return result;
155 }
156
IsLegalInputArgumentRingMode(int32_t ringerMode)157 bool AudioCommonNapi::IsLegalInputArgumentRingMode(int32_t ringerMode)
158 {
159 bool result = false;
160 switch (ringerMode) {
161 case AudioRingerMode::RINGER_MODE_SILENT:
162 case AudioRingerMode::RINGER_MODE_VIBRATE:
163 case AudioRingerMode::RINGER_MODE_NORMAL:
164 result = true;
165 break;
166 default:
167 result = false;
168 break;
169 }
170 return result;
171 }
172
173 } // namespace AudioStandard
174 } // namespace OHOS