cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Highlighted
Contributor
Contributor
1,177 Views
Registered: ‎06-28-2016

tcp/ip select does not work on petalinux zynq

Jump to solution

Hello,

 

i made a Vivado 2017.4.1 design as follows (Name: server):

 

 Simple Design

Just Zynq...

 

1. Then i build and export this design.

 

2. SDK -> Linux Project (Hello World)

 

3. petalinux-create --type project --template zynq --name server

 

->

 

INFO: Create project: server
INFO: New project successfully created in /home/czymic/projects/2017.4/server

 

4. cd Server

 

5. petalinux-config --get-hw-description=/mnt/hgfs/share/2017.4/server/server.sdk

 

->

 

"Configuration Dialog"

input: save
input: Exit

 

-->

 

INFO: Getting hardware description...
cp: omitting directory '/mnt/hgfs/share/2017.4/server/server.sdk/Firmware'
cp: omitting directory '/mnt/hgfs/share/2017.4/server/server.sdk/RemoteSystemsTempFiles'
cp: omitting directory '/mnt/hgfs/share/2017.4/server/server.sdk/system_wrapper_hw_platform_0'
INFO: Rename system_wrapper.hdf to system.hdf
[INFO] generating Kconfig for project
                                                                                                                                                      
[INFO] menuconfig project
/home/czymic/projects/2017.4/server/build/misc/config/Kconfig.syshw:30:warning: defaults for choice values not supported
/home/czymic/projects/2017.4/server/build/misc/config/Kconfig:568:warning: config symbol defined without type


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

[INFO] sourcing bitbake
[INFO] generating plnxtool conf
[INFO] generating meta-plnx-generated layer
~/projects/2017.4/server/build/misc/plnx-generated ~/projects/2017.4/server
~/projects/2017.4/server
[INFO] generating machine configuration
[INFO] generating bbappends for project . This may take time !
~/projects/2017.4/server/build/misc/plnx-generated ~/projects/2017.4/server
~/projects/2017.4/server
[INFO] generating u-boot configuration files
                                                                                                                                                        
[INFO] generating kernel configuration files
[INFO] generating kconfig for Rootfs
Generate rootfs kconfig
[INFO] oldconfig rootfs
[INFO] generating petalinux-user-image.bb

 

6. petalinux-build

 

->

 

[INFO] building project
[INFO] sourcing bitbake
INFO: bitbake petalinux-user-image
Parsing recipes: 100% |###################################################################################################################| Time: 0:01:29
Parsing of 2466 .bb files complete (0 cached, 2466 parsed). 3259 targets, 226 skipped, 0 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies
Initialising tasks: 100% |################################################################################################################| Time: 0:00:06
Checking sstate mirror object availability: 100% |########################################################################################| Time: 0:00:10
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
fsbl-2017.4+gitAUTOINC+77448ae629-r0 do_compile: NOTE: fsbl: compiling from external source tree /home/czymic/petalinux/2017.4/tools/hsm/data/embeddedsw
NOTE: Tasks Summary: Attempted 2444 tasks of which 2395 didn't need to be rerun and all succeeded.
INFO: Copying Images from deploy to images
INFO: Creating images/linux directory
NOTE: Failed to copy built images to tftp dir:  /tftpboot
[INFO] successfully built Project

 

7. petalinux-package --boot --force --fsbl images/linux/zynq_fsbl.elf --fpga images/linux/system_wrapper.bit --u-boot

 

-->

 

INFO: File in BOOT BIN: "/home/czymic/projects/2017.4/server/images/linux/zynq_fsbl.elf"
INFO: File in BOOT BIN: "/home/czymic/projects/2017.4/server/images/linux/system_wrapper.bit"
INFO: File in BOOT BIN: "/home/czymic/projects/2017.4/server/images/linux/u-boot.elf"
INFO: Generating zynq binary package BOOT.BIN...
INFO: Binary is ready.
WARNING: Unable to access the TFTPBOOT folder /tftpboot!!!
WARNING: Skip file copy to TFTPBOOT folder!!!

 

8. Write Firmware:

 

#include <stdio.h>

#include <arpa/inet.h>

#include <unistd.h>

 

#define SOCKET_ERROR -1

#define INVALID_SOCKET SOCKET_ERROR

#define CHANNEL_BUFFER_SIZE 4

 

int main()

