target: constify structures
[openocd.git] / src / target / openrisc / jsp_server.c
1 /***************************************************************************
2 * Copyright (C) 2014 by Franck Jullien *
3 * franck.jullien@gmail.com *
4 * *
5 * Based on ./src/server/telnet_server.c *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
21 ***************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <server/telnet_server.h>
28
29 #include "or1k_tap.h"
30 #include "or1k_du.h"
31 #include "jsp_server.h"
32
33 static char *jsp_port;
34
35 /**A skim of the relevant RFCs suggests that if my application simply sent the
36 * characters IAC DONT LINEMODE (\377\376\042) as soon as the client connects,
37 * the client should be forced into character mode. However it doesn't make any difference.
38 */
39
40 static const char * const negotiate =
41 "\xFF\xFB\x03" /* IAC WILL Suppress Go Ahead */
42 "\xFF\xFB\x01" /* IAC WILL Echo */
43 "\xFF\xFD\x03" /* IAC DO Suppress Go Ahead */
44 "\xFF\xFE\x01"; /* IAC DON'T Echo */
45
46 /* The only way we can detect that the socket is closed is the first time
47 * we write to it, we will fail. Subsequent write operations will
48 * succeed. Shudder!
49 */
50 static int telnet_write(struct connection *connection, const void *data, int len)
51 {
52 struct telnet_connection *t_con = connection->priv;
53 if (t_con->closed)
54 return ERROR_SERVER_REMOTE_CLOSED;
55
56 if (connection_write(connection, data, len) == len)
57 return ERROR_OK;
58 t_con->closed = 1;
59 return ERROR_SERVER_REMOTE_CLOSED;
60 }
61
62 int jsp_poll_read(void *priv)
63 {
64 struct jsp_service *jsp_service = (struct jsp_service *)priv;
65 unsigned char out_buffer[10];
66 unsigned char in_buffer[10];
67 int out_len = 0;
68 int in_len;
69
70 if (!jsp_service->connection)
71 return ERROR_FAIL;
72
73 memset(out_buffer, 0, 10);
74
75 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info, &out_len, out_buffer, &in_len, in_buffer);
76 if (in_len)
77 telnet_write(jsp_service->connection, in_buffer, in_len);
78
79 return ERROR_OK;
80 }
81
82 static int jsp_new_connection(struct connection *connection)
83 {
84 struct telnet_connection *telnet_connection = malloc(sizeof(struct telnet_connection));
85 struct jsp_service *jsp_service = connection->service->priv;
86
87 connection->priv = telnet_connection;
88
89 /* initialize telnet connection information */
90 telnet_connection->closed = 0;
91 telnet_connection->line_size = 0;
92 telnet_connection->line_cursor = 0;
93 telnet_connection->option_size = 0;
94 telnet_connection->state = TELNET_STATE_DATA;
95
96 /* negotiate telnet options */
97 telnet_write(connection, negotiate, strlen(negotiate));
98
99 /* print connection banner */
100 if (jsp_service->banner) {
101 telnet_write(connection, jsp_service->banner, strlen(jsp_service->banner));
102 telnet_write(connection, "\r\n", 2);
103 }
104
105 jsp_service->connection = connection;
106
107 int retval = target_register_timer_callback(&jsp_poll_read, 1, 1, jsp_service);
108 if (ERROR_OK != retval)
109 return retval;
110
111 return ERROR_OK;
112 }
113
114 static int jsp_input(struct connection *connection)
115 {
116 int bytes_read;
117 unsigned char buffer[TELNET_BUFFER_SIZE];
118 unsigned char *buf_p;
119 struct telnet_connection *t_con = connection->priv;
120 struct jsp_service *jsp_service = connection->service->priv;
121
122 bytes_read = connection_read(connection, buffer, TELNET_BUFFER_SIZE);
123
124 if (bytes_read == 0)
125 return ERROR_SERVER_REMOTE_CLOSED;
126 else if (bytes_read == -1) {
127 LOG_ERROR("error during read: %s", strerror(errno));
128 return ERROR_SERVER_REMOTE_CLOSED;
129 }
130
131 buf_p = buffer;
132 while (bytes_read) {
133 switch (t_con->state) {
134 case TELNET_STATE_DATA:
135 if (*buf_p == 0xff)
136 t_con->state = TELNET_STATE_IAC;
137 else {
138 int out_len = 1;
139 int in_len;
140 unsigned char in_buffer[10];
141 or1k_adv_jtag_jsp_xfer(jsp_service->jtag_info,
142 &out_len, buf_p, &in_len,
143 in_buffer);
144 if (in_len)
145 telnet_write(connection,
146 in_buffer, in_len);
147 }
148 break;
149 case TELNET_STATE_IAC:
150 switch (*buf_p) {
151 case 0xfe:
152 t_con->state = TELNET_STATE_DONT;
153 break;
154 case 0xfd:
155 t_con->state = TELNET_STATE_DO;
156 break;
157 case 0xfc:
158 t_con->state = TELNET_STATE_WONT;
159 break;
160 case 0xfb:
161 t_con->state = TELNET_STATE_WILL;
162 break;
163 }
164 break;
165 case TELNET_STATE_SB:
166 break;
167 case TELNET_STATE_SE:
168 break;
169 case TELNET_STATE_WILL:
170 case TELNET_STATE_WONT:
171 case TELNET_STATE_DO:
172 case TELNET_STATE_DONT:
173 t_con->state = TELNET_STATE_DATA;
174 break;
175 default:
176 LOG_ERROR("unknown telnet state");
177 exit(-1);
178 }
179
180 bytes_read--;
181 buf_p++;
182 }
183
184 return ERROR_OK;
185 }
186
187 static int jsp_connection_closed(struct connection *connection)
188 {
189 struct telnet_connection *t_con = connection->priv;
190 struct jsp_service *jsp_service = connection->service->priv;
191
192 if (t_con->prompt) {
193 free(t_con->prompt);
194 t_con->prompt = NULL;
195 }
196
197 int retval = target_unregister_timer_callback(&jsp_poll_read, jsp_service);
198 if (ERROR_OK != retval)
199 return retval;
200
201 if (connection->priv) {
202 free(connection->priv);
203 connection->priv = NULL;
204 } else
205 LOG_ERROR("BUG: connection->priv == NULL");
206
207 return ERROR_OK;
208 }
209
210 int jsp_init(struct or1k_jtag *jtag_info, char *banner)
211 {
212 struct jsp_service *jsp_service = malloc(sizeof(struct jsp_service));
213 jsp_service->banner = banner;
214 jsp_service->jtag_info = jtag_info;
215
216 return add_service("jsp",
217 jsp_port,
218 1,
219 jsp_new_connection,
220 jsp_input,
221 jsp_connection_closed,
222 jsp_service);
223 }
224
225 COMMAND_HANDLER(handle_jsp_port_command)
226 {
227 return CALL_COMMAND_HANDLER(server_pipe_command, &jsp_port);
228 }
229
230 static const struct command_registration jsp_command_handlers[] = {
231 {
232 .name = "jsp_port",
233 .handler = handle_jsp_port_command,
234 .mode = COMMAND_ANY,
235 .help = "Specify port on which to listen "
236 "for incoming JSP telnet connections.",
237 .usage = "[port_num]",
238 },
239 COMMAND_REGISTRATION_DONE
240 };
241
242 int jsp_register_commands(struct command_context *cmd_ctx)
243 {
244 jsp_port = strdup("7777");
245 return register_commands(cmd_ctx, NULL, jsp_command_handlers);
246 }
247