1 /*
2 * Copyright (c) 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 #include "net_connect_adapter_impl.h"
17
18 #include "cellular_data_client.h"
19 #include "core_service_client.h"
20 #include "net_connect_utils.h"
21 #include "nweb_log.h"
22
23 namespace OHOS::NWeb {
NetConnectCallbackImpl(std::shared_ptr<NetConnCallback> cb)24 NetConnectCallbackImpl::NetConnectCallbackImpl(std::shared_ptr<NetConnCallback> cb) : cb_(cb) {}
25
NetAvailable(sptr<NetHandle> & netHandle)26 int32_t NetConnectCallbackImpl::NetAvailable(sptr<NetHandle> &netHandle)
27 {
28 if (netHandle == nullptr) {
29 WVLOG_E("NetConnCallback enter, net available, netHandle is nullptr.");
30 return 0;
31 }
32
33 WVLOG_I("NetConnCallback enter, net available, net id = %{public}d.", netHandle->GetNetId());
34 if (cb_ != nullptr) {
35 return cb_->NetAvailable();
36 }
37
38 return 0;
39 }
40
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)41 int32_t NetConnectCallbackImpl::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
42 const sptr<NetAllCapabilities> &netAllCap)
43 {
44 if (netHandle == nullptr || netAllCap == nullptr) {
45 WVLOG_E("NetConnCallback enter, NetCapabilitiesChange, netHandle or netAllCap is nullptr.");
46 return 0;
47 }
48
49 WVLOG_I("NetConnCallback enter, NetCapabilitiesChange, net id = %{public}d.", netHandle->GetNetId());
50 WVLOG_D("NetAllCapabilities dump, %{public}s.", netAllCap->ToString("").c_str());
51 NetConnectSubtype subtype = NetConnectSubtype::SUBTYPE_UNKNOWN;
52 RadioTech radioTech = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
53 for (auto bearerTypes : netAllCap->bearerTypes_) {
54 if (bearerTypes == BEARER_CELLULAR) {
55 int32_t slotId = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
56 if (slotId < 0) {
57 WVLOG_E("get default soltId failed, ret = %{public}d.", slotId);
58 slotId = 0;
59 }
60 sptr<NetworkState> networkState = nullptr;
61 CoreServiceClient::GetInstance().GetNetworkState(slotId, networkState);
62 if (networkState != nullptr) {
63 radioTech = networkState->GetPsRadioTech();
64 WVLOG_I("net radio tech = %{public}d.", static_cast<int32_t>(radioTech));
65 subtype = NetConnectUtils::ConvertToConnectsubtype(networkState->GetPsRadioTech());
66 }
67 }
68 NetConnectType type = NetConnectUtils::ConvertToConnectType(bearerTypes, radioTech);
69 WVLOG_I("net connect type = %{public}s.", NetConnectUtils::ConnectTypeToString(type).c_str());
70 if (cb_ != nullptr) {
71 return cb_->NetCapabilitiesChange(type, subtype);
72 }
73 }
74
75 return 0;
76 }
77
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)78 int32_t NetConnectCallbackImpl::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
79 {
80 if (netHandle == nullptr || info == nullptr) {
81 WVLOG_E("NetConnCallback enter, NetCapabilitiesChange, netHandle or info is nullptr.");
82 return 0;
83 }
84
85 WVLOG_I("NetConnCallback enter, NetConnectionPropertiesChange, net id = %{public}d.", netHandle->GetNetId());
86 WVLOG_D("%{public}s.", info->ToString("").c_str());
87 if (cb_ != nullptr) {
88 return cb_->NetConnectionPropertiesChange();
89 }
90
91 return 0;
92 }
93
NetLost(sptr<NetHandle> & netHandle)94 int32_t NetConnectCallbackImpl::NetLost(sptr<NetHandle> &netHandle)
95 {
96 WVLOG_I("NetConnCallback enter, NetLost, net id = %{public}d.", netHandle->GetNetId());
97 if (cb_ != nullptr) {
98 return cb_->NetUnavailable();
99 }
100 return 0;
101 }
102
NetUnavailable()103 int32_t NetConnectCallbackImpl::NetUnavailable()
104 {
105 WVLOG_I("NetConnCallback enter, NetUnavailable.");
106 if (cb_ != nullptr) {
107 return cb_->NetUnavailable();
108 }
109 return 0;
110 }
111
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)112 int32_t NetConnectCallbackImpl::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
113 {
114 WVLOG_I("NetConnCallback enter, NetBlockStatusChange, net id = %{public}d, blocked = %{public}d.",
115 netHandle->GetNetId(), blocked);
116 return 0;
117 }
118 } // namespace OHOS::NWeb