NetBurner 3.1
Dynamic Web Content

Dynamic content can be generated at run-time on your NetBurner device. For example, you can a web page for an instrument in which the content changes to match the current state of the instrument, such as gauges, light indicators, switches, etc. This can done by creating a web page and inserting a variable, JavaScript, or generating the entire page. There are many examples of this in the folder.

The methods for creating dynamic content are:

  1. Inserting function call tags in a HTML document
  2. Inserting variable tags in a HTML document
  3. Creating a HTTP GET request handler
  4. Creating a HTTP POST hander
  5. Web Sockets, AJAX, and JavaScript

The CPPCALL and FUNCTIONCALL Tags

When the web server encounters a CPPCALL tag it will call the corresponding function in your application code. Note that the space characters are important: ``

In your application source code create a function with the signature:

void YourFunctionName( int sock, PCSTR url )
{
// add your dynamic code here
}

For example, to display the web client IP address and port number that made the web page request:

<html>
<body>
Client Address: <!--CPPCALL webShowClientInfo -->
</body>
</html>

In your application:

void webShowClientInfo( int sock, PCSTR url )
{
IPADDR localIP = GetSocketLocalAddr(socket);
uint32_t remotePort = GetSocketRemotePort(socket);
localIP.fdprint(socket); // Print IP address
fdprintf(socket, ": %d<br>\r\n", remotePort);} // Print port number
}

Prior to the 2.8.x development tools release the function call tags were called as C functions instead of C++ functions. For reverse compatibility the tag can still be used. To prevent C++ name mangling you must add the extern “C” declaration at the top of your application file for each of your web functions. For example,

extern "C"
{
void webFunction1( int sock, PCSTR url );
void webFunction2( int sock, PCSTR url );
}

The VARIABLE Tag

The VARIABLE tag has two uses:

  1. Simply inserting a variable or expression
  2. Calling a function with variable parameters

To display a variable the format is:

<!--VARIABLE <name> -->

Where <name> is the name of the application variable or an expression. For example, the system time tick variable, TimeTick can be displayed with

<!--VARIABLE TimeTick -->

Or you can display the time in seconds with the equation

<!--VARIABLE TimeTick/TICKS_PER_SECOND -->

The VARIABLE tag is processed during the compilation of the application by parsing the text between “<!—VARIABLE” and the trailing “ -->”, then the NetBurner tools automatically convert it into a function call with a signature that contains a file descriptor and variable. For example, ( fd, TimeTick/TICKS_PER_SECOND );

The following parameter types are defined by the functions located in .h:

void WriteHtmlVariable(int fd, char c);
void WriteHtmlVariable(int fd, int i);
void WriteHtmlVariable(int fd, short i);
void WriteHtmlVariable(int fd, long i);
void WriteHtmlVariable(int fd, BYTE b);
void WriteHtmlVariable(int fd, WORD w);
void WriteHtmlVariable(int fd, unsigned long dw);
void WriteHtmlVariable(int fd, const char *);
void WriteHtmlVariable(int fd, MACADR ip);

The INCLUDE Tag and htmlvar.h Header File

The functions that are created from the VARIABLE tags are located in an auto-generated file named htmldata.cpp. Since these functions reference the variable names, there must be a way for the linker to resolve them. For example, to display TimeTick the application would need to include <nbrtos.h>, otherwise a linker error will occur.

Include files can be handled two ways: you can use an <INCLUDE headername.h> tag in the HTML code, or you can create a header file in your project with the name “htmlvar.h” that will be automatically included if no tags are detected in the HTML source code.
An example htmlvar.h file that exposes two variables, the <nbrtos.h> header file, and a function that takes an integer parameter is shown below:

#ifndef HTMLVARS_H_
#define HTMLVARS_H_
#include <nbrtos.h> // For access to TimeTick
extern int gIntVal;
extern char gStrVal[];
const char *FooWithParameters(int fd, int v);
#endif /*HTMLVARS_H_*/

An example HTML file using the INCLUDE tag:

<html>
<body>
<!--INCLUDE myIncludeFile.h -->
Value = <!--VARIABLE MyVar --><br>
</body>
</html>

Calling a Function with Parameters

If you need to specify a function call, but need to pass a parameter, the CPPCALL and FUNCTIONCALL tags will not work because the parameters are fixed as the socket file descriptor and URL. In this case we can use the VARIABLE tag to achieve the functionality of calling a function with a variable.

The include file (e.g. htmlvar.h) must specify the function definition in the format below. In this case we are passing an integer value ‘v’. The first parameter must always be the socket file descriptor.

const char * myIntFunction(int fd,int v);

The HTML source code then used the VARIABLE tag with the function definition below. In this example we are passing the integer value of TimeTick.

<!--VARIABLE MyIntFunction(fd,TimeTick) -->

When the application is compiled the function created will be: WriteHtmlVariable( fd, MyFunction(fd,TimeTick) );

This function returns an empty string, which will have no effect on the web page. An example of what a function might do is shown below:

const char *FooWithParameters(int fd, int v)
{
fdprintf(fd, "This function was called with parameter v = %d\r\n", v);
return "\0"; // Return a const char * here of zero length so it will
not print anything
}

Extending the VARIABLE Tag

The VARIABLE functionality can be extended to support user defined types, such as displaying a user defined structure or class. Let’s say you have a structure you want to display on a web page called myStruct:

struct my_struct {
int i;
char buf[80];
DWORD dVal;
} MY_STRUCT;
MY_STRUCT myStruct;

In your include file add the function definition: void WriteHtmlVariable(int fd, MY_STRUCT myStruct);

Now you can display it on the web page with the VARIABLE tag:

<!--VARIABLE myStruct -->

What this look like behind the scenes is: WriteHtmlVariable( fd, myStruct );

Note that you still have to write the implementation of the above function. The function below is the source code for the MAC address type already defined by the system:

void WriteHtmlVariable(int fd, MACADR ma)
{
PBYTE lpb = ( PBYTE ) &ma;
for (int I = 0; I < 5; i++)
{
fd_writehex( fd, lpb[i] );
write(fd,":",1);
}
fd_writehex( fd, lpb[6] );
}