/*-
 * Copyright (c) 2003 Andrey Simonenko
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *  @(#)$Id: ipa_time.h,v 1.2.2.2 2006/03/23 00:56:11 simon Exp $
 */

#ifndef IPA_TIME_H
#define IPA_TIME_H

#define UPDATE_TIME_DEF		(1 * SECONDS_IN_MINUTE)	/* update_time default value. */
#define WAKEUP_TIME_DEF		(10 * SECONDS_IN_MINUTE)/* wakeup_time default value. */
#define SENSITIVE_TIME_DEF	(SECONDS_IN_MINUTE / 2)	/* sensitive_time default value. */

#define ACTIVE_FLAG		1	/* Something is active. */
#define INACTIVE_FLAG		0	/* Something is inactive. */
#define UNKNOWN_ACTIVE_FLAG	-1	/* Unknown state. */
#define IS_ACTIVE(x)	((x)->is_active == ACTIVE_FLAG)
#define IS_INACTIVE(x)	((x)->is_active == INACTIVE_FLAG)

#define TIME_TO_SEC(tm)	((tm)->tm_hour * SECONDS_IN_HOUR + (tm)->tm_min * SECONDS_IN_MINUTE + (tm)->tm_sec)

/*
 * If time of scheduled event is less than current time
 * no more than FAKE_TIME_DELTA, then fake current time.
 * To disable this feature, set this macro to zero.
 */
#ifndef FAKE_TIME_DELTA
# define FAKE_TIME_DELTA	5
#endif

/*
 * One time event.
 */
struct tevent {
	SLIST_ENTRY(tevent) link;
	u_int	event_sec;		/* Next event time. */
	u_int	event_step;		/* Interval between events. */
};

#define TEVENT_NSIZE	10
#define TEVENT_NALLOC	10

/*
 * List of all time events.
 */
SLIST_HEAD(tevents_list, tevent);

/*
 * One time interval in "worktime" parameter in seconds
 * starting from midnight.
 */
struct tint {
	STAILQ_ENTRY(tint) link;
	u_int		sec1;		/* From sec1. */
	u_int		sec2;		/* Till sec2. */
};

#define TINT_NSIZE	20
#define TINT_NALLOC	20

/*
 * List of all time intervals for one day.
 */
STAILQ_HEAD(tint_list, tint);

/*
 * List of all tint_lists.
 */
struct tint_set {
	STAILQ_ENTRY(tint_set) link;	/* For list building of all tint_lists. */
	struct tint_list list;		/* One tint_list. */
};

STAILQ_HEAD(tint_sets, tint_set);

/*
 * One worktime.
 */
struct worktime {
	SLIST_ENTRY(worktime) link;	/* For list of all worktimes. */
	const struct tint_list *tint_list[DAYS_IN_WEEK];
	const struct tint *curtint;	/* Current time interval. */
	u_int		active_sec;	/* Time when to make worktime active. */
	u_int		inactive_sec;	/* Time when to make worktime inactive. */
	int		is_active;	/* 1, if worktime is active. */
};

#define WORKTIME_NSIZE	5
#define WORKTIME_NALLOC	5

/*
 * List of all worktimes.
 */
SLIST_HEAD(worktimes_list, worktime);

#ifdef WITH_LIMITS
/*
 * [+<X>] <time> or <time> +<X> like parameter value.
 */
struct texp {
	u_int		seconds;	/* Number of seconds. */
	int		side;		/* 0 if +X <time>, 1 if <time> +X. */
	char		upto;		/* TEXP_UPTO_xxx. */
};

#define TEXP_UPTO_NOTSET	'-'
#define TEXP_UPTO_SIMPLE	' '
#define TEXP_UPTO_MINUTE	'm'
#define TEXP_UPTO_HOUR		'h'
#define TEXP_UPTO_DAY		'D'
#define TEXP_UPTO_WEEK		'W'
#define TEXP_UPTO_MONTH		'M'

#endif /* WITH_LIMITS */

#define BUF_SEC_SIZE	9	/* xx:xx:xx */
#define BUF_TIME_SIZE	17	/* Max buffer length for UINT_MAX value. */

extern u_int	wakeup_time;
extern u_int	freeze_time;
extern u_int	sleep_after_dump;
extern u_int	sensitive_time;
extern int	debug_time;
extern int	debug_worktime;

extern const struct worktime *global_worktime;
extern struct worktime worktime_default;

extern const struct tevent *global_update_tevent;
extern const struct tevent *global_append_tevent;

extern const char *const wdays[];

extern const char *const active_msg[];

extern ipa_mzone *tint_mzone;
extern struct tint_sets tint_sets;

extern ipa_mzone *worktime_mzone;
extern struct worktimes_list worktimes_list;

extern ipa_mzone *tevent_mzone;
extern struct tevents_list tevents_list;

extern ipa_tm	curdate;
extern u_int	cursec;
extern int	newday_flag;
extern u_int	curwday;

#ifdef WITH_ANY_LIMITS
extern char	*ipa_tm_buf;
#endif

extern char	*tdiff_buf;

extern void	sec_to_time(u_int, ipa_tm *);
extern const char *time_to_buf(u_int);
extern const char *tdiff_to_buf(double);
extern const char *sec_to_buf(u_int);
extern const char *tint_to_buf(const struct tint *);

extern void	free_tint_set(struct tint_set *);
extern void	free_worktimes(void);
extern void	init_worktime_default(void);
extern const struct worktime *find_worktime(struct worktime *);

extern u_int	worktimes_check_sec;

extern void	worktimes_newday(int);
extern void	worktimes_check(void);

#ifdef WITH_LIMITS
extern int	set_wday(ipa_tm *);
extern void	ipa_tm_addtime(ipa_tm *, u_int);
extern void	ipa_tm_texp(ipa_tm *, const struct texp *);
#endif

#ifdef WITH_THRESHOLDS
extern void	ipa_tm_subtime(ipa_tm *, u_int);
#endif

#ifdef WITH_ANY_LIMITS
extern const char *ipa_tm_to_buf(const ipa_tm *);
extern int	cmp_ipa_tm(const ipa_tm *, const ipa_tm *);
extern u_int	ipa_tm_diff(const ipa_tm *, const ipa_tm *);
extern int	check_ipa_tm(const ipa_tm *);
extern void	fix_240000(ipa_tm *);
extern int	check_worktime_subset(const struct worktime *, const struct worktime *);
#endif

#endif /* !IPA_TIME_H */


syntax highlighted by Code2HTML, v. 0.9.1