• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "dupdate_net_manager.h"
17 
18 #include <thread>
19 #include <unistd.h>
20 
21 #include "update_define.h"
22 #include "update_log.h"
23 
24 namespace OHOS {
25 namespace UpdateEngine {
NetManager()26 NetManager::NetManager()
27 {
28     observer_ = new NetObserver();
29 }
30 
~NetManager()31 NetManager::~NetManager() {}
32 
Init()33 void NetManager::Init()
34 {
35     ENGINE_LOGI("NetManager init");
36     if (observer_ == nullptr) {
37         ENGINE_LOGE("net ovserver is nullptr");
38         return;
39     }
40     observer_->SetCallback(weak_from_this());
41     observer_->StartObserver();
42 }
43 
RegisterCallback(NetChangeCallbackType callbackType,const std::set<NetType> & netTypes,NetObserverCallback cb)44 bool NetManager::RegisterCallback(
45     NetChangeCallbackType callbackType, const std::set<NetType> &netTypes, NetObserverCallback cb)
46 {
47     ENGINE_LOGI("RegisterCallback callbackType = %{public}d", callbackType);
48     if (netTypes.empty()) {
49         ENGINE_LOGE("RegisterCallback netTypes is empty");
50         return false;
51     }
52     for (auto netType : netTypes) {
53         if (!IsBaseNetType(netType)) {
54             // 只允许注册基础网络类型
55             ENGINE_LOGE("RegisterCallback unSurport netType = %{public}d", netType);
56             return false;
57         }
58     }
59     std::lock_guard<std::mutex> guard(netChangeMutex_);
60     netChangeCallbackMap_[callbackType] = {netTypes, cb};
61     return true;
62 }
63 
UnregisterCallback(NetChangeCallbackType callbackType)64 void NetManager::UnregisterCallback(NetChangeCallbackType callbackType)
65 {
66     std::lock_guard<std::mutex> guard(netChangeMutex_);
67     for (auto iter = netChangeCallbackMap_.begin(); iter != netChangeCallbackMap_.end();) {
68         if (iter->first == callbackType) {
69             iter = netChangeCallbackMap_.erase(iter);
70             break;
71         }
72         iter++;
73     }
74 }
75 
GetNetType()76 NetType NetManager::GetNetType()
77 {
78     return netType_;
79 }
80 
IsNetAvailable(NetType netType)81 bool NetManager::IsNetAvailable(NetType netType)
82 {
83     return (CAST_UINT(netType_) & CAST_UINT(netType)) > 0;
84 }
85 
OnNetChange(NetType netType)86 bool NetManager::OnNetChange(NetType netType)
87 {
88     if (!IsBaseNetType(netType)) {
89         ENGINE_LOGE("OnNetChange illegal netType = %{public}d", netType);
90         return false;
91     }
92     if (netType == netType_) {
93         return true;
94     }
95     netType_ = netType;
96     ENGINE_LOGI("OnNetChange type = %{public}d", netType);
97     std::lock_guard<std::mutex> guard(netChangeMutex_);
98     for (auto iter = netChangeCallbackMap_.begin(); iter != netChangeCallbackMap_.end();) {
99         NetChangeCallback netChangeCallback = iter->second;
100         if (netChangeCallback.netTypes.count(netType) != 0) {
101             netChangeCallback.function(netType);
102         }
103         iter++;
104     }
105     return true;
106 }
107 
IsBaseNetType(NetType netType)108 bool NetManager::IsBaseNetType(NetType netType)
109 {
110     return netType == NetType::NO_NET || netType == NetType::CELLULAR || netType == NetType::METERED_WIFI ||
111            netType == NetType::NOT_METERED_WIFI;
112 }
113 } // namespace UpdateEngine
114 } // namespace OHOS