Embedded Web server

Source: Internet
Author: User

To implement a camera connected to the embedded Development Board on a PC via Web control.

Limited to the Development Board environment, can not choose Appche and other large Web servers, the choice of Boa.

For PC-side cross-platform, it is not possible to use ActiveX controls, if only on the Windows platform is possible, using this method, the PC side to install plug-ins.

So I chose a plain HTML page.

If you just display static content, simply place the HTML page in the root of the BOA, but if you want to create a dynamic page, you have to use the CGI interface.

The Web server running on the embedded also basically only supports the CGI interface, does not support what asp,jsp.

CGI is the Universal Gateway interface, but only interface, can be implemented using any language to achieve input and output, the Web server to redirect input and output

The Web server redirects the input from the Web page to the CGI program in the Cgi-bin directory as input, and the output of the CGI program is redirected to the Web server and sent to the browser via the Web server.

In the embedded environment, the ready-made programming language is shell,c/c++, which is certainly supported, Php,python,perl is also supported, but the scripting language to be executed, it must be configured in another environment,

Just as Java needs a JRE to run.

Here, the Web server program not only to display, but also to control the camera, to communicate with the camera control program, so the shell is not too realistic, so choose c language.

In C, there are two basic types of functions to be implemented, one is to send data from the Web page to extract the parameters of the transfer, and the second is a read-write configuration file. This is a phase in which the communication function is not added.

Draw on other people's Code, the first is to extract page parameters

Header file:

/* Cgivars.h */

#ifndef _cgivars_h
#define _cgivars_h

/* Method */
#define GET0
#define POST1


/* Function Prototypes */
int Getrequestmethod ();
Char **getgetvars ();
Char **getpostvars ();
int cleanUp (int form_method, char **getvars, char **postvars);

void Admincheck ();


#endif/*!_cgivars_h */

Source file:

/* CGIVARS.C
* (C) Copyright, Moreton Bay (http://www.moretonbay.com).
* See HTTP (www.w3.org) and RFC
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "Cgivars.h"

/* Local function Prototypes */
Char Hex2char (char *hex);
void Unescape_url (char *url);
Char x2c (char *what);

/* Hex2char */
/* RFC */
Char Hex2char (char *hex) {
Char Char_value;
Char_value = (hex[0] >= ' A '? ((Hex[0] & 0xdf)-' A ') +: (hex[0]-' 0 '));
Char_value *= 16;
Char_value + = (hex[1] >= ' A '? ((Hex[1] & 0xdf)-' A ') +: (hex[1]-' 0 '));
return char_value;
}

/* Unescape_url */
/* RFC */
void Unescape_url (char *url) {
int n, K;
For (n=0, K=0;url[k];++n, ++k) {
if ((url[n] = url[k]) = = '% ') {
Url[n] = Hex2char (&url[k+1]);
K + = 2;
}
}
Url[n] = ' + ';
}


