Asterisk - The Open Source Telephony Project  21.4.1
Functions | Variables
pbx_timing.c File Reference

PBX timing routines. More...

#include "asterisk.h"
#include "asterisk/localtime.h"
#include "asterisk/logger.h"
#include "asterisk/pbx.h"
#include "asterisk/strings.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Functions

int ast_build_timing (struct ast_timing *i, const char *info_in)
 Construct a timing bitmap, for use in time-based conditionals. More...
 
int ast_check_timing (const struct ast_timing *i)
 Evaluate a pre-constructed bitmap as to whether the current time falls within the range specified. More...
 
int ast_check_timing2 (const struct ast_timing *i, const struct timeval tv)
 Evaluate a pre-constructed bitmap as to whether a particular time falls within the range specified. More...
 
int ast_destroy_timing (struct ast_timing *i)
 Deallocates memory structures associated with a timing bitmap. More...
 
static unsigned get_range (char *src, int max, const char *const names[], const char *msg)
 helper function to return a range up to max (7, 12, 31 respectively). names, if supplied, is an array of names that should be mapped to numbers.
 
static void get_timerange (struct ast_timing *i, char *times)
 store a bitmask of valid times, one bit each 1 minute
 
static int lookup_name (const char *s, const char *const names[], int max)
 Helper for get_range. return the index of the matching entry, starting from 1. If names is not supplied, try numeric values.
 

Variables

static const char *const days []
 
static const char *const months []
 

Detailed Description

PBX timing routines.

Author
Corey Farrell git@c.nosp@m.fwar.nosp@m.e.com

Definition in file pbx_timing.c.

Function Documentation

int ast_build_timing ( struct ast_timing i,
const char *  info_in 
)

Construct a timing bitmap, for use in time-based conditionals.

Parameters
iPointer to an ast_timing structure.
info_inStandard string containing a timerange, weekday range, monthday range, and month range, as well as an optional timezone.
Return values
1on success.
0on failure.

Definition at line 197 of file pbx_timing.c.

References ast_strdup, ast_strdupa, ast_timing::daymask, ast_timing::dowmask, get_range(), get_timerange(), ast_timing::monthmask, and ast_timing::timezone.

198 {
199  char *info;
200  int j, num_fields, last_sep = -1;
201 
202  i->timezone = NULL;
203 
204  /* Check for empty just in case */
205  if (ast_strlen_zero(info_in)) {
206  return 0;
207  }
208 
209  /* make a copy just in case we were passed a static string */
210  info = ast_strdupa(info_in);
211 
212  /* count the number of fields in the timespec */
213  for (j = 0, num_fields = 1; info[j] != '\0'; j++) {
214  if (info[j] == '|' || info[j] == ',') {
215  last_sep = j;
216  num_fields++;
217  }
218  }
219 
220  /* save the timezone, if it is specified */
221  if (num_fields == 5) {
222  i->timezone = ast_strdup(info + last_sep + 1);
223  }
224 
225  /* Assume everything except time */
226  i->monthmask = 0xfff; /* 12 bits */
227  i->daymask = 0x7fffffffU; /* 31 bits */
228  i->dowmask = 0x7f; /* 7 bits */
229  /* on each call, use strsep() to move info to the next argument */
230  get_timerange(i, strsep(&info, "|,"));
231  if (info)
232  i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week");
233  if (info)
234  i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day");
235  if (info)
236  i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month");
237  return 1;
238 }
unsigned int daymask
Definition: pbx.h:174
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
unsigned int monthmask
Definition: pbx.h:173
static void get_timerange(struct ast_timing *i, char *times)
store a bitmask of valid times, one bit each 1 minute
Definition: pbx_timing.c:108
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
static unsigned get_range(char *src, int max, const char *const names[], const char *msg)
helper function to return a range up to max (7, 12, 31 respectively). names, if supplied, is an array of names that should be mapped to numbers.
Definition: pbx_timing.c:65
unsigned int dowmask
Definition: pbx.h:175
char * timezone
Definition: pbx.h:177
int ast_check_timing ( const struct ast_timing i)

Evaluate a pre-constructed bitmap as to whether the current time falls within the range specified.

Parameters
iPointer to an ast_timing structure.
Return values
1if the time matches.
0if the current time falls outside of the specified range.

Definition at line 240 of file pbx_timing.c.

References ast_check_timing2(), and ast_tvnow().

241 {
242  return ast_check_timing2(i, ast_tvnow());
243 }
int ast_check_timing2(const struct ast_timing *i, const struct timeval tv)
Evaluate a pre-constructed bitmap as to whether a particular time falls within the range specified...
Definition: pbx_timing.c:245
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159
int ast_check_timing2 ( const struct ast_timing i,
const struct timeval  tv 
)

Evaluate a pre-constructed bitmap as to whether a particular time falls within the range specified.

Parameters
iPointer to an ast_timing structure.
tvSpecified time
Return values
1if the time matches.
0if the time falls outside of the specified range.

Definition at line 245 of file pbx_timing.c.

References ast_localtime(), ast_timing::daymask, ast_timing::dowmask, ast_timing::minmask, ast_timing::monthmask, ast_timing::timezone, ast_tm::tm_hour, ast_tm::tm_mday, ast_tm::tm_min, ast_tm::tm_mon, and ast_tm::tm_wday.

Referenced by ast_check_timing().

246 {
247  struct ast_tm tm;
248 
249  ast_localtime(&tv, &tm, i->timezone);
250 
251  /* If it's not the right month, return */
252  if (!(i->monthmask & (1 << tm.tm_mon)))
253  return 0;
254 
255  /* If it's not that time of the month.... */
256  /* Warning, tm_mday has range 1..31! */
257  if (!(i->daymask & (1 << (tm.tm_mday-1))))
258  return 0;
259 
260  /* If it's not the right day of the week */
261  if (!(i->dowmask & (1 << tm.tm_wday)))
262  return 0;
263 
264  /* Sanity check the hour just to be safe */
265  if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) {
266  ast_log(LOG_WARNING, "Insane time...\n");
267  return 0;
268  }
269 
270  /* Now the tough part, we calculate if it fits
271  in the right time based on min/hour */
272  if (!(i->minmask[tm.tm_hour * 2 + (tm.tm_min >= 30 ? 1 : 0)] & (1 << (tm.tm_min >= 30 ? tm.tm_min - 30 : tm.tm_min))))
273  return 0;
274 
275  /* If we got this far, then we're good */
276  return 1;
277 }
unsigned int daymask
Definition: pbx.h:174
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1739
unsigned int minmask[48]
Definition: pbx.h:176
unsigned int monthmask
Definition: pbx.h:173
unsigned int dowmask
Definition: pbx.h:175
char * timezone
Definition: pbx.h:177
int ast_destroy_timing ( struct ast_timing i)

Deallocates memory structures associated with a timing bitmap.

Parameters
iPointer to an ast_timing structure.
Return values
0success
non-zerofailure (number suitable to pass to
See also
strerror)

Definition at line 279 of file pbx_timing.c.

References ast_timing::timezone.

Referenced by include_free().

280 {
281  if (i->timezone) {
282  ast_free(i->timezone);
283  i->timezone = NULL;
284  }
285  return 0;
286 }
char * timezone
Definition: pbx.h:177