Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_time_test.c Source File

pal_time_test.c

00001 /*******************************************************************************
00002  * Copyright 2016, 2017 ARM Ltd.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  *******************************************************************************/
00016 
00017 #include "pal.h"
00018 #include "unity.h"
00019 #include "unity_fixture.h"
00020 #include "storage.h"
00021 #include "test_runners.h"
00022 #include <string.h>
00023 #include <stdlib.h>
00024 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00025 #include "pal_sst.h"
00026 #else
00027 #include "sotp.h"
00028 #endif
00029 
00030 
00031 #define TRACE_GROUP "PAL"
00032 
00033 #define PAL_RUNNING_TEST_TIME   5  //estimation on length of test in seconds
00034 
00035 TEST_GROUP(pal_time);
00036 
00037 TEST_SETUP(pal_time)
00038 {
00039     palStatus_t status = PAL_SUCCESS;
00040 
00041     //init pal
00042     status = pal_init();
00043     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00044 
00045 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00046     // Reset storage before pal_initTime since there might be CMAC lefovers
00047     // in internal flash which might fail storage access in pal_initTime
00048     pal_SSTReset ();
00049 
00050 #if !PAL_USE_HW_TRNG
00051     // If no hardware trng - entropy must be injected for random to work
00052     uint8_t entropy_buf[48] = { 0 };
00053     status = pal_osEntropyInject(entropy_buf, sizeof(entropy_buf));
00054     TEST_ASSERT(status == PAL_SUCCESS || status == PAL_ERR_ENTROPY_EXISTS);
00055 #endif
00056 
00057 #else 
00058     sotp_reset();
00059 #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00060 
00061     // Initialize the time module
00062     status = pal_initTime();
00063     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00064 
00065 }
00066 
00067 TEST_TEAR_DOWN(pal_time)
00068 {
00069 
00070 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00071     pal_SSTReset ();
00072 #else
00073     sotp_reset();
00074 #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00075 
00076     pal_destroy();
00077 }
00078 
00079 /*! \brief Check the APIs `pal_osSetTime()` and `pal_osGetTime()` with different scenarios
00080 * for valid and non-valid scenarios and epoch values.
00081 * The test also checks that the time increases.
00082 *
00083 * | # |    Step                        |   Expected  |
00084 * |---|--------------------------------|-------------|
00085 * | 1 | Start a loop for the following steps.                                            | PAL_SUCCESS |
00086 * | 2 | Set time to invalid value using `pal_osSetTime`.                                 | PAL_ERR_INVALID_TIME |
00087 * | 3 | Get time using `pal_osGetTime`.                                                  | PAL_SUCCESS |
00088 * | 4 | Set time to valid value using `pal_osSetTime`.                                   | PAL_SUCCESS |
00089 * | 5 | Sleep.                                                                           | PAL_SUCCESS |
00090 * | 6 | Get time using `pal_osGetTime` and check that it equals set time + sleep time.   | PAL_SUCCESS |
00091 */
00092 TEST(pal_time, RealTimeClockTest1)
00093 {
00094     palStatus_t status;
00095     uint64_t curTime = 0;
00096     uint64_t lastTimeSeen = 0;
00097     const uint64_t minSecSinceEpoch = PAL_MIN_SEC_FROM_EPOCH + 1; //At least 47 years passed from 1.1.1970 in seconds
00098 
00099     /*#1*/
00100     for (int i=0; i < 2; i++)
00101     {
00102     /*#2*/
00103         status = pal_osSetTime(3);
00104         TEST_ASSERT_EQUAL_HEX(PAL_ERR_INVALID_TIME , status); // Less than current epoch time -> error
00105 
00106     /*#3*/
00107         curTime = pal_osGetTime();
00108         TEST_ASSERT_TRUE(lastTimeSeen <= curTime); //Time was not previously set; 0 is acceptable
00109     /*#4*/
00110         status = pal_osSetTime(minSecSinceEpoch);
00111         TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status); // More than current epoch time -> success
00112     /*#5*/
00113         int milliDelay = 1500;
00114         pal_osDelay(milliDelay); //500 milliseconds
00115     /*#6*/
00116         curTime = pal_osGetTime();
00117         TEST_ASSERT_TRUE(curTime > minSecSinceEpoch);
00118         TEST_ASSERT_TRUE(curTime <= minSecSinceEpoch+(int)ceil((float)milliDelay/1000));
00119         lastTimeSeen = curTime;
00120     }
00121 }
00122 
00123 
00124 /*! \brief Check Weak Set Time - Forward flow.
00125 *
00126 * | # |    Step                        |   Expected  |
00127 * |---|--------------------------------|-------------|
00128 * | 1 | checking RTC and RBP flow - not set RBP SAVED TIME  + LAST TIME BACK + RTC to new time                        | PAL_SUCCESS |
00129 * | 2 | checking RTC and RBP flow - not set RBP SAVED TIME  + LAST TIME BACK to new time but set RTC to new time      | PAL_SUCCESS |
00130 * | 3 | checking RTC and RBP flow - set RBP SAVED TIME  + LAST TIME BACK + RTC to new time                            | PAL_SUCCESS |
00131 */
00132 TEST(pal_time, OsWeakSetTime_Forward)
00133 {
00134 #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00135     palStatus_t status;
00136     uint64_t setTimeInSeconds = 0;
00137     uint64_t curentTimeInSeconds=0;
00138     uint64_t pal_Time = 0;
00139     uint64_t getTime = 0;
00140     size_t actualLenBytes = 0;
00141 
00142 #if (PAL_USE_HW_RTC)
00143     //This code is to preserve system time
00144     uint64_t testStartTime = 0;
00145     status = pal_plat_osGetRtcTime(&testStartTime);
00146 #endif
00147 
00148 
00149     /*#1*/
00150 #if (PAL_USE_HW_RTC)
00151     pal_plat_osSetRtcTime(PAL_MIN_RTC_SET_TIME);
00152 #endif
00153     pal_osSetTime(PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100);
00154     curentTimeInSeconds = pal_osGetTime();
00155 
00156     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&curentTimeInSeconds, sizeof(uint64_t), false);
00157     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00158 
00159     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&curentTimeInSeconds, sizeof(uint64_t), false);
00160     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00161 
00162 #if (PAL_USE_HW_RTC)
00163     uint64_t rtcTime = 0;
00164     status = pal_plat_osSetRtcTime(curentTimeInSeconds);
00165     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00166 #endif//PAL_USE_HW_RTC
00167 
00168     setTimeInSeconds = curentTimeInSeconds + (50 * PAL_ONE_SEC);
00169     status = pal_osSetWeakTime(setTimeInSeconds);
00170 
00171     pal_Time = pal_osGetTime();
00172     if (pal_Time - setTimeInSeconds > 5)
00173     {
00174         status = PAL_ERR_GENERAL_BASE;
00175     }
00176     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00177     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00178     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00179     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00180 
00181     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00182     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00183     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00184 
00185 #if (PAL_USE_HW_RTC)
00186     status = pal_plat_osGetRtcTime(&rtcTime);
00187     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00188     TEST_ASSERT_NOT_EQUAL(rtcTime, setTimeInSeconds);
00189 #endif//PAL_USE_HW_RTC
00190 
00191     /*#2*/
00192     curentTimeInSeconds = pal_osGetTime();
00193 #if (PAL_USE_HW_RTC)
00194     pal_plat_osSetRtcTime(curentTimeInSeconds);
00195 #endif//PAL_USE_HW_RTC
00196     
00197     setTimeInSeconds = curentTimeInSeconds+(200 * PAL_ONE_SEC);
00198 
00199     status = pal_osSetWeakTime(setTimeInSeconds);
00200     pal_Time = pal_osGetTime();
00201     if (pal_Time - setTimeInSeconds > 5)
00202     {
00203         status = PAL_ERR_GENERAL_BASE;
00204     }
00205     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00206 
00207     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00208     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00209     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00210 
00211     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00212     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00213     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00214 
00215 #if (PAL_USE_HW_RTC)
00216     status = pal_plat_osGetRtcTime(&rtcTime);
00217     TEST_ASSERT_EQUAL_UINT64(rtcTime, setTimeInSeconds);
00218 #endif//PAL_USE_HW_RTC
00219 
00220     /*#3*/
00221     curentTimeInSeconds = pal_osGetTime();
00222     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&curentTimeInSeconds, sizeof(uint64_t), false);
00223     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00224 
00225     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME,  (uint8_t*)&curentTimeInSeconds, sizeof(uint64_t), false);
00226     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00227 
00228 #if (PAL_USE_HW_RTC)
00229     status = pal_plat_osSetRtcTime(curentTimeInSeconds);
00230     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00231 #endif//PAL_USE_HW_RTC
00232 
00233     setTimeInSeconds = curentTimeInSeconds + PAL_MINIMUM_FORWARD_LATENCY_SEC + (100 * PAL_ONE_SEC);
00234     status = pal_osSetWeakTime(setTimeInSeconds);
00235     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00236 
00237     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00238     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00239     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00240 
00241 #if (PAL_USE_HW_RTC)
00242     status = pal_plat_osGetRtcTime(&rtcTime);
00243     TEST_ASSERT_EQUAL_UINT64(rtcTime, setTimeInSeconds);
00244 #endif//PAL_USE_HW_RTC
00245 
00246 #if (PAL_USE_HW_RTC)
00247     //restore System time
00248     pal_plat_osSetRtcTime(testStartTime + PAL_RUNNING_TEST_TIME);
00249 #endif
00250 #else // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00251     TEST_IGNORE_MESSAGE("Ignored, PAL_INT_FLASH_NUM_SECTIONS not set to 2 or PAL_USE_INTERNAL_FLASH not set or MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT is not defined");
00252 #endif // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00253 }
00254 
00255 /*! \brief Check Weak Set Time - Backward flow.
00256 *
00257 * | # |    Step                        |   Expected  |
00258 * |---|--------------------------------|-------------|
00259 * | 1 | checking RBP flow - set RBP SAVED TIME and LAST TIME BACK to new time           | PAL_SUCCESS |
00260 * | 2 | checking RBP flow - not set RBP SAVED TIME and LAST TIME BACK to new time       | PAL_SUCCESS |
00261 */
00262 TEST(pal_time, OsWeakSetTime_Backward)
00263 {
00264 #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00265     uint64_t setTimeInSeconds = 0;
00266     uint64_t curentTimeInSeconds = 0;
00267     palStatus_t status = PAL_SUCCESS;
00268     uint64_t getTimeValueBackward = 0;
00269     uint64_t pal_Time = 0;
00270 #if (PAL_USE_HW_RTC)
00271     //This code is to preserve system time
00272     uint64_t testStartTime = 0;
00273     status = pal_plat_osGetRtcTime(&testStartTime);
00274 #endif
00275 
00276     /*#1*/
00277 #if (PAL_USE_HW_RTC)
00278     pal_plat_osSetRtcTime(PAL_MIN_RTC_SET_TIME);
00279 
00280 #endif
00281 
00282     //set time to a valid one
00283     pal_osSetTime(PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100);
00284     curentTimeInSeconds = pal_osGetTime();
00285 
00286     getTimeValueBackward = curentTimeInSeconds - (3 * PAL_MINIMUM_FORWARD_LATENCY_SEC);
00287     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t *)&getTimeValueBackward, sizeof(uint64_t), false);
00288     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00289 
00290 
00291     setTimeInSeconds = curentTimeInSeconds - (6 * PAL_SECONDS_PER_MIN);
00292     status = pal_osSetWeakTime(setTimeInSeconds);
00293 
00294     pal_Time = pal_osGetTime();
00295     if (pal_Time - setTimeInSeconds > 5)
00296     {
00297         status = PAL_ERR_GENERAL_BASE;
00298     }
00299     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00300 
00301     uint64_t getTime = 0;
00302     size_t actualLenBytes = 0;
00303 
00304     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00305     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00306     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00307 
00308     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00309     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00310     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00311 
00312     /*#2*/
00313     curentTimeInSeconds = pal_osGetTime();
00314     getTimeValueBackward = curentTimeInSeconds - (3 * PAL_MINIMUM_FORWARD_LATENCY_SEC);
00315     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t *)&getTimeValueBackward, sizeof(uint64_t), false);
00316     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00317 
00318     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&getTimeValueBackward, sizeof(uint64_t), false);
00319     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00320 
00321     setTimeInSeconds = curentTimeInSeconds - (12 * PAL_SECONDS_PER_MIN);
00322 
00323     status = pal_osSetWeakTime(setTimeInSeconds);
00324     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00325 
00326     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00327     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00328     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00329     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00330     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00331     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00332 
00333 #if (PAL_USE_HW_RTC)
00334     //restore System time
00335     pal_plat_osSetRtcTime(testStartTime + PAL_RUNNING_TEST_TIME);
00336 #endif
00337 #else // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00338     TEST_IGNORE_MESSAGE("Ignored, PAL_INT_FLASH_NUM_SECTIONS not set to 2 or PAL_USE_INTERNAL_FLASH not set or MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT is not defined");
00339 #endif // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00340 }
00341 
00342 /*! \brief Weak Strong Set Time- minimalStoredLag flow.
00343 *
00344 * | # |    Step                        |   Expected  |
00345 * |---|--------------------------------|-------------|
00346 * | 1 | checking RBP flow- set RBP SAVED TIME to new time                           | PAL_SUCCESS |
00347 * | 2 | checking RBP flow- not set RBP SAVED TIME   to new time                     | PAL_SUCCESS |
00348 */
00349 TEST(pal_time, OsWeakSetTime_minimalStoredLag)
00350 {
00351 #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00352     palStatus_t status;
00353     uint64_t setTimeInSeconds = 0;
00354     uint64_t curentTimeInSeconds = 0;
00355     uint64_t getTime = 0;
00356     size_t actualLenBytes = 0;
00357     uint64_t setTimeValue = 0;
00358 
00359 #if (PAL_USE_HW_RTC)
00360     //This code is to preserve system time
00361     uint64_t testStartTime = 0;
00362     status = pal_plat_osGetRtcTime(&testStartTime);
00363 #endif
00364 
00365     /*#1*/
00366 #if (PAL_USE_HW_RTC)
00367     pal_plat_osSetRtcTime(PAL_MIN_RTC_SET_TIME);
00368 #endif
00369     pal_osSetTime(PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100);
00370     curentTimeInSeconds = pal_osGetTime();
00371     setTimeInSeconds = curentTimeInSeconds;
00372 
00373     setTimeValue = curentTimeInSeconds - (PAL_MINIMUM_STORAGE_LATENCY_SEC + 50);
00374     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&setTimeValue, sizeof(uint64_t), false);
00375     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00376 
00377     status = pal_osSetWeakTime(setTimeInSeconds);
00378     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00379 
00380     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00381     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00382     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00383 
00384     /*#2*/
00385     curentTimeInSeconds = pal_osGetTime();
00386     setTimeInSeconds = curentTimeInSeconds - 50;
00387 
00388     setTimeValue = curentTimeInSeconds;
00389     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t *)&setTimeValue, sizeof(uint64_t), false);
00390     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00391 
00392     status = pal_osSetWeakTime(setTimeInSeconds);
00393     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00394 
00395     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00396     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00397     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00398 
00399 #if (PAL_USE_HW_RTC)
00400     //restore System time
00401     pal_plat_osSetRtcTime(testStartTime + PAL_RUNNING_TEST_TIME);
00402 #endif
00403 #else // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00404     TEST_IGNORE_MESSAGE("Ignored, PAL_INT_FLASH_NUM_SECTIONS not set to 2 or PAL_USE_INTERNAL_FLASH not set or MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT is not defined");
00405 #endif // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00406 
00407 }
00408 
00409 /*! \brief Check Strong Set Time.
00410 *
00411 * | # |    Step                        |   Expected  |
00412 * |---|--------------------------------|-------------|
00413 * | 1 | checking RTC flow - set new RTC time                                            | PAL_SUCCESS |
00414 * | 2 | checking RTC flow - not set RTC new time                                        | PAL_SUCCESS |
00415 * | 3 | checking RBP flow - set RBP SAVED TIME and LAST TIME BACK to new time           | PAL_SUCCESS |
00416 * | 4 | checking RBP flow - not set RBP SAVED TIME and LAST TIME BACK to new time       | PAL_SUCCESS |
00417 */
00418 TEST(pal_time, OsStrongSetTime)
00419 {
00420 #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00421     palStatus_t status;
00422     uint64_t setTimeInSeconds = 0;
00423     uint64_t curentTimeInSeconds = 0;
00424     uint64_t pal_Time = 0;
00425     uint64_t getTime = 0;
00426     size_t actualLenBytes = 0;
00427     uint64_t setTimeValue = 0;
00428 
00429 #if (PAL_USE_HW_RTC)
00430     //This code is to preserve system time
00431     uint64_t testStartTime = 0;
00432     status = pal_plat_osGetRtcTime(&testStartTime);
00433 #endif
00434 
00435     /*#1*/
00436 #if (PAL_USE_HW_RTC)
00437     pal_plat_osSetRtcTime(PAL_MIN_RTC_SET_TIME);
00438 #endif
00439     pal_osSetTime(PAL_MIN_SEC_FROM_EPOCH + PAL_SECONDS_PER_DAY * 100);
00440     curentTimeInSeconds = pal_osGetTime();
00441     setTimeInSeconds = curentTimeInSeconds;
00442 
00443 #if (PAL_USE_HW_RTC)
00444     uint64_t rtcTime = 0;
00445     rtcTime = curentTimeInSeconds - (50 + PAL_MINIMUM_RTC_LATENCY_SEC);
00446     status = pal_plat_osSetRtcTime(rtcTime);
00447     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00448 #endif//PAL_USE_HW_RTC
00449 
00450     status = pal_osSetStrongTime(setTimeInSeconds);
00451     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00452 
00453 #if (PAL_USE_HW_RTC)
00454     status = pal_plat_osGetRtcTime(&rtcTime);
00455     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00456     TEST_ASSERT_EQUAL_UINT64(rtcTime, setTimeInSeconds);
00457 #endif//PAL_USE_HW_RTC
00458 
00459     pal_Time = pal_osGetTime();
00460     if (pal_Time - setTimeInSeconds > 5)
00461     {
00462         status = PAL_ERR_GENERAL_BASE;
00463     }
00464     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00465 
00466     /*#2*/
00467     curentTimeInSeconds = pal_osGetTime();
00468     setTimeInSeconds = curentTimeInSeconds;
00469 
00470 #if (PAL_USE_HW_RTC)
00471     rtcTime = curentTimeInSeconds;
00472     status = pal_plat_osSetRtcTime(rtcTime - 50);
00473     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00474 #endif//PAL_USE_HW_RTC
00475 
00476     status = pal_osSetStrongTime(setTimeInSeconds);
00477 
00478 #if (PAL_USE_HW_RTC)
00479     status = pal_plat_osGetRtcTime(&rtcTime);
00480     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00481     TEST_ASSERT_NOT_EQUAL(rtcTime, setTimeInSeconds);
00482 #endif//PAL_USE_HW_RTC
00483 
00484     pal_Time = pal_osGetTime();
00485     if (pal_Time - setTimeInSeconds > 5){
00486         status = PAL_ERR_GENERAL_BASE;
00487     }
00488     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00489 
00490     /*#3*/
00491     curentTimeInSeconds = pal_osGetTime();
00492     setTimeInSeconds = curentTimeInSeconds;
00493     setTimeValue = curentTimeInSeconds - (PAL_MINIMUM_FORWARD_LATENCY_SEC + 1*PAL_ONE_SEC);
00494     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&setTimeValue, sizeof(uint64_t), false);
00495     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00496 
00497     status = pal_osSetStrongTime(setTimeInSeconds);
00498     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00499 
00500     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00501     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00502     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00503 
00504     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t),  &actualLenBytes);
00505     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00506     TEST_ASSERT_EQUAL_UINT64(getTime, setTimeInSeconds);
00507 
00508     pal_Time = pal_osGetTime();
00509     if (pal_Time - setTimeInSeconds > 5)
00510     {
00511         status = PAL_ERR_GENERAL_BASE;
00512     }
00513     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00514 
00515     /*#4*/
00516     curentTimeInSeconds = pal_osGetTime();
00517     setTimeInSeconds = curentTimeInSeconds;
00518 
00519     setTimeValue = curentTimeInSeconds - 5;
00520     status = storage_rbp_write(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t *)&setTimeValue, sizeof(uint64_t), false);
00521     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00522 
00523     status = storage_rbp_write(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t *)&setTimeValue, sizeof(uint64_t), false);
00524     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00525 
00526     status = pal_osSetStrongTime(setTimeInSeconds);
00527     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00528 
00529     status = storage_rbp_read(STORAGE_RBP_SAVED_TIME_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00530     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00531     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00532 
00533     status = storage_rbp_read(STORAGE_RBP_LAST_TIME_BACK_NAME, (uint8_t*)&getTime, sizeof(uint64_t), &actualLenBytes);
00534     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00535     TEST_ASSERT_NOT_EQUAL(getTime, setTimeInSeconds);
00536 
00537     pal_Time = pal_osGetTime();
00538     if (pal_Time - setTimeInSeconds > 5)
00539     {
00540         status = PAL_ERR_GENERAL_BASE;
00541     }
00542     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00543 
00544 #if (PAL_USE_HW_RTC)
00545     //restore System time
00546     pal_plat_osSetRtcTime(testStartTime + PAL_RUNNING_TEST_TIME);
00547 #endif
00548 #else // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00549     TEST_IGNORE_MESSAGE("Ignored, PAL_INT_FLASH_NUM_SECTIONS not set to 2 or PAL_USE_INTERNAL_FLASH not set or MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT is not defined");
00550 #endif // #if (((PAL_INT_FLASH_NUM_SECTIONS == 2) && PAL_USE_INTERNAL_FLASH) || defined (MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT))
00551 
00552 }