sample.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <cmux.h>
  11. #include <rtthread.h>
  12. #define CMUX_PPP_NAME "cmux_ppp"
  13. #define CMUX_AT_NAME "cmux_at"
  14. #define CMUX_PPP_PORT 1
  15. #define CMUX_AT_PORT 2
  16. #define DBG_TAG "cmux.sample"
  17. #ifdef CMUX_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif
  22. #include <rtdbg.h>
  23. struct cmux *sample = RT_NULL;
  24. int cmux_sample_start(void)
  25. {
  26. rt_err_t result;
  27. sample = cmux_object_find(CMUX_DEPEND_NAME);
  28. if(sample == RT_NULL)
  29. {
  30. result = -RT_ERROR;
  31. LOG_E("Can't find %s", CMUX_DEPEND_NAME);
  32. goto end;
  33. }
  34. result =cmux_start(sample);
  35. if(result != RT_EOK)
  36. {
  37. LOG_E("cmux sample start error. Can't find %s", CMUX_DEPEND_NAME);
  38. goto end;
  39. }
  40. LOG_I("cmux sample (%s) start successful.", CMUX_DEPEND_NAME);
  41. #ifdef CMUX_AT_NAME
  42. result = cmux_attach(sample, CMUX_AT_PORT, CMUX_AT_NAME, RT_DEVICE_FLAG_DMA_RX, RT_NULL);
  43. if(result != RT_EOK)
  44. {
  45. LOG_E("cmux attach (%s) failed.", CMUX_AT_NAME);
  46. goto end;
  47. }
  48. LOG_I("cmux object channel (%s) attach successful.", CMUX_AT_NAME);
  49. #endif
  50. #ifdef CMUX_PPP_NAME
  51. result = cmux_attach(sample, CMUX_PPP_PORT, CMUX_PPP_NAME, RT_DEVICE_FLAG_DMA_RX, RT_NULL);
  52. if(result != RT_EOK)
  53. {
  54. LOG_E("cmux attach %s failed.", CMUX_PPP_NAME);
  55. goto end;
  56. }
  57. LOG_I("cmux object channel (%s) attach successful.", CMUX_PPP_NAME);
  58. #endif
  59. end:
  60. return RT_EOK;
  61. }
  62. INIT_APP_EXPORT(cmux_sample_start);
  63. MSH_CMD_EXPORT_ALIAS(cmux_sample_start, cmux_start, a sample of cmux function);
  64. int cmux_sample_stop(void)
  65. {
  66. rt_err_t result;
  67. #ifdef CMUX_AT_NAME
  68. result = cmux_detach(sample, CMUX_AT_NAME);
  69. if(result != RT_EOK)
  70. {
  71. LOG_E("cmux object (%s) detach failed.", CMUX_AT_NAME);
  72. goto end;
  73. }
  74. #endif
  75. #ifdef CMUX_PPP_NAME
  76. result = cmux_detach(sample, CMUX_PPP_NAME);
  77. if(result != RT_EOK)
  78. {
  79. LOG_E("cmux object (%s) detach failed.", CMUX_PPP_NAME);
  80. goto end;
  81. }
  82. #endif
  83. result = cmux_stop(sample);
  84. if(result != RT_EOK)
  85. {
  86. LOG_E("cmux sample stop error.");
  87. goto end;
  88. }
  89. end:
  90. return RT_EOK;
  91. }
  92. MSH_CMD_EXPORT_ALIAS(cmux_sample_stop, cmux_stop, a sample of cmux function);