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/>.
26 #include "streambuf.h"
28 sbuf_t
*sbufInit(sbuf_t
*sbuf
, uint8_t *ptr
, uint8_t *end
)
35 void sbufWriteU8(sbuf_t
*dst
, uint8_t val
)
40 void sbufWriteU16(sbuf_t
*dst
, uint16_t val
)
42 sbufWriteU8(dst
, val
>> 0);
43 sbufWriteU8(dst
, val
>> 8);
46 void sbufWriteU32(sbuf_t
*dst
, uint32_t val
)
48 sbufWriteU8(dst
, val
>> 0);
49 sbufWriteU8(dst
, val
>> 8);
50 sbufWriteU8(dst
, val
>> 16);
51 sbufWriteU8(dst
, val
>> 24);
54 void sbufWriteU16BigEndian(sbuf_t
*dst
, uint16_t val
)
56 sbufWriteU8(dst
, val
>> 8);
57 sbufWriteU8(dst
, (uint8_t)val
);
60 void sbufWriteU32BigEndian(sbuf_t
*dst
, uint32_t val
)
62 sbufWriteU8(dst
, val
>> 24);
63 sbufWriteU8(dst
, val
>> 16);
64 sbufWriteU8(dst
, val
>> 8);
65 sbufWriteU8(dst
, (uint8_t)val
);
69 void sbufFill(sbuf_t
*dst
, uint8_t data
, int len
)
71 memset(dst
->ptr
, data
, len
);
75 void sbufWriteData(sbuf_t
*dst
, const void *data
, int len
)
77 memcpy(dst
->ptr
, data
, len
);
81 void sbufWriteString(sbuf_t
*dst
, const char *string
)
83 sbufWriteData(dst
, string
, strlen(string
));
86 void sbufWriteStringWithZeroTerminator(sbuf_t
*dst
, const char *string
)
88 sbufWriteData(dst
, string
, strlen(string
) + 1);
91 uint8_t sbufReadU8(sbuf_t
*src
)
96 uint16_t sbufReadU16(sbuf_t
*src
)
99 ret
= sbufReadU8(src
);
100 ret
|= sbufReadU8(src
) << 8;
104 uint32_t sbufReadU32(sbuf_t
*src
)
107 ret
= sbufReadU8(src
);
108 ret
|= sbufReadU8(src
) << 8;
109 ret
|= sbufReadU8(src
) << 16;
110 ret
|= sbufReadU8(src
) << 24;
114 void sbufReadData(sbuf_t
*src
, void *data
, int len
)
116 memcpy(data
, src
->ptr
, len
);
119 // reader - return bytes remaining in buffer
120 // writer - return available space
121 int sbufBytesRemaining(sbuf_t
*buf
)
123 return buf
->end
- buf
->ptr
;
126 uint8_t* sbufPtr(sbuf_t
*buf
)
131 const uint8_t* sbufConstPtr(const sbuf_t
*buf
)
136 // advance buffer pointer
137 // reader - skip data
138 // writer - commit written data
139 void sbufAdvance(sbuf_t
*buf
, int size
)
144 // modifies streambuf so that written data are prepared for reading
145 void sbufSwitchToReader(sbuf_t
*buf
, uint8_t *base
)