Cleanup of config/includes.
[fw/openocd] / src / server / server.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
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.                                   *
15  *                                                                         *
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.                          *
20  *                                                                         *
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  ***************************************************************************/
24
25 #ifndef OPENOCD_SERVER_SERVER_H
26 #define OPENOCD_SERVER_SERVER_H
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <helper/log.h>
33 #include <helper/replacements.h>
34
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38
39 enum connection_type {
40         CONNECTION_TCP,
41         CONNECTION_PIPE,
42         CONNECTION_STDINOUT
43 };
44
45 #define CONNECTION_LIMIT_UNLIMITED              (-1)
46
47 struct connection {
48         int fd;
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;
53         bool input_pending;
54         void *priv;
55         struct connection *next;
56 };
57
58 typedef int (*new_connection_handler_t)(struct connection *connection);
59 typedef int (*input_handler_t)(struct connection *connection);
60 typedef int (*connection_closed_handler_t)(struct connection *connection);
61
62 struct service {
63         char *name;
64         enum connection_type type;
65         char *port;
66         unsigned short portnumber;
67         int fd;
68         struct sockaddr_in sin;
69         int max_connections;
70         struct connection *connections;
71         new_connection_handler_t new_connection;
72         input_handler_t input;
73         connection_closed_handler_t connection_closed;
74         void *priv;
75         struct service *next;
76 };
77
78 int add_service(char *name, const char *port,
79                 int max_connections, new_connection_handler_t new_connection_handler,
80                 input_handler_t in_handler, connection_closed_handler_t close_handler,
81                 void *priv);
82 int remove_service(const char *name, const char *port);
83
84 int server_host_os_entry(void);
85 int server_host_os_close(void);
86
87 int server_preinit(void);
88 int server_init(struct command_context *cmd_ctx);
89 int server_quit(void);
90 void server_free(void);
91 void exit_on_signal(int sig);
92
93 int server_loop(struct command_context *command_context);
94
95 int server_register_commands(struct command_context *context);
96
97 int connection_write(struct connection *connection, const void *data, int len);
98 int connection_read(struct connection *connection, void *data, int len);
99
100 /**
101  * Used by server_loop(), defined in server_stubs.c
102  */
103 void openocd_sleep_prelude(void);
104 /**
105  * Used by server_loop(), defined in server_stubs.c
106  */
107 void openocd_sleep_postlude(void);
108
109 /**
110  * Defines an extended command handler function declaration to enable
111  * access to (and manipulation of) the server port number.
112  * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
113  * to receive the specified port number.
114  */
115 COMMAND_HELPER(server_pipe_command, char **out);
116
117 COMMAND_HELPER(server_port_command, unsigned short *out);
118
119 #define ERROR_SERVER_REMOTE_CLOSED              (-400)
120 #define ERROR_CONNECTION_REJECTED               (-401)
121
122 #endif /* OPENOCD_SERVER_SERVER_H */