嵌入式linux項目開發(一)——web資料互動__Oracle

來源:互聯網
上載者:User
嵌入式linux項目開發(一)——web資料互動

    本文講解如何在移植好BOA、CGIC、SQLite的嵌入式web伺服器上通過資料互動web網頁配置個人資訊,由CGI程式將配置的資訊儲存到SQLite資料庫。 一、嵌入式web伺服器工作原理

    伺服器軟體(BOA)始終在HTTP 連接埠守候客戶的串連請求,當客戶向伺服器發起一個串連請求後,客戶和伺服器之間經過“三步握手”建立起串連。在接收到用戶端的HTTP 要求訊息後,伺服器對訊息進行解析,包括:讀取請求URL、映射到對應的物理檔案、區分用戶端請求的資源是靜態頁面還是CGI 應用程式等。如果客戶請求的是靜態檔案,那麼伺服器讀取相應的磁碟檔案,並將其作為HTTP響應訊息中的實體返回給用戶端,如果用戶端請求的是CGI 應用程式,那麼伺服器將建立相應的CGI 應用程式進程,並將各種所需資訊(用戶端請求資訊、伺服器端相關資訊等)按CGI 規範傳遞給CGI 應用程式進程,此後由CGI 應用程式接管控制。

        CGI應用程式讀取從Web 服務器傳遞來的各種資訊,並對用戶端的請求進行解釋和處理,如使用SQL 陳述式來檢索或者更新資料庫,或者將從用戶端獲得的資料,按與被監控對象所定義的通訊協定重新組幀,從UART 口發送到被監控對象。最後CGI 應用程式會將處理結果按照CGI 規範返回給Web 服務器,Web 服務器會對CGI 應用程式的處理結果進行解析,並在此基礎上產生HTTP 響應資訊返回給用戶端。

    嵌入式web伺服器所要實現的功能是讓用戶端使用瀏覽器向伺服器發送 HTTP請求,伺服器響應用戶端的請求後,並引導到指定的指令碼程式,對命令進行解析,將資訊交給後台——CGI去處理。CGI解析資訊後,向遠程裝置發出控制資訊。裝置響應後,返回給CGI控制資訊,CGI再將資訊解析成變數輸出到Web Server上,最後用戶端得到WebServer發回的頁面訊息(HTML),就能得到現場裝置的運行狀態,實現對現場裝置的遠程監控。

    BOA 伺服器的的主要功能如下圖所示:


    伺服器中主要包括Boa和CGI兩部分,其中Boa管理著返回用戶端的WEB頁面,而CGI控制著用戶端和伺服器端的資訊交換,所以我們可以通過編寫相應的CGI程式來實現所需要的功能。Boa伺服器的實現主要分為兩步,boa伺服器的移植和CGI程式的設計。

        CGI的程式編寫包括兩個部分:HTML代碼和C代碼;CGI程式與Boa Web伺服器之間通過環境變數、命令列參數和標準輸入等方式進行通訊。一般採用表單編碼資訊通過環境變數QUERY_STRING傳遞。在編寫CGI程式時,需要注意

    A、REQUEST_METHOD

    要求方法有兩種:GET和POST。

<form method=get action=/cgi-bin/config.cgi>

    B、環境變數

    在選用不同的方法時,所對應的環境變數也不同。選擇GET時,所對應的環境變數是QUERY_STRING,通過調用庫函數getenv()來得到環境變數的具體值,即querystring=getenv(QUERY_STRING)。系統功能架構圖如下:

        本文主要使用表單實現個人資訊的提交,通過CGI程式處理將資訊資料存放區到SQLite資料庫,其餘網路攝影機資訊採集和GPRS模組由於沒有相關硬體裝置,不在本文實現。

二、HTML資料互動網頁設計


Config.html:

<!doctype html><head><meta http-equiv="Content-Type" content="text/html "><title>系統參數配置</title></head> <body><h3>配置選項</h3> <form enctype="multipart/form-data" action="cgi-bin/config.cgi" method="post">    <fieldset style='width:300px' >    <table>    <legend>個人資訊</legend>        <tr>            <td><label for="name">姓名:</label></td>            <td><input type="text" id="name" name="name" /></td>        </tr>        <tr>            <td><label for="age">年齡:</label></td>            <td><input type="text" id="age" name="age" /></td>        </tr>        <tr>            <td><label for="sex">性別:</label></td>            <td><input type="radio" name='sex' value='boy' />男 <input type="radio" name='sex' value='girl'/>女</td>        </tr>        <tr>            <td><label for="company">單位:</label></td>            <td><input type="text" id="company" name="company" /></td>        </tr>        <tr>            <td><label for="profession">職業:</label></td>            <td><input type="text" id="profession" name="profession" /></td>        </tr>        <tr>            <td><label for="idnumber">社會安全號碼碼:</label></td>            <td><input type="text" id="idnumber" name="idnumber" /></td>        </tr>        <tr>            <td><label for="qq">QQ碼:</label></td>            <td><input type="text" id="qq" name="QQ" /></td>        </tr>        <tr>            <td><label for="email">Email:</label></td>            <td><input type="text" id="email" name="email" /></td>        </tr>        <tr>            <td><label for="telephone">聯絡電話:</label></td>            <td><input type="text" id="telephone" name="telephone" /></td>        </tr>        <tr>            <td>            <center>                <input type="submit" id="ok" name="ok" value="確定"/></td>            </center>            </td>        </tr>    </table>    </fieldset>    <br></form>  </body>
三、CGI程式設計

    將接收的配置資訊返回給用戶端瀏覽器,同時將配置資訊寫入SQLite資料庫。

