+static int tcl_new_connection(struct connection *connection);
+static int tcl_input(struct connection *connection);
+static int tcl_output(struct connection *connection, const void *buf, ssize_t len);
+static int tcl_closed(struct connection *connection);
+
+static int tcl_target_callback_event_handler(struct target *target,
+ enum target_event event, void *priv)
+{
+ struct connection *connection = priv;
+ struct tcl_connection *tclc;
+ char buf[256];
+
+ tclc = connection->priv;
+
+ if (tclc->tc_notify) {
+ snprintf(buf, sizeof(buf), "type target_event event %s\r\n\x1a", target_event_name(event));
+ tcl_output(connection, buf, strlen(buf));
+ }
+
+ if (tclc->tc_laststate != target->state) {
+ tclc->tc_laststate = target->state;
+ if (tclc->tc_notify) {
+ snprintf(buf, sizeof(buf), "type target_state state %s\r\n\x1a", target_state_name(target));
+ tcl_output(connection, buf, strlen(buf));
+ }
+ }
+
+ return ERROR_OK;
+}
+
+static int tcl_target_callback_reset_handler(struct target *target,
+ enum target_reset_mode reset_mode, void *priv)
+{
+ struct connection *connection = priv;
+ struct tcl_connection *tclc;
+ char buf[256];
+
+ tclc = connection->priv;
+
+ if (tclc->tc_notify) {
+ snprintf(buf, sizeof(buf), "type target_reset mode %s\r\n\x1a", target_reset_mode_name(reset_mode));
+ tcl_output(connection, buf, strlen(buf));
+ }
+
+ return ERROR_OK;
+}
+
+static int tcl_target_callback_trace_handler(struct target *target,
+ size_t len, uint8_t *data, void *priv)
+{
+ struct connection *connection = priv;
+ struct tcl_connection *tclc;
+ char *header = "type target_trace data ";
+ char *trailer = "\r\n\x1a";
+ size_t hex_len = len * 2 + 1;
+ size_t max_len = hex_len + strlen(header) + strlen(trailer);
+ char *buf, *hex;
+
+ tclc = connection->priv;
+
+ if (tclc->tc_trace) {
+ hex = malloc(hex_len);
+ buf = malloc(max_len);
+ hexify(hex, data, len, hex_len);
+ snprintf(buf, max_len, "%s%s%s", header, hex, trailer);
+ tcl_output(connection, buf, strlen(buf));
+ free(hex);
+ free(buf);
+ }
+
+ return ERROR_OK;
+}