• 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 "stream_buffer.h"
17 
18 namespace OHOS {
19 namespace Sensors {
StreamBuffer(const StreamBuffer & buf)20 StreamBuffer::StreamBuffer(const StreamBuffer &buf)
21 {
22     Clone(buf);
23 }
24 
operator =(const StreamBuffer & other)25 StreamBuffer &StreamBuffer::operator=(const StreamBuffer &other)
26 {
27     if (this != &other) {
28         Clone(other);
29     }
30     return *this;
31 }
32 
Reset()33 void StreamBuffer::Reset()
34 {
35 #ifdef OHOS_BUILD_ENABLE_RUST
36     StreamBufferReset(streamBufferPtr_.get());
37 #else
38     rPos_ = 0;
39     wPos_ = 0;
40     rCount_ = 0;
41     wCount_ = 0;
42     rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK;
43 #endif // OHOS_BUILD_ENABLE_RUST
44 }
45 
Clean()46 void StreamBuffer::Clean()
47 {
48 #ifdef OHOS_BUILD_ENABLE_RUST
49     StreamBufferClean(streamBufferPtr_.get());
50 #else
51     Reset();
52     errno_t ret = memset_sp(&szBuff_, sizeof(szBuff_), 0, sizeof(szBuff_));
53     if (ret != EOK) {
54         SEN_HILOGE("Call memset_s fail");
55         return;
56     }
57 #endif // OHOS_BUILD_ENABLE_RUST
58 }
59 
Read(std::string & buf)60 bool StreamBuffer::Read(std::string &buf)
61 {
62 #ifdef OHOS_BUILD_ENABLE_RUST
63     const int32_t ERROR_STATUS_READ = 1;
64     if (StreamBufferGetRpos(streamBufferPtr_.get()) == StreamBufferGetWpos(streamBufferPtr_.get())) {
65         SEN_HILOGE("Not enough memory to read, errCode:%{public}d", STREAM_BUF_READ_FAIL);
66         StreamBufferSetRwErrStatus(streamBufferPtr_.get(), ERROR_STATUS_READ);
67         return false;
68     }
69     buf = ReadBuf();
70     StreamBufferSetRpos(streamBufferPtr_.get(),
71         StreamBufferGetRpos(streamBufferPtr_.get()) + static_cast<int32_t>(buf.length()) + 1);
72     return (buf.length() > 0);
73 #else
74     if (rPos_ == wPos_) {
75         SEN_HILOGE("Not enough memory to read");
76         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ;
77         return false;
78     }
79     buf = ReadBuf();
80     rPos_ += buf.length() + 1;
81     return (buf.length() > 0);
82 #endif // OHOS_BUILD_ENABLE_RUST
83 }
84 
Write(const std::string & buf)85 bool StreamBuffer::Write(const std::string &buf)
86 {
87     return Write(buf.c_str(), buf.length()+1);
88 }
89 
Read(StreamBuffer & buf)90 bool StreamBuffer::Read(StreamBuffer &buf)
91 {
92 #ifdef OHOS_BUILD_ENABLE_RUST
93     return StreamBufferRead(streamBufferPtr_.get(), buf.streamBufferPtr_.get());
94 #else
95     return buf.Write(Data(), Size());
96 #endif // OHOS_BUILD_ENABLE_RUST
97 }
98 
Write(const StreamBuffer & buf)99 bool StreamBuffer::Write(const StreamBuffer &buf)
100 {
101 #ifdef OHOS_BUILD_ENABLE_RUST
102     return StreamBufferWrite(streamBufferPtr_.get(), buf.streamBufferPtr_.get());
103 #else
104     return Write(buf.Data(), buf.Size());
105 #endif // OHOS_BUILD_ENABLE_RUST
106 }
107 
Read(char * buf,size_t size)108 bool StreamBuffer::Read(char *buf, size_t size)
109 {
110 #ifdef OHOS_BUILD_ENABLE_RUST
111     return StreamBufferReadChar(streamBufferPtr_.get(), buf, size);
112 #else
113     if (ChkRWError()) {
114         return false;
115     }
116     if (buf == nullptr) {
117         SEN_HILOGE("Invalid input parameter, buf is nullptr");
118         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ;
119         return false;
120     }
121     if (size == 0) {
122         SEN_HILOGE("Invalid input parameter, size:%{public}zu", size);
123         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ;
124         return false;
125     }
126     if (rPos_ + size > wPos_) {
127         SEN_HILOGE("Memory out of bounds on read");
128         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ;
129         return false;
130     }
131     errno_t ret = memcpy_sp(buf, size, ReadBuf(), size);
132     if (ret != EOK) {
133         SEN_HILOGE("Failed to call memcpy_sp");
134         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_READ;
135         return false;
136     }
137     rPos_ += size;
138     ++rCount_;
139     return true;
140 #endif // OHOS_BUILD_ENABLE_RUST
141 }
142 
Write(const char * buf,size_t size)143 bool StreamBuffer::Write(const char *buf, size_t size)
144 {
145 #ifdef OHOS_BUILD_ENABLE_RUST
146     return StreamBufferWriteChar(streamBufferPtr_.get(), buf, size);
147 #else
148     if (ChkRWError()) {
149         return false;
150     }
151     if (buf == nullptr) {
152         SEN_HILOGE("Invalid input parameter, buf is nullptr");
153         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE;
154         return false;
155     }
156     if (size == 0) {
157         SEN_HILOGE("Invalid input parameter, size:%{public}zu", size);
158         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE;
159         return false;
160     }
161     if (wPos_ + size > MAX_STREAM_BUF_SIZE) {
162         SEN_HILOGE("The write length exceeds buffer. wPos:%{public}zu, size:%{public}zu, maxBufSize:%{public}zu",
163                    wPos_, size, MAX_STREAM_BUF_SIZE);
164         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE;
165         return false;
166     }
167     errno_t ret = memcpy_sp(&szBuff_[wPos_], GetAvailableBufSize(), buf, size);
168     if (ret != EOK) {
169         SEN_HILOGE("Failed to call memcpy_sp");
170         rwErrorStatus_ = ErrorStatus::ERROR_STATUS_WRITE;
171         return false;
172     }
173     wPos_ += size;
174     ++wCount_;
175     return true;
176 #endif // OHOS_BUILD_ENABLE_RUST
177 }
178 
ReadBuf() const179 const char *StreamBuffer::ReadBuf() const
180 {
181 #ifdef OHOS_BUILD_ENABLE_RUST
182     return StreamBufferReadBuf(streamBufferPtr_.get());
183 #else
184     return &szBuff_[rPos_];
185 #endif // OHOS_BUILD_ENABLE_RUST
186 }
187 
Clone(const StreamBuffer & buf)188 bool StreamBuffer::Clone(const StreamBuffer &buf)
189 {
190     Clean();
191 #ifdef OHOS_BUILD_ENABLE_RUST
192     return Write(StreamBufferData(buf.streamBufferPtr_.get()), StreamBufferSize(buf.streamBufferPtr_.get()));
193 #else
194     return Write(buf.Data(), buf.Size());
195 #endif // OHOS_BUILD_ENABLE_RUST
196 }
197 #ifndef OHOS_BUILD_ENABLE_RUST
198 
ChkRWError() const199 bool StreamBuffer::ChkRWError() const
200 {
201     return (rwErrorStatus_ != ErrorStatus::ERROR_STATUS_OK);
202 }
203 
SeekReadPos(size_t n)204 bool StreamBuffer::SeekReadPos(size_t n)
205 {
206     size_t pos = rPos_ + n;
207     if (pos > wPos_) {
208         SEN_HILOGE("The position in the calculation is not as expected. pos:%{public}zu, [0, %{public}zu]", pos, wPos_);
209         return false;
210     }
211     rPos_ = pos;
212     return true;
213 }
214 
IsEmpty() const215 bool StreamBuffer::IsEmpty() const
216 {
217     return (rPos_ == wPos_);
218 }
219 
Size() const220 size_t StreamBuffer::Size() const
221 {
222 #ifdef OHOS_BUILD_ENABLE_RUST
223     return StreamBufferSize(&rustStreamBuffer_);
224 #else
225     return wPos_;
226 #endif // OHOS_BUILD_ENABLE_RUST
227 }
228 
UnreadSize() const229 size_t StreamBuffer::UnreadSize() const
230 {
231     return ((wPos_ <= rPos_) ? 0 : (wPos_ - rPos_));
232 }
233 
GetAvailableBufSize() const234 size_t StreamBuffer::GetAvailableBufSize() const
235 {
236     return ((wPos_ >= MAX_STREAM_BUF_SIZE) ? 0 : (MAX_STREAM_BUF_SIZE - wPos_));
237 }
238 
GetErrorStatusRemark() const239 const std::string &StreamBuffer::GetErrorStatusRemark() const
240 {
241 #ifdef OHOS_BUILD_ENABLE_RUST
242     return StreamBufferGetErrorStatusRemark(streamBufferPtr_.get());
243 #else
244     static const std::vector<std::pair<ErrorStatus, std::string>> remark {
245         {ErrorStatus::ERROR_STATUS_OK, "OK"},
246         {ErrorStatus::ERROR_STATUS_READ, "READ_ERROR"},
247         {ErrorStatus::ERROR_STATUS_WRITE, "WRITE_ERROR"},
248     };
249     static const std::string invalidStatus { "UNKNOWN" };
250     auto tIter = std::find_if(remark.cbegin(), remark.cend(), [this](const auto &item) {
251         return (item.first == rwErrorStatus_);
252     });
253     return (tIter != remark.cend() ? tIter->second : invalidStatus);
254 #endif // OHOS_BUILD_ENABLE_RUST
255 }
256 
Data() const257 const char *StreamBuffer::Data() const
258 {
259 #ifdef OHOS_BUILD_ENABLE_RUST
260     return StreamBufferData(&rustStreamBuffer_);
261 #else
262     return &szBuff_[0];
263 #endif // OHOS_BUILD_ENABLE_RUST
264 }
265 
WriteBuf() const266 const char *StreamBuffer::WriteBuf() const
267 {
268     return &szBuff_[wPos_];
269 }
270 #endif // OHOS_BUILD_ENABLE_RUST
271 } // namespace Sensors
272 } // namespace OHOS
273