/* Getrequestmethod
* Retn:from_method (GET or POST) on success,
*-1 on failure. */
int Getrequestmethod () {
Char *request_method;
int Form_method;

Request_method = getenv ("Request_method");
if (Request_method = = NULL)
return-1;

if (!strcmp (Request_method, "GET") | |!strcmp (REQUEST_METHOD, "HEAD") {
Form_method = GET;
} else if (!strcmp (Request_method, "POST")) {
Form_method = POST;
} else {
/* WTF was it then?!! */
return-1;
}
return form_method;
}


/* Getgetvars
* Retn:getvars * *
Char **getgetvars () {
int i;
Char **getvars;
Char *getinput;
Char **pairlist;
int paircount = 0;
Char *nvpair;
Char *eqpos;

GetInput = getenv ("query_string");
if (getinput)
GetInput = StrDup (getinput);

Debugmsg (1, ("gin=%s\n", GetInput));

/* Change all plusses back to spaces */
for (i=0; getinput && getinput[i]; i++)
if (getinput[i] = = ' + ')
Getinput[i] = ";

Pairlist = (char * *) malloc (256*sizeof (char * *));
Paircount = 0;
Nvpair = GetInput? Strtok (GetInput, "&"): NULL;
while (Nvpair) {
pairlist[paircount++]= StrDup (Nvpair);
if (! ( PAIRCOUNT%256))
Pairlist = (char * *) realloc (pairlist, (paircount+256) *sizeof (char * *));
Nvpair = Strtok (NULL, "&");
}

Pairlist[paircount] = 0;
Getvars = (char * *) malloc ((paircount*2+1) *sizeof (char * *));
for (i= 0; i<paircount; i++) {
if (EQPOS=STRCHR (pairlist[i], ' = ')) {
*eqpos = ' + ';
Unescape_url (getvars[i*2+1] = StrDup (eqpos+1));
} else {
Unescape_url (getvars[i*2+1] = StrDup (""));
}
Unescape_url (getvars[i*2] = StrDup (Pairlist[i]));
}
GETVARS[PAIRCOUNT*2] = 0;
for (i=0;pairlist[i];i++)
Free (pairlist[i]);
Free (pairlist);
if (getinput)
Free (getinput);
return getvars;
}


/* Getpostvars
* Retn:postvars * *
Char **getpostvars () {
int i;
int content_length;
Char **postvars;
Char *postinput;
Char **pairlist;
int paircount = 0;
Char *nvpair;
Char *eqpos;

Postinput = getenv ("Content_length");
if (!postinput)
Exit (1);
if (! ( Content_length = Atoi (postinput)))
Exit (1);
if (! ( Postinput = (char *) malloc (content_length+1)))
Exit (1);
if (!fread (Postinput, Content_length, 1, stdin))
Exit (1);
Postinput[content_length] = ' + ';

Debugmsg (1, ("pin=%s\n", Postinput));

for (i=0;postinput[i];i++)
if (postinput[i] = = ' + ')
Postinput[i] = ";

Pairlist = (char * *) malloc (256*sizeof (char * *));
Paircount = 0;
Nvpair = Strtok (Postinput, "&");
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= ' + ';
Unescape_url (postvars[i*2+1] = StrDup (eqpos+1));
} else {
Unescape_url (postvars[i*2+1] = StrDup (""));
}
Unescape_url (postvars[i*2]= strdup (Pairlist[i]));
}
POSTVARS[PAIRCOUNT*2] = 0;

for (i=0;pairlist[i];i++)
Free (pairlist[i]);
Free (pairlist);
Free (postinput);

return postvars;
}

/* CleanUp
* Free the Mallocs * *
int cleanUp (int form_method, char **getvars, char **postvars) {
int i;

if (postvars) {
for (i=0;postvars[i];i++)
Free (postvars[i]);
Free (postvars);
}
if (getvars) {
for (i=0;getvars[i];i++)
Free (getvars[i]);
Free (getvars);
}

return 0;
}


void Admincheck () {
char * userName = NULL;
char * errpage = "location:unauthorizederror.htm\n\n";

UserName = getenv ("Remote_user");

if (UserName = = NULL) goto err;

if (!strcmp ("admin", UserName))
{
Return
}


err:printf ("%s\n", errpage);

Fflush (stdout);
Exit (0);
}

Read the configuration file for the reference of others, other people's reading is read from the configuration file, while the write configuration is simply written into a temporary file, written into the configuration file is the actual control of the camera's service program implementation.

I'm going to change it directly into the configuration file.

Header file:

/*config_phase.h*/

#ifndef _config_phase_h
#define _config_phase_h

#define CONFIG_FILE_NAME "/etc/cfg/config.ini"
#define CONFIG_TEMP_FILE_PROFIX "/tmp/change"
#define Max_file_name_len 30
#define MAX_LINE_LENGTH 80

typedef struct
{
int index;
char * data;

int is_changed;
} Config_item;

