Testing the Library

Jump to: navigation, search
<< Before Running the Code
Using Ethernet or PPP >>



Now you are ready to start using Treck protocols in loopback mode. The loopback address is defined as 127.0.0.1. You should setup a simple client/server to make sure that packets traverse all the way down the stack and back up. The loopback driver sits below TCP/IP so it is a good test to see if you have things working thus far. If you are not familiar with "sockets" programming, then you should review the Introduction to BSD Sockets. Other good reference material on this subject would include, "Introduction to TCP/IP Volume 3 (BSD Edition)" by Douglas E. Comer, and "UNIX Network Programming Volume 1", by W. Richard Stevens. After you have completed the loopback test, you are ready to move onto the next section.

The reason that we suggest that you complete the loopback test first is to reduce the amount of items that you are attempting to debug. It is VERY difficult to debug your RTOS interface, Timer interface, Device Driver, and Application all at the same time. From our experience, we have found that most people have trouble with the device driver (because it is where the software meets the hardware). If you test first with loopback, you can isolate your debugging to the RTOS, Timer, and Loopback application code first and debug the device driver last. One interesting item about Treck protocols is that you do not need to recompile the protocol stack in order to add your device driver later, so performing this task should not be too much of a burden. If you are not familiar with sockets programming, the loopback interface is a great place to learn.


Warning Warning: You should not attempt to add your device driver until you are confident that the stack is working correctly in loopback mode.


We have included an example of using the loopback interface. It is not intended to be an example of using the protocol stack for high performance. It is written to be easy to understand and follow. Please feel free to use this code as a guide in writing your loopback test program.

Example Loopback Application

#include <trsocket.h>
#include <stdio.h> /* for printf */
 
#define TM_PORT_LOOPBACK_TEST htons (10)
 
char bufferArray[1024];
 
/*
 * Example of a UDP socket loop back send and receive.
 */
void main (void)
{
    int                errorCode;
    int                failed;
    struct sockaddr_in addr;
    unsigned long      ipAddr;
    int                sd;
    int                len;
    int                recvdLength;
    int                sentLength;
 
    failed = 0;
    sd = TM_SOCKET_ERROR;
    recvdLength = TM_SOCKET_ERROR;
    sentLength = TM_SOCKET_ERROR;
    ipAddr = 0UL;
/* Amount of data to send, and receive */
    len = sizeof(bufferArray);
/* Example setting the tick length at 10 milliseconds */
    errorCode = tfInitTreckOptions(TM_OPTION_TICK_LENGTH,
                                   (unsigned long)10);
    if (errorCode != TM_ENOERROR)
    {
        printf("tfInitTreckOptions failed %d\n", errorCode);
        failed = 1;
    }
    if (failed == 0)
    {
/* Start the Treck initialization */
        errorCode = tfStartTreck ();
    }
    if (errorCode != TM_ENOERROR && failed == 0)
    {
        printf("tfStartTreck failed %d\n", errorCode);
        failed = 1;
    }
    if (failed == 0)
    {
/* Open a UDP socket */
        sd = socket (PF_INET, SOCK_DGRAM, IP_PROTOUDP);
    }
 
    if ((sd == TM_SOCKET_ERROR) && (failed == 0))
    {
/* Retrieve the socket error for failed socket call */
        errorCode = tfGetSocketError(sd);
        printf("socket failed %d\n", errorCode);
        failed = 1;
    }
    if (failed == 0)
    {
/* Bind the UDP socket to a well known UDP port */
        addr.sin_family = PF_INET;
        addr.sin_port = TM_PORT_LOOPBACK_TEST;
        addr.sin_addr.s_addr = 0;
        errorCode = bind(sd,
                         (struct sockaddr TM_FAR *)&addr, 
                         sizeof(struct sockaddr_in));
    }
    if ((errorCode == TM_SOCKET_ERROR) && (failed == 0))
    {
/* Retrieve the socket error for failed bind call */
        errorCode = tfGetSocketError(sd);
        printf("bind failed %d\n", errorCode);
        failed = 1;
    }
    if (failed == 0)
    {
/* Send some loop back data to our own socket */
        addr.sin_addr.s_addr = inet_addr ("127.0.0.1");
        sentLength = sendto(sd,
                            bufferArray,
                            len,
                            0,
                            (struct sockaddr TM_FAR *)&addr,
                            sizeof(struct sockaddr_in));
        if ((sentLength == TM_SOCKET_ERROR) && (failed == 0))
        {
/* Retrieve the socket error for failed sendto call */
            errorCode = tfGetSocketError(sd);
            printf("sendto failed %d\n", errorCode);
            failed = 1;
        }
        if (failed == 0)
        {
            printf("%d sent successfully\n", sentLength);
/* Receive some loop back data from our own socket */
            recvdLength = recvfrom(sd,
                                   bufferArray,
                                   len,
                                   0,
                                   (struct sockaddr TM_FAR *)0,
                                   0);
        }
 
        if ((recvdLength == TM_SOCKET_ERROR) && (failed == 0))
        {
/* Retrieve the socket error for failed recvfrom call */
            errorCode = tfGetSocketError(sd);
            printf("recvfrom failed %d\n", errorCode);
            failed = 1;
        }
        if (failed == 0)
        {
            printf("%d received succesfully\n", recvdLength);
        }
    }
    if (sd != TM_SOCKET_ERROR)
    {
/* All done. Close the socket */
        (void)tfClose(sd);
        if (failed == 0)
        {
            printf("UDP LOOP BACK Test success\n");
        }
    }
}


<< Before Running the Code
Using Ethernet or PPP >>