BL4S100 with XBee ZB

Digi BL4S100 with XBee ZB User manual

  • Hello! I am an AI chatbot trained to assist you with the Digi BL4S100 with XBee ZB User manual. I’ve already reviewed the document and can help you find the information you need or explain it in simple terms. Just ask your questions, and providing more details will help me assist you more effectively!
TCP/IP Volume 2
Users Manual
019-0144_G
Dynamic C Users Manual
Part Number 019-0144 Printed in the U.S.A.
Digi International Inc. © 2006-2010 All rights reserved.
Digi International Inc. reserves the right to make changes and
improvements to its products without providing notice.
Trademarks
Rabbit
®
and Dynamic C
®
are registered trademarks of Digi International Inc.
Windows
®
is a registered trademark of Microsoft Corporation
The latest revision of this manual is available at www.rabbit.com.
TCP/IP Volume 2 User’s Manual rabbit.com 3
TABLE OF CONTENTS
1. Introduction.....................................................................................................................................................7
2. Web-Enabling Your Application .............................................................................................................8
2.1 Designing Your Application...................................................................................................................8
2.2 The Smallest Web Server in the WWW...............................................................................................9
2.3 Web Server Architecture.......................................................................................................................11
2.3.1 Application Block ..........................................................................................................................12
2.3.2 HTTP Block ...................................................................................................................................13
2.3.3 HTTP Block Subcomponents.........................................................................................................14
2.3.4 Zserver Block.................................................................................................................................14
2.4 Architecture of a Toy Application.......................................................................................................16
2.5 A Simple but Realistic Application.....................................................................................................18
2.6 Adding Access Controls........................................................................................................................21
2.7 A Full-Featured Application.................................................................................................................27
2.8 Living Without RabbitWeb and FAT..................................................................................................32
3. Server Utility Library..................................................................................................................................36
3.1 Data Structures for Zserver.lib.............................................................................................................36
3.1.1 MIMETypeMap Structure..............................................................................................................37
3.1.2 ServerSpec Structure......................................................................................................................38
3.1.3 ServerAuth Structure......................................................................................................................40
3.1.4 ServerPermissions Structure ..........................................................................................................40
3.1.5 RuleEntry Structure........................................................................................................................41
3.1.6 ServerContext Structure.................................................................................................................41
3.1.7 SSpecStat Structure........................................................................................................................42
3.1.8 sspec_fatinfo Structure...................................................................................................................42
3.1.9 FormVar Structure..........................................................................................................................43
3.1.10 SSpecFileHandle Structure ..........................................................................................................43
3.2 Constants Used in Zserver.lib...............................................................................................................43
3.2.1 ServerSpec Type Field...................................................................................................................43
3.2.2 ServerSpec Vartype Field...............................................................................................................44
3.2.3 ServerPermissions Servermask Field.............................................................................................44
3.2.4 Configuration Macros ....................................................................................................................44
3.2.5 Macros for Control Data Initialization...........................................................................................46
3.3 File Compression Support.....................................................................................................................48
3.4 HTML Forms..........................................................................................................................................49
3.5 API Functions.........................................................................................................................................50
TCP/IP Volume 2 User’s Manual rabbit.com 4
4. HTTP Server................................................................................................................................................161
4.1 HTTP Server Data Structures.............................................................................................................162
4.1.1 HttpState.......................................................................................................................................162
4.2 Configuration Macros..........................................................................................................................165
4.2.1 Sending Customized HTTP Headers to the Client.......................................................................167
4.2.2 Saving Custom Headers from the Client......................................................................................168
4.3 Authentication Methods......................................................................................................................169
4.4 Setting the Time Zone.........................................................................................................................170
4.5 Sample Programs..................................................................................................................................170
4.5.1 Serving Static Web Pages ............................................................................................................170
4.5.2 Dynamic Web Pages Without HTML Forms...............................................................................173
4.5.3 Web Pages With HTML Forms ...................................................................................................177
4.5.4 HTML Forms Using Zserver.lib..................................................................................................183
4.6 HTTP File Upload................................................................................................................................188
4.6.1 What is a CGI Function and Why is It Useful? ...........................................................................188
4.6.2 How Do I Use the New CGI Facility?.........................................................................................189
4.7 API Functions for HTTP Servers.......................................................................................................209
5. RabbitWeb....................................................................................................................................................283
5.1 Getting Started: A Simple Example..................................................................................................283
5.1.1 Dynamic C Application Code for Humidity Detector .................................................................283
5.1.2 HTML Pages for Humidity Detector ...........................................................................................288
5.2 Dynamic C Language Enhancements for RabbitWeb....................................................................293
5.2.1 Registering Variables, Arrays and Structures..............................................................................293
5.2.2 Web Guards..................................................................................................................................294
5.2.3 Security Features..........................................................................................................................297
5.2.4 Handling Variable Changes .........................................................................................................299
5.3 ZHTML Scripting Language..............................................................................................................301
5.3.1 SSI Tags, Statements and Variables.............................................................................................301
5.3.2 Flow Control ................................................................................................................................302
5.3.3 Selection Variables.......................................................................................................................304
5.3.4 Checkboxes and RadioButtons ....................................................................................................305
5.3.5 Error Handling .............................................................................................................................306
5.3.6 Security: Permissions and Authentication ...................................................................................307
5.4 TCP to Serial Port Configuration Example......................................................................................308
5.4.1 Dynamic C Application Code......................................................................................................308
5.4.2 HTML Page for TCP to Serial Port Example ..............................................................................320
5.5 RabbitWeb Reference..........................................................................................................................322
5.5.1 Language Enhancements Grammar .............................................................................................322
5.5.2 Configuration Macros ..................................................................................................................323
5.5.3 Compiler Directives.....................................................................................................................324
5.5.4 ZHTML Grammar........................................................................................................................325
5.5.5 RabbitWeb Functions...................................................................................................................326
6. HTTP Client..................................................................................................................................................328
6.1 Configuration Macros..........................................................................................................................328
6.2 API Functions.......................................................................................................................................329
6.2.1 Initialization Functions.................................................................................................................329
6.2.2 Connect and Request Functions...................................................................................................329
6.2.3 Read Server Response Functions.................................................................................................329
6.2.4 Miscellaneous Functions..............................................................................................................329
6.2.5 Function Descriptions ..................................................................................................................330
TCP/IP Volume 2 User’s Manual rabbit.com 5
7. FTP Client.....................................................................................................................................................345
7.1 Configuration Macros..........................................................................................................................345
7.2 API Functions.......................................................................................................................................346
7.3 Sample FTP Transfer...........................................................................................................................352
8. FTP Server ...................................................................................................................................................353
8.1 Configuration Macros..........................................................................................................................354
8.2 File Handlers.........................................................................................................................................356
8.2.1 Replacing the Default Handlers...................................................................................................356
8.2.2 File Handlers Specification..........................................................................................................356
8.3 API Functions.......................................................................................................................................367
8.4 Sample FTP Server..............................................................................................................................374
8.5 Getting Through a Firewall ................................................................................................................375
8.6 FTP Server Commands .......................................................................................................................375
8.7 Reply Codes to FTP Commands........................................................................................................377
9. TFTP Client..................................................................................................................................................378
9.1 BOOTP/DHCP .....................................................................................................................................378
9.2 Data Structure for TFTP......................................................................................................................379
9.3 API Functions.......................................................................................................................................379
10. SMTP Mail Client.....................................................................................................................................385
10.1 Sample Conversation.........................................................................................................................385
10.2 SMTP Authentication........................................................................................................................386
10.3 Sample Sending of an E-mail...........................................................................................................387
10.4 Configuration Macros........................................................................................................................388
10.5 API Functions.....................................................................................................................................389
11. POP3 Client ...............................................................................................................................................397
11.1 Configuration......................................................................................................................................397
11.2 Steps to Receive E-mail....................................................................................................................398
11.3 Call-Back Function............................................................................................................................398
11.3.1 Normal call-back........................................................................................................................398
11.3.2 POP_PARSE_EXTRA call-back...............................................................................................398
11.4 API Functions.....................................................................................................................................399
11.5 Sample Receiving of E-mail.............................................................................................................401
11.5.1 Sample Conversation .................................................................................................................402
12. SNMP............................................................................................................................................................403
12.1 SNMP Overview................................................................................................................................403
12.1.1 Managed Objects........................................................................................................................404
12.1.2 SNMP Agent..............................................................................................................................404
12.1.3 MIBs...........................................................................................................................................405
12.1.4 SMI.............................................................................................................................................406
TCP/IP Volume 2 User’s Manual rabbit.com 6
12.2 Demo Program ...................................................................................................................................408
12.2.1 Creating Managed Objects.........................................................................................................409
12.2.2 Callback Functions.....................................................................................................................410
12.2.3 Creating Communities ...............................................................................................................412
12.2.4 Creating the MIB........................................................................................................................413
12.2.5 Defining Managed Objects with SMI........................................................................................415
12.2.6 Running the SNMP Agent .........................................................................................................419
12.3 Configuration Macros........................................................................................................................420
12.4 API Functions.....................................................................................................................................422
13. Telnet............................................................................................................................................................477
13.1 Telnet (Dynamic C 7.05 and Later) ................................................................................................477
13.1.1 Setup...........................................................................................................................................477
13.1.2 API Functions (Dynamic C 7.05 and Later) ..............................................................................478
13.2 Telnet (pre-Dynamic C 7.05)...........................................................................................................483
13.2.1 Configuration Macros ................................................................................................................483
13.2.2 API Functions.............................................................................................................................483
13.2.3 An Example Telnet Server.........................................................................................................485
13.2.4 An Example Telnet Client..........................................................................................................486
14. General Purpose Console ..................................................................................................................487
14.1 Zconsole Features..............................................................................................................................487
14.1.1 File System Requirement...........................................................................................................487
14.1.2 TCP/IP and Zconsole.................................................................................................................488
14.2 Login Name and Password...............................................................................................................488
14.3 Zconsole Commands and Messages................................................................................................488
14.3.1 Zconsole Command Data Structure...........................................................................................488
14.4 Zconsole Command Array................................................................................................................489
14.4.1 Zconsole Commands..................................................................................................................490
14.4.2 Zconsole Error Messages...........................................................................................................497
14.5 Zconsole I/O Interface.......................................................................................................................500
14.5.1 How to Include an I/O Method ..................................................................................................500
14.5.2 Predefined I/O Methods.............................................................................................................500
14.5.3 Multiple I/O Streams..................................................................................................................501
14.6 Zconsole Execution ...........................................................................................................................502
14.6.1 File System Initialization...........................................................................................................502
14.6.2 Serial Buffers .............................................................................................................................502
14.6.3 Using TCP/IP.............................................................................................................................502
14.6.4 Required Zconsole Functions.....................................................................................................503
14.6.5 Useful Zconsole Function..........................................................................................................504
14.6.6 Zconsole Execution Choices......................................................................................................511
14.7 Backup System...................................................................................................................................512
14.7.1 Data Structure for Backup System.............................................................................................512
14.7.2 Array Definition for Backup System .........................................................................................513
14.8 Zconsole Macros................................................................................................................................514
14.9 Sample Program.................................................................................................................................516
Index...................................................................................................................................................................520
TCP/IP Volume 2 User’s Manual rabbit.com 7
1. INTRODUCTION
The Dynamic C TCP/IP Users Manual, Vol. 2 is intended for embedded system designers and support
professionals who are using a Rabbit-based controller board. Most of the information contained here is
meant for use with Ethernet- or WiFi-enabled boards, but using only serial communication is also an
option. Knowledge of networks and TCP/IP (Transmission Control Protocol/Internet Protocol) is assumed.
For an overview of these two topics a separate manual is provided, An Introduction to TCP/IP. A basic
understanding of HTML (HyperText Markup Language) is also assumed. For information on this subject,
there are numerous sources on the Web and in any major book store.
The Dynamic C implementation of TCP/IP comprises several libraries. The main library is
DCRTCP.LIB. As of Dynamic C 7.05, this library is a light wrapper around DNS.LIB, IP.LIB,
NET.LIB, TCP.LIB and UDP.LIB. These libraries implement DNS (Domain Name Server), IP, TCP,
and UDP (User Datagram Protocol). This, along with the libraries ARP.LIB, ICMP.LIB, IGMP.LIB
and PPP.LIB are the transport and network layers of the TCP/IP protocol stack.
The Dynamic C libraries:
BOOTP.LIB
FTP_SERVER.LIB
FTP_CLIENT.LIB
HTTP.LIB
HTTP_CLIENT.LIB
POP3.LIB
SMNP.LIB
SMTP.LIB
TFTP.LIB
VSERIAL.LIB
implement application-layer protocols. Except for BOOTP, which is described in the Dynamic C TCP/IP
Users Manual, Vol. 1, these protocols are described here in the following chapters.
All user-callable functions are listed and described in their appropriate chapter. Example programs
throughout both volumes of the manual illustrate the use of all the different protocols. The sample code
also provides templates for creating servers and clients of various types.
To address embedded system design needs, additional functionality has been included in Dynamic C’s
implementation of TCP/IP. There are step-by-step instructions on how to create HTML forms, allowing
remote access and manipulation of information. There is also a serial-based console that can be used with
TCP/IP to open up legacy systems for additional control and monitoring. The console may also be used for
configuration when a serial port is available. HTML forms and the console are discussed in Chapter 4 and
Chapter 14, respectively.
Multiple interfaces are supported starting with Dynamic C version 7.30.
TCP/IP Volume 2 User’s Manual rabbit.com 8
2. WEB-ENABLING YOUR APPLICATION
This chapter, and the next three, describe how to add web browser control to your application. Web-
enabling is a logical and appealing choice for adding a user interface to your application, since the neces-
sary hardware (an Ethernet or serial port) is available on all Rabbit core modules and SBCs. Most users of
your application will be familiar with at least one web browser (Netscape, Mozilla, Internet Explorer,
Opera), with its graphical user interface, so they will be ready to start controlling your application with
minimal training.
This chapter provides an overview of the steps you will need to take to web-enable an application. Knowl-
edge of browsers, and something of their capability, is assumed. With this knowledge, you can understand
the concepts described in this chapter. The following chapters go into more detail about the specific librar-
ies; but for simple programs, you may be able to use just the information in this chapter along with the
sample code to write a working application.
Dynamic C provides libraries that implement most of the functions required to implement a web server,
more formally known as an HTTP (HyperText Transfer Protocol) server. (The browser is formally called
an HTTP client). You only need to write code specific to your application, such as dealing with I/Os and
the Rabbit peripheral devices, and possibly some code to help the HTTP server generate the appropriate
responses back to the users web browser. In addition, there is a small amount of “boilerplate” that needs to
be written to include and configure the HTTP server and any ancillary libraries such as the TCP/IP suite
and filesystems.
2.1 Designing Your Application
Should you decide to web-enable your application, you probably already have some idea of the format and
layout of the web pages that will be presented to the browser. Unless the application only returns informa-
tion and does not allow any updates (such as a data logger), you will probably need to lay out some forms.
Forms, in web parlance, allow the browser’s user to fill in some information then submit it to the server.
The server then performs the requested actions and sends a confirmation back to the browser. This is the
most common means for implementing control of the server as opposed to merely querying it.
There are several other things to consider. Answers to the following list of questions will determine the
pieces of software that need to be gathered into your application, and how they link together.
Does access to some or all resources need to be limited to a select set of users?
If so, how confident does your application need to be that the users credentials are valid?
Do you need to be able to upload large amounts of data (over, say, 250 bytes)?
Do you want to update the web pages themselves, or maybe even the entire application firmware?
Is the application small, medium, or large?
TCP/IP Volume 2 User’s Manual rabbit.com 9
Do you want to use this same (web) interface to configure all aspects of the application including, for
example, the network settings? In other words, is the web interface going to be the only interface once
the unit leaves the factory?
The first and second questions relate to user authentication and access control. The next two questions
relate to the HTTP upload facility. The last two questions concern the overall design of your application; in
particular, a large application may necessitate more storage than is usually available for a given Rabbit
product, and may require a sophisticated filesystem to manage the large number of resources.
Since the terms small, medium and large are rather vague, we shall define them by example. A small appli-
cation would be limited to less than 10 different web pages, and up to about 30 different “controls” (but-
tons to press, dials to twiddle, options to select etc.). A large application may have upwards of 100 pages,
and more than 10KB of configurable data. A medium application sits, as you might expect, near the mid-
dle of these.
Note that we are not considering the size of the application other than the web interface part. For example,
you may have a sophisticated G-code interpreter and motion control system, where the web interface is
limited to simply enabling/disabling the actuators and showing an error log to maintenance personnel. For
the purposes of our discussion, this would be a small application.
The next section describes a “smaller-than-small” application, that is, a toy, which we use to show the bare
essentials of a web-enabled application.
2.2 The Smallest Web Server in the WWW
Before moving on to real applications, the following sample code shows how to create the simplest possi-
ble web server. It does nothing but show “Hello WWW” on the browser. There are two files needed for
this. The first is the Dynamic C code to be loaded to the target board (which must support TCP/IP). The
second is the web page content itself, written in a syntax known as HTML (HyperText Markup Language).
The second file is effectively included in the program, using the #ximport directive.
// toy_http.c
#define TCPCONFIG 1
#use “dcrtcp.lib”
#use “http.lib”
#ximport “hellowww.html” hellowww_html
SSPEC_MIMETABLE_START
SSPEC_MIME(“.html”, “text/html”)
SSPEC_MIMETABLE_END
SSPEC_RESOURCETABLE_START
SSPEC_RESOURCE_XMEMFILE("/hellowww.html", hellowww_html)
SSPEC_RESOURCETABLE_END
void main() {
sock_init();
http_init();
for (;;) http_handler();
}
TCP/IP Volume 2 User’s Manual rabbit.com 10
The second file, named hellowww.html is coded as follows:
<HTML>
<HEAD><TITLE>Hello, WWW</TITLE></HEAD>
<BODY><H1>Hello, WWW</H1></BODY></HTML>
That’s all there is to it. However, there is actually a lot of activity going on beneath the covers. For a start,
the #use “dcrtcp.lib” directive and the TCPCONFIG macro definition bring in the TCP/IP networking suite
and configure it. Unless you have a private test network, you probably have to modify the default setting -
how to do that is beyond the scope of this chapter; it is described in volume 1 of the manual. The #use
“http.lib” statement is required in order to bring in the web server. The next lines down to the start of the
main() function are setting up tables that are consulted by the HTTP server and other libraries in order to
“do the right thing.” Finally, the main() function calls the necessary runtime initialization of the network
and the HTTP server. It then calls the HTTP server in an endless loop, which drives the entire system into
motion.
The .html file is ASCII text, in HTML syntax, which is transferred back to the browser when it is
requested. Apart from the server adding some header lines, the .html file is transferred verbatim. This
markup is merely telling the browser to display “Hello, WWW” as a 1st level heading, i.e., big bold text.
This is specified by the second line. The first line adds a title to the page, which most browsers display in
the window bar.
To see this web page on screen, the user needs to tell their browser what to get. If doing it manually, they
would need to enter something like “http://10.10.6.100/hellowww.html” in the browsers URL entry field.
The browser strips off the http://10.10.6.100 part of it, and sends the rest to the specified host address
(10.10.6.100) using a TCP connection to port 80 (interpreted from the http:// part). The server gets the /hel-
lowww.html part, which it knows about since it has a page of that name, and returns the contents of that
file as a response. The browser interprets the HTML it receives, and generates a nice visual rendition of
the contents.
TCP/IP Volume 2 User’s Manual rabbit.com 11
2.3 Web Server Architecture
Before describing a real application, it is useful to know how such an application is organized. The follow-
ing diagram shows all of the relevant components of a web-enabled application. There may seem to be a
large number of components, however keep in mind that not all components need to be used by your appli-
cation.
Figure 2.1 Components in a web-enabled application.
Resources
Requests,
Context
Rule
Table
User
Table
Battery-
Backed
RAM
#web
Variables
HTTP-X SSI
Dynamic
Resource
Table
FS2
FAT
Second
Flash
Serial
Flash
CGI
Functions
Metadata
Authorization
File Systems
Storage
(Resource Manager)
Compile-Time
Initialization
Run-Time
Initialization
Main
Loop
Application
Specifics, I/O
Application
HTTP
Static
Resource
Table
Program
Flash
MIME
Table
Zserver
Virtual File System
TCP/IP
TCP/IP Volume 2 User’s Manual rabbit.com 12
2.3.1 Application Block
At the top of this diagram is a block, called “Application,” consisting of five sub-blocks. The Application
block represents the code that you have to create. Everything below this is provided by the libraries,
although you will need to specify some parts of the interface to these components. This will be described
in detail in the following sections.
The application block is subdivided into 5 parts:
1. Compile-time initialization. This includes things like selection of the appropriate library modules;
initialization of static (constant) data structures and tables; selecting default network configuration;
and inclusion of static resources (external files) via the #ximport or #zimport directives. The
arrows leading from the “Compile-Time Initialization” sub-block indicate the tables that may be set
up at compile time; namely:
The MIME type mapping table. This mandatory table indicates to the browser how the content
is to be presented to the user. This is necessary for the browser, and needs to be specified by the
server, however the server does not need to be particularly aware of the details.
The rule table. This is only necessary if a filesystem is in use. It is used by the resource man-
ager to apply access permissions to the resources contained in a filesystem. This is necessary
because not all filesystems can associate file ownership and access rights with individual files.
The static resource table. This is the classic method of defining resources in Dynamic C. This
table is optional, since all necessary resources may be loaded in a filesystem, or in the dynamic
resource table. Most applications will contain at least a few static resources, as an initial default
or fallback, or for data that will never change such as a logo image.
Program flash. This really represents the loading of resource files into program memory via the
#ximport directive. There will almost always need to be a few #ximport files, but this can
be limited to a few kilobytes total.
2. Runtime initialization. Your main() function needs to call some specific library functions, once
only, when it starts:
sock_init(). This is always mandatory. It initializes the TCP/IP networking system.
sspec_automount(). This is optional. It initializes the available filesystems (FS2 and/or
FAT) for use by the resource manager, Zserver.
http_init(). This is mandatory. It initializes the HTTP server.
Various functions for setting up a user ID table, the rule table and/or the dynamic resource
table. These are optional, but would be used in the majority of applications. The user ID table
can only be initialized at run time, unlike the other tables that may, at least partially, be initial-
ized at compile-time.
3. Main loop. The final code in the main() function continuously calls http_handler() and
possibly other functions. This is mandatory, since it allows the HTTP server to process requests
from the network. Other functions may be specific to your application. For example, you may need
to poll an I/O device in order to obtain best performance.
4. Application specifics and I/O. This is really your part of the application or, if you like, the “back
end” to the HTTP server. There are a number of ways that your application can communicate with
the HTTP server. (These are not all shown on the diagram since it would add needless complexity.)
Your application can directly call functions in the HTTP server, in the resource manager (Zserver),
TCP/IP Volume 2 User’s Manual rabbit.com 13
in TCP/IP, and just about anywhere else. One very clean and powerful interface is provided via
#web variables, provided by RabbitWeb software which was introduced in Dynamic C 8.50.
5. CGI functions. CGI stands for “Common Gateway Interface,” however this acronym has a more
specific use in Dynamic C—it refers to a C function that is called by the HTTP server to generate
some dynamic content for the browser. This is the only truly optional block. Many applications can
be written without resorting to CGI functions; however, there are some cases where the power and
flexibility of a CGI will be required. Prior to Dynamic C 8.50, writing a robust CGI was the most
difficult part of the process. Starting with Dynamic C 8.50, there is a new style of CGI writing that
simplifies the process and reduces the chances of error. The old-style CGI is still supported for back-
wards compatibility.
2.3.2 HTTP Block
Let us now progress to the HTTP server itself. In the diagram, this is the block with two circles inside. The
server is responsible for fielding requests from the outside world. Each request is analyzed to determine
the resource that is being requested, the user who is making the request, and whether the user is authorized
to obtain that resource. If the resource is available, the user is known and has the proper permissions, then
the resource is transmitted back to the browser.
Following the above steps in more detail, we have:
1. Analyze the request: obtain the resource name. Part of the information provided by the browser is a
request header that contains a URL (Uniform Resource Locator). The URL is simply the name of
the resource to retrieve. URLs typically look like a file name in a Unix-style filesystem, that is, com-
ponent directory and file names separated by slash (/) characters.
2. Obtain the user ID. The browser has the option of sending the username and password of its user. If
it does not do this, then the userid is “anonymous.” If this is not good enough, then the browser can
always try again when it is denied a protected resource. On receipt of user credentials (name and
password), the HTTP server consults the resource manager (which in turn looks up the rule table) to
see if the users credentials are OK. If they are, then the resource manager also determines the
group(s) of which this user is a member. Thereafter, all access and permission checking is based on
the group, not the individual user.
1
3. Return the resource. Having verified the group access rights (if necessary), the resource is transmit-
ted back to the user. The resource may be an HTML or image file obtained from program memory
or a filesystem, or it may be a script file that is processed “on the fly” to generate markup language.
It may even represent a CGI function that will be called to generate all the necessary response. Note
that a complete response requires a small amount of header information to be prefixed to the actual
resource. The HTTP server usually takes care of this, however CGIs sometimes need to generate the
header themselves.
Referring to the diagram in Figure 2.1, you can see that there are several arrows leading in and out of the
HTTP server block. These represent lines of communication, and the arrow heads indicate the usual direc-
tion of data flow or, for function calls, “who calls whom.”
1. This is a necessary optimization. There may be hundreds of individual users; however, the majority of
these would be considered to be in a single “class,” with that class giving equal access to all its members.
Considering the class, i.e., group, as the entity that is requesting a resource reduces the amount of infor-
mation that needs to be stored.
TCP/IP Volume 2 User’s Manual rabbit.com 14
2.3.3 HTTP Block Subcomponents
The inner circles represent subcomponents of the server. The first of these, RabbitWeb, was introduced in
Dynamic C 8.50. RabbitWeb is an extension to C language syntax to simplify presentation of C language
objects (variables, structures) to a browser. RabbitWeb allows you to write web pages in a special scripting
language. The script makes it easy to generate HTTP, which is the format expected by the browser. In addi-
tion, the script allows the contents of your C language objects to be turned into HTML fragments for pre-
sentation by the browser. See Chapter 5 for details about RabbitWeb.
The small block named “#web Variables,” between the Application block and the RabbitWeb circle, indi-
cates that the #web variables are the means of communication between your application and the server.
Since #web variables are really just ordinary C variables, arrays or structures, they are extremely easy to
manipulate by your application. Since they also have the property of being registered with the web server,
the server has easy access too. (Registering an object with the web server is discussed in Chapter 5.)
The second circle in the HTTP server block represents the classic way of generating dynamic content. SSI
(Server Side Includes) is also a scripting language. It is not nearly as easy to use SSI as it is to use Rabbit-
Web; however, an SSI can generate the same content as a RabbitWeb script. It is just that you will need to
write CGI functions, and such functions can get large and complicated fairly quickly! In fact, SSI has the
ability to invoke CGI functions whereas RabbitWeb does not. In addition, SSIs have the ability to include
other resources directly in the primary returned resource much like how #include works in ANSI C.
The server also communicates with lower layers in the diagram. On the right hand side is the TCP/IP
block. This is the pipeline to the outside world, i.e., the browser. Usually only the server needs to talk
directly to TCP/IP (via a TCP socket). Prior to Dynamic C 8.50, it was often necessary for the applica-
tion’s CGI functions to call TCP/IP functions. This is no longer recommended. Instead, there are functions
in the HTTP server that should be called to mediate all networking calls.
2.3.4 Zserver Block
Directly under the HTTP server block is the Zserver, or resource manager, block. This is the “central tele-
phone exchange” of the entire application. It controls access to many of the other blocks in the diagram. In
spite of its importance and central placing, you do not usually need to be aware of its inner workings.
Zserver has applicability to other types of servers, such as FTP, because it provides a consistent interface to
the various different types of resource. As indicated in the diagram, Zserver is architected as a resource
manager and a virtual filesystem. The virtual filesystem is basically a notational convenience for accessing
all resources using a uniform naming scheme. The external appearance of the virtual filesystem is mod-
elled on the Unix approach. In Unix, all storage devices, and the filesystems contained therein, are
accessed from a single starting point known as the root directory, written as a single slash (/) character.
Under the root directory may be any number of files and directories. Some of these directories may actu-
ally refer to a completely different device and filesystem. The term for such directory entries is mount-
point.
Note the distinction between this naming convention and the one used by (PC) DOS and similar operating
systems. In DOS, you have to explicitly indicate the device by prefixing the file name. For example,
C:\index.htm and A:\index.htm are different files, on different devices. On Unix you create two mount
points in the root directory; /backup and /production for example. Then, the above mentioned files are
known as /backup/index.htm and /production/index.htm. This may seem like a fine distinction, however it
matches better with the naming convention used by HTTP, i.e., the URL. It also offers greater flexibility
with regards to naming devices.
TCP/IP Volume 2 User’s Manual rabbit.com 15
Zserver does not currently allow arbitrary mount-point names like Unix. Instead, there is an established
convention for each filesystem. If FS2 is in use, then there is a mount-point called “/fs2.” If the FAT file-
system is in use, then one or more mount points called “/A,” “/B,” “/C” etc. are created.
Since Zserver is the resource manager, it takes responsibility for mapping the various filesystems and
resource types into a single unified API. This API not only takes care of the detailed differences between
the various filesystem APIs, but also allows some functions to be emulated that are not natively supported
by the underlying filesystem.
In addition to the resource storage and filesystem, the resource manager needs to be able to associate other
data with each resource. This other data is divided into two categories, which are listed in the blocks on the
left of the diagram.
The two categories are “metadata” and “authorization.” Metadata consists of two tables: the MIME table
and the Rule table. The authorization data is currently just a single table of userids. The reason for the split
into two categories is this: the metadata is logically associated with individual resources, whereas the
authorization data is a mapping from external entities (“users”) to the unit in which authorization is per-
formed, namely user groups. The Rule table has some overlap, since it associates groups with individual
resource permissions.
The lowest blocks in the diagram are divided into two groups, with a dashed outline. The upper group is
labelled “filesystems,” and the lower “storage.” Both of these groups are indefinitely extensible, meaning
that new classes of storage and their organization (filesystems) may be added in future releases of
Dynamic C, or by you. The arrows between these groups are indicative of the most common patterns of
communication; others may be defined.
TCP/IP Volume 2 User’s Manual rabbit.com 16
2.4 Architecture of a Toy Application
Using the diagram of the previous section as a basis, we now focus on writing a simple web-enabled appli-
cation. The following diagram is the same as the one above, except that the relevant parts have been visu-
ally emphasized. This diagram is essentially the toy application that was described at the start of this
chapter. It shows the mandatory components for all web-enabled applications. Later, we introduce the
other elements of the diagram to show how a fully featured application is built up.
Figure 2.2 Minimum components for a web-enabled application
Resources
Requests,
Context
Rule
Table
User
Table
Battery-
Backed
RAM
#web
Variables
HTTP-X SSI
Dynamic
Resource
Table
FS2
FAT
Second
Flash
Serial
Flash
CGI
Functions
Metadata
Authorization
File Systems
Storage
(Resource Manager)
Compile-Time
Initialization
Run-Time
Initialization
Main
Loop
Application
Specifics, I/O
Application
HTTP
Static
Resource
Table
Program
Flash
MIME
Table
Zserver
Virtual File System
TCP/IP
TCP/IP Volume 2 User’s Manual rabbit.com 17
Let us work again from left to right in the Application block. To reiterate, the Application block represents
the coding that you have to do. First, there is the compile-time initialization. Taking the super-simple
example illustrated in Figure , Dynamic C code is given with the relevant part highlighted in boldface.
#define TCPCONFIG 1
#use “dcrtcp.lib”
#use “http.lib”
#ximport “hellowww.html” hellowww_html
SSPEC_MIMETABLE_START
SSPEC_MIME(“.html”, “text/html”)
SSPEC_MIMETABLE_END
SSPEC_RESOURCETABLE_START
SSPEC_RESOURCE_XMEMFILE("/hellowww.html", hellowww_html)
SSPEC_RESOURCETABLE_END
void main() {
sock_init();
http_init();
for (;;) http_handler();
}
The first boldface line is the #ximport directive. This tells the compiler to include the specified file in
the program flash, and make it accessible via the hellowww_html constant. In the diagram, the arrow
from compile-time initialization to program flash represents this inclusion. In most cases you would be
including more than just one file.
The three lines starting with SSPEC_MIMETABLE_START are initialization statements for the MIME
table. In this case, there is a single mapping from resources that end with “.html” to a MIME type of
“text/html.” All MIME types are registered with the relevant standards body, and must be entered correctly
so that the browser does not get confused. “text/html” is the registered MIME type for HTML.
The next three lines, starting with SSPEC_RESOURCETABLE_START, set up the static resource table.
Again, this contains a single entry that associates the resource name “/hellowww.html” with the file that
was #ximported on the first line. Note that the resource name suffix (.html) matches the first parameter of
the SSPEC_MIME entry.
Although not directly indicated on the diagram, the other compile-time initialization that is always
required is the #use of the appropriate libraries. In this case, the first three lines create a default TCP/IP
configuration (TCPCONFIG = 1) and bring in the networking and HTTP libraries. Note that http.lib
automatically includes zserver.lib.
Back in the Application block of the diagram, we move right and consider the runtime initialization block.
This is contained in the main() function. sock_init() comes first, to initialize the TCP/IP network
library and bring up the necessary interface(s). http_init() resets the HTTP library to a known state.
The last statement embodies the Main Loop sub-block. This is always required. Typically, only
http_handler() needs to be called; however, your application can insert calls to its own polling and
event handling code. Since this is such a simple example, there is not even any application-specific code.
TCP/IP Volume 2 User’s Manual rabbit.com 18
2.5 A Simple but Realistic Application
To turn the above toy example into something more realistic, we need to add some application specifics,
and the ability to customize the resource returned to the browser depending on the relevant state of the
application. The following diagram shows the necessary parts.
Figure 2.3 Minimum components for a web-enabled application with dynamic content.
The easiest way to introduce dynamic content is to use RabbitWeb and the associated scripting language.
SSI can be used instead (described in Section 4.5.2.1). This example, illustrated in Figure , assumes that
you have RabbitWeb. Chapter 5 describes RabbitWeb and the scripting language, ZHTML, in detail.
Resources
Requests,
Context
Rule
Table
User
Table
SSI
FS2
FAT
Second
Flash
Serial
Flash
CGI
Functions
Metadata
Authorization
File Systems
Storage
Battery-
Backed
RAM
(Resource Manager)
Compile-Time
Initialization
Run-Time
Initialization
Main
Loop
Application
Specifics, I/O
Application
HTTP
Static
Resource
Table
Program
Flash
MIME
Table
Zserver
Virtual File System
TCP/IP
#web
Variables
HTTP-X
Dynamic
Resource
Table
Optional
TCP/IP Volume 2 User’s Manual rabbit.com 19
The following code is a simplification of Samples\tcpip\rabbitweb\web.c.
#define TCPCONFIG 1
#define USE_RABBITWEB 1
#use "dcrtcp.lib"
#use "http.lib"
#ximport "my_app.zhtml" my_app_zhtml
SSPEC_MIMETABLE_START
SSPEC_MIME_FUNC(".html", "text/html", zhtml_handler),
SSPEC_MIMETABLE_END
SSPEC_RESOURCETABLE_START
SSPEC_RESOURCE_XMEMFILE("/index.html", my_app_zhtml)
SSPEC_RESOURCETABLE_END
int io_state;
#web io_state
void my_io_polling(void);
void main()
{
sock_init();
http_init();
for (;;) {
my_io_polling();
http_handler();
}
}
void my_io_polling()
{
io_state = read_that_io_device();
}
The differences between the above code and the toy example in the previous section are in boldface.
All the differences relate to the use of RabbitWeb. The first addition is a #define of USE_RABBITWEB.
This is necessary in order to include the necessary library code.
Next, there is a modification to the MIME table. The SSPEC_MIME_FUNC macro defines an entry that
says that if the resource name ends with “.html” then the MIME type is text/html (as before), and there is a
special scripting function that must be run by the HTTP server. This scripting function is called
zhtml_handler; it is provided by the HTTP library. ZHTML is the unique embedded scripting lan-
guage that converts script files into ordinary HTML so the browser can understand it.
1
The int io_state and #web statements define and register a web variable. Such a variable is an ordi-
nary global variable as far as your C program is concerned. In addition, the script is able to access it.
my_io_polling() is a function that is part of the Application Specifics sub-block. As the name sug-
gests, it is called regularly to poll some external device so as to keep the #web variable up-to-date. The
TCP/IP Volume 2 User’s Manual rabbit.com 20
implementation of the my_io_polling() function is shown updating the #web variable, but we don’t
specify the actual I/O reading function since that is too, well, application specific.
Now you may be wondering what this scripting language, ZHTML, looks like. The following code shows
the contents of the my_app.zhtml file:
<HTML><HEAD><TITLE>Web Variables</TITLE></HEAD>
<BODY><H1>Web Variables</H1>
<P>The current value of io_state is
<?z echo($io_state) ?>
</P>
</BODY></HTML>
This looks like plain HTML, and it is, with the only difference being the existence of special commands
flanked by “<?z” and “?>.” In this case, the command simply echos the current value of the web variable
that was registered. The value (binary in the global variable) is converted to ASCII text by a default
printf() conversion, in this case “%d” because the variable is an integer. When the browser gets the
results returned by the HTTP server, it will see:
<HTML><HEAD><TITLE>Web Variables</TITLE></HEAD>
<BODY><H1>Web Variables</H1>
<P>The current value of io_state is
50
</P>
</BODY></HTML>
Where the “50” represents the current variable value—of course, it may be any decimal value that an inte-
ger variable could take: -32768 through 32767.
This is still a trivial example, but it is infinitely more real-world than the toy example. We have introduced
the concept of dynamic content, which is required for embedded type applications. One thing that has been
glossed over is how (and even whether) the variable can be updated from the browser, rather than just
within the application. Yes, all #web variables may be updated via the browser. This requires use of
HTML forms, which is a subject covered in Chapter 4 and Chapter 5. We will not go over this again here;
however, the possibility of remote updating introduces us to the topic of the next section, access control.
1. Most applications will want to use a different resource suffix to distinguish between “ordinary” HTML
files and script files. The samples provided with dynamic C use .zhtml for script files, and .html for plain
HTML. In this sample, we only have script files, so it is convenient to retain the .html suffix. The other
reason for this relates to the way the HTTP server handles requests for a directory. If given a URL of “/”,
the HTTP server will append “index.html” to determine the actual resource. We take advantage of this
default behavior so that this sample would work as expected.
/