COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
Collaboration diagram for COSMOS SLIP support functions:

Functions

int32_t slip_check_crc (uint8_t *sbuf, uint16_t ssize)
 Check SLIP packet CRC. More...
 
int32_t slip_check_crc (slip_t &sbuf)
 
int32_t slip_unpack (uint8_t *sbuf, uint16_t ssize, uint8_t *rbuf, uint16_t rsize)
 Unpack SLIP packet. More...
 
int32_t slip_unpack (slip_t &sbuf, slip_t &rbuf)
 
int32_t slip_decode (uint8_t *sbuf, uint16_t ssize, uint8_t *rbuf, uint16_t rsize)
 Decode SLIP packet. More...
 
int32_t slip_decode (slip_t &sbuf, slip_t &rbuf)
 
int32_t slip_encode (uint8_t *rbuf, uint16_t rsize, uint8_t *sbuf, uint16_t ssize)
 Encode data in to SLIP packet. More...
 
int32_t slip_encode (slip_t &rbuf, slip_t &sbuf)
 
int32_t slip_pack (uint8_t *rbuf, uint16_t rsize, uint8_t *sbuf, uint16_t ssize)
 Pack data in to SLIP packet. More...
 
int32_t slip_pack (slip_t &rbuf, slip_t &sbuf)
 
uint16_t slip_calc_crc (uint8_t *buf, uint16_t size)
 Calculate CRC-16-CCITT. More...
 
uint16_t slip_calc_crc (slip_t &buf)
 
uint16_t slip_get_crc (uint8_t *buf, uint16_t index)
 Get CRC from SLIP buffer. More...
 
uint16_t slip_get_crc (slip_t &buf)
 
uint16_t slip_set_crc (uint8_t *buf, uint16_t index)
 Set CRC for SLIP buffer. More...
 
uint16_t slip_set_crc (slip_t &buf)
 

Detailed Description

Function Documentation

int32_t slip_check_crc ( uint8_t *  sbuf,
uint16_t  ssize 
)

Check SLIP packet CRC.

Get the CRC from the end of a SLIP packet and check it against the calculated CRC.

Parameters
sbufPointer to buffer containing SLIP encoded data plus CRC.
ssizeTotal size of buffer, including CRC.
Returns
0 if CRC checks out, otherwise SLIP_ERROR_CRC.
48  {
49  uint16_t crc, crc2;
50 
51  crc = slip_get_crc(sbuf,ssize-2);
52  crc2 = slip_calc_crc(sbuf,ssize-2);
53  if (crc != crc2)
54  return (SLIP_ERROR_CRC);
55  return 0;
56  }
png_uint_32 crc
Definition: png.c:2173
uint16_t slip_get_crc(uint8_t *buf, uint16_t index)
Get CRC from SLIP buffer.
Definition: sliplib.cpp:364
#define SLIP_ERROR_CRC
Definition: cosmos-errno.h:100
uint16_t slip_calc_crc(uint8_t *buf, uint16_t size)
Calculate CRC-16-CCITT.
Definition: sliplib.cpp:322
int32_t slip_check_crc ( slip_t sbuf)
59  {
60  uint16_t crc, crc2;
61 
62  crc = slip_get_crc(sbuf);
63  crc2 = slip_calc_crc(sbuf);
64  if (crc != crc2)
65  return (SLIP_ERROR_CRC);
66  return 0;
67  }
png_uint_32 crc
Definition: png.c:2173
uint16_t slip_get_crc(uint8_t *buf, uint16_t index)
Get CRC from SLIP buffer.
Definition: sliplib.cpp:364
#define SLIP_ERROR_CRC
Definition: cosmos-errno.h:100
uint16_t slip_calc_crc(uint8_t *buf, uint16_t size)
Calculate CRC-16-CCITT.
Definition: sliplib.cpp:322
int32_t slip_unpack ( uint8_t *  sbuf,
uint16_t  ssize,
uint8_t *  rbuf,
uint16_t  rsize 
)

Unpack SLIP packet.

Convert SLIP data in one buffer to raw ASCII data in second buffer and return length.

