之移植boa到TL-4379+WEB远程操作开发板
【创龙AM4379 Cortex-A9试用体验】之移植boa到TL-4379+WEB远程操作开发板
TL-4379开发板,出厂时已实现了基于ligththttpd的WEB服务,而对于嵌入式系统来讲,多数情况下是使用boa这个各方面性能更好的WEB服务承载工具,这篇使用报告,我们介绍一下将boa移植到TL-4379开发板,实现通过PC或移动终端浏览器访问TL-4379的软硬件资源。
1. 移植boa
1.1 下载boa源码
从http://www.boa.org/下载Boa源码,得到boa-0.94.13.tar.gz文件,通过VMWare工具拷贝到Ubuntu虚拟机,执行解压命令:
tar -zxvf boa-0.94.13.tar.gz
执行结果如图所示:
1.2 安装必要的工具
由于我们安装的Ubuntu虚拟机,部分依赖库没有安装,我这里需要提前安装,否则在交叉编译boa时,会报错,报错信息如图所示:
sudo apt-get install bison
执行过程如图所示:
sudo apt-get install flex
执行过程如图所示:
1.3 生成并修改Makefile
进入boa源码目录:
cd ./boa-0.94.13/src
执行./configure命令,如图所示:
命令执行结果如图所示:
生成Makefile后,我们将Makefile中的编译工具修改Wie交叉编译工具链,修改内容如图所示:
此时,我们就可以执行make命令了,但是在执行交叉编译时,会弹出如下错误:
解决方法:
修改的内容如图所示:
经过上述步骤我们就可以完成交叉编译了。
但是在我们配置boa到TL-4379开发板时,会提示新的错误,boa无法在开发板上正常启动,所以,除了上述的修改配置外,我们还需要在src源码下修改两处:
1)log.c
将src/log.c文件中的部分如下图所示的代码注释掉:
2)boa.c
将src/boa.c文件中的如下图所示的代码注释掉:
1.4 交叉编译
经过上述的一些修改、配置之后,我们执行make命令,系统生成boa可执行文件,如下图所示:
我们查看一下文件属性,看看其是否是基于ARM的可执行文件:
最后,执行命令,缩减boa的文件大小:
arm-linux-gnueabihf-strip boa
1.5 创建能够在TL-4379开发板上运行boa的配置文件
这一步完成Boa的配置,使其能够支持CGI程序的执行。Boa需要在/etc目录下建立一个boa目录,里面放入Boa的主要配置文件boa.conf。在Boa源码目录下已有一个示例boa.conf,可以在其基础上进行修改,修改后的文件内容如下所示:
# Boa v0.94 configuration file
# File format has not changed from 0.93
# File format has changed little from 0.92
# version changes are noted in the comments
#
# The Boa configuration file is parsed witha lex/yacc or flex/bison
# generated parser. If it reports an error, the line number willbe
# provided; it should be easy to spot. The syntax of each of these
# rules is very simple, and they can occurin any order. Where possible
# these directives mimic those of NCSAhttpd 1.3; I saw no reason to
# introduce gratuitous differences.
# $Id: boa.conf,v 1.25 2002/03/22 04:33:09jnelson Exp $
# The "ServerRoot" is not in thisconfiguration file. It can be compiled
# into the server (see defines.h) orspecified on the command line with
# the -c option, for example:
#
# boa -c /usr/local/boa
# Port: The port Boa runs on. The default port for http servers is 80.
# If it is less than 1024, the server mustbe started as root.
Port 80
# Listen: the Internet address to bind(2)to. If you leave it out,
# it takes the behavior before 0.93.17.2,which is to bind to all
# addresses (INADDR_ANY). You only get one "Listen"directive,
# if you want service on multiple IPaddresses, you have three choices:
# 1. Run boa without a "Listen" directive
# a. All addresses are treated the same; makes sense if the addresses
# are localhost, ppp, and eth0.
# b. Use the VirtualHost directive below to point requests to different
# files. Should be good for a verylarge number of addresses (web
# hosting clients).
# 2. Run one copy of boa per IP address, each has its own configuration
# with a "Listen" directive. No big deal up to a few tens of addresses.
# Nice separation between clients.
# The name you provide gets run through inet_aton(3),so you have to use dotted
# quad notation. This configuration is too important to trustsome DNS.
#Listen 192.68.0.5
# User: The name or UID the server should run as.
# Group: The group name or GID the servershould run as.
User root
#Group nogroup
Group 0
# ServerAdmin: The email address whereserver problems should be sent.
# Note: this is not currently used, exceptas an environment variable
# for CGIs.
#ServerAdmin root@localhost
# ErrorLog: The location of the error logfile. If this does not start
# with /, it is considered relative to theserver root.
# Set to /dev/null if you don't want errorslogged.
# If unset, defaults to /dev/stderr
ErrorLog /var/log/boa/error_log
# Please NOTE: Sending the logs to a pipe('|'), as shown below,
# issomewhat experimental and might fail under heavy load.
# "Usual libc implementations ofprintf will stall the whole
# process if the receiving end of a pipe stops reading."
#ErrorLog "|/usr/sbin/cronolog--symlink=/var/log/boa/error_log /var/log/boa/error-%Y%m%d.log"
# AccessLog: The location of the access logfile. If this does not
# start with /, it is considered relativeto the server root.
# Comment out or set to /dev/null (lesseffective) to disable
# Access logging.
AccessLog /var/log/boa/access_log
# Please NOTE: Sending the logs to a pipe('|'), as shown below,
# issomewhat experimental and might fail under heavy load.
# "Usual libc implementations ofprintf will stall the whole
# process if the receiving end of a pipe stops reading."
#AccessLog "|/usr/sbin/cronolog --symlink=/var/log/boa/access_log/var/log/boa/access-%Y%m%d.log"
# UseLocaltime: Logical switch. Uncomment to use localtime
# instead of UTC time
#UseLocaltime
# VerboseCGILogs: this is just a logicalswitch.
# Itsimply notes the start and stop times of cgis in the error log
# Comment out to disable.
#VerboseCGILogs
# ServerName: the name of this server thatshould be sent back to
# clients if different than that returnedby gethostname + gethostbyname
#ServerName www.your.org.here
ServerName www.linuxidc.com
# VirtualHost: a logical switch.
# Comment out to disable.
# Given DocumentRoot /var/www, requests oninterface 'A' or IP 'IP-A'
# become /var/www/IP-A.
# Example: http://localhost/ becomes /var/www/127.0.0.1
#
# Not used until version 0.93.17.2. This "feature" also breakscommonlog
# output rules, it prepends the interfacenumber to each access_log line.
# You are expected to fix that problem witha postprocessing script.
#VirtualHost
# DocumentRoot: The root directory of theHTML documents.
# Comment out to disable server non userfiles.
DocumentRoot /var/www
# UserDir: The name of the directory whichis appended onto a user's home
# directory if a ~user request is recieved.
UserDir public_html
# DirectoryIndex: Name of the file to useas a pre-written HTML
# directory index. Please MAKE AND USE THESE FILES. On the
# fly creation of directory indexes can be_slow_.
# Comment out to always use DirectoryMaker
DirectoryIndex index.html
# DirectoryMaker: Name of program used tocreate a directory listing.
# Comment out to disable directorylistings. If both this and
# DirectoryIndex are commented out,accessing a directory will give
# an error (though accessing files in thedirectory are still ok).
DirectoryMaker /usr/lib/boa/boa_indexer
# DirectoryCache: If DirectoryIndex doesn'texist, and DirectoryMaker
# has been commented out, the theon-the-fly indexing of Boa can be used
# to generate indexes of directories. Bewarned that the output is
# extremely minimal and can cause delayswhen slow disks are used.
# Note: The DirectoryCache must be writableby the same user/group that
# Boa runs as.
# DirectoryCache /var/spool/boa/dircache
# KeepAliveMax: Number of KeepAliverequests to allow per connection
# Comment out, or set to 0 to disablekeepalive processing
KeepAliveMax 1000
# KeepAliveTimeout: seconds to wait beforekeepalive connection times out
KeepAliveTimeout 10
# MimeTypes: This is the file that is usedto generate mime type pairs
# and Content-Type fields for boa.
# Set to /dev/null if you do not want toload a mime types file.
# Do *not* comment out (better useAddType!)
MimeTypes /etc/mime.types
# DefaultType: MIME type used if the fileextension is unknown, or there
# is no file extension.
DefaultType text/plain
# CGIPath: The value of the $PATHenvironment variable given to CGI progs.
CGIPath /bin:/usr/bin:/usr/local/bin
# SinglePostLimit: The maximum allowablenumber of bytes in
# a single POST. Default is normally 1MB.
# AddType: adds types without editingmime.types
# Example: AddType type extension[extension ...]
# Uncomment the next line if you want .cgifiles to execute from anywhere
#AddType application/x-httpd-cgi cgi
# Redirect, Alias, and ScriptAlias all havethe same semantics -- they
# match the beginning of a request and takeappropriate action. Use
# Redirect for other servers, Alias for thesame server, and ScriptAlias
# to enable directories for scriptexecution.
# Redirect allows you to tell clients aboutdocuments which used to exist in
# your server's namespace, but do notanymore. This allows you to tell the
# clients where to look for the relocateddocument.
# Example: Redirect /barhttp://elsewhere/feh/bar
# Aliases: Aliases one path to another.
# Example: Alias /path1/bar /path2/foo
Alias /doc /usr/doc
# ScriptAlias: Maps a virtual path to adirectory for serving scripts
# Example: ScriptAlias /htbin/ /www/htbin/
#ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
2. 移植boa到TL-4379
第一节,我们交叉编译了boa,并且编写了boa.conf文件,这一节,我将把boa从Ubuntu虚拟机拷贝到TL-4379开发板。
首先将编译好的boa,boa.conf文件拷贝到NFS共享目录,如图所示:
2.1 TL-4379上电
1)给TL-4379开发板上电,并执行如下命令:
mount -t nsf 192.168.1.108:/nfsshare /mnt-o nolock
2)创建必要的目录
mkdir /etc/boa
mkdir /var/www
mkdir /var/www/cgi-bin
mkdir /var/log/boa
3)创建日志文件
cd /var/log/boa
touch access_log
chmod 777 access_log
2.2 拷贝文件
cp /mnt/boa /usr/local/boa
cp /mnt/boa.conf /etc/boa/
2.3 拷贝mime.types
将Ubuntu /etc下载的mime.types文件拷贝到开发板的/etc目录下。
3. 编写一个简单的静态HTML文件
编写一个简单的静态网页测试文件,如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Boa 静态网页测试</title>
</head>
<body>
<h1> Welcome to Boa sever!</h1>
</body>
</html>
4. 测试静态网页
由于TL-4379开发板默认开机启动lighthttpd程序,而该程序又占用了80端口,所以我们首选关闭该WEB服务程序,执行命令和结果如图所示:
进入/usr/local目录,执行如下命令:
./boa
如图所示:
我们在PC机的浏览器中,输入TL-4379开发板的IP地址,显示如下:
浏览器上显示的内容,与我们编写的静态网页内容完全一致,说明我们移植的boa是成功的。
5. 测试动态网页
我们移植boa的一个重要原因,是我们系统通过PC机或移动终端的浏览器即可操作或查看TL-4379开发板上的硬件资源。我们进一步测试一下基于boa+cgi的动态网页。
5.1 编写LED灯html文件
<!DOCTYPE html PUBLIC "-//W3C//DTDHTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>LED Control</title>
<body>
<form method="GET" action="/cgi-bin/led.cgi">
<tr>
<td > <strong> LED1:</strong></td>
<td><input name="mode" type="RADIO"value="led_on" checked="checked"/>open
<input name="mode" type="RADIO"value="led_off" />close
</td>
</tr>
<tr>
<td><input type="SUBMIT" name="button1"id="button1" value="submit"/></td>
</tr>
</form>
</body>
</html>
5.1 编写LED灯的CGI代码leg.cgi.c
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <stdio.h>
#include "cgic.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
char mode[64];
void get_mode()
{
cgiFormStringNoNewlines("mode",mode, 64);
}
int cgiMain()
{
int rnt = 0;
get_mode();
if(strncmp(mode,"led_off",7) == 0)
{
rnt = 1;
system("echo 0 > /sys/class/leds/user-led0/brightness");
}
else if(strncmp(mode,"led_on",6) == 0)
{
rnt = 2;
system("echo 1 > /sys/class/leds/user-led0/brightness");
}
else
{
}
cgiHeaderContentType("text/html\n\n");
fprintf(cgiOut,"<HTML>\n");
fprintf(cgiOut,"<body>\n");
if(rnt == 0)
{
fprintf(cgiOut," OPEN LED FAILED!\n");
}
else if(rnt == 1)
{
fprintf(cgiOut,"close led successed!\n");
}
else if(rnt == 2)
{
fprintf(cgiOut,"open led successed\n");
}
else
{
}
fprintf(cgiOut,"</body>\n");
fprintf(cgiOut,"</html>\n");
close(fd);
return 0;
}
5.1 下载CGI源码并编译CGI程序
我们首先下载cgi库,网址如下:https://boutell.com/cgic/#obtain
将下载到的文件cgic207.tar拷贝到Ubuntu系统,执行解压命令:
tar -zxvf cgic207.tar
将加压后的所有文件拷贝到我们存放led.cgi.c的文件夹中,如命令所示:
cp cgic207/* test_cgi
执行命令:
交叉编译led.cgi的结果如图所示:
将index.html和led.cgi分别拷贝到开发板的/var/www和/var/www/cgi-bin目录下,并设置led.cgi的权限:
chmod 777 led.cgi
5.2 测试
关闭lighthttpd:
执行命令启动boa:
cd /usr/local
./bao
在PC的浏览器输入TL-4379开发板的IP地址,如图所示:
TL-4379开发板上电时,默认4个LED全亮,我们这里只控制LED0,其初始状态如图所示:
我们在网页上选择close,并点击submit按钮,如图所示:
命令执行结果如图所示:
TL-4379的LED0熄灭,如图所示:
从上图中可以看到,从原来的4盏灯全亮变为只有剩下的3盏灯亮。
我们再次在网页上选择open,点击submit按钮,执行结果如图所示:
在TL-4379开发板上,我发现LED0再次点亮,出现4盏灯全亮,如图所示:
6. 小结
我们通过从零开始是移植boa,配置boa到TL-4379开发板,再到测试静态网页,最后测试动态网页,达到了通过PC机的浏览器动态操作开发板板载资源的目的,又进一步的完善了把TL-4379作为车间数据采集服务器的功能,使通过TL-4379查询车间现场数据的方式多样化,为将来客户的不同需求做了较好的技术储备。