program to test the sending of small packets of data from memory over EthernetInterface using mbed rtos

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of testEthIntfTCPSocket by S K UCI

Committer:
uci1
Date:
Fri Sep 27 23:16:09 2013 +0000
Revision:
2:a625f2ff0174
Parent:
1:6b90cbfe0c9e
program for testing the sending of small packets of data over EthernetInterface with mbed rtos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:53afc51b78a9 1 #include "mbed.h"
uci1 0:53afc51b78a9 2
uci1 0:53afc51b78a9 3 #include "TCPSocketConnection.h"
uci1 0:53afc51b78a9 4 #include "EthernetInterface.h"
uci1 0:53afc51b78a9 5 #include "string"
uci1 0:53afc51b78a9 6 #include "fakeData.h"
uci1 0:53afc51b78a9 7
uci1 0:53afc51b78a9 8 //#define DEBUG // turn on debug output if defined (note this slows down processing due to serial baud rate!)
uci1 0:53afc51b78a9 9 //#define PRINT_DOIO_RESULT // print the number of bytes DoIO returns
uci1 0:53afc51b78a9 10
uci1 0:53afc51b78a9 11 DigitalOut led1(LED1);
uci1 0:53afc51b78a9 12 DigitalOut led2(LED2);
uci1 0:53afc51b78a9 13 DigitalOut led3(LED3);
uci1 0:53afc51b78a9 14 DigitalOut led4(LED4);
uci1 0:53afc51b78a9 15
uci1 0:53afc51b78a9 16 static const char* kRserv = "128.195.204.151";
uci1 0:53afc51b78a9 17 static const unsigned short kRport = 6666;
uci1 0:53afc51b78a9 18
uci1 0:53afc51b78a9 19 static const char* kMyIp = "128.195.204.148";
uci1 0:53afc51b78a9 20 static const char* kMyMask = "255.255.255.0";
uci1 0:53afc51b78a9 21 static const char* kMyGate = "128.195.204.1";
uci1 0:53afc51b78a9 22
uci1 0:53afc51b78a9 23 static const unsigned int kBSTime = 946684800u; // 1/1/2000 00:00:00 UTC
uci1 0:53afc51b78a9 24 static const int kSecsPerDay = 3600*24;
uci1 0:53afc51b78a9 25 static const int kBuffSize = 1024;
uci1 0:53afc51b78a9 26 // all chunks must be smaller than kBuffSize
uci1 0:53afc51b78a9 27 static const int kNCsizes = 5;
uci1 0:53afc51b78a9 28 static const int gChunkSize[kNCsizes] = { 9, 26, 711, 40, 432 };
uci1 0:53afc51b78a9 29 static const int kTotFakeDat = 2097152; // if sending fake data, send 2 MB total
uci1 0:53afc51b78a9 30 static const int kTimeout = 300; // seconds
uci1 0:53afc51b78a9 31
uci1 0:53afc51b78a9 32 static char gBuff[kBuffSize];
uci1 0:53afc51b78a9 33 static LocalFileSystem local("local");
uci1 0:53afc51b78a9 34 static EthernetInterface* gEth(0);
uci1 0:53afc51b78a9 35 static TCPSocketConnection* gSock(0);
uci1 0:53afc51b78a9 36 static Serial gCpu( USBTX, USBRX );
uci1 0:53afc51b78a9 37
uci1 0:53afc51b78a9 38 // function pointer to send, receive, etc
uci1 0:53afc51b78a9 39 typedef int (TCPSocketConnection::*TCPSendRecv)(char*, int);
uci1 0:53afc51b78a9 40 #define CALL_MEMBER_FN(object,ptrToMember) ((object).*(ptrToMember))
uci1 0:53afc51b78a9 41
uci1 0:53afc51b78a9 42 // forward declarations
uci1 0:53afc51b78a9 43 bool Connect(const int timeout);
uci1 0:53afc51b78a9 44 void SendData(FILE* inf, const int timeout);
uci1 0:53afc51b78a9 45 FILE* OpenSDFile(const char* name, const char* mode);
uci1 0:53afc51b78a9 46 void SendAllFakeData(const int totalBytes, const int timeout);
uci1 0:53afc51b78a9 47 void SendAllFiles(const int timeout);
uci1 0:53afc51b78a9 48 bool IsTimedOut(const int timeout_clock);
uci1 0:53afc51b78a9 49 void dispStrBytes(const char* const s, const int len);
uci1 0:53afc51b78a9 50 int DoIO(char* const data,
uci1 0:53afc51b78a9 51 const int length,
uci1 0:53afc51b78a9 52 const int timeout_clock,
uci1 0:53afc51b78a9 53 TCPSendRecv fcn);
uci1 0:53afc51b78a9 54 int ReceiveAll(char* const buf, const int mlen, const int timeout_clock);
uci1 0:53afc51b78a9 55 int SendAll(char* const data, const int length, const int timeout_clock);
uci1 0:53afc51b78a9 56
uci1 0:53afc51b78a9 57
uci1 0:53afc51b78a9 58 int main() {
uci1 0:53afc51b78a9 59 led1=1;
uci1 0:53afc51b78a9 60 set_time(kBSTime);
uci1 0:53afc51b78a9 61 if (Connect(time(0)+kTimeout)) {
uci1 0:53afc51b78a9 62 led2=1;
uci1 0:53afc51b78a9 63 SendAllFakeData(kTotFakeDat, time(0)+kTimeout);
uci1 0:53afc51b78a9 64 } else {
uci1 0:53afc51b78a9 65 led4=1;
uci1 0:53afc51b78a9 66 }
uci1 0:53afc51b78a9 67 if (gSock!=0) {
uci1 0:53afc51b78a9 68 gSock->close();
uci1 0:53afc51b78a9 69 }
uci1 0:53afc51b78a9 70 if (gEth!=0) {
uci1 0:53afc51b78a9 71 gEth->disconnect();
uci1 0:53afc51b78a9 72 }
uci1 0:53afc51b78a9 73 while (true) {
uci1 2:a625f2ff0174 74 led1=0; Thread::wait(500); led1=1; Thread::wait(500);
uci1 0:53afc51b78a9 75 }
uci1 0:53afc51b78a9 76 }
uci1 0:53afc51b78a9 77
uci1 0:53afc51b78a9 78 bool Connect(const int timeout) {
uci1 0:53afc51b78a9 79
uci1 0:53afc51b78a9 80 #ifdef DEBUG
uci1 0:53afc51b78a9 81 printf("Connect\r\n");
uci1 0:53afc51b78a9 82 #endif
uci1 0:53afc51b78a9 83
uci1 0:53afc51b78a9 84 gEth = new EthernetInterface;
uci1 0:53afc51b78a9 85 gSock = new TCPSocketConnection;
uci1 0:53afc51b78a9 86
uci1 2:a625f2ff0174 87 #ifdef DEBUG
uci1 2:a625f2ff0174 88 printf("made eth and sock\r\n");
uci1 2:a625f2ff0174 89 #endif
uci1 2:a625f2ff0174 90
uci1 0:53afc51b78a9 91 gEth->init(kMyIp, kMyMask, kMyGate);
uci1 0:53afc51b78a9 92
uci1 2:a625f2ff0174 93 #ifdef DEBUG
uci1 2:a625f2ff0174 94 printf("called init\r\n");
uci1 2:a625f2ff0174 95 #endif
uci1 2:a625f2ff0174 96
uci1 0:53afc51b78a9 97 bool isConn = false;
uci1 0:53afc51b78a9 98
uci1 0:53afc51b78a9 99 while ( (isConn==false) && (IsTimedOut(timeout)==false) ) {
uci1 2:a625f2ff0174 100 Thread::wait(250); // TODO : change wait!!
uci1 0:53afc51b78a9 101 isConn = (gEth->connect()==0);
uci1 0:53afc51b78a9 102 }
uci1 0:53afc51b78a9 103 #ifdef DEBUG
uci1 0:53afc51b78a9 104 printf("eth isConn=%d\r\n",(int)isConn);
uci1 0:53afc51b78a9 105 #endif
uci1 0:53afc51b78a9 106
uci1 0:53afc51b78a9 107 isConn=false;
uci1 0:53afc51b78a9 108 while ( (isConn==false) && (IsTimedOut(timeout)==false) ) {
uci1 2:a625f2ff0174 109 Thread::wait(250);
uci1 0:53afc51b78a9 110 isConn = (gSock->connect(kRserv, kRport)==0);
uci1 0:53afc51b78a9 111 }
uci1 0:53afc51b78a9 112 #ifdef DEBUG
uci1 0:53afc51b78a9 113 printf("sock isConn=%d\r\n",(int)isConn);
uci1 0:53afc51b78a9 114 #endif
uci1 0:53afc51b78a9 115 return isConn;
uci1 0:53afc51b78a9 116 }
uci1 0:53afc51b78a9 117
uci1 0:53afc51b78a9 118 void SendAllFakeData(const int totalBytes, const int timeout) {
uci1 0:53afc51b78a9 119 // send the fake data in chunks
uci1 0:53afc51b78a9 120
uci1 0:53afc51b78a9 121 const int* const csend = gChunkSize + kNCsizes;
uci1 0:53afc51b78a9 122 const int* cs = gChunkSize;
uci1 0:53afc51b78a9 123
uci1 0:53afc51b78a9 124 int br=0; // bytes read/sent so far
uci1 0:53afc51b78a9 125 int tcs=0; // this chunk size
uci1 0:53afc51b78a9 126 while ( br<totalBytes ) {
uci1 0:53afc51b78a9 127
uci1 0:53afc51b78a9 128 // read a chunk
uci1 0:53afc51b78a9 129 tcs = (*cs < kFakeDataSize) ? (*cs) : kFakeDataSize;
uci1 0:53afc51b78a9 130 memcpy(gBuff, gFakeData, tcs);
uci1 0:53afc51b78a9 131
uci1 0:53afc51b78a9 132 // send this chunk
uci1 0:53afc51b78a9 133 br += SendAll(gBuff, tcs, timeout);
uci1 0:53afc51b78a9 134
uci1 0:53afc51b78a9 135 // change chunk size
uci1 0:53afc51b78a9 136 ++cs;
uci1 0:53afc51b78a9 137 if (cs==csend) {
uci1 0:53afc51b78a9 138 cs = gChunkSize;
uci1 0:53afc51b78a9 139 }
uci1 0:53afc51b78a9 140 }
uci1 0:53afc51b78a9 141 }
uci1 0:53afc51b78a9 142
uci1 0:53afc51b78a9 143 bool IsTimedOut(const int timeout_clock) {
uci1 0:53afc51b78a9 144 if (timeout_clock==0) {
uci1 0:53afc51b78a9 145 // for not obeying timeout option
uci1 0:53afc51b78a9 146 return false;
uci1 0:53afc51b78a9 147 } else {
uci1 0:53afc51b78a9 148 const int ct = time(0);
uci1 0:53afc51b78a9 149 if ( (ct==0) ||
uci1 0:53afc51b78a9 150 (abs(static_cast<double>(timeout_clock-ct))>kSecsPerDay) ) {
uci1 0:53afc51b78a9 151 // clock problems! timeout now.
uci1 0:53afc51b78a9 152 #ifdef DEBUG
uci1 0:53afc51b78a9 153 printf("clock problem. timing out\r\n");
uci1 0:53afc51b78a9 154 #endif
uci1 0:53afc51b78a9 155 return true;
uci1 0:53afc51b78a9 156 } else {
uci1 0:53afc51b78a9 157 const bool rt = (ct>timeout_clock);
uci1 0:53afc51b78a9 158 #ifdef DEBUG
uci1 0:53afc51b78a9 159 if (rt) {
uci1 0:53afc51b78a9 160 printf("timing out.\r\n");
uci1 0:53afc51b78a9 161 }
uci1 0:53afc51b78a9 162 #endif
uci1 0:53afc51b78a9 163 return rt;
uci1 0:53afc51b78a9 164 }
uci1 0:53afc51b78a9 165 }
uci1 0:53afc51b78a9 166 }
uci1 0:53afc51b78a9 167
uci1 0:53afc51b78a9 168 void dispStrBytes(const char* const s, const int len) {
uci1 0:53afc51b78a9 169 const char* c = s;
uci1 0:53afc51b78a9 170 for (int i=0; i<len; ++i, ++c) {
uci1 0:53afc51b78a9 171 if (*c>0x1F && *c<0x7F) {
uci1 0:53afc51b78a9 172 printf("%c", *c);
uci1 0:53afc51b78a9 173 } else {
uci1 0:53afc51b78a9 174 printf(".x%02x.", *c);
uci1 0:53afc51b78a9 175 }
uci1 0:53afc51b78a9 176 }
uci1 0:53afc51b78a9 177 }
uci1 0:53afc51b78a9 178
uci1 0:53afc51b78a9 179 int DoIO(char* const data,
uci1 0:53afc51b78a9 180 const int length,
uci1 0:53afc51b78a9 181 const int timeout_clock,
uci1 0:53afc51b78a9 182 TCPSendRecv fcn) {
uci1 0:53afc51b78a9 183 // TODO: if B64, must return number of bytes of raw (non encoded) message
uci1 0:53afc51b78a9 184 #ifdef DEBUG
uci1 0:53afc51b78a9 185 printf("DoIO data:\r\n");
uci1 0:53afc51b78a9 186 dispStrBytes(data, length);
uci1 0:53afc51b78a9 187 printf("\r\n");
uci1 0:53afc51b78a9 188 #endif
uci1 0:53afc51b78a9 189 int res=0;
uci1 0:53afc51b78a9 190 int b=0;
uci1 0:53afc51b78a9 191 while ( (length>b) ) {
uci1 0:53afc51b78a9 192 if (IsTimedOut(timeout_clock)) {
uci1 0:53afc51b78a9 193 #ifdef DEBUG
uci1 0:53afc51b78a9 194 printf("DoIO timing out\r\n");
uci1 0:53afc51b78a9 195 #endif
uci1 0:53afc51b78a9 196 break;
uci1 0:53afc51b78a9 197 }
uci1 0:53afc51b78a9 198 res = CALL_MEMBER_FN(*gSock, fcn)(data+b, length-b);
uci1 0:53afc51b78a9 199 switch (res) {
uci1 0:53afc51b78a9 200 case -1:
uci1 0:53afc51b78a9 201 // TODO: how to check the error?
uci1 0:53afc51b78a9 202 continue;
uci1 0:53afc51b78a9 203 case 0:
uci1 1:6b90cbfe0c9e 204 return b;
uci1 0:53afc51b78a9 205 default:
uci1 0:53afc51b78a9 206 b += res;
uci1 0:53afc51b78a9 207 };
uci1 0:53afc51b78a9 208 //wait_ms(100);
uci1 0:53afc51b78a9 209 }
uci1 0:53afc51b78a9 210 #if defined(DEBUG) || defined(PRINT_DOIO_RESULT)
uci1 0:53afc51b78a9 211 printf("return b=%d\r\n",b);
uci1 0:53afc51b78a9 212 #endif
uci1 0:53afc51b78a9 213 return b; // timeout
uci1 0:53afc51b78a9 214 }
uci1 0:53afc51b78a9 215
uci1 0:53afc51b78a9 216 int ReceiveAll(char* const buf, const int mlen, const int timeout_clock) {
uci1 0:53afc51b78a9 217 // use regular receive; DoIO will do a receive_all but use our timeout
uci1 0:53afc51b78a9 218 return DoIO(buf, mlen, timeout_clock, &TCPSocketConnection::receive);
uci1 0:53afc51b78a9 219 }
uci1 0:53afc51b78a9 220
uci1 0:53afc51b78a9 221 int SendAll(char* const data, const int length, const int timeout_clock) {
uci1 0:53afc51b78a9 222 // use regular send; DoIO will do a send_all but use our timeout
uci1 0:53afc51b78a9 223 return DoIO(data, length, timeout_clock, &TCPSocketConnection::send);
uci1 0:53afc51b78a9 224 }