main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "shell.h"
  16. #include "chprintf.h"
  17. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(4096)
  18. #define CONSOLE_WA_SIZE THD_WORKING_AREA_SIZE(4096)
  19. #define TEST_WA_SIZE THD_WORKING_AREA_SIZE(4096)
  20. #define cputs(msg) chMsgSend(cdtp, (msg_t)msg)
  21. static thread_t *cdtp;
  22. static thread_t *shelltp1;
  23. static thread_t *shelltp2;
  24. static const ShellCommand commands[] = {
  25. {NULL, NULL}
  26. };
  27. static const ShellConfig shell_cfg1 = {
  28. (BaseSequentialStream *)&SD1,
  29. commands
  30. };
  31. static const ShellConfig shell_cfg2 = {
  32. (BaseSequentialStream *)&SD2,
  33. commands
  34. };
  35. /*
  36. * Console print server done using synchronous messages. This makes the access
  37. * to the C printf() thread safe and the print operation atomic among threads.
  38. * In this example the message is the zero terminated string itself.
  39. */
  40. static THD_FUNCTION(console_thread, arg) {
  41. (void)arg;
  42. while (!chThdShouldTerminateX()) {
  43. thread_t *tp = chMsgWait();
  44. puts((char *)chMsgGet(tp));
  45. fflush(stdout);
  46. chMsgRelease(tp, MSG_OK);
  47. }
  48. }
  49. /**
  50. * @brief Shell termination handler.
  51. *
  52. * @param[in] id event id.
  53. */
  54. static void termination_handler(eventid_t id) {
  55. (void)id;
  56. if (shelltp1 && chThdTerminatedX(shelltp1)) {
  57. chThdWait(shelltp1);
  58. shelltp1 = NULL;
  59. chThdSleepMilliseconds(10);
  60. cputs("Init: shell on SD1 terminated");
  61. chSysLock();
  62. oqResetI(&SD1.oqueue);
  63. chSchRescheduleS();
  64. chSysUnlock();
  65. }
  66. if (shelltp2 && chThdTerminatedX(shelltp2)) {
  67. chThdWait(shelltp2);
  68. shelltp2 = NULL;
  69. chThdSleepMilliseconds(10);
  70. cputs("Init: shell on SD2 terminated");
  71. chSysLock();
  72. oqResetI(&SD2.oqueue);
  73. chSchRescheduleS();
  74. chSysUnlock();
  75. }
  76. }
  77. static event_listener_t sd1fel, sd2fel;
  78. /**
  79. * @brief SD1 status change handler.
  80. *
  81. * @param[in] id event id.
  82. */
  83. static void sd1_handler(eventid_t id) {
  84. eventflags_t flags;
  85. (void)id;
  86. flags = chEvtGetAndClearFlags(&sd1fel);
  87. if ((flags & CHN_CONNECTED) && (shelltp1 == NULL)) {
  88. cputs("Init: connection on SD1");
  89. shelltp1 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  90. "shell1", NORMALPRIO + 10,
  91. shellThread, (void *)&shell_cfg1);
  92. }
  93. if (flags & CHN_DISCONNECTED) {
  94. cputs("Init: disconnection on SD1");
  95. chSysLock();
  96. iqResetI(&SD1.iqueue);
  97. chSchRescheduleS();
  98. chSysUnlock();
  99. }
  100. }
  101. /**
  102. * @brief SD2 status change handler.
  103. *
  104. * @param[in] id event id.
  105. */
  106. static void sd2_handler(eventid_t id) {
  107. eventflags_t flags;
  108. (void)id;
  109. flags = chEvtGetAndClearFlags(&sd2fel);
  110. if ((flags & CHN_CONNECTED) && (shelltp2 == NULL)) {
  111. cputs("Init: connection on SD2");
  112. shelltp2 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  113. "shell2", NORMALPRIO + 10,
  114. shellThread, (void *)&shell_cfg2);
  115. }
  116. if (flags & CHN_DISCONNECTED) {
  117. cputs("Init: disconnection on SD2");
  118. chSysLock();
  119. iqResetI(&SD2.iqueue);
  120. chSchRescheduleS();
  121. chSysUnlock();
  122. }
  123. }
  124. static evhandler_t fhandlers[] = {
  125. termination_handler,
  126. sd1_handler,
  127. sd2_handler
  128. };
  129. /*------------------------------------------------------------------------*
  130. * Simulator main. *
  131. *------------------------------------------------------------------------*/
  132. int main(void) {
  133. event_listener_t tel;
  134. /*
  135. * System initializations.
  136. * - HAL initialization, this also initializes the configured device drivers
  137. * and performs the board-specific initializations.
  138. * - Kernel initialization, the main() function becomes a thread and the
  139. * RTOS is active.
  140. */
  141. halInit();
  142. chSysInit();
  143. /*
  144. * Serial ports (simulated) initialization.
  145. */
  146. sdStart(&SD1, NULL);
  147. sdStart(&SD2, NULL);
  148. /*
  149. * Shell manager initialization.
  150. */
  151. shellInit();
  152. chEvtRegister(&shell_terminated, &tel, 0);
  153. /*
  154. * Console thread started.
  155. */
  156. cdtp = chThdCreateFromHeap(NULL, CONSOLE_WA_SIZE, "console",
  157. NORMALPRIO + 1, console_thread, NULL);
  158. /*
  159. * Initializing connection/disconnection events.
  160. */
  161. cputs("Shell service started on SD1, SD2");
  162. cputs(" - Listening for connections on SD1");
  163. chEvtRegister(chnGetEventSource(&SD1), &sd1fel, 1);
  164. cputs(" - Listening for connections on SD2");
  165. chEvtRegister(chnGetEventSource(&SD2), &sd2fel, 2);
  166. /*
  167. * Events servicing loop.
  168. */
  169. while (!chThdShouldTerminateX())
  170. chEvtDispatch(fhandlers, chEvtWaitOne(ALL_EVENTS));
  171. /*
  172. * Clean simulator exit.
  173. */
  174. chEvtUnregister(chnGetEventSource(&SD1), &sd1fel);
  175. chEvtUnregister(chnGetEventSource(&SD2), &sd2fel);
  176. return 0;
  177. }