Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Tests/Test11.h

Committer:
ashleymills
Date:
2014-01-29
Revision:
74:e52ac9624f7f
Parent:
72:0e8e769fcf76

File content as of revision 74:e52ac9624f7f:

#pragma once
#include "VodafoneTestCase.h"

extern const char* gTest11Description;

class Test11 : public VodafoneTestCase {
   public:
      Test11(VodafoneUSBModem *m) : VodafoneTestCase(m) {
         _smsLen = 32;
         _smsMaxSize = 160;     // max size of SMS using 
         _numberLen = 16;
      }
      
   private:
      
      virtual void setupTest() {
         allocStorage();
      }
      
      virtual void endTest() {
         freeStorage();
      }
      
      virtual bool executeTest() {
         LOG(gTest11Description);
         int numIterations = 10;
         size_t smCount;
              
         // Clear out the SMS mail box before we run this test.
         // loop 3 times with a 1/2 second break in between each
         LOG("... Clearing out SMS mail box first");
         bool smsCleared = false;
         for(int j=0; j<3; j++) {
            if(_modem->getSMCount(&smCount)!=0) {
               LOG("Failure getting SM count");
               return false;
            }
         
            for(int i=0; i<smCount; i++) {
               if(_modem->getSM(_senderNumber,_smsJunkBuffer,_smsMaxSize)!=0) {
                  LOG("Strange! The SMS count is bigger than zero but I can't fetch the SMS?");
                  return false;
               }
               LOG("Got SMS: %s",_smsJunkBuffer);
               LOG("Clearing that out before running the test.");
               smsCleared = true;
            }
            Thread::wait(500);
         }
         if(!smsCleared) {
            LOG("Nothing found to clear out.");
         }

         LOG("Getting MSISDN");
         _modem->sendUSSD("*#100#",_ownNumber,_numberLen);
         LOG("Got  MSISDN %s",_ownNumber);
         for(int i=0; i<numIterations; i++) {
            createRandomString(_smsOut,_smsLen);
            
            LOG("Artificially waiting for 2 seconds");
            Thread::wait(2000);
            LOG("Attempting to send SMS %d of %d",i+1,numIterations);
            int tries = 3;
            int sent = 0;
            while(tries--) { 
               if(_modem->sendSM(_ownNumber,_smsOut)==0) {
                  sent = 1;
                  break;
               }
               LOG("Error sending short message");
            }
            
            if(!sent) {
               LOG("Failure to send SMS after 3 tries, aborting.");
               return false;
            } else {
               LOG("Sent: %s",_smsOut);
            }
            
            
            bool gotMessage = false;
            Timer t;
            t.start();
            int msToWait = 60000;
            LOG("Waiting %d seconds for response",msToWait/1000);
            while(!gotMessage && t.read_ms()<msToWait) {
               if(_modem->getSMCount(&smCount)!=0) {
                  LOG("Failure getting SM count");
                  return false;
               }
               
               if(smCount>0) {
                  if(_modem->getSM(_senderNumber,_smsIn,_smsLen)!=0) {
                     LOG("Failure getting SM");
                     return false;
                  }
                  LOG("Got SMS: %s after %f seconds.",_smsIn,t.read_ms()/1000.0);
                  if(strcmp(_smsIn,_smsOut)!=0) {
                     LOG("FAIL: strings did not match");
                     return false;
                  }
                  gotMessage = true;
               }
               Thread::wait(50);
            }
            if(!gotMessage) {
               LOG("FAIL: timeout on waiting for SMS");
               return false;
            }
         }
         
         return true;
      }
      
      void createRandomString(char *target, int len) {
         for(int i=0; i<len; i++) {
            target[i] = 65+rand()%16;
         }
         target[len-1] = 0x00;
      }
      
      void allocStorage() {
         _ownNumber    = (char*)malloc(_numberLen*sizeof(char));
         _senderNumber = (char*)malloc(_numberLen*sizeof(char));
         _smsOut       = (char*)malloc(_smsLen*sizeof(char));
         _smsIn        = (char*)malloc(_smsLen*sizeof(char));
         _smsJunkBuffer= (char*)malloc(_smsMaxSize*sizeof(char));
      }
      
      void freeStorage() {
         free(_ownNumber);
         free(_senderNumber);
         free(_smsOut);
         free(_smsIn);
         free(_smsJunkBuffer);
      }
      
      char* _ownNumber;
      char* _senderNumber;
      char* _smsOut;
      char* _smsIn;
      char* _smsJunkBuffer;
      int _smsLen;
      int _smsMaxSize;
      int _numberLen;
};