ximolcd 源程序

下面是我在某公司做的一個lcd屏移植程序,代碼如下:

下面是:

/*********************************************
Filename:ximotech.h
Developer:silon
Date:2010-12-15
*********************************************/
#include     <stdio.h>
#include     <stdlib.h>
#include     <unistd.h>
#include     <sys/types.h> 
#include     <sys/stat.h>
#include     <fcntl.h> 
#include     <termios.h>
#include     <errno.h>
#include	   <memory.h>
#include	   <time.h>
#include	   <string.h>
#include     <sys/socket.h>
#include     <sys/ioctl.h>
#include     <net/if.h>
#include     <pthread.h>	

//char buff_read[255];

FILE *file;

char sys_traffic[32];//當前流量信息
char sys_gateway[33];//當前網關
int  sys_nic_count;//系統網卡數量
int  sys_br_count;//系統網橋數量
char sys_nic_name[20][20];//系統各網卡名字
char sys_nic_ip[20][20];//各網卡IP
char sys_nic_mask[20][20];//各網卡子網掩碼"/xx"
char sys_br_name[20][20];//系統各網橋出口網卡名稱
char sys_users[33];//當前用戶數
char sys_connections[33];//當前連接數
char sys_performance[33];//當前CPU、內存佔用率

char LCD[32];//LCD當前顯示內容
int LCD_cursor;//LCD當前光標位置0-31
int LCD_key;//LCD當前按鍵碼,
int LCD_light;//LCD燈狀態:1爲亮,0爲滅;

char *dev_name; //保存串口名稱,默認是/dev/ssyS1
int dev_fd;//串口fd
   
static char *menu0= "    XIMOTECH    www.ximotech.com";
static char *menu1= "   Main menu    Show information";
static char *menu2= "   Main menu     Set parameters ";
static char *menu11="Show information Nic IP Address ";
static char *menu12="Show information     Gateway    ";
static char *menu13="Show information     Users      ";
static char *menu14="Show information  Connections   ";
static char *menu15="Show information  Performance   ";
static char *menu21=" Set Parameters Default Gateway ";
static char *menu22=" Set Parameters  Nic IP Address ";

static char *dir_br = "/xmntc/run/br";
static char *dir_dev = "/proc/net/dev";
static char *dir_route = "/proc/net/route";
static char *dir_users = "/xmntc/run/ipsum";
//static char *dir_connections = "/proc/sys/net/netfilter/nf_conntrack_count";
static char *dir_connections = "/root/ximolcdtest/zhang.txt";

static char *dir_cpu = "/proc/stat";
static char *dir_mem = "/proc/meminfo";

static int up_key=-72;//up鍵碼定義0xB8
static int down_key=-24;//down鍵碼定義0xE8
static int enter_key=-40;//enter鍵碼定義0xD8
static int esc_key=0x78;//esc鍵碼定義0x78
static int no_opration_time=600;//無操作時間,10分鐘

struct timeval last_operation_time;//最後一次按鍵的時間

//add myself varia start
char buff_write_clear[2];
    char buff_write_reset[2];
    char buff_write_direction_left[2];
    char buff_write_direction_right[2];
    char buff_write_hide_cursor[2];
    char buff_write_show_cursor[2];
    char buff_write_move_cursor_left_one[2];
    char buff_write_move_cursor_right_one[2];
    char buff_write_move_lcd_left_one[2];
    char buff_write_move_lcd_right_one[2];
    char buff_write_cursor_location1[3];
    char buff_write_cursor_location2[3];
    char buff_write_lcd_light[2];
    char buff_write_lcd_dark[2];
    char buff_start[2];
char buff_write[512];

char in[20];
char out[20];
//add myself varia end


int get_nic_IP_Address();//獲取各網卡IP地址、子網掩碼

int read_dev();//讀取所有網卡
int read_br_num();//讀取需計算網卡命稱

int Main_menu(int para);//主菜單,0
int Show_information(int para);//顯示信息,1
int Set_parameters(int para);//設置參數,2


int counttime();//計算上一次按鍵到現在之間的時間差,返回秒

int lcd_clear(int fd);//LCD顯示內容清空
int lcd_reset(int fd);//LCD復位,光標移到第一行第一位
int lcd_direction_left(int fd);//光標移動方向設置爲向左
int lcd_direction_right(int fd);//光標移動方向設置爲向右
int lcd_hide_cursor(int fd);//不顯示光標
int lcd_show_cursor(int fd);//顯示光標
int lcd_move_cursor_left_one(int fd);//LCD光標左移一位
int lcd_move_cursor_right_one(int fd);//LCD光標右移一位
int lcd_move_lcd_left_one(int fd);//LCD顯示內容左移一位
int lcd_move_lcd_right_one(int fd);//LCD顯示內容右移一位
int lcd_cursor_location1(int fd);//光標定位在第一行
int lcd_cursor_location2(int fd);//光標定位在第二行
int lcd_light(int fd);//LCD燈亮
int lcd_dark(int fd);//LCD燈滅
int lcd_init();//LCD初始化
int lcd_writestring(int fd,char *stringbuff);//往LCD當前光標處寫字符串
int set_Parity(int fd,int databits,int stopbits,int parity);//設置串口數據位、停止位、校驗位
void set_speed(int fd, int speed);//設置串口速率                                              
int OpenDev(char *Dev);//串口打開


下面是主程序:

#include "ximolcd.h"

extern char**environ;

void timeThread()
{
	int timecou;
  while (1) 
	{
		if (LCD_light==1) {
			timecou=counttime();
			if (timecou>no_opration_time) {
				lcd_dark(dev_fd);
			  LCD_light=0;
			}
		}
		system("sleep 1");
	}
}

 /*讀鍵值*/
void readThread()
{
	int ret;
  char thread_buff_read[10]="";
  while (1) 
	{
	
    ret = read(dev_fd, thread_buff_read, sizeof(thread_buff_read));
    if (ret < 0) 
    {
        printf("read key err!\n");
        continue;
    } 
    switch (thread_buff_read[0])
    {
    	case -72:
	      gettimeofday(&last_operation_time,NULL);
	      if (LCD_light==0) {
	      	lcd_light(dev_fd);
	      	LCD_light=1;
	      }
	      LCD_key=-72;
	      break;
	    case -24:
	      gettimeofday(&last_operation_time,NULL);
	      if (LCD_light==0) {
	      	lcd_light(dev_fd);
	      	LCD_light=1;
	      }
	      LCD_key=-24;
	      break;
    	case -40:
	      gettimeofday(&last_operation_time,NULL);
	      if (LCD_light==0) {
	      	lcd_light(dev_fd);
	      	LCD_light=1;
	      }
	      LCD_key=-40;
	      break;
    	case 0x78:
    		gettimeofday(&last_operation_time,NULL);
    		if (LCD_light==0) {
	      	lcd_light(dev_fd);
	      	LCD_light=1;
	      }
    		LCD_key=0x78;
			break;
		default :break;
    }
  }
}

int main(int argc,char **argv)
{
	//dev_name=(argc<2)?"/dev/ttyS1":argv[1];
	dev_name=(argc<2)?"/dev/cuau1":argv[1];
	lcd_init();
	
	char *tmpstr;
	int i;
	int flag1=0,flag2=0;
	char traffic_str[33]="   IN:            OUT:          ";
	int ret;
	pthread_t id1,id2;
	ret = pthread_create(&id1, NULL, (void*)readThread, NULL);
	if(ret != 0) {
		printf("\readThread_create is failed!\n");
	}
	ret = pthread_create(&id2, NULL, (void*)timeThread, NULL);
	if(ret != 0) {
		printf("\timeThread_create is failed!\n");
	}
	
	while(1)
	{
		read_br_num();
		get_traffic();//讀取traffic
		//read_br_num();
		tmpstr=sys_traffic;
		for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
		lcd_writestring(dev_fd,tmpstr);
		
		if(LCD_key==(-40))
		{
			while(1)
			{
				switch (LCD_key)
				{
					case -40:
						LCD_key=0;
						tmpstr=menu0;
						lcd_writestring(dev_fd,tmpstr);
						while(1)
						{
							switch (LCD_key)
							{
								case -40:
									LCD_key=0;
									Main_menu(1);
									break;
								case 0x78:
									LCD_key=0;
									flag1=1;
									break;
								default:break;	
							}
							if(flag1==1) 
							{
								flag1=0;
								tmpstr=menu0;
								lcd_writestring(dev_fd,tmpstr);
								break;
							}
							system("sleep 0.2");
						}
					case 0x78:
						LCD_key=0;
						flag2=1;
						break;
					default:break;			
				}
				if(flag2==1) 
				{
					flag2=0;
					tmpstr=traffic_str;
					lcd_writestring(dev_fd,tmpstr);
					break;
				}
				system("sleep 0.2");
			}
		}
	}
}


最後面是最後一個.c文件:

#include "ximolcd.h"

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300 };

int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,
38400,  19200,  9600, 4800, 2400, 1200,  300 };

//unsigned long long rec1,rec2,rec3;
//unsigned long long tra1,tra2,tra3;
//modify the up parameter flow
float rec1,rec2,rec3;
float tra1,tra2,tra3;

char buffer[500];


int countTable[256] =
{
0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 
3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
};
int Count(int v)
{ 
  return countTable[v]; 
}


/*int read_br_num()
{
	file = fopen(dir_br, "r");
	if(file == NULL)
	{
		printf("not open the file!\n");
		return -1;
	}
	sys_br_count = 0;
	char tmpstr[20];
	while (fgets(tmpstr,20,file) != NULL)
	{
		if ((strstr(tmpstr,"TH")!=NULL) || (strstr(tmpstr,"LAN")!=NULL))
		{
	    memcpy(sys_br_name[sys_br_count],tmpstr,4);
			sys_br_count++;
		}
	}
	fclose(file);
}*/

int read_br_num()
{
	FILE *stream;
	char *cmd="floweye if stat";
	stream = popen(cmd, "r" );
	
	sys_br_count = 0;
	char tmpstr[20];
	while (fgets(tmpstr,20,stream) != NULL)
	{
		if (strstr(tmpstr,"em")!=NULL)
		{
	    	memcpy(sys_br_name[sys_br_count],tmpstr,3);
			sys_br_count++;
		}
	}
	fclose(stream);
}


/*int read_dev()
{
	file = fopen(dir_dev, "r");
	if(file == NULL)
	{
		printf("no file\n");
		return -1;
	}
	char *p;
	sys_nic_count = 0;
	char tmpstr[20];
	while (fgets(tmpstr,20,file) != NULL)
	{
		if ((strstr(tmpstr,"TH")!=NULL) || (strstr(tmpstr,"LAN")!=NULL))
		{
			p=strstr(tmpstr,":");
			p=p-4;
			memcpy(sys_nic_name[sys_nic_count],p,4);
			sys_nic_count++;
		}
	}
	fclose(file);
}*/
	
