Skip to content

Commit

Permalink
FIXUP: Cleanup after review part2
Browse files Browse the repository at this point in the history
  • Loading branch information
brummer-simon committed Aug 9, 2021
1 parent ffbd4c9 commit ba1588b
Showing 1 changed file with 39 additions and 163 deletions.
202 changes: 39 additions & 163 deletions tests/gnrc_sock_tcp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ void dump_args(int argc, char **argv)
printf("\n");
}

void print_result(const char *name, int err)
{
if (err) {
printf("%s: returns %s\n", name, strerror(err));
} else {
printf("%s: returns %d\n", name, err);
}
}

int sock_tcp_connect_cmd(int argc, char **argv)
{
dump_args(argc, argv);
Expand All @@ -43,42 +52,7 @@ int sock_tcp_connect_cmd(int argc, char **argv)
uint16_t flags = 0;

int err = sock_tcp_connect(sock, &ep, local_port, flags);
switch (err) {
case -EADDRINUSE:
printf("%s: returns -EADDRINUSE\n", argv[0]);
break;

case -EAFNOSUPPORT:
printf("%s: returns -EAFNOSUPPORT\n", argv[0]);
break;

case -ECONNREFUSED:
printf("%s: returns -ECONNREFUSED\n", argv[0]);
break;

case -EINVAL:
printf("%s: returns -EINVAL\n", argv[0]);
break;

case -ENETUNREACH:
printf("%s: returns -ENETUNREACH\n", argv[0]);
break;

case -ENOMEM:
printf("%s: returns -ENOMEM\n", argv[0]);
break;

case -EPERM:
printf("%s: returns -EPERM\n", argv[0]);
break;

case -ETIMEDOUT:
printf("%s: returns -ETIMEDOUT\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
}
print_result(argv[0], err);
return 0;
}

Expand All @@ -98,26 +72,7 @@ int sock_tcp_listen_cmd(int argc, char **argv)
uint16_t flags = 0;

int err = sock_tcp_listen(&sock_queue, &ep, socks, SOCK_TCP_QUEUE_SIZE, flags);
switch (err) {
case -EADDRINUSE:
printf("%s: returns -EADDRINUSE\n", argv[0]);
break;

case -EAFNOSUPPORT:
printf("%s: returns -EAFNOSUPPORT\n", argv[0]);
break;

case -EINVAL:
printf("%s: returns -EINVAL\n", argv[0]);
break;

case -ENOMEM:
printf("%s: returns -ENOMEM\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
}
print_result(argv[0], err);
return 0;
}

Expand All @@ -136,34 +91,7 @@ int sock_tcp_accept_cmd(int argc, char **argv)
uint16_t timeout = atol(argv[1]);

int err = sock_tcp_accept(&sock_queue, &tmp, timeout);
switch (err) {
case -EAGAIN:
printf("%s: returns -EAGAIN\n", argv[0]);
break;

case -ECONNABORTED:
printf("%s: returns -ECONNABORTED\n", argv[0]);
break;

case -EINVAL:
printf("%s: returns -EINVAL\n", argv[0]);
break;

case -ENOMEM:
printf("%s: returns -ENOMEM\n", argv[0]);
break;

case -EPERM:
printf("%s: returns -EPERM\n", argv[0]);
break;

case -ETIMEDOUT:
printf("%s: returns -ETIMEDOUT\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
}
print_result(argv[0], err);

if (tmp) {
sock = tmp;
Expand All @@ -181,32 +109,16 @@ int sock_tcp_read_cmd(int argc, char **argv)

while (rcvd < to_receive) {
int ret = sock_tcp_read(sock, buffer + rcvd, to_receive - rcvd, timeout);
switch (ret) {
case 0:
printf("%s: returns 0\n", argv[0]);
return ret;

case -EAGAIN:
printf("%s: returns -EAGAIN\n", argv[0]);
if (ret > 0) {
rcvd += ret;
} else {
print_result(argv[0], ret);
if (ret == -EAGAIN) {
continue;

case -ECONNABORTED:
printf("%s: returns -ECONNABORTED\n", argv[0]);
return ret;

case -ECONNRESET:
printf("%s: returns -ECONNABORTED\n", argv[0]);
return ret;

case -ENOTCONN:
printf("%s: returns -ENOTCONN\n", argv[0]);
return ret;

case -ETIMEDOUT:
printf("%s: returns -ETIMEDOUT\n", argv[0]);
} else {
return ret;
}
}
rcvd += ret;
}
buffer[to_receive] = '\0';
printf("%s: received %u %s\n", argv[0], rcvd, buffer);
Expand All @@ -223,24 +135,12 @@ int sock_tcp_write_cmd(int argc, char **argv)
while (sent < payload_size)
{
int ret = sock_tcp_write(sock, payload + sent, payload_size - sent);
switch (ret) {
case -ECONNABORTED:
printf("%s: returns -ECONNABORTED\n", argv[0]);
return ret;

case -ECONNRESET:
printf("%s: returns -ECONNABORTED\n", argv[0]);
return ret;

case -ENOMEM:
printf("%s: returns -ENOMEM\n", argv[0]);
return ret;

case -ENOTCONN:
printf("%s: returns -ENOTCONN\n", argv[0]);
return ret;
if (ret >= 0) {
sent += ret;
} else {
print_result(argv[0], ret);
continue;
}
sent += ret;
}
printf("%s: sent %u\n", argv[0], sent);
return 0;
Expand All @@ -251,20 +151,12 @@ int sock_tcp_get_local_cmd(int argc, char **argv)
dump_args(argc, argv);
sock_tcp_ep_t ep = SOCK_IPV6_EP_ANY;
int err = sock_tcp_get_local(sock, &ep);
switch (err) {
case 0:
printf("%s: returns 0\n", argv[0]);
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
break;

case -EADDRNOTAVAIL:
printf("%s: returns -EADDRNOTAVAIL\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
print_result(argv[0], err);
if (err == 0) {
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
}
return 0;
}
Expand All @@ -274,20 +166,12 @@ int sock_tcp_queue_get_local_cmd(int argc, char **argv)
dump_args(argc, argv);
sock_tcp_ep_t ep = SOCK_IPV6_EP_ANY;
int err = sock_tcp_queue_get_local(&sock_queue, &ep);
switch (err) {
case 0:
printf("%s: returns 0\n", argv[0]);
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
break;

case -EADDRNOTAVAIL:
printf("%s: returns -EADDRNOTAVAIL\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
print_result(argv[0], err);
if (err == 0) {
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
}
return 0;
}
Expand All @@ -297,20 +181,12 @@ int sock_tcp_get_remote_cmd(int argc, char **argv)
dump_args(argc, argv);
sock_tcp_ep_t ep = SOCK_IPV6_EP_ANY;
int err = sock_tcp_get_remote(sock, &ep);
switch (err) {
case 0:
printf("%s: returns 0\n", argv[0]);
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
break;

case -EADDRNOTAVAIL:
printf("%s: returns -EADDRNOTAVAIL\n", argv[0]);
break;

default:
printf("%s: returns %d\n", argv[0], err);
print_result(argv[0], err);
if (err == 0) {
printf("Endpoint: addr.ipv6=");
ipv6_addr_print((ipv6_addr_t *) ep.addr.ipv6);
printf(" netif=%u port=%u\n", ep.netif, ep.port);
}
return 0;
}
Expand Down

0 comments on commit ba1588b

Please sign in to comment.