nuttx/drivers/video/vnc/vnc_server.c

374 lines
10 KiB
C
Raw Normal View History

2016-04-14 19:09:18 +02:00
/****************************************************************************
* drivers/video/vnc/vnc_server.c
2016-04-14 19:09:18 +02: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
2016-04-14 19:09:18 +02:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2016-04-14 19:09:18 +02: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.
2016-04-14 19:09:18 +02:00
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include "nuttx/config.h"
#include <stdint.h>
2016-04-14 19:09:18 +02:00
#include <stdlib.h>
#include <string.h>
2016-04-14 19:09:18 +02:00
#include <assert.h>
#include <errno.h>
#if defined(CONFIG_VNCSERVER_DEBUG) && !defined(CONFIG_DEBUG_GRAPHICS)
# undef CONFIG_DEBUG_ERROR
# undef CONFIG_DEBUG_WARN
# undef CONFIG_DEBUG_INFO
# undef CONFIG_DEBUG_GRAPHICS_ERROR
# undef CONFIG_DEBUG_GRAPHICS_WARN
# undef CONFIG_DEBUG_GRAPHICS_INFO
# define CONFIG_DEBUG_ERROR 1
# define CONFIG_DEBUG_WARN 1
# define CONFIG_DEBUG_INFO 1
# define CONFIG_DEBUG_GRAPHICS 1
# define CONFIG_DEBUG_GRAPHICS_ERROR 1
# define CONFIG_DEBUG_GRAPHICS_WARN 1
# define CONFIG_DEBUG_GRAPHICS_INFO 1
#endif
2016-04-14 19:09:18 +02:00
#include <debug.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <nuttx/kmalloc.h>
#include <nuttx/queue.h>
#include <nuttx/net/net.h>
2016-04-14 19:09:18 +02:00
#include "vnc_server.h"
/****************************************************************************
* Public Data
****************************************************************************/
/* Given a display number as an index, the following array can be used to
* look-up the session structure for that display.
*/
FAR struct vnc_session_s *g_vnc_sessions[RFB_MAX_DISPLAYS];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: vnc_reset_session
*
* Description:
* Conclude the current VNC session. This function re-initializes the
* session structure; it does not free either the session structure nor
* the framebuffer so that they may be re-used.
*
* Input Parameters:
* session - An instance of the session structure.
*
* Returned Value:
* None
*
****************************************************************************/
static void vnc_reset_session(FAR struct vnc_session_s *session,
FAR uint8_t *fb, int display)
{
2016-04-18 00:48:30 +02:00
int i;
/* Close any open sockets */
if (session->state >= VNCSERVER_CONNECTED)
{
psock_close(&session->connect);
psock_close(&session->listen);
}
2016-04-18 00:48:30 +02:00
/* [Re-]initialize the session. */
memset(&session->connect, 0, sizeof(struct socket));
memset(&session->listen, 0, sizeof(struct socket));
2016-04-18 00:48:30 +02:00
/* Put all of the pre-allocated update structures into the freelist */
sq_init(&session->updqueue);
sq_init(&session->updfree);
2016-04-18 00:48:30 +02:00
for (i = 0; i < CONFIG_VNCSERVER_NUPDATES; i++)
2016-04-18 00:48:30 +02:00
{
sq_addlast((FAR sq_entry_t *)&session->updpool[i], &session->updfree);
2016-04-18 00:48:30 +02:00
}
2016-04-18 00:48:30 +02:00
/* Set the INITIALIZED state */
nxsem_reset(&session->freesem, CONFIG_VNCSERVER_NUPDATES);
nxsem_reset(&session->queuesem, 0);
session->fb = fb;
session->display = display;
session->state = VNCSERVER_INITIALIZED;
session->nwhupd = 0;
session->change = true;
#ifdef CONFIG_VNCSERVER_TOUCH
session->touch.maxpoint = 1;
#endif
/* Careful not to disturb the keyboard/mouse callouts set by
* vnc_fbinitialize(). Client related data left in garbage state.
*/
}
/****************************************************************************
* Name: vnc_connect
*
* Description:
* Wait for a connection from the VNC client
*
* Input Parameters:
* session - An instance of the session structure.
* port - The listen port to use
*
* Returned Value:
* Returns zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
static int vnc_connect(FAR struct vnc_session_s *session, int port)
{
struct sockaddr_in addr;
int ret;
ginfo("Connecting display %d\n", session->display);
/* Create a listening socket */
addr.sin_family = AF_INET;
addr.sin_port = HTONS(port);
addr.sin_addr.s_addr = INADDR_ANY;
ret = psock_socket(AF_INET, SOCK_STREAM, 0, &session->listen);
if (ret < 0)
{
return ret;
}
/* Bind the listening socket to a local address */
ret = psock_bind(&session->listen, (struct sockaddr *)&addr,
sizeof(struct sockaddr_in));
if (ret < 0)
{
goto errout_with_listener;
}
/* Listen for a connection */
ret = psock_listen(&session->listen, 5);
if (ret < 0)
{
goto errout_with_listener;
}
/* Connect to the client */
ginfo("Accepting connection for Display %d\n", session->display);
ret = psock_accept(&session->listen, NULL, NULL, &session->connect, 0);
if (ret < 0)
{
goto errout_with_listener;
}
ginfo("Display %d connected\n", session->display);
session->state = VNCSERVER_CONNECTED;
return OK;
errout_with_listener:
psock_close(&session->listen);
return ret;
}
2016-04-14 19:09:18 +02:00
/****************************************************************************
* Pubic Functions
****************************************************************************/
/****************************************************************************
* Name: vnc_server
*
* Description:
* The VNC server daemon. This daemon is implemented as a kernel thread.
*
* Input Parameters:
* Standard kernel thread arguments (all ignored)
*
* Returned Value:
* This function does not return.
*
****************************************************************************/
int vnc_server(int argc, FAR char *argv[])
{
FAR struct vnc_session_s *session;
FAR uint8_t *fb;
2016-04-14 19:09:18 +02:00
int display;
int ret;
/* A single argument is expected: A display port number in ASCII form */
2016-04-14 19:09:18 +02:00
if (argc != 2)
{
2016-06-17 16:43:22 +02:00
/* In this case the start-up logic will probably hang, waiting for the
* display-related semaphore to be set.
*/
gerr("ERROR: Unexpected number of arguments: %d\n", argc);
ret = -EINVAL;
2016-06-17 16:43:22 +02:00
goto errout_with_hang;
2016-04-14 19:09:18 +02:00
}
display = atoi(argv[1]);
if (display < 0 || display >= RFB_MAX_DISPLAYS)
{
2016-06-17 16:43:22 +02:00
/* In this case the start-up logic will probably hang, waiting for the
* display-related semaphore to be set.
*/
gerr("ERROR: Invalid display number: %d\n", display);
ret = -EINVAL;
2016-06-17 16:43:22 +02:00
goto errout_with_hang;
2016-04-14 19:09:18 +02:00
}
ginfo("Server started for Display %d\n", display);
/* Allocate the framebuffer memory. We rely on the fact that
* the KMM allocator will align memory to 32-bits or better.
*/
fb = kmm_zalloc(RFB_SIZE);
if (fb == NULL)
{
gerr("ERROR: Failed to allocate framebuffer memory: %lu KB\n",
(unsigned long)(RFB_SIZE / 1024));
ret = -ENOMEM;
goto errout_with_post;
}
2016-04-14 19:09:18 +02:00
/* Allocate a session structure for this display */
session = kmm_zalloc(sizeof(struct vnc_session_s));
2016-04-14 19:09:18 +02:00
if (session == NULL)
{
gerr("ERROR: Failed to allocate session\n");
ret = -ENOMEM;
goto errout_with_fb;
2016-04-14 19:09:18 +02:00
}
g_vnc_sessions[display] = session;
nxsem_init(&session->freesem, 0, CONFIG_VNCSERVER_NUPDATES);
nxsem_init(&session->queuesem, 0, 0);
#ifdef CONFIG_FB_SYNC
nxsem_init(&session->vsyncsem, 0, 0);
#endif
/* Inform any waiter that we have started */
vnc_reset_session(session, fb, display);
nxsem_post(&g_fbstartup[display].fbinit);
2016-04-14 19:09:18 +02:00
/* Loop... handling each each VNC client connection to this display. Only
* a single client is allowed for each display.
*/
for (; ; )
{
/* Release the last session and [Re-]initialize the session structure
* for the next connection.
*/
vnc_reset_session(session, fb, display);
g_fbstartup[display].result = -EBUSY;
2016-04-14 19:09:18 +02:00
/* Establish a connection with the VNC client */
ret = vnc_connect(session, RFB_DISPLAY_PORT(display));
if (ret >= 0)
{
ginfo("New VNC connection\n");
2016-04-14 19:09:18 +02:00
2016-04-17 01:48:15 +02:00
/* Perform the VNC initialization sequence after the client has
2018-09-14 14:55:45 +02:00
* successfully connected to the server. Negotiate security,
2016-04-17 01:48:15 +02:00
* framebuffer and color properties.
*/
ret = vnc_negotiate(session);
if (ret < 0)
{
gerr("ERROR: Failed to negotiate security/framebuffer: %d\n",
ret);
continue;
}
/* Start the VNC updater thread that sends all Server-to-Client
* messages.
*/
ret = vnc_start_updater(session);
if (ret < 0)
{
gerr("ERROR: Failed to start updater thread: %d\n", ret);
continue;
}
2016-04-14 19:09:18 +02:00
/* Let the framebuffer driver know that we are ready to perform
* updates.
*/
g_fbstartup[display].result = OK;
/* Run the VNC receiver on this trhead. The VNC receiver handles
* all Client-to-Server messages. The VNC receiver function does
* not return until the session has been terminated (or an error
* occurs).
*/
ret = vnc_receiver(session);
ginfo("Session terminated with %d\n", ret);
UNUSED(ret);
2016-04-14 19:09:18 +02:00
/* Stop the VNC updater thread. */
ret = vnc_stop_updater(session);
if (ret < 0)
{
gerr("ERROR: Failed to stop updater thread: %d\n", ret);
}
}
2016-04-14 19:09:18 +02:00
}
errout_with_fb:
kmm_free(fb);
errout_with_post:
g_fbstartup[display].result = ret;
2016-06-17 16:43:22 +02:00
errout_with_hang:
return EXIT_FAILURE;
2016-04-14 19:09:18 +02:00
}