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/cellular/Cellular.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 CELLULAR_H |
AxedaCorp | 0:65004368569c | 18 | #define CELLULAR_H |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | #include "IPStack.h" |
AxedaCorp | 0:65004368569c | 21 | #include "MTSBufferedIO.h" |
AxedaCorp | 0:65004368569c | 22 | #include "mbed.h" |
AxedaCorp | 0:65004368569c | 23 | #include <string> |
AxedaCorp | 0:65004368569c | 24 | #include <vector> |
AxedaCorp | 0:65004368569c | 25 | |
AxedaCorp | 0:65004368569c | 26 | namespace mts |
AxedaCorp | 0:65004368569c | 27 | { |
AxedaCorp | 0:65004368569c | 28 | |
AxedaCorp | 0:65004368569c | 29 | /** This is a class for communicating with a Multi-Tech Systems SocketModem iCell. The |
AxedaCorp | 0:65004368569c | 30 | * SocketModem iCell is a family of carrier certified embedded cellular radio modules with |
AxedaCorp | 0:65004368569c | 31 | * a common hardware footprint and AT command set for built in IP-stack functionality. |
AxedaCorp | 0:65004368569c | 32 | * This class supports three main types of cellular radio interactions including: |
AxedaCorp | 0:65004368569c | 33 | * configuration and status AT command processing, SMS processing, and TCP Socket |
AxedaCorp | 0:65004368569c | 34 | * data connections. It should be noted that the radio can not process commands or |
AxedaCorp | 0:65004368569c | 35 | * SMS messages while having an open data connection at the same time. The concurrent |
AxedaCorp | 0:65004368569c | 36 | * capability may be added in a future release. This class also inherits from IPStack |
AxedaCorp | 0:65004368569c | 37 | * providing a common set of commands for communication devices that have an onboard |
AxedaCorp | 0:65004368569c | 38 | * IP Stack. It is also integrated with the standard mbed Sockets package and can therefore |
AxedaCorp | 0:65004368569c | 39 | * be used seamlessly with clients and services built on top of this interface already within |
AxedaCorp | 0:65004368569c | 40 | * the mbed library. |
AxedaCorp | 0:65004368569c | 41 | * |
AxedaCorp | 0:65004368569c | 42 | * All of the following examples use the Pin Names for the Freedom KL46Z board coupled with |
AxedaCorp | 0:65004368569c | 43 | * the SocketModem Shield Arduino compatible board. Please chage Pin Names accordingly to |
AxedaCorp | 0:65004368569c | 44 | * match your hardware configuration. It also assumes the use of RTS/CTS hardware handshaking |
AxedaCorp | 0:65004368569c | 45 | * using GPIOs. To disable this you will need to change settings on the radio module and |
AxedaCorp | 0:65004368569c | 46 | * and use the MTSSerial class instead of MTSSerialFlowControl. The default baud rate for the |
AxedaCorp | 0:65004368569c | 47 | * cellular radio is 115200 bps. |
AxedaCorp | 0:65004368569c | 48 | * |
AxedaCorp | 0:65004368569c | 49 | * The following set of example code demonstrates how to send and receive configuration and |
AxedaCorp | 0:65004368569c | 50 | * status AT commands with the radio, create a data connection and test it: |
AxedaCorp | 0:65004368569c | 51 | * @code |
AxedaCorp | 0:65004368569c | 52 | * #include "mbed.h" |
AxedaCorp | 0:65004368569c | 53 | * #include "Cellular.h" |
AxedaCorp | 0:65004368569c | 54 | * #include "MTSSerialFlowControl.h" |
AxedaCorp | 0:65004368569c | 55 | * |
AxedaCorp | 0:65004368569c | 56 | * using namespace mts; |
AxedaCorp | 0:65004368569c | 57 | * |
AxedaCorp | 0:65004368569c | 58 | * main() { |
AxedaCorp | 0:65004368569c | 59 | * //Setup serial interface to radio |
AxedaCorp | 0:65004368569c | 60 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
AxedaCorp | 0:65004368569c | 61 | * serial->baud(115200); |
AxedaCorp | 0:65004368569c | 62 | * |
AxedaCorp | 0:65004368569c | 63 | * //Setup Cellular class |
AxedaCorp | 0:65004368569c | 64 | * Cellular* cellular = Cellular::getInstance(); |
AxedaCorp | 0:65004368569c | 65 | * cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
AxedaCorp | 0:65004368569c | 66 | * |
AxedaCorp | 0:65004368569c | 67 | * //Run status and configuration commands |
AxedaCorp | 0:65004368569c | 68 | * printf("\n\r////Start Status and Configuration Commands////\n\r"); |
AxedaCorp | 0:65004368569c | 69 | * printf("Command Test: %s\n\r", getCodeNames(cellular->test()).c_str()); |
AxedaCorp | 0:65004368569c | 70 | * printf("Signal Strength: %d\n\r", cellular->getSignalStrength()); |
AxedaCorp | 0:65004368569c | 71 | * printf("Registration State: %s\n\r", Cellular::getRegistrationNames(cellular->getRegistration()).c_str()); |
AxedaCorp | 0:65004368569c | 72 | * printf("Send Basic Command (AT): %s\n\r", getCodeNames(cellular->sendBasicCommand("AT", 1000)).c_str()); |
AxedaCorp | 0:65004368569c | 73 | * printf("Send Command (AT+CSQ): %s\n\r", cellular->sendCommand("AT+CSQ", 1000).c_str()); |
AxedaCorp | 0:65004368569c | 74 | * |
AxedaCorp | 0:65004368569c | 75 | * //Start Test |
AxedaCorp | 0:65004368569c | 76 | * printf("\n\r////Start Network Connectivity Test////\n\r"); |
AxedaCorp | 0:65004368569c | 77 | * printf("Set APN: %s\n\r", getCodeNames(cellular->setApn("wap.cingular")).c_str()); //Use APN from service provider!!! |
AxedaCorp | 0:65004368569c | 78 | * |
AxedaCorp | 0:65004368569c | 79 | * //Setup a data connection |
AxedaCorp | 0:65004368569c | 80 | * printf("Attempting to Connect, this may take some time...\n\r"); |
AxedaCorp | 0:65004368569c | 81 | * while (!cellular->connect()) { |
AxedaCorp | 0:65004368569c | 82 | * printf("Failed to connect... Trying again.\n\r"); |
AxedaCorp | 0:65004368569c | 83 | * wait(1); |
AxedaCorp | 0:65004368569c | 84 | * } |
AxedaCorp | 0:65004368569c | 85 | * printf("Connected to the Network!\n\r"); |
AxedaCorp | 0:65004368569c | 86 | * |
AxedaCorp | 0:65004368569c | 87 | * //Try pinging default server "8.8.8.8" |
AxedaCorp | 0:65004368569c | 88 | * printf("Ping Valid: %s\n\r", cellular->ping() ? "true" : "false"); |
AxedaCorp | 0:65004368569c | 89 | * |
AxedaCorp | 0:65004368569c | 90 | * printf("End Program\n\r"); |
AxedaCorp | 0:65004368569c | 91 | * } |
AxedaCorp | 0:65004368569c | 92 | * @endcode |
AxedaCorp | 0:65004368569c | 93 | * |
AxedaCorp | 0:65004368569c | 94 | * The following set of example code demonstrates how process SMS messages: |
AxedaCorp | 0:65004368569c | 95 | * @code |
AxedaCorp | 0:65004368569c | 96 | * #include "mbed.h" |
AxedaCorp | 0:65004368569c | 97 | * #include "Cellular.h" |
AxedaCorp | 0:65004368569c | 98 | * #include "MTSSerialFlowControl.h" |
AxedaCorp | 0:65004368569c | 99 | * |
AxedaCorp | 0:65004368569c | 100 | * using namespace mts; |
AxedaCorp | 0:65004368569c | 101 | * |
AxedaCorp | 0:65004368569c | 102 | * main() { |
AxedaCorp | 0:65004368569c | 103 | * //Setup serial interface to radio |
AxedaCorp | 0:65004368569c | 104 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
AxedaCorp | 0:65004368569c | 105 | * serial->baud(115200); |
AxedaCorp | 0:65004368569c | 106 | * |
AxedaCorp | 0:65004368569c | 107 | * //Setup Cellular class |
AxedaCorp | 0:65004368569c | 108 | * Cellular* cellular = Cellular::getInstance(); |
AxedaCorp | 0:65004368569c | 109 | * cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
AxedaCorp | 0:65004368569c | 110 | * |
AxedaCorp | 0:65004368569c | 111 | * //Start test |
AxedaCorp | 0:65004368569c | 112 | * printf("AT Test: %s\n\r", getCodeNames(cellular->test()).c_str()); |
AxedaCorp | 0:65004368569c | 113 | * |
AxedaCorp | 0:65004368569c | 114 | * //Waiting for network registration |
AxedaCorp | 0:65004368569c | 115 | * printf("Checking Network Registration, this may take some time...\n\r"); |
AxedaCorp | 0:65004368569c | 116 | * while (cellular->getRegistration() != Cellular::REGISTERED) { |
AxedaCorp | 0:65004368569c | 117 | * printf("Still waiting... Checking again.\n\r"); |
AxedaCorp | 0:65004368569c | 118 | * wait(1); |
AxedaCorp | 0:65004368569c | 119 | * } |
AxedaCorp | 0:65004368569c | 120 | * printf("Connected to the Network!\n\r"); |
AxedaCorp | 0:65004368569c | 121 | * |
AxedaCorp | 0:65004368569c | 122 | * //Send SMS Message |
AxedaCorp | 0:65004368569c | 123 | * Code code; |
AxedaCorp | 0:65004368569c | 124 | * std::string sMsg("Hello from Multi-Tech MBED!"); |
AxedaCorp | 0:65004368569c | 125 | * std::string sPhoneNum("16128675309"); //Put your phone number here or leave Jenny's... |
AxedaCorp | 0:65004368569c | 126 | * |
AxedaCorp | 0:65004368569c | 127 | * printf("Sending message [%s] to [%s]\r\n", sMsg.c_str(), sPhoneNum.c_str()); |
AxedaCorp | 0:65004368569c | 128 | * code = cellular->sendSMS(sPhoneNum, sMsg); |
AxedaCorp | 0:65004368569c | 129 | * |
AxedaCorp | 0:65004368569c | 130 | * if(code != SUCCESS) { |
AxedaCorp | 0:65004368569c | 131 | * printf("Error during SMS send [%s]\r\n", getCodeNames(code).c_str()); |
AxedaCorp | 0:65004368569c | 132 | * } else { |
AxedaCorp | 0:65004368569c | 133 | * printf("Success!\r\n"); |
AxedaCorp | 0:65004368569c | 134 | * } |
AxedaCorp | 0:65004368569c | 135 | * |
AxedaCorp | 0:65004368569c | 136 | * //Try and receive SMS messages |
AxedaCorp | 0:65004368569c | 137 | * //To determine your radio's phone number send yourself an SMS and check the received # |
AxedaCorp | 0:65004368569c | 138 | * printf("Checking Received Messages\r\n"); |
AxedaCorp | 0:65004368569c | 139 | * std::vector<Cellular::Sms> vSms = cellular->getReceivedSms(); |
AxedaCorp | 0:65004368569c | 140 | * printf("\r\n"); |
AxedaCorp | 0:65004368569c | 141 | * for(unsigned int i = 0; i < vSms.size(); i++) { |
AxedaCorp | 0:65004368569c | 142 | * printf("[%d][%s][%s][%s]\r\n", i, vSms[i].timestamp.c_str(), |
AxedaCorp | 0:65004368569c | 143 | * vSms[i].phoneNumber.c_str(), vSms[i].message.c_str()); |
AxedaCorp | 0:65004368569c | 144 | * } |
AxedaCorp | 0:65004368569c | 145 | * printf("End Program\n\r"); |
AxedaCorp | 0:65004368569c | 146 | * } |
AxedaCorp | 0:65004368569c | 147 | * @endcode |
AxedaCorp | 0:65004368569c | 148 | * |
AxedaCorp | 0:65004368569c | 149 | * The following set of example code demonstrates how to setup and use a TCP socket connection |
AxedaCorp | 0:65004368569c | 150 | * using the native commands from this class: |
AxedaCorp | 0:65004368569c | 151 | * @code |
AxedaCorp | 0:65004368569c | 152 | * #include "mbed.h" |
AxedaCorp | 0:65004368569c | 153 | * #include "Cellular.h" |
AxedaCorp | 0:65004368569c | 154 | * #include "MTSSerialFlowControl.h" |
AxedaCorp | 0:65004368569c | 155 | * |
AxedaCorp | 0:65004368569c | 156 | * using namespace mts; |
AxedaCorp | 0:65004368569c | 157 | * |
AxedaCorp | 0:65004368569c | 158 | * main() { |
AxedaCorp | 0:65004368569c | 159 | * //Define connection parameters |
AxedaCorp | 0:65004368569c | 160 | * Code code; |
AxedaCorp | 0:65004368569c | 161 | * const int TEST_PORT = 5798; |
AxedaCorp | 0:65004368569c | 162 | * const std::string TEST_SERVER("204.26.122.96"); |
AxedaCorp | 0:65004368569c | 163 | * |
AxedaCorp | 0:65004368569c | 164 | * //Setup serial interface to radio |
AxedaCorp | 0:65004368569c | 165 | * MTSSerialFlowControl* serial = new MTSSerialFlowControl(PTD3, PTD2, PTA12, PTC8); |
AxedaCorp | 0:65004368569c | 166 | * serial->baud(115200); |
AxedaCorp | 0:65004368569c | 167 | * |
AxedaCorp | 0:65004368569c | 168 | * //Setup Cellular class |
AxedaCorp | 0:65004368569c | 169 | * Cellular* cellular = Cellular::getInstance(); |
AxedaCorp | 0:65004368569c | 170 | * cellular->init(serial, PTA4, PTC9); //DCD and DTR pins for KL46Z |
AxedaCorp | 0:65004368569c | 171 | * |
AxedaCorp | 0:65004368569c | 172 | * //Start test |
AxedaCorp | 0:65004368569c | 173 | * printf("AT Test: %s\n\r", getCodeNames(cellular->test()).c_str()); |
AxedaCorp | 0:65004368569c | 174 | * |
AxedaCorp | 0:65004368569c | 175 | * printf("Setting APN\r\n"); |
AxedaCorp | 0:65004368569c | 176 | * code = cellular->setApn("wap.cingular"); // Use from your service provider! |
AxedaCorp | 0:65004368569c | 177 | * if(code == SUCCESS) { |
AxedaCorp | 0:65004368569c | 178 | * printf("Success!\r\n"); |
AxedaCorp | 0:65004368569c | 179 | * } else { |
AxedaCorp | 0:65004368569c | 180 | * printf("Error during APN setup [%s]\r\n", getCodeNames(code).c_str()); |
AxedaCorp | 0:65004368569c | 181 | * } |
AxedaCorp | 0:65004368569c | 182 | * |
AxedaCorp | 0:65004368569c | 183 | * //Setup a data connection |
AxedaCorp | 0:65004368569c | 184 | * printf("Attempting to Connect, this may take some time...\n\r"); |
AxedaCorp | 0:65004368569c | 185 | * while (!cellular->connect()) { |
AxedaCorp | 0:65004368569c | 186 | * printf("Failed to connect... Trying again.\n\r"); |
AxedaCorp | 0:65004368569c | 187 | * wait(1); |
AxedaCorp | 0:65004368569c | 188 | * } |
AxedaCorp | 0:65004368569c | 189 | * printf("Connected to the Network!\n\r"); |
AxedaCorp | 0:65004368569c | 190 | * |
AxedaCorp | 0:65004368569c | 191 | * printf("Opening a TCP Socket...\r\n"); |
AxedaCorp | 0:65004368569c | 192 | * if(cellular->open(TEST_SERVER, TEST_PORT, IPStack::TCP)) { |
AxedaCorp | 0:65004368569c | 193 | * printf("Success!\r\n"); |
AxedaCorp | 0:65004368569c | 194 | * } else { |
AxedaCorp | 0:65004368569c | 195 | * printf("Error during TCP socket open [%s:%d]\r\n", TEST_SERVER.c_str(), TEST_PORT); |
AxedaCorp | 0:65004368569c | 196 | * } |
AxedaCorp | 0:65004368569c | 197 | * |
AxedaCorp | 0:65004368569c | 198 | * char data[] = "My Test Echo Message"; |
AxedaCorp | 0:65004368569c | 199 | * int size = sizeof(data); |
AxedaCorp | 0:65004368569c | 200 | * printf("WRITE: [%d] [%s]\r\n", size, data); |
AxedaCorp | 0:65004368569c | 201 | * int bytesWritten = cellular->write(data, size, 10000); |
AxedaCorp | 0:65004368569c | 202 | * if(bytesWritten == size) { |
AxedaCorp | 0:65004368569c | 203 | * printf("Successfully wrote message!\r\n"); |
AxedaCorp | 0:65004368569c | 204 | * } else { |
AxedaCorp | 0:65004368569c | 205 | * printf("Failed to write message!\r\n"); |
AxedaCorp | 0:65004368569c | 206 | * } |
AxedaCorp | 0:65004368569c | 207 | * |
AxedaCorp | 0:65004368569c | 208 | * printf("Waiting 5 seconds\r\n"); |
AxedaCorp | 0:65004368569c | 209 | * wait(5); |
AxedaCorp | 0:65004368569c | 210 | * |
AxedaCorp | 0:65004368569c | 211 | * printf("Reading from socket for 10 seconds\r\n"); |
AxedaCorp | 0:65004368569c | 212 | * char response[size]; |
AxedaCorp | 0:65004368569c | 213 | * int bytesRead = cellular->read(response, size, 10000); |
AxedaCorp | 0:65004368569c | 214 | * response[size - 1] = '\0'; |
AxedaCorp | 0:65004368569c | 215 | * printf("READ: [%d] [%s]\r\n", bytesRead, response); |
AxedaCorp | 0:65004368569c | 216 | * |
AxedaCorp | 0:65004368569c | 217 | * //Check to see if echo was successful |
AxedaCorp | 0:65004368569c | 218 | * if (strcmp(data, response) == 0) { |
AxedaCorp | 0:65004368569c | 219 | * printf("Echo Successful!\n\r"); |
AxedaCorp | 0:65004368569c | 220 | * } else { |
AxedaCorp | 0:65004368569c | 221 | * printf("Echo failed!\n\r"); |
AxedaCorp | 0:65004368569c | 222 | * } |
AxedaCorp | 0:65004368569c | 223 | * |
AxedaCorp | 0:65004368569c | 224 | * //Cleaning up the connection |
AxedaCorp | 0:65004368569c | 225 | * printf("Closing socket: %s\r\n", cellular->close() ? "Success" : "Failure"); |
AxedaCorp | 0:65004368569c | 226 | * printf("Disconnecting...\r\n"); |
AxedaCorp | 0:65004368569c | 227 | * cellular->disconnect(); |
AxedaCorp | 0:65004368569c | 228 | * printf("End Program\n\r"); |
AxedaCorp | 0:65004368569c | 229 | * } |
AxedaCorp | 0:65004368569c | 230 | * @endcode |
AxedaCorp | 0:65004368569c | 231 | */ |
AxedaCorp | 0:65004368569c | 232 | |
AxedaCorp | 0:65004368569c | 233 | class Cellular : virtual mts::IPStack |
AxedaCorp | 0:65004368569c | 234 | { |
AxedaCorp | 0:65004368569c | 235 | public: |
AxedaCorp | 0:65004368569c | 236 | |
AxedaCorp | 0:65004368569c | 237 | /// An enumeration of radio registration states with a cell tower. |
AxedaCorp | 0:65004368569c | 238 | enum Registration { |
AxedaCorp | 0:65004368569c | 239 | NOT_REGISTERED, REGISTERED, SEARCHING, DENIED, UNKNOWN, ROAMING |
AxedaCorp | 0:65004368569c | 240 | }; |
AxedaCorp | 0:65004368569c | 241 | |
AxedaCorp | 0:65004368569c | 242 | /** This structure contains the data for an SMS message. |
AxedaCorp | 0:65004368569c | 243 | */ |
AxedaCorp | 0:65004368569c | 244 | struct Sms { |
AxedaCorp | 0:65004368569c | 245 | /// Message Phone Number |
AxedaCorp | 0:65004368569c | 246 | std::string phoneNumber; |
AxedaCorp | 0:65004368569c | 247 | /// Message Body |
AxedaCorp | 0:65004368569c | 248 | std::string message; |
AxedaCorp | 0:65004368569c | 249 | /// Message Timestamp |
AxedaCorp | 0:65004368569c | 250 | std::string timestamp; |
AxedaCorp | 0:65004368569c | 251 | }; |
AxedaCorp | 0:65004368569c | 252 | |
AxedaCorp | 0:65004368569c | 253 | /** Destructs a Cellular object and frees all related resources. |
AxedaCorp | 0:65004368569c | 254 | */ |
AxedaCorp | 0:65004368569c | 255 | ~Cellular(); |
AxedaCorp | 0:65004368569c | 256 | |
AxedaCorp | 0:65004368569c | 257 | /** This static function is used to create or get a reference to a |
AxedaCorp | 0:65004368569c | 258 | * Cellular object. Cellular uses the singleton pattern, which means |
AxedaCorp | 0:65004368569c | 259 | * that you can only have one existing at a time. The first time you |
AxedaCorp | 0:65004368569c | 260 | * call getInstance this method creates a new uninitialized Cellular |
AxedaCorp | 0:65004368569c | 261 | * object and returns it. All future calls to this method will return |
AxedaCorp | 0:65004368569c | 262 | * a reference to the instance created during the first call. Note that |
AxedaCorp | 0:65004368569c | 263 | * you must call init on the returned instance before mnaking any other |
AxedaCorp | 0:65004368569c | 264 | * calls. If using this class's bindings to any of the Socket package |
AxedaCorp | 0:65004368569c | 265 | * classes like TCPSocketConnection, you must call this method and the |
AxedaCorp | 0:65004368569c | 266 | * init method on the returned object first, before even creating the |
AxedaCorp | 0:65004368569c | 267 | * other objects. |
AxedaCorp | 0:65004368569c | 268 | * |
AxedaCorp | 0:65004368569c | 269 | * @returns a reference to the single Cellular obect that has been created. |
AxedaCorp | 0:65004368569c | 270 | */ |
AxedaCorp | 0:65004368569c | 271 | static Cellular* getInstance(); |
AxedaCorp | 0:65004368569c | 272 | |
AxedaCorp | 0:65004368569c | 273 | /** This method initializes the object with the underlying radio |
AxedaCorp | 0:65004368569c | 274 | * interface to use. Note that this function MUST be called before |
AxedaCorp | 0:65004368569c | 275 | * any other calls will function correctly on a Cellular object. Also |
AxedaCorp | 0:65004368569c | 276 | * note that MTSBufferedIO is abstract, so you must use one of |
AxedaCorp | 0:65004368569c | 277 | * its inherited classes like MTSSerial or MTSSerialFlowControl. |
AxedaCorp | 0:65004368569c | 278 | * |
AxedaCorp | 0:65004368569c | 279 | * @param io the buffered io interface that is attached to the cellular |
AxedaCorp | 0:65004368569c | 280 | * radio. |
AxedaCorp | 0:65004368569c | 281 | * @param DCD this is the dcd signal from the radio. If attached the |
AxedaCorp | 0:65004368569c | 282 | * the pin must be passed in here for this class to operate correctly. |
AxedaCorp | 0:65004368569c | 283 | * The default is not connected. |
AxedaCorp | 0:65004368569c | 284 | * @param DTR this is the dtr signal from the radio. If attached the |
AxedaCorp | 0:65004368569c | 285 | * the pin must be passed in here for this class to operate correctly. |
AxedaCorp | 0:65004368569c | 286 | * The default is not connected. |
AxedaCorp | 0:65004368569c | 287 | * @returns true if the init was successful, otherwise false. |
AxedaCorp | 0:65004368569c | 288 | */ |
AxedaCorp | 0:65004368569c | 289 | bool init(MTSBufferedIO* io, PinName DCD = NC, PinName DTR = NC); |
AxedaCorp | 0:65004368569c | 290 | |
AxedaCorp | 0:65004368569c | 291 | // Radio link related commands |
AxedaCorp | 0:65004368569c | 292 | /** This method establishes a data connection on the cellular radio. |
AxedaCorp | 0:65004368569c | 293 | * Note that before calling you must have an activated radio and if |
AxedaCorp | 0:65004368569c | 294 | * using a SIM card set the APN using the setApn method. The APN can |
AxedaCorp | 0:65004368569c | 295 | * be obtained from your cellular service provider. |
AxedaCorp | 0:65004368569c | 296 | * |
AxedaCorp | 0:65004368569c | 297 | * @returns true if the connection was successfully established, otherwise |
AxedaCorp | 0:65004368569c | 298 | * false on an error. |
AxedaCorp | 0:65004368569c | 299 | */ |
AxedaCorp | 0:65004368569c | 300 | virtual bool connect(); |
AxedaCorp | 0:65004368569c | 301 | |
AxedaCorp | 0:65004368569c | 302 | /** This method is used to stop a previously established cellular data connection. |
AxedaCorp | 0:65004368569c | 303 | */ |
AxedaCorp | 0:65004368569c | 304 | virtual void disconnect(); |
AxedaCorp | 0:65004368569c | 305 | |
AxedaCorp | 0:65004368569c | 306 | /** This method is used to check if the radio currently has a data connection |
AxedaCorp | 0:65004368569c | 307 | * established. |
AxedaCorp | 0:65004368569c | 308 | * |
AxedaCorp | 0:65004368569c | 309 | * @returns true if a data connection exists, otherwise false. |
AxedaCorp | 0:65004368569c | 310 | */ |
AxedaCorp | 0:65004368569c | 311 | virtual bool isConnected(); |
AxedaCorp | 0:65004368569c | 312 | |
AxedaCorp | 0:65004368569c | 313 | // TCP and UDP Socket related commands |
AxedaCorp | 0:65004368569c | 314 | // For behavior of the following methods refer to IPStack.h documentation |
AxedaCorp | 0:65004368569c | 315 | virtual bool bind(unsigned int port); |
AxedaCorp | 0:65004368569c | 316 | virtual bool open(const std::string& address, unsigned int port, Mode mode); |
AxedaCorp | 0:65004368569c | 317 | virtual bool isOpen(); |
AxedaCorp | 0:65004368569c | 318 | virtual bool close(); |
AxedaCorp | 0:65004368569c | 319 | virtual int read(char* data, int max, int timeout = -1); |
AxedaCorp | 0:65004368569c | 320 | virtual int write(const char* data, int length, int timeout = -1); |
AxedaCorp | 0:65004368569c | 321 | virtual unsigned int readable(); |
AxedaCorp | 0:65004368569c | 322 | virtual unsigned int writeable(); |
AxedaCorp | 0:65004368569c | 323 | |
AxedaCorp | 0:65004368569c | 324 | //Other |
AxedaCorp | 0:65004368569c | 325 | /** A method to reset the Multi-Tech Socket Modem. This command brings down the |
AxedaCorp | 0:65004368569c | 326 | * PPP link if it is up. After this function is called, at least 30 seconds should |
AxedaCorp | 0:65004368569c | 327 | * be allowed for the cellular radio to come back up before any other Cellular |
AxedaCorp | 0:65004368569c | 328 | * functions are called. |
AxedaCorp | 0:65004368569c | 329 | */ |
AxedaCorp | 0:65004368569c | 330 | virtual void reset(); |
AxedaCorp | 0:65004368569c | 331 | |
AxedaCorp | 0:65004368569c | 332 | //Cellular Radio Specific |
AxedaCorp | 0:65004368569c | 333 | /** A method for sending a generic AT command to the radio. Note that you cannot |
AxedaCorp | 0:65004368569c | 334 | * send commands and have a data connection at the same time. |
AxedaCorp | 0:65004368569c | 335 | * |
AxedaCorp | 0:65004368569c | 336 | * @param command the command to send to the radio without the escape character. |
AxedaCorp | 0:65004368569c | 337 | * @param timeoutMillis the time in millis to wait for a response before returning. |
AxedaCorp | 0:65004368569c | 338 | * @param esc escape character to add at the end of the command, defaults to |
AxedaCorp | 0:65004368569c | 339 | * carriage return (CR). Does not append any character if esc == 0. |
AxedaCorp | 0:65004368569c | 340 | * @returns all data received from the radio after the command as a string. |
AxedaCorp | 0:65004368569c | 341 | */ |
AxedaCorp | 0:65004368569c | 342 | std::string sendCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); |
AxedaCorp | 0:65004368569c | 343 | |
AxedaCorp | 0:65004368569c | 344 | /** A method for sending a basic AT command to the radio. A basic AT command is |
AxedaCorp | 0:65004368569c | 345 | * one that simply has a response of either OK or ERROR without any other information. |
AxedaCorp | 0:65004368569c | 346 | * Note that you cannot send commands and have a data connection at the same time. |
AxedaCorp | 0:65004368569c | 347 | * |
AxedaCorp | 0:65004368569c | 348 | * @param command the command to send to the radio without the escape character. |
AxedaCorp | 0:65004368569c | 349 | * @param timeoutMillis the time in millis to wait for a response before returning. |
AxedaCorp | 0:65004368569c | 350 | * @param esc escape character to add at the end of the command, defaults to |
AxedaCorp | 0:65004368569c | 351 | * carriage return (CR). |
AxedaCorp | 0:65004368569c | 352 | * @returns the standard Code enumeration. |
AxedaCorp | 0:65004368569c | 353 | */ |
AxedaCorp | 0:65004368569c | 354 | Code sendBasicCommand(const std::string& command, unsigned int timeoutMillis, char esc = CR); |
AxedaCorp | 0:65004368569c | 355 | |
AxedaCorp | 0:65004368569c | 356 | /** This method is used to get the IP address of the device, which is determined |
AxedaCorp | 0:65004368569c | 357 | * via DHCP by the cellular carrier. |
AxedaCorp | 0:65004368569c | 358 | * |
AxedaCorp | 0:65004368569c | 359 | * @returns the devices IP address. |
AxedaCorp | 0:65004368569c | 360 | */ |
AxedaCorp | 0:65004368569c | 361 | std::string getDeviceIP(); |
AxedaCorp | 0:65004368569c | 362 | |
AxedaCorp | 0:65004368569c | 363 | /** A method for testing command access to the radio. This method sends the |
AxedaCorp | 0:65004368569c | 364 | * command "AT" to the radio, which is a standard radio test to see if you |
AxedaCorp | 0:65004368569c | 365 | * have command access to the radio. The function returns when it receives |
AxedaCorp | 0:65004368569c | 366 | * the expected response from the radio. |
AxedaCorp | 0:65004368569c | 367 | * |
AxedaCorp | 0:65004368569c | 368 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 369 | */ |
AxedaCorp | 0:65004368569c | 370 | Code test(); |
AxedaCorp | 0:65004368569c | 371 | |
AxedaCorp | 0:65004368569c | 372 | /** A method for configuring command ehco capability on the radio. This command |
AxedaCorp | 0:65004368569c | 373 | * sets whether sent characters are echoed back from the radio, in which case you |
AxedaCorp | 0:65004368569c | 374 | * will receive back every command you send. |
AxedaCorp | 0:65004368569c | 375 | * |
AxedaCorp | 0:65004368569c | 376 | * @param state if true echo will be turned off, otherwise it will be turned on. |
AxedaCorp | 0:65004368569c | 377 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 378 | */ |
AxedaCorp | 0:65004368569c | 379 | Code echo(bool state); |
AxedaCorp | 0:65004368569c | 380 | |
AxedaCorp | 0:65004368569c | 381 | /** A method for getting the signal strength of the radio. This method allows you to |
AxedaCorp | 0:65004368569c | 382 | * get a value that maps to signal strength in dBm. Here 0-1 is Poor, 2-9 is Marginal, |
AxedaCorp | 0:65004368569c | 383 | * 10-14 is Ok, 15-19 is Good, and 20+ is Excellent. If you get a result of 99 the |
AxedaCorp | 0:65004368569c | 384 | * signal strength is not known or not detectable. |
AxedaCorp | 0:65004368569c | 385 | * |
AxedaCorp | 0:65004368569c | 386 | * @returns an integer representing the signal strength. |
AxedaCorp | 0:65004368569c | 387 | */ |
AxedaCorp | 0:65004368569c | 388 | int getSignalStrength(); |
AxedaCorp | 0:65004368569c | 389 | |
AxedaCorp | 0:65004368569c | 390 | /** This method is used to check the registration state of the radio with the cell tower. |
AxedaCorp | 0:65004368569c | 391 | * If not appropriatley registered with the tower you cannot make a cellular connection. |
AxedaCorp | 0:65004368569c | 392 | * |
AxedaCorp | 0:65004368569c | 393 | * @returns the registration state as an enumeration type. |
AxedaCorp | 0:65004368569c | 394 | */ |
AxedaCorp | 0:65004368569c | 395 | Registration getRegistration(); |
AxedaCorp | 0:65004368569c | 396 | |
AxedaCorp | 0:65004368569c | 397 | /** This method is used to set the radios APN if using a SIM card. Note that the APN |
AxedaCorp | 0:65004368569c | 398 | * must be set correctly before you can make a data connection. The APN for your SIM |
AxedaCorp | 0:65004368569c | 399 | * can be obtained by contacting your cellular service provider. |
AxedaCorp | 0:65004368569c | 400 | * |
AxedaCorp | 0:65004368569c | 401 | * @param the APN as a string. |
AxedaCorp | 0:65004368569c | 402 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 403 | */ |
AxedaCorp | 0:65004368569c | 404 | Code setApn(const std::string& apn); |
AxedaCorp | 0:65004368569c | 405 | |
AxedaCorp | 0:65004368569c | 406 | /** This method is used to set the DNS which enables the use of URLs instead |
AxedaCorp | 0:65004368569c | 407 | * of IP addresses when making a socket connection. |
AxedaCorp | 0:65004368569c | 408 | * |
AxedaCorp | 0:65004368569c | 409 | * @param the DNS server address as a string in form xxx.xxx.xxx.xxx. |
AxedaCorp | 0:65004368569c | 410 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 411 | */ |
AxedaCorp | 0:65004368569c | 412 | Code setDns(const std::string& primary, const std::string& secondary = "0.0.0.0"); |
AxedaCorp | 0:65004368569c | 413 | |
AxedaCorp | 0:65004368569c | 414 | /** This method is used test network connectivity by pinging a server. |
AxedaCorp | 0:65004368569c | 415 | * |
AxedaCorp | 0:65004368569c | 416 | * @param address the address of the server in format xxx.xxx.xxx.xxx. |
AxedaCorp | 0:65004368569c | 417 | * @returns true if the ping was successful, otherwise false. |
AxedaCorp | 0:65004368569c | 418 | */ |
AxedaCorp | 0:65004368569c | 419 | bool ping(const std::string& address = "8.8.8.8"); |
AxedaCorp | 0:65004368569c | 420 | |
AxedaCorp | 0:65004368569c | 421 | /** This method can be used to trade socket functionality for performance. |
AxedaCorp | 0:65004368569c | 422 | * In order to enable a socket connection to be closed by the client side programtically, |
AxedaCorp | 0:65004368569c | 423 | * this class must process all read and write data on the socket to guard the special |
AxedaCorp | 0:65004368569c | 424 | * escape character used to close an open socket connection. It is recommened that you |
AxedaCorp | 0:65004368569c | 425 | * use the default of true unless the overhead of these operations is too significant. |
AxedaCorp | 0:65004368569c | 426 | * |
AxedaCorp | 0:65004368569c | 427 | * @param enabled set to true if you want the socket closeable, otherwise false. The default |
AxedaCorp | 0:65004368569c | 428 | * is true. |
AxedaCorp | 0:65004368569c | 429 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 430 | */ |
AxedaCorp | 0:65004368569c | 431 | Code setSocketCloseable(bool enabled = true); //ETX closes socket (ETX and DLE in payload are escaped with DLE) |
AxedaCorp | 0:65004368569c | 432 | |
AxedaCorp | 0:65004368569c | 433 | /** This method is used to send an SMS message. Note that you cannot send an |
AxedaCorp | 0:65004368569c | 434 | * SMS message and have a data connection open at the same time. |
AxedaCorp | 0:65004368569c | 435 | * |
AxedaCorp | 0:65004368569c | 436 | * @param phoneNumber the phone number to send the message to as a string. |
AxedaCorp | 0:65004368569c | 437 | * @param message the text message to be sent. |
AxedaCorp | 0:65004368569c | 438 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 439 | */ |
AxedaCorp | 0:65004368569c | 440 | Code sendSMS(const std::string& phoneNumber, const std::string& message); |
AxedaCorp | 0:65004368569c | 441 | |
AxedaCorp | 0:65004368569c | 442 | /** This method is used to send an SMS message. Note that you cannot send an |
AxedaCorp | 0:65004368569c | 443 | * SMS message and have a data connection open at the same time. |
AxedaCorp | 0:65004368569c | 444 | * |
AxedaCorp | 0:65004368569c | 445 | * @param sms an Sms struct that contains all SMS transaction information. |
AxedaCorp | 0:65004368569c | 446 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 447 | */ |
AxedaCorp | 0:65004368569c | 448 | Code sendSMS(const Sms& sms); |
AxedaCorp | 0:65004368569c | 449 | |
AxedaCorp | 0:65004368569c | 450 | /** This method retrieves all of the SMS messages currently available for |
AxedaCorp | 0:65004368569c | 451 | * this phone number. |
AxedaCorp | 0:65004368569c | 452 | * |
AxedaCorp | 0:65004368569c | 453 | * @returns a vector of existing SMS messages each as an Sms struct. |
AxedaCorp | 0:65004368569c | 454 | */ |
AxedaCorp | 0:65004368569c | 455 | std::vector<Cellular::Sms> getReceivedSms(); |
AxedaCorp | 0:65004368569c | 456 | |
AxedaCorp | 0:65004368569c | 457 | /** This method can be used to remove/delete all received SMS messages |
AxedaCorp | 0:65004368569c | 458 | * even if they have never been retrieved or read. |
AxedaCorp | 0:65004368569c | 459 | * |
AxedaCorp | 0:65004368569c | 460 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 461 | */ |
AxedaCorp | 0:65004368569c | 462 | Code deleteAllReceivedSms(); |
AxedaCorp | 0:65004368569c | 463 | |
AxedaCorp | 0:65004368569c | 464 | /** This method can be used to remove/delete all received SMS messages |
AxedaCorp | 0:65004368569c | 465 | * that have been retrieved by the user through the getReceivedSms method. |
AxedaCorp | 0:65004368569c | 466 | * Messages that have not been retrieved yet will be unaffected. |
AxedaCorp | 0:65004368569c | 467 | * |
AxedaCorp | 0:65004368569c | 468 | * @returns the standard AT Code enumeration. |
AxedaCorp | 0:65004368569c | 469 | */ |
AxedaCorp | 0:65004368569c | 470 | Code deleteOnlyReceivedReadSms(); |
AxedaCorp | 0:65004368569c | 471 | |
AxedaCorp | 0:65004368569c | 472 | /** A static method for getting a string representation for the Registration |
AxedaCorp | 0:65004368569c | 473 | * enumeration. |
AxedaCorp | 0:65004368569c | 474 | * |
AxedaCorp | 0:65004368569c | 475 | * @param code a Registration enumeration. |
AxedaCorp | 0:65004368569c | 476 | * @returns the enumeration name as a string. |
AxedaCorp | 0:65004368569c | 477 | */ |
AxedaCorp | 0:65004368569c | 478 | static std::string getRegistrationNames(Registration registration); |
AxedaCorp | 0:65004368569c | 479 | |
AxedaCorp | 0:65004368569c | 480 | private: |
AxedaCorp | 0:65004368569c | 481 | static Cellular* instance; //Static pointer to the single Cellular object. |
AxedaCorp | 0:65004368569c | 482 | |
AxedaCorp | 0:65004368569c | 483 | MTSBufferedIO* io; //IO interface obect that the radio is accessed through. |
AxedaCorp | 0:65004368569c | 484 | bool echoMode; //Specifies if the echo mode is currently enabled. |
AxedaCorp | 0:65004368569c | 485 | |
AxedaCorp | 0:65004368569c | 486 | bool pppConnected; //Specifies if a PPP session is currently connected. |
AxedaCorp | 0:65004368569c | 487 | std::string apn; //A string that holds the APN for the radio. |
AxedaCorp | 0:65004368569c | 488 | |
AxedaCorp | 0:65004368569c | 489 | Mode mode; //The current socket Mode. |
AxedaCorp | 0:65004368569c | 490 | bool socketOpened; //Specifies if a Socket is presently opened. |
AxedaCorp | 0:65004368569c | 491 | bool socketCloseable; //Specifies is a Socket can be closed. |
AxedaCorp | 0:65004368569c | 492 | unsigned int local_port; //Holds the local port for socket connections. |
AxedaCorp | 0:65004368569c | 493 | std::string local_address; //Holds the local address for socket connections. |
AxedaCorp | 0:65004368569c | 494 | unsigned int host_port; //Holds the remote port for socket connections. |
AxedaCorp | 0:65004368569c | 495 | std::string host_address; //Holds the remote address for socket connections. |
AxedaCorp | 0:65004368569c | 496 | DigitalIn* dcd; //Maps to the radios dcd signal |
AxedaCorp | 0:65004368569c | 497 | DigitalOut* dtr; //Maps to the radios dtr signal |
AxedaCorp | 0:65004368569c | 498 | |
AxedaCorp | 0:65004368569c | 499 | Cellular(); //Private constructor, use the getInstance() method. |
AxedaCorp | 0:65004368569c | 500 | Cellular(MTSBufferedIO* io); //Private constructor, use the getInstance() method. |
AxedaCorp | 0:65004368569c | 501 | }; |
AxedaCorp | 0:65004368569c | 502 | |
AxedaCorp | 0:65004368569c | 503 | } |
AxedaCorp | 0:65004368569c | 504 | |
AxedaCorp | 0:65004368569c | 505 | #endif /* CELLULAR_H */ |