• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "circle_stream_buffer.h"
17 
18 namespace OHOS {
19 namespace Sensors {
CheckWrite(size_t size)20 bool CircleStreamBuffer::CheckWrite(size_t size)
21 {
22 #ifdef OHOS_BUILD_ENABLE_RUST
23     return StreamBufferCheckWrite(streamBufferPtr_.get(), size);
24 #else
25     size_t availSize = GetAvailableBufSize();
26     if (size > availSize && rPos_ > 0) {
27         CopyDataToBegin();
28         availSize = GetAvailableBufSize();
29     }
30     return (availSize >= size);
31 #endif // OHOS_BUILD_ENABLE_RUST
32 }
33 
Write(const char * buf,size_t size)34 bool CircleStreamBuffer::Write(const char *buf, size_t size)
35 {
36 #ifdef OHOS_BUILD_ENABLE_RUST
37     return CircleStreamBufferWrite(streamBufferPtr_.get(), buf, size);
38 #else
39     if (!CheckWrite(size)) {
40         SEN_HILOGE("Buffer is overflow, availableSize:%{public}zu, size:%{public}zu,"
41                    "unreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu",
42                    GetAvailableBufSize(), size, UnreadSize(), rPos_, wPos_);
43         return false;
44     }
45     return StreamBuffer::Write(buf, size);
46 #endif // OHOS_BUILD_ENABLE_RUST
47 }
48 
CopyDataToBegin()49 void CircleStreamBuffer::CopyDataToBegin()
50 {
51 #ifdef OHOS_BUILD_ENABLE_RUST
52     CircleStreamBufferCopyDataToBegin(streamBufferPtr_.get());
53 #else
54     size_t unreadSize = UnreadSize();
55     if (unreadSize > 0 && rPos_ > 0) {
56         size_t pos = 0;
57         for (size_t i = rPos_; i <= wPos_;) {
58             szBuff_[pos++] = szBuff_[i++];
59         }
60     }
61     SEN_HILOGD("UnreadSize:%{public}zu, rPos:%{public}zu, wPos:%{public}zu", unreadSize, rPos_, wPos_);
62     rPos_ = 0;
63     wPos_ = unreadSize;
64 #endif // OHOS_BUILD_ENABLE_RUST
65 }
66 } // namespace Sensors
67 } // namespace OHOS