• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 NET_CONN_SERVICE_H
17 #define NET_CONN_SERVICE_H
18 
19 #include <functional>
20 #include <list>
21 #include <memory>
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 #include "singleton.h"
27 #include "system_ability.h"
28 
29 #include "network.h"
30 #include "net_activate.h"
31 #include "net_conn_event_handler.h"
32 #include "net_conn_service_iface.h"
33 #include "net_conn_service_stub.h"
34 #include "net_score.h"
35 #include "net_supplier.h"
36 #include "http_proxy.h"
37 
38 namespace OHOS {
39 namespace NetManagerStandard {
40 constexpr uint32_t MAX_REQUEST_NUM = 2000;
41 class NetConnService : public SystemAbility,
42                        public INetActivateCallback,
43                        public NetConnServiceStub,
44                        public std::enable_shared_from_this<NetConnService> {
45     DECLARE_DELAYED_SINGLETON(NetConnService)
46     DECLARE_SYSTEM_ABILITY(NetConnService)
47 
48     using NET_SUPPLIER_MAP = std::map<uint32_t, sptr<NetSupplier>>;
49     using NET_NETWORK_MAP = std::map<int32_t, std::shared_ptr<Network>>;
50     using NET_ACTIVATE_MAP = std::map<uint32_t, sptr<NetActivate>>;
51 
52 public:
53     void OnStart() override;
54     void OnStop() override;
55     /**
56      * The interface in NetConnService can be called when the system is ready
57      *
58      * @return Returns 0, the system is ready, otherwise the system is not ready
59      */
60     int32_t SystemReady() override;
61 
62     /**
63      * The interface is register the network
64      *
65      * @param bearerType Bearer Network Type
66      * @param ident Unique identification of mobile phone card
67      * @param netCaps Network capabilities registered by the network supplier
68      * @param supplierId out param, return supplier id
69      *
70      * @return function result
71      */
72     int32_t RegisterNetSupplier(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps,
73                                 uint32_t &supplierId) override;
74 
75     /**
76      * The interface is unregister the network
77      *
78      * @param supplierId The id of the network supplier
79      *
80      * @return Returns 0, unregister the network successfully, otherwise it will fail
81      */
82     int32_t UnregisterNetSupplier(uint32_t supplierId) override;
83 
84     /**
85      * Register supplier callback
86      *
87      * @param supplierId The id of the network supplier
88      * @param callback INetSupplierCallback callback interface
89      *
90      * @return Returns 0, unregister the network successfully, otherwise it will fail
91      */
92     int32_t RegisterNetSupplierCallback(uint32_t supplierId, const sptr<INetSupplierCallback> &callback) override;
93 
94     /**
95      * Register net connection callback
96      *
97      * @param netSpecifier specifier information
98      * @param callback The callback of INetConnCallback interface
99      *
100      * @return Returns 0, successfully register net connection callback, otherwise it will failed
101      */
102     int32_t RegisterNetConnCallback(const sptr<INetConnCallback> &callback) override;
103 
104     /**
105      * Register net connection callback by NetSpecifier
106      *
107      * @param netSpecifier specifier information
108      * @param callback The callback of INetConnCallback interface
109      * @param timeoutMS net connection time out
110      *
111      * @return Returns 0, successfully register net connection callback, otherwise it will failed
112      */
113     int32_t RegisterNetConnCallback(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback,
114                                     const uint32_t &timeoutMS) override;
115 
116     /**
117      * Unregister net connection callback
118      *
119      * @return Returns 0, successfully unregister net connection callback, otherwise it will fail
120      */
121     int32_t UnregisterNetConnCallback(const sptr<INetConnCallback> &callback) override;
122 
123     int32_t UpdateNetStateForTest(const sptr<NetSpecifier> &netSpecifier, int32_t netState) override;
124     /**
125      * The interface is update network connection status information
126      *
127      * @param supplierId The id of the network supplier
128      * @param netSupplierInfo network connection status information
129      *
130      * @return Returns 0, successfully update the network connection status information, otherwise it will fail
131      */
132     int32_t UpdateNetSupplierInfo(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo) override;
133 
134     /**
135      * The interface is update network link attribute information
136      *
137      * @param supplierId The id of the network supplier
138      * @param netLinkInfo network link attribute information
139      *
140      * @return Returns 0, successfully update the network link attribute information, otherwise it will fail
141      */
142     int32_t UpdateNetLinkInfo(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo) override;
143 
144     /**
145      * The interface names which NetBearType is equal than bearerType
146      *
147      * @param bearerType Network bearer type
148      * @param ifaceNames save the obtained ifaceNames
149      * @return Returns 0, successfully get the network link attribute iface name, otherwise it will fail
150      */
151     int32_t GetIfaceNames(NetBearType bearerType, std::list<std::string> &ifaceNames) override;
152 
153     /**
154      * The interface is get the iface name for network
155      *
156      * @param bearerType Network bearer type
157      * @param ident Unique identification of mobile phone card
158      * @param ifaceName save the obtained ifaceName
159      * @return Returns 0, successfully get the network link attribute iface name, otherwise it will fail
160      */
161     int32_t GetIfaceNameByType(NetBearType bearerType, const std::string &ident, std::string &ifaceName) override;
162 
163     /**
164      * register network detection return result method
165      *
166      * @param netId  Network ID
167      * @param callback The callback of INetDetectionCallback interface
168      * @return int32_t  Returns 0, unregister the network successfully, otherwise it will fail
169      */
170     int32_t RegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback) override;
171 
172     /**
173      * unregister network detection return result method
174      *
175      * @param netId Network ID
176      * @param callback  The callback of INetDetectionCallback interface
177      * @return int32_t  Returns 0, unregister the network successfully, otherwise it will fail
178      */
179     int32_t UnRegisterNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback) override;
180 
181     /**
182      * The interface of network detection called by the application
183      *
184      * @param netId network ID
185      * @return int32_t Whether the network probe is successful
186      */
187     int32_t NetDetection(int32_t netId) override;
188     bool CheckGetDefaultNetPermission();
189     int32_t GetDefaultNet(int32_t &netId) override;
190     int32_t HasDefaultNet(bool &flag) override;
191     int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList) override;
192     int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) override;
193     int32_t GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList) override;
194     int32_t GetAllNets(std::list<int32_t> &netIdList) override;
195     int32_t GetSpecificUidNet(int32_t uid, int32_t &netId) override;
196     int32_t GetConnectionProperties(int32_t netId, NetLinkInfo &info) override;
197     int32_t GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap) override;
198     int32_t BindSocket(int32_t socket_fd, int32_t netId) override;
199     void HandleDetectionResult(uint32_t supplierId, bool ifValid);
200     int32_t RestrictBackgroundChanged(bool isRestrictBackground);
201     /**
202      * Set airplane mode
203      *
204      * @param state airplane state
205      * @return Returns 0, successfully set airplane mode, otherwise it will fail
206      */
207     int32_t SetAirplaneMode(bool state) override;
208     /**
209      * Dump
210      *
211      * @param fd file description
212      * @param args unused
213      * @return Returns 0, successfully get dump info, otherwise it will fail
214      */
215     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
216     /**
217      * Is default network metered
218      *
219      * @param save the metered state
220      * @return Returns 0, Successfully get whether the default network is metered, otherwise it will fail
221      */
222     int32_t IsDefaultNetMetered(bool &isMetered) override;
223 
224     /**
225      * Set http proxy server
226      *
227      * @param httpProxy the http proxy server
228      * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_HTTP_PROXY_INVALID if httpProxy is null string
229      */
230     int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy) override;
231 
232     /**
233      * Get http proxy server
234      *
235      * @param httpProxy output param, the http proxy server
236      * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_NO_HTTP_PROXY if httpProxy is null string
237      */
238     int32_t GetGlobalHttpProxy(HttpProxy &httpProxy) override;
239 
240     /**
241      * Get net id by identifier
242      *
243      * @param ident Net identifier
244      * @param netId output param, the net id
245      * @return NETMANAGER_SUCCESS if OK, ERR_NO_NET_IDENT if ident is null string
246      */
247     int32_t GetNetIdByIdentifier(const std::string &ident, int32_t &netId) override;
248 
249     /**
250      * Activate network timeout
251      *
252      * @param reqId Net request id
253      */
254     void OnNetActivateTimeOut(uint32_t reqId) override;
255 
256     int32_t SetAppNet(int32_t netId) override;
257 
258 private:
259     bool Init();
260     std::list<sptr<NetSupplier>> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident = "");
261     sptr<NetSupplier> GetNetSupplierFromList(
262         NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps);
263     int32_t ActivateNetwork(const sptr<NetSpecifier> &netSpecifier,
264         const sptr<INetConnCallback> &callback, const uint32_t &timeoutMS);
265     void CallbackForSupplier(sptr<NetSupplier>& supplier, CallbackType type);
266     void CallbackForAvailable(sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback);
267     uint32_t FindBestNetworkForRequest(sptr<NetSupplier>& supplier, sptr<NetActivate>& netActivateNetwork);
268     void SendRequestToAllNetwork(sptr<NetActivate> request);
269     void SendBestScoreAllNetwork(uint32_t reqId, int32_t bestScore, uint32_t supplierId);
270     void SendAllRequestToNetwork(sptr<NetSupplier> supplier);
271     void FindBestNetworkForAllRequest();
272     void MakeDefaultNetWork(sptr<NetSupplier>& oldService, sptr<NetSupplier>& newService);
273     void NotFindBestSupplier(uint32_t reqId, const sptr<NetActivate> &active,
274         const sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback);
275     void CreateDefaultRequest();
276     int32_t RegUnRegNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback, bool isReg);
277     int32_t GenerateNetId();
278     bool FindSameCallback(const sptr<INetConnCallback> &callback, uint32_t &reqId);
279     void GetDumpMessage(std::string &message);
280     sptr<NetSupplier> FindNetSupplier(uint32_t supplierId);
281     void SendGlobalHttpProxyChangeBroadcast();
282     void RequestAllNetworkExceptDefault();
283 
284 private:
285     enum ServiceRunningState {
286         STATE_STOPPED = 0,
287         STATE_RUNNING,
288     };
289 
290     bool registerToService_;
291     ServiceRunningState state_;
292     sptr<NetSpecifier> defaultNetSpecifier_ = nullptr;
293     sptr<NetActivate> defaultNetActivate_ = nullptr;
294     sptr<NetSupplier> defaultNetSupplier_ = nullptr;
295     NET_SUPPLIER_MAP netSuppliers_;
296     NET_ACTIVATE_MAP netActivates_;
297     NET_NETWORK_MAP networks_;
298     std::unique_ptr<NetScore> netScore_ = nullptr;
299     sptr<NetConnServiceIface> serviceIface_ = nullptr;
300     std::atomic<int32_t> netIdLastValue_ = MIN_NET_ID - 1;
301     HttpProxy httpProxy_;
302     std::mutex netManagerMutex_;
303     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ = nullptr;
304     std::shared_ptr<NetConnEventHandler> netConnEventHandler_ = nullptr;
305 };
306 } // namespace NetManagerStandard
307 } // namespace OHOS
308 #endif // NET_CONN_SERVICE_H
309