slip_proto.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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,char *buf,int *pos, int *esc)
  13. {
  14. int ret;
  15. switch(c){
  16. case SLIP_END:
  17. *esc = 0;
  18. ret=*pos;
  19. *pos=0;
  20. return(ret);
  21. case SLIP_ESC:
  22. *esc = 1;
  23. return(0);
  24. case SLIP_ESC_ESC:
  25. if(*esc){
  26. *esc = 0;
  27. c = SLIP_ESC;
  28. }
  29. break;
  30. case SLIP_ESC_END:
  31. if(*esc){
  32. *esc = 0;
  33. c = SLIP_END;
  34. }
  35. break;
  36. }
  37. buf[(*pos)++] = c;
  38. return(0);
  39. }
  40. static inline int slip_esc(unsigned char *s, unsigned char *d, int len)
  41. {
  42. unsigned char *ptr = d;
  43. unsigned char c;
  44. /*
  45. * Send an initial END character to flush out any
  46. * data that may have accumulated in the receiver
  47. * due to line noise.
  48. */
  49. *ptr++ = SLIP_END;
  50. /*
  51. * For each byte in the packet, send the appropriate
  52. * character sequence, according to the SLIP protocol.
  53. */
  54. while (len-- > 0) {
  55. switch(c = *s++) {
  56. case SLIP_END:
  57. *ptr++ = SLIP_ESC;
  58. *ptr++ = SLIP_ESC_END;
  59. break;
  60. case SLIP_ESC:
  61. *ptr++ = SLIP_ESC;
  62. *ptr++ = SLIP_ESC_ESC;
  63. break;
  64. default:
  65. *ptr++ = c;
  66. break;
  67. }
  68. }
  69. *ptr++ = SLIP_END;
  70. return (ptr - d);
  71. }
  72. #endif
  73. /*
  74. * Overrides for Emacs so that we follow Linus's tabbing style.
  75. * Emacs will notice this stuff at the end of the file and automatically
  76. * adjust the settings for this buffer only. This must remain at the end
  77. * of the file.
  78. * ---------------------------------------------------------------------------
  79. * Local variables:
  80. * c-file-style: "linux"
  81. * End:
  82. */