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