cron.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Cron module
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "lmem.h"
  5. #include "user_interface.h"
  6. #include "c_types.h"
  7. #include "c_string.h"
  8. #include "ets_sys.h"
  9. #include "time.h"
  10. #include "rtc/rtctime.h"
  11. #include "stdlib.h"
  12. #include "mem.h"
  13. struct cronent_desc {
  14. uint64_t min; // Minutes repeat - bits 0-59
  15. uint32_t hour; // Hours repeat - bits 0-23
  16. uint32_t dom; // Day-of-month repeat - bits 0-30
  17. uint16_t mon; // Monthly repeat - bits 0-11
  18. uint8_t dow; // Day-of-week repeat - bits 0-6
  19. };
  20. typedef struct cronent_ud {
  21. struct cronent_desc desc;
  22. int cb_ref;
  23. } cronent_ud_t;
  24. static ETSTimer cron_timer;
  25. static int *cronent_list = 0;
  26. static size_t cronent_count = 0;
  27. static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min, uint8_t max) {
  28. uint64_t res = 0;
  29. if (str[0] == '*') {
  30. uint32_t each = 1;
  31. *end = str + 1;
  32. if (str[1] == '/') {
  33. each = strtol(str + 2, end, 10);
  34. if (end != 0)
  35. if (each == 0 || each >= max - min) {
  36. return luaL_error(L, "invalid spec (each %d)", each);
  37. }
  38. }
  39. for (int i = 0; i <= (max - min); i++) {
  40. if (((min + i) % each) == 0) res |= (uint64_t)1 << i;
  41. }
  42. } else {
  43. uint32_t val;
  44. while (1) {
  45. val = strtol(str, end, 10);
  46. if (val < min || val > max) {
  47. return luaL_error(L, "invalid spec (val %d out of range %d..%d)", val, min, max);
  48. }
  49. res |= (uint64_t)1 << (val - min);
  50. if (**end != ',') break;
  51. str = *end + 1;
  52. }
  53. }
  54. return res;
  55. }
  56. static int lcron_parsedesc(lua_State *L, char *str, struct cronent_desc *desc) {
  57. char *s = str;
  58. desc->min = lcron_parsepart(L, s, &s, 0, 59);
  59. if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str);
  60. desc->hour = lcron_parsepart(L, s + 1, &s, 0, 23);
  61. if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str);
  62. desc->dom = lcron_parsepart(L, s + 1, &s, 1, 31);
  63. if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str);
  64. desc->mon = lcron_parsepart(L, s + 1, &s, 1, 12);
  65. if (*s != ' ') return luaL_error(L, "invalid spec (separator @%d)", s - str);
  66. desc->dow = lcron_parsepart(L, s + 1, &s, 0, 6);
  67. if (*s != 0) return luaL_error(L, "invalid spec (trailing @%d)", s - str);
  68. return 0;
  69. }
  70. static int lcron_create(lua_State *L) {
  71. // Check arguments
  72. char *strdesc = (char*)luaL_checkstring(L, 1);
  73. luaL_checkanyfunction(L, 2);
  74. // Parse description
  75. struct cronent_desc desc;
  76. lcron_parsedesc(L, strdesc, &desc);
  77. // Allocate userdata
  78. cronent_ud_t *ud = lua_newuserdata(L, sizeof(cronent_ud_t));
  79. // Set metatable
  80. luaL_getmetatable(L, "cron.entry");
  81. lua_setmetatable(L, -2);
  82. // Set callback
  83. lua_pushvalue(L, 2);
  84. ud->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  85. // Set entry
  86. ud->desc = desc;
  87. // Store entry
  88. lua_pushvalue(L, -1);
  89. cronent_list = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1));
  90. cronent_list[cronent_count++] = luaL_ref(L, LUA_REGISTRYINDEX);
  91. return 1;
  92. }
  93. static size_t lcron_findindex(lua_State *L, cronent_ud_t *ud) {
  94. cronent_ud_t *eud;
  95. size_t i;
  96. for (i = 0; i < cronent_count; i++) {
  97. lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_list[i]);
  98. eud = lua_touserdata(L, -1);
  99. lua_pop(L, 1);
  100. if (eud == ud) break;
  101. }
  102. if (i == cronent_count) return -1;
  103. return i;
  104. }
  105. static int lcron_schedule(lua_State *L) {
  106. cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
  107. char *strdesc = (char*)luaL_checkstring(L, 2);
  108. struct cronent_desc desc;
  109. lcron_parsedesc(L, strdesc, &desc);
  110. ud->desc = desc;
  111. size_t i = lcron_findindex(L, ud);
  112. if (i == -1) {
  113. lua_pushvalue(L, 1);
  114. cronent_list = os_realloc(cronent_list, sizeof(int) * (cronent_count + 1));
  115. cronent_list[cronent_count++] = lua_ref(L, LUA_REGISTRYINDEX);
  116. }
  117. return 0;
  118. }
  119. static int lcron_handler(lua_State *L) {
  120. cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
  121. luaL_checkanyfunction(L, 2);
  122. lua_pushvalue(L, 2);
  123. luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
  124. ud->cb_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  125. return 0;
  126. }
  127. static int lcron_unschedule(lua_State *L) {
  128. cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
  129. size_t i = lcron_findindex(L, ud);
  130. if (i == -1) return 0;
  131. luaL_unref(L, LUA_REGISTRYINDEX, cronent_list[i]);
  132. memmove(cronent_list + i, cronent_list + i + 1, sizeof(int) * (cronent_count - i - 1));
  133. cronent_count--;
  134. return 0;
  135. }
  136. static int lcron_delete(lua_State *L) {
  137. cronent_ud_t *ud = luaL_checkudata(L, 1, "cron.entry");
  138. lcron_unschedule(L);
  139. luaL_unref(L, LUA_REGISTRYINDEX, ud->cb_ref);
  140. return 0;
  141. }
  142. static int lcron_reset(lua_State *L) {
  143. for (size_t i = 0; i < cronent_count; i++) {
  144. luaL_unref(L, LUA_REGISTRYINDEX, cronent_list[i]);
  145. }
  146. cronent_count = 0;
  147. os_free(cronent_list);
  148. cronent_list = 0;
  149. return 0;
  150. }
  151. static void cron_handle_time(uint8_t mon, uint8_t dom, uint8_t dow, uint8_t hour, uint8_t min) {
  152. lua_State *L = lua_getstate();
  153. struct cronent_desc desc;
  154. desc.mon = (uint16_t)1 << (mon - 1);
  155. desc.dom = (uint32_t)1 << (dom - 1);
  156. desc.dow = ( uint8_t)1 << dow;
  157. desc.hour = (uint32_t)1 << hour;
  158. desc.min = (uint64_t)1 << min;
  159. for (size_t i = 0; i < cronent_count; i++) {
  160. lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_list[i]);
  161. cronent_ud_t *ent = lua_touserdata(L, -1);
  162. lua_pop(L, 1);
  163. if ((ent->desc.mon & desc.mon ) == 0) continue;
  164. if ((ent->desc.dom & desc.dom ) == 0) continue;
  165. if ((ent->desc.dow & desc.dow ) == 0) continue;
  166. if ((ent->desc.hour & desc.hour) == 0) continue;
  167. if ((ent->desc.min & desc.min ) == 0) continue;
  168. lua_rawgeti(L, LUA_REGISTRYINDEX, ent->cb_ref);
  169. lua_rawgeti(L, LUA_REGISTRYINDEX, cronent_list[i]);
  170. lua_call(L, 1, 0);
  171. }
  172. }
  173. static void cron_handle_tmr() {
  174. struct rtc_timeval tv;
  175. rtctime_gettimeofday(&tv);
  176. if (tv.tv_sec == 0) { // Wait for RTC time
  177. ets_timer_arm_new(&cron_timer, 1000, 0, 1);
  178. return;
  179. }
  180. time_t t = tv.tv_sec;
  181. struct tm tm;
  182. gmtime_r(&t, &tm);
  183. uint32_t diff = 60000 - tm.tm_sec * 1000 - tv.tv_usec / 1000;
  184. if (tm.tm_sec == 59) {
  185. t++;
  186. diff += 60000;
  187. gmtime_r(&t, &tm);
  188. }
  189. ets_timer_arm_new(&cron_timer, diff, 0, 1);
  190. cron_handle_time(tm.tm_mon + 1, tm.tm_mday, tm.tm_wday, tm.tm_hour, tm.tm_min);
  191. }
  192. static const LUA_REG_TYPE cronent_map[] = {
  193. { LSTRKEY( "schedule" ), LFUNCVAL( lcron_schedule ) },
  194. { LSTRKEY( "handler" ), LFUNCVAL( lcron_handler ) },
  195. { LSTRKEY( "unschedule" ), LFUNCVAL( lcron_unschedule ) },
  196. { LSTRKEY( "__gc" ), LFUNCVAL( lcron_delete ) },
  197. { LSTRKEY( "__index" ), LROVAL( cronent_map ) },
  198. { LNILKEY, LNILVAL }
  199. };
  200. static const LUA_REG_TYPE cron_map[] = {
  201. { LSTRKEY( "schedule" ), LFUNCVAL( lcron_create ) },
  202. { LSTRKEY( "reset" ), LFUNCVAL( lcron_reset ) },
  203. { LNILKEY, LNILVAL }
  204. };
  205. int luaopen_cron( lua_State *L ) {
  206. ets_timer_disarm(&cron_timer);
  207. ets_timer_setfn(&cron_timer, cron_handle_tmr, 0);
  208. ets_timer_arm_new(&cron_timer, 1000, 0, 1);
  209. luaL_rometatable(L, "cron.entry", (void *)cronent_map);
  210. return 0;
  211. }
  212. NODEMCU_MODULE(CRON, "cron", cron_map, luaopen_cron);