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 118:da77017d6c09, committed 2013-12-31
- Comitter:
- jengbrecht
- Date:
- Tue Dec 31 22:02:07 2013 +0000
- Parent:
- 117:e59a616ffe16
- Parent:
- 116:8b58efef3528
- Child:
- 119:544578fd290c
- Commit message:
- Merged.
Changed in this revision
--- a/include_me.h Tue Dec 31 22:00:53 2013 +0000 +++ b/include_me.h Tue Dec 31 22:02:07 2013 +0000 @@ -3,4 +3,4 @@ #include "MTSSerial.h" #include "MTSSerialFlowControl.h" #include "TCPSocketConnection.h" -#include "UDPSocket.h" \ No newline at end of file +#include "Transport.h" \ No newline at end of file
--- a/tests/blinky_ping_test.h Tue Dec 31 22:00:53 2013 +0000 +++ b/tests/blinky_ping_test.h Tue Dec 31 22:02:07 2013 +0000 @@ -1,24 +1,35 @@ #include "mbed.h" #include "include_me.h" -using namespace mts; - #define MAX_TRIES 5 #define MAX_REGISTRATION_TRIES 10 -bool ping_test(const std::string& apn, const std::string& server); +#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 blinkyPingTest() { +void pingTest() { DigitalOut ledG(LED1); DigitalOut ledR(LED2); - std::string apn = "wap.cingular"; - std::string server = "8.8.8.8"; ledG = 1; ledR = 1; - - if (ping_test(apn, server)) { + + 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 { @@ -27,9 +38,59 @@ } } -bool ping_test(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) { 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(); @@ -50,8 +111,6 @@ wait(3); } - printf("signal strength: %d\n\r", cell->getSignalStrength()); - i = 0; printf("setting APN to %s\n\r", apn.c_str()); while (i++ < MAX_TRIES) {
--- a/tests/flowControlLoopback.h Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -#include "MTSSerialFlowControl.h" - -using namespace mts; - -void flowControlLoopback() { - char buf[16]; - int size = 16; - int tmp = 0; - MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTD1, PTD0); - //MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTC4, PTC3, , ); - serial->baud(9600); - - wait(3); - printf("starting flow control loopback\n\r"); - - while (true) { - if ((tmp = serial->read(buf, size)) < 0) { - printf("read failed\n\r"); - } - if ((tmp = serial->write(buf, tmp)) < 0) { - printf("write failed\n\r"); - } - } -} \ No newline at end of file
--- a/tests/flowControlPassThrough.h Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -#include "MTSSerialFlowControl.h" - -using namespace mts; - -void flowControlPassThrough() { - int tmp; - char buf[16]; - int size = 16; - int baud = 115200; - - MTSSerialFlowControl *port1 = new MTSSerialFlowControl(PTC4, PTC3, PTD5, PTA13, 64, 64); - port1->baud(baud); - MTSSerialFlowControl *port2 = new MTSSerialFlowControl(PTD3, PTD2, PTD1, PTD0, 64, 64); - port2->baud(baud); - - wait(3); - printf("starting flow control passthrough - baud: %d\n\r", baud); - - while (true) { - if ((tmp = port1->read(buf, size)) < 0) { - printf("read on port1 failed\n\r"); - } else if (tmp > 0) { - if ((tmp = port2->write(buf, tmp)) < 0) { - printf("write on port2 failed\n\r"); - } - } - if ((tmp = port2->read(buf, size)) < 0) { - printf("read on port2 failed\n\r"); - } else if (tmp > 0) { - if ((tmp = port1->write(buf, tmp)) < 0) { - printf("write on port1 failed\n\r"); - } - } - } -} \ No newline at end of file
--- a/tests/loopback.h Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -#include "MTSSerial.h" - -using namespace mts; - -void loopback() { - char buf[16]; - int size = 16; - int tmp = 0; - //MTSSerial* serial = new MTSSerial(PTD3, PTD2); - MTSSerial* serial = new MTSSerial(PTC4, PTC3); - serial->baud(9600); - - wait(3); - printf("starting loopback\n\r"); - - while (true) { - if ((tmp = serial->read(buf, size)) < 0) { - printf("read failed\n\r"); - } - if ((tmp = serial->write(buf, tmp)) < 0) { - printf("write failed\n\r"); - } - } -} \ No newline at end of file
--- a/tests/passThrough.h Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -#include "MTSSerial.h" - -using namespace mts; - -void passThrough() { - int tmp; - char buf[128]; - int size = 128; - - MTSSerial *port1 = new MTSSerial(PTC4, PTC3, 512, 512); - port1->baud(19200); - MTSSerial *port2 = new MTSSerial(PTD3, PTD2, 512, 512); - port2->baud(19200); - - wait(3); - printf("starting passthrough\n\r"); - - while (true) { - if ((tmp = port1->read(buf, size)) < 0) { - printf("read on port1 failed\n\r"); - } else if (tmp > 0) { - if ((tmp = port2->write(buf, tmp)) < 0) { - printf("write on port2 failed\n\r"); - } - } - if ((tmp = port2->read(buf, size)) < 0) { - printf("read on port2 failed\n\r"); - } else if (tmp > 0) { - if ((tmp = port1->write(buf, tmp)) < 0) { - printf("write on port1 failed\n\r"); - } - } - } -} \ No newline at end of file
--- a/tests/serial_test.cpp Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -#include "passThrough.h" -#include "loopback.h" -#include "flowControlLoopback.h" -#include "flowControlPassThrough.h" - -void serial_test() { - //passThrough(); - //loopback(); - //flowControlLoopback(); - flowControlPassThrough(); -}
--- a/tests/test.h Tue Dec 31 22:00:53 2013 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -#ifndef TEST_H -#define TEST_H - -#include "testMTSCircularBuffer.h" - -#endif /* TEST_H */ \ No newline at end of file
--- a/tests/test_SMS.h Tue Dec 31 22:00:53 2013 +0000 +++ b/tests/test_SMS.h Tue Dec 31 22:02:07 2013 +0000 @@ -2,15 +2,17 @@ #ifndef _TEST_SMS_H_ #define _TEST_SMS_H_ +using namespace mts; + void sendSms() { - Cellular::Code code; + Code code; std::string sMsg("Hello from Multi-Tech MBED!"); - std::string sPhoneNum("16514085259"); + std::string sPhoneNum(""); // insert your phone number prepended with a 1, if you number is 222-867-5309 you'd put 12228675309 printf("Sending message [%s] to [%s]\r\n", sMsg.c_str(), sPhoneNum.c_str()); code = Cellular::getInstance()->sendSMS(sPhoneNum, sMsg); - if(code != Cellular::CELL_OK) { + if(code != SUCCESS) { printf("Error during SMS send [%d]\r\n", (int)code); } else { printf("Success!\r\n");
--- a/tests/test_TCP_Socket.h Tue Dec 31 22:00:53 2013 +0000 +++ b/tests/test_TCP_Socket.h Tue Dec 31 22:02:07 2013 +0000 @@ -1,6 +1,8 @@ #ifndef _TEST_TCP_SOCKET_H_ #define _TEST_TCP_SOCKET_H_ +#define CELL_SHIELD 0 // if using a cell shield board, change to 1 + using namespace mts; const char PATTERN_LINE1[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|"; const char PATTERN[] = "abcdefghijklmnopqrstuvwzyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()[]{}|\r\n" @@ -33,8 +35,14 @@ const std::string TEST_SERVER("204.26.122.5"); printf("TCP SOCKET TESTING\r\n"); +#if CELL_SHIELD + 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 { @@ -48,16 +56,43 @@ } else { printf("Error setting socket closeable [%d]\r\n", (int)code); } +#else + Transport::setTransport(Transport::WIFI); + MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); + serial->baud(9600); + Wifi::getInstance()->init(serial); - printf("Establishing PPP Connection\r\n"); - if(Cellular::getInstance()->connect()) { + code = Wifi::getInstance()->setNetwork("Mtech_guest", Wifi::WPA2, "MultiModem2!"); + if(code == SUCCESS) { printf("Success!\r\n"); } else { - printf("Error during PPP connection\r\n"); + printf("Error during network setup [%d]\r\n", (int)code); + } + code = Wifi::getInstance()->setDeviceIP(); + if(code == SUCCESS) { + printf("Success!\r\n"); + } else { + printf("Error during IP setup [%d]\r\n", (int)code); + } +#endif + + 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 during connection\r\n"); } 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]\r\n", TEST_SERVER.c_str(), TEST_PORT); @@ -73,12 +108,20 @@ printf("Closing socket\r\n"); +#if CELL_SHIELD Cellular::getInstance()->close(); +#else + Wifi::getInstance()->close(); +#endif wait(10); printf("Disconnecting\r\n"); +#if CELL_SHIELD Cellular::getInstance()->disconnect(); +#else + Wifi::getInstance()->disconnect(); +#endif } bool testTcpSocketIteration() { @@ -92,7 +135,11 @@ printf("Receiving Data\r\n"); tmr.start(); do { +#if CELL_SHIELD int size = Cellular::getInstance()->read(buffer, bufferSize, 1000); +#else + int size = Wifi::getInstance()->read(buffer, bufferSize, 1000); +#endif if(size != -1) { result.append(buffer, size); } else { @@ -146,21 +193,33 @@ result.clear(); printf("Writing to socket: 2\r\n"); +#if CELL_SHIELD if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) { +#else + if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) { +#endif printf("Successfully wrote '2'\r\n"); } else { printf("Failed to write '2'\r\n"); return false; } printf("Expecting 'how many ? >:\r\n"); +#if CELL_SHIELD bytesRead = Cellular::getInstance()->read(buffer, bufferSize, 10000); +#else + bytesRead = Wifi::getInstance()->read(buffer, bufferSize, 10000); +#endif if(bytesRead != -1) { result.append(buffer, bytesRead); printf("READ: [%d] [%s]\r\n", bytesRead, result.c_str()); if(result.find("how many") != std::string::npos) { printf("Successfully found 'how many'\r\n"); printf("Writing to socket: 2\r\n"); +#if CELL_SHIELD if(Cellular::getInstance()->write("2\r\n", 3, 10000) == 3) { +#else + if(Wifi::getInstance()->write("2\r\n", 3, 10000) == 3) { +#endif printf("Successfully wrote '2'\r\n"); } else { printf("Failed to write '2'\r\n");