Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:53

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup ScoreTimecounterValInstall
0007  */
0008 
0009 /*
0010  * Copyright (C) 2021 embedded brains GmbH & Co. KG
0011  *
0012  * Redistribution and use in source and binary forms, with or without
0013  * modification, are permitted provided that the following conditions
0014  * are met:
0015  * 1. Redistributions of source code must retain the above copyright
0016  *    notice, this list of conditions and the following disclaimer.
0017  * 2. Redistributions in binary form must reproduce the above copyright
0018  *    notice, this list of conditions and the following disclaimer in the
0019  *    documentation and/or other materials provided with the distribution.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0022  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0025  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0026  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0027  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0028  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0029  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0030  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0031  * POSSIBILITY OF SUCH DAMAGE.
0032  */
0033 
0034 /*
0035  * This file is part of the RTEMS quality process and was automatically
0036  * generated.  If you find something that needs to be fixed or
0037  * worded better please post a report or patch to an RTEMS mailing list
0038  * or raise a bug report:
0039  *
0040  * https://www.rtems.org/bugs.html
0041  *
0042  * For information on updating and regenerating please refer to the How-To
0043  * section in the Software Requirements Engineering chapter of the
0044  * RTEMS Software Engineering manual.  The manual is provided as a part of
0045  * a release.  For development sources please refer to the online
0046  * documentation at:
0047  *
0048  * https://docs.rtems.org
0049  */
0050 
0051 #ifdef HAVE_CONFIG_H
0052 #include "config.h"
0053 #endif
0054 
0055 #include <rtems.h>
0056 #include <rtems/timecounter.h>
0057 #include <rtems/score/atomic.h>
0058 #include <rtems/score/threaddispatch.h>
0059 
0060 #include "tx-support.h"
0061 
0062 #include <rtems/test.h>
0063 
0064 /**
0065  * @defgroup ScoreTimecounterValInstall spec:/score/timecounter/val/install
0066  *
0067  * @ingroup TestsuitesValidationTimecounter0
0068  *
0069  * @brief Tests timecounter installation related functions and directives of
0070  *   the Clock Manager.
0071  *
0072  * This test case performs the following actions:
0073  *
0074  * - Call the simple timecounter tick service with a zero delta and offset.
0075  *   This will lead to an overflow to zero of the timehand generation.  It
0076  *   shall not change the initial clock values.
0077  *
0078  * - Call the directives to get the initial value of CLOCK_REALTIME and the
0079  *   initial boot time.
0080  *
0081  *   - Check the initial CLOCK_REALTIME in seconds and nanoseconds format.
0082  *
0083  *   - Check that CLOCK_REALTIME is frozen in seconds and nanoseconds format.
0084  *
0085  *   - Check the initial CLOCK_REALTIME in coarse resolution in seconds and
0086  *     nanoseconds format.
0087  *
0088  *   - Check that CLOCK_REALTIME is frozen in coarse resolution in seconds and
0089  *     nanoseconds format.
0090  *
0091  *   - Check the initial CLOCK_REALTIME in binary time format.
0092  *
0093  *   - Check that CLOCK_REALTIME is frozen in binary time format.
0094  *
0095  *   - Check the initial CLOCK_REALTIME in coarse resolution in binary time
0096  *     format.
0097  *
0098  *   - Check that CLOCK_REALTIME is frozen in coarse resolution in binary time
0099  *     format.
0100  *
0101  *   - Check the initial CLOCK_REALTIME in seconds and microseconds format.
0102  *
0103  *   - Check that CLOCK_REALTIME is frozen in seconds and microseconds format.
0104  *
0105  *   - Check the initial CLOCK_REALTIME in coarse resolution in seconds and
0106  *     microseconds format.
0107  *
0108  *   - Check that CLOCK_REALTIME is frozen in coarse resolution in seconds and
0109  *     microseconds format.
0110  *
0111  *   - Check the initial boot time in seconds and nanoseconds format.
0112  *
0113  *   - Check the initial boot time in binary time format.
0114  *
0115  *   - Check the initial boot time in seconds and microseconds format.
0116  *
0117  * - Call the directives to get the initial value of CLOCK_MONOTONIC and the
0118  *   initial boot time.
0119  *
0120  *   - Check the initial CLOCK_MONOTONIC in seconds and nanoseconds format.
0121  *
0122  *   - Check that CLOCK_MONOTONIC is frozen in seconds and nanoseconds format.
0123  *
0124  *   - Check the initial CLOCK_MONOTONIC in coarse resolution in seconds and
0125  *     nanoseconds format.
0126  *
0127  *   - Check that CLOCK_MONOTONIC is frozen in coarse resolution in seconds and
0128  *     nanoseconds format.
0129  *
0130  *   - Check the initial CLOCK_MONOTONIC in binary time format.
0131  *
0132  *   - Check that CLOCK_MONOTONIC is frozen in binary time format.
0133  *
0134  *   - Check the initial CLOCK_MONOTONIC in coarse resolution in binary time
0135  *     format.
0136  *
0137  *   - Check that CLOCK_MONOTONIC is frozen in coarse resolution in binary time
0138  *     format.
0139  *
0140  *   - Check the initial CLOCK_MONOTONIC in signed binary time format.
0141  *
0142  *   - Check that CLOCK_MONOTONIC is frozen in signed binary time format.
0143  *
0144  *   - Check the initial CLOCK_MONOTONIC in seconds and microseconds format.
0145  *
0146  *   - Check that CLOCK_MONOTONIC is frozen in seconds and microseconds format.
0147  *
0148  *   - Check the initial CLOCK_MONOTONIC in coarse resolution in seconds and
0149  *     microseconds format.
0150  *
0151  *   - Check that CLOCK_MONOTONIC is frozen in coarse resolution in seconds and
0152  *     microseconds format.
0153  *
0154  * - Install timecounter of different quality levels and frequencies.
0155  *
0156  *   - Install a timecounter with a high quality level and normal frequency.
0157  *     Check that it was installed.
0158  *
0159  *   - Install a timecounter with a high quality level and low frequency. Check
0160  *     that it was not installed.
0161  *
0162  *   - Install a timecounter with a high quality level and high frequency.
0163  *     Check that it was installed.
0164  *
0165  *   - Install a timecounter with a low quality level.  Check that it was not
0166  *     installed.
0167  *
0168  * - Call the directives to get the time in the highest resolution available to
0169  *   the system.
0170  *
0171  *   - Check that the timecounter was used by rtems_clock_get_realtime().
0172  *
0173  *   - Check that the timecounter was used by
0174  *     rtems_clock_get_realtime_bintime().
0175  *
0176  *   - Check that the timecounter was used by
0177  *     rtems_clock_get_realtime_timeval().
0178  *
0179  *   - Check that the timecounter was used by rtems_clock_get_monotonic().
0180  *
0181  *   - Check that the timecounter was used by
0182  *     rtems_clock_get_monotonic_bintime().
0183  *
0184  *   - Check that the timecounter was used by
0185  *     rtems_clock_get_monotonic_sbintime().
0186  *
0187  *   - Check that the timecounter was used by
0188  *     rtems_clock_get_monotonic_timeval().
0189  *
0190  * - Call the directives to get the time in a coarse resolution.
0191  *
0192  *   - Check that the timecounter was not used by
0193  *     rtems_clock_get_realtime_coarse().
0194  *
0195  *   - Check that the timecounter was not used by
0196  *     rtems_clock_get_realtime_coarse_bintime().
0197  *
0198  *   - Check that the timecounter was not used by
0199  *     rtems_clock_get_realtime_coarse_timeval().
0200  *
0201  *   - Check that the timecounter was not used by
0202  *     rtems_clock_get_monotonic_coarse().
0203  *
0204  *   - Check that the timecounter was not used by
0205  *     rtems_clock_get_monotonic_coarse_bintime().
0206  *
0207  *   - Check that the timecounter was not used by
0208  *     rtems_clock_get_monotonic_coarse_timeval().
0209  *
0210  *   - Check that the timecounter was not used by rtems_clock_get_boot_time().
0211  *
0212  *   - Check that the timecounter was not used by
0213  *     rtems_clock_get_boot_time_bintime().
0214  *
0215  *   - Check that the timecounter was not used by
0216  *     rtems_clock_get_boot_time_timeval().
0217  *
0218  * - Call the directives to get the time in the highest resolution available to
0219  *   the system.
0220  *
0221  *   - Prepare the timecounter to get a large time difference.  Check that
0222  *     rtems_clock_get_realtime() returns the correct time.
0223  *
0224  *   - Prepare the timecounter to get a large time difference.  Check that
0225  *     rtems_clock_get_realtime_bintime() returns the correct time.
0226  *
0227  *   - Prepare the timecounter to get a large time difference.  Check that
0228  *     rtems_clock_get_realtime_timeval() returns the correct time.
0229  *
0230  *   - Prepare the timecounter to get a large time difference.  Check that
0231  *     rtems_clock_get_monotonic() returns the correct time.
0232  *
0233  *   - Prepare the timecounter to get a large time difference.  Check that
0234  *     rtems_clock_get_monotonic_bintime() returns the correct time.
0235  *
0236  *   - Prepare the timecounter to get a large time difference.  Check that
0237  *     rtems_clock_get_monotonic_sbintime() returns the correct time.
0238  *
0239  *   - Prepare the timecounter to get a large time difference.  Check that
0240  *     rtems_clock_get_monotonic_timeval() returns the correct time.
0241  *
0242  * - Update the oldest timehand after a large time interval.
0243  *
0244  * - Call the simple timecounter tick service with non-zero delta and offset
0245  *   parameter values so that exactly one second passed.
0246  *
0247  *   - Check that exactly one second passed due to the simple clock tick
0248  *     service.
0249  *
0250  * - Install a very high quality timecounter with a low frequency to test the
0251  *   NTP support.
0252  *
0253  *   - Let the seconds value of CLOCK_REALTIME not change. Check that the NTP
0254  *     update second handler is not called.
0255  *
0256  *   - Let the seconds value of CLOCK_REALTIME change by one. Check that the
0257  *     NTP update second handler is called exactly once.
0258  *
0259  *   - Let the seconds value of CLOCK_REALTIME change by 200. Check that the
0260  *     NTP update second handler is called exactly 200 times.
0261  *
0262  *   - Let the seconds value of CLOCK_REALTIME change by 201. Check that the
0263  *     NTP update second handler is called exactly twice.
0264  *
0265  *   - Let the seconds value of CLOCK_REALTIME change by one. Check that the
0266  *     NTP update second handler is incremented the CLOCK_REALTIME by one
0267  *     second.
0268  *
0269  *   - Let the seconds value of CLOCK_REALTIME change by one. Check that the
0270  *     NTP update second handler is decremented the CLOCK_REALTIME by one
0271  *     second.
0272  *
0273  *   - Let the seconds value of CLOCK_REALTIME change by one. Check that the
0274  *     NTP update second handler increased the timecounter frequency.
0275  *
0276  *   - Let the seconds value of CLOCK_REALTIME change by one. Check that the
0277  *     NTP update second handler decreased the timecounter frequency.
0278  *
0279  * @{
0280  */
0281 
0282 typedef struct {
0283   struct timecounter base;
0284   Atomic_Ulong counter;
0285 } Timecounter;
0286 
0287 static Timecounter high_quality_low_frequency;
0288 
0289 static Timecounter high_quality_normal_frequency;
0290 
0291 static Timecounter high_quality_high_frequency;
0292 
0293 static Timecounter low_quality;
0294 
0295 static Timecounter very_high_quality;
0296 
0297 static uint32_t ntp_counter;
0298 
0299 static uint32_t GetTimecount( struct timecounter *base )
0300 {
0301   Timecounter *tc;
0302 
0303   tc = (Timecounter *) base;
0304 
0305   return (uint32_t) _Atomic_Fetch_add_ulong(
0306     &tc->counter,
0307     1,
0308     ATOMIC_ORDER_RELAXED
0309   );
0310 }
0311 
0312 static uint32_t GetCounter( const Timecounter *tc )
0313 {
0314   return (uint32_t) _Atomic_Load_ulong(
0315     &tc->counter,
0316     ATOMIC_ORDER_RELAXED
0317   );
0318 }
0319 
0320 static void SetCounter( Timecounter *tc, uint32_t counter )
0321 {
0322   _Atomic_Store_ulong(
0323     &tc->counter,
0324     counter,
0325     ATOMIC_ORDER_RELAXED
0326   );
0327 }
0328 
0329 static void NtpUpdateCounter( int64_t *adjustment, time_t *newsec )
0330 {
0331   (void) newsec;
0332   T_eq_i64( *adjustment, 0 );
0333   ++ntp_counter;
0334 }
0335 
0336 static void NtpUpdateSecondIncrement( int64_t *adjustment, time_t *newsec )
0337 {
0338   (void) adjustment;
0339   ++(*newsec);
0340 }
0341 
0342 static void NtpUpdateSecondDecrement( int64_t *adjustment, time_t *newsec )
0343 {
0344   (void) adjustment;
0345   --(*newsec);
0346 }
0347 
0348 static void NtpUpdateAdjustmentFaster( int64_t *adjustment, time_t *newsec )
0349 {
0350   *adjustment = ( (int64_t) 5000 ) << 32;
0351   (void) newsec;
0352 }
0353 
0354 static void NtpUpdateAdjustmentSlower( int64_t *adjustment, time_t *newsec )
0355 {
0356   *adjustment = -( (int64_t) 5000 ) << 32;
0357   (void) newsec;
0358 }
0359 
0360 static void CallTimecounterTick( void )
0361 {
0362   Per_CPU_Control *cpu_self;
0363 
0364   cpu_self = _Thread_Dispatch_disable();
0365   rtems_timecounter_tick();
0366   _Thread_Dispatch_enable( cpu_self );
0367 }
0368 
0369 /**
0370  * @brief Call the simple timecounter tick service with a zero delta and
0371  *   offset. This will lead to an overflow to zero of the timehand generation.
0372  *   It shall not change the initial clock values.
0373  */
0374 static void ScoreTimecounterValInstall_Action_0( void )
0375 {
0376   ISR_lock_Context lock_context;
0377 
0378   _Timecounter_Acquire( &lock_context );
0379   _Timecounter_Tick_simple( 0, 0, &lock_context );
0380 }
0381 
0382 /**
0383  * @brief Call the directives to get the initial value of CLOCK_REALTIME and
0384  *   the initial boot time.
0385  */
0386 static void ScoreTimecounterValInstall_Action_1( void )
0387 {
0388   struct bintime  bt;
0389   struct timespec ts;
0390   struct timeval  tv;
0391 
0392   /*
0393    * Check the initial CLOCK_REALTIME in seconds and nanoseconds format.
0394    */
0395   rtems_clock_get_realtime( &ts );
0396   T_eq_i64( ts.tv_sec, 567993600 );
0397   T_eq_u64( ts.tv_nsec, 0 );
0398 
0399   /*
0400    * Check that CLOCK_REALTIME is frozen in seconds and nanoseconds format.
0401    */
0402   rtems_clock_get_realtime( &ts );
0403   T_eq_i64( ts.tv_sec, 567993600 );
0404   T_eq_u64( ts.tv_nsec, 0 );
0405 
0406   /*
0407    * Check the initial CLOCK_REALTIME in coarse resolution in seconds and
0408    * nanoseconds format.
0409    */
0410   rtems_clock_get_realtime_coarse( &ts );
0411   T_eq_i64( ts.tv_sec, 567993600 );
0412   T_eq_u64( ts.tv_nsec, 0 );
0413 
0414   /*
0415    * Check that CLOCK_REALTIME is frozen in coarse resolution in seconds and
0416    * nanoseconds format.
0417    */
0418   rtems_clock_get_realtime_coarse( &ts );
0419   T_eq_i64( ts.tv_sec, 567993600 );
0420   T_eq_u64( ts.tv_nsec, 0 );
0421 
0422   /*
0423    * Check the initial CLOCK_REALTIME in binary time format.
0424    */
0425   rtems_clock_get_realtime_bintime( &bt );
0426   T_eq_i64( bt.sec, 567993600 );
0427   T_eq_u64( bt.frac, 0 );
0428 
0429   /*
0430    * Check that CLOCK_REALTIME is frozen in binary time format.
0431    */
0432   rtems_clock_get_realtime_bintime( &bt );
0433   T_eq_i64( bt.sec, 567993600 );
0434   T_eq_u64( bt.frac, 0 );
0435 
0436   /*
0437    * Check the initial CLOCK_REALTIME in coarse resolution in binary time
0438    * format.
0439    */
0440   rtems_clock_get_realtime_coarse_bintime( &bt );
0441   T_eq_i64( bt.sec, 567993600 );
0442   T_eq_u64( bt.frac, 0 );
0443 
0444   /*
0445    * Check that CLOCK_REALTIME is frozen in coarse resolution in binary time
0446    * format.
0447    */
0448   rtems_clock_get_realtime_coarse_bintime( &bt );
0449   T_eq_i64( bt.sec, 567993600 );
0450   T_eq_u64( bt.frac, 0 );
0451 
0452   /*
0453    * Check the initial CLOCK_REALTIME in seconds and microseconds format.
0454    */
0455   rtems_clock_get_realtime_timeval( &tv );
0456   T_eq_i64( tv.tv_sec, 567993600 );
0457   T_eq_long( tv.tv_usec, 0 );
0458 
0459   /*
0460    * Check that CLOCK_REALTIME is frozen in seconds and microseconds format.
0461    */
0462   rtems_clock_get_realtime_timeval( &tv );
0463   T_eq_i64( tv.tv_sec, 567993600 );
0464   T_eq_long( tv.tv_usec, 0 );
0465 
0466   /*
0467    * Check the initial CLOCK_REALTIME in coarse resolution in seconds and
0468    * microseconds format.
0469    */
0470   rtems_clock_get_realtime_coarse_timeval( &tv );
0471   T_eq_i64( tv.tv_sec, 567993600 );
0472   T_eq_long( tv.tv_usec, 0 );
0473 
0474   /*
0475    * Check that CLOCK_REALTIME is frozen in coarse resolution in seconds and
0476    * microseconds format.
0477    */
0478   rtems_clock_get_realtime_coarse_timeval( &tv );
0479   T_eq_i64( tv.tv_sec, 567993600 );
0480   T_eq_long( tv.tv_usec, 0 );
0481 
0482   /*
0483    * Check the initial boot time in seconds and nanoseconds format.
0484    */
0485   rtems_clock_get_boot_time( &ts );
0486   T_eq_i64( ts.tv_sec, 567993599 );
0487   T_eq_u64( ts.tv_nsec, 0 );
0488 
0489   /*
0490    * Check the initial boot time in binary time format.
0491    */
0492   rtems_clock_get_boot_time_bintime( &bt );
0493   T_eq_i64( bt.sec, 567993599 );
0494   T_eq_u64( bt.frac, 0 );
0495 
0496   /*
0497    * Check the initial boot time in seconds and microseconds format.
0498    */
0499   rtems_clock_get_boot_time_timeval( &tv );
0500   T_eq_i64( tv.tv_sec, 567993599 );
0501   T_eq_long( tv.tv_usec, 0 );
0502 }
0503 
0504 /**
0505  * @brief Call the directives to get the initial value of CLOCK_MONOTONIC and
0506  *   the initial boot time.
0507  */
0508 static void ScoreTimecounterValInstall_Action_2( void )
0509 {
0510   struct bintime  bt;
0511   sbintime_t      sb;
0512   struct timespec ts;
0513   struct timeval  tv;
0514 
0515   /*
0516    * Check the initial CLOCK_MONOTONIC in seconds and nanoseconds format.
0517    */
0518   rtems_clock_get_monotonic( &ts );
0519   T_eq_i64( ts.tv_sec, 1 );
0520   T_eq_u64( ts.tv_nsec, 0 );
0521 
0522   /*
0523    * Check that CLOCK_MONOTONIC is frozen in seconds and nanoseconds format.
0524    */
0525   rtems_clock_get_monotonic( &ts );
0526   T_eq_i64( ts.tv_sec, 1 );
0527   T_eq_u64( ts.tv_nsec, 0 );
0528 
0529   /*
0530    * Check the initial CLOCK_MONOTONIC in coarse resolution in seconds and
0531    * nanoseconds format.
0532    */
0533   rtems_clock_get_monotonic_coarse( &ts );
0534   T_eq_i64( ts.tv_sec, 1 );
0535   T_eq_u64( ts.tv_nsec, 0 );
0536 
0537   /*
0538    * Check that CLOCK_MONOTONIC is frozen in coarse resolution in seconds and
0539    * nanoseconds format.
0540    */
0541   rtems_clock_get_monotonic_coarse( &ts );
0542   T_eq_i64( ts.tv_sec, 1 );
0543   T_eq_u64( ts.tv_nsec, 0 );
0544 
0545   /*
0546    * Check the initial CLOCK_MONOTONIC in binary time format.
0547    */
0548   rtems_clock_get_monotonic_bintime( &bt );
0549   T_eq_i64( bt.sec, 1 );
0550   T_eq_u64( bt.frac, 0 );
0551 
0552   /*
0553    * Check that CLOCK_MONOTONIC is frozen in binary time format.
0554    */
0555   rtems_clock_get_monotonic_bintime( &bt );
0556   T_eq_i64( bt.sec, 1 );
0557   T_eq_u64( bt.frac, 0 );
0558 
0559   /*
0560    * Check the initial CLOCK_MONOTONIC in coarse resolution in binary time
0561    * format.
0562    */
0563   rtems_clock_get_monotonic_coarse_bintime( &bt );
0564   T_eq_i64( bt.sec, 1 );
0565   T_eq_u64( bt.frac, 0 );
0566 
0567   /*
0568    * Check that CLOCK_MONOTONIC is frozen in coarse resolution in binary time
0569    * format.
0570    */
0571   rtems_clock_get_monotonic_coarse_bintime( &bt );
0572   T_eq_i64( bt.sec, 1 );
0573   T_eq_u64( bt.frac, 0 );
0574 
0575   /*
0576    * Check the initial CLOCK_MONOTONIC in signed binary time format.
0577    */
0578   sb = rtems_clock_get_monotonic_sbintime();
0579   T_eq_i64( sb, SBT_1S );
0580 
0581   /*
0582    * Check that CLOCK_MONOTONIC is frozen in signed binary time format.
0583    */
0584   sb = rtems_clock_get_monotonic_sbintime();
0585   T_eq_i64( sb, SBT_1S );
0586 
0587   /*
0588    * Check the initial CLOCK_MONOTONIC in seconds and microseconds format.
0589    */
0590   rtems_clock_get_monotonic_timeval( &tv );
0591   T_eq_i64( tv.tv_sec, 1 );
0592   T_eq_long( tv.tv_usec, 0 );
0593 
0594   /*
0595    * Check that CLOCK_MONOTONIC is frozen in seconds and microseconds format.
0596    */
0597   rtems_clock_get_monotonic_timeval( &tv );
0598   T_eq_i64( tv.tv_sec, 1 );
0599   T_eq_long( tv.tv_usec, 0 );
0600 
0601   /*
0602    * Check the initial CLOCK_MONOTONIC in coarse resolution in seconds and
0603    * microseconds format.
0604    */
0605   rtems_clock_get_monotonic_coarse_timeval( &tv );
0606   T_eq_i64( tv.tv_sec, 1 );
0607   T_eq_long( tv.tv_usec, 0 );
0608 
0609   /*
0610    * Check that CLOCK_MONOTONIC is frozen in coarse resolution in seconds and
0611    * microseconds format.
0612    */
0613   rtems_clock_get_monotonic_coarse_timeval( &tv );
0614   T_eq_i64( tv.tv_sec, 1 );
0615   T_eq_long( tv.tv_usec, 0 );
0616 }
0617 
0618 /**
0619  * @brief Install timecounter of different quality levels and frequencies.
0620  */
0621 static void ScoreTimecounterValInstall_Action_3( void )
0622 {
0623   Timecounter *hqlf;
0624   Timecounter *hqnf;
0625   Timecounter *hqhf;
0626   Timecounter *lq;
0627   sbintime_t   sb;
0628 
0629   hqlf = &high_quality_low_frequency;
0630   hqnf = &high_quality_normal_frequency;
0631   hqhf = &high_quality_high_frequency;
0632   lq = &low_quality;
0633 
0634   /*
0635    * Install a timecounter with a high quality level and normal frequency.
0636    * Check that it was installed.
0637    */
0638   hqnf->base.tc_get_timecount = GetTimecount;
0639   hqnf->base.tc_counter_mask = 0xffffffff;
0640   hqnf->base.tc_frequency = 0x20000000;
0641   hqnf->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 1;
0642   rtems_timecounter_install( &hqnf->base );
0643 
0644   T_eq_u32( GetCounter( hqnf ), 1 );
0645 
0646   sb = rtems_clock_get_monotonic_sbintime();
0647   T_eq_u32( GetCounter( hqnf ), 2 );
0648   T_eq_i64( sb, SBT_1S + 8 );
0649 
0650   /*
0651    * Install a timecounter with a high quality level and low frequency. Check
0652    * that it was not installed.
0653    */
0654   hqlf->base.tc_get_timecount = GetTimecount;
0655   hqlf->base.tc_counter_mask = 0xffffffff;
0656   hqlf->base.tc_frequency = 0x10000000;
0657   hqlf->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 1;
0658   rtems_timecounter_install( &hqlf->base );
0659 
0660   T_eq_u32( GetCounter( hqlf ), 0 );
0661   T_eq_u32( GetCounter( hqnf ), 2 );
0662 
0663   sb = rtems_clock_get_monotonic_sbintime();
0664   T_eq_u32( GetCounter( hqlf ), 0 );
0665   T_eq_u32( GetCounter( hqnf ), 3 );
0666   T_eq_i64( sb, SBT_1S + 16 );
0667 
0668   /*
0669    * Install a timecounter with a high quality level and high frequency. Check
0670    * that it was installed.
0671    */
0672   hqhf->base.tc_get_timecount = GetTimecount;
0673   hqhf->base.tc_counter_mask = 0xffffffff;
0674   hqhf->base.tc_frequency = 0x40000000;
0675   hqhf->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 1;
0676   rtems_timecounter_install( &hqhf->base );
0677 
0678   T_eq_u32( GetCounter( hqlf ), 0 );
0679   T_eq_u32( GetCounter( hqnf ), 4 );
0680   T_eq_u32( GetCounter( hqhf ), 1 );
0681 
0682   sb = rtems_clock_get_monotonic_sbintime();
0683   T_eq_u32( GetCounter( hqlf ), 0 );
0684   T_eq_u32( GetCounter( hqnf ), 4 );
0685   T_eq_u32( GetCounter( hqhf ), 2 );
0686   T_eq_i64( sb, SBT_1S + 28 );
0687 
0688   /*
0689    * Install a timecounter with a low quality level.  Check that it was not
0690    * installed.
0691    */
0692   lq->base.tc_get_timecount = GetTimecount;
0693   lq->base.tc_counter_mask = 0xffffffff;
0694   lq->base.tc_frequency = 0x80000000;
0695   lq->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER;
0696   rtems_timecounter_install( &lq->base );
0697 
0698   T_eq_u32( GetCounter( hqlf ), 0 );
0699   T_eq_u32( GetCounter( hqnf ), 4 );
0700   T_eq_u32( GetCounter( hqhf ), 2 );
0701   T_eq_u32( GetCounter( lq ), 0 );
0702 
0703   sb = rtems_clock_get_monotonic_sbintime();
0704   T_eq_u32( GetCounter( hqlf ), 0 );
0705   T_eq_u32( GetCounter( hqnf ), 4 );
0706   T_eq_u32( GetCounter( hqhf ), 3 );
0707   T_eq_u32( GetCounter( lq ), 0 );
0708   T_eq_i64( sb, SBT_1S + 32 );
0709 }
0710 
0711 /**
0712  * @brief Call the directives to get the time in the highest resolution
0713  *   available to the system.
0714  */
0715 static void ScoreTimecounterValInstall_Action_4( void )
0716 {
0717   Timecounter    *tc;
0718   uint32_t        counter;
0719   struct bintime  bt;
0720   struct timespec ts;
0721   struct timeval  tv;
0722 
0723   tc = &high_quality_high_frequency;
0724   counter = GetCounter( tc );
0725 
0726   /*
0727    * Check that the timecounter was used by rtems_clock_get_realtime().
0728    */
0729   rtems_clock_get_realtime( &ts );
0730   T_eq_u32( GetCounter( tc ), counter + 1 );
0731 
0732   /*
0733    * Check that the timecounter was used by rtems_clock_get_realtime_bintime().
0734    */
0735   rtems_clock_get_realtime_bintime( &bt );
0736   T_eq_u32( GetCounter( tc ), counter + 2 );
0737 
0738   /*
0739    * Check that the timecounter was used by rtems_clock_get_realtime_timeval().
0740    */
0741   rtems_clock_get_realtime_timeval( &tv );
0742   T_eq_u32( GetCounter( tc ), counter + 3 );
0743 
0744   /*
0745    * Check that the timecounter was used by rtems_clock_get_monotonic().
0746    */
0747   rtems_clock_get_monotonic( &ts );
0748   T_eq_u32( GetCounter( tc ), counter + 4 );
0749 
0750   /*
0751    * Check that the timecounter was used by
0752    * rtems_clock_get_monotonic_bintime().
0753    */
0754   rtems_clock_get_monotonic_bintime( &bt );
0755   T_eq_u32( GetCounter( tc ), counter + 5 );
0756 
0757   /*
0758    * Check that the timecounter was used by
0759    * rtems_clock_get_monotonic_sbintime().
0760    */
0761   (void) rtems_clock_get_monotonic_sbintime();
0762   T_eq_u32( GetCounter( tc ), counter + 6 );
0763 
0764   /*
0765    * Check that the timecounter was used by
0766    * rtems_clock_get_monotonic_timeval().
0767    */
0768   rtems_clock_get_monotonic_timeval( &tv );
0769   T_eq_u32( GetCounter( tc ), counter + 7 );
0770 }
0771 
0772 /**
0773  * @brief Call the directives to get the time in a coarse resolution.
0774  */
0775 static void ScoreTimecounterValInstall_Action_5( void )
0776 {
0777   Timecounter    *tc;
0778   uint32_t        counter;
0779   struct bintime  bt;
0780   struct timespec ts;
0781   struct timeval  tv;
0782 
0783   tc = &high_quality_high_frequency;
0784   counter = GetCounter( tc );
0785 
0786   /*
0787    * Check that the timecounter was not used by
0788    * rtems_clock_get_realtime_coarse().
0789    */
0790   rtems_clock_get_realtime_coarse( &ts );
0791   T_eq_u32( GetCounter( tc ), counter );
0792 
0793   /*
0794    * Check that the timecounter was not used by
0795    * rtems_clock_get_realtime_coarse_bintime().
0796    */
0797   rtems_clock_get_realtime_coarse_bintime( &bt );
0798   T_eq_u32( GetCounter( tc ), counter );
0799 
0800   /*
0801    * Check that the timecounter was not used by
0802    * rtems_clock_get_realtime_coarse_timeval().
0803    */
0804   rtems_clock_get_realtime_coarse_timeval( &tv );
0805   T_eq_u32( GetCounter( tc ), counter );
0806 
0807   /*
0808    * Check that the timecounter was not used by
0809    * rtems_clock_get_monotonic_coarse().
0810    */
0811   rtems_clock_get_monotonic_coarse( &ts );
0812   T_eq_u32( GetCounter( tc ), counter );
0813 
0814   /*
0815    * Check that the timecounter was not used by
0816    * rtems_clock_get_monotonic_coarse_bintime().
0817    */
0818   rtems_clock_get_monotonic_coarse_bintime( &bt );
0819   T_eq_u32( GetCounter( tc ), counter );
0820 
0821   /*
0822    * Check that the timecounter was not used by
0823    * rtems_clock_get_monotonic_coarse_timeval().
0824    */
0825   rtems_clock_get_monotonic_coarse_timeval( &tv );
0826   T_eq_u32( GetCounter( tc ), counter );
0827 
0828   /*
0829    * Check that the timecounter was not used by rtems_clock_get_boot_time().
0830    */
0831   rtems_clock_get_boot_time( &ts );
0832   T_eq_u32( GetCounter( tc ), counter );
0833 
0834   /*
0835    * Check that the timecounter was not used by
0836    * rtems_clock_get_boot_time_bintime().
0837    */
0838   rtems_clock_get_boot_time_bintime( &bt );
0839   T_eq_u32( GetCounter( tc ), counter );
0840 
0841   /*
0842    * Check that the timecounter was not used by
0843    * rtems_clock_get_boot_time_timeval().
0844    */
0845   rtems_clock_get_boot_time_timeval( &tv );
0846   T_eq_u32( GetCounter( tc ), counter );
0847 }
0848 
0849 /**
0850  * @brief Call the directives to get the time in the highest resolution
0851  *   available to the system.
0852  */
0853 static void ScoreTimecounterValInstall_Action_6( void )
0854 {
0855   Timecounter    *tc;
0856   uint32_t        counter;
0857   struct bintime  bt;
0858   sbintime_t      sb;
0859   struct timespec ts;
0860   struct timeval  tv;
0861 
0862   tc = &high_quality_high_frequency;
0863   counter = 3 * tc->base.tc_frequency + 123456789;
0864 
0865   /*
0866    * Prepare the timecounter to get a large time difference.  Check that
0867    * rtems_clock_get_realtime() returns the correct time.
0868    */
0869   SetCounter( tc, counter );
0870   rtems_clock_get_realtime( &ts );
0871   T_eq_i64( ts.tv_sec, 567993603 );
0872   T_eq_u64( ts.tv_nsec, 114978100 );
0873 
0874   /*
0875    * Prepare the timecounter to get a large time difference.  Check that
0876    * rtems_clock_get_realtime_bintime() returns the correct time.
0877    */
0878   SetCounter( tc, counter );
0879   rtems_clock_get_realtime_bintime( &bt );
0880   T_eq_i64( bt.sec, 567993603 );
0881   T_eq_u64( bt.frac, 2120971587975905280 );
0882 
0883   /*
0884    * Prepare the timecounter to get a large time difference.  Check that
0885    * rtems_clock_get_realtime_timeval() returns the correct time.
0886    */
0887   SetCounter( tc, counter );
0888   rtems_clock_get_realtime_timeval( &tv );
0889   T_eq_i64( tv.tv_sec, 567993603 );
0890   T_eq_long( tv.tv_usec, 114978 );
0891 
0892   /*
0893    * Prepare the timecounter to get a large time difference.  Check that
0894    * rtems_clock_get_monotonic() returns the correct time.
0895    */
0896   SetCounter( tc, counter );
0897   rtems_clock_get_monotonic( &ts );
0898   T_eq_i64( ts.tv_sec, 4 );
0899   T_eq_u64( ts.tv_nsec, 114978100 );
0900 
0901   /*
0902    * Prepare the timecounter to get a large time difference.  Check that
0903    * rtems_clock_get_monotonic_bintime() returns the correct time.
0904    */
0905   SetCounter( tc, counter );
0906   rtems_clock_get_monotonic_bintime( &bt );
0907   T_eq_i64( bt.sec, 4 );
0908   T_eq_u64( bt.frac, 2120971587975905280 );
0909 
0910   /*
0911    * Prepare the timecounter to get a large time difference.  Check that
0912    * rtems_clock_get_monotonic_sbintime() returns the correct time.
0913    */
0914   SetCounter( tc, counter );
0915   sb = rtems_clock_get_monotonic_sbintime();
0916   T_eq_i64( sb, 17673696364 );
0917 
0918   /*
0919    * Prepare the timecounter to get a large time difference.  Check that
0920    * rtems_clock_get_monotonic_timeval() returns the correct time.
0921    */
0922   SetCounter( tc, counter );
0923   rtems_clock_get_monotonic_timeval( &tv );
0924   T_eq_i64( tv.tv_sec, 4 );
0925   T_eq_long( tv.tv_usec, 114978 );
0926 }
0927 
0928 /**
0929  * @brief Update the oldest timehand after a large time interval.
0930  */
0931 static void ScoreTimecounterValInstall_Action_7( void )
0932 {
0933   Timecounter    *tc;
0934   struct bintime  bt;
0935 
0936   tc = &high_quality_high_frequency;
0937 
0938   SetCounter( tc, 0 );
0939   rtems_clock_get_realtime_bintime( &bt );
0940   T_eq_i64( bt.sec, 567993600 );
0941   T_eq_u64( bt.frac, 103079215104 );
0942 
0943   SetCounter( tc, 2 * tc->base.tc_frequency );
0944   CallTimecounterTick();
0945 
0946   SetCounter( tc, 2 * tc->base.tc_frequency );
0947   rtems_clock_get_realtime_bintime( &bt );
0948   T_eq_i64( bt.sec, 567993602 );
0949   T_eq_u64( bt.frac, 103079215104 );
0950 }
0951 
0952 /**
0953  * @brief Call the simple timecounter tick service with non-zero delta and
0954  *   offset parameter values so that exactly one second passed.
0955  */
0956 static void ScoreTimecounterValInstall_Action_8( void )
0957 {
0958   ISR_lock_Context lock_context;
0959   Timecounter     *tc;
0960   struct bintime   bt;
0961 
0962   tc = &high_quality_high_frequency;
0963 
0964   _Timecounter_Acquire( &lock_context );
0965   _Timecounter_Tick_simple(
0966     tc->base.tc_frequency / 2,
0967     GetCounter( tc ) - tc->base.tc_frequency / 2,
0968     &lock_context
0969   );
0970 
0971   /*
0972    * Check that exactly one second passed due to the simple clock tick service.
0973    */
0974   rtems_clock_get_realtime_bintime( &bt );
0975   T_eq_i64( bt.sec, 567993603 );
0976   T_eq_u64( bt.frac, 103079215104 );
0977 }
0978 
0979 /**
0980  * @brief Install a very high quality timecounter with a low frequency to test
0981  *   the NTP support.
0982  */
0983 static void ScoreTimecounterValInstall_Action_9( void )
0984 {
0985   Timecounter   *tc;
0986   struct bintime bt;
0987 
0988   tc = &very_high_quality;
0989   tc->base.tc_get_timecount = GetTimecount;
0990   tc->base.tc_counter_mask = 0xffffffff;
0991   tc->base.tc_frequency = 0x01000000;
0992   tc->base.tc_quality = RTEMS_TIMECOUNTER_QUALITY_CLOCK_DRIVER + 2;
0993   rtems_timecounter_install( &tc->base );
0994 
0995   T_eq_u32( GetCounter( tc ), 1 );
0996 
0997   rtems_clock_get_realtime_bintime( &bt );
0998   T_eq_i64( bt.sec, 567993603 );
0999   T_eq_u64( bt.frac, 1219770712064 );
1000 
1001   /*
1002    * Let the seconds value of CLOCK_REALTIME not change. Check that the NTP
1003    * update second handler is not called.
1004    */
1005   _Timecounter_Set_NTP_update_second( NtpUpdateCounter );
1006   SetCounter( tc, tc->base.tc_frequency / 2 );
1007   CallTimecounterTick();
1008   _Timecounter_Set_NTP_update_second( NULL );
1009 
1010   T_eq_u32( ntp_counter, 0 );
1011 
1012   rtems_clock_get_realtime_bintime( &bt );
1013   T_eq_i64( bt.sec, 567993603 );
1014   T_eq_u64( bt.frac, UINT64_C( 9223373256625487872 ) );
1015 
1016   /*
1017    * Let the seconds value of CLOCK_REALTIME change by one. Check that the NTP
1018    * update second handler is called exactly once.
1019    */
1020   _Timecounter_Set_NTP_update_second( NtpUpdateCounter );
1021   SetCounter( tc, tc->base.tc_frequency );
1022   CallTimecounterTick();
1023   _Timecounter_Set_NTP_update_second( NULL );
1024 
1025   T_eq_u32( ntp_counter, 1 );
1026 
1027   rtems_clock_get_realtime_bintime( &bt );
1028   T_eq_i64( bt.sec, 567993604 );
1029   T_eq_u64( bt.frac, 1219770712064 );
1030 
1031   /*
1032    * Let the seconds value of CLOCK_REALTIME change by 200. Check that the NTP
1033    * update second handler is called exactly 200 times.
1034    */
1035   _Timecounter_Set_NTP_update_second( NtpUpdateCounter );
1036   SetCounter( tc, 201 * tc->base.tc_frequency );
1037   CallTimecounterTick();
1038   _Timecounter_Set_NTP_update_second( NULL );
1039 
1040   T_eq_u32( ntp_counter, 201 );
1041 
1042   rtems_clock_get_realtime_bintime( &bt );
1043   T_eq_i64( bt.sec, 567993804 );
1044   T_eq_u64( bt.frac, 1219770712064 );
1045 
1046   /*
1047    * Let the seconds value of CLOCK_REALTIME change by 201. Check that the NTP
1048    * update second handler is called exactly twice.
1049    */
1050   _Timecounter_Set_NTP_update_second( NtpUpdateCounter );
1051   SetCounter( tc, 402 * tc->base.tc_frequency );
1052   CallTimecounterTick();
1053   _Timecounter_Set_NTP_update_second( NULL );
1054 
1055   T_eq_u32( ntp_counter, 203 );
1056 
1057   rtems_clock_get_realtime_bintime( &bt );
1058   T_eq_i64( bt.sec, 567994005 );
1059   T_eq_u64( bt.frac, 1219770712064 );
1060 
1061   /*
1062    * Let the seconds value of CLOCK_REALTIME change by one. Check that the NTP
1063    * update second handler is incremented the CLOCK_REALTIME by one second.
1064    */
1065   _Timecounter_Set_NTP_update_second( NtpUpdateSecondIncrement );
1066   SetCounter( tc, 403 * tc->base.tc_frequency );
1067   CallTimecounterTick();
1068   _Timecounter_Set_NTP_update_second( NULL );
1069 
1070   rtems_clock_get_realtime_bintime( &bt );
1071   T_eq_i64( bt.sec, 567994007 );
1072   T_eq_u64( bt.frac, 1219770712064 );
1073 
1074   /*
1075    * Let the seconds value of CLOCK_REALTIME change by one. Check that the NTP
1076    * update second handler is decremented the CLOCK_REALTIME by one second.
1077    */
1078   _Timecounter_Set_NTP_update_second( NtpUpdateSecondDecrement );
1079   SetCounter( tc, 404 * tc->base.tc_frequency );
1080   CallTimecounterTick();
1081   _Timecounter_Set_NTP_update_second( NULL );
1082 
1083   rtems_clock_get_realtime_bintime( &bt );
1084   T_eq_i64( bt.sec, 567994007 );
1085   T_eq_u64( bt.frac, 1219770712064 );
1086 
1087   /*
1088    * Let the seconds value of CLOCK_REALTIME change by one. Check that the NTP
1089    * update second handler increased the timecounter frequency.
1090    */
1091   _Timecounter_Set_NTP_update_second( NtpUpdateAdjustmentFaster );
1092   SetCounter( tc, 405 * tc->base.tc_frequency );
1093   CallTimecounterTick();
1094   _Timecounter_Set_NTP_update_second( NULL );
1095 
1096   SetCounter( tc, 406 * tc->base.tc_frequency );
1097   rtems_clock_get_realtime_bintime( &bt );
1098   T_eq_i64( bt.sec, 567994009 );
1099   T_eq_u64( bt.frac, 92353004044288 );
1100 
1101   /*
1102    * Let the seconds value of CLOCK_REALTIME change by one. Check that the NTP
1103    * update second handler decreased the timecounter frequency.
1104    */
1105   _Timecounter_Set_NTP_update_second( NtpUpdateAdjustmentSlower );
1106   SetCounter( tc, 407 * tc->base.tc_frequency );
1107   CallTimecounterTick();
1108   _Timecounter_Set_NTP_update_second( NULL );
1109 
1110   SetCounter( tc, 408 * tc->base.tc_frequency );
1111   rtems_clock_get_realtime_bintime( &bt );
1112   T_eq_i64( bt.sec, 567994011 );
1113   T_eq_u64( bt.frac, 92353004044288 );
1114 }
1115 
1116 /**
1117  * @fn void T_case_body_ScoreTimecounterValInstall( void )
1118  */
1119 T_TEST_CASE( ScoreTimecounterValInstall )
1120 {
1121   ScoreTimecounterValInstall_Action_0();
1122   ScoreTimecounterValInstall_Action_1();
1123   ScoreTimecounterValInstall_Action_2();
1124   ScoreTimecounterValInstall_Action_3();
1125   ScoreTimecounterValInstall_Action_4();
1126   ScoreTimecounterValInstall_Action_5();
1127   ScoreTimecounterValInstall_Action_6();
1128   ScoreTimecounterValInstall_Action_7();
1129   ScoreTimecounterValInstall_Action_8();
1130   ScoreTimecounterValInstall_Action_9();
1131 }
1132 
1133 /** @} */