Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_main.c Source File

test_main.c

00001 
00002 /*******************************************************************************
00003  * Copyright 2016, 2017 ARM Ltd.
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  *******************************************************************************/
00017 
00018 #include "stdio.h"
00019 
00020 #include "PlatIncludes.h"
00021 #include "test_runners.h"
00022 #include "pal_BSP.h"
00023 #include "pal.h"
00024 #include "mbed_trace.h"
00025 #include "unity.h"
00026 #include "unity_fixture.h"
00027 
00028 extern struct _Unity Unity;
00029 
00030 #define PAL_TEST_STATUS_FILE_LOCATION "/tstSts"
00031 #define PAL_TEST_STATUS_FILE_DATA_MAX_SIZE 128
00032 
00033 void * g_palTestNetworkInterface = NULL;
00034 void * g_palTestTLSInterfaceCTX = NULL;
00035 
00036 
00037 pal_args_t g_args; // defiend as global so it could persist 
00038                    // during task execution on FreeRTOS
00039 
00040 #ifdef DEBUG
00041 #define PAL_TESTS_LOG_LEVEL ((uint8_t)((TRACE_MASK_LEVEL & TRACE_ACTIVE_LEVEL_ALL) | (TRACE_MASK_CONFIG & TRACE_CARRIAGE_RETURN)))
00042 #else
00043 #define PAL_TESTS_LOG_LEVEL ((uint8_t)((TRACE_MASK_LEVEL & TRACE_ACTIVE_LEVEL_ERROR) | (TRACE_MASK_CONFIG & TRACE_CARRIAGE_RETURN)))
00044 #endif
00045 
00046 
00047 palTestsStatusData_t palTestStatus = {-1,-1,-1,0,0,0};
00048 
00049 
00050 palStatus_t getPalTestStatus(void)
00051 {
00052     palStatus_t status = PAL_SUCCESS, status2 = PAL_SUCCESS;
00053     char filePath[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00054     palFileDescriptor_t fd = 0;
00055     size_t dataSizeWritten = 0;
00056     char data[128] = {0};
00057   
00058 
00059     status = pal_fsGetMountPoint(PAL_FS_PARTITION_PRIMARY, PAL_MAX_FILE_AND_FOLDER_LENGTH, filePath);
00060     if (PAL_SUCCESS == status) 
00061     {
00062         strncat(filePath,PAL_TEST_STATUS_FILE_LOCATION,PAL_MAX_FILE_AND_FOLDER_LENGTH - strlen(filePath));
00063         status = pal_fsFopen(filePath, PAL_FS_FLAG_READONLY, &fd);
00064          if (PAL_SUCCESS == status)
00065          {
00066 
00067              status =  pal_fsFread(&fd, (void *)data, PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, &dataSizeWritten);
00068              if ((PAL_SUCCESS == status) && (dataSizeWritten > 0))
00069              {
00070                  printf("reading DATA into test\r\n");
00071                  sscanf(data,"%i %i %i %llu %llu %llu", &palTestStatus.module, &palTestStatus.test, &palTestStatus.inner, &palTestStatus.numOfTestsFailures, &palTestStatus.numberOfTests, &palTestStatus.numberOfIgnoredTests);
00072              }
00073              status2 = pal_fsFclose(&fd);
00074              if (PAL_SUCCESS != status2) 
00075              {
00076                  PAL_LOG(ERR,"Failed to close data file of test status after read");
00077              }
00078              status2 = pal_fsUnlink(filePath);
00079              if (PAL_SUCCESS != status2) 
00080              {
00081                  PAL_LOG(ERR,"Failed to delete data file of test status after read");
00082              }
00083          }
00084         else if (PAL_ERR_FS_NO_FILE == status) {
00085             //this is not an error... in most times there will be no file
00086             status = PAL_SUCCESS;
00087         }
00088     }
00089 
00090     PAL_LOG(DBG,"*********************************\n"
00091             "** Test status:                **\n"
00092             "** Module %d                   **\n"
00093             "** Test %d                     **\n"
00094             "** Inner %d                    **\n"
00095             "** num of tests failures %llu  **\n"
00096             "** num of tests %llu           **\n"
00097             "** num of ignored tests %llu   **\n"
00098             "*********************************\n",
00099             palTestStatus.module, palTestStatus.test, palTestStatus.inner,
00100             palTestStatus.numOfTestsFailures, palTestStatus.numberOfTests,
00101             palTestStatus.numberOfIgnoredTests);
00102 
00103     return status;
00104 }
00105 
00106 void updatePalTestStatusAfterReboot(void)
00107 {
00108     if (palTestStatus.numberOfTests > 0)
00109     {
00110         Unity.TestFailures = palTestStatus.numOfTestsFailures;
00111         Unity.NumberOfTests = palTestStatus.numberOfTests;
00112         Unity.CurrentTestIgnored =palTestStatus.numberOfIgnoredTests;
00113         PAL_LOG(DBG,"Unity number of tests was updated\r\n");
00114     }
00115 }
00116 
00117 
00118 palStatus_t setPalTestStatus(palTestsStatusData_t palRebootTestStatus)
00119 {
00120     palStatus_t status = PAL_SUCCESS, status2 = PAL_SUCCESS;;
00121     char filePath[PAL_MAX_FILE_AND_FOLDER_LENGTH] = {0};
00122     palFileDescriptor_t fd = 0;
00123     size_t dataSizeWritten = 0;
00124     char data[PAL_TEST_STATUS_FILE_DATA_MAX_SIZE] = {0};
00125     
00126 
00127     status = pal_fsGetMountPoint(PAL_FS_PARTITION_PRIMARY, PAL_MAX_FILE_AND_FOLDER_LENGTH, filePath);
00128     if (PAL_SUCCESS == status) 
00129     {
00130         strncat(filePath,PAL_TEST_STATUS_FILE_LOCATION,PAL_MAX_FILE_AND_FOLDER_LENGTH - strlen(filePath));
00131         status = pal_fsFopen(filePath, PAL_FS_FLAG_READWRITETRUNC, &fd);
00132          if (PAL_SUCCESS == status)
00133          {
00134              snprintf((char *)data,PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, "%d %d %d %llu %llu %llu ", palRebootTestStatus.module, palRebootTestStatus.test, palRebootTestStatus.inner, palRebootTestStatus.numOfTestsFailures, palRebootTestStatus.numberOfTests, palRebootTestStatus.numberOfIgnoredTests);
00135              status =  pal_fsFwrite(&fd, (void *)data, PAL_TEST_STATUS_FILE_DATA_MAX_SIZE, &dataSizeWritten);
00136              pal_fsFclose(&fd);
00137              if (PAL_SUCCESS != status2) 
00138              {
00139                  PAL_LOG(ERR,"Failed to close data file of test status after write");
00140              }
00141          }
00142     }
00143     return status;
00144 }
00145 
00146 
00147 palStatus_t palTestReboot(palTestModules_t module ,palTestSOTPTests_t test )
00148 {
00149     palStatus_t status = PAL_SUCCESS;
00150     palTestsStatusData_t palRebootTestStatus;
00151     palRebootTestStatus.module = module;
00152     palRebootTestStatus.test = test;
00153     palRebootTestStatus.inner = 1;
00154     palRebootTestStatus.numberOfTests = Unity.NumberOfTests;
00155     palRebootTestStatus.numOfTestsFailures = Unity.TestFailures;
00156     palRebootTestStatus.numberOfIgnoredTests = Unity.CurrentTestIgnored;
00157 
00158     status = setPalTestStatus(palRebootTestStatus);
00159     if (PAL_SUCCESS != status)
00160     {
00161         PAL_LOG(ERR,"Failed to set test status before reboot");
00162     }
00163     else
00164     {
00165         pal_osReboot();
00166     }
00167     return status;
00168 }
00169 
00170 
00171 
00172 
00173 
00174 void TEST_pal_all_GROUPS_RUNNER(void)
00175 {
00176     PRINT_MEMORY_STATS;
00177     switch (palTestStatus.module) // fall through is in design
00178     {
00179         case -1:
00180             //TEST_pal_sanity_GROUP_RUNNER(); // always run this at least once
00181         case PAL_TEST_MODULE_SOTP:
00182             PRINT_MEMORY_STATS;
00183             /**
00184              *  CAUTION:THIS TEST MUDOLE REBOOTS THE SYSTEM
00185              *  THIS TEST MUST BE 1ST!!!!!
00186              *  DO NOT MOVE THIS TEST!!!!!
00187             */
00188             #ifndef PAL_SKIP_TEST_MODULE_SOTP
00189                 TEST_pal_SOTP_GROUP_RUNNER();
00190             #endif
00191         case PAL_TEST_MODULE_RTOS:
00192             PRINT_MEMORY_STATS;
00193             #ifndef PAL_SKIP_TEST_MODULE_RTOS
00194                 TEST_pal_rtos_GROUP_RUNNER();
00195             #endif
00196         case PAL_TEST_MODULE_SOCKET:
00197             PRINT_MEMORY_STATS;
00198             #ifndef PAL_SKIP_TEST_MODULE_NETWORK
00199                 TEST_pal_socket_GROUP_RUNNER();
00200             #endif
00201         case PAL_TEST_MODULE_CRYPTO:
00202             PRINT_MEMORY_STATS;
00203             #ifndef PAL_SKIP_TEST_MODULE_CRYPTO
00204                 TEST_pal_crypto_GROUP_RUNNER();
00205             #endif
00206         case PAL_TEST_MODULE_FILESYSTEM:
00207             PRINT_MEMORY_STATS;
00208             #ifndef PAL_SKIP_TEST_MODULE_FILESYSTEM
00209                 TEST_pal_fileSystem_GROUP_RUNNER();
00210             #endif
00211         case PAL_TEST_MODULE_UPDATE:
00212             PRINT_MEMORY_STATS;
00213             #ifndef PAL_SKIP_TEST_MODULE_UPDATE
00214                 TEST_pal_update_GROUP_RUNNER();
00215             #endif
00216         case PAL_TEST_MODULE_INTERNALFLASH:
00217             PRINT_MEMORY_STATS;
00218             #ifndef PAL_SKIP_TEST_MODULE_INTERNALFLASH
00219                 TEST_pal_internalFlash_GROUP_RUNNER();
00220             #endif
00221         case PAL_TEST_MODULE_TLS:
00222             PRINT_MEMORY_STATS;
00223             #ifndef PAL_SKIP_TEST_MODULE_TLS
00224                 TEST_pal_tls_GROUP_RUNNER();
00225             #endif
00226             PRINT_MEMORY_STATS;
00227             break;
00228         default:
00229             PAL_PRINTF("this should not happen!!! Error Error Error");
00230     }
00231 }
00232 
00233 
00234 
00235 void palTestMain(palTestModules_t modules,void* network)
00236 {
00237     const char * myargv[] = {"app","-v"};
00238     g_palTestNetworkInterface = network; 
00239     g_palTestTLSInterfaceCTX = network;
00240     mbed_trace_init();
00241     mbed_trace_config_set(PAL_TESTS_LOG_LEVEL);
00242     palStatus_t getTestStatusReturnValue = getPalTestStatus();
00243     if (PAL_SUCCESS != getTestStatusReturnValue) 
00244     {
00245         PAL_LOG(ERR,"%s: Failed to get current status of tests 0x%" PRIu32 "\r\n",__FUNCTION__,getTestStatusReturnValue);
00246     }
00247 
00248     UnityPrint("*****PAL_TEST_START*****");
00249     UNITY_PRINT_EOL();
00250     switch (modules) 
00251     {
00252         case PAL_TEST_MODULE_ALL:
00253         {
00254             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_all_GROUPS_RUNNER);
00255             break;
00256         }
00257         case PAL_TEST_MODULE_SOTP:
00258         {
00259             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_SOTP_GROUP_RUNNER);
00260             break;
00261         }
00262 
00263         case PAL_TEST_MODULE_RTOS:
00264         {
00265             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_rtos_GROUP_RUNNER);
00266             break;
00267         }
00268 
00269         case PAL_TEST_MODULE_SOCKET:
00270         {
00271             UnityMain(sizeof(myargv)/ sizeof(myargv[0]), myargv, TEST_pal_socket_GROUP_RUNNER);
00272             break;
00273         }
00274 
00275         case PAL_TEST_MODULE_CRYPTO:
00276         {
00277             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_crypto_GROUP_RUNNER);
00278             break;
00279         }
00280 
00281         case PAL_TEST_MODULE_FILESYSTEM:
00282         {
00283             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_fileSystem_GROUP_RUNNER);
00284             break;
00285         }
00286 
00287         case PAL_TEST_MODULE_UPDATE:
00288         {
00289             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_update_GROUP_RUNNER);
00290             break;
00291         }
00292 
00293         case PAL_TEST_MODULE_INTERNALFLASH:
00294         {
00295             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_internalFlash_GROUP_RUNNER);
00296             break;
00297         }
00298 
00299         case PAL_TEST_MODULE_TLS:
00300         {
00301             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_tls_GROUP_RUNNER);
00302             break;
00303         }
00304         
00305         case PAL_TEST_MODULE_SANITY:
00306         {
00307             UnityMain(sizeof(myargv) / sizeof(myargv[0]), myargv, TEST_pal_sanity_GROUP_RUNNER);
00308             break;
00309         }
00310 
00311         default:
00312         {
00313             UnityPrint("*****ERROR WRONG TEST SUITE WAS CHOOSEN*****");                
00314             break;
00315         }
00316 
00317     }
00318     UnityPrint("*****PAL_TEST_END*****");
00319     UNITY_PRINT_EOL();
00320 
00321     mbed_trace_free();
00322 
00323 }
00324 
00325 void palAllTestMain(void* network)
00326 {
00327     palTestMain(PAL_TEST_MODULE_ALL, network);
00328 }
00329 
00330 void palFileSystemTestMain(void* network)
00331 {
00332     palTestMain(PAL_TEST_MODULE_FILESYSTEM, network);
00333 }
00334 
00335 void palNetworkTestMain(void* network)
00336 {
00337    palTestMain(PAL_TEST_MODULE_SOCKET, network); 
00338 }
00339 
00340 void palCryptoTestMain(void* network)
00341 {
00342    palTestMain(PAL_TEST_MODULE_CRYPTO, network); 
00343 }
00344 
00345 void palRTOSTestMain(void* network)
00346 {
00347    palTestMain(PAL_TEST_MODULE_RTOS, network); 
00348 }
00349 
00350 void palStorageTestMain(void* network)
00351 {
00352    palTestMain(PAL_TEST_MODULE_INTERNALFLASH, network); 
00353 }
00354 
00355 void palTLSTestMain(void* network)
00356 {
00357    palTestMain(PAL_TEST_MODULE_TLS, network); 
00358 }
00359 
00360 void palUpdateTestMain(void* network)
00361 {
00362    palTestMain(PAL_TEST_MODULE_UPDATE, network); 
00363 }
00364 
00365 void palSOTPTestMain(void* network)
00366 {
00367     palTestMain(PAL_TEST_MODULE_SOTP, network); 
00368 }
00369 
00370 void palSanityTestMain(void* network)
00371 {
00372      palTestMain(PAL_TEST_MODULE_SANITY, network); 
00373 }
00374 
00375 
00376 
00377