• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
RegisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)24 int32_t NetConnectAdapterImpl::RegisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)
25 {
26     if (cb == nullptr) {
27         WVLOG_E("register NetConnCallback, cb is nullptr.");
28         return -1;
29     }
30 
31     sptr<NetConnectCallbackImpl> callbackImpl = new (std::nothrow) NetConnectCallbackImpl(cb);
32     if (callbackImpl == nullptr) {
33         WVLOG_E("new NetConnectCallbackImpl failed.");
34         return -1;
35     }
36 
37     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->RegisterNetConnCallback(callbackImpl);
38     if (ret != NETMANAGER_SUCCESS) {
39         WVLOG_E("register NetConnCallback failed, ret = %{public}d.", ret);
40         return -1;
41     }
42 
43     netConnCallbackMap_.insert(std::make_pair(cb.get(), callbackImpl));
44     WVLOG_I("register NetConnCallback success.");
45     return 0;
46 }
47 
UnregisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)48 int32_t NetConnectAdapterImpl::UnregisterNetConnCallback(std::shared_ptr<NetConnCallback> cb)
49 {
50     if (cb == nullptr) {
51         WVLOG_E("unregister NetConnCallback, cb is nullptr.");
52         return -1;
53     }
54 
55     auto it = netConnCallbackMap_.find(cb.get());
56     if (it == netConnCallbackMap_.end()) {
57         WVLOG_E("unregister NetConnCallback, not find the NetConnCallback.");
58         return -1;
59     }
60 
61     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->UnregisterNetConnCallback(it->second);
62     if (ret != NETMANAGER_SUCCESS) {
63         WVLOG_E("unregister NetConnCallback failed, ret = %{public}d.", ret);
64         return -1;
65     }
66 
67     netConnCallbackMap_.erase(it);
68     WVLOG_I("uregister NetConnCallback success.");
69     return 0;
70 }
71 
GetDefaultNetConnect(NetConnectType & type,NetConnectSubtype & subtype)72 int32_t NetConnectAdapterImpl::GetDefaultNetConnect(NetConnectType &type, NetConnectSubtype &subtype)
73 {
74     NetHandle netHandle;
75     int32_t ret = DelayedSingleton<NetConnClient>::GetInstance()->GetDefaultNet(netHandle);
76     if (ret != NETMANAGER_SUCCESS) {
77         WVLOG_E("get default net failed, ret = %{public}d.", ret);
78         return -1;
79     }
80     WVLOG_I("get default net success, net id = %{public}d.", netHandle.GetNetId());
81 
82     NetAllCapabilities netAllCap;
83     ret = DelayedSingleton<NetConnClient>::GetInstance()->GetNetCapabilities(netHandle, netAllCap);
84     if (ret != NETMANAGER_SUCCESS) {
85         WVLOG_E("get default net capbilities failed, ret = %{public}d.", ret);
86         return -1;
87     }
88     WVLOG_I("get default net capbilities success");
89 
90     subtype = NetConnectSubtype::SUBTYPE_UNKNOWN;
91     RadioTech radioTech = RadioTech::RADIO_TECHNOLOGY_UNKNOWN;
92     for (auto bearerTypes : netAllCap.bearerTypes_) {
93         if (bearerTypes == BEARER_CELLULAR) {
94             int32_t slotId = CellularDataClient::GetInstance().GetDefaultCellularDataSlotId();
95             if (slotId < 0) {
96                 WVLOG_E("get default soltId failed, ret = %{public}d.", slotId);
97                 slotId = 0;
98             }
99             sptr<NetworkState> networkState = nullptr;
100             CoreServiceClient::GetInstance().GetNetworkState(slotId, networkState);
101             if (networkState != nullptr) {
102                 radioTech = networkState->GetPsRadioTech();
103                 WVLOG_I("net radio tech = %{public}d.", static_cast<int32_t>(radioTech));
104                 subtype = NetConnectUtils::ConvertToConnectsubtype(radioTech);
105             }
106         }
107         type = NetConnectUtils::ConvertToConnectType(bearerTypes, radioTech);
108         WVLOG_I("net connect type = %{public}s.", NetConnectUtils::ConnectTypeToString(type).c_str());
109         break;
110     }
111     WVLOG_D("NetAllCapabilities dump, %{public}s.", netAllCap.ToString("").c_str());
112     return 0;
113 }
114 } // namespace OHOS::NWeb