1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#![allow(missing_docs)]
16cc290419Sopenharmony_ciuse crate::common::sendmsg::send_msg;
17cc290419Sopenharmony_ciuse crate::common::uds::{PollNode, UdsAddr, UdsServer};
18cc290419Sopenharmony_ciuse crate::config::ErrCode;
19cc290419Sopenharmony_ciuse crate::config::HdcCommand;
20cc290419Sopenharmony_ciuse crate::config::TaskMessage;
21cc290419Sopenharmony_ciuse crate::daemon_lib::sys_para::set_dev_item;
22cc290419Sopenharmony_ciuse crate::{transfer, utils};
23cc290419Sopenharmony_ciuse crate::utils::hdc_log::*;
24cc290419Sopenharmony_ciuse libc::{POLLERR, POLLHUP, POLLIN, POLLNVAL, POLLRDHUP, SOCK_STREAM};
25cc290419Sopenharmony_ci
26cc290419Sopenharmony_ciuse std::collections::HashMap;
27cc290419Sopenharmony_ciuse std::sync::{Arc, Once};
28cc290419Sopenharmony_ci#[cfg(feature = "host")]
29cc290419Sopenharmony_ciextern crate ylong_runtime_static as ylong_runtime;
30cc290419Sopenharmony_ciuse ylong_runtime::sync::waiter::Waiter;
31cc290419Sopenharmony_ciuse ylong_runtime::sync::Mutex;
32cc290419Sopenharmony_ci
33cc290419Sopenharmony_ciconst JPID_SOCKET_PATH: &str = "ohjpid-control";
34cc290419Sopenharmony_ciconst PATH_LEN: usize = JPID_SOCKET_PATH.as_bytes().len() + 1;
35cc290419Sopenharmony_ci
36cc290419Sopenharmony_ci#[allow(unused)]
37cc290419Sopenharmony_ci#[derive(Clone, Debug, PartialEq)]
38cc290419Sopenharmony_cipub enum DisplayType {
39cc290419Sopenharmony_ci    AllApp = 0,
40cc290419Sopenharmony_ci    DebugApp = 1,
41cc290419Sopenharmony_ci    ReleaseApp = 2,
42cc290419Sopenharmony_ci    AllAppWithType = 3,
43cc290419Sopenharmony_ci}
44cc290419Sopenharmony_ci
45cc290419Sopenharmony_citype NodeMap = Arc<Mutex<HashMap<i32, PollNode>>>;
46cc290419Sopenharmony_citype SocketPairVec = Arc<Mutex<Vec<(i32, i32)>>>;
47cc290419Sopenharmony_citype SocketpairMap = Arc<Mutex<HashMap<u32, SocketPairVec>>>;
48cc290419Sopenharmony_citype Trackers = Arc<Mutex<Vec<(u32, u32, DisplayType)>>>;
49cc290419Sopenharmony_ci
50cc290419Sopenharmony_cipub trait JdwpBase: Send + Sync + 'static {}
51cc290419Sopenharmony_cipub struct Jdwp {
52cc290419Sopenharmony_ci    poll_node_map: NodeMap,
53cc290419Sopenharmony_ci    socketpair_map: SocketpairMap,
54cc290419Sopenharmony_ci    new_process_waiter: Arc<Waiter>,
55cc290419Sopenharmony_ci    trackers: Trackers,
56cc290419Sopenharmony_ci    await_fd: i32,
57cc290419Sopenharmony_ci}
58cc290419Sopenharmony_ci
59cc290419Sopenharmony_ciimpl JdwpBase for Jdwp {}
60cc290419Sopenharmony_ci
61cc290419Sopenharmony_citype JdWpShare = Arc<Jdwp>;
62cc290419Sopenharmony_ci
63cc290419Sopenharmony_ciimpl Default for Jdwp {
64cc290419Sopenharmony_ci    fn default() -> Self {
65cc290419Sopenharmony_ci        Self::new()
66cc290419Sopenharmony_ci    }
67cc290419Sopenharmony_ci}
68cc290419Sopenharmony_ci
69cc290419Sopenharmony_ciimpl Jdwp {
70cc290419Sopenharmony_ci    pub fn get_instance() -> JdWpShare {
71cc290419Sopenharmony_ci        static mut INSTANCE: Option<JdWpShare> = None;
72cc290419Sopenharmony_ci        unsafe {
73cc290419Sopenharmony_ci            INSTANCE
74cc290419Sopenharmony_ci                .get_or_insert_with(|| Arc::new(Jdwp::new()))
75cc290419Sopenharmony_ci                .clone()
76cc290419Sopenharmony_ci        }
77cc290419Sopenharmony_ci    }
78cc290419Sopenharmony_ci
79cc290419Sopenharmony_ci    pub fn new() -> Self {
80cc290419Sopenharmony_ci        Self {
81cc290419Sopenharmony_ci            poll_node_map: Arc::new(Mutex::new(HashMap::default())),
82cc290419Sopenharmony_ci            socketpair_map: Arc::new(Mutex::new(HashMap::default())),
83cc290419Sopenharmony_ci            new_process_waiter: Arc::new(Waiter::new()),
84cc290419Sopenharmony_ci            trackers: Arc::new(Mutex::new(Vec::new())),
85cc290419Sopenharmony_ci            await_fd: UdsServer::wrap_event_fd(),
86cc290419Sopenharmony_ci        }
87cc290419Sopenharmony_ci    }
88cc290419Sopenharmony_ci}
89cc290419Sopenharmony_ci
90cc290419Sopenharmony_ciimpl Jdwp {
91cc290419Sopenharmony_ci    async fn put_socketpair(&self, target_pid: u32, fd: i32, fd2: i32) {
92cc290419Sopenharmony_ci        let socketpair_map = self.socketpair_map.clone();
93cc290419Sopenharmony_ci        let mut socketpair_map_lock = socketpair_map.lock().await;
94cc290419Sopenharmony_ci        if let Some(vec) = socketpair_map_lock.get(&target_pid) {
95cc290419Sopenharmony_ci            let mut lock = vec.lock().await;
96cc290419Sopenharmony_ci            lock.push((fd, fd2));
97cc290419Sopenharmony_ci        } else {
98cc290419Sopenharmony_ci            let vec = vec![(fd, fd2)];
99cc290419Sopenharmony_ci            socketpair_map_lock.insert(target_pid, Arc::new(Mutex::new(vec)));
100cc290419Sopenharmony_ci        }
101cc290419Sopenharmony_ci    }
102cc290419Sopenharmony_ci
103cc290419Sopenharmony_ci    pub async fn send_fd_to_target(&self, target_pid: u32, fd: i32, fd2: i32, parameter: &str) -> bool {
104cc290419Sopenharmony_ci        let map = self.poll_node_map.clone();
105cc290419Sopenharmony_ci        let map = map.lock().await;
106cc290419Sopenharmony_ci        let keys = map.keys();
107cc290419Sopenharmony_ci        for k in keys {
108cc290419Sopenharmony_ci            let v = map.get(k);
109cc290419Sopenharmony_ci            if let Some(node) = v {
110cc290419Sopenharmony_ci                if node.ppid == target_pid {
111cc290419Sopenharmony_ci                    crate::info!("send_fd_to_target pid:{target_pid}, fd:({fd}, {fd2})");
112cc290419Sopenharmony_ci                    self.put_socketpair(target_pid, fd, fd2).await;
113cc290419Sopenharmony_ci
114cc290419Sopenharmony_ci                    let bytes = fd.to_be_bytes();
115cc290419Sopenharmony_ci                    let fd_bytes = bytes.as_slice();
116cc290419Sopenharmony_ci                    let param_bytes = parameter.as_bytes();
117cc290419Sopenharmony_ci                    let param_bytes = [fd_bytes, param_bytes].concat();
118cc290419Sopenharmony_ci                    let param_bytes = param_bytes.as_slice();
119cc290419Sopenharmony_ci                    let ret = send_msg(node.fd, fd, param_bytes);
120cc290419Sopenharmony_ci                    crate::info!("send_fd_to_target ret:{}", ret);
121cc290419Sopenharmony_ci                    return ret > 0;
122cc290419Sopenharmony_ci                }
123cc290419Sopenharmony_ci            }
124cc290419Sopenharmony_ci        }
125cc290419Sopenharmony_ci        false
126cc290419Sopenharmony_ci    }
127cc290419Sopenharmony_ci
128cc290419Sopenharmony_ci    async fn send_process_list(trackers: Trackers, node_map: NodeMap) {
129cc290419Sopenharmony_ci        let trackers = trackers.lock().await;
130cc290419Sopenharmony_ci        for (channel_id2, session_id2, display) in trackers.iter() {
131cc290419Sopenharmony_ci            let message = Self::get_process_list_with_pkg_name(node_map.clone(), display.clone()).await;
132cc290419Sopenharmony_ci            let len = message.as_bytes().len();
133cc290419Sopenharmony_ci            let len_str = format!("{:04x}\n", len);
134cc290419Sopenharmony_ci            let mut header = len_str.as_bytes().to_vec();
135cc290419Sopenharmony_ci            let mut buffer = Vec::<u8>::new();
136cc290419Sopenharmony_ci            buffer.append(&mut header);
137cc290419Sopenharmony_ci            buffer.append(&mut message.as_str().as_bytes().to_vec());
138cc290419Sopenharmony_ci
139cc290419Sopenharmony_ci            let data = TaskMessage {
140cc290419Sopenharmony_ci                channel_id: *channel_id2,
141cc290419Sopenharmony_ci                command: HdcCommand::KernelEchoRaw,
142cc290419Sopenharmony_ci                payload: buffer.to_vec(),
143cc290419Sopenharmony_ci            };
144cc290419Sopenharmony_ci            transfer::put(*session_id2, data).await;
145cc290419Sopenharmony_ci        }
146cc290419Sopenharmony_ci    }
147cc290419Sopenharmony_ci
148cc290419Sopenharmony_ci    pub async fn add_tracker(&self, channel_id: u32, session_id: u32, display: DisplayType) {
149cc290419Sopenharmony_ci        let mut trackers_lock = self.trackers.lock().await;
150cc290419Sopenharmony_ci        trackers_lock.push((channel_id, session_id, display));
151cc290419Sopenharmony_ci        drop(trackers_lock);
152cc290419Sopenharmony_ci
153cc290419Sopenharmony_ci        let node_map = self.poll_node_map.clone();
154cc290419Sopenharmony_ci        Self::send_process_list(self.trackers.clone(), node_map).await;
155cc290419Sopenharmony_ci    }
156cc290419Sopenharmony_ci
157cc290419Sopenharmony_ci    pub async fn get_process_list(&self) -> String {
158cc290419Sopenharmony_ci        let mut result = String::from("");
159cc290419Sopenharmony_ci        let map = self.poll_node_map.clone();
160cc290419Sopenharmony_ci        let map = map.lock().await;
161cc290419Sopenharmony_ci        let keys = map.keys();
162cc290419Sopenharmony_ci        for key in keys {
163cc290419Sopenharmony_ci            let value = map.get(key);
164cc290419Sopenharmony_ci            if let Some(v) = value {
165cc290419Sopenharmony_ci                result.push_str((v.ppid.to_string() + "\n").as_str());
166cc290419Sopenharmony_ci            }
167cc290419Sopenharmony_ci        }
168cc290419Sopenharmony_ci        result
169cc290419Sopenharmony_ci    }
170cc290419Sopenharmony_ci
171cc290419Sopenharmony_ci    pub async fn get_process_list_with_pkg_name(map: NodeMap, display: DisplayType) -> String {
172cc290419Sopenharmony_ci        let mut result = String::from("");
173cc290419Sopenharmony_ci        let map = map.lock().await;
174cc290419Sopenharmony_ci        let keys = map.keys();
175cc290419Sopenharmony_ci        for key in keys {
176cc290419Sopenharmony_ci            let value = map.get(key);
177cc290419Sopenharmony_ci            if let Some(v) = value {
178cc290419Sopenharmony_ci                if display == DisplayType::AllApp {
179cc290419Sopenharmony_ci                    result.push_str((v.ppid.to_string() + " " + v.pkg_name.as_str() + "\n").as_str());
180cc290419Sopenharmony_ci                } else if display == DisplayType::DebugApp {
181cc290419Sopenharmony_ci                    if v.is_debug {
182cc290419Sopenharmony_ci                        result.push_str((v.ppid.to_string() + " " + v.pkg_name.as_str() + "\n").as_str());
183cc290419Sopenharmony_ci                    }
184cc290419Sopenharmony_ci                } else if display == DisplayType::AllAppWithType {
185cc290419Sopenharmony_ci                    let mut apptype = String::from("release");
186cc290419Sopenharmony_ci                    if v.is_debug {
187cc290419Sopenharmony_ci                        apptype = String::from("debug");
188cc290419Sopenharmony_ci                    }
189cc290419Sopenharmony_ci                    result.push_str((
190cc290419Sopenharmony_ci                        v.ppid.to_string() + " "
191cc290419Sopenharmony_ci                        + v.pkg_name.as_str() + " "
192cc290419Sopenharmony_ci                        + apptype.as_str() + "\n").as_str());
193cc290419Sopenharmony_ci                }
194cc290419Sopenharmony_ci            }
195cc290419Sopenharmony_ci        }
196cc290419Sopenharmony_ci        result
197cc290419Sopenharmony_ci    }
198cc290419Sopenharmony_ci
199cc290419Sopenharmony_ci    fn chang_item_once() {
200cc290419Sopenharmony_ci        static ONCE: Once = Once::new();
201cc290419Sopenharmony_ci        ONCE.call_once(|| {
202cc290419Sopenharmony_ci                let result = set_dev_item("persist.hdc.jdwp", "0");
203cc290419Sopenharmony_ci                crate::info!("set persist.hdc.jdwp to 0 result: {}", result);
204cc290419Sopenharmony_ci            }
205cc290419Sopenharmony_ci        );
206cc290419Sopenharmony_ci    }
207cc290419Sopenharmony_ci
208cc290419Sopenharmony_ci    pub async fn handle_client(fd: i32, waiter: Arc<Waiter>, node_map: NodeMap) {
209cc290419Sopenharmony_ci        crate::info!("handle_client start...");
210cc290419Sopenharmony_ci        let mut buffer: [u8; 1024] = [0; 1024];
211cc290419Sopenharmony_ci        let size = UdsServer::wrap_recv(fd, &mut buffer);
212cc290419Sopenharmony_ci        let u32_size = std::mem::size_of::<u32>();
213cc290419Sopenharmony_ci        if size > u32_size as isize {
214cc290419Sopenharmony_ci            let len = u32::from_le_bytes(buffer[0..u32_size].try_into().unwrap_or_default());
215cc290419Sopenharmony_ci            let pid = u32::from_le_bytes(buffer[u32_size..2 * u32_size].try_into().unwrap_or_default());
216cc290419Sopenharmony_ci            if pid == 0 || len == 0 {
217cc290419Sopenharmony_ci                crate::warn!("Data parsing exception. pid {pid}, len {len}");
218cc290419Sopenharmony_ci                return;
219cc290419Sopenharmony_ci            }
220cc290419Sopenharmony_ci            crate::info!("pid:{}", pid);
221cc290419Sopenharmony_ci            let is_debug = u32::from_le_bytes(buffer[u32_size * 2..3 * u32_size].try_into().unwrap_or_default()) == 1;
222cc290419Sopenharmony_ci            crate::info!("debug:{}", is_debug);
223cc290419Sopenharmony_ci            let pkg_name = String::from_utf8(buffer[u32_size * 3..len as usize].to_vec()).unwrap_or_default();
224cc290419Sopenharmony_ci            crate::info!("pkg name:{}", pkg_name);
225cc290419Sopenharmony_ci            Self::chang_item_once();
226cc290419Sopenharmony_ci
227cc290419Sopenharmony_ci            let node_map = node_map.clone();
228cc290419Sopenharmony_ci            let mut map = node_map.lock().await;
229cc290419Sopenharmony_ci            let node = PollNode::new(fd, pid, pkg_name.clone(), is_debug);
230cc290419Sopenharmony_ci            let mut key_ = -1;
231cc290419Sopenharmony_ci            for (key, value) in map.iter() {
232cc290419Sopenharmony_ci                if value.pkg_name == pkg_name {
233cc290419Sopenharmony_ci                    key_ = *key;
234cc290419Sopenharmony_ci                    UdsServer::wrap_close(value.fd);
235cc290419Sopenharmony_ci                    break;
236cc290419Sopenharmony_ci                }
237cc290419Sopenharmony_ci            }
238cc290419Sopenharmony_ci            if key_ != -1 {
239cc290419Sopenharmony_ci                map.remove(&key_);
240cc290419Sopenharmony_ci            }
241cc290419Sopenharmony_ci            map.insert(fd, node);
242cc290419Sopenharmony_ci            drop(map);
243cc290419Sopenharmony_ci
244cc290419Sopenharmony_ci            waiter.wake_one();
245cc290419Sopenharmony_ci        } else if size <= u32_size as isize {
246cc290419Sopenharmony_ci            crate::info!("The data length is too short, size = {size}");
247cc290419Sopenharmony_ci        }
248cc290419Sopenharmony_ci    }
249cc290419Sopenharmony_ci
250cc290419Sopenharmony_ci    pub fn jdwp_listen(&self) -> bool {
251cc290419Sopenharmony_ci        let fd = UdsServer::wrap_socket(SOCK_STREAM);
252cc290419Sopenharmony_ci        let name = JPID_SOCKET_PATH.as_bytes();
253cc290419Sopenharmony_ci        let socket_name = &mut [0u8; PATH_LEN];
254cc290419Sopenharmony_ci        socket_name[0] = b'\0';
255cc290419Sopenharmony_ci        socket_name[1..].copy_from_slice(name);
256cc290419Sopenharmony_ci        let addr = UdsAddr::parse_abstract(&socket_name[1..]);
257cc290419Sopenharmony_ci        if let Ok(addr_obj) = &addr {
258cc290419Sopenharmony_ci            if let Err(ret) = UdsServer::wrap_bind(fd, addr_obj) {
259cc290419Sopenharmony_ci                crate::error!("bind fail. {ret:?}");
260cc290419Sopenharmony_ci                return false;
261cc290419Sopenharmony_ci            }
262cc290419Sopenharmony_ci            let ret = UdsServer::wrap_listen(fd);
263cc290419Sopenharmony_ci            if ret < 0 {
264cc290419Sopenharmony_ci                crate::error!("listen fail, ret = {ret}");
265cc290419Sopenharmony_ci                return false;
266cc290419Sopenharmony_ci            }
267cc290419Sopenharmony_ci            let node_map = self.poll_node_map.clone();
268cc290419Sopenharmony_ci            let waiter = self.new_process_waiter.clone();
269cc290419Sopenharmony_ci            ylong_runtime::spawn(async move {
270cc290419Sopenharmony_ci                loop {
271cc290419Sopenharmony_ci                    let client_fd = UdsServer::wrap_accept(fd);
272cc290419Sopenharmony_ci                    if client_fd == -1 {
273cc290419Sopenharmony_ci                        crate::error!("wrap_accept failed");
274cc290419Sopenharmony_ci                        break;
275cc290419Sopenharmony_ci                    }
276cc290419Sopenharmony_ci                    let map = node_map.clone();
277cc290419Sopenharmony_ci                    let w = waiter.clone();
278cc290419Sopenharmony_ci                    utils::spawn(Self::handle_client(client_fd, w, map));
279cc290419Sopenharmony_ci                }
280cc290419Sopenharmony_ci            });
281cc290419Sopenharmony_ci            true
282cc290419Sopenharmony_ci        } else {
283cc290419Sopenharmony_ci            crate::info!("parse addr fail  ");
284cc290419Sopenharmony_ci            false
285cc290419Sopenharmony_ci        }
286cc290419Sopenharmony_ci    }
287cc290419Sopenharmony_ci
288cc290419Sopenharmony_ci    pub fn start_data_looper(&self) {
289cc290419Sopenharmony_ci        let node_map = self.poll_node_map.clone();
290cc290419Sopenharmony_ci        let socketpair_map = self.socketpair_map.clone();
291cc290419Sopenharmony_ci        let trackers = self.trackers.clone();
292cc290419Sopenharmony_ci        let await_fd = self.await_fd;
293cc290419Sopenharmony_ci        utils::spawn(async move {
294cc290419Sopenharmony_ci            loop {
295cc290419Sopenharmony_ci                let mut poll_nodes = Vec::<PollNode>::new();
296cc290419Sopenharmony_ci                let mut fd_node = PollNode::new(await_fd, 0, "".to_string(), false);
297cc290419Sopenharmony_ci                fd_node.revents = 0;
298cc290419Sopenharmony_ci                fd_node.events = POLLIN;
299cc290419Sopenharmony_ci                poll_nodes.push(fd_node);
300cc290419Sopenharmony_ci
301cc290419Sopenharmony_ci                let node_map_value = node_map.lock().await;
302cc290419Sopenharmony_ci                let keys = node_map_value.keys();
303cc290419Sopenharmony_ci                for k in keys {
304cc290419Sopenharmony_ci                    if let Some(n) = node_map_value.get(k) {
305cc290419Sopenharmony_ci                        poll_nodes.push(n.clone());
306cc290419Sopenharmony_ci                    }
307cc290419Sopenharmony_ci                }
308cc290419Sopenharmony_ci                let size = poll_nodes.len();
309cc290419Sopenharmony_ci
310cc290419Sopenharmony_ci                drop(node_map_value);
311cc290419Sopenharmony_ci                UdsServer::wrap_poll(poll_nodes.as_mut_slice(), size.try_into().unwrap_or_default(), -1);
312cc290419Sopenharmony_ci                let mut node_map_value = node_map.lock().await;
313cc290419Sopenharmony_ci                for pnode in &poll_nodes {
314cc290419Sopenharmony_ci                    if pnode.revents & (POLLNVAL | POLLRDHUP | POLLHUP | POLLERR) != 0 {
315cc290419Sopenharmony_ci                        crate::info!("start_data_looper remove node:{}", pnode.pkg_name);
316cc290419Sopenharmony_ci                        node_map_value.remove(&pnode.fd);
317cc290419Sopenharmony_ci                        UdsServer::wrap_close(pnode.fd);
318cc290419Sopenharmony_ci
319cc290419Sopenharmony_ci                        let pid = pnode.ppid;
320cc290419Sopenharmony_ci                        let mut socketpair_map_lock = socketpair_map.lock().await;
321cc290419Sopenharmony_ci                        if let Some(vec) = socketpair_map_lock.remove(&pid) {
322cc290419Sopenharmony_ci                            let lock = vec.lock().await;
323cc290419Sopenharmony_ci                            for (fd0, fd1) in lock.iter() {
324cc290419Sopenharmony_ci                                //此处close socketpair,会导致hdcd和aa之间的pipe异常,hdcd读取不到aa force-stop的返回结果
325cc290419Sopenharmony_ci                                crate::info!("start_data_looper close pid:{}, socketpair_fd:({},{})", pid, fd0, fd1);
326cc290419Sopenharmony_ci                            }
327cc290419Sopenharmony_ci                        }
328cc290419Sopenharmony_ci                    } else if pnode.fd == await_fd && pnode.revents & POLLIN != 0 {
329cc290419Sopenharmony_ci                        UdsServer::wrap_read_fd(await_fd);
330cc290419Sopenharmony_ci                    }
331cc290419Sopenharmony_ci                }
332cc290419Sopenharmony_ci                drop(node_map_value);
333cc290419Sopenharmony_ci                let trackers = trackers.clone();
334cc290419Sopenharmony_ci                let node_map = node_map.clone();
335cc290419Sopenharmony_ci                Self::send_process_list(trackers, node_map).await;
336cc290419Sopenharmony_ci            }
337cc290419Sopenharmony_ci        });
338cc290419Sopenharmony_ci    }
339cc290419Sopenharmony_ci
340cc290419Sopenharmony_ci    pub async fn create_fd_event_poll(&self) {
341cc290419Sopenharmony_ci        loop {
342cc290419Sopenharmony_ci            let waiter = self.new_process_waiter.clone();
343cc290419Sopenharmony_ci            waiter.wait().await;
344cc290419Sopenharmony_ci
345cc290419Sopenharmony_ci            UdsServer::wrap_write_fd(self.await_fd);
346cc290419Sopenharmony_ci        }
347cc290419Sopenharmony_ci    }
348cc290419Sopenharmony_ci
349cc290419Sopenharmony_ci    pub async fn init(&self) -> ErrCode {
350cc290419Sopenharmony_ci        if !self.jdwp_listen() {
351cc290419Sopenharmony_ci            crate::info!("jdwp_listen failed");
352cc290419Sopenharmony_ci            return ErrCode::ModuleJdwpFailed;
353cc290419Sopenharmony_ci        }
354cc290419Sopenharmony_ci
355cc290419Sopenharmony_ci        self.start_data_looper();
356cc290419Sopenharmony_ci        set_dev_item("persist.hdc.jdwp", "0");
357cc290419Sopenharmony_ci        let result = set_dev_item("persist.hdc.jdwp", "1");
358cc290419Sopenharmony_ci        crate::info!("init set persist.hdc.jdwp to 1 result: {}", result);
359cc290419Sopenharmony_ci        self.create_fd_event_poll().await;
360cc290419Sopenharmony_ci        ErrCode::Success
361cc290419Sopenharmony_ci    }
362cc290419Sopenharmony_ci}
363cc290419Sopenharmony_ci
364cc290419Sopenharmony_cipub async fn stop_session_task(session_id: u32) {
365cc290419Sopenharmony_ci    let jdwp = Jdwp::get_instance();
366cc290419Sopenharmony_ci    let mut trackers = jdwp.trackers.lock().await;
367cc290419Sopenharmony_ci    trackers.retain(|x| x.1 != session_id);
368cc290419Sopenharmony_ci}
369cc290419Sopenharmony_ci
370cc290419Sopenharmony_cipub async fn stop_task(session_id: u32, channel_id: u32) {
371cc290419Sopenharmony_ci    let jdwp = Jdwp::get_instance();
372cc290419Sopenharmony_ci    let mut trackers = jdwp.trackers.lock().await;
373cc290419Sopenharmony_ci    trackers.retain(|x| x.1 != session_id || x.0 != channel_id);
374cc290419Sopenharmony_ci}
375