This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/66AK2H12: Using BSD Sockets and getting task handle

Part Number: 66AK2H12

Tool/software: TI-RTOS

XDCtools 3.32, SYS/BIOS 6.46, CCS 7.4, PDK 4.0.7, NDK 2.25

I am trying to write a bsd socket application to run on ARM0. I read in the NDK API (spru524j) in the section on BSD that I should only include files in <NDK_install_dir>/packages/ti/ndk/inc/bsd and to be sure and not include files in <NDK_install_dir>/packages/ti/ndk/inc/. 

"The file containing BSD-style code can include header files found in the ti/ndk/inc/bsd directory tree (for
example, sys/socket.h) and should not need to include any NDK header file found in ti/ndk/inc and its
subdirectories other than the /bsd subdirectory."

To use the socket interface I need to open the file system with fdOpenSession() which takes a HANDLE to the current task. HANDLE is not defined in <NDK_install_dir>/packages/ti/ndk/inc/bsd. Further, the examples show calling TaskSelf() to get the HANDLE but TaskSelf() is in <NDK_install_dir>/packages\ti\ndk\inc\os\osif.h which I'm not suppose to include.

So how do I get the handle to my task running the BSD socket code and where do I find HANDLE defined?

Thanks,

Mike

  • The team is notified. They will post their feedback directly here.

    BR
    Tsvetolin Shulev
  • Hey Mike,

    Can you check if you have the following setting in your *.cfg file.
    Global.enableCodeGeneration=true;

    Or is it equal to false?

    Regards,
    Dalton
  • Hey Mike,

    We checked your other NDK forum thread and noticed you had Global.enableCodeGeneration=false;. If it were true the call to fdOpenSession() would be automatic, but since you have it set to false we have to work around this.

    Can you try the following:

    //newfile.c
    #include <ti/ndk/inc/netmain.h>
    
    void openFdSession()
    {
        fdOpenSession(TaskSelf());
    }
    
    void closeFdSession()
    {
        fdCloseSession(TaskSelf());
    }

    //yoursocket.c
    #include <sys/socket.h>
    extern void openFdSession();
    extern void closeFdSession();
    
    void yourSocketFunction()
    {
       openFdSession();
    
       //your sockets code
    
       closeFdSession();
    }

    This should allow you to open the file descriptor session without any compilation errors or conflicts. 

    Regards,

    Dalton

  • I also need to use poll() which looks like it is under ndk but not under bsd and takes HANDLE. Will the above work  for that?

    More generally, about all the other documented code in the NDK API in addition to the one section on BSD?

    And what about the definition of SOCKET. Can I use the socket descriptor passed to my task from the DaemonNew callback?

  • Hi Mike,

    Use void * wherever you need to use HANDLE. I'll need to ask my colleague about the DaemonNew function, ill get back to you on that.

    Regards,
    Dalton
  • Do I close the passed socket using fdCloseI() or close()?
  • I've now got #include nightmares going on but before I try to untangle it all I'd like to get more confidence that it will work given that:

    • The socket is passed to me from the DeamonNew callback and is void * in one and int in the oether
    • I need to use poll()

    As a quick test I went back to the example provided in the PDK NIMU_emacExample_EVMK2H_armBiosExampleProject and tried to convert that to use BSD sockets interface. (FWIW: I am disappointed to see that the example provided for this used 'Legacy' code that the NDK API documentation clearly says will be eliminated in the future).

    I updated the file udpHello.c and added the BSD include path to my compiler's search path as directed in the NDK API. I'm pasting the entirety of the udpHello.c below. Following that are the compiler errors I'm getting. Note that I'm not even trying to use the poll() function yet. Can you help me get this HelloWorld example working please? That would give me some confidence. Otherwise my approach might be to use the 'Legacy' Non-BSD interface.

    Kind Regards,
    Mike

    //#include <ti/ndk/inc/netmain.h>
    #include <xdc/std.h>
    #include <sys/socket.h>
    
    //
    // dtask_udp_hello() - UDP Echo Server Daemon Function
    // (SOCK_DGRAM, port 7)
    //
    // Returns "1" if socket 's' is still open, and "0" if its been closed
    //
    int dtask_udp_hello( int s, uint32_t unused )
    {
        struct sockaddr_in sin1;
        struct timeval     to;
        int                i,tmp;
        char               pBuf[5000];
    //    HANDLE             hBuffer;
        struct sockaddr from;
        socklen_t fromlen;
        (void)unused;
    
        // Configure our socket timeout to be 3 seconds
        to.tv_sec  = 3;
        to.tv_usec = 0;
        setsockopt( s, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) );
        setsockopt( s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) );
    
        for(;;)
        {
            tmp = sizeof( sin1 );
            i = (int)recvfrom( s, pBuf, 5000, MSG_WAITALL, &from, &fromlen );
    //        i = (int)recvncfrom( s, (void **)&pBuf, 0, (PSA)&sin1, &tmp, &hBuffer );
    
            // Spit any data back out
            if( i >= 0 )
            {
                sendto( s, pBuf, 5000, 0, &from, fromlen );
    //            recvncfree( hBuffer );
            }
            else
                break;
        }
    
        // Since the socket is still open, return "1"
        // (we need to leave UDP sockets open)
        return(1);    
    }
    

    Compile Errors:

    Description	Resource	Path	Location	Type
    '_types_fd_set' has no member named 'count'	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 121, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\socketndk.h	C/C++ Problem
    '_types_fd_set' has no member named 'count'	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 129, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\socketndk.h	C/C++ Problem
    '_types_fd_set' has no member named 'fd'	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 121, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\socketndk.h	C/C++ Problem
    gmake: *** [udpHello.o] Error 1	NIMU_emacExample_EVMK2H_armBiosExampleProject		 	C/C++ Problem
    gmake: Target 'all' not remade because of errors.	NIMU_emacExample_EVMK2H_armBiosExampleProject		 	C/C++ Problem
    recipe for target 'udpHello.o' failed	subdir_rules.mk	/NIMU_emacExample_EVMK2H_armBiosExampleProject/Debug	line 121	C/C++ Problem
    

    And a ton of warnings (many of which to be fair exist in the original example. most of these are the format() warnings and the pragma warnings. again a bit disappointing that an out of the box example would have so many warnings). There are several fdXXXX() argument warnings that are of concern. 

    Compile Warnings:

    Description	Resource	Path	Location	Type
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 271	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 482	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 540	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 555	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 264	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 272	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 278	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 287	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 293	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 302	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 308	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'int32_t' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 331	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'Qmss_QueueHnd' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 335	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 2 has type 'uint32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 340	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 3 has type 'int32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 302	C/C++ Problem
    format '%d' expects argument of type 'int', but argument 3 has type 'uint32_t' [-Wformat=]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 340	C/C++ Problem
    format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 272	C/C++ Problem
    format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 287	C/C++ Problem
    format '%x' expects argument of type 'unsigned int', but argument 3 has type 'long unsigned int' [-Wformat=]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 302	C/C++ Problem
    ignoring #pragma DATA_ALIGN  [-Wunknown-pragmas]	policy_dsp_arm.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 2	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 74	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 78	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 79	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 80	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 87	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 89	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 107	C/C++ Problem
    ignoring #pragma DATA_SECTION  [-Wunknown-pragmas]	policy_dsp_arm.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 1	C/C++ Problem
    implicit declaration of function 'System_flush' [-Wimplicit-function-declaration]	helloWorld.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 188	C/C++ Problem
    passing argument 1 of 'fdsetRemoveEntry' from incompatible pointer type	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 123, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\socketndk.h	C/C++ Problem
    passing argument 1 of 'fdsetTestEntry' from incompatible pointer type	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 125, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\socketndk.h	C/C++ Problem
    passing argument 2 of 'fdSelect' from incompatible pointer type	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 317, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\sys\socket.h	C/C++ Problem
    passing argument 2 of 'Pa_getBufferReq' from incompatible pointer type	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 262	C/C++ Problem
    passing argument 3 of 'fdSelect' from incompatible pointer type	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 317, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\sys\socket.h	C/C++ Problem
    passing argument 3 of 'Pa_getBufferReq' from incompatible pointer type	nimu_pa_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 262	C/C++ Problem
    passing argument 4 of 'fdSelect' from incompatible pointer type	NIMU_emacExample_EVMK2H_armBiosExampleProject		line 317, external location: C:\ti\ndk_2_25_01_11\packages\ti\ndk\inc\bsd\sys\socket.h	C/C++ Problem
    unused variable 'i' [-Wunused-variable]	helloWorld.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 134	C/C++ Problem
    unused variable 'pHostDesc' [-Wunused-variable]	nimu_cppi_qmss_iface.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 396	C/C++ Problem
    variable 'tmp' set but not used [-Wunused-but-set-variable]	udpHello.c	/NIMU_emacExample_EVMK2H_armBiosExampleProject	line 54	C/C++ Problem
    

  • Hey Mike,

    You can use the BSD socket APIs within the daemon function – just call the BSD API with the SOCKET that was passed in as an argument, and cast it to an int. Also, change the function signature to use “void *” instead of “SOCKET”.

    For example:

    #include <sys/socket.h>
    
    void myDaemonFxn(void *skt, uint32_t unused)
    { 
    
        send((int)skt, someDataBuffer, amtToSend, 0);
    
    …
    }

    This will work because the NDK BSD socket() API just calls the legacy socket() API underneath and casts the SOCKET to an int and returns it (the file descriptor is really the address of the socket …).

    Regards,

    Dalton

  • Hi Mike,

    Did this get resolved?

    Thanks,
    Todd
  • Todd I backed away from trying to use BSD. My design requires using Poll() and other functions described in the NDK API. I don't know what else will bite me. IMO the BSD is not ready for prime time here. All these work arounds; casting, creating stubs for fdOpenSession, fdEtc, to get it to compile does not give me a warm fuzzy feeling. The NDK manual needs some serious revision if BSD is going to remain as it is going forward. Sorry to be sounding negative but trying to be honest.
    Thanks for your help.
    Mike
  • Hi Mike,

    We have made improvements to the BSD support in newer NDK versions, but that means moving to newer SYS/BIOS, etc. components also....and if you have something working, it's probably not the time to bite that off.

    We appreciate the feedback. I'll forward this thread to the development manager in charge of the NDK. If necessary, can we follow-up with you offline?

    Todd
  • Hi Todd.

    Sure you are welcome to follow up . I assume you have access to my email info.

    Mike