remote_bitbang: missed closing brace in macro REMOTE_BITBANG_RAISE_ERROR
[openocd/cmsis-dap.git] / src / jtag / drivers / remote_bitbang.c
blob941a07f147857ab2c71e7afa6c0ba21c42eb9cfe
1 /***************************************************************************
2 * Copyright (C) 2011 by Richard Uhler *
3 * ruhler@mit.edu *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <netdb.h>
28 #include <jtag/interface.h>
29 #include "bitbang.h"
31 /* from unix man page and sys/un.h: */
32 #define UNIX_PATH_MAX 108
34 /* arbitrary limit on host name length: */
35 #define REMOTE_BITBANG_HOST_MAX 255
37 #define REMOTE_BITBANG_RAISE_ERROR(expr ...) \
38 do { \
39 LOG_ERROR(expr); \
40 LOG_ERROR("Terminating openocd."); \
41 exit(-1); \
42 } while (0)
44 static char remote_bitbang_host[REMOTE_BITBANG_HOST_MAX] = "openocd";
45 static uint16_t remote_bitbang_port;
47 FILE *remote_bitbang_in;
48 FILE *remote_bitbang_out;
50 static void remote_bitbang_putc(int c)
52 if (EOF == fputc(c, remote_bitbang_out))
53 REMOTE_BITBANG_RAISE_ERROR("remote_bitbang_putc: %s", strerror(errno));
56 static int remote_bitbang_quit(void)
58 if (EOF == fputc('Q', remote_bitbang_out)) {
59 LOG_ERROR("fputs: %s", strerror(errno));
60 return ERROR_FAIL;
63 if (EOF == fflush(remote_bitbang_out)) {
64 LOG_ERROR("fflush: %s", strerror(errno));
65 return ERROR_FAIL;
68 /* We only need to close one of the FILE*s, because they both use the same */
69 /* underlying file descriptor. */
70 if (EOF == fclose(remote_bitbang_out)) {
71 LOG_ERROR("fclose: %s", strerror(errno));
72 return ERROR_FAIL;
75 LOG_INFO("remote_bitbang interface quit");
76 return ERROR_OK;
79 /* Get the next read response. */
80 static int remote_bitbang_rread(void)
82 if (EOF == fflush(remote_bitbang_out)) {
83 remote_bitbang_quit();
84 REMOTE_BITBANG_RAISE_ERROR("fflush: %s", strerror(errno));
87 int c = fgetc(remote_bitbang_in);
88 switch (c) {
89 case '0':
90 return 0;
91 case '1':
92 return 1;
93 default:
94 remote_bitbang_quit();
95 REMOTE_BITBANG_RAISE_ERROR(
96 "remote_bitbang: invalid read response: %c(%i)", c, c);
100 static int remote_bitbang_read(void)
102 remote_bitbang_putc('R');
103 return remote_bitbang_rread();
106 static void remote_bitbang_write(int tck, int tms, int tdi)
108 char c = '0' + ((tck ? 0x4 : 0x0) | (tms ? 0x2 : 0x0) | (tdi ? 0x1 : 0x0));
109 remote_bitbang_putc(c);
112 static void remote_bitbang_reset(int trst, int srst)
114 char c = 'r' + ((trst ? 0x2 : 0x0) | (srst ? 0x1 : 0x0));
115 remote_bitbang_putc(c);
118 static void remote_bitbang_blink(int on)
120 char c = on ? 'B' : 'b';
121 remote_bitbang_putc(c);
124 static struct bitbang_interface remote_bitbang_bitbang = {
125 .read = &remote_bitbang_read,
126 .write = &remote_bitbang_write,
127 .reset = &remote_bitbang_reset,
128 .blink = &remote_bitbang_blink,
131 static int remote_bitbang_speed(int speed)
133 return ERROR_OK;
136 static int remote_bitbang_init_tcp(void)
138 LOG_INFO("Connecting to %s:%i", remote_bitbang_host, remote_bitbang_port);
139 int fd = socket(PF_INET, SOCK_STREAM, 0);
140 if (fd < 0) {
141 LOG_ERROR("socket: %s", strerror(errno));
142 return ERROR_FAIL;
145 struct hostent *hent = gethostbyname(remote_bitbang_host);
146 if (hent == NULL) {
147 char *errorstr = "???";
148 switch (h_errno) {
149 case HOST_NOT_FOUND:
150 errorstr = "host not found";
151 break;
152 case NO_ADDRESS:
153 errorstr = "no address";
154 break;
155 case NO_RECOVERY:
156 errorstr = "no recovery";
157 break;
158 case TRY_AGAIN:
159 errorstr = "try again";
160 break;
162 LOG_ERROR("gethostbyname: %s", errorstr);
163 return ERROR_FAIL;
166 struct sockaddr_in addr;
167 addr.sin_family = AF_INET;
168 addr.sin_port = htons(remote_bitbang_port);
169 addr.sin_addr = *(struct in_addr *)hent->h_addr;
170 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
171 LOG_ERROR("connect: %s", strerror(errno));
172 return ERROR_FAIL;
175 remote_bitbang_in = fdopen(fd, "r");
176 if (remote_bitbang_in == NULL) {
177 LOG_ERROR("fdopen: failed to open read stream");
178 return ERROR_FAIL;
181 remote_bitbang_out = fdopen(fd, "w");
182 if (remote_bitbang_out == NULL) {
183 LOG_ERROR("fdopen: failed to open write stream");
184 return ERROR_FAIL;
187 LOG_INFO("remote_bitbang driver initialized");
188 return ERROR_OK;
191 static int remote_bitbang_init_unix(void)
193 LOG_INFO("Connecting to unix socket %s", remote_bitbang_host);
194 int fd = socket(PF_UNIX, SOCK_STREAM, 0);
195 if (fd < 0) {
196 LOG_ERROR("socket: %s", strerror(errno));
197 return ERROR_FAIL;
200 struct sockaddr_un addr;
201 addr.sun_family = AF_UNIX;
202 strncpy(addr.sun_path, remote_bitbang_host, UNIX_PATH_MAX);
203 addr.sun_path[UNIX_PATH_MAX-1] = '\0';
205 if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
206 LOG_ERROR("connect: %s", strerror(errno));
207 return ERROR_FAIL;
210 remote_bitbang_in = fdopen(fd, "r");
211 if (remote_bitbang_in == NULL) {
212 LOG_ERROR("fdopen: failed to open read stream");
213 return ERROR_FAIL;
216 remote_bitbang_out = fdopen(fd, "w");
217 if (remote_bitbang_out == NULL) {
218 LOG_ERROR("fdopen: failed to open write stream");
219 return ERROR_FAIL;
222 LOG_INFO("remote_bitbang driver initialized");
223 return ERROR_OK;
226 static int remote_bitbang_init(void)
228 bitbang_interface = &remote_bitbang_bitbang;
230 LOG_INFO("Initializing remote_bitbang driver");
231 if (remote_bitbang_port == 0)
232 return remote_bitbang_init_unix();
233 return remote_bitbang_init_tcp();
236 static int remote_bitbang_khz(int khz, int *jtag_speed)
238 *jtag_speed = 0;
239 return ERROR_OK;
242 static int remote_bitbang_speed_div(int speed, int *khz)
244 /* I don't think this really matters any. */
245 *khz = 1;
246 return ERROR_OK;
249 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_port_command)
251 if (CMD_ARGC == 1) {
252 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], remote_bitbang_port);
253 return ERROR_OK;
255 return ERROR_COMMAND_SYNTAX_ERROR;
258 COMMAND_HANDLER(remote_bitbang_handle_remote_bitbang_host_command)
260 if (CMD_ARGC == 1) {
261 strncpy(remote_bitbang_host, CMD_ARGV[0], REMOTE_BITBANG_HOST_MAX);
262 remote_bitbang_host[REMOTE_BITBANG_HOST_MAX-1] = '\0';
263 return ERROR_OK;
265 return ERROR_COMMAND_SYNTAX_ERROR;
268 static const struct command_registration remote_bitbang_command_handlers[] = {
270 .name = "remote_bitbang_port",
271 .handler = remote_bitbang_handle_remote_bitbang_port_command,
272 .mode = COMMAND_CONFIG,
273 .help = "Set the port to use to connect to the remote jtag.\n"
274 " if 0, use unix sockets to connect to the remote jtag.",
275 .usage = "port_number",
278 .name = "remote_bitbang_host",
279 .handler = remote_bitbang_handle_remote_bitbang_host_command,
280 .mode = COMMAND_CONFIG,
281 .help = "Set the host to use to connect to the remote jtag.\n"
282 " if port is 0, this is the name of the unix socket to use.",
283 .usage = "host_name",
285 COMMAND_REGISTRATION_DONE,
288 struct jtag_interface remote_bitbang_interface = {
289 .name = "remote_bitbang",
290 .execute_queue = &bitbang_execute_queue,
291 .speed = &remote_bitbang_speed,
292 .commands = remote_bitbang_command_handlers,
293 .init = &remote_bitbang_init,
294 .quit = &remote_bitbang_quit,
295 .khz = &remote_bitbang_khz,
296 .speed_div = &remote_bitbang_speed_div,