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