alarm.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #define LOG_MODULE LOG_MAIN_MODULE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <assert.h>
  6. #include "alarm.h"
  7. #include "log.h"
  8. #include "ntp.h"
  9. #include "typedefs.h"
  10. #include "rtc.h"
  11. #define n 2
  12. struct _snooze
  13. {
  14. struct _tm snoozeStart;
  15. struct _tm snoozeEnd;
  16. };
  17. struct _alarm alarm[n];
  18. struct _snooze snooze[n];
  19. int checkAlarms(){
  20. int i = 0;
  21. int check = 0;
  22. for (i = 0; i < n; i++){
  23. setState(i);
  24. if (alarm[i].state == 1){
  25. check = 1;
  26. }
  27. }
  28. if (check == 1){
  29. return 1;
  30. }
  31. return 0;
  32. }
  33. int alarmExist(int id){
  34. int g;
  35. for (g = 0; g < n; g++){
  36. if (alarm[g].id == id){
  37. return g;
  38. }
  39. }
  40. return -1;
  41. }
  42. struct _alarm getAlarm(int idx){
  43. return alarm[idx];
  44. }
  45. int getState(int idx){
  46. return alarm[idx].state;
  47. }
  48. int maxAlarms(){
  49. return n;
  50. }
  51. void setState(int idx){
  52. struct _tm ct;
  53. X12RtcGetClock(&ct);
  54. if (NtpCompareTime(ct, alarm[idx].time) == 1 && alarm[idx].time.tm_year != false){
  55. alarm[idx].state = 1;
  56. } else {
  57. alarm[idx].state = 0;
  58. }
  59. /*if (NtpCompareTime(alarm[idx].time,snooze[idx].snoozeStart)){
  60. alarm[idx].state = 2;
  61. }
  62. if (alarm[idx].state == 2){
  63. if (NtpCompareTime(alarm[idx].time, snooze[idx].snoozeEnd)){
  64. snooze[idx].snoozeStart.tm_min += alarm[idx].snooze + 1;
  65. snooze[idx].snoozeEnd.tm_min += alarm[idx].snooze + 1;
  66. }
  67. }*/
  68. }
  69. /*void getAlarm(struct _alarm *am){
  70. int i = 0;
  71. for (i = 0; i < n; i++){
  72. am[i] = alarm[i];
  73. }
  74. }*/
  75. void setAlarm(struct _tm time, char* name, u_long ip, u_short port, char* url, char snooze, int id, int idx){
  76. alarm[idx].time = time;
  77. strncpy(alarm[idx].name, name, sizeof(alarm[idx].name));
  78. alarm[idx].ip = ip;
  79. alarm[idx].port = port;
  80. strncpy(alarm[idx].url, url, sizeof(alarm[idx].url));
  81. alarm[idx].snooze = snooze;
  82. alarm[idx].id = id;
  83. alarm[idx].state = 0;
  84. //snooze[idx].snoozeStart = time;
  85. //snooze[idx].snoozeEnd = time;
  86. //snooze[idx].snoozeStart += 1;
  87. //snooze[idx].snoozeEnd += (snooze +1);
  88. }
  89. void deleteAlarm(int idx){
  90. struct _tm tm;
  91. tm.tm_year = 0;
  92. alarm[idx].time = tm;
  93. alarm[idx].port = 0;
  94. alarm[idx].snooze = 5;
  95. alarm[idx].id = -1;
  96. alarm[idx].state = -1;
  97. }
  98. void handleAlarm(int idx){
  99. alarm[idx].time.tm_mday = alarm[idx].time.tm_mday + 1;
  100. alarm[idx].state = 0;
  101. }