test_npl_sem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. Unit tests for the Semaphore api (ble_npl_sem):
  21. ble_npl_error_t ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens);
  22. ble_npl_error_t ble_npl_sem_release(struct ble_npl_sem *sem);
  23. ble_npl_error_t ble_npl_sem_pend(struct ble_npl_sem *sem, uint32_t timeout);
  24. uint16_t ble_npl_sem_get_count(struct ble_npl_sem *sem);
  25. */
  26. #include "test_util.h"
  27. #include "nimble/nimble_npl.h"
  28. //#include "os/os.h"
  29. #define TEST_ITERATIONS 10
  30. #define TASK1_PRIO 1
  31. #define TASK2_PRIO 1
  32. #define TASK1_STACK_SIZE 1028
  33. #define TASK2_STACK_SIZE 1028
  34. static struct ble_npl_task task1;
  35. static struct ble_npl_task task2;
  36. static ble_npl_stack_t task1_stack[TASK1_STACK_SIZE];
  37. static ble_npl_stack_t task2_stack[TASK2_STACK_SIZE];
  38. struct ble_npl_sem task1_sem;
  39. struct ble_npl_sem task2_sem;
  40. /* Task 1 handler function */
  41. void *
  42. task1_handler(void *arg)
  43. {
  44. for (int i = 0; i < TEST_ITERATIONS; i++)
  45. {
  46. /* Release semaphore to task 2 */
  47. SuccessOrQuit(ble_npl_sem_release(&task1_sem),
  48. "ble_npl_sem_release: error releasing task2_sem.");
  49. /* Wait for semaphore from task 2 */
  50. SuccessOrQuit(ble_npl_sem_pend(&task2_sem, BLE_NPL_TIME_FOREVER),
  51. "ble_npl_sem_pend: error waiting for task2_sem.");
  52. }
  53. printf("All tests passed\n");
  54. exit(PASS);
  55. return NULL;
  56. }
  57. /* Task 2 handler function */
  58. void *
  59. task2_handler(void *arg)
  60. {
  61. while(1)
  62. {
  63. /* Wait for semaphore from task1 */
  64. SuccessOrQuit(ble_npl_sem_pend(&task1_sem, BLE_NPL_TIME_FOREVER),
  65. "ble_npl_sem_pend: error waiting for task1_sem.");
  66. /* Release task2 semaphore */
  67. SuccessOrQuit(ble_npl_sem_release(&task2_sem),
  68. "ble_npl_sem_release: error releasing task1_sem.");
  69. }
  70. return NULL;
  71. }
  72. /* Initialize task 1 exposed data objects */
  73. void
  74. task1_init(void)
  75. {
  76. /* Initialize task1 semaphore */
  77. SuccessOrQuit(ble_npl_sem_init(&task1_sem, 0),
  78. "ble_npl_sem_init: task1 returned error.");
  79. }
  80. /* Initialize task 2 exposed data objects */
  81. void
  82. task2_init(void)
  83. {
  84. /* Initialize task1 semaphore */
  85. SuccessOrQuit(ble_npl_sem_init(&task2_sem, 0),
  86. "ble_npl_sem_init: task2 returned error.");
  87. }
  88. /**
  89. * init_app_tasks
  90. *
  91. * This function performs initializations that are required before tasks run.
  92. *
  93. * @return int 0 success; error otherwise.
  94. */
  95. static int
  96. init_app_tasks(void)
  97. {
  98. /*
  99. * Call task specific initialization functions to initialize any shared objects
  100. * before initializing the tasks with the OS.
  101. */
  102. task1_init();
  103. task2_init();
  104. /*
  105. * Initialize tasks 1 and 2 with the OS.
  106. */
  107. ble_npl_task_init(&task1, "task1", task1_handler, NULL, TASK1_PRIO,
  108. BLE_NPL_WAIT_FOREVER, task1_stack, TASK1_STACK_SIZE);
  109. ble_npl_task_init(&task2, "task2", task2_handler, NULL, TASK2_PRIO,
  110. BLE_NPL_WAIT_FOREVER, task2_stack, TASK2_STACK_SIZE);
  111. return 0;
  112. }
  113. /**
  114. * main
  115. *
  116. * The main function for the application. This function initializes the system and packages,
  117. * calls the application specific task initialization function, then waits and dispatches
  118. * events from the OS default event queue in an infinite loop.
  119. */
  120. int
  121. main(int argc, char **arg)
  122. {
  123. /* Initialize application specific tasks */
  124. init_app_tasks();
  125. while (1)
  126. {
  127. ble_npl_eventq_run(ble_npl_eventq_dflt_get());
  128. }
  129. /* main never returns */
  130. }