NetBurner 3.5.0
PDF Version
 
Dynamic Web Content

Dynamic web content (web content created at run-time) can be created using the following NetBurner HTML tags:

  • A C++ callback function CPPCALL:
    <!--CPPCALL YourCppFunction -->
    When the web server delivers the web page it will call your C++ function.
  • The VARIABLE tag:
    <!--VARIABLE YourVariable -->
    . When the web server delivery the web page it will insert the specified variable.
  • A C callback function FUNCTIONCALL. This tag is included for those porting from older tool sets. FUNCTIONCALL is a C only tag, and is deprecated.

Dynamic Content Using the CPPCALL Tag

To add a C++ callback we can add a function to main.cpp such as:

void webHelloWorld( int sock, PCSTR url )
{
writestring( sock, "Hello World" );
}
int writestring(int fd, const char *str)
Write a null terminated ascii string to the stream associated with a file descriptor (fd)....



And modify index.html to add the tag:

<html>
<body>
The main page for the AppWizard project.
<!--CPPCALL webHelloWorld -->
</body>
</html>



When a web client requests the index.html page, the web server will begin steaming the static content. When it gets to the CPPCALL, it will call the webHelloWorld() function, which will send "Hello World" to the client using the open TCP socket. When the function returns, the web server will continue sending the index.html web page. A web page can have any number of CPPCALL and VARIABLE tags. The parameters passed to the CPPCALL function are the open TCP socket handle, and a pointer to a constant string containing the entire URL of the client web browser. In this way your function can parse the URL if that is important to your application. For example, anything after a '?' character is application specific.

Displaying Variables Using the VARIABLE Tag

Variables in an application can be displayed on a web page using the VARIABLE tag. This can be useful for displaying information such as time, an IP address, temperature, etc. The format of the tag 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 -->
#define TICKS_PER_SECOND
System clock ticks per second.
Definition nbrtos/include/constants.h:41

The VARIABLE tag is processed during the compilation of the application by parsing the text between <!--VARIABLE and the trailing --> and converting it into a function call such as: WriteHtmlVariable( fd, TimeTick/TICKS_PER_SECOND ); In this function fd is a file descriptor to the current TCP connection to the web client.

Variable types are handled with C++, but you do not need to know anything about C++ to use this feature.
The parameter types are defined by the function definitions located in \nburn\include\htmlfiles.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);
Used to store and manipulate MAC addresses.
Definition nettypes.h:69

In addition, we have included a class named IPCAST() that takes a 32-bit value and converts it into an IP address format (e.g. 192.168.1.2). This example will display the IP address in dotted notation, rather than a 32-bit integer:

IP address: <!--VARIABLE IPCAST(IpAddress) !-->



Linking to Variables Using the INCLUDE Tag and htmlvar.h Header File

When an application with web content is built by the NetBurner tools, a file named htmldata.cpp is created. When using the VARIABLE tag for dynamic data, the application must be able to link to the variable. This can be achieved in either of two ways:

  • Create a header file in your project named htmlvar.h.
  • Use the INCLUDE HTML tag in the .html folder.

    An example of a htmlvar.h file to display a variable named Temperature, and a VARIABLE function callback named webMyVarFunction() to display an integer:
#ifndef HTMLVARS_H_
#define HTMLVARS_H_
#include <startnet.h>
extern int Temperature;
const char *webMyVarFunction(int fd, int v);
#endif // HTMLVARS_H_



Function Callbacks With Variables Using the VARIABLE Tag

If you need to use a function that can take a parameter, the CPPCALL tag will not work because the parameters are fixed as the socket fd and URL. However, the VARIABLE tag be used for both the function call and variable parameter. The htmlvar.h include file must specify the function definition in one of the formats described in the previous section. the format below. For example, to pass an integer variable webMyInteger in a callback function named webMyFunction:

const char * webMyVarFunction(int fd, int webMyInteger);

The HTML file calls the function with:

<!--VARIABLE MyFunction(fd, webMyInteger) -->

When the application is compiled the resultant function call will be:

WriteHtmlVariable( fd, MyFunction(fd, webMyInteger) );

In your .cpp source code the function could look something like this:

const char * webMyVarFunction(int fd, int v)
{
char buffer[255];
siprintf( buffer, "MyFunction() was called with v = %d\r\n", v );
writestring( fd, buffer );
return "\0"; // Return a const char * of zero length so it will not print to the HTML page
}

Extending the VARIABLE tag for User Defined Types

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

struct my_struct {
int i;
char buf[80];
uint32_t dVal;
} MY_STRUCT;
MY_STRUCT MyStruct;

In your include file add the function definition: void WriteHtmlVariable(int fd, MY_STRUCT MyStruct); You then need to implement the function to display the data. For example:

void WriteHtmlVariable(int fd, MY_STRUCT MyStruct)
{
fdprintf(fd, "int: %d<br>", i);
fdprintf(fd, "buf: %s<br>", buf);
fdprintf(fd, "uint32_t: %ld<br>", dVal);
}
int fdprintf(int fc, const char *format,...)
printf to a file descriptor

You can then display it on the web page with the VARIABLE tag:

<!--VARIABLE MyStruct -->