server: add function to get openocd shutdown status
[fw/openocd] / src / server / server.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2005 by Dominic Rath                                    *
5  *   Dominic.Rath@gmx.de                                                   *
6  *                                                                         *
7  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
8  *   oyvind.harboe@zylin.com                                               *
9  *                                                                         *
10  *   Copyright (C) 2008 by Spencer Oliver                                  *
11  *   spen@spen-soft.co.uk                                                  *
12  ***************************************************************************/
13
14 #ifndef OPENOCD_SERVER_SERVER_H
15 #define OPENOCD_SERVER_SERVER_H
16
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <helper/log.h>
22 #include <helper/replacements.h>
23
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
26 #endif
27
28 enum connection_type {
29         CONNECTION_TCP,
30         CONNECTION_PIPE,
31         CONNECTION_STDINOUT
32 };
33
34 #define CONNECTION_LIMIT_UNLIMITED              (-1)
35
36 struct connection {
37         int fd;
38         int fd_out;     /* When using pipes we're writing to a different fd */
39         struct sockaddr_in sin;
40         struct command_context *cmd_ctx;
41         struct service *service;
42         bool input_pending;
43         void *priv;
44         struct connection *next;
45 };
46
47 struct service_driver {
48         /** the name of the server */
49         const char *name;
50         /** optional minimal setup to accept a connection during keep-alive */
51         int (*new_connection_during_keep_alive_handler)(struct connection *connection);
52         /**
53          * complete code to accept a new connection.
54          * If 'new_connection_during_keep_alive_handler' above is present, this can be
55          * either called alone during the server_loop, or after the function above.
56          * Check the implementation in gdb_server.
57          * */
58         int (*new_connection_handler)(struct connection *connection);
59         /** callback to handle incoming data */
60         int (*input_handler)(struct connection *connection);
61         /** callback to tear down the connection */
62         int (*connection_closed_handler)(struct connection *connection);
63         /** called periodically to send keep-alive messages on the connection */
64         void (*keep_client_alive_handler)(struct connection *connection);
65 };
66
67 struct service {
68         char *name;
69         enum connection_type type;
70         char *port;
71         unsigned short portnumber;
72         int fd;
73         struct sockaddr_in sin;
74         int max_connections;
75         struct connection *connections;
76         int (*new_connection_during_keep_alive)(struct connection *connection);
77         int (*new_connection)(struct connection *connection);
78         int (*input)(struct connection *connection);
79         int (*connection_closed)(struct connection *connection);
80         void (*keep_client_alive)(struct connection *connection);
81         void *priv;
82         struct service *next;
83 };
84
85 int add_service(const struct service_driver *driver, const char *port,
86                 int max_connections, void *priv);
87 int remove_service(const char *name, const char *port);
88
89 int server_host_os_entry(void);
90 int server_host_os_close(void);
91
92 int server_preinit(void);
93 int server_init(struct command_context *cmd_ctx);
94 int server_quit(void);
95 void server_free(void);
96 void exit_on_signal(int sig);
97
98 void server_keep_clients_alive(void);
99
100 int server_loop(struct command_context *command_context);
101
102 int server_register_commands(struct command_context *context);
103
104 int connection_write(struct connection *connection, const void *data, int len);
105 int connection_read(struct connection *connection, void *data, int len);
106
107 bool openocd_is_shutdown_pending(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 */