Parameters
sbufcharacter buffer to read SLIP data from
ssizesize of SLIP buffer
rbufcharacter buffer to store raw data
rsizesize of raw buffer, including terminal 0
Returns
bytes in raw buffer
80 {
81  if (slip_check_crc(sbuf, ssize))
82  {
83  return (SLIP_ERROR_CRC);
84  }
85 
86  return (slip_decode(sbuf, ssize-2, rbuf, rsize));
87 }
int32_t slip_decode(uint8_t *sbuf, uint16_t ssize, uint8_t *rbuf, uint16_t rsize)
Decode SLIP packet.
Definition: sliplib.cpp:109
#define SLIP_ERROR_CRC
Definition: cosmos-errno.h:100
int32_t slip_check_crc(uint8_t *sbuf, uint16_t ssize)
Check SLIP packet CRC.
Definition: sliplib.cpp:47
int32_t slip_unpack ( slip_t sbuf,
slip_t rbuf 
)
90 {
91  if (slip_check_crc(sbuf))
92  {
93  return (SLIP_ERROR_CRC);
94  }
95 
96  return (slip_decode(sbuf, rbuf));
97 }
int32_t slip_decode(uint8_t *sbuf, uint16_t ssize, uint8_t *rbuf, uint16_t rsize)
Decode SLIP packet.
Definition: sliplib.cpp:109
#define SLIP_ERROR_CRC
Definition: cosmos-errno.h:100
int32_t slip_check_crc(uint8_t *sbuf, uint16_t ssize)
Check SLIP packet CRC.
Definition: sliplib.cpp:47
int32_t slip_decode ( uint8_t *  sbuf,
uint16_t  ssize,
uint8_t *  rbuf,
uint16_t  rsize 
)

Decode SLIP packet.

Convert SLIP data in one buffer to raw ASCII data in second buffer and return length.

Parameters
sbufcharacter buffer to read SLIP data from
ssizesize of SLIP buffer
rbufcharacter buffer to store raw data
rsizesize of raw buffer, including terminal 0
Returns
bytes in raw buffer
110 {
111  uint16_t i, j, ch;
112 
113  i = j = 0;
114  do
115  {
116  if (j > ssize-3)
117  return (SLIP_ERROR_PACKING);
118  ch = sbuf[j++];
119  if (i < rsize)
120  {
121  switch (ch)
122  {
123  case SLIP_FESC:
124  if (j > ssize-3)
125  return (SLIP_ERROR_PACKING);
126  ch = sbuf[j++];
127  switch (ch)
128  {
129  case SLIP_TFEND:
130  rbuf[i] = SLIP_FEND;
131  break;
132  case SLIP_TFESC:
133  rbuf[i] = SLIP_FESC;
134  break;
135  }
136  ++i;
137  break;
138  case SLIP_FEND:
139  break;
140  default:
141  rbuf[i] = ch;
142  ++i;
143  break;
144  }
145  }
146  else
147  return (SLIP_ERROR_BUFFER);
148  } while (ch != SLIP_FEND);
149 
150  return (i);
151 }
#define SLIP_ERROR_BUFFER
Definition: cosmos-errno.h:102
int i
Definition: rw_test.cpp:37
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
#define SLIP_ERROR_PACKING
Definition: cosmos-errno.h:101
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t slip_decode ( slip_t sbuf,
slip_t rbuf 
)
154 {
155  size_t j, ch;
156  rbuf.clear();
157 
158  j = 0;
159  do
160  {
161  if (j > sbuf.size()-3)
162  {
163  return (SLIP_ERROR_PACKING);
164  }
165  ch = sbuf[j++];
166  } while (ch != SLIP_FEND);
167 
168  do
169  {
170  if (j > sbuf.size()-3)
171  {
172  return (SLIP_ERROR_PACKING);
173  }
174  ch = sbuf[j++];
175  switch (ch)
176  {
177  case SLIP_FESC:
178  if (j > sbuf.size()-3)
179  return (SLIP_ERROR_PACKING);
180  ch = sbuf[j++];
181  switch (ch)
182  {
183  case SLIP_TFEND:
184  rbuf.push_back(SLIP_FEND);
185  break;
186  case SLIP_TFESC:
187  rbuf.push_back(SLIP_FESC);
188  break;
189  }
190  break;
191  case SLIP_FEND:
192  break;
193  default:
194  rbuf.push_back(ch);
195  break;
196  }
197  } while (ch != SLIP_FEND);
198 
199  return (rbuf.size());
200 }
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
#define SLIP_ERROR_PACKING
Definition: cosmos-errno.h:101
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t slip_encode ( uint8_t *  rbuf,
uint16_t  rsize,
uint8_t *  sbuf,
uint16_t  ssize 
)

