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 "net_link_info.h"
22 #include "nweb_log.h"
23
24 namespace OHOS::NWeb {
RegisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)25 int32_t NetConnectAdapterImpl::RegisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)
26 {
27 static int32_t count = 0;
28 if (cb == nullptr) {
29 WVLOG_E("register NetConnCallback, cb is nullptr.");
30 return -1;
31 }
32
33 sptr<NetConnectCallbackImpl> callbackImpl = new (std::nothrow) NetConnectCallbackImpl(cb);
34 if (callbackImpl == nullptr) {
35 WVLOG_E("new NetConnectCallbackImpl failed.");
36 return -1;
37 }
38
39 int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(callbackImpl);
40 if (ret != NETMANAGER_SUCCESS) {
41 WVLOG_E("register NetConnCallback failed, ret = %{public}d.", ret);
42 return -1;
43 }
44
45 int32_t id = count++;
46 if (count < 0) {
47 count = 0;
48 }
49
50 netConnCallbackMap_.insert(std::make_pair(id, callbackImpl));
51 WVLOG_I("register NetConnCallback success.");
52 return id;
53 }
54
UnregisterNetConnCallback(int32_t id)55 int32_t NetConnectAdapterImpl::UnregisterNetConnCallback(int32_t id)
56 {
57 auto it = netConnCallbackMap_.find(id);
58 if (it == netConnCallbackMap_.end()) {
59 WVLOG_E("unregister NetConnCallback, not find the NetConnCallback.");
60 return -1;
61 }
62
63 int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(it->second);
64 if (ret != NETMANAGER_SUCCESS) {
65 WVLOG_E("unregister NetConnCallback failed, ret = %{public}d.", ret);
66 return -1;
67 }
68
69 netConnCallbackMap_.erase(it);
70 WVLOG_I("uregister NetConnCallback success.");
71 return 0;
72 }
73
GetDefaultNetConnect(NetConnectType & type,NetConnectSubtype & subtype)74 int32_t NetConnectAdapterImpl::GetDefaultNetConnect(NetConnectType &type, NetConnectSubtype &subtype)
75 {
76 NetHandle netHandle;
77 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
78 if (ret != NETMANAGER_SUCCESS) {
79 WVLOG_E("get default net failed, ret = %{public}d.", ret);
80 return -1;
81 }
82 WVLOG_I("get default net success, net id = %{public}d.", netHandle.GetNetId());
83
84 NetAllCapabilities netAllCap;
85 ret = NetConnClient::GetInstance().GetNetCapabilities(netHandle, netAllCap);
86 if (ret != NETMANAGER_SUCCESS) {
87 WVLOG_E("get default net capbilities failed, ret = %{public}d.", ret);
88 return -1;
89 }
90 WVLOG_I("get default net capbilities success");
91
92 subtype = NetConnectSubtype::SUBTYPE_UNKNOWN;
93 RadioTech radioTech = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
94 for (auto bearerTypes : netAllCap.bearerTypes_) {
95 if (bearerTypes == BEARER_CELLULAR) {
96 int32_t slotId = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
97 if (slotId < 0) {
98 WVLOG_E("get default soltId failed, ret = %{public}d.", slotId);
99 slotId = 0;
100 }
101 sptr<NetworkState> networkState = nullptr;
102 CoreServiceClient::GetInstance().GetNetworkState(slotId, networkState);
103 if (networkState != nullptr) {
104 radioTech = networkState->GetPsRadioTech();
105 WVLOG_I("net radio tech = %{public}d.", static_cast<int32_t>(radioTech));
106 subtype = NetConnectUtils::ConvertToConnectsubtype(radioTech);
107 }
108 }
109 type = NetConnectUtils::ConvertToConnectType(bearerTypes, radioTech);
110 WVLOG_I("net connect type = %{public}s.", NetConnectUtils::ConnectTypeToString(type).c_str());
111 break;
112 }
113 WVLOG_D("NetAllCapabilities dump, %{public}s.", netAllCap.ToString("").c_str());
114 return 0;
115 }
116
GetDnsServersInternal(const NetHandle & netHandle)117 std::vector<std::string> NetConnectAdapterImpl::GetDnsServersInternal(const NetHandle &netHandle)
118 {
119 std::vector<std::string> servers;
120 NetLinkInfo info;
121 int32_t ret = NetConnClient::GetInstance().GetConnectionProperties(netHandle, info);
122 if (ret != NETMANAGER_SUCCESS) {
123 WVLOG_E("get net properties failed, ret = %{public}d.", ret);
124 return servers;
125 }
126 WVLOG_D("get net properties for dns servers success, net id = %{public}d, "
127 "netinfo = %{public}s.", netHandle.GetNetId(), info.ToString(" ").c_str());
128
129 for (const auto &dns : info.dnsList_) {
130 servers.emplace_back(dns.address_);
131 }
132 WVLOG_I("get dns servers success, net id = %{public}d, servers size = %{public}d.",
133 netHandle.GetNetId(), static_cast<int32_t>(servers.size()));
134 return servers;
135 }
136
GetDnsServers()137 std::vector<std::string> NetConnectAdapterImpl::GetDnsServers()
138 {
139 NetHandle netHandle;
140 int32_t ret = NetConnClient::GetInstance().GetDefaultNet(netHandle);
141 if (ret != NETMANAGER_SUCCESS) {
142 WVLOG_E("get default net for dns servers failed, ret = %{public}d.", ret);
143 return std::vector<std::string>();
144 }
145
146 return GetDnsServersInternal(netHandle);
147 }
148
GetDnsServersByNetId(int32_t netId)149 std::vector<std::string> NetConnectAdapterImpl::GetDnsServersByNetId(int32_t netId)
150 {
151 WVLOG_I("get dns servers by net id %{public}d.", netId);
152 if (netId == -1) {
153 return GetDnsServers();
154 }
155
156 std::list<sptr<NetManagerStandard::NetHandle>> netHandleList;
157 int32_t ret = NetConnClient::GetInstance().GetAllNets(netHandleList);
158 if (ret != NETMANAGER_SUCCESS) {
159 WVLOG_E("get all nets by net id for dns servers failed, ret = %{public}d.", ret);
160 return std::vector<std::string>();
161 }
162
163 for (sptr<NetManagerStandard::NetHandle> netHandle : netHandleList) {
164 if (netHandle->GetNetId() == netId) {
165 return GetDnsServersInternal(*netHandle);
166 }
167 }
168 return std::vector<std::string>();
169 }
170 } // namespace OHOS::NWeb
171