ME910 support
Fork of MTS-Cellular by
Test/TestSMS.h@17:dee902f7d00e, 2014-06-05 (annotated)
- Committer:
- mfiore
- Date:
- Thu Jun 05 17:29:32 2014 +0000
- Revision:
- 17:dee902f7d00e
- Child:
- 18:fa0d8120f81f
add unit tests for SMS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfiore | 17:dee902f7d00e | 1 | #ifndef TESTSMS_H |
mfiore | 17:dee902f7d00e | 2 | #define TESTSMS_H |
mfiore | 17:dee902f7d00e | 3 | |
mfiore | 17:dee902f7d00e | 4 | #include "mtsas.h" |
mfiore | 17:dee902f7d00e | 5 | #include <string> |
mfiore | 17:dee902f7d00e | 6 | #include <vector> |
mfiore | 17:dee902f7d00e | 7 | |
mfiore | 17:dee902f7d00e | 8 | using namespace mts; |
mfiore | 17:dee902f7d00e | 9 | |
mfiore | 17:dee902f7d00e | 10 | class TestSMS : public TestCollection |
mfiore | 17:dee902f7d00e | 11 | { |
mfiore | 17:dee902f7d00e | 12 | public: |
mfiore | 17:dee902f7d00e | 13 | TestSMS(); |
mfiore | 17:dee902f7d00e | 14 | virtual void run(); |
mfiore | 17:dee902f7d00e | 15 | |
mfiore | 17:dee902f7d00e | 16 | private: |
mfiore | 17:dee902f7d00e | 17 | MTSSerialFlowControl* io; |
mfiore | 17:dee902f7d00e | 18 | Cellular* radio; |
mfiore | 17:dee902f7d00e | 19 | }; |
mfiore | 17:dee902f7d00e | 20 | |
mfiore | 17:dee902f7d00e | 21 | TestSMS::TestSMS() : TestCollection("TestSMS") {} |
mfiore | 17:dee902f7d00e | 22 | |
mfiore | 17:dee902f7d00e | 23 | void TestSMS::run() { |
mfiore | 17:dee902f7d00e | 24 | string number; |
mfiore | 17:dee902f7d00e | 25 | string response; |
mfiore | 17:dee902f7d00e | 26 | vector<string> parts; |
mfiore | 17:dee902f7d00e | 27 | vector<Cellular::Sms> rmessages; |
mfiore | 17:dee902f7d00e | 28 | vector<string> smessages; |
mfiore | 17:dee902f7d00e | 29 | smessages.push_back("testing SMS 1"); |
mfiore | 17:dee902f7d00e | 30 | smessages.push_back("testing SMS 2"); |
mfiore | 17:dee902f7d00e | 31 | smessages.push_back("This is a longer SMS message. It is the third and final message that will be sent."); |
mfiore | 17:dee902f7d00e | 32 | |
mfiore | 17:dee902f7d00e | 33 | MTSLog::setLogLevel(MTSLog::TRACE_LEVEL); |
mfiore | 17:dee902f7d00e | 34 | |
mfiore | 17:dee902f7d00e | 35 | Test::start("Setup"); |
mfiore | 17:dee902f7d00e | 36 | io = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
mfiore | 17:dee902f7d00e | 37 | io->baud(115200); |
mfiore | 17:dee902f7d00e | 38 | radio = CellularFactory::create(io); |
mfiore | 17:dee902f7d00e | 39 | if (! radio) { |
mfiore | 17:dee902f7d00e | 40 | logError("radio is NULL"); |
mfiore | 17:dee902f7d00e | 41 | Test::assertTrue(false); |
mfiore | 17:dee902f7d00e | 42 | } |
mfiore | 17:dee902f7d00e | 43 | radio->configureSignals(PTA4, PTC9, PTA20); |
mfiore | 17:dee902f7d00e | 44 | |
mfiore | 17:dee902f7d00e | 45 | for (int i = 0; i < 10; i++) { |
mfiore | 17:dee902f7d00e | 46 | if (i >= 10) { |
mfiore | 17:dee902f7d00e | 47 | Test::assertTrue(false); |
mfiore | 17:dee902f7d00e | 48 | } |
mfiore | 17:dee902f7d00e | 49 | if (radio->setApn("wap.cingular") == SUCCESS) { |
mfiore | 17:dee902f7d00e | 50 | break; |
mfiore | 17:dee902f7d00e | 51 | } else { |
mfiore | 17:dee902f7d00e | 52 | wait(1); |
mfiore | 17:dee902f7d00e | 53 | } |
mfiore | 17:dee902f7d00e | 54 | } |
mfiore | 17:dee902f7d00e | 55 | Test::end(); |
mfiore | 17:dee902f7d00e | 56 | |
mfiore | 17:dee902f7d00e | 57 | Test::start("Send SMS"); |
mfiore | 17:dee902f7d00e | 58 | logInfo("finding my phone number"); |
mfiore | 17:dee902f7d00e | 59 | while (true) { |
mfiore | 17:dee902f7d00e | 60 | response = radio->sendCommand("AT+CNUM", 1000); |
mfiore | 17:dee902f7d00e | 61 | if (response.find("ERROR") == string::npos && response.find("error") == string::npos) { |
mfiore | 17:dee902f7d00e | 62 | break; |
mfiore | 17:dee902f7d00e | 63 | } |
mfiore | 17:dee902f7d00e | 64 | |
mfiore | 17:dee902f7d00e | 65 | wait(1); |
mfiore | 17:dee902f7d00e | 66 | } |
mfiore | 17:dee902f7d00e | 67 | if (response.find("My Number") != string::npos) { |
mfiore | 17:dee902f7d00e | 68 | parts = Text::split(response, ","); |
mfiore | 17:dee902f7d00e | 69 | number = parts[1]; |
mfiore | 17:dee902f7d00e | 70 | size_t fquote = number.find("\""); |
mfiore | 17:dee902f7d00e | 71 | size_t bquote = number.rfind("\""); |
mfiore | 17:dee902f7d00e | 72 | number = number.substr(fquote + 1, bquote - 1); |
mfiore | 17:dee902f7d00e | 73 | logInfo("my phone number: [%s]", number.c_str()); |
mfiore | 17:dee902f7d00e | 74 | } else { |
mfiore | 17:dee902f7d00e | 75 | Test::assertTrue(false); |
mfiore | 17:dee902f7d00e | 76 | } |
mfiore | 17:dee902f7d00e | 77 | |
mfiore | 17:dee902f7d00e | 78 | Test::assertTrue(radio->sendSMS(number, smessages[0]) == SUCCESS); |
mfiore | 17:dee902f7d00e | 79 | Test::assertTrue(radio->sendSMS(number, smessages[1]) == SUCCESS); |
mfiore | 17:dee902f7d00e | 80 | Test::end(); |
mfiore | 17:dee902f7d00e | 81 | |
mfiore | 17:dee902f7d00e | 82 | wait(30); |
mfiore | 17:dee902f7d00e | 83 | |
mfiore | 17:dee902f7d00e | 84 | Test::start("Receive SMS"); |
mfiore | 17:dee902f7d00e | 85 | rmessages = radio->getReceivedSms(); |
mfiore | 17:dee902f7d00e | 86 | Test::assertTrue(rmessages.size() == 2); |
mfiore | 17:dee902f7d00e | 87 | for (int i = 0; i < rmessages.size(); i++) { |
mfiore | 17:dee902f7d00e | 88 | Test::assertTrue(rmessages[i].message == smessages[i]); |
mfiore | 17:dee902f7d00e | 89 | } |
mfiore | 17:dee902f7d00e | 90 | Test::end(); |
mfiore | 17:dee902f7d00e | 91 | |
mfiore | 17:dee902f7d00e | 92 | Test::start("Send another SMS"); |
mfiore | 17:dee902f7d00e | 93 | Test::assertTrue(radio->sendSMS(number, smessages[2]) == SUCCESS); |
mfiore | 17:dee902f7d00e | 94 | Test::end(); |
mfiore | 17:dee902f7d00e | 95 | |
mfiore | 17:dee902f7d00e | 96 | wait(30); |
mfiore | 17:dee902f7d00e | 97 | |
mfiore | 17:dee902f7d00e | 98 | /* the SMS that we haven't "read" yet should not get deleted */ |
mfiore | 17:dee902f7d00e | 99 | Test::start("Delete Read SMS Messages"); |
mfiore | 17:dee902f7d00e | 100 | Test::assertTrue(radio->deleteOnlyReceivedReadSms() == SUCCESS); |
mfiore | 17:dee902f7d00e | 101 | Test::assertTrue(radio->getReceivedSms().size() == 1); |
mfiore | 17:dee902f7d00e | 102 | Test::end(); |
mfiore | 17:dee902f7d00e | 103 | |
mfiore | 17:dee902f7d00e | 104 | Test::start("Receive another SMS"); |
mfiore | 17:dee902f7d00e | 105 | rmessages = radio->getReceivedSms(); |
mfiore | 17:dee902f7d00e | 106 | Test::assertTrue(rmessages.size() == 1); |
mfiore | 17:dee902f7d00e | 107 | Test::assertTrue(rmessages[0].message == smessages[2]); |
mfiore | 17:dee902f7d00e | 108 | Test::end(); |
mfiore | 17:dee902f7d00e | 109 | |
mfiore | 17:dee902f7d00e | 110 | Test::start("Delete All SMS Messages"); |
mfiore | 17:dee902f7d00e | 111 | Test::assertTrue(radio->deleteAllReceivedSms() == SUCCESS); |
mfiore | 17:dee902f7d00e | 112 | Test::assertTrue(radio->getReceivedSms().size() == 0); |
mfiore | 17:dee902f7d00e | 113 | Test::end(); |
mfiore | 17:dee902f7d00e | 114 | } |
mfiore | 17:dee902f7d00e | 115 | |
mfiore | 17:dee902f7d00e | 116 | #endif |