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

Committer:
kranjan
Date:
Sat Jan 04 05:28:45 2014 +0000
Revision:
141:571e0ef6c8dc
Parent:
124:6d964b4343c8
Child:
143:c7d8fe37981b
Added licensing header to all files in the library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kranjan 141:571e0ef6c8dc 1 /* Universal Socket Modem Interface Library
kranjan 141:571e0ef6c8dc 2 * Copyright (c) 2013 Multi-Tech Systems
kranjan 141:571e0ef6c8dc 3 *
kranjan 141:571e0ef6c8dc 4 * Licensed under the Apache License, Version 2.0 (the "License");
kranjan 141:571e0ef6c8dc 5 * you may not use this file except in compliance with the License.
kranjan 141:571e0ef6c8dc 6 * You may obtain a copy of the License at
kranjan 141:571e0ef6c8dc 7 *
kranjan 141:571e0ef6c8dc 8 * http://www.apache.org/licenses/LICENSE-2.0
kranjan 141:571e0ef6c8dc 9 *
kranjan 141:571e0ef6c8dc 10 * Unless required by applicable law or agreed to in writing, software
kranjan 141:571e0ef6c8dc 11 * distributed under the License is distributed on an "AS IS" BASIS,
kranjan 141:571e0ef6c8dc 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kranjan 141:571e0ef6c8dc 13 * See the License for the specific language governing permissions and
kranjan 141:571e0ef6c8dc 14 * limitations under the License.
kranjan 141:571e0ef6c8dc 15 */
kranjan 141:571e0ef6c8dc 16
mfiore 124:6d964b4343c8 17 #ifndef TESTPING_H
mfiore 124:6d964b4343c8 18 #define TESTPING_H
mfiore 124:6d964b4343c8 19
mfiore 26:f3e06d63967f 20 #include "mbed.h"
mfiore 26:f3e06d63967f 21 #include "include_me.h"
mfiore 26:f3e06d63967f 22
mfiore 26:f3e06d63967f 23 #define MAX_TRIES 5
mfiore 31:31b95ea90f80 24 #define MAX_REGISTRATION_TRIES 10
mfiore 26:f3e06d63967f 25
mfiore 124:6d964b4343c8 26 // 0 for shield board with wifi
mfiore 124:6d964b4343c8 27 // 1 for shield board with cellular
mfiore 124:6d964b4343c8 28 #define CELL_SHIELD 0
mfiore 124:6d964b4343c8 29
mfiore 124:6d964b4343c8 30 /* tries to ping 8.8.8.8 (Google's DNS server)
mfiore 124:6d964b4343c8 31 * blinks green LED if successful, red LED if failure */
mfiore 115:b26176f23e89 32
mfiore 115:b26176f23e89 33 using namespace mts;
mfiore 115:b26176f23e89 34
mfiore 115:b26176f23e89 35 bool cellPingTest(const std::string& apn, const std::string& server);
mfiore 115:b26176f23e89 36 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key);
mfiore 26:f3e06d63967f 37 void blinkLed(DigitalOut led);
mfiore 26:f3e06d63967f 38
mfiore 124:6d964b4343c8 39 void testPing() {
mfiore 26:f3e06d63967f 40 DigitalOut ledG(LED1);
mfiore 26:f3e06d63967f 41 DigitalOut ledR(LED2);
mfiore 26:f3e06d63967f 42
mfiore 26:f3e06d63967f 43 ledG = 1;
mfiore 26:f3e06d63967f 44 ledR = 1;
mfiore 115:b26176f23e89 45
mfiore 115:b26176f23e89 46 std::string server = "8.8.8.8"; // Google's DNS server
mfiore 115:b26176f23e89 47 #if CELL_SHIELD
mfiore 115:b26176f23e89 48 std::string apn = "wap.cingular"; // APN of sim card
mfiore 115:b26176f23e89 49 if (cellPingTest(apn, server)) {
mfiore 115:b26176f23e89 50 #else
mfiore 124:6d964b4343c8 51 std::string ssid = ""; // ssid of wireless network
mfiore 115:b26176f23e89 52 Wifi::SecurityType type = Wifi::WPA2; // NONE, WEP64, WEP128, WPA, WPA2
mfiore 124:6d964b4343c8 53 std::string key = ""; // password for network (if type is not "NONE")
mfiore 115:b26176f23e89 54 if (wifiPingTest(server, ssid, type, key)) {
mfiore 115:b26176f23e89 55 #endif
mfiore 26:f3e06d63967f 56 printf("success!\n\r");
mfiore 26:f3e06d63967f 57 blinkLed(ledG);
mfiore 26:f3e06d63967f 58 } else {
mfiore 26:f3e06d63967f 59 printf("failure!\n\r");
mfiore 26:f3e06d63967f 60 blinkLed(ledR);
mfiore 26:f3e06d63967f 61 }
mfiore 26:f3e06d63967f 62 }
mfiore 26:f3e06d63967f 63
mfiore 115:b26176f23e89 64 bool wifiPingTest(const std::string& server, const std::string& ssid, Wifi::SecurityType type, const std::string& key) {
mfiore 26:f3e06d63967f 65 int i;
mfiore 26:f3e06d63967f 66
mfiore 124:6d964b4343c8 67 for (int i = 6; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 68 wait(2);
mfiore 124:6d964b4343c8 69 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 70 }
mfiore 124:6d964b4343c8 71
mfiore 115:b26176f23e89 72 Transport::setTransport(Transport::WIFI);
mfiore 115:b26176f23e89 73 MTSSerial* serial = new MTSSerial(PTD3, PTD2, 256, 256);
mfiore 115:b26176f23e89 74 serial->baud(9600);
mfiore 115:b26176f23e89 75 Wifi* wifi = Wifi::getInstance();
mfiore 115:b26176f23e89 76 wifi->init(serial);
mfiore 115:b26176f23e89 77
mfiore 115:b26176f23e89 78 i = 0;
mfiore 115:b26176f23e89 79 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 80 if (wifi->setNetwork(ssid, type, key) == SUCCESS) {
mfiore 115:b26176f23e89 81 printf("set network\r\n");
mfiore 115:b26176f23e89 82 break;
mfiore 115:b26176f23e89 83 } else {
mfiore 115:b26176f23e89 84 printf("failed to set network\r\n");
mfiore 115:b26176f23e89 85 }
mfiore 115:b26176f23e89 86 wait(1);
mfiore 115:b26176f23e89 87 }
mfiore 115:b26176f23e89 88
mfiore 115:b26176f23e89 89 i = 0;
mfiore 115:b26176f23e89 90 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 91 if (wifi->setDeviceIP() == SUCCESS) {
mfiore 115:b26176f23e89 92 printf("set IP\r\n");
mfiore 115:b26176f23e89 93 break;
mfiore 115:b26176f23e89 94 } else {
mfiore 115:b26176f23e89 95 printf("failed to set IP\r\n");
mfiore 115:b26176f23e89 96 }
mfiore 115:b26176f23e89 97 wait(1);
mfiore 115:b26176f23e89 98 }
mfiore 115:b26176f23e89 99
mfiore 115:b26176f23e89 100 i = 0;
mfiore 115:b26176f23e89 101 while (i++ < MAX_TRIES) {
mfiore 115:b26176f23e89 102 if (wifi->connect()) {
mfiore 115:b26176f23e89 103 printf("connected\r\n");
mfiore 115:b26176f23e89 104 break;
mfiore 115:b26176f23e89 105 } else {
mfiore 115:b26176f23e89 106 printf("failed to connect\r\n");
mfiore 115:b26176f23e89 107 }
mfiore 115:b26176f23e89 108 wait(1);
mfiore 115:b26176f23e89 109 }
mfiore 115:b26176f23e89 110
mfiore 115:b26176f23e89 111 printf("pinging %s\n\r", server.c_str());
mfiore 115:b26176f23e89 112 return wifi->ping(server);
mfiore 115:b26176f23e89 113 }
mfiore 115:b26176f23e89 114
mfiore 115:b26176f23e89 115 bool cellPingTest(const std::string& apn, const std::string& server) {
mfiore 115:b26176f23e89 116 int i;
mfiore 115:b26176f23e89 117
mfiore 124:6d964b4343c8 118
mfiore 124:6d964b4343c8 119 for (int i = 30; i >= 0; i = i - 2) {
mfiore 124:6d964b4343c8 120 wait(2);
mfiore 124:6d964b4343c8 121 printf("Waiting %d seconds...\n\r", i);
mfiore 124:6d964b4343c8 122 }
mfiore 124:6d964b4343c8 123
mfiore 115:b26176f23e89 124 Transport::setTransport(Transport::CELLULAR);
mfiore 26:f3e06d63967f 125 MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8);
mfiore 26:f3e06d63967f 126 serial->baud(115200);
mfiore 105:1977b7154940 127 Cellular* cell = Cellular::getInstance();
mfiore 105:1977b7154940 128 cell->init(serial);
mfiore 26:f3e06d63967f 129
mfiore 31:31b95ea90f80 130 i = 0;
mfiore 31:31b95ea90f80 131 while (i++ < MAX_REGISTRATION_TRIES) {
mfiore 31:31b95ea90f80 132 if (cell->getRegistration() == Cellular::REGISTERED) {
mfiore 31:31b95ea90f80 133 printf("registered with tower\n\r");
mfiore 31:31b95ea90f80 134 break;
mfiore 31:31b95ea90f80 135 } else if (i >= MAX_REGISTRATION_TRIES) {
mfiore 31:31b95ea90f80 136 printf("failed to register with tower\n\r");
mfiore 31:31b95ea90f80 137 return false;
mfiore 31:31b95ea90f80 138 }
mfiore 31:31b95ea90f80 139 wait(3);
mfiore 31:31b95ea90f80 140 }
mfiore 31:31b95ea90f80 141
mfiore 26:f3e06d63967f 142 i = 0;
mfiore 26:f3e06d63967f 143 printf("setting APN to %s\n\r", apn.c_str());
mfiore 26:f3e06d63967f 144 while (i++ < MAX_TRIES) {
mfiore 105:1977b7154940 145 if (cell->setApn(apn) == SUCCESS) {
mfiore 26:f3e06d63967f 146 printf("successfully set APN\n\r");
mfiore 26:f3e06d63967f 147 break;
mfiore 31:31b95ea90f80 148 } else if (i >= MAX_TRIES) {
mfiore 26:f3e06d63967f 149 printf("failed to set APN\n\r");
mfiore 26:f3e06d63967f 150 return false;
mfiore 26:f3e06d63967f 151 }
mfiore 26:f3e06d63967f 152 wait(1);
mfiore 26:f3e06d63967f 153 }
mfiore 26:f3e06d63967f 154
mfiore 26:f3e06d63967f 155 i = 0;
mfiore 26:f3e06d63967f 156 printf("bringing up PPP link\n\r");
mfiore 26:f3e06d63967f 157 while (i++ < MAX_TRIES) {
mfiore 26:f3e06d63967f 158 if (cell->connect()) {
mfiore 26:f3e06d63967f 159 printf("PPP link is up\n\r");
mfiore 26:f3e06d63967f 160 break;
mfiore 31:31b95ea90f80 161 } else if (i >= MAX_TRIES) {
mfiore 26:f3e06d63967f 162 printf("failed to bring PPP link up\n\r");
mfiore 26:f3e06d63967f 163 return false;
mfiore 26:f3e06d63967f 164 }
mfiore 26:f3e06d63967f 165 wait(1);
mfiore 26:f3e06d63967f 166 }
mfiore 26:f3e06d63967f 167
mfiore 26:f3e06d63967f 168 printf("pinging %s\n\r", server.c_str());
mfiore 26:f3e06d63967f 169 return cell->ping(server);
mfiore 26:f3e06d63967f 170 }
mfiore 26:f3e06d63967f 171
mfiore 26:f3e06d63967f 172 void blinkLed(DigitalOut led) {
mfiore 26:f3e06d63967f 173 led = 0;
mfiore 26:f3e06d63967f 174
mfiore 26:f3e06d63967f 175 while (true) {
mfiore 26:f3e06d63967f 176 wait(0.25);
mfiore 26:f3e06d63967f 177 led = !led;
mfiore 26:f3e06d63967f 178 }
mfiore 124:6d964b4343c8 179 }
mfiore 124:6d964b4343c8 180
kranjan 141:571e0ef6c8dc 181 #endif