void AlarmInit(void) { /*initialize alarm*/ /* allarme ore in punto o 30 minuti*/ if (rtc_tm.tm_min < 15) { ring_tm = setNextQuarterPastHourAlarm(rtc_tm); } else if (rtc_tm.tm_min < 30) { ring_tm = setNextHalfHourAlarm(rtc_tm); } else if (rtc_tm.tm_min < 45) { ring_tm = setNextQuarterToHourAlarm(rtc_tm); } else { ring_tm = setNextHourAlarm(rtc_tm); } }
void BellManager(int fd, struct BellConfigurationSt BellConf, struct BellHnd Bell) { int JustRing = 0; SetAlarm(fd); //log_message(LOG_FILE, "SetAlarm executed"); /* Read the current alarm settings */ retval = ioctl(fd, RTC_ALM_READ, &ring_tm); if (retval == -1) { log_message(LOG_FILE, "RTC_ALM_READ ioctl error"); perror("RTC_ALM_READ ioctl"); exit(errno); } fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n", ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec); fprintf(stderr, "Waiting for alarm...\r\n"); fflush(stderr); // /* Enable alarm interrupts */ // retval = ioctl(fd, RTC_AIE_ON, 0); // if (retval == -1) // { // log_message(LOG_FILE, "RTC_AIE_ON ioctl error"); // perror("RTC_AIE_ON ioctl"); // exit(errno); // } /* Enable every second interrupts */ retval = ioctl(fd, RTC_PIE_ON, 0); if (retval == -1) { log_message(LOG_FILE, "RTC_PIE_ON ioctl error"); perror("RTC_AIE_ON ioctl"); exit(errno); } /* This blocks until the alarm ring causes an interrupt */ retval = read(fd, &data, sizeof(unsigned long)); if (retval == -1) { log_message(LOG_FILE, "RTC read error"); perror("read"); exit(errno); } irqcount++; //log_message(LOG_FILE, "okay! Alarm rang."); /* Read the RTC time/date */ retval = ioctl(fd, RTC_RD_TIME, &rtc_tm); if (retval == -1) { log_message(LOG_FILE, "RTC_RD_TIME ioctl error"); perror("RTC_RD_TIME ioctl"); exit(errno); } /* reset alarm only whe the min 00 is passed*/ if(rtc_tm.tm_min != 00 && rtc_tm.tm_min != 15 && rtc_tm.tm_min != 30 && rtc_tm.tm_min != 45) { alarm_checked = 0; } /*check alarm if it set*/ if (rtc_tm.tm_min == 00 && alarm_checked == 0) { fprintf(stderr, "It's %02d:%02d:%02d. Ring From:%d to:%d enable:%d \n", ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec, BellConf.RingFrom,BellConf.RingTo, BellConf.HoursEnable); JustRing = CheckMessa(rtc_tm,BellConf,Bell); /* check the No Sound Zone*/ if(ring_tm.tm_hour >= BellConf.RingFrom && ring_tm.tm_hour <= BellConf.RingTo && BellConf.HoursEnable && JustRing == 0) { for (i = 0;i< (ring_tm.tm_hour > 12 ?ring_tm.tm_hour - 12 : ring_tm.tm_hour); i++) { /*play bells TODO support ring to 00h if necessary*/ playBell(&Bell.IDBellHamH, BellConf.DelayTime_0); log_message(LOG_FILE, "Ora piena."); } } /* */ alarm_checked =1; /*set next alarm*/ ring_tm = setNextQuarterPastHourAlarm(rtc_tm); } if (rtc_tm.tm_min == 15 && alarm_checked == 0) { fprintf(stderr, "It's %02d:%02d:%02d. Ring From:%d to:%d enable:%d \n", ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec, BellConf.RingFrom,BellConf.RingTo, BellConf.QuarterEnable); JustRing = CheckMessa(rtc_tm,BellConf,Bell); /* check the No Sound Zone*/ if(ring_tm.tm_hour >= BellConf.RingFrom && ring_tm.tm_hour <= BellConf.RingTo && BellConf.QuarterEnable && JustRing == 0) { /*play bells*/ playBell(&Bell.IDBellHamHH, BellConf.DelayTime_0); log_message(LOG_FILE, "Quarto d'ora."); /*set next alarm*/ } alarm_checked =1; ring_tm = setNextHalfHourAlarm(rtc_tm); } if (rtc_tm.tm_min == 30 && alarm_checked == 0) { fprintf(stderr, "It's %02d:%02d:%02d. Ring From:%d to:%d enable:%d \n", ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec, BellConf.RingFrom,BellConf.RingTo, BellConf.HalfEnable); JustRing = CheckMessa(rtc_tm,BellConf,Bell); /* check the No Sound Zone*/ if(ring_tm.tm_hour >= BellConf.RingFrom && ring_tm.tm_hour <= BellConf.RingTo && BellConf.HalfEnable && JustRing == 0) { /*play bells*/ playBell(&Bell.IDBellHamHH, BellConf.DelayTime_0); log_message(LOG_FILE, "Ora mezza."); } alarm_checked =1; /*set next alarm*/ ring_tm = setNextQuarterToHourAlarm(rtc_tm); } if (rtc_tm.tm_min == 45 && alarm_checked == 0) { fprintf(stderr, "It's %02d:%02d:%02d. Ring From:%d to:%d enable:%d \n", ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec, BellConf.RingFrom,BellConf.RingTo, BellConf.QuarterEnable); JustRing = CheckMessa(rtc_tm,BellConf,Bell); /* check the No Sound Zone*/ if(ring_tm.tm_hour >= BellConf.RingFrom && ring_tm.tm_hour <= BellConf.RingTo && BellConf.QuarterEnable && JustRing == 0) { /*play bells*/ playBell(&Bell.IDBellHamHH, BellConf.DelayTime_0); log_message(LOG_FILE, "Quarto d'ora."); } alarm_checked =1; /*set next alarm*/ ring_tm = setNextHourAlarm(rtc_tm); } UnSetAlarm(fd); }
int main(int argc, char **argv) { /*variabili timer rtc */ int fd, retval, irqcount = 0; unsigned long data; struct rtc_time rtc_tm; struct rtc_time ring_tm; const char *rtc = default_rtc; int i=0; int IDgreen=-1,IDbuzzer=-1,IDyellow=-1; fprintf(stderr, "\n...fraMartino is started and run in background."); /* port J7.3 e J7.7 impostati come out */ IDgreen=bindOutputChannel(7,3); IDyellow=bindOutputChannel(7,7); IDbuzzer=bindOutputChannel(7,4); if((IDgreen==-1)||(IDyellow==-1)) { fprintf(stderr, "\nError: unable to access gpio resource. Exit.\n"); exit(-1); } daemonize(); log_message(LOG_FILE,"*Started*"); /* apre la comunicazione con il device RTC */ fd = open(rtc, O_RDONLY); if (fd == -1) { perror(rtc); exit(errno); } /* Read the RTC time/date */ retval = ioctl(fd, RTC_RD_TIME, &rtc_tm); if (retval == -1) { perror("RTC_RD_TIME ioctl"); exit(errno); } fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%02d.\n",rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); /*initialize alarm*/ /* allarme ore in punto o 30 minuti*/ if (rtc_tm.tm_min<30) { ring_tm=setNextHalfHourAlarm(rtc_tm); } else { ring_tm=setNextHourAlarm(rtc_tm); } while(1){ /*Set the alarm*/ retval = ioctl(fd, RTC_ALM_SET, &ring_tm); if (retval == -1) { if (errno == ENOTTY) { fprintf(stderr,"\n...Alarm IRQs not supported.\n"); } perror("RTC_ALM_SET ioctl"); exit(errno); } /* Read the current alarm settings */ retval = ioctl(fd, RTC_ALM_READ, &ring_tm); if (retval == -1) { perror("RTC_ALM_READ ioctl"); exit(errno); } fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n",ring_tm.tm_hour, ring_tm.tm_min, ring_tm.tm_sec); fprintf(stderr, "Waiting for alarm..."); fflush(stderr); /* Enable alarm interrupts */ retval = ioctl(fd, RTC_AIE_ON, 0); if (retval == -1) { perror("RTC_AIE_ON ioctl"); exit(errno); } /* This blocks until the alarm ring causes an interrupt */ retval = read(fd, &data, sizeof(unsigned long)); if (retval == -1) { perror("read"); exit(errno); } irqcount++; fprintf(stderr, " okay! Alarm rang.\n"); /* Read the RTC time/date */ retval = ioctl(fd, RTC_RD_TIME, &rtc_tm); if (retval == -1) { perror("RTC_RD_TIME ioctl"); exit(errno); } if ((rtc_tm.tm_min == 00)) { /*play bells*/ for (i=0;i<(ring_tm.tm_hour>12?ring_tm.tm_hour-12:ring_tm.tm_hour);i++) { playBell(&IDgreen,&IDbuzzer,2,&fd); printf("\n %d\n",i+1); } log_message(LOG_FILE,"Ora piena."); /*set next alarm*/ ring_tm=setNextHalfHourAlarm(rtc_tm); } if ((rtc_tm.tm_min == 30)) { /*play bells*/ playBell(&IDyellow,&IDbuzzer,2,&fd); log_message(LOG_FILE,"Ora mezza."); /*set next alarm*/ ring_tm=setNextHourAlarm(rtc_tm); } /* Disable alarm interrupts */ retval = ioctl(fd, RTC_AIE_OFF, 0); if (retval == -1) { perror("RTC_AIE_OFF ioctl"); exit(errno); } } return 0; }