嵌入式web终端开发
这个网页上用cgi,javascript,html,c写的,其实我自己比较了解到是html,其次是javascript,对于c书到时看过几片了,但要编程感觉还是不够,cgi以前都没听说过,是最近才看到电子书,现在公司给了我他们以前做的web的程序,看到自己云里雾里的,网上cgi的资料也少,对于c写的cgi,太少了,所以很少棘手,请大家帮忙,分析下程序.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "cgi.h"
typedef unsigned char BYTE;
#define Ver1 2
#define Ver2 2
//#define Protocol_Length 174
char *Get_Val[100];
char first_str[]="Audio_Output_Model";
char second_str[]="Audio_Bit";
char third_str[]="Audio_Type";
char fourth_str[]="Audio_Layer";
char fifth_str[]="Audio_sampling_rate";
char seps[]="=&";
int ret,recvDataLength;
int Length,DesUserID,UserNameLength;
int cgi_audio_stream_type,cgi_audio_layer,cgi_audio_bit_rate,cgi_audio_sampling_rate,cgi_audio_channel;
int t_audio_stream_type,t_audio_layer,t_audio_bit_rate,t_audio_sampling_rate,t_audio_channel;
int road;
int wt;
char str1[]="r1";//路数
char str2[]="r2";
char str3[]="r3";
char str4[]="r4";
char str5[]="w1";
char str6[]="w2";
char str7[]="w3";
char str8[]="w4";
int cgi_init();
int cgi_out(char *buf,int buflen);
int cgi_recv(char *buf);
int cgi_free();
int net_get_err_info(int err_code,char *ret_val);
int Send_Terminal_Audio(int audio_stream_type,int audio_layer,int audio_bit_rate,int audio_sampling_rate,int audio_channel)
{
//printf("%d",road);
if ((road!=1)&&(road!=2)&&(road!=3)&&(road!=4))
{
unsigned char buf[2000];
int i,sum;
i=NoSetTerminal();
buf[0]=0xfb;
buf[1]=((sizeof(pro_Terminal)+17)/256) & 0xff;
buf[2]=(sizeof(pro_Terminal)+17) & 0xff;
buf[3]=0x0c;
buf[4]=0x02;
buf[5]=0;
buf[6]=0;
buf[7]=0;
buf[8]=0;
buf[9]=0;
buf[10]=0;
buf[11]=0;
buf[12]=0;
buf[13]=Ver1;
buf[14]=Ver2;
road=wt;
if (audio_stream_type!=0)
{
pro_Terminal.aud_coding_standard_status[wt-1]=0x01;
pro_Terminal.aud_coding_standard[wt-1]=audio_stream_type;
}
if (audio_layer!=0)
{
pro_Terminal.aud_layer_status[wt-1]=0x01;
pro_Terminal.aud_layer[wt-1]=audio_layer;
}
if (audio_bit_rate!=0)
{
pro_Terminal.aud_bit_rate_status[wt-1]=0x01;
pro_Terminal.aud_bit_rate[wt-1][0]=(((audio_bit_rate/256)/256)/256) & 0xff;
pro_Terminal.aud_bit_rate[wt-1][1]=((audio_bit_rate/256)/256) & 0xff;
pro_Terminal.aud_bit_rate[wt-1][2]=(audio_bit_rate/256) & 0xff;
pro_Terminal.aud_bit_rate[wt-1][3]=audio_bit_rate & 0xff;
}
if (audio_sampling_rate!=0)
{
pro_Terminal.aud_sampling_rate_status[wt-1]=0x01;
pro_Terminal.aud_sampling_rate[wt-1]=audio_sampling_rate;
}
if (audio_channel!=8)
{
pro_Terminal.aud_mode_status[wt-1]=0x01;
pro_Terminal.aud_mode[wt-1]=audio_channel;
}
memcpy(buf+15,&pro_Terminal,sizeof(pro_Terminal));
sum=0;
for(i=0;i<sizeof(pro_Terminal)+15;i++)
{
sum=sum+buf[i];
}
buf[sizeof(pro_Terminal)+15]=sum & 0x7f;
buf[sizeof(pro_Terminal)+16]=0xfe;
cgi_init();
cgi_out((char*)buf,sizeof(pro_Terminal)+17);
cgi_free();
return 1;
}
}
int Send_Check()
{
unsigned char buf[2000];
int i,sum;
//i=NoSetTerminal();
buf[0]=0xfb;
buf[1]=0x00;
buf[2]=0x11;
buf[3]=0x0c;
buf[4]=0x01;
buf[5]=0;
buf[6]=0;
buf[7]=0;
buf[8]=0;
buf[9]=0;
buf[10]=0;
buf[11]=0;
buf[12]=0;
buf[13]=Ver1;
buf[14]=Ver2;
sum=0;
for(i=1;i<15;i++)
{
sum=sum+buf[i];
}
buf[15]=sum & 0x7f;
buf[16]=0xfe;
cgi_out((char*)buf,17);
return 1;
}
int GetIntFromByte(unsigned char* buf,int Len)
{
int i,sum;
sum=0;
for(i=0;i<Len;i++)
{
sum=sum+buf[i]*(int)pow(256,Len-1-i);
}
return sum;
}
int main(void)
{
int i,n,cmp_result,recvDataLength;
char GetCgi[2048],first_value[10],second_value[10],third_value[10],fourth_value[10],fifth_value[10];
char x;
char *p_temp;
n=0;
printf("Content-Type:text/html;charset=gb2312\n\n");
printf("<html>\n");
printf("<head>\n");
printf("<title>音频属性</title>\n");
printf("<script language="JavaScript">\n");
printf("function CheckData()\n");
printf("{\n");
printf("var str;\n");
printf("var i_str;\n");
printf("str=document.forms[0].Audio_sampling_rate.value;\n");
printf("i_str=parseInt(str);\n");
printf("if(isNaN(str))\n");
printf("{\n");
printf("alert("输入不正确,请重新输入.");\n");
printf("return false;\n");
printf("}\n");
printf("if((i_str<0)||(i_str>3))\n");
printf("{\n");
printf("alert("输入不正确,请重新输入.");\n");
printf("return false;\n");
printf("}\n");
printf("return true;\n");
printf("}\n");
printf("</script>\n");
printf("</head>\n");
printf("<body onload=check(); bgcolor="#336699" text="FFFFFF">\n");
if(getenv("CONTENT_LENGTH"))
{
cgi_init();
ret=Send_Check();
unsigned char databuf[32768];
recvDataLength=cgi_recv((char*)databuf);
memcpy(&pro_Terminal,databuf+15,sizeof(pro_Terminal));
cgi_free();
n=atoi(getenv("CONTENT_LENGTH"));
for(i=0;i<n;i++)
{
x=getchar();
sprintf(GetCgi+i,"%c",x);
}
//printf("%s<br>",GetCgi);
i=0;
p_temp=strtok(GetCgi,seps);
while(p_temp!=NULL)
{
Get_Val[i]=p_temp;
i=i+1;
p_temp=strtok(NULL,seps);
}
n=i;
for(i=0;i<n;i++)//查询
{
cmp_result=strcmp(Get_Val[i],str1);
if(cmp_result==0)
{
road=1;
}
cmp_result=strcmp(Get_Val[i],str2);
if(cmp_result==0)
{
road=2;
}
cmp_result=strcmp(Get_Val[i],str3);
if(cmp_result==0)
{
road=3;
}
cmp_result=strcmp(Get_Val[i],str4);
if(cmp_result==0)
{
road=4;
}
}
for(i=0;i<n;i++)//设置
{
cmp_result=strcmp(Get_Val[i],str5);
if(cmp_result==0)
{
wt=1;
}
cmp_result=strcmp(Get_Val[i],str6);
if(cmp_result==0)
{
wt=2;
}
cmp_result=strcmp(Get_Val[i],str7);
if(cmp_result==0)
{
wt=3;
}
cmp_result=strcmp(Get_Val[i],str8);
if(cmp_result==0)
{
wt=4;
}
}
for(i=0;i<n;i++)
{
cmp_result=strcmp(Get_Val[i],first_str);
if(cmp_result==0)
{
strcpy(first_value,Get_Val[i+1]);
//printf("first_Value:%s<br>",first_value);
if(strcmp(first_value, "Stereo")==0)
cgi_audio_channel=0;
if(strcmp(first_value,"Joint")==0)
cgi_audio_channel=1;
if(strcmp(first_value,"Dual")==0)
cgi_audio_channel=2;
if(strcmp(first_value,"Mono")==0)
cgi_audio_channel=3;
}
cmp_result=strcmp(Get_Val[i],second_str);
if(cmp_result==0)
{
strcpy(second_value,Get_Val[i+1]);
//printf("second_Value:%s<br>",second_value);
cgi_audio_bit_rate=atoi(second_value)*1000;
}
cmp_result=strcmp(Get_Val[i],third_str);
if(cmp_result==0)
{
strcpy(third_value,Get_Val[i+1]); // 1(mpeg1);2(mpeg2);6(ac3);7(aac);21(g.711);23(g.723)
//printf("second_Value:%s<br>",second_value);
if(strcmp(third_value,"mpeg1")==0)
cgi_audio_stream_type=1;
if(strcmp(third_value,"mpeg2")==0)
cgi_audio_stream_type=2;
if(strcmp(third_value,"ac3")==0)
cgi_audio_stream_type=6;
if(strcmp(third_value,"aac")==0)
cgi_audio_stream_type=7;
if(strcmp(third_value,"g.711")==0)
cgi_audio_stream_type=21;
if(strcmp(third_value,"g.723")==0)
cgi_audio_stream_type=23;
}
cmp_result=strcmp(Get_Val[i],fourth_str);
if(cmp_result==0)
{
strcpy(fourth_value,Get_Val[i+1]);
//printf("second_Value:%s<br>",second_value);
if(strcmp(fourth_value,"layer1")==0)
cgi_audio_layer=1;
if(strcmp(fourth_value,"layer2")==0)
cgi_audio_layer=2;
if(strcmp(fourth_value,"layer3")==0)
cgi_audio_layer=3;
}
cmp_result=strcmp(Get_Val[i],fifth_str);
if(cmp_result==0)
{
strcpy(fifth_value,Get_Val[i+1]);
cgi_audio_sampling_rate=atoi(fifth_value);
//printf("second_Value:%s<br>",second_value);
}
}
if(cgi_audio_stream_type==pro_Terminal.aud_coding_standard[wt-1])
t_audio_stream_type=0;
else
t_audio_stream_type=cgi_audio_stream_type;
if(cgi_audio_layer==pro_Terminal.aud_layer[wt-1])
t_audio_layer=0;
else
t_audio_layer=cgi_audio_layer;
if(cgi_audio_bit_rate==pro_Terminal.aud_bit_rate[wt-1][0]*256*256*256+pro_Terminal.aud_bit_rate[wt-1][1]*256*256+pro_Terminal.aud_bit_rate[wt-1][2]*256+pro_Terminal.aud_bit_rate[wt-1][3])
t_audio_bit_rate=0;
else
t_audio_bit_rate=cgi_audio_bit_rate;
if(cgi_audio_sampling_rate==pro_Terminal.aud_sampling_rate[wt-1])
t_audio_sampling_rate=0;
else
t_audio_sampling_rate=cgi_audio_sampling_rate;
if(cgi_audio_channel==pro_Terminal.aud_mode[wt-1])
t_audio_channel=8;
else
t_audio_channel=cgi_audio_channel;
ret=Send_Terminal_Audio(t_audio_stream_type,t_audio_layer,t_audio_bit_rate,t_audio_sampling_rate,t_audio_channel);
}
cgi_init();
ret=Send_Check();
unsigned char databuf[32768];
recvDataLength=cgi_recv((char*)databuf);
memcpy(&pro_Terminal,databuf+15,sizeof(pro_Terminal));
cgi_free();
if(recvDataLength<=0)
{
printf("data error1");
}
[解决办法]
程序没发完,后面的主要是html和javascript方面的内容,自己多分析下困难不大,就是上面的部分不是很懂,特别是用c写的cgi部分还是怪自己基础不扎实,不过目前没办法,老大给了任务,要想办法完成请大家多赐教。
[解决办法]
怎么没有人回复啊
[解决办法]
把你不懂的部分提出来才有人回答,没有任何注释的原代码让别人给你分析一下是很困难的