FILE *openglobeconfig ();
void Closeglobeconfig (FILE * fp);
int Readglobeconfig (FILE *fp, int config_num, Config_item *items[]);
void Set_config_item (Config_item *items[], int config_num, int index, char * value);

Config_item * * Init_config_item (Config_item *items[], int config_num, int *config_list);
void Free_config_item (Config_item *items[], int config_num);
int getconfvalues (int *config_list, Config_item *items[], int config_num);
void item_update (int index, Config_item *items[], int config_num, char *data);
char *getdata (int index, Config_item *items[], int config_num);
int Saveconfig (char **postvars, config_item **items, int *config_list, char **config_name, int config_num);

Source file:

/* Config_phase.c:phase Config.txt

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "config_phase.h"
#include "sysconf.h"

Char trace_string[200];

void swap (Config_item *a[], int i, int j)
{
Config_item *temp = A[i];
A[i] = A[j];
A[J] = temp;
}

int partition (Config_item *a[], int p, int r)
{
int x = a[r]->index;
int i = p-1;
Int J;
for (j=p; j<r; j + +)
{
if (A[j]->index <= x)
{
i++;
Swap (A, I, j);
}
}
Swap (A, i+1, R);
return i+1;
}

void Quicksort (Config_item *a[], int p, int r)
{
int q;
if (P < r)
{
Q = partition (A, p, R);
Quicksort (A, p, q-1);
Quicksort (A, q+1, R);
}
}


FILE *openglobeconfig ()//return NULL for open error
{
FILE * FP;
fp = fopen (Config_file_name, "RB");

return FP;
}

void Closeglobeconfig (FILE * fp)
{
Fclose (FP);
}

int Readglobeconfig (FILE *fp, int config_num, Config_item *items[])
{
int i;
int j=0;
Char Temp_line[max_line_length];
Char *fake_index, *fake_data;

Quicksort (items, 0, config_num-1);

For (i=0, i<items[config_num-1]->index; i++)//index must start from 1
{
memset (temp_line, 0, max_line_length);
if (Fgets (Temp_line, Max_line_length, FP))
{
if (Items[j]->index = = i+1)
{
Fake_index = Strtok (temp_line, "=");
Fake_data = Strtok (NULL, "=");
if (Fake_data = = NULL)
Fake_data = "";
memset (items[j]->data, 0, strlen (items[j]->data));
strncpy (Items[j]->data, Fake_data, strlen (Fake_data)); With '/n '
Items[j]->data[strlen (Items[j]->data)-1] = 0;
SSCANF (Temp_line, "%s=%s", Fake_index, Items[j]->data);
j + +;
}
}
Else
{
ASSERT ("read NULL line\n");
return 0;
}
}

return 1;
}


FILE *opentempconfig ()//return NULL for open error
{
FILE * FP;
Char Temp_file_name[max_file_name_len];

memset (temp_file_name, 0, Max_file_name_len);
sprintf (Temp_file_name, "%s%d", Config_temp_file_profix, Getpid ());

fp = fopen (Temp_file_name, "WB");

return FP;
}

void Closetempconfig (FILE * fp)
{
Fclose (FP);
}

void Unlinktempconfig ()
{
Char Temp_file_name[max_file_name_len];

memset (temp_file_name, 0, Max_file_name_len);
sprintf (Temp_file_name, "%s%d", Config_temp_file_profix, Getpid ());
Unlink (Temp_file_name);
}


int Writetempconfig (FILE *fp, int config_num, Config_item *items[])
{
int i; Make sure we had sorted items in Readglobeconfig ();
int j = 0;

For (i=0, i<config_num; i++)//index must start from 1
{
if (1 = = items[i]->is_changed)
j + +;
}

fprintf (FP, "%d\n", j);

for (i=0; i<config_num; i++)
{
if (1== items[i]->is_changed)
fprintf (FP, "%d=%s\n", Items[i]->index, Items[i]->data);
}

return 1;
}

int savetempconfig (int config_num, Config_item *items[])
{
FILE * FP,*W_FD,*R_FD;
int I, j=0;

For (i=0, i<config_num; i++)//index must start from 1
{
if (1 = = items[i]->is_changed)
j + +;
}

if (J > 0) {
fp = Opentempconfig ();
fprintf (FP, "%d\n", j);

for (i=0; i<config_num; i++)
{
if (1 = = items[i]->is_changed)
fprintf (FP, "%d=%s\n", Items[i]->index, Items[i]->data);
}

Fclose (FP);

R_FD = Open_read_fifo ();
W_FD = Open_write_fifo ();
Notify_config (W_FD);
Close_write_fifo (W_FD);

Read_fifo (R_FD);
Close_read_fifo ();


Unlinktempconfig ();

}

Return J;
}

Config_item * * Init_config_item (Config_item *items[], int config_num, int *config_list)
{
int i;

Items = malloc (sizeof (Config_item *) *config_num);

for (i=0; i<config_num; i++)
{
Items[i] = malloc (sizeof (Config_item));
Items[i]->data = malloc (max_line_length);
memset (items[i]->data, 0, max_line_length);
Items[i]->index = Config_list[i];
items[i]->is_changed = 0;
}

return items;
}

void Set_config_item (Config_item *items[], int config_num, int index, char * value)
{
int i;

for (i = 0; i < config_num; i++)
if (Items[i]->index = = index)
strcat (Items[i]->data,value);

}

void Free_config_item (Config_item *items[], int config_num)
{
int i;
for (i=0; i<config_num; i++)
{
Free (items[i]->data);
Items[i]->data = NULL;
Free (items[i]);
Items[i] = NULL;
}
Free (items);
Items = NULL;
}

int getconfvalues (int *config_list, Config_item *items[], int config_num)
{
int i;
FILE * FP;

fp = Openglobeconfig ();

if (! Readglobeconfig (FP, config_num, items))
{
ASSERT ("read config error\n");
Closeglobeconfig (FP);
return 0;
}
Closeglobeconfig (FP);

return 1;
}

void item_update (int index, Config_item *items[], int config_num, char *data)
{
int i;

for (i=0; i<config_num; i++)
{
if (Items[i]->index = = index)
{
if (strcmp (Items[i]->data, data )//What ' s the matter
{
memset (trace_string, 0, +);
sprintf (Trace_string, "old:%d:%d:%s\n", items[i]-> Index, strlen (items[i]->data), items[i]->data);
Trace (trace_string);
memset (items[i]->data, 0, strlen (items[i]->data));
strcpy (items[i]->data, data);
items[i]->is_changed = 1;

memset (trace_string, 0, +);
sprintf (trace_string, "new:%d:%d:%s?%d\n", Items[i]->index, strlen (items[i]->data), items[i]->data,items[ i]->is_changed);
Trace (trace_string);
}
}
}

}

char *getdata (int index, Config_item *items[], int config_num)
{
int i;
int Len;
for (i=0; i<config_num; i++)
{
if (Items[i]->index = = index)
{
/*
if (!strcmp (items[i]->data, "\ n"))
memcpy (Items[i]->data, "null", sizeof ("null"));
Else
{
Len = strlen (items[i]->data);
* (items[i]->data+len-1) = 0;
}
*/
Return items[i]->data;
}
}

return NULL;
}

int Saveconfig (char **postvars, config_item **items, int *config_list, char **config_name, int config_num)
{
int I, J;

Getconfvalues (config_list, items, config_num);
if (Postvars = = 0)
return 0;

for (i=0; postvars[i]; i = i + 2)
{
for (j=0; j<config_num; j + +)
{
if (!strcmp (Postvars[i], config_name[j]))
Item_update (Config_list[j], items, Config_num, postvars[i+1]);
}
}

Return Savetempconfig (config_num, items);
}

#endif

Embedded Web server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.