본문 바로가기

Software

RTC Alarm 설정 샘플 코드

728x90
디바이스명 : /dev/rtc  
 필요헤더 : #include <linux/rtc.h>  
 시간 READ/SET  
 READ  
 ioctl : RTC_RD_TIME  
 struct rtc_time {  
      int tm_sec; //(00~59)  
      int tm_min; //(00~59)  
      int tm_hour; //(00~23)  
      int tm_mday; //(01~31)  
      int tm_mon; //(01~12)  
      int tm_year; //(00~99)  
      int tm_wday; //(0~6)  
      int tm_yday; //alarm unused  
      int tm_isdst; //alarm unused  
 };  
 SET  
 ioctl : RTC_SET_TIME  
 struct rtc_time {  
      int tm_sec; //(00~59)  
      int tm_min; //(00~59)  
      int tm_hour; //(00~23)  
      int tm_mday; //(01~31)  
      int tm_mon; //(01~12)  
      int tm_year; //(00~99)  
      int tm_wday; //(0~6)  
      int tm_yday; //alarm unused  
      int tm_isdst; //alarm unused  
 };  
 알람 READ/SET  
 READ  
 ioctl : RTC_WKALM_RD  
 struct rtc_time {  
      int tm_sec; //alarm unused  
      int tm_min; //(00~59)  
      int tm_hour; //(00~23)  
      int tm_mday; //(01~31)  
      int tm_mon; //alarm unused  
      int tm_year; //alarm unused  
      int tm_wday; //(0~6)  
      int tm_yday; //alarm unused  
      int tm_isdst; //alarm unused  
 };  
 SET ※alarm값중 미사용 값에 대해서는 0xff  
 ioctl : RTC_WKALM_SET  
 struct rtc_wkalrm {  
      unsigned char enabled; //(1:enable, 0:disable)  
      unsigned char pending; //unused   
      struct rtc_time time;         
 };  
 struct rtc_time {  
      int tm_sec; //alarm unused  
      int tm_min; //(00~59)  
      int tm_hour; //(00~23)  
      int tm_mday; //(01~31)  
      int tm_mon; //alarm unused  
      int tm_year; //alarm unused  
      int tm_wday; //(0~6)  
      int tm_yday; //alarm unused  
      int tm_isdst; //alarm unused  
 };  
1:  #include <stdio.h>  
2:  #include <string.h>  
3:  #include <time.h>  
4:  #include <fcntl.h>  
5:  #include <unistd.h>  
6:  #include <sys/ioctl.h>  
7:  #include <linux/rtc.h>  
8:  #define DEFAULT_RTC "/dev/rtc"  
9:  static const char *device = DEFAULT_RTC;  
10:  static void write_alarm(const struct rtc_wkalrm *alarm1)  
11:  {  
12:      int res;  
13:      int fd;  
14:      fd = open(device, O_RDWR);  
15:      if (fd < 0) {  
16:          printf("open fail\n");  
17:      }  
18:      res = ioctl(fd, RTC_WKALM_SET, alarm1);  
19:      if (res < 0) {  
20:          printf("ioctl(RTC_WKALM_SET) %d\n", res);  
21:      }  
22:      close(fd);  
23:  }  
24:  static void read_alarm(struct rtc_wkalrm *alarm)  
25:  {  
26:      int res;  
27:      int fd;  
28:      fd = open(device, O_RDWR);  
29:      res = ioctl(fd, RTC_WKALM_RD, alarm);  
30:      if (res < 0) {  
31:          perror("ioctl(RTC_RD_TIME)");  
32:      }  
33:      close(fd);  
34:  }  
35:  int main(int argc, char *argv[]){  
36:      struct rtc_wkalrm alarm_wr;  
37:      struct rtc_wkalrm alarm_rd;  
38:      struct tm tm, *tmp;  
39:      time_t t;  
40:      if (argc != 5){  


728x90