]> git.sur5r.net Git - openocd/blob - src/server/tcl_server.c
server: tcl_notifications command
[openocd] / src / server / tcl_server.c
1 /***************************************************************************
2  *   Copyright (C) 2010 Ã˜yvind Harboe                                      *
3  *   oyvind.harboe@zylin.com                                               *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
19  ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "tcl_server.h"
26 #include <target/target.h>
27
28 #define TCL_SERVER_VERSION              "TCL Server 0.1"
29 #define TCL_MAX_LINE                    (4096)
30
31 struct tcl_connection {
32         int tc_linedrop;
33         int tc_lineoffset;
34         char tc_line[TCL_MAX_LINE];
35         int tc_outerror;/* flag an output error */
36         enum target_state tc_laststate;
37         bool tc_notify;
38 };
39
40 static char *tcl_port;
41
42 /* handlers */
43 static int tcl_new_connection(struct connection *connection);
44 static int tcl_input(struct connection *connection);
45 static int tcl_output(struct connection *connection, const void *buf, ssize_t len);
46 static int tcl_closed(struct connection *connection);
47
48 static int tcl_target_callback_event_handler(struct target *target,
49                 enum target_event event, void *priv)
50 {
51         struct connection *connection = priv;
52         struct tcl_connection *tclc;
53         char buf[256];
54
55         tclc = connection->priv;
56
57         if (tclc->tc_notify) {
58                 snprintf(buf, sizeof(buf), "type target_event event %s\r\n\x1a", target_event_name(event));
59                 tcl_output(connection, buf, strlen(buf));
60         }
61
62         if (tclc->tc_laststate != target->state) {
63                 tclc->tc_laststate = target->state;
64                 if (tclc->tc_notify) {
65                         snprintf(buf, sizeof(buf), "type target_state state %s\r\n\x1a", target_state_name(target));
66                         tcl_output(connection, buf, strlen(buf));
67                 }
68         }
69
70         return ERROR_OK;
71 }
72
73 static int tcl_target_callback_reset_handler(struct target *target,
74                 enum target_reset_mode reset_mode, void *priv)
75 {
76         struct connection *connection = priv;
77         struct tcl_connection *tclc;
78         char buf[256];
79
80         tclc = connection->priv;
81
82         if (tclc->tc_notify) {
83                 snprintf(buf, sizeof(buf), "type target_reset mode %s\r\n\x1a", target_reset_mode_name(reset_mode));
84                 tcl_output(connection, buf, strlen(buf));
85         }
86
87         return ERROR_OK;
88 }
89
90 /* write data out to a socket.
91  *
92  * this is a blocking write, so the return value must equal the length, if
93  * that is not the case then flag the connection with an output error.
94  */
95 int tcl_output(struct connection *connection, const void *data, ssize_t len)
96 {
97         ssize_t wlen;
98         struct tcl_connection *tclc;
99
100         tclc = connection->priv;
101         if (tclc->tc_outerror)
102                 return ERROR_SERVER_REMOTE_CLOSED;
103
104         wlen = connection_write(connection, data, len);
105
106         if (wlen == len)
107                 return ERROR_OK;
108
109         LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
110         tclc->tc_outerror = 1;
111         return ERROR_SERVER_REMOTE_CLOSED;
112 }
113
114 /* connections */
115 static int tcl_new_connection(struct connection *connection)
116 {
117         struct tcl_connection *tclc;
118
119         tclc = malloc(sizeof(struct tcl_connection));
120         if (tclc == NULL)
121                 return ERROR_CONNECTION_REJECTED;
122
123         memset(tclc, 0, sizeof(struct tcl_connection));
124         connection->priv = tclc;
125
126         struct target *target = get_current_target(connection->cmd_ctx);
127         if (target != NULL)
128                 tclc->tc_laststate = target->state;
129
130         /* store the connection object on cmd_ctx so we can access it from command handlers */
131         connection->cmd_ctx->output_handler_priv = connection;
132
133         target_register_event_callback(tcl_target_callback_event_handler, connection);
134         target_register_reset_callback(tcl_target_callback_reset_handler, connection);
135
136         return ERROR_OK;
137 }
138
139 static int tcl_input(struct connection *connection)
140 {
141         Jim_Interp *interp = (Jim_Interp *)connection->cmd_ctx->interp;
142         int retval;
143         int i;
144         ssize_t rlen;
145         const char *result;
146         int reslen;
147         struct tcl_connection *tclc;
148         unsigned char in[256];
149
150         rlen = connection_read(connection, &in, sizeof(in));
151         if (rlen <= 0) {
152                 if (rlen < 0)
153                         LOG_ERROR("error during read: %s", strerror(errno));
154                 return ERROR_SERVER_REMOTE_CLOSED;
155         }
156
157         tclc = connection->priv;
158         if (tclc == NULL)
159                 return ERROR_CONNECTION_REJECTED;
160
161         /* push as much data into the line as possible */
162         for (i = 0; i < rlen; i++) {
163                 /* buffer the data */
164                 tclc->tc_line[tclc->tc_lineoffset] = in[i];
165                 if (tclc->tc_lineoffset < TCL_MAX_LINE)
166                         tclc->tc_lineoffset++;
167                 else
168                         tclc->tc_linedrop = 1;
169
170                 /* ctrl-z is end of command. When testing from telnet, just
171                  * press ctrl-z a couple of times first to put telnet into the
172                  * mode where it will send 0x1a in response to pressing ctrl-z
173                  */
174                 if (in[i] != '\x1a')
175                         continue;
176
177                 /* process the line */
178                 if (tclc->tc_linedrop) {
179 #define ESTR "line too long\n"
180                         retval = tcl_output(connection, ESTR, sizeof(ESTR));
181                         if (retval != ERROR_OK)
182                                 return retval;
183 #undef ESTR
184                 } else {
185                         tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
186                         retval = command_run_line(connection->cmd_ctx, tclc->tc_line);
187                         result = Jim_GetString(Jim_GetResult(interp), &reslen);
188                         retval = tcl_output(connection, result, reslen);
189                         if (retval != ERROR_OK)
190                                 return retval;
191                         /* Always output ctrl-d as end of line to allow multiline results */
192                         tcl_output(connection, "\x1a", 1);
193                 }
194
195                 tclc->tc_lineoffset = 0;
196                 tclc->tc_linedrop = 0;
197         }
198
199         return ERROR_OK;
200 }
201
202 static int tcl_closed(struct connection *connection)
203 {
204         /* cleanup connection context */
205         if (connection->priv) {
206                 free(connection->priv);
207                 connection->priv = NULL;
208         }
209
210         target_unregister_event_callback(tcl_target_callback_event_handler, connection);
211         target_unregister_reset_callback(tcl_target_callback_reset_handler, connection);
212
213         return ERROR_OK;
214 }
215
216 int tcl_init(void)
217 {
218         if (strcmp(tcl_port, "disabled") == 0) {
219                 LOG_INFO("tcl server disabled");
220                 return ERROR_OK;
221         }
222
223         return add_service("tcl", tcl_port, 1,
224                 &tcl_new_connection, &tcl_input,
225                 &tcl_closed, NULL);
226 }
227
228 COMMAND_HANDLER(handle_tcl_port_command)
229 {
230         return CALL_COMMAND_HANDLER(server_pipe_command, &tcl_port);
231 }
232
233 COMMAND_HANDLER(handle_tcl_notifications_command)
234 {
235         struct connection *connection = NULL;
236         struct tcl_connection *tclc = NULL;
237
238         if (CMD_CTX->output_handler_priv != NULL)
239                 connection = CMD_CTX->output_handler_priv;
240
241         if (connection != NULL && !strcmp(connection->service->name, "tcl")) {
242                 tclc = connection->priv;
243                 return CALL_COMMAND_HANDLER(handle_command_parse_bool, &tclc->tc_notify, "Target Notification output is");
244         } else {
245                 LOG_ERROR("%s: can only be called from the tcl server", CMD_NAME);
246                 return ERROR_COMMAND_SYNTAX_ERROR;
247         }
248 }
249
250 static const struct command_registration tcl_command_handlers[] = {
251         {
252                 .name = "tcl_port",
253                 .handler = handle_tcl_port_command,
254                 .mode = COMMAND_ANY,
255                 .help = "Specify port on which to listen "
256                         "for incoming Tcl syntax.  "
257                         "Read help on 'gdb_port'.",
258                 .usage = "[port_num]",
259         },
260         {
261                 .name = "tcl_notifications",
262                 .handler = handle_tcl_notifications_command,
263                 .mode = COMMAND_EXEC,
264                 .help = "Target Notification output",
265                 .usage = "[on|off]",
266         },
267         COMMAND_REGISTRATION_DONE
268 };
269
270 int tcl_register_commands(struct command_context *cmd_ctx)
271 {
272         tcl_port = strdup("6666");
273         return register_commands(cmd_ctx, NULL, tcl_command_handlers);
274 }