cmux.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_DEBUG
  17. /* CMUX using long frame mode by default */
  18. #define CMUX_RECV_READ_MAX 2048
  19. #ifndef CMUX_BUFFER_SIZE
  20. #define CMUX_BUFFER_SIZE (CMUX_RECV_READ_MAX * 2)
  21. #endif
  22. #define CMUX_SW_VERSION "1.1.0"
  23. #define CMUX_SW_VERSION_NUM 0x10100
  24. struct cmux_buffer
  25. {
  26. rt_uint8_t data[CMUX_BUFFER_SIZE]; /* cmux data buffer */
  27. rt_uint8_t *read_point;
  28. rt_uint8_t *write_point;
  29. rt_uint8_t *end_point;
  30. int flag_found; /* the flag whether you find cmux frame */
  31. };
  32. struct cmux_frame
  33. {
  34. rt_uint8_t channel; /* the frame channel */
  35. rt_uint8_t control; /* the type of frame */
  36. int data_length; /* frame length */
  37. rt_uint8_t *data; /* the point for cmux data */
  38. };
  39. struct frame
  40. {
  41. struct cmux_frame *frame; /* the point for cmux frame */
  42. rt_slist_t frame_list; /* slist for different virtual serial */
  43. };
  44. struct cmux_vcoms
  45. {
  46. struct rt_device device; /* virtual device */
  47. rt_slist_t flist; /* head of frame_list */
  48. rt_uint16_t frame_index; /* the length of flist */
  49. rt_uint8_t link_port; /* link port id */
  50. 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 */
  51. struct cmux_frame *frame;
  52. rt_size_t length;
  53. rt_uint8_t *data;
  54. };
  55. struct cmux
  56. {
  57. struct rt_device *dev; /* device object */
  58. const struct cmux_ops *ops; /* cmux device ops interface */
  59. struct cmux_buffer *buffer; /* cmux buffer */
  60. struct cmux_frame *frame; /* cmux frame point */
  61. rt_thread_t recv_tid; /* receive thread point */
  62. rt_uint8_t vcom_num; /* the cmux port number */
  63. struct cmux_vcoms *vcoms; /* array */
  64. struct rt_event *event; /* internal communication */
  65. rt_slist_t list; /* cmux list */
  66. void *user_data; /* reserve */
  67. };
  68. struct cmux_ops
  69. {
  70. rt_err_t (*start) (struct cmux *obj);
  71. rt_err_t (*stop) (struct cmux *obj);
  72. rt_err_t (*control) (struct cmux *obj, int cmd, void *arg);
  73. };
  74. /* those function is opening for external file */
  75. rt_err_t cmux_init(struct cmux *object, const char *dev_name, rt_uint8_t vcom_num, void *user_data);
  76. rt_err_t cmux_start(struct cmux *object);
  77. rt_err_t cmux_stop(struct cmux *object);
  78. rt_err_t cmux_attach(struct cmux *object, int port, const char *alias_name, rt_uint16_t flags, void *user_data);
  79. rt_err_t cmux_detach(struct cmux *object, const char *alias_name);
  80. void cmux_at_cmd_cfg(uint8_t mode, uint8_t subset, uint32_t port_speed, uint32_t N1, uint32_t T1, uint32_t N2,
  81. uint32_t T2, uint32_t T3, uint32_t k);
  82. /* cmux_utils */
  83. rt_uint8_t cmux_frame_check(const rt_uint8_t *input, int length);
  84. struct cmux *cmux_object_find(const char *name);
  85. #ifdef __cplusplus
  86. }
  87. #endif
  88. #endif /* __CMUX_H__ */