1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSImplClassicClock |
---|
7 | * |
---|
8 | * @brief This source file contains the implementation of |
---|
9 | * _TOD_Validate(). |
---|
10 | */ |
---|
11 | |
---|
12 | /* |
---|
13 | * COPYRIGHT (c) 1989-2007. |
---|
14 | * On-Line Applications Research Corporation (OAR). |
---|
15 | * |
---|
16 | * Redistribution and use in source and binary forms, with or without |
---|
17 | * modification, are permitted provided that the following conditions |
---|
18 | * are met: |
---|
19 | * 1. Redistributions of source code must retain the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer. |
---|
21 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
22 | * notice, this list of conditions and the following disclaimer in the |
---|
23 | * documentation and/or other materials provided with the distribution. |
---|
24 | * |
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
35 | * POSSIBILITY OF SUCH DAMAGE. |
---|
36 | */ |
---|
37 | |
---|
38 | #ifdef HAVE_CONFIG_H |
---|
39 | #include "config.h" |
---|
40 | #endif |
---|
41 | |
---|
42 | #include <rtems/rtems/clockimpl.h> |
---|
43 | #include <rtems/score/todimpl.h> |
---|
44 | #include <rtems/config.h> |
---|
45 | |
---|
46 | /* |
---|
47 | * The following array contains the number of days in all months. |
---|
48 | * The first dimension should be 0 for leap years, and 1 otherwise. |
---|
49 | * The second dimension should range from 1 to 12 for January to |
---|
50 | * December, respectively. |
---|
51 | */ |
---|
52 | static const uint32_t _TOD_Days_per_month[ 2 ][ 13 ] = { |
---|
53 | { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, |
---|
54 | { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } |
---|
55 | }; |
---|
56 | |
---|
57 | rtems_status_code _TOD_Validate( |
---|
58 | const rtems_time_of_day *the_tod, |
---|
59 | TOD_Ticks_validation ticks_validation |
---|
60 | ) |
---|
61 | { |
---|
62 | size_t leap_year_index; |
---|
63 | uint32_t days_in_month; |
---|
64 | uint32_t ticks_per_second; |
---|
65 | uint32_t ticks_mask; |
---|
66 | |
---|
67 | if ( the_tod == NULL ) { |
---|
68 | return RTEMS_INVALID_ADDRESS; |
---|
69 | } |
---|
70 | |
---|
71 | ticks_per_second = rtems_clock_get_ticks_per_second(); |
---|
72 | ticks_mask = (uint32_t) ticks_validation; |
---|
73 | |
---|
74 | if ( ( the_tod->ticks & ticks_mask ) >= ticks_per_second ) { |
---|
75 | return RTEMS_INVALID_CLOCK; |
---|
76 | } |
---|
77 | |
---|
78 | if ( the_tod->second >= TOD_SECONDS_PER_MINUTE ) { |
---|
79 | return RTEMS_INVALID_CLOCK; |
---|
80 | } |
---|
81 | |
---|
82 | if ( the_tod->minute >= TOD_MINUTES_PER_HOUR ) { |
---|
83 | return RTEMS_INVALID_CLOCK; |
---|
84 | } |
---|
85 | |
---|
86 | if ( the_tod->hour >= TOD_HOURS_PER_DAY ) { |
---|
87 | return RTEMS_INVALID_CLOCK; |
---|
88 | } |
---|
89 | |
---|
90 | if ( the_tod->month == 0 || the_tod->month > TOD_MONTHS_PER_YEAR ) { |
---|
91 | return RTEMS_INVALID_CLOCK; |
---|
92 | } |
---|
93 | |
---|
94 | if ( the_tod->year < TOD_BASE_YEAR || the_tod->year > TOD_LATEST_YEAR ) { |
---|
95 | return RTEMS_INVALID_CLOCK; |
---|
96 | } |
---|
97 | |
---|
98 | if ( the_tod->day == 0 ) { |
---|
99 | return RTEMS_INVALID_CLOCK; |
---|
100 | } |
---|
101 | |
---|
102 | leap_year_index = _TOD_Get_leap_year_index( the_tod->year ); |
---|
103 | days_in_month = _TOD_Days_per_month[ leap_year_index ][ the_tod->month ]; |
---|
104 | |
---|
105 | if ( the_tod->day > days_in_month ) { |
---|
106 | return RTEMS_INVALID_CLOCK; |
---|
107 | } |
---|
108 | |
---|
109 | return RTEMS_SUCCESSFUL; |
---|
110 | } |
---|