Abstract:Based on a detailed introduction to the implementation and configuration of an Embedded Web Server Boa, a program for online web Remote Monitoring gpio (General Input/Output) is used as an example, this article introduces the CPU program design technology in the embedded Linux system.
Keywords:Embedded System Linux boa cgi gpio
1 Overview
With the popularization of Internet applications, more and more information products need to access the Internet for remote access through web pages. The embedded web system provides an economic and practical Internet embedded access solution. Here we introduce CGI programming technology in Embedded Linux with an embedded web server boa.
2. Implementation and configuration of Web Server Boa
2.1 In uClinux, there are three major Web servers: httpd, thttpd, and boa. Httpd is the simplest Web server. It has the weakest function, does not support authentication, and does not support CGI. Both thttpd and BOA support authentication, CGI, and other functions. Boa is a small HTTP server with a single task. It has open source code and excellent performance. It is especially suitable for embedded systems. Currently, the uClinux Code contains the boa source code. To implement BOA in uClinux, you only need to configure and modify boa. The following is the configuration process.
(1) Compile BOA to the kernel
First, you need to compile BOA to the kernel, that is, execute make menuconfig, and select BOA under the network application item in the Application menu. This operation requires re-compiling the kernel.
(2) Compile the configuration file boa. conf.
In the Linux operating system, application configurations are provided in the form of configuration files, usually in the target board/etc/directory or/etc/config directory. However, the boa configuration file boa. cont is usually rotated in the target board/home/httpd/directory.
For example, a typical boa. conf file format is as follows:
Servername Samsung-arm
DocumentRoot/home/httpd
ScriptAlias/cgi-bin/home/httpd/cgi-bin/
ScriptAlias/index.html/home/httpd/index.html
It specifies that HTML pages must be placed in the/home/httpd directory, and CGI external extensions must be placed in the/home/httpd/cgi-bin directory.
(3) Compile and burn the kernel
After re-compiling the kernel, you can use a writing tool to burn the kernel. Then, you can access the Web server on the Development Board through the IE browser on the PC. For example, you can enter the IP address of the Development Board http: // 192.168.0.101 to access the webpage index.html. In addition, by writing CGI external extensions, You can implement dynamic Web technology. The following describes in detail.
2.2 Implementation and configuration of b0a in Linux with MMU Platform
For platforms with MMU (Memory Management Unit), such as armlinux and ppclinux, you can download a mainstream BOA release package online. Because it runs in the target system, you must use the cross-compilation tool to compile the program. That is, you must modify the compiler in Boa/src/makefile. For example:
Cc =/linuxppc/Cdk/bin/PowerPC-Linux-gcc
CPP =/linuxppc/Cdk/bin/PowerPC-Linux-G ++
Then, execute make in the BOA/src directory to generate the boa executable file. compile it into the kernel and write it to the storage device to access the Boa Server.
3 CGI programming technology
CGI (Common Gateway Interface) is a standard interface for external application extension applications to interact with WWW servers. External extended applications compiled according to CGI standards can process the data input by the client browser to complete the interaction between the client and the server. The CGI Specification defines how the Web server sends messages to the extended application, and how to process the extended application after receiving the information. CGI provides many functions that cannot be implemented by static HTML web pages, such as search engines and Web-based database access.
3.1 Working Principle
(1) Working Principles of WWW and CGI
HTTP is the foundation of www. It is based on the customer/server model. A server can provide services for customers distributed in the network; it is a "no connection" protocol established on the TCP/IP protocol. Each connection only processes one request. On the server, a daemon process is running to listen to the port and wait for the request from the customer. When a request arrives, a sub-process is created to serve the user's connection. Depending on the request, the server returns an HTML file or calls an external application through CGI to return the processing result. The server interacts with external programs and scripts through CGI. Based on the method taken by the client during request, the server collects information provided by the customer, and send this part of information to the specified CGI extension. The CGI extension processes the information and returns the result to the server. Then, the server analyzes the information and sends the result back to the client.
The external CGI program communicates with the WWW server and transmits relevant parameters and processing results through environment variables, command line parameters, and standard input. The server provides a channel for information exchange between the client (browser) and CGI extensions. The standard input of CGI is the standard output of the server, while the standard output of CGI is the standard input of the server. The customer's request is transmitted to the CGI standard input through the standard output of the server. After CGI processes the information, it sends the result to its standard input, and then the server sends the processing result to the client.
(2) URL Encoding
The client browser sends data to the server in the encoding format. This encoding is CRL encoding. The main task of coding is to escape the names and values of form fields. The specific practice is that the spaces in each pair of fields and values will be replaced with a plus sign (+) character, characters that are not letters or numbers will be replaced with their hexadecimal numbers in the format of % HH. HH is the ASCII hexadecimal value of the character. <Br> the tag is replaced with "% 0d % 0a ".
Information is arranged in the order they appear in the form. The name of the data field and the value of the data field are connected by the equal sign (=. Each pair of Names/values is connected by the "&" character. After these encoding processes, the form signal becomes a continuous pipeline stream, which contains all the information that will be sent to the server.
Because the form input information is encoded and passed to the script program, the CGI extension program must decode these parameters before using them.
3.2 CGI external extension program Compilation
Server programs can receive information in three ways: environment variables, command lines, and standard input. The method attribute of the <form> label determines the method used.
In "method = get", it is normal to pass form encoding information to CGI programs through commands. Most form encoding information is transmitted through the QUERY_STRING environment variable. If "method = post" is used, the form information is read through standard input. Another way to transmit information to CGI without using a form is to directly retrieve the information after the URL address, and use the question mark (?) between the information and the URL (?) .
The following is a detailed description of the application example of gpio (General Input/Output) of the remote monitoring ARM chip.
(1) Get Method
The get method is a request for data and is used to obtain static documents. When the get method is used, the CGI program obtains data from the environment variable QUERY_STRING. To process client requests, CGI must analyze the strings in QUERY_STRING. When you need to obtain data from the server without changing the data on the server, you should use the get method. However, if the string contained in the request exceeds a certain length, it is generally 1024 bytes, you can only use the POST method. The get method sends the request information by adding parameters after the URL. These parameters will be passed to the CGI program in the environment variable QUERY_STRING. For the Form Format and CGI decoding program of the get method, refer to the implementation of the post method.
(2) Post Method
The post method is generally used when the browser transmits data from a filled form to the server, and the POST method must be used when the data sent exceeds 1024 bytes. When the POST method is used, the web server transmits data to the standard input stdin of the CGI program. The length of the sent data exists in the environment variable content_length, and the data format of the POST method is:
Variable1 = value1 & variable2 = value2 & etc
The CGI program must check the request_method environment variable to determine whether the POST method is used and whether to read stdin. The form defined by the POST method in the HTML document is as follows:
<Form method = post action = "/cgi-bin/cgi_gpio.cgi">
<Input type = "radio" name = RB value = "0"> operate P0 <br>
<Input type = "radio" name = RB value = "1"> operate P1 <br>
<Input type = "radio" name = RB value = "2"> operate P2 <br>
<Input name = "OK" type = submit value = "OK"> <input>
Name = "cancel" type = reset value = "reset"> </form>
The server script program it calls is/cgi/bin/cgi_gpio.cgi. To decode the form in the CGI extension, refer to the following program:
/* Function getpostvars */
Char ** getpostvars (){
Int I;
Int content_length;
Char ** postvars;
Char * postinput;
Char ** pairlist;
Int paircount = 0;
Chr * nvpair;
Char * eqpos;
Postinput = getenv ("content_length"); // obtain the number of bytes transmitted to the Program Data
If (! Postinput)
Exit ();
If (! Content_length = atoi (postinput) // obtain the length of information
Exit (1 );
If (! (Postinput = (char *) malloc (content_length + 1 )))
Exit (1 );
If (! Fread (postinput, content_length, 1, stadin ))
Exit (1 );
Postinput [content_length] = '0 '';
For (I = 0; postinput [I]; I ++)
If (postinput [I] = ''+ '')
Postinput [I] = '''; // easy to add
Pairlist = (char **) malloc (256 * sizeof (char **));
Paircount = 0;
Nvpair = strtok (postinput, "&"); // segment the information from the position where the "&" character appears, and process the result in sequence
While (nvpair ){
Pairlist [paircount ++] = strdup (nvpair );
If (! (Paircount % 256 ))
Pairlist = (char **) realloc (pairlist, (paircount + 256) * sizeof (char **));
Nvpair = strtok (null ,"&");
}
Pairlist [paircount] = 0;
Postvars = (char **) malloc (paircount * 2 + 1) * sizeof (char **));
For (I = 0; I <paircount; I ++ ){
If (eqpos = strchr (pairlist [I], ''= '')){
* Eqpos = ''0 '';
Unescape_url (postvars [I * 2 + 1] = strdup (eqpos + 1); // call the unescape_url function to continue decoding.
} Else {
Unescape_url (postvars [I * 2 + 1]) = strdup (""));
}
Postvars [paircount * 2] = 0;
For (I = 0; pairlist [I]; I ++)
Free (pairlist [I]);
Free (pairlist );
Free (postinput );
Return postvars;
}
The unescape_url function then calls the x2c function and decodes (not a byte or number) special characters from its % HH representation into text characters.
/* Unescape_url function */
Static void unescape_url (char * URL ){
Int X, Y;
For (x = 0, y = 0; Url [y]; ++ X, ++ y ){
If (URL [x] = URL [y]) = ''% ''){
URL [x] = x2c (& URL [Y + 1]);
Y + = 2;
}
}
URL [x] = '0 '';
}
(3) Direct URL and parameter transfer method
This is a way to transmit information to CGI without using forms. It appends the information directly after the URL address, and uses the number (?) between the information and the URL (?) . For example, a cgi_gpio.cgi script can be started from the following link:
<A href =/cgi-gpio.cgi !? Flag = 0 operate P0 </a>
<A href>/* cgi-bin/cgi_gpio.cgi? Flag = 1 operate P1 </a>
<A href =/cgi-bin_gpio.cgi? Flag = 2 operate P2 </a>
.
.
.
The CGI extension can use the following code to receive information: char * get_input; // used to receive environment variables
.
.
.
Get_input = getenv ("QUERY_STRING ");
If (get_input ){
Get_input = strdup (get_input );
Printf ("QUERY_STRING if % s", get_input );
}
/* Determine flag = x information */
If (! Strcmp (get_input, "flag = 0 ")
... // Operate P0
Else if (! Strcmp (get_input, "flag = 1 ")
... // Operate p1
Else
... // Operate p2
The above three methods can be selected based on different application scenarios and application requirements.
Conclusion
Embedded Web Server system solutions can be widely used in many fields, such as remote monitoring of automation equipment, embedded GSM Short Message Platform, and remote home medicine. Moreover, with the continuous development of the Internet application field, the embedded Internet technology will be more widely used and developed.