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