• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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     int32_t GetDefaultNet(int32_t &netId) override;
189     int32_t HasDefaultNet(bool &flag) override;
190     int32_t GetAddressesByName(const std::string &host, int32_t netId, std::vector<INetAddr> &addrList) override;
191     int32_t GetAddressByName(const std::string &host, int32_t netId, INetAddr &addr) override;
192     int32_t GetSpecificNet(NetBearType bearerType, std::list<int32_t> &netIdList) override;
193     int32_t GetAllNets(std::list<int32_t> &netIdList) override;
194     int32_t GetSpecificUidNet(int32_t uid, int32_t &netId) override;
195     int32_t GetConnectionProperties(int32_t netId, NetLinkInfo &info) override;
196     int32_t GetNetCapabilities(int32_t netId, NetAllCapabilities &netAllCap) override;
197     int32_t BindSocket(int32_t socket_fd, int32_t netId) override;
198     void HandleDetectionResult(uint32_t supplierId, bool ifValid);
199     int32_t RestrictBackgroundChanged(bool isRestrictBackground);
200     /**
201      * Set airplane mode
202      *
203      * @param state airplane state
204      * @return Returns 0, successfully set airplane mode, otherwise it will fail
205      */
206     int32_t SetAirplaneMode(bool state) override;
207     /**
208      * Dump
209      *
210      * @param fd file description
211      * @param args unused
212      * @return Returns 0, successfully get dump info, otherwise it will fail
213      */
214     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
215     /**
216      * Is default network metered
217      *
218      * @param save the metered state
219      * @return Returns 0, Successfully get whether the default network is metered, otherwise it will fail
220      */
221     int32_t IsDefaultNetMetered(bool &isMetered) override;
222 
223     /**
224      * Set http proxy server
225      *
226      * @param httpProxy the http proxy server
227      * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_HTTP_PROXY_INVALID if httpProxy is null string
228      */
229     int32_t SetGlobalHttpProxy(const HttpProxy &httpProxy) override;
230 
231     /**
232      * Get http proxy server
233      *
234      * @param httpProxy output param, the http proxy server
235      * @return NETMANAGER_SUCCESS if OK, NET_CONN_ERR_NO_HTTP_PROXY if httpProxy is null string
236      */
237     int32_t GetGlobalHttpProxy(HttpProxy &httpProxy) override;
238 
239     /**
240      * Get net id by identifier
241      *
242      * @param ident Net identifier
243      * @param netId output param, the net id
244      * @return NETMANAGER_SUCCESS if OK, ERR_NO_NET_IDENT if ident is null string
245      */
246     int32_t GetNetIdByIdentifier(const std::string &ident, int32_t &netId) override;
247 
248     /**
249      * Activate network timeout
250      *
251      * @param reqId Net request id
252      */
253     void OnNetActivateTimeOut(uint32_t reqId) override;
254 
255     int32_t SetAppNet(int32_t netId) override;
256 
257 private:
258     bool Init();
259     std::list<sptr<NetSupplier>> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident = "");
260     sptr<NetSupplier> GetNetSupplierFromList(NetBearType bearerType, const std::string &ident,
261                                              const std::set<NetCap> &netCaps);
262     int32_t ActivateNetwork(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback,
263                             const uint32_t &timeoutMS);
264     void CallbackForSupplier(sptr<NetSupplier> &supplier, CallbackType type);
265     void CallbackForAvailable(sptr<NetSupplier> &supplier, const sptr<INetConnCallback> &callback);
266     uint32_t FindBestNetworkForRequest(sptr<NetSupplier> &supplier, sptr<NetActivate> &netActivateNetwork);
267     void SendRequestToAllNetwork(sptr<NetActivate> request);
268     void SendBestScoreAllNetwork(uint32_t reqId, int32_t bestScore, uint32_t supplierId);
269     void SendAllRequestToNetwork(sptr<NetSupplier> supplier);
270     void FindBestNetworkForAllRequest();
271     void MakeDefaultNetWork(sptr<NetSupplier> &oldService, sptr<NetSupplier> &newService);
272     void NotFindBestSupplier(uint32_t reqId, const sptr<NetActivate> &active, const sptr<NetSupplier> &supplier,
273                              const sptr<INetConnCallback> &callback);
274     void CreateDefaultRequest();
275     int32_t RegUnRegNetDetectionCallback(int32_t netId, const sptr<INetDetectionCallback> &callback, bool isReg);
276     int32_t GenerateNetId();
277     bool FindSameCallback(const sptr<INetConnCallback> &callback, uint32_t &reqId);
278     void GetDumpMessage(std::string &message);
279     sptr<NetSupplier> FindNetSupplier(uint32_t supplierId);
280     int32_t RegisterNetSupplierAsync(NetBearType bearerType, const std::string &ident, const std::set<NetCap> &netCaps,
281                                      uint32_t &supplierId);
282     int32_t UnregisterNetSupplierAsync(uint32_t supplierId);
283     int32_t RegisterNetSupplierCallbackAsync(uint32_t supplierId, const sptr<INetSupplierCallback> &callback);
284     int32_t RegisterNetConnCallbackAsync(const sptr<NetSpecifier> &netSpecifier, const sptr<INetConnCallback> &callback,
285                                          const uint32_t &timeoutMS);
286     int32_t UnregisterNetConnCallbackAsync(const sptr<INetConnCallback> &callback);
287     int32_t RegUnRegNetDetectionCallbackAsync(int32_t netId, const sptr<INetDetectionCallback> &callback, bool isReg);
288     int32_t UpdateNetStateForTestAsync(const sptr<NetSpecifier> &netSpecifier, int32_t netState);
289     int32_t UpdateNetSupplierInfoAsync(uint32_t supplierId, const sptr<NetSupplierInfo> &netSupplierInfo);
290     int32_t UpdateNetLinkInfoAsync(uint32_t supplierId, const sptr<NetLinkInfo> &netLinkInfo);
291     int32_t NetDetectionAsync(int32_t netId);
292     int32_t RestrictBackgroundChangedAsync(bool restrictBackground);
293     int32_t SetGlobalHttpProxyAsync(const HttpProxy &httpProxy);
294     void SendGlobalHttpProxyChangeBroadcast();
295     void RequestAllNetworkExceptDefault();
296 
297 private:
298     enum ServiceRunningState {
299         STATE_STOPPED = 0,
300         STATE_RUNNING,
301     };
302 
303     bool registerToService_;
304     ServiceRunningState state_;
305     sptr<NetSpecifier> defaultNetSpecifier_ = nullptr;
306     sptr<NetActivate> defaultNetActivate_ = nullptr;
307     sptr<NetSupplier> defaultNetSupplier_ = nullptr;
308     NET_SUPPLIER_MAP netSuppliers_;
309     NET_ACTIVATE_MAP netActivates_;
310     NET_NETWORK_MAP networks_;
311     std::unique_ptr<NetScore> netScore_ = nullptr;
312     sptr<NetConnServiceIface> serviceIface_ = nullptr;
313     std::atomic<int32_t> netIdLastValue_ = MIN_NET_ID - 1;
314     HttpProxy httpProxy_;
315     std::mutex netManagerMutex_;
316     std::shared_ptr<AppExecFwk::EventRunner> netConnEventRunner_ = nullptr;
317     std::shared_ptr<NetConnEventHandler> netConnEventHandler_ = nullptr;
318 };
319 } // namespace NetManagerStandard
320 } // namespace OHOS
321 #endif // NET_CONN_SERVICE_H
322