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
, (uint8_t)val
);
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
, (uint8_t)val
);
64 void sbufFill(sbuf_t
*dst
, uint8_t data
, int len
)
66 memset(dst
->ptr
, data
, len
);
70 void sbufWriteData(sbuf_t
*dst
, const void *data
, int len
)
72 memcpy(dst
->ptr
, data
, len
);
76 void sbufWriteString(sbuf_t
*dst
, const char *string
)
78 sbufWriteData(dst
, string
, strlen(string
));
81 void sbufWriteStringWithZeroTerminator(sbuf_t
*dst
, const char *string
)
83 sbufWriteData(dst
, string
, strlen(string
) + 1);
86 uint8_t sbufReadU8(sbuf_t
*src
)
91 uint16_t sbufReadU16(sbuf_t
*src
)
94 ret
= sbufReadU8(src
);
95 ret
|= sbufReadU8(src
) << 8;
99 uint32_t sbufReadU32(sbuf_t
*src
)
102 ret
= sbufReadU8(src
);
103 ret
|= sbufReadU8(src
) << 8;
104 ret
|= sbufReadU8(src
) << 16;
105 ret
|= sbufReadU8(src
) << 24;
109 void sbufReadData(sbuf_t
*src
, void *data
, int len
)
111 memcpy(data
, src
->ptr
, len
);
114 // reader - return bytes remaining in buffer
115 // writer - return available space
116 int sbufBytesRemaining(sbuf_t
*buf
)
118 return buf
->end
- buf
->ptr
;
121 uint8_t* sbufPtr(sbuf_t
*buf
)
126 const uint8_t* sbufConstPtr(const sbuf_t
*buf
)
131 // advance buffer pointer
132 // reader - skip data
133 // writer - commit written data
134 void sbufAdvance(sbuf_t
*buf
, int size
)
139 // modifies streambuf so that written data are prepared for reading
140 void sbufSwitchToReader(sbuf_t
*buf
, uint8_t *base
)