Riot client using sock_udp causes segmentation fault

144 views Asked by At

I am trying to build a server - client in RIOT OS using c and sockets for their commnunication. They need to be able to exchnage messages. the client first needs to be configured to be able to read the values from the hardware and then send them to the server. I am new at this so I would like some help. My server is running wihout any problem at the moment, but the client is always causing segmentation fault and I do not know ehy. I have found the line. using make debug PORT=tap1. It is :

code for server

#include <stdio.h>
#include <inttypes.h>

#include "net/gnrc.h"
#include "net/gnrc/ipv6.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netif/hdr.h"
#include "net/gnrc/udp.h"
#include "net/gnrc/pktdump.h"
#include "timex.h"
#include "utlist.h"
#include "xtimer.h"

#include "net/sock/udp.h"


uint8_t buf[128];

//SERVER
int main(void)
{
    puts("RIOT network stack example application");

    /* get interfaces and print their addresses */
    gnrc_netif_t *netif = NULL;
    while ((netif = gnrc_netif_iter(netif))) {
        ipv6_addr_t ipv6_addrs[CONFIG_GNRC_NETIF_IPV6_ADDRS_NUMOF];
        int res = gnrc_netapi_get(netif->pid, NETOPT_IPV6_ADDR, 0, ipv6_addrs,
                                  sizeof(ipv6_addrs));

        if (res < 0) {
            continue;
        }
        for (unsigned i = 0; i < (unsigned)(res / sizeof(ipv6_addr_t)); i++) {
            char ipv6_addr[IPV6_ADDR_MAX_STR_LEN];

            ipv6_addr_to_str(ipv6_addr, &ipv6_addrs[i], IPV6_ADDR_MAX_STR_LEN);
            printf("My address is %s\n", ipv6_addr);
        }
    }
    sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
    sock_udp_t sock;

    local.port = 12345;

    if (sock_udp_create(&sock, &local, NULL, 0) < 0) {
        puts("Error creating UDP sock");
        return 1;
    }

    while (1) {
        sock_udp_ep_t remote;
        ssize_t res;

        if ((res = sock_udp_recv(&sock, buf, sizeof(buf), SOCK_NO_TIMEOUT,
                                 &remote)) >= 0) {
            puts("Received a message");
            if (sock_udp_send(&sock, buf, res, &remote) < 0) {
                puts("Error sending reply");
            }
        }
    }

    return 0;
}

code for client :


#include "net/netopt.h" 
#include "net/gnrc/nettype.h" 
#include "net/gnrc/pkt.h"

#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include <stdio.h>

#include "net/af.h"
#include "net/protnum.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"
#include "xtimer.h"
#include "net/protnum.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"

#include "saul_reg.h"

#include "shell.h"
#include "board.h"
#include "periph_conf.h"
#include "xtimer.h"  //for udp and tcp and processes
#include "thread.h" //for threads
#include "msg.h" //for threads
#include "net/gnrc/pktbuf.h" //for packets
//#include <pkt.h>
#include <pthread.h>

#include "periph/gpio.h" //file periph.h: Parameter definitions for mapping peripherals directly to SAUL.

#define Q_SZ 1000
//#define THREAD_STACKSIZE_MAIN 
//#include "net/ipv6/addr.h"  //for udp and tcp

#ifdef BOARD_ESP32_WROOM
#if MODULE_MRF24J40
#define MRF24J40_PARAM_CS       GPIO16       /* MRF24J40 CS signal    */
#define MRF24J40_PARAM_RESET    GPIO17       /* MRF24J40 RESET signal */
#define MRF24J40_PARAM_INT      GPIO34       /* MRF24J40 INT signal   */
#define MRF24J40_PARAM_SPI_CLK  SPI_CLK_1MHZ /* SPI clock frequency */
#endif
#if MODULE_ENC28J80
#define ENC28J80_PARAM_CS       GPIO32      /* ENC28J80 CS signal    */
#define ENC28J80_PARAM_RESET    GPIO33      /* ENC28J80 RESET signal */
#define ENC28J80_PARAM_INT      GPIO35      /* ENC28J80 INT signal   */
#endif
#endif

