bypass_upper_run.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-11-20 zhujiale the first version
  9. * 2025-11-24 ChuanN-sudo add standardized utest documentation block
  10. */
  11. /**
  12. * Test Case Name: Serial Bypass Upper Run Test
  13. *
  14. * Test Objectives:
  15. * - Validate serial device bypass upper layer registration and data processing mechanisms.
  16. * - Verify correct character reception and filtering through bypass callback functions.
  17. * - Ensure proper handling of bypass level priorities and callback execution order.
  18. * - Test core APIs: rt_bypass_upper_register(), rt_bypass_upper_unregister(), rt_hw_serial_isr()
  19. *
  20. * Test Scenarios:
  21. * - Register bypass callback at RT_BYPASS_LEVEL_1 to intercept incoming serial data.
  22. * - Verify bypass callback receives expected character values and executes correct number of times.
  23. * - Register bypass callback at RT_BYPASS_MAX_LEVEL with different character sequence.
  24. * - Test bypass unregistration and verify proper cleanup of callback handlers.
  25. *
  26. * Verification Metrics:
  27. * - Bypass callback executes exactly 10 times for first test case with character 'a'.
  28. * - Second test case processes 20 characters in ascending sequence ('b' to 'u').
  29. * - Character values received in callback match expected values from mock getc function.
  30. * - Bypass registration and unregistration complete without memory leaks or errors.
  31. * - Serial device operations restore to original state after test completion.
  32. *
  33. * Dependencies:
  34. * - Hardware requirements: Platform with serial console device (UART) support.
  35. * - Software configuration:
  36. * - RT_USING_UTESTCASES must be enabled (select "RT-Thread Utestcases" in menuconfig).
  37. * - RT_UTEST_SERIAL_BYPASS must be enabled (enable via: RT-Thread Utestcases -> Kernel Components -> Drivers -> Serial Test -> Serial Bypass Test).
  38. * - Environmental Assumptions: Serial device initialized and operational before test execution.
  39. *
  40. * Expected Results:
  41. * - Final output: "[ PASSED ] [ result ] testcase (components.drivers.serial.bypass_upper)"
  42. * - No assertions triggered during character validation or callback execution.
  43. */
  44. #include <rtthread.h>
  45. #include <rtdevice.h>
  46. #include "utest.h"
  47. static struct rt_serial_device* _serial0;
  48. static int cnt;
  49. #define __REG32(x) (*((volatile unsigned int*)((rt_ubase_t)x)))
  50. #define UART_FR(base) __REG32(base + 0x18)
  51. #define UART_DR(base) __REG32(base + 0x00)
  52. #define UARTFR_TXFF 0x20
  53. static rt_err_t utest_upper_run(struct rt_serial_device* serial, char ch, void* data)
  54. {
  55. uassert_true(ch == 'a');
  56. cnt++;
  57. return RT_EOK;
  58. }
  59. static int utest_getc(struct rt_serial_device* serial)
  60. {
  61. static rt_uint8_t num = 0;
  62. if (num == 10)
  63. return -1;
  64. num++;
  65. return 'a';
  66. }
  67. struct hw_uart_device
  68. {
  69. rt_size_t hw_base;
  70. rt_size_t irqno;
  71. };
  72. static int uart_putc(struct rt_serial_device* serial, char c)
  73. {
  74. struct hw_uart_device* uart;
  75. RT_ASSERT(serial != RT_NULL);
  76. uart = (struct hw_uart_device*)serial->parent.user_data;
  77. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  78. UART_DR(uart->hw_base) = c;
  79. return 1;
  80. }
  81. static const struct rt_uart_ops _utest_ops =
  82. {
  83. RT_NULL,
  84. RT_NULL,
  85. uart_putc,
  86. utest_getc,
  87. };
  88. static rt_err_t utest_lower_run_test2(struct rt_serial_device* serial, char ch, void* data)
  89. {
  90. static rt_uint8_t num = 0;
  91. num++;
  92. uassert_true(ch == ('a' + num));
  93. return RT_EOK;
  94. }
  95. static int utest_getc_2(struct rt_serial_device* serial)
  96. {
  97. static rt_uint8_t num = 0;
  98. if (num == 20)
  99. return -1;
  100. num++;
  101. return 'a' + num;
  102. }
  103. static const struct rt_uart_ops _utest_ops2 =
  104. {
  105. RT_NULL,
  106. RT_NULL,
  107. uart_putc,
  108. utest_getc_2,
  109. };
  110. static void bypass_upper_001(void)
  111. {
  112. const struct rt_uart_ops* tmp = _serial0->ops;
  113. _serial0->ops = &_utest_ops;
  114. rt_bypass_upper_register(_serial0, "utest", RT_BYPASS_LEVEL_1, utest_upper_run, RT_NULL);
  115. rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
  116. uassert_true(cnt == 10);
  117. _serial0->ops = tmp;
  118. rt_bypass_upper_unregister(_serial0, RT_BYPASS_LEVEL_1);
  119. }
  120. static void bypass_upper_002(void)
  121. {
  122. const struct rt_uart_ops* tmp = _serial0->ops;
  123. _serial0->ops = &_utest_ops2;
  124. rt_bypass_upper_register(_serial0, "utest", RT_BYPASS_MAX_LEVEL, utest_lower_run_test2, RT_NULL);
  125. rt_hw_serial_isr(_serial0, RT_SERIAL_EVENT_RX_IND);
  126. rt_thread_mdelay(100);
  127. uassert_true(cnt == 10);
  128. _serial0->ops = tmp;
  129. rt_bypass_upper_unregister(_serial0, RT_BYPASS_MAX_LEVEL);
  130. }
  131. static rt_err_t utest_tc_init(void)
  132. {
  133. _serial0 = (struct rt_serial_device*)rt_console_get_device();
  134. return RT_EOK;
  135. }
  136. static rt_err_t utest_tc_cleanup(void)
  137. {
  138. return RT_EOK;
  139. }
  140. static void _testcase(void)
  141. {
  142. UTEST_UNIT_RUN(bypass_upper_001);
  143. UTEST_UNIT_RUN(bypass_upper_002);
  144. }
  145. UTEST_TC_EXPORT(_testcase, "components.drivers.serial.bypass_upper", utest_tc_init, utest_tc_cleanup, 10);