linux arm 嵌入式webserver 项目案例
CFLAGS += -std=gnu99 -Wall -ljpeg -lappweb
appweb.conf
#
# appweb.conf - Appweb configuration for the simple server sample
#
ErrorLog "stdout" level=2
#ErrorLog "error.log" size=10MB level=2 backup=5 append anew stamp=1hr
Log rx conn=5 first=2 headers=3 body=5 limits=5 size=10K exclude="jpg,gif,png,ico,css,js"
Log tx first=3 headers=3 body=5 limits=5 time=6 size=10K exclude="jpg,gif,png,ico,css,js"
Listen *:80
Home "/etc/appweb"
Documents "/var/web"
LoadModulePath "/lib"
DirectoryIndex index.html
Cache 1hour
ExitTimeout 10secs
RequestParseTimeout 30secs
InactivityTimeout 30secs
RequestTimeout 40min
SessionTimeout 30secs
MemoryPolicy restart
LimitBuffer 32K
LimitCache 1MB
LimitCacheItem 512K
LimitChunk 128K
LimitClients 20
LimitConnections 100
LimitFiles 0
LimitKeepAlive 400
LimitMemory 300MB
LimitProcesses 100
LimitRequestsPerClient 200
LimitRequestBody 64MB
LimitRequestForm 32K
LimitRequestHeader 128K
LimitRequestHeaderLines 128
LimitResponseBody 6GB
LimitSessions 1000
LimitUpload 1GB
LimitUri 64K
LimitWorkers 10
AddHandler fileHandler html gif jpeg jpg png pdf ico css js txt ""
<if DIR_MODULE>
Options Indexes
IndexOrder ascending name
IndexOptions FancyIndexing FoldersFirst
</if>
# Authorization
# AuthType basic
# authpass auth.conf appweb zjh user
# AuthType digest
# authpass --cipher md5 auth.conf appweb zjh user
<Route />
AuthRealm "appweb"
AuthType digest
include auth.conf
<Route ^/ihf/>
SetHandler actionHandler
</Route>
</Route>
#
# Enable the action handler for simple URI to "C" bindings
# This is used by the web-form Auth mech
#
<if CGI_MODULE>
LoadModule cgiHandler libmod_cgi
AddHandler cgiHandler cgi
ScriptAlias /cgi-bin/ "$Documents/cgi-bin/"
<else>
AddHandler errorHandler exe cgi cgi-nph bat cmd pl py
</if>
11
#
# auth.conf - Authorization data
#
User zjh ecb933d4a576ed38f49f6b8bad0e2f20 user
main.c
#include
#include
#include
#include
#include
#include
// 针对自己的开发板修改Led控制函数
int led_ctl(int ctl, int n)
{
int fd = open("/dev/led", O_RDWR);
if (fd < 0)
{
perror("open");
return -1;
}
if (ioctl(fd, ctl, n) < 0)
{
perror("ioctl");
close(fd);
return -1;
}
close(fd);
return 0;
}
// 针对自己的开发板修改温度获取函数
int get_temp(float *val)
{
#define IOCTL_DS18B20_S_RESET 0x10002
#define IOCTL_DS18B20_S_START 0x10003
#define IOCTL_DS18B20_S_BIT 0x10004
#define IOCTL_DS18B20_G_TEMPERATURE 0x10001
int bit = 12;
int tmp;
int fd = open("/dev/ds18b20", O_RDWR);
if (fd < 0)
{
perror("open");
return -1;
}
if (ioctl(fd, IOCTL_DS18B20_S_BIT, &bit) < 0)
{
perror("ioctl");
close(fd);
return -1;
}
if (read(fd, &tmp,4) < 0)
{
perror("read");
close(fd);
return -1;
}
*val = tmp;
*val /= 100;
return 0;
}
static void led_action(HttpConn *conn)
{
HttpQueue *q;
int n, ctl;
q = conn->writeq;
/* Set the HTTP response status */
httpSetStatus(conn, 200);
n = httpGetIntParam(conn, "n", 1);
ctl = httpGetIntParam(conn, "ctl", 1);
printf("n = %d,ctl = %d\n", n, ctl);
led_ctl(ctl, n);
httpFinalize(conn);
}
static void temp_action(HttpConn *conn)
{
HttpQueue *q;
float val;
q = conn->writeq;
/* Set the HTTP response status */
httpSetStatus(conn, 200);
/* Add desired headers. "Set" will overwrite, add will create if not already defined. */
httpAddHeaderString(conn, "Content-Type", "text/plain");
httpSetHeaderString(conn, "Cache-Control", "no-cache");
if (get_temp(&val) == 0)
httpWrite(q, "%.2f", val);
else
httpWrite(q, "Error");
httpFinalize(conn);
}
int main(int argc, char **argv)
{
Mpr *mpr;
MaAppweb *appweb;
MaServer *server;
if ((mpr = mprCreate(0, NULL, MPR_USER_EVENTS_THREAD)) == 0)
{
mprError("Cannot create the web server runtime");
return -1;
}
mprStart();
appweb = maCreateAppweb();
mprAddRoot(appweb);
server = maCreateServer(appweb, 0);
if (maParseConfig(server, "/etc/appweb/appweb.conf", 0) < 0)
{
mprError("Cannot parse the config file %s", "appweb.conf");
return -1;
}
httpDefineAction("/ihf/led", led_action);
httpDefineAction("/ihf/temp", temp_action);
if (maStartServer(server) < 0) {
mprError("Cannot start the web server");
return -1;
}
mprServiceEvents(-1, 0);
maStopServer(server);
mprRemoveRoot(appweb);
mprDestroy(MPR_EXIT_DEFAULT);
return 0;
}
mjpg-streamer-r63-mod.tar
另外附上基于事件驱动模型的socket函数库。
当有数据可读时,系统就会调用用户注册的函数。
当有新的客户连接时,系统就会调用用户注册的函数。
源码下载地址:https://sourceforge.net/projects/libsocketc/