cmux.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-15 xiangxistu the first version
  9. */
  10. #ifndef __CMUX_H__
  11. #define __CMUX_H__
  12. #include <rtdef.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define CMUX_BUFFER_SIZE 2048
  17. #define CMUX_SW_VERSION "1.0.0"
  18. #define CMUX_SW_VERSION_NUM 0x10000
  19. struct cmux_buffer
  20. {
  21. rt_uint8_t data[CMUX_BUFFER_SIZE]; /* cmux data buffer */
  22. rt_uint8_t *read_point;
  23. rt_uint8_t *write_point;
  24. rt_uint8_t *end_point;
  25. int flag_found; /* the flag whether you find cmux frame */
  26. };
  27. struct cmux_frame
  28. {
  29. rt_uint8_t channel; /* the frame channel */
  30. rt_uint8_t control; /* the type of frame */
  31. int data_length; /* frame length */
  32. rt_uint8_t *data; /* the point for cmux data */
  33. };
  34. struct frame
  35. {
  36. struct cmux_frame *frame; /* the point for cmux frame */
  37. rt_slist_t frame_list; /* slist for different virtual serial */
  38. };
  39. struct cmux_vcoms
  40. {
  41. struct rt_device device; /* virtual device */
  42. rt_slist_t flist; /* head of frame_list */
  43. rt_uint16_t frame_index; /* the length of flist */
  44. rt_uint8_t link_port; /* link port id */
  45. rt_bool_t frame_using_status; /* This is designed for long frame when we read data; the flag will be "1" when long frame haven't reading done */
  46. struct cmux_frame *frame;
  47. rt_size_t length;
  48. rt_uint8_t *data;
  49. };
  50. struct cmux
  51. {
  52. struct rt_device *dev; /* device object */
  53. const struct cmux_ops *ops; /* cmux device ops interface */
  54. struct cmux_buffer *buffer; /* cmux buffer */
  55. struct cmux_frame *frame; /* cmux frame point */
  56. rt_thread_t recv_tid; /* receive thread point */
  57. rt_uint8_t vcom_num; /* the cmux port number */
  58. struct cmux_vcoms *vcoms; /* array */
  59. struct rt_event *event; /* internal communication */
  60. rt_slist_t list; /* cmux list */
  61. void *user_data; /* reserve */
  62. };
  63. struct cmux_ops
  64. {
  65. rt_err_t (*start) (struct cmux *obj);
  66. rt_err_t (*stop) (struct cmux *obj);
  67. rt_err_t (*control) (struct cmux *obj, int cmd, void *arg);
  68. };
  69. /* those function is opening for external file */
  70. rt_err_t cmux_init(struct cmux *object, const char *dev_name, rt_uint8_t vcom_num, void *user_data);
  71. rt_err_t cmux_start(struct cmux *object);
  72. rt_err_t cmux_stop(struct cmux *object);
  73. rt_err_t cmux_attach(struct cmux *object, int port, const char *alias_name, rt_uint16_t flags, void *user_data);
  74. rt_err_t cmux_detach(struct cmux *object, const char *alias_name);
  75. /* cmux_utils */
  76. rt_uint8_t cmux_frame_check(const rt_uint8_t *input, int length);
  77. struct cmux *cmux_object_find(const char *name);
  78. #ifdef __cplusplus
  79. }
  80. #endif
  81. #endif /* __CMUX_H__ */