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, uint32_t uid = 0,
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     uint32_t GetUid() const;
63 
64 private:
65     bool CompareByNetworkIdent(const std::string &ident, NetBearType bearerType, bool skipCheckIdent);
66     bool CompareByNetworkCapabilities(const NetCaps &netCaps);
67     bool CompareByNetworkNetType(NetBearType bearerType);
68     bool CompareByNetworkBand(uint32_t netLinkUpBand, uint32_t netLinkDownBand);
69     bool HaveCapability(NetCap netCap) const;
70     bool HaveTypes(const std::set<NetBearType> &bearerTypes) const;
71     void TimeOutNetAvailable();
72 
73 private:
74     uint32_t requestId_ = 1;
75     sptr<NetSpecifier> netSpecifier_ = nullptr;
76     sptr<INetConnCallback> netConnCallback_ = nullptr;
77     sptr<NetSupplier> netServiceSupplied_ = nullptr;
78     uint32_t timeoutMS_ = 0;
79     std::weak_ptr<INetActivateCallback> timeoutCallback_;
80     std::shared_ptr<AppExecFwk::EventHandler> netActEventHandler_;
81     std::string activateName_ = "";
82     uint32_t uid_ = 0;
83     int32_t registerType_ = REGISTER;
84 };
85 } // namespace NetManagerStandard
86 } // namespace OHOS
87 #endif // NET_ACTIVATE_H