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 let epoll_manager: Box::<EpollManager> = Box::default();
39 Box::into_raw(epoll_manager)
40 }
41 /// Drop unique_ptr of StreamSocket for C++ code
42 ///
43 /// # Safety
44 ///
45 /// The pointer which pointed the memory already initialized must be valid.
46 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
47 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
48 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
49 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
50 #[no_mangle]
StreamSocketDelete(raw: *mut EpollManager)51 pub unsafe extern "C" fn StreamSocketDelete(raw: *mut EpollManager) {
52 if !raw.is_null() {
53 drop(Box::from_raw(raw));
54 }
55 }
56 /// Obtain StreamSocket's fd
57 ///
58 /// # Safety
59 ///
60 /// The pointer which pointed the memory already initialized must be valid.
61 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
62 #[no_mangle]
StreamSocketGetFd(object: *const EpollManager) -> i3263 pub unsafe extern "C" fn StreamSocketGetFd(object: *const EpollManager) -> i32 {
64 info!(LOG_LABEL, "enter StreamSocketGetFd");
65 if let Some(obj) = EpollManager::as_ref(object) {
66 obj.socket_fd()
67 } else {
68 SocketStatusCode::FdFail.into()
69 }
70 }
71 /// Close socket fd after Sending data.
72 ///
73 /// # Safety
74 ///
75 /// The pointer which pointed the memory already initialized must be valid.
76 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
77 #[no_mangle]
StreamSocketClose(object: *mut EpollManager) -> i3278 pub unsafe extern "C" fn StreamSocketClose(object: *mut EpollManager) -> i32 {
79 info!(LOG_LABEL, "enter StreamSocketClose");
80 if let Some(obj) = EpollManager::as_mut(object) {
81 obj.socket_close();
82 SocketStatusCode::Ok.into()
83 } else {
84 SocketStatusCode::SocketCloseFail.into()
85 }
86 }
87 /// Set socket fd
88 ///
89 /// # Safety
90 ///
91 /// The pointer which pointed the memory already initialized must be valid.
92 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
93 #[no_mangle]
StreamSocketSetFd(object: *mut EpollManager, fd: i32) -> i3294 pub unsafe extern "C" fn StreamSocketSetFd(object: *mut EpollManager, fd: i32) -> i32 {
95 info!(LOG_LABEL, "enter StreamSocketSetFd");
96 if let Some(obj) = EpollManager::as_mut(object) {
97 obj.socket_set_fd(fd);
98 SocketStatusCode::Ok.into()
99 } else {
100 SocketStatusCode::SocketSetFdFail.into()
101 }
102 }
103