2008-11-28 15:42:52 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libnx/nxmu/nx_connect.c
|
2008-11-28 15:42:52 +01:00
|
|
|
*
|
2021-02-23 19:55:00 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2008-11-28 15:42:52 +01:00
|
|
|
*
|
2021-02-23 19:55:00 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-28 15:42:52 +01:00
|
|
|
*
|
2021-02-23 19:55:00 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-28 15:42:52 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 19:12:29 +01:00
|
|
|
#include <stdint.h>
|
2008-11-28 15:42:52 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2017-10-08 20:14:18 +02:00
|
|
|
#include <nuttx/signal.h>
|
2021-02-15 06:11:27 +01:00
|
|
|
#include <nuttx/mqueue.h>
|
2011-07-24 22:49:01 +02:00
|
|
|
#include <nuttx/nx/nx.h>
|
2013-12-27 20:18:20 +01:00
|
|
|
#include <nuttx/nx/nxmu.h>
|
2013-03-09 22:12:20 +01:00
|
|
|
|
2013-12-28 15:40:03 +01:00
|
|
|
#include "nxcontext.h"
|
2008-11-28 15:42:52 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Each client is assigned a unique ID using the g_nxcid counter. That
|
2017-11-01 01:46:49 +01:00
|
|
|
* counter increments as each new counter is created and is protected for
|
|
|
|
* thread safety with g_nxlibsem. Note that these are the only global values
|
2008-11-28 15:42:52 +01:00
|
|
|
* in the NX implementation. This is because the client ID must be unique
|
|
|
|
* even across all server instances.
|
|
|
|
*
|
|
|
|
* NOTE: that client ID 0 is reserved for the server(s) themselves
|
|
|
|
*/
|
|
|
|
|
2017-10-08 16:13:47 +02:00
|
|
|
static sem_t g_nxlibsem = SEM_INITIALIZER(1);
|
2009-12-15 19:12:29 +01:00
|
|
|
static uint32_t g_nxcid = 1;
|
2008-11-28 15:42:52 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nx_connectinstance
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Open a connection from a client to the NX server. One one client
|
|
|
|
* connection is normally needed per thread as each connection can host
|
|
|
|
* multiple windows.
|
|
|
|
*
|
2008-11-29 21:25:28 +01:00
|
|
|
* NOTES:
|
|
|
|
* - This function returns before the connection is fully instantiated.
|
|
|
|
* it is necessary to wait for the connection event before using the
|
|
|
|
* returned handle.
|
|
|
|
* - Multiple instances of the NX server may run at the same time,
|
|
|
|
* each with different message queue names.
|
2008-11-28 15:42:52 +01:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* svrmqname - The name for the server incoming message queue
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2008-11-28 15:42:52 +01:00
|
|
|
* Success: A non-NULL handle used with subsequent NX accesses
|
|
|
|
* Failure: NULL is returned and errno is set appropriately
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2008-12-01 23:13:03 +01:00
|
|
|
NXHANDLE nx_connectinstance(FAR const char *svrmqname)
|
2008-11-28 15:42:52 +01:00
|
|
|
{
|
2019-03-13 16:16:30 +01:00
|
|
|
FAR struct nxmu_conn_s *conn;
|
2012-05-17 21:32:48 +02:00
|
|
|
struct nxsvrmsg_s outmsg;
|
2008-11-28 15:42:52 +01:00
|
|
|
char climqname[NX_CLIENT_MXNAMELEN];
|
|
|
|
struct mq_attr attr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Sanity checking */
|
|
|
|
|
2016-06-11 22:14:08 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
2008-11-29 00:04:54 +01:00
|
|
|
if (!svrmqname)
|
2008-11-28 15:42:52 +01:00
|
|
|
{
|
2013-12-27 20:18:20 +01:00
|
|
|
set_errno(EINVAL);
|
2008-11-28 15:42:52 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Allocate the NX client structure */
|
|
|
|
|
2019-03-13 16:16:30 +01:00
|
|
|
conn = (FAR struct nxmu_conn_s *)lib_uzalloc(sizeof(struct nxmu_conn_s));
|
2008-11-28 15:42:52 +01:00
|
|
|
if (!conn)
|
|
|
|
{
|
2013-12-27 20:18:20 +01:00
|
|
|
set_errno(ENOMEM);
|
2008-11-28 15:42:52 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the client MQ name */
|
|
|
|
|
|
|
|
nxmu_semtake(&g_nxlibsem);
|
|
|
|
conn->cid = g_nxcid++;
|
|
|
|
nxmu_semgive(&g_nxlibsem);
|
|
|
|
|
|
|
|
sprintf(climqname, NX_CLIENT_MQNAMEFMT, conn->cid);
|
|
|
|
|
|
|
|
/* Open the client MQ for reading */
|
|
|
|
|
|
|
|
attr.mq_maxmsg = CONFIG_NX_MXCLIENTMSGS;
|
|
|
|
attr.mq_msgsize = NX_MXCLIMSGLEN;
|
|
|
|
attr.mq_flags = 0;
|
|
|
|
|
|
|
|
#ifdef CONFIG_NX_BLOCKING
|
2021-02-15 06:11:27 +01:00
|
|
|
conn->crdmq = _MQ_OPEN(climqname, O_RDONLY | O_CREAT, 0666, &attr);
|
2008-11-28 15:42:52 +01:00
|
|
|
#else
|
2021-02-15 06:11:27 +01:00
|
|
|
conn->crdmq = _MQ_OPEN(climqname, O_RDONLY | O_CREAT | O_NONBLOCK,
|
|
|
|
0666, &attr);
|
2008-11-28 15:42:52 +01:00
|
|
|
#endif
|
2021-02-15 06:11:27 +01:00
|
|
|
if (conn->crdmq < 0)
|
2008-11-28 15:42:52 +01:00
|
|
|
{
|
2021-02-15 06:11:27 +01:00
|
|
|
_NX_SETERRNO(conn->crdmq);
|
|
|
|
gerr("ERROR: _MQ_OPEN(%s) failed: %d\n", climqname,
|
|
|
|
_NX_GETERRNO(conn->crdmq));
|
2008-11-28 15:42:52 +01:00
|
|
|
goto errout_with_conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the server MQ for writing */
|
|
|
|
|
|
|
|
attr.mq_maxmsg = CONFIG_NX_MXSERVERMSGS;
|
|
|
|
attr.mq_msgsize = NX_MXSVRMSGLEN;
|
|
|
|
attr.mq_flags = 0;
|
|
|
|
|
2021-02-15 06:11:27 +01:00
|
|
|
conn->cwrmq = _MQ_OPEN(svrmqname, O_WRONLY | O_CREAT, 0666, &attr);
|
|
|
|
if (conn->cwrmq < 0)
|
2008-11-28 15:42:52 +01:00
|
|
|
{
|
2021-02-15 06:11:27 +01:00
|
|
|
_NX_SETERRNO(conn->cwrmq);
|
|
|
|
gerr("ERROR: _MQ_OPEN(%s) failed: %d\n", svrmqname,
|
|
|
|
_NX_GETERRNO(conn->crdmq));
|
2008-11-28 15:42:52 +01:00
|
|
|
goto errout_with_rmq;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Inform the server that this client exists */
|
|
|
|
|
2012-05-17 21:32:48 +02:00
|
|
|
outmsg.msgid = NX_SVRMSG_CONNECT;
|
|
|
|
outmsg.conn = conn;
|
2008-11-28 15:42:52 +01:00
|
|
|
|
2012-05-17 21:32:48 +02:00
|
|
|
ret = nxmu_sendserver(conn, &outmsg, sizeof(struct nxsvrmsg_s));
|
2008-11-28 15:42:52 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:46:23 +02:00
|
|
|
gerr("ERROR: nxmu_sendserver failed: %d\n", errno);
|
2008-11-28 15:42:52 +01:00
|
|
|
goto errout_with_wmq;
|
|
|
|
}
|
|
|
|
|
2008-11-29 21:25:28 +01:00
|
|
|
#if 0
|
2008-11-28 15:42:52 +01:00
|
|
|
/* Now read until we get a response to this message. The server will
|
|
|
|
* respond with either (1) NX_CLIMSG_CONNECTED, in which case the state
|
|
|
|
* will change to NX_CLISTATE_CONNECTED, or (2) NX_CLIMSG_DISCONNECTED
|
|
|
|
* in which case, nx_message will fail with errno = EHOSTDOWN.
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = nx_eventhandler((NXHANDLE)conn);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-12 17:46:23 +02:00
|
|
|
gerr("ERROR: nx_message failed: %d\n", errno);
|
2008-11-28 15:42:52 +01:00
|
|
|
goto errout_with_wmq;
|
|
|
|
}
|
2017-10-08 20:14:18 +02:00
|
|
|
|
|
|
|
_SIG_USLEEP(300000);
|
2008-11-28 15:42:52 +01:00
|
|
|
}
|
|
|
|
while (conn->state != NX_CLISTATE_CONNECTED);
|
2008-11-29 21:25:28 +01:00
|
|
|
#endif
|
2008-11-28 15:42:52 +01:00
|
|
|
return (NXHANDLE)conn;
|
|
|
|
|
|
|
|
errout_with_wmq:
|
2021-02-15 06:11:27 +01:00
|
|
|
_MQ_CLOSE(conn->cwrmq);
|
2008-11-28 15:42:52 +01:00
|
|
|
errout_with_rmq:
|
2021-02-15 06:11:27 +01:00
|
|
|
_MQ_CLOSE(conn->crdmq);
|
2008-11-28 15:42:52 +01:00
|
|
|
errout_with_conn:
|
2013-12-30 19:42:18 +01:00
|
|
|
lib_ufree(conn);
|
2008-11-28 15:42:52 +01:00
|
|
|
errout:
|
|
|
|
return NULL;
|
|
|
|
}
|