rtc.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef _RTC_H
  2. #define _RTC_H
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <strings.h>
  11. struct rtc_time {
  12. int tm_sec;
  13. int tm_min;
  14. int tm_hour;
  15. int tm_mday;
  16. int tm_mon;
  17. int tm_year;
  18. int tm_wday;
  19. int tm_yday;
  20. int tm_isdst;
  21. };
  22. int rtc_fd;
  23. extern int open_rtc(void)
  24. {
  25. system("rmmod *.ko");
  26. system("insmod rtc_dev.ko");
  27. system("insmod rtc_drv.ko");
  28. rtc_fd =open("/dev/rtc",O_RDWR);
  29. if(rtc_fd<0)
  30. {
  31. printf("cannot open /dev/rtc!\n");
  32. return -1;
  33. }
  34. return 0;
  35. }
  36. extern char *read_rtc(void)
  37. {
  38. struct rtc_time read_time;
  39. static char buf[50];
  40. int ret;
  41. ret = read(rtc_fd, &read_time, sizeof(read_time));
  42. if (ret == 0){
  43. sprintf(buf, "%d-%d-%d %d:%d:%d", read_time.tm_year, read_time.tm_mon, read_time.tm_mday,
  44. read_time.tm_hour, read_time.tm_min, read_time.tm_sec);
  45. }
  46. else{
  47. printf("cannot read!\n");
  48. return NULL;
  49. }
  50. return buf;
  51. }
  52. extern int write_rtc(char *str)
  53. {
  54. struct rtc_time write_time;
  55. int ret;
  56. char input_time[512]= "2012-11-15 10:25:13";
  57. char *p = NULL;
  58. int i;
  59. if (str){
  60. bzero(input_time, 512);
  61. strcpy(input_time, str);
  62. }
  63. p = strtok(input_time, " ");
  64. p = strtok(NULL, ":");
  65. for (i = 0; p; i++) {
  66. *(&write_time.tm_hour-i) = atoi(p);
  67. p = strtok(NULL, ":");
  68. }
  69. p = strtok(input_time, "-");
  70. for (i = 0; p; i++) {
  71. *(&write_time.tm_year-i) = atoi(p);
  72. p = strtok(NULL, "-");
  73. }
  74. ret = write(rtc_fd, &write_time, sizeof(write_time));
  75. if (ret != 0){
  76. printf("cannot write!\n");
  77. return -1;
  78. }
  79. return 0;
  80. }
  81. #endif