Fork HTTPClient and Modfiy code for mbed 6.0
Dependents: mbed-demo-http-get-json
data/HTTPiCal.cpp@40:bcb19f8dbba3, 2017-03-14 (annotated)
- Committer:
- WiredHome
- Date:
- Tue Mar 14 02:59:32 2017 +0000
- Revision:
- 40:bcb19f8dbba3
- Parent:
- 39:21fc7a4b6927
- Child:
- 41:37e7df011c1f
Revisions to clean up the iCal interface.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 39:21fc7a4b6927 | 1 | #include "HTTPiCal.h" |
WiredHome | 39:21fc7a4b6927 | 2 | |
WiredHome | 40:bcb19f8dbba3 | 3 | //#define DEBUG "iCal" |
WiredHome | 39:21fc7a4b6927 | 4 | #include <cstdio> |
WiredHome | 39:21fc7a4b6927 | 5 | #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) |
WiredHome | 39:21fc7a4b6927 | 6 | #define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 39:21fc7a4b6927 | 7 | #define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 39:21fc7a4b6927 | 8 | #define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 39:21fc7a4b6927 | 9 | #define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); |
WiredHome | 39:21fc7a4b6927 | 10 | #else |
WiredHome | 39:21fc7a4b6927 | 11 | #define DBG(x, ...) |
WiredHome | 39:21fc7a4b6927 | 12 | #define WARN(x, ...) |
WiredHome | 39:21fc7a4b6927 | 13 | #define ERR(x, ...) |
WiredHome | 39:21fc7a4b6927 | 14 | #define INFO(x, ...) |
WiredHome | 39:21fc7a4b6927 | 15 | #endif |
WiredHome | 39:21fc7a4b6927 | 16 | |
WiredHome | 39:21fc7a4b6927 | 17 | HTTPiCal::HTTPiCal(int count) { |
WiredHome | 39:21fc7a4b6927 | 18 | EventList = (Event_T *)malloc(count * sizeof(Event_T)); |
WiredHome | 39:21fc7a4b6927 | 19 | if (EventList) { |
WiredHome | 39:21fc7a4b6927 | 20 | EventSpaceCount = count; |
WiredHome | 39:21fc7a4b6927 | 21 | EventCount = 0; |
WiredHome | 39:21fc7a4b6927 | 22 | seeking = idle; |
WiredHome | 39:21fc7a4b6927 | 23 | } else { |
WiredHome | 39:21fc7a4b6927 | 24 | error("no space for event list"); |
WiredHome | 39:21fc7a4b6927 | 25 | } |
WiredHome | 39:21fc7a4b6927 | 26 | } |
WiredHome | 39:21fc7a4b6927 | 27 | |
WiredHome | 39:21fc7a4b6927 | 28 | HTTPiCal::~HTTPiCal() { |
WiredHome | 39:21fc7a4b6927 | 29 | if (EventList) |
WiredHome | 39:21fc7a4b6927 | 30 | free(EventList); |
WiredHome | 39:21fc7a4b6927 | 31 | } |
WiredHome | 39:21fc7a4b6927 | 32 | |
WiredHome | 39:21fc7a4b6927 | 33 | void HTTPiCal::SetTimeWindow(time_t StartTime, time_t EndTime) { |
WiredHome | 39:21fc7a4b6927 | 34 | gridStartTime = StartTime; |
WiredHome | 39:21fc7a4b6927 | 35 | gridEndTime = EndTime; |
WiredHome | 39:21fc7a4b6927 | 36 | } |
WiredHome | 39:21fc7a4b6927 | 37 | |
WiredHome | 39:21fc7a4b6927 | 38 | bool HTTPiCal::GetEvent(unsigned int i, Event_T * event) { |
WiredHome | 39:21fc7a4b6927 | 39 | if (i < EventCount) { |
WiredHome | 39:21fc7a4b6927 | 40 | *event = EventList[i]; |
WiredHome | 39:21fc7a4b6927 | 41 | return true; |
WiredHome | 39:21fc7a4b6927 | 42 | } else { |
WiredHome | 39:21fc7a4b6927 | 43 | return false; |
WiredHome | 39:21fc7a4b6927 | 44 | } |
WiredHome | 39:21fc7a4b6927 | 45 | } |
WiredHome | 39:21fc7a4b6927 | 46 | |
WiredHome | 39:21fc7a4b6927 | 47 | void HTTPiCal::writeReset() { |
WiredHome | 39:21fc7a4b6927 | 48 | //INFO("writeReset()"); |
WiredHome | 39:21fc7a4b6927 | 49 | EventCount = 0; |
WiredHome | 40:bcb19f8dbba3 | 50 | seeking = idle; |
WiredHome | 39:21fc7a4b6927 | 51 | lineBuf[0] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 52 | } |
WiredHome | 39:21fc7a4b6927 | 53 | |
WiredHome | 39:21fc7a4b6927 | 54 | int HTTPiCal::write(const char * buf, size_t len) { |
WiredHome | 39:21fc7a4b6927 | 55 | const char * pStart = buf; |
WiredHome | 39:21fc7a4b6927 | 56 | const char * pEOL; |
WiredHome | 39:21fc7a4b6927 | 57 | size_t origLen = len; |
WiredHome | 39:21fc7a4b6927 | 58 | |
WiredHome | 39:21fc7a4b6927 | 59 | pEOL = strchr(pStart, '\n'); |
WiredHome | 39:21fc7a4b6927 | 60 | //INFO("\r\n\r\nwrite[%d:%d] = \r\n%s\r\nend-write\r\n", len, pEOL - pStart, buf); |
WiredHome | 40:bcb19f8dbba3 | 61 | //printf("lineBuf:[%d] %s\r\n", len, buf); |
WiredHome | 39:21fc7a4b6927 | 62 | while (pEOL && (pEOL - pStart) < len) { |
WiredHome | 39:21fc7a4b6927 | 63 | int lbLen = strlen(lineBuf); |
WiredHome | 39:21fc7a4b6927 | 64 | strncpy(lineBuf + lbLen, pStart, (pEOL - pStart)); |
WiredHome | 39:21fc7a4b6927 | 65 | lineBuf[lbLen + (pEOL - pStart) + 1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 66 | if (lineBuf[pEOL - pStart + lbLen - 1] == '\r') |
WiredHome | 39:21fc7a4b6927 | 67 | lineBuf[pEOL - pStart + lbLen - 1] = '\0'; |
WiredHome | 40:bcb19f8dbba3 | 68 | //printf("lineBuf:[%s]\r\n", lineBuf); |
WiredHome | 39:21fc7a4b6927 | 69 | ParseICalStream(lineBuf, gridStartTime, gridEndTime, -300); |
WiredHome | 39:21fc7a4b6927 | 70 | //INFO(""); |
WiredHome | 39:21fc7a4b6927 | 71 | lineBuf[0] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 72 | len -= (pEOL - pStart); |
WiredHome | 39:21fc7a4b6927 | 73 | pStart = pEOL + 1; |
WiredHome | 39:21fc7a4b6927 | 74 | while (pStart && *pStart && *pStart <= ' ') { |
WiredHome | 39:21fc7a4b6927 | 75 | pStart++; |
WiredHome | 39:21fc7a4b6927 | 76 | len--; |
WiredHome | 39:21fc7a4b6927 | 77 | } |
WiredHome | 39:21fc7a4b6927 | 78 | if (*pStart) |
WiredHome | 39:21fc7a4b6927 | 79 | pEOL = strchr(pStart, '\n'); |
WiredHome | 39:21fc7a4b6927 | 80 | else |
WiredHome | 39:21fc7a4b6927 | 81 | pEOL = NULL; |
WiredHome | 39:21fc7a4b6927 | 82 | } |
WiredHome | 39:21fc7a4b6927 | 83 | if (len) { |
WiredHome | 39:21fc7a4b6927 | 84 | strncpy(lineBuf, pStart, len); |
WiredHome | 39:21fc7a4b6927 | 85 | lineBuf[len] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 86 | //INFO("fragment:[%s]", lineBuf); |
WiredHome | 39:21fc7a4b6927 | 87 | } |
WiredHome | 39:21fc7a4b6927 | 88 | //INFO("write returns %d", origLen); |
WiredHome | 39:21fc7a4b6927 | 89 | return origLen; |
WiredHome | 39:21fc7a4b6927 | 90 | } |
WiredHome | 39:21fc7a4b6927 | 91 | |
WiredHome | 39:21fc7a4b6927 | 92 | void HTTPiCal::setDataType(const char* type) { |
WiredHome | 39:21fc7a4b6927 | 93 | //INFO("setDataType(%s)", type); |
WiredHome | 39:21fc7a4b6927 | 94 | } |
WiredHome | 39:21fc7a4b6927 | 95 | |
WiredHome | 39:21fc7a4b6927 | 96 | //void HTTPiCal::setLocation(const char * location) { |
WiredHome | 39:21fc7a4b6927 | 97 | // |
WiredHome | 39:21fc7a4b6927 | 98 | //} |
WiredHome | 39:21fc7a4b6927 | 99 | |
WiredHome | 39:21fc7a4b6927 | 100 | void HTTPiCal::setIsChunked(bool chunked) { |
WiredHome | 39:21fc7a4b6927 | 101 | INFO("setIsChunked(%d)", chunked); |
WiredHome | 39:21fc7a4b6927 | 102 | m_chunked = chunked; |
WiredHome | 39:21fc7a4b6927 | 103 | } |
WiredHome | 39:21fc7a4b6927 | 104 | |
WiredHome | 39:21fc7a4b6927 | 105 | void HTTPiCal::setDataLen(size_t len) { |
WiredHome | 39:21fc7a4b6927 | 106 | //INFO("setDataLen(%d)", len); |
WiredHome | 39:21fc7a4b6927 | 107 | } |
WiredHome | 39:21fc7a4b6927 | 108 | |
WiredHome | 39:21fc7a4b6927 | 109 | |
WiredHome | 39:21fc7a4b6927 | 110 | const char * RPT_DAYS[] = { "SU", "MO", "TU", "WE", "TH", "FR", "SA", "" }; |
WiredHome | 39:21fc7a4b6927 | 111 | |
WiredHome | 39:21fc7a4b6927 | 112 | |
WiredHome | 39:21fc7a4b6927 | 113 | const char * HTTPiCal::RepeatDayAbbrev(int i) |
WiredHome | 39:21fc7a4b6927 | 114 | { |
WiredHome | 39:21fc7a4b6927 | 115 | if (i < 7) |
WiredHome | 39:21fc7a4b6927 | 116 | return RPT_DAYS[i]; |
WiredHome | 39:21fc7a4b6927 | 117 | else |
WiredHome | 39:21fc7a4b6927 | 118 | return RPT_DAYS[7]; |
WiredHome | 39:21fc7a4b6927 | 119 | } |
WiredHome | 39:21fc7a4b6927 | 120 | |
WiredHome | 39:21fc7a4b6927 | 121 | void HTTPiCal::SortEvents() |
WiredHome | 39:21fc7a4b6927 | 122 | { |
WiredHome | 39:21fc7a4b6927 | 123 | bool swapped; |
WiredHome | 39:21fc7a4b6927 | 124 | int e; |
WiredHome | 39:21fc7a4b6927 | 125 | Event_T Event; |
WiredHome | 39:21fc7a4b6927 | 126 | |
WiredHome | 39:21fc7a4b6927 | 127 | do { |
WiredHome | 39:21fc7a4b6927 | 128 | swapped = false; |
WiredHome | 39:21fc7a4b6927 | 129 | for (e=0; e<EventCount-1; e++) { |
WiredHome | 39:21fc7a4b6927 | 130 | if (EventList[e].Start > EventList[e+1].Start) { |
WiredHome | 39:21fc7a4b6927 | 131 | Event = EventList[e]; |
WiredHome | 39:21fc7a4b6927 | 132 | EventList[e] = EventList[e+1]; |
WiredHome | 39:21fc7a4b6927 | 133 | EventList[e+1] = Event; |
WiredHome | 39:21fc7a4b6927 | 134 | swapped = true; |
WiredHome | 39:21fc7a4b6927 | 135 | } |
WiredHome | 39:21fc7a4b6927 | 136 | } |
WiredHome | 39:21fc7a4b6927 | 137 | } while (swapped); |
WiredHome | 39:21fc7a4b6927 | 138 | } |
WiredHome | 39:21fc7a4b6927 | 139 | |
WiredHome | 39:21fc7a4b6927 | 140 | uint16_t HTTPiCal::AtoIxN(const char * p, int n) |
WiredHome | 39:21fc7a4b6927 | 141 | { |
WiredHome | 39:21fc7a4b6927 | 142 | uint16_t res = 0; |
WiredHome | 39:21fc7a4b6927 | 143 | |
WiredHome | 39:21fc7a4b6927 | 144 | while (n--) { |
WiredHome | 39:21fc7a4b6927 | 145 | res = (res * 10) + (*p - '0'); |
WiredHome | 39:21fc7a4b6927 | 146 | p++; |
WiredHome | 39:21fc7a4b6927 | 147 | } |
WiredHome | 39:21fc7a4b6927 | 148 | return res; |
WiredHome | 39:21fc7a4b6927 | 149 | } |
WiredHome | 39:21fc7a4b6927 | 150 | |
WiredHome | 39:21fc7a4b6927 | 151 | // YYYYMMDD[THHMMSS[Z]] |
WiredHome | 39:21fc7a4b6927 | 152 | // VALUE=DATE:YYYYMMDD |
WiredHome | 39:21fc7a4b6927 | 153 | time_t HTTPiCal::ParseDateStamp(const char * string, tz_sec_t tzoSec) |
WiredHome | 39:21fc7a4b6927 | 154 | { |
WiredHome | 39:21fc7a4b6927 | 155 | time_t tStamp; |
WiredHome | 39:21fc7a4b6927 | 156 | struct tm t; |
WiredHome | 39:21fc7a4b6927 | 157 | struct tm * tnow; |
WiredHome | 39:21fc7a4b6927 | 158 | |
WiredHome | 39:21fc7a4b6927 | 159 | time(&tStamp); |
WiredHome | 39:21fc7a4b6927 | 160 | tnow = localtime(&tStamp); |
WiredHome | 39:21fc7a4b6927 | 161 | if (strncmp(string, "VALUE=DATE:", 11) == 0) { |
WiredHome | 39:21fc7a4b6927 | 162 | string += 11; |
WiredHome | 39:21fc7a4b6927 | 163 | } |
WiredHome | 39:21fc7a4b6927 | 164 | //INFO("ParseDateStamp(%s,%d)\n", string, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 165 | t.tm_year = AtoIxN(string, 4) - 1900; |
WiredHome | 39:21fc7a4b6927 | 166 | t.tm_mon = AtoIxN(string+4, 2) - 1; |
WiredHome | 39:21fc7a4b6927 | 167 | t.tm_mday = AtoIxN(string+6, 2); |
WiredHome | 39:21fc7a4b6927 | 168 | if (strlen(string) > 8) { |
WiredHome | 39:21fc7a4b6927 | 169 | t.tm_hour = AtoIxN(string+9, 2); |
WiredHome | 39:21fc7a4b6927 | 170 | t.tm_min = AtoIxN(string+11, 2); |
WiredHome | 39:21fc7a4b6927 | 171 | t.tm_sec = AtoIxN(string+13, 2); |
WiredHome | 39:21fc7a4b6927 | 172 | t.tm_isdst = tnow->tm_isdst; |
WiredHome | 39:21fc7a4b6927 | 173 | } else { |
WiredHome | 39:21fc7a4b6927 | 174 | t.tm_hour = 0; |
WiredHome | 39:21fc7a4b6927 | 175 | t.tm_min = 0; |
WiredHome | 39:21fc7a4b6927 | 176 | t.tm_sec = 0; |
WiredHome | 39:21fc7a4b6927 | 177 | t.tm_isdst = tnow->tm_isdst; |
WiredHome | 39:21fc7a4b6927 | 178 | } |
WiredHome | 39:21fc7a4b6927 | 179 | tStamp = mktime(&t); |
WiredHome | 39:21fc7a4b6927 | 180 | if (string[strlen(string)-1] == 'Z') { |
WiredHome | 39:21fc7a4b6927 | 181 | //INFO("Applying tzoSec %d", tzoSec); |
WiredHome | 39:21fc7a4b6927 | 182 | tStamp = tStamp + tzoTZIDSec; |
WiredHome | 39:21fc7a4b6927 | 183 | } else { |
WiredHome | 39:21fc7a4b6927 | 184 | tStamp = tStamp + tzoSec; |
WiredHome | 39:21fc7a4b6927 | 185 | } |
WiredHome | 39:21fc7a4b6927 | 186 | return tStamp; |
WiredHome | 39:21fc7a4b6927 | 187 | } |
WiredHome | 39:21fc7a4b6927 | 188 | |
WiredHome | 39:21fc7a4b6927 | 189 | char * HTTPiCal::FormatCTime(time_t t) |
WiredHome | 39:21fc7a4b6927 | 190 | { |
WiredHome | 39:21fc7a4b6927 | 191 | static char temp[4][80]; |
WiredHome | 39:21fc7a4b6927 | 192 | static int i = 0; |
WiredHome | 39:21fc7a4b6927 | 193 | |
WiredHome | 39:21fc7a4b6927 | 194 | i &= 3; |
WiredHome | 39:21fc7a4b6927 | 195 | strcpy(temp[i], ctime(&t)); |
WiredHome | 39:21fc7a4b6927 | 196 | temp[i][strlen(temp[i])-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 197 | return temp[i++]; |
WiredHome | 39:21fc7a4b6927 | 198 | } |
WiredHome | 39:21fc7a4b6927 | 199 | |
WiredHome | 39:21fc7a4b6927 | 200 | |
WiredHome | 39:21fc7a4b6927 | 201 | void HTTPiCal::ShowEventInfo(Event_T & Event) |
WiredHome | 39:21fc7a4b6927 | 202 | { |
WiredHome | 39:21fc7a4b6927 | 203 | char scratch[80]; |
WiredHome | 39:21fc7a4b6927 | 204 | #define LF "\r\n" |
WiredHome | 39:21fc7a4b6927 | 205 | |
WiredHome | 39:21fc7a4b6927 | 206 | printf("******* Summary: %s" LF, Event.Summary); |
WiredHome | 39:21fc7a4b6927 | 207 | printf(" Location: %s" LF, Event.Location); |
WiredHome | 39:21fc7a4b6927 | 208 | printf(" Category: %s" LF, Event.Category); |
WiredHome | 39:21fc7a4b6927 | 209 | printf(" Priority: %d" LF, Event.Priority); |
WiredHome | 39:21fc7a4b6927 | 210 | sprintf(scratch, "%lu ", Event.Start); |
WiredHome | 39:21fc7a4b6927 | 211 | sprintf(scratch + strlen(scratch), "%s", (Event.Start == 0) ? "" : ctime(&Event.Start)); |
WiredHome | 39:21fc7a4b6927 | 212 | scratch[strlen(scratch)-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 213 | printf(" Start: %s" LF, scratch); |
WiredHome | 39:21fc7a4b6927 | 214 | sprintf(scratch, "%lu ", Event.End); |
WiredHome | 39:21fc7a4b6927 | 215 | sprintf(scratch + strlen(scratch), "%s", (Event.End == 0) ? "" : ctime(&Event.End)); |
WiredHome | 39:21fc7a4b6927 | 216 | scratch[strlen(scratch)-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 217 | printf(" End: %s" LF, scratch); |
WiredHome | 39:21fc7a4b6927 | 218 | printf(" Count: %d" LF, Event.Count); |
WiredHome | 39:21fc7a4b6927 | 219 | printf(" Interval: %d" LF, Event.Interval); |
WiredHome | 39:21fc7a4b6927 | 220 | printf(" RepeatFrq: %d" LF, Event.RepeatFreq); |
WiredHome | 39:21fc7a4b6927 | 221 | printf(" RepeatDay: %02X" LF, Event.RepeatDays); |
WiredHome | 39:21fc7a4b6927 | 222 | printf(" RepeatMonthDay: %08X" LF, Event.RepeatMonthDay); |
WiredHome | 39:21fc7a4b6927 | 223 | printf(" RepeatMonthDayRev: %08X" LF, Event.RepeatMonthDayRev); |
WiredHome | 39:21fc7a4b6927 | 224 | printf(" RepeatMonth: %04X" LF, Event.RepeatMonths); |
WiredHome | 39:21fc7a4b6927 | 225 | sprintf(scratch, "%lu ", Event.Until); |
WiredHome | 39:21fc7a4b6927 | 226 | sprintf(scratch + strlen(scratch), "%s", (Event.Until == 0) ? "" : ctime(&Event.Until)); |
WiredHome | 39:21fc7a4b6927 | 227 | scratch[strlen(scratch)-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 228 | printf(" Until: %s" LF, scratch); |
WiredHome | 39:21fc7a4b6927 | 229 | printf("" LF); |
WiredHome | 39:21fc7a4b6927 | 230 | } |
WiredHome | 39:21fc7a4b6927 | 231 | |
WiredHome | 39:21fc7a4b6927 | 232 | |
WiredHome | 39:21fc7a4b6927 | 233 | /// Computes the intersection of time1 and time2 ranges, and modifies time1 |
WiredHome | 39:21fc7a4b6927 | 234 | /// range to represent the intersection. |
WiredHome | 39:21fc7a4b6927 | 235 | /// |
WiredHome | 39:21fc7a4b6927 | 236 | /// start1 is input as the start of the time1 range, and is written |
WiredHome | 39:21fc7a4b6927 | 237 | /// to represent the intersection of the two ranges. |
WiredHome | 39:21fc7a4b6927 | 238 | /// end1 is input as the end of the time1 range and is written to |
WiredHome | 39:21fc7a4b6927 | 239 | /// represent the intersection of the two ranges. |
WiredHome | 39:21fc7a4b6927 | 240 | /// start2 is the start of the time2 range. |
WiredHome | 39:21fc7a4b6927 | 241 | /// end2 is the end of the time2 range. |
WiredHome | 39:21fc7a4b6927 | 242 | /// returns true if the ranges have an intersection, and the time1 range |
WiredHome | 39:21fc7a4b6927 | 243 | /// values have been modified. |
WiredHome | 39:21fc7a4b6927 | 244 | /// |
WiredHome | 39:21fc7a4b6927 | 245 | bool HTTPiCal::TimeIntersects(time_t * start1, time_t * end1, time_t * start2, time_t * end2) |
WiredHome | 39:21fc7a4b6927 | 246 | { |
WiredHome | 39:21fc7a4b6927 | 247 | // |----Time1----| |
WiredHome | 39:21fc7a4b6927 | 248 | // |--Time2--| false |
WiredHome | 39:21fc7a4b6927 | 249 | // |
WiredHome | 39:21fc7a4b6927 | 250 | // |----Time1----| |
WiredHome | 39:21fc7a4b6927 | 251 | // |--Time2--| false |
WiredHome | 39:21fc7a4b6927 | 252 | // |
WiredHome | 39:21fc7a4b6927 | 253 | // |----Time1----| |
WiredHome | 39:21fc7a4b6927 | 254 | // |----Time2----| |
WiredHome | 39:21fc7a4b6927 | 255 | // |-Time1-| true |
WiredHome | 39:21fc7a4b6927 | 256 | // |
WiredHome | 39:21fc7a4b6927 | 257 | // |----Time1----| |
WiredHome | 39:21fc7a4b6927 | 258 | // |----Time2----| |
WiredHome | 39:21fc7a4b6927 | 259 | // |-Time1-| true |
WiredHome | 39:21fc7a4b6927 | 260 | // |
WiredHome | 39:21fc7a4b6927 | 261 | // |----Time1-------| |
WiredHome | 39:21fc7a4b6927 | 262 | // |-Time2-| |
WiredHome | 39:21fc7a4b6927 | 263 | // |-Time1-| true |
WiredHome | 39:21fc7a4b6927 | 264 | // |
WiredHome | 39:21fc7a4b6927 | 265 | // | Time1 (end1 == 0) |
WiredHome | 39:21fc7a4b6927 | 266 | // | Time2 (end2 == 0) true |
WiredHome | 39:21fc7a4b6927 | 267 | // |
WiredHome | 39:21fc7a4b6927 | 268 | if (*start1 == *start2 && *end1 == 0 && *end2 == 0) |
WiredHome | 39:21fc7a4b6927 | 269 | return true; |
WiredHome | 39:21fc7a4b6927 | 270 | if (*end1 < *start2 || *end2 < *start1) |
WiredHome | 39:21fc7a4b6927 | 271 | return false; |
WiredHome | 39:21fc7a4b6927 | 272 | if (max(*start1,*start2) < min(*end1,*end2)) { |
WiredHome | 39:21fc7a4b6927 | 273 | *start1 = max(*start1,*start2); |
WiredHome | 39:21fc7a4b6927 | 274 | *end1 = min(*end1,*end2); |
WiredHome | 39:21fc7a4b6927 | 275 | return true; |
WiredHome | 39:21fc7a4b6927 | 276 | } else { |
WiredHome | 39:21fc7a4b6927 | 277 | return false; |
WiredHome | 39:21fc7a4b6927 | 278 | } |
WiredHome | 39:21fc7a4b6927 | 279 | } |
WiredHome | 39:21fc7a4b6927 | 280 | |
WiredHome | 39:21fc7a4b6927 | 281 | bool HTTPiCal::isLeapYear(time_t t) |
WiredHome | 39:21fc7a4b6927 | 282 | { |
WiredHome | 39:21fc7a4b6927 | 283 | int year; |
WiredHome | 39:21fc7a4b6927 | 284 | struct tm * ts; |
WiredHome | 39:21fc7a4b6927 | 285 | ts = localtime(&t); |
WiredHome | 39:21fc7a4b6927 | 286 | |
WiredHome | 39:21fc7a4b6927 | 287 | year = 1900 + ts->tm_year + 1; |
WiredHome | 39:21fc7a4b6927 | 288 | if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) |
WiredHome | 39:21fc7a4b6927 | 289 | return true; |
WiredHome | 39:21fc7a4b6927 | 290 | else |
WiredHome | 39:21fc7a4b6927 | 291 | return false; |
WiredHome | 39:21fc7a4b6927 | 292 | } |
WiredHome | 39:21fc7a4b6927 | 293 | |
WiredHome | 39:21fc7a4b6927 | 294 | time_t HTTPiCal::NextInterval(time_t curTime, int repeatFreq, int interval) |
WiredHome | 39:21fc7a4b6927 | 295 | { |
WiredHome | 39:21fc7a4b6927 | 296 | const time_t secperday = 60*60*24; |
WiredHome | 39:21fc7a4b6927 | 297 | const int repeatFactor[] = {0, 1, 7, 30, 365}; |
WiredHome | 39:21fc7a4b6927 | 298 | int delta = repeatFactor[repeatFreq]; |
WiredHome | 39:21fc7a4b6927 | 299 | if (repeatFreq == 4 && isLeapYear(curTime)) |
WiredHome | 39:21fc7a4b6927 | 300 | delta += 1; |
WiredHome | 39:21fc7a4b6927 | 301 | //INFO("freq %d, interval %d, delta %d", repeatFreq, interval, delta); |
WiredHome | 39:21fc7a4b6927 | 302 | return delta * interval * secperday; |
WiredHome | 39:21fc7a4b6927 | 303 | } |
WiredHome | 39:21fc7a4b6927 | 304 | |
WiredHome | 39:21fc7a4b6927 | 305 | |
WiredHome | 39:21fc7a4b6927 | 306 | // start1,end1 is the time range representing the visible grid |
WiredHome | 39:21fc7a4b6927 | 307 | // start2,end2 is the time range of the event being tested |
WiredHome | 39:21fc7a4b6927 | 308 | // Event is also the event being tested and permits testing the repeat information. |
WiredHome | 39:21fc7a4b6927 | 309 | // |
WiredHome | 39:21fc7a4b6927 | 310 | // If the event repeat pattern intersects with the display pattern, indicate this as "true" |
WiredHome | 39:21fc7a4b6927 | 311 | // |
WiredHome | 39:21fc7a4b6927 | 312 | bool HTTPiCal::RepeatMaskIntersects(time_t * start1, time_t * end1, time_t * start2, time_t * end2, Event_T * Event) |
WiredHome | 39:21fc7a4b6927 | 313 | { |
WiredHome | 39:21fc7a4b6927 | 314 | bool intersects = false; |
WiredHome | 39:21fc7a4b6927 | 315 | |
WiredHome | 39:21fc7a4b6927 | 316 | //INFO("RepeatFreq: %d", Event->RepeatFreq); |
WiredHome | 39:21fc7a4b6927 | 317 | if (Event->RepeatFreq == rptfDaily) { |
WiredHome | 39:21fc7a4b6927 | 318 | //INFO("rptfDaily is not handled"); |
WiredHome | 39:21fc7a4b6927 | 319 | } else if (Event->RepeatFreq == rptfWeekly) { |
WiredHome | 39:21fc7a4b6927 | 320 | struct tm * timeinfo; |
WiredHome | 39:21fc7a4b6927 | 321 | timeinfo = localtime(start1); |
WiredHome | 39:21fc7a4b6927 | 322 | uint8_t daymask = Event->RepeatDays; |
WiredHome | 39:21fc7a4b6927 | 323 | // now, check the tm_wday (0=Sunday, 1=Monday, ...) and see if we intersect with the event time |
WiredHome | 39:21fc7a4b6927 | 324 | uint8_t testmask = 1 << timeinfo->tm_wday; |
WiredHome | 39:21fc7a4b6927 | 325 | //INFO("Mask: Event mask: %02X, test mask: %02X", daymask, testmask); |
WiredHome | 39:21fc7a4b6927 | 326 | if (daymask & testmask) |
WiredHome | 39:21fc7a4b6927 | 327 | intersects = true; |
WiredHome | 39:21fc7a4b6927 | 328 | else |
WiredHome | 39:21fc7a4b6927 | 329 | intersects = false; |
WiredHome | 39:21fc7a4b6927 | 330 | //INFO(" intersects: %02X", daymask & testmask); |
WiredHome | 39:21fc7a4b6927 | 331 | return intersects; |
WiredHome | 39:21fc7a4b6927 | 332 | } else if (Event->RepeatFreq == rptfYearly) { |
WiredHome | 39:21fc7a4b6927 | 333 | //struct tm * timeinfo; |
WiredHome | 39:21fc7a4b6927 | 334 | //timeinfo = localtime(start1); |
WiredHome | 39:21fc7a4b6927 | 335 | //INFO("rptfYearly is not handled well yet"); |
WiredHome | 39:21fc7a4b6927 | 336 | } |
WiredHome | 39:21fc7a4b6927 | 337 | //INFO("Mask: no handler, returning true"); |
WiredHome | 39:21fc7a4b6927 | 338 | return true; |
WiredHome | 39:21fc7a4b6927 | 339 | } |
WiredHome | 39:21fc7a4b6927 | 340 | |
WiredHome | 39:21fc7a4b6927 | 341 | bool HTTPiCal::RepeatIntersects(time_t * start1, time_t * end1, time_t * start2, time_t * end2, Event_T * Event) |
WiredHome | 39:21fc7a4b6927 | 342 | { |
WiredHome | 39:21fc7a4b6927 | 343 | INFO("** 1: (%s, %s)", FormatCTime(*start1), *end1 ? FormatCTime(*end1) : ""); |
WiredHome | 39:21fc7a4b6927 | 344 | INFO(" 2: (%s, %s)", FormatCTime(*start2), *end2 ? FormatCTime(*end2) : ""); |
WiredHome | 39:21fc7a4b6927 | 345 | INFO(" ev: (%s, %s)", FormatCTime(Event->Start), Event->End ? FormatCTime(Event->End) : ""); |
WiredHome | 39:21fc7a4b6927 | 346 | if (TimeIntersects(start1, end1, start2, end2)) |
WiredHome | 39:21fc7a4b6927 | 347 | return true; |
WiredHome | 39:21fc7a4b6927 | 348 | if (Event && Event->RepeatFreq) { |
WiredHome | 39:21fc7a4b6927 | 349 | INFO("RepeatFreq: %d", Event->RepeatFreq); |
WiredHome | 39:21fc7a4b6927 | 350 | if (Event->Start < *start2 && Event->Until > *start2 ) { // Until=.... |
WiredHome | 39:21fc7a4b6927 | 351 | INFO("Repeat until: %d", Event->Until); |
WiredHome | 39:21fc7a4b6927 | 352 | do { |
WiredHome | 39:21fc7a4b6927 | 353 | time_t interval = NextInterval(*start1, Event->RepeatFreq, (Event->Interval == 0) ? 1 : Event->Interval); |
WiredHome | 39:21fc7a4b6927 | 354 | *start1 = *start1 + interval; |
WiredHome | 39:21fc7a4b6927 | 355 | if (*end1) |
WiredHome | 39:21fc7a4b6927 | 356 | *end1 = *end1 + interval; |
WiredHome | 39:21fc7a4b6927 | 357 | INFO("** 1: (%s, %s)", FormatCTime(*start1), *end1 ? FormatCTime(*end1) : ""); |
WiredHome | 39:21fc7a4b6927 | 358 | INFO("until (%24s, %s)", " ", FormatCTime(Event->Until)); |
WiredHome | 39:21fc7a4b6927 | 359 | INFO(" 2: (%s, %s)", FormatCTime(*start2), *end2 ? FormatCTime(*end2) : ""); |
WiredHome | 39:21fc7a4b6927 | 360 | if (!RepeatMaskIntersects(start1, end1, start2, end2, Event)) { |
WiredHome | 39:21fc7a4b6927 | 361 | continue; // we're not on a repeat cycle (e.g. wrong day of the week) |
WiredHome | 39:21fc7a4b6927 | 362 | } |
WiredHome | 39:21fc7a4b6927 | 363 | if (TimeIntersects(start1, end1, start2, end2)) { |
WiredHome | 39:21fc7a4b6927 | 364 | return true; |
WiredHome | 39:21fc7a4b6927 | 365 | } |
WiredHome | 39:21fc7a4b6927 | 366 | } while ((*end2 == 0 || *start1 < *end2) && *start1 < Event->Until); |
WiredHome | 39:21fc7a4b6927 | 367 | } else if (Event->Start < *start2 && Event->Count) { // Count= |
WiredHome | 39:21fc7a4b6927 | 368 | INFO("Repeat count %d", Event->Count); |
WiredHome | 39:21fc7a4b6927 | 369 | int count = Event->Count - 1; |
WiredHome | 39:21fc7a4b6927 | 370 | do { |
WiredHome | 39:21fc7a4b6927 | 371 | time_t interval = NextInterval(*start1, Event->RepeatFreq, (Event->Interval == 0) ? 1 : Event->Interval); |
WiredHome | 39:21fc7a4b6927 | 372 | *start1 = *start1 + interval; |
WiredHome | 39:21fc7a4b6927 | 373 | if (*end1) |
WiredHome | 39:21fc7a4b6927 | 374 | *end1 = *end1 + interval; |
WiredHome | 39:21fc7a4b6927 | 375 | INFO("** 1: (%s, %s) - %d", FormatCTime(*start1), *end1 ? FormatCTime(*end1) : "", count); |
WiredHome | 39:21fc7a4b6927 | 376 | INFO(" 2: (%s, %s)", FormatCTime(*start2), *end2 ? FormatCTime(*end2) : ""); |
WiredHome | 39:21fc7a4b6927 | 377 | if (!RepeatMaskIntersects(start1, end1, start2, end2, Event)) { |
WiredHome | 39:21fc7a4b6927 | 378 | continue; // we're not on a repeat cycle (e.g. wrong day of the week) |
WiredHome | 39:21fc7a4b6927 | 379 | } |
WiredHome | 39:21fc7a4b6927 | 380 | if (TimeIntersects(start1, end1, start2, end2)) { |
WiredHome | 39:21fc7a4b6927 | 381 | return true; |
WiredHome | 39:21fc7a4b6927 | 382 | } |
WiredHome | 39:21fc7a4b6927 | 383 | } while (--count && *end1 < *start2); |
WiredHome | 39:21fc7a4b6927 | 384 | } else if (Event->Start < *start2) { // no Count= and no Until= |
WiredHome | 39:21fc7a4b6927 | 385 | INFO(" no Repeat end"); |
WiredHome | 39:21fc7a4b6927 | 386 | do { |
WiredHome | 39:21fc7a4b6927 | 387 | int rptFreq = Event->RepeatFreq; |
WiredHome | 39:21fc7a4b6927 | 388 | if (Event->RepeatFreq == 2 && Event->RepeatDays != 0) |
WiredHome | 39:21fc7a4b6927 | 389 | rptFreq--; |
WiredHome | 39:21fc7a4b6927 | 390 | time_t interval = NextInterval(*start1, rptFreq, (Event->Interval == 0) ? 1 : Event->Interval); |
WiredHome | 39:21fc7a4b6927 | 391 | *start1 = *start1 + interval; |
WiredHome | 39:21fc7a4b6927 | 392 | if (*end1) |
WiredHome | 39:21fc7a4b6927 | 393 | *end1 = *end1 + interval; |
WiredHome | 39:21fc7a4b6927 | 394 | INFO("== 1: (%s, %s)", FormatCTime(*start1), *end1 ? FormatCTime(*end1) : ""); |
WiredHome | 39:21fc7a4b6927 | 395 | INFO(" 2: (%s, %s)", FormatCTime(*start2), *end2 ? FormatCTime(*end2) : ""); |
WiredHome | 39:21fc7a4b6927 | 396 | if (!RepeatMaskIntersects(start1, end1, start2, end2, Event)) { |
WiredHome | 39:21fc7a4b6927 | 397 | continue; // we're not on a repeat cycle (e.g. wrong day of the week) |
WiredHome | 39:21fc7a4b6927 | 398 | } |
WiredHome | 39:21fc7a4b6927 | 399 | if (TimeIntersects(start1, end1, start2, end2)) { |
WiredHome | 39:21fc7a4b6927 | 400 | return true; |
WiredHome | 39:21fc7a4b6927 | 401 | } |
WiredHome | 39:21fc7a4b6927 | 402 | } while (*start1 < *end2 || (*end2 == 0 && *start1 < *start2)); |
WiredHome | 39:21fc7a4b6927 | 403 | } else { |
WiredHome | 39:21fc7a4b6927 | 404 | INFO("falling out"); |
WiredHome | 39:21fc7a4b6927 | 405 | } |
WiredHome | 39:21fc7a4b6927 | 406 | } |
WiredHome | 39:21fc7a4b6927 | 407 | INFO(" no intersection"); |
WiredHome | 39:21fc7a4b6927 | 408 | return false; |
WiredHome | 39:21fc7a4b6927 | 409 | } |
WiredHome | 39:21fc7a4b6927 | 410 | |
WiredHome | 39:21fc7a4b6927 | 411 | // All the stuff between |
WiredHome | 39:21fc7a4b6927 | 412 | // BEGIN:VEVENT |
WiredHome | 39:21fc7a4b6927 | 413 | // ... |
WiredHome | 39:21fc7a4b6927 | 414 | // END:VEVENT |
WiredHome | 39:21fc7a4b6927 | 415 | // |
WiredHome | 39:21fc7a4b6927 | 416 | void HTTPiCal::ParseEvent(Event_T * Event, const char * pStart, tz_sec_t tzoSec) |
WiredHome | 39:21fc7a4b6927 | 417 | { |
WiredHome | 39:21fc7a4b6927 | 418 | INFO("ParseEvent(...,'%s',%d)", pStart, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 419 | if (strncmp(pStart, "DTSTART:", 8) == 0) { |
WiredHome | 39:21fc7a4b6927 | 420 | Event->Start = ParseDateStamp(pStart+8, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 421 | INFO(" Start: %s\n", ctime(&Event->Start)); |
WiredHome | 39:21fc7a4b6927 | 422 | } else if (strncmp(pStart, "DTSTART;", 8) == 0) { |
WiredHome | 39:21fc7a4b6927 | 423 | const char * p = pStart + 8; |
WiredHome | 39:21fc7a4b6927 | 424 | tzoSec = ParseTZID(p); |
WiredHome | 39:21fc7a4b6927 | 425 | p = strrchr(pStart, ':'); |
WiredHome | 39:21fc7a4b6927 | 426 | if (p) { |
WiredHome | 39:21fc7a4b6927 | 427 | Event->Start = ParseDateStamp(p+1, 0); // example was localtime with GMT -06:00 tzoSec); |
WiredHome | 39:21fc7a4b6927 | 428 | INFO(" Start: %s", ctime(&Event->Start)); |
WiredHome | 39:21fc7a4b6927 | 429 | } |
WiredHome | 39:21fc7a4b6927 | 430 | } else if (strncmp(pStart, "DTEND:", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 431 | Event->End = ParseDateStamp(pStart+6, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 432 | //INFO(" End: %d\n", mktime(&Event->eventEnd)); |
WiredHome | 39:21fc7a4b6927 | 433 | } else if (strncmp(pStart, "DTEND;", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 434 | const char * p = pStart + 6; |
WiredHome | 39:21fc7a4b6927 | 435 | tzoSec = ParseTZID(p); |
WiredHome | 39:21fc7a4b6927 | 436 | p = strrchr(pStart, ':'); |
WiredHome | 39:21fc7a4b6927 | 437 | if (p) { |
WiredHome | 39:21fc7a4b6927 | 438 | Event->End = ParseDateStamp(p+1, 0); // example was localtime with GMT -06:00 tzoSec); |
WiredHome | 39:21fc7a4b6927 | 439 | INFO(" End: %s", ctime(&Event->End)); |
WiredHome | 39:21fc7a4b6927 | 440 | } |
WiredHome | 39:21fc7a4b6927 | 441 | } else if (strncmp(pStart, "SUMMARY:", 8) == 0) { |
WiredHome | 39:21fc7a4b6927 | 442 | strncpy(Event->Summary, pStart+8, SUMMARY_CHARS-1); |
WiredHome | 39:21fc7a4b6927 | 443 | Event->Summary[SUMMARY_CHARS-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 444 | //INFO(" Summary: %s\n", Event->Summary); |
WiredHome | 39:21fc7a4b6927 | 445 | } else if (strncmp(pStart, "LOCATION:", 9) == 0) { |
WiredHome | 39:21fc7a4b6927 | 446 | strncpy(Event->Location, pStart+9, LOCATION_CHARS-1); |
WiredHome | 39:21fc7a4b6927 | 447 | Event->Location[LOCATION_CHARS-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 448 | //INFO(" Location: %s\n", Event->Location); |
WiredHome | 39:21fc7a4b6927 | 449 | } else if (strncmp(pStart, "PRIORITY:", 9) == 0) { |
WiredHome | 39:21fc7a4b6927 | 450 | Event->Priority = *(pStart+9) - '0'; |
WiredHome | 39:21fc7a4b6927 | 451 | //INFO(" Priority: %d\n", Event->Priority); |
WiredHome | 39:21fc7a4b6927 | 452 | } else if (strncmp(pStart, "CATEGORIES:", 11) == 0) { |
WiredHome | 39:21fc7a4b6927 | 453 | strncpy(Event->Category, pStart+11, CATEGORY_CHARS-1); |
WiredHome | 39:21fc7a4b6927 | 454 | Event->Category[CATEGORY_CHARS-1] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 455 | //INFO(" Category: %s\n", Event->Category); |
WiredHome | 39:21fc7a4b6927 | 456 | } else if (strncmp(pStart, "RRULE:", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 457 | //RRULE:FREQ=WEEKLY;UNTIL=20140502T180000;BYDAY=MO,TU,WE,TH,FR |
WiredHome | 39:21fc7a4b6927 | 458 | const char * p1, *p2; |
WiredHome | 39:21fc7a4b6927 | 459 | //INFO("%s", pStart); |
WiredHome | 39:21fc7a4b6927 | 460 | p1 = pStart + 6; // p1 = FREQ=WEEKLY;UNTIL=20140502T180000;BYDAY=MO,TU,WE,TH,FR |
WiredHome | 39:21fc7a4b6927 | 461 | p2 = strchr(p1, ';'); |
WiredHome | 39:21fc7a4b6927 | 462 | //if (p2) |
WiredHome | 39:21fc7a4b6927 | 463 | // *p2++ = '\0'; |
WiredHome | 39:21fc7a4b6927 | 464 | while (*p1) { |
WiredHome | 39:21fc7a4b6927 | 465 | INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 466 | if (strncmp(p1, "FREQ=", 5) == 0) { |
WiredHome | 39:21fc7a4b6927 | 467 | //INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 468 | p1 += 5; // p1 = WEEKLY;UNTIL=20140502T180000;BYDAY=MO,TU,WE,TH,FR |
WiredHome | 39:21fc7a4b6927 | 469 | if (strncmp(p1, "WEEKLY", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 470 | //INFO(" %s", p1); |
WiredHome | 39:21fc7a4b6927 | 471 | Event->RepeatFreq = rptfWeekly; |
WiredHome | 39:21fc7a4b6927 | 472 | p1 += 6; |
WiredHome | 39:21fc7a4b6927 | 473 | } else if (strncmp(p1, "DAILY", 5) == 0) { |
WiredHome | 39:21fc7a4b6927 | 474 | //INFO(" %s", p1); |
WiredHome | 39:21fc7a4b6927 | 475 | Event->RepeatFreq = rptfDaily; |
WiredHome | 39:21fc7a4b6927 | 476 | p1 += 5; |
WiredHome | 39:21fc7a4b6927 | 477 | } else if (strncmp(p1, "MONTHLY", 7) == 0) { |
WiredHome | 39:21fc7a4b6927 | 478 | //INFO(" %s", p1); |
WiredHome | 39:21fc7a4b6927 | 479 | Event->RepeatFreq = rptfMonthly; |
WiredHome | 39:21fc7a4b6927 | 480 | p1 += 7; |
WiredHome | 39:21fc7a4b6927 | 481 | } else if (strncmp(p1, "YEARLY", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 482 | //INFO(" %s", p1); |
WiredHome | 39:21fc7a4b6927 | 483 | Event->RepeatFreq = rptfYearly; |
WiredHome | 39:21fc7a4b6927 | 484 | p1 += 6; |
WiredHome | 39:21fc7a4b6927 | 485 | } |
WiredHome | 39:21fc7a4b6927 | 486 | } else if (strncmp(p1, "INTERVAL=", 9) == 0) { // INTERVAL=2 |
WiredHome | 39:21fc7a4b6927 | 487 | //INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 488 | p1 += 9; |
WiredHome | 39:21fc7a4b6927 | 489 | Event->Interval = atoi(p1); |
WiredHome | 39:21fc7a4b6927 | 490 | } else if (strncmp(p1, "COUNT=", 6) == 0) { // COUNT=12; |
WiredHome | 39:21fc7a4b6927 | 491 | //INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 492 | p1 += 6; // p1 = |
WiredHome | 39:21fc7a4b6927 | 493 | Event->Count = atoi(p1); |
WiredHome | 39:21fc7a4b6927 | 494 | } else if (strncmp(p1, "UNTIL=", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 495 | //INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 496 | p1 += 6; // p1 = 20140502T180000;BYDAY=MO,TU,WE,TH,FR |
WiredHome | 39:21fc7a4b6927 | 497 | //printf("UNTIL= {%s}\n", p1); |
WiredHome | 39:21fc7a4b6927 | 498 | Event->Until = ParseDateStamp(p1, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 499 | //printf("UNTIL:: %d: %d\n", Event->Until, tzoSec); |
WiredHome | 39:21fc7a4b6927 | 500 | } else if (strncmp(p1, "BYDAY=", 6) == 0) { |
WiredHome | 39:21fc7a4b6927 | 501 | //INFO("%s", p1); |
WiredHome | 39:21fc7a4b6927 | 502 | p1 += 6; // p1 = MO,TU,WE,TH,FR |
WiredHome | 39:21fc7a4b6927 | 503 | while (*p1 >= ' ') { |
WiredHome | 39:21fc7a4b6927 | 504 | //INFO(" %s", p1); |
WiredHome | 39:21fc7a4b6927 | 505 | for (int d=0; d<7; d++) { |
WiredHome | 39:21fc7a4b6927 | 506 | if (strncmp(p1,RepeatDayAbbrev(d),2) == 0) { |
WiredHome | 39:21fc7a4b6927 | 507 | Event->RepeatDays |= (1 << d); |
WiredHome | 39:21fc7a4b6927 | 508 | //INFO(" %s %02X", RepeatDayAbbrev(d), Event->RepeatDays); |
WiredHome | 39:21fc7a4b6927 | 509 | break; |
WiredHome | 39:21fc7a4b6927 | 510 | } |
WiredHome | 39:21fc7a4b6927 | 511 | } |
WiredHome | 39:21fc7a4b6927 | 512 | p1 += 3; |
WiredHome | 39:21fc7a4b6927 | 513 | } |
WiredHome | 39:21fc7a4b6927 | 514 | //INFO(" RepeatDay: %02X", Event->RepeatDays); |
WiredHome | 39:21fc7a4b6927 | 515 | } else if (strncmp(p1, "BYMONTHDAY=", 11) == 0) { |
WiredHome | 39:21fc7a4b6927 | 516 | // RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15 |
WiredHome | 39:21fc7a4b6927 | 517 | p1 += 11; |
WiredHome | 39:21fc7a4b6927 | 518 | while (*p1 >= ' ') { |
WiredHome | 39:21fc7a4b6927 | 519 | const char * px = p1; |
WiredHome | 39:21fc7a4b6927 | 520 | while (*px >= ' ' && *px != ',') { // find , or ; or <nul> |
WiredHome | 39:21fc7a4b6927 | 521 | px++; |
WiredHome | 39:21fc7a4b6927 | 522 | } |
WiredHome | 39:21fc7a4b6927 | 523 | //if (*px) |
WiredHome | 39:21fc7a4b6927 | 524 | // *px++ = '\0'; |
WiredHome | 39:21fc7a4b6927 | 525 | int num = atoi(p1); |
WiredHome | 39:21fc7a4b6927 | 526 | if (num >= 0) |
WiredHome | 39:21fc7a4b6927 | 527 | Event->RepeatMonthDay |= (1 << num); |
WiredHome | 39:21fc7a4b6927 | 528 | else |
WiredHome | 39:21fc7a4b6927 | 529 | Event->RepeatMonthDayRev |= (1 << -num); |
WiredHome | 39:21fc7a4b6927 | 530 | p1 = px; |
WiredHome | 39:21fc7a4b6927 | 531 | } |
WiredHome | 39:21fc7a4b6927 | 532 | INFO(" RepeatMonthDay: %08X", Event->RepeatMonthDay); |
WiredHome | 39:21fc7a4b6927 | 533 | } else if (strncmp(p1, "BYMONTH=", 8) == 0) { |
WiredHome | 39:21fc7a4b6927 | 534 | // RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3 |
WiredHome | 39:21fc7a4b6927 | 535 | p1 += 8; |
WiredHome | 39:21fc7a4b6927 | 536 | while (*p1 >= ' ') { |
WiredHome | 39:21fc7a4b6927 | 537 | const char * px = p1; |
WiredHome | 39:21fc7a4b6927 | 538 | while (*px >= ' ' && *px != ',') { // find , or ; or <nul> |
WiredHome | 39:21fc7a4b6927 | 539 | px++; |
WiredHome | 39:21fc7a4b6927 | 540 | } |
WiredHome | 39:21fc7a4b6927 | 541 | //if (*px) |
WiredHome | 39:21fc7a4b6927 | 542 | // *px++ = '\0'; |
WiredHome | 39:21fc7a4b6927 | 543 | int num = atoi(p1); |
WiredHome | 39:21fc7a4b6927 | 544 | if (num >= 0) |
WiredHome | 39:21fc7a4b6927 | 545 | Event->RepeatMonths |= (1 << num); |
WiredHome | 39:21fc7a4b6927 | 546 | //else |
WiredHome | 39:21fc7a4b6927 | 547 | // ; // Event->RepeatMonthsRev |= (1 << -num); |
WiredHome | 39:21fc7a4b6927 | 548 | p1 = px; |
WiredHome | 39:21fc7a4b6927 | 549 | } |
WiredHome | 39:21fc7a4b6927 | 550 | INFO(" RepeatMonths: %04X", Event->RepeatMonths); |
WiredHome | 39:21fc7a4b6927 | 551 | } |
WiredHome | 39:21fc7a4b6927 | 552 | if (!p2) |
WiredHome | 39:21fc7a4b6927 | 553 | break; |
WiredHome | 39:21fc7a4b6927 | 554 | p1 = p2 + 1; |
WiredHome | 39:21fc7a4b6927 | 555 | p2 = strchr(p1, ';'); |
WiredHome | 39:21fc7a4b6927 | 556 | //if (p2) |
WiredHome | 39:21fc7a4b6927 | 557 | // *p2++ = '\0'; |
WiredHome | 39:21fc7a4b6927 | 558 | } |
WiredHome | 39:21fc7a4b6927 | 559 | } |
WiredHome | 39:21fc7a4b6927 | 560 | } |
WiredHome | 39:21fc7a4b6927 | 561 | |
WiredHome | 39:21fc7a4b6927 | 562 | |
WiredHome | 39:21fc7a4b6927 | 563 | // TZID="(GMT -06:00)":20140519T063000 |
WiredHome | 39:21fc7a4b6927 | 564 | // TZID:(UTC-06:00) Central Time (US & Canada) |
WiredHome | 39:21fc7a4b6927 | 565 | // TZID:(GMT -06:00) |
WiredHome | 39:21fc7a4b6927 | 566 | HTTPiCal::tz_sec_t HTTPiCal::ParseTZID(const char * string) |
WiredHome | 39:21fc7a4b6927 | 567 | { |
WiredHome | 39:21fc7a4b6927 | 568 | tz_sec_t tzo = 0; |
WiredHome | 39:21fc7a4b6927 | 569 | bool sign = false; |
WiredHome | 39:21fc7a4b6927 | 570 | |
WiredHome | 39:21fc7a4b6927 | 571 | INFO("ParseTZID(%s)", string); // TZID="(GMT -06:00)":20140519T063000 |
WiredHome | 39:21fc7a4b6927 | 572 | string += 5; // "(GMT -06:00)":20140519T063000 |
WiredHome | 39:21fc7a4b6927 | 573 | if (*string == '"') |
WiredHome | 39:21fc7a4b6927 | 574 | string++; // (GMT -06:00)":20140519T063000 |
WiredHome | 39:21fc7a4b6927 | 575 | if ((strncmp(string, "(UTC", 4) == 0) |
WiredHome | 39:21fc7a4b6927 | 576 | || (strncmp(string, "(GMT", 4) == 0) ){ |
WiredHome | 39:21fc7a4b6927 | 577 | string += 4; |
WiredHome | 39:21fc7a4b6927 | 578 | if (*string == ' ') |
WiredHome | 39:21fc7a4b6927 | 579 | string++; |
WiredHome | 39:21fc7a4b6927 | 580 | if (*string == '-') { |
WiredHome | 39:21fc7a4b6927 | 581 | sign = true; |
WiredHome | 39:21fc7a4b6927 | 582 | string++; |
WiredHome | 39:21fc7a4b6927 | 583 | } |
WiredHome | 39:21fc7a4b6927 | 584 | tzo = atoi(string) * 3600; |
WiredHome | 39:21fc7a4b6927 | 585 | string = strchr(string, ':'); |
WiredHome | 39:21fc7a4b6927 | 586 | if (string) { |
WiredHome | 39:21fc7a4b6927 | 587 | string++; |
WiredHome | 39:21fc7a4b6927 | 588 | tzo += atoi(string) * 60; |
WiredHome | 39:21fc7a4b6927 | 589 | } |
WiredHome | 39:21fc7a4b6927 | 590 | if (sign) |
WiredHome | 39:21fc7a4b6927 | 591 | tzo = -tzo; |
WiredHome | 39:21fc7a4b6927 | 592 | INFO(" tzo = %d", tzo); |
WiredHome | 39:21fc7a4b6927 | 593 | } else { |
WiredHome | 39:21fc7a4b6927 | 594 | ERR("Unhandled TZID(%s)", string); |
WiredHome | 39:21fc7a4b6927 | 595 | } |
WiredHome | 39:21fc7a4b6927 | 596 | return tzo; |
WiredHome | 39:21fc7a4b6927 | 597 | } |
WiredHome | 39:21fc7a4b6927 | 598 | |
WiredHome | 39:21fc7a4b6927 | 599 | int HTTPiCal::ParseICalStream(const char * pStart, time_t gridStartTime, time_t gridEndTime, tz_min_t tzoMin, bool showEvents) |
WiredHome | 39:21fc7a4b6927 | 600 | { |
WiredHome | 39:21fc7a4b6927 | 601 | INFO("Parse(%s)", pStart); |
WiredHome | 39:21fc7a4b6927 | 602 | //INFO("EventCount: %d, EventSpaceCount: %d, seeking: %d", EventCount, EventSpaceCount, seeking); |
WiredHome | 39:21fc7a4b6927 | 603 | if (pStart && *pStart && EventCount < EventSpaceCount) { |
WiredHome | 39:21fc7a4b6927 | 604 | switch (seeking) { |
WiredHome | 39:21fc7a4b6927 | 605 | case idle: |
WiredHome | 39:21fc7a4b6927 | 606 | if (strncmp(pStart, "BEGIN:VTIMEZONE", 15) == 0) { |
WiredHome | 39:21fc7a4b6927 | 607 | //INFO("begin:timezone"); |
WiredHome | 39:21fc7a4b6927 | 608 | seeking = inTimeZone; |
WiredHome | 39:21fc7a4b6927 | 609 | } else if (strncmp(pStart, "BEGIN:VEVENT", 12) == 0) { |
WiredHome | 39:21fc7a4b6927 | 610 | //INFO("begin:vevent"); |
WiredHome | 39:21fc7a4b6927 | 611 | seeking = inEvent; |
WiredHome | 39:21fc7a4b6927 | 612 | EventList[EventCount].Start = 0; |
WiredHome | 39:21fc7a4b6927 | 613 | EventList[EventCount].End = 0; |
WiredHome | 39:21fc7a4b6927 | 614 | EventList[EventCount].Until = 0; |
WiredHome | 39:21fc7a4b6927 | 615 | EventList[EventCount].Summary[0] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 616 | EventList[EventCount].Location[0] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 617 | EventList[EventCount].Category[0] = '\0'; |
WiredHome | 39:21fc7a4b6927 | 618 | EventList[EventCount].Count = 0; |
WiredHome | 39:21fc7a4b6927 | 619 | EventList[EventCount].Interval = 0; |
WiredHome | 39:21fc7a4b6927 | 620 | EventList[EventCount].RepeatFreq = rptfNone; |
WiredHome | 39:21fc7a4b6927 | 621 | EventList[EventCount].RepeatDays = 0; |
WiredHome | 39:21fc7a4b6927 | 622 | EventList[EventCount].RepeatMonthDay = 0; |
WiredHome | 39:21fc7a4b6927 | 623 | EventList[EventCount].RepeatMonthDayRev = 0; |
WiredHome | 39:21fc7a4b6927 | 624 | EventList[EventCount].RepeatMonths = 0; |
WiredHome | 39:21fc7a4b6927 | 625 | EventList[EventCount].Priority = 5; // 5 is Normal |
WiredHome | 39:21fc7a4b6927 | 626 | } |
WiredHome | 39:21fc7a4b6927 | 627 | break; |
WiredHome | 39:21fc7a4b6927 | 628 | case inTimeZone: |
WiredHome | 39:21fc7a4b6927 | 629 | //INFO("inTimeZone:"); |
WiredHome | 39:21fc7a4b6927 | 630 | // Can also pick up daylight savings time |
WiredHome | 39:21fc7a4b6927 | 631 | if (strncmp(pStart, "END:VTIMEZONE", 13) == 0) { |
WiredHome | 39:21fc7a4b6927 | 632 | seeking = idle; |
WiredHome | 39:21fc7a4b6927 | 633 | } else if ((strncmp(pStart, "TZID:", 5) == 0) |
WiredHome | 39:21fc7a4b6927 | 634 | || (strncmp(pStart, "TZID=", 5) == 0) ) { |
WiredHome | 39:21fc7a4b6927 | 635 | tzoTZIDSec = ParseTZID(pStart); |
WiredHome | 39:21fc7a4b6927 | 636 | tzAdjusted = true; |
WiredHome | 39:21fc7a4b6927 | 637 | pStart += 5; |
WiredHome | 39:21fc7a4b6927 | 638 | } else if (strncmp(pStart, "BEGIN:STANDARD", 14) == 0) { |
WiredHome | 39:21fc7a4b6927 | 639 | |
WiredHome | 39:21fc7a4b6927 | 640 | } else if (strncmp(pStart, "BEGIN:DAYLIGHT", 14) == 0) { |
WiredHome | 39:21fc7a4b6927 | 641 | |
WiredHome | 39:21fc7a4b6927 | 642 | } |
WiredHome | 39:21fc7a4b6927 | 643 | break; |
WiredHome | 39:21fc7a4b6927 | 644 | case inEvent: |
WiredHome | 39:21fc7a4b6927 | 645 | //INFO("inEvent:"); |
WiredHome | 39:21fc7a4b6927 | 646 | // inEvent |
WiredHome | 39:21fc7a4b6927 | 647 | if (strncmp(pStart, "END:VEVENT", 10) == 0) { |
WiredHome | 39:21fc7a4b6927 | 648 | // Timezone offset |
WiredHome | 39:21fc7a4b6927 | 649 | if (!tzAdjusted) { |
WiredHome | 39:21fc7a4b6927 | 650 | EventList[EventCount].Start += (60 * tzoMin); |
WiredHome | 39:21fc7a4b6927 | 651 | if (EventList[EventCount].End) |
WiredHome | 39:21fc7a4b6927 | 652 | EventList[EventCount].End += (60 * tzoMin); |
WiredHome | 39:21fc7a4b6927 | 653 | } |
WiredHome | 39:21fc7a4b6927 | 654 | // Process it |
WiredHome | 40:bcb19f8dbba3 | 655 | if (showEvents) { |
WiredHome | 40:bcb19f8dbba3 | 656 | //printf("ev: %d\r\n", EventCount); |
WiredHome | 39:21fc7a4b6927 | 657 | ShowEventInfo(EventList[EventCount]); |
WiredHome | 40:bcb19f8dbba3 | 658 | } |
WiredHome | 39:21fc7a4b6927 | 659 | // Force to ALWAYS |
WiredHome | 39:21fc7a4b6927 | 660 | time_t aStart = EventList[EventCount].Start; |
WiredHome | 39:21fc7a4b6927 | 661 | time_t aEnd = EventList[EventCount].End; |
WiredHome | 39:21fc7a4b6927 | 662 | if (gridStartTime |
WiredHome | 39:21fc7a4b6927 | 663 | && RepeatIntersects(&aStart, &aEnd, &gridStartTime, &gridEndTime, &EventList[EventCount])) { |
WiredHome | 39:21fc7a4b6927 | 664 | EventCount++; |
WiredHome | 39:21fc7a4b6927 | 665 | if (showEvents) { |
WiredHome | 39:21fc7a4b6927 | 666 | INFO(" +++++ Added Event %d", EventCount);//, EventList[EventCount].Summary); |
WiredHome | 39:21fc7a4b6927 | 667 | } |
WiredHome | 39:21fc7a4b6927 | 668 | } |
WiredHome | 39:21fc7a4b6927 | 669 | seeking = idle; |
WiredHome | 39:21fc7a4b6927 | 670 | } else { |
WiredHome | 39:21fc7a4b6927 | 671 | //INFO("parse event data"); |
WiredHome | 39:21fc7a4b6927 | 672 | ParseEvent(&EventList[EventCount], pStart, 60 * tzoMin); |
WiredHome | 39:21fc7a4b6927 | 673 | } |
WiredHome | 39:21fc7a4b6927 | 674 | // End of inEvent |
WiredHome | 39:21fc7a4b6927 | 675 | break; |
WiredHome | 39:21fc7a4b6927 | 676 | default: |
WiredHome | 39:21fc7a4b6927 | 677 | INFO("default:"); |
WiredHome | 39:21fc7a4b6927 | 678 | seeking = idle; |
WiredHome | 39:21fc7a4b6927 | 679 | break; |
WiredHome | 39:21fc7a4b6927 | 680 | } |
WiredHome | 39:21fc7a4b6927 | 681 | } // while |
WiredHome | 39:21fc7a4b6927 | 682 | return EventCount; |
WiredHome | 39:21fc7a4b6927 | 683 | } |