nx_start.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /****************************************************************************
  2. * graphics/nxmu/nx_start.c
  3. *
  4. * Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdbool.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. #include <sched.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/board.h>
  46. #include <nuttx/kthread.h>
  47. #include <nuttx/nx/nx.h>
  48. #include "nxfe.h"
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. static bool g_nxserver_started;
  53. /****************************************************************************
  54. * Private Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: nx_server
  58. *
  59. * Description:
  60. * NX server thread. This is the entry point into the server kernel
  61. * thread that serializes the multi-threaded accesses to the display.
  62. *
  63. * Input Parameters:
  64. * Standard task start-up parameters (none of which are used)
  65. *
  66. * Returned Value:
  67. * This function does not normally return but may exit with EXIT_FAILURE
  68. * under certain error conditions.
  69. *
  70. ****************************************************************************/
  71. int nx_server(int argc, char *argv[])
  72. {
  73. FAR NX_DRIVERTYPE *dev;
  74. int ret;
  75. #if defined(CONFIG_NXSTART_EXTERNINIT)
  76. /* Use external graphics driver initialization */
  77. dev = board_graphics_setup(CONFIG_NXSTART_DEVNO);
  78. if (!dev)
  79. {
  80. gerr("ERROR: board_graphics_setup failed, devno=%d\n", CONFIG_NXSTART_DEVNO);
  81. return EXIT_FAILURE;
  82. }
  83. #elif defined(CONFIG_NX_LCDDRIVER)
  84. /* Initialize the LCD device */
  85. ret = board_lcd_initialize();
  86. if (ret < 0)
  87. {
  88. gerr("ERROR: board_lcd_initialize failed: %d\n", ret);
  89. return EXIT_FAILURE;
  90. }
  91. /* Get the device instance */
  92. dev = board_lcd_getdev(CONFIG_NXSTART_DEVNO);
  93. if (!dev)
  94. {
  95. gerr("ERROR: board_lcd_getdev failed, devno=%d\n", CONFIG_NXSTART_DEVNO);
  96. return EXIT_FAILURE;
  97. }
  98. /* Turn the LCD on at 75% power */
  99. (void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
  100. #else /* CONFIG_NX_LCDDRIVER */
  101. /* Initialize the frame buffer device.
  102. * REVISIT: display == 0 is assumed.
  103. */
  104. ret = up_fbinitialize(CONFIG_NXSTART_DISPLAYNO);
  105. if (ret < 0)
  106. {
  107. gerr("ERROR: up_fbinitialize failed: %d\n", ret);
  108. return EXIT_FAILURE;
  109. }
  110. dev = up_fbgetvplane(CONFIG_NXSTART_DISPLAYNO, CONFIG_NXSTART_VPLANE);
  111. if (!dev)
  112. {
  113. gerr("ERROR: up_fbgetvplane failed, vplane=%d\n", CONFIG_NXSTART_VPLANE);
  114. return EXIT_FAILURE;
  115. }
  116. #endif /* CONFIG_NX_LCDDRIVER */
  117. /* Then start the server (nx_run does not normally return) */
  118. ret = nx_run(dev);
  119. ginfo("nx_run returned: %d\n", ret);
  120. return EXIT_FAILURE;
  121. }
  122. /****************************************************************************
  123. * Public Functions
  124. ****************************************************************************/
  125. /****************************************************************************
  126. * Name: nx_start
  127. *
  128. * Description:
  129. * nx_start() provides a wrapper function to simplify and standardize the
  130. * starting of the NX server.
  131. *
  132. * NOTE: Currently, many applications include logic to start the NX
  133. * server from application initialization logic. That, of course, cannot
  134. * work in the NuttX kernel build because the resources required by the
  135. * NX server are private to the kernel mode logic.
  136. *
  137. * nx_start() can be called (indirectly) from applications via the
  138. * boardctl() interface with the BOARDIOC_NX_START command.
  139. *
  140. * Input Parameters:
  141. * None
  142. *
  143. * Returned Value:
  144. * Zero (OK) is returned on success. This indicates that the NX server
  145. * has been successfully started, is running, and waiting to accept
  146. * connections from NX clients.
  147. *
  148. * A negated errno value is returned on failure. The errno value indicates
  149. * the nature of the failure.
  150. *
  151. ****************************************************************************/
  152. int nx_start(void)
  153. {
  154. /* Do nothing is the server has already been started */
  155. if (!g_nxserver_started)
  156. {
  157. pid_t server;
  158. /* Start the server kernel thread */
  159. ginfo("Starting server task\n");
  160. server = kernel_thread("NX Server", CONFIG_NXSTART_SERVERPRIO,
  161. CONFIG_NXSTART_SERVERSTACK, nx_server, NULL);
  162. if (server < 0)
  163. {
  164. int errcode = errno;
  165. DEBUGASSERT(errcode > 0);
  166. gerr("ERROR: Failed to create nx_server kernel thread: %d\n", errcode);
  167. return -errcode;
  168. }
  169. g_nxserver_started = true;
  170. /* Wait a bit to make sure that the server get started. NOTE that
  171. * this operation cannot be done from the IDLE thread!
  172. */
  173. usleep(50*1000);
  174. }
  175. return OK;
  176. }