int read_dev()
{
	FILE *stream;
	//char *cmd="floweye if stat";
	char *cmd="ifconfig";
	stream = popen(cmd, "r" );
	
	sys_nic_count = 0;
	char tmpstr[20];
	while (fgets(tmpstr,20,stream) != NULL)
	{
		if (strstr(tmpstr,"em")!=NULL)
		{
	    	memcpy(sys_nic_name[sys_nic_count],tmpstr,3);
			sys_nic_count++;
		}
	}
	int i;
	//for(i=0;i<5;i++)
		//printf("read dev later the nic is %s,the length is %d\n",sys_nic_name[i],strlen(sys_nic_name[i]));
	fclose(stream);
	return 0;
}
/*
int get_default_gateway_set()
{
	FILE *file;
	int i;
	file = fopen(dir_route, "r");
	if(file == NULL)
	{
		printf("no file\n");
		return -1;
	}
	char tmpstr[500]="";
	char tmpdes[20]="";
	char tmpgate[20]="";
	int tmptmpint=0;
	while (fgets(tmpstr,500,file) != NULL)
	{
		sscanf(tmpstr,"%*s%s%s%*s%*s%*s%*s%*s%*s%*s%*s",tmpdes,tmpgate);
		printf("tmpdes:%s,tmpgate:%s\n",tmpdes,tmpgate);
		if (strstr(tmpdes,"00000000")!=NULL) 
		{
			int tmptmpint1,tmptmpint2,tmptmpint3,tmptmpint4;
			char *tmpc;
			char tmpc1[4]="";
			tmpc=tmpgate;
			strncpy(tmpc1,tmpc,2);
			tmptmpint4=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmptmpint3=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmptmpint2=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmptmpint1=strtol(tmpc1,NULL,16);
			sprintf(sys_gateway,"Default Gateway %3d.%3d.%3d.%3d ",tmptmpint1,tmptmpint2,tmptmpint3,tmptmpint4);
			tmptmpint=1;
			break;
		}
	}
	if (tmptmpint==0) {sprintf(sys_gateway,"Default Gateway 000.000.000.000 ");}
	fclose(file);
	for (i=16;i<31;i++) {
		if (sys_gateway[i]==' ') {sys_gateway[i]='0';}
	}
}
*/

int get_default_gateway_set()
{
	FILE *stream;
	//char *cmd="floweye if stat";
	char *cmd="netstat -nr";
	stream = popen(cmd, "r" );

	char tmpstr[500]="";
	char tmpdes[20]="";
	char tmpgate[22]="";
	int tmpint=0;
	//while (fgets(tmpstr,500,file) != NULL)
	while (fgets(tmpstr,500,stream) != NULL)
	{
		
		//sscanf(tmpstr,"%*s%s%s%*s%*s%*s%*s%*s%*s%*s%*s",tmpdes,tmpgate);
		sscanf(tmpstr,"%s%s%*s%*s%*s",tmpdes,tmpgate);
		//printf("-------------------------\n");
		//printf("tmpdes:%s,tmpgate:%s\n",tmpdes,tmpgate);
		if ((strstr(tmpdes,"default")!=NULL)  ) 
		{
			int i,length1,length2,length3,length4;
			char *Ppoint1,*Ppoint2,*Ppoint3,*Ppoint4;
			char tmpgate1[20]="",tmpgate2[20]="",tmpgate3[20]="",tmpgate4[20]="";
			Ppoint1=strstr(tmpgate,".");
			Ppoint1++;
			//stpcpy(tmpgate1,Ppoint1);
			Ppoint2=strstr(Ppoint1,".");
			Ppoint2++;		
			Ppoint3=strstr(Ppoint2,".");
			Ppoint3++;
			length1=strlen(tmpgate)-strlen(Ppoint1);
			length2=strlen(Ppoint1)-strlen(Ppoint2);
			length3=strlen(Ppoint2)-strlen(Ppoint3);
			
			int long tmpint1,tmpint2,tmpint3,tmpint4;
			char *tmpc;
			char tmpc1[5]="";
			tmpc=tmpgate;
			strncpy(tmpc1,tmpc,length1-1);
			tmpint4=strtol(tmpc1,NULL,10);

			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length1;
			strncpy(tmpc1,tmpc,length2-1);
			tmpint3=strtol(tmpc1,NULL,10);

			
			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length2;
			strncpy(tmpc1,tmpc,length3-1);
			strncpy(tmpc1,tmpc,1);
			printf("the tmpc1 is %s\n",tmpc1);
			tmpint2=strtol(tmpc1,NULL,10);

			
			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length3;
			strncpy(tmpc1,tmpc,4);
			tmpint1=strtol(tmpc1,NULL,10);
			
			sprintf(sys_gateway,"Default Gateway %3d.%3d.%3d.%3d ",tmpint4,tmpint3,tmpint2,tmpint1);
			tmpint=1;
			break;
		}
	}
	if (tmpint==0) {sprintf(sys_gateway,"Default Gateway 000.000.000.000 ");}
	fclose(stream);
	int i;
	for (i=16;i<31;i++) {
		if (sys_gateway[i]==' ') {sys_gateway[i]='0';}
	}
	return 0;
}

int Set_default_gateway(int para)//設置默認網關地址
{

	int i;
	int entercount=0;
	char *tmpstr;
	char tmpsystem2[50]="";
	char tmpsystem1[50]="";
	char tmpstr1[50]="";
	int tmpint1,tmpint2,tmpint3,tmpintt4;
	get_default_gateway_set();
  tmpstr=sys_gateway;
  lcd_writestring(dev_fd,tmpstr);
  lcd_cursor_location2(dev_fd);
  lcd_show_cursor(dev_fd);
  LCD_cursor=16;
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      if ((LCD_cursor==16)||(LCD_cursor==20)||(LCD_cursor==24)||(LCD_cursor==28)) {
	      	if (sys_gateway[LCD_cursor]<'2') {
	      	  sys_gateway[LCD_cursor]+=1;
	      	  tmpstr=sys_gateway;
    		    lcd_writestring(dev_fd,tmpstr);
    		    lcd_cursor_location2(dev_fd);
    		    for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	        }
	      }
	      if ((LCD_cursor==17)||(LCD_cursor==21)||(LCD_cursor==25)||(LCD_cursor==29)) {
	      	if (sys_gateway[LCD_cursor-1]=='2') {
	      		if (sys_gateway[LCD_cursor]<'5') {
	      	    sys_gateway[LCD_cursor]+=1;
	      	    tmpstr=sys_gateway;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		    }
	        } else {
	        	if (sys_gateway[LCD_cursor]<'9') {
	      	    sys_gateway[LCD_cursor]+=1;
	      	    tmpstr=sys_gateway;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	          }
	        }
	      }
	      if ((LCD_cursor==18)||(LCD_cursor==22)||(LCD_cursor==26)||(LCD_cursor==30)) {
	      	if ((sys_gateway[LCD_cursor-2]=='2')&&(sys_gateway[LCD_cursor-1]=='5')) {
	      		if (LCD_cursor==30) {
	      		  if (sys_gateway[LCD_cursor]<'4') {
	      	      sys_gateway[LCD_cursor]+=1;
	      	      tmpstr=sys_gateway;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		      } 
    		    } else {
    		    	if (sys_gateway[LCD_cursor]<'5') {
	      	      sys_gateway[LCD_cursor]+=1;
	      	      tmpstr=sys_gateway;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		      } 
    		    }
	        } else {
	        	if (sys_gateway[LCD_cursor]<'9') {
	      	    sys_gateway[LCD_cursor]+=1;
	      	    tmpstr=sys_gateway;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	          }
	        }
	      }
	      break;
	    case -24:
	      LCD_key=0;
	      if ((LCD_cursor==18)||(LCD_cursor==22)||(LCD_cursor==26)||(LCD_cursor==30)) {
	      	if ((sys_gateway[LCD_cursor-2]=='0')&&(sys_gateway[LCD_cursor-1]=='0')) { 
	          if (sys_gateway[LCD_cursor]=='1') {
	          	sys_gateway[LCD_cursor]='0';
	          	tmpstr=sys_gateway;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	          } 
	        } 
	      } 
		  if(LCD_cursor>15){
	          if (sys_gateway[LCD_cursor]>'0') {
	            sys_gateway[LCD_cursor]-=1;
	            tmpstr=sys_gateway;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	          }
	      }
	      break;
    	case -40:
	      LCD_key=0;
	      if (LCD_cursor==30) {
	      	entercount++;
    		  if (entercount>2) { 
    		  	entercount=0;
    		  	tmpstr=&sys_gateway[16];
    		  //	if (strstr(tmpstr,"000.000.000.000")!=NULL) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		  	strncpy(tmpstr1,tmpstr,3);
    		  	sscanf(tmpstr1,"%d",&tmpint1);
    		  	if (tmpint1>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		  	tmpstr=&sys_gateway[20];
    		  	strncpy(tmpstr1,tmpstr,3);
    		  	sscanf(tmpstr1,"%d",&tmpint2);
    		  	if (tmpint2>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		  	tmpstr=&sys_gateway[24];
    		  	strncpy(tmpstr1,tmpstr,3);
    		  	sscanf(tmpstr1,"%d",&tmpint3);
    		  	if (tmpint3>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		  	tmpstr=&sys_gateway[28];
    		  	strncpy(tmpstr1,tmpstr,3);
    		  	sscanf(tmpstr1,"%d",&tmpintt4);
    		  	if ((tmpintt4>255)&&(tmpintt4<1)) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		  	lcd_hide_cursor(dev_fd);
//				sprintf(tmpsystem2,"route add default gw %d.%d.%d.%d",tmpint1,tmpint2,tmpint3,tmpintt4);
				sprintf(tmpsystem2,"route add default %d.%d.%d.%d",tmpint1,tmpint2,tmpint3,tmpintt4);
				//get_gateway();
    		  	char tempstr[35]="";
    		  	int tempret;
    		  	//lcd_hide_cursor(dev_fd);
    		  	sprintf(tempstr,"                  seting......  ");
    		  	lcd_writestring(dev_fd,tempstr);
    		  	//tmpstr=&sys_gateway[16];
				if(strstr(tmpstr,"Not defined")==NULL){
	    		  	//sprintf(tmpsystem1,"route del default gw %s",tmpstr);

					//modify code flow
	    		  	sprintf(tmpsystem1,"route del default");
					
					printf("%s\n",tmpsystem1);
	    		  	system(tmpsystem1); //刪除默認網關
	     			system("sleep 0.5");
				}
				printf("%s\n",tmpsystem2);
				tempret=system(tmpsystem2);//添加默認網關
				
    		  	if (tempret==0) {
    		  		sprintf(tempstr,"                Set successfully");
    		  	} else {
    		  		sprintf(tempstr,"                Set unsuccessful");
				}
    		  	lcd_writestring(dev_fd,tempstr);
    		  	system("sleep 1.5");
    		  	tmpstr=menu21;
    		    lcd_writestring(dev_fd,tmpstr);
    		    lcd_cursor_location1(dev_fd);
    		    return 0;
    		  }
    	  } else {
    	  	if (sys_gateway[LCD_cursor+1]=='.') {
    	  		lcd_move_cursor_right_one(dev_fd);
    	  		LCD_cursor++;
    	  	}
    	  	lcd_move_cursor_right_one(dev_fd);
    	  	LCD_cursor++;
    	  	entercount=0;
    	  }
	      break;
    	case 0x78:
    		LCD_key=0;
    		if (LCD_cursor==16) {
    		  tmpstr=menu21;
    		  lcd_writestring(dev_fd,tmpstr);
    		  lcd_cursor_location1(dev_fd);
          lcd_hide_cursor(dev_fd);
    		  return 0;
    	  } else {
    	  	if (sys_gateway[LCD_cursor-1]=='.') {
    	  		lcd_move_cursor_left_one(dev_fd);
    	  		LCD_cursor--;
    	  	}
    	  	lcd_move_cursor_left_one(dev_fd);
    	  	LCD_cursor--;
    	  }
    }
    system("sleep 0.2");
  }	
	
}
int Set_NIC_IP_address(int para)//設置各網卡IP地址
{
	int currnic=0;//當前第幾個網卡
	int entercount=0;//查看狀態下的enter次數
	int entercount1=0;//編輯狀態下的enter次數
	int esccount=0;
	int editing=0;//爲1時是編輯狀態,爲0時是查看狀態
	int i;
	char tmpstr[39];
	char tmpstr1[39];
	get_nic_IP_Address_set();
	sprintf(tmpstr1,"%d/%d    %s",currnic+1,sys_nic_count,sys_nic_name[currnic]);
	for (i=strlen(tmpstr1);i<13;i++) 
		{strcat(tmpstr1," ");}
	sprintf(tmpstr,"%s/%s%s",tmpstr1,sys_nic_mask[currnic],sys_nic_ip[currnic]);
  for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
  lcd_writestring(dev_fd,tmpstr);
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;entercount=0;esccount=0;
	      if (editing==0) {
	        if (currnic>0)
	        {
	        	currnic--;
            sprintf(tmpstr1,"%d/%d    %s",currnic+1,sys_nic_count,sys_nic_name[currnic]);
	          for (i=strlen(tmpstr1);i<13;i++) {strcat(tmpstr1," ");}
          	sprintf(tmpstr,"%s/%s%s",tmpstr1,sys_nic_mask[currnic],sys_nic_ip[currnic]);
            for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
            lcd_writestring(dev_fd,tmpstr);
	        }
	      } else {
	      	if (LCD_cursor==14)  {
	      		if (tmpstr[LCD_cursor]<'3') {
	        	  tmpstr[LCD_cursor]+=1;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      lcd_move_cursor_left_one(dev_fd);
    		      lcd_move_cursor_left_one(dev_fd);
	          }
	      	}
	      	if (LCD_cursor==15)  {
	      		if (tmpstr[14]=='3') {
	      			if (tmpstr[LCD_cursor]<'1') {
	        	    tmpstr[LCD_cursor]+=1;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        lcd_move_cursor_left_one(dev_fd);
	            }
	      		} else {
	      			if (tmpstr[LCD_cursor]<'9') {
	        	    tmpstr[LCD_cursor]+=1;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        lcd_move_cursor_left_one(dev_fd);
	            }
	          }
	      	}
	        if ((LCD_cursor==16)||(LCD_cursor==20)||(LCD_cursor==24)||(LCD_cursor==28)) {
	        	if (tmpstr[LCD_cursor]<'2') {
	        	  tmpstr[LCD_cursor]+=1;
    		      lcd_writestring(dev_fd,tmpstr);
    		      lcd_cursor_location2(dev_fd);
    		      for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	          }
	        }
	        if ((LCD_cursor==17)||(LCD_cursor==21)||(LCD_cursor==25)||(LCD_cursor==29)) {
	        	if (tmpstr[LCD_cursor-1]=='2') {
	        		if (tmpstr[LCD_cursor]<'5') {
	        	    tmpstr[LCD_cursor]+=1;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		      }
	          } else {
	          	if (tmpstr[LCD_cursor]<'9') {
	        	    tmpstr[LCD_cursor]+=1;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	            }
	          }
	        }
	        if ((LCD_cursor==18)||(LCD_cursor==22)||(LCD_cursor==26)||(LCD_cursor==30)) {
	        	if ((tmpstr[LCD_cursor-2]=='2')&&(tmpstr[LCD_cursor-1]=='5')) {
	        		if (LCD_cursor==30) {
	        		  if (tmpstr[LCD_cursor]<'4') {
	        	      tmpstr[LCD_cursor]+=1;
    		          lcd_writestring(dev_fd,tmpstr);
    		          lcd_cursor_location2(dev_fd);
    		          for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		        } 
    		      } else {
    		      	if (tmpstr[LCD_cursor]<'5') {
	        	      tmpstr[LCD_cursor]+=1;
    		          lcd_writestring(dev_fd,tmpstr);
    		          lcd_cursor_location2(dev_fd);
    		          for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
    		        } 
    		      }
	          } else {
	          	if (tmpstr[LCD_cursor]<'9') {
	        	    tmpstr[LCD_cursor]+=1;
    		        lcd_writestring(dev_fd,tmpstr);
    		        lcd_cursor_location2(dev_fd);
    		        for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	            }
	          }
	        }	      	
	      }                   
	      break;
	    case -24:
	      LCD_key=0;entercount=0;esccount=0;
	      if (editing==0) {
	        if (currnic<(sys_nic_count-1))
	        {
	        	currnic++;
            sprintf(tmpstr1,"%d/%d    %s",currnic+1,sys_nic_count,sys_nic_name[currnic]);
	          for (i=strlen(tmpstr1);i<13;i++) {strcat(tmpstr1," ");}
          	sprintf(tmpstr,"%s/%s%s",tmpstr1,sys_nic_mask[currnic],sys_nic_ip[currnic]);
            for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
            lcd_writestring(dev_fd,tmpstr);
	        }  
	      } else {
		      	if ((LCD_cursor==14) || (LCD_cursor==15)) {
	      			if (tmpstr[LCD_cursor]>'0') {
	           	 tmpstr[LCD_cursor]-=1;
    		       lcd_writestring(dev_fd,tmpstr);
    		       lcd_cursor_location2(dev_fd);
    		       if (LCD_cursor==14) {lcd_move_cursor_left_one(dev_fd);}
    		       lcd_move_cursor_left_one(dev_fd);
    		       //system("sleep 0.2");
	           }
	      	}
	        if ((LCD_cursor==18)||(LCD_cursor==22)||(LCD_cursor==26)||(LCD_cursor==30)) {
	       	if ((tmpstr[LCD_cursor-2]=='0')&&(tmpstr[LCD_cursor-1]=='0')) { 
	           if (tmpstr[LCD_cursor]=='1') {                                                 
	           	 tmpstr[LCD_cursor]='0';
    		       lcd_writestring(dev_fd,tmpstr);
    		       lcd_cursor_location2(dev_fd);
    		       for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	           } 
	         } 
	       }  
	       	 if (LCD_cursor>15) {
	           if (tmpstr[LCD_cursor]>'0') {
	             tmpstr[LCD_cursor]-=1;
    		       lcd_writestring(dev_fd,tmpstr);
    		       lcd_cursor_location2(dev_fd);
    		       for (i=16;i<LCD_cursor;i++) {lcd_move_cursor_right_one(dev_fd);}
	           }
	         }
	       
	      }
	      break;
    	case -40:
	      LCD_key=0;esccount=0;
	      if (editing==0) {
	      	entercount++;
	        if (entercount==3) {
	        	editing=1;entercount=0;
	        	lcd_cursor_location2(dev_fd);
	        	lcd_move_cursor_left_one(dev_fd);
	        	lcd_move_cursor_left_one(dev_fd);
	        	lcd_show_cursor(dev_fd);
	        	LCD_cursor=14;
	        	entercount1=0;
	        }
	      } else {
	      	if (LCD_cursor==30) {
	      		entercount1++;
	      		if (entercount1==3) {
	      			entercount1=0;	                                                                                                                         
	      			char tempstr[32]="";
	      			char tempip[20]="";
	      			char tempmask[4]="";
	      			char *tempstr1;
					char tempstr3[40]="";
    		  	  int tempret;
    		  	  char *tmptmpstr;
    		  	  char tmptmpstr1[50]="";
    		  	  int tmptmpint;
    		  	  tmptmpstr=&tmpstr[14];
    		    	strncpy(tmptmpstr1,tmptmpstr,2);
    		    	sscanf(tmptmpstr1,"%d",&tmptmpint);
    		    	if (tmptmpint>31) {
    		    		lcd_cursor_location2(dev_fd);
    		    		lcd_move_cursor_left_one(dev_fd);
	        	        lcd_move_cursor_left_one(dev_fd);
    		    		LCD_cursor=14;
    		    		break;
    		    	}
    		    	tmptmpstr=&tmpstr[16];
    		    	strncpy(tmptmpstr1,tmptmpstr,3);
    		    	sscanf(tmptmpstr1,"%d",&tmptmpint);
    		    	if (tmptmpint>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		    	tmptmpstr=&tmpstr[20];
    		    	strncpy(tmptmpstr1,tmptmpstr,3);
    		    	sscanf(tmptmpstr1,"%d",&tmptmpint);
    		    	if (tmptmpint>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		    	tmptmpstr=&tmpstr[24];
    		    	strncpy(tmptmpstr1,tmptmpstr,3);
    		    	sscanf(tmptmpstr1,"%d",&tmptmpint);
    		    	if (tmptmpint>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
    		    	tmptmpstr=&tmpstr[28];
    		    	strncpy(tmptmpstr1,tmptmpstr,3);
    		    	sscanf(tmptmpstr1,"%d",&tmptmpint);
    		    	if (tmptmpint>255) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}	
    		  	  tempstr1=&tmpstr[13];
    		  	  strncpy(tempmask,tempstr1,3);
				  if(tempmask[1]=='0'){
					tempmask[1]=tempmask[2];
					tempmask[2]='\0';
				  }
				  tmptmpint=atoi(tempmask);
				  if (tmptmpint>31) {lcd_cursor_location2(dev_fd);LCD_cursor=16;break;}
				  
				  lcd_hide_cursor(dev_fd);
    		  	  sprintf(tempstr,"                  seting......  ");
    		  	  lcd_writestring(dev_fd,tempstr);
    		  	  tempstr1=&tmpstr[16];
    		  	  strncpy(tempip,tempstr1,15); 
	   		  	  int tmpip1,tmpip2,tmpip3,tmpip4;
    		  	  sscanf(tempip,"%d.%d.%d.%d",&tmpip1,&tmpip2,&tmpip3,&tmpip4);
				  tempstr1=tempstr3;
				  if((tmpip1+tmpip2+tmpip3+tmpip4)==0){
				  sprintf(tempstr1,"ifconfig %s %d.%d.%d.%d up",sys_nic_name[currnic],tmpip1,tmpip2,tmpip3,tmpip4);
				  }
				  else{
    		  	  sprintf(tempstr1,"ifconfig %s %d.%d.%d.%d%s up",sys_nic_name[currnic],tmpip1,tmpip2,tmpip3,tmpip4,tempmask);
    		  	  }
				  printf("%s\n",tempstr1);                                                                                                         
    		  	  tempret=system(tempstr1);
    		  	  system("sleep 0.3");
    		  	  if (tempret==0) {
    		  	  	sprintf(tempstr,"                Set successfully");
    		  	  } else {sprintf(tempstr,"                Set unsuccessful");}
    		  	  lcd_writestring(dev_fd,tempstr);
    		  	  system("sleep 1.5");
	      			sprintf(tmpstr,"%s",menu22);
    		      lcd_writestring(dev_fd,tmpstr);
    		      return 0;
	      		}
	      	} else {
	      		entercount1=0;
	      		if (tmpstr[LCD_cursor+1]=='.') {
    	  	  	lcd_move_cursor_right_one(dev_fd);
    	  	  	LCD_cursor++;
    	  	  }
    	  	  lcd_move_cursor_right_one(dev_fd);
    	    	LCD_cursor++;
	      	}
	      }
	      break;
    	case 0x78:
    		LCD_key=0;entercount=0;
    		if (editing==0) {
    		  sprintf(tmpstr,"%s",menu22);
    		  lcd_writestring(dev_fd,tmpstr);
    		  return 0;
    		} else {
    			//printf("%d,%d,%d\n",LCD_cursor,esccount,editing);
    		  if (LCD_cursor==14)  {
    				esccount++;
    				if (esccount==3) {
    			  	esccount=0;editing=0;
	          	lcd_hide_cursor(dev_fd);
	          }
    			} else {
    				esccount=0;
    				if (tmpstr[LCD_cursor-1]=='.') {
    	  	  	lcd_move_cursor_left_one(dev_fd);
    	  	  	LCD_cursor--;
    	  	  }
    	  	  lcd_move_cursor_left_one(dev_fd);
    	    	LCD_cursor--;
    			}
    		} break;
			default:break;
    }
    system("sleep 0.2");
  }	
}

/*int get_traffic()//統計當前流量
{
	file = fopen(dir_dev, "r");
	//printf("-----------------------------\n");
	if(file == NULL)
	{
		printf("no file\n");
		return -1;
	}
	char *p;
	char tmpstr[500];
	int i,tmpint;
	rec1=0;rec2=0;
	tra1=0;tra2=0;
	while (fgets(tmpstr,500,file) != NULL)
	{
		if ((strstr(tmpstr,"TH")!=NULL) || (strstr(tmpstr,"LAN")!=NULL))
		{
			tmpint=0;
			for (i=0;i<sys_br_count;i++)
			{
				if (strstr(tmpstr,sys_br_name[i])!=NULL) {tmpint++;}
			}
			//printf("1----sys_br_count:%d  tmpint:%d\n",sys_br_count,tmpint);
			if ((sys_br_count==0) || (tmpint>0))
			{
				strcpy(buffer, tmpstr + 7);
		    sscanf(buffer,"%u%*u%*u%*u%*u%*u%*u%*u%u",&rec3,&tra3);
	    	rec1 += rec3;
	    	tra1 += tra3;
	    	//printf("i:%d\n",i);
			}
		}
	}
	fclose(file);
	system("sleep 1");
	file = fopen(dir_dev, "r");
	while (fgets(tmpstr,500,file) != NULL)
	{
		if ((strstr(tmpstr,"TH")!=NULL) || (strstr(tmpstr,"LAN")!=NULL))
		{
			tmpint=0;
			for (i=0;i<sys_br_count;i++)
			{
				if (strstr(tmpstr,sys_br_name[i])!=NULL) {tmpint++;}
			}
			//printf("2----sys_br_count:%d  tmpint:%d\n",sys_br_count,tmpint);
			if ((sys_br_count==0) || (tmpint>0))
			{
				strcpy(buffer, tmpstr + 7);
		    sscanf(buffer,"%u%*u%*u%*u%*u%*u%*u%*u%u",&rec3,&tra3);
	    	rec2 += rec3;
	    	tra2 += tra3;
			}
		}
	}
	rec3=rec2-rec1;
	tra3=tra2-tra1;
	char *recdanwei;
	char *tradanwei;
	double din,dout;
	recdanwei=" ";
	din=rec3*8;
	//printf("din:%4.1f\n",din);
	if (din>1024) {
		din/=1024;
		recdanwei="K";
	}
	if (din>1024) {
		din/=1024;
		recdanwei="M";
	}
	if (din>1024) {
		din/=1024;
		recdanwei="G";
	}
	tradanwei=" ";
	dout=tra3*8;
	//printf("dout:%4.1f\n",dout);
	if (dout>1024) {
		dout/=1024;
		tradanwei="K";
	}
	if (dout>1024) {
		dout/=1024;
		tradanwei="M";
	}
	if (dout>1024) {
		dout/=1024;
		tradanwei="G";
	}
  fclose(file); 
	sprintf(sys_traffic,"   IN:%4.0f%sb/s    OUT:%4.0f%sb/s",din,recdanwei,dout,tradanwei);
}*/

int get_traffic()//統計當前流量
{

	FILE *stream;
	char *cmd="floweye if stat";
	stream = popen(cmd, "r" );
	
	char tmpstr[500];
	int i,tmpint;
	char bpsin[10]="",bpsout[10]="";
	float bpsInFloat,bpsOutFloat;
	char *recdanwei;
	char *tradanwei;
	float din,dout;
	rec1=0;tra1=0;
	while (fgets(tmpstr,500,stream) != NULL)
	{
		if (strstr(tmpstr,"em")!=NULL)
		{
			tmpint=0;
			for (i=0;i<sys_br_count;i++)
			{
				if (strstr(tmpstr,sys_br_name[i])!=NULL) {tmpint++;}
			}
			//printf("1----sys_br_count:%d  tmpint:%d\n",sys_br_count,tmpint);
			if ((sys_br_count==0) || (tmpint>0))
			{
				//add myself code flow start
			    sscanf(tmpstr,"%*s%s%s",bpsin,bpsout);
				char CbpsIntmp[10]="",CbpsOuttmp[10]="";
				if(strstr(bpsin,"M") !=NULL){
					for(i=0;i<strlen(bpsin)-1;i++)
						CbpsIntmp[i]=bpsin[i];
					CbpsIntmp[i]='\0';
					bpsInFloat=atof(CbpsIntmp)*1024*1024;
				}
				else if(strstr(bpsin,"K") !=NULL){
					for(i=0;i<strlen(bpsin)-1;i++)
						CbpsIntmp[i]=bpsin[i];
					CbpsIntmp[i]='\0';
					bpsInFloat=atof(CbpsIntmp)*1024;
				}else{
					bpsInFloat=atof(bpsin);
				}

				if(strstr(bpsout,"M") !=NULL){
					for(i=0;i<strlen(bpsout)-1;i++)
						CbpsOuttmp[i]=bpsout[i];
					CbpsOuttmp[i]='\0';
					bpsOutFloat=atof(CbpsOuttmp)*1024*1024;
				}
				else if(strstr(bpsout,"K") !=NULL){
					for(i=0;i<strlen(bpsout)-1;i++)
						CbpsOuttmp[i]=bpsout[i];
					CbpsOuttmp[i]='\0';
					bpsOutFloat=atof(CbpsOuttmp)*1024;
				}else{
					bpsOutFloat=atof(bpsout);
				}			
		    	rec1 += bpsInFloat;
		    	tra1 += bpsOutFloat;
			}
		}
	}
	fclose(stream);
    //add myself code up end
	
	recdanwei=" ";
	din=rec1*8;
	//printf("din:%4.1f\n",din);
	if (din>1024) {
		din/=1024;
		recdanwei="K";
	}
	if (din>1024) {
		din/=1024;
		recdanwei="M";
	}
	if (din>1024) {
		din/=1024;
		recdanwei="G";
	}
	tradanwei=" ";
	dout=tra1*8;
	//printf("dout:%4.1f\n",dout);
	if (dout>1024) {
		dout/=1024;
		tradanwei="K";
	}
	if (dout>1024) {
		dout/=1024;
		tradanwei="M";
	}
	if (dout>1024) {
		dout/=1024;
		tradanwei="G";
	}
	sprintf(sys_traffic,"   IN:%4.0f%sb/s    OUT:%4.0f%sb/s",din,recdanwei,dout,tradanwei);
	system("sleep 0.5");
	return 0;
	
}


/*int Traffic()//顯示流量
{
	char *tmpstr;
  while (1) 
	{
		read_br_num();
    get_traffic();
    read_br_num();
    tmpstr=sys_traffic;
    int i;
    for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
    lcd_writestring(dev_fd,tmpstr);
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      break;
	    case -24:
	      LCD_key=0;
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu11;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 0.2");
  }	
}*/

int get_nic_IP_Address_set()//獲取各網卡IP地址、子網掩碼
{
	struct ifreq ifreq;
  int sock;
	int i,j;
	int tmpint;
	read_dev();
	for (i=0;i<sys_nic_count;i++)
	{
		if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
       perror("socket");
       return ;
    }
    strcpy(ifreq.ifr_name,sys_nic_name[i]);
    if(ioctl(sock,SIOCGIFADDR,&ifreq)<0) {
      sprintf(sys_nic_ip[i],"000.000.000.000");
    } else {
      sprintf(sys_nic_ip[i],"%3d.%3d.%3d.%3d",
      (unsigned char)ifreq.ifr_addr.sa_data[2],
      (unsigned char)ifreq.ifr_addr.sa_data[3],
      (unsigned char)ifreq.ifr_addr.sa_data[4],
      (unsigned char)ifreq.ifr_addr.sa_data[5]);
      for (j=0;j<15;j++) {
		    if (sys_nic_ip[i][j]==' ') {sys_nic_ip[i][j]='0';}
	    }
    }
    if(ioctl(sock,SIOCGIFNETMASK,&ifreq)<0) {
      sprintf(sys_nic_mask[i],"00");
    } else {
      sprintf(sys_nic_mask[i],"%2d",
      Count((unsigned char)ifreq.ifr_netmask.sa_data[2])+
      Count((unsigned char)ifreq.ifr_netmask.sa_data[3])+
      Count((unsigned char)ifreq.ifr_netmask.sa_data[4])+
      Count((unsigned char)ifreq.ifr_netmask.sa_data[5]));
      for (j=0;j<2;j++) {
		    if (sys_nic_mask[i][j]==' ') {sys_nic_mask[i][j]='0';}
	    }
    }
	}
}

/*
int get_nic_IP_Address()//獲取各網卡IP地址、子網掩碼
{
	struct ifreq ifreq;
  int sock;
	int i;
	int tmpint;
	read_dev();
	for (i=0;i<sys_nic_count;i++)
	{
		if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
       		perror("socket");
       		return ;
    	}
	    strcpy(ifreq.ifr_name,sys_nic_name[i]);
	    if(ioctl(sock,SIOCGIFADDR,&ifreq)<0) {
	      sprintf(sys_nic_ip[i],"Not set");
	    } else {
	      sprintf(sys_nic_ip[i],"%d.%d.%d.%d",
	      (unsigned char)ifreq.ifr_addr.sa_data[2],
	      (unsigned char)ifreq.ifr_addr.sa_data[3],
	      (unsigned char)ifreq.ifr_addr.sa_data[4],
	      (unsigned char)ifreq.ifr_addr.sa_data[5]);
	    }
		printf("the %dth ip is %s\n",i+1,sys_nic_ip[i]);
	    if(ioctl(sock,SIOCGIFNETMASK,&ifreq)<0) {
	      sprintf(sys_nic_mask[i],"Not set");
	    } else {
	      sprintf(sys_nic_mask[i],"%d",
	      Count((unsigned char)ifreq.ifr_netmask.sa_data[2])+
	      Count((unsigned char)ifreq.ifr_netmask.sa_data[3])+
	      Count((unsigned char)ifreq.ifr_netmask.sa_data[4])+
	      Count((unsigned char)ifreq.ifr_netmask.sa_data[5]));
	      
	    }
	}
}*/

int get_nic_IP_Address()//獲取各網卡IP地址、子網掩碼
{
	struct ifreq ifreq;
  	int sock;
	int i;
	int tmpint;
	read_dev();

	for (i=0;i<sys_nic_count;i++)
	{
		if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
           perror("socket");
           return ;
           }
        strcpy(ifreq.ifr_name,sys_nic_name[i]);
        if(ioctl(sock,SIOCGIFADDR,&ifreq)<0) {
          sprintf(sys_nic_ip[i],"Not set");
        } else {
          sprintf(sys_nic_ip[i],"%d.%d.%d.%d",
          (unsigned char)ifreq.ifr_addr.sa_data[2],
          (unsigned char)ifreq.ifr_addr.sa_data[3],
          (unsigned char)ifreq.ifr_addr.sa_data[4],
          (unsigned char)ifreq.ifr_addr.sa_data[5]);
        }
        if(ioctl(sock,SIOCGIFNETMASK,&ifreq)<0) {
          sprintf(sys_nic_mask[i],"Not set");
        } else {
          sprintf(sys_nic_mask[i],"%d",
          Count((unsigned char)ifreq.ifr_netmask.sa_data[2])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[3])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[4])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[5]));
          
        }
	}
	 for(i=0;i<sys_nic_count;i++)
        printf("%s/%s\n",sys_nic_ip[i],sys_nic_mask[i]);
}
/*

int get_nic_IP_Address()
{
    struct ifreq ifreq;
    int sock;
    int i;
    int tmpint;
	char sys_nic_ip[20][20];
char sys_nic_mask[20][20];
char sys_nic_name[20][20]={{"em0"},{"em1"},{"em2"},{"em3"}};
    //read_dev();
    for (i=0;i<4;i++)
    {
        if((sock=socket(AF_INET,SOCK_STREAM,0))<0){
           perror("socket");
           return ;
           }
        strcpy(ifreq.ifr_name,sys_nic_name[i]);
        if(ioctl(sock,SIOCGIFADDR,&ifreq)<0) {
          sprintf(sys_nic_ip[i],"Not set");
        } else {
          sprintf(sys_nic_ip[i],"%d.%d.%d.%d",
          (unsigned char)ifreq.ifr_addr.sa_data[2],
          (unsigned char)ifreq.ifr_addr.sa_data[3],
          (unsigned char)ifreq.ifr_addr.sa_data[4],
          (unsigned char)ifreq.ifr_addr.sa_data[5]);
        }
        if(ioctl(sock,SIOCGIFNETMASK,&ifreq)<0) {
          sprintf(sys_nic_mask[i],"Not set");
        } else {
          sprintf(sys_nic_mask[i],"%d",
          Count((unsigned char)ifreq.ifr_netmask.sa_data[2])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[3])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[4])+
          Count((unsigned char)ifreq.ifr_netmask.sa_data[5]));
          
        }

    }

    for(i=0;i<4;i++)
        printf("%s/%s\n",sys_nic_ip[i],sys_nic_mask[i]);

    return 0;
}*/



/*int Nic_IP_Address()//顯示各網卡IP地址
{
	int currnic=0;
	char tmpstr[39];
	get_nic_IP_Address();
	if (strlen(sys_nic_name[currnic])==4) {
		//sprintf(tmpstr,"NIC(%d/%d)    %s%s/%s",currnic+1,sys_nic_count,
	    //    sys_nic_name[currnic],sys_nic_ip[currnic],sys_nic_mask[currnic]);
		   if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
				sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
		   }	
	       else{
	       sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
		   sys_nic_name[currnic],sys_nic_mask[currnic]);
	      }	 
	}
	if (strlen(sys_nic_name[currnic])==5) {
		if(strstr(sys_nic_mask[currnic],"Not")==NULL){
			if(strlen(sys_nic_mask[currnic])==1){
			sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
			sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
			}
			else{
			sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
			sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
			}
		}
		else{
		sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
		sys_nic_name[currnic],sys_nic_mask[currnic]);
		}
	}
	int i;
  for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
  lcd_writestring(dev_fd,tmpstr);
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      if (currnic>0)
	      {
	      	currnic--;
          if (strlen(sys_nic_name[currnic])==4) {
		    if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
				sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
		    else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      if (strlen(sys_nic_name[currnic])==5) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
			else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
          lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
	    case -24:
	      LCD_key=0;
	      if (currnic<(sys_nic_count-1))
	      {
	      	currnic++;
          if (strlen(sys_nic_name[currnic])==4) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
			else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      if (strlen(sys_nic_name[currnic])==5) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
					sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
		    else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	        for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
          lcd_writestring(dev_fd,tmpstr);                                                                
	      }
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		sprintf(tmpstr,"%s",menu11);
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 0.2");
  }	
}*/

int Nic_IP_Address()//顯示各網卡IP地址
{
	int currnic=0;
	char tmpstr[39];
	get_nic_IP_Address();
	printf("print ip address flow\n");
	if (strlen(sys_nic_name[currnic])==3) {
		//sprintf(tmpstr,"NIC(%d/%d)    %s%s/%s",currnic+1,sys_nic_count,
	    //    sys_nic_name[currnic],sys_nic_ip[currnic],sys_nic_mask[currnic]);
		   if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
				sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
		   }	
	       else{
	       sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
		   sys_nic_name[currnic],sys_nic_mask[currnic]);
	      }	 
	}
	if (strlen(sys_nic_name[currnic])==4) {
		if(strstr(sys_nic_mask[currnic],"Not")==NULL){
			if(strlen(sys_nic_mask[currnic])==1){
			sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
			sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
			}
			else{
			sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
			sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
			}
		}
		else{
		sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
		sys_nic_name[currnic],sys_nic_mask[currnic]);
		}
	}
	int i;
  	for (i=strlen(tmpstr);i<32;i++) 
		{strcat(tmpstr," ");}
  	lcd_writestring(dev_fd,tmpstr);
  	while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      if (currnic>0)
	      {
	      	currnic--;
          if (strlen(sys_nic_name[currnic])==3) {
		    if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
				sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
		    else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      if (strlen(sys_nic_name[currnic])==4) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
			else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
          lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
	    case -24:
	      LCD_key=0;
	      if (currnic<(sys_nic_count-1))
	      {
	      	currnic++;
          if (strlen(sys_nic_name[currnic])==3) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
				sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
				sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
			else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	      if (strlen(sys_nic_name[currnic])==4) {
			if(strstr(sys_nic_mask[currnic],"Not")==NULL){
				if(strlen(sys_nic_mask[currnic])==1){
					sprintf(tmpstr,"%d/%d   %s   /%s  %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);	
				}
				else{
					sprintf(tmpstr,"%d/%d   %s   /%s %s",currnic+1,sys_nic_count,
					sys_nic_name[currnic],sys_nic_mask[currnic],sys_nic_ip[currnic]);
				}
			}
		    else{
	        sprintf(tmpstr,"%d/%d   %s          %s",currnic+1,sys_nic_count,
	        sys_nic_name[currnic],sys_nic_mask[currnic]);
	        }
	      }
	        for (i=strlen(tmpstr);i<32;i++) {strcat(tmpstr," ");}
          lcd_writestring(dev_fd,tmpstr);                                                                
	      }
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		sprintf(tmpstr,"%s",menu11);
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 0.2");
  }	
}


/*int get_gateway()//獲取當前網關
{
	FILE *file;
	int i;
	file = fopen(dir_route, "r");
	if(file == NULL)
	{
		printf("no file\n");
		return -1;
	}
	char tmpstr[500]="";
	char tmpdes[20]="";
	char tmpgate[20]="";
	int tmpint=0;
	while (fgets(tmpstr,500,file) != NULL)
	{
		
		sscanf(tmpstr,"%*s%s%s%*s%*s%*s%*s%*s%*s%*s%*s",tmpdes,tmpgate);
		//printf("-------------------------\n");
		//printf("tmpdes:%s,tmpgate:%s\n",tmpdes,tmpgate);
		if (strstr(tmpdes,"00000000")!=NULL) 
		{
			int long tmpint1,tmpint2,tmpint3,tmpint4;
			char *tmpc;
			char tmpc1[5]="";
			tmpc=tmpgate;
			strncpy(tmpc1,tmpc,2);
			tmpint4=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmpint3=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmpint2=strtol(tmpc1,NULL,16);
			tmpc+=2;
			strncpy(tmpc1,tmpc,2);
			tmpint1=strtol(tmpc1,NULL,16);
			sprintf(sys_gateway,"Default Gateway %d.%d.%d.%d ",tmpint1,tmpint2,tmpint3,tmpint4);
			tmpint=1;
			break;
		}
	}
	if (tmpint==0) {sprintf(sys_gateway,"Default Gateway   Not defined   ");}
	fclose(file);
}*/

int get_gateway()//獲取當前網關
{
	FILE *stream;
	//char *cmd="floweye if stat";
	char *cmd="netstat -nr";
	stream = popen(cmd, "r" );

	char tmpstr[500]="";
	char tmpdes[20]="";
	char tmpgate[22]="";
	int tmpint=0;
	//while (fgets(tmpstr,500,file) != NULL)
	while (fgets(tmpstr,500,stream) != NULL)
	{
		
		//sscanf(tmpstr,"%*s%s%s%*s%*s%*s%*s%*s%*s%*s%*s",tmpdes,tmpgate);
		sscanf(tmpstr,"%s%s%*s%*s%*s",tmpdes,tmpgate);
		//printf("-------------------------\n");
		//printf("tmpdes:%s,tmpgate:%s\n",tmpdes,tmpgate);
		printf("the gateway is %s\n",tmpgate);
		if ((strstr(tmpdes,"default")!=NULL)  ) 
		{
			int i,length1,length2,length3,length4;
			char *Ppoint1,*Ppoint2,*Ppoint3,*Ppoint4;
			char tmpgate1[20]="",tmpgate2[20]="",tmpgate3[20]="",tmpgate4[20]="";
			Ppoint1=strstr(tmpgate,".");
			Ppoint1++;
			//stpcpy(tmpgate1,Ppoint1);
			printf("the gateway1 is %s\n",Ppoint1);
			Ppoint2=strstr(Ppoint1,".");
			Ppoint2++;
			printf("the gateway2 is %s\n",Ppoint2);
			Ppoint3=strstr(Ppoint2,".");
			Ppoint3++;
			printf("the gateway3 is %s\n",Ppoint3);

			

			length1=strlen(tmpgate)-strlen(Ppoint1);
			length2=strlen(Ppoint1)-strlen(Ppoint2);
			length3=strlen(Ppoint2)-strlen(Ppoint3);

			printf("the length indi is %d\t%d\t%d\n",length1,length2,length3);
			
			int long tmpint1,tmpint2,tmpint3,tmpint4;
			char *tmpc;
			char tmpc1[5]="";
			tmpc=tmpgate;
			strncpy(tmpc1,tmpc,length1-1);
			printf("the tmpc1 is %s\n",tmpc1);
			tmpint4=strtol(tmpc1,NULL,10);

			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length1;
			strncpy(tmpc1,tmpc,length2-1);
			printf("the tmpc1 is %s\n",tmpc1);
			tmpint3=strtol(tmpc1,NULL,10);

			
			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length2;
			strncpy(tmpc1,tmpc,length3-1);
			strncpy(tmpc1,tmpc,1);
			printf("the tmpc1 is %s\n",tmpc1);
			tmpint2=strtol(tmpc1,NULL,10);

			
			for(i=0;i<5;i++)
				tmpc1[i]='\0';
			tmpc+=length3;
			strncpy(tmpc1,tmpc,4);
			printf("the tmpc1 is %s\n",tmpc1);
			tmpint1=strtol(tmpc1,NULL,10);
			
			sprintf(sys_gateway,"Default Gateway %d.%d.%d.%d ",tmpint4,tmpint3,tmpint2,tmpint1);
			tmpint=1;
			printf("the sys_gateway is %s\n",sys_gateway);
			break;
		}
	}
	if (tmpint==0) {sprintf(sys_gateway,"Default Gateway   Not defined   ");}
	fclose(stream);
}


int Gateway()//顯示默認網關
{
	int i;
	char *tmpstr;
	get_gateway();
  	tmpstr=sys_gateway;
  	int tmpintint;
  	tmpintint=strlen(tmpstr);
  	for (i=tmpintint;i<32;i++) {strcat(tmpstr," ");}
  	lcd_writestring(dev_fd,tmpstr);
  	while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      break;
	    case -24:
	      LCD_key=0;
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu12;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 1");
  }	
}

int get_users()//獲取當前用戶數
{
	FILE *file;
	file = fopen(dir_users, "r");
	if(file == NULL)
	{
		sprintf(sys_users,"     Users       Not Definition ");
		return -1;
	}
	char tmpstr[100];
	int tmpint=0;
	while (fgets(tmpstr,100,file) != NULL)
	{
		tmpint++;
	}
	sprintf(sys_users,"     Users      ");
	sprintf(tmpstr,"%d",tmpint);
	int i;
	for (i=0;i<(16-strlen(tmpstr));i++) {strcat(sys_users," ");}
	strcat(sys_users,tmpstr); 
	fclose(file);
}

int Users()//顯示當前用戶數
{
	int i;
	char *tmpstr;
  while (1) 
	{
		get_users();
    tmpstr=sys_users;
    for (i=0;i<(33-strlen(tmpstr));i++) {strcat(tmpstr," ");}
    lcd_writestring(dev_fd,tmpstr);
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      break;
	    case -24:
	      LCD_key=0;
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu13;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 1");
  }	
}

/*int get_connections()//獲取當前連接數
{
	FILE *file;
	file = fopen(dir_connections, "r");
	if(file == NULL)
	{
		sprintf(sys_connections,"  Connections      Not Found ");
		return -1;
	}
	char tmpstr[20];
	char tmpstr1[20]="";
	while (fgets(tmpstr,100,file) != NULL) {}
	int i;
	strncpy(tmpstr1,tmpstr,strlen(tmpstr)-1);
	sprintf(sys_connections,"  Connections   ");
	for (i=0;i<(16-strlen(tmpstr1));i++) {strcat(sys_connections," ");}
	strcat(sys_connections,tmpstr1);
	fclose(file);
}*/

int get_connections()//獲取當前連接數
{
	FILE *stream;
	char *cmd="who";
	stream = popen(cmd, "r" );
	char tmpstr[20];
	char tmpstr0[500];
	char tmpstr1[20]="";
	int tmpint=0;
	printf("hello\n");
	while (fgets(tmpstr0,500,stream) != NULL)
	{	
		tmpint++;
		printf("..........");
	}
	sprintf(tmpstr,"%d",tmpint);
	printf("the connections are %s\n",tmpstr);
	
	printf("the connections are %d\n",tmpint);
	int i;
	strcpy(tmpstr1,tmpstr);
	printf("the tmpstrl is %s\n",tmpstr1);
	sprintf(sys_connections,"  Connections   ");
	for (i=0;i<(16-strlen(tmpstr1));i++) {strcat(sys_connections," ");}
	strcat(sys_connections,tmpstr1);
	printf("that's right!\n");
	fclose(stream);
	return 0;
	
}



int Connections()//顯示當前連接數
{
	int i;
	char *tmpstr;
  while (1) 
	{
	get_connections();
    tmpstr=sys_connections;
    for (i=0;i<(32-strlen(tmpstr));i++) {strcat(tmpstr," ");}
    lcd_writestring(dev_fd,tmpstr);
    
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      break;
	    case -24:
	      LCD_key=0;
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu14;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
    system("sleep 1");
  }	
}

/*int get_performance()//獲取當前CPU、內存佔用情況
{
	float  cpu1[10],cpu2[10];
	int cpucount,i,tmpint;
	float cpuper,memper;
	float memtotal,memfree;
	char tmpstr[200],tmpstr1[200],tmpstr2[200];
	FILE *file;
	file = fopen(dir_cpu, "r");
	if(file == NULL)
	{
		sprintf(sys_performance,"CPU:    Not File");
		return -1;
	}
	cpucount=0;
	while (fgets(tmpstr,200,file) != NULL) 
	{
		if (strstr(tmpstr,"cpu")!=NULL) {cpucount++;}
	}
	fseek(file,0,SEEK_SET);
	if (fgets(tmpstr,200,file) != NULL) 
	{
		strcpy(tmpstr1,tmpstr+5);
		sscanf(tmpstr1,"%f%f%f%f%f%f%f%f%f",&cpu1[1],&cpu1[2],&cpu1[3],&cpu1[4],&cpu1[5],&cpu1[6],&cpu1[7],&cpu1[8],&cpu1[9]);
	}
	fclose(file);
	system("sleep 1");
	file = fopen(dir_cpu, "r");
	if (fgets(tmpstr,200,file) != NULL) 
	{
		char *tmpstr1=tmpstr+5;
		sscanf(tmpstr1,"%f%f%f%f%f%f%f%f%f",&cpu2[1],&cpu2[2],&cpu2[3],&cpu2[4],&cpu2[5],&cpu2[6],&cpu2[7],&cpu2[8],&cpu2[9]);
	}
	fclose(file);
	cpu1[0]=0;cpu2[0]=0;
	for (i=1;i<10;i++)
	{
		cpu1[0]+=cpu1[i];
		cpu2[0]+=cpu2[i];
	}
	file = fopen(dir_mem, "r");
	if(file == NULL)
	{
		sprintf(sys_performance,"MEM:    Not File");
		return -1;
	}
	char tmpchar1[10],tmpchar2[10];
	while (fgets(tmpstr,200,file) != NULL) 
	{
		if (strstr(tmpstr,"MemTotal:")!=NULL)
			{
				sscanf(tmpstr,"%*s%f%s",&memtotal,tmpchar1);
			}
		if (strstr(tmpstr,"MemFree:")!=NULL)
			{
				sscanf(tmpstr,"%*s%f%s",&memfree,tmpchar2);
			}
	}
	fclose(file);
	if (strstr(tmpchar1,"k")!=NULL) {memtotal*=1024;}
	if (strstr(tmpchar1,"m")!=NULL) {memtotal*=1048576;}
	if (strstr(tmpchar2,"k")!=NULL) {memfree*=1024;}
	if (strstr(tmpchar2,"m")!=NULL) {memfree*=1048576;}
	float tmpfloat1;
	if (memtotal > 1073741824) {
		tmpfloat1=memtotal/1073741824;
		strcpy(tmpstr,"GB");
	} else if (memtotal > 1048576) {
		       tmpfloat1=memtotal/1048576;
		       strcpy(tmpstr,"MB");
	       } else if (memtotal > 1024) {
		              tmpfloat1=memtotal/1024;
		              strcpy(tmpstr,"KB");
	              }
  cpuper=100*((cpu2[0]-cpu1[0])-(cpu2[4]-cpu1[4]))/(cpu2[0]-cpu1[0]);	
  memper=100*(memtotal-memfree)/memtotal;
  sprintf(sys_performance,"CPU:%3.1f%%",cpuper);
  for (i=strlen(sys_performance);i<10;i++) {strcat(sys_performance," ");}
  sprintf(tmpstr1,"%2dcore",cpucount-1);
  strcat(sys_performance,tmpstr1);
  sprintf(tmpstr2,"MEM:%3.1f%%",memper);
  strcat(sys_performance,tmpstr2);
  sprintf(tmpstr1,"%3.1f%s",tmpfloat1,tmpstr);
  int tmptmpint=strlen(sys_performance);
  
  for (i=tmptmpint;i<(32-strlen(tmpstr1));i++) {strcat(sys_performance," ");}
  strcat(sys_performance,tmpstr1);
}*/


