void i2c_ds13x7_sync(uint32_t timestamp) { #ifdef CLOCK_DATETIME_SUPPORT ds13x7_reg_t rtc; struct clock_datetime_t d; memset( &rtc, 0, sizeof(rtc)); clock_localtime( &d, timestamp); rtc.ch = 0; rtc.sec = i2b( d.sec ); rtc.min = i2b( d.min ); rtc.hour = i2b( d.hour ); rtc.day = d.dow; rtc.date = i2b( d.day ); rtc.month = i2b( d.month ); rtc.century = (d.year>= 100 ? 1:0); rtc.year = i2b( d.year % 100 ); i2c_ds13x7_set_block( 0, (char *)&rtc, sizeof(rtc) ); // not the Ctrl reg #endif }
int16_t parse_cmd_lastdcf(char *cmd, char *output, uint16_t len) { clock_datetime_t date; uint32_t last_valid; last_valid = dcf77_get_last_valid_timestamp(); clock_localtime(&date, last_valid); return generate_time_string(&date, output, len); }
void cron_periodic(void) { clock_datetime_t d, ld; uint32_t timestamp = clock_get_time(); /* fix last_check */ if (timestamp < last_check) { clock_datetime(&d, timestamp); last_check = timestamp - d.sec; return; } /* Check tasks at most once in a minute and only if at least one exists */ if (!head || (timestamp - last_check) < 60) return; /* get time and date from unix timestamp */ clock_datetime(&d, timestamp); clock_localtime(&ld, timestamp); /* truncate secs */ timestamp -= d.sec; #ifdef CRON_ANACRON_SUPPORT uint8_t skip_anacron = 0; if ((timestamp - last_check) > 60) skip_anacron = cron_anacron(last_check, timestamp); #endif /* check every event for a match */ struct cron_event_linkedlist *current = head; struct cron_event_linkedlist *exec; while (current) { /* backup current cronjob and advance current */ exec = current; current = current->next; #ifdef CRON_ANACRON_SUPPORT if (skip_anacron && exec->event.anacron) continue; #endif /* if it matches all conditions , execute the handler function */ if (cron_check_event(&exec->event.cond, exec->event.use_utc, &d, &ld)) cron_execute(exec); } /* save the actual timestamp */ last_check = timestamp; }
uint8_t encode_timestamp_text(uint8_t * ptr, timestamp_t ts) { clock_datetime_t dt; memset(&dt, 0, sizeof(dt)); clock_localtime(&dt, ts); ptr[0] = SNMP_TYPE_STRING; ptr[1] = snprintf_P((char *) (ptr + 2), TIMESTAMP_TEXT_LENGTH, PSTR(TIMESTAMP_TEXT_FORMAT), dt.day, dt.month, dt.year + 1900, dt.hour, dt.min, dt.sec); return ptr[1] + 2; }
void cron_static_periodic(void) { /* convert time to something useful */ clock_datetime_t d, ld; uint32_t timestamp = clock_get_time(); /* fix last_check */ if (timestamp < last_check) { clock_datetime(&d, timestamp); last_check = timestamp - d.sec; return; } /* Only check the tasks every minute */ if ((timestamp - last_check) < 60) return; clock_datetime(&d, timestamp); clock_localtime(&ld, timestamp); struct cron_static_event_t event; /* check every event for a match */ for (uint8_t i = 0;; i++) { memcpy_P(&event, &events[i], sizeof(struct cron_static_event_t)); /* end of task list reached */ if (event.handler == NULL) break; /* if it matches, execute the handler function */ if (cron_check_event(&event.cond, event.use_utc, &d, &ld)) { event.handler(); } } /* save the actual timestamp */ last_check = timestamp - d.sec; }
uint8_t cron_anacron(uint32_t starttime, uint32_t endtime) { clock_datetime_t d, ld; struct cron_event_linkedlist *curr; /* count anacron jobs and set pending */ uint8_t count = 0; for (curr = head; curr != 0; curr = curr->next) { if ((curr->event.anacron_pending = curr->event.anacron)) count++; } #ifdef DEBUG_CRON debug_printf("cron: %i anacron jobs found\n", count); #endif if (count == 0) return 0; /* alloc space for anacron list */ struct cron_event_linkedlist **tab = __builtin_alloca(count * sizeof(struct cron_event_linkedlist *)); if (!tab) { #ifdef DEBUG_CRON debug_printf("cron: not enough ram!\n"); #endif return 0; } /* limit range */ if ((endtime - starttime) > CRON_ANACRON_MAXAGE) starttime = endtime - CRON_ANACRON_MAXAGE; /* prepare anacron tab (reverse time order!!) */ uint8_t pos = 0; uint32_t timestamp; for (timestamp = endtime; timestamp > starttime && pos < count; timestamp -= 60) { clock_datetime(&d, timestamp); clock_localtime(&ld, timestamp); for (curr = head; curr != 0; curr = curr->next) { if (curr->event.anacron_pending && cron_check_event(&curr->event.cond, curr->event.use_utc, &d, &ld)) { curr->event.anacron_pending = 0; tab[pos++] = curr; } } } #ifdef DEBUG_CRON debug_printf("cron: %i pending anacron jobs\n", pos); #endif /* process jobs */ while (pos) cron_execute(tab[--pos]); return 1; }