With the rise of internet technology, web-based applications have become the mainstream in the management and interaction of embedded devices. This program structure is the B/S structure that everyone is very familiar, that is, running a web server supporting scripts or CGI on an embedded device can generate dynamic pages. On the user end, you can manage and monitor embedded devices through a web browser, it is very convenient and practical. This section describes the development and Transplantation of such applications.
You must first successfully transplant a web server that supports scripts or CGI on the embedded device before developing applications.
1. Embedded Web Server Transplantation
Because embedded devices generally have limited resources and do not need to process requests from many users at the same time, the most common Linux servers such as Apache are not used, some Web servers specially designed for embedded devices are required. These web servers are suitable for embedded applications in the storage space and the memory space occupied by the operation.
Typical embedded web servers include BOA (www.boa.org) and thttpd (http://www.acme.com/software/thttpd/). The main difference between them and high-performance Web servers such as Apache is that they are generally single-process servers, only after a user request is completed can the request of another user be responded without concurrent response, but this is sufficient for embedded devices.
We will introduce the migration of commonly used BOA servers. Boa is a very small Web server with only about 60 kb of executable code. It is a single-task web server that can only complete user requests in sequence, rather than fork new processes to process concurrent connection requests. However, BOA supports CGI and can execute a process for the CGI program fork. BOA's design goal is speed and security. In the performance tests published on its site, BOA's performance is better than that of Apache servers.
Step 1: port the boa program. Download the Boa Source code from www.boa.org. The latest version is 0.94.13. decompress it and enter the SRC subdirectory of the source code directory.
# Tar xzf boa-0.94.13.tar.gz
# Cd BOA-0.94.13/src
Generate makefile
#./Configure
Modify the MAKEFILE file, find cc = GCC, change it to CC = arm-Linux-GCC, and find CPP = gcc-e, change it to CPP = arm-Linux-gcc-E and save and exit.
Run make to compile the program. The executable program is BOA, and debugging information is stripped. The final program size is about 60 kb.
# Make
# Arm-Linux-strip Boa
Step 2 complete the boa configuration so that it can support CGI program execution. BOA needs to create a boa directory under the/etc directory and put boa. conf, the main configuration file of boa. There is an example boa. conf In The Boa Source Code directory. You can modify it based on it. The following explains the meaning of the file:
# The listening port number. The default value is 80, which generally does not need to be modified.
Port 80
# The IP address called by BIND, which is usually commented out, indicates that it is bound to inaddr_any and is assigned to all the IP addresses of the server.
# Listen 192.68.0.5
# Which user is running? That is, it has the permissions of this user. Generally, it is nobody and requires
To have a nobody user in/etc/passwd
User nobody
# Which user group is used for running? That is, it has the permissions of this user group. Generally, it is a nogroup. It must have a nogroup group in the/etc/group file.
Group nogroup
# The email address that sends an alarm when an error occurs on the server. It is not used currently and is commented out.
# Serveradmin root @ localhost
# Error log file. If it does not start with "/", it indicates starting from the root path of the server. If no error log is required, use #/dev/null.Note that the/var/log/BOA directory must be created in the following settings.
Errorlog/var/log/BOA/error_log
# Access log files. If it does not start with "/", it indicates starting from the root path of the server. If no error logs are required, use #/dev/null or comment out the logs directly.Note that the/var/log/BOA directory must be created in the following settings.
# Accesslog/var/log/BOA/access_log
# Whether to use the local time. If not commented out, the local time is used. The UTC time is used for comment-out.
# Uselocaltime
# Whether to record CGI running information. If it is not commented out, it is recorded. If it is commented out, it is not recorded.
# Verbosecgilogs
# Server name
Servername www.hyesco.com
# Whether to enable the VM function, that is, the device can have multiple network interfaces, and each interface can have a virtual web server. Usually comment out, that is, you do not need to start
# Virtualhost
# The main directory of the HTML document. If it does not start with "/", it indicates starting from the root path of the server.
DocumentRoot/var/WWW
# If you receive a user request, add the directory name after the user's main directory
Userdir public_html
# The file name of the HTML Directory Index, that is, the file name returned when no user only specifies the Access Directory
Directoryindex index.html
# When the HTML directory does not have an index file and the user only specifies the Access Directory, BOA will call the program to generate the index file and return it to the user, because the process is slow and it is best not to execute it, you can comment out or add the file specified by directoryindex to each HTML directory.
# Directorymaker/usr/lib/BOA/boa_indexer
# If directoryindex does not exist and directorymaker is commented out, use the built-in index Generation Program of BOA to generate the index file of the Directory and output it to the following directory. The directory must be read and written by BOA.
# Directorycache/var/spool/BOA/dircache
# The maximum number of HTTP sustained requests allowed by a connection. If it is commented out or set to 0, HTTP sustained effect is disabled.
Keepalivemax 1000
# The number of waiting times of the server between two requests during HTTP persistence. In seconds, the connection will be closed when the request times out.
Keepalivetimeout 10
# Specify the location of the mime. types file. If it does not start with "/", it indicates starting from the root path of the server. Avoid using the mime. types file. In this case, use addtype to specify
Mimetypes/etc/mime. Types
# The default MIME type used when there is no or unknown File Extension
Defaulttype text/plain
# Provide the PATH environment variable value of the CGI program
Cgipath/bin:/usr/local/bin
# Associate the file extension with the MIME type, which is the same as the mime. types file. If mime. types is used
# File. If the mime. types file is not used, you must use
# Addtype application/X-httpd-CGI
# Specify the document redirection path
# Redirect/bar http: // elsewhere/Feh/Bar
# Add an alias to a path
Alias/doc/usr/doc
# It is very important to specify the actual path corresponding to the virtual path of the CGI script. Generally, all CGI scripts must be placed in the actual path. When a user accesses the execution, enter the site, virtual path, and CGI Script Name.
ScriptAlias/cgi-bin // var/www/cgi-bin/
You can modify boa. conf as needed, but you must ensure that other auxiliary files and settings must be consistent with the configuration in Boa. conf, otherwise boa will not work properly. In the above example, we also need to create the directory/var/log/BOA where the log file is located, create the main directory/var/WWW of the HTML document, and set mime. copy the types file to the/etc directory and create the directory/var/www/cgi-bin/where the CGI script is located /. The mime. types file is used to specify the MIME type corresponding to different file extensions. Generally, one can be copied directly from the Linux host, most of which are under the/etc directory of the host.
The third step is to test whether BOA can work normally, whether static html pages can be accessed normally, and whether CGI scripts can run normally. Generally, NFS is used for testing, you can copy the/etc directory on the Embedded Target System to the NFS shared directory of the host, and then mount the etc directory under the NFS shared directory to the/etc directory on the target system again. In this way, various configuration files under the etc directory can be modified on the host and saved to the target system.
Assume that the host/NFS directory is a shared directory, and create a WWW sub-directory under it as the main directory of the web site. Its content is as follows:
# Ls/nfs/WWW
Cgi-bin images index.html
Index.html is the homepage for testing, images is the subdirectory for storing various images, and cgi-bin is the directory for storing CGI scripts. Boa. CONF configuration. Currently, the main directory of the HTML document is/var/WWW, And the CGI script directory is/var/www/cgi-bin, run the following command to mount the/nfs/WWW directory of the host to the/var/WWW directory on the target board. Then you can run BOA:
# Mount-t nfs 192.168.0.20:/nfs/www/var/www-O nolock
# Boa
Run the browser on the workstation to perform the test. In the address bar, enter the IP address of the target system, that is
Http: // 192.168.0.162
, You can see the relevant page, indicating that the static html page has passed the test.
Next we will test the CGI script. We need a CGI script for testing. You can write the simplest Hello World Program. The sample code is as follows:
# Include
Void main (){
Printf ("Content-Type: text/html \ n ");
Printf ("\ n ");
Printf ("CGI output \ n ");
Printf ("\ n ");
Printf ("Hello, world. \ n ");
Printf ("\ n ");
Printf ("\ n ");
Exit (0 );
}
Then perform cross-compilation to copy the obtained helloworld. cgi to the/nfs/www/cgi-bin directory of the host.
# Arm-Linux-gcc-O helloworld. cgi helloworld. c
# Cp helloworld. cgi/nfs/www/cgi-bin
Enter http: // 192.168.67.16/cgi-bin/helloworld. cgi in the address bar of the browser. The relevant page is displayed, indicating that the CGI script has passed the test.
Now we can let BOA work properly on the Embedded Target System, and the embedded web server is successfully transplanted. In the above porting process, it is best to set boa. the Error Log File errorlog in conf allows BOA to record error information. When testing static html pages and CGI scripts, it is best to view the Error Log File regardless of the result; CGI script tests are prone to errors with insufficient permissions. Ensure that the main directory, CGI script directory, and temporary file directory accessed by BOA (if the TMP environment variable is not set, the default value is the/tmp directory. the user in conf indicates.
Currently, CGI and server scripts, such as JSP and ASP, are used to generate dynamic web pages in Web technology. However, the latter requires the Web server to support the running of these scripts. In Embedded Web servers, considering resource restrictions, only CGI support is generally provided. Therefore, web applications on embedded devices are actually CGI-based program development.
CGI (Common Gate intergace) is a program running on a web server that provides an interface for HTML pages on the same client. Let's look at a practical example: there is a message book on a common personal homepage. The message book works in the following way: Users enter some information, such as names, next, the user clicks "Leave a message" (currently working on the client), and the browser sends the information to the CGI program on the server, the CGI program processes the data on the server according to the predefined method. In this example, the user-submitted information is stored in the specified file, finally, the CGI program sends a "End message" page to the client. You can see it in your browser.
Before CGI programming, we should first understand some HTML knowledge. CGI can be implemented in multiple programming languages, including C, C ++, Perl, etc. However, in the development of embedded devices, it generally does not use Perl or other explanatory languages, this language also requires support modules for interpretation and execution, which will occupy storage space and memory. The most common method is to write it in C, of course, however, C is not very suitable for developing programs that require a large number of string operations such as CGI, and programming is cumbersome. Therefore, for a professional developer, the first thing that comes to mind is whether there are reusable libraries to support quick and efficient CGI program development. Fortunately, there are many open source code C libraries that support CGI development. Here we will only introduce cgic. If you are interested, you can search for other C libraries on the Internet.
Transplantation of cgic Library
Cgic is a standard C library that supports CGI development. It can be used for free. You only need to make a public declaration in the developed site and program documentation, indicating that the program uses the cgic library, users can also purchase commercial licenses without making a public statement.
Cgic provides the following functions:
1. analyze data and automatically correct data sent from defective browsers;
2. transparently receive from data sent using the get or POST method;
3. files can be uploaded;
4. Set and receive cookies;
5. process the carriage return in the from element in a consistent way;
6. Provides the string, integer, floating point, single-choice or multiple-choice function to receive data;
7. Provides border check for numeric fields;
8. It can convert CGI Environment Variables into non-null strings in C;
9. Provides CGI program debugging means to play back the CGI status during CGI program execution;
In short, cgic is a powerful Standard C library that supports CGI development and supports multiple operating systems such as Linux, UNIX, and windows.
The following describes the cgic porting process.
Download the source code from the main cgic site http://www.boutell.com/cgic/and the latest version is 2.05. Decompress the package and enter the source code directory.
# Tar xzf cgic205.tar.gz
# Cd cgic205
Modify the MAKEFILE file, find cc = GCC, change it to CC = arm-Linux-GCC, find AR = AR, and change it to AR = arm-Linux-ar, find ranlib = ranlib and change it to ranlib = arm-Linux-ranlib. Find GCC cgictest. O-o cgictest. cgi $ {libs} and change it to $ (CC)
$ (Cflags) cgictest. o-O cgictest. CGI $ {libs}, find GCC capture. o-O capture $ {libs} and change it to $ (CC) $ (cflags) capture. o-O capture $ {libs}, save and exit.
Then run make to compile the cgic library libcgic. A. we verify the correctness of the cgic library by debugging the auxiliary Program Capture and the test program cgictest. cgi.
Copy capture and cgictest. cgi to the/nfs/www/cgi-bin directory of the host.
Enter http: // 192.168.67.16/cgi-bin/cgictest. cgi in the address bar of the wks browser. The page shows that the cgic library and test script are successfully transplanted. Cgictest. CGI fully demonstrates the functions of the cgic library. It is best to master cgictest before developing CGI programs based on the cgic library. CGI program is also a reference example for users to develop specific applications.
HTML template Creation
Web-based application development generally disconnects the interface from the program logic and allows you to change the interface to a certain extent, such as changing the properties of the interface text and creating multilingual versions, without modifying the program logic. The interface is generally made by the artist, while the programmer is responsible for the implementation of specific functions. In HTML, form is the most important means of transmitting information. It applies to any browser. There are many elements in the form, including the input text box, single-choice, multi-choice box, button, and so on, which can provide information interaction. For more information about Object Description and syntax, see other HTML books. Based on application requirements, the artist or other designers design the final web page as a template developed by programmers.
The CGI program generally receives form data for data processing, and generates a new page based on the processing result and returns it to the browser. The form data is generally submitted to the server using the POST method, which is obtained by the CGI program. The program must map the interface data with the internal data for further processing. The CGI program gets data from the page based on the element name/value. However, it is troublesome for CGI to return to the page. Because the interface may change after the program is developed, and some places where the program needs to process may not have form elements, for the program, the form element name cannot be used as the basis for differentiation, the general method is to mark with comments in HTML.
The programmer needs to follow certain rules in the template for each form element and any other places that need to be processed by the program. For example, the next line of the comment is the form Element row, and the comment mark is created. The CGI program can judge and process the form Element Information Based on the annotation mark. The program reads the template file row by row and checks whether there are annotation marks. If yes, the next row needs to be processed and the data is assigned to the form element, finally, you can return the page with data to the browser.
The HTML template also needs to focus on the input check. Based on the principle that the earlier the input check, the better, you need to check the data submitted by the user on the user interface. Currently, JavaScript scripts are generally used. When a user submits data, the onsubmit method of the form object is called, and the user input can be checked in this method. Common checks include required, Maximum/minimum length, characters, numbers, email addresses, IP addresses, and regular expressions.
CGI program development
CGI programs generally receive form data, process data according to application requirements, and generate a new page based on the processing results and return it to the browser. Form data is generally submitted to the server using the POST method, which is obtained by the CGI program. The program differentiates data based on the element name/value. After the data is processed, it reads the corresponding template file, fill in the corresponding data to HTML text according to the annotation mark, and generate the final page and return it to the browser.
The general logic of the program is:
1. Check whether scripts can be run;
2. process the data submitted by the user, differentiate the data according to the element name in the element name/value, and then process the data according to the application requirements;
3. Fill in the processing result filling form, and fill the corresponding data in the HTML text according to the annotation mark, and generate the final page and return it to the browser.
For details about the specific code implementation, you can refer to the relevant chapter in "embedded Linux system development details-Based on ep93xx series arm. (Full text)