移动物体监控系统-sprint4嵌入式web服务器开发
一、BOA嵌入式服务器的移植
step1:下载BOA服务器并解压,进入boa下面的src目录,执行./configure生成必须的配置文件以及Makefile
step2:修改Makefile文件
cc=arm-linux-gcc
cpp=arm-linux-gcc -E
step3:编译make
编译时出错:util.c:100:1:error:posting "t" and "->" does not give a vaild preprocessing token
将util.c文件中 time-offset=TIMEZONE_OFFSET(t) 注释掉,改为time_offset=0;
step3:将编译生成的boa可执行文件拷贝到根文件系统的/sbin/目录中
step4:配置BOA
将boa.conf拷贝到根文件系统的/etc/boa目录下。改变的配置项:
a.DocumentRoot /web/指明开发板网页路径
b.user Group 注释掉
c.Mime Type 媒体文件 设置为 /dev/null
d.Errorlog 错误日志文件 设置为 /dev/null(设置为/dev/consoce表示错误打印到终端)
e.Access log 访问日志 设置为/dev/null
step5:开发板运行"#boa",浏览器登录开发板IP-192.168.19.127,即可登录嵌入式web服务器的网页设计界面。
二、CGI快速入门-网页控制

控制流程:1、用户浏览器---(请求连接)--->web服务器 (web服务器包括a.供浏览器登录的网页界面程序,b.共服务器调用的CGI程序(扩展服务器功能));
2、浏览器登录的微博服务器指定的网页界面,给予指令-->调用设定CGI程序进行控制;
3、获取的返回结果->web服务器发送到网络中,用户即可通过浏览器远程获取数据信息。
三、CGIC库移植
step1:解压CGIC库
step2:修改Makefile
cc=arm-linux-gcc
AR=arm-linux-ar
RANLIB=arm-linux-ranlib
arm-linux-gcc cgictest.o -o cgictest.cgi ${LIBS}
arm-linux-gcc capture.o -o capture ${LIBS}
step3:拷贝libcgic.a到根文件系统中
step4:BOA配置文件修改(boa.conf)
scriptAlias /cam/ /web/cam/ (指明CGI程序路径)
step5:运行boa程序,在pc的浏览器上输入开发板IP地址 /cam/cgictest.cgi,网页正常打开则表明boa和cgic移植成功。
四、CGI程序设计
其中,网页界面.html、获取监控图片和视频的cgi程序开发
step1:网页界面在原有的indx.html中修改
<a class="menu" href="/cam/image.cgi"> //获取图片的程序,跳转运行/CGI程序
step2:CGI程序:主程序cgiMain()下
1、print_file(cgiOut,"../top.html"); //加头,将top.html内容打印到cgiOut
2、total = list_pic("/mnt/sd","jpg"); //将图片加入显示列表
3、if (cgiFormSubmitClicked("gopage") == cgiFormSuccess) //处理用户的选择请求
4、show_pic(start,end,total); //显示图片
5、print_file(cgiOut,"../bottom.html"); //加尾,将bottom.html内容打印到cgiOut
step3:编译生成.cgi文件 arm-linux-gcc -L ./cgic205/ -lcgi -I ./cgic205/image.c -o image.cgi
step4:获取视频的程序开发
(1)<a class="menu" href="/cam/movie.cgi">
(2)fprintf(cgiOut, "<embed src = \" / sd / % s\" autostart = true loop = true width = \"640\" height =\"480\">< / embed>","01-19700101000405.avi");
在index.html中添加语句(1)
建立movie.c中添加语句(2),编译 arm-linux-gcc -L ./cgic205/ -lcgi -I ./cgic205/movie.c -o movie.cgi
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <fcntl.h> 4 #include <sys/ioctl.h> 5 #include <sys/stat.h> 6 #include <unistd.h> 7 #include <cgic.h> 8 #include <dirent.h> 9 #include <string.h> 10 11 struct pic_list 12 { 13 char name[30]; 14 }g_img[2000]; 15 16 //加头,将top.html内容打印到cgiOut 17 void print_file(FILE *dst_fp,const char *src) 18 { 19 FILE* src_fp; 20 char line[1024]; 21 22 src_fp = fopen(src, "r"); 23 while (NULL != fgets(line, 1024, src_fp)) 24 fputs(line, dst_fp); 25 fclose(src_fp); 26 } 27 28 29 int cmp_sfc(const char* fn,const char *sf) 30 { 31 char* tfn = (char*)fn; 32 for (; *tfn != '.' && *tfn != 0; tfn++) 33 if (*tfn == 0) 34 return -1; 35 tfn++; 36 37 return strcmp(tfn, sf); 38 } 39 40 // 41 int list_pic(const char* path,const char *sfx) 42 { 43 //遍历/mnt/sd目录,将所有的图片文件的名字加入列表 44 DIR* dir; 45 struct dirent* ptr; 46 int i = 0; 47 48 //1、打开目录 49 dir = opendir(path); 50 51 //2、读取目录中一个文件 52 while ((ptr = readdir(dir)) != NULL) 53 { 54 55 //3、判断该文件的后缀是否为jpg 56 if (0 == cmp_sfc(ptr->d_name, sfx)) 57 { 58 //4、如果是图片,将其文件名加入列表 59 strcpy(g_img[i].name, ptr->d_name); 60 i++; 61 } 62 } 63 64 return i; 65 66 } 67 68 //显示图片 69 void show_pic(int start, int end, int total) 70 { 71 int i,j = 0; 72 73 fprintf(cgiOut, "<tr>"); 74 fprintf(cgiOut, "<td align=center>"); 75 fprintf(cgiOut, "<table border=\"1\">"); 76 77 for (i = start; i < end + 1; i++) 78 { 79 if ((j % 4) == 0) 80 { 81 fprintf(cgiOut, "<tr>"); 82 fprintf(cgiOut, "<td>"); 83 } 84 j++; 85 86 fprintf(cgiOut, "<img src = \"/sd/%s\" width =\"160\" height = \"120\" / >", g_img[i].name); 87 88 if ((j % 4) == 0) 89 { 90 fprintf(cgiOut, "<tr>"); 91 fprintf(cgiOut, "<td>"); 92 } 93 } 94 fprintf(cgiOut, "</table>"); 95 fprintf(cgiOut, "<p class=\"little\">-- 第%d页 共%d页 --</p>", start / 16 + 1, total % 16 ? total / 16 + 1 : total / 16); 96 fprintf(cgiOut, "</td>"); 97 fprintf(cgiOut, "</tr>"); 98 } 99 100 //界面选择 101 void show_select_form(int total) 102 { 103 int i, pgn; 104 105 pgn = total / 16; 106 107 if (total % 16) 108 pgn++; 109 110 fprintf(cgiOut, "<tr>"); 111 fprintf(cgiOut, "<td align=center>"); 112 113 fprintf(cgiOut, "<br><form>"); 114 fprintf(cgiOut, "转到第"); 115 fprintf(cgiOut, "<select name=\"selectpage\">"); 116 for (i = 0; i < pgn; i++) 117 fprintf(cgiOut, "<option value=\"opt%d\">%d</option>", i, i + 1); 118 119 fprintf(cgiOut, "</select>"); 120 fprintf(cgiOut, "页 "); 121 fprintf(cgiOut, "<input type=\"submit\" name=\"gopage\" value=\"go\"/>"); 122 fprintf(cgiOut, "<form>"); 123 124 fprintf(cgiOut, "</td>"); 125 fprintf(cgiOut, "</tr>"); 126 } 127 128 int cgiMain() 129 { 130 int fd; 131 int led_control, led_state; 132 char *data; 133 int start, end, total; 134 start = 0; 135 end = 15; 136 137 //3、结果显示信息 138 139 //加头 140 print_file(cgiOut, "../top.html"); 141 142 //将图片加入显示列表 143 total=list_pic("/mnt/sd","jpg"); 144 145 //处理用户的选择请求 146 if (cgiFormSubmitClicked("gopage") == cgiFormSuccess) { 147 148 int i, sel; 149 char** optlist; 150 char tmp[10]; 151 152 optlist = (char**)malloc(sizeof(char*) * total); 153 154 for (i = 0; i < total; i++) 155 { 156 sprintf(tmp, "opt%d", i); 157 optlist[i] = strdup(tmp); 158 } 159 160 cgiFormSelectSingle("selectpage", optlist, total, &sel, 0); 161 162 start = sel * 16; 163 end = start + 15; 164 end = total < end ? total : end; 165 //fprintf(cgiOut, "<p>s=%d, e=%d</p>", start, end); 166 167 free(optlist); 168 } 169 170 //显示图片 171 show_pic(start,end,total); 172 show_select_form(total); 173 174 175 //加尾 176 print_file(cgiOut, "../bottom.html"); 177 return 0; 178 }

浙公网安备 33010602011771号