| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #ifndef _RTC_H
- #define _RTC_H
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <strings.h>
- 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
|