1 /*
2  * FreeRTOS V202212.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26 
27 #ifndef _TEST_UTILS_H_
28 #define _TEST_UTILS_H_
29 
30 /**
31  * @brief      Retry expression or statement with exponential backoff
32  *
33  * @param      xCommand            The expression or statement that should be
34  *                                 retried
35  * @param      xSuccessStatus      The success status where a xCommand need not
36  *                                 be retried any more
37  * @param      ulStartingPeriodMs  The initial delay period in milliseconds
38  * @param      lRetries            The number of times to retry.  xCommand will
39  *                                 be tried once, and then retried n times.
40  *
41  * @code
42  *  int a = 0;
43  *  RETRY_EXPONENTIAL( printf( "foo\n" ), 4, 150, 8 );
44  *  RETRY_EXPONENTIAL( a = printf( "bar\n" ), 0, 250, 8 );
45  *  RETRY_EXPONENTIAL( a = printf( "bar\n" ), 0, 250, 8 );
46  *  RETRY_EXPONENTIAL( a = connect_to_server(), CONNECTION_SUCCESS, 250, 8 );
47  *  RETRY_EXPONENTIAL( a++, 10, 250, 8 );
48  * @endcode
49  *
50  * @return     None
51  */
52 #define RETRY_EXPONENTIAL(                                               \
53         xCommand, xSuccessStatus, ulStartingPeriodMs, lRetries )         \
54     {                                                                    \
55         int32_t lRetried = 0;                                            \
56         uint32_t ulPeriodMs = ulStartingPeriodMs;                        \
57         int32_t lStatus;                                                 \
58         for( ; lRetried <= lRetries; lRetried++ ) {                      \
59             if( lRetried ) {                                             \
60                 configPRINTF( ( "retrying \"%s\", %d of %d, in %d ms\n", \
61                                 # xCommand, lRetried,                    \
62                                 lRetries, ulPeriodMs ) );                \
63                 vTaskDelay( pdMS_TO_TICKS( ulPeriodMs ) );               \
64                 ulPeriodMs *= 2;                                         \
65             }                                                            \
66             lStatus = xCommand;                                          \
67             if( xSuccessStatus == lStatus ) {                            \
68                 break;                                                   \
69             }                                                            \
70             configPRINTF( ( "expected %d, got %d\n",                     \
71                             xSuccessStatus, lStatus ) );                 \
72         }                                                                \
73     }
74 
75 /**
76  * @brief      Returns the file name at the end of a windows path
77  *
78  * @param      full_path  The full path
79  *
80  * @return     file name
81  */
82 #define WIN_FILENAME( full_path ) \
83     ( strrchr( full_path, '\\' ) ? strrchr( full_path, '\\' ) + 1 : full_path )
84 
85 /**
86  * @brief      Returns the file name at the end of a linux path
87  *
88  * @param      full_path  The full path
89  *
90  * @return     file name
91  */
92 #define NIX_FILENAME( full_path ) \
93     ( strrchr( full_path, '/' ) ? strrchr( full_path, '/' ) + 1 : full_path )
94 
95 /**
96  * The name of the current file, stripped of the path
97  */
98 #define __FILENAME__    WIN_FILENAME( NIX_FILENAME( __FILE__ ) )
99 
100 
101 #endif /* end of include guard: _AWS_TEST_UTILS_H_ */
102