Asterisk - The Open Source Telephony Project  18.5.0
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"
Include dependency graph for pbx_timing.c:

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. More...
 
static void get_timerange (struct ast_timing *i, char *times)
 store a bitmask of valid times, one bit each 1 minute More...
 
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. More...
 

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

◆ ast_build_timing()

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

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

/brief Build timing

/param i info /param info_in

Definition at line 203 of file pbx_timing.c.

References ast_strdup, ast_strdupa, ast_strlen_zero, ast_timing::daymask, days, ast_timing::dowmask, get_range(), get_timerange(), sip_to_pjsip::info(), ast_timing::monthmask, months, NULL, strsep(), and ast_timing::timezone.

204 {
205  char *info;
206  int j, num_fields, last_sep = -1;
207 
208  i->timezone = NULL;
209 
210  /* Check for empty just in case */
211  if (ast_strlen_zero(info_in)) {
212  return 0;
213  }
214 
215  /* make a copy just in case we were passed a static string */
216  info = ast_strdupa(info_in);
217 
218  /* count the number of fields in the timespec */
219  for (j = 0, num_fields = 1; info[j] != '\0'; j++) {
220  if (info[j] == ',') {
221  last_sep = j;
222  num_fields++;
223  }
224  }
225 
226  /* save the timezone, if it is specified */
227  if (num_fields == 5) {
228  i->timezone = ast_strdup(info + last_sep + 1);
229  }
230 
231  /* Assume everything except time */
232  i->monthmask = 0xfff; /* 12 bits */
233  i->daymask = 0x7fffffffU; /* 31 bits */
234  i->dowmask = 0x7f; /* 7 bits */
235  /* on each call, use strsep() to move info to the next argument */
236  get_timerange(i, strsep(&info, "|,"));
237  if (info)
238  i->dowmask = get_range(strsep(&info, "|,"), 7, days, "day of week");
239  if (info)
240  i->daymask = get_range(strsep(&info, "|,"), 31, NULL, "day");
241  if (info)
242  i->monthmask = get_range(strsep(&info, "|,"), 12, months, "month");
243  return 1;
244 }
unsigned int daymask
Definition: pbx.h:174
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
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:300
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
static const char *const months[]
Definition: pbx_timing.c:180
unsigned int dowmask
Definition: pbx.h:175
def info(msg)
static const char *const days[]
Definition: pbx_timing.c:168
char * strsep(char **str, const char *delims)
char * timezone
Definition: pbx.h:177

◆ ast_check_timing()

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
Returns1, if the time matches or 0, if the current time falls outside of the specified range.

Definition at line 246 of file pbx_timing.c.

References ast_check_timing2(), and ast_tvnow().

247 {
248  return ast_check_timing2(i, ast_tvnow());
249 }
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:251
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150

◆ ast_check_timing2()

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
Returns1, if the time matches or 0, if the time falls outside of the specified range.

Definition at line 251 of file pbx_timing.c.

References ast_localtime(), ast_log, ast_timing::daymask, ast_timing::dowmask, LOG_WARNING, 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(), and pbx_builtin_gotoiftime().

252 {
253  struct ast_tm tm;
254 
255  ast_localtime(&tv, &tm, i->timezone);
256 
257  /* If it's not the right month, return */
258  if (!(i->monthmask & (1 << tm.tm_mon)))
259  return 0;
260 
261  /* If it's not that time of the month.... */
262  /* Warning, tm_mday has range 1..31! */
263  if (!(i->daymask & (1 << (tm.tm_mday-1))))
264  return 0;
265 
266  /* If it's not the right day of the week */
267  if (!(i->dowmask & (1 << tm.tm_wday)))
268  return 0;
269 
270  /* Sanity check the hour just to be safe */
271  if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) {
272  ast_log(LOG_WARNING, "Insane time...\n");
273  return 0;
274  }
275 
276  /* Now the tough part, we calculate if it fits
277  in the right time based on min/hour */
278  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))))
279  return 0;
280 
281  /* If we got this far, then we're good */
282  return 1;
283 }
unsigned int daymask
Definition: pbx.h:174
#define LOG_WARNING
Definition: logger.h:274
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
#define ast_log
Definition: astobj2.c:42
unsigned int dowmask
Definition: pbx.h:175
char * timezone
Definition: pbx.h:177

◆ ast_destroy_timing()

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 285 of file pbx_timing.c.

References ast_free, NULL, and ast_timing::timezone.

Referenced by iftime(), include_free(), pbx_builtin_execiftime(), and pbx_builtin_gotoiftime().

286 {
287  if (i->timezone) {
288  ast_free(i->timezone);
289  i->timezone = NULL;
290  }
291  return 0;
292 }
#define NULL
Definition: resample.c:96
#define ast_free(a)
Definition: astmm.h:182
char * timezone
Definition: pbx.h:177

◆ get_range()

static unsigned get_range ( char *  src,
int  max,
const char *const  names[],
const char *  msg 
)
static

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 at line 65 of file pbx_timing.c.

References ast_log, ast_strlen_zero, end, LOG_WARNING, lookup_name(), and strsep().

Referenced by ast_build_timing().

