OpenOCD
rtt_server.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Copyright (C) 2016-2017 by Marc Schink <dev@zapb.de>
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <stdint.h>
12 #include <rtt/rtt.h>
13 
14 #include "server.h"
15 #include "rtt_server.h"
16 
26 struct rtt_service {
27  unsigned int channel;
28 };
29 
30 static int read_callback(unsigned int channel, const uint8_t *buffer,
31  size_t length, void *user_data)
32 {
33  int ret;
34  struct connection *connection;
35  size_t offset;
36 
37  connection = (struct connection *)user_data;
38  offset = 0;
39 
40  while (offset < length) {
42 
43  if (ret < 0) {
44  LOG_ERROR("Failed to write data to socket.");
45  return ERROR_FAIL;
46  }
47 
48  offset += ret;
49  }
50 
51  return ERROR_OK;
52 }
53 
55 {
56  int ret;
57  struct rtt_service *service;
58 
60 
61  LOG_DEBUG("rtt: New connection for channel %u", service->channel);
62 
64 
65  if (ret != ERROR_OK)
66  return ret;
67 
68  return ERROR_OK;
69 }
70 
72 {
73  struct rtt_service *service;
74 
77 
78  LOG_DEBUG("rtt: Connection for channel %u closed", service->channel);
79 
80  return ERROR_OK;
81 }
82 
83 static int rtt_input(struct connection *connection)
84 {
85  int bytes_read;
86  unsigned char buffer[1024];
87  struct rtt_service *service;
88  size_t length;
89 
91  bytes_read = connection_read(connection, buffer, sizeof(buffer));
92 
93  if (!bytes_read)
95  else if (bytes_read < 0) {
96  LOG_ERROR("error during read: %s", strerror(errno));
98  }
99 
100  length = bytes_read;
101  rtt_write_channel(service->channel, buffer, &length);
102 
103  return ERROR_OK;
104 }
105 
106 static const struct service_driver rtt_service_driver = {
107  .name = "rtt",
108  .new_connection_during_keep_alive_handler = NULL,
109  .new_connection_handler = rtt_new_connection,
110  .input_handler = rtt_input,
111  .connection_closed_handler = rtt_connection_closed,
112  .keep_client_alive_handler = NULL,
113 };
114 
115 COMMAND_HANDLER(handle_rtt_start_command)
116 {
117  int ret;
118  struct rtt_service *service;
119 
120  if (CMD_ARGC != 2)
122 
123  service = malloc(sizeof(struct rtt_service));
124 
125  if (!service)
126  return ERROR_FAIL;
127 
128  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], service->channel);
129 
131 
132  if (ret != ERROR_OK) {
133  free(service);
134  return ERROR_FAIL;
135  }
136 
137  return ERROR_OK;
138 }
139 
140 COMMAND_HANDLER(handle_rtt_stop_command)
141 {
142  if (CMD_ARGC != 1)
144 
145  remove_service("rtt", CMD_ARGV[0]);
146 
147  return ERROR_OK;
148 }
149 
151  {
152  .name = "start",
153  .handler = handle_rtt_start_command,
154  .mode = COMMAND_ANY,
155  .help = "Start a RTT server",
156  .usage = "<port> <channel>"
157  },
158  {
159  .name = "stop",
160  .handler = handle_rtt_stop_command,
161  .mode = COMMAND_ANY,
162  .help = "Stop a RTT server",
163  .usage = "<port>"
164  },
166 };
167 
168 static const struct command_registration rtt_server_command_handlers[] = {
169  {
170  .name = "server",
171  .mode = COMMAND_ANY,
172  .help = "RTT server",
173  .usage = "",
175  },
177 };
178 
179 static const struct command_registration rtt_command_handlers[] = {
180  {
181  .name = "rtt",
182  .mode = COMMAND_ANY,
183  .help = "RTT",
184  .usage = "",
186  },
188 };
189 
191 {
193 }
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:425
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:268
@ COMMAND_ANY
Definition: command.h:42
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
int rtt_register_sink(unsigned int channel_index, rtt_sink_read read, void *user_data)
Register an RTT sink.
Definition: rtt/rtt.c:206
int rtt_unregister_sink(unsigned int channel_index, rtt_sink_read read, void *user_data)
Unregister an RTT sink.
Definition: rtt/rtt.c:232
int rtt_write_channel(unsigned int channel_index, const uint8_t *buffer, size_t *length)
Write to an RTT channel.
Definition: rtt/rtt.c:288
static int rtt_connection_closed(struct connection *connection)
Definition: rtt_server.c:71
static const struct command_registration rtt_command_handlers[]
Definition: rtt_server.c:179
static int rtt_new_connection(struct connection *connection)
Definition: rtt_server.c:54
static int rtt_input(struct connection *connection)
Definition: rtt_server.c:83
COMMAND_HANDLER(handle_rtt_start_command)
Definition: rtt_server.c:115
static int read_callback(unsigned int channel, const uint8_t *buffer, size_t length, void *user_data)
Definition: rtt_server.c:30
static const struct command_registration rtt_server_subcommand_handlers[]
Definition: rtt_server.c:150
int rtt_server_register_commands(struct command_context *ctx)
Definition: rtt_server.c:190
static const struct command_registration rtt_server_command_handlers[]
Definition: rtt_server.c:168
static const struct service_driver rtt_service_driver
Definition: rtt_server.c:106
int connection_write(struct connection *connection, const void *data, int len)
Definition: server.c:730
int connection_read(struct connection *connection, void *data, int len)
Definition: server.c:742
int remove_service(const char *name, const char *port)
Definition: server.c:354
int add_service(const struct service_driver *driver, const char *port, int max_connections, void *priv)
Definition: server.c:197
#define CONNECTION_LIMIT_UNLIMITED
Definition: server.h:34
#define ERROR_SERVER_REMOTE_CLOSED
Definition: server.h:119
const char * name
Definition: command.h:229
struct service * service
Definition: server.h:41
unsigned int channel
Definition: rtt_server.c:27
const char * name
the name of the server
Definition: server.h:49
Definition: server.h:67
void * priv
Definition: server.h:81
#define NULL
Definition: usb.h:16
uint8_t offset[4]
Definition: vdebug.c:9