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 crate::error::SessionStatusCode;
18 use hilog_rust::{info, hilog, HiLogLabel, LogType};
19 const LOG_LABEL: HiLogLabel = HiLogLabel {
20     log_type: LogType::LogCore,
21     domain: 0xD002700,
22     tag: "stream_session_ffi"
23 };
24 /// Create unique_ptr of StreamSession for C++ code
25 ///
26 /// # Safety
27 ///
28 /// The pointer which pointed the memory already initialized must be valid.
29 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
30 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
31 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
32 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
33 #[no_mangle]
StreamSessionCreate() -> *mut StreamSession34 pub unsafe extern "C" fn StreamSessionCreate() -> *mut StreamSession {
35     info!(LOG_LABEL, "enter StreamSessionCreate");
36     let stream_session: Box::<StreamSession> = Box::default();
37     Box::into_raw(stream_session)
38 }
39 /// Drop unique_ptr of StreamSession for C++ code
40 ///
41 /// # Safety
42 ///
43 /// The pointer which pointed the memory already initialized must be valid.
44 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
45 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
46 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
47 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
48 #[no_mangle]
StreamSessionDelete(raw: *mut StreamSession)49 pub unsafe extern "C" fn StreamSessionDelete(raw: *mut StreamSession) {
50     info!(LOG_LABEL, "enter StreamSessionDelete");
51     if !raw.is_null() {
52         drop(Box::from_raw(raw));
53     }
54 }
55 /// Set StreamSession's uid
56 ///
57 /// # Safety
58 ///
59 /// The pointer which pointed the memory already initialized must be valid.
60 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
61 #[no_mangle]
StreamSessionSetUid(object: *mut StreamSession, uid: i32) -> i3262 pub unsafe extern "C" fn StreamSessionSetUid(object: *mut StreamSession, uid: i32) -> i32 {
63     info!(LOG_LABEL, "enter StreamSessionSetUid");
64     if let Some(obj) = StreamSession::as_mut(object) {
65         obj.set_uid(uid);
66         SessionStatusCode::Ok.into()
67     } else {
68         SessionStatusCode::SetUidFail.into()
69     }
70 }
71 /// Set StreamSession's fd
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]
StreamSessionSetFd(object: *mut StreamSession, fd: i32) -> i3278 pub unsafe extern "C" fn StreamSessionSetFd(object: *mut StreamSession, fd: i32) -> i32 {
79     info!(LOG_LABEL, "enter StreamSessionSetFd");
80     if let Some(obj) = StreamSession::as_mut(object) {
81         obj.set_fd(fd);
82         SessionStatusCode::Ok.into()
83     } else {
84         SessionStatusCode::SetFdFail.into()
85     }
86 }
87 /// Set StreamSession's pid
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]
StreamSessionSetPid(object: *mut StreamSession, pid: i32) -> i3294 pub unsafe extern "C" fn StreamSessionSetPid(object: *mut StreamSession, pid: i32) -> i32 {
95     info!(LOG_LABEL, "enter StreamSessionSetPid");
96     if let Some(obj) = StreamSession::as_mut(object) {
97         obj.set_pid(pid);
98         SessionStatusCode::Ok.into()
99     } else {
100         SessionStatusCode::SetPidFail.into()
101     }
102 }
103 /// Obtain StreamSession's uid
104 ///
105 /// # Safety
106 ///
107 /// The pointer which pointed the memory already initialized must be valid.
108 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
109 #[no_mangle]
StreamSessionGetUid(object: *const StreamSession) -> i32110 pub unsafe extern "C" fn StreamSessionGetUid(object: *const StreamSession) -> i32 {
111     info!(LOG_LABEL, "enter StreamSessionGetUid");
112     if let Some(obj) = StreamSession::as_ref(object) {
113         obj.uid()
114     } else {
115         SessionStatusCode::UidFail.into()
116     }
117 }
118 /// Obtain StreamSession's pid
119 ///
120 /// # Safety
121 ///
122 /// The pointer which pointed the memory already initialized must be valid.
123 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
124 #[no_mangle]
StreamSessionGetPid(object: *const StreamSession) -> i32125 pub unsafe extern "C" fn StreamSessionGetPid(object: *const StreamSession) -> i32 {
126     info!(LOG_LABEL, "enter StreamSessionGetPid");
127     if let Some(obj) = StreamSession::as_ref(object) {
128         obj.pid()
129     } else {
130         SessionStatusCode::PidFail.into()
131     }
132 }
133 
134 /// Obtain StreamSession's fd
135 ///
136 /// # Safety
137 ///
138 /// The pointer which pointed the memory already initialized must be valid.
139 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
140 #[no_mangle]
StreamSessionGetFd(object: *const StreamSession) -> i32141 pub unsafe extern "C" fn StreamSessionGetFd(object: *const StreamSession) -> i32 {
142     info!(LOG_LABEL, "enter StreamSessionGetFd");
143     if let Some(obj) = StreamSession::as_ref(object) {
144         obj.session_fd()
145     } else {
146         SessionStatusCode::FdFail.into()
147     }
148 }
149 /// Set StreamSession's tokentype
150 ///
151 /// # Safety
152 ///
153 /// The pointer which pointed the memory already initialized must be valid.
154 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
155 #[no_mangle]
StreamSessionSetTokenType(object: *mut StreamSession, style: i32) -> i32156 pub unsafe extern "C" fn StreamSessionSetTokenType(object: *mut StreamSession, style: i32) -> i32 {
157     info!(LOG_LABEL, "enter StreamSessionSetTokenType");
158     if let Some(obj) = StreamSession::as_mut(object) {
159         obj.set_token_type(style);
160         SessionStatusCode::Ok.into()
161     } else {
162         SessionStatusCode::SetTokenTypeFail.into()
163     }
164 }
165 /// Obtain StreamSession's tokentype
166 ///
167 /// # Safety
168 ///
169 /// The pointer which pointed the memory already initialized must be valid.
170 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
171 #[no_mangle]
StreamSessionGetTokenType(object: *const StreamSession) -> i32172 pub unsafe extern "C" fn StreamSessionGetTokenType(object: *const StreamSession) -> i32 {
173     info!(LOG_LABEL, "enter StreamSessionGetTokenType");
174     if let Some(obj) = StreamSession::as_ref(object) {
175         obj.token_type()
176     } else {
177         SessionStatusCode::TokenTypeFail.into()
178     }
179 }
180 /// Obtain StreamSession's moduletype
181 ///
182 /// # Safety
183 ///
184 /// The pointer which pointed the memory already initialized must be valid.
185 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
186 #[no_mangle]
StreamSessionGetModuleType(object: *const StreamSession) -> i32187 pub unsafe extern "C" fn StreamSessionGetModuleType(object: *const StreamSession) -> i32 {
188     info!(LOG_LABEL, "enter StreamSessionGetModuleType");
189     if let Some(obj) = StreamSession::as_ref(object) {
190         obj.module_type()
191     } else {
192         SessionStatusCode::ModuleTypeFail.into()
193     }
194 }
195 /// Close StreamSession's fd
196 ///
197 /// # Safety
198 ///
199 /// The pointer which pointed the memory already initialized must be valid.
200 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
201 #[no_mangle]
StreamSessionClose(object: *mut StreamSession) -> i32202 pub unsafe extern "C" fn StreamSessionClose(object: *mut StreamSession) -> i32 {
203     info!(LOG_LABEL, "enter StreamSessionClose");
204     if let Some(obj) = StreamSession::as_mut(object) {
205         obj.session_close();
206         SessionStatusCode::Ok.into()
207     } else {
208         SessionStatusCode::CloseFail.into()
209     }
210 }
211 
212 /// Send message via StreamSessions
213 ///
214 /// # Safety
215 ///
216 /// The pointer which pointed the memory already initialized must be valid.
217 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
218 #[no_mangle]
StreamSessionSendMsg(object: *const StreamSession, buf: *const c_char, size: usize) -> bool219 pub unsafe extern "C" fn StreamSessionSendMsg(object: *const StreamSession, buf: *const c_char, size: usize) -> bool {
220     info!(LOG_LABEL, "enter StreamSessionSendMsg");
221     if let Some(obj) = StreamSession::as_ref(object) {
222         obj.session_send_msg(buf, size)
223     } else {
224         false
225     }
226 }