Skip to content
This repository was archived by the owner on Mar 21, 2020. It is now read-only.

Commit ecf1651

Browse files
committedMay 31, 2018
Add simple control client
1 parent 6c080a1 commit ecf1651

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dvbs2_rate
22
dvbs2_tx
3+
dvbs2_tx_ctl
34
*.o
45

‎Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GR_COMP=gnuradio-runtime gnuradio-blocks gnuradio-dtv gnuradio-osmosdr
77
CFLAGS=`pkg-config --cflags $(GR_COMP)` -DVERSION=\"$(GIT_VERSION)\"
88
LDFLAGS=`pkg-config --libs $(GR_COMP)` -lboost_system
99

10-
all: dvbs2_tx dvbs2_rate
10+
all: dvbs2_tx dvbs2_tx_ctl dvbs2_rate
1111

1212
dvbs2_tx: dvbs2_tx.o app_conf.o ctl_if.o
1313
$(CPP) -o dvbs2_tx dvbs2_tx.o app_conf.o ctl_if.o $(LDFLAGS)
@@ -21,6 +21,9 @@ app_conf.o: app_conf.cpp
2121
ctl_if.o: ctl_if.c
2222
$(CC) -c $(CFLAGS) ctl_if.c
2323

24+
dvbs2_tx_ctl:
25+
$(CC) -o dvbs2_tx_ctl dvbs2_tx_ctl.c
26+
2427
dvbs2_rate:
2528
$(CC) -o dvbs2_rate dvbs2_rate.c -lm
2629

‎dvbs2_tx_ctl.c

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <sys/socket.h>
4+
#include <sys/un.h>
5+
#include <stdlib.h>
6+
#include <sys/select.h>
7+
8+
#define CONTROL_SOCKET_NAME "/tmp/control_socket"
9+
10+
int main (int argc, char **argv)
11+
{
12+
struct sockaddr_un my_addr;
13+
unsigned char buf [100];
14+
int client_socket;
15+
ssize_t rc;
16+
struct sockaddr_un to_addr;
17+
socklen_t to_len;
18+
struct timeval timeout;
19+
fd_set read_fds;
20+
21+
client_socket = socket (AF_UNIX, SOCK_DGRAM, 0);
22+
if (client_socket < 0) {
23+
perror ("socket (AF_UNIX, SOCK_DGRAM, 0):");
24+
return 1;
25+
}
26+
27+
/* Setup a return address so the server can return a reply. */
28+
memset (&my_addr, 0, sizeof (my_addr));
29+
my_addr.sun_family = AF_UNIX;
30+
snprintf (my_addr.sun_path, sizeof (my_addr.sun_path) - 1, "/tmp/%d-return", getpid ());
31+
unlink (my_addr.sun_path);
32+
if (bind (client_socket, (struct sockaddr *)&my_addr, sizeof (my_addr)) < 0) {
33+
perror ("bind (client_socket,,,):");
34+
return 1;
35+
}
36+
37+
/* Send message to server. */
38+
memset (&to_addr, 0, sizeof (to_addr));
39+
to_addr.sun_family = AF_UNIX;
40+
snprintf (to_addr.sun_path, sizeof (to_addr.sun_path) - 1, CONTROL_SOCKET_NAME);
41+
to_len = sizeof (to_addr);
42+
43+
if (argc >= 2) {
44+
/* Send the provided numerical value to the server. */
45+
buf [0] = 1; /* Set command. */
46+
buf [1] = atoi (argv [1]);
47+
} else {
48+
/* Poll the server. */
49+
buf [0] = 0; /* Poll command. */
50+
}
51+
52+
rc = sendto (client_socket, buf, 2, 0,
53+
(struct sockaddr *)&to_addr, to_len);
54+
if (rc < 0) {
55+
perror ("sendto (client_sock,,,):");
56+
return 1;
57+
}
58+
59+
/* Wait for a reply - but only for 2 seconds. */
60+
timeout.tv_sec = 2;
61+
timeout.tv_usec = 0;
62+
FD_ZERO (&read_fds);
63+
FD_SET (client_socket, &read_fds);
64+
rc = select (client_socket + 1, &read_fds, NULL, NULL, &timeout);
65+
if (rc == 1) {
66+
/* Got something. */
67+
char buf [100];
68+
struct sockaddr_un from;
69+
socklen_t from_len = sizeof (from);
70+
int x;
71+
72+
rc = recvfrom (client_socket, &buf, sizeof (buf), 0,
73+
(struct sockaddr *)&from, &from_len);
74+
75+
printf ("Modtaget ");
76+
for (x = 0; x < rc; x++) {
77+
printf (" %02X", buf [x]);
78+
}
79+
printf ("\n");
80+
} else {
81+
printf ("Timeout waiting for data\n");
82+
}
83+
84+
close (client_socket);
85+
unlink (my_addr.sun_path);
86+
}

0 commit comments

Comments
 (0)