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          obj.get_error_status_remark()
180      } else {
181          std::ptr::null()
182      }
183  }
184  /// Buf Bytes will be writen into streambuffer's sz_buff.
185  ///
186  /// # Safety
187  ///
188  /// The pointer which pointed the memory already initialized must be valid.
189  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
190  #[no_mangle]
StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool191  pub unsafe extern "C" fn StreamBufferWriteChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
192      info!(LOG_LABEL, "enter StreamBufferWriteChar");
193      if let Some(obj) = StreamBuffer::as_mut(object) {
194          obj.write_char_usize(buf, size)
195      } else {
196          false
197      }
198  }
199  /// Check whether the condition of writing could be satisfied or not.
200  ///
201  /// # Safety
202  ///
203  /// The pointer which pointed the memory already initialized must be valid.
204  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
205  #[no_mangle]
StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool206  pub unsafe extern "C" fn StreamBufferCheckWrite(object: *mut StreamBuffer, size: usize) -> bool {
207      info!(LOG_LABEL, "enter StreamBufferCheckWrite");
208      if let Some(obj) = StreamBuffer::as_mut(object) {
209          obj.check_write(size)
210      } else {
211          false
212      }
213  }
214  /// CircleStreamBuffer will copy data to beginning.
215  ///
216  /// # Safety
217  ///
218  /// The pointer which pointed the memory already initialized must be valid.
219  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
220  #[no_mangle]
CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32221  pub unsafe extern "C" fn CircleStreamBufferCopyDataToBegin(object: *mut StreamBuffer) -> i32 {
222      info!(LOG_LABEL, "enter CircleStreamBufferCopyDataToBegin");
223      if let Some(obj) = StreamBuffer::as_mut(object) {
224          obj.copy_data_to_begin();
225          BufferStatusCode::Ok.into()
226      } else {
227          BufferStatusCode::CopyDataToBeginFail.into()
228      }
229  }
230  /// Read sz_buf to buf.
231  ///
232  /// # Safety
233  ///
234  /// The pointer which pointed the memory already initialized must be valid.
235  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
236  #[no_mangle]
StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool237  pub unsafe extern "C" fn StreamBufferReadChar(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
238      info!(LOG_LABEL, "enter StreamBufferReadChar");
239      if let Some(obj) = StreamBuffer::as_mut(object) {
240          obj.read_char_usize(buf, size)
241      } else {
242          false
243      }
244  }
245  /// Write sz_buf to buf.
246  ///
247  /// # Safety
248  ///
249  /// The pointer which pointed the memory already initialized must be valid.
250  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
251  #[no_mangle]
CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool252  pub unsafe extern "C" fn CircleStreamBufferWrite(object: *mut StreamBuffer, buf: *const c_char, size: usize) -> bool {
253      info!(LOG_LABEL, "enter CircleStreamBufferWrite");
254      if let Some(obj) = StreamBuffer::as_mut(object) {
255          obj.circle_write(buf, size)
256      } else {
257          false
258      }
259  }
260  /// read packets on client.
261  ///
262  /// # Safety
263  ///
264  /// The pointer which pointed the memory already initialized must be valid.
265  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
266  #[no_mangle]
ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient, callback_fun: ClientPacketCallBackFun) -> i32267  pub unsafe extern "C" fn ReadClientPackets(object: *mut StreamBuffer, stream_client: *const CSensorServiceClient,
268      callback_fun: ClientPacketCallBackFun) -> i32 {
269      info!(LOG_LABEL,"enter ReadClientPackets");
270      if let Some(obj) = StreamBuffer::as_mut(object) {
271          obj.read_client_packets(stream_client, callback_fun);
272          BufferStatusCode::Ok.into()
273      } else {
274          BufferStatusCode::ReadClientPacketsFail.into()
275      }
276  }
277  /// StreamBufferReadBuf.
278  ///
279  /// # Safety
280  ///
281  /// The pointer which pointed the memory already initialized must be valid.
282  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
283  #[no_mangle]
StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char284  pub unsafe extern "C" fn StreamBufferReadBuf(object: *const StreamBuffer) -> *const c_char {
285      if let Some(obj) = StreamBuffer::as_ref(object) {
286          obj.read_buf()
287      } else {
288          std::ptr::null()
289      }
290  }
291  /// obtain streambuffer's r_count field.
292  ///
293  /// # Safety
294  ///
295  /// The pointer which pointed the memory already initialized must be valid.
296  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
297  #[no_mangle]
StreamBufferGetRcount(object: *const StreamBuffer) -> i32298  pub unsafe extern "C" fn StreamBufferGetRcount(object: *const StreamBuffer) -> i32 {
299      if let Some(obj) = StreamBuffer::as_ref(object) {
300          obj.r_count() as i32
301      } else {
302          BufferStatusCode::RcountFail.into()
303      }
304  }
305  /// obtain streambuffer's w_count field.
306  ///
307  /// # Safety
308  ///
309  /// The pointer which pointed the memory already initialized must be valid.
310  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
311  #[no_mangle]
StreamBufferGetWcount(object: *const StreamBuffer) -> i32312  pub unsafe extern "C" fn StreamBufferGetWcount(object: *const StreamBuffer) -> i32 {
313      if let Some(obj) = StreamBuffer::as_ref(object) {
314          obj.w_count() as i32
315      } else {
316          BufferStatusCode::WcountFail.into()
317      }
318  }
319  /// obtain streambuffer's w_pos field.
320  ///
321  /// # Safety
322  ///
323  /// The pointer which pointed the memory already initialized must be valid.
324  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
325  #[no_mangle]
StreamBufferGetWpos(object: *const StreamBuffer) -> i32326  pub unsafe extern "C" fn StreamBufferGetWpos(object: *const StreamBuffer) -> i32 {
327      if let Some(obj) = StreamBuffer::as_ref(object) {
328          obj.w_pos() as i32
329      } else {
330          BufferStatusCode::WposFail.into()
331      }
332  }
333  /// obtain streambuffer's r_pos field.
334  ///
335  /// # Safety
336  ///
337  /// The pointer which pointed the memory already initialized must be valid.
338  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
339  #[no_mangle]
StreamBufferGetRpos(object: *const StreamBuffer) -> i32340  pub unsafe extern "C" fn StreamBufferGetRpos(object: *const StreamBuffer) -> i32 {
341      if let Some(obj) = StreamBuffer::as_ref(object) {
342          obj.r_pos() as i32
343      } else {
344          BufferStatusCode::RposFail.into()
345      }
346  }
347  /// obtain streambuffer's sz_buff field.
348  ///
349  /// # Safety
350  ///
351  /// The pointer which pointed the memory already initialized must be valid.
352  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
353  #[no_mangle]
StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char354  pub unsafe extern "C" fn StreamBufferGetSzBuff(object: *const StreamBuffer) -> *const c_char {
355      if let Some(obj) = StreamBuffer::as_ref(object) {
356          obj.sz_buff()
357      } else {
358          std::ptr::null()
359      }
360  }
361  /// obtain streambuffer's rw_err_status field.
362  ///
363  /// # Safety
364  ///
365  /// The pointer which pointed the memory already initialized must be valid.
366  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
367  #[no_mangle]
StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32368  pub unsafe extern "C" fn StreamBufferSetRwErrStatus(object: *mut StreamBuffer, rw_error_status: ErrorStatus) -> i32 {
369      if let Some(obj) = StreamBuffer::as_mut(object) {
370          obj.set_rw_error_status(rw_error_status);
371          BufferStatusCode::Ok.into()
372      } else {
373          BufferStatusCode::SetRwErrStatusFail.into()
374      }
375  }
376  /// set streambuffer's r_pos field.
377  ///
378  /// # Safety
379  ///
380  /// The pointer which pointed the memory already initialized must be valid.
381  /// Makesure the memory shouldn't be dropped while whose pointer is being used.
382  #[no_mangle]
StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32383  pub unsafe extern "C" fn StreamBufferSetRpos(object: *mut StreamBuffer, r_pos: i32) -> i32 {
384      if let Some(obj) = StreamBuffer::as_mut(object) {
385          obj.set_r_pos(r_pos as usize);
386          BufferStatusCode::Ok.into()
387      } else {
388          BufferStatusCode::SetRposFail.into()
389      }
390  }
391  
392