{

    printf("Hello World\n");

 

    struct sockaddr_in server_addr = { 0 };

    struct sockaddr_in client_addr = { 0 };

    int s_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

 

    server_addr.sin_family = AF_INET;

    server_addr.sin_port = htons(5000);

    inet_aton("0.0.0.0", (void*) &server_addr.sin_addr.s_addr);

 

    if (bind(s_server, (void*) &server_addr, sizeof(server_addr)) != INVALID_SOCKET)

    {

        if (listen(s_server, 1) != INVALID_SOCKET)

        {

            int s_client = INVALID_SOCKET;

 

            socklen_t nSize = 0;

 

            if ((s_client = accept(s_server, (void*) &client_addr, &nSize)) != INVALID_SOCKET)

            {

                fd_set fds;

 

                FD_ZERO(&fds);

                FD_SET(s_client, &fds);

 

                select(1, &fds, NULL, NULL, NULL);

 

                if (FD_ISSET(s_client, &fds))

                {

                    printf("Data received!\n");

 

                    close(s_client);

                    s_client = INVALID_SOCKET;

                }

            }

        }

    }

 

    return 0;

}

 

This Firmware is waiting for an incomming Connection on port 5000 and is waiting on receiving Input data. if it receives any data it closes the Connection. Thats all.

 

9 Test Firmware on zynq (Server design with default petalinux configuration)

 

Result: The firmware is able to establish the connection but select does not return even if data is present.

 

10. Writing Windows Consolen App with Visual Studio 6.0

 

#include "stdafx.h"

#include <stdio.h>
#include <winsock2.h>

 

#pragma comment(lib, "ws2_32.lib")

 

int main()
{
   WSADATA wsa = { 0 };

   printf("Hello World\n");

 

   if (WSAStartup(MAKEWORD(2, 2), &wsa) == 0)
   {
       struct sockaddr_in server_addr = { 0 };
       struct sockaddr_in client_addr = { 0 };
       int s_server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

      

       server_addr.sin_family = AF_INET;
       server_addr.sin_port = htons(5000);
       server_addr.sin_addr.s_addr = inet_addr("0.0.0.0");
  
       if (bind(s_server, (SOCKADDR*) &server_addr, sizeof(server_addr)) != SOCKET_ERROR)
       {
         if (listen(s_server, 1) != SOCKET_ERROR)
         {
           int s_client = INVALID_SOCKET;
           int nSize = sizeof(client_addr);

          

           if ((s_client = accept(s_server, (SOCKADDR*) &client_addr, &nSize)) != INVALID_SOCKET)
           {
             int nSelect = 0;
             fd_set fds;

     

             FD_ZERO(&fds);
             FD_SET(s_client, &fds);

     

             select(1, &fds, NULL, NULL, NULL);

             printf("Data received!\n");

             closesocket(s_client);
             s_client = INVALID_SOCKET;
           }
         }
       }

      

       WSACleanup();
     }

    

     return 0;
}

 

This Windows Consolen App is equivalent to the Linux App (Firmware see 8.) it has two differences:

 

a.) It Needs a WSAStartup call to initialize tcp/ip on Windows.

b.) It works.

 

Now my question:

 

What must i do to enable the select functionality on petalinux?

 

Thank you...

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Scholar
Scholar
1,420 Views
Registered: ‎05-28-2013

I would guess the problem is here:

select(1, &fds, NULL, NULL, NULL);

The first argument needs to be the highest file descriptor plus one. So try this:

select(s_client + 1, &fds, NULL, NULL, NULL);

Under Windows it probably works to use hard-coded 1, but in Unix / Linux / petalinux, all programs inherit three file descriptors when they start:

  • 0 = stdin
  • 1 = stdout
  • 2 = stderr

So that means that the lowest possible file descriptor for s_client will be 3.

 

(edit: fix typo on stdin/sterr)

View solution in original post

1 Reply
Highlighted
Scholar
Scholar
1,421 Views
Registered: ‎05-28-2013

I would guess the problem is here:

select(1, &fds, NULL, NULL, NULL);

The first argument needs to be the highest file descriptor plus one. So try this:

select(s_client + 1, &fds, NULL, NULL, NULL);

Under Windows it probably works to use hard-coded 1, but in Unix / Linux / petalinux, all programs inherit three file descriptors when they start:

  • 0 = stdin
  • 1 = stdout
  • 2 = stderr

So that means that the lowest possible file descriptor for s_client will be 3.

 

(edit: fix typo on stdin/sterr)

View solution in original post