2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
21 #include "streambuf.h"
23 sbuf_t
*sbufInit(sbuf_t
*sbuf
, uint8_t *ptr
, uint8_t *end
)
30 void sbufWriteU8(sbuf_t
*dst
, uint8_t val
)
35 void sbufWriteU16(sbuf_t
*dst
, uint16_t val
)
37 sbufWriteU8(dst
, val
>> 0);
38 sbufWriteU8(dst
, val
>> 8);
41 void sbufWriteU32(sbuf_t
*dst
, uint32_t val
)
43 sbufWriteU8(dst
, val
>> 0);
44 sbufWriteU8(dst
, val
>> 8);
45 sbufWriteU8(dst
, val
>> 16);
46 sbufWriteU8(dst
, val
>> 24);
49 void sbufWriteU16BigEndian(sbuf_t
*dst
, uint16_t val
)
51 sbufWriteU8(dst
, val
>> 8);
52 sbufWriteU8(dst
, val
>> 0);
55 void sbufWriteU32BigEndian(sbuf_t
*dst
, uint32_t val
)
57 sbufWriteU8(dst
, val
>> 24);
58 sbufWriteU8(dst
, val
>> 16);
59 sbufWriteU8(dst
, val
>> 8);
60 sbufWriteU8(dst
, val
>> 0);
63 void sbufFill(sbuf_t
*dst
, uint8_t data
, int len
)
65 memset(dst
->ptr
, data
, len
);
69 void sbufWriteData(sbuf_t
*dst
, const void *data
, int len
)
71 memcpy(dst
->ptr
, data
, len
);
75 bool sbufWriteDataSafe(sbuf_t
*dst
, const void *data
, int len
)
77 // only write if data does not overflow buffer
78 if (sbufBytesRemaining(dst
) >= len
) {
79 memcpy(dst
->ptr
, data
, len
);
86 void sbufWriteString(sbuf_t
*dst
, const char *string
)
88 sbufWriteData(dst
, string
, strlen(string
));
91 void sbufWriteStringWithZeroTerminator(sbuf_t
*dst
, const char *string
)
93 sbufWriteData(dst
, string
, strlen(string
) + 1);
96 uint8_t sbufReadU8(sbuf_t
*src
)
101 uint16_t sbufReadU16(sbuf_t
*src
)
104 ret
= sbufReadU8(src
);
105 ret
|= sbufReadU8(src
) << 8;
109 uint32_t sbufReadU32(sbuf_t
*src
)
112 ret
= sbufReadU8(src
);
113 ret
|= sbufReadU8(src
) << 8;
114 ret
|= sbufReadU8(src
) << 16;
115 ret
|= sbufReadU8(src
) << 24;
119 void sbufReadData(const sbuf_t
*src
, void *data
, int len
)
121 memcpy(data
, src
->ptr
, len
);
124 bool sbufReadU8Safe(uint8_t *dst
, sbuf_t
*src
)
126 if (sbufBytesRemaining(src
)) {
127 const uint8_t value
= sbufReadU8(src
);
136 bool sbufReadI8Safe(int8_t *dst
, sbuf_t
*src
)
138 return sbufReadU8Safe((uint8_t*)dst
, src
);
141 bool sbufReadU16Safe(uint16_t *dst
, sbuf_t
*src
)
143 if (sbufBytesRemaining(src
) >= 2) { // check there are enough bytes left in the buffer to read a uint16_t
144 const uint16_t value
= sbufReadU16(src
);
153 bool sbufReadI16Safe(int16_t *dst
, sbuf_t
*src
)
155 return sbufReadU16Safe((uint16_t*)dst
, src
);
158 bool sbufReadU32Safe(uint32_t *dst
, sbuf_t
*src
)
160 if (sbufBytesRemaining(src
) >= 4) { // check there are enough bytes left in the buffer to read a uint32_t
161 const uint32_t value
= sbufReadU32(src
);
170 bool sbufReadI32Safe(int32_t *dst
, sbuf_t
*src
)
172 return sbufReadU32Safe((uint32_t*)dst
, src
);
175 bool sbufReadDataSafe(const sbuf_t
*src
, void *data
, int len
)
177 if (sbufBytesRemaining(src
) >= len
) {
178 sbufReadData(src
, data
, len
);
184 // reader - return bytes remaining in buffer
185 // writer - return available space
186 int sbufBytesRemaining(const sbuf_t
*buf
)
188 return buf
->end
- buf
->ptr
;
191 uint8_t* sbufPtr(sbuf_t
*buf
)
196 const uint8_t* sbufConstPtr(const sbuf_t
*buf
)
201 // advance buffer pointer
202 // reader - skip data
203 // writer - commit written data
204 void sbufAdvance(sbuf_t
*buf
, int size
)
209 // modifies streambuf so that written data are prepared for reading
210 void sbufSwitchToReader(sbuf_t
*buf
, uint8_t *base
)