Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Test14.h Source File

Test14.h

00001 #pragma once
00002 #include "VodafoneTestCase.h"
00003 #include "TestHelper.h"
00004 #include "socket.h"
00005 
00006 extern const char *gTest14Description;
00007 
00008 class Test14 : public VodafoneTestCase {
00009    public: 
00010       Test14(VodafoneUSBModem *m) : VodafoneTestCase(m) {}
00011    private:
00012    
00013       virtual void setupTest(){}
00014       
00015       virtual bool executeTest() {
00016          char urlBuffer[125],socketBuffer[256],strBytesToSend[32];
00017          bool outcome = true;
00018          int sockfd = NULL, socketBufferLength = 256, ret = -1;
00019          int bytesToSend = 128, nextBytesToSend = 128;
00020          unsigned char currentChar = 0;
00021          float timeOut = 1800; // half an hour
00022          
00023          LOG("Description: %s",gTest14Description);
00024          LOG("Connecting to internet");
00025          if(_modem->connect(APN,APN_USERNAME,APN_PASSWORD)==0) {
00026             LOG("Connected to internet");
00027          } else {
00028             LOG("Failed to connect to internet");
00029             _modem->disconnect();
00030             return false;
00031          }
00032          
00033          Timer t;
00034          for(int i=0; i<14; i++) {
00035             LOG("Doing upload for %d bytes",nextBytesToSend);
00036             // connect to socket and push message
00037             if(!connectToSocket("109.74.199.96",7777,&sockfd)) {
00038                LOG("Error connecting to socket");
00039                _modem->disconnect();
00040                return false;
00041             }
00042             
00043             // send password
00044             write(sockfd,"7834_0hf093",11);
00045                    
00046             ret = ::recv(sockfd,socketBuffer,socketBufferLength,0);
00047             socketBuffer[3] = 0x00;
00048             if(ret<0||strcmp(socketBuffer,"ACK")!=0) {
00049                LOG("Got :\"%s\"",socketBuffer);
00050                LOG("Error receiving ACK.");
00051                close(sockfd);
00052                _modem->disconnect();
00053                return false;
00054             } 
00055         
00056             // send file size
00057             sprintf(strBytesToSend,"%d",bytesToSend);
00058             write(sockfd,strBytesToSend,strlen(strBytesToSend));
00059 
00060             // get ACK
00061             if(read(sockfd,socketBuffer,256)<=0) {
00062                LOG("Timeout or error waiting for ack\r\n");
00063                close(sockfd);
00064                _modem->disconnect();
00065                return false;
00066             }
00067 
00068             socketBuffer[3] = 0x00;
00069             if(strcmp(socketBuffer,"ACK")!=0) {
00070                LOG("Failure to receive ACK\r\n");
00071                close(sockfd);
00072                _modem->disconnect();
00073                return false;
00074             }
00075 
00076             // sending file
00077             t.reset();
00078             t.start();
00079             int bytesSent = 0, currentBufferSize = 0;
00080             currentChar = 0;
00081             while(bytesToSend&&t.read()<timeOut) {
00082                if(bytesToSend<socketBufferLength) {
00083                   currentBufferSize = bytesToSend;
00084                } else {
00085                   currentBufferSize = socketBufferLength;
00086                }
00087 
00088                for(int i=0; i<currentBufferSize; i++) {
00089                   socketBuffer[i] = currentChar;
00090                   currentChar++;
00091                }
00092 
00093                if(write(sockfd,socketBuffer,currentBufferSize)!=currentBufferSize) {
00094                   LOG("Failure sending present buffer, failed at %d\r\n",bytesToSend);
00095                   close(sockfd);
00096                   _modem->disconnect();
00097                   return false;
00098                }
00099                bytesToSend -= currentBufferSize;
00100             }
00101             t.stop();
00102             
00103             if(t.read()>=timeOut) {
00104                LOG("Timeout when uploading file");
00105                close(sockfd);
00106                _modem->disconnect();
00107                return false;
00108             }
00109 
00110             // wait for confirm
00111             if(read(sockfd,socketBuffer,256)<=0) {
00112                LOG("Timeout or error waiting for ack\r\n");
00113                close(sockfd);
00114                _modem->disconnect();
00115                return false;
00116             }
00117 
00118             socketBuffer[3] = 0x00;
00119             if(strcmp(socketBuffer,"ACK")!=0) {
00120                LOG("Failure to receive ACK\r\n");
00121                close(sockfd);
00122                _modem->disconnect();
00123                return false;
00124             }
00125             LOG("   DONE. Sent %d bytes in %f seconds for an average of %f kb/s",
00126                nextBytesToSend,t.read(),((float)nextBytesToSend/1000.0)/t.read());
00127             close(sockfd);
00128          
00129             nextBytesToSend*=2;
00130             bytesToSend = nextBytesToSend;
00131          }
00132          
00133          _modem->disconnect();
00134          return true;
00135       }
00136       
00137       virtual void endTest() {}
00138       char *_ussdResponse;
00139 };