aboutsummaryrefslogtreecommitdiff
path: root/src/platforms/libftdi/gdb_if.c
blob: ff49b57a128808c16b39a3377c1e4e33d1bb072d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * This file is part of the Black Magic Debug project.
 *
 * Copyright (C) 2011  Black Sphere Technologies Ltd.
 * Written by Gareth McMullin <gareth@blacksphere.co.nz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* This file implements a transparent channel over which the GDB Remote
 * Serial Debugging protocol is implemented.  This implementation for Linux
 * uses a TCP server on port 2000.
 */
#include <stdio.h>

#ifndef WIN32
#   include <sys/socket.h>
#   include <netinet/in.h>
#   include <sys/select.h>
#else
#   include <windows.h>
#   include <ws2tcpip.h>
#endif

#include <assert.h>

#include "general.h"
#include "gdb_if.h"

static int gdb_if_serv, gdb_if_conn;

int gdb_if_init(void)
{
#ifdef WIN32
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
	struct sockaddr_in addr;
	int opt;

	addr.sin_family = AF_INET;
	addr.sin_port = htons(2000);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);

	assert((gdb_if_serv = socket(PF_INET, SOCK_STREAM, 0)) != -1);
	opt = 1;
	assert(setsockopt(gdb_if_serv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != -1);

	assert(bind(gdb_if_serv, (void*)&addr, sizeof(addr)) != -1);
	assert(listen(gdb_if_serv, 1) != -1);

	DEBUG("Listening on TCP:2000\n");

	return 0;
}


unsigned char gdb_if_getchar(void)
{
	unsigned char ret;
	int i = 0;

	while(i <= 0) {
		if(gdb_if_conn <= 0) {
			gdb_if_conn = accept(gdb_if_serv, NULL, NULL);
			DEBUG("Got connection\n");
		}
		i = recv(gdb_if_conn, &ret, 1, 0);
		if(i <= 0) {
			gdb_if_conn = -1;
			DEBUG("Dropped broken connection\n");
			/* Return '+' in case we were waiting for an ACK */
			return '+';
		}
	}
	return ret;
}

unsigned char gdb_if_getchar_to(int timeout)
{
	fd_set fds;
	struct timeval tv;

	if(gdb_if_conn == -1) return -1;

	tv.tv_sec = timeout / 1000;
	tv.tv_usec = (timeout % 1000) * 1000;

	FD_ZERO(&fds);
	FD_SET(gdb_if_conn, &fds);

	if(select(gdb_if_conn+1, &fds, NULL, NULL, &tv) > 0)
		return gdb_if_getchar();

	return -1;
}

void gdb_if_putchar(unsigned char c, int flush)
{
	(void)flush;

	if (gdb_if_conn > 0)
		send(gdb_if_conn, &c, 1, 0);
}