1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
25 #ifndef OPENOCD_SERVER_SERVER_H
26 #define OPENOCD_SERVER_SERVER_H
32 #include <helper/log.h>
33 #include <helper/replacements.h>
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
39 enum connection_type {
45 #define CONNECTION_LIMIT_UNLIMITED (-1)
49 int fd_out; /* When using pipes we're writing to a different fd */
50 struct sockaddr_in sin;
51 struct command_context *cmd_ctx;
52 struct service *service;
55 struct connection *next;
58 struct service_driver {
59 /** the name of the server */
61 /** optional minimal setup to accept a connection during keep-alive */
62 int (*new_connection_during_keep_alive_handler)(struct connection *connection);
64 * complete code to accept a new connection.
65 * If 'new_connection_during_keep_alive_handler' above is present, this can be
66 * either called alone during the server_loop, or after the function above.
67 * Check the implementation in gdb_server.
69 int (*new_connection_handler)(struct connection *connection);
70 /** callback to handle incoming data */
71 int (*input_handler)(struct connection *connection);
72 /** callback to tear down the connection */
73 int (*connection_closed_handler)(struct connection *connection);
74 /** called periodically to send keep-alive messages on the connection */
75 void (*keep_client_alive_handler)(struct connection *connection);
80 enum connection_type type;
82 unsigned short portnumber;
84 struct sockaddr_in sin;
86 struct connection *connections;
87 int (*new_connection_during_keep_alive)(struct connection *connection);
88 int (*new_connection)(struct connection *connection);
89 int (*input)(struct connection *connection);
90 int (*connection_closed)(struct connection *connection);
91 void (*keep_client_alive)(struct connection *connection);
96 int add_service(const struct service_driver *driver, const char *port,
97 int max_connections, void *priv);
98 int remove_service(const char *name, const char *port);
100 int server_host_os_entry(void);
101 int server_host_os_close(void);
103 int server_preinit(void);
104 int server_init(struct command_context *cmd_ctx);
105 int server_quit(void);
106 void server_free(void);
107 void exit_on_signal(int sig);
109 void server_keep_clients_alive(void);
111 int server_loop(struct command_context *command_context);
113 int server_register_commands(struct command_context *context);
115 int connection_write(struct connection *connection, const void *data, int len);
116 int connection_read(struct connection *connection, void *data, int len);
119 * Defines an extended command handler function declaration to enable
120 * access to (and manipulation of) the server port number.
121 * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
122 * to receive the specified port number.
124 COMMAND_HELPER(server_pipe_command, char **out);
126 COMMAND_HELPER(server_port_command, unsigned short *out);
128 #define ERROR_SERVER_REMOTE_CLOSED (-400)
129 #define ERROR_CONNECTION_REJECTED (-401)
131 #endif /* OPENOCD_SERVER_SERVER_H */