12
12
#include "rtc/rtctime.h"
13
13
#include "mem.h"
14
14
15
+ static const char * CRON_ENTRY_METATABLE = "cron.entry" ;
16
+
17
+ #if 0
18
+ # define cron_dbg (...) printf(__VA_ARGS__)
19
+ #else
20
+ # define cron_dbg (...)
21
+ #endif
22
+
15
23
struct cronent_desc {
16
24
uint64_t min ; // Minutes repeat - bits 0-59
17
25
uint32_t hour ; // Hours repeat - bits 0-23
@@ -51,15 +59,14 @@ static uint64_t lcron_parsepart(lua_State *L, char *str, char **end, uint8_t min
51
59
}
52
60
} else {
53
61
uint32_t val ;
54
- while ( 1 ) {
62
+ do {
55
63
val = strtol (str , end , 10 );
56
64
if (val < min || val > max ) {
57
65
return luaL_error (L , "invalid spec (val %d out of range %d..%d)" , val , min , max );
58
66
}
59
67
res |= (uint64_t )1 << (val - min );
60
- if (* * end != ',' ) break ;
61
68
str = * end + 1 ;
62
- }
69
+ } while ( * * end == ',' );
63
70
}
64
71
return res ;
65
72
}
@@ -101,7 +108,7 @@ static int lcron_create(lua_State *L) {
101
108
// Allocate userdata onto the stack
102
109
cronent_ud_t * ud = lua_newuserdata (L , sizeof (cronent_ud_t ));
103
110
// Set metatable
104
- luaL_getmetatable (L , "cron.entry" );
111
+ luaL_getmetatable (L , CRON_ENTRY_METATABLE );
105
112
lua_setmetatable (L , -2 );
106
113
// Set callback
107
114
lua_pushvalue (L , 2 );
@@ -134,7 +141,7 @@ static size_t lcron_findindex(lua_State *L, cronent_ud_t *ud) {
134
141
}
135
142
136
143
static int lcron_schedule (lua_State * L ) {
137
- cronent_ud_t * ud = luaL_checkudata (L , 1 , "cron.entry" );
144
+ cronent_ud_t * ud = luaL_checkudata (L , 1 , CRON_ENTRY_METATABLE );
138
145
char * strdesc = (char * )luaL_optstring (L , 2 , NULL );
139
146
140
147
if (strdesc != NULL ) {
@@ -145,14 +152,16 @@ static int lcron_schedule(lua_State *L) {
145
152
146
153
size_t i = lcron_findindex (L , ud );
147
154
155
+ cron_dbg ("cron: schedule %p at index %d\n" , ud , i );
156
+
148
157
lua_pushvalue (L , 1 ); // copy ud to top of stack
149
158
lua_rawseti (L , -2 , i ); // install into table
150
159
151
160
return 0 ;
152
161
}
153
162
154
163
static int lcron_handler (lua_State * L ) {
155
- cronent_ud_t * ud = luaL_checkudata (L , 1 , "cron.entry" );
164
+ cronent_ud_t * ud = luaL_checkudata (L , 1 , CRON_ENTRY_METATABLE );
156
165
luaL_checktype (L , 2 , LUA_TFUNCTION );
157
166
lua_pushvalue (L , 2 );
158
167
luaL_unref (L , LUA_REGISTRYINDEX , ud -> cb_ref );
@@ -161,9 +170,11 @@ static int lcron_handler(lua_State *L) {
161
170
}
162
171
163
172
static int lcron_unschedule (lua_State * L ) {
164
- cronent_ud_t * ud = luaL_checkudata (L , 1 , "cron.entry" );
173
+ cronent_ud_t * ud = luaL_checkudata (L , 1 , CRON_ENTRY_METATABLE );
165
174
size_t i = lcron_findindex (L , ud );
166
175
176
+ cron_dbg ("cron: unschedule %p at index %d\n" , ud , i );
177
+
167
178
lua_pushnil (L );
168
179
lua_rawseti (L , -2 , i );
169
180
@@ -172,7 +183,7 @@ static int lcron_unschedule(lua_State *L) {
172
183
173
184
// scheduled entries are pinned, so we cannot arrive at the __gc metamethod
174
185
static int lcron_delete (lua_State * L ) {
175
- cronent_ud_t * ud = luaL_checkudata (L , 1 , "cron.entry" );
186
+ cronent_ud_t * ud = luaL_checkudata (L , 1 , CRON_ENTRY_METATABLE );
176
187
luaL_unref (L , LUA_REGISTRYINDEX , ud -> cb_ref );
177
188
return 0 ;
178
189
}
@@ -182,6 +193,8 @@ static int lcron_reset(lua_State *L) {
182
193
luaL_unref (L , LUA_REGISTRYINDEX , cronent_table_ref );
183
194
cronent_table_ref = luaL_ref (L , LUA_REGISTRYINDEX );
184
195
196
+ cron_dbg ("cron: cronent_table_ref is %d\n" , cronent_table_ref );
197
+
185
198
return 0 ;
186
199
}
187
200
@@ -198,10 +211,14 @@ static void cron_handle_time(uint8_t mon, uint8_t dom, uint8_t dow, uint8_t hour
198
211
size_t count = lua_objlen (L , -1 );
199
212
200
213
for (size_t i = 1 ; i <= count ; i ++ ) {
214
+ cron_dbg ("cron: handle_time index %d (of %d; top %d)\n" , i , count , lua_gettop (L ));
215
+
201
216
lua_rawgeti (L , -1 , i );
202
217
cronent_ud_t * ent = lua_touserdata (L , -1 );
203
218
lua_pop (L , 1 );
204
219
220
+ cron_dbg (" ... is %p\n" , ent );
221
+
205
222
if ((ent -> desc .mon & desc .mon ) == 0 ) continue ;
206
223
if ((ent -> desc .dom & desc .dom ) == 0 ) continue ;
207
224
if ((ent -> desc .dow & desc .dow ) == 0 ) continue ;
@@ -261,7 +278,7 @@ int luaopen_cron( lua_State *L ) {
261
278
//cron_handle_tmr determines when to execute a scheduled cron job
262
279
//My guess: To be sure to give the other modules required by cron enough time to get to a ready state, restart cron_timer.
263
280
os_timer_arm (& cron_timer , 1000 , 0 );
264
- luaL_rometatable (L , "cron.entry" , LROT_TABLEREF (cronent ));
281
+ luaL_rometatable (L , CRON_ENTRY_METATABLE , LROT_TABLEREF (cronent ));
265
282
266
283
cronent_table_ref = LUA_NOREF ;
267
284
lcron_reset (L );
0 commit comments