1 /*
2 * Copyright (c) 2020 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /* TCP Sample for TTCN-3 based Sanity Check */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <errno.h>
12
13 #include <zephyr/posix/sys/socket.h>
14 #include <zephyr/net/socket.h>
15
16 #include <zephyr/data/json.h>
17 #include <tp.h>
18
19 #define UDP_PORT 4242
20
21 #define perror(fmt, args...) \
22 do { \
23 printf("Error: " fmt "(): %s\n" ## args, strerror(errno)); \
24 exit(errno); \
25 } while (0) \
26
27 /*
28 * This application is used together with the TTCN-3 based sanity check
29 * to validate the functionality of the TCP.
30 *
31 * samples/net/sockets/tcp/README.rst
32 *
33 * Eventually UDP based test protocol might be terminated in the user space
34 * (see udp() below), but at the moment it's just a dummy loop
35 * to keep the sample running in order to execute TTCN-3 TCP sanity check.
36 */
main(void)37 int main(void)
38 {
39 while (true) {
40 k_sleep(K_SECONDS(1));
41 }
42 return 0;
43 }
44
udp(void)45 void udp(void)
46 {
47 int fd = socket(AF_INET, SOCK_DGRAM, 0);
48
49 if (fd < 0) {
50 perror("socket");
51 }
52
53 {
54 struct sockaddr_in sin;
55
56 sin.sin_family = AF_INET;
57 sin.sin_addr.s_addr = htonl(INADDR_ANY);
58 sin.sin_port = htons(UDP_PORT);
59
60 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
61 perror("bind");
62 }
63
64 printf("Listening on UDP port %d\n", UDP_PORT);
65 }
66
67 {
68 #ifndef BUF_SIZE
69 #define BUF_SIZE 4096
70 #endif
71 char *buf = malloc(BUF_SIZE);
72
73 if (buf == NULL) {
74 perror("malloc");
75 }
76
77 for (;;) {
78 ssize_t len = recv(fd, buf, BUF_SIZE, 0);
79
80 if (len < 0) {
81 perror("recv");
82 }
83
84 printf("Received %ld bytes\n", (long)len);
85 }
86 }
87 }
88