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