• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2025 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::sync::{Arc, Mutex};
15 
16 use ffi::{NetInfo, NetUnregistration};
17 
18 use super::Observer;
19 
20 pub struct NetObserverWrapper {
21     inner: Arc<Mutex<Vec<Box<dyn Observer>>>>,
22 }
23 
24 impl NetObserverWrapper {
new(inner: Arc<Mutex<Vec<Box<dyn Observer>>>>) -> Self25     pub fn new(inner: Arc<Mutex<Vec<Box<dyn Observer>>>>) -> Self {
26         Self { inner }
27     }
28 }
29 
30 impl NetObserverWrapper {
net_available(&self, net_id: i32)31     pub(crate) fn net_available(&self, net_id: i32) {
32         let inner = self.inner.lock().unwrap();
33         for observer in inner.iter() {
34             observer.net_available(net_id);
35         }
36     }
37 
net_lost(&self, net_id: i32)38     pub(crate) fn net_lost(&self, net_id: i32) {
39         let inner = self.inner.lock().unwrap();
40         for observer in inner.iter() {
41             observer.net_lost(net_id);
42         }
43     }
44 
net_capability_changed(&self, net_id: i32, net_info: NetInfo)45     pub(crate) fn net_capability_changed(&self, net_id: i32, net_info: NetInfo) {
46         let inner = self.inner.lock().unwrap();
47         for observer in inner.iter() {
48             observer.net_capability_changed(net_id, &net_info);
49         }
50     }
51 }
52 
53 unsafe impl Send for NetUnregistration {}
54 unsafe impl Sync for NetUnregistration {}
55 
56 #[cxx::bridge(namespace = "OHOS::Request")]
57 pub mod ffi {
58     #[namespace = "OHOS::NetManagerStandard"]
59     #[derive(Debug)]
60     #[repr(i32)]
61     enum NetCap {
62         NET_CAPABILITY_MMS = 0,
63         NET_CAPABILITY_SUPL = 1,
64         NET_CAPABILITY_DUN = 2,
65         NET_CAPABILITY_IA = 3,
66         NET_CAPABILITY_XCAP = 4,
67         NET_CAPABILITY_BIP = 5,
68         NET_CAPABILITY_NOT_METERED = 11,
69         NET_CAPABILITY_INTERNET = 12,
70         NET_CAPABILITY_NOT_VPN = 15,
71         NET_CAPABILITY_VALIDATED = 16,
72         NET_CAPABILITY_PORTAL = 17,
73         NET_CAPABILITY_INTERNAL_DEFAULT = 18,
74         NET_CAPABILITY_CHECKING_CONNECTIVITY = 31,
75         NET_CAPABILITY_END = 32,
76     }
77 
78     #[namespace = "OHOS::NetManagerStandard"]
79     #[derive(Debug)]
80     #[repr(i32)]
81     enum NetBearType {
82         BEARER_CELLULAR = 0,
83         BEARER_WIFI = 1,
84         BEARER_BLUETOOTH = 2,
85         BEARER_ETHERNET = 3,
86         BEARER_VPN = 4,
87         BEARER_WIFI_AWARE = 5,
88         BEARER_DEFAULT,
89     }
90 
91     #[derive(Debug)]
92     struct NetInfo {
93         caps: Vec<NetCap>,
94         bear_types: Vec<NetBearType>,
95     }
96 
97     extern "Rust" {
98         type NetObserverWrapper;
99 
net_available(&self, net_id: i32)100         fn net_available(&self, net_id: i32);
net_lost(&self, net_id: i32)101         fn net_lost(&self, net_id: i32);
net_capability_changed(&self, net_id: i32, net_info: NetInfo)102         fn net_capability_changed(&self, net_id: i32, net_info: NetInfo);
103     }
104 
105     unsafe extern "C++" {
106         include!("net_all_capabilities.h");
107         include!("request_utils_network.h");
108 
109         #[namespace = "OHOS::NetManagerStandard"]
110         type NetCap;
111         #[namespace = "OHOS::NetManagerStandard"]
112         type NetBearType;
113 
114         type NetUnregistration;
unregister(self: &NetUnregistration) -> i32115         fn unregister(self: &NetUnregistration) -> i32;
116 
117         #[allow(unused)]
RegisterNetObserver( wrapper: Box<NetObserverWrapper>, error: &mut i32, ) -> UniquePtr<NetUnregistration>118         fn RegisterNetObserver(
119             wrapper: Box<NetObserverWrapper>,
120             error: &mut i32,
121         ) -> UniquePtr<NetUnregistration>;
122     }
123 }
124