Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*  Init
0004  *
0005  *  This routine is the XXX.
0006  *
0007  *  Input parameters:
0008  *    argument - task argument
0009  *
0010  *  Output parameters:  NONE
0011  *
0012  *  COPYRIGHT (c) 2008.
0013  *  On-Line Applications Research Corporation (OAR).
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifdef HAVE_CONFIG_H
0038 #include "config.h"
0039 #endif
0040 
0041 #define CONFIGURE_INIT
0042 #include "system.h"
0043 
0044 #include <rtems/score/watchdogimpl.h>
0045 
0046 const char rtems_test_name[] = "SPWATCHDOG";
0047 
0048 typedef struct {
0049   Watchdog_Control Base;
0050   int counter;
0051 } test_watchdog;
0052 
0053 static void test_watchdog_routine( Watchdog_Control *base )
0054 {
0055   test_watchdog *watchdog = (test_watchdog *) base;
0056 
0057   ++watchdog->counter;
0058 }
0059 
0060 static void test_watchdog_static_init( void )
0061 {
0062   static Watchdog_Control a = WATCHDOG_INITIALIZER(
0063     test_watchdog_routine
0064   );
0065   Watchdog_Control b;
0066 
0067   memset( &b, 0, sizeof( b ) );
0068   _Watchdog_Preinitialize( &b, _Per_CPU_Get_by_index( 0 ) );
0069   _Watchdog_Initialize(
0070     &b,
0071     test_watchdog_routine
0072   );
0073 
0074   rtems_test_assert( memcmp( &a, &b, sizeof( a ) ) == 0 );
0075 }
0076 
0077 static void test_watchdog_config( void )
0078 {
0079   rtems_test_assert( _Watchdog_Nanoseconds_per_tick == 10000000 );
0080   rtems_test_assert( _Watchdog_Ticks_per_second == 100 );
0081   rtems_test_assert( rtems_clock_get_ticks_per_second() == 100 );
0082   #undef rtems_clock_get_ticks_per_second
0083   rtems_test_assert( rtems_clock_get_ticks_per_second() == 100 );
0084 }
0085 
0086 static bool test_watchdog_is_inactive( test_watchdog *watchdog )
0087 {
0088   return _Watchdog_Get_state( &watchdog->Base ) == WATCHDOG_INACTIVE;
0089 }
0090 
0091 static void test_watchdog_init( test_watchdog *watchdog, int counter )
0092 {
0093   _Watchdog_Preinitialize( &watchdog->Base, _Per_CPU_Get_snapshot() );
0094   _Watchdog_Initialize( &watchdog->Base, test_watchdog_routine );
0095   rtems_test_assert( test_watchdog_is_inactive( watchdog ) ) ;
0096   watchdog->counter = counter;
0097 }
0098 
0099 static uint64_t test_watchdog_tick( Watchdog_Header *header, uint64_t now )
0100 {
0101 #if ISR_LOCK_NEEDS_OBJECT
0102   ISR_lock_Control lock = ISR_LOCK_INITIALIZER( "Test" );
0103 #endif
0104   ISR_lock_Context lock_context;
0105   Watchdog_Control *first;
0106 
0107   _ISR_lock_ISR_disable_and_acquire( &lock, &lock_context );
0108 
0109   ++now;
0110   first = _Watchdog_Header_first( header );
0111 
0112   if ( first != NULL ) {
0113     _Watchdog_Tickle( header, first, now, &lock, &lock_context );
0114   }
0115 
0116   _ISR_lock_Release_and_ISR_enable( &lock, &lock_context );
0117   _ISR_lock_Destroy( &lock );
0118 
0119   return now;
0120 }
0121 
0122 static void test_watchdog_operations( void )
0123 {
0124   Watchdog_Header header;
0125   uint64_t now;
0126   test_watchdog a;
0127   test_watchdog b;
0128   test_watchdog c;
0129 
0130   _Watchdog_Header_initialize( &header );
0131   rtems_test_assert( _RBTree_Is_empty( &header.Watchdogs ) );
0132   rtems_test_assert( header.first == NULL );
0133 
0134   test_watchdog_init( &a, 10 );
0135   test_watchdog_init( &b, 20 );
0136   test_watchdog_init( &c, 30 );
0137 
0138   now = 0;
0139   now = test_watchdog_tick( &header, now );
0140 
0141   _Watchdog_Insert( &header, &a.Base, now + 1 );
0142   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0143   rtems_test_assert( !test_watchdog_is_inactive( &a ) ) ;
0144   rtems_test_assert( a.Base.expire == 2 );
0145   rtems_test_assert( a.counter == 10 );
0146 
0147   _Watchdog_Remove( &header, &a.Base );
0148   rtems_test_assert( header.first == NULL );
0149   rtems_test_assert( test_watchdog_is_inactive( &a ) ) ;
0150   rtems_test_assert( a.Base.expire == 2 );
0151   rtems_test_assert( a.counter == 10 );
0152 
0153   _Watchdog_Remove( &header, &a.Base );
0154   rtems_test_assert( header.first == NULL );
0155   rtems_test_assert( test_watchdog_is_inactive( &a ) ) ;
0156   rtems_test_assert( a.Base.expire == 2 );
0157   rtems_test_assert( a.counter == 10 );
0158 
0159   _Watchdog_Insert( &header, &a.Base, now + 1 );
0160   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0161   rtems_test_assert( !test_watchdog_is_inactive( &a ) ) ;
0162   rtems_test_assert( a.Base.expire == 2 );
0163   rtems_test_assert( a.counter == 10 );
0164 
0165   _Watchdog_Insert( &header, &b.Base, now + 1 );
0166   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0167   rtems_test_assert( !test_watchdog_is_inactive( &b ) ) ;
0168   rtems_test_assert( b.Base.expire == 2 );
0169   rtems_test_assert( b.counter == 20 );
0170 
0171   _Watchdog_Insert( &header, &c.Base, now + 2 );
0172   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0173   rtems_test_assert( !test_watchdog_is_inactive( &c ) ) ;
0174   rtems_test_assert( c.Base.expire == 3 );
0175   rtems_test_assert( c.counter == 30 );
0176 
0177   _Watchdog_Remove( &header, &a.Base );
0178   rtems_test_assert( header.first == &b.Base.Node.RBTree );
0179   rtems_test_assert( test_watchdog_is_inactive( &a ) ) ;
0180   rtems_test_assert( a.Base.expire == 2 );
0181   rtems_test_assert( a.counter == 10 );
0182 
0183   _Watchdog_Remove( &header, &b.Base );
0184   rtems_test_assert( header.first == &c.Base.Node.RBTree );
0185   rtems_test_assert( test_watchdog_is_inactive( &b ) ) ;
0186   rtems_test_assert( b.Base.expire == 2 );
0187   rtems_test_assert( b.counter == 20 );
0188 
0189   _Watchdog_Remove( &header, &c.Base );
0190   rtems_test_assert( header.first == NULL );
0191   rtems_test_assert( test_watchdog_is_inactive( &c ) ) ;
0192   rtems_test_assert( c.Base.expire == 3 );
0193   rtems_test_assert( c.counter == 30 );
0194 
0195   _Watchdog_Insert( &header, &a.Base, now + 2 );
0196   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0197   rtems_test_assert( !test_watchdog_is_inactive( &a ) ) ;
0198   rtems_test_assert( a.Base.expire == 3 );
0199   rtems_test_assert( a.counter == 10 );
0200 
0201   _Watchdog_Insert( &header, &b.Base, now + 2 );
0202   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0203   rtems_test_assert( !test_watchdog_is_inactive( &b ) ) ;
0204   rtems_test_assert( b.Base.expire == 3 );
0205   rtems_test_assert( b.counter == 20 );
0206 
0207   _Watchdog_Insert( &header, &c.Base, now + 3 );
0208   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0209   rtems_test_assert( !test_watchdog_is_inactive( &c ) ) ;
0210   rtems_test_assert( c.Base.expire == 4 );
0211   rtems_test_assert( c.counter == 30 );
0212 
0213   now = test_watchdog_tick( &header, now );
0214   rtems_test_assert( !_RBTree_Is_empty( &header.Watchdogs ) );
0215   rtems_test_assert( header.first == &a.Base.Node.RBTree );
0216   rtems_test_assert( !test_watchdog_is_inactive( &a ) ) ;
0217   rtems_test_assert( a.Base.expire == 3 );
0218   rtems_test_assert( a.counter == 10 );
0219   rtems_test_assert( !test_watchdog_is_inactive( &b ) ) ;
0220   rtems_test_assert( b.Base.expire == 3 );
0221   rtems_test_assert( b.counter == 20 );
0222   rtems_test_assert( !test_watchdog_is_inactive( &c ) ) ;
0223   rtems_test_assert( c.Base.expire == 4 );
0224   rtems_test_assert( c.counter == 30 );
0225 
0226   now = test_watchdog_tick( &header, now );
0227   rtems_test_assert( !_RBTree_Is_empty( &header.Watchdogs ) );
0228   rtems_test_assert( header.first == &c.Base.Node.RBTree );
0229   rtems_test_assert( test_watchdog_is_inactive( &a ) ) ;
0230   rtems_test_assert( a.Base.expire == 3 );
0231   rtems_test_assert( a.counter == 11 );
0232   rtems_test_assert( test_watchdog_is_inactive( &b ) ) ;
0233   rtems_test_assert( b.Base.expire == 3 );
0234   rtems_test_assert( b.counter == 21 );
0235   rtems_test_assert( !test_watchdog_is_inactive( &c ) ) ;
0236   rtems_test_assert( c.Base.expire == 4 );
0237   rtems_test_assert( c.counter == 30 );
0238 
0239   now = test_watchdog_tick( &header, now );
0240   rtems_test_assert( _RBTree_Is_empty( &header.Watchdogs ) );
0241   rtems_test_assert( header.first == NULL );
0242   rtems_test_assert( test_watchdog_is_inactive( &a ) ) ;
0243   rtems_test_assert( a.Base.expire == 3 );
0244   rtems_test_assert( a.counter == 11 );
0245   rtems_test_assert( test_watchdog_is_inactive( &b ) ) ;
0246   rtems_test_assert( b.Base.expire == 3 );
0247   rtems_test_assert( b.counter == 21 );
0248   rtems_test_assert( test_watchdog_is_inactive( &c ) ) ;
0249   rtems_test_assert( c.Base.expire == 4 );
0250   rtems_test_assert( c.counter == 31 );
0251 
0252   _Watchdog_Header_destroy( &header );
0253 }
0254 
0255 rtems_task Init(
0256   rtems_task_argument argument
0257 )
0258 {
0259   rtems_time_of_day  time;
0260   rtems_status_code  status;
0261 
0262   TEST_BEGIN();
0263 
0264   test_watchdog_operations();
0265   test_watchdog_static_init();
0266   test_watchdog_config();
0267 
0268   build_time( &time, 12, 31, 1988, 9, 0, 0, 0 );
0269 
0270   status = rtems_clock_set( &time );
0271   directive_failed( status, "rtems_clock_set" );
0272 
0273   Task_name[ 1 ]  = rtems_build_name( 'T', 'A', '1', ' ' );
0274   Timer_name[ 1 ] = rtems_build_name( 'T', 'M', '1', ' ' );
0275 
0276   status = rtems_task_create(
0277     Task_name[ 1 ],
0278     1,
0279     RTEMS_MINIMUM_STACK_SIZE * 2,
0280     RTEMS_DEFAULT_MODES,
0281     RTEMS_DEFAULT_ATTRIBUTES,
0282     &Task_id[ 1 ]
0283   );
0284   directive_failed( status, "rtems_task_create of TA1" );
0285 
0286   status = rtems_task_start( Task_id[ 1 ], Task_1, 0 );
0287   directive_failed( status, "rtems_task_start of TA1" );
0288 
0289   puts( "INIT - rtems_timer_create - creating timer 1" );
0290   status = rtems_timer_create( Timer_name[ 1 ], &Timer_id[ 1 ] );
0291   directive_failed( status, "rtems_timer_create" );
0292 
0293   printf( "INIT - timer 1 has id (0x%" PRIxrtems_id ")\n", Timer_id[ 1 ] );
0294 
0295   /* Task_1() will end the test */
0296   rtems_task_exit();
0297 }