Main Page | Data Structures | File List | Data Fields | Globals

alarmcheck.c

Go to the documentation of this file.
00001 /**
00002  * This file is part of alarmd
00003  *
00004  * Contact Person: David Weinehall <david.weinehall@nokia.com>
00005  *
00006  * Copyright (C) 2006 Nokia Corporation
00007  * alarmd and libalarm are free software; you can redistribute them
00008  * and/or modify them under the terms of the GNU Lesser General Public
00009  * License version 2.1 as published by the Free Software Foundation.
00010  *
00011  * alarmd and libalarm are distributed in the hope that they will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this software; if not, write to the Free
00018  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00019  * 02110-1301 USA
00020  */
00021 
00022 #include <cal.h>
00023 #include <time.h>
00024 #include <alarmd/alarm_data.h>
00025 
00026 typedef enum {
00027         ALARM_ERR = -1,                 /* The alarm was spurious
00028                                          * or something failed
00029                                          */
00030         ALARM_NORMAL = 0,               /* Alarm; bootup in normal mode */
00031         ALARM_ACTDEAD = 1,              /* Alarm; bootup in acting dead mode */
00032         ALARM_FUTURE = 2                /* Alarm reprogrammed; shutdown */
00033 } alarmretval;
00034 
00035 #define TIME_T_24H                      24 * 60 * 60    /* 24h in seconds */
00036 
00037 /*
00038  * Check the alarm status
00039  * @return ALARM_ERR If there are no pending alarms (and on error),
00040  *         ALARM_NORMAL to bootup in normal mode
00041  *         ALARM_ACTDEAD to bootup in acting dead mode
00042  *         ALARM_FUTURE the alarm was reprogrammed; shutdown
00043  */
00044 static int check_alarm_status(void)
00045 {
00046         struct cal *cal_data = NULL;
00047         time_t alarm_time = (time_t)-1;
00048         time_t now;
00049         int olderrno = errno;
00050         int status = ALARM_ERR;
00051         alarmaction action = ALARM_ACTION_INVALID;
00052 
00053         /* If time() fails, ignore the alarm */
00054         if ((now = time(NULL)) == -1) {
00055                 errno = olderrno;
00056                 goto EXIT;
00057         }
00058 
00059         /* Retrieve the alarm stored in CAL */
00060         if (cal_init(&cal_data) == 0) {
00061                 void *ptr = NULL;
00062                 unsigned long len;
00063 
00064                 if (cal_read_block(cal_data, "alarm", &ptr, &len,
00065                                    CAL_FLAG_USER) == 0) {
00066                         struct alarm_data *alarmdata = (struct alarm_data *)ptr;
00067 
00068                         alarm_time = alarm_data->alarm_time;
00069                         action = alarm_data->action;
00070                 } else {
00071                         goto EXIT;
00072                 }
00073 
00074                 cal_finish(cal_data);
00075         } else {
00076                 goto EXIT;
00077         }
00078 
00079         /* If alarm_time is == -1, then no alarm has been set */
00080         if (alarm_time == -1) {
00081                 status = ALARM_ERR;
00082                 goto EXIT;
00083         } else if ((alarm_time - now) > TIME_T_24H) {
00084                 status = ALARM_FUTURE;
00085 
00086         } else {
00087                 if (action == ALARM_ACTION_NORMAL)
00088                         status = ALARM_NORMAL;
00089                 else if (action == ALARM_ACTION_ACTDEAD)
00090                         status = ALARM_ACTDEAD;
00091                 else
00092                         status = ALARM_ERR;
00093         }
00094 
00095 EXIT:
00096         return status;
00097 }

Generated on Thu Dec 21 18:23:30 2006 for Alarmd by  doxygen 1.4.2