-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathtcp_example.c
116 lines (99 loc) · 2.56 KB
/
tcp_example.c
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
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libsbp/sbp.h>
#include <libsbp/legacy/system.h>
#include <libsbp/legacy/compat.h>
char *tcp_ip_addr = NULL;
char *tcp_ip_port = NULL;
static sbp_msg_callbacks_node_t heartbeat_callback_node;
int socket_desc = -1;
void usage(char *prog_name) {
fprintf(stderr, "usage: %s [-a address -p port]\n", prog_name);
}
void setup_socket()
{
struct sockaddr_in server;
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
fprintf(stderr, "Could not create socket\n");
}
memset(&server, '0', sizeof(server));
server.sin_addr.s_addr = inet_addr(tcp_ip_addr);
server.sin_family = AF_INET;
server.sin_port = htons(atoi(tcp_ip_port));
if (connect(socket_desc, (struct sockaddr *)&server , sizeof(server)) < 0)
{
fprintf(stderr, "Connection error\n");
}
}
void close_socket()
{
close(socket_desc);
}
void heartbeat_callback(u16 sender_id, u8 len, u8 msg[], void *context)
{
(void)sender_id, (void)len, (void)msg, (void)context;
fprintf(stdout, "%s\n", __FUNCTION__);
}
s32 socket_read(u8 *buff, u32 n, void *context)
{
(void)context;
s32 result;
result = read(socket_desc, buff, n);
return result;
}
int main(int argc, char **argv)
{
int opt;
int result = 0;
sbp_state_t s;
if (argc <= 2) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
while ((opt = getopt(argc, argv, "a:p:")) != -1) {
switch (opt) {
case 'a':
tcp_ip_addr = (char *)calloc(strlen(optarg) + 1, sizeof(char));
if (!tcp_ip_addr) {
fprintf(stderr, "Cannot allocate memory!\n");
exit(EXIT_FAILURE);
}
strcpy(tcp_ip_addr, optarg);
break;
case 'p':
tcp_ip_port = (char *)calloc(strlen(optarg) + 1, sizeof(char));
if (!tcp_ip_port) {
fprintf(stderr, "Cannot allocate memory!\n");
exit(EXIT_FAILURE);
}
strcpy(tcp_ip_port, optarg);
break;
case 'h':
usage(argv[0]);
exit(EXIT_FAILURE);
default:
break;
}
}
if ((!tcp_ip_addr) || (!tcp_ip_port)) {
fprintf(stderr, "Please supply the address and port of the SBP data stream!\n");
exit(EXIT_FAILURE);
}
setup_socket();
sbp_state_init(&s);
sbp_payload_callback_register(&s, SBP_MSG_HEARTBEAT, &heartbeat_callback, NULL,
&heartbeat_callback_node);
while(1) {
sbp_process(&s, &socket_read);
}
close_socket();
free(tcp_ip_addr);
free(tcp_ip_port);
return result;
}