简体   繁体   中英

How to get multicast to work on stm32f7 with LWIP and FreeRTOS

Hi i am trying to get a UDP multicast socket up and running on an STM32f7 (NucleoF767zi-board) using LWIP and FreeRTOS.

I have already implemented the LWIP and FreeRTOS middleware, and it seems to work fine. I have also set up a windows program which sends multicast messages to 239.192.0.4 port 60003, and have tested on other devices on my network that the messages gets recieved fine. But when i use the STM32f7 processor everything seems to work fine, but it doesn't receive anything.

I use this code i found from a similar guide to join the multicast group and get no errors when i debug my way through the code:

int Bind(int sock, uint16_t port) {
    //struct sockaddr_in serv_addr;
    memset((char*) &serv_addr, 0, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(port);
    if (bind(sock, (struct sockaddr* ) &serv_addr,
            (socklen_t ) sizeof(serv_addr)) < 0)
        return -1;
    return 0;
}

int JoinGroup(int sock, const char *join_ip) {
    ip_mreq mreq;
    mreq.imr_multiaddr.s_addr = inet_addr(join_ip);
    ip4_addr_t localAddress = MX_LWIP_Get_IP();//Gets the local IP interface address
    mreq.imr_interface.s_addr = localAddress.addr;
    ;
    if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &mreq,
            sizeof(mreq)) < 0)
        return -1;
    return 0;
}

int MulticastStart() {
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    int reuse = 1;
/* Dont know if SO_REUSEADDER is nenecessary doesn't seem to make a difference*/
    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse,
            sizeof(reuse)) < 0) {
        perror("Setting SO_REUSEADDR error");
    } else
        printf("Setting SO_REUSEADDR...OK.\n");

    Bind(sock, 60003);
    JoinGroup(sock, "239.192.0.4");
    return sock;
    // Now you can do recvfrom() in RTOS task.
}

I then have a FreeRTOS task where i try to print out the data received from recvfrom(), but recvfrom() remains blocked like it never receives anything. Also when i can see that my other devices receives the messages from the multicast group just fine.

void StartLWIPReceiverTask(void const *argument) {
    osSemaphoreWait(LWIPsemaphore, osWaitForever); // Wait indefinitely for a free semaphore
    int sock = MulticastStart();
    char test[256];
    memset(&test, 0, sizeof(test));
    int addrlen = sizeof(serv_addr);
    /* Infinite loop */
    for (;;) {
        int nbytes = recvfrom(sock, test, 255, 0,
                (struct sockaddr* ) &serv_addr, (socklen_t* )&addrlen);
        if (nbytes > 0) {
            HAL_UART_Transmit(&huart3, (uint8_t*) test, (uint16_t) strlen(test),
                    1000);
            memset(&test, 0, sizeof(test));
        }
        osDelay(1);
    }
}

I have set the following flags: in lwipopts.h:

#define LWIP_IGMP 1

#define LWIP_IPV4 1

#define LWIP_SOCKET 1

#define LWIP_UDP 1

#define SO_REUSE 1 

in ethernetif.c:

netif->flags |= NETIF_FLAG_IGMP; //in low_level_init function

in stm32f7xx_hal_eth.c:

macinit.PromiscuousMode = ETH_PROMISCUOUS_MODE_ENABLE;
macinit.MulticastFramesFilter = ETH_MULTICASTFRAMESFILTER_NONE;

I dont get any errors, so i am pretty lost and would appreciate any tips at this point.

if somebody else encounter this problem, i solved it by making a new project and copying over the code and set the same flags i the same files. not sure why it works now, but if it works it works:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM