A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Revision 124:6d964b4343c8, committed 2014-01-02
- Comitter:
- mfiore
- Date:
- Thu Jan 02 18:11:25 2014 +0000
- Parent:
- 121:5a7fba896c98
- Child:
- 125:850c51cbeff7
- Commit message:
- finish cleaning up test directory; socket, socket echo, and ping tests can be run on wifi or cellular by changing #define value in each test header; test_main.cpp has commented out main function with all tests ready to go
Changed in this revision
--- a/tests/blinky_ping_test.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,151 +0,0 @@ -#include "mbed.h" -#include "include_me.h" - -#define MAX_TRIES 5 -#define MAX_REGISTRATION_TRIES 10 - -#define CELL_SHIELD 0 // if using a cell shield board, change to 1 - -using namespace mts; - -/* APN is ignored if using wifi */ -bool cellPingTest(const std::string& apn, const std::string& server); -bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key); -void blinkLed(DigitalOut led); - -void pingTest() { - DigitalOut ledG(LED1); - DigitalOut ledR(LED2); - - ledG = 1; - ledR = 1; - - std::string server = "8.8.8.8"; // Google's DNS server -#if CELL_SHIELD - std::string apn = "wap.cingular"; // APN of sim card - if (cellPingTest(apn, server)) { -#else - std::string ssid = "Mtech_guest"; // ssid of wireless network - Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2 - std::string key = "MultiModem2!"; // password for network (if type is not "NONE") - if (wifiPingTest(server, ssid, type, key)) { -#endif - printf("success!\n\r"); - blinkLed(ledG); - } else { - printf("failure!\n\r"); - blinkLed(ledR); - } -} - -bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) { - int i; - - Transport::setTransport(Transport::WIFI); - MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); - serial->baud(9600); - Wifi* wifi = Wifi::getInstance(); - wifi->init(serial); - - printf("waiting for radio to come up\r\n"); - wait(5); - - i = 0; - while (i++ < MAX_TRIES) { - if (wifi->setNetwork(ssid, type, key) == SUCCESS) { - printf("set network\r\n"); - break; - } else { - printf("failed to set network\r\n"); - } - wait(1); - } - - i = 0; - while (i++ < MAX_TRIES) { - if (wifi->setDeviceIP() == SUCCESS) { - printf("set IP\r\n"); - break; - } else { - printf("failed to set IP\r\n"); - } - wait(1); - } - - i = 0; - while (i++ < MAX_TRIES) { - if (wifi->connect()) { - printf("connected\r\n"); - break; - } else { - printf("failed to connect\r\n"); - } - wait(1); - } - - printf("pinging %s\n\r", server.c_str()); - return wifi->ping(server); -} - -bool cellPingTest(const std::string& apn, const std::string& server) { - int i; - - Transport::setTransport(Transport::CELLULAR); - MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); - serial->baud(115200); - Cellular* cell = Cellular::getInstance(); - cell->init(serial); - - printf("waiting for radio to come up\n\r"); - wait(30); - - i = 0; - while (i++ < MAX_REGISTRATION_TRIES) { - if (cell->getRegistration() == Cellular::REGISTERED) { - printf("registered with tower\n\r"); - break; - } else if (i >= MAX_REGISTRATION_TRIES) { - printf("failed to register with tower\n\r"); - return false; - } - wait(3); - } - - i = 0; - printf("setting APN to %s\n\r", apn.c_str()); - while (i++ < MAX_TRIES) { - if (cell->setApn(apn) == SUCCESS) { - printf("successfully set APN\n\r"); - break; - } else if (i >= MAX_TRIES) { - printf("failed to set APN\n\r"); - return false; - } - wait(1); - } - - i = 0; - printf("bringing up PPP link\n\r"); - while (i++ < MAX_TRIES) { - if (cell->connect()) { - printf("PPP link is up\n\r"); - break; - } else if (i >= MAX_TRIES) { - printf("failed to bring PPP link up\n\r"); - return false; - } - wait(1); - } - - printf("pinging %s\n\r", server.c_str()); - return cell->ping(server); -} - -void blinkLed(DigitalOut led) { - led = 0; - - while (true) { - wait(0.25); - led = !led; - } -} \ No newline at end of file
--- a/tests/testMTSCircularBuffer.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,276 +0,0 @@ -#ifndef TESTMTSCIRCULARBUFFER_H -#define TESTMTSCIRCULARBUFFER_H - -#include "MTSCircularBuffer.h" -#include "Vars.h" - -using namespace mts; - -int capacity = 0; -MTSCircularBuffer* buffer = new MTSCircularBuffer(5); - -void callback() -{ - capacity = buffer->remaining(); -} - -int testMTSCircularBuffer() -{ - printf("Testing: MTSCircularBuffer\r\n"); - int failed = 0; - char byte; - - - //Test getSize method - if (buffer->capacity() != 5) { - printf("Failed: capacity()\r\n"); - failed++; - } - - //Test clear function - buffer->write("AT", 2); - buffer->clear(); - if (buffer->size() != 0) { - printf("Failed: clear()\r\n"); - failed++; - } - - /* The next set of test all rely on an empty buffer!!! */ - - //Test isEmpty method - empty buffer - if (buffer->isEmpty() != true) { - printf("Failed: isEmpty() - empty\r\n"); - failed++; - } - - //Test isFull method - empty buffer - if (buffer->isFull() == true) { - printf("Failed: isFull() - empty\r\n"); - failed++; - } - - //Test capacity method - empty buffer - if (buffer->remaining() != 5) { - printf("Failed: remaining() - empty\r\n"); - failed++; - } - - //Test available method - empty buffer - if (buffer->size() != 0) { - printf("Failed: size() - empty\r\n"); - failed++; - } - - /* The next set of tests all rely on a full buffer */ - - //Test bulk write method - int tmp = buffer->write("Test", 5); - if (tmp != 5) { - printf("Failed: bulk write()\r\n"); - failed++; - } - - //Test isEmpty method - full buffer - if (buffer->isEmpty() == true) { - printf("Failed: isEmpty() - full\r\n"); - failed++; - } - - //Test isFull method - full buffer - if (buffer->isFull() == false) { - printf("Failed: isFull() - full\r\n"); - failed++; - } - - //Test capacity method - full buffer - if (buffer->remaining() != 0) { - printf("Failed: remaining() - full\r\n"); - failed++; - } - - //Test available method - full buffer - if (buffer->size() != 5) { - printf("Failed: size() - full\r\n"); - failed++; - } - - //Test single overwrite method - if (buffer->write('A') != 0) { - printf("Failed: write() - overwrite\r\n"); - failed++; - } - - //Test bulk overwrite method - if (buffer->write("Test", 5) != 0) { - printf("Failed: bulk write() - overflow\r\n"); - failed++; - } - - //Test single read method - if ((buffer->read(byte) < 1 && byte != 'T') || buffer->remaining() != 1) { - printf("Failed: single read()\r\n"); - failed++; - } - - //Test bulk read method - char data[5]; - if (buffer->read(data, 4) != 4 || data[0] != 'e' || data[1] != 's' || data[2] != 't' || data[3] != '\0') { - printf("Failed: bulk read()\r\n"); - failed++; - } - - //Test wrap write/read methods - tmp = buffer->write("AT", 3); - if (tmp != 3 || (buffer->read(byte) < 1 && byte != 'A') || (buffer->read(byte) < 1 && byte != 'T')) { - printf("Failed: wrap write()/read()\r\n"); - failed++; - } - buffer->clear(); - - /* The next set of test are focused all on the attach methods */ - - //Test attach with greater than below level - buffer->attach(&callback, 3, Vars::GREATER); - buffer->write("ABC", 3); - if (capacity != 0) { - printf("Failed: attach() - greater/below\r\n"); - failed++; - } - - //Test attach with greater than above level - buffer->write('T'); - if (capacity != 1) { - printf("Failed: attach() - greater/above\r\n"); - failed++; - } - buffer->clear(); - capacity = 0; - - //Test attach with greater equal than below level - buffer->attach(&callback, 3, Vars::GREATER_EQUAL); - buffer->write("AB", 2); - if (capacity != 0) { - printf("Failed: attach() - greater equal/below\r\n"); - failed++; - } - - //Test attach with greater equal than above level - buffer->write('T'); - if (capacity != 2) { - printf("Failed: attach() - greater equal/above\r\n"); - failed++; - } - - //Test attach with less than above level - buffer->clear(); - buffer->write("ABC", 3); - capacity = 0; - buffer->attach(&callback, 2, Vars::LESS); - buffer->read(byte); - if (capacity != 0) { - printf("Failed: attach() - less_equal/above\r\n"); - failed++; - } - - //Test attach with less than below level - buffer->read(byte); - if (capacity != 4) { - printf("Failed: attach() - less_equal/below%d\r\n", capacity); - failed++; - } - - //Test attach with less equal than above level - buffer->clear(); - buffer->write("Test", 4); - capacity = 0; - buffer->attach(&callback, 2, Vars::LESS_EQUAL); - buffer->read(byte); - if (capacity != 0) { - printf("Failed: attach() - less equal/above\r\n"); - failed++; - } - - //Test attach with less equal than below level - buffer->read(byte); - if (capacity != 3) { - printf("Failed: attach() - less equal/below%d\r\n", capacity); - failed++; - } - - //Test attach with less equal than above level - buffer->clear(); - buffer->write("Test", 4); - capacity = 0; - buffer->attach(&callback, 2, Vars::EQUAL); - buffer->read(byte); - if (capacity != 0) { - printf("Failed: attach() - equal/above\r\n"); - failed++; - } - - //Test attach with less equal than below level - buffer->read(byte); - if (capacity != 3) { - printf("Failed: attach() - equal/below%d\r\n", capacity); - failed++; - } - - - //Test Ins and Outs - { - const char inData[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; - const int size = sizeof(inData); - char outData[size]; - - int bytesWritten = 0; - int bytesRead = 0; - buffer->clear(); - - Timer tmr; - tmr.start(); - do { - int remaining = size - bytesRead; - int readable = buffer->size(); - if(remaining) { - if(readable) { - //printf("READABLE [%d]\r\n", readable); - int received = buffer->read(&outData[bytesRead], remaining); - bytesRead += received; - //printf("READ [%d] TOTAL[%d] REMAINING[%d]\r\n", received, bytesRead, size - bytesRead); - } - } - - remaining = size - bytesWritten; - int writeable = buffer->remaining(); - if(remaining) { - if(writeable) { - //printf("WRITEABLE [%d]\r\n", writeable); - int written = buffer->write(&inData[bytesWritten], remaining); - bytesWritten += written; - remaining = size - bytesWritten; - //printf("WROTE [%d] TOTAL[%d] REMAINING[%d]\r\n", written, bytesWritten, size - bytesWritten); - } - } - - } while (tmr.read_ms() <= 5000 && bytesRead < size); - - printf("INPUT [%d]: [%s]\r\n", size, inData); - printf("OUTPUT [%d]: [%s]\r\n", bytesRead, outData); - for(int i = 0; i < size - 1; i++) { - if(inData[i] != outData[i]) { - printf("Failed: Buffers do not match at index %d\r\n", i); - failed++; - break; - } - } - } - - printf("Finished Testing: MTSCircularBuffer\r\n"); - return failed; -} - - - - - -#endif /* TESTMTSCIRCULARBUFFER_H */ \ No newline at end of file
--- a/tests/testMTSSerial.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -#ifndef TESTMTSSERIAL_H -#define TESTMTSSERIAL_H - -#include "MTSSerial.h" -#include "Vars.h" -#include <sstream> - -DigitalOut led(LED1); - -MTSSerial* serial = new MTSSerial(PTD3, PTD2); -int baud = 9600; -char buf[32]; -int size = 32; -int ret; - -void testMTSSerial() { - /* - serial->baud(baud); - - printf ("starting to read\n\r"); - - while (true) { - ret = serial->read(buf, size); - if (ret < 0) { - printf ("read failed\n\r"); - } - ret = serial->write(buf, ret); - if (ret < 0) { - printf ("write failed\n\r"); - } - } - */ -} - -#endif /* TESTMTSSERIAL_H */ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_MTS_Circular_Buffer.h Thu Jan 02 18:11:25 2014 +0000 @@ -0,0 +1,273 @@ +#ifndef TESTMTSCIRCULARBUFFER_H +#define TESTMTSCIRCULARBUFFER_H + +#include "MTSCircularBuffer.h" +#include "Vars.h" + +/* unit tests for the circular buffer class */ + +using namespace mts; + +int capacity = 0; +MTSCircularBuffer* buffer = new MTSCircularBuffer(5); + +void callback() +{ + capacity = buffer->remaining(); +} + +int testMTSCircularBuffer() +{ + printf("Testing: MTSCircularBuffer\r\n"); + int failed = 0; + char byte; + + + //Test getSize method + if (buffer->capacity() != 5) { + printf("Failed: capacity()\r\n"); + failed++; + } + + //Test clear function + buffer->write("AT", 2); + buffer->clear(); + if (buffer->size() != 0) { + printf("Failed: clear()\r\n"); + failed++; + } + + /* The next set of test all rely on an empty buffer!!! */ + + //Test isEmpty method - empty buffer + if (buffer->isEmpty() != true) { + printf("Failed: isEmpty() - empty\r\n"); + failed++; + } + + //Test isFull method - empty buffer + if (buffer->isFull() == true) { + printf("Failed: isFull() - empty\r\n"); + failed++; + } + + //Test capacity method - empty buffer + if (buffer->remaining() != 5) { + printf("Failed: remaining() - empty\r\n"); + failed++; + } + + //Test available method - empty buffer + if (buffer->size() != 0) { + printf("Failed: size() - empty\r\n"); + failed++; + } + + /* The next set of tests all rely on a full buffer */ + + //Test bulk write method + int tmp = buffer->write("Test", 5); + if (tmp != 5) { + printf("Failed: bulk write()\r\n"); + failed++; + } + + //Test isEmpty method - full buffer + if (buffer->isEmpty() == true) { + printf("Failed: isEmpty() - full\r\n"); + failed++; + } + + //Test isFull method - full buffer + if (buffer->isFull() == false) { + printf("Failed: isFull() - full\r\n"); + failed++; + } + + //Test capacity method - full buffer + if (buffer->remaining() != 0) { + printf("Failed: remaining() - full\r\n"); + failed++; + } + + //Test available method - full buffer + if (buffer->size() != 5) { + printf("Failed: size() - full\r\n"); + failed++; + } + + //Test single overwrite method + if (buffer->write('A') != 0) { + printf("Failed: write() - overwrite\r\n"); + failed++; + } + + //Test bulk overwrite method + if (buffer->write("Test", 5) != 0) { + printf("Failed: bulk write() - overflow\r\n"); + failed++; + } + + //Test single read method + if ((buffer->read(byte) < 1 && byte != 'T') || buffer->remaining() != 1) { + printf("Failed: single read()\r\n"); + failed++; + } + + //Test bulk read method + char data[5]; + if (buffer->read(data, 4) != 4 || data[0] != 'e' || data[1] != 's' || data[2] != 't' || data[3] != '\0') { + printf("Failed: bulk read()\r\n"); + failed++; + } + + //Test wrap write/read methods + tmp = buffer->write("AT", 3); + if (tmp != 3 || (buffer->read(byte) < 1 && byte != 'A') || (buffer->read(byte) < 1 && byte != 'T')) { + printf("Failed: wrap write()/read()\r\n"); + failed++; + } + buffer->clear(); + + /* The next set of test are focused all on the attach methods */ + + //Test attach with greater than below level + buffer->attach(&callback, 3, Vars::GREATER); + buffer->write("ABC", 3); + if (capacity != 0) { + printf("Failed: attach() - greater/below\r\n"); + failed++; + } + + //Test attach with greater than above level + buffer->write('T'); + if (capacity != 1) { + printf("Failed: attach() - greater/above\r\n"); + failed++; + } + buffer->clear(); + capacity = 0; + + //Test attach with greater equal than below level + buffer->attach(&callback, 3, Vars::GREATER_EQUAL); + buffer->write("AB", 2); + if (capacity != 0) { + printf("Failed: attach() - greater equal/below\r\n"); + failed++; + } + + //Test attach with greater equal than above level + buffer->write('T'); + if (capacity != 2) { + printf("Failed: attach() - greater equal/above\r\n"); + failed++; + } + + //Test attach with less than above level + buffer->clear(); + buffer->write("ABC", 3); + capacity = 0; + buffer->attach(&callback, 2, Vars::LESS); + buffer->read(byte); + if (capacity != 0) { + printf("Failed: attach() - less_equal/above\r\n"); + failed++; + } + + //Test attach with less than below level + buffer->read(byte); + if (capacity != 4) { + printf("Failed: attach() - less_equal/below%d\r\n", capacity); + failed++; + } + + //Test attach with less equal than above level + buffer->clear(); + buffer->write("Test", 4); + capacity = 0; + buffer->attach(&callback, 2, Vars::LESS_EQUAL); + buffer->read(byte); + if (capacity != 0) { + printf("Failed: attach() - less equal/above\r\n"); + failed++; + } + + //Test attach with less equal than below level + buffer->read(byte); + if (capacity != 3) { + printf("Failed: attach() - less equal/below%d\r\n", capacity); + failed++; + } + + //Test attach with less equal than above level + buffer->clear(); + buffer->write("Test", 4); + capacity = 0; + buffer->attach(&callback, 2, Vars::EQUAL); + buffer->read(byte); + if (capacity != 0) { + printf("Failed: attach() - equal/above\r\n"); + failed++; + } + + //Test attach with less equal than below level + buffer->read(byte); + if (capacity != 3) { + printf("Failed: attach() - equal/below%d\r\n", capacity); + failed++; + } + + //Test Ins and Outs + { + const char inData[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; + const int size = sizeof(inData); + char outData[size]; + + int bytesWritten = 0; + int bytesRead = 0; + buffer->clear(); + + Timer tmr; + tmr.start(); + do { + int remaining = size - bytesRead; + int readable = buffer->size(); + if(remaining) { + if(readable) { + //printf("READABLE [%d]\r\n", readable); + int received = buffer->read(&outData[bytesRead], remaining); + bytesRead += received; + //printf("READ [%d] TOTAL[%d] REMAINING[%d]\r\n", received, bytesRead, size - bytesRead); + } + } + + remaining = size - bytesWritten; + int writeable = buffer->remaining(); + if(remaining) { + if(writeable) { + //printf("WRITEABLE [%d]\r\n", writeable); + int written = buffer->write(&inData[bytesWritten], remaining); + bytesWritten += written; + remaining = size - bytesWritten; + //printf("WROTE [%d] TOTAL[%d] REMAINING[%d]\r\n", written, bytesWritten, size - bytesWritten); + } + } + + } while (tmr.read_ms() <= 5000 && bytesRead < size); + + printf("INPUT [%d]: [%s]\r\n", size, inData); + printf("OUTPUT [%d]: [%s]\r\n", bytesRead, outData); + for(int i = 0; i < size - 1; i++) { + if(inData[i] != outData[i]) { + printf("Failed: Buffers do not match at index %d\r\n", i); + failed++; + break; + } + } + } + + printf("Finished Testing: MTSCircularBuffer\r\n"); + return failed; +} + +#endif /* TESTMTSCIRCULARBUFFER_H */ \ No newline at end of file
--- a/tests/test_SMS.h Tue Dec 31 23:41:59 2013 +0000 +++ b/tests/test_SMS.h Thu Jan 02 18:11:25 2014 +0000 @@ -7,7 +7,7 @@ void sendSms() { Code code; std::string sMsg("Hello from Multi-Tech MBED!"); - std::string sPhoneNum(""); // insert your phone number prepended with a 1, if you number is 222-867-5309 you'd put 12228675309 + std::string sPhoneNum( /* your 10-digit phone number prepended with a 1, e.g. 12228675309 */); printf("Sending message [%s] to [%s]\r\n", sMsg.c_str(), sPhoneNum.c_str()); code = Cellular::getInstance()->sendSMS(sPhoneNum, sMsg);
--- a/tests/test_TCPSocketConnection_Socket_Echo.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,91 +0,0 @@ -#ifndef _TEST_TCPSOCKETCONNECTION_SOCKET_ECHO_H_ -#define _TEST_TCPSOCKETCONNECTION_SOCKET_ECHO_H_ - -#include "TCPSocketConnection.h" -//Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo' - -void testTcpSocketConnectionSocketEcho() -{ - using namespace mts; - - Code code; - const int TEST_PORT = 5798; - const std::string TEST_SERVER("204.26.122.96"); - char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; - const int size = sizeof(buffer); - char echoData[size]; - - TCPSocketConnection sock; - - printf("TCP SOCKET TESTING\r\n"); - printf("Setting APN\r\n"); - code = Cellular::getInstance()->setApn("b2b.tmobile.com"); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error during APN setup [%d]\r\n", (int)code); - } - - printf("Setting Socket Closeable\r\n"); - code = Cellular::getInstance()->setSocketCloseable(); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error setting socket closeable [%d]\r\n", (int)code); - } - - printf("Establishing PPP Connection\r\n"); - if(Cellular::getInstance()->connect()) { - printf("Success!\r\n"); - } else { - printf("Error during PPP connection\r\n"); - } - - printf("Opening a TCP Socket\r\n"); - if(sock.connect(TEST_SERVER.c_str(), TEST_PORT) == 0) { - printf("Success!\r\n"); - } else { - printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT); - } - - printf("Sending buffer\r\n"); - sock.set_blocking(false, 10000); - int bytesWritten = sock.send_all(buffer, size); - if(bytesWritten == size) { - printf("Successfully sent buffer\r\n"); - } else { - printf("Failed to send buffer\r\n"); - } - - printf("Receiving echo (timeout = 15 seconds)\r\n"); - Timer tmr; - int bytesRead = 0; - sock.set_blocking(false, 1000); - tmr.start(); - do { - bytesRead += sock.receive_all(&echoData[bytesRead], size - bytesRead); - printf("Total bytes read %d\r\n", bytesRead); - } while (tmr.read_ms() <= 15000 && bytesRead < size); - - //Safely Cap at Max Size - echoData[size - 1] = '\0'; - printf("Comparing Buffers\r\n"); - printf("SENT: [%s]\r\n", buffer); - printf("RECV: [%s]\r\n", echoData); - - for(int i = 0; i < size - 1; i++) { - if(buffer[i] != echoData[i]) { - printf("Buffers do not match at index %d\r\n", i); - break; - } - } - - printf("Closing socket\r\n"); - sock.close(); - - - printf("Disconnecting\r\n"); - Cellular::getInstance()->disconnect(); -} - -#endif
--- a/tests/test_TCP_Socket.h Tue Dec 31 23:41:59 2013 +0000 +++ b/tests/test_TCP_Socket.h Thu Jan 02 18:11:25 2014 +0000 @@ -1,7 +1,12 @@ #ifndef _TEST_TCP_SOCKET_H_ #define _TEST_TCP_SOCKET_H_ -#define CELL_SHIELD 0 // if using a cell shield board, change to 1 +// 0 for shield board with wifi +// 1 for shield board with cellular +#define CELL_SHIELD 0 + +/* test TCP socket communication + * will keep talking to server until data doesn't match expected */ using namespace mts; const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"; @@ -15,7 +20,6 @@ "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}\\\r\n" "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}*"; - const char MENU[] = "1 send ascii pattern until keypress\r\n" "2 send ascii pattern (numbered)\r\n" "3 send pattern and close socket\r\n" @@ -31,11 +35,16 @@ void testTcpSocket() { Code code; + /* this test is set up to interact with a server listening at the following address and port */ const int TEST_PORT = 7000; - const std::string TEST_SERVER("204.26.122.5"); + const std::string TEST_SERVER("204.26.122.5"; printf("TCP SOCKET TESTING\r\n"); #if CELL_SHIELD + for (int i = 30; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } Transport::setTransport(Transport::CELLULAR); MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); serial->baud(115200); @@ -48,21 +57,17 @@ } else { printf("Error during APN setup [%d]\r\n", (int)code); } - - printf("Setting Socket Closeable\r\n"); - code = Cellular::getInstance()->setSocketCloseable(); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error setting socket closeable [%d]\r\n", (int)code); - } #else + for (int i = 6; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } Transport::setTransport(Transport::WIFI); MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); serial->baud(9600); Wifi::getInstance()->init(serial); - code = Wifi::getInstance()->setNetwork("Mtech_guest", Wifi::WPA2, "MultiModem2!"); + code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */); if(code == SUCCESS) { printf("Success!\r\n"); } else { @@ -106,7 +111,6 @@ printf("Successful Iterations: %d\r\n", count); } - printf("Closing socket\r\n"); #if CELL_SHIELD Cellular::getInstance()->close(); @@ -125,7 +129,6 @@ } bool testTcpSocketIteration() { - Timer tmr; int bytesRead = 0; const int bufferSize = 1024; @@ -188,8 +191,6 @@ } } - - result.clear(); printf("Writing to socket: 2\r\n"); @@ -233,9 +234,7 @@ return false; } - return true; } - -#endif +#endif \ No newline at end of file
--- a/tests/test_TCP_Socket_Connection.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -#ifndef _TEST_TCPCONNECTION_H_ -#define _TEST_TCPCONNECTION_H_ - -#include "TCPSocketConnection.h" - -using namespace mts; - -void testTcpSocketConnection() -{ - Cellular::Code code; - const int TEST_PORT = 7000; - const std::string TEST_SERVER("204.26.122.5"); - - printf("TCP SOCKET TESTING\r\n"); - printf("Setting APN\r\n"); - //code = Cellular::getInstance()->setApn("b2b.tmobile.com"); - code = Cellular::getInstance()->setApn("wap.cingular"); - if(code == Cellular::CELL_OK) { - printf("Success!\r\n"); - } else { - printf("Error during APN setup [%d]\r\n", (int)code); - } - - printf("Setting Socket Closeable\r\n"); - code = Cellular::getInstance()->setSocketCloseable(); - if(code == Cellular::CELL_OK) { - printf("Success!\r\n"); - } else { - printf("Error setting socket closeable [%d]\r\n", (int)code); - } - - printf("Establishing PPP Connection\r\n"); - if(Cellular::getInstance()->connect()) { - printf("Success!\r\n"); - } else { - printf("Error during PPP connection\r\n"); - } - - TCPSocketConnection socket; - - printf("Opening a TCP Socket\r\n"); - if(socket.connect(TEST_SERVER.c_str(), TEST_PORT) != -1) { - printf("Success!\r\n"); - } else { - printf("Error during TCP socket connect [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT); - } - - printf("Reading from socket for 15 seconds\r\n"); - char data[1024] = { 0 }; - socket.set_blocking(false, 15000); - int bytesRead = socket.receive_all(data, 1023); //1 less than max - printf("READ: [%d] [%s]\r\n", bytesRead, data); - - - printf("Waiting 10 seconds\r\n"); - wait(10); - - printf("Writing to socket\r\n"); - sprintf(data, "3\r\n"); - socket.set_blocking(false, 10000); - int bytesWritten = socket.send_all(data, 4); - if(bytesWritten == 4) { - printf("Successfully wrote 'q'\r\n"); - } else { - printf("Failed to write 'q'\r\n"); - } - - printf("Reading from socket for 15 seconds\r\n"); - socket.set_blocking(false, 15000); - bytesRead = socket.receive_all(data, 1023); //1 less than max - printf("READ: [%d] [%s]\r\n", bytesRead, data); - - printf("Closing socket\r\n"); - socket.close(); - - - printf("Disconnecting\r\n"); - Cellular::getInstance()->disconnect(); -} - -#endif
--- a/tests/test_TCP_Socket_Echo.h Tue Dec 31 23:41:59 2013 +0000 +++ b/tests/test_TCP_Socket_Echo.h Thu Jan 02 18:11:25 2014 +0000 @@ -1,59 +1,83 @@ #ifndef _TEST_TCP_SOCKET_ECHO_H_ #define _TEST_TCP_SOCKET_ECHO_H_ +// 0 for shield board with wifi +// 1 for shield board with cellular +#define CELL_SHIELD 0 +/* test TCP socket communication + * designed to talk to remote echo server + * will talk to server until echo doesn't match sent data */ //Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo' + +using namespace mts; + bool testTcpSocketEchoLoop(); void testTcpSocketEcho() { - using namespace mts; - Code code; const int TEST_PORT = 5798; - const std::string TEST_SERVER("204.26.122.96"); + const std::string TEST_SERVER( /* public IP of server running the netcat command given above */); printf("TCP SOCKET TESTING\r\n"); +#if CELL_SHIELD + for (int i = 30; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } + Transport::setTransport(Transport::CELLULAR); + MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); + serial->baud(115200); + Cellular::getInstance()->init(serial); + printf("Setting APN\r\n"); - code = Cellular::getInstance()->setApn("b2b.tmobile.com"); + code = Cellular::getInstance()->setApn("wap.cingular"); if(code == SUCCESS) { printf("Success!\r\n"); } else { printf("Error during APN setup [%d]\r\n", (int)code); } +#else + for (int i = 6; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } + Transport::setTransport(Transport::WIFI); + MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); + serial->baud(9600); + Wifi::getInstance()->init(serial); - printf("Setting Socket Closeable\r\n"); - code = Cellular::getInstance()->setSocketCloseable(); + code = Wifi::getInstance()->setNetwork("your wireless network" /* SSID of wireless */, Wifi::WPA2 /* security type of wireless */, "your wireless network password" /* password for wireless */); + if(code == SUCCESS) { + printf("Success!\r\n"); + } else { + printf("Error during network setup [%d]\r\n", (int)code); + } + code = Wifi::getInstance()->setDeviceIP(); if(code == SUCCESS) { printf("Success!\r\n"); } else { - printf("Error setting socket closeable [%d]\r\n", (int)code); + printf("Error during IP setup [%d]\r\n", (int)code); } +#endif - printf("Setting Local Port\r\n"); - if(Cellular::getInstance()->bind(5000)) { + printf("Establishing Connection\r\n"); +#if CELL_SHIELD + if(Cellular::getInstance()->connect()) { +#else + if(Wifi::getInstance()->connect()) { +#endif printf("Success!\r\n"); } else { - printf("Error setting local port [%d]\r\n", (int)code); - } - - printf("Setting Primary DNS\r\n"); - code = Cellular::getInstance()->setDns("8.8.8.8"); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error setting primary DNS [%d]\r\n", (int)code); - } - - printf("Establishing PPP Connection\r\n"); - if(Cellular::getInstance()->connect()) { - printf("Success!\r\n"); - } else { - printf("Error during PPP connection. Aborting.\r\n"); + printf("Error during connection. Aborting.\r\n"); return; } - printf("Opening a TCP Socket\r\n"); +#if CELL_SHIELD if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { +#else + if(Wifi::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { +#endif printf("Success!\r\n"); } else { printf("Error during TCP socket open [%s:%d]. Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT); @@ -67,11 +91,20 @@ } printf("Closing socket\r\n"); +#if CELL_SHIELD Cellular::getInstance()->close(); - +#else + Wifi::getInstance()->close(); +#endif - //printf("Disconnecting\r\n"); -// Cellular::getInstance()->disconnect(); + wait(10); + + printf("Disconnecting\r\n"); +#if CELL_SHIELD + Cellular::getInstance()->disconnect(); +#else + Wifi::getInstance()->disconnect(); +#endif } bool testTcpSocketEchoLoop() { @@ -95,12 +128,20 @@ char echoData[size]; printf("Sending buffer\r\n"); +#if CELL_SHIELD int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000); +#else + int bytesWritten = Wifi::getInstance()->write(buffer, size, 10000); +#endif if(bytesWritten == size) { printf("Successfully sent buffer\r\n"); } else { printf("Failed to send buffer. Closing socket and aborting.\r\n"); +#if CELL_SHIELD Cellular::getInstance()->close(); +#else + Wifi::getInstance()->close(); +#endif return false; } @@ -109,12 +150,20 @@ int bytesRead = 0; tmr.start(); do { +#if CELL_SHIELD int status = Cellular::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000); +#else + int status = Wifi::getInstance()->read(&echoData[bytesRead], size - bytesRead, 1000); +#endif if(status != -1) { bytesRead += status; } else { printf("Error reading from socket. Closing socket and aborting.\r\n"); +#if CELL_SHIELD Cellular::getInstance()->close(); +#else + Wifi::getInstance()->close(); +#endif return false; } printf("Total bytes read %d\r\n", bytesRead);
--- a/tests/test_UDP_Socket.h Tue Dec 31 23:41:59 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -#ifndef _TEST_UDP_SOCKET_ECHO_H_ -#define _TEST_UDP_SOCKET_ECHO_H_ - - -//Setup a netcat server with command: ncat -l 5798 -k -c 'xargs -n1 --null echo' - -void testUdpSocket() { - using namespace mts; - - Code code; - const int TEST_PORT = 5798; - const std::string TEST_SERVER("204.26.122.96"); - const char buffer[] = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890*"; - const int size = sizeof(buffer); - char receiveBuffer[size]; - - printf("UDP SOCKET TESTING\r\n"); - printf("Setting APN\r\n"); - code = Cellular::getInstance()->setApn("b2b.tmobile.com"); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error during APN setup [%d]\r\n", (int)code); - } - - printf("Setting Socket Closeable\r\n"); - code = Cellular::getInstance()->setSocketCloseable(); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error setting socket closeable [%d]\r\n", (int)code); - } - - printf("Setting Local Port\r\n"); - if(Cellular::getInstance()->bind(5000)) { - printf("Success!\r\n"); - } else { - printf("Error setting local port [%d]\r\n", (int)code); - } - - printf("Setting Primary DNS\r\n"); - code = Cellular::getInstance()->setDns("8.8.8.8"); - if(code == SUCCESS) { - printf("Success!\r\n"); - } else { - printf("Error setting primary DNS [%d]\r\n", (int)code); - } - - printf("Establishing PPP Connection\r\n"); - if(Cellular::getInstance()->connect()) { - printf("Success!\r\n"); - } else { - printf("Error during PPP connection. Aborting.\r\n"); - return; - } - - printf("Opening a UDP Socket\r\n"); - if(Cellular::getInstance()->open(TEST_SERVER, TEST_PORT, IPStack::UDP)) { - printf("Success!\r\n"); - } else { - printf("Error during UDP socket open [%s:%d]. Aborting.\r\n", TEST_SERVER.c_str(), TEST_PORT); - return; - } - - printf("Sending buffer\r\n"); - int bytesWritten = Cellular::getInstance()->write(buffer, size, 10000); - if(bytesWritten == size) { - printf("Successfully sent buffer\r\n"); - } else { - printf("Failed to send buffer. Closing socket and aborting.\r\n"); - Cellular::getInstance()->close(); - return; - } - - printf("Receiving echo (timeout = 15 seconds)\r\n"); - Timer tmr; - int bytesRead = 0; - tmr.start(); - do { - int status = Cellular::getInstance()->read(&receiveBuffer[bytesRead], size - bytesRead, 1000); - if(status != -1) { - bytesRead += status; - } else { - printf("Error reading from socket. Closing socket and aborting.\r\n"); - Cellular::getInstance()->close(); - return; - } - printf("Total bytes read %d\r\n", bytesRead); - } while (tmr.read_ms() <= 15000 && bytesRead < size); - - - //Safely Cap at Max Size - receiveBuffer[size - 1] = '\0'; - printf("Comparing Buffers\r\n"); - printf("SENT: [%s]\r\n", buffer); - printf("RECV: [%s]\r\n", receiveBuffer); - - printf("Closing socket\r\n"); - Cellular::getInstance()->close(); - - - printf("Disconnecting\r\n"); - Cellular::getInstance()->disconnect(); -} - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_main.cpp Thu Jan 02 18:11:25 2014 +0000 @@ -0,0 +1,47 @@ +#include "mbed.h" +#include "include_me.h" + +// uncomment only the header corresponding to the test you want to run +//#include "test_ping.h" +//#include "test_SMS.h" +//#include "test_TCP_Socket.h" +//#include "test_TCP_Socket_Echo.h" +//#include "test_MTS_Circular_Buffer.h" + + +//int main() { + // uncomment only one test at a time + + // PING TEST + //testPing(); + + /* + // SMS TEST + for (int i = 30; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } + Transport::setTransport(Transport::CELLULAR); + MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); + serial->baud(115200); + Cellular* cell = Cellular::getInstance(); + cell->init(serial); + while (cell->getRegistration() != Cellular::REGISTERED); + while (cell->setApn("wap.cingular") != SUCCESS); + + sendSms(); + while (true) { + receiveSms(); + wait(15); + } + */ + + // TCP SOCKET TEST + //testTcpSocket(); + + // TCP SOCKET ECHO TEST + //testTcpSocketEcho(); + + // CIRCULAR BUFFER TEST + //testMTSCircularBuffer(); +//} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_ping.h Thu Jan 02 18:11:25 2014 +0000 @@ -0,0 +1,165 @@ +#ifndef TESTPING_H +#define TESTPING_H + +#include "mbed.h" +#include "include_me.h" + +#define MAX_TRIES 5 +#define MAX_REGISTRATION_TRIES 10 + +// 0 for shield board with wifi +// 1 for shield board with cellular +#define CELL_SHIELD 0 + +/* tries to ping 8.8.8.8 (Google's DNS server) + * blinks green LED if successful, red LED if failure */ + +using namespace mts; + +bool cellPingTest(const std::string& apn, const std::string& server); +bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key); +void blinkLed(DigitalOut led); + +void testPing() { + DigitalOut ledG(LED1); + DigitalOut ledR(LED2); + + ledG = 1; + ledR = 1; + + std::string server = "8.8.8.8"; // Google's DNS server +#if CELL_SHIELD + std::string apn = "wap.cingular"; // APN of sim card + if (cellPingTest(apn, server)) { +#else + std::string ssid = ""; // ssid of wireless network + Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2 + std::string key = ""; // password for network (if type is not "NONE") + if (wifiPingTest(server, ssid, type, key)) { +#endif + printf("success!\n\r"); + blinkLed(ledG); + } else { + printf("failure!\n\r"); + blinkLed(ledR); + } +} + +bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) { + int i; + + for (int i = 6; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } + + Transport::setTransport(Transport::WIFI); + MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); + serial->baud(9600); + Wifi* wifi = Wifi::getInstance(); + wifi->init(serial); + + i = 0; + while (i++ < MAX_TRIES) { + if (wifi->setNetwork(ssid, type, key) == SUCCESS) { + printf("set network\r\n"); + break; + } else { + printf("failed to set network\r\n"); + } + wait(1); + } + + i = 0; + while (i++ < MAX_TRIES) { + if (wifi->setDeviceIP() == SUCCESS) { + printf("set IP\r\n"); + break; + } else { + printf("failed to set IP\r\n"); + } + wait(1); + } + + i = 0; + while (i++ < MAX_TRIES) { + if (wifi->connect()) { + printf("connected\r\n"); + break; + } else { + printf("failed to connect\r\n"); + } + wait(1); + } + + printf("pinging %s\n\r", server.c_str()); + return wifi->ping(server); +} + +bool cellPingTest(const std::string& apn, const std::string& server) { + int i; + + + for (int i = 30; i >= 0; i = i - 2) { + wait(2); + printf("Waiting %d seconds...\n\r", i); + } + + Transport::setTransport(Transport::CELLULAR); + MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); + serial->baud(115200); + Cellular* cell = Cellular::getInstance(); + cell->init(serial); + + i = 0; + while (i++ < MAX_REGISTRATION_TRIES) { + if (cell->getRegistration() == Cellular::REGISTERED) { + printf("registered with tower\n\r"); + break; + } else if (i >= MAX_REGISTRATION_TRIES) { + printf("failed to register with tower\n\r"); + return false; + } + wait(3); + } + + i = 0; + printf("setting APN to %s\n\r", apn.c_str()); + while (i++ < MAX_TRIES) { + if (cell->setApn(apn) == SUCCESS) { + printf("successfully set APN\n\r"); + break; + } else if (i >= MAX_TRIES) { + printf("failed to set APN\n\r"); + return false; + } + wait(1); + } + + i = 0; + printf("bringing up PPP link\n\r"); + while (i++ < MAX_TRIES) { + if (cell->connect()) { + printf("PPP link is up\n\r"); + break; + } else if (i >= MAX_TRIES) { + printf("failed to bring PPP link up\n\r"); + return false; + } + wait(1); + } + + printf("pinging %s\n\r", server.c_str()); + return cell->ping(server); +} + +void blinkLed(DigitalOut led) { + led = 0; + + while (true) { + wait(0.25); + led = !led; + } +} + +#endif \ No newline at end of file