📄 esmart.c
字号:
/*
* esmart.c -- E-Sm@rt main functions
*
* Copyright (C) 2002 hisense
*
* Author: kingmonkey <monkey96142@sina.com.cn>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Exceptionally, hisense grants discretionary and conditional permissions for
*additional use of the text contained in the company's release of the hisense
* e-sm@rt provisions set forth hereunder.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the provisions of the GNU
* General Public License.
*
* $Id: esmart.c,v 1.0 2002/04/16 16:50:41 kingmonkey $
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <dirent.h>
#include <errno.h>
#include <termios.h>
#include <getopt.h>
#include <string.h>
#include <fcntl.h>
#include <mathf.h>
#include <linux/sockios.h>
#include <linux/socket.h>
#include <linux/fs.h>
#include <linux/if.h>
#include <linux/in.h>
#include <linux/icmp.h>
#include <linux/route.h>
#include <linux/curtain.h>
#include <linux/metric.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <signal.h>
#include "local.h"
#include "common.h"
#include "esmart.h"
#include "config.h"
#include "mysocket.h"
#include "timer.h"
#define SERVER_PORT 6666
#define ALARM_ADDR 0xe0000000
#define HDR_LEN 2
#define TAIL_LEN 1
#define NUM_LEN 2
#define CHECK_LEN 1
#define MAXLINE 1024
#define IO_ADDRA 0x10000086
#define IO_ADDRB 0x1000008e
#define LIGHT1 0x0400
#define LIGHT2 0x0010
#define LIGHT3 0x0020
#define CURTAIN_ON 0x01
#define ALARM 0x0080 // 警报器所在端口的位置
#define POOR_LIGHT 0x0200 //LCD背光
#define homeaddr 0x07 //家庭地址
#define EMERGENCY 0x0001
//#define WIRELESS 0x0007
#define SETALARM 0x0004
#define CANCALARM 0x0002
#define DEBUG 1
//#define TCS
#undef TCS
extern u8 *metric_file[4];
u32 light[MAX_LIGHT_NBR]={LIGHT1,LIGHT2,LIGHT3};
u8 metric_dev[4][15]={"/dev/metric0", //电表
"/dev/metric1", //电表
"/dev/metric2", //气表
"/dev/metric3" //纯水表
};
s32 cebus_fd;
s32 lcd_fd[2];
s32 boa_fd[2];
#ifdef TCS
s32 tcs_fd;
#else
s32 modem_fd[2];
#endif
u8 tx_buf[MAX_BUF_LEN];
u8 local_buf[MAX_BUF_LEN];
u8 alarm_data[MAX_ALARM_MESSAGE_LEN];
u8 now=5;
u8 alloc_addr=0;
s32 maxfd,maxi;
fd_set allset;
struct sockaddr_in serv_addr;
s32 listenfd;
#define get_state(x,y) (x>>y)&1
/**********************home ia function***************************/
u8 device_online(u8 addr);
u8 device_ishome(u8 addr,u8 type);
/*************************************************/
void create_socket();
void clear_socket();
s32 read_com(s32 fd,u8 *buf,s32 maxcnt);
s32 ftoa(float a,u8 *buf);
s32 open_com(s32 com,s32 baud_rate);
void prosses_alarm();
u16 alarm2type(u16 alarm,u8 *buf);
u16 check_alarm();
u16 alarm2text(u16 alarm,u8* buf);
void alarm_onoff(u8 on);
void light_onoff(u8 which,u8 on);
void poorlight_onoff(u8 on);
void curtain_onoff(u8 on);
//void homeia_command(u8 *buf);
//s16 homeia_check(u8 ia,u8 *buf);
s16 trans_data(u8 *buf,s16 len);
void boa_check(u8 cmd,u8 *buf,u8 len);
void lcd_command(u8 cmd,u8 *buf,u8 len);
u16 recv_data(u8 *buf,s16 len);
u16 produce_transdata(u8 property,u8 cmd,u16 len );
void prosses_broadcast(s32 fd,u8 *cmd_data,u16 len);
s32 dispose_socket(s32 fd,u8 *data,u16 length);
void dispose_lcd(u8 * data, u8 length);
void dispose_tcs(s32 fd,u8 * data,u8 length);
void dispose_modem(u8 * data,u8 length);
void dispose_homeia(u8 * data, u8 length);
void dispose_boa(u8 * data, u8 length);
void tcs_check(s32 fd,u8 cmd,u8 *buf,u8 len);
void tcs_command(s32 fd,u8 cmd,u8 *buf,u8 len);
void prosses_check(s32 fd,u8* cmd_data,u16 len);
void prosses_command(s32 fd,u8 *cmd_data,u16 len);
//s16 my_ultoa(u32 val,u8 *buf);
void set_metric(u8 *buf,s16 len);
void set_metric_value(u8 *buf,s16 len);
void send_local(s32 fd,u8 cmd_type,u8 cmd_nbr,s16 len);
void lcd_transmit(u8 cmd,u8 *buf,u8 len);
void lcd_set_argument(u8 cmd,u8 *buf,u8 len);
void set_argument(s32 fd,u8 *buf,u16 len);
void lcd_check(u8 cmd,u8 *buf,u8 len);
void boa_set_argument(u8 cmd,u8 *buf,u8 len);
u8 do_sum(u8 *buf,u8 len);
u8 get_unused_addr();
u8 read_metric(u8 mode,u8 *buf);
void write_file(u8 num,u8 *buf,s16 len);
s16 set_argfile(u8 *buf,s16 len);
extern void alarm_timer(s32 sig);
void showall(u8 *buf,s32 len);
void save_all();
void client_clear();
void showall(u8 *buf,s32 len){
s32 i;
printf("[show all,data len =%d]:",len);
for(i=0;i<len;i++){
printf("%x ",buf[i]);
}
printf("\n");
}
int main(int argc,char **argv)
{
/***************超时重发定时器******************/
struct sigaction sa;
/*************************the local volatile***************************/
fd_set rfds;
u16 alarm;
s32 nread;
s32 ret,flags;
s32 i,n,clifd;
// s32 i,n,listenfd,clifd;
u8 buf[MAXLINE];
fd_set rset;
s32 mfd;
u16 len=0;
s32 temp;
s32 cli_len;
struct sockaddr_in cli_addr;//,serv_addr;
timer_lst * mytimer;
struct timeval tv={
0, // second
100*1000 // u second;
};
sa.sa_handler =alarm_timer;
if (sigaction(SIGALRM, &sa, NULL) < 0) {
#ifdef DEBUG
printf("alarm timer is error!\n");
#endif
exit(1);
}
FD_ZERO(&allset);
maxi=-1;
/******************** read the config file*************************/
read_io_config();
read_network_config();
read_sensor_config();
read_metric_config();
read_mode_config();
read_metric_config();
read_password_file();
read_dialnbr();
read_curtain_len();
read_io_state();
initial_message();
net_config();
parse_alarm_mask();
read_homeia();
/*******************open the com0 for jiadian ******************************/
// cebus_fd=0;
// maxfd=cebus_fd;
cebus_fd=open_com(1,9600); //网络家电
if(cebus_fd<0){
#ifdef DEBUG
printf("can't open com 0\n");
#endif
}else
FD_SET(cebus_fd,&allset);
maxfd=cebus_fd;
/************************open com1 for tcs or modem fifo*******************/
#ifdef TCS
tcs_fd=open_com(0,9600); //语音模块
if(tcs_fd>0)
FD_SET(tcs_fd,&allset);
maxfd=MAX(maxfd,tcs_fd);
#else
if ((modem_fd[0]=open(name[MODEM][0],O_RDONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[MODEM][0]);
#endif
exit(0);
}
FD_SET(modem_fd[0],&allset);
maxfd=MAX(maxfd,modem_fd[0]);
open(name[MODEM][0],O_WRONLY);
open(name[MODEM][1],O_RDONLY|O_NONBLOCK);
if((modem_fd[1]=open(name[MODEM][1],O_WRONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[MODEM][1]);
#endif
exit(0);
}
local_buf[3]=1;
local_buf[4]='8';
local_buf[5]='0';
local_buf[6]='6';
send_local(modem_fd[1],0x3,0x1,4);
#endif
/************************open lcd fifo*******************/
if ((lcd_fd[0]=open(name[LCD][0],O_RDONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[i][0]);
#endif
exit(0);
}
FD_SET(lcd_fd[0],&allset);
maxfd=MAX(maxfd,lcd_fd[0]);
open(name[LCD][0],O_WRONLY);
open(name[LCD][1],O_RDONLY|O_NONBLOCK);
if((lcd_fd[1]=open(name[LCD][1],O_WRONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[LCD][1]);
#endif
exit(0);
}
/************************open boa fifo*******************/
if ((boa_fd[0]=open(name[BOA][0],O_RDONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[BOA][0]);
#endif
exit(0);
}
FD_SET(boa_fd[0],&allset);
maxfd=MAX(maxfd,boa_fd[0]);
open(name[BOA][0],O_WRONLY);
open(name[BOA][1],O_RDONLY|O_NONBLOCK);
if((boa_fd[1]=open(name[BOA][1],O_WRONLY|O_NONBLOCK))<0){
#ifdef DEBUG
printf("can't open fifo %s\n",name[BOA][1]);
#endif
exit(0);
}
/******************* create local socket listen fd ******************/
create_socket();
local_buf[0]=0xff;
local_buf[1]=0xef;
local_buf[2]=homeaddr;
local_buf[3]=0;
local_buf[4]=do_sum(local_buf,4);
i=0;
while(i<3){
i++;
write(cebus_fd,local_buf,5);
sleep(1);
read(cebus_fd,tx_buf,2);
if((tx_buf[0]==0xff)&&(tx_buf[1]==0xef))
break;
}
#ifdef DEBUG
if(i==3)
printf("the cebus is not online\n");
#endif
local_buf[0]=0xff;
local_buf[1]=0xeb;
local_buf[2]=0x0;
local_buf[3]=do_sum(local_buf,3);
write(cebus_fd,local_buf,4);
/****************** main for select ************************/
for(;;){
rset=allset;
if((n=select(maxfd+1,&rset,NULL,NULL,&tv))<0){
#ifdef DEBUG
printf("select error....\n");
#endif
continue;
}
if(n==0){
// alarm=check_alarm(); //警报查询处理
alarm=0;
if(alarm){
alarm_onoff(1);
#ifdef ESMART_SYSTEM_DEBUG
DSYS("the alarm is 0x%x.....\n",alarm);
#endif
alarm2text(alarm,alarm_data);
printf("the alarm data is:%s\n",alarm_data);
for(i=0;i<MAX_SENSOR_NBR;i++)
if(alarm & (0x8000>>i)){
tx_buf[len+6]=i;
break;
}
/*******LCD报警显示*******/
#ifdef DEBUG
printf("send alarm to lcd .....\n");
#endif
send_local(lcd_fd[1],0X04,0x01,0);
/********语音报警********/
len=alarm2type(alarm,local_buf+3);
#ifdef TCS
mfd=tcs_fd;
#else
mfd=modem_fd[1];
#endif
#ifdef DEBUG
printf("send the alarm to tcs....\n");
#endif
send_local(mfd,0x04,0x01,len);
mytimer=malloc(sizeof(struct timer_lst));
init_timer(mfd,0,len+4,local_buf,mytimer);
add_timer(mytimer);
len=produce_transdata(0x36,0x1,1);
len=trans_data(tx_buf,len);
mfd=server_open(1);
if(mfd>0){
write(mfd,tx_buf,len);
i=client_add(mfd);
FD_SET(mfd,&allset);
if(mfd>maxfd)
maxfd=mfd;
if(i>maxi)
maxi=i;
mytimer=malloc(sizeof(timer_lst));
init_timer(mfd,1,len,tx_buf,mytimer);
add_timer(mytimer);
}
}
continue;
}
if(FD_ISSET(listenfd,&rset)){
if((clifd=accept(listenfd,(struct sockaddr *)&cli_addr,&cli_len))<0){
#ifdef DEBUG
printf("serv_accept error:%d\n",clifd);
#endif
continue;
}
i=client_add(clifd);
FD_SET(clifd,&allset);
if(clifd>maxfd)
maxfd=clifd;
if(i>maxi)
maxi=i;
continue;
}
for(i=0;i<=maxi;i++){
printf("2: here the socket clifd...\n");
if((clifd=client[i].fd)<0)
continue;
if(FD_ISSET(clifd,&rset)){
if((nread=read(clifd,buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on fd %d\n",clifd);
#endif
continue;
}else if(nread==0){
client_del(clifd);
FD_CLR(clifd,&allset);
close(clifd);
#ifdef DEBUG
printf("the socket %d closed\n",clifd);
#endif
continue;
}else{
dispose_socket(clifd, buf,nread);
//处理小区信息
}
}
}
if(FD_ISSET(lcd_fd[0],&rset)){
printf("3: here the lcd fd=%d...\n",lcd_fd[0]);
if((nread=read(lcd_fd[0],buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on lcd_fd \n");
#endif
continue;
}else{
showall(buf,nread);
dispose_lcd(buf,nread); //处理本地lcd
}
}
#ifdef TCS
if(FD_ISSET(tcs_fd,&rset)){
printf("4: here the tcs fd= %d ...\n",tcs_fd);
if((nread=read_com(tcs_fd,buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on com2..\n");
#endif
continue;
}else{
dispose_tcs(tcs_fd,buf,nread); //处理语音模块信息
showall(buf,nread);
}
}
#else
if(FD_ISSET(modem_fd[0],&rset)){
printf("5: here the modem fd= %d ...\n",modem_fd[0]);
if((nread=read(modem_fd[0],buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on modem_fd \n");
#endif
continue;
}else{
showall(buf,nread);
dispose_tcs(modem_fd[1],buf,nread); //处理本地MODEM
}
}
#endif
if(FD_ISSET(cebus_fd,&rset)){
printf("6: here the cebus fd=%d ...\n",cebus_fd);
if((nread=read_com(cebus_fd,buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on com1\n");
#endif
continue;
}else
dispose_homeia(buf,nread); //处理网络家电
}
if(FD_ISSET(boa_fd[0],&rset)){
printf("7: here the boa fd=%d ...\n",boa_fd[0]);
if((nread=read(boa_fd[0],buf,MAXLINE))<0){
#ifdef DEBUG
printf("read error on boa_fd \n");
#endif
continue;
}else
{
showall(buf,nread);
dispose_boa(buf,nread); //处理本地boa
}
}
}
}
s32 open_com(s32 com,s32 baud_rate)
{
static u8 * sci[2]={"/dev/ttyS0","/dev/ttyS1"};
s32 h,baudrate;
struct termios newtio;
bzero(&newtio,sizeof(newtio));
switch ((s32)(baud_rate / 100))
{
case 24: baudrate |= B2400; break;
case 96: baudrate |= B9600; break;
case 192: baudrate |= B19200; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -