Connection Manager library for u-blox cellular modules. It manages the modem for keeping data connection always active.
CNLib.cpp@1:29ad1d1ac1f9, 2016-01-21 (annotated)
- Committer:
- msinig
- Date:
- Thu Jan 21 14:00:25 2016 +0000
- Revision:
- 1:29ad1d1ac1f9
- Parent:
- 0:86284a262735
fix bugs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
msinig | 0:86284a262735 | 1 | #include "CNLib.h" |
msinig | 0:86284a262735 | 2 | #include "CNUtil.h" |
msinig | 0:86284a262735 | 3 | #include "CNManager.h" |
msinig | 0:86284a262735 | 4 | |
msinig | 0:86284a262735 | 5 | #define PROFILE "0" |
msinig | 0:86284a262735 | 6 | |
msinig | 0:86284a262735 | 7 | int CNLib::powerOnModem(){ |
msinig | 0:86284a262735 | 8 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 9 | int res = RESP_OK; |
msinig | 0:86284a262735 | 10 | PinName pn MDM_IF( = MDMPWRON, = D4); |
msinig | 0:86284a262735 | 11 | |
msinig | 0:86284a262735 | 12 | #ifdef TARGET_UBLOX_C027 |
msinig | 0:86284a262735 | 13 | int i = 20; |
msinig | 0:86284a262735 | 14 | if (pn != NC) { |
msinig | 0:86284a262735 | 15 | INFO("Modem::wakeup\r\n"); |
msinig | 0:86284a262735 | 16 | DigitalOut pin(pn, 1); |
msinig | 0:86284a262735 | 17 | while (i--) { |
msinig | 0:86284a262735 | 18 | // SARA-U2/LISA-U2 50..80us |
msinig | 0:86284a262735 | 19 | pin = 0; ::wait_us(50); |
msinig | 0:86284a262735 | 20 | pin = 1; ::wait_ms(10); |
msinig | 0:86284a262735 | 21 | |
msinig | 0:86284a262735 | 22 | // SARA-G35 >5ms, LISA-C2 > 150ms, LEON-G2 >5ms |
msinig | 0:86284a262735 | 23 | pin = 0; ::wait_ms(150); |
msinig | 0:86284a262735 | 24 | pin = 1; ::wait_ms(100); |
msinig | 0:86284a262735 | 25 | |
msinig | 0:86284a262735 | 26 | // purge any messages |
msinig | 0:86284a262735 | 27 | purge(); |
msinig | 0:86284a262735 | 28 | |
msinig | 0:86284a262735 | 29 | // check interface |
msinig | 0:86284a262735 | 30 | sendFormated("AT\r\n"); |
msinig | 0:86284a262735 | 31 | res = waitFinalResp(NULL,NULL,1000); |
msinig | 0:86284a262735 | 32 | |
msinig | 0:86284a262735 | 33 | if (RESPOK(res)) |
msinig | 0:86284a262735 | 34 | break; |
msinig | 0:86284a262735 | 35 | } |
msinig | 0:86284a262735 | 36 | if (i < 0) { |
msinig | 0:86284a262735 | 37 | ERROR("No Reply from Modem\r\n"); |
msinig | 0:86284a262735 | 38 | goto failure; |
msinig | 0:86284a262735 | 39 | } |
msinig | 0:86284a262735 | 40 | } |
msinig | 0:86284a262735 | 41 | #endif |
msinig | 0:86284a262735 | 42 | |
msinig | 0:86284a262735 | 43 | TRACE("%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 44 | return res; |
msinig | 0:86284a262735 | 45 | |
msinig | 0:86284a262735 | 46 | failure: |
msinig | 0:86284a262735 | 47 | ERROR("%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 48 | return res; |
msinig | 0:86284a262735 | 49 | } |
msinig | 0:86284a262735 | 50 | |
msinig | 0:86284a262735 | 51 | int CNLib::initModem() |
msinig | 0:86284a262735 | 52 | { |
msinig | 0:86284a262735 | 53 | int res; |
msinig | 0:86284a262735 | 54 | TRACE("CNLib::%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 55 | |
msinig | 0:86284a262735 | 56 | // echo off |
msinig | 0:86284a262735 | 57 | sendFormated("AT E0\r\n"); |
msinig | 0:86284a262735 | 58 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 59 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 60 | goto failure; |
msinig | 0:86284a262735 | 61 | // enable verbose error messages |
msinig | 0:86284a262735 | 62 | sendFormated("AT+CMEE=2\r\n"); |
msinig | 0:86284a262735 | 63 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 64 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 65 | goto failure; |
msinig | 0:86284a262735 | 66 | // set baud rate |
msinig | 0:86284a262735 | 67 | sendFormated("AT+IPR=115200\r\n"); |
msinig | 0:86284a262735 | 68 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 69 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 70 | goto failure; |
msinig | 0:86284a262735 | 71 | |
msinig | 0:86284a262735 | 72 | // wait some time until baudrate is applied |
msinig | 0:86284a262735 | 73 | wait_ms(200); // SARA-G > 40ms |
msinig | 0:86284a262735 | 74 | |
msinig | 0:86284a262735 | 75 | // get the manufacturer |
msinig | 0:86284a262735 | 76 | sendFormated("AT+CGMI\r\n"); |
msinig | 0:86284a262735 | 77 | res = waitFinalResp(_cbString, _dev.manu); |
msinig | 0:86284a262735 | 78 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 79 | goto failure; |
msinig | 0:86284a262735 | 80 | // get the model identification |
msinig | 0:86284a262735 | 81 | sendFormated("AT+CGMM\r\n"); |
msinig | 0:86284a262735 | 82 | res = waitFinalResp(_cbString, _dev.model); |
msinig | 0:86284a262735 | 83 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 84 | goto failure; |
msinig | 0:86284a262735 | 85 | // get the |
msinig | 0:86284a262735 | 86 | sendFormated("AT+CGMR\r\n"); |
msinig | 0:86284a262735 | 87 | res = waitFinalResp(_cbString, _dev.ver); |
msinig | 0:86284a262735 | 88 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 89 | goto failure; |
msinig | 0:86284a262735 | 90 | // Returns the product serial number, IMEI (International Mobile Equipment Identity) |
msinig | 0:86284a262735 | 91 | sendFormated("AT+CGSN\r\n"); |
msinig | 0:86284a262735 | 92 | res = waitFinalResp(_cbString, _dev.imei); |
msinig | 0:86284a262735 | 93 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 94 | goto failure; |
msinig | 0:86284a262735 | 95 | // enable power saving |
msinig | 0:86284a262735 | 96 | if (_dev.lpm != LPM_DISABLED) { |
msinig | 0:86284a262735 | 97 | // enable power saving (requires flow control, cts at least) |
msinig | 0:86284a262735 | 98 | sendFormated("AT+UPSV=1\r\n"); |
msinig | 0:86284a262735 | 99 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 100 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 101 | goto failure; |
msinig | 0:86284a262735 | 102 | _dev.lpm = LPM_ACTIVE; |
msinig | 0:86284a262735 | 103 | } |
msinig | 0:86284a262735 | 104 | // identify the module |
msinig | 0:86284a262735 | 105 | sendFormated("ATI\r\n"); |
msinig | 0:86284a262735 | 106 | res = waitFinalResp(_cbATI, &_dev.dev ); |
msinig | 0:86284a262735 | 107 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 108 | goto failure; |
msinig | 0:86284a262735 | 109 | if (_dev.dev == DEV_UNKNOWN){ |
msinig | 0:86284a262735 | 110 | ERROR("CNLib::%s UNKOWN device \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 111 | goto failure; |
msinig | 0:86284a262735 | 112 | } |
msinig | 0:86284a262735 | 113 | if ((_dev.dev == DEV_LISA_U2) || (_dev.dev == DEV_LEON_G2)) { |
msinig | 0:86284a262735 | 114 | // enable the network identification feature |
msinig | 0:86284a262735 | 115 | sendFormated("AT+UGPIOC=20,2\r\n"); |
msinig | 0:86284a262735 | 116 | res = waitFinalResp(NULL,NULL,20000); |
msinig | 0:86284a262735 | 117 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 118 | goto failure; |
msinig | 0:86284a262735 | 119 | } |
msinig | 0:86284a262735 | 120 | else if ((_dev.dev == DEV_SARA_U2) || (_dev.dev == DEV_SARA_U2) || (_dev.dev == DEV_SARA_G35)) { |
msinig | 0:86284a262735 | 121 | // enable the network identification feature |
msinig | 0:86284a262735 | 122 | sendFormated("AT+UGPIOC=16,2\r\n"); |
msinig | 0:86284a262735 | 123 | res = waitFinalResp(NULL,NULL,20000); |
msinig | 0:86284a262735 | 124 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 125 | goto failure; |
msinig | 0:86284a262735 | 126 | } |
msinig | 0:86284a262735 | 127 | //enable rat technology unsolicited |
msinig | 0:86284a262735 | 128 | sendFormated("AT+UREG=1\r\n"); |
msinig | 0:86284a262735 | 129 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 130 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 131 | goto failure; |
msinig | 0:86284a262735 | 132 | // enable the psd registration unsolicited result code |
msinig | 0:86284a262735 | 133 | sendFormated("AT+CGREG=2\r\n"); |
msinig | 0:86284a262735 | 134 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 135 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 136 | goto failure; |
msinig | 0:86284a262735 | 137 | // enable the network registration unsolicited result code |
msinig | 0:86284a262735 | 138 | sendFormated("AT+CREG=2\r\n"); |
msinig | 0:86284a262735 | 139 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 140 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 141 | goto failure; |
msinig | 0:86284a262735 | 142 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 143 | return res; |
msinig | 0:86284a262735 | 144 | |
msinig | 0:86284a262735 | 145 | failure: |
msinig | 0:86284a262735 | 146 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 147 | return res; |
msinig | 0:86284a262735 | 148 | } |
msinig | 0:86284a262735 | 149 | |
msinig | 0:86284a262735 | 150 | /*************************************************************************/ |
msinig | 0:86284a262735 | 151 | |
msinig | 0:86284a262735 | 152 | int CNLib::_mcbCPIN(int type, const char* buf, int len, Sim* sim) |
msinig | 0:86284a262735 | 153 | { |
msinig | 0:86284a262735 | 154 | int ret = WAIT; |
msinig | 0:86284a262735 | 155 | if (sim) { |
msinig | 0:86284a262735 | 156 | if (type == TYPE_PLUS){ |
msinig | 0:86284a262735 | 157 | char s[16]; |
msinig | 0:86284a262735 | 158 | if (sscanf(buf, "\r\n+CPIN: %[^\r]\r\n", s) >= 1){ |
msinig | 0:86284a262735 | 159 | if (!strcmp("READY", s)){ |
msinig | 0:86284a262735 | 160 | *sim = SIM_READY; |
msinig | 0:86284a262735 | 161 | } |
msinig | 0:86284a262735 | 162 | else if (!strcmp("SIM PIN", s)) |
msinig | 0:86284a262735 | 163 | *sim = SIM_PIN; |
msinig | 0:86284a262735 | 164 | else if (!strcmp("SIM PUK", s)) |
msinig | 0:86284a262735 | 165 | *sim = SIM_PUK; |
msinig | 0:86284a262735 | 166 | |
msinig | 0:86284a262735 | 167 | |
msinig | 0:86284a262735 | 168 | } |
msinig | 0:86284a262735 | 169 | } else if (type == TYPE_ERROR_CME) { |
msinig | 0:86284a262735 | 170 | if (strstr(buf, "+CME ERROR: SIM not inserted")){ |
msinig | 0:86284a262735 | 171 | *sim = SIM_MISSING; |
msinig | 0:86284a262735 | 172 | ret = RESP_OK; |
msinig | 0:86284a262735 | 173 | } |
msinig | 0:86284a262735 | 174 | else if (strstr(buf, "+CME ERROR: incorrect password\r\n")){ |
msinig | 0:86284a262735 | 175 | *sim = WRONG_PIN; |
msinig | 0:86284a262735 | 176 | ret = RESP_OK; |
msinig | 0:86284a262735 | 177 | } |
msinig | 0:86284a262735 | 178 | else{ |
msinig | 0:86284a262735 | 179 | *sim = SIM_UNKNOWN; |
msinig | 0:86284a262735 | 180 | ret = RESP_OK; |
msinig | 0:86284a262735 | 181 | } |
msinig | 0:86284a262735 | 182 | } |
msinig | 0:86284a262735 | 183 | } |
msinig | 0:86284a262735 | 184 | return ret; |
msinig | 0:86284a262735 | 185 | } |
msinig | 0:86284a262735 | 186 | |
msinig | 0:86284a262735 | 187 | int CNLib::simInit(const char* simpin) |
msinig | 0:86284a262735 | 188 | { |
msinig | 0:86284a262735 | 189 | int res; |
msinig | 0:86284a262735 | 190 | TRACE("CNLib::%s Enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 191 | if (simpin == NULL){ |
msinig | 0:86284a262735 | 192 | sendFormated("AT+CPIN?\r\n"); |
msinig | 0:86284a262735 | 193 | } |
msinig | 0:86284a262735 | 194 | else if (simpin != NULL && *simpin != NULL) |
msinig | 0:86284a262735 | 195 | sendFormated("AT+CPIN=\"%s\"\r\n", simpin); |
msinig | 0:86284a262735 | 196 | res = waitFinalResp(_mcbCPIN, &_dev.sim, 20000); |
msinig | 0:86284a262735 | 197 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 198 | goto failure; |
msinig | 0:86284a262735 | 199 | |
msinig | 0:86284a262735 | 200 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 201 | return res; |
msinig | 0:86284a262735 | 202 | |
msinig | 0:86284a262735 | 203 | failure: |
msinig | 0:86284a262735 | 204 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 205 | return res; |
msinig | 0:86284a262735 | 206 | } |
msinig | 0:86284a262735 | 207 | |
msinig | 0:86284a262735 | 208 | |
msinig | 0:86284a262735 | 209 | int CNLib::getSimInfo(){ |
msinig | 0:86284a262735 | 210 | int res; |
msinig | 0:86284a262735 | 211 | // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card. |
msinig | 0:86284a262735 | 212 | // ICCID is a serial number identifying the SIM. |
msinig | 0:86284a262735 | 213 | sendFormated("AT+CCID\r\n"); |
msinig | 0:86284a262735 | 214 | res = waitFinalResp(_cbCCID, _dev.ccid); |
msinig | 0:86284a262735 | 215 | if (res != RESP_OK) |
msinig | 0:86284a262735 | 216 | goto failure; |
msinig | 0:86284a262735 | 217 | // Request IMSI (International Mobile Subscriber Identification) |
msinig | 0:86284a262735 | 218 | sendFormated("AT+CIMI\r\n"); |
msinig | 0:86284a262735 | 219 | res = waitFinalResp(_cbString, _dev.imsi); |
msinig | 0:86284a262735 | 220 | if (RESP_OK != res) |
msinig | 0:86284a262735 | 221 | goto failure; |
msinig | 0:86284a262735 | 222 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 223 | return res; |
msinig | 0:86284a262735 | 224 | |
msinig | 0:86284a262735 | 225 | failure: |
msinig | 0:86284a262735 | 226 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 227 | return res; |
msinig | 0:86284a262735 | 228 | } |
msinig | 0:86284a262735 | 229 | |
msinig | 0:86284a262735 | 230 | /*****************************************************************************/ |
msinig | 0:86284a262735 | 231 | |
msinig | 0:86284a262735 | 232 | int CNLib::getNetworkInfo() |
msinig | 0:86284a262735 | 233 | { |
msinig | 0:86284a262735 | 234 | int res; |
msinig | 0:86284a262735 | 235 | TRACE("CNLib::%s Enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 236 | sendFormated("AT+COPS?\r\n"); |
msinig | 0:86284a262735 | 237 | res = waitFinalResp(_cbCOPS, &_net); |
msinig | 0:86284a262735 | 238 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 239 | goto failure; |
msinig | 0:86284a262735 | 240 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 241 | return res; |
msinig | 0:86284a262735 | 242 | |
msinig | 0:86284a262735 | 243 | failure: |
msinig | 0:86284a262735 | 244 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 245 | return res; |
msinig | 0:86284a262735 | 246 | } |
msinig | 0:86284a262735 | 247 | |
msinig | 0:86284a262735 | 248 | int CNLib::setNetAutoReg() |
msinig | 0:86284a262735 | 249 | { |
msinig | 0:86284a262735 | 250 | int res; |
msinig | 0:86284a262735 | 251 | TRACE("CNLib::%s Enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 252 | sendFormated("AT+COPS=0\r\n"); |
msinig | 0:86284a262735 | 253 | res = waitFinalResp(); |
msinig | 0:86284a262735 | 254 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 255 | goto failure; |
msinig | 0:86284a262735 | 256 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 257 | return res; |
msinig | 0:86284a262735 | 258 | |
msinig | 0:86284a262735 | 259 | failure: |
msinig | 0:86284a262735 | 260 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 261 | return res; |
msinig | 0:86284a262735 | 262 | } |
msinig | 0:86284a262735 | 263 | |
msinig | 0:86284a262735 | 264 | /****************************************************************/ |
msinig | 0:86284a262735 | 265 | |
msinig | 0:86284a262735 | 266 | int CNLib::getInternalStatusContext() |
msinig | 0:86284a262735 | 267 | { |
msinig | 0:86284a262735 | 268 | int res; |
msinig | 0:86284a262735 | 269 | TRACE("CNLib::%s Enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 270 | |
msinig | 0:86284a262735 | 271 | int context; |
msinig | 0:86284a262735 | 272 | sendFormated("AT+UPSND=" PROFILE ",8\r\n"); |
msinig | 0:86284a262735 | 273 | res = waitFinalResp(_cbUPSND, &context); |
msinig | 0:86284a262735 | 274 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 275 | goto failure; |
msinig | 0:86284a262735 | 276 | if (context == 0) |
msinig | 0:86284a262735 | 277 | _ip = NOIP; |
msinig | 0:86284a262735 | 278 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 279 | return res; |
msinig | 0:86284a262735 | 280 | |
msinig | 0:86284a262735 | 281 | failure: |
msinig | 0:86284a262735 | 282 | TRACE("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 283 | _ip = NOIP; |
msinig | 0:86284a262735 | 284 | return res; |
msinig | 0:86284a262735 | 285 | } |
msinig | 0:86284a262735 | 286 | |
msinig | 0:86284a262735 | 287 | int CNLib::disableInternalContext() |
msinig | 0:86284a262735 | 288 | { |
msinig | 0:86284a262735 | 289 | int res; |
msinig | 0:86284a262735 | 290 | TRACE("CNLib::%s Enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 291 | _ip = NOIP; |
msinig | 0:86284a262735 | 292 | sendFormated("AT+UPSDA=" PROFILE ",4\r\n"); |
msinig | 0:86284a262735 | 293 | res = waitFinalResp(NULL,NULL,40*1000); |
msinig | 0:86284a262735 | 294 | if (!RESPOK(res)) |
msinig | 0:86284a262735 | 295 | goto failure; |
msinig | 0:86284a262735 | 296 | TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 297 | return res; |
msinig | 0:86284a262735 | 298 | |
msinig | 0:86284a262735 | 299 | failure: |
msinig | 0:86284a262735 | 300 | ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res); |
msinig | 0:86284a262735 | 301 | return res; |
msinig | 0:86284a262735 | 302 | } |