aboutsummaryrefslogtreecommitdiffstats
path: root/src/fl2k_tcp.c
blob: ad0aa1864ce6a26c61220b02a6eeaac7a82efed4 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * osmo-fl2k, turns FL2000-based USB 3.0 to VGA adapters into
 * low cost DACs
 *
 * Copyright (C) 2016-2018 by Steve Markgraf <steve@steve-m.de>
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * 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 2 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/>.
 */

#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef _WIN32
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h> /* for TCP_NODELAY */
#include <fcntl.h>
#else
#include <winsock2.h>
#include "getopt/getopt.h"
#endif

#include "osmo-fl2k.h"

#ifdef _WIN32
#pragma comment(lib, "ws2_32.lib")

typedef int socklen_t;

#else
#define closesocket close
#define SOCKADDR struct sockaddr
#define SOCKET int
#define SOCKET_ERROR -1
#endif

static SOCKET s;
static fl2k_dev_t *dev = NULL;
static volatile int do_exit = 0;
static volatile int connected = 0;
static char *txbuf = NULL;
static fd_set readfds;
static SOCKET sock;

void usage(void)
{
	fprintf(stderr,
		"fl2k_tcp, a spectrum client for FL2K VGA dongles\n\n"
		"Usage:\t[-a server address]\n"
		"\t[-d device index (default: 0)]\n"
		"\t[-p port (default: 1234)]\n"
		"\t[-s samplerate in Hz (default: 100 MS/s)]\n"
		"\t[-b number of buffers (default: 4)]\n"
	);
	exit(1);
}

#ifdef _WIN32
BOOL WINAPI
sighandler(int signum)
{
	if (CTRL_C_EVENT == signum) {
		fprintf(stderr, "Signal caught, exiting!\n");
		fl2k_stop_tx(dev);
		do_exit = 1;
		return TRUE;
	}
	return FALSE;
}
#else
static void sighandler(int signum)
{
	fprintf(stderr, "Signal caught, exiting!\n");
	do_exit = 1;
	fl2k_stop_tx(dev);
}
#endif

void fl2k_callback(fl2k_data_info_t *data_info)
{
	int left = FL2K_BUF_LEN;
	int received;
	int r;
	struct timeval tv = { 1, 0 };

	if (!connected)
		return;

	data_info->sampletype_signed = 1;
	data_info->r_buf = txbuf;

	while (!do_exit && (left > 0)) {
		FD_ZERO(&readfds);
		FD_SET(sock, &readfds);
		tv.tv_sec = 1;
		tv.tv_usec = 0;
		r = select(sock + 1, &readfds, NULL, NULL, &tv);

		if (r) {
			received = recv(sock, txbuf + (FL2K_BUF_LEN - left), left, 0);
			left -= received;
		}
	}
}

int main(int argc, char **argv)
{
	int r, opt, i;
	char *addr = "127.0.0.1";
	int port = 1234;
	uint32_t samp_rate = 100000000;
	struct sockaddr_in local, remote;
	uint32_t buf_num = 0;
	int dev_index = 0;
	int dev_given = 0;
	int flag = 1;

#ifdef _WIN32
	WSADATA wsd;
	i = WSAStartup(MAKEWORD(2,2), &wsd);
#else
	struct sigaction sigact, sigign;
#endif

	while ((opt = getopt(argc, argv, "d:s:a:p:b:")) != -1) {
		switch (opt) {
		case 'd':
			dev_index = (uint32_t)atoi(optarg);
			dev_given = 1;
			break;
		case 's':
			samp_rate = (uint32_t)atof(optarg);
			break;
		case 'a':
			addr = optarg;
			break;
		case 'p':
			port = atoi(optarg);
			break;
		case 'b':
			buf_num = atoi(optarg);
			break;
		default:
			usage();
			break;
		}
	}

	if (argc < optind)
		usage();

	if (dev_index < 0) {
		exit(1);
	}

	txbuf = malloc(FL2K_BUF_LEN);

	if (!txbuf) {
		fprintf(stderr, "malloc error!\n");
		exit(1);
	}

	fl2k_open(&dev, (uint32_t)dev_index);
	if (NULL == dev) {
		fprintf(stderr, "Failed to open fl2k device #%d.\n", dev_index);
		exit(1);
	}

	r = fl2k_start_tx(dev, fl2k_callback, NULL, buf_num);

	/* Set the sample rate */
	r = fl2k_set_sample_rate(dev, samp_rate);
	if (r < 0)
		fprintf(stderr, "WARNING: Failed to set sample rate.\n");

#ifndef _WIN32
	sigact.sa_handler = sighandler;
	sigemptyset(&sigact.sa_mask);
	sigact.sa_flags = 0;
	sigign.sa_handler = SIG_IGN;
	sigaction(SIGINT, &sigact, NULL);
	sigaction(SIGTERM, &sigact, NULL);
	sigaction(SIGQUIT, &sigact, NULL);
	sigaction(SIGPIPE, &sigact, NULL);
#else
	SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
#endif

	sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	memset(&remote, 0, sizeof(remote));

	remote.sin_family = AF_INET;
	remote.sin_port = htons(port);
	remote.sin_addr.s_addr = inet_addr(addr);

	fprintf(stderr, "Connecting to %s:%d...\n", addr, port);
	while (connect(sock, (struct sockaddr *)&remote, sizeof(remote)) != 0) {
#ifndef _WIN32
		usleep(500000);
#else
		Sleep(0.5);
#endif
		if (do_exit)
			goto out;
	}

	setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,sizeof(flag));
	fprintf(stderr, "Connected\n");
	connected = 1;

	while (!do_exit) {
#ifndef _WIN32
		usleep(500000);
#else
		Sleep(0.5);
#endif
	}

out:
	free(txbuf);
	fl2k_close(dev);
	closesocket(s);
#ifdef _WIN32
	WSACleanup();
#endif

	return 0;
}