Encode data in to SLIP packet.

Convert raw ASCII in one buffer to SLIP encoded data in second buffer.

Parameters
rbufBuffer with raw data
rsizeNumber of bytes in raw buffer
sbufBuffer to place SLIP data in
ssizeMaximum size of SLIP buffer
Returns
bytes actually written
213 {
214  uint16_t i, j;
215 
216  i = j = 0;
217  if (ssize < 3)
218  return (SLIP_ERROR_BUFFER);
219  sbuf[i++] = SLIP_FEND;
220 
221  for (j=0; j<rsize; ++j)
222  {
223  switch (rbuf[j])
224  {
225  case SLIP_FEND:
226  if (i > ssize-4)
227  return (SLIP_ERROR_BUFFER);
228  sbuf[i++] = SLIP_FESC;
229  sbuf[i++] = SLIP_TFEND;
230  break;
231  case SLIP_FESC:
232  if (i > ssize-4)
233  return (SLIP_ERROR_BUFFER);
234  sbuf[i++] = SLIP_FESC;
235  sbuf[i++] = SLIP_TFESC;
236  break;
237  default:
238  if (i > ssize-3)
239  return (SLIP_ERROR_BUFFER);
240  sbuf[i++] = rbuf[j];
241  break;
242  }
243  }
244  if (ssize < i+1)
245  return (SLIP_ERROR_BUFFER);
246  sbuf[i++] = SLIP_FEND;
247  return (i);
248 }
#define SLIP_ERROR_BUFFER
Definition: cosmos-errno.h:102
int i
Definition: rw_test.cpp:37
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t slip_encode ( slip_t rbuf,
slip_t sbuf 
)
251 {
252  sbuf.clear();
253  sbuf.push_back(SLIP_FEND);
254 
255  for (size_t j=0; j<rbuf.size(); ++j)
256  {
257  switch (rbuf[j])
258  {
259  case SLIP_FEND:
260  sbuf.push_back(SLIP_FESC);
261  sbuf.push_back(SLIP_TFEND);
262  break;
263  case SLIP_FESC:
264  sbuf.push_back(SLIP_FESC);
265  sbuf.push_back(SLIP_TFESC);
266  break;
267  default:
268  sbuf.push_back(rbuf[j]);
269  break;
270  }
271  }
272  sbuf.push_back(SLIP_FEND);
273  sbuf.push_back(0);
274  sbuf.push_back(0);
275  return (sbuf.size());
276 }
#define SLIP_FESC
SLIP Buffer Escape character.
Definition: sliplib.h:60
#define SLIP_TFEND
SLIP Buffer Escaped End character.
Definition: sliplib.h:64
#define SLIP_FEND
SLIP Buffer End character.
Definition: sliplib.h:56
#define SLIP_TFESC
SLIP Buffer Escaped Escape character.
Definition: sliplib.h:68
int32_t slip_pack ( uint8_t *  rbuf,
uint16_t  rsize,
uint8_t *  sbuf,
uint16_t  ssize 
)

Pack data in to SLIP packet.

Convert raw ASCII in one buffer to SLIP encoded data in second buffer, appending a 16 bit CCITT CRC.

