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.

Sendindg data from the website to the board

Other Parts Discussed in Thread: EK-TM4C1294XL

Hi everypone, I'm working with the example enet_io of mi Tiva C board (ek-TM4C1294xl). I watn to modify one of the set functions (C, JavaScript) in order to send certain data to the board. I have been trying to modify the function but I don't understand how these pieces of codes works at all. 

JavaScript code that open the communication:

  req.open("GET", "/cgi-bin/set_speed?percent=" + speed_txt.value +
                        "&id" + Math.random(), true);
        req.onreadystatechange = speedComplete;
        req.send(null);

And the red coloured C function is:

else if(ustrncmp(pcName, "/cgi-bin/set_speed?percent=", 12) == 0)
    {
        static char pcBuf[];

        //
        // Extract the parameter and set the actual speed requested.
        //
        io_set_animation_speed_string((char*)pcName + 27);

        //
        // Get the current speed setting as a string to send back.
        //
        io_get_animation_speed_string(pcBuf, 6);

        psFile->data = pcBuf;
        psFile->len = strlen(pcBuf);
        psFile->index = psFile->len;
        psFile->pextension = NULL;
        return(psFile);
    }

My problem is with the arguments of the blue coloured function, I don't know why de argument add 27 to the pcName pointer. I assume that the data is in that adress. Someone knows how this example send the data to the board?

Thanks in advanced!

 

MARTIN C!

  • Martin Castro said:
    My problem is with the arguments of the blue coloured function, I don't know why de argument add 27 to the pcName pointer. I assume that the data is in that adress.

    This number, 27, comes from the number of characters in "/cgi-bin/set_speed?percent=" string.

    This string is seen in this code line,

    else if(ustrncmp(pcName, "/cgi-bin/set_speed?percent=", 12) == 0)

    ustrncmp() compares pcName[] with the match string, starting from the top of pcName[]
    The match string has 27 characters, but above code compares just the first 12...
    If the code would correctly assign 27, instead of 12, you should be immediately aware of the source of this number ;-)

    Tsuneo

  • Thanks Tsuneo, I understand the C implementation of the function, but my problem is the functionallity of the JavaScript-C interaction. For example:
    This is the entire JavaScript function that sends the datal:

    function speedSet()
    {
        var req = false;
        var speed_txt = document.getElementById("speed_percent");
        function speedComplete()
        {
            if(req.readyState == 4)
            {
                if(req.status == 200)
                {
                    document.getElementById("current_speed").innerHTML =
                                            "<div>" + req.responseText + "</div>";
                }
            }
        }
        if(window.XMLHttpRequest)
        {
            req = new XMLHttpRequest();
        }
        else if(window.ActiveXObject)
        {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if(req)
        {
            req.open("GET", "/cgi-bin/set_speed?percent=" + speed_txt.value +
                            "&id" + Math.random(), true);
            req.onreadystatechange = speedComplete;
            req.send(null);
        }
    }

    The C code works comparing the red coloured string label. My questions about that are:

    • Does the interrogation (?) and the equal (=) symbols plays an especial roll in the send of the data or are just one character more?
    • Is the "&id" after the + symbol, the reference of the speed_txt.value?

    Then the C code:

     //
        // Set the animation speed?
        //
        else if(ustrncmp(pcName, "/cgi-bin/set_speed?percent=", 12) == 0)
        {
            static char pcBuf[6];

            //
            // Extract the parameter and set the actual speed requested.
            //
            io_set_animation_speed_string((char*)pcName + 27);

            //
            // Get the current speed setting as a string to send back.
            //
            io_get_animation_speed_string(pcBuf, 6);

    Basically it calls the blue coloured function, and the argument is a  character pointer who points 27 characters after the pcName adress:

    • Is the data send in that adress?

    And finally the blue and orange coloured functions:

    void
    io_set_animation_speed_string(char *pcBuf)
    {
        unsigned long ulSpeed;

        //
        // Parse the passed parameter as a decimal number.
        //
        ulSpeed = 0;
        while((*pcBuf >= '0') && (*pcBuf <= '9'))
        {
            ulSpeed *= 10;
            ulSpeed += (*pcBuf - '0');
            pcBuf++;
        }

        //
        // If the number is valid, set the new speed.
        //
        if(ulSpeed <= 100)
        {
            g_ulAnimSpeed = ulSpeed;
            io_set_timer(g_ulAnimSpeed);
        }
    }

    //*****************************************************************************
    //
    // Set the speed of the animation shown on the display.
    //
    //*****************************************************************************
    void
    io_set_animation_speed(unsigned long ulSpeed)
    {
        //
        // If the number is valid, set the new speed.
        //
        if(ulSpeed <= 100)
        {
            g_ulAnimSpeed = ulSpeed;
            io_set_timer(g_ulAnimSpeed);
        }
    }

    First of all I don't get how the data is obtain in a long format variable (ulSpeed) at the green coloured while:

    I assume that it is validitying the caracthers because there are percent values, but if I want to get the data received in a common uint_32t variable:

    • How can I modify this piece of code?

    I know I made a lot of questions, but the truth is that I've been working a lot with this example and I don't understand these issues.

    Thanks in advanced! Regards! :)

    MARTIN C!

     

  • If you would understand this fact, your questions are self-explanatory.

    Receiving a URI string sent by XMLHttpRequest object in the client JavaScript, lwIP stack calls fs_open() callback with a string pointer to the URI. The stack parses neither '?' nor '=' on the URI string. It is left for your firmware in fs_open().

    Tsuneo

  • Hi Tsuneo I have been reading about the issue, I wanted to know if::

    • All URLs in a request represents the path of a file in server, why some ones starts with /cgi-bin/... (like the case mentiones above) and another just a name who is compared in the c code like 

    <script>

    led.open("GET", "/ledstate?id=" + Math.random(), true);
    led.onreadystatechange = ledComplete;
    led.send(null);

    </script>

    //c code

    else if(ustrncmp(pcName, "/ledstate", 9) == 0)
    {
    static char pcBuf[4];

    //
    // Get the state of the LED
    //
    io_get_ledstate(pcBuf, 4);

    psFile->data = pcBuf;
    psFile->len = strlen(pcBuf);
    psFile->index = psFile->len;
    psFile->pextension = NULL;
    return(psFile);
    }

    1. The webpage of the enet_lwip example has a window in wich the update is done via cgi/ssi, Is the the mentioned part of the path is connected with it?
    2. The ? symbol is used when sending information by the request, and the = after a variable name to get the information from the client. Examples of these are:
    • req.open("GET", "/cgi-bin/toggle_led?id" + Math.random(), true);
    •  led.open("GET", "/ledstate?id=" + Math.random(), true);

    That's fine by me but Idon't understand why in one case the url stars with /cgi-bin/ and in the other cases no. And finally the other url is when sending data from the client:

    • req.open("GET", "/cgi-bin/set_speed?percent=" + speed_txt.value +"&id" + Math.random(), true);

    when speed_txt.value represents the value set via web by the client. But why is +"&id" + in the URL? what represents?

    As you see I'm a newie in the topic and I'm trying to understand the code, that's why i ask a lot :)  anykind of help is welcome! Thanks in advanced, Regards

    Martin C!