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