?? new_communication.c
字號:
/********************************************************************
*以下為串口通訊進程 *
*功能:實現(xiàn)家庭網(wǎng)絡中網(wǎng)關與各子節(jié)點通訊 *
*進程名:communication.c *
*此程序為測試版 版號:1.1 *
*完成日期:2004.7.18 *
*進程包含主要函數(shù)有: *
*1、 main函數(shù) //主函數(shù) *
*2、 Signal_Handler_IO //信號處理函數(shù),響應主動上報的報警信息 *
*3、 Alarm_Inf //報警信息處理函數(shù) *
*4、 Renew_Html_Inf //網(wǎng)頁中小區(qū)信息處理函數(shù) *
*5、 Renew_Html //動態(tài)網(wǎng)頁刷新函數(shù) *
*6、 Control_Order_Send //接收并向下轉(zhuǎn)發(fā)控制命令函數(shù) *
*7、 Receive_Packet //接收子節(jié)點脈沖量、模擬量函數(shù) *
*8、 Send_Mail_packet //發(fā)送郵件數(shù)據(jù)包函數(shù) *
*9、 Get_Mail_Address //獲取郵件服務器IP地址 *
*10、Get_User_Inf //獲取用戶信息 *
*11、Get_Collect_Time //從IIC中讀出數(shù)據(jù)采集設定時間參數(shù) *
*12、Anniu_Inf_Send //緊急報警按鈕信息發(fā)送郵件函數(shù) *
*13、Judge_Node_Type //判斷節(jié)點類型函數(shù) *
*14、Select_Time_Interval //網(wǎng)關定時采集數(shù)據(jù)時間判斷函數(shù) *
*進程包含自定義頭文件有: *
*maillip.h //郵件函數(shù)庫頭文件 *
*iic.h //I2C函數(shù)庫頭文件 *
*htmllib.h //動態(tài)網(wǎng)頁函數(shù)庫文件 *
*ds1302.h //ds1302實時時鐘函數(shù)頭文件 *
*communication.h //進程子定義函數(shù)庫頭文件 *
*********************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#include "maillip.h"
#include "iic.h"
#include "htmllib.h"
#include "ds1302.h"
#include "new_communication.h"
/********************************************************************
*信號處理函數(shù) *
*功能:有信號到來,則調(diào)用此函數(shù)。 *
*返回值: wait_flag = FALSE *
*********************************************************************/
void Signal_Handler_IO (int status)
{
printf("received SIGIO signal.\n");
wait_flag = FALSE;
}
/*********************************************************************
*報警信息處理函數(shù) *
*功能:接收并處理來自子節(jié)點的報警或安防信息,并發(fā)送郵件 *
*返回值:0或1或2或-1 *
*返回1表示收到設防信息并設置設防標志; *
*返回0表示收到撤防信息并設置撤防標志; *
*返回2表示收到報警信息并設置報警標志; *
*返回-1表示此數(shù)據(jù)包有誤; *
**********************************************************************/
int Alarm_Inf(BYTE pac_buffer[20])
{
BYTE alarm_buf[20];
BYTE fun_code; //定義數(shù)據(jù)包功能碼
BYTE *alarm_inf,tmp_buf[20];
BYTE *SendMailStr;
BYTE tittle[30]; //定義郵件的標題
BYTE body[50]; //定義郵件的內(nèi)容
BYTE TimeBuffer[8];
BYTE netwk,node; //定義網(wǎng)絡號和節(jié)點號
int show_node;
alarm_inf = tmp_buf;
strcpy(alarm_buf,pac_buffer);
if(alarm_buf[0]==0x44)
{
/*網(wǎng)絡中有兩種節(jié)點號,Zigbee節(jié)點號和撥碼設置節(jié)點號 *
*在網(wǎng)絡初始后他們建立一一對應關系 */
netwk = (alarm_buf[1]&0xf0); //將高四位網(wǎng)絡號賦給netwk
show_node = (int)(alarm_buf[1]&0x0f);//將低四位Zigbee節(jié)點號賦給show_node
fun_code=(alarm_buf[3]&0x0f);
node=(alarm_buf[3]>>4)&0x0f;//將低四位撥碼設置節(jié)點號賦給node
if(fun_code==0x0f)
{
tmp_buf[0] = alarm_buf[5];
tmp_buf[1] = '\0';
if(alarm_buf[5] == alarm_set_mode)
{
/*發(fā)送設防郵件*/
sprintf(tittle,"she fang xin xi");
sprintf(body,"an fang xi tong qi dong");
SendMailStr = Send_Mail_Packet(netwk,node,alarm_inf);
mail(DstStr,SrcStr,tittle,body,SendMailStr);
NetNode[(int)netwk][show_node].state="設防";
Get_Time(&TimeBuffer[0]);
sprintf(NetNode[(int)netwk][show_node].show_node_time, \
"(%02i/%02i;%02i:%02i)",ds_1302_time.month,ds_1302_time.date, \
ds_1302_time.hour,ds_1302_time.min);
/*點亮安防信息指示燈*/
leddev = open("/dev/led",O_RDWR | O_NOCTTY );
if ( leddev == -1 )
{
printf("why not open file \n");
//exit(0);
}
alarm_flag=1; /*設防標志為置1*/
return(1);
}
else if(alarm_buf[5] == alarm_cancel_mode)
{
/*發(fā)送撤防郵件*/
sprintf(tittle,"che fang xing xi");
sprintf(body,"an fang xi tong guan bi");
SendMailStr = Send_Mail_Packet(netwk,node,alarm_inf);
mail(DstStr,SrcStr,tittle,body,SendMailStr);
NetNode[(int)netwk][show_node].state="撤防";
Get_Time(&TimeBuffer[0]);
sprintf(NetNode[(int)netwk][show_node].show_node_time, \
"(%02i/%02i;%02i:%02i)",ds_1302_time.month,ds_1302_time.date, \
ds_1302_time.hour,ds_1302_time.min);
if(alarm_flag==1)
{
close(leddev); /*熄滅安防信息指示燈*/
}
alarm_flag = 0; /*設防標志為置0*/
return(0);
}
else
{printf("alarm data error");return(-1);}
}
else if((fun_code==0x04)||(fun_code==0x05))/*傳送一路報警信息*/
{
if(alarm_flag==0)/*未設防不進行報警處理*/
{
return(2);
}
else if(alarm_flag==1)
{
/*發(fā)送報警郵件*/
tmp_buf[0] = alarm_buf[5];
tmp_buf[1] = '\0';
sprintf(tittle,"jie dian bao jing");
sprintf(body,"you jie dian bao jing");
SendMailStr = Send_Mail_Packet(netwk,node,alarm_inf);
mail(DstStr,SrcStr,tittle,body,SendMailStr);
AlarmNodeNum = show_node;
AlarmNetNum = (int)netwk;
alarm_tran_flag = 1;
NetNode[AlarmNetNum][AlarmNodeNum].state="報警";
/*獲取報警時間*/
Get_Time(&TimeBuffer[0]);
sprintf(NetNode[(int)netwk][show_node].show_node_time, \
"(%02i/%02i;%02i:%02i)",ds_1302_time.month,ds_1302_time.date, \
ds_1302_time.hour,ds_1302_time.min);
return(2);
}
else
{printf("alarm_flag error\n");return(-1);}
}
else
{printf("fun_code error\n");return(-1);}
}
else
{printf("information error\n");return(-1);}
}
/***************************************************************************
*小區(qū)信息處理函數(shù) *
*功能:刷新網(wǎng)頁中的小區(qū)信息。 *
***************************************************************************/
void Renew_Html_Inf(BYTE PipeBuffer1[PIPEBUFFER_SIZE],int num)
{
BYTE TimeBuffer[8];
BYTE buffer[30];
int i;
i = num; //i 表示是小區(qū)的第i條信息
/*獲取小區(qū)信息發(fā)送時間*/
Get_Time(&TimeBuffer[0]);
memset(PipeInfBuffer[i],'\0',sizeof(PipeInfBuffer[i]));
sprintf(buffer,"T(%02i-%02i %02i:%02i)", \
ds_1302_time.month,ds_1302_time.date,ds_1302_time.hour,ds_1302_time.min);
strcat(PipeInfBuffer[i],buffer);
strcat(PipeInfBuffer[i],PipeBuffer1);
}
/***************************************************************************
*函數(shù)名:Renew_Html 動態(tài)網(wǎng)頁刷新 *
* 功能:將數(shù)據(jù)實時添入網(wǎng)頁中,動態(tài)刷新網(wǎng)頁函數(shù) *
***************************************************************************/
void Renew_Html(void)
{
int fd1,i,j,restr,size;
BYTE TimeBuffer[8];
BYTE tmp_buffer[PIPEBUFFER_SIZE+1];
fd1= open("/tmp/index.html", O_WRONLY | O_CREAT | O_TRUNC,0666 );
if (fd1<0) {printf("can't open index.html");}
htmlHeader(fd1);
htmlBody(fd1);
htmlFormStr(fd1);
/*獲取網(wǎng)頁顯示時間*/
Get_Time(&TimeBuffer[0]);
size = sprintf(tmp_buffer,"時間: %s 20%02i-%02i-%02i %02i:%02i:%02i", \
&my_1302_days[TimeBuffer[5]-1][0],ds_1302_time.year,ds_1302_time.month, \
ds_1302_time.date,ds_1302_time.hour,ds_1302_time.min,ds_1302_time.sec);
htmlText(fd1,tmp_buffer,size);
htmlTextHead(fd1);
htmlText(fd1,"<P><font face=\"宋體\" size=\"4\" color=\"#FF00FF\"><b>個人信息:</b></font></P>",73);
htmlText(fd1,"<P><font face=\"楷體_GB2312\" size=\"3\" color=\"#008080\"></font></P>",64);
size = sprintf(tmp_buffer,"用戶名: %s",NameStr);
htmlText(fd1,tmp_buffer,size);
size = sprintf(tmp_buffer,"家庭地址: %s",AddressStr);
htmlText(fd1,tmp_buffer,size);
htmlTextEnd(fd1);
htmlText(fd1,"<P><font face=\"宋體\" size=\"4\" color=\"#FF00FF\"><b>家庭網(wǎng)絡信息:</b></font></P>",77);
htmlText(fd1,"<P><font face=\"楷體_GB2312\" size=\"3\" color=\"#008080\"></font></P>",64);
htmlText2(fd1);
for(i=0;i<NetNum;i++)
{
/*子網(wǎng)1中有節(jié)點0,而子網(wǎng)0中沒有節(jié)點0,有一一對應表*/
/*子網(wǎng)0中節(jié)點0是主節(jié)點*/
if(i!=0)
{j=0;}
else
{j=1;}
for(;j<NodeNum;j++)
{
if(NetNode[i][j].flag==1)
{
restr = (int)(NetNode[i][j].real_node>>4)&0x0f;
htmlText3(fd1);
size = sprintf(tmp_buffer,"%d",i);
htmlText5(fd1,tmp_buffer,size);
size = sprintf(tmp_buffer,"%d",restr);//顯示節(jié)點號
htmlText5(fd1,tmp_buffer,size);
size = sprintf(tmp_buffer,"%u",NetNode[i][j].node_data);
htmlText5(fd1,tmp_buffer,size);//顯示節(jié)點采集數(shù)據(jù)
size = sprintf(tmp_buffer,"%s",NetNode[i][j].show_node_time);
htmlText5(fd1,tmp_buffer,size);//顯示節(jié)點采集數(shù)據(jù)時間
size = sprintf(tmp_buffer,"%d",NetNode[i][j].cell);
htmlText5(fd1,tmp_buffer,size);//顯示節(jié)點電池電量
size = sprintf(tmp_buffer,"%s",NetNode[i][j].funtion);
htmlText5(fd1,tmp_buffer,size);//顯示節(jié)點功能
size = sprintf(tmp_buffer,"%s",NetNode[i][j].state);
htmlText5(fd1,tmp_buffer,size);//顯示節(jié)點狀態(tài)
htmlText6(fd1);
}
}
}
htmlText7(fd1);
htmlTextHead1(fd1);
for(i=0;i<SetInfNum+1;i++)
{
/*用是否顯示T來區(qū)分信息是否讀過*/
if(PipeInfBuffer[i][0]=='T')
{
size = sprintf(tmp_buffer,"小區(qū)信息:%s",PipeInfBuffer[i]);
htmlText1(fd1,tmp_buffer,size);}/*未讀過的小區(qū)信息顯示*/
else
{size = sprintf(tmp_buffer,"小區(qū)信息:%s",PipeInfBuffer[i]);
htmlText(fd1,tmp_buffer,size);}/*已讀過的小區(qū)信息顯示*/
}
htmlTextEnd1(fd1);
if(anniu_flag==1)
{htmlAnniuStr(fd1,AnniuTimeBuffer,TimeSize);}
else
{htmlAnniuQuit(fd1);}
htmlText(fd1,"<p><input type=\"submit\" value=\"信息確認\" name=\"B0\"></p>",55);
htmlFormEnd(fd1);
htmlText4(fd1,"<td width=\"100%\"><p align=center>Copyright© By智園慧家 2002--2003<BR>E-mail:<A href=\"sohu\">ercdc.bjut.edu.cn</A></p></td>",127);
htmlFooter(fd1);
close(fd1);
}
/***************************************************************************
*函數(shù)名:Control_Order_Send 接收并向下轉(zhuǎn)發(fā)控制命令函數(shù) *
*功能:接收控制網(wǎng)頁通過管道(pipe)傳送控制命令,并將按通信協(xié)議打包發(fā)送 *
***************************************************************************/
void Control_Order_Send(int fd,BYTE PipeBuffer[BUFFER_SIZE])
{
int i,res,con_len;
BYTE con_ch;
WORD con_netwk,con_node,re;
BYTE TimeBuffer[8];
int read_flag = 0;
int con_order_flag;
BYTE data_buf[20];
int alarm_return;
int client_fd,show_node;
int open_mode2=O_WRONLY|O_NONBLOCK;
BYTE SenConData[10];
BYTE ResDevData[7]={0x44,0x00,0x02,0x00,0x98,0x00,0x00};//發(fā)送幀格式
BYTE PipeBuf[BUFFER_SIZE];
BYTE some_data[BUFFER_SIZE];
strcpy(PipeBuf,PipeBuffer);
con_len=strlen(PipeBuf);
PipeBuf[con_len] = '\0';
if(strcmp(PipeBuf,"ok") == 0)
{
/*網(wǎng)頁中確認按鈕動作,用戶已看到報警信息 *
*因而給報警節(jié)點發(fā)送命令關掉報警燈 */
con_order_flag = 0;/*無需給網(wǎng)頁返回應答信息因而con_order_flag置0*/
memset(SenConData,'\0',sizeof(SenConData));
SenConData[0] = 0x44;
SenConData[1] = NetNode[AlarmNetNum][AlarmNodeNum].zigbee_node;
SenConData[2] = 0x03;
SenConData[3] = NetNode[AlarmNetNum][AlarmNodeNum].real_node;
SenConData[4] = set_relay_order;
SenConData[5] = 0x3f;
SenConData[6] = Bit_Xor(6,SenConData);
}
else
{
/*收到網(wǎng)頁發(fā)送的控制命令,按不同類型組成命令幀,準備發(fā)送*/
con_order_flag = 1;/*需給網(wǎng)頁返回應答信息因而con_order_flag置1*/
/*打開應答管道,命令控制成功則返回應答信息給網(wǎng)頁*/
client_fd = open(CLIENT_NAME,open_mode2);/*打開返回應答管道*/
if(client_fd == -1){
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -