⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uialarm.c

📁 嵌入工linux开发的源码
💻 C
📖 第 1 页 / 共 3 页
字号:

//******************************************************************************
//
//        User include
//
//******************************************************************************
#include <sys/syscall.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
                    
#include "uiWnd.h"
#include "uiWM_Queue.h"
#include "Database.h"
#include "uiAlarm.h"

extern TGuiWindow *gpTopWindow;
extern TGuiMessageQueue* SysMsgQueue;
extern int gLanguage;

//******************************************************************************
//
//        Function Declaration
//
//******************************************************************************
//#define ALARM_DRV_DEBUG

// 存放闹铃时间的table
static AlarmNode *gpAlarmTab = NULL;
static AlarmNode *gpGarbabeAlarm = NULL;
static AlarmNode gCurrentAlarm;
//static hw_alarmHandler pFunEnqueue=NULL;
//static int bSetAlarm;


int gSetAlarm = 0;

//******************************************************************************
//
//        Function Declaration
//
//******************************************************************************
static int time_comp(sysTime t1, sysTime t2);
static void time_assign(sysTime *pTime, sysTime t);
static int time_isZero(sysTime t);
static int   hw_calendar_caladdday(unsigned short *input_day, unsigned short *input_month, unsigned short *input_year, unsigned short *input_week, int add_day);
AlarmNode *init_AlarmLinklist(void);
int  isAlarmTimeExist(AlarmNode* head, char *id);
void InsertAlarmLinklist(AlarmNode *newNode);
static void freeAlarmLinklist(AlarmNode* head);
int  getFirstAlarmTime(AlarmNode * pAlarm);
static int  freeFirstAlarmTime(void);
void stopSysAlarmer(void);
void timeOutsysAlarm(void);
void add2GabageList(void);
AlarmNode * change2NextNode(void);
void hw_rtcSetAlarm(void);
int AlarmOutofTime(void);

#ifdef _OLD_VER_
int  extractAlarmTime(AlarmNode *pAlarm);
#endif

//******************************************************************************
//     功能:比较大小时间大小
//        t1  ==  t2    : 0
//        t1  >   t2    : 1
//        t1  <   t2    : 2
//******************************************************************************
static int time_comp(sysTime t1, sysTime t2)
{
	if( t1.year > t2.year )
	{
		return  1;
	}
	else if( t1.year < t2.year )
	{
		return  2;
	}
	else if( t1.month > t2.month )
	{
		return  1;
	}
	else if( t1.month < t2.month )
	{
		return  2;
	}
	else if( t1.day > t2.day )
	{
		return  1;
	}
	else if( t1.day < t2.day )
	{
		return  2;
	}
	else if( t1.hour > t2.hour )
	{
		return  1;
	}
	else if( t1.hour < t2.hour )
	{
		return  2;
	}
	else if( t1.minute > t2.minute )
	{
		return  1;
	}
	else if( t1.minute < t2.minute )
	{
		return  2;
	}
	else if( t1.second > t2.second )
	{
		return  1;
	}
	else if( t1.second < t2.second )
	{
		return  2;
	}
	else   //时间完全相同
	{
		return  0 ;
	}
}
//********************************************************************************
//      设定时间值到pTime变数上
//********************************************************************************
static void time_assign(sysTime *pTime, sysTime t)
{
    pTime->year=t.year;
    pTime->month=t.month;
    pTime->day=t.day;
    pTime->hour=t.hour;
    pTime->minute=t.minute;
    pTime->second=t.second;
    pTime->week=t.week;
}
//********************************************************************************
//      判断时间是否为零
//      传回值: 0-时间不为零  1-时间为零
//********************************************************************************

static int time_isZero(sysTime t)
{
	if( t.year==0 && t.month==0 && t.day==0 && t.hour==0 && t.minute==0 && t.second==0)
		return 1;
	return 0;
}
//********************************************************************************
//      将目前日期加上add_day天
//      传回值: 0-失败  非0-weekcount
//********************************************************************************
static int hw_calendar_caladdday(unsigned short *input_day, unsigned short *input_month, unsigned short *input_year, unsigned short *input_week, int add_day)
{
	int day_num[12]={31,28,31,30,31,30,31,31,30,31,30,31};
	int temp_days;
	int days, new_day;
	int weekcount = 0;
	int i, setFeb;
	
	if ( (*input_month<=0) || (*input_month>12) || (*input_year<=0) )
		return(0);
	
	days=((*input_year)-1+((*input_year)-1)/4-((*input_year)-1)/100+((*input_year)-1)/400)%7;
	for (i=0;i<(*input_month)-1;i++)
		days+=day_num[i];
	if (((*input_year)%4 == 0 && (*input_year)%100 != 0) || (*input_year)%400 == 0)
	{
		if ((*input_month)>2)
			days++;
		day_num[1]=29;
		setFeb=1;
	}
	else
		setFeb=0;
	
	if ( (*input_day) > day_num[(*input_month)-1] )
		return(0);
	
	new_day=(*input_day)+add_day;
	while ( new_day > day_num[(*input_month)-1] )
	{
		new_day -= day_num[(*input_month)-1];
		(*input_month)++;
		if( (*input_month) > 12 )
		{
			(*input_month)=1;
			(*input_year)++;
			if (((*input_year)%4 == 0 && (*input_year)%100 != 0) || (*input_year)%400 == 0)
			{
				day_num[1]=29;
				setFeb=1;
			}
			else
			{
				day_num[1]=28;
				setFeb=0;
			}
		}
		else // month is still smaller than 12...
		{
			;
		}
	}
	// now we have new year, new month, and new day;
	temp_days=((*input_year)-1+((*input_year)-1)/4-((*input_year)-1)/100+((*input_year)-1)/400)%7;
	for (i=0;i<(*input_month)-1;i++)
		temp_days+=day_num[i];
	
	*input_week=(temp_days+new_day)%7;
	*input_day=new_day;
	
	return (weekcount);
}                    


//************************************************
//  初始化单向链结串列
//************************************************
AlarmNode *init_AlarmLinklist(void)
{            
	int i, hDB, count, hRet;        
	char alarmID[PID_SIZE], alarmMess[MSG_SIZE];
	sysTime alarmTime;
	struct SchDb    Schedule;       
	struct AlarmDb  AlarmDB;
                          
	gpAlarmTab=(AlarmNode *)sc_malloc(sizeof(AlarmNode));
	memset(gpAlarmTab, 0, (unsigned long)sizeof(AlarmNode));
	gpAlarmTab->next=NULL;
	
	//初始化Garbage Collection linklist
	gpGarbabeAlarm=(AlarmNode *)sc_malloc(sizeof(AlarmNode));
	gpGarbabeAlarm->next=NULL;
	         
#ifndef __WIN32__
	*(unsigned int *)(RTCCTL) &= 0xfffff0ff; // disable interrupt of alarm // by zhangxueping
#endif
                               
// by zhangxp
////////////////////////////////                  
	hDB=db_open("schdb",0,0);
	if(hDB>=0) 
	{
		hRet = db_getRecordNumber(hDB,&count);
		if(hRet==_DB_CORRECT)
		{
        	for(i=0; i<count; i++)
			{
				hRet = db_recRead(hDB, &Schedule);	
				if(hRet != _DB_CORRECT)
				{
					//db_close(hDB);
					break;
				}   

				if(Schedule.alarm_flag)
				{        
					alarmTime.year=Schedule.start_year;
					alarmTime.month=Schedule.start_month;
					alarmTime.day=Schedule.start_day;
					if((Schedule.start_hour*60+Schedule.start_minute)>(Schedule.alarm_hour*60+Schedule.alarm_minute))
					{
						alarmTime.hour=((Schedule.start_hour*60+Schedule.start_minute)-(Schedule.alarm_hour*60+Schedule.alarm_minute))/60;
						alarmTime.minute=((Schedule.start_hour*60+Schedule.start_minute)-(Schedule.alarm_hour*60+Schedule.alarm_minute))%60;
					}
					else
					{
						alarmTime.hour=Schedule.start_hour;
						alarmTime.minute=Schedule.start_minute;
					}

					alarmTime.second=30;
					sprintf(alarmID,"sche%08x%08x",Schedule.sch_id, i);
					memset(alarmMess,0,sizeof(alarmMess));
					memcpy(alarmMess,Schedule.sch_title,sizeof(Schedule.sch_title));
					//strcat(alarmMess,"It is time for your appointment!");					
					guiAddAlarm(&alarmTime, alarmID, alarmMess, Schedule.sch_period);
				}

				hRet = db_recNext(hDB);
				if((hRet != _DB_CORRECT) && (i!=count-1))
				{
					//db_close(hDB);
					break;
				}

			}
		}

		db_close(hDB);
		
	}
	
    
	hDB = db_open("AlarmDb",0,0);
	if(hDB>=0) 
	{
		hRet = db_getRecordNumber(hDB,&count);
		if(hRet==_DB_CORRECT)
		{
			for(i=0; i<count; i++)
			{
				hRet = db_recRead(hDB, &AlarmDB);	
				if(hRet != _DB_CORRECT)
				{
					//db_close(hDB);
					break;
				}   
				
				alarmTime.year=AlarmDB.start_year;
				alarmTime.month=AlarmDB.start_month;
				alarmTime.day=AlarmDB.start_day;
				alarmTime.hour=AlarmDB.start_hour;
				alarmTime.minute=AlarmDB.start_minute;
				
				alarmTime.second=30;
				sprintf(alarmID,"sche%08x%08x",AlarmDB.alarm_id, i);
				memset(alarmMess,0,sizeof(alarmMess));
				memcpy(alarmMess,AlarmDB.alarm_title,sizeof(AlarmDB.alarm_title));
				guiAddAlarm(&alarmTime, alarmID, alarmMess, AlarmDB.alarm_period);

				hRet = db_recNext(hDB);
				if((hRet != _DB_CORRECT) && (i!=count-1))
				{
					//db_close(hDB);
					break;
				}

			}
		}

		db_close(hDB);

	}

///////////////////////////////


	return gpAlarmTab;
}
//**********************************************************
//   检查串列中的 id 是否存在
//**********************************************************
int isAlarmTimeExist(AlarmNode* head, char *id)
{
	AlarmNode *pTmp,*pPreTmp;
	
	pPreTmp=head;
	pTmp=head->next;
	
	while(pTmp!=NULL)
	{
		//若program id相同的话表示资料已经存在
		if (strcmp(pTmp->pAlarmID,id)==0)
		{
			return 1 ;
		}
		
		pPreTmp=pTmp;    //保留前一个node
		pTmp=pTmp->next; //下移一个node
	}
	
	return 0 ;
}

//*****************************************************
//  插入 一个时间资料到linklist中
//  pAlarm   :指向Alarm型态的资料结构
//  传回值   :无
//*****************************************************
void InsertAlarmLinklist(AlarmNode *newNode)
{
	AlarmNode *temp,*pPretemp;
	
	pPretemp=gpAlarmTab;
	temp = gpAlarmTab->next;
	
	while(temp!=NULL)
	{
		if( temp->time.year > newNode->time.year )      //表示新增的值较小(年)
		{
			newNode->next=pPretemp->next;
			pPretemp->next=newNode;
			break;  //找到要加的位置,跳出回圈
		}
		else if( temp->time.year == newNode->time.year )//表示新增的值一样大(年)
		{
			if( temp->time.month > newNode->time.month )//(月)
			{
				newNode->next=pPretemp->next;
				pPretemp->next=newNode;
				break;  //找到要加的位置,跳出回圈
			}
			else if( temp->time.month == newNode->time.month )//(月)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -