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_ACTIVATE_H
17 #define NET_ACTIVATE_H
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "i_net_conn_callback.h"
25 #include "net_specifier.h"
26 #include "net_supplier.h"
27 class NetSupplier;
28 
29 namespace OHOS {
30 namespace NetManagerStandard {
31 constexpr uint32_t DEFAULT_REQUEST_ID = 0;
32 constexpr uint32_t MIN_REQUEST_ID = DEFAULT_REQUEST_ID + 1;
33 constexpr uint32_t MAX_REQUEST_ID = 0x7FFFFFFF;
34 class INetActivateCallback {
35 public:
36     virtual ~INetActivateCallback() = default;
37 
38 public:
39     virtual void OnNetActivateTimeOut(uint32_t reqId) = 0;
40 };
41 
42 class NetActivate : public std::enable_shared_from_this<NetActivate> {
43 public:
44     using TimeOutHandler = std::function<int32_t(uint32_t &reqId)>;
45 
46 public:
47     NetActivate(const sptr<NetSpecifier> &specifier, const sptr<INetConnCallback> &callback,
48                 std::weak_ptr<INetActivateCallback> timeoutCallback, const uint32_t &timeoutMS,
49                 const std::shared_ptr<AppExecFwk::EventHandler> &netActEventHandler,
50                 const int32_t registerType = REGISTER);
51     ~NetActivate();
52     bool MatchRequestAndNetwork(sptr<NetSupplier> supplier, bool skipCheckIdent = false);
53     void SetRequestId(uint32_t reqId);
54     uint32_t GetRequestId() const;
55     sptr<NetSupplier> GetServiceSupply() const;
56     void SetServiceSupply(sptr<NetSupplier> netServiceSupplied);
57     sptr<INetConnCallback> GetNetCallback();
58     sptr<NetSpecifier> GetNetSpecifier();
59     int32_t GetRegisterType() const;
60     std::set<NetBearType> GetBearType() const;
61     void StartTimeOutNetAvailable();
62 private:
63     bool CompareByNetworkIdent(const std::string &ident, NetBearType bearerType, bool skipCheckIdent);
64     bool CompareByNetworkCapabilities(const NetCaps &netCaps);
65     bool CompareByNetworkNetType(NetBearType bearerType);
66     bool CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand);
67     bool HaveCapability(NetCap netCap) const;
68     bool HaveTypes(const std::set<NetBearType> &bearerTypes) const;
69     void TimeOutNetAvailable();
70 
71 private:
72     uint32_t requestId_ = 1;
73     sptr<NetSpecifier> netSpecifier_ = nullptr;
74     sptr<INetConnCallback> netConnCallback_ = nullptr;
75     sptr<NetSupplier> netServiceSupplied_ = nullptr;
76     uint32_t timeoutMS_ = 0;
77     std::weak_ptr<INetActivateCallback> timeoutCallback_;
78     std::shared_ptr<AppExecFwk::EventHandler> netActEventHandler_;
79     std::string activateName_ = "";
80     int32_t registerType_ = REGISTER;
81 };
82 } // namespace NetManagerStandard
83 } // namespace OHOS
84 #endif // NET_ACTIVATE_H