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