libamxp  1.4.0
Patterns C Implementation
amxp_scheduler.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or without modification,
8 ** are permitted provided that the following conditions are met:
9 **
10 ** 1. Redistributions of source code must retain the above copyright notice,
11 ** this list of conditions and the following disclaimer.
12 **
13 ** 2. Redistributions in binary form must reproduce the above copyright notice,
14 ** this list of conditions and the following disclaimer in the documentation
15 ** and/or other materials provided with the distribution.
16 **
17 ** Subject to the terms and conditions of this license, each copyright holder
18 ** and contributor hereby grants to those receiving rights under this license
19 ** a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable
20 ** (except for failure to satisfy the conditions of this license) patent license
21 ** to make, have made, use, offer to sell, sell, import, and otherwise transfer
22 ** this software, where such license applies only to those patent claims, already
23 ** acquired or hereafter acquired, licensable by such copyright holder or contributor
24 ** that are necessarily infringed by:
25 **
26 ** (a) their Contribution(s) (the licensed copyrights of copyright holders and
27 ** non-copyrightable additions of contributors, in source or binary form) alone;
28 ** or
29 **
30 ** (b) combination of their Contribution(s) with the work of authorship to which
31 ** such Contribution(s) was added by such copyright holder or contributor, if,
32 ** at the time the Contribution is added, such addition causes such combination
33 ** to be necessarily infringed. The patent license shall not apply to any other
34 ** combinations which include the Contribution.
35 **
36 ** Except as expressly stated above, no rights or licenses from any copyright
37 ** holder or contributor is granted under this license, whether expressly, by
38 ** implication, estoppel or otherwise.
39 **
40 ** DISCLAIMER
41 **
42 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
43 ** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 ** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
46 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
48 ** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
49 ** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50 ** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
51 ** USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 **
53 ****************************************************************************/
54 
55 #if !defined(__AMXP_SCHEDULER_H__)
56 #define __AMXP_SCHEDULER_H__
57 
58 #ifdef __cplusplus
59 extern "C"
60 {
61 #endif
62 
63 #include <stdbool.h>
64 #include <stdint.h>
65 
66 #include <amxp/amxp_signal.h>
67 #include <amxp/amxp_slot.h>
68 #include <amxp/amxp_timer.h>
69 #include <amxp/amxp_cron.h>
70 
71 #ifndef __AMXC_LLIST_H__
72 #error "Missing include <amxc/amxc_llist.h>"
73 #endif
74 
75 #ifndef __AMXC_HTABLE_H__
76 #error "Missing include <amxc/amxc_htable.h>"
77 #endif
78 
126 typedef struct _scheduler_item {
129  uint32_t duration;
131  amxc_ts_t next;
133  bool enabled;
134  amxc_llist_it_t lit;
135  amxc_htable_it_t hit;
137 
170 typedef struct _scheduler {
174  amxc_htable_t items;
175  amxc_llist_t ordered_items;
177 
209 int amxp_scheduler_new(amxp_scheduler_t** scheduler);
210 
230 void amxp_scheduler_delete(amxp_scheduler_t** scheduler);
231 
263 int amxp_scheduler_init(amxp_scheduler_t* scheduler);
264 
281 void amxp_scheduler_clean(amxp_scheduler_t* scheduler);
282 
305 int amxp_scheduler_enable(amxp_scheduler_t* scheduler, bool enable);
306 
322 int amxp_scheduler_use_local_time(amxp_scheduler_t* scheduler, bool use_local_time);
323 
341 
376  const char* id,
377  amxp_slot_fn_t fn,
378  void* priv);
379 
401 int amxp_scheduler_disconnect(amxp_scheduler_t* scheduler, const char* id, amxp_slot_fn_t fn);
402 
440  const char* id,
441  const char* cron_expr,
442  uint32_t duration);
443 
482  const char* id,
483  const char* cron_begin,
484  const char* cron_end);
542  const char* id,
543  const char* time,
544  const char* days_of_week,
545  uint32_t duration);
546 
607  const char* id,
608  const char* start_time,
609  const char* end_time,
610  const char* days_of_week);
611 
632  const char* id);
633 
652  const char* id,
653  bool enable);
654 
655 
670 
671 #ifdef __cplusplus
672 }
673 #endif
674 
675 #endif // __AMXP_SCHEDULER_H__
Ambiorix cron expression parser.
Ambiorix signal manager and signal API header file.
Ambiorix slot API header file.
Ambiorix timer API header file.
int amxp_scheduler_connect(amxp_scheduler_t *scheduler, const char *id, amxp_slot_fn_t fn, void *priv)
Connects a callback function to the scheduler.
int amxp_scheduler_use_local_time(amxp_scheduler_t *scheduler, bool use_local_time)
Use local time or UTC time in calculation for next trigger times.
int amxp_scheduler_set_weekly_begin_end_item(amxp_scheduler_t *scheduler, const char *id, const char *start_time, const char *end_time, const char *days_of_week)
Adds a schedule item or updates a schedule item using a start time, end time and list of week days.
int amxp_scheduler_enable_item(amxp_scheduler_t *scheduler, const char *id, bool enable)
Enables or disable a schedule item.
struct _scheduler_item amxp_scheduler_item_t
Structure containing a schedule item.
int amxp_scheduler_new(amxp_scheduler_t **scheduler)
Allocates a amxp_scheduler_t structures and initializes to an empty scheduler.
int amxp_scheduler_set_weekly_item(amxp_scheduler_t *scheduler, const char *id, const char *time, const char *days_of_week, uint32_t duration)
Adds a schedule item or updates a schedule item using a time and list of week days.
void amxp_scheduler_delete(amxp_scheduler_t **scheduler)
Frees the previously allocated amxp_scheduler_t structure.
int amxp_scheduler_disconnect(amxp_scheduler_t *scheduler, const char *id, amxp_slot_fn_t fn)
Disconnects a callback function from the scheduler.
int amxp_scheduler_remove_item(amxp_scheduler_t *scheduler, const char *id)
Removes a schedule item from the scheduler.
amxp_signal_mngr_t * amxp_scheduler_get_sigmngr(amxp_scheduler_t *scheduler)
Gets the signal manager of a scheduler.
int amxp_scheduler_enable(amxp_scheduler_t *scheduler, bool enable)
Enables or disable the scheduler.
int amxp_scheduler_set_cron_begin_end_item(amxp_scheduler_t *scheduler, const char *id, const char *cron_begin, const char *cron_end)
Adds a schedule item or updates a schedule item using a cron expressions.
int amxp_scheduler_set_cron_item(amxp_scheduler_t *scheduler, const char *id, const char *cron_expr, uint32_t duration)
Adds a schedule item or updates a schedule item using a cron expression.
struct _scheduler amxp_scheduler_t
Structure containing a scheduler.
int amxp_scheduler_init(amxp_scheduler_t *scheduler)
Initializes a amxp_scheduler_t to an empty scheduler.
int amxp_scheduler_update(amxp_scheduler_t *scheduler)
Forces recalculation of the schedule items' next occurrence time.
void amxp_scheduler_clean(amxp_scheduler_t *scheduler)
Cleans the scheduler.
void(* amxp_slot_fn_t)(const char *const sig_name, const amxc_var_t *const data, void *const priv)
Slot callback function signature.
Definition: amxp_slot.h:93
Structure containing the signal manager information.
Definition: amxp_signal.h:103
The timer type.
Definition: amxp_timer.h:163
Structure containing parsed cron expression.
Definition: amxp_cron.h:103
Structure containing a schedule item.
amxp_cron_expr_t end_cron
amxc_llist_it_t lit
amxp_cron_expr_t cron
amxc_htable_it_t hit
amxp_timer_t * timer
Structure containing a scheduler.
amxc_htable_t items
amxp_signal_mngr_t sigmngr
amxp_timer_t * timer
bool use_local_time
amxc_llist_t ordered_items