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