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
Diff: tests/test_ping.h
- Revision:
- 124:6d964b4343c8
- Parent:
- 115:b26176f23e89
- Child:
- 141:571e0ef6c8dc
--- /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