Simple test for USSD message.
Dependencies: CellularUSBModem
Fork of UbloxUSBModem by
UbloxModem.cpp@9:67b85d905628, 2014-02-24 (annotated)
- Committer:
- nherriot
- Date:
- Mon Feb 24 15:22:48 2014 +0000
- Revision:
- 9:67b85d905628
- Parent:
- 6:5a1583d0e6cd
Simple test for USSD message.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 5:60b48a013e86 | 1 | /* UbloxModem.cpp */ |
mbed_official | 5:60b48a013e86 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
mbed_official | 5:60b48a013e86 | 3 | * |
mbed_official | 5:60b48a013e86 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 5:60b48a013e86 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
mbed_official | 5:60b48a013e86 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
mbed_official | 5:60b48a013e86 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
mbed_official | 5:60b48a013e86 | 8 | * furnished to do so, subject to the following conditions: |
mbed_official | 5:60b48a013e86 | 9 | * |
mbed_official | 5:60b48a013e86 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 5:60b48a013e86 | 11 | * substantial portions of the Software. |
mbed_official | 5:60b48a013e86 | 12 | * |
mbed_official | 5:60b48a013e86 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 5:60b48a013e86 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 5:60b48a013e86 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 5:60b48a013e86 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 5:60b48a013e86 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 5:60b48a013e86 | 18 | */ |
mbed_official | 5:60b48a013e86 | 19 | |
mbed_official | 5:60b48a013e86 | 20 | #define __DEBUG__ 3 |
mbed_official | 5:60b48a013e86 | 21 | #ifndef __MODULE__ |
mbed_official | 5:60b48a013e86 | 22 | #define __MODULE__ "UbloxModem.cpp" |
mbed_official | 5:60b48a013e86 | 23 | #endif |
mbed_official | 5:60b48a013e86 | 24 | |
mbed_official | 5:60b48a013e86 | 25 | #include "core/fwk.h" |
mbed_official | 5:60b48a013e86 | 26 | #include "sms/GSMSMSInterface.h" |
mbed_official | 5:60b48a013e86 | 27 | #include "sms/CDMASMSInterface.h" |
mbed_official | 5:60b48a013e86 | 28 | |
mbed_official | 5:60b48a013e86 | 29 | #include "UbloxModem.h" |
mbed_official | 5:60b48a013e86 | 30 | |
mbed_official | 5:60b48a013e86 | 31 | UbloxModem::UbloxModem(IOStream* atStream, IOStream* pppStream) : |
mbed_official | 5:60b48a013e86 | 32 | m_at(atStream), // Construct ATCommandsInterface with the AT serial channel |
mbed_official | 5:60b48a013e86 | 33 | m_CdmaSms(&m_at), // Construct SMSInterface with the ATCommandsInterface |
mbed_official | 5:60b48a013e86 | 34 | m_GsmSms(&m_at), // Construct SMSInterface with the ATCommandsInterface |
mbed_official | 5:60b48a013e86 | 35 | m_ussd(&m_at), // Construct USSDInterface with the ATCommandsInterface |
mbed_official | 5:60b48a013e86 | 36 | m_linkMonitor(&m_at), // Construct LinkMonitor with the ATCommandsInterface |
mbed_official | 5:60b48a013e86 | 37 | m_ppp(pppStream ? pppStream : atStream), // Construct PPPIPInterface with the PPP serial channel |
mbed_official | 5:60b48a013e86 | 38 | m_ipInit(false), // PPIPInterface connection is initially down |
mbed_official | 5:60b48a013e86 | 39 | m_smsInit(false), // SMSInterface starts un-initialised |
mbed_official | 5:60b48a013e86 | 40 | m_ussdInit(false), // USSDInterface starts un-initialised |
mbed_official | 5:60b48a013e86 | 41 | m_linkMonitorInit(false), // LinkMonitor subsystem starts un-initialised |
mbed_official | 5:60b48a013e86 | 42 | m_atOpen(false), // ATCommandsInterface starts in a closed state |
mbed_official | 5:60b48a013e86 | 43 | m_onePort(pppStream == NULL), |
mbed_official | 5:60b48a013e86 | 44 | m_gsm(true) |
mbed_official | 5:60b48a013e86 | 45 | { |
mbed_official | 5:60b48a013e86 | 46 | } |
mbed_official | 5:60b48a013e86 | 47 | |
mbed_official | 6:5a1583d0e6cd | 48 | |
mbed_official | 6:5a1583d0e6cd | 49 | class AtiProcessor : public IATCommandsProcessor |
mbed_official | 6:5a1583d0e6cd | 50 | { |
mbed_official | 6:5a1583d0e6cd | 51 | public: |
mbed_official | 6:5a1583d0e6cd | 52 | AtiProcessor() |
mbed_official | 6:5a1583d0e6cd | 53 | { |
mbed_official | 6:5a1583d0e6cd | 54 | i = 0; |
mbed_official | 6:5a1583d0e6cd | 55 | str[0] = '\0'; |
mbed_official | 6:5a1583d0e6cd | 56 | } |
mbed_official | 6:5a1583d0e6cd | 57 | const char* getInfo(void) { return str; } |
mbed_official | 6:5a1583d0e6cd | 58 | private: |
mbed_official | 6:5a1583d0e6cd | 59 | virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
mbed_official | 6:5a1583d0e6cd | 60 | { |
mbed_official | 6:5a1583d0e6cd | 61 | int l = strlen(line); |
mbed_official | 6:5a1583d0e6cd | 62 | if (i + l + 2 > sizeof(str)) |
mbed_official | 6:5a1583d0e6cd | 63 | return NET_OVERFLOW; |
mbed_official | 6:5a1583d0e6cd | 64 | if (i) str[i++] = ','; |
mbed_official | 6:5a1583d0e6cd | 65 | strcat(&str[i], line); |
mbed_official | 6:5a1583d0e6cd | 66 | i += l; |
mbed_official | 6:5a1583d0e6cd | 67 | return OK; |
mbed_official | 6:5a1583d0e6cd | 68 | } |
mbed_official | 6:5a1583d0e6cd | 69 | virtual int onNewEntryPrompt(ATCommandsInterface* pInst) |
mbed_official | 6:5a1583d0e6cd | 70 | { |
mbed_official | 6:5a1583d0e6cd | 71 | return OK; |
mbed_official | 6:5a1583d0e6cd | 72 | } |
mbed_official | 6:5a1583d0e6cd | 73 | protected: |
mbed_official | 6:5a1583d0e6cd | 74 | char str[256]; |
mbed_official | 6:5a1583d0e6cd | 75 | int i; |
mbed_official | 6:5a1583d0e6cd | 76 | }; |
mbed_official | 6:5a1583d0e6cd | 77 | |
mbed_official | 5:60b48a013e86 | 78 | class CREGProcessor : public IATCommandsProcessor |
mbed_official | 5:60b48a013e86 | 79 | { |
mbed_official | 5:60b48a013e86 | 80 | public: |
mbed_official | 5:60b48a013e86 | 81 | CREGProcessor(bool gsm) : status(STATUS_REGISTERING) |
mbed_official | 5:60b48a013e86 | 82 | { |
mbed_official | 5:60b48a013e86 | 83 | m_gsm = gsm; |
mbed_official | 5:60b48a013e86 | 84 | } |
mbed_official | 5:60b48a013e86 | 85 | enum REGISTERING_STATUS { STATUS_REGISTERING, STATUS_OK, STATUS_FAILED }; |
mbed_official | 5:60b48a013e86 | 86 | REGISTERING_STATUS getStatus() |
mbed_official | 5:60b48a013e86 | 87 | { |
mbed_official | 5:60b48a013e86 | 88 | return status; |
mbed_official | 5:60b48a013e86 | 89 | } |
mbed_official | 5:60b48a013e86 | 90 | const char* getAtCommand() |
mbed_official | 5:60b48a013e86 | 91 | { |
mbed_official | 5:60b48a013e86 | 92 | return m_gsm ? "AT+CREG?" : "AT+CSS?"; |
mbed_official | 5:60b48a013e86 | 93 | } |
mbed_official | 5:60b48a013e86 | 94 | private: |
mbed_official | 5:60b48a013e86 | 95 | virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
mbed_official | 5:60b48a013e86 | 96 | { |
mbed_official | 5:60b48a013e86 | 97 | int r; |
mbed_official | 5:60b48a013e86 | 98 | if (m_gsm) |
mbed_official | 5:60b48a013e86 | 99 | { |
mbed_official | 5:60b48a013e86 | 100 | if( sscanf(line, "+CREG: %*d,%d", &r) == 1 ) |
mbed_official | 5:60b48a013e86 | 101 | { |
mbed_official | 5:60b48a013e86 | 102 | status = (r == 1 || r == 5) ? STATUS_OK : |
mbed_official | 5:60b48a013e86 | 103 | (r == 0 || r == 2) ? STATUS_REGISTERING : |
mbed_official | 5:60b48a013e86 | 104 | // (r == 3) ? STATUS_FAILED : |
mbed_official | 5:60b48a013e86 | 105 | STATUS_FAILED; |
mbed_official | 5:60b48a013e86 | 106 | } |
mbed_official | 5:60b48a013e86 | 107 | } |
mbed_official | 5:60b48a013e86 | 108 | else |
mbed_official | 5:60b48a013e86 | 109 | { |
mbed_official | 5:60b48a013e86 | 110 | char bc[3] = ""; |
mbed_official | 5:60b48a013e86 | 111 | if(sscanf(line, "%*s %*c,%2s,%*d",bc)==1) |
mbed_official | 5:60b48a013e86 | 112 | { |
mbed_official | 5:60b48a013e86 | 113 | status = (strcmp("Z", bc) == 0) ? STATUS_REGISTERING : STATUS_OK; |
mbed_official | 5:60b48a013e86 | 114 | } |
mbed_official | 5:60b48a013e86 | 115 | } |
mbed_official | 5:60b48a013e86 | 116 | return OK; |
mbed_official | 5:60b48a013e86 | 117 | } |
mbed_official | 5:60b48a013e86 | 118 | virtual int onNewEntryPrompt(ATCommandsInterface* pInst) |
mbed_official | 5:60b48a013e86 | 119 | { |
mbed_official | 5:60b48a013e86 | 120 | return OK; |
mbed_official | 5:60b48a013e86 | 121 | } |
mbed_official | 5:60b48a013e86 | 122 | volatile REGISTERING_STATUS status; |
mbed_official | 5:60b48a013e86 | 123 | bool m_gsm; |
mbed_official | 5:60b48a013e86 | 124 | }; |
mbed_official | 5:60b48a013e86 | 125 | |
mbed_official | 5:60b48a013e86 | 126 | int UbloxModem::connect(const char* apn, const char* user, const char* password) |
mbed_official | 5:60b48a013e86 | 127 | { |
mbed_official | 5:60b48a013e86 | 128 | if( !m_ipInit ) |
mbed_official | 5:60b48a013e86 | 129 | { |
mbed_official | 5:60b48a013e86 | 130 | m_ipInit = true; |
mbed_official | 5:60b48a013e86 | 131 | m_ppp.init(); |
mbed_official | 5:60b48a013e86 | 132 | } |
mbed_official | 5:60b48a013e86 | 133 | m_ppp.setup(user, password, m_gsm ? DEFAULT_MSISDN_GSM : DEFAULT_MSISDN_CDMA); |
mbed_official | 5:60b48a013e86 | 134 | |
mbed_official | 5:60b48a013e86 | 135 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 136 | if(ret) |
mbed_official | 5:60b48a013e86 | 137 | { |
mbed_official | 5:60b48a013e86 | 138 | return ret; |
mbed_official | 5:60b48a013e86 | 139 | } |
mbed_official | 5:60b48a013e86 | 140 | |
mbed_official | 5:60b48a013e86 | 141 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 142 | { |
mbed_official | 5:60b48a013e86 | 143 | m_smsInit = false; //SMS status reset |
mbed_official | 5:60b48a013e86 | 144 | m_ussdInit = false; //USSD status reset |
mbed_official | 5:60b48a013e86 | 145 | m_linkMonitorInit = false; //Link monitor status reset |
mbed_official | 5:60b48a013e86 | 146 | } |
mbed_official | 5:60b48a013e86 | 147 | |
mbed_official | 5:60b48a013e86 | 148 | ATCommandsInterface::ATResult result; |
mbed_official | 5:60b48a013e86 | 149 | |
mbed_official | 5:60b48a013e86 | 150 | if(apn != NULL) |
mbed_official | 5:60b48a013e86 | 151 | { |
mbed_official | 5:60b48a013e86 | 152 | char cmd[48]; |
mbed_official | 5:60b48a013e86 | 153 | int tries = 30; |
mbed_official | 5:60b48a013e86 | 154 | sprintf(cmd, "AT+CGDCONT=1,\"IP\",\"%s\"", apn); |
mbed_official | 5:60b48a013e86 | 155 | do //Try 30 times because for some reasons it can fail *a lot* with the K3772-Z dongle |
mbed_official | 5:60b48a013e86 | 156 | { |
mbed_official | 5:60b48a013e86 | 157 | ret = m_at.executeSimple(cmd, &result); |
mbed_official | 5:60b48a013e86 | 158 | DBG("Result of command: Err code=%d", ret); |
mbed_official | 5:60b48a013e86 | 159 | if(ret) |
mbed_official | 5:60b48a013e86 | 160 | { |
mbed_official | 5:60b48a013e86 | 161 | Thread::wait(500); |
mbed_official | 5:60b48a013e86 | 162 | } |
mbed_official | 5:60b48a013e86 | 163 | } while(ret && --tries); |
mbed_official | 5:60b48a013e86 | 164 | DBG("ATResult: AT return=%d (code %d)", result.result, result.code); |
mbed_official | 5:60b48a013e86 | 165 | DBG("APN set to %s", apn); |
mbed_official | 5:60b48a013e86 | 166 | } |
mbed_official | 5:60b48a013e86 | 167 | |
mbed_official | 5:60b48a013e86 | 168 | //Connect |
mbed_official | 5:60b48a013e86 | 169 | DBG("Connecting"); |
mbed_official | 5:60b48a013e86 | 170 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 171 | { |
mbed_official | 5:60b48a013e86 | 172 | m_at.close(); // Closing AT parser |
mbed_official | 5:60b48a013e86 | 173 | m_atOpen = false; //Will need to be reinitialized afterwards |
mbed_official | 5:60b48a013e86 | 174 | } |
mbed_official | 5:60b48a013e86 | 175 | |
mbed_official | 5:60b48a013e86 | 176 | DBG("Connecting PPP"); |
mbed_official | 5:60b48a013e86 | 177 | |
mbed_official | 5:60b48a013e86 | 178 | ret = m_ppp.connect(); |
mbed_official | 5:60b48a013e86 | 179 | DBG("Result of connect: Err code=%d", ret); |
mbed_official | 5:60b48a013e86 | 180 | return ret; |
mbed_official | 5:60b48a013e86 | 181 | } |
mbed_official | 5:60b48a013e86 | 182 | |
mbed_official | 5:60b48a013e86 | 183 | |
mbed_official | 5:60b48a013e86 | 184 | int UbloxModem::disconnect() |
mbed_official | 5:60b48a013e86 | 185 | { |
mbed_official | 5:60b48a013e86 | 186 | DBG("Disconnecting from PPP"); |
mbed_official | 5:60b48a013e86 | 187 | int ret = m_ppp.disconnect(); |
mbed_official | 5:60b48a013e86 | 188 | if(ret) |
mbed_official | 5:60b48a013e86 | 189 | { |
mbed_official | 5:60b48a013e86 | 190 | ERR("Disconnect returned %d, still trying to disconnect", ret); |
mbed_official | 5:60b48a013e86 | 191 | } |
mbed_official | 5:60b48a013e86 | 192 | |
mbed_official | 5:60b48a013e86 | 193 | //Ugly but leave dongle time to recover |
mbed_official | 5:60b48a013e86 | 194 | Thread::wait(500); |
mbed_official | 5:60b48a013e86 | 195 | |
mbed_official | 5:60b48a013e86 | 196 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 197 | { |
mbed_official | 5:60b48a013e86 | 198 | //ATCommandsInterface::ATResult result; |
mbed_official | 5:60b48a013e86 | 199 | DBG("Starting AT thread"); |
mbed_official | 5:60b48a013e86 | 200 | ret = m_at.open(); |
mbed_official | 5:60b48a013e86 | 201 | if(ret) |
mbed_official | 5:60b48a013e86 | 202 | { |
mbed_official | 5:60b48a013e86 | 203 | return ret; |
mbed_official | 5:60b48a013e86 | 204 | } |
mbed_official | 5:60b48a013e86 | 205 | } |
mbed_official | 5:60b48a013e86 | 206 | |
mbed_official | 5:60b48a013e86 | 207 | DBG("Trying to hangup"); |
mbed_official | 5:60b48a013e86 | 208 | |
mbed_official | 5:60b48a013e86 | 209 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 210 | { |
mbed_official | 5:60b48a013e86 | 211 | //Reinit AT parser |
mbed_official | 5:60b48a013e86 | 212 | ret = m_at.init(false); |
mbed_official | 5:60b48a013e86 | 213 | DBG("Result of command: Err code=%d\n", ret); |
mbed_official | 5:60b48a013e86 | 214 | if(ret) |
mbed_official | 5:60b48a013e86 | 215 | { |
mbed_official | 5:60b48a013e86 | 216 | m_at.close(); // Closing AT parser |
mbed_official | 5:60b48a013e86 | 217 | DBG("AT Parser closed, could not complete disconnection"); |
mbed_official | 5:60b48a013e86 | 218 | return NET_TIMEOUT; |
mbed_official | 5:60b48a013e86 | 219 | } |
mbed_official | 5:60b48a013e86 | 220 | |
mbed_official | 5:60b48a013e86 | 221 | } |
mbed_official | 5:60b48a013e86 | 222 | return OK; |
mbed_official | 5:60b48a013e86 | 223 | } |
mbed_official | 5:60b48a013e86 | 224 | |
mbed_official | 5:60b48a013e86 | 225 | int UbloxModem::sendSM(const char* number, const char* message) |
mbed_official | 5:60b48a013e86 | 226 | { |
mbed_official | 5:60b48a013e86 | 227 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 228 | if(ret) |
mbed_official | 5:60b48a013e86 | 229 | { |
mbed_official | 5:60b48a013e86 | 230 | return ret; |
mbed_official | 5:60b48a013e86 | 231 | } |
mbed_official | 5:60b48a013e86 | 232 | |
mbed_official | 5:60b48a013e86 | 233 | ISMSInterface* sms; |
mbed_official | 5:60b48a013e86 | 234 | if (m_gsm) sms = &m_GsmSms; |
mbed_official | 5:60b48a013e86 | 235 | else sms = &m_CdmaSms; |
mbed_official | 5:60b48a013e86 | 236 | if(!m_smsInit) |
mbed_official | 5:60b48a013e86 | 237 | { |
mbed_official | 5:60b48a013e86 | 238 | ret = sms->init(); |
mbed_official | 5:60b48a013e86 | 239 | if(ret) |
mbed_official | 5:60b48a013e86 | 240 | { |
mbed_official | 5:60b48a013e86 | 241 | return ret; |
mbed_official | 5:60b48a013e86 | 242 | } |
mbed_official | 5:60b48a013e86 | 243 | m_smsInit = true; |
mbed_official | 5:60b48a013e86 | 244 | } |
mbed_official | 5:60b48a013e86 | 245 | |
mbed_official | 5:60b48a013e86 | 246 | ret = sms->send(number, message); |
mbed_official | 5:60b48a013e86 | 247 | if(ret) |
mbed_official | 5:60b48a013e86 | 248 | { |
mbed_official | 5:60b48a013e86 | 249 | return ret; |
mbed_official | 5:60b48a013e86 | 250 | } |
mbed_official | 5:60b48a013e86 | 251 | |
mbed_official | 5:60b48a013e86 | 252 | return OK; |
mbed_official | 5:60b48a013e86 | 253 | } |
mbed_official | 5:60b48a013e86 | 254 | |
mbed_official | 5:60b48a013e86 | 255 | int UbloxModem::getSM(char* number, char* message, size_t maxLength) |
mbed_official | 5:60b48a013e86 | 256 | { |
mbed_official | 5:60b48a013e86 | 257 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 258 | if(ret) |
mbed_official | 5:60b48a013e86 | 259 | { |
mbed_official | 5:60b48a013e86 | 260 | return ret; |
mbed_official | 5:60b48a013e86 | 261 | } |
mbed_official | 5:60b48a013e86 | 262 | |
mbed_official | 5:60b48a013e86 | 263 | ISMSInterface* sms; |
mbed_official | 5:60b48a013e86 | 264 | if (m_gsm) sms = &m_GsmSms; |
mbed_official | 5:60b48a013e86 | 265 | else sms = &m_CdmaSms; |
mbed_official | 5:60b48a013e86 | 266 | if(!m_smsInit) |
mbed_official | 5:60b48a013e86 | 267 | { |
mbed_official | 5:60b48a013e86 | 268 | ret = sms->init(); |
mbed_official | 5:60b48a013e86 | 269 | if(ret) |
mbed_official | 5:60b48a013e86 | 270 | { |
mbed_official | 5:60b48a013e86 | 271 | return ret; |
mbed_official | 5:60b48a013e86 | 272 | } |
mbed_official | 5:60b48a013e86 | 273 | m_smsInit = true; |
mbed_official | 5:60b48a013e86 | 274 | } |
mbed_official | 5:60b48a013e86 | 275 | |
mbed_official | 5:60b48a013e86 | 276 | ret = sms->get(number, message, maxLength); |
mbed_official | 5:60b48a013e86 | 277 | if(ret) |
mbed_official | 5:60b48a013e86 | 278 | { |
mbed_official | 5:60b48a013e86 | 279 | return ret; |
mbed_official | 5:60b48a013e86 | 280 | } |
mbed_official | 5:60b48a013e86 | 281 | |
mbed_official | 5:60b48a013e86 | 282 | return OK; |
mbed_official | 5:60b48a013e86 | 283 | } |
mbed_official | 5:60b48a013e86 | 284 | |
mbed_official | 5:60b48a013e86 | 285 | int UbloxModem::getSMCount(size_t* pCount) |
mbed_official | 5:60b48a013e86 | 286 | { |
mbed_official | 5:60b48a013e86 | 287 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 288 | if(ret) |
mbed_official | 5:60b48a013e86 | 289 | { |
mbed_official | 5:60b48a013e86 | 290 | return ret; |
mbed_official | 5:60b48a013e86 | 291 | } |
mbed_official | 5:60b48a013e86 | 292 | |
mbed_official | 5:60b48a013e86 | 293 | ISMSInterface* sms; |
mbed_official | 5:60b48a013e86 | 294 | if (m_gsm) sms = &m_GsmSms; |
mbed_official | 5:60b48a013e86 | 295 | else sms = &m_CdmaSms; |
mbed_official | 5:60b48a013e86 | 296 | if(!m_smsInit) |
mbed_official | 5:60b48a013e86 | 297 | { |
mbed_official | 5:60b48a013e86 | 298 | ret = sms->init(); |
mbed_official | 5:60b48a013e86 | 299 | if(ret) |
mbed_official | 5:60b48a013e86 | 300 | { |
mbed_official | 5:60b48a013e86 | 301 | return ret; |
mbed_official | 5:60b48a013e86 | 302 | } |
mbed_official | 5:60b48a013e86 | 303 | m_smsInit = true; |
mbed_official | 5:60b48a013e86 | 304 | } |
mbed_official | 5:60b48a013e86 | 305 | |
mbed_official | 5:60b48a013e86 | 306 | ret = sms->getCount(pCount); |
mbed_official | 5:60b48a013e86 | 307 | if(ret) |
mbed_official | 5:60b48a013e86 | 308 | { |
mbed_official | 5:60b48a013e86 | 309 | return ret; |
mbed_official | 5:60b48a013e86 | 310 | } |
mbed_official | 5:60b48a013e86 | 311 | |
mbed_official | 5:60b48a013e86 | 312 | return OK; |
mbed_official | 5:60b48a013e86 | 313 | } |
mbed_official | 5:60b48a013e86 | 314 | |
mbed_official | 5:60b48a013e86 | 315 | ATCommandsInterface* UbloxModem::getATCommandsInterface() |
mbed_official | 5:60b48a013e86 | 316 | { |
mbed_official | 5:60b48a013e86 | 317 | return &m_at; |
mbed_official | 5:60b48a013e86 | 318 | } |
mbed_official | 5:60b48a013e86 | 319 | |
mbed_official | 5:60b48a013e86 | 320 | int UbloxModem::init() |
mbed_official | 5:60b48a013e86 | 321 | { |
mbed_official | 5:60b48a013e86 | 322 | if(m_atOpen) |
mbed_official | 5:60b48a013e86 | 323 | { |
mbed_official | 5:60b48a013e86 | 324 | return OK; |
mbed_official | 5:60b48a013e86 | 325 | } |
mbed_official | 5:60b48a013e86 | 326 | |
mbed_official | 5:60b48a013e86 | 327 | DBG("Starting AT thread if needed"); |
mbed_official | 5:60b48a013e86 | 328 | int ret = m_at.open(); |
mbed_official | 5:60b48a013e86 | 329 | if(ret) |
mbed_official | 5:60b48a013e86 | 330 | { |
mbed_official | 5:60b48a013e86 | 331 | return ret; |
mbed_official | 5:60b48a013e86 | 332 | } |
mbed_official | 5:60b48a013e86 | 333 | |
mbed_official | 5:60b48a013e86 | 334 | DBG("Sending initialisation commands"); |
mbed_official | 5:60b48a013e86 | 335 | ret = m_at.init(false); |
mbed_official | 5:60b48a013e86 | 336 | if(ret) |
mbed_official | 5:60b48a013e86 | 337 | { |
mbed_official | 5:60b48a013e86 | 338 | return ret; |
mbed_official | 5:60b48a013e86 | 339 | } |
mbed_official | 5:60b48a013e86 | 340 | |
mbed_official | 5:60b48a013e86 | 341 | ATCommandsInterface::ATResult result; |
mbed_official | 6:5a1583d0e6cd | 342 | AtiProcessor atiProcessor; |
mbed_official | 6:5a1583d0e6cd | 343 | do |
mbed_official | 6:5a1583d0e6cd | 344 | { |
mbed_official | 6:5a1583d0e6cd | 345 | ret = m_at.execute("ATI", &atiProcessor, &result); |
mbed_official | 6:5a1583d0e6cd | 346 | } |
mbed_official | 6:5a1583d0e6cd | 347 | while (ret != OK); |
mbed_official | 6:5a1583d0e6cd | 348 | { |
mbed_official | 6:5a1583d0e6cd | 349 | const char* info = atiProcessor.getInfo(); |
mbed_official | 6:5a1583d0e6cd | 350 | DBG("Modem Identification [%s]", info); |
mbed_official | 6:5a1583d0e6cd | 351 | if (strstr(info, "LISA-C200")) |
mbed_official | 6:5a1583d0e6cd | 352 | { |
mbed_official | 6:5a1583d0e6cd | 353 | m_gsm = false; // it is CDMA modem |
mbed_official | 6:5a1583d0e6cd | 354 | m_onePort = true; // force use of only one port |
mbed_official | 6:5a1583d0e6cd | 355 | } |
mbed_official | 6:5a1583d0e6cd | 356 | } |
mbed_official | 6:5a1583d0e6cd | 357 | |
mbed_official | 5:60b48a013e86 | 358 | CREGProcessor cregProcessor(m_gsm); |
mbed_official | 5:60b48a013e86 | 359 | //Wait for network registration |
mbed_official | 5:60b48a013e86 | 360 | do |
mbed_official | 5:60b48a013e86 | 361 | { |
mbed_official | 5:60b48a013e86 | 362 | DBG("Waiting for network registration"); |
mbed_official | 5:60b48a013e86 | 363 | ret = m_at.execute(cregProcessor.getAtCommand(), &cregProcessor, &result); |
mbed_official | 5:60b48a013e86 | 364 | DBG("Result of command: Err code=%d\n", ret); |
mbed_official | 5:60b48a013e86 | 365 | DBG("ATResult: AT return=%d (code %d)\n", result.result, result.code); |
mbed_official | 5:60b48a013e86 | 366 | if(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING) |
mbed_official | 5:60b48a013e86 | 367 | { |
mbed_official | 5:60b48a013e86 | 368 | Thread::wait(3000); |
mbed_official | 5:60b48a013e86 | 369 | } |
mbed_official | 5:60b48a013e86 | 370 | } while(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING); |
mbed_official | 5:60b48a013e86 | 371 | if(cregProcessor.getStatus() == CREGProcessor::STATUS_FAILED) |
mbed_official | 5:60b48a013e86 | 372 | { |
mbed_official | 5:60b48a013e86 | 373 | ERR("Registration denied"); |
mbed_official | 5:60b48a013e86 | 374 | return NET_AUTH; |
mbed_official | 5:60b48a013e86 | 375 | } |
mbed_official | 5:60b48a013e86 | 376 | |
mbed_official | 5:60b48a013e86 | 377 | m_atOpen = true; |
mbed_official | 5:60b48a013e86 | 378 | |
mbed_official | 5:60b48a013e86 | 379 | return OK; |
mbed_official | 5:60b48a013e86 | 380 | } |
mbed_official | 5:60b48a013e86 | 381 | |
mbed_official | 5:60b48a013e86 | 382 | int UbloxModem::cleanup() |
mbed_official | 5:60b48a013e86 | 383 | { |
mbed_official | 5:60b48a013e86 | 384 | if(m_ppp.isConnected()) |
mbed_official | 5:60b48a013e86 | 385 | { |
mbed_official | 5:60b48a013e86 | 386 | WARN("Data connection is still open"); //Try to encourage good behaviour from the user |
mbed_official | 5:60b48a013e86 | 387 | m_ppp.disconnect(); |
mbed_official | 5:60b48a013e86 | 388 | } |
mbed_official | 5:60b48a013e86 | 389 | |
mbed_official | 5:60b48a013e86 | 390 | m_smsInit = false; |
mbed_official | 5:60b48a013e86 | 391 | m_ussdInit = false; |
mbed_official | 5:60b48a013e86 | 392 | m_linkMonitorInit = false; |
mbed_official | 5:60b48a013e86 | 393 | //We don't reset m_ipInit as PPPIPInterface::init() only needs to be called once |
mbed_official | 5:60b48a013e86 | 394 | |
mbed_official | 5:60b48a013e86 | 395 | if(m_atOpen) |
mbed_official | 5:60b48a013e86 | 396 | { |
mbed_official | 5:60b48a013e86 | 397 | m_at.close(); |
mbed_official | 5:60b48a013e86 | 398 | m_atOpen = false; |
mbed_official | 5:60b48a013e86 | 399 | } |
mbed_official | 5:60b48a013e86 | 400 | |
mbed_official | 5:60b48a013e86 | 401 | return OK; |
mbed_official | 5:60b48a013e86 | 402 | } |
mbed_official | 5:60b48a013e86 | 403 | |
mbed_official | 5:60b48a013e86 | 404 | int UbloxModem::sendUSSD(const char* command, char* result, size_t maxLength) |
mbed_official | 5:60b48a013e86 | 405 | { |
mbed_official | 5:60b48a013e86 | 406 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 407 | if(ret) |
mbed_official | 5:60b48a013e86 | 408 | { |
mbed_official | 5:60b48a013e86 | 409 | return ret; |
mbed_official | 5:60b48a013e86 | 410 | } |
mbed_official | 5:60b48a013e86 | 411 | |
mbed_official | 5:60b48a013e86 | 412 | if(!m_ussdInit) |
mbed_official | 5:60b48a013e86 | 413 | { |
mbed_official | 5:60b48a013e86 | 414 | ret = m_ussd.init(); |
mbed_official | 5:60b48a013e86 | 415 | if(ret) |
mbed_official | 5:60b48a013e86 | 416 | { |
mbed_official | 5:60b48a013e86 | 417 | return ret; |
mbed_official | 5:60b48a013e86 | 418 | } |
mbed_official | 5:60b48a013e86 | 419 | m_ussdInit = true; |
mbed_official | 5:60b48a013e86 | 420 | } |
mbed_official | 5:60b48a013e86 | 421 | |
mbed_official | 5:60b48a013e86 | 422 | ret = m_ussd.send(command, result, maxLength); |
mbed_official | 5:60b48a013e86 | 423 | if(ret) |
mbed_official | 5:60b48a013e86 | 424 | { |
mbed_official | 5:60b48a013e86 | 425 | return ret; |
mbed_official | 5:60b48a013e86 | 426 | } |
mbed_official | 5:60b48a013e86 | 427 | |
mbed_official | 5:60b48a013e86 | 428 | return OK; |
mbed_official | 5:60b48a013e86 | 429 | } |
mbed_official | 5:60b48a013e86 | 430 | |
mbed_official | 5:60b48a013e86 | 431 | int UbloxModem::getLinkState(int* pRssi, LinkMonitor::REGISTRATION_STATE* pRegistrationState, LinkMonitor::BEARER* pBearer) |
mbed_official | 5:60b48a013e86 | 432 | { |
mbed_official | 5:60b48a013e86 | 433 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 434 | if(ret) |
mbed_official | 5:60b48a013e86 | 435 | { |
mbed_official | 5:60b48a013e86 | 436 | return ret; |
mbed_official | 5:60b48a013e86 | 437 | } |
mbed_official | 5:60b48a013e86 | 438 | |
mbed_official | 5:60b48a013e86 | 439 | if(!m_linkMonitorInit) |
mbed_official | 5:60b48a013e86 | 440 | { |
mbed_official | 5:60b48a013e86 | 441 | ret = m_linkMonitor.init(); |
mbed_official | 6:5a1583d0e6cd | 442 | ret = m_linkMonitor.init(m_gsm); |
mbed_official | 5:60b48a013e86 | 443 | if(ret) |
mbed_official | 5:60b48a013e86 | 444 | { |
mbed_official | 5:60b48a013e86 | 445 | return ret; |
mbed_official | 5:60b48a013e86 | 446 | } |
mbed_official | 5:60b48a013e86 | 447 | m_linkMonitorInit = true; |
mbed_official | 5:60b48a013e86 | 448 | } |
mbed_official | 5:60b48a013e86 | 449 | |
mbed_official | 5:60b48a013e86 | 450 | ret = m_linkMonitor.getState(pRssi, pRegistrationState, pBearer); |
mbed_official | 5:60b48a013e86 | 451 | if(ret) |
mbed_official | 5:60b48a013e86 | 452 | { |
mbed_official | 5:60b48a013e86 | 453 | return ret; |
mbed_official | 5:60b48a013e86 | 454 | } |
mbed_official | 5:60b48a013e86 | 455 | |
mbed_official | 5:60b48a013e86 | 456 | return OK; |
mbed_official | 5:60b48a013e86 | 457 | } |
mbed_official | 5:60b48a013e86 | 458 | |
mbed_official | 5:60b48a013e86 | 459 | #include "USBHost.h" |
mbed_official | 5:60b48a013e86 | 460 | #include "UbloxGSMModemInitializer.h" |
mbed_official | 5:60b48a013e86 | 461 | #include "UbloxCDMAModemInitializer.h" |
mbed_official | 5:60b48a013e86 | 462 | |
mbed_official | 5:60b48a013e86 | 463 | UbloxUSBModem::UbloxUSBModem() : |
mbed_official | 5:60b48a013e86 | 464 | UbloxModem(&m_atStream, &m_pppStream), |
mbed_official | 5:60b48a013e86 | 465 | m_dongle(), // Construct WANDongle: USB interface with two serial channels to the modem (USBSerialStream objects) |
mbed_official | 5:60b48a013e86 | 466 | m_atStream(m_dongle.getSerial(1)), // AT commands are sent down one serial channel. |
mbed_official | 5:60b48a013e86 | 467 | m_pppStream(m_dongle.getSerial(0)), // PPP connections are managed via another serial channel. |
mbed_official | 5:60b48a013e86 | 468 | m_dongleConnected(false) // Dongle is initially not ready for anything |
mbed_official | 5:60b48a013e86 | 469 | { |
mbed_official | 5:60b48a013e86 | 470 | USBHost* host = USBHost::getHostInst(); |
mbed_official | 5:60b48a013e86 | 471 | m_dongle.addInitializer(new UbloxGSMModemInitializer(host)); |
mbed_official | 5:60b48a013e86 | 472 | m_dongle.addInitializer(new UbloxCDMAModemInitializer(host)); |
mbed_official | 5:60b48a013e86 | 473 | } |
mbed_official | 5:60b48a013e86 | 474 | |
mbed_official | 5:60b48a013e86 | 475 | int UbloxUSBModem::init() |
mbed_official | 5:60b48a013e86 | 476 | { |
mbed_official | 5:60b48a013e86 | 477 | if( !m_dongleConnected ) |
mbed_official | 5:60b48a013e86 | 478 | { |
mbed_official | 5:60b48a013e86 | 479 | m_dongleConnected = true; |
mbed_official | 5:60b48a013e86 | 480 | while( !m_dongle.connected() ) |
mbed_official | 5:60b48a013e86 | 481 | { |
mbed_official | 5:60b48a013e86 | 482 | m_dongle.tryConnect(); |
mbed_official | 5:60b48a013e86 | 483 | Thread::wait(10); |
mbed_official | 5:60b48a013e86 | 484 | } |
mbed_official | 5:60b48a013e86 | 485 | if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAU200) |
mbed_official | 5:60b48a013e86 | 486 | { |
mbed_official | 5:60b48a013e86 | 487 | INFO("Using a u-blox LISA-U200 3G/WCDMA Modem"); |
mbed_official | 5:60b48a013e86 | 488 | } |
mbed_official | 5:60b48a013e86 | 489 | else if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAC200) |
mbed_official | 5:60b48a013e86 | 490 | { |
mbed_official | 5:60b48a013e86 | 491 | INFO("Using a u-blox LISA-C200 CDMA Modem"); |
mbed_official | 5:60b48a013e86 | 492 | m_gsm = false; |
mbed_official | 5:60b48a013e86 | 493 | m_onePort = true; |
mbed_official | 5:60b48a013e86 | 494 | } |
mbed_official | 5:60b48a013e86 | 495 | else |
mbed_official | 5:60b48a013e86 | 496 | { |
mbed_official | 5:60b48a013e86 | 497 | WARN("Using an Unknown Dongle"); |
mbed_official | 5:60b48a013e86 | 498 | } |
mbed_official | 5:60b48a013e86 | 499 | } |
mbed_official | 5:60b48a013e86 | 500 | return UbloxModem::init(); |
mbed_official | 5:60b48a013e86 | 501 | } |
mbed_official | 5:60b48a013e86 | 502 | |
mbed_official | 5:60b48a013e86 | 503 | int UbloxUSBModem::cleanup() |
mbed_official | 5:60b48a013e86 | 504 | { |
mbed_official | 5:60b48a013e86 | 505 | UbloxModem::cleanup(); |
mbed_official | 5:60b48a013e86 | 506 | m_dongle.disconnect(); |
mbed_official | 5:60b48a013e86 | 507 | m_dongleConnected = false; |
mbed_official | 5:60b48a013e86 | 508 | return OK; |
mbed_official | 5:60b48a013e86 | 509 | } |
mbed_official | 5:60b48a013e86 | 510 | |
mbed_official | 5:60b48a013e86 | 511 | UbloxSerModem::UbloxSerModem() : |
mbed_official | 5:60b48a013e86 | 512 | UbloxModem(&m_atStream, NULL), |
mbed_official | 5:60b48a013e86 | 513 | m_Serial(P0_15,P0_16), |
mbed_official | 5:60b48a013e86 | 514 | m_atStream(m_Serial) |
mbed_official | 5:60b48a013e86 | 515 | { |
mbed_official | 5:60b48a013e86 | 516 | m_Serial.baud(115200); |
mbed_official | 5:60b48a013e86 | 517 | } |
mbed_official | 5:60b48a013e86 | 518 |