config.c源碼:

#include <stdio.h>   #include <string.h>   #include <unistd.h>   #include <fcntl.h>   #include <sys/stat.h>   #include <stdlib.h>#include "cgic.h" #include "sqlite3.h" #define SQL_SIZE 256 int cgiMain(int argc, char **argv){char name[64];char age[4];char sex[8];char company[64];char profession[16];char idnumber[24];char qq[24];char email[64];char telephone[16];//回顯資訊到HTML網頁cgiHeaderContentType("text/html"); printf("<html>\n\n");printf("<p>\n\n");//擷取表單中的name的值,存入name緩衝區if(cgiFormString("name", name, sizeof(name)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function name failed");exit(-1);}printf("姓名:%s\n\n",name);printf("<br>\n\n");if(cgiFormString("age", age, sizeof(age)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function age failed");exit(-1);}printf("年齡:%s\n\n",age);printf("<br>\n\n");if(cgiFormString("sex", sex, sizeof(sex)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function sex failed");exit(-1);}printf("性別:%s\n\n",sex);printf("<br>\n\n");if(cgiFormString("company", company, sizeof(company)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function company failed");exit(-1);}printf("單位:%s\n\n",company);printf("<br>\n\n");if(cgiFormString("profession", profession, sizeof(profession)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function profession failed");exit(-1);}printf("職業:%s\n\n",profession);printf("<br>\n\n");if(cgiFormString("idnumber", idnumber, sizeof(idnumber)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function idnumber failed");exit(-1);}printf("社會安全號碼碼:%s\n\n",idnumber);printf("<br>\n\n");if(cgiFormString("email", email, sizeof(email)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function email failed");exit(-1);}printf("Email:%s\n\n",email);printf("<br>\n\n");if(cgiFormString("telephone", telephone, sizeof(telephone)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function telephone failed");exit(-1);}printf("Telepone:%s\n\n",telephone);printf("<br>\n\n");if(cgiFormString("qq", qq, sizeof(qq)) != cgiFormSuccess){fprintf(stderr, "cgiFormString function qq failed");printf("cgiFormString function qq failed");exit(-1);}printf("QQ號碼:%s\n\n",qq);printf("<br>\n\n");printf("</p>\n\n");printf("</html>\n\n");//資料存放區到資料庫sqlite3 *db;int result;result = sqlite3_open("person.db", &db);    if(result != SQLITE_OK)    {        printf("Fail to sqlite3_open person.db: %s.\n", sqlite3_errmsg(db));        exit(-1);    }char *errmsg;char sql[SQL_SIZE];bzero(sql, SQL_SIZE);strcpy(sql, "CREATE TABLE person(ID varchar(24) PRIMARY KEY,name text,age text, sex text, company text, profession text, qq text, email text, telephone text);");    if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != 0)    {        printf("Fail to exec sql: %s.\n", errmsg);        printf("\n%s\n",sql);        exit(-1);    }    bzero(sql, SQL_SIZE);strcpy(sql, "INSERT INTO person values('1234567899876543', '天山老妖', '30', 'boy', '天山', '嵌入式工程師', '123455678', 'hello@qq.com', '123344');");if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != 0)    {        printf("Fail to exec sql: %s.\n\n", errmsg);        printf("\n%s\n",sql);        exit(-1);    }result = sqlite3_close(db);    if(result != 0)    {        fprintf(stderr, "Fail to sqlite3_open person.db : %s.\n", sqlite3_errmsg(db));        exit(-1);    }return 0;}

    注意:程式需要使用CGIC庫的介面函數、SQLite資料庫的介面函數,因此編譯時間需要包括相關檔案sqlite3.c、cgic.c,同時需要SQLite資料庫的動態連結程式庫支援,因此需要將編譯後SQLite資料庫的lib目錄下的檔案拷貝到交叉編譯工具的lib目錄中,編譯時間需指定-lsqlite3。

編譯器:arm-linux-gcc -o config.cgi config.c  sqlite3.c cgic.c -lsqlite3

將編譯後的config.cgi程式拷貝到開發板的www目錄下的cgi-bin目錄

開啟用戶端瀏覽器:http://192.168.6.210/config.html

輸入配置資訊,提交將會顯示配置的內容資訊

查看資料庫中的內容:

sqlite> select * from person;

1234567899876543||30|boy|||123455678|hello@qq.com|123344

sqlite>


    至此,在BOA搭建的嵌入式WEB伺服器上,使用CGIC處理HTML網頁表單提交的資訊,儲存到SQLite資料庫的簡單項目已經完成。本項目只是簡單的介紹的整個過程,是一個入門級的項目。如果有實際項目需求需要更精細的學習相關知識。


本文出自 “生命不息,奮鬥不止” 部落格,轉載請與作者聯絡。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.