2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
24 #include "streambuf.h"
26 sbuf_t
*sbufInit(sbuf_t
*sbuf
, uint8_t *ptr
, uint8_t *end
)
33 void sbufWriteU8(sbuf_t
*dst
, uint8_t val
)
38 void sbufWriteU16(sbuf_t
*dst
, uint16_t val
)
40 sbufWriteU8(dst
, val
>> 0);
41 sbufWriteU8(dst
, val
>> 8);
44 void sbufWriteU32(sbuf_t
*dst
, uint32_t val
)
46 sbufWriteU8(dst
, val
>> 0);
47 sbufWriteU8(dst
, val
>> 8);
48 sbufWriteU8(dst
, val
>> 16);
49 sbufWriteU8(dst
, val
>> 24);
52 void sbufWriteU16BigEndian(sbuf_t
*dst
, uint16_t val
)
54 sbufWriteU8(dst
, val
>> 8);
55 sbufWriteU8(dst
, (uint8_t)val
);
58 void sbufWriteU32BigEndian(sbuf_t
*dst
, uint32_t val
)
60 sbufWriteU8(dst
, val
>> 24);
61 sbufWriteU8(dst
, val
>> 16);
62 sbufWriteU8(dst
, val
>> 8);
63 sbufWriteU8(dst
, (uint8_t)val
);
67 void sbufFill(sbuf_t
*dst
, uint8_t data
, int len
)
69 memset(dst
->ptr
, data
, len
);
73 void sbufWriteData(sbuf_t
*dst
, const void *data
, int len
)
75 memcpy(dst
->ptr
, data
, len
);
79 void sbufWriteString(sbuf_t
*dst
, const char *string
)
81 sbufWriteData(dst
, string
, strlen(string
));
84 void sbufWriteStringWithZeroTerminator(sbuf_t
*dst
, const char *string
)
86 sbufWriteData(dst
, string
, strlen(string
) + 1);
89 uint8_t sbufReadU8(sbuf_t
*src
)
94 uint16_t sbufReadU16(sbuf_t
*src
)
97 ret
= sbufReadU8(src
);
98 ret
|= sbufReadU8(src
) << 8;
102 uint32_t sbufReadU32(sbuf_t
*src
)
105 ret
= sbufReadU8(src
);
106 ret
|= sbufReadU8(src
) << 8;
107 ret
|= sbufReadU8(src
) << 16;
108 ret
|= sbufReadU8(src
) << 24;
112 void sbufReadData(sbuf_t
*src
, void *data
, int len
)
114 memcpy(data
, src
->ptr
, len
);
117 // reader - return bytes remaining in buffer
118 // writer - return available space
119 int sbufBytesRemaining(sbuf_t
*buf
)
121 return buf
->end
- buf
->ptr
;
124 uint8_t* sbufPtr(sbuf_t
*buf
)
129 const uint8_t* sbufConstPtr(const sbuf_t
*buf
)
134 // advance buffer pointer
135 // reader - skip data
136 // writer - commit written data
137 void sbufAdvance(sbuf_t
*buf
, int size
)
142 // modifies streambuf so that written data are prepared for reading
143 void sbufSwitchToReader(sbuf_t
*buf
, uint8_t *base
)