Daniele Lacamera / Mbed 2 deprecated Pico_Robustness_Test

Dependencies:   PicoTCP lpc1768-picotcp-eth mbed-rtos mbed

Fork of Pico_Robustness_Test by TASS Belgium

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <stdarg.h>
00003 #include "EthernetInterface.h"
00004 
00005 #define msgdbg(...)
00006 
00007 //#define msgdbg printf
00008 
00009 
00010 #define SENDING_RETRIES    3u
00011 #define READING_RETRIES    7u
00012 #define DHCP_RETRIES       10u
00013 #define BUFFER_SIZE        256
00014 
00015 #define NUMBER_OF_HOSTS   3u
00016 
00017 #define MBED_HOST               "www.mbed.org"
00018 #define MBED_HOST_REQUEST       "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"
00019 
00020 #define MEDIAFAX_HOST           "www.mediafax.ro"
00021 #define MEDIAFAX_HOST_REQUEST   "GET / HTTP/1.1\nHost: www.mediafax.ro\n\n"
00022 
00023 #define NXP_HOST                "www.nxp.com"
00024 #define NXP_HOST_REQUEST        "GET /documents/user_manual/UM10360.pdf HTTP/1.0\nHost: www.nxp.com\n\n"
00025 
00026 int totalTime = 0;
00027 
00028 struct WebStats
00029 {
00030     char request[70];
00031     char host[20];
00032     int avgDuration;
00033     int maxReceived;
00034     int minReceived;
00035     int snapshots;
00036 };
00037 
00038 struct WebStats webStatistics[NUMBER_OF_HOSTS]= 
00039 {
00040     {MBED_HOST_REQUEST,MBED_HOST,0,0,0x7FFFFFFF,0},
00041     {MEDIAFAX_HOST_REQUEST,MEDIAFAX_HOST,0,0,0x7FFFFFFF,0},
00042     {NXP_HOST_REQUEST,NXP_HOST,0,0,0x7FFFFFFF,0}
00043 };
00044 
00045 void inline printMemoryStats(void);
00046 void printStatistics(void)
00047 {
00048     printf("\nTotal Time : %f seconds\n",(float)totalTime/1000.0f);    
00049     for(int i=0;i<NUMBER_OF_HOSTS;i++)
00050     {
00051         printf("Host number : %d | %s\n",i, webStatistics[i].host);
00052         printf("MaxRecv : %d\n",webStatistics[i].maxReceived);
00053         printf("MinRecv : %d\n",webStatistics[i].minReceived);
00054         printf("Avg duration : %d ms\n",webStatistics[i].avgDuration);
00055         printf("Total snapshots : %d\n\n",webStatistics[i].snapshots);
00056     }
00057     printMemoryStats();    
00058 }
00059 
00060 struct MemoryUsage
00061 {
00062     uint32_t curFreeRam;
00063     uint32_t minFreeRam;
00064     uint32_t maxFreeRam;
00065     uint32_t avgFreeRam;
00066     uint32_t stackPointer;
00067     uint32_t cntSamples;
00068 };
00069 
00070 struct MemoryUsage memoryStats = {0,0xFFFFFFFF,0,0,0};
00071 
00072 int fakePrintf(void* pvHeapInfo, char const* pFormatString, ...)
00073 {
00074     struct MemoryUsage * memStat = (struct MemoryUsage *)pvHeapInfo;
00075     static const char* freeRamFormat = "%d bytes in";
00076     va_list valist;
00077     
00078     if(memcmp(pFormatString,freeRamFormat,strlen(freeRamFormat)) == 0)
00079     {
00080         va_start(valist, pFormatString);
00081         unsigned long freeSize = va_arg(valist, unsigned long);
00082         memStat->curFreeRam = freeSize;
00083         if(memStat->minFreeRam > freeSize)
00084             memStat->minFreeRam = freeSize;
00085         
00086         if(memStat->maxFreeRam < freeSize)
00087             memStat->maxFreeRam = freeSize;
00088             
00089         memStat->avgFreeRam = ((memStat->avgFreeRam * memStat->cntSamples) + freeSize)/(++memStat->cntSamples);
00090     }
00091     else
00092     {
00093         // ignore format
00094     }
00095     return 0;
00096 }
00097 
00098 void inline printMemoryStats(void)
00099 {
00100     if(memoryStats.cntSamples == 1)
00101         printf("\n\n***** Initial Memory Report *****\n");
00102     else
00103         printf("\n\n********* Memory Report *********\n");
00104     
00105     printf("Current free memory : %d bytes\n",memoryStats.curFreeRam);
00106     printf("Maximum free memory : %d bytes\n",memoryStats.maxFreeRam);
00107     printf("Minimum free memory : %d bytes\n",memoryStats.minFreeRam);
00108     printf("Average free memory : %d bytes\n",memoryStats.avgFreeRam);
00109     printf("****************************\n");
00110 }
00111 
00112 void inline memoryStamp(void)
00113 {
00114     __heapstats((__heapprt)fakePrintf, &memoryStats);
00115 }
00116 
00117 
00118 int main() {
00119     
00120     int retries = 0;
00121     EthernetInterface eth;
00122     TCPSocketConnection client;
00123     
00124     printf("Initialising...\n");
00125 
00126     // use DHCP
00127     eth.init();
00128 
00129     // attempt DHCP and if timing out then try again
00130     while (eth.connect()) {
00131         retries++;
00132         printf("[%d] DHCP timeout %d\n",PICO_TIME_MS(),retries);
00133         if(retries >= DHCP_RETRIES)
00134         {
00135             printf("DHCP failed. Bailing out..\n");
00136             goto failure;
00137         }
00138     };
00139     
00140     printf("[%d] Starting the robustness test...\n",PICO_TIME_MS());
00141     
00142     while(1) 
00143     {
00144         for(int i=0;i<NUMBER_OF_HOSTS;i++,client.close())
00145         {
00146             int retries = 0;
00147             int received = 0;
00148             int time = 0;
00149             char tmpBuffer[BUFFER_SIZE];
00150             int ret;
00151             
00152             printf("Mbed --> %s\n",webStatistics[i].host);
00153             
00154             time = PICO_TIME_MS();
00155             // connecting
00156             if (client.connect(webStatistics[i].host, 80)) {
00157                 printf("Failed to connect to : %s\n",webStatistics[i].host);
00158                 continue;
00159             }                    
00160             
00161             client.set_blocking(false,1500);
00162             retries = 0;
00163             
00164             // sending request
00165 
00166             ret = client.send_all(webStatistics[i].request,strlen(webStatistics[i].request));
00167             if (ret <= 0) 
00168             {
00169                 printf("This test failed big time, ret=%d, err=%d!!\n", ret, pico_err);
00170                 while(1);;;
00171             }
00172             
00173             retries = 0;
00174             // start reading
00175             while(true)
00176             {
00177                 ret = client.receive(tmpBuffer,sizeof(tmpBuffer));
00178                 if (ret == 0) {
00179                     printf("Read timeout: ret = %d, err = %d, retry = %d\n", ret, pico_err, retries);
00180                     retries++;
00181                 } else if (ret < 0) {
00182                     if (pico_err != PICO_ERR_ESHUTDOWN) {
00183                         printf("Read error, bailing out. Ret = %d, err = %d\n", ret, pico_err);
00184                         while(1);;;
00185                     }
00186                     client.close();
00187                     break;
00188                 } else {
00189                     received += ret;
00190                 }
00191                 
00192                 if(retries == READING_RETRIES) {
00193                     printf("Read operation failed too many times. Giving up.\n");
00194                     break;
00195                 }      
00196             }
00197             
00198             
00199             
00200             //Snapshot!
00201             time = PICO_TIME_MS() - time;
00202             
00203             if(webStatistics[i].maxReceived < received)
00204             {
00205                 webStatistics[i].maxReceived = received;
00206             }
00207             
00208             if(webStatistics[i].minReceived > received)
00209             {
00210                 webStatistics[i].minReceived = received;
00211             }
00212             printf("Received : %d bytes\n",received);
00213             if (received <= 0) {
00214                printf("Test FAILED!\n"); 
00215                while(1);;;  
00216             }
00217             webStatistics[i].avgDuration = ((webStatistics[i].avgDuration *(webStatistics[i].snapshots++) ) + time)/webStatistics[i].snapshots;
00218             totalTime += time;
00219             memoryStamp();
00220         }
00221         
00222         printStatistics();
00223         Thread::wait(500);
00224     }
00225     failure:    
00226     printf("Fatal error. Main thread is inactive\n");
00227     while(1);
00228 }