u-blox USB modems (GSM and CDMA)
Dependencies: CellularUSBModem
Dependents: C027_CANInterfaceComm C027_ModemTransparentUSBCDC_revb UbloxModemHTTPClientTest C027_HTTPClientTest ... more
Legacy Networking Libray
This is an mbed 2 networking library. For an mbed OS 5 compatible library, please see:
Import libraryC027Interface
Socket interface for C027Interface. Implements the NetworkSocketAPI
UbloxModem.cpp@5:60b48a013e86, 2013-12-16 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Dec 16 09:00:36 2013 +0000
- Revision:
- 5:60b48a013e86
- Child:
- 6:5a1583d0e6cd
Synchronized with git revision 170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c
Full URL: https://github.com/mbedmicro/mbed/commit/170ac6562b7b2b5bb43f8ecf82b2af18b37eeb9c/
improve USB host library and cellular modem stack
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 | 5:60b48a013e86 | 48 | class CREGProcessor : public IATCommandsProcessor |
mbed_official | 5:60b48a013e86 | 49 | { |
mbed_official | 5:60b48a013e86 | 50 | public: |
mbed_official | 5:60b48a013e86 | 51 | CREGProcessor(bool gsm) : status(STATUS_REGISTERING) |
mbed_official | 5:60b48a013e86 | 52 | { |
mbed_official | 5:60b48a013e86 | 53 | m_gsm = gsm; |
mbed_official | 5:60b48a013e86 | 54 | } |
mbed_official | 5:60b48a013e86 | 55 | enum REGISTERING_STATUS { STATUS_REGISTERING, STATUS_OK, STATUS_FAILED }; |
mbed_official | 5:60b48a013e86 | 56 | REGISTERING_STATUS getStatus() |
mbed_official | 5:60b48a013e86 | 57 | { |
mbed_official | 5:60b48a013e86 | 58 | return status; |
mbed_official | 5:60b48a013e86 | 59 | } |
mbed_official | 5:60b48a013e86 | 60 | const char* getAtCommand() |
mbed_official | 5:60b48a013e86 | 61 | { |
mbed_official | 5:60b48a013e86 | 62 | return m_gsm ? "AT+CREG?" : "AT+CSS?"; |
mbed_official | 5:60b48a013e86 | 63 | } |
mbed_official | 5:60b48a013e86 | 64 | private: |
mbed_official | 5:60b48a013e86 | 65 | virtual int onNewATResponseLine(ATCommandsInterface* pInst, const char* line) |
mbed_official | 5:60b48a013e86 | 66 | { |
mbed_official | 5:60b48a013e86 | 67 | int r; |
mbed_official | 5:60b48a013e86 | 68 | if (m_gsm) |
mbed_official | 5:60b48a013e86 | 69 | { |
mbed_official | 5:60b48a013e86 | 70 | if( sscanf(line, "+CREG: %*d,%d", &r) == 1 ) |
mbed_official | 5:60b48a013e86 | 71 | { |
mbed_official | 5:60b48a013e86 | 72 | status = (r == 1 || r == 5) ? STATUS_OK : |
mbed_official | 5:60b48a013e86 | 73 | (r == 0 || r == 2) ? STATUS_REGISTERING : |
mbed_official | 5:60b48a013e86 | 74 | // (r == 3) ? STATUS_FAILED : |
mbed_official | 5:60b48a013e86 | 75 | STATUS_FAILED; |
mbed_official | 5:60b48a013e86 | 76 | } |
mbed_official | 5:60b48a013e86 | 77 | } |
mbed_official | 5:60b48a013e86 | 78 | else |
mbed_official | 5:60b48a013e86 | 79 | { |
mbed_official | 5:60b48a013e86 | 80 | char bc[3] = ""; |
mbed_official | 5:60b48a013e86 | 81 | if(sscanf(line, "%*s %*c,%2s,%*d",bc)==1) |
mbed_official | 5:60b48a013e86 | 82 | { |
mbed_official | 5:60b48a013e86 | 83 | status = (strcmp("Z", bc) == 0) ? STATUS_REGISTERING : STATUS_OK; |
mbed_official | 5:60b48a013e86 | 84 | } |
mbed_official | 5:60b48a013e86 | 85 | } |
mbed_official | 5:60b48a013e86 | 86 | return OK; |
mbed_official | 5:60b48a013e86 | 87 | } |
mbed_official | 5:60b48a013e86 | 88 | virtual int onNewEntryPrompt(ATCommandsInterface* pInst) |
mbed_official | 5:60b48a013e86 | 89 | { |
mbed_official | 5:60b48a013e86 | 90 | return OK; |
mbed_official | 5:60b48a013e86 | 91 | } |
mbed_official | 5:60b48a013e86 | 92 | volatile REGISTERING_STATUS status; |
mbed_official | 5:60b48a013e86 | 93 | bool m_gsm; |
mbed_official | 5:60b48a013e86 | 94 | }; |
mbed_official | 5:60b48a013e86 | 95 | |
mbed_official | 5:60b48a013e86 | 96 | int UbloxModem::connect(const char* apn, const char* user, const char* password) |
mbed_official | 5:60b48a013e86 | 97 | { |
mbed_official | 5:60b48a013e86 | 98 | if( !m_ipInit ) |
mbed_official | 5:60b48a013e86 | 99 | { |
mbed_official | 5:60b48a013e86 | 100 | m_ipInit = true; |
mbed_official | 5:60b48a013e86 | 101 | m_ppp.init(); |
mbed_official | 5:60b48a013e86 | 102 | } |
mbed_official | 5:60b48a013e86 | 103 | m_ppp.setup(user, password, m_gsm ? DEFAULT_MSISDN_GSM : DEFAULT_MSISDN_CDMA); |
mbed_official | 5:60b48a013e86 | 104 | |
mbed_official | 5:60b48a013e86 | 105 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 106 | if(ret) |
mbed_official | 5:60b48a013e86 | 107 | { |
mbed_official | 5:60b48a013e86 | 108 | return ret; |
mbed_official | 5:60b48a013e86 | 109 | } |
mbed_official | 5:60b48a013e86 | 110 | |
mbed_official | 5:60b48a013e86 | 111 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 112 | { |
mbed_official | 5:60b48a013e86 | 113 | m_smsInit = false; //SMS status reset |
mbed_official | 5:60b48a013e86 | 114 | m_ussdInit = false; //USSD status reset |
mbed_official | 5:60b48a013e86 | 115 | m_linkMonitorInit = false; //Link monitor status reset |
mbed_official | 5:60b48a013e86 | 116 | } |
mbed_official | 5:60b48a013e86 | 117 | |
mbed_official | 5:60b48a013e86 | 118 | ATCommandsInterface::ATResult result; |
mbed_official | 5:60b48a013e86 | 119 | |
mbed_official | 5:60b48a013e86 | 120 | if(apn != NULL) |
mbed_official | 5:60b48a013e86 | 121 | { |
mbed_official | 5:60b48a013e86 | 122 | char cmd[48]; |
mbed_official | 5:60b48a013e86 | 123 | int tries = 30; |
mbed_official | 5:60b48a013e86 | 124 | sprintf(cmd, "AT+CGDCONT=1,\"IP\",\"%s\"", apn); |
mbed_official | 5:60b48a013e86 | 125 | do //Try 30 times because for some reasons it can fail *a lot* with the K3772-Z dongle |
mbed_official | 5:60b48a013e86 | 126 | { |
mbed_official | 5:60b48a013e86 | 127 | ret = m_at.executeSimple(cmd, &result); |
mbed_official | 5:60b48a013e86 | 128 | DBG("Result of command: Err code=%d", ret); |
mbed_official | 5:60b48a013e86 | 129 | if(ret) |
mbed_official | 5:60b48a013e86 | 130 | { |
mbed_official | 5:60b48a013e86 | 131 | Thread::wait(500); |
mbed_official | 5:60b48a013e86 | 132 | } |
mbed_official | 5:60b48a013e86 | 133 | } while(ret && --tries); |
mbed_official | 5:60b48a013e86 | 134 | DBG("ATResult: AT return=%d (code %d)", result.result, result.code); |
mbed_official | 5:60b48a013e86 | 135 | DBG("APN set to %s", apn); |
mbed_official | 5:60b48a013e86 | 136 | } |
mbed_official | 5:60b48a013e86 | 137 | |
mbed_official | 5:60b48a013e86 | 138 | //Connect |
mbed_official | 5:60b48a013e86 | 139 | DBG("Connecting"); |
mbed_official | 5:60b48a013e86 | 140 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 141 | { |
mbed_official | 5:60b48a013e86 | 142 | m_at.close(); // Closing AT parser |
mbed_official | 5:60b48a013e86 | 143 | m_atOpen = false; //Will need to be reinitialized afterwards |
mbed_official | 5:60b48a013e86 | 144 | } |
mbed_official | 5:60b48a013e86 | 145 | |
mbed_official | 5:60b48a013e86 | 146 | DBG("Connecting PPP"); |
mbed_official | 5:60b48a013e86 | 147 | |
mbed_official | 5:60b48a013e86 | 148 | ret = m_ppp.connect(); |
mbed_official | 5:60b48a013e86 | 149 | DBG("Result of connect: Err code=%d", ret); |
mbed_official | 5:60b48a013e86 | 150 | return ret; |
mbed_official | 5:60b48a013e86 | 151 | } |
mbed_official | 5:60b48a013e86 | 152 | |
mbed_official | 5:60b48a013e86 | 153 | |
mbed_official | 5:60b48a013e86 | 154 | int UbloxModem::disconnect() |
mbed_official | 5:60b48a013e86 | 155 | { |
mbed_official | 5:60b48a013e86 | 156 | DBG("Disconnecting from PPP"); |
mbed_official | 5:60b48a013e86 | 157 | int ret = m_ppp.disconnect(); |
mbed_official | 5:60b48a013e86 | 158 | if(ret) |
mbed_official | 5:60b48a013e86 | 159 | { |
mbed_official | 5:60b48a013e86 | 160 | ERR("Disconnect returned %d, still trying to disconnect", ret); |
mbed_official | 5:60b48a013e86 | 161 | } |
mbed_official | 5:60b48a013e86 | 162 | |
mbed_official | 5:60b48a013e86 | 163 | //Ugly but leave dongle time to recover |
mbed_official | 5:60b48a013e86 | 164 | Thread::wait(500); |
mbed_official | 5:60b48a013e86 | 165 | |
mbed_official | 5:60b48a013e86 | 166 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 167 | { |
mbed_official | 5:60b48a013e86 | 168 | //ATCommandsInterface::ATResult result; |
mbed_official | 5:60b48a013e86 | 169 | DBG("Starting AT thread"); |
mbed_official | 5:60b48a013e86 | 170 | ret = m_at.open(); |
mbed_official | 5:60b48a013e86 | 171 | if(ret) |
mbed_official | 5:60b48a013e86 | 172 | { |
mbed_official | 5:60b48a013e86 | 173 | return ret; |
mbed_official | 5:60b48a013e86 | 174 | } |
mbed_official | 5:60b48a013e86 | 175 | } |
mbed_official | 5:60b48a013e86 | 176 | |
mbed_official | 5:60b48a013e86 | 177 | DBG("Trying to hangup"); |
mbed_official | 5:60b48a013e86 | 178 | |
mbed_official | 5:60b48a013e86 | 179 | if (m_onePort) |
mbed_official | 5:60b48a013e86 | 180 | { |
mbed_official | 5:60b48a013e86 | 181 | //Reinit AT parser |
mbed_official | 5:60b48a013e86 | 182 | ret = m_at.init(false); |
mbed_official | 5:60b48a013e86 | 183 | DBG("Result of command: Err code=%d\n", ret); |
mbed_official | 5:60b48a013e86 | 184 | if(ret) |
mbed_official | 5:60b48a013e86 | 185 | { |
mbed_official | 5:60b48a013e86 | 186 | m_at.close(); // Closing AT parser |
mbed_official | 5:60b48a013e86 | 187 | DBG("AT Parser closed, could not complete disconnection"); |
mbed_official | 5:60b48a013e86 | 188 | return NET_TIMEOUT; |
mbed_official | 5:60b48a013e86 | 189 | } |
mbed_official | 5:60b48a013e86 | 190 | |
mbed_official | 5:60b48a013e86 | 191 | } |
mbed_official | 5:60b48a013e86 | 192 | return OK; |
mbed_official | 5:60b48a013e86 | 193 | } |
mbed_official | 5:60b48a013e86 | 194 | |
mbed_official | 5:60b48a013e86 | 195 | int UbloxModem::sendSM(const char* number, const char* message) |
mbed_official | 5:60b48a013e86 | 196 | { |
mbed_official | 5:60b48a013e86 | 197 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 198 | if(ret) |
mbed_official | 5:60b48a013e86 | 199 | { |
mbed_official | 5:60b48a013e86 | 200 | return ret; |
mbed_official | 5:60b48a013e86 | 201 | } |
mbed_official | 5:60b48a013e86 | 202 | |
mbed_official | 5:60b48a013e86 | 203 | ISMSInterface* sms; |
mbed_official | 5:60b48a013e86 | 204 | if (m_gsm) sms = &m_GsmSms; |
mbed_official | 5:60b48a013e86 | 205 | else sms = &m_CdmaSms; |
mbed_official | 5:60b48a013e86 | 206 | if(!m_smsInit) |
mbed_official | 5:60b48a013e86 | 207 | { |
mbed_official | 5:60b48a013e86 | 208 | ret = sms->init(); |
mbed_official | 5:60b48a013e86 | 209 | if(ret) |
mbed_official | 5:60b48a013e86 | 210 | { |
mbed_official | 5:60b48a013e86 | 211 | return ret; |
mbed_official | 5:60b48a013e86 | 212 | } |
mbed_official | 5:60b48a013e86 | 213 | m_smsInit = true; |
mbed_official | 5:60b48a013e86 | 214 | } |
mbed_official | 5:60b48a013e86 | 215 | |
mbed_official | 5:60b48a013e86 | 216 | ret = sms->send(number, message); |
mbed_official | 5:60b48a013e86 | 217 | if(ret) |
mbed_official | 5:60b48a013e86 | 218 | { |
mbed_official | 5:60b48a013e86 | 219 | return ret; |
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::getSM(char* number, char* message, size_t maxLength) |
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->get(number, message, maxLength); |
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::getSMCount(size_t* pCount) |
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->getCount(pCount); |
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 | ATCommandsInterface* UbloxModem::getATCommandsInterface() |
mbed_official | 5:60b48a013e86 | 286 | { |
mbed_official | 5:60b48a013e86 | 287 | return &m_at; |
mbed_official | 5:60b48a013e86 | 288 | } |
mbed_official | 5:60b48a013e86 | 289 | |
mbed_official | 5:60b48a013e86 | 290 | int UbloxModem::init() |
mbed_official | 5:60b48a013e86 | 291 | { |
mbed_official | 5:60b48a013e86 | 292 | if(m_atOpen) |
mbed_official | 5:60b48a013e86 | 293 | { |
mbed_official | 5:60b48a013e86 | 294 | return OK; |
mbed_official | 5:60b48a013e86 | 295 | } |
mbed_official | 5:60b48a013e86 | 296 | |
mbed_official | 5:60b48a013e86 | 297 | DBG("Starting AT thread if needed"); |
mbed_official | 5:60b48a013e86 | 298 | int ret = m_at.open(); |
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 | |
mbed_official | 5:60b48a013e86 | 304 | DBG("Sending initialisation commands"); |
mbed_official | 5:60b48a013e86 | 305 | ret = m_at.init(false); |
mbed_official | 5:60b48a013e86 | 306 | if(ret) |
mbed_official | 5:60b48a013e86 | 307 | { |
mbed_official | 5:60b48a013e86 | 308 | return ret; |
mbed_official | 5:60b48a013e86 | 309 | } |
mbed_official | 5:60b48a013e86 | 310 | |
mbed_official | 5:60b48a013e86 | 311 | ATCommandsInterface::ATResult result; |
mbed_official | 5:60b48a013e86 | 312 | CREGProcessor cregProcessor(m_gsm); |
mbed_official | 5:60b48a013e86 | 313 | //Wait for network registration |
mbed_official | 5:60b48a013e86 | 314 | do |
mbed_official | 5:60b48a013e86 | 315 | { |
mbed_official | 5:60b48a013e86 | 316 | DBG("Waiting for network registration"); |
mbed_official | 5:60b48a013e86 | 317 | ret = m_at.execute(cregProcessor.getAtCommand(), &cregProcessor, &result); |
mbed_official | 5:60b48a013e86 | 318 | DBG("Result of command: Err code=%d\n", ret); |
mbed_official | 5:60b48a013e86 | 319 | DBG("ATResult: AT return=%d (code %d)\n", result.result, result.code); |
mbed_official | 5:60b48a013e86 | 320 | if(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING) |
mbed_official | 5:60b48a013e86 | 321 | { |
mbed_official | 5:60b48a013e86 | 322 | Thread::wait(3000); |
mbed_official | 5:60b48a013e86 | 323 | } |
mbed_official | 5:60b48a013e86 | 324 | } while(cregProcessor.getStatus() == CREGProcessor::STATUS_REGISTERING); |
mbed_official | 5:60b48a013e86 | 325 | if(cregProcessor.getStatus() == CREGProcessor::STATUS_FAILED) |
mbed_official | 5:60b48a013e86 | 326 | { |
mbed_official | 5:60b48a013e86 | 327 | ERR("Registration denied"); |
mbed_official | 5:60b48a013e86 | 328 | return NET_AUTH; |
mbed_official | 5:60b48a013e86 | 329 | } |
mbed_official | 5:60b48a013e86 | 330 | |
mbed_official | 5:60b48a013e86 | 331 | m_atOpen = true; |
mbed_official | 5:60b48a013e86 | 332 | |
mbed_official | 5:60b48a013e86 | 333 | return OK; |
mbed_official | 5:60b48a013e86 | 334 | } |
mbed_official | 5:60b48a013e86 | 335 | |
mbed_official | 5:60b48a013e86 | 336 | int UbloxModem::cleanup() |
mbed_official | 5:60b48a013e86 | 337 | { |
mbed_official | 5:60b48a013e86 | 338 | if(m_ppp.isConnected()) |
mbed_official | 5:60b48a013e86 | 339 | { |
mbed_official | 5:60b48a013e86 | 340 | WARN("Data connection is still open"); //Try to encourage good behaviour from the user |
mbed_official | 5:60b48a013e86 | 341 | m_ppp.disconnect(); |
mbed_official | 5:60b48a013e86 | 342 | } |
mbed_official | 5:60b48a013e86 | 343 | |
mbed_official | 5:60b48a013e86 | 344 | m_smsInit = false; |
mbed_official | 5:60b48a013e86 | 345 | m_ussdInit = false; |
mbed_official | 5:60b48a013e86 | 346 | m_linkMonitorInit = false; |
mbed_official | 5:60b48a013e86 | 347 | //We don't reset m_ipInit as PPPIPInterface::init() only needs to be called once |
mbed_official | 5:60b48a013e86 | 348 | |
mbed_official | 5:60b48a013e86 | 349 | if(m_atOpen) |
mbed_official | 5:60b48a013e86 | 350 | { |
mbed_official | 5:60b48a013e86 | 351 | m_at.close(); |
mbed_official | 5:60b48a013e86 | 352 | m_atOpen = false; |
mbed_official | 5:60b48a013e86 | 353 | } |
mbed_official | 5:60b48a013e86 | 354 | |
mbed_official | 5:60b48a013e86 | 355 | return OK; |
mbed_official | 5:60b48a013e86 | 356 | } |
mbed_official | 5:60b48a013e86 | 357 | |
mbed_official | 5:60b48a013e86 | 358 | int UbloxModem::sendUSSD(const char* command, char* result, size_t maxLength) |
mbed_official | 5:60b48a013e86 | 359 | { |
mbed_official | 5:60b48a013e86 | 360 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 361 | if(ret) |
mbed_official | 5:60b48a013e86 | 362 | { |
mbed_official | 5:60b48a013e86 | 363 | return ret; |
mbed_official | 5:60b48a013e86 | 364 | } |
mbed_official | 5:60b48a013e86 | 365 | |
mbed_official | 5:60b48a013e86 | 366 | if(!m_ussdInit) |
mbed_official | 5:60b48a013e86 | 367 | { |
mbed_official | 5:60b48a013e86 | 368 | ret = m_ussd.init(); |
mbed_official | 5:60b48a013e86 | 369 | if(ret) |
mbed_official | 5:60b48a013e86 | 370 | { |
mbed_official | 5:60b48a013e86 | 371 | return ret; |
mbed_official | 5:60b48a013e86 | 372 | } |
mbed_official | 5:60b48a013e86 | 373 | m_ussdInit = true; |
mbed_official | 5:60b48a013e86 | 374 | } |
mbed_official | 5:60b48a013e86 | 375 | |
mbed_official | 5:60b48a013e86 | 376 | ret = m_ussd.send(command, result, maxLength); |
mbed_official | 5:60b48a013e86 | 377 | if(ret) |
mbed_official | 5:60b48a013e86 | 378 | { |
mbed_official | 5:60b48a013e86 | 379 | return ret; |
mbed_official | 5:60b48a013e86 | 380 | } |
mbed_official | 5:60b48a013e86 | 381 | |
mbed_official | 5:60b48a013e86 | 382 | return OK; |
mbed_official | 5:60b48a013e86 | 383 | } |
mbed_official | 5:60b48a013e86 | 384 | |
mbed_official | 5:60b48a013e86 | 385 | int UbloxModem::getLinkState(int* pRssi, LinkMonitor::REGISTRATION_STATE* pRegistrationState, LinkMonitor::BEARER* pBearer) |
mbed_official | 5:60b48a013e86 | 386 | { |
mbed_official | 5:60b48a013e86 | 387 | int ret = init(); |
mbed_official | 5:60b48a013e86 | 388 | if(ret) |
mbed_official | 5:60b48a013e86 | 389 | { |
mbed_official | 5:60b48a013e86 | 390 | return ret; |
mbed_official | 5:60b48a013e86 | 391 | } |
mbed_official | 5:60b48a013e86 | 392 | |
mbed_official | 5:60b48a013e86 | 393 | if(!m_linkMonitorInit) |
mbed_official | 5:60b48a013e86 | 394 | { |
mbed_official | 5:60b48a013e86 | 395 | ret = m_linkMonitor.init(); |
mbed_official | 5:60b48a013e86 | 396 | if(ret) |
mbed_official | 5:60b48a013e86 | 397 | { |
mbed_official | 5:60b48a013e86 | 398 | return ret; |
mbed_official | 5:60b48a013e86 | 399 | } |
mbed_official | 5:60b48a013e86 | 400 | m_linkMonitorInit = true; |
mbed_official | 5:60b48a013e86 | 401 | } |
mbed_official | 5:60b48a013e86 | 402 | |
mbed_official | 5:60b48a013e86 | 403 | ret = m_linkMonitor.getState(pRssi, pRegistrationState, pBearer); |
mbed_official | 5:60b48a013e86 | 404 | if(ret) |
mbed_official | 5:60b48a013e86 | 405 | { |
mbed_official | 5:60b48a013e86 | 406 | return ret; |
mbed_official | 5:60b48a013e86 | 407 | } |
mbed_official | 5:60b48a013e86 | 408 | |
mbed_official | 5:60b48a013e86 | 409 | return OK; |
mbed_official | 5:60b48a013e86 | 410 | } |
mbed_official | 5:60b48a013e86 | 411 | |
mbed_official | 5:60b48a013e86 | 412 | #include "USBHost.h" |
mbed_official | 5:60b48a013e86 | 413 | #include "UbloxGSMModemInitializer.h" |
mbed_official | 5:60b48a013e86 | 414 | #include "UbloxCDMAModemInitializer.h" |
mbed_official | 5:60b48a013e86 | 415 | |
mbed_official | 5:60b48a013e86 | 416 | UbloxUSBModem::UbloxUSBModem() : |
mbed_official | 5:60b48a013e86 | 417 | UbloxModem(&m_atStream, &m_pppStream), |
mbed_official | 5:60b48a013e86 | 418 | m_dongle(), // Construct WANDongle: USB interface with two serial channels to the modem (USBSerialStream objects) |
mbed_official | 5:60b48a013e86 | 419 | m_atStream(m_dongle.getSerial(1)), // AT commands are sent down one serial channel. |
mbed_official | 5:60b48a013e86 | 420 | m_pppStream(m_dongle.getSerial(0)), // PPP connections are managed via another serial channel. |
mbed_official | 5:60b48a013e86 | 421 | m_dongleConnected(false) // Dongle is initially not ready for anything |
mbed_official | 5:60b48a013e86 | 422 | { |
mbed_official | 5:60b48a013e86 | 423 | USBHost* host = USBHost::getHostInst(); |
mbed_official | 5:60b48a013e86 | 424 | m_dongle.addInitializer(new UbloxGSMModemInitializer(host)); |
mbed_official | 5:60b48a013e86 | 425 | m_dongle.addInitializer(new UbloxCDMAModemInitializer(host)); |
mbed_official | 5:60b48a013e86 | 426 | } |
mbed_official | 5:60b48a013e86 | 427 | |
mbed_official | 5:60b48a013e86 | 428 | int UbloxUSBModem::init() |
mbed_official | 5:60b48a013e86 | 429 | { |
mbed_official | 5:60b48a013e86 | 430 | if( !m_dongleConnected ) |
mbed_official | 5:60b48a013e86 | 431 | { |
mbed_official | 5:60b48a013e86 | 432 | m_dongleConnected = true; |
mbed_official | 5:60b48a013e86 | 433 | while( !m_dongle.connected() ) |
mbed_official | 5:60b48a013e86 | 434 | { |
mbed_official | 5:60b48a013e86 | 435 | m_dongle.tryConnect(); |
mbed_official | 5:60b48a013e86 | 436 | Thread::wait(10); |
mbed_official | 5:60b48a013e86 | 437 | } |
mbed_official | 5:60b48a013e86 | 438 | if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAU200) |
mbed_official | 5:60b48a013e86 | 439 | { |
mbed_official | 5:60b48a013e86 | 440 | INFO("Using a u-blox LISA-U200 3G/WCDMA Modem"); |
mbed_official | 5:60b48a013e86 | 441 | } |
mbed_official | 5:60b48a013e86 | 442 | else if(m_dongle.getDongleType() == WAN_DONGLE_TYPE_UBLOX_LISAC200) |
mbed_official | 5:60b48a013e86 | 443 | { |
mbed_official | 5:60b48a013e86 | 444 | INFO("Using a u-blox LISA-C200 CDMA Modem"); |
mbed_official | 5:60b48a013e86 | 445 | m_gsm = false; |
mbed_official | 5:60b48a013e86 | 446 | m_onePort = true; |
mbed_official | 5:60b48a013e86 | 447 | } |
mbed_official | 5:60b48a013e86 | 448 | else |
mbed_official | 5:60b48a013e86 | 449 | { |
mbed_official | 5:60b48a013e86 | 450 | WARN("Using an Unknown Dongle"); |
mbed_official | 5:60b48a013e86 | 451 | } |
mbed_official | 5:60b48a013e86 | 452 | } |
mbed_official | 5:60b48a013e86 | 453 | return UbloxModem::init(); |
mbed_official | 5:60b48a013e86 | 454 | } |
mbed_official | 5:60b48a013e86 | 455 | |
mbed_official | 5:60b48a013e86 | 456 | int UbloxUSBModem::cleanup() |
mbed_official | 5:60b48a013e86 | 457 | { |
mbed_official | 5:60b48a013e86 | 458 | UbloxModem::cleanup(); |
mbed_official | 5:60b48a013e86 | 459 | m_dongle.disconnect(); |
mbed_official | 5:60b48a013e86 | 460 | m_dongleConnected = false; |
mbed_official | 5:60b48a013e86 | 461 | return OK; |
mbed_official | 5:60b48a013e86 | 462 | } |
mbed_official | 5:60b48a013e86 | 463 | |
mbed_official | 5:60b48a013e86 | 464 | UbloxSerModem::UbloxSerModem() : |
mbed_official | 5:60b48a013e86 | 465 | UbloxModem(&m_atStream, NULL), |
mbed_official | 5:60b48a013e86 | 466 | m_Serial(P0_15,P0_16), |
mbed_official | 5:60b48a013e86 | 467 | m_atStream(m_Serial) |
mbed_official | 5:60b48a013e86 | 468 | { |
mbed_official | 5:60b48a013e86 | 469 | m_Serial.baud(115200); |
mbed_official | 5:60b48a013e86 | 470 | } |
mbed_official | 5:60b48a013e86 | 471 |