1414
1515// / \brief A Serial Line IP (SLIP) Encoder.
1616// /
17- // / Serial Line IP (SLIP) is a packet framing protocol: SLIP defines a
18- // / sequence of characters that frame IP packets on a serial line and
19- // / nothing more. It provides no addressing, packet type identification,
20- // / error detection/correction or compression mechanisms. Because the
21- // / protocol does so little, though, it is usually very easy to
22- // / implement.
17+ // / Serial Line IP (SLIP) is a packet framing protocol: SLIP defines a sequence
18+ // / of characters that frame IP packets on a serial line and nothing more. It
19+ // / provides no addressing, packet type identification, error detection,
20+ // / correction or compression mechanisms. Because the protocol does so little
21+ // / its implementation is trivial and fast.
2322// /
2423// / \sa http://tools.ietf.org/html/rfc1055
2524class SLIP
2625{
2726public:
28- // / \brief Encode the given buffer.
29- // / \param buffer A pointer to the buffer to be encoded.
30- // / \param size The size of the buffer pointed to by \p buffer.
31- // / \param encoded A pointer to the target encoded buffer. This buffer must have sufficient memory allocated.
32- // / \returns The number of bytes written to the encoded \p buffer.
33- // / \warning The encoded buffer must have at least getEncodedBufferSize() allocated.
34- static size_t encode (const uint8_t * buffer, size_t size, uint8_t * encoded)
27+ // / \brief Encode a byte buffer with the SLIP encoder.
28+ // / \param buffer A pointer to the unencoded buffer to encode.
29+ // / \param size The number of bytes in the \p buffer.
30+ // / \param encodedBuffer The buffer for the encoded bytes.
31+ // / \returns The number of bytes written to the \p encodedBuffer.
32+ // / \warning The encodedBuffer must have at least getEncodedBufferSize() allocated.
33+ static size_t encode (const uint8_t * buffer,
34+ size_t size,
35+ uint8_t * encodedBuffer)
3536 {
3637 if (size == 0 )
3738 return 0 ;
@@ -40,38 +41,40 @@ class SLIP
4041 size_t write_index = 0 ;
4142
4243 // double-ENDed, flush any data that may have accumulated due to line noise
43- encoded [write_index++] = END;
44+ encodedBuffer [write_index++] = END;
4445
4546 while (read_index < size)
4647 {
4748 if (buffer[read_index] == END)
4849 {
49- encoded [write_index++] = ESC;
50- encoded [write_index++] = ESC_END;
50+ encodedBuffer [write_index++] = ESC;
51+ encodedBuffer [write_index++] = ESC_END;
5152 read_index++;
5253 }
5354 else if (buffer[read_index] == ESC)
5455 {
55- encoded [write_index++] = ESC;
56- encoded [write_index++] = ESC_ESC;
56+ encodedBuffer [write_index++] = ESC;
57+ encodedBuffer [write_index++] = ESC_ESC;
5758 read_index++;
5859 }
5960 else
6061 {
61- encoded [write_index++] = buffer[read_index++];
62+ encodedBuffer [write_index++] = buffer[read_index++];
6263 }
6364 }
6465
6566 return write_index;
6667 }
6768
6869 // / \brief Decode a SLIP-encoded buffer.
69- // / \param source The SLIP-encoded buffer to decode.
70- // / \param size The size of the SLIP-encoded buffer.
71- // / \param destination The target buffer for the decoded bytes.
72- // / \returns The number of bytes in the decoded buffer.
73- // / \warning destination must have a minimum capacity of size.
74- static size_t decode (const uint8_t * buffer, size_t size, uint8_t * decoded)
70+ // / \param encodedBuffer A pointer to the \p encodedBuffer to decode.
71+ // / \param size The number of bytes in the \p encodedBuffer.
72+ // / \param decodedBuffer The target buffer for the decoded bytes.
73+ // / \returns The number of bytes written to the \p decodedBuffer.
74+ // / \warning decodedBuffer must have a minimum capacity of size.
75+ static size_t decode (const uint8_t * encodedBuffer,
76+ size_t size,
77+ uint8_t * decodedBuffer)
7578 {
7679 if (size == 0 )
7780 return 0 ;
@@ -81,21 +84,21 @@ class SLIP
8184
8285 while (read_index < size)
8386 {
84- if (buffer [read_index] == END)
87+ if (encodedBuffer [read_index] == END)
8588 {
8689 // flush or done
8790 read_index++;
8891 }
89- else if (buffer [read_index] == ESC)
92+ else if (encodedBuffer [read_index] == ESC)
9093 {
91- if (buffer [read_index+1 ] == ESC_END)
94+ if (encodedBuffer [read_index+1 ] == ESC_END)
9295 {
93- decoded [write_index++] = END;
96+ decodedBuffer [write_index++] = END;
9497 read_index += 2 ;
9598 }
96- else if (buffer [read_index+1 ] == ESC_ESC)
99+ else if (encodedBuffer [read_index+1 ] == ESC_ESC)
97100 {
98- decoded [write_index++] = ESC;
101+ decodedBuffer [write_index++] = ESC;
99102 read_index += 2 ;
100103 }
101104 else
@@ -105,24 +108,25 @@ class SLIP
105108 }
106109 else
107110 {
108- decoded [write_index++] = buffer [read_index++];
111+ decodedBuffer [write_index++] = encodedBuffer [read_index++];
109112 }
110113 }
111114
112115 return write_index;
113116 }
114117
115- // / \brief Get the maximum encoded buffer size needed for a given source size.
118+ // / \brief Get the maximum encoded buffer size needed for a given unencoded buffer size.
116119 // /
117- // / SLIP has a start and a end markers (192 and 219).
118- // / Marker value is replaced by 2 bytes in the encoded buffer.
119- // / So in the worst case of sending a buffer with only '192' or '219',
120- // / the encoded buffer length will be 2 * buffer.size() + 2
121- // / \param sourceSize The size of the buffer to be encoded.
120+ // / SLIP has a start and a end markers (192 and 219). Marker value is
121+ // / replaced by 2 bytes in the encoded buffer. So in the worst case of
122+ // / sending a buffer with only '192' or '219', the encoded buffer length
123+ // / will be 2 * buffer.size() + 2
124+ // /
125+ // / \param unencodedBufferSize The size of the buffer to be encoded.
122126 // / \returns the maximum size of the required encoded buffer.
123- static size_t getEncodedBufferSize (size_t sourceSize )
127+ static size_t getEncodedBufferSize (size_t unencodedBufferSize )
124128 {
125- return sourceSize * 2 + 2 ;
129+ return unencodedBufferSize * 2 + 2 ;
126130 }
127131
128132 // / \brief Key constants used in the SLIP protocol.
0 commit comments