/*int get_performance()//獲取當前CPU、內存佔用情況
{
	float  cpu1[10],cpu2[10];
	int cpucount,i,tmpint;
	float cpuper,memper;
	float memtotal,memfree;
	char tmpstr[200],tmpstr1[200],tmpstr2[200];
	FILE *file;
	file = fopen(dir_cpu, "r");
	if(file == NULL)
	{
		sprintf(sys_performance,"CPU:    Not File");
		return -1;
	}
	cpucount=0;
	while (fgets(tmpstr,200,file) != NULL) 
	{
		if (strstr(tmpstr,"cpu")!=NULL) {cpucount++;}
	}
	fseek(file,0,SEEK_SET);
	if (fgets(tmpstr,200,file) != NULL) 
	{
		strcpy(tmpstr1,tmpstr+5);
		sscanf(tmpstr1,"%f%f%f%f%f%f%f%f%f",&cpu1[1],&cpu1[2],&cpu1[3],&cpu1[4],&cpu1[5],&cpu1[6],&cpu1[7],&cpu1[8],&cpu1[9]);
	}
	fclose(file);
	system("sleep 1");
	file = fopen(dir_cpu, "r");
	if (fgets(tmpstr,200,file) != NULL) 
	{
		char *tmpstr1=tmpstr+5;
		sscanf(tmpstr1,"%f%f%f%f%f%f%f%f%f",&cpu2[1],&cpu2[2],&cpu2[3],&cpu2[4],&cpu2[5],&cpu2[6],&cpu2[7],&cpu2[8],&cpu2[9]);
	}
	fclose(file);
	cpu1[0]=0;cpu2[0]=0;
	for (i=1;i<10;i++)
	{
		cpu1[0]+=cpu1[i];
		cpu2[0]+=cpu2[i];
	}
	file = fopen(dir_mem, "r");
	if(file == NULL)
	{
		sprintf(sys_performance,"MEM:    Not File");
		return -1;
	}
	char tmpchar1[10],tmpchar2[10];
	while (fgets(tmpstr,200,file) != NULL) 
	{
		if (strstr(tmpstr,"MemTotal:")!=NULL)
			{
				sscanf(tmpstr,"%*s%f%s",&memtotal,tmpchar1);
			}
		if (strstr(tmpstr,"MemFree:")!=NULL)
			{
				sscanf(tmpstr,"%*s%f%s",&memfree,tmpchar2);
			}
	}
	fclose(file);
	if (strstr(tmpchar1,"k")!=NULL) {memtotal*=1024;}
	if (strstr(tmpchar1,"m")!=NULL) {memtotal*=1048576;}
	if (strstr(tmpchar2,"k")!=NULL) {memfree*=1024;}
	if (strstr(tmpchar2,"m")!=NULL) {memfree*=1048576;}
	float tmpfloat1;
	if (memtotal > 1073741824) {
		tmpfloat1=memtotal/1073741824;
		strcpy(tmpstr,"GB");
	} else if (memtotal > 1048576) {
		       tmpfloat1=memtotal/1048576;
		       strcpy(tmpstr,"MB");
	       } else if (memtotal > 1024) {
		              tmpfloat1=memtotal/1024;
		              strcpy(tmpstr,"KB");
	              }
  cpuper=100*((cpu2[0]-cpu1[0])-(cpu2[4]-cpu1[4]))/(cpu2[0]-cpu1[0]);	
  memper=100*(memtotal-memfree)/memtotal;
  sprintf(sys_performance,"CPU:%3.1f%%",cpuper);
  for (i=strlen(sys_performance);i<10;i++) {strcat(sys_performance," ");}
  sprintf(tmpstr1,"%2dcore",cpucount-1);
  strcat(sys_performance,tmpstr1);
  sprintf(tmpstr2,"MEM:%3.1f%%",memper);
  strcat(sys_performance,tmpstr2);
  sprintf(tmpstr1,"%3.1f%s",tmpfloat1,tmpstr);
  int tmptmpint=strlen(sys_performance);
  
  for (i=tmptmpint;i<(32-strlen(tmpstr1));i++) {strcat(sys_performance," ");}
  strcat(sys_performance,tmpstr1);
}*/

int get_performance()//獲取當前CPU、內存佔用情況
{

		FILE *stream;
		char *cmd="vmstat";
		stream = popen(cmd, "r" );
		char tmpstr[20];
		char tmpstr0[600];
		char tmpstr1[20]="";
		char tmpstr2[200];
		int tmpint=0;
		float cpuper,memper,tmpfloat1;
		char *Ppoint1;
		while (fgets(tmpstr0,600,stream) != NULL){	
			tmpint++;
			if(3 == tmpint){
				sscanf(tmpstr0,"%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%f",&cpuper);
				break;
			}	
		}
		fclose(stream);

		cmd="dmesg -a |grep memory";
		stream = popen(cmd, "r" );
		char *Ppint1;
		unsigned long memtotal,memfree;
		float Memory1,Memory2,Memoryfree;
		int MemoryAvali;
		char tmpstrMemory[2];
		tmpint=0;
		while (fgets(tmpstr0,600,stream) != NULL)
		{	
			tmpint++;
			printf("ok0\n");
			if (1==tmpint)
			{
			printf("ok1\n");
			printf("the memtotal is %s\n",tmpstr0);
				Ppint1=strstr(tmpstr0,"(");
				Ppint1++;
				sscanf(Ppint1,"%d%*s",&memtotal);
				//all is depend on kb because the memory is not base on byte any more and avoid the big mumber
				if (strstr(Ppint1,"M")!=NULL) {
					if(memtotal >1024){
						tmpfloat1=memtotal/1024.0;
						strcpy(tmpstrMemory,"GB");
					}
					else {tmpfloat1=(float)memtotal; strcpy(tmpstrMemory,"MB");}
					memtotal*=1024;
				}
				//if (strstr(Ppint1,"M")!=NULL) {memtotal*=1048576;}
				
				printf("the memtotal is %ld\n",memtotal);
			}
			if (2==tmpint)
			{
				Ppint1=strstr(tmpstr0,"(");
				Ppint1++;
				sscanf(Ppint1,"%d",&memfree);
				if (strstr(Ppint1,"M")!=NULL) {
					memfree*=1024;
				}
				break;
			}
			printf("ok2\n");
		}
		fclose(stream);

  		memper=100*(memtotal-memfree)/memtotal;
		printf("the memper is %3.1f%%\n",memper);
		
		cmd="sysctl kern.smp.cpus";
		stream = popen(cmd, "r" );
		char cpuCnt[3]="";
		int cupCentInt=0;
		tmpint=0;
		if(fgets(tmpstr0,600,stream) != NULL)
		{	printf("the cpumessege is %s\n",tmpstr0);
			
			Ppint1=strstr(tmpstr0,":");
			printf("the cpumessege is %s\n",Ppint1);
			Ppint1++;
			strncpy(cpuCnt,Ppint1,3);
			cupCentInt=atoi(cpuCnt);
			printf("the cpuint is %d\n",cupCentInt);
			
		}
		else{
			fclose(stream);
			return 0;
			}
		fclose(stream);
	    sprintf(sys_performance,"CPU:%3.1f%%",100-cpuper);
	    int i;
	    for (i=strlen(sys_performance);i<10;i++) {strcat(sys_performance," ");}
	    sprintf(tmpstr1,"%2dcore",cupCentInt);
	    strcat(sys_performance,tmpstr1);
	    sprintf(tmpstr2,"MEM:%3.1f%%",memper);
	    strcat(sys_performance,tmpstr2);
	    sprintf(tmpstr1,"%3.1f%s",tmpfloat1,tmpstrMemory);
	    int tmptmpint=strlen(sys_performance);
	    for (i=tmptmpint;i<(32-strlen(tmpstr1));i++) {strcat(sys_performance," ");}
	    strcat(sys_performance,tmpstr1);
	    printf("end\n");
	    printf("%s\n",sys_performance);
	    return 0;
  
}

int Performance()//顯示當前CPU、內存佔用情況
{
	int i;
	char *tmpstr;
  while (1) 
	{
		get_performance();
    tmpstr=sys_performance;
    lcd_writestring(dev_fd,tmpstr);
		
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      break;
	    case -24:
	      LCD_key=0;
	      break;
    	case -40:
	      LCD_key=0;
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu15;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
    }
	system("sleep 1");
  }	
}

int Main_menu(int para)//主菜單,0
{
	int ret;
	char *tmpstr;
	int mypara=para;
	if (mypara==1) tmpstr=menu1;
	if (mypara==2) tmpstr=menu2;
	lcd_writestring(dev_fd,tmpstr);
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
    		LCD_key=0;
	      if (mypara==2) 
	      {
	      	mypara=1;
	      	tmpstr=menu1;
	      	lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
	    case -24:
	    	LCD_key=0;
	      if (mypara==1) 
	      {
	      	mypara=2;
	      	tmpstr=menu2;
	      	lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
    	case -40:
    		LCD_key=0;
	      if (mypara==1) Show_information(11);
	      if (mypara==2) Set_parameters(21);
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu0;
    		lcd_writestring(dev_fd,tmpstr);
    		return 0;
     }
     system("sleep 0.2");
  }	
}

