1 /*
2  * Copyright (C) 2023 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 use super::*;
17 use hilog_rust::{hilog, HiLogLabel, LogType};
18 use crate::{error::SocketStatusCode, epoll_manager::EpollManager};
19 use std::mem::drop;
20 use std::ffi::c_char;
21 const LOG_LABEL: HiLogLabel = HiLogLabel {
22     log_type: LogType::LogCore,
23     domain: 0xD002700,
24     tag: "stream_socket_ffi"
25 };
26 
27 /// Create unique_ptr of StreamSocket for C++ code
28 ///
29 /// # Safety
30 ///
31 /// The pointer which pointed the memory already initialized must be valid.
32 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
33 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
34 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
35 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
36 #[no_mangle]
StreamSocketCreate() -> *mut EpollManager37 pub unsafe extern "C" fn StreamSocketCreate() -> *mut EpollManager {
38     info!(LOG_LABEL, "enter StreamSocketCreate");
39     let epoll_manager: Box::<EpollManager> = Box::default();
40     Box::into_raw(epoll_manager)
41 }
42 /// Drop unique_ptr of StreamSocket for C++ code
43 ///
44 /// # Safety
45 ///
46 /// The pointer which pointed the memory already initialized must be valid.
47 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
48 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
49 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
50 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
51 #[no_mangle]
StreamSocketDelete(raw: *mut EpollManager)52 pub unsafe extern "C" fn StreamSocketDelete(raw: *mut EpollManager) {
53     info!(LOG_LABEL, "enter StreamSocketDelete");
54     if !raw.is_null() {
55         drop(Box::from_raw(raw));
56     }
57 }
58 /// Obtain StreamSocket's fd
59 ///
60 /// # Safety
61 ///
62 /// The pointer which pointed the memory already initialized must be valid.
63 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
64 #[no_mangle]
StreamSocketGetFd(object: *const EpollManager) -> i3265 pub unsafe extern "C" fn StreamSocketGetFd(object: *const EpollManager) -> i32 {
66     info!(LOG_LABEL, "enter StreamSocketGetFd");
67     if let Some(obj) = EpollManager::as_ref(object) {
68         obj.socket_fd()
69     } else {
70         SocketStatusCode::FdFail.into()
71     }
72 }
73 /// Close socket fd after Sending data.
74 ///
75 /// # Safety
76 ///
77 /// The pointer which pointed the memory already initialized must be valid.
78 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
79 #[no_mangle]
StreamSocketClose(object: *mut EpollManager) -> i3280 pub unsafe extern "C" fn StreamSocketClose(object: *mut EpollManager) -> i32 {
81     info!(LOG_LABEL, "enter StreamSocketClose");
82     if let Some(obj) = EpollManager::as_mut(object) {
83         obj.socket_close();
84         SocketStatusCode::Ok.into()
85     } else {
86         SocketStatusCode::SocketCloseFail.into()
87     }
88 }
89 /// Set socket fd
90 ///
91 /// # Safety
92 ///
93 /// The pointer which pointed the memory already initialized must be valid.
94 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
95 #[no_mangle]
StreamSocketSetFd(object: *mut EpollManager, fd: i32) -> i3296 pub unsafe extern "C" fn StreamSocketSetFd(object: *mut EpollManager, fd: i32) -> i32 {
97     info!(LOG_LABEL, "enter StreamSocketSetFd");
98     if let Some(obj) = EpollManager::as_mut(object) {
99         obj.socket_set_fd(fd);
100         SocketStatusCode::Ok.into()
101     } else {
102         SocketStatusCode::SocketSetFdFail.into()
103     }
104 }
105