1 /*
2 * Copyright (c) 2022-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
16 #include "daudio_util.h"
17
18 #include <ctime>
19 #include <cstddef>
20 #include <iomanip>
21 #include <map>
22 #include <random>
23 #include <sstream>
24 #include <sys/time.h>
25
26 #include "softbus_bus_center.h"
27
28 #include "audio_event.h"
29 #include "daudio_constants.h"
30 #include "daudio_errorcode.h"
31 #include "daudio_log.h"
32 #include "parameter.h"
33
34 #undef DH_LOG_TAG
35 #define DH_LOG_TAG "DAudioUtil"
36
37 namespace OHOS {
38 namespace DistributedHardware {
39 using JsonTypeCheckFunc = bool (*)(const json &jsonObj, const std::string &key);
40 constexpr int32_t WORD_WIDTH_8 = 8;
41 constexpr int32_t WORD_WIDTH_4 = 4;
42 constexpr size_t INT32_SHORT_ID_LENGTH = 20;
43 constexpr size_t INT32_MIN_ID_LENGTH = 3;
44 constexpr size_t INT32_PLAINTEXT_LENGTH = 4;
45 constexpr uint8_t MAX_KEY_DH_ID_LEN = 20;
46
47 std::map<std::string, JsonTypeCheckFunc> typeCheckMap = {
48 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_TYPE, &DistributedHardware::IsInt32),
49 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_EVENT_CONTENT, &DistributedHardware::IsString),
50 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_DH_ID, &DistributedHardware::IsString),
51 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_DEV_ID, &DistributedHardware::IsString),
52 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_RESULT, &DistributedHardware::IsInt32),
53 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_EVENT_TYPE, &DistributedHardware::IsInt32),
54 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_AUDIO_PARAM, &DistributedHardware::IsAudioParam),
55 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_ATTRS, &DistributedHardware::IsString),
56 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_RANDOM_TASK_CODE, &DistributedHardware::IsString),
57 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_SAMPLING_RATE, &DistributedHardware::IsInt32),
58 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_CHANNELS, &DistributedHardware::IsInt32),
59 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_FORMAT, &DistributedHardware::IsInt32),
60 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_SOURCE_TYPE, &DistributedHardware::IsInt32),
61 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_CONTENT_TYPE, &DistributedHardware::IsInt32),
62 std::map<std::string, JsonTypeCheckFunc>::value_type(KEY_STREAM_USAGE, &DistributedHardware::IsInt32),
63 };
64
65 std::map<int32_t, std::string> eventNameMap = {
66 std::make_pair(EVENT_UNKNOWN, "EVENT_UNKNOWN"),
67 std::make_pair(OPEN_CTRL, "OPEN_CTRL"),
68 std::make_pair(CLOSE_CTRL, "CLOSE_CTRL"),
69 std::make_pair(CTRL_OPENED, "CTRL_OPENED"),
70 std::make_pair(CTRL_CLOSED, "CTRL_CLOSED"),
71 std::make_pair(NOTIFY_OPEN_CTRL_RESULT, "NOTIFY_OPEN_CTRL_RESULT"),
72 std::make_pair(NOTIFY_CLOSE_CTRL_RESULT, "NOTIFY_CLOSE_CTRL_RESULT"),
73 std::make_pair(DATA_OPENED, "DATA_OPENED"),
74 std::make_pair(DATA_CLOSED, "DATA_CLOSED"),
75
76 std::make_pair(OPEN_SPEAKER, "OPEN_SPEAKER"),
77 std::make_pair(CLOSE_SPEAKER, "CLOSE_SPEAKER"),
78 std::make_pair(SPEAKER_OPENED, "SPEAKER_OPENED"),
79 std::make_pair(SPEAKER_CLOSED, "SPEAKER_CLOSED"),
80 std::make_pair(NOTIFY_OPEN_SPEAKER_RESULT, "NOTIFY_OPEN_SPEAKER_RESULT"),
81 std::make_pair(NOTIFY_CLOSE_SPEAKER_RESULT, "NOTIFY_CLOSE_SPEAKER_RESULT"),
82 std::make_pair(NOTIFY_HDF_SPK_DUMP, "NOTIFY_HDF_SPK_DUMP"),
83 std::make_pair(NOTIFY_HDF_MIC_DUMP, "NOTIFY_HDF_MIC_DUMP"),
84
85 std::make_pair(OPEN_MIC, "OPEN_MIC"),
86 std::make_pair(CLOSE_MIC, "CLOSE_MIC"),
87 std::make_pair(MIC_OPENED, "MIC_OPENED"),
88 std::make_pair(MIC_CLOSED, "MIC_CLOSED"),
89 std::make_pair(NOTIFY_OPEN_MIC_RESULT, "NOTIFY_OPEN_MIC_RESULT"),
90 std::make_pair(NOTIFY_CLOSE_MIC_RESULT, "NOTIFY_CLOSE_MIC_RESULT"),
91
92 std::make_pair(VOLUME_SET, "VOLUME_SET"),
93 std::make_pair(VOLUME_GET, "VOLUME_GET"),
94 std::make_pair(VOLUME_CHANGE, "VOLUME_CHANGE"),
95 std::make_pair(VOLUME_MIN_GET, "VOLUME_MIN_GET"),
96 std::make_pair(VOLUME_MAX_GET, "VOLUME_MAX_GET"),
97 std::make_pair(VOLUME_MUTE_SET, "VOLUME_MUTE_SET"),
98
99 std::make_pair(AUDIO_FOCUS_CHANGE, "AUDIO_FOCUS_CHANGE"),
100 std::make_pair(AUDIO_RENDER_STATE_CHANGE, "AUDIO_RENDER_STATE_CHANGE"),
101
102 std::make_pair(SET_PARAM, "SET_PARAM"),
103 std::make_pair(SEND_PARAM, "SEND_PARAM"),
104
105 std::make_pair(AUDIO_ENCODER_ERR, "AUDIO_ENCODER_ERR"),
106 std::make_pair(AUDIO_DECODER_ERR, "AUDIO_DECODER_ERR"),
107
108 std::make_pair(CHANGE_PLAY_STATUS, "CHANGE_PLAY_STATUS"),
109
110 std::make_pair(MMAP_SPK_START, "MMAP_SPK_START"),
111 std::make_pair(MMAP_SPK_STOP, "MMAP_SPK_STOP"),
112 std::make_pair(MMAP_MIC_START, "MMAP_MIC_START"),
113 std::make_pair(MMAP_MIC_STOP, "MMAP_MIC_STOP"),
114 std::make_pair(AUDIO_START, "AUDIO_START"),
115 std::make_pair(AUDIO_STOP, "AUDIO_STOP")
116 };
117
GetEventNameByType(const int32_t eventType)118 std::string GetEventNameByType(const int32_t eventType)
119 {
120 auto iter = eventNameMap.find(eventType);
121 if (iter == eventNameMap.end()) {
122 DHLOGE("Can't find matched eventname");
123 return "EVENT_UNKNOWN";
124 }
125 return iter->second;
126 }
127
GetLocalDeviceNetworkId(std::string & networkId)128 int32_t GetLocalDeviceNetworkId(std::string &networkId)
129 {
130 NodeBasicInfo basicInfo = { { 0 } };
131 int32_t ret = GetLocalNodeDeviceInfo(PKG_NAME.c_str(), &basicInfo);
132 if (ret != DH_SUCCESS) {
133 DHLOGE("Failed to obtain the network ID of the local device. ret: %d", ret);
134 return ret;
135 }
136
137 networkId = std::string(basicInfo.networkId);
138 return DH_SUCCESS;
139 }
140
GetRandomID()141 std::string GetRandomID()
142 {
143 static std::random_device rd;
144 static std::uniform_int_distribution<uint64_t> dist(0ULL, 0xFFFFFFFFFFFFFFFFULL);
145 uint64_t ab = dist(rd);
146 uint64_t cd = dist(rd);
147 uint32_t a, b, c, d;
148 std::stringstream ss;
149 ab = (ab & 0xFFFFFFFFFFFF0FFFULL) | 0x0000000000004000ULL;
150 cd = (cd & 0x3FFFFFFFFFFFFFFFULL) | 0x8000000000000000ULL;
151 a = (ab >> 32U);
152 b = (ab & 0xFFFFFFFFU);
153 c = (cd >> 32U);
154 d = (cd & 0xFFFFFFFFU);
155 ss << std::hex << std::nouppercase << std::setfill('0');
156 ss << std::setw(WORD_WIDTH_8) << (a);
157 ss << std::setw(WORD_WIDTH_4) << (b >> 16U);
158 ss << std::setw(WORD_WIDTH_4) << (b & 0xFFFFU);
159 ss << std::setw(WORD_WIDTH_4) << (c >> 16U);
160 ss << std::setw(WORD_WIDTH_4) << (c & 0xFFFFU);
161 ss << std::setw(WORD_WIDTH_8) << d;
162
163 return ss.str();
164 }
165
GetAnonyString(const std::string & value)166 std::string GetAnonyString(const std::string &value)
167 {
168 std::string res;
169 std::string tmpStr("******");
170 size_t strLen = value.length();
171 if (strLen < INT32_MIN_ID_LENGTH) {
172 return tmpStr;
173 }
174
175 if (strLen <= INT32_SHORT_ID_LENGTH) {
176 res += value[0];
177 res += tmpStr;
178 res += value[strLen - 1];
179 } else {
180 res.append(value, 0, INT32_PLAINTEXT_LENGTH);
181 res += tmpStr;
182 res.append(value, strLen - INT32_PLAINTEXT_LENGTH, INT32_PLAINTEXT_LENGTH);
183 }
184
185 return res;
186 }
187
GetDevTypeByDHId(int32_t dhId)188 int32_t GetDevTypeByDHId(int32_t dhId)
189 {
190 DHLOGI("Get dev type by dhId: %d.", dhId);
191 if (static_cast<uint32_t>(dhId) & 0x8000000) {
192 return AUDIO_DEVICE_TYPE_MIC;
193 } else if (static_cast<uint32_t>(dhId) & 0x7ffffff) {
194 return AUDIO_DEVICE_TYPE_SPEAKER;
195 }
196 return AUDIO_DEVICE_TYPE_UNKNOWN;
197 }
198
GetCurrentTime(int64_t & tvSec,int64_t & tvNSec)199 void GetCurrentTime(int64_t &tvSec, int64_t &tvNSec)
200 {
201 struct timespec time;
202 if (clock_gettime(CLOCK_MONOTONIC, &time) < 0) {
203 DHLOGE("Get current time failed");
204 }
205 tvSec = time.tv_sec;
206 tvNSec = time.tv_nsec;
207 }
208
GetNowTimeUs()209 int64_t GetNowTimeUs()
210 {
211 std::chrono::microseconds nowUs =
212 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
213 return nowUs.count();
214 }
215
GetAudioParamStr(const std::string & params,const std::string & key,std::string & value)216 int32_t GetAudioParamStr(const std::string ¶ms, const std::string &key, std::string &value)
217 {
218 size_t step = key.size();
219 if (step >= params.size()) {
220 return ERR_DH_AUDIO_FAILED;
221 }
222 size_t pos = params.find(key);
223 if (pos == params.npos || params.at(pos + step) != '=') {
224 return ERR_DH_AUDIO_NOT_FOUND_KEY;
225 }
226 size_t splitPosEnd = params.find(';', pos);
227 if (splitPosEnd != params.npos) {
228 value = params.substr(pos + step + 1, splitPosEnd - pos - step - 1);
229 } else {
230 value = params.substr(pos + step + 1);
231 }
232 return DH_SUCCESS;
233 }
234
GetAudioParamBool(const std::string & params,const std::string & key,bool & value)235 int32_t GetAudioParamBool(const std::string ¶ms, const std::string &key, bool &value)
236 {
237 std::string val;
238 int32_t ret = GetAudioParamStr(params, key, val);
239 if (ret != DH_SUCCESS) {
240 DHLOGE("Get audio param string fail, error code %d.", ret);
241 return ret;
242 }
243
244 value = (val != "0");
245 return DH_SUCCESS;
246 }
247
GetAudioParamInt(const std::string & params,const std::string & key,int32_t & value)248 int32_t GetAudioParamInt(const std::string ¶ms, const std::string &key, int32_t &value)
249 {
250 std::string val = "0";
251 int32_t ret = GetAudioParamStr(params, key, val);
252 if (ret != DH_SUCCESS) {
253 DHLOGE("Get audio param string fail, error code %d.", ret);
254 return ret;
255 }
256 if (!CheckIsNum(val)) {
257 DHLOGE("String is not number. str:%s.", val.c_str());
258 return ERR_DH_AUDIO_NOT_SUPPORT;
259 }
260 value = std::stoi(val);
261 return DH_SUCCESS;
262 }
263
JsonParamCheck(const json & jsonObj,const std::initializer_list<std::string> & keys)264 bool JsonParamCheck(const json &jsonObj, const std::initializer_list<std::string> &keys)
265 {
266 if (jsonObj.is_discarded()) {
267 DHLOGE("Json parameter is invalid.");
268 return false;
269 }
270
271 for (auto it = keys.begin(); it != keys.end(); it++) {
272 if (!jsonObj.contains(*it)) {
273 DHLOGE("Json parameter not contain param(%s).", (*it).c_str());
274 return false;
275 }
276
277 auto iter = typeCheckMap.find(*it);
278 if (iter == typeCheckMap.end()) {
279 DHLOGE("Check is not supported yet, key %s.", (*it).c_str());
280 return false;
281 }
282 JsonTypeCheckFunc &func = iter->second;
283 bool res = (*func)(jsonObj, *it);
284 if (!res) {
285 DHLOGE("The key %s value format in json is illegal.", (*it).c_str());
286 return false;
287 }
288 }
289 return true;
290 }
291
IsString(const cJSON * jsonObj,const std::string & key)292 static bool IsString(const cJSON *jsonObj, const std::string &key)
293 {
294 if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) {
295 DHLOGE("JSON parameter is invalid.");
296 return false;
297 }
298 cJSON *paramValue = cJSON_GetObjectItemCaseSensitive(jsonObj, key.c_str());
299 if (paramValue == nullptr) {
300 DHLOGE("paramValue is null");
301 return false;
302 }
303
304 if (cJSON_IsString(paramValue)) {
305 return true;
306 }
307 return false;
308 }
309
CJsonParamCheck(const cJSON * jsonObj,const std::initializer_list<std::string> & keys)310 bool CJsonParamCheck(const cJSON *jsonObj, const std::initializer_list<std::string> &keys)
311 {
312 if (jsonObj == nullptr || !cJSON_IsObject(jsonObj)) {
313 DHLOGE("JSON parameter is invalid.");
314 return false;
315 }
316
317 for (auto it = keys.begin(); it != keys.end(); it++) {
318 cJSON *paramValue = cJSON_GetObjectItemCaseSensitive(jsonObj, (*it).c_str());
319 if (paramValue == nullptr) {
320 DHLOGE("JSON parameter does not contain key: %s", (*it).c_str());
321 return false;
322 }
323 bool res = IsString(jsonObj, *it);
324 if (!res) {
325 DHLOGE("The key %s value format in JSON is illegal.", (*it).c_str());
326 return false;
327 }
328 }
329 return true;
330 }
331
IsString(const json & jsonObj,const std::string & key)332 bool IsString(const json &jsonObj, const std::string &key)
333 {
334 return jsonObj[key].is_string();
335 }
336
IsInt32(const json & jsonObj,const std::string & key)337 bool IsInt32(const json &jsonObj, const std::string &key)
338 {
339 return jsonObj[key].is_number_integer() && INT32_MIN <= jsonObj[key] && jsonObj[key] <= INT32_MAX;
340 }
341
IsAudioParam(const json & jsonObj,const std::string & key)342 bool IsAudioParam(const json &jsonObj, const std::string &key)
343 {
344 return JsonParamCheck(jsonObj[key],
345 { KEY_SAMPLING_RATE, KEY_CHANNELS, KEY_FORMAT, KEY_SOURCE_TYPE, KEY_CONTENT_TYPE, KEY_STREAM_USAGE });
346 }
347
CalculateSampleNum(uint32_t sampleRate,uint32_t timems)348 int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems)
349 {
350 return (sampleRate * timems) / AUDIO_MS_PER_SECOND;
351 }
352
GetCurNano()353 int64_t GetCurNano()
354 {
355 int64_t result = -1;
356 struct timespec time;
357 clockid_t clockId = CLOCK_MONOTONIC;
358 int ret = clock_gettime(clockId, &time);
359 if (ret < 0) {
360 DHLOGE("GetCurNanoTime fail, ret: %d", ret);
361 return result;
362 }
363 result = (time.tv_sec * AUDIO_NS_PER_SECOND) + time.tv_nsec;
364 return result;
365 }
366
AbsoluteSleep(int64_t nanoTime)367 int32_t AbsoluteSleep(int64_t nanoTime)
368 {
369 int32_t ret = -1;
370 if (nanoTime <= 0) {
371 DHLOGE("AbsoluteSleep invalid sleep time : %d ns", nanoTime);
372 return ret;
373 }
374 struct timespec time;
375 time.tv_sec = nanoTime / AUDIO_NS_PER_SECOND;
376 time.tv_nsec = nanoTime - (time.tv_sec * AUDIO_NS_PER_SECOND);
377
378 clockid_t clockId = CLOCK_MONOTONIC;
379 ret = clock_nanosleep(clockId, TIMER_ABSTIME, &time, nullptr);
380 if (ret != 0) {
381 DHLOGE("AbsoluteSleep may failed, ret is : %d", ret);
382 }
383 return ret;
384 }
385
CalculateOffset(const int64_t frameIndex,const int64_t framePeriodNs,const int64_t startTime)386 int64_t CalculateOffset(const int64_t frameIndex, const int64_t framePeriodNs, const int64_t startTime)
387 {
388 int64_t totalOffset = GetCurNano() - startTime;
389 return totalOffset - frameIndex * framePeriodNs;
390 }
391
UpdateTimeOffset(const int64_t frameIndex,const int64_t framePeriodNs,int64_t & startTime)392 int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime)
393 {
394 int64_t timeOffset = 0;
395 if (frameIndex == 0) {
396 startTime = GetCurNano();
397 } else if (frameIndex % AUDIO_OFFSET_FRAME_NUM == 0) {
398 timeOffset = CalculateOffset(frameIndex, framePeriodNs, startTime);
399 }
400 return timeOffset;
401 }
402
CheckIsNum(const std::string & jsonString)403 bool CheckIsNum(const std::string &jsonString)
404 {
405 if (jsonString.empty() || jsonString.size() > MAX_KEY_DH_ID_LEN) {
406 DHLOGE("Json string size %d, is zero or too long.", jsonString.size());
407 return false;
408 }
409 for (char const &c : jsonString) {
410 if (!std::isdigit(c)) {
411 DHLOGE("Json string is not number.");
412 return false;
413 }
414 }
415 return true;
416 }
417
CheckDevIdIsLegal(const std::string & devId)418 bool CheckDevIdIsLegal(const std::string &devId)
419 {
420 if (devId.empty() || devId.size() > DAUDIO_MAX_DEVICE_ID_LEN) {
421 DHLOGE("DevId size %d, is zero or too long.", devId.size());
422 return false;
423 }
424 for (char const &c : devId) {
425 if (!std::isalnum(c)) {
426 DHLOGE("DevId is not number or letter.");
427 return false;
428 }
429 }
430 return true;
431 }
432
IsOutDurationRange(int64_t startTime,int64_t endTime,int64_t lastStartTime)433 bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime)
434 {
435 int64_t currentInterval = endTime - startTime;
436 int64_t twiceInterval = startTime - lastStartTime;
437 return (currentInterval > MAX_TIME_INTERVAL_US || twiceInterval > MAX_TIME_INTERVAL_US) ? true : false;
438 }
439
GetCJsonString(const char * key,const char * value)440 std::string GetCJsonString(const char *key, const char *value)
441 {
442 cJSON *jParam = cJSON_CreateObject();
443 if (jParam == nullptr) {
444 DHLOGE("Failed to create cJSON object.");
445 return "Failed to create cJSON object.";
446 }
447 cJSON_AddStringToObject(jParam, key, value);
448 char *jsonData = cJSON_PrintUnformatted(jParam);
449 if (jsonData == nullptr) {
450 DHLOGE("Failed to create JSON data.");
451 cJSON_Delete(jParam);
452 return "Failed to create JSON data.";
453 }
454 std::string content(jsonData);
455 cJSON_Delete(jParam);
456 cJSON_free(jsonData);
457 DHLOGD("create cJSON success : %s", content.c_str());
458 return content;
459 }
460
ParseStringFromArgs(std::string args,const char * key)461 std::string ParseStringFromArgs(std::string args, const char *key)
462 {
463 DHLOGD("ParseStringFrom Args : %s", args.c_str());
464 cJSON *jParam = cJSON_Parse(args.c_str());
465 if (jParam == nullptr) {
466 DHLOGE("Failed to parse JSON: %s", cJSON_GetErrorPtr());
467 return "Failed to parse JSON";
468 }
469 if (!CJsonParamCheck(jParam, { key })) {
470 DHLOGE("Not found the key : %s.", key);
471 cJSON_Delete(jParam);
472 return "Not found the key.";
473 }
474 cJSON *dhIdItem = cJSON_GetObjectItem(jParam, key);
475 if (dhIdItem == NULL || !cJSON_IsString(dhIdItem)) {
476 DHLOGE("Not found the value of the key : %s.", key);
477 cJSON_Delete(jParam);
478 return "Not found the value.";
479 }
480 std::string content(dhIdItem->valuestring);
481 cJSON_Delete(jParam);
482 DHLOGD("Parsed string is: %s.", content.c_str());
483 return content;
484 }
485
486 template <typename T>
GetSysPara(const char * key,T & value)487 bool GetSysPara(const char *key, T &value)
488 {
489 if (key == nullptr) {
490 DHLOGE("GetSysPara: key is nullptr");
491 return false;
492 }
493 char paraValue[20] = {0}; // 20 for system parameter
494 auto res = GetParameter(key, "-1", paraValue, sizeof(paraValue));
495 if (res <= 0) {
496 DHLOGD("GetSysPara fail, key:%{public}s res:%{public}d", key, res);
497 return false;
498 }
499 DHLOGI("GetSysPara: key:%{public}s value:%{public}s", key, paraValue);
500 std::stringstream valueStr;
501 valueStr << paraValue;
502 valueStr >> value;
503 return true;
504 }
505
506 template bool GetSysPara(const char *key, int32_t &value);
507 template bool GetSysPara(const char *key, uint32_t &value);
508 template bool GetSysPara(const char *key, int64_t &value);
509 template bool GetSysPara(const char *key, std::string &value);
510
IsParamEnabled(const std::string & key,bool & isEnabled)511 bool IsParamEnabled(const std::string &key, bool &isEnabled)
512 {
513 // by default: old trans
514 int32_t policyFlag = 0;
515 if (GetSysPara(key.c_str(), policyFlag) && policyFlag == 1) {
516 isEnabled = true;
517 return true;
518 }
519 isEnabled = false;
520 return false;
521 }
522
SaveFile(std::string fileName,uint8_t * audioData,int32_t size)523 void SaveFile(std::string fileName, uint8_t *audioData, int32_t size)
524 {
525 std::ofstream ofs(fileName, std::ios::binary | std::ios::out | std::ios::app);
526 if (!ofs.is_open()) {
527 DHLOGE("open file failed");
528 return;
529 }
530 ofs.write(reinterpret_cast<char*>(audioData), size);
531 ofs.close();
532 }
533 } // namespace DistributedHardware
534 } // namespace OHOS