• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "network_observer.h"
17 #include "network_constant.h"
18 
19 #if HAS_TELEPHONY
20 #include "core_service_client.h"
21 #endif
22 
23 #include "netmanager_base_log.h"
24 #include "securec.h"
25 
26 static constexpr const char *NETWORK_NONE = "none";
27 
28 static constexpr const char *NETWORK_WIFI = "WiFi";
29 
30 static std::mutex OBSERVER_MUTEX;
31 
32 namespace OHOS::NetManagerStandard {
33 std::map<EventManager *, sptr<NetworkObserver>> g_observerMap;
34 
35 #if HAS_TELEPHONY
CellularTypeToString(Telephony::SignalInformation::NetworkType type)36 static std::string CellularTypeToString(Telephony::SignalInformation::NetworkType type)
37 {
38     switch (type) {
39         case Telephony::SignalInformation::NetworkType::GSM:
40             return "2g";
41         case Telephony::SignalInformation::NetworkType::CDMA:
42         case Telephony::SignalInformation::NetworkType::WCDMA:
43         case Telephony::SignalInformation::NetworkType::TDSCDMA:
44             return "3g";
45         case Telephony::SignalInformation::NetworkType::LTE:
46             return "4g";
47         default:
48             break;
49     }
50     return "5g";
51 }
52 #endif
53 
MakeNetworkResponse(napi_env env,NetworkType * data)54 static napi_value MakeNetworkResponse(napi_env env, NetworkType *data)
55 {
56     auto deleter = [](NetworkType *t) { delete t; };
57     std::unique_ptr<NetworkType, decltype(deleter)> netType(data, deleter);
58 
59     napi_value obj = NapiUtils::CreateObject(env);
60     if (netType->bearerTypes.find(BEARER_WIFI) != netType->bearerTypes.end()) {
61         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_WIFI);
62         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
63         return obj;
64     }
65 
66 #if HAS_TELEPHONY
67     if (netType->bearerTypes.find(BEARER_CELLULAR) != netType->bearerTypes.end()) {
68         std::vector<sptr<Telephony::SignalInformation>> vec;
69         DelayedRefSingleton<Telephony::CoreServiceClient>::GetInstance().GetSignalInfoList(0, vec);
70         if (vec.empty()) {
71             NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
72             NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
73             return obj;
74         }
75 
76         std::sort(vec.begin(), vec.end(),
77                   [](const sptr<Telephony::SignalInformation> &info1, const sptr<Telephony::SignalInformation> &info2)
78                       -> bool { return info1->GetSignalLevel() > info2->GetSignalLevel(); });
79         NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, CellularTypeToString(vec[0]->GetNetworkType()));
80         NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, true);
81         return obj;
82     }
83 #endif
84 
85     NapiUtils::SetStringPropertyUtf8(env, obj, KEY_TYPE, NETWORK_NONE);
86     NapiUtils::SetBooleanProperty(env, obj, KEY_METERED, false);
87     return obj;
88 }
89 
NetAvailable(sptr<NetHandle> & netHandle)90 int32_t NetworkObserver::NetAvailable(sptr<NetHandle> &netHandle)
91 {
92     return 0;
93 }
94 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)95 int32_t NetworkObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
96 {
97     NETMANAGER_BASE_LOGI("NetworkObserver::NetCapabilitiesChange");
98 
99     std::lock_guard<std::mutex> lock(OBSERVER_MUTEX);
100     if (!manager_) {
101         NETMANAGER_BASE_LOGI("no event manager");
102         return 0;
103     }
104 
105     if (manager_->HasEventListener(EVENT_SUBSCRIBE)) {
106         auto netType = new NetworkType;
107         netType->bearerTypes = netAllCap->bearerTypes_;
108         manager_->EmitByUv(EVENT_SUBSCRIBE, netType, CallbackTemplate<MakeNetworkResponse>);
109     } else {
110         NETMANAGER_BASE_LOGI("NO EVENT_SUBSCRIBE");
111     }
112     return 0;
113 }
114 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)115 int32_t NetworkObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
116 {
117     return 0;
118 }
119 
NetLost(sptr<NetHandle> & netHandle)120 int32_t NetworkObserver::NetLost(sptr<NetHandle> &netHandle)
121 {
122     return 0;
123 }
124 
NetUnavailable()125 int32_t NetworkObserver::NetUnavailable()
126 {
127     NETMANAGER_BASE_LOGI("NetworkObserver::NetUnavailable");
128 
129     std::lock_guard<std::mutex> lock(OBSERVER_MUTEX);
130     if (!manager_) {
131         NETMANAGER_BASE_LOGI("no event manager");
132         return 0;
133     }
134     if (manager_->HasEventListener(EVENT_SUBSCRIBE)) {
135         auto netType = new NetworkType;
136         manager_->EmitByUv(EVENT_SUBSCRIBE, netType, CallbackTemplate<MakeNetworkResponse>);
137     } else {
138         NETMANAGER_BASE_LOGI("NO EVENT_SUBSCRIBE");
139     }
140     return 0;
141 }
142 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)143 int32_t NetworkObserver::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
144 {
145     return 0;
146 }
147 
SetManager(EventManager * manager)148 void NetworkObserver::SetManager(EventManager *manager)
149 {
150     manager_ = manager;
151 }
152 } // namespace OHOS::NetManagerStandard
153