1 //******************************************************************
3 // Copyright 2014 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "iotivity_config.h"
53 #define TIMEOUT_USED 1
54 #define TIMEOUT_UNUSED 2
57 pthread_t thread_id = 0; // 0: initial thread id (meaningless)
63 time_t timeout_seconds;
66 } timeout_list[TIMEOUTS];
69 * Return the number of seconds between before and after, (after - before).
70 * This must be async-signal safe, so it cannot use difftime().
72 time_t timespec_diff(const time_t after, const time_t before)
74 return after - before;
78 * Add positive seconds to a timespec, nothing if seconds is negative.
80 void timespec_add(time_t * to, const time_t seconds)
82 if (to && seconds > 0)
90 long int getSeconds(struct tm* tp)
92 long int nInterval = 0;
94 nInterval = (tp->tm_hour * SECS_PER_HOUR);
95 nInterval += (tp->tm_min * SECS_PER_MIN);
96 nInterval += (tp->tm_sec * SECOND);
98 printf("%ld", nInterval);
103 long int getRelativeSecondsOfDayofweek(int ia, int ib)
106 return (((long int)(7 - (ib - ia))) * SECS_PER_DAY);
108 return (((long int)((ib - ia))) * SECS_PER_DAY);
111 long int getRelativeIntervalOfWeek(struct tm* tp)
114 struct tm* current, *midnight;
115 time_t delayed_time = 0;
118 current = localtime(¤t_time);
122 printf("ERROR; Getting local time fails\n");
126 midnight = (struct tm* )malloc(sizeof(struct tm));
130 printf("ERROR; Memory allocation fails\n");
134 memcpy(midnight, current, sizeof(struct tm));
136 midnight->tm_hour = 0;
137 midnight->tm_min = 0;
138 midnight->tm_sec = 0;
141 // Seconds from midnight.
142 delayed_time = current_time - mktime(midnight);
143 delayed_time = getRelativeSecondsOfDayofweek(current->tm_wday, tp->tm_wday) - delayed_time;
144 delayed_time = delayed_time + getSeconds(tp);
151 long int getSecondsFromAbsTime(struct tm* tp)
154 time_t delayed_time = 0;
157 localtime(¤t_time);
159 delayed_time = mktime(tp) - current_time;
164 time_t registerTimer(const time_t seconds, int *id, void *cb)
178 // get the current time
181 for (idx = 0; idx < TIMEOUTS; ++idx)
182 if (!((timeout_list[idx].timeout_state & TIMEOUT_USED) & TIMEOUT_USED))
185 if (TIMEOUTS == idx) // reach to end of timer list
188 // idx th timeout will be used.
189 // Reset and set state of the timer
190 timeout_list[idx].timeout_state = 0;
191 timeout_list[idx].timeout_state |= TIMEOUT_USED;
193 // calculate when the timeout should fire
195 timespec_add(&then, seconds);
197 timeout_list[idx].timeout_time = then;
198 timeout_list[idx].timeout_seconds = seconds;
200 // printf( "\nbefore timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
201 timeout_list[idx].cb = cb;
202 // printf( " after timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
204 // How long till the next timeout?
206 for (i = 0; i < TIMEOUTS; i++)
208 if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED)) == TIMEOUT_USED)
210 const time_t secs = timespec_diff(timeout_list[i].timeout_time, now);
212 if (secs >= 0 && secs < next)
218 /* Return the timeout number. */
219 return timeout_list[idx].timeout_time;
222 void unregisterTimer(int idx)
224 if( 0 <= idx && idx < TIMEOUTS)
225 timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
235 /* Check all timeouts that are used and armed, but not passed yet. */
236 for (i = 0; i < TIMEOUTS; i++)
238 if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED)) == TIMEOUT_USED)
240 const time_t seconds = timespec_diff(timeout_list[i].timeout_time, now);
244 /* timeout [i] fires! */
245 timeout_list[i].timeout_state = TIMEOUT_UNUSED;
246 if (timeout_list[i].cb)
248 timeout_list[i].cb();
255 void *loop(void *threadid)
269 int res = pthread_create(&thread_id, NULL, loop, NULL);
273 printf("ERROR; return code from pthread_create() is %d\n", res);
279 #else // WITH_ARDUINO
280 time_t timeToSecondsFromNow(tmElements_t *t_then)
285 then = makeTime((*t_then));
287 return (time_t) (then - t);
290 time_t registerTimer(const time_t seconds, int *id, void (*cb)())
299 // get the current time
302 for (idx = 0; idx < TIMEOUTS; ++idx)
303 if (!((timeout_list[idx].timeout_state & TIMEOUT_USED) & TIMEOUT_USED))
306 if (TIMEOUTS == idx)// reach to end of timer list
309 // idx th timeout will be used.
310 // Reset and set state of the timer
311 timeout_list[idx].timeout_state = 0;
312 timeout_list[idx].timeout_state |= TIMEOUT_USED;
314 // calculate when the timeout should fire
316 timespec_add(&then, seconds);
318 timeout_list[idx].timeout_time = then;
319 timeout_list[idx].timeout_seconds = seconds;
321 // printf( "\nbefore timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
322 timeout_list[idx].cb = cb;
323 // printf( " after timeout_list[idx].cb = %X\n", timeout_list[idx].cb);
325 // How long till the next timeout?
327 for (i = 0; i < TIMEOUTS; i++)
329 if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED))
332 const time_t secs = timespec_diff(timeout_list[i].timeout_time,
335 if (secs >= 0 && secs < next)
341 /* Return the timeout number. */
342 return timeout_list[idx].timeout_time;
345 void unregisterTimer(int idx)
347 if( 0 <= idx && idx < TIMEOUTS)
348 timeout_list[idx].timeout_state = TIMEOUT_UNUSED;
358 /* Check all timeouts that are used and armed, but not passed yet. */
359 for (i = 0; i < TIMEOUTS; i++)
361 if ((timeout_list[i].timeout_state & (TIMEOUT_USED | TIMEOUT_UNUSED))
364 const time_t seconds = timespec_diff(timeout_list[i].timeout_time,
369 /* timeout [i] fires! */
370 timeout_list[i].timeout_state = TIMEOUT_UNUSED;
371 if (timeout_list[i].cb)
373 timeout_list[i].cb();