1 /*
2  * Copyright (c) 2022-2024 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 OHOS_DAUDIO_UTIL_H
17 #define OHOS_DAUDIO_UTIL_H
18 
19 #include <chrono>
20 #include <fstream>
21 #include <map>
22 #include <string>
23 #include "cJSON.h"
24 
25 #define AUDIO_MS_PER_SECOND 1000
26 #define AUDIO_US_PER_SECOND 1000000
27 #define AUDIO_NS_PER_SECOND ((int64_t)1000000000)
28 namespace OHOS {
29 namespace DistributedHardware {
30 const std::string DUMP_SERVER_PARA = "sys.daudio.dump.write.enable";
31 const std::string DUMP_SERVICE_DIR = "/data/local/tmp/";
32 
33 int32_t GetLocalDeviceNetworkId(std::string &networkId);
34 std::string GetRandomID();
35 std::string GetAnonyString(const std::string &value);
36 int32_t GetDevTypeByDHId(int32_t dhId);
37 int64_t GetNowTimeUs();
38 int32_t GetAudioParamStr(const std::string &params, const std::string &key, std::string &value);
39 int32_t GetAudioParamBool(const std::string &params, const std::string &key, bool &value);
40 int32_t GetAudioParamInt(const std::string &params, const std::string &key, int32_t &value);
41 bool CJsonParamCheck(const cJSON *jsonObj, const std::initializer_list<std::string> &keys);
42 bool IsString(const cJSON *jsonObj, const std::string &key);
43 bool IsInt32(const cJSON *jsonObj, const std::string &key);
44 bool IsAudioParam(const cJSON *jsonObj, const std::string &key);
45 int32_t CalculateSampleNum(uint32_t sampleRate, uint32_t timems);
46 int64_t GetCurNano();
47 int32_t AbsoluteSleep(int64_t nanoTime);
48 int64_t CalculateOffset(const int64_t frameIndex, const int64_t framePeriodNs, const int64_t startTime);
49 int64_t UpdateTimeOffset(const int64_t frameIndex, const int64_t framePeriodNs, int64_t &startTime);
50 void GetCurrentTime(int64_t &tvSec, int64_t &tvNSec);
51 bool CheckIsNum(const std::string &jsonString);
52 bool CheckDevIdIsLegal(const std::string &devId);
53 bool IsOutDurationRange(int64_t startTime, int64_t endTime, int64_t lastStartTime);
54 void SaveFile(std::string fileName, uint8_t *audioData, int32_t size);
55 std::string GetCJsonString(const char *key, const char *value);
56 std::string ParseStringFromArgs(std::string args, const char *key);
57 std::string GetEventNameByType(const int32_t eventType);
58 
59 template <typename T>
60 bool GetSysPara(const char *key, T &value);
61 bool IsParamEnabled(const std::string &key, bool &isEnabled);
62 
63 class DumpFileUtil {
64 public:
65     static void OpenDumpFile(const std::string &para, const std::string &fileName, FILE **file);
66     static void CloseDumpFile(FILE **dumpFile);
67     static void WriteDumpFile(FILE *dumpFile, void *buffer, size_t bufferSize);
68 
69     static std::map<std::string, std::string> g_lastPara;
70 
71 private:
72     static FILE *OpenDumpFileInner(const std::string &para, const std::string &fileName);
73     static void ChangeDumpFileState(const std::string &para, FILE **dumpFile, const std::string &fileName);
74 };
75 } // namespace DistributedHardware
76 } // namespace OHOS
77 #endif // OHOS_DAUDIO_UTIL_H
78