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_DISTRIBUTED_INPUT_UTILS_TOOL_H
17 #define OHOS_DISTRIBUTED_INPUT_UTILS_TOOL_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include <linux/input.h>
23 #include "nlohmann/json.hpp"
24 
25 #include "dinput_log.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace DistributedInput {
30 struct DevInfo {
31     std::string networkId;
32     std::string deviceName;
33     uint16_t deviceType;
34 };
35 
36 DevInfo GetLocalDeviceInfo();
37 std::string GetLocalNetworkId();
38 std::string GetUUIDBySoftBus(const std::string &networkId);
39 uint64_t GetCurrentTimeUs();
40 std::string SetAnonyId(const std::string &message);
41 /* IsString, IsUInt32 and IsUInt64 are used to valid json key */
42 bool IsBoolean(const nlohmann::json &jsonObj, const std::string &key);
43 bool IsString(const nlohmann::json &jsonObj, const std::string &key);
44 bool IsInt32(const nlohmann::json &jsonObj, const std::string &key);
45 bool IsInt64(const nlohmann::json &jsonObj, const std::string &key);
46 bool IsUInt16(const nlohmann::json &jsonObj, const std::string &key);
47 bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key);
48 bool IsUInt64(const nlohmann::json &jsonObj, const std::string &key);
49 bool IsArray(const nlohmann::json &jsonObj, const std::string &key);
50 std::string GetNodeDesc(std::string parameters);
51 std::string GetAnonyString(const std::string &value);
52 std::string GetAnonyInt32(const int32_t value);
53 std::string Sha256(const std::string &string);
54 void CloseFd(int &fd);
55 int BitIsSet(const unsigned long *array, int bit);
56 void SplitStringToVector(const std::string &str, const char split, std::vector<std::string> &vecStr);
57 int OpenInputDeviceFdByPath(const std::string &devicePath);
58 std::string ConvertErrNo();
59 void ScanInputDevicesPath(const std::string &dirName, std::vector<std::string> &vecInputDevPath);
60 
61 /**
62  * If we pressed the sink keyboard while mouse pass throuth from source to sink, we need reset the virtual keyboard
63  * key states at the source side.
64  */
65 void ResetVirtualDevicePressedKeys(const std::vector<std::string> &nodePaths);
66 
67 std::string GetString(const std::vector<std::string> &vec);
68 int32_t GetRandomInt32(int32_t randMin, int32_t randMax);
69 std::string JointDhIds(const std::vector<std::string> &dhids);
70 std::vector<std::string> SplitDhIdString(const std::string &dhIdsString);
71 } // namespace DistributedInput
72 } // namespace DistributedHardware
73 } // namespace OHOS
74 #endif // OHOS_DISTRIBUTED_INPUT_UTILS_TOOL_H