1 /*
2 * Copyright (c) 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/if.h>
17 #include "net_info_observer.h"
18 #include <unistd.h>
19 #include "net_conn_constants.h"
20 #include "net_mgr_log_wrapper.h"
21 #include "net_stats_service.h"
22 #include "net_stats_utils.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26
NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> & netHandle,const sptr<NetManagerStandard::NetAllCapabilities> & netAllCap)27 int32_t NetInfoObserver::NetCapabilitiesChange(sptr<NetManagerStandard::NetHandle> &netHandle,
28 const sptr<NetManagerStandard::NetAllCapabilities> &netAllCap)
29 {
30 NETMGR_LOG_D("NetInfoObserver NetCapabilitiesChange");
31 return 0;
32 }
33
NetAvailable(sptr<NetManagerStandard::NetHandle> & netHandle)34 int32_t NetInfoObserver::NetAvailable(sptr<NetManagerStandard::NetHandle> &netHandle)
35 {
36 NETMGR_LOG_D("NetInfoObserver NetAvailable");
37 return 0;
38 }
39
NetLost(sptr<NetManagerStandard::NetHandle> & netHandle)40 int32_t NetInfoObserver::NetLost(sptr<NetManagerStandard::NetHandle> &netHandle)
41 {
42 isNeedUpdate_ = true;
43 NETMGR_LOG_I("NetInfoObserver NetLost");
44 return 0;
45 }
46
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)47 int32_t NetInfoObserver::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
48 {
49 NETMGR_LOG_D("NetInfoObserver NetConnectionPropertiesChange");
50 if (info == nullptr) {
51 isNeedUpdate_ = true;
52 return -1;
53 }
54 NETMGR_LOG_D("NetInfoObserver ifName: %{public}s, idnet: %{public}s",
55 info->ifaceName_.c_str(), info->ident_.c_str());
56 if (info->ident_.empty()) { // wifi场景
57 uint64_t ifindex = if_nametoindex(info->ifaceName_.c_str());
58 DelayedSingleton<NetStatsService>::GetInstance()->ProcessNetConnectionPropertiesChange(INT32_MAX, ifindex_);
59 isNeedUpdate_ = true;
60 return 0;
61 }
62
63 int32_t ident = 0;
64 NetStatsUtils::ConvertToInt32(info->ident_, ident);
65 if (!isNeedUpdate_ && ident == ident_ && info->ifaceName_ == ifaceName_) {
66 return 0;
67 }
68 isNeedUpdate_ = false;
69 ident_ = ident;
70 ifaceName_ = info->ifaceName_;
71 ifindex_ = if_nametoindex(ifaceName_.c_str());
72 DelayedSingleton<NetStatsService>::GetInstance()->ProcessNetConnectionPropertiesChange(ident_, ifindex_);
73 return 0;
74 }
75
76 } // namespace NetManagerStandard
77 } // namespace OHOS
78