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 OHOS_DM_ANONYMOUS_H
17 #define OHOS_DM_ANONYMOUS_H
18
19 #include <map>
20 #include <string>
21
22 #include "dm_device_info.h"
23 #include "json_object.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 extern const char* PRINT_LIST_SPLIT;
28 extern const int32_t LIST_SPLIT_LEN;
29
30 DM_EXPORT std::string GetAnonyString(const std::string &value);
31 std::string GetAnonyStringList(const std::vector<std::string> &values);
32 std::string GetAnonyInt32(const int32_t value);
33 std::string GetAnonyInt32List(const std::vector<int32_t> &values);
34 bool IsNumberString(const std::string &inputString);
35 bool IsString(const JsonItemObject &jsonObj, const std::string &key);
36 DM_EXPORT bool IsInt32(const JsonItemObject &jsonObj, const std::string &key);
37 bool IsUint32(const JsonItemObject &jsonObj, const std::string &key);
38 bool IsInt64(const JsonItemObject &jsonObj, const std::string &key);
39 bool IsUint64(const JsonItemObject &jsonObj, const std::string &key);
40 bool IsArray(const JsonItemObject &jsonObj, const std::string &key);
41 bool IsBool(const JsonItemObject &jsonObj, const std::string &key);
42 std::string ConvertMapToJsonString(const std::map<std::string, std::string> ¶mMap);
43 void ParseMapFromJsonString(const std::string &jsonStr, std::map<std::string, std::string> ¶mMap);
44 bool IsInvalidPeerTargetId(const PeerTargetId &targetId);
45 std::string ConvertCharArray2String(const char *srcData, uint32_t srcLen);
46 int32_t StringToInt(const std::string &str, int32_t base);
47 int64_t StringToInt64(const std::string &str, int32_t base);
48 void VersionSplitToInt(const std::string &str, const char split, std::vector<int32_t> &numVec);
49 bool CompareVecNum(const std::vector<int32_t> &srcVecNum, const std::vector<int32_t> &sinkVecNum);
50 bool CompareVersion(const std::string &remoteVersion, const std::string &oldVersion);
51 bool GetVersionNumber(const std::string dmVersion, int32_t &versionNum);
52 std::string ComposeStr(const std::string &pkgName, uint16_t subscribeId);
53 std::string GetCallerPkgName(const std::string &pkgName);
54 uint16_t GetSubscribeId(const std::string &pkgName);
55 template <class T>
GetAnonyInteger(const T value)56 std::string GetAnonyInteger(const T value)
57 {
58 std::string tempString = std::to_string(value);
59 size_t length = tempString.length();
60 if (length == 0x01) {
61 tempString[0] = '*';
62 return tempString;
63 }
64 for (size_t i = 1; i < length - 1; i++) {
65 tempString[i] = '*';
66 }
67 return tempString;
68 }
69
70 template <class T>
GetAnonyIntegerList(const std::vector<T> & values)71 std::string GetAnonyIntegerList(const std::vector<T> &values)
72 {
73 std::string temp = "[ ";
74 bool flag = false;
75 for (auto const &v : values) {
76 temp += GetAnonyInteger(v) + PRINT_LIST_SPLIT;
77 flag = true;
78 }
79 if (flag) {
80 temp.erase(temp.length() - LIST_SPLIT_LEN);
81 }
82 temp += " ]";
83 return temp;
84 }
85
86 template <class T>
GetIntegerList(const std::vector<T> & values)87 std::string GetIntegerList(const std::vector<T> &values)
88 {
89 std::string temp = "[ ";
90 bool flag = false;
91 for (auto const &v : values) {
92 temp += std::to_string(v) + PRINT_LIST_SPLIT;
93 flag = true;
94 }
95 if (flag) {
96 temp.erase(temp.length() - LIST_SPLIT_LEN);
97 }
98 temp += " ]";
99 return temp;
100 }
101
102 bool IsIdLengthValid(const std::string &inputID);
103 bool IsMessageLengthValid(const std::string &inputMessage);
104 bool IsValueExist(const std::multimap<std::string, int32_t> unorderedmap, const std::string &udid, int32_t userId);
105 bool IsDmCommonNotifyEventValid(DmCommonNotifyEvent dmCommonNotifyEvent);
106 std::string GetSubStr(const std::string &rawStr, const std::string &separator, int32_t index);
107 bool IsJsonValIntegerString(const JsonItemObject &jsonObj, const std::string &key);
108 std::string GetAnonyJsonString(const std::string &value);
109 int64_t GetCurrentTimestamp();
110 } // namespace DistributedHardware
111 } // namespace OHOS
112 #endif // OHOS_DM_ANONYMOUS_H
113