#ifndef ARDUINO_LED
#define ARDUINO_LED     (0)
#endif


#ifdef STDIO_UART_BAUDRATE
#define SERIAL_BAUDRATE STDIO_UART_BAUDRATE
#else
#define SERIAL_BAUDRATE 115200
#endif

//uint8_t buf[7];
static char stack[THREAD_STACKSIZE_MAIN];

// Assign the default LED pin
int ledPin = ARDUINO_LED;

// input buffer for receiving chars on the serial port
int buf[64];

// counter that counts the number of received chars
int count = 0;

static saul_reg_t* saul_dht_temp;
static saul_reg_t* saul_dht_humid;

static kernel_pid_t snd_pid;
gnrc_pktsnip_t *pkt;

sock_udp_ep_t local = SOCK_IPV6_EP_ANY;
sock_udp_t sock;

void *snd_handler(void *arg){ 
    pkt->data = &arg;
       //pkt->data = &phy.val[0]; //crushes
 
    sock_udp_ep_t remote = { .family = AF_INET6 };
    ssize_t res;


    remote.port = 12345;
    ipv6_addr_set_all_nodes_multicast((ipv6_addr_t *)&remote.addr.ipv6,
                                          IPV6_ADDR_MCAST_SCP_LINK_LOCAL);
    while(1){
        if (sock_udp_send(&sock, pkt->data, sizeof(pkt->data), &remote) < 0) {
            puts("Error sending message");
            sock_udp_close(&sock);
            break;
        }
         if ((res = sock_udp_recv(&sock, buf, sizeof(buf), 1 * US_PER_SEC,
                                NULL)) < 0) {
            if (res == -ETIMEDOUT) {
                puts("Timed out");
            }
            else {
                puts("Error receiving message");
            }
         }
         else {
            printf("Received message: \"");
            for (int i = 0; i < res; i++) {
                printf("%c", buf[i]);
            }
            printf("\"\n");
         }
        xtimer_sleep(2); //sleep for 1  
     }
     return NULL;
}


int main(void){
    puts("Welcome to RIOT!\n");
    //puts("Type `help` for help, type `saul` to see all SAUL devices\n");
    //char line_buf[SHELL_DEFAULT_BUFSIZE];
    //shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
    
    saul_dht_humid = saul_reg_find_type(SAUL_SENSE_HUM);
    saul_dht_temp = saul_reg_find_type(SAUL_SENSE_TEMP);
    phydat_t phy;
    phydat_t phy2;
    


    local.port = 0xabcd;

    if (sock_udp_create(&sock, &local, NULL, 0) < 0) {
        puts("Error creating UDP sock");
        return 1;
    }
    
    while(1){
        //gpio_toggle(g2); 
        saul_reg_read(saul_dht_temp, &phy);  //read a value( a set of values ) from a device
        saul_reg_read(saul_dht_humid, &phy2);
        printf("temperature: %d %d %d Humid: %d %d %d \n",
           phy.val[0], phy.unit,phy.scale, phy2.val[0], phy2.unit, phy2.scale);
          
          snd_pid = thread_create(stack,  /* stack array pointer */
                            sizeof(stack), /* stack size */
                            THREAD_PRIORITY_MAIN - 1, /* thread priority*/
                            THREAD_CREATE_STACKTEST, /* flag: thread configuration flag, usually*/
                            snd_handler, /* thread handler function */
                             (void *) &phy2.val[0], /* argument of thread_handler function*/
                            "snd_handler");   
        
       //pthread_exit(&snd_pid);
    
    }   
    puts("server ending!\n");
    
    return 0;
}

Of course, in the same directories exist also the makefiles with the modules.

The error I am getting is this in the photo :

enter image description here

To run the server-cleint, first I need to create a tap interface and the make commands.

I have tried raising the size of the stack but it did not help. I continued receiving the same error. It would be really helpful to know why it is caused segmentation fault.

0

There are 0 answers