• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SOFT_BUS_MANAGER_H
17 #define SOFT_BUS_MANAGER_H
18 
19 #include <functional>
20 #include <cinttypes>
21 #include <memory>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 
26 #include "device_manager.h"
27 #include "remote_command_executor.h"
28 #include "socket.h"
29 
30 namespace OHOS {
31 namespace Security {
32 namespace AccessToken {
33 class SoftBusManager final {
34 public:
35     virtual ~SoftBusManager();
36 
37     /**
38      * @brief Get instance of SoftBusManager
39      *
40      * @return SoftBusManager's instance.
41      * @since 1.0
42      * @version 1.0
43      */
44     static SoftBusManager &GetInstance();
45 
46     /**
47      * @brief Bind soft bus service.
48      *
49      * @since 1.0
50      * @version 1.0
51      */
52     void Initialize();
53 
54     /**
55      * @brief Unbind soft bus service when DPMS has been destroyed.
56      *
57      * @since 1.0
58      * @version 1.0
59      */
60     void Destroy();
61 
62     /**
63      * @brief Open session with the peer device sychronized.
64      *
65      * @param deviceUdid The udid of peer device.
66      * @return Session id if open successfully, otherwise return -1(Constant::FAILURE).
67      * @since 1.0
68      * @version 1.0
69      */
70     int BindService(const std::string &deviceUdid);
71 
72     /**
73      * @brief Close socket with the peer device.
74      *
75      * @param socketFd The socket id need to close.
76      * @return 0 if close successfully, otherwise return -1(Constant::FAILURE).
77      * @since 1.0
78      * @version 1.0
79      */
80     int CloseSocket(int socketFd);
81 
82     /**
83      * @brief Get UUID(networkId) by deviceNodeId.
84      *
85      * @param deviceNodeId The valid networkId or deviceId(UDID) or deviceUuid.
86      * @return uuid if deviceManager is ready, empty string otherwise.
87      * @since 1.0
88      * @version 1.0
89      */
90     std::string GetUniversallyUniqueIdByNodeId(const std::string &deviceNodeId);
91 
92     /**
93      *  @brief Get deviceId(UDID) by deviceNodeId.
94      *
95      * @param deviceNodeId The valid networkId or deviceId(UDID) or deviceUuid.
96      * @return udid if deviceManager work correctly, empty string otherwise.
97      * @since 1.0
98      * @version 1.0
99      */
100     std::string GetUniqueDeviceIdByNodeId(const std::string &deviceNodeId);
101 
102     bool GetNetworkIdBySocket(const int32_t socket, std::string& networkId);
103 
104 public:
105     static const std::string SESSION_NAME;
106 
107 private:
108     SoftBusManager();
109     int DeviceInit();
110     bool CheckAndCopyStr(char* dest, uint32_t destLen, const std::string& src);
111     int32_t InitSocketAndListener(const std::string& networkId, ISocketListener& listener);
112     int32_t ServiceSocketInit();
113 
114     /**
115      * @brief Fulfill local device info
116      *
117      * @return 0 if operate successfully, otherwise return -1(Constant::FAILURE).
118      * @since 1.0
119      * @version 1.0
120      */
121     int FulfillLocalDeviceInfo();
122 
123     /**
124      * @brief add all trusted device info.
125      *
126      * @since 1.0
127      * @version 1.0
128      */
129     int AddTrustedDeviceInfo();
130 
131     std::string GetUuidByNodeId(const std::string &nodeId) const;
132     std::string GetUdidByNodeId(const std::string &nodeId) const;
133 
134     // soft bus session server opened flag
135     bool isSoftBusServiceBindSuccess_;
136     std::atomic_bool inited_;
137 
138     // init mutex
139     std::mutex mutex_;
140 
141     // fulfill thread mutex
142     std::mutex fulfillMutex_;
143 
144     // soft bus service socket fd
145     int32_t socketFd_ = -1;
146 
147     // soft bus client socket with networkId map
148     std::mutex clientSocketMutex_;
149     std::map<int32_t, std::string> clientSocketMap_;
150 };
151 }  // namespace AccessToken
152 }  // namespace Security
153 }  // namespace OHOS
154 #endif  // SOFT_BUS_MANAGER_H
155