slip_proto.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #ifndef __UM_SLIP_PROTO_H__
  6. #define __UM_SLIP_PROTO_H__
  7. /* SLIP protocol characters. */
  8. #define SLIP_END 0300 /* indicates end of frame */
  9. #define SLIP_ESC 0333 /* indicates byte stuffing */
  10. #define SLIP_ESC_END 0334 /* ESC ESC_END means END 'data' */
  11. #define SLIP_ESC_ESC 0335 /* ESC ESC_ESC means ESC 'data' */
  12. static inline int slip_unesc(unsigned char c, unsigned char *buf, int *pos,
  13. int *esc)
  14. {
  15. int ret;
  16. switch(c){
  17. case SLIP_END:
  18. *esc = 0;
  19. ret=*pos;
  20. *pos=0;
  21. return(ret);
  22. case SLIP_ESC:
  23. *esc = 1;
  24. return(0);
  25. case SLIP_ESC_ESC:
  26. if(*esc){
  27. *esc = 0;
  28. c = SLIP_ESC;
  29. }
  30. break;
  31. case SLIP_ESC_END:
  32. if(*esc){
  33. *esc = 0;
  34. c = SLIP_END;
  35. }
  36. break;
  37. }
  38. buf[(*pos)++] = c;
  39. return(0);
  40. }
  41. static inline int slip_esc(unsigned char *s, unsigned char *d, int len)
  42. {
  43. unsigned char *ptr = d;
  44. unsigned char c;
  45. /*
  46. * Send an initial END character to flush out any
  47. * data that may have accumulated in the receiver
  48. * due to line noise.
  49. */
  50. *ptr++ = SLIP_END;
  51. /*
  52. * For each byte in the packet, send the appropriate
  53. * character sequence, according to the SLIP protocol.
  54. */
  55. while (len-- > 0) {
  56. switch(c = *s++) {
  57. case SLIP_END:
  58. *ptr++ = SLIP_ESC;
  59. *ptr++ = SLIP_ESC_END;
  60. break;
  61. case SLIP_ESC:
  62. *ptr++ = SLIP_ESC;
  63. *ptr++ = SLIP_ESC_ESC;
  64. break;
  65. default:
  66. *ptr++ = c;
  67. break;
  68. }
  69. }
  70. *ptr++ = SLIP_END;
  71. return (ptr - d);
  72. }
  73. #endif
  74. /*
  75. * Overrides for Emacs so that we follow Linus's tabbing style.
  76. * Emacs will notice this stuff at the end of the file and automatically
  77. * adjust the settings for this buffer only. This must remain at the end
  78. * of the file.
  79. * ---------------------------------------------------------------------------
  80. * Local variables:
  81. * c-file-style: "linux"
  82. * End:
  83. */