Parameters
rbufBuffer with raw data
rsizeNumber of bytes in raw buffer
sbufBuffer to place SLIP data in
ssizeMaximum size of SLIP buffer
Returns
bytes actually written
289 {
290  int32_t i;
291 
292  i = slip_encode(rbuf, rsize, sbuf, ssize);
293  if (i < 0) return (SLIP_ERROR_BUFFER);
294  if (ssize < i+3) return (SLIP_ERROR_BUFFER);
295  slip_set_crc(sbuf,i);
296  i+=2;
297  return (i);
298 }
int32_t slip_encode(uint8_t *rbuf, uint16_t rsize, uint8_t *sbuf, uint16_t ssize)
Encode data in to SLIP packet.
Definition: sliplib.cpp:212
#define SLIP_ERROR_BUFFER
Definition: cosmos-errno.h:102
int i
Definition: rw_test.cpp:37
uint16_t slip_set_crc(uint8_t *buf, uint16_t index)
Set CRC for SLIP buffer.
Definition: sliplib.cpp:388
int32_t slip_pack ( slip_t rbuf,
slip_t sbuf 
)
301 {
302  int32_t iretn;
303 
304  iretn = slip_encode(rbuf, sbuf);
305  if (iretn < 0)
306  {
307  return iretn;
308  }
309 
310  slip_set_crc(sbuf);
311  return sbuf.size();
312 }
int32_t slip_encode(uint8_t *rbuf, uint16_t rsize, uint8_t *sbuf, uint16_t ssize)
Encode data in to SLIP packet.
Definition: sliplib.cpp:212
int iretn
Definition: rw_test.cpp:37
uint16_t slip_set_crc(uint8_t *buf, uint16_t index)
Set CRC for SLIP buffer.
Definition: sliplib.cpp:388
uint16_t slip_calc_crc ( uint8_t *  buf,
uint16_t  size 
)

Calculate CRC-16-CCITT.

Calculate 16-bit CCITT CRC for the indicated buffer and number of bytes. The initial shift register value is 0xffff, and the calculation starts with the LSB, so the Polynomial is 0x8408.

Parameters
bufbytes to calculate on
sizenumber of bytes
Returns
calculated CRC
323 {
324  uint16_t crc = 0xffff;
325  uint8_t ch;
326  int i, j;
327 
328  for (i=0; i<size; ++i)
329  {
330  ch = buf[i];
331  for (j=0; j<8; j++)
332  {
333  crc = (crc >> 1)^(((ch^crc)&0x01)?0x8408:0);
334  ch >>= 1;
335  }
336  }
337  return (crc);
338 }
int i
Definition: rw_test.cpp:37
png_uint_32 crc
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
uint16_t slip_calc_crc ( slip_t buf)
341 {
342  uint16_t crc = 0xffff;
343  uint8_t ch;
344 
345  for (size_t i=0; i<buf.size()-2; ++i)
346  {
347  ch = buf[i];
348  for (size_t j=0; j<8; ++j)
349  {
350  crc = (crc >> 1)^(((ch^crc)&0x01)?0x8408:0);
351  ch >>= 1;
352  }
353  }
354  return (crc);
355 }
int i
Definition: rw_test.cpp:37
png_uint_32 crc
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
uint16_t slip_get_crc ( uint8_t *  buf,
uint16_t  index 
)

Get CRC from SLIP buffer.

Extract the CRC from the specified point in a SLIP buffer and return it as an unsigned integer.

Parameters
bufSLIP buffer
indexLocation in buffer of start of CRC
Returns
CRC
365 {
366  uint16_t crc;
367 
368  memcpy(&crc, &buf[index], 2);
369 
370  return (crc);
371 }
png_uint_32 crc
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
uint16_t slip_get_crc ( slip_t buf)
374 {
375  uint16_t crc;
376 
377  memcpy(&crc, &buf[buf.size()-2], 2);
378 
379  return (crc);
380 }
png_uint_32 crc
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
uint16_t slip_set_crc ( uint8_t *  buf,
uint16_t  index 
)

Set CRC for SLIP buffer.

Calculate the CRC for the specified length SLIP buffer and append it to the buffer.

Parameters
bufSLIP buffer
indexLocation in buffer of start of CRC
Returns
Calculated CRC
389 {
390  uint16_t crc;
391 
392  crc = slip_calc_crc(buf, index);
393  memcpy(&buf[index], &crc, 2);
394 
395  return (crc);
396 }
png_uint_32 crc
Definition: png.c:2173
uint16_t slip_calc_crc(uint8_t *buf, uint16_t size)
Calculate CRC-16-CCITT.
Definition: sliplib.cpp:322
char buf[128]
Definition: rw_test.cpp:40
uint16_t slip_set_crc ( slip_t buf)
399 {
400  uint16_t crc;
401 
402  crc = slip_calc_crc(buf);
403  memcpy(&buf[buf.size()-2], &crc, 2);
404 
405  return (crc);
406 }
png_uint_32 crc
Definition: png.c:2173
uint16_t slip_calc_crc(uint8_t *buf, uint16_t size)
Calculate CRC-16-CCITT.
Definition: sliplib.cpp:322
char buf[128]
Definition: rw_test.cpp:40