• 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 #ifndef STREAM_BUFFER_H
17 #define STREAM_BUFFER_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "nocopyable.h"
23 #include "securec.h"
24 
25 #include "proto.h"
26 #include "sensors_errors.h"
27 
28 #ifdef OHOS_BUILD_ENABLE_RUST
29 #include "rust_binding.h"
30 #endif // OHOS_BUILD_ENABLE_RUST
31 
32 namespace OHOS {
33 namespace Sensors {
34 class StreamBuffer {
35     static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SENSOR_LOG_DOMAIN, "StreamBuffer" };
36 public:
37     StreamBuffer() = default;
38     explicit StreamBuffer(const StreamBuffer &buf);
39     virtual StreamBuffer &operator=(const StreamBuffer &other);
40     virtual ~StreamBuffer() = default;
41     void Reset();
42     void Clean();
43     bool Read(std::string &buf);
44     bool Read(StreamBuffer &buf);
45     bool Read(char *buf, size_t size);
46     bool Write(const std::string &buf);
47     bool Write(const StreamBuffer &buf);
48     virtual bool Write(const char *buf, size_t size);
49     const std::string& GetErrorStatusRemark() const;
50     bool ChkRWError() const;
51 #ifndef OHOS_BUILD_ENABLE_RUST
52     bool SeekReadPos(size_t n);
53     bool IsEmpty() const;
54     size_t Size() const;
55     size_t UnreadSize() const;
56     size_t GetAvailableBufSize() const;
57     const char* Data() const;
58     const char *WriteBuf() const;
59 #endif // OHOS_BUILD_ENABLE_RUST
60     template<typename T>
61     bool Read(T &data);
62     template<typename T>
63     bool Write(const T &data);
64     template<typename T>
65     bool Read(std::vector<T> &data);
66     template<typename T>
67     bool Write(const std::vector<T> &data);
68     const char *ReadBuf() const;
69 
70     template<typename T>
71     StreamBuffer &operator >> (T &data);
72     template<typename T>
73     StreamBuffer &operator << (const T &data);
74     DISALLOW_MOVE(StreamBuffer);
75 
76 protected:
77     bool Clone(const StreamBuffer &buf);
78 #ifdef OHOS_BUILD_ENABLE_RUST
79 public:
80     std::unique_ptr<RustStreamBuffer, void(*)(RustStreamBuffer*)> streamBufferPtr_ { StreamBufferCreate(),
81         StreamBufferDelete };
82 #else
83     enum class ErrorStatus {
84         ERROR_STATUS_OK,
85         ERROR_STATUS_READ,
86         ERROR_STATUS_WRITE,
87     };
88     ErrorStatus rwErrorStatus_ = ErrorStatus::ERROR_STATUS_OK;
89     size_t rCount_ { 0 };
90     size_t wCount_ { 0 };
91     size_t rPos_ { 0 };
92     size_t wPos_ { 0 };
93     char szBuff_[MAX_STREAM_BUF_SIZE + 1] = {};
94 #endif // OHOS_BUILD_ENABLE_RUST
95 };
96 
97 template<typename T>
Read(T & data)98 bool StreamBuffer::Read(T &data)
99 {
100     if (!Read(reinterpret_cast<char *>(&data), sizeof(data))) {
101 #ifdef OHOS_BUILD_ENABLE_RUST
102         const char* s = StreamBufferGetErrorStatusRemark(streamBufferPtr_.get());
103         SEN_HILOGE("[%{public}s] size:%{public}zu count:%{public}d",
104             s, sizeof(data), StreamBufferGetRcount(streamBufferPtr_.get()) + 1);
105 #else
106         SEN_HILOGE("%{public}s, size:%{public}zu, count:%{public}zu",
107             GetErrorStatusRemark().c_str(), sizeof(data), rCount_ + 1);
108 #endif // OHOS_BUILD_ENABLE_RUST
109         return false;
110     }
111     return true;
112 }
113 
114 template<typename T>
Write(const T & data)115 bool StreamBuffer::Write(const T &data)
116 {
117     if (!Write(reinterpret_cast<const char *>(&data), sizeof(data))) {
118 #ifdef OHOS_BUILD_ENABLE_RUST
119         const char* s = StreamBufferGetErrorStatusRemark(streamBufferPtr_.get());
120         SEN_HILOGE("[%{public}s] size:%{public}zu,count:%{public}d",
121             s, sizeof(data), StreamBufferGetWcount(streamBufferPtr_.get()) + 1);
122 #else
123         SEN_HILOGE("%{public}s, size:%{public}zu, count:%{public}zu",
124             GetErrorStatusRemark().c_str(), sizeof(data), wCount_ + 1);
125 #endif // OHOS_BUILD_ENABLE_RUST
126         return false;
127     }
128     return true;
129 }
130 
131 template<typename T>
Read(std::vector<T> & data)132 bool StreamBuffer::Read(std::vector<T> &data)
133 {
134     size_t size = 0;
135     if (!Read(size)) {
136         SEN_HILOGE("Read vector size failed");
137         return false;
138     }
139     if (size > MAX_VECTOR_SIZE) {
140         SEN_HILOGE("Vector size is invalid, size:%{public}zu", size);
141         return false;
142     }
143     for (size_t i = 0; i < size; i++) {
144         T val;
145         if (!Read(val)) {
146             SEN_HILOGE("Read vector data failed");
147             return false;
148         }
149         data.push_back(val);
150     }
151     return true;
152 }
153 
154 template<typename T>
Write(const std::vector<T> & data)155 bool StreamBuffer::Write(const std::vector<T> &data)
156 {
157     if (data.size() > INT32_MAX) {
158         SEN_HILOGE("Vector exceeds the max range");
159         return false;
160     }
161     size_t size = data.size();
162     if (!Write(size)) {
163         SEN_HILOGE("Write vector size failed");
164         return false;
165     }
166     for (const auto &item : data) {
167         if (!Write(item)) {
168             SEN_HILOGE("Write vector data failed");
169             return false;
170         }
171     }
172     return true;
173 }
174 
175 template<typename T>
176 StreamBuffer &StreamBuffer::operator>>(T &data)
177 {
178     if (!Read(data)) {
179         SEN_HILOGW("Read data failed");
180     }
181     return *this;
182 }
183 
184 template<typename T>
185 StreamBuffer &StreamBuffer::operator<<(const T &data)
186 {
187     if (!Write(data)) {
188         SEN_HILOGW("Write data failed");
189     }
190     return *this;
191 }
192 }  // namespace Sensors
193 }  // namespace OHOS
194 #endif  // STREAM_BUFFER_H