66 {
67  int start, end; /* start and ending position */
68  unsigned int mask = 0;
69  char *part;
70 
71  /* Check for whole range */
72  if (ast_strlen_zero(src) || !strcmp(src, "*")) {
73  return (1 << max) - 1;
74  }
75 
76  while ((part = strsep(&src, "&"))) {
77  /* Get start and ending position */
78  char *endpart = strchr(part, '-');
79  if (endpart) {
80  *endpart++ = '\0';
81  }
82  /* Find the start */
83  if ((start = lookup_name(part, names, max)) < 0) {
84  ast_log(LOG_WARNING, "Invalid %s '%s', skipping element\n", msg, part);
85  continue;
86  }
87  if (endpart) { /* find end of range */
88  if ((end = lookup_name(endpart, names, max)) < 0) {
89  ast_log(LOG_WARNING, "Invalid end %s '%s', skipping element\n", msg, endpart);
90  continue;
91  }
92  } else {
93  end = start;
94  }
95  /* Fill the mask. Remember that ranges are cyclic */
96  mask |= (1 << end); /* initialize with last element */
97  while (start != end) {
98  mask |= (1 << start);
99  if (++start >= max) {
100  start = 0;
101  }
102  }
103  }
104  return mask;
105 }
#define LOG_WARNING
Definition: logger.h:274
char * end
Definition: eagi_proxy.c:73
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
char * strsep(char **str, const char *delims)
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 suppli...
Definition: pbx_timing.c:42
#define max(a, b)
Definition: f2c.h:198

◆ get_timerange()

static void get_timerange ( struct ast_timing i,
char *  times 
)
static

store a bitmask of valid times, one bit each 1 minute

Definition at line 108 of file pbx_timing.c.

References ast_log, ast_strlen_zero, LOG_WARNING, ast_timing::minmask, and strsep().

Referenced by ast_build_timing().

109 {
110  char *endpart, *part;
111  int x;
112  int st_h, st_m;
113  int endh, endm;
114  int minute_start, minute_end;
115 
116  /* start disabling all times, fill the fields with 0's, as they may contain garbage */
117  memset(i->minmask, 0, sizeof(i->minmask));
118 
119  /* 1-minute per bit */
120  /* Star is all times */
121  if (ast_strlen_zero(times) || !strcmp(times, "*")) {
122  /* 48, because each hour takes 2 integers; 30 bits each */
123  for (x = 0; x < 48; x++) {
124  i->minmask[x] = 0x3fffffff; /* 30 bits */
125  }
126  return;
127  }
128  /* Otherwise expect a range */
129  while ((part = strsep(&times, "&"))) {
130  if (!(endpart = strchr(part, '-'))) {
131  if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) {
132  ast_log(LOG_WARNING, "%s isn't a valid time.\n", part);
133  continue;
134  }
135  i->minmask[st_h * 2 + (st_m >= 30 ? 1 : 0)] |= (1 << (st_m % 30));
136  continue;
137  }
138  *endpart++ = '\0';
139  /* why skip non digits? Mostly to skip spaces */
140  while (*endpart && !isdigit(*endpart)) {
141  endpart++;
142  }
143  if (!*endpart) {
144  ast_log(LOG_WARNING, "Invalid time range starting with '%s-'.\n", part);
145  continue;
146  }
147  if (sscanf(part, "%2d:%2d", &st_h, &st_m) != 2 || st_h < 0 || st_h > 23 || st_m < 0 || st_m > 59) {
148  ast_log(LOG_WARNING, "'%s' isn't a valid start time.\n", part);
149  continue;
150  }
151  if (sscanf(endpart, "%2d:%2d", &endh, &endm) != 2 || endh < 0 || endh > 23 || endm < 0 || endm > 59) {
152  ast_log(LOG_WARNING, "'%s' isn't a valid end time.\n", endpart);
153  continue;
154  }
155  minute_start = st_h * 60 + st_m;
156  minute_end = endh * 60 + endm;
157  /* Go through the time and enable each appropriate bit */
158  for (x = minute_start; x != minute_end; x = (x + 1) % (24 * 60)) {
159  i->minmask[x / 30] |= (1 << (x % 30));
160  }
161  /* Do the last one */
162  i->minmask[x / 30] |= (1 << (x % 30));
163  }
164  /* All done */
165  return;
166 }
#define LOG_WARNING
Definition: logger.h:274
unsigned int minmask[48]
Definition: pbx.h:176
#define ast_strlen_zero(foo)
Definition: strings.h:52
#define ast_log
Definition: astobj2.c:42
char * strsep(char **str, const char *delims)

◆ lookup_name()

static int lookup_name ( const char *  s,
const char *const  names[],
int  max 
)
static

Helper for get_range. return the index of the matching entry, starting from 1. If names is not supplied, try numeric values.

Definition at line 42 of file pbx_timing.c.

Referenced by get_range().

43 {
44  int i;
45 
46  if (names && *s > '9') {
47  for (i = 0; names[i]; i++) {
48  if (!strcasecmp(s, names[i])) {
49  return i;
50  }
51  }
52  }
53 
54  /* Allow months and weekdays to be specified as numbers, as well */
55  if (sscanf(s, "%2d", &i) == 1 && i >= 1 && i <= max) {
56  /* What the array offset would have been: "1" would be at offset 0 */
57  return i - 1;
58  }
59  return -1; /* error return */
60 }
#define max(a, b)
Definition: f2c.h:198

Variable Documentation

◆ days

const char* const days[]
static

Definition at line 168 of file pbx_timing.c.

Referenced by ast_build_timing().

◆ months

const char* const months[]
static

Definition at line 180 of file pbx_timing.c.

Referenced by ast_build_timing().