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 let stream_buffer: Box::<StreamBuffer> = Box::default();
36 Box::into_raw(stream_buffer)
37 }
38 /// Drop unique_ptr of stream_buffer for C++ code
39 ///
40 /// # Safety
41 ///
42 /// The pointer which pointed the memory already initialized must be valid.
43 /// If uninitialized memory requires special handling, please refer to std::mem::MaybeUninit.
44 /// The pointer needs to be aligned for access. If the memory pointed to by the pointer is a compact
45 /// memory layout and requires special consideration. Please refer to (#[repr(packed)]).
46 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
47 #[no_mangle]
StreamBufferDelete(raw: *mut StreamBuffer)48 pub unsafe extern "C" fn StreamBufferDelete(raw: *mut StreamBuffer) {
49 if !raw.is_null() {
50 drop(Box::from_raw(raw));
51 }
52 }
53 /// Obtain the first address of sz_buff
54 ///
55 /// # Safety
56 ///
57 /// The pointer which pointed the memory already initialized must be valid.
58 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
59 #[no_mangle]
StreamBufferData(object: *const StreamBuffer) -> *const c_char60 pub unsafe extern "C" fn StreamBufferData(object: *const StreamBuffer) -> *const c_char {
61 info!(LOG_LABEL, "enter data");
62 if let Some(obj) = StreamBuffer::as_ref(object) {
63 obj.data()
64 } else {
65 std::ptr::null()
66 }
67 }
68 /// Obtain position writen
69 ///
70 /// # Safety
71 ///
72 /// The pointer which pointed the memory already initialized must be valid.
73 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
74 #[no_mangle]
StreamBufferSize(object: *const StreamBuffer) -> usize75 pub unsafe extern "C" fn StreamBufferSize(object: *const StreamBuffer) -> usize {
76 info!(LOG_LABEL, "enter size");
77 if let Some(obj) = StreamBuffer::as_ref(object) {
78 obj.size()
79 } else {
80 0
81 }
82 }
83 /// Reset StreamBuffer value
84 ///
85 /// # Safety
86 ///
87 /// The pointer which pointed the memory already initialized must be valid.
88 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
89 #[no_mangle]
StreamBufferReset(object: *mut StreamBuffer) -> i3290 pub unsafe extern "C" fn StreamBufferReset(object: *mut StreamBuffer) -> i32 {
91 info!(LOG_LABEL, "enter StreamBufferReset");
92 if let Some(obj) = StreamBuffer::as_mut(object) {
93 obj.reset();
94 BufferStatusCode::Ok.into()
95 } else {
96 BufferStatusCode::ResetFail.into()
97 }
98 }
99 /// Clean StreamBuffer value
100 ///
101 /// # Safety
102 ///
103 /// The pointer which pointed the memory already initialized must be valid.
104 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
105 #[no_mangle]
StreamBufferClean(object: *mut StreamBuffer) -> i32106 pub unsafe extern "C" fn StreamBufferClean(object: *mut StreamBuffer) -> i32 {
107 info!(LOG_LABEL, "enter clean");
108 if let Some(obj) = StreamBuffer::as_mut(object) {
109 obj.clean();
110 BufferStatusCode::Ok.into()
111 } else {
112 BufferStatusCode::CleanFail.into()
113 }
114 }
115 /// Write object data into buf
116 ///
117 /// # Safety
118 ///
119 /// The pointer which pointed the memory already initialized must be valid.
120 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
121 #[no_mangle]
StreamBufferWrite(object: *mut StreamBuffer, buf: *const StreamBuffer) -> bool122 pub unsafe extern "C" fn StreamBufferWrite(object: *mut StreamBuffer, buf: *const StreamBuffer) -> bool {
123 info!(LOG_LABEL, "enter StreamBufferWrite");
124 if let Some(obj) = StreamBuffer::as_mut(object) {
125 if let Some(buffer) = StreamBuffer::as_ref(buf) {
126 obj.write_streambuffer(buffer)
127 } else {
128 false
129 }
130 } else {
131 false
132 }
133 }
134 /// Read object data into buf
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]
StreamBufferRead(object: *const StreamBuffer, buf: *mut StreamBuffer) -> bool141 pub unsafe extern "C" fn StreamBufferRead(object: *const StreamBuffer, buf: *mut StreamBuffer) -> bool {
142 info!(LOG_LABEL, "enter StreamBufferRead");
143 if let Some(obj) = StreamBuffer::as_ref(object) {
144 if let Some(buffer) = StreamBuffer::as_mut(buf) {
145 obj.read_streambuffer(buffer)
146 } else {
147 false
148 }
149 } else {
150 false
151 }
152 }
153 /// Obtain status of reading or writing
154 ///
155 /// # Safety
156 ///
157 /// The pointer which pointed the memory already initialized must be valid.
158 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
159 #[no_mangle]
StreamBufferChkRWError(object: *const StreamBuffer) -> bool160 pub unsafe extern "C" fn StreamBufferChkRWError(object: *const StreamBuffer) -> bool {
161 if let Some(obj) = StreamBuffer::as_ref(object) {
162 obj.chk_rwerror()
163 } else {
164 false
165 }
166 }
167 /// Obtain remarked string of status
168 ///
169 /// # Safety
170 ///
171 /// The pointer which pointed the memory already initialized must be valid.
172 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
173 #[no_mangle]
StreamBufferGetErrorStatusRemark(object: *const StreamBuffer) -> *const c_char174 pub unsafe extern "C" fn StreamBufferGetErrorStatusRemark(object: *const StreamBuffer) -> *const c_char {
175 info!(LOG_LABEL, "enter StreamBufferGetErrorStatusRemark");
176 if let Some(obj) = StreamBuffer::as_ref(object) {
177 obj.get_error_status_remark()
178 } else {
179 std::ptr::null()
180 }
181 }
182 /// Buf Bytes will be writen into streambuffer's sz_buff.
183 ///
184 /// # Safety
185 ///
186 /// The pointer which pointed the memory already initialized must be valid.
187 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
188 #[no_mangle]
StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool189 pub unsafe extern "C" fn StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
190 info!(LOG_LABEL, "enter StreamBufferWriteChar");
191 if let Some(obj) = StreamBuffer::as_mut(object) {
192 obj.write_char_usize(buf, size)
193 } else {
194 false
195 }
196 }
197 /// Check whether the condition of writing could be satisfied or not.
198 ///
199 /// # Safety
200 ///
201 /// The pointer which pointed the memory already initialized must be valid.
202 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
203 #[no_mangle]
StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool204 pub unsafe extern "C" fn StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool {
205 info!(LOG_LABEL, "enter StreamBufferCheckWrite");
206 if let Some(obj) = StreamBuffer::as_mut(object) {
207 obj.check_write(size)
208 } else {
209 false
210 }
211 }
212 /// CircleStreamBuffer will copy data to beginning.
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]
CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32219 pub unsafe extern "C" fn CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32 {
220 info!(LOG_LABEL, "enter CircleStreamBufferCopyDataToBegin");
221 if let Some(obj) = StreamBuffer::as_mut(object) {
222 obj.copy_data_to_begin();
223 BufferStatusCode::Ok.into()
224 } else {
225 BufferStatusCode::CopyDataToBeginFail.into()
226 }
227 }
228 /// Read sz_buf to buf.
229 ///
230 /// # Safety
231 ///
232 /// The pointer which pointed the memory already initialized must be valid.
233 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
234 #[no_mangle]
StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool235 pub unsafe extern "C" fn StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
236 info!(LOG_LABEL, "enter StreamBufferReadChar");
237 if let Some(obj) = StreamBuffer::as_mut(object) {
238 obj.read_char_usize(buf, size)
239 } else {
240 false
241 }
242 }
243 /// Write sz_buf to buf.
244 ///
245 /// # Safety
246 ///
247 /// The pointer which pointed the memory already initialized must be valid.
248 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
249 #[no_mangle]
CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool250 pub unsafe extern "C" fn CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
251 info!(LOG_LABEL, "enter CircleStreamBufferWrite");
252 if let Some(obj) = StreamBuffer::as_mut(object) {
253 obj.circle_write(buf, size)
254 } else {
255 false
256 }
257 }
258 /// read packets on client.
259 ///
260 /// # Safety
261 ///
262 /// The pointer which pointed the memory already initialized must be valid.
263 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
264 #[no_mangle]
ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient, callback_fun: ClientPacketCallBackFun) -> i32265 pub unsafe extern "C" fn ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient,
266 callback_fun: ClientPacketCallBackFun) -> i32 {
267 info!(LOG_LABEL,"enter ReadClientPackets");
268 if let Some(obj) = StreamBuffer::as_mut(object) {
269 obj.read_client_packets(stream_client, callback_fun);
270 BufferStatusCode::Ok.into()
271 } else {
272 BufferStatusCode::ReadClientPacketsFail.into()
273 }
274 }
275 /// StreamBufferReadBuf.
276 ///
277 /// # Safety
278 ///
279 /// The pointer which pointed the memory already initialized must be valid.
280 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
281 #[no_mangle]
StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char282 pub unsafe extern "C" fn StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char {
283 if let Some(obj) = StreamBuffer::as_ref(object) {
284 obj.read_buf()
285 } else {
286 std::ptr::null()
287 }
288 }
289 /// obtain streambuffer's r_count field.
290 ///
291 /// # Safety
292 ///
293 /// The pointer which pointed the memory already initialized must be valid.
294 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
295 #[no_mangle]
StreamBufferGetRcount(object: *const StreamBuffer) -> i32296 pub unsafe extern "C" fn StreamBufferGetRcount(object: *const StreamBuffer) -> i32 {
297 if let Some(obj) = StreamBuffer::as_ref(object) {
298 obj.r_count() as i32
299 } else {
300 BufferStatusCode::RcountFail.into()
301 }
302 }
303 /// obtain streambuffer's w_count field.
304 ///
305 /// # Safety
306 ///
307 /// The pointer which pointed the memory already initialized must be valid.
308 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
309 #[no_mangle]
StreamBufferGetWcount(object: *const StreamBuffer) -> i32310 pub unsafe extern "C" fn StreamBufferGetWcount(object: *const StreamBuffer) -> i32 {
311 if let Some(obj) = StreamBuffer::as_ref(object) {
312 obj.w_count() as i32
313 } else {
314 BufferStatusCode::WcountFail.into()
315 }
316 }
317 /// obtain streambuffer's w_pos field.
318 ///
319 /// # Safety
320 ///
321 /// The pointer which pointed the memory already initialized must be valid.
322 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
323 #[no_mangle]
StreamBufferGetWpos(object: *const StreamBuffer) -> i32324 pub unsafe extern "C" fn StreamBufferGetWpos(object: *const StreamBuffer) -> i32 {
325 if let Some(obj) = StreamBuffer::as_ref(object) {
326 obj.w_pos() as i32
327 } else {
328 BufferStatusCode::WposFail.into()
329 }
330 }
331 /// obtain streambuffer's r_pos field.
332 ///
333 /// # Safety
334 ///
335 /// The pointer which pointed the memory already initialized must be valid.
336 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
337 #[no_mangle]
StreamBufferGetRpos(object: *const StreamBuffer) -> i32338 pub unsafe extern "C" fn StreamBufferGetRpos(object: *const StreamBuffer) -> i32 {
339 if let Some(obj) = StreamBuffer::as_ref(object) {
340 obj.r_pos() as i32
341 } else {
342 BufferStatusCode::RposFail.into()
343 }
344 }
345 /// obtain streambuffer's sz_buff field.
346 ///
347 /// # Safety
348 ///
349 /// The pointer which pointed the memory already initialized must be valid.
350 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
351 #[no_mangle]
StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char352 pub unsafe extern "C" fn StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char {
353 if let Some(obj) = StreamBuffer::as_ref(object) {
354 obj.sz_buff()
355 } else {
356 std::ptr::null()
357 }
358 }
359 /// obtain streambuffer's rw_err_status field.
360 ///
361 /// # Safety
362 ///
363 /// The pointer which pointed the memory already initialized must be valid.
364 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
365 #[no_mangle]
StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32366 pub unsafe extern "C" fn StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32 {
367 if let Some(obj) = StreamBuffer::as_mut(object) {
368 obj.set_rw_error_status(rw_error_status);
369 BufferStatusCode::Ok.into()
370 } else {
371 BufferStatusCode::SetRwErrStatusFail.into()
372 }
373 }
374 /// set streambuffer's r_pos field.
375 ///
376 /// # Safety
377 ///
378 /// The pointer which pointed the memory already initialized must be valid.
379 /// Makesure the memory shouldn't be dropped while whose pointer is being used.
380 #[no_mangle]
StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32381 pub unsafe extern "C" fn StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32 {
382 if let Some(obj) = StreamBuffer::as_mut(object) {
383 obj.set_r_pos(r_pos as usize);
384 BufferStatusCode::Ok.into()
385 } else {
386 BufferStatusCode::SetRposFail.into()
387 }
388 }
389
390