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