nx_connect.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /****************************************************************************
  2. * libs/libnx/nxmu/nx_connect.c
  3. *
  4. * Copyright (C) 2008-2009, 2011-2013 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 <stdint.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <fcntl.h>
  43. #include <semaphore.h>
  44. #include <mqueue.h>
  45. #include <errno.h>
  46. #include <debug.h>
  47. #include <nuttx/signal.h>
  48. #include <nuttx/semaphore.h>
  49. #include <nuttx/nx/nx.h>
  50. #include <nuttx/nx/nxmu.h>
  51. #include "nxcontext.h"
  52. /****************************************************************************
  53. * Private Data
  54. ****************************************************************************/
  55. /* Each client is assigned a unique ID using the g_nxcid counter. That
  56. * counter increments as each new counter is created and is protected for
  57. * thread safety with g_nxlibsem. Note that these are the only global values
  58. * in the NX implementation. This is because the client ID must be unique
  59. * even across all server instances.
  60. *
  61. * NOTE: that client ID 0 is reserved for the server(s) themselves
  62. */
  63. static sem_t g_nxlibsem = SEM_INITIALIZER(1);
  64. static uint32_t g_nxcid = 1;
  65. /****************************************************************************
  66. * Public Functions
  67. ****************************************************************************/
  68. /****************************************************************************
  69. * Name: nx_connectinstance
  70. *
  71. * Description:
  72. * Open a connection from a client to the NX server. One one client
  73. * connection is normally needed per thread as each connection can host
  74. * multiple windows.
  75. *
  76. * NOTES:
  77. * - This function returns before the connection is fully instantiated.
  78. * it is necessary to wait for the connection event before using the
  79. * returned handle.
  80. * - Multiple instances of the NX server may run at the same time,
  81. * each with different message queue names.
  82. *
  83. * Input Parameters:
  84. * svrmqname - The name for the server incoming message queue
  85. *
  86. * Returned Value:
  87. * Success: A non-NULL handle used with subsequent NX accesses
  88. * Failure: NULL is returned and errno is set appropriately
  89. *
  90. ****************************************************************************/
  91. NXHANDLE nx_connectinstance(FAR const char *svrmqname)
  92. {
  93. FAR struct nxfe_conn_s *conn;
  94. struct nxsvrmsg_s outmsg;
  95. char climqname[NX_CLIENT_MXNAMELEN];
  96. struct mq_attr attr;
  97. int ret;
  98. /* Sanity checking */
  99. #ifdef CONFIG_DEBUG_FEATURES
  100. if (!svrmqname)
  101. {
  102. set_errno(EINVAL);
  103. return NULL;
  104. }
  105. #endif
  106. /* Allocate the NX client structure */
  107. conn = (FAR struct nxfe_conn_s *)lib_uzalloc(sizeof(struct nxfe_conn_s));
  108. if (!conn)
  109. {
  110. set_errno(ENOMEM);
  111. goto errout;
  112. }
  113. /* Create the client MQ name */
  114. nxmu_semtake(&g_nxlibsem);
  115. conn->cid = g_nxcid++;
  116. nxmu_semgive(&g_nxlibsem);
  117. sprintf(climqname, NX_CLIENT_MQNAMEFMT, conn->cid);
  118. /* Open the client MQ for reading */
  119. attr.mq_maxmsg = CONFIG_NX_MXCLIENTMSGS;
  120. attr.mq_msgsize = NX_MXCLIMSGLEN;
  121. attr.mq_flags = 0;
  122. #ifdef CONFIG_NX_BLOCKING
  123. conn->crdmq = mq_open(climqname, O_RDONLY|O_CREAT, 0666, &attr);
  124. #else
  125. conn->crdmq = mq_open(climqname, O_RDONLY|O_CREAT|O_NONBLOCK, 0666, &attr);
  126. #endif
  127. if (conn->crdmq == (mqd_t)-1)
  128. {
  129. gerr("ERROR: mq_open(%s) failed: %d\n", climqname, errno);
  130. goto errout_with_conn;
  131. }
  132. /* Open the server MQ for writing */
  133. attr.mq_maxmsg = CONFIG_NX_MXSERVERMSGS;
  134. attr.mq_msgsize = NX_MXSVRMSGLEN;
  135. attr.mq_flags = 0;
  136. conn->cwrmq = mq_open(svrmqname, O_WRONLY|O_CREAT, 0666, &attr);
  137. if (conn->cwrmq == (mqd_t)-1)
  138. {
  139. gerr("ERROR: mq_open(%s) failed: %d\n", svrmqname, errno);
  140. goto errout_with_rmq;
  141. }
  142. /* Inform the server that this client exists */
  143. outmsg.msgid = NX_SVRMSG_CONNECT;
  144. outmsg.conn = conn;
  145. ret = nxmu_sendserver(conn, &outmsg, sizeof(struct nxsvrmsg_s));
  146. if (ret < 0)
  147. {
  148. gerr("ERROR: nxmu_sendserver failed: %d\n", errno);
  149. goto errout_with_wmq;
  150. }
  151. #if 0
  152. /* Now read until we get a response to this message. The server will
  153. * respond with either (1) NX_CLIMSG_CONNECTED, in which case the state
  154. * will change to NX_CLISTATE_CONNECTED, or (2) NX_CLIMSG_DISCONNECTED
  155. * in which case, nx_message will fail with errno = EHOSTDOWN.
  156. */
  157. do
  158. {
  159. ret = nx_eventhandler((NXHANDLE)conn);
  160. if (ret < 0)
  161. {
  162. gerr("ERROR: nx_message failed: %d\n", errno);
  163. goto errout_with_wmq;
  164. }
  165. _SIG_USLEEP(300000);
  166. }
  167. while (conn->state != NX_CLISTATE_CONNECTED);
  168. #endif
  169. return (NXHANDLE)conn;
  170. errout_with_wmq:
  171. mq_close(conn->cwrmq);
  172. errout_with_rmq:
  173. mq_close(conn->crdmq);
  174. errout_with_conn:
  175. lib_ufree(conn);
  176. errout:
  177. return NULL;
  178. }