I tried to compile this program with gcc 4.8.4, then this program will be uploaded to the board MSP430. And I get the warning
/home/doni/RIOT-tcp/examples/gnrc_tcp_srv/main.c:117:19: error: cast from pointer to integer of different size [-Werror=pointer-to-int-cast]
This error comes for the following code:
#include <stdio.h>
#include <errno.h>
#include "thread.h"
#include "net/af.h"
#include "net/gnrc/ipv6.h"
#include "net/conn/tcp.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
#ifndef LOCAL_PORT
#define LOCAL_PORT 80 /* The Portnumber the server is listening on */
#endif
#ifndef BACKLOG
#define BACKLOG 2 /* Number of possible parallel connections */
#endif
#ifndef NBYTE
#define NBYTE (2048) /* Amount of data to transmit */
#endif
conn_tcp_t conn;
conn_tcp_t conn_queue[BACKLOG];
uint8_t bufs[BACKLOG][NBYTE];
uint8_t stacks[BACKLOG][THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF];
void *srv_thread(__attribute__((unused)) void *arg);
int main(void)
{
kernel_pid_t ifs[GNRC_NETIF_NUMOF];
ipv6_addr_t local_addr;
gnrc_ipv6_netif_t *entry = NULL;
char ipv6_addr_str[IPV6_ADDR_MAX_STR_LEN];
int32_t err = 0;
/* Disable 15.4 Retransmissions on the first network device */
//netopt_enable_t opt = NETOPT_ENABLE;
netopt_enable_t opt = NETOPT_DISABLE;
gnrc_netif_get(ifs);
gnrc_netapi_set(ifs[0], NETOPT_AUTOACK, 0, &opt, sizeof(opt));
gnrc_netapi_set(ifs[0], NETOPT_ACK_REQ, 0, &opt, sizeof(opt));
printf("\nStarting server: LOCAL_PORT=%d, BACKLOG=%d, NBYTE=%d\n\n", LOCAL_PORT, BACKLOG, NBYTE);
/* Print all link-local addresses of the server */
printf("The Server listens on the following addresses: \n");
entry = gnrc_ipv6_netif_get(ifs[0]);
for (int i = 0; i < GNRC_IPV6_NETIF_ADDR_NUMOF; i++) {
if ((ipv6_addr_is_link_local(&entry->addrs[i].addr)) && !(entry->addrs[i].flags & GNRC_IPV6_NETIF_ADDR_FLAGS_NON_UNICAST)) {
ipv6_addr_to_str(ipv6_addr_str, &entry->addrs[i].addr, IPV6_ADDR_MAX_STR_LEN);
printf(" - %s\n", ipv6_addr_str);
}
}
/* Connection should accept from all address connections) */
ipv6_addr_set_unspecified(&local_addr);
/* Create connection struct */
err = conn_tcp_create(&conn, &local_addr, sizeof(ipv6_addr_t), AF_INET6, LOCAL_PORT);
switch (err) {
case 0:
DEBUG("conn_tcp_create() : 0 : ok \n");
break;
case -EINVAL:
printf("conn_tcp_create() : -EINVAL : Invalid addr_len \n");
break;
case -EADDRNOTAVAIL:
printf("conn_tcp_create() : -EADDRNOTAVAIL : Invalid addr \n");
break;
case -EAFNOSUPPORT:
printf("conn_tcp_create() : -EAFNOSUPPORT : Invalid AF-Family\n");
break;
default:
printf("conn_tcp_create() : %"PRIi32" : Unexpected return value: Go fix it!\n", err);
}
/* Initialize connection structs to wait for incomming connections */
err = conn_tcp_listen(&conn, conn_queue, sizeof(conn_queue) / sizeof(conn_queue[0]));
switch (err) {
case -EADDRINUSE:
printf("conn_tcp_listen() : -EADDRINUSE : Address is already in use\n");
return 0;
case -EAFNOSUPPORT:
printf("conn_tcp_listen() : -EAFNOSUPPORT: conn and conn_queue have a different address format\n");
return 0;
default:
DEBUG("conn_tcp_listen() : %"PRIi32" : ok, %"PRIi32" connections are waiting for peers\n", err, err);
}
/* Start Threads to handle each connection */
for (int i = 0; i < err; i += 1) {
thread_create((char *)stacks[i], sizeof(stacks[i]), THREAD_PRIORITY_MAIN, 0, srv_thread, (void *)(intptr_t)i, NULL);
}
return 0;
}
void *srv_thread(__attribute__((unused)) void *arg)
{
int32_t tid = (int32_t)arg;
int32_t err = 0;
uint32_t rcvd = 0;
uint32_t sent = 0;
uint32_t cycles = 0;
/* Structs to hold connection Info */
ipv6_addr_t local_addr;
ipv6_addr_t peer_addr;
char local_addr_str[IPV6_ADDR_MAX_STR_LEN];
char peer_addr_str[IPV6_ADDR_MAX_STR_LEN];
uint16_t local_port;
uint16_t peer_port;
/* Handle on the current Connection */
conn_tcp_t *peer = NULL;
/* Connection handling code */
DEBUG("Srv running: TID=%"PRIi32"\n", tid);
while (1) {
/* Handle incomming Connection */
err = conn_tcp_accept(&conn, &peer);
switch (err) {
case 0:
DEBUG("TID=%"PRIi32" : conn_tcp_accept() : 0 : ok \n", tid);
break;
case -EOPNOTSUPP:
printf("TID=%"PRIi32" : conn_tcp_accept() : -EOPNOTSUPP : Operation not supported on active Connection\n", tid);
return 0;
case -EAGAIN:
printf("TID=%"PRIi32" : conn_tcp_accept() : -EAGAIN : All pending connections are already handled\n", tid);
return 0;
default:
printf("TID=%"PRIi32" : conn_tcp_accept() : %"PRIi32" : Unexpected return value: Go fix it!\n", tid, err);
return 0;
}
/* Get Connections local Info */
err = conn_tcp_getlocaladdr(peer, &local_addr, &local_port);
switch (err) {
case 0:
printf("TID=%"PRIi32" : conn_tcp_getlocaladdr() : 0 : No Address copied\n", tid);
break;
default:
if (!ipv6_addr_equal((ipv6_addr_t *)peer->local_addr, &local_addr)) {
printf("TID=%"PRIi32" : conn_tcp_getlocaladdr() : %"PRIi32" : Unexpected local Address\n", tid, err);
break;
}
if (peer->local_port != local_port) {
printf("TID=%"PRIi32" : conn_tcp_getlocaladdr() : %"PRIi32" : Unexpected local Port\n", tid, err);
break;
}
DEBUG("TID=%"PRIi32" : conn_tcp_getlocaladdr() : %"PRIi32" : ok\n", tid, err);
break;
}
/* Get Connections peer Info */
err = conn_tcp_getpeeraddr(peer, &peer_addr, &peer_port);
switch (err) {
case 0:
printf("TID=%"PRIi32" : conn_tcp_getpeeraddr() : 0 : No Address copied\n", tid);
break;
default:
if (!ipv6_addr_equal((ipv6_addr_t *)peer->peer_addr, &peer_addr)) {
printf("TID=%"PRIi32" : conn_tcp_getpeeraddr() : %"PRIi32" : Unexpected peer Address\n", tid, err);
break;
}
if (peer->peer_port != peer_port) {
printf("TID=%"PRIi32" : conn_tcp_getpeeraddr() : %"PRIi32" : Unexpected peer Port\n", tid, err);
break;
}
DEBUG("TID=%"PRIi32" : conn_tcp_getpeeraddr() : %"PRIi32" : ok\n", tid, err);
break;
}
/* Print Connection info */
ipv6_addr_to_str(local_addr_str, &local_addr, IPV6_ADDR_MAX_STR_LEN);
ipv6_addr_to_str(peer_addr_str, &peer_addr, IPV6_ADDR_MAX_STR_LEN);
DEBUG("TID=%"PRIi32" : Connection Info : Local=[%s]:%"PRIu16" -- Peer=[%s]:%"PRIu16"\n", tid, local_addr_str, local_port, peer_addr_str, peer_port);
/* Receive Data */
for (rcvd = 0; rcvd < sizeof(bufs[tid]); rcvd += err) {
err = conn_tcp_recv(peer, (void *)(bufs[tid] + rcvd), sizeof(bufs[tid]) - rcvd);
switch (err) {
case -EPERM:
printf("TID=%"PRIi32" : conn_tcp_rcvd() : -EPERM : No permission. Caller is not the owner of the connection\n", tid);
return 0;
case -EALREADY:
printf("TID=%"PRIi32" : conn_tcp_rcvd() : -EALREADY : Connection already closed or closing\n", tid);
continue;
case -ETIME:
printf("TID=%"PRIi32" : conn_tcp_rcvd() : -ETIME : User call Timeout. Reconnect\n", tid);
continue;
default:
if (err >= 0) {
DEBUG("TID=%"PRIi32" : conn_tcp_rcvd() : %"PRIi32" : %"PRIi32" Bytes received from Peer=[%s]:%"PRIu16"\n", tid, err, err, peer_addr_str, peer_port);
}
else {
printf("TID=%"PRIi32" : conn_tcp_rcvd() : %"PRIi32" : Unexpected return value: Go fix it!\n", tid, err);
return 0;
}
}
}
/* Check received data against test pattern */
for (uint32_t i=0; i < sizeof(bufs[tid]) - 1; i+= 2){
if( bufs[tid][i] != 0xFF || bufs[tid][i+1] != 0x00 ) {
printf("TID=%"PRIi32" : Payload verfication failed\n", tid);
break;
}
}
/* Fill Buffer with a test pattern */
for (uint32_t i=0; i < sizeof(bufs[tid]) - 1; i+= 2){
bufs[tid][i] = 0xAA;
bufs[tid][i+1] = 0x77;
}
/* Send Data */
for (sent = 0; sent < sizeof(bufs[tid]); sent += err) {
err = conn_tcp_send(peer, bufs[tid] + sent, sizeof(bufs[tid]) - sent);
switch (err) {
case -EPERM:
printf("TID=%"PRIi32" : conn_tcp_send() : -EPERM : No permission. Caller is not the owner of the connection\n", tid);
return 0;
case -EALREADY:
printf("TID=%"PRIi32" : conn_tcp_send() : -EALREADY : Connection already closed or closing\n", tid);
continue;
case -ETIME:
printf("TID=%"PRIi32" : conn_tcp_send() : -ETIME : User call Timeout. Reconnect\n", tid);
continue;
default:
if (err >= 0) {
DEBUG("TID=%"PRIi32" : conn_tcp_send() : %"PRIi32" : %"PRIi32" Bytes sent to Peer=[%s]:%"PRIu16"\n", tid, err, err, peer_addr_str, peer_port);
}
else {
printf("TID=%"PRIi32" : conn_tcp_send() : %"PRIi32" : Unexpected return value: Go fix it!\n", tid, err);
return 0;
}
}
}
/* Close Connection */
err = conn_tcp_close(peer);
switch (err) {
case 0:
DEBUG("TID=%"PRIi32" : conn_tcp_close() : 0 : ok\n", tid);
break;
case -EPERM:
printf("TID=%"PRIi32" : conn_tcp_close() : -EPERM : No permission. Caller is not the owner of the connection\n", tid);
break;
case -EALREADY:
printf("TID=%"PRIi32" : conn_tcp_close() : -EALREADY : Connection already closed or closing\n", tid);
continue;
case -ETIME:
printf("TID=%"PRIi32" : conn_tcp_close() : -ETIME : User call Timeout. Reconnect\n", tid);
continue;
default:
printf("TID=%"PRIi32" : conn_tcp_close() : %"PRIi32" : Unexpected return value: Go fix it!\n", tid, err);
}
printf("TID=%"PRIi32" : %"PRIi32" test cycles completed\n", tid, cycles);
cycles += 1;
}
return 0;
}
problems in the
int32_t tid = (int32_t)arg;
How can I fix this warning?
This code was written for a 32-bit CPU. But on the MSP430, a pointer has 16 bits, so it would not be possible to stuff a 32-bit number into it.
Some of the code alreay used
intptr_t
to get a numeric type that fits into a pointer, but other places useint32_t
.You have to replace those occurrences of
int32_t
where pointer values are handled. But, more importantly, you also have to check that these numbers actually fit into a 16-bit type.