int Show_information(int para)//顯示信息,1
{
	char *tmpstr;
	int mypara=para;
	switch (mypara)
	{
		case 11:tmpstr=menu11;break;
		case 12:tmpstr=menu12;break;
		case 13:tmpstr=menu13;break;
		case 14:tmpstr=menu14;break;
		case 15:tmpstr=menu15;break;
  }
  lcd_writestring(dev_fd,tmpstr);
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      if (mypara>11) 
	      {
	      	mypara--;
	        switch (mypara)
	        {
	        	case 11:tmpstr=menu11;break;
	        	case 12:tmpstr=menu12;break;
	        	case 13:tmpstr=menu13;break;
	        	case 14:tmpstr=menu14;break;
	        	case 15:tmpstr=menu15;break;
	        		
          }
          lcd_writestring(dev_fd,tmpstr);
        }
	      break;
	    case -24:
	      LCD_key=0;
	      if (mypara<15) 
	      { 
	      	mypara++;
	        switch (mypara)
	        {
	        	case 11:tmpstr=menu11;break;
	        	case 12:tmpstr=menu12;break;
	        	case 13:tmpstr=menu13;break;
	        	case 14:tmpstr=menu14;break;
	        	case 15:tmpstr=menu15;break;
          }
          lcd_writestring(dev_fd,tmpstr);
        }
	      break;
    	case -40:
	      LCD_key=0;
	      switch (mypara)
	      {
	      	
	      	//case 11:Nic_IP_Address(121);break;
	      	//why up's agruments is 121?(so the flow is myself)
	      	case 11:Nic_IP_Address();break;
	      	case 12:Gateway();break;
	      	case 13:Users();break;
	      	case 14:Connections();break;
	      	case 15:Performance();break;	
        }
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu1;
    		lcd_writestring(dev_fd,tmpstr);
    		return 1;
    }
    system("sleep 0.2");
  }
}
int Set_parameters(int para)//設置參數,2
{
	char *tmpstr;
	int mypara=para;
	if (mypara==21) tmpstr=menu21;
	if (mypara==22) tmpstr=menu22;
	lcd_writestring(dev_fd,tmpstr);
  while (1) 
	{
    switch (LCD_key)
    {
    	case -72:
	      LCD_key=0;
	      if (mypara==22) 
	      {
	      	mypara=21;
	      	tmpstr=menu21;
	      	lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
	    case -24:
	      LCD_key=0;
	      if (mypara==21) 
	      {
	      	mypara=22;
	      	tmpstr=menu22;
	      	lcd_writestring(dev_fd,tmpstr);
	      }
	      break;
    	case -40:
	      LCD_key=0;
	      if (mypara==21) Set_default_gateway(211);
	      if (mypara==22) Set_NIC_IP_address(221); 
	      break;
    	case 0x78:
    		LCD_key=0;
    		tmpstr=menu2;
    		lcd_writestring(dev_fd,tmpstr);
    		return 2;
     }
     system("sleep 0.2");
  }	
}

int counttime() //計算上一次按鍵到現在之間的時間差,返回秒
{
	struct timeval currtime;
  float timeuse;
  int i;
  gettimeofday(&currtime,NULL);
  timeuse=currtime.tv_sec-last_operation_time.tv_sec;
  return(timeuse);
}
int lcd_clear(int fd)
{
	int ret;
	char buff_write_clear[3]="";
	buff_write_clear[0]=0xf8;
	buff_write_clear[1]=0x01;
	ret=write(fd,buff_write_clear, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_reset(int fd)
{
	int ret;
	char buff_write_reset[3]="";
	buff_write_reset[0]=0xf8;
	buff_write_reset[1]=0x02;
	ret=write(fd,buff_write_reset, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_direction_left(int fd)
{
	int ret;
	char buff_write_direction_left[3]="";
	buff_write_direction_left[0]=0xf8;
	buff_write_direction_left[1]=0x05;
	ret=write(fd,buff_write_direction_left, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_direction_right(int fd)
{
	int ret;
	char buff_write_direction_right[3]="";
	buff_write_direction_right[0]=0xf8;
	buff_write_direction_right[1]=0x06;
	ret=write(fd,buff_write_direction_right, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_hide_cursor(int fd)
{
	int ret;
	char buff_write_hide_cursor[3]="";
	buff_write_hide_cursor[0]=0xf8;
	buff_write_hide_cursor[1]=0x0c;
	ret=write(fd,buff_write_hide_cursor, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_show_cursor(int fd)
{
	int ret;
	char buff_write_show_cursor[3]="";
	buff_write_show_cursor[0]=0xf8;
	buff_write_show_cursor[1]=0x0f;
	ret=write(fd,buff_write_show_cursor, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_move_cursor_left_one(int fd)
{
	int ret;
	char buff_write_move_cursor_left_one[2];
	buff_write_move_cursor_left_one[0]=0xf8;
	buff_write_move_cursor_left_one[1]=0x10;
	ret=write(fd,buff_write_move_cursor_left_one, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_move_cursor_right_one(int fd)
{
	int ret;
	char buff_write_move_cursor_right_one[3]="";
	buff_write_move_cursor_right_one[0]=0xf8;
	buff_write_move_cursor_right_one[1]=0x14;
	ret=write(fd,buff_write_move_cursor_right_one, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_move_lcd_left_one(int fd)
{
	int ret;
	char buff_write_move_lcd_left_one[3]="";
	buff_write_move_lcd_left_one[0]=0xf8;
	buff_write_move_lcd_left_one[1]=0x18;
	ret=write(fd,buff_write_move_lcd_left_one, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_move_lcd_right_one(int fd)
{
	int ret;
	char buff_write_move_lcd_right_one[2];
	buff_write_move_lcd_right_one[0]=0xf8;
	buff_write_move_lcd_right_one[1]=0x1c;
	ret=write(fd,buff_write_move_lcd_right_one, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_cursor_location1(int fd)
{
	int ret;
	char buff_write_cursor_location1[4]="";
	buff_write_cursor_location1[0]=0xf8;
	buff_write_cursor_location1[1]=0x80;
	buff_write_cursor_location1[2]=0x00;
	ret=write(fd,buff_write_cursor_location1, 3);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_cursor_location2(int fd)
{
	int ret;
	char buff_write_cursor_location2[4]="";
	buff_write_cursor_location2[0]=0xf8;
	buff_write_cursor_location2[1]=0x80;
	buff_write_cursor_location2[2]=0x40;
	ret=write(fd,buff_write_cursor_location2, 3);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_light(int fd)
{
	int ret;
  char buff_write_lcd_light[3]="";
	buff_write_lcd_light[0]=0xf8;
	buff_write_lcd_light[1]=0x28;
	ret=write(fd,buff_write_lcd_light, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
int lcd_dark(int fd)
{
	int ret;
  char buff_write_lcd_dark[3]="";
	buff_write_lcd_dark[0]=0xf8;
	buff_write_lcd_dark[1]=0x2c;
	ret=write(fd,buff_write_lcd_dark, 2);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}
/*int lcd_init()
{
	int ret;
  char buff_write[10]="";
	dev_fd = OpenDev(dev_name);
	if (dev_fd>0)
		set_speed(dev_fd,9600);
	if (dev_fd==-1)
	{
		printf("Can't Open Serial Port!\n");
		exit(0);
	}
	if (set_Parity(dev_fd,8,1,'N') == 0)
	{
		printf("Set Parity Error\n");
		exit(1);
	}
	buff_write[0] = 0xf8;
	buff_write[1] = 0x01;
	buff_write[2] = 0xf8;
	buff_write[3] = 0x0c;
	buff_write[4] = 0xf8;
	buff_write[5] = 0x28;
	buff_write[6] = 0xf8;
	buff_write[7] = 0x06;
	buff_write[8] = 0xf8;
	buff_write[9] = 0x02;
	ret=write(dev_fd,buff_write, 10); //是否加上 ret=
	gettimeofday(&last_operation_time,NULL);
	if (-1 == ret)
	{
		perror("Write data error!");
		return -1;
	}
	else
		return ret;
}*/

void    set_raw_io( int fd )        //set input and output in raw mode
{
    struct termios options;
    
    if  ( tcgetattr( fd,&options)  !=  0)
    {
        perror("SetupSerial 4");
        return;
    }
    //  options.c_cflag &=~HUPCL;
    options.c_iflag &=~(IXON|IXOFF|IXANY|ICRNL);
    //  options.c_iflag |=IGNCR;
    options.c_lflag=0;
    options.c_lflag  &= ~(ICANON | ECHO | ECHOE | ISIG);  /*Input*/
    options.c_oflag  &= ~OPOST;   /*Output*/
    
    tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
    if (tcsetattr(fd,TCSANOW,&options) != 0)
    {
        perror("SetupSerial 5");
        return;
    }
    
}


int lcd_init()
{
	int ret;
  	char buff_write[10]="";

	 buff_write_clear[0]=0xf8;
    buff_write_clear[1]=0x01;
    
    buff_write_reset[0]=0xf8;
    buff_write_reset[1]=0x02;
    
    buff_write_direction_left[0]=0xf8;
    buff_write_direction_left[1]=0x05;
    
    buff_write_direction_right[0]=0xf8;
    buff_write_direction_right[1]=0x06;
    
    buff_write_hide_cursor[0]=0xf8;
    buff_write_hide_cursor[1]=0x0c;
    
    buff_write_show_cursor[0]=0xf8;
    buff_write_show_cursor[1]=0x0f;
    
    
    buff_write_move_cursor_left_one[0]=0xf8;
    buff_write_move_cursor_left_one[1]=0x10;
    
    buff_write_move_cursor_right_one[0]=0xf8;
    buff_write_move_cursor_right_one[1]=0x14;
    
    buff_write_move_lcd_left_one[0]=0xf8;
    buff_write_move_lcd_left_one[1]=0x18;
    
    buff_write_move_lcd_right_one[0]=0xf8;
    buff_write_move_lcd_right_one[1]=0x1c;
    
    buff_write_cursor_location1[0]=0xf8;
    buff_write_cursor_location1[1]=0x80;
    buff_write_cursor_location1[2]=0x00;
    
    buff_write_cursor_location2[0]=0xf8;
    buff_write_cursor_location2[1]=0x80;
    buff_write_cursor_location2[2]=0x40;
    
    
    
    buff_write_lcd_light[0]=0xf8;
    buff_write_lcd_light[1]=0x28;
    
    buff_write_lcd_dark[0]=0xf8;
    buff_write_lcd_dark[1]=0x2c;
  
	dev_fd = OpenDev(dev_name);
	if (dev_fd>0)
		set_speed(dev_fd,9600);
	if (dev_fd==-1)
	{
		printf("Can't Open Serial Port!\n");
		exit(0);
	}
	if (set_Parity(dev_fd,8,1,'N') == 0)
	{
		printf("Set Parity Error\n");
		exit(1);
	}


	set_raw_io( dev_fd);
	write(dev_fd,buff_write_clear, 2);
    write(dev_fd,buff_write_reset, 2);
    write(dev_fd,buff_write_move_lcd_left_one, 2);
	
	gettimeofday(&last_operation_time,NULL);
	
	return 0;

	/*memset(out, ' ', 20);
   	memset(in, ' ', 20);
        out[0]=0xF8;
        out[1]=0x03;
        out[3]='O';
        out[4]='U';
        out[5]='T';
        out[6]=':';
        in[0]=0xF8;
        in[1]=0x03;
        in[3]='I';
        in[4]='N';
        in[5]=':';
        out[19]=0xA0;
        in[19]=0xA0;
      
        write(dev_fd, buff_write_cursor_location1, 3);
        write(dev_fd, out, sizeof(out));
        write(dev_fd, buff_write_cursor_location2, 3);
        write(dev_fd, in, sizeof(in));

	close(dev_fd);
	printf("the lcd will print in and out\n");

	system("sleep 1");	
	while(1);*/
        
	
	
}


int lcd_writestring(int fd,char *stringbuff)
{
	int ret,i;
	int len=strlen(stringbuff);
  char buff_write[len+4];
  //lcd_clear(fd);
  memset(buff_write,0,len+4);
  lcd_reset(fd);
	buff_write[0]=0xf8;
	buff_write[1]=0x03;
	buff_write[len+2]=0xa0;
	for (i=0;i<len;i++)
	{
		buff_write[i+2]=stringbuff[i];
	}
	ret=write(fd,buff_write, len+3);
	if (-1 == ret)
	{
		perror("lcd_writestring Write data error!");
		return -1;
	}
	else
		return ret;
}


int set_Parity(int fd,int databits,int stopbits,int parity)
{
	struct termios options;
	if  ( tcgetattr( fd,&options)  !=  0)
	{
		perror("SetupSerial 1");
		return(0);
	}
	options.c_lflag &= ~(ECHO | ECHOE | ICANON | ISIG);
	
	options.c_cflag &= ~CSIZE;
	switch (databits) /*設置數據位數*/
	{
	case 7:
		options.c_cflag |= CS7;
		break;
	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		fprintf(stderr,"Unsupported data size\n");
		return (0);
	}
	switch (parity)
	{
	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;   /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* Enable parity checking */
		break;
	case 'o':
	case 'O':
		options.c_cflag |= (PARODD | PARENB);  /* 設置爲奇效驗*/
		options.c_iflag |= INPCK;             /* Disnable parity checking */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* Enable parity */
		options.c_cflag &= ~PARODD;   /* 轉換爲偶效驗*/
		options.c_iflag |= INPCK;       /* Disnable parity checking */
		break;
	case 'S':
	case 's':  /*as no parity*/
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported parity\n");
		return (0);
	}
	/* 設置停止位*/
	switch (stopbits)
	{
	case 1:
		options.c_cflag &= ~CSTOPB;
		break;
	case 2:
		options.c_cflag |= CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported stop bits\n");
		return (0);
	}
	/* Set input parity option */
	if (parity != 'n')
		options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 150; // 15 seconds
    options.c_cc[VMIN] = 0;
	
	tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
	if (tcsetattr(fd,TCSAFLUSH,&options) != 0)
	{
		perror("SetupSerial 3");
		return (0);
	}
	return (1);
}

void set_speed(int fd, int speed)
{
	int   i;
	int   status;
	struct termios   Opt;
	tcgetattr(fd, &Opt);
	for ( i= 0;  i < sizeof(speed_arr) / sizeof(int);  i++)
	{
		if  (speed == name_arr[i])
		{
			
			tcflush(fd, TCIOFLUSH);
			cfsetispeed(&Opt, speed_arr[i]);
			
			
			cfsetospeed(&Opt, speed_arr[i]);
			status = tcsetattr(fd, TCSAFLUSH, &Opt);
			if  (status != 0)
				perror("tcsetattr fd1");
			return;
		}
		tcflush(fd,TCIOFLUSH);
	}
}

int OpenDev(char *Dev)
{
	int	fd = open( Dev, O_RDWR|O_NOCTTY );         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
	{ /*設置數據位數*/
		perror("Can't Open Serial Port");
		return -1;
	}
	else
		return fd;
	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章