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.

Why I need to use "fdOpenSession(TaskSelf())" in sendto() from another thread?

Other Parts Discussed in Thread: SYSBIOS

Hi

I am working with UDP socket.

Using udpEcho example, I could write my application without problem.

But, when I tried to send a datagram from another thread using sendto(), always this function returned -1.

The problem was that the same routine that sends the buffer through sendto() works with the thread that received a buffer using recvfrom(), but  does not work from another thread that doesn't receive anything.

Googling a lot, I found some posts that discussed this kind of issue. And, in one, I found that we need to place  "fdOpenSession(TaskSelf())" in the beginning of the routine that sends the datagram and "fdCloseSession(TaskSelf());" at the end.

I did it and it WORKED!!!!

That's amazing, but, I don't have a minimum idea why it worked.

Can someone explan why "fdOpenSession(TaskSelf())" works?

Thanks.

Sergio

  • Hi Sergio,

    The fdOpenSession function plugs in some information (e.g. semaphore and file description table) into the TI-RTOS Kernel task's environment. Each Task which uses the stack needs these items.

    There is a configuration parameter that allows you to avoid this. Look in the .cfg file for the TCP Echo example.

    var Global    = xdc.useModule('ti.ndk.config.Global');
    ...
    Global.autoOpenCloseFD = true;

    With the autoOpenCloseFD parameter set to true, you do not have to call fdOpenSession in the code (for dynamically created tasks). The downside is that every task in the application will call fdOpenSession as part of a create hook. Even ones that do not use the stack. For more details/restriction about this configuration parameter, please refer to the cdoc information.

    Todd

     

  • Todd

    The spru524i manual explain about fdOpenSession in page 39:

    3.1.2.1 When to Initialize the File Descriptor Environment

    But, my compiler still warns about TaskSelf() : declared implicitly.

    How to declare TaskSelf() explicitly, please?

    Thanks

    Sergio

  • It is ti/ndk/inc/os/osif.h. This file should be pulled in by the following

    #include <ti/ndk/inc/netmain.h>

  • Hi Todd

    Didn't work.

    If I place #include <ti/ndk/inc/netmain.h> following errors appears:

    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 72: error #102: "IP6N" has already been declared in the current scope
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 80: error #258: invalid redeclaration of type name "IP6N" (declared at line 66 of "C:\ti\tirtos_tivac_2_10_01_38\products\ndk_2_24_01_18\packages\ti\ndk\inc\usertype.h")
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 116: error #249: function "FD_ZERO" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 127: error #249: function "FD_CLR" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 138: error #249: function "FD_ISSET" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 149: error #249: function "FD_SET" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 157: error #249: function "accept" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 165: error #249: function "bind" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 174: error #249: function "connect" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 183: error #249: function "getsockopt" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 192: error #249: function "listen" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 200: error #249: function "recv" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 208: error #249: function "recvfrom" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 217: error #249: function "send" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 225: error #249: function "sendto" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 234: error #249: function "setsockopt" has already been defined
    "C:/ti/tirtos_tivac_2_10_01_38/products/ndk_2_24_01_18/packages/ti/ndk/inc/bsd/sys/socket.h", line 244: error #249: function "socket" has already been defined


    Perhaps we need another header file.

    Thanks

    Sergio
  • It looks like you are using sys/socket.h. If you don't want to use the .cfg parameter to set it up for you automatically, I would just use Task_self() and include the kernel's Task.h

    #include <ti/sysbios/knl/Task.h>

     

    fdCloseSession(Task_self());

  • Hi Todd

    Yes, (Task_self()) worked.

    Thank you very much.

    Sergio