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
test_ping.h
00001 /* Universal Socket Modem Interface Library 00002 * Copyright (c) 2013 Multi-Tech Systems 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef TESTPING_H 00018 #define TESTPING_H 00019 00020 #include "mbed.h" 00021 #include "include_me.h" 00022 00023 #define MAX_TRIES 5 00024 #define MAX_REGISTRATION_TRIES 10 00025 00026 // 0 for shield board with wifi 00027 // 1 for shield board with cellular 00028 #define CELL_SHIELD 0 00029 00030 /* tries to ping 8.8.8.8 (Google's DNS server) 00031 * blinks green LED if successful, red LED if failure */ 00032 00033 using namespace mts; 00034 00035 bool cellPingTest(const std::string& apn, const std::string& server); 00036 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key); 00037 void blinkLed(DigitalOut led); 00038 00039 void testPing() { 00040 DigitalOut ledG(LED1); 00041 DigitalOut ledR(LED2); 00042 00043 ledG = 1; 00044 ledR = 1; 00045 00046 std::string server = "8.8.8.8"; // Google's DNS server 00047 #if CELL_SHIELD 00048 std::string apn = "wap.cingular"; // APN of sim card 00049 if (cellPingTest(apn, server)) { 00050 #else 00051 std::string ssid = ""; // ssid of wireless network 00052 Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2 00053 std::string key = ""; // password for network (if type is not "NONE") 00054 if (wifiPingTest(server, ssid, type, key)) { 00055 #endif 00056 printf("success!\n\r"); 00057 blinkLed(ledG); 00058 } else { 00059 printf("failure!\n\r"); 00060 blinkLed(ledR); 00061 } 00062 } 00063 00064 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) { 00065 int i; 00066 00067 for (int i = 6; i >= 0; i = i - 2) { 00068 wait(2); 00069 printf("Waiting %d seconds...\n\r", i); 00070 } 00071 00072 Transport::setTransport(Transport::WIFI); 00073 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256); 00074 serial->baud(9600); 00075 Wifi* wifi = Wifi::getInstance(); 00076 wifi->init(serial); 00077 00078 i = 0; 00079 while (i++ < MAX_TRIES) { 00080 if (wifi->setNetwork(ssid, type, key) == SUCCESS) { 00081 printf("set network\r\n"); 00082 break; 00083 } else { 00084 printf("failed to set network\r\n"); 00085 } 00086 wait(1); 00087 } 00088 00089 i = 0; 00090 while (i++ < MAX_TRIES) { 00091 if (wifi->setDeviceIP() == SUCCESS) { 00092 printf("set IP\r\n"); 00093 break; 00094 } else { 00095 printf("failed to set IP\r\n"); 00096 } 00097 wait(1); 00098 } 00099 00100 i = 0; 00101 while (i++ < MAX_TRIES) { 00102 if (wifi->connect()) { 00103 printf("connected\r\n"); 00104 break; 00105 } else { 00106 printf("failed to connect\r\n"); 00107 } 00108 wait(1); 00109 } 00110 00111 printf("pinging %s\n\r", server.c_str()); 00112 return wifi->ping(server); 00113 } 00114 00115 bool cellPingTest(const std::string& apn, const std::string& server) { 00116 int i; 00117 00118 Transport::setTransport(Transport::CELLULAR); 00119 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); 00120 serial->baud(115200); 00121 Cellular* cell = Cellular::getInstance(); 00122 cell->init(serial); 00123 00124 i = 0; 00125 while (i++ < MAX_REGISTRATION_TRIES) { 00126 if (cell->getRegistration() == Cellular::REGISTERED) { 00127 printf("registered with tower\n\r"); 00128 break; 00129 } else if (i >= MAX_REGISTRATION_TRIES) { 00130 printf("failed to register with tower\n\r"); 00131 return false; 00132 } 00133 wait(3); 00134 } 00135 00136 i = 0; 00137 printf("setting APN to %s\n\r", apn.c_str()); 00138 while (i++ < MAX_TRIES) { 00139 if (cell->setApn(apn) == SUCCESS) { 00140 printf("successfully set APN\n\r"); 00141 break; 00142 } else if (i >= MAX_TRIES) { 00143 printf("failed to set APN\n\r"); 00144 return false; 00145 } 00146 wait(1); 00147 } 00148 00149 i = 0; 00150 printf("bringing up PPP link\n\r"); 00151 while (i++ < MAX_TRIES) { 00152 if (cell->connect()) { 00153 printf("PPP link is up\n\r"); 00154 break; 00155 } else if (i >= MAX_TRIES) { 00156 printf("failed to bring PPP link up\n\r"); 00157 return false; 00158 } 00159 wait(1); 00160 } 00161 00162 printf("pinging %s\n\r", server.c_str()); 00163 return cell->ping(server); 00164 } 00165 00166 void blinkLed(DigitalOut led) { 00167 led = 0; 00168 00169 while (true) { 00170 wait(0.25); 00171 led = !led; 00172 } 00173 } 00174 00175 #endif
Generated on Tue Jul 12 2022 21:46:23 by 1.7.2