Skip to content

Commit 9a2e71a

Browse files
committed
task-06: port to sock
1 parent df79250 commit 9a2e71a

3 files changed

Lines changed: 20 additions & 22 deletions

File tree

task-06/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ USEMODULE += gnrc_netdev_default
2626
USEMODULE += auto_init_gnrc_netif
2727
USEMODULE += gnrc_ipv6_default
2828
USEMODULE += gnrc_icmpv6_echo
29-
USEMODULE += gnrc_conn_udp
29+
USEMODULE += gnrc_sock_udp
3030

3131
include $(RIOTBASE)/Makefile.include

task-06/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
# 6. UDP Client / Server
44

5-
The transport layer (UDP, TCP, etc) is accessed through [`conn`](https://doc.riot-os.org/group__net__conn.html) driver API
5+
The transport layer (UDP, TCP, etc) is accessed through [`sock`](https://doc.riot-os.org/group__net__sock.html) driver API
66

77
![Networking overview](../overview-net.png)
88

99
Look at the new modules in the [`Makefile`](Makefile)
1010
```
11-
USEMODULE += gnrc_conn_udp
11+
USEMODULE += gnrc_sock_udp
1212
```
1313

14-
[`udp.c`](udp.c) utilizes `conn_udp_sendto()` and `conn_udp_recvfrom()` to exchange UDP packets
14+
[`udp.c`](udp.c) utilizes `sock_udp_send()` and `sock_udp_recv()` to exchange UDP packets
1515

1616
## Task 6.1: Use UDP for messaging
1717
1. Compile and run on two `native` instances
@@ -90,7 +90,7 @@ USEMODULE += gnrc_conn_udp
9090
```
9191
udp <tap0-IPv6-addr> 8888 hello
9292
```
93-
[Read the Doc](https://doc.riot-os.org/group__net__conn.html)
93+
[Read the Doc](https://doc.riot-os.org/group__net__sock.html)
9494
9595
## Task 6.3 -- Exchange UDP packets with your neighbors
9696
* Compile, flash and run on the board `BOARD=samr21-xpro make all flash term`

task-06/udp.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,37 @@
22
#include <stdint.h>
33
#include <stdio.h>
44

5-
#include "net/af.h"
6-
#include "net/conn/udp.h"
5+
#include "net/sock/udp.h"
76
#include "net/ipv6/addr.h"
87
#include "thread.h"
98

109
#define SERVER_MSG_QUEUE_SIZE (8)
1110
#define SERVER_BUFFER_SIZE (64)
1211

1312
static bool server_running = false;
14-
static conn_udp_t conn;
13+
static sock_udp_t sock;
1514
static char server_buffer[SERVER_BUFFER_SIZE];
1615
static char server_stack[THREAD_STACKSIZE_DEFAULT];
1716
static msg_t server_msg_queue[SERVER_MSG_QUEUE_SIZE];
1817

1918
void *_udp_server(void *args)
2019
{
21-
uint16_t port = (uint16_t) atoi(args);
22-
ipv6_addr_t server_addr = IPV6_ADDR_UNSPECIFIED;
20+
sock_udp_ep_t server = { .port = atoi(args), .family = AF_INET6 };
2321
msg_init_queue(server_msg_queue, SERVER_MSG_QUEUE_SIZE);
2422

25-
if(conn_udp_create(&conn, &server_addr, sizeof(server_addr), AF_INET6, port) < 0) {
23+
if(sock_udp_create(&sock, &server, NULL, 0) < 0) {
2624
return NULL;
2725
}
2826

2927
server_running = true;
30-
printf("Success: started UDP server on port %" PRIu8 "\n", port);
28+
printf("Success: started UDP server on port %u\n", server.port);
3129

3230
while (1) {
3331
int res;
34-
ipv6_addr_t src;
35-
size_t src_len = sizeof(ipv6_addr_t);
36-
if ((res = conn_udp_recvfrom(&conn, server_buffer, sizeof(server_buffer) - 1,
37-
&src, &src_len, &port)) < 0) {
32+
33+
if ((res = sock_udp_recv(&sock, server_buffer,
34+
sizeof(server_buffer) - 1, SOCK_NO_TIMEOUT,
35+
NULL)) < 0) {
3836
puts("Error while receiving");
3937
}
4038
else if (res == 0) {
@@ -51,20 +49,20 @@ void *_udp_server(void *args)
5149

5250
int udp_send(int argc, char **argv)
5351
{
52+
int res;
53+
sock_udp_ep_t remote = { .family = AF_INET6 };
54+
5455
if (argc != 4) {
5556
puts("Usage: udp <ipv6-addr> <port> <payload>");
5657
return -1;
5758
}
5859

59-
int res;
60-
ipv6_addr_t src = IPV6_ADDR_UNSPECIFIED, dst;
61-
if (ipv6_addr_from_str(&dst, argv[1]) == NULL) {
60+
if (ipv6_addr_from_str((ipv6_addr_t *)&remote.addr, argv[1]) == NULL) {
6261
puts("Error: unable to parse destination address");
6362
return 1;
6463
}
65-
66-
if((res = conn_udp_sendto(argv[3], strlen(argv[3]), &src, sizeof(src), &dst, sizeof(dst),
67-
AF_INET6, 1234, (uint16_t) (atoi(argv[2])))) < 0) {
64+
remote.port = atoi(argv[2]);
65+
if((res = sock_udp_send(NULL, argv[3], strlen(argv[3]), &remote)) < 0) {
6866
puts("could not send");
6967
}
7068
else {

0 commit comments

Comments
 (0)