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::{info, hilog, HiLogLabel, LogType};
18 use crate::error::BufferStatusCode;
19 const LOG_LABEL: HiLogLabel = HiLogLabel {
20 log_type: LogType::LogCore,
21 domain: 0xD002700,
22 tag: "stream_buffer_ffi"
23 };
24 /// Create unique_ptr of stream_buffer 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]
StreamBufferCreate() -> *mut StreamBuffer34 pub unsafe extern "C" fn StreamBufferCreate() -> *mut StreamBuffer {
35 info!(LOG_LABEL, "enter StreamBufferCreate");
36 let stream_buffer: Box::<StreamBuffer> = Box::default();
37 Box::into_raw(stream_buffer)
38 }
39 /// Drop unique_ptr of stream_buffer 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]
StreamBufferDelete(raw: *mut StreamBuffer)49 pub unsafe extern "C" fn StreamBufferDelete(raw: *mut StreamBuffer) {
50 info!(LOG_LABEL, "enter StreamBufferDelete");
51 if !raw.is_null() {
52 drop(Box::from_raw(raw));
53 }
54 }
55 /// Obtain the first address of sz_buff
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]
StreamBufferData(object: *const StreamBuffer) -> *const c_char62 pub unsafe extern "C" fn StreamBufferData(object: *const StreamBuffer) -> *const c_char {
63 info!(LOG_LABEL, "enter data");
64 if let Some(obj) = StreamBuffer::as_ref(object) {
65 obj.data()
66 } else {
67 std::ptr::null()
68 }
69 }
70 /// Obtain position writen
71 ///
72 /// # Safety
73 ///
74 /// The pointer which pointed the memory already initialized must be valid.
75 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
76 #[no_mangle]
StreamBufferSize(object: *const StreamBuffer) -> usize77 pub unsafe extern "C" fn StreamBufferSize(object: *const StreamBuffer) -> usize {
78 info!(LOG_LABEL, "enter size");
79 if let Some(obj) = StreamBuffer::as_ref(object) {
80 obj.size()
81 } else {
82 0
83 }
84 }
85 /// Reset StreamBuffer value
86 ///
87 /// # Safety
88 ///
89 /// The pointer which pointed the memory already initialized must be valid.
90 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
91 #[no_mangle]
StreamBufferReset(object: *mut StreamBuffer) -> i3292 pub unsafe extern "C" fn StreamBufferReset(object: *mut StreamBuffer) -> i32 {
93 info!(LOG_LABEL, "enter StreamBufferReset");
94 if let Some(obj) = StreamBuffer::as_mut(object) {
95 obj.reset();
96 BufferStatusCode::Ok.into()
97 } else {
98 BufferStatusCode::ResetFail.into()
99 }
100 }
101 /// Clean StreamBuffer value
102 ///
103 /// # Safety
104 ///
105 /// The pointer which pointed the memory already initialized must be valid.
106 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
107 #[no_mangle]
StreamBufferClean(object: *mut StreamBuffer) -> i32108 pub unsafe extern "C" fn StreamBufferClean(object: *mut StreamBuffer) -> i32 {
109 info!(LOG_LABEL, "enter clean");
110 if let Some(obj) = StreamBuffer::as_mut(object) {
111 obj.clean();
112 BufferStatusCode::Ok.into()
113 } else {
114 BufferStatusCode::CleanFail.into()
115 }
116 }
117 /// Write object data into buf
118 ///
119 /// # Safety
120 ///
121 /// The pointer which pointed the memory already initialized must be valid.
122 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
123 #[no_mangle]
StreamBufferWrite(object: *mut StreamBuffer, buf: *const StreamBuffer) -> bool124 pub unsafe extern "C" fn StreamBufferWrite(object: *mut StreamBuffer, buf: *const StreamBuffer) -> bool {
125 info!(LOG_LABEL, "enter StreamBufferWrite");
126 if let Some(obj) = StreamBuffer::as_mut(object) {
127 if let Some(buffer) = StreamBuffer::as_ref(buf) {
128 obj.write_streambuffer(buffer)
129 } else {
130 false
131 }
132 } else {
133 false
134 }
135 }
136 /// Read object data into buf
137 ///
138 /// # Safety
139 ///
140 /// The pointer which pointed the memory already initialized must be valid.
141 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
142 #[no_mangle]
StreamBufferRead(object: *const StreamBuffer, buf: *mut StreamBuffer) -> bool143 pub unsafe extern "C" fn StreamBufferRead(object: *const StreamBuffer, buf: *mut StreamBuffer) -> bool {
144 info!(LOG_LABEL, "enter StreamBufferRead");
145 if let Some(obj) = StreamBuffer::as_ref(object) {
146 if let Some(buffer) = StreamBuffer::as_mut(buf) {
147 obj.read_streambuffer(buffer)
148 } else {
149 false
150 }
151 } else {
152 false
153 }
154 }
155 /// Obtain status of reading or writing
156 ///
157 /// # Safety
158 ///
159 /// The pointer which pointed the memory already initialized must be valid.
160 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
161 #[no_mangle]
StreamBufferChkRWError(object: *const StreamBuffer) -> bool162 pub unsafe extern "C" fn StreamBufferChkRWError(object: *const StreamBuffer) -> bool {
163 if let Some(obj) = StreamBuffer::as_ref(object) {
164 obj.chk_rwerror()
165 } else {
166 false
167 }
168 }
169 /// Obtain remarked string of status
170 ///
171 /// # Safety
172 ///
173 /// The pointer which pointed the memory already initialized must be valid.
174 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
175 #[no_mangle]
StreamBufferGetErrorStatusRemark(object: *const StreamBuffer) -> *const c_char176 pub unsafe extern "C" fn StreamBufferGetErrorStatusRemark(object: *const StreamBuffer) -> *const c_char {
177 info!(LOG_LABEL, "enter StreamBufferGetErrorStatusRemark");
178 if let Some(obj) = StreamBuffer::as_ref(object) {
179 // SAFETY: The Rust side creates a CString string and this function should be called only here
180 obj.get_error_status_remark()
181 } else {
182 std::ptr::null()
183 }
184 }
185 /// Buf Bytes will be writen into streambuffer's sz_buff.
186 ///
187 /// # Safety
188 ///
189 /// The pointer which pointed the memory already initialized must be valid.
190 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
191 #[no_mangle]
StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool192 pub unsafe extern "C" fn StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
193 info!(LOG_LABEL, "enter StreamBufferWriteChar");
194 if let Some(obj) = StreamBuffer::as_mut(object) {
195 obj.write_char_usize(buf, size)
196 } else {
197 false
198 }
199 }
200 /// Check whether the condition of writing could be satisfied or not.
201 ///
202 /// # Safety
203 ///
204 /// The pointer which pointed the memory already initialized must be valid.
205 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
206 #[no_mangle]
StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool207 pub unsafe extern "C" fn StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool {
208 info!(LOG_LABEL, "enter StreamBufferCheckWrite");
209 if let Some(obj) = StreamBuffer::as_mut(object) {
210 obj.check_write(size)
211 } else {
212 false
213 }
214 }
215 /// CircleStreamBuffer will copy data to beginning.
216 ///
217 /// # Safety
218 ///
219 /// The pointer which pointed the memory already initialized must be valid.
220 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
221 #[no_mangle]
CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32222 pub unsafe extern "C" fn CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32 {
223 info!(LOG_LABEL, "enter CircleStreamBufferCopyDataToBegin");
224 if let Some(obj) = StreamBuffer::as_mut(object) {
225 obj.copy_data_to_begin();
226 BufferStatusCode::Ok.into()
227 } else {
228 BufferStatusCode::CopyDataToBeginFail.into()
229 }
230 }
231 /// Read sz_buf to buf.
232 ///
233 /// # Safety
234 ///
235 /// The pointer which pointed the memory already initialized must be valid.
236 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
237 #[no_mangle]
StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool238 pub unsafe extern "C" fn StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
239 info!(LOG_LABEL, "enter StreamBufferReadChar");
240 if let Some(obj) = StreamBuffer::as_mut(object) {
241 obj.read_char_usize(buf, size)
242 } else {
243 false
244 }
245 }
246 /// Write sz_buf to buf.
247 ///
248 /// # Safety
249 ///
250 /// The pointer which pointed the memory already initialized must be valid.
251 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
252 #[no_mangle]
CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool253 pub unsafe extern "C" fn CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
254 info!(LOG_LABEL, "enter CircleStreamBufferWrite");
255 if let Some(obj) = StreamBuffer::as_mut(object) {
256 obj.circle_write(buf, size)
257 } else {
258 false
259 }
260 }
261 /// read packets on client.
262 ///
263 /// # Safety
264 ///
265 /// The pointer which pointed the memory already initialized must be valid.
266 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
267 #[no_mangle]
ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient, callback_fun: ClientPacketCallBackFun) -> i32268 pub unsafe extern "C" fn ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient,
269 callback_fun: ClientPacketCallBackFun) -> i32 {
270 info!(LOG_LABEL,"enter ReadClientPackets");
271 if let Some(obj) = StreamBuffer::as_mut(object) {
272 obj.read_client_packets(stream_client, callback_fun);
273 BufferStatusCode::Ok.into()
274 } else {
275 BufferStatusCode::ReadClientPacketsFail.into()
276 }
277 }
278 /// StreamBufferReadBuf.
279 ///
280 /// # Safety
281 ///
282 /// The pointer which pointed the memory already initialized must be valid.
283 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
284 #[no_mangle]
StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char285 pub unsafe extern "C" fn StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char {
286 if let Some(obj) = StreamBuffer::as_ref(object) {
287 obj.read_buf()
288 } else {
289 std::ptr::null()
290 }
291 }
292 /// obtain streambuffer's r_count field.
293 ///
294 /// # Safety
295 ///
296 /// The pointer which pointed the memory already initialized must be valid.
297 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
298 #[no_mangle]
StreamBufferGetRcount(object: *const StreamBuffer) -> i32299 pub unsafe extern "C" fn StreamBufferGetRcount(object: *const StreamBuffer) -> i32 {
300 if let Some(obj) = StreamBuffer::as_ref(object) {
301 obj.r_count() as i32
302 } else {
303 BufferStatusCode::RcountFail.into()
304 }
305 }
306 /// obtain streambuffer's w_count field.
307 ///
308 /// # Safety
309 ///
310 /// The pointer which pointed the memory already initialized must be valid.
311 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
312 #[no_mangle]
StreamBufferGetWcount(object: *const StreamBuffer) -> i32313 pub unsafe extern "C" fn StreamBufferGetWcount(object: *const StreamBuffer) -> i32 {
314 if let Some(obj) = StreamBuffer::as_ref(object) {
315 obj.w_count() as i32
316 } else {
317 BufferStatusCode::WcountFail.into()
318 }
319 }
320 /// obtain streambuffer's w_pos field.
321 ///
322 /// # Safety
323 ///
324 /// The pointer which pointed the memory already initialized must be valid.
325 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
326 #[no_mangle]
StreamBufferGetWpos(object: *const StreamBuffer) -> i32327 pub unsafe extern "C" fn StreamBufferGetWpos(object: *const StreamBuffer) -> i32 {
328 if let Some(obj) = StreamBuffer::as_ref(object) {
329 obj.w_pos() as i32
330 } else {
331 BufferStatusCode::WposFail.into()
332 }
333 }
334 /// obtain streambuffer's r_pos field.
335 ///
336 /// # Safety
337 ///
338 /// The pointer which pointed the memory already initialized must be valid.
339 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
340 #[no_mangle]
StreamBufferGetRpos(object: *const StreamBuffer) -> i32341 pub unsafe extern "C" fn StreamBufferGetRpos(object: *const StreamBuffer) -> i32 {
342 if let Some(obj) = StreamBuffer::as_ref(object) {
343 obj.r_pos() as i32
344 } else {
345 BufferStatusCode::RposFail.into()
346 }
347 }
348 /// obtain streambuffer's sz_buff field.
349 ///
350 /// # Safety
351 ///
352 /// The pointer which pointed the memory already initialized must be valid.
353 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
354 #[no_mangle]
StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char355 pub unsafe extern "C" fn StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char {
356 if let Some(obj) = StreamBuffer::as_ref(object) {
357 obj.sz_buff()
358 } else {
359 std::ptr::null()
360 }
361 }
362 /// obtain streambuffer's rw_err_status field.
363 ///
364 /// # Safety
365 ///
366 /// The pointer which pointed the memory already initialized must be valid.
367 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
368 #[no_mangle]
StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32369 pub unsafe extern "C" fn StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32 {
370 if let Some(obj) = StreamBuffer::as_mut(object) {
371 obj.set_rw_error_status(rw_error_status);
372 BufferStatusCode::Ok.into()
373 } else {
374 BufferStatusCode::SetRwErrStatusFail.into()
375 }
376 }
377 /// set streambuffer's r_pos field.
378 ///
379 /// # Safety
380 ///
381 /// The pointer which pointed the memory already initialized must be valid.
382 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
383 #[no_mangle]
StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32384 pub unsafe extern "C" fn StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32 {
385 if let Some(obj) = StreamBuffer::as_mut(object) {
386 obj.set_r_pos(r_pos as usize);
387 BufferStatusCode::Ok.into()
388 } else {
389 BufferStatusCode::SetRposFail.into()
390 }
391 }
392
393