Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp
00001 #include <mbed.h> 00002 #include <stdarg.h> 00003 #include "EthernetInterface.h" 00004 00005 #define ECHO_SERVER_PORT 7 00006 #define BUFFER_QUANTITY (1024*1024) 00007 00008 struct MemoryUsage 00009 { 00010 uint32_t curFreeRam; 00011 uint32_t minFreeRam; 00012 uint32_t maxFreeRam; 00013 uint32_t avgFreeRam; 00014 uint32_t stackPointer; 00015 uint32_t cntSamples; 00016 }; 00017 00018 struct MemoryUsage memoryStats = {0,0xFFFFFFFF,0,0,0}; 00019 00020 int fakePrintf(void* pvHeapInfo, char const* pFormatString, ...) 00021 { 00022 struct MemoryUsage * memStat = (struct MemoryUsage *)pvHeapInfo; 00023 static const char* freeRamFormat = "%d bytes in"; 00024 va_list valist; 00025 00026 if(memcmp(pFormatString,freeRamFormat,strlen(freeRamFormat)) == 0) 00027 { 00028 va_start(valist, pFormatString); 00029 unsigned long freeSize = va_arg(valist, unsigned long); 00030 memStat->curFreeRam = freeSize; 00031 if(memStat->minFreeRam > freeSize) 00032 memStat->minFreeRam = freeSize; 00033 00034 if(memStat->maxFreeRam < freeSize) 00035 memStat->maxFreeRam = freeSize; 00036 00037 memStat->avgFreeRam = ((memStat->avgFreeRam * memStat->cntSamples) + freeSize)/(++memStat->cntSamples); 00038 } 00039 else 00040 { 00041 // ignore format 00042 } 00043 return 0; 00044 } 00045 00046 void inline printMemoryStats(void) 00047 { 00048 if(memoryStats.cntSamples == 1) 00049 printf("\n\n***** Initial Memory Report *****\n"); 00050 else 00051 printf("\n\n********* Memory Report *********\n"); 00052 00053 printf("Current free memory : %d bytes\n",memoryStats.curFreeRam); 00054 printf("Maximum free memory : %d bytes\n",memoryStats.maxFreeRam); 00055 printf("Minimum free memory : %d bytes\n",memoryStats.minFreeRam); 00056 printf("Average free memory : %d bytes\n",memoryStats.avgFreeRam); 00057 printf("Stack pointer address : %d\n",memoryStats.stackPointer); 00058 printf("****************************\n"); 00059 } 00060 00061 void inline memoryStamp(void) 00062 { 00063 __heapstats((__heapprt)fakePrintf, &memoryStats); 00064 } 00065 00066 void inline stackPtrSnapshot(void) 00067 { 00068 memoryStats.stackPointer = __current_sp(); 00069 } 00070 00071 int main() { 00072 00073 printf("Ethernet Interface memory test....\n"); 00074 00075 // Initial memory status 00076 memoryStamp(); 00077 stackPtrSnapshot(); // snapshot of the stack pointer before starting benchmark to see the SP. 00078 printMemoryStats(); 00079 00080 EthernetInterface eth; 00081 int connections = 0; 00082 eth.init(); //Use DHCP 00083 eth.connect(); 00084 printf("IP Address %s\n", eth.getIPAddress()); 00085 00086 TCPSocketServer server; 00087 server.bind(ECHO_SERVER_PORT); 00088 server.listen(); 00089 00090 while (true) { 00091 printf("\nWait for new connection...\n"); 00092 printf("Client number %d\n",++connections); 00093 TCPSocketConnection client; 00094 server.accept(client); 00095 client.set_blocking(false, 1500); // Timeout after (1.5)s 00096 printf("Connection from: %s\n", client.get_address()); 00097 char buffer[1024]; 00098 while (true) { 00099 00100 int dataReceived = 0; 00101 int dataSent = 0; 00102 00103 printf("\n\n\nStarting the receiving part...\n"); 00104 while(dataReceived < BUFFER_QUANTITY) 00105 { 00106 int n = client.receive(buffer, sizeof(buffer)); 00107 if (n <= 0) { 00108 printf("Receive error\n"); 00109 break; 00110 } 00111 dataReceived += n; 00112 memoryStamp(); 00113 } 00114 00115 00116 printf("Received : %d bytes\nExpected : %d bytes\n",dataReceived,BUFFER_QUANTITY); 00117 if(dataReceived < BUFFER_QUANTITY) 00118 { 00119 printf("Receiving part of the test has failed. Exiting connection.\n"); 00120 break; 00121 } 00122 else{ 00123 printf("Receiving has passed...\n"); 00124 } 00125 00126 printf("\n\n\nStarting the sending part...\n"); 00127 while(dataSent < BUFFER_QUANTITY) 00128 { 00129 int n = client.send_all(buffer, sizeof(buffer)); 00130 if (n <= 0) { 00131 printf("Send error\n"); 00132 break; 00133 } 00134 dataSent += n; 00135 memoryStamp(); 00136 } 00137 00138 printf("Sent : %d bytes\nExpected : %d bytes\n",dataSent,BUFFER_QUANTITY); 00139 if(dataSent < BUFFER_QUANTITY) 00140 { 00141 printf("Sending part of the test has failed. Exiting connection.\n"); 00142 break; 00143 } 00144 else 00145 { 00146 printf("Sending test has passed...\n"); 00147 } 00148 00149 00150 printf("\n\n\nStarting echo part...\n"); 00151 dataReceived = dataSent = 0; 00152 while((dataReceived+dataSent) < 2*BUFFER_QUANTITY) 00153 { 00154 int n = client.receive(buffer, sizeof(buffer)); 00155 if (n <= 0) { 00156 printf("Receive error\n"); 00157 break; 00158 } 00159 dataReceived += n; 00160 00161 n = client.send_all(buffer, n); 00162 if (n <= 0) { 00163 printf("Send error\n"); 00164 break; 00165 } 00166 dataSent += n; 00167 memoryStamp(); 00168 } 00169 00170 printf("Echo size : %d bytes\nExpected : %d bytes\n",(dataReceived+dataSent),2*BUFFER_QUANTITY); 00171 if((dataReceived+dataSent) < 2*BUFFER_QUANTITY) 00172 { 00173 printf("Echo test has failed.Exiting connection...\n"); 00174 } 00175 else 00176 { 00177 printf("Echo test has passed...\n"); 00178 } 00179 break; 00180 } 00181 client.close(); 00182 printf("Test was finished...\n"); 00183 printMemoryStats(); 00184 } 00185 00186 return 0; 00187 }
Generated on Wed Jul 20 2022 00:34:26 by
1.7.2