2016-12-26 14:19:12 +01:00
|
|
|
// Cron module
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "user_interface.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
2016-12-26 14:19:12 +01:00
|
|
|
#include "ets_sys.h"
|
|
|
|
#include "time.h"
|
|
|
|
#include "rtc/rtctime.h"
|
|
|
|
#include "mem.h"
|
|
|
|
|
|
|
|
struct cronent_desc {
|
|
|
|
uint64_t min; // Minutes repeat - bits 0-59
|
|
|
|
uint32_t hour; // Hours repeat - bits 0-23
|
|
|
|
uint32_t dom; // Day-of-month repeat - bits 0-30
|
|
|
|
uint16_t mon; // Monthly repeat - bits 0-11
|
|
|
|
uint8_t dow; // Day-of-week repeat - bits 0-6
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct cronent_ud {
|
|
|
|
struct cronent_desc desc;
|
|
|
|
int cb_ref;
|
|
|
|
} cronent_ud_t;
|
|
|
|
|
|
|
|
static ETSTimer cron_timer;
|
|
|
|
|
2020-06-13 04:50:03 +02:00
|
|
|
static int cronent_table_ref;
|
2016-12-26 14:19:12 +01:00
|
|
|
|
|
|
|
static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min, uint8_t max) {
|
|
|
|
uint64_t res = 0;
|
2018-10-10 18:36:00 +02:00
|
|
|
|
|
|
|
/* Gobble whitespace before potential stars; no strtol on that path */
|
|
|
|
while (*str != '\0' && (*str == ' ' || *str == '\t')) {
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
if (str[0] == '*') {
|
|
|
|
uint32_t each = 1;
|
|
|
|
*end = str + 1;
|
|
|
|
if (str[1] == '/') {
|
|
|
|
each = strtol(str + 2, end, 10);
|
|
|
|
if (each == 0 || each >= max - min) {
|
|
|
|
return luaL_error(L, "invalid spec (each %d)", each);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i <= (max - min); i++) {
|
2018-10-10 18:36:00 +02:00
|
|
|
if ((i % each) == 0) res |= (uint64_t)1 << i;
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32_t val;
|
|
|
|
while (1) {
|
|
|
|
val = strtol(str, end, 10);
|
|
|
|
if (val < min || val > max) {
|
|
|
|
return luaL_error(L, "invalid spec (val %d out of range %d..%d)", val, min, max);
|
|
|
|
}
|
2017-03-27 08:20:08 +02:00
|
|
|
res |= (uint64_t)1 << (val - min);
|
2016-12-26 14:19:12 +01:00
|
|
|
if (**end != ',') break;
|
|
|
|
str = *end + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) {
|
|
|
|
char *s = str;
|
|
|
|
desc->min = lcron_parsepart(L, s, &s, 0, 59);
|
2018-10-10 18:36:00 +02:00
|
|
|
if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
|
2016-12-26 14:19:12 +01:00
|
|
|
desc->hour = lcron_parsepart(L, s + 1, &s, 0, 23);
|
2018-10-10 18:36:00 +02:00
|
|
|
if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
|
2016-12-26 14:19:12 +01:00
|
|
|
desc->dom = lcron_parsepart(L, s + 1, &s, 1, 31);
|
2018-10-10 18:36:00 +02:00
|
|
|
if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
|
2016-12-26 14:19:12 +01:00
|
|
|
desc->mon = lcron_parsepart(L, s + 1, &s, 1, 12);
|
2018-10-10 18:36:00 +02:00
|
|
|
if (*s != ' ' && *s != '\t') return luaL_error(L, "invalid spec (separator @%d)", s - str);
|
2017-03-27 08:20:08 +02:00
|
|
|
desc->dow = lcron_parsepart(L, s + 1, &s, 0, 6);
|
2018-10-10 18:36:00 +02:00
|
|
|
while (*s != '\0' && (*s == ' ' || *s == '\t')) {
|
|
|
|
s++;
|
|
|
|
}
|
2016-12-26 14:19:12 +01:00
|
|
|
if (*s != 0) return luaL_error(L, "invalid spec (trailing @%d)", s - str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_create(lua_State *L) {
|
|
|
|
// Check arguments
|
|
|
|
char *strdesc = (char*)luaL_checkstring(L, 1);
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
// Grab the table of all entries onto the stack
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_table_ref);
|
|
|
|
|
|
|
|
// Find a free index
|
|
|
|
int ix = 1;
|
|
|
|
while(lua_rawgeti(L, -1, ix) != LUA_TNIL) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
ix++;
|
|
|
|
}
|
|
|
|
lua_pop(L, 1); // pop the nil off the stack
|
|
|
|
|
|
|
|
// Allocate userdata onto the stack
|
2016-12-26 14:19:12 +01:00
|
|
|
cronent_ud_t *ud = lua_newuserdata(L, sizeof(cronent_ud_t));
|
|
|
|
// Set metatable
|
|
|
|
luaL_getmetatable(L, "cron.entry");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
// Set callback
|
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
ud->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
// Set entry
|
2020-06-13 04:50:03 +02:00
|
|
|
lcron_parsedesc(L, strdesc, &ud->desc);
|
|
|
|
|
|
|
|
// Store entry to table while preserving userdata
|
|
|
|
lua_pushvalue(L, -1); // clone userdata
|
|
|
|
lua_rawseti(L, -3, ix); // -userdata; userdata, cronent table, cb, desc
|
|
|
|
|
|
|
|
return 1; // just the userdata
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
|
|
|
|
2020-06-13 04:50:03 +02:00
|
|
|
// Finds index, leaves table at top of stack for convenience
|
2016-12-26 14:19:12 +01:00
|
|
|
static size_t lcron_findindex(lua_State *L, cronent_ud_t *ud) {
|
2020-06-13 04:50:03 +02:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_table_ref);
|
|
|
|
size_t count = lua_objlen(L, -1);
|
|
|
|
|
|
|
|
for (size_t i = 1; i <= count; i++) {
|
|
|
|
lua_rawgeti(L, -1, i);
|
|
|
|
cronent_ud_t *eud = lua_touserdata(L, -1);
|
2016-12-26 14:19:12 +01:00
|
|
|
lua_pop(L, 1);
|
2020-06-13 04:50:03 +02:00
|
|
|
if (eud == ud) {
|
|
|
|
return i;
|
|
|
|
}
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
return count + 1;
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_schedule(lua_State *L) {
|
|
|
|
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
|
|
|
|
char *strdesc = (char*)luaL_checkstring(L, 2);
|
|
|
|
struct cronent_desc desc;
|
|
|
|
lcron_parsedesc(L, strdesc, &desc);
|
|
|
|
ud->desc = desc;
|
2020-06-13 04:50:03 +02:00
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
size_t i = lcron_findindex(L, ud);
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
lua_pushvalue(L, 1); // copy ud to top of stack
|
|
|
|
lua_rawseti(L, -2, i); // install into table
|
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_handler(lua_State *L) {
|
|
|
|
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_checktype(L, 2, LUA_TFUNCTION);
|
2016-12-26 14:19:12 +01:00
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
|
|
|
|
ud->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_unschedule(lua_State *L) {
|
|
|
|
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
|
|
|
|
size_t i = lcron_findindex(L, ud);
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_rawseti(L, -2, i);
|
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-06-13 04:50:03 +02:00
|
|
|
// scheduled entries are pinned, so we cannot arrive at the __gc metamethod
|
2016-12-26 14:19:12 +01:00
|
|
|
static int lcron_delete(lua_State *L) {
|
|
|
|
cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lcron_reset(lua_State *L) {
|
2020-06-13 04:50:03 +02:00
|
|
|
lua_newtable(L);
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, cronent_table_ref);
|
|
|
|
cronent_table_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cron_handle_time(uint8_t mon, uint8_t dom, uint8_t dow, uint8_t hour, uint8_t min) {
|
|
|
|
lua_State *L = lua_getstate();
|
|
|
|
struct cronent_desc desc;
|
|
|
|
desc.mon = (uint16_t)1 << (mon - 1);
|
|
|
|
desc.dom = (uint32_t)1 << (dom - 1);
|
|
|
|
desc.dow = ( uint8_t)1 << dow;
|
|
|
|
desc.hour = (uint32_t)1 << hour;
|
|
|
|
desc.min = (uint64_t)1 << min;
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_table_ref);
|
|
|
|
size_t count = lua_objlen(L, -1);
|
|
|
|
|
|
|
|
for (size_t i = 1; i <= count; i++) {
|
|
|
|
lua_rawgeti(L, -1, i);
|
2017-10-30 11:05:41 +01:00
|
|
|
cronent_ud_t *ent = lua_touserdata(L, -1);
|
2016-12-26 14:19:12 +01:00
|
|
|
lua_pop(L, 1);
|
2020-06-13 04:50:03 +02:00
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
if ((ent->desc.mon & desc.mon ) == 0) continue;
|
|
|
|
if ((ent->desc.dom & desc.dom ) == 0) continue;
|
|
|
|
if ((ent->desc.dow & desc.dow ) == 0) continue;
|
|
|
|
if ((ent->desc.hour & desc.hour) == 0) continue;
|
|
|
|
if ((ent->desc.min & desc.min ) == 0) continue;
|
2020-06-13 04:50:03 +02:00
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, ent->cb_ref);
|
2020-06-13 04:50:03 +02:00
|
|
|
lua_rawgeti(L, -2, i); // get ud again
|
2020-06-16 09:19:55 +02:00
|
|
|
luaL_pcallx(L, 1, 0);
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
lua_pop(L, 1); // pop table
|
2016-12-26 14:19:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cron_handle_tmr() {
|
|
|
|
struct rtc_timeval tv;
|
2017-08-24 21:17:31 +02:00
|
|
|
rtctime_gettimeofday(&tv);
|
2016-12-26 14:19:12 +01:00
|
|
|
if (tv.tv_sec == 0) { // Wait for RTC time
|
2018-04-13 21:41:14 +02:00
|
|
|
os_timer_arm(&cron_timer, 1000, 0);
|
2016-12-26 14:19:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
time_t t = tv.tv_sec;
|
|
|
|
struct tm tm;
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
uint32_t diff = 60000 - tm.tm_sec * 1000 - tv.tv_usec / 1000;
|
|
|
|
if (tm.tm_sec == 59) {
|
|
|
|
t++;
|
|
|
|
diff += 60000;
|
|
|
|
gmtime_r(&t, &tm);
|
|
|
|
}
|
2018-04-13 21:41:14 +02:00
|
|
|
os_timer_arm(&cron_timer, diff, 0);
|
2016-12-26 14:19:12 +01:00
|
|
|
cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min);
|
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
LROT_BEGIN(cronent, NULL, LROT_MASK_GC_INDEX)
|
|
|
|
LROT_FUNCENTRY( __gc, lcron_delete )
|
|
|
|
LROT_TABENTRY( __index, cronent )
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( schedule, lcron_schedule )
|
|
|
|
LROT_FUNCENTRY( handler, lcron_handler )
|
|
|
|
LROT_FUNCENTRY( unschedule, lcron_unschedule )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(cronent, NULL, LROT_MASK_GC_INDEX)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(cron, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( schedule, lcron_create )
|
|
|
|
LROT_FUNCENTRY( reset, lcron_reset )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(cron, NULL, 0)
|
2016-12-26 14:19:12 +01:00
|
|
|
|
2018-04-13 21:41:14 +02:00
|
|
|
#include "pm/swtimer.h"
|
2016-12-26 14:19:12 +01:00
|
|
|
|
|
|
|
int luaopen_cron( lua_State *L ) {
|
2018-04-13 21:41:14 +02:00
|
|
|
os_timer_disarm(&cron_timer);
|
|
|
|
os_timer_setfn(&cron_timer, cron_handle_tmr, 0);
|
|
|
|
SWTIMER_REG_CB(cron_handle_tmr, SWTIMER_RESTART);
|
|
|
|
//cron_handle_tmr determines when to execute a scheduled cron job
|
|
|
|
//My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer.
|
|
|
|
os_timer_arm(&cron_timer, 1000, 0);
|
2019-05-08 13:08:20 +02:00
|
|
|
luaL_rometatable(L, "cron.entry", LROT_TABLEREF(cronent));
|
2020-06-13 04:50:03 +02:00
|
|
|
|
|
|
|
cronent_table_ref = LUA_NOREF;
|
|
|
|
lcron_reset(L);
|
|
|
|
|
2016-12-26 14:19:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
NODEMCU_MODULE(CRON, "cron", cron, luaopen_cron);
|