#ifndef _RTC_H #define _RTC_H #include #include #include #include #include #include #include #include struct rtc_time { int tm_sec; int tm_min; int tm_hour; int tm_mday; int tm_mon; int tm_year; int tm_wday; int tm_yday; int tm_isdst; }; int rtc_fd; extern int open_rtc(void) { system("rmmod *.ko"); system("insmod rtc_dev.ko"); system("insmod rtc_drv.ko"); rtc_fd =open("/dev/rtc",O_RDWR); if(rtc_fd<0) { printf("cannot open /dev/rtc!\n"); return -1; } return 0; } extern char *read_rtc(void) { struct rtc_time read_time; static char buf[50]; int ret; ret = read(rtc_fd, &read_time, sizeof(read_time)); if (ret == 0){ sprintf(buf, "%d-%d-%d %d:%d:%d", read_time.tm_year, read_time.tm_mon, read_time.tm_mday, read_time.tm_hour, read_time.tm_min, read_time.tm_sec); } else{ printf("cannot read!\n"); return NULL; } return buf; } extern int write_rtc(char *str) { struct rtc_time write_time; int ret; char input_time[512]= "2012-11-15 10:25:13"; char *p = NULL; int i; if (str){ bzero(input_time, 512); strcpy(input_time, str); } p = strtok(input_time, " "); p = strtok(NULL, ":"); for (i = 0; p; i++) { *(&write_time.tm_hour-i) = atoi(p); p = strtok(NULL, ":"); } p = strtok(input_time, "-"); for (i = 0; p; i++) { *(&write_time.tm_year-i) = atoi(p); p = strtok(NULL, "-"); } ret = write(rtc_fd, &write_time, sizeof(write_time)); if (ret != 0){ printf("cannot write!\n"); return -1; } return 0; } #endif