150a07fd2Sopenharmony_ci/*
250a07fd2Sopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
350a07fd2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
450a07fd2Sopenharmony_ci * you may not use this file except in compliance with the License.
550a07fd2Sopenharmony_ci * You may obtain a copy of the License at
650a07fd2Sopenharmony_ci *
750a07fd2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
850a07fd2Sopenharmony_ci *
950a07fd2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1050a07fd2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1150a07fd2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1250a07fd2Sopenharmony_ci * See the License for the specific language governing permissions and
1350a07fd2Sopenharmony_ci * limitations under the License.
1450a07fd2Sopenharmony_ci */
1550a07fd2Sopenharmony_ci
1650a07fd2Sopenharmony_ci#ifndef OHOS_DAUDIO_UTIL_H
1750a07fd2Sopenharmony_ci#define OHOS_DAUDIO_UTIL_H
1850a07fd2Sopenharmony_ci
1950a07fd2Sopenharmony_ci#include <chrono>
2050a07fd2Sopenharmony_ci#include <fstream>
2150a07fd2Sopenharmony_ci#include <map>
2250a07fd2Sopenharmony_ci#include <string>
2350a07fd2Sopenharmony_ci#include "cJSON.h"
2450a07fd2Sopenharmony_ci
2550a07fd2Sopenharmony_ci#define AUDIO_MS_PER_SECOND 1000
2650a07fd2Sopenharmony_ci#define AUDIO_US_PER_SECOND 1000000
2750a07fd2Sopenharmony_ci#define AUDIO_NS_PER_SECOND ((int64_t)1000000000)
2850a07fd2Sopenharmony_cinamespace OHOS {
2950a07fd2Sopenharmony_cinamespace DistributedHardware {
3050a07fd2Sopenharmony_ciconst std::string DUMP_SERVER_PARA = "sys.daudio.dump.write.enable";
3150a07fd2Sopenharmony_ciconst std::string DUMP_SERVICE_DIR = "/data/local/tmp/";
3250a07fd2Sopenharmony_ci
3350a07fd2Sopenharmony_ciint32_t GetLocalDeviceNetworkId(std::string &networkId);
3450a07fd2Sopenharmony_cistd::string GetRandomID();
3550a07fd2Sopenharmony_cistd::string GetAnonyString(const std::string &value);
3650a07fd2Sopenharmony_ciint32_t GetDevTypeByDHId(int32_t dhId);
3750a07fd2Sopenharmony_ciint64_t GetNowTimeUs();
3850a07fd2Sopenharmony_ciint32_t GetAudioParamStr(const std::string &params, const std::string &key, std::string &value);
3950a07fd2Sopenharmony_ciint32_t GetAudioParamBool(const std::string &params, const std::string &key, bool &value);
4050a07fd2Sopenharmony_ciint32_t GetAudioParamInt(const std::string &params, const std::string &key, int32_t &value);
4150a07fd2Sopenharmony_cibool CJsonParamCheck(const cJSON *jsonObj, const std::initializer_list<std::string> &keys);
4250a07fd2Sopenharmony_cibool IsString(const cJSON *jsonObj, const std::string &key);
4350a07fd2Sopenharmony_cibool IsInt32(const cJSON *jsonObj, const std::string &key);
4450a07fd2Sopenharmony_cibool IsAudioParam(const cJSON *jsonObj, const std::string &key);
4550a07fd2Sopenharmony_ciint32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems);
4650a07fd2Sopenharmony_ciint64_t GetCurNano();
4750a07fd2Sopenharmony_ciint32_t AbsoluteSleep(int64_t nanoTime);
4850a07fd2Sopenharmony_ciint64_t CalculateOffset(const int64_t frameIndex, const int64_t framePeriodNs, const int64_t startTime);
4950a07fd2Sopenharmony_ciint64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime);
5050a07fd2Sopenharmony_civoid GetCurrentTime(int64_t &tvSec, int64_t &tvNSec);
5150a07fd2Sopenharmony_cibool CheckIsNum(const std::string &jsonString);
5250a07fd2Sopenharmony_cibool CheckDevIdIsLegal(const std::string &devId);
5350a07fd2Sopenharmony_cibool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime);
5450a07fd2Sopenharmony_civoid SaveFile(std::string fileName, uint8_t *audioData, int32_t size);
5550a07fd2Sopenharmony_cistd::string GetCJsonString(const char *key, const char *value);
5650a07fd2Sopenharmony_cistd::string ParseStringFromArgs(std::string args, const char *key);
5750a07fd2Sopenharmony_cistd::string GetEventNameByType(const int32_t eventType);
5850a07fd2Sopenharmony_ci
5950a07fd2Sopenharmony_citemplate <typename T>
6050a07fd2Sopenharmony_cibool GetSysPara(const char *key, T &value);
6150a07fd2Sopenharmony_cibool IsParamEnabled(const std::string &key, bool &isEnabled);
6250a07fd2Sopenharmony_ci
6350a07fd2Sopenharmony_ciclass DumpFileUtil {
6450a07fd2Sopenharmony_cipublic:
6550a07fd2Sopenharmony_ci    static void OpenDumpFile(const std::string &para, const std::string &fileName, FILE **file);
6650a07fd2Sopenharmony_ci    static void CloseDumpFile(FILE **dumpFile);
6750a07fd2Sopenharmony_ci    static void WriteDumpFile(FILE *dumpFile, void *buffer, size_t bufferSize);
6850a07fd2Sopenharmony_ci
6950a07fd2Sopenharmony_ci    static std::map<std::string, std::string> g_lastPara;
7050a07fd2Sopenharmony_ci
7150a07fd2Sopenharmony_ciprivate:
7250a07fd2Sopenharmony_ci    static FILE *OpenDumpFileInner(const std::string &para, const std::string &fileName);
7350a07fd2Sopenharmony_ci    static void ChangeDumpFileState(const std::string &para, FILE **dumpFile, const std::string &fileName);
7450a07fd2Sopenharmony_ci};
7550a07fd2Sopenharmony_ci} // namespace DistributedHardware
7650a07fd2Sopenharmony_ci} // namespace OHOS
7750a07fd2Sopenharmony_ci#endif // OHOS_DAUDIO_UTIL_H
78