]> git.sur5r.net Git - openocd/blob - src/server/server.h
c6a15bc297f056b555efae1295c7a4c24b5dc220
[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 SERVER_H
26 #define SERVER_H
27
28 #include <helper/log.h>
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33
34 enum connection_type {
35         CONNECTION_TCP,
36         CONNECTION_PIPE,
37         CONNECTION_STDINOUT
38 };
39
40 #define CONNECTION_LIMIT_UNLIMITED              (-1)
41
42 struct connection {
43         int fd;
44         int fd_out;     /* When using pipes we're writing to a different fd */
45         struct sockaddr_in sin;
46         struct command_context *cmd_ctx;
47         struct service *service;
48         int input_pending;
49         void *priv;
50         struct connection *next;
51 };
52
53 typedef int (*new_connection_handler_t)(struct connection *connection);
54 typedef int (*input_handler_t)(struct connection *connection);
55 typedef int (*connection_closed_handler_t)(struct connection *connection);
56
57 struct service {
58         char *name;
59         enum connection_type type;
60         char *port;
61         unsigned short portnumber;
62         int fd;
63         struct sockaddr_in sin;
64         int max_connections;
65         struct connection *connections;
66         new_connection_handler_t new_connection;
67         input_handler_t input;
68         connection_closed_handler_t connection_closed;
69         void *priv;
70         struct service *next;
71 };
72
73 int add_service(char *name, const char *port,
74                 int max_connections, new_connection_handler_t new_connection_handler,
75                 input_handler_t in_handler, connection_closed_handler_t close_handler,
76                 void *priv);
77
78 int server_preinit(void);
79 int server_init(struct command_context *cmd_ctx);
80 int server_quit(void);
81 void exit_on_signal(int);
82
83 int server_loop(struct command_context *command_context);
84
85 int server_register_commands(struct command_context *context);
86
87 int connection_write(struct connection *connection, const void *data, int len);
88 int connection_read(struct connection *connection, void *data, int len);
89
90 /**
91  * Used by server_loop(), defined in server_stubs.c
92  */
93 void openocd_sleep_prelude(void);
94 /**
95  * Used by server_loop(), defined in server_stubs.c
96  */
97 void openocd_sleep_postlude(void);
98
99 /**
100  * Defines an extended command handler function declaration to enable
101  * access to (and manipulation of) the server port number.
102  * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
103  * to receive the specified port number.
104  */
105 COMMAND_HELPER(server_pipe_command, char **out);
106
107 COMMAND_HELPER(server_port_command, unsigned short *out);
108
109 #define ERROR_SERVER_REMOTE_CLOSED              (-400)
110 #define ERROR_CONNECTION_REJECTED               (-401)
111
112 #endif  /* SERVER_H */