• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-2025 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_observer.h"
17 
18 #include "account_task_manager.h"
19 #include "common_defs.h"
20 #include "device_auth.h"
21 #include "device_auth_defines.h"
22 #include "hc_log.h"
23 #include "net_conn_client.h"
24 #include "net_conn_constants.h"
25 
26 using namespace OHOS;
27 using namespace OHOS::NetManagerStandard;
28 
StartObserver()29 void NetObserver::StartObserver()
30 {
31     LOGI("[NetObserver]: Start to register net connection callback.");
32     NetSpecifier netSpecifier;
33     NetAllCapabilities netAllCapabilities;
34     netAllCapabilities.netCaps_.insert(NetManagerStandard::NetCap::NET_CAPABILITY_INTERNET);
35     netSpecifier.ident_ = "";
36     netSpecifier.netCapabilities_ = netAllCapabilities;
37     sptr<NetSpecifier> specifier = new NetSpecifier(netSpecifier);
38     int32_t ret = NetConnClient::GetInstance().RegisterNetConnCallback(specifier, this, 0);
39     if (ret == NetConnResultCode::NET_CONN_SUCCESS) {
40         LOGI("[NetObserver]: Register net connection callback succeeded.");
41         netConnCallback_ = this;
42         return;
43     }
44     LOGE("[NetObserver]: Register net connection callback failed, errCode = %" LOG_PUB "d.", ret);
45 }
46 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)47 int32_t NetObserver::NetCapabilitiesChange(sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
48 {
49     LOGI("[NetObserver]: Net capabilities change.");
50     if (netAllCap == nullptr) {
51         LOGE("[NetObserver]: netAllCap is null!");
52         return 0;
53     }
54     return HandleNetAllCap(*netAllCap);
55 }
56 
NetLost(sptr<NetHandle> & netHandle)57 int32_t NetObserver::NetLost(sptr<NetHandle> &netHandle)
58 {
59     LOGI("[NetObserver]: Net lost!");
60     return 0;
61 }
62 
NetAvailable(sptr<NetHandle> & netHandle)63 int32_t NetObserver::NetAvailable(sptr<NetHandle> &netHandle)
64 {
65     LOGI("[NetObserver]: Net available.");
66     return 0;
67 }
68 
HandleNetAllCap(const NetAllCapabilities & netAllCap)69 int32_t NetObserver::HandleNetAllCap(const NetAllCapabilities &netAllCap)
70 {
71     if (netAllCap.netCaps_.count(NET_CAPABILITY_INTERNET) <= 0 ||
72         netAllCap.netCaps_.count(NET_CAPABILITY_VALIDATED) <= 0) {
73         for (auto netCap : netAllCap.netCaps_) {
74             LOGI("[NetObserver]: No net, netCap is: %" LOG_PUB "d", static_cast<int32_t>(netCap));
75         }
76         return 0;
77     }
78     LOGI("[NetObserver]: Net available, reload credential manager.");
79     int32_t res = ExecuteAccountAuthCmd(DEFAULT_OS_ACCOUNT, RELOAD_CRED_MGR, nullptr, nullptr);
80     LOGI("[NetObserver]: Reload credential manager res: %" LOG_PUB "d.", res);
81     return 0;
82 }
83 
StopObserver()84 void NetObserver::StopObserver()
85 {
86     LOGI("[NetObserver]: Start to unregister net connection callback.");
87     if (netConnCallback_ == nullptr) {
88         LOGW("[NetObserver]: Net connection callback is null.");
89         return;
90     }
91     int32_t ret = NetConnClient::GetInstance().UnregisterNetConnCallback(netConnCallback_);
92     LOGI("[NetObserver]: unregister net connection callback res: %" LOG_PUB "d.", ret);
93     netConnCallback_ = nullptr;
94 }