* add C027_Support fork

Fork of C027_Support by u-blox

Committer:
mazgch
Date:
Tue May 27 08:21:53 2014 +0000
Revision:
80:34985b4d821e
Parent:
79:291df065e345
Child:
81:3966a5c17037
improved psd auth handling, added file api

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 18:e5697801df29 1 #include "mbed.h"
mazgch 18:e5697801df29 2 #include "MDM.h"
mazgch 74:208e3e32d263 3 #include "Relax.h"
mazgch 74:208e3e32d263 4 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 5 #include "C027_api.h"
mazgch 74:208e3e32d263 6 #endif
mazgch 18:e5697801df29 7
mazgch 74:208e3e32d263 8 #define PROFILE "0" //!< this is the psd profile used
mazgch 74:208e3e32d263 9 #define MAX_SIZE 128 //!< max expected messages
mazgch 74:208e3e32d263 10 //! test if it is a socket
mazgch 21:c4d64830bf02 11 #define ISSOCKET(s) (((s) >= 0) && ((s) < (sizeof(_sockets)/sizeof(*_sockets))))
mazgch 75:ce6e12067d0c 12 //! check for timeout
mazgch 75:ce6e12067d0c 13 #define TIMEOUT(t, ms) ((ms != TIMEOUT_BLOCKING) && (ms < t.read_ms()))
mazgch 80:34985b4d821e 14 //! registration ok check helper
mazgch 79:291df065e345 15 #define REG_OK(r) ((r == REG_HOME) || (r == REG_ROAMING))
mazgch 80:34985b4d821e 16 //! registration done check helper (no need to poll further)
mazgch 79:291df065e345 17 #define REG_DONE(r) ((r == REG_HOME) || (r == REG_ROAMING) || (r == REG_DENIED))
mazgch 79:291df065e345 18
mazgch 74:208e3e32d263 19 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 20 void dumpAtCmd(const char* buf, int len)
mazgch 21:c4d64830bf02 21 {
mazgch 75:ce6e12067d0c 22 ::printf(" %3d \"", len);
mazgch 21:c4d64830bf02 23 while (len --) {
mazgch 21:c4d64830bf02 24 char ch = *buf++;
mazgch 75:ce6e12067d0c 25 if ((ch > 0x1F) && (ch != 0x7F)) { // is printable
mazgch 75:ce6e12067d0c 26 if (ch == '%') ::printf("%%");
mazgch 75:ce6e12067d0c 27 else if (ch == '"') ::printf("\\\"");
mazgch 75:ce6e12067d0c 28 else if (ch == '\\') ::printf("\\\\");
mazgch 75:ce6e12067d0c 29 else putchar(ch);
mazgch 75:ce6e12067d0c 30 } else {
mazgch 75:ce6e12067d0c 31 if (ch == '\a') ::printf("\\a"); // BEL (0x07)
mazgch 75:ce6e12067d0c 32 else if (ch == '\b') ::printf("\\b"); // Backspace (0x08)
mazgch 75:ce6e12067d0c 33 else if (ch == '\t') ::printf("\\t"); // Horizontal Tab (0x09)
mazgch 75:ce6e12067d0c 34 else if (ch == '\n') ::printf("\\n"); // Linefeed (0x0A)
mazgch 75:ce6e12067d0c 35 else if (ch == '\v') ::printf("\\v"); // Vertical Tab (0x0B)
mazgch 75:ce6e12067d0c 36 else if (ch == '\f') ::printf("\\f"); // Formfeed (0x0C)
mazgch 75:ce6e12067d0c 37 else if (ch == '\r') ::printf("\\r"); // Carriage Return (0x0D)
mazgch 76:f7c3dd568dae 38 else ::printf("\\x%02x", (unsigned char)ch);
mazgch 75:ce6e12067d0c 39 }
mazgch 21:c4d64830bf02 40 }
mazgch 75:ce6e12067d0c 41 ::printf("\"\r\n");
mazgch 21:c4d64830bf02 42 }
mazgch 74:208e3e32d263 43
mazgch 75:ce6e12067d0c 44 #define ERROR(fmt) (_debugLevel < 0) ? : ::printf(RED(fmt))
mazgch 75:ce6e12067d0c 45 #define INFO(fmt) (_debugLevel < 1) ? : ::printf(GRE(fmt))
mazgch 75:ce6e12067d0c 46 #define TRACE(...) (_debugLevel < 2) ? : ::printf(__VA_ARGS__)
mazgch 74:208e3e32d263 47
mazgch 31:a0bed6c1e05d 48 #if 1 // colored terminal output using ANSI escape sequences
mazgch 75:ce6e12067d0c 49 #define COL(c,t) "\033[" c t "\033[" "39m"
mazgch 31:a0bed6c1e05d 50 #else
mazgch 31:a0bed6c1e05d 51 #define COL(c,t) t
mazgch 31:a0bed6c1e05d 52 #endif
mazgch 31:a0bed6c1e05d 53 #define BLA(t) COL("30m",t)
mazgch 31:a0bed6c1e05d 54 #define RED(t) COL("31m",t)
mazgch 31:a0bed6c1e05d 55 #define GRE(t) COL("32m",t)
mazgch 31:a0bed6c1e05d 56 #define YEL(t) COL("33m",t)
mazgch 31:a0bed6c1e05d 57 #define BLU(t) COL("34m",t)
mazgch 31:a0bed6c1e05d 58 #define MAG(t) COL("35m",t)
mazgch 31:a0bed6c1e05d 59 #define CYA(t) COL("36m",t)
mazgch 31:a0bed6c1e05d 60 #define WHY(t) COL("37m",t)
mazgch 74:208e3e32d263 61
mazgch 74:208e3e32d263 62 #else
mazgch 74:208e3e32d263 63
mazgch 75:ce6e12067d0c 64 #define ERROR(...) (void)0 // no tracing
mazgch 74:208e3e32d263 65 #define INFO(...) (void)0 // no tracing
mazgch 74:208e3e32d263 66 #define TRACE(...) (void)0 // no tracing
mazgch 74:208e3e32d263 67
mazgch 31:a0bed6c1e05d 68 #endif
mazgch 21:c4d64830bf02 69
mazgch 44:9d12223b78ff 70 MDMParser* MDMParser::inst;
mazgch 44:9d12223b78ff 71
mazgch 21:c4d64830bf02 72 MDMParser::MDMParser(void)
mazgch 21:c4d64830bf02 73 {
mazgch 44:9d12223b78ff 74 inst = this;
mazgch 31:a0bed6c1e05d 75 memset(&_dev, 0, sizeof(_dev));
mazgch 31:a0bed6c1e05d 76 memset(&_net, 0, sizeof(_net));
mazgch 54:7ba8e4c218e2 77 _net.lac = 0xFFFF;
mazgch 54:7ba8e4c218e2 78 _net.ci = 0xFFFFFFFF;
mazgch 35:9275215a3a5b 79 _ip = NOIP;
mazgch 76:f7c3dd568dae 80 _init = false;
mazgch 31:a0bed6c1e05d 81 memset(_sockets, 0, sizeof(_sockets));
mazgch 74:208e3e32d263 82 #ifdef MDM_DEBUG
mazgch 76:f7c3dd568dae 83 _debugLevel = 1;
mazgch 74:208e3e32d263 84 _debugTime.start();
mazgch 74:208e3e32d263 85 #endif
mazgch 74:208e3e32d263 86 }
mazgch 74:208e3e32d263 87
mazgch 18:e5697801df29 88 int MDMParser::send(const char* buf, int len)
mazgch 18:e5697801df29 89 {
mazgch 74:208e3e32d263 90 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 91 if (_debugLevel >= 3) {
mazgch 75:ce6e12067d0c 92 ::printf("%10.3f AT send ", _debugTime.read_ms()*0.001);
mazgch 74:208e3e32d263 93 dumpAtCmd(buf,len);
mazgch 74:208e3e32d263 94 }
mazgch 21:c4d64830bf02 95 #endif
mazgch 18:e5697801df29 96 return _send(buf, len);
mazgch 18:e5697801df29 97 }
mazgch 18:e5697801df29 98
mazgch 21:c4d64830bf02 99 int MDMParser::sendFormated(const char* format, ...) {
mazgch 21:c4d64830bf02 100 char buf[MAX_SIZE];
mazgch 21:c4d64830bf02 101 va_list args;
mazgch 21:c4d64830bf02 102 va_start(args, format);
mazgch 21:c4d64830bf02 103 int len = vsnprintf(buf,sizeof(buf), format, args);
mazgch 21:c4d64830bf02 104 va_end(args);
mazgch 21:c4d64830bf02 105 return send(buf, len);
mazgch 21:c4d64830bf02 106 }
mazgch 21:c4d64830bf02 107
mazgch 26:07be5faf8925 108 int MDMParser::waitFinalResp(_CALLBACKPTR cb /* = NULL*/,
mazgch 26:07be5faf8925 109 void* param /* = NULL*/,
mazgch 26:07be5faf8925 110 int timeout_ms /*= 5000*/)
mazgch 26:07be5faf8925 111 {
mazgch 69:4d6fa520dfca 112 char buf[MAX_SIZE + 64 /* add some more space for framing */];
mazgch 21:c4d64830bf02 113 Timer timer;
mazgch 21:c4d64830bf02 114 timer.start();
mazgch 21:c4d64830bf02 115 do {
mazgch 21:c4d64830bf02 116 int ret = getLine(buf, sizeof(buf));
mazgch 74:208e3e32d263 117 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 118 if ((_debugLevel >= 3) && (ret != WAIT) && (ret != NOT_FOUND))
mazgch 21:c4d64830bf02 119 {
mazgch 31:a0bed6c1e05d 120 int len = LENGTH(ret);
mazgch 31:a0bed6c1e05d 121 int type = TYPE(ret);
mazgch 31:a0bed6c1e05d 122 const char* s = (type == TYPE_UNKNOWN)? YEL("UNK") :
mazgch 35:9275215a3a5b 123 (type == TYPE_TEXT) ? MAG("TXT") :
mazgch 53:cb0d94b9de3a 124 (type == TYPE_OK ) ? GRE("OK ") :
mazgch 31:a0bed6c1e05d 125 (type == TYPE_ERROR) ? RED("ERR") :
mazgch 31:a0bed6c1e05d 126 (type == TYPE_PLUS) ? CYA(" + ") :
mazgch 31:a0bed6c1e05d 127 (type == TYPE_PROMPT) ? BLU(" > ") :
mazgch 31:a0bed6c1e05d 128 "..." ;
mazgch 75:ce6e12067d0c 129 ::printf("%10.3f AT read %s", _debugTime.read_ms()*0.001, s);
mazgch 74:208e3e32d263 130 dumpAtCmd(buf, len);
mazgch 21:c4d64830bf02 131 }
mazgch 21:c4d64830bf02 132 #endif
mazgch 21:c4d64830bf02 133 if ((ret != WAIT) && (ret != NOT_FOUND))
mazgch 18:e5697801df29 134 {
mazgch 21:c4d64830bf02 135 int type = TYPE(ret);
mazgch 21:c4d64830bf02 136 // handle unsolicited commands here
mazgch 21:c4d64830bf02 137 if (type == TYPE_PLUS) {
mazgch 21:c4d64830bf02 138 const char* cmd = buf+3;
mazgch 54:7ba8e4c218e2 139 int a, b, c, d, r;
mazgch 23:05a1aeeb5fd9 140 char s[32];
mazgch 21:c4d64830bf02 141
mazgch 31:a0bed6c1e05d 142 // SMS Command ---------------------------------
mazgch 31:a0bed6c1e05d 143 // +CNMI: <mem>,<index>
mazgch 31:a0bed6c1e05d 144 if (sscanf(cmd, "CMTI: \"%*[^\"]\",%d", &a) == 1) {
mazgch 31:a0bed6c1e05d 145 TRACE("New SMS at index %d\r\n", a);
mazgch 21:c4d64830bf02 146 // Socket Specific Command ---------------------------------
mazgch 21:c4d64830bf02 147 // +UUSORD: <socket>,<length>
mazgch 21:c4d64830bf02 148 } else if ((sscanf(cmd, "UUSORD: %d,%d", &a, &b) == 2) &&
mazgch 63:42cb563a25bc 149 ISSOCKET(a) /*&& (_sockets[a].state == SOCK_CONNECTED)*/) {
mazgch 31:a0bed6c1e05d 150 TRACE("Socket %d: %d bytes pending\r\n", a, b);
mazgch 21:c4d64830bf02 151 _sockets[a].pending = b;
mazgch 69:4d6fa520dfca 152 // +UUSORF: <socket>,<length>
mazgch 69:4d6fa520dfca 153 } else if ((sscanf(cmd, "UUSORF: %d,%d", &a, &b) == 2) &&
mazgch 69:4d6fa520dfca 154 ISSOCKET(a) /*&& (_sockets[a].state == SOCK_CONNECTED)*/) {
mazgch 69:4d6fa520dfca 155 TRACE("Socket %d: %d bytes pending\r\n", a, b);
mazgch 69:4d6fa520dfca 156 _sockets[a].pending = b;
mazgch 21:c4d64830bf02 157 // +UUSOCL: <socket>
mazgch 21:c4d64830bf02 158 } else if ((sscanf(cmd, "UUSOCL: %d", &a) == 1) &&
mazgch 21:c4d64830bf02 159 ISSOCKET(a) && (_sockets[a].state == SOCK_CONNECTED)) {
mazgch 31:a0bed6c1e05d 160 TRACE("Socket %d: closed by remote host\r\n", a);
mazgch 21:c4d64830bf02 161 _sockets[a].state = SOCK_CREATED/*=CLOSED*/;
mazgch 21:c4d64830bf02 162 }
mazgch 32:8f12ac182bbb 163 if (_dev.dev == DEV_LISA_C200) {
mazgch 21:c4d64830bf02 164 // CDMA Specific -------------------------------------------
mazgch 21:c4d64830bf02 165 // +CREG: <n><SID>,<NID>,<stat>
mazgch 54:7ba8e4c218e2 166 if (sscanf(cmd, "CREG: %*d,%d,%d,%d",&a,&b,&c) == 3) {
mazgch 54:7ba8e4c218e2 167 // _net.sid = a;
mazgch 54:7ba8e4c218e2 168 // _net.nid = b;
mazgch 79:291df065e345 169 if (c == 0) _net.csd = REG_NONE; // not registered, home network
mazgch 79:291df065e345 170 else if (c == 1) _net.csd = REG_HOME; // registered, home network
mazgch 79:291df065e345 171 else if (c == 2) _net.csd = REG_NONE; // not registered, but MT is currently searching a new operator to register to
mazgch 79:291df065e345 172 else if (c == 3) _net.csd = REG_DENIED; // registration denied
mazgch 79:291df065e345 173 else if (c == 5) _net.csd = REG_ROAMING; // registered, roaming
mazgch 79:291df065e345 174 _net.psd = _net.csd; // fake PSD registration (CDMA is always registered)
mazgch 31:a0bed6c1e05d 175 _net.act = ACT_CDMA;
mazgch 21:c4d64830bf02 176 // +CSS: <mode>[,<format>,<oper>[,<AcT>]]
mazgch 21:c4d64830bf02 177 } else if (sscanf(cmd, "CSS %*c,%2s,%*d",s) == 1) {
mazgch 31:a0bed6c1e05d 178 //_net.reg = (strcmp("Z", s) == 0) ? REG_UNKNOWN : REG_HOME;
mazgch 21:c4d64830bf02 179 }
mazgch 21:c4d64830bf02 180 } else {
mazgch 21:c4d64830bf02 181 // GSM/UMTS Specific -------------------------------------------
mazgch 79:291df065e345 182 // +UUPSDD: <profile_id>
mazgch 79:291df065e345 183 if (sscanf(cmd, "UUPSDD: %d",&a) == 1) {
mazgch 79:291df065e345 184 if (*PROFILE == a) _ip = NOIP;
mazgch 79:291df065e345 185 } else {
mazgch 79:291df065e345 186 // +CREG|CGREG: <n>,<stat>[,<lac>,<ci>[,AcT[,<rac>]]] // reply to AT+CREG|AT+CGREG
mazgch 79:291df065e345 187 // +CREG|CGREG: <stat>[,<lac>,<ci>[,AcT[,<rac>]]] // URC
mazgch 79:291df065e345 188 b = 0xFFFF; c = 0xFFFFFFFF; d = -1;
mazgch 79:291df065e345 189 r = sscanf(cmd, "%s %*d,%d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d);
mazgch 79:291df065e345 190 if (r <= 1)
mazgch 79:291df065e345 191 r = sscanf(cmd, "%s %d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d);
mazgch 79:291df065e345 192 if (r >= 2) {
mazgch 79:291df065e345 193 Reg *reg = !strcmp(s, "CREG:") ? &_net.csd :
mazgch 79:291df065e345 194 !strcmp(s, "CGREG:") ? &_net.psd : NULL;
mazgch 79:291df065e345 195 if (reg) {
mazgch 79:291df065e345 196 // network status
mazgch 79:291df065e345 197 if (a == 0) *reg = REG_NONE; // 0: not registered, home network
mazgch 79:291df065e345 198 else if (a == 1) *reg = REG_HOME; // 1: registered, home network
mazgch 79:291df065e345 199 else if (a == 2) *reg = REG_NONE; // 2: not registered, but MT is currently searching a new operator to register to
mazgch 79:291df065e345 200 else if (a == 3) *reg = REG_DENIED; // 3: registration denied
mazgch 79:291df065e345 201 else if (a == 4) *reg = REG_UNKNOWN; // 4: unknown
mazgch 79:291df065e345 202 else if (a == 5) *reg = REG_ROAMING; // 5: registered, roaming
mazgch 79:291df065e345 203 if ((r >= 3) && (b != 0xFFFF)) _net.lac = b; // location area code
mazgch 79:291df065e345 204 if ((r >= 4) && (c != 0xFFFFFFFF)) _net.ci = c; // cell ID
mazgch 79:291df065e345 205 // access technology
mazgch 79:291df065e345 206 if (r >= 5) {
mazgch 79:291df065e345 207 if (d == 0) _net.act = ACT_GSM; // 0: GSM
mazgch 79:291df065e345 208 else if (d == 1) _net.act = ACT_GSM; // 1: GSM COMPACT
mazgch 79:291df065e345 209 else if (d == 2) _net.act = ACT_UTRAN; // 2: UTRAN
mazgch 79:291df065e345 210 else if (d == 3) _net.act = ACT_EDGE; // 3: GSM with EDGE availability
mazgch 79:291df065e345 211 else if (d == 4) _net.act = ACT_UTRAN; // 4: UTRAN with HSDPA availability
mazgch 79:291df065e345 212 else if (d == 5) _net.act = ACT_UTRAN; // 5: UTRAN with HSUPA availability
mazgch 79:291df065e345 213 else if (d == 6) _net.act = ACT_UTRAN; // 6: UTRAN with HSDPA and HSUPA availability
mazgch 79:291df065e345 214 }
mazgch 79:291df065e345 215 }
mazgch 54:7ba8e4c218e2 216 }
mazgch 21:c4d64830bf02 217 }
mazgch 21:c4d64830bf02 218 }
mazgch 21:c4d64830bf02 219 }
mazgch 21:c4d64830bf02 220 if (cb) {
mazgch 21:c4d64830bf02 221 int len = LENGTH(ret);
mazgch 21:c4d64830bf02 222 int ret = cb(type, buf, len, param);
mazgch 21:c4d64830bf02 223 if (WAIT != ret)
mazgch 21:c4d64830bf02 224 return ret;
mazgch 21:c4d64830bf02 225 }
mazgch 75:ce6e12067d0c 226 if (type == TYPE_OK) return RESP_OK;
mazgch 75:ce6e12067d0c 227 if (type == TYPE_ERROR) return RESP_ERROR;
mazgch 75:ce6e12067d0c 228 if (type == TYPE_PROMPT) return RESP_PROMPT;
mazgch 21:c4d64830bf02 229 }
mazgch 21:c4d64830bf02 230 // relax a bit
mazgch 74:208e3e32d263 231 RELAX_MS(10);
mazgch 21:c4d64830bf02 232 }
mazgch 75:ce6e12067d0c 233 while (!TIMEOUT(timer, timeout_ms));
mazgch 21:c4d64830bf02 234 return WAIT;
mazgch 21:c4d64830bf02 235 }
mazgch 21:c4d64830bf02 236
mazgch 31:a0bed6c1e05d 237 int MDMParser::_cbString(int type, const char* buf, int len, char* str)
mazgch 21:c4d64830bf02 238 {
mazgch 37:cc3433329d66 239 if (str && (type == TYPE_UNKNOWN)) {
mazgch 31:a0bed6c1e05d 240 if (sscanf(buf, "\r\n%s\r\n", str) == 1)
mazgch 31:a0bed6c1e05d 241 /*nothing*/;
mazgch 21:c4d64830bf02 242 }
mazgch 21:c4d64830bf02 243 return WAIT;
mazgch 21:c4d64830bf02 244 }
mazgch 21:c4d64830bf02 245
mazgch 31:a0bed6c1e05d 246 int MDMParser::_cbInt(int type, const char* buf, int len, int* val)
mazgch 31:a0bed6c1e05d 247 {
mazgch 37:cc3433329d66 248 if (val && (type == TYPE_UNKNOWN)) {
mazgch 31:a0bed6c1e05d 249 if (sscanf(buf, "\r\n%d\r\n", val) == 1)
mazgch 31:a0bed6c1e05d 250 /*nothing*/;
mazgch 31:a0bed6c1e05d 251 }
mazgch 31:a0bed6c1e05d 252 return WAIT;
mazgch 31:a0bed6c1e05d 253 }
mazgch 31:a0bed6c1e05d 254
mazgch 31:a0bed6c1e05d 255 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 256
mazgch 57:869bd35f44cc 257 bool MDMParser::connect(
mazgch 57:869bd35f44cc 258 const char* simpin,
mazgch 80:34985b4d821e 259 const char* apn, const char* username,
mazgch 80:34985b4d821e 260 const char* password, Auth auth,
mazgch 74:208e3e32d263 261 PinName pn)
mazgch 57:869bd35f44cc 262 {
mazgch 75:ce6e12067d0c 263 bool ok = init(simpin, NULL, pn);
mazgch 74:208e3e32d263 264 #ifdef MDM_DEBUG
mazgch 75:ce6e12067d0c 265 if (_debugLevel >= 1) dumpDevStatus(&_dev);
mazgch 74:208e3e32d263 266 #endif
mazgch 75:ce6e12067d0c 267 if (!ok)
mazgch 57:869bd35f44cc 268 return false;
mazgch 75:ce6e12067d0c 269 ok = registerNet();
mazgch 74:208e3e32d263 270 #ifdef MDM_DEBUG
mazgch 75:ce6e12067d0c 271 if (_debugLevel >= 1) dumpNetStatus(&_net);
mazgch 74:208e3e32d263 272 #endif
mazgch 75:ce6e12067d0c 273 if (!ok)
mazgch 57:869bd35f44cc 274 return false;
mazgch 80:34985b4d821e 275 IP ip = join(apn,username,password,auth);
mazgch 74:208e3e32d263 276 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 277 if (_debugLevel >= 1) dumpIp(ip);
mazgch 74:208e3e32d263 278 #endif
mazgch 57:869bd35f44cc 279 if (ip == NOIP)
mazgch 57:869bd35f44cc 280 return false;
mazgch 57:869bd35f44cc 281 return true;
mazgch 57:869bd35f44cc 282 }
mazgch 57:869bd35f44cc 283
mazgch 74:208e3e32d263 284 bool MDMParser::init(const char* simpin, DevStatus* status, PinName pn)
mazgch 21:c4d64830bf02 285 {
mazgch 74:208e3e32d263 286 int i = 10;
mazgch 76:f7c3dd568dae 287 memset(&_dev, 0, sizeof(_dev));
mazgch 74:208e3e32d263 288 if (pn != NC) {
mazgch 74:208e3e32d263 289 INFO("Modem::wakeup\r\n");
mazgch 74:208e3e32d263 290 DigitalOut pin(pn, 1);
mazgch 74:208e3e32d263 291 while (i--) {
mazgch 79:291df065e345 292 // SARA-U2/LISA-U2 50..80us
mazgch 79:291df065e345 293 pin = 0; wait_us(50);
mazgch 79:291df065e345 294 pin = 1; wait_ms(10);
mazgch 79:291df065e345 295
mazgch 79:291df065e345 296 // SARA-G35 >5ms, LISA-C2 > 150ms
mazgch 79:291df065e345 297 pin = 0; wait_ms(150);
mazgch 79:291df065e345 298 pin = 1; wait_ms(100);
mazgch 79:291df065e345 299
mazgch 79:291df065e345 300 // purge any messages
mazgch 79:291df065e345 301 while (WAIT != waitFinalResp(NULL,NULL,0))
mazgch 79:291df065e345 302 /* nothing */;
mazgch 74:208e3e32d263 303 // check interface and disable local echo
mazgch 74:208e3e32d263 304 sendFormated("AT\r\n");
mazgch 74:208e3e32d263 305 if(RESP_OK == waitFinalResp(NULL,NULL,500))
mazgch 74:208e3e32d263 306 break;
mazgch 74:208e3e32d263 307 }
mazgch 75:ce6e12067d0c 308 if (i < 0) {
mazgch 75:ce6e12067d0c 309 ERROR("No Reply from Modem");
mazgch 74:208e3e32d263 310 return false;
mazgch 75:ce6e12067d0c 311 }
mazgch 21:c4d64830bf02 312 }
mazgch 76:f7c3dd568dae 313 _init = true;
mazgch 75:ce6e12067d0c 314
mazgch 74:208e3e32d263 315 INFO("Modem::init\r\n");
mazgch 21:c4d64830bf02 316 // echo off
mazgch 21:c4d64830bf02 317 sendFormated("AT E0\r\n");
mazgch 52:8071747a7cb3 318 if(RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 319 return false;
mazgch 21:c4d64830bf02 320 // enable verbose error messages
mazgch 21:c4d64830bf02 321 sendFormated("AT+CMEE=2\r\n");
mazgch 52:8071747a7cb3 322 if(RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 323 return false;
mazgch 21:c4d64830bf02 324 // set baud rate
mazgch 21:c4d64830bf02 325 sendFormated("AT+IPR=115200\r\n");
mazgch 52:8071747a7cb3 326 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 327 return false;
mazgch 74:208e3e32d263 328 RELAX_MS(40);
mazgch 21:c4d64830bf02 329 // identify the module
mazgch 21:c4d64830bf02 330 sendFormated("ATI\r\n");
mazgch 52:8071747a7cb3 331 if (RESP_OK != waitFinalResp(_cbATI, &_dev.dev))
mazgch 21:c4d64830bf02 332 return false;
mazgch 32:8f12ac182bbb 333 if (_dev.dev == DEV_UNKNOWN)
mazgch 21:c4d64830bf02 334 return false;
mazgch 32:8f12ac182bbb 335 // device specific init
mazgch 32:8f12ac182bbb 336 if (_dev.dev == DEV_LISA_C200) {
mazgch 32:8f12ac182bbb 337 // get the manufacturer
mazgch 32:8f12ac182bbb 338 sendFormated("AT+GMI\r\n");
mazgch 52:8071747a7cb3 339 if (RESP_OK != waitFinalResp(_cbString, _dev.manu))
mazgch 32:8f12ac182bbb 340 return false;
mazgch 32:8f12ac182bbb 341 // get the model identification
mazgch 32:8f12ac182bbb 342 sendFormated("AT+GMM\r\n");
mazgch 52:8071747a7cb3 343 if (RESP_OK != waitFinalResp(_cbString, _dev.model))
mazgch 32:8f12ac182bbb 344 return false;
mazgch 32:8f12ac182bbb 345 // get the sw version
mazgch 32:8f12ac182bbb 346 sendFormated("AT+GMR\r\n");
mazgch 52:8071747a7cb3 347 if (RESP_OK != waitFinalResp(_cbString, _dev.ver))
mazgch 32:8f12ac182bbb 348 return false;
mazgch 21:c4d64830bf02 349 // Return the pseudo ESN or MEID
mazgch 21:c4d64830bf02 350 sendFormated("AT+GSN\r\n");
mazgch 52:8071747a7cb3 351 if (RESP_OK != waitFinalResp(_cbString, _dev.meid))
mazgch 26:07be5faf8925 352 return false;
mazgch 50:d76aece8038f 353 #if 0
mazgch 50:d76aece8038f 354 // enable power saving
mazgch 50:d76aece8038f 355 if (_dev.lpm != LPM_DISABLED) {
mazgch 50:d76aece8038f 356 // enable power saving (requires flow control, cts at least)
mazgch 50:d76aece8038f 357 sendFormated("AT+UPSV=1,1280\r\n");
mazgch 52:8071747a7cb3 358 if (RESP_OK != waitFinalResp())
mazgch 50:d76aece8038f 359 return false;
mazgch 50:d76aece8038f 360 _dev.lpm = LPM_ACTIVE;
mazgch 50:d76aece8038f 361 }
mazgch 50:d76aece8038f 362 #endif
mazgch 26:07be5faf8925 363 } else {
mazgch 32:8f12ac182bbb 364 if (_dev.dev == DEV_LISA_U200) {
mazgch 35:9275215a3a5b 365 // enable the network identification feature
mazgch 21:c4d64830bf02 366 sendFormated("AT+UGPIOC=20,2\r\n");
mazgch 52:8071747a7cb3 367 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 368 return false;
mazgch 21:c4d64830bf02 369 } else {
mazgch 35:9275215a3a5b 370 // enable the network identification feature
mazgch 21:c4d64830bf02 371 sendFormated("AT+UGPIOC=16,2\r\n");
mazgch 52:8071747a7cb3 372 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 373 return false;
mazgch 21:c4d64830bf02 374 }
mazgch 21:c4d64830bf02 375 // check the sim card
mazgch 31:a0bed6c1e05d 376 for (int i = 0; (i < 5) && (_dev.sim != SIM_READY); i++) {
mazgch 21:c4d64830bf02 377 sendFormated("AT+CPIN?\r\n");
mazgch 31:a0bed6c1e05d 378 int ret = waitFinalResp(_cbCPIN, &_dev.sim);
mazgch 62:a02f026bdd7c 379 // having an error here is ok (sim may still be initializing)
mazgch 52:8071747a7cb3 380 if ((RESP_OK != ret) && (RESP_ERROR != ret))
mazgch 21:c4d64830bf02 381 return false;
mazgch 31:a0bed6c1e05d 382 // Enter PIN if needed
mazgch 77:55788e619858 383 if (_dev.sim == SIM_PIN) {
mazgch 57:869bd35f44cc 384 if (!simpin) {
mazgch 75:ce6e12067d0c 385 ERROR("SIM PIN not available\r\n");
mazgch 31:a0bed6c1e05d 386 return false;
mazgch 31:a0bed6c1e05d 387 }
mazgch 57:869bd35f44cc 388 sendFormated("AT+CPIN=%s\r\n", simpin);
mazgch 52:8071747a7cb3 389 if (RESP_OK != waitFinalResp(_cbCPIN, &_dev.sim))
mazgch 31:a0bed6c1e05d 390 return false;
mazgch 62:a02f026bdd7c 391 } else if (_dev.sim != SIM_READY) {
mazgch 74:208e3e32d263 392 RELAX_MS(1000);
mazgch 62:a02f026bdd7c 393 }
mazgch 21:c4d64830bf02 394 }
mazgch 77:55788e619858 395 if (_dev.sim != SIM_READY) {
mazgch 77:55788e619858 396 if (_dev.sim == SIM_MISSING)
mazgch 77:55788e619858 397 ERROR("SIM not inserted\r\n");
mazgch 21:c4d64830bf02 398 return false;
mazgch 77:55788e619858 399 }
mazgch 32:8f12ac182bbb 400 // get the manufacturer
mazgch 32:8f12ac182bbb 401 sendFormated("AT+CGMI\r\n");
mazgch 52:8071747a7cb3 402 if (RESP_OK != waitFinalResp(_cbString, _dev.manu))
mazgch 32:8f12ac182bbb 403 return false;
mazgch 32:8f12ac182bbb 404 // get the model identification
mazgch 32:8f12ac182bbb 405 sendFormated("AT+CGMM\r\n");
mazgch 52:8071747a7cb3 406 if (RESP_OK != waitFinalResp(_cbString, _dev.model))
mazgch 32:8f12ac182bbb 407 return false;
mazgch 32:8f12ac182bbb 408 // get the
mazgch 32:8f12ac182bbb 409 sendFormated("AT+CGMR\r\n");
mazgch 52:8071747a7cb3 410 if (RESP_OK != waitFinalResp(_cbString, _dev.ver))
mazgch 32:8f12ac182bbb 411 return false;
mazgch 21:c4d64830bf02 412 // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card.
mazgch 21:c4d64830bf02 413 // ICCID is a serial number identifying the SIM.
mazgch 21:c4d64830bf02 414 sendFormated("AT+CCID\r\n");
mazgch 52:8071747a7cb3 415 if (RESP_OK != waitFinalResp(_cbCCID, _dev.ccid))
mazgch 21:c4d64830bf02 416 return false;
mazgch 21:c4d64830bf02 417 // Returns the product serial number, IMEI (International Mobile Equipment Identity)
mazgch 21:c4d64830bf02 418 sendFormated("AT+CGSN\r\n");
mazgch 52:8071747a7cb3 419 if (RESP_OK != waitFinalResp(_cbString, _dev.imei))
mazgch 21:c4d64830bf02 420 return false;
mazgch 50:d76aece8038f 421 // enable power saving
mazgch 50:d76aece8038f 422 if (_dev.lpm != LPM_DISABLED) {
mazgch 50:d76aece8038f 423 // enable power saving (requires flow control, cts at least)
mazgch 50:d76aece8038f 424 sendFormated("AT+UPSV=1\r\n");
mazgch 52:8071747a7cb3 425 if (RESP_OK != waitFinalResp())
mazgch 50:d76aece8038f 426 return false;
mazgch 50:d76aece8038f 427 _dev.lpm = LPM_ACTIVE;
mazgch 50:d76aece8038f 428 }
mazgch 79:291df065e345 429 // enable the psd registration unsolicited result code
mazgch 79:291df065e345 430 sendFormated("AT+CGREG=2\r\n");
mazgch 79:291df065e345 431 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 432 return false;
mazgch 21:c4d64830bf02 433 }
mazgch 79:291df065e345 434 // enable the network registration unsolicited result code
mazgch 79:291df065e345 435 sendFormated("AT+CREG=%d\r\n", (_dev.dev == DEV_LISA_C200) ? 1 : 2);
mazgch 79:291df065e345 436 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 437 return false;
mazgch 31:a0bed6c1e05d 438 // Setup SMS in text mode
mazgch 31:a0bed6c1e05d 439 sendFormated("AT+CMGF=1\r\n");
mazgch 52:8071747a7cb3 440 if (RESP_OK != waitFinalResp())
mazgch 31:a0bed6c1e05d 441 return false;
mazgch 31:a0bed6c1e05d 442 // setup new message indication
mazgch 75:ce6e12067d0c 443 sendFormated("AT+CNMI=2,1\r\n");
mazgch 52:8071747a7cb3 444 if (RESP_OK != waitFinalResp())
mazgch 31:a0bed6c1e05d 445 return false;
mazgch 21:c4d64830bf02 446 // Request IMSI (International Mobile Subscriber Identification)
mazgch 21:c4d64830bf02 447 sendFormated("AT+CIMI\r\n");
mazgch 52:8071747a7cb3 448 if (RESP_OK != waitFinalResp(_cbString, _dev.imsi))
mazgch 21:c4d64830bf02 449 return false;
mazgch 28:4d9509e3b1cf 450 if (status)
mazgch 31:a0bed6c1e05d 451 memcpy(status, &_dev, sizeof(DevStatus));
mazgch 31:a0bed6c1e05d 452 return true;
mazgch 31:a0bed6c1e05d 453 }
mazgch 31:a0bed6c1e05d 454
mazgch 74:208e3e32d263 455 bool MDMParser::powerOff(void)
mazgch 74:208e3e32d263 456 {
mazgch 76:f7c3dd568dae 457 if (_init) {
mazgch 76:f7c3dd568dae 458 INFO("Modem::powerOff\r\n");
mazgch 76:f7c3dd568dae 459 sendFormated("AT+CPWROFF\r\n");
mazgch 76:f7c3dd568dae 460 if (RESP_OK != waitFinalResp(NULL,NULL,120*1000))
mazgch 76:f7c3dd568dae 461 return false;
mazgch 76:f7c3dd568dae 462 _init = false;
mazgch 76:f7c3dd568dae 463 }
mazgch 74:208e3e32d263 464 return true;
mazgch 74:208e3e32d263 465 }
mazgch 74:208e3e32d263 466
mazgch 32:8f12ac182bbb 467 int MDMParser::_cbATI(int type, const char* buf, int len, Dev* dev)
mazgch 31:a0bed6c1e05d 468 {
mazgch 37:cc3433329d66 469 if ((type == TYPE_UNKNOWN) && dev) {
mazgch 31:a0bed6c1e05d 470 if (strstr(buf, "SARA-G350")) {
mazgch 32:8f12ac182bbb 471 *dev = DEV_SARA_G350;
mazgch 32:8f12ac182bbb 472 /*TRACE("Identified Device: SARA-G350 2G\\n")*/;
mazgch 31:a0bed6c1e05d 473 } else if (strstr(buf, "LISA-U200")) {
mazgch 32:8f12ac182bbb 474 *dev = DEV_LISA_U200;
mazgch 32:8f12ac182bbb 475 /*TRACE("Identified Device: LISA-U200 2G/3G\r\n")*/;
mazgch 31:a0bed6c1e05d 476 } else if (strstr(buf, "LISA-C200")) {
mazgch 32:8f12ac182bbb 477 *dev= DEV_LISA_C200;
mazgch 32:8f12ac182bbb 478 /*TRACE("Identified Device: LISA-C200 CDMA\r\n")*/;
mazgch 31:a0bed6c1e05d 479 }
mazgch 28:4d9509e3b1cf 480 }
mazgch 31:a0bed6c1e05d 481 return WAIT;
mazgch 31:a0bed6c1e05d 482 }
mazgch 31:a0bed6c1e05d 483
mazgch 31:a0bed6c1e05d 484 int MDMParser::_cbCPIN(int type, const char* buf, int len, Sim* sim)
mazgch 31:a0bed6c1e05d 485 {
mazgch 75:ce6e12067d0c 486 if (sim) {
mazgch 75:ce6e12067d0c 487 if (type == TYPE_PLUS){
mazgch 75:ce6e12067d0c 488 char s[16];
mazgch 75:ce6e12067d0c 489 if (sscanf(buf, "\r\n+CPIN: %[^\r]\r\n", s) >= 1)
mazgch 75:ce6e12067d0c 490 *sim = (0 == strcmp("READY", s)) ? SIM_READY : SIM_PIN;
mazgch 75:ce6e12067d0c 491 } else if (type == TYPE_ERROR) {
mazgch 75:ce6e12067d0c 492 if (strstr(buf, "+CME ERROR: SIM not inserted"))
mazgch 75:ce6e12067d0c 493 *sim = SIM_MISSING;
mazgch 31:a0bed6c1e05d 494 }
mazgch 31:a0bed6c1e05d 495 }
mazgch 31:a0bed6c1e05d 496 return WAIT;
mazgch 21:c4d64830bf02 497 }
mazgch 21:c4d64830bf02 498
mazgch 26:07be5faf8925 499 int MDMParser::_cbCCID(int type, const char* buf, int len, char* ccid)
mazgch 26:07be5faf8925 500 {
mazgch 26:07be5faf8925 501 if ((type == TYPE_PLUS) && ccid){
mazgch 26:07be5faf8925 502 if (sscanf(buf, "\r\n+CCID: %[^\r]\r\n", ccid) == 1)
mazgch 31:a0bed6c1e05d 503 /*TRACE("Got CCID: %s\r\n", ccid)*/;
mazgch 26:07be5faf8925 504 }
mazgch 26:07be5faf8925 505 return WAIT;
mazgch 26:07be5faf8925 506 }
mazgch 26:07be5faf8925 507
mazgch 75:ce6e12067d0c 508 bool MDMParser::registerNet(NetStatus* status /*= NULL*/, int timeout_ms /*= 180000*/)
mazgch 75:ce6e12067d0c 509 {
mazgch 75:ce6e12067d0c 510 Timer timer;
mazgch 75:ce6e12067d0c 511 timer.start();
mazgch 75:ce6e12067d0c 512 INFO("Modem::register\r\n");
mazgch 75:ce6e12067d0c 513 while (!checkNetStatus(status) && !TIMEOUT(timer, timeout_ms))
mazgch 75:ce6e12067d0c 514 RELAX_MS(1000);
mazgch 79:291df065e345 515 if (_net.csd == REG_DENIED) ERROR("CSD Registration Denied\r\n");
mazgch 79:291df065e345 516 if (_net.psd == REG_DENIED) ERROR("PSD Registration Denied\r\n");
mazgch 79:291df065e345 517 return REG_OK(_net.csd) || REG_OK(_net.psd);
mazgch 75:ce6e12067d0c 518 }
mazgch 75:ce6e12067d0c 519
mazgch 28:4d9509e3b1cf 520 bool MDMParser::checkNetStatus(NetStatus* status /*= NULL*/)
mazgch 21:c4d64830bf02 521 {
mazgch 75:ce6e12067d0c 522 memset(&_net, 0, sizeof(_net));
mazgch 75:ce6e12067d0c 523 _net.lac = 0xFFFF;
mazgch 75:ce6e12067d0c 524 _net.ci = 0xFFFFFFFF;
mazgch 21:c4d64830bf02 525 // check registration
mazgch 21:c4d64830bf02 526 sendFormated("AT+CREG?\r\n");
mazgch 52:8071747a7cb3 527 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 528 return false;
mazgch 79:291df065e345 529 if (_dev.dev != DEV_LISA_C200) {
mazgch 79:291df065e345 530 // check PSD registration
mazgch 79:291df065e345 531 sendFormated("AT+CGREG?\r\n");
mazgch 79:291df065e345 532 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 533 return false;
mazgch 79:291df065e345 534 }
mazgch 79:291df065e345 535 if (REG_OK(_net.csd) || REG_OK(_net.psd))
mazgch 75:ce6e12067d0c 536 {
mazgch 75:ce6e12067d0c 537 // check modem specific status messages
mazgch 75:ce6e12067d0c 538 if (_dev.dev == DEV_LISA_C200) {
mazgch 75:ce6e12067d0c 539 sendFormated("AT+CSS?\r\n");
mazgch 75:ce6e12067d0c 540 if (RESP_OK != waitFinalResp())
mazgch 75:ce6e12067d0c 541 return false;
mazgch 75:ce6e12067d0c 542 // get the Telephone number
mazgch 75:ce6e12067d0c 543 sendFormated("AT$MDN?\r\n");
mazgch 75:ce6e12067d0c 544 if (RESP_OK != waitFinalResp(_cbString, _net.num))
mazgch 75:ce6e12067d0c 545 return false;
mazgch 75:ce6e12067d0c 546 // check if we have a Mobile Directory Number
mazgch 79:291df065e345 547 if (!*_net.num || (memcmp(_net.num, "000000", 6) == 0))
mazgch 75:ce6e12067d0c 548 return false;
mazgch 75:ce6e12067d0c 549 // get the the Network access identifier string
mazgch 75:ce6e12067d0c 550 char nai[64];
mazgch 75:ce6e12067d0c 551 sendFormated("AT$QCMIPNAI?\r\n");
mazgch 75:ce6e12067d0c 552 if (RESP_OK != waitFinalResp(_cbString, nai))
mazgch 75:ce6e12067d0c 553 return false;
mazgch 75:ce6e12067d0c 554 } else {
mazgch 75:ce6e12067d0c 555 // check operator selection
mazgch 75:ce6e12067d0c 556 sendFormated("AT+COPS?\r\n");
mazgch 75:ce6e12067d0c 557 if (RESP_OK != waitFinalResp(_cbCOPS, &_net))
mazgch 75:ce6e12067d0c 558 return false;
mazgch 75:ce6e12067d0c 559 // Returns the MSISDNs related to this subscriber
mazgch 75:ce6e12067d0c 560 sendFormated("AT+CNUM\r\n");
mazgch 75:ce6e12067d0c 561 if (RESP_OK != waitFinalResp(_cbCNUM, _net.num))
mazgch 75:ce6e12067d0c 562 return false;
mazgch 75:ce6e12067d0c 563 }
mazgch 75:ce6e12067d0c 564 // Returns the signal strength indication
mazgch 75:ce6e12067d0c 565 sendFormated("AT+CSQ\r\n");
mazgch 75:ce6e12067d0c 566 if (RESP_OK != waitFinalResp(_cbCSQ, &_net))
mazgch 32:8f12ac182bbb 567 return false;
mazgch 75:ce6e12067d0c 568 }
mazgch 28:4d9509e3b1cf 569 if (status) {
mazgch 31:a0bed6c1e05d 570 memcpy(status, &_net, sizeof(NetStatus));
mazgch 25:4045d02e44f1 571 }
mazgch 79:291df065e345 572 return REG_DONE(_net.csd) && REG_DONE(_net.psd);
mazgch 31:a0bed6c1e05d 573 }
mazgch 31:a0bed6c1e05d 574
mazgch 31:a0bed6c1e05d 575 int MDMParser::_cbCOPS(int type, const char* buf, int len, NetStatus* status)
mazgch 31:a0bed6c1e05d 576 {
mazgch 31:a0bed6c1e05d 577 if ((type == TYPE_PLUS) && status){
mazgch 31:a0bed6c1e05d 578 int act = 99;
mazgch 31:a0bed6c1e05d 579 // +COPS: <mode>[,<format>,<oper>[,<AcT>]]
mazgch 31:a0bed6c1e05d 580 if (sscanf(buf, "\r\n+COPS: %*d,%*d,\"%[^\"]\",%d",status->opr,&act) >= 1) {
mazgch 31:a0bed6c1e05d 581 if (act == 0) status->act = ACT_GSM; // 0: GSM,
mazgch 31:a0bed6c1e05d 582 else if (act == 2) status->act = ACT_UTRAN; // 2: UTRAN
mazgch 31:a0bed6c1e05d 583 }
mazgch 31:a0bed6c1e05d 584 }
mazgch 31:a0bed6c1e05d 585 return WAIT;
mazgch 31:a0bed6c1e05d 586 }
mazgch 31:a0bed6c1e05d 587
mazgch 31:a0bed6c1e05d 588 int MDMParser::_cbCNUM(int type, const char* buf, int len, char* num)
mazgch 31:a0bed6c1e05d 589 {
mazgch 31:a0bed6c1e05d 590 if ((type == TYPE_PLUS) && num){
mazgch 31:a0bed6c1e05d 591 int a;
mazgch 31:a0bed6c1e05d 592 if ((sscanf(buf, "\r\n+CNUM: \"My Number\",\"%31[^\"]\",%d", num, &a) == 2) &&
mazgch 31:a0bed6c1e05d 593 ((a == 129) || (a == 145))) {
mazgch 31:a0bed6c1e05d 594 }
mazgch 31:a0bed6c1e05d 595 }
mazgch 31:a0bed6c1e05d 596 return WAIT;
mazgch 31:a0bed6c1e05d 597 }
mazgch 31:a0bed6c1e05d 598
mazgch 54:7ba8e4c218e2 599 int MDMParser::_cbCSQ(int type, const char* buf, int len, NetStatus* status)
mazgch 31:a0bed6c1e05d 600 {
mazgch 54:7ba8e4c218e2 601 if ((type == TYPE_PLUS) && status){
mazgch 54:7ba8e4c218e2 602 int a,b;
mazgch 54:7ba8e4c218e2 603 char _ber[] = { 49, 43, 37, 25, 19, 13, 7, 0 }; // see 3GPP TS 45.008 [20] subclause 8.2.4
mazgch 31:a0bed6c1e05d 604 // +CSQ: <rssi>,<qual>
mazgch 54:7ba8e4c218e2 605 if (sscanf(buf, "\r\n+CSQ: %d,%d",&a,&b) == 2) {
mazgch 54:7ba8e4c218e2 606 if (a != 99) status->rssi = -113 + 2*a; // 0: -113 1: -111 ... 30: -53 dBm with 2 dBm steps
mazgch 54:7ba8e4c218e2 607 if ((b != 99) && (b < sizeof(_ber))) status->ber = _ber[b]; //
mazgch 31:a0bed6c1e05d 608 }
mazgch 31:a0bed6c1e05d 609 }
mazgch 31:a0bed6c1e05d 610 return WAIT;
mazgch 31:a0bed6c1e05d 611 }
mazgch 21:c4d64830bf02 612
mazgch 21:c4d64830bf02 613 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 614 // internet connection
mazgch 21:c4d64830bf02 615
mazgch 79:291df065e345 616 MDMParser::IP MDMParser::join(const char* apn /*= NULL*/, const char* username /*= NULL*/,
mazgch 79:291df065e345 617 const char* password /*= NULL*/, Auth auth /*= AUTH_DETECT*/)
mazgch 21:c4d64830bf02 618 {
mazgch 78:caf0014375cb 619 INFO("Modem::join\r\n");
mazgch 32:8f12ac182bbb 620 _ip = NOIP;
mazgch 32:8f12ac182bbb 621 if (_dev.dev == DEV_LISA_C200) {
mazgch 76:f7c3dd568dae 622 // make a dumy dns lookup (which will fail, so ignore the result)
mazgch 76:f7c3dd568dae 623 sendFormated("AT+UDNSRN=0,\"u-blox.com\"\r\n");
mazgch 76:f7c3dd568dae 624 waitFinalResp();
mazgch 76:f7c3dd568dae 625 // This fake lookup will enable the IP connection and we
mazgch 76:f7c3dd568dae 626 // should have an IP after this, so we check it
mazgch 76:f7c3dd568dae 627
mazgch 21:c4d64830bf02 628 //Get local IP address
mazgch 21:c4d64830bf02 629 sendFormated("AT+CMIP?\r\n");
mazgch 52:8071747a7cb3 630 if (RESP_OK != waitFinalResp(_cbCMIP, &_ip))
mazgch 31:a0bed6c1e05d 631 return NOIP;
mazgch 21:c4d64830bf02 632 } else {
mazgch 21:c4d64830bf02 633 // check gprs attach status
mazgch 72:d1e943ad6558 634 sendFormated("AT+CGATT=1\r\n");
mazgch 74:208e3e32d263 635 if (RESP_OK != waitFinalResp(NULL,NULL,3*60*1000))
mazgch 31:a0bed6c1e05d 636 return NOIP;
mazgch 31:a0bed6c1e05d 637
mazgch 31:a0bed6c1e05d 638 // Check the profile
mazgch 31:a0bed6c1e05d 639 int a = 0;
mazgch 79:291df065e345 640 bool force = true;
mazgch 31:a0bed6c1e05d 641 sendFormated("AT+UPSND=" PROFILE ",8\r\n");
mazgch 52:8071747a7cb3 642 if (RESP_OK != waitFinalResp(_cbUPSND, &a))
mazgch 31:a0bed6c1e05d 643 return NOIP;
mazgch 79:291df065e345 644 if (a == 1 && force) {
mazgch 31:a0bed6c1e05d 645 // disconnect the profile already if it is connected
mazgch 31:a0bed6c1e05d 646 sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
mazgch 58:e38a2e942fbb 647 if (RESP_OK != waitFinalResp(NULL,NULL,40*1000))
mazgch 31:a0bed6c1e05d 648 return NOIP;;
mazgch 79:291df065e345 649 a = 0;
mazgch 31:a0bed6c1e05d 650 }
mazgch 79:291df065e345 651 if (a == 0) {
mazgch 79:291df065e345 652 // Set up the APN
mazgch 79:291df065e345 653 if (apn) {
mazgch 79:291df065e345 654 sendFormated("AT+UPSD=" PROFILE ",1,\"%s\"\r\n", apn);
mazgch 79:291df065e345 655 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 656 return NOIP;
mazgch 79:291df065e345 657 }
mazgch 79:291df065e345 658 if (username) {
mazgch 79:291df065e345 659 sendFormated("AT+UPSD=" PROFILE ",2,\"%s\"\r\n", username);
mazgch 79:291df065e345 660 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 661 return NOIP;
mazgch 79:291df065e345 662 }
mazgch 79:291df065e345 663 if (password) {
mazgch 79:291df065e345 664 sendFormated("AT+UPSD=" PROFILE ",3,\"%s\"\r\n", password);
mazgch 79:291df065e345 665 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 666 return NOIP;
mazgch 79:291df065e345 667 }
mazgch 79:291df065e345 668 // Set up the dynamic IP address assignment.
mazgch 79:291df065e345 669 sendFormated("AT+UPSD=" PROFILE ",7,\"0.0.0.0\"\r\n");
mazgch 52:8071747a7cb3 670 if (RESP_OK != waitFinalResp())
mazgch 31:a0bed6c1e05d 671 return NOIP;
mazgch 79:291df065e345 672 // try different Authentication Protocols
mazgch 79:291df065e345 673 // 0 = none
mazgch 79:291df065e345 674 // 1 = PAP (Password Authentication Protocol)
mazgch 79:291df065e345 675 // 2 = CHAP (Challenge Handshake Authentication Protocol)
mazgch 79:291df065e345 676 int i = AUTH_NONE;
mazgch 79:291df065e345 677 while (i <= AUTH_CHAP)
mazgch 79:291df065e345 678 {
mazgch 79:291df065e345 679 if ((auth == AUTH_DETECT) || (auth == i)) {
mazgch 79:291df065e345 680 // Set up the dynamic IP address assignment.
mazgch 79:291df065e345 681 sendFormated("AT+UPSD=" PROFILE ",6,%d\r\n", i);
mazgch 79:291df065e345 682 if (RESP_OK != waitFinalResp())
mazgch 79:291df065e345 683 return NOIP;
mazgch 79:291df065e345 684 // Activate the profile and make connection
mazgch 79:291df065e345 685 sendFormated("AT+UPSDA=" PROFILE ",3\r\n");
mazgch 79:291df065e345 686 if (RESP_OK == waitFinalResp(NULL,NULL,150*1000))
mazgch 79:291df065e345 687 break;
mazgch 79:291df065e345 688 }
mazgch 79:291df065e345 689 i ++;
mazgch 79:291df065e345 690 }
mazgch 79:291df065e345 691 if (i > AUTH_CHAP) {
mazgch 79:291df065e345 692 ERROR("Your modem APN/password/username may be wrong\r\n");
mazgch 31:a0bed6c1e05d 693 return NOIP;
mazgch 79:291df065e345 694 }
mazgch 74:208e3e32d263 695 }
mazgch 21:c4d64830bf02 696 //Get local IP address
mazgch 21:c4d64830bf02 697 sendFormated("AT+UPSND=" PROFILE ",0\r\n");
mazgch 52:8071747a7cb3 698 if (RESP_OK != waitFinalResp(_cbUPSND, &_ip))
mazgch 31:a0bed6c1e05d 699 return NOIP;
mazgch 31:a0bed6c1e05d 700 }
mazgch 32:8f12ac182bbb 701 return _ip;
mazgch 31:a0bed6c1e05d 702 }
mazgch 31:a0bed6c1e05d 703
mazgch 32:8f12ac182bbb 704 int MDMParser::_cbCMIP(int type, const char* buf, int len, IP* ip)
mazgch 32:8f12ac182bbb 705 {
mazgch 32:8f12ac182bbb 706 if ((type == TYPE_PLUS) && ip) {
mazgch 32:8f12ac182bbb 707 int a,b,c,d;
mazgch 32:8f12ac182bbb 708 if (sscanf(buf, "\r\n+CMIP: " IPSTR, &a,&b,&c,&d) == 4)
mazgch 32:8f12ac182bbb 709 *ip = IPADR(a,b,c,d);
mazgch 32:8f12ac182bbb 710 }
mazgch 32:8f12ac182bbb 711 return WAIT;
mazgch 32:8f12ac182bbb 712 }
mazgch 32:8f12ac182bbb 713
mazgch 31:a0bed6c1e05d 714 int MDMParser::_cbUPSND(int type, const char* buf, int len, int* act)
mazgch 31:a0bed6c1e05d 715 {
mazgch 31:a0bed6c1e05d 716 if ((type == TYPE_PLUS) && act) {
mazgch 31:a0bed6c1e05d 717 if (sscanf(buf, "\r\n+UPSND: %*d,%*d,%d", act) == 1)
mazgch 31:a0bed6c1e05d 718 /*nothing*/;
mazgch 21:c4d64830bf02 719 }
mazgch 31:a0bed6c1e05d 720 return WAIT;
mazgch 31:a0bed6c1e05d 721 }
mazgch 31:a0bed6c1e05d 722
mazgch 31:a0bed6c1e05d 723 int MDMParser::_cbUPSND(int type, const char* buf, int len, IP* ip)
mazgch 31:a0bed6c1e05d 724 {
mazgch 31:a0bed6c1e05d 725 if ((type == TYPE_PLUS) && ip) {
mazgch 31:a0bed6c1e05d 726 int a,b,c,d;
mazgch 31:a0bed6c1e05d 727 // +UPSND=<profile_id>,<param_tag>[,<dynamic_param_val>]
mazgch 31:a0bed6c1e05d 728 if (sscanf(buf, "\r\n+UPSND: " PROFILE ",0,\"" IPSTR "\"", &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 729 *ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 730 }
mazgch 31:a0bed6c1e05d 731 return WAIT;
mazgch 31:a0bed6c1e05d 732 }
mazgch 31:a0bed6c1e05d 733
mazgch 31:a0bed6c1e05d 734 int MDMParser::_cbUDNSRN(int type, const char* buf, int len, IP* ip)
mazgch 31:a0bed6c1e05d 735 {
mazgch 31:a0bed6c1e05d 736 if ((type == TYPE_PLUS) && ip) {
mazgch 31:a0bed6c1e05d 737 int a,b,c,d;
mazgch 31:a0bed6c1e05d 738 if (sscanf(buf, "\r\n+UDNSRN: \""IPSTR"\"", &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 739 *ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 740 }
mazgch 31:a0bed6c1e05d 741 return WAIT;
mazgch 21:c4d64830bf02 742 }
mazgch 21:c4d64830bf02 743
mazgch 21:c4d64830bf02 744 bool MDMParser::disconnect(void)
mazgch 21:c4d64830bf02 745 {
mazgch 31:a0bed6c1e05d 746 if (_ip == NOIP)
mazgch 21:c4d64830bf02 747 return true;
mazgch 74:208e3e32d263 748 INFO("Modem::disconnect\r\n");
mazgch 32:8f12ac182bbb 749 if (_dev.dev == DEV_LISA_C200) {
mazgch 76:f7c3dd568dae 750 // There something to do here
mazgch 21:c4d64830bf02 751 } else {
mazgch 21:c4d64830bf02 752 sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
mazgch 52:8071747a7cb3 753 if (RESP_OK != waitFinalResp())
mazgch 32:8f12ac182bbb 754 return false;
mazgch 21:c4d64830bf02 755 }
mazgch 31:a0bed6c1e05d 756 _ip = NOIP;
mazgch 21:c4d64830bf02 757 return true;
mazgch 21:c4d64830bf02 758 }
mazgch 21:c4d64830bf02 759
mazgch 31:a0bed6c1e05d 760 MDMParser::IP MDMParser::gethostbyname(const char* host)
mazgch 21:c4d64830bf02 761 {
mazgch 31:a0bed6c1e05d 762 IP ip = NOIP;
mazgch 31:a0bed6c1e05d 763 int a,b,c,d;
mazgch 31:a0bed6c1e05d 764 if (sscanf(host, IPSTR, &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 765 ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 766 else {
mazgch 31:a0bed6c1e05d 767 sendFormated("AT+UDNSRN=0,\"%s\"\r\n", host);
mazgch 52:8071747a7cb3 768 if (RESP_OK != waitFinalResp(_cbUDNSRN, &ip))
mazgch 31:a0bed6c1e05d 769 return false;
mazgch 21:c4d64830bf02 770 }
mazgch 31:a0bed6c1e05d 771 return ip;
mazgch 21:c4d64830bf02 772 }
mazgch 21:c4d64830bf02 773
mazgch 21:c4d64830bf02 774 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 775 // sockets
mazgch 21:c4d64830bf02 776
mazgch 21:c4d64830bf02 777 int MDMParser::_cbUSOCR(int type, const char* buf, int len, int* socket)
mazgch 21:c4d64830bf02 778 {
mazgch 21:c4d64830bf02 779 if ((type == TYPE_PLUS) && socket) {
mazgch 21:c4d64830bf02 780 const char* p = strstr(buf,"+USOCR: ");
mazgch 21:c4d64830bf02 781 if (p)
mazgch 21:c4d64830bf02 782 *socket = atoi(p+8);
mazgch 21:c4d64830bf02 783 }
mazgch 21:c4d64830bf02 784 return WAIT;
mazgch 21:c4d64830bf02 785 }
mazgch 21:c4d64830bf02 786
mazgch 63:42cb563a25bc 787 int MDMParser::socketSocket(IpProtocol ipproto, int port)
mazgch 21:c4d64830bf02 788 {
mazgch 47:9a89e5195721 789 TRACE("socketSocket(%d)\r\n", ipproto);
mazgch 21:c4d64830bf02 790 if(ipproto == IPPROTO_TCP) {
mazgch 63:42cb563a25bc 791 sendFormated("AT+USOCR=6\r\n");
mazgch 63:42cb563a25bc 792 } else if ((ipproto == IPPROTO_UDP) && (port == -1)){
mazgch 63:42cb563a25bc 793 sendFormated("AT+USOCR=17\r\n");
mazgch 63:42cb563a25bc 794 } else if (ipproto == IPPROTO_UDP){
mazgch 63:42cb563a25bc 795 sendFormated("AT+USOCR=17,%d\r\n", port);
mazgch 21:c4d64830bf02 796 } else { // other types not supported
mazgch 21:c4d64830bf02 797 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 798 }
mazgch 21:c4d64830bf02 799 int socket = -1;
mazgch 52:8071747a7cb3 800 if (RESP_OK != waitFinalResp(_cbUSOCR, &socket))
mazgch 21:c4d64830bf02 801 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 802 if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_FREE))
mazgch 21:c4d64830bf02 803 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 804 // successfull
mazgch 21:c4d64830bf02 805 _sockets[socket].state = SOCK_CREATED;
mazgch 21:c4d64830bf02 806 _sockets[socket].pending = 0;
mazgch 66:69072b3c5bca 807 _sockets[socket].timeout_ms = TIMEOUT_BLOCKING;
mazgch 21:c4d64830bf02 808 return socket;
mazgch 21:c4d64830bf02 809 }
mazgch 21:c4d64830bf02 810
mazgch 21:c4d64830bf02 811 bool MDMParser::socketConnect(int socket, const char * host, int port)
mazgch 21:c4d64830bf02 812 {
mazgch 47:9a89e5195721 813 TRACE("socketConnect(%d,%s,%d)\r\n", socket, host,port);
mazgch 31:a0bed6c1e05d 814 IP ip = gethostbyname(host);
mazgch 31:a0bed6c1e05d 815 if (ip == NOIP)
mazgch 21:c4d64830bf02 816 return false;
mazgch 21:c4d64830bf02 817 // connect to socket
mazgch 21:c4d64830bf02 818 if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_CREATED))
mazgch 21:c4d64830bf02 819 return false;
mazgch 21:c4d64830bf02 820 sendFormated("AT+USOCO=%d,\"" IPSTR "\",%d\r\n", socket, IPNUM(ip), port);
mazgch 52:8071747a7cb3 821 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 822 return false;
mazgch 21:c4d64830bf02 823 _sockets[socket].state = SOCK_CONNECTED;
mazgch 21:c4d64830bf02 824 return true;
mazgch 21:c4d64830bf02 825 }
mazgch 21:c4d64830bf02 826
mazgch 44:9d12223b78ff 827 bool MDMParser::socketIsConnected(int socket)
mazgch 44:9d12223b78ff 828 {
mazgch 47:9a89e5195721 829 TRACE("socketIsConnected(%d)\r\n", socket);
mazgch 47:9a89e5195721 830 if (!ISSOCKET(socket))
mazgch 47:9a89e5195721 831 return false;
mazgch 47:9a89e5195721 832 return _sockets[socket].state == SOCK_CONNECTED;
mazgch 44:9d12223b78ff 833 }
mazgch 44:9d12223b78ff 834
mazgch 66:69072b3c5bca 835 bool MDMParser::socketSetBlocking(int socket, int timeout_ms)
mazgch 44:9d12223b78ff 836 {
mazgch 47:9a89e5195721 837 TRACE("socketSetBlocking(%d,%d)\r\n", socket, timeout_ms);
mazgch 44:9d12223b78ff 838 if (!ISSOCKET(socket))
mazgch 44:9d12223b78ff 839 return false;
mazgch 44:9d12223b78ff 840 _sockets[socket].timeout_ms = timeout_ms;
mazgch 44:9d12223b78ff 841 return true;
mazgch 44:9d12223b78ff 842 }
mazgch 44:9d12223b78ff 843
mazgch 21:c4d64830bf02 844 bool MDMParser::socketClose(int socket)
mazgch 21:c4d64830bf02 845 {
mazgch 47:9a89e5195721 846 TRACE("socketClose(%d)\r\n", socket);
mazgch 21:c4d64830bf02 847 if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_CONNECTED))
mazgch 21:c4d64830bf02 848 return false;
mazgch 21:c4d64830bf02 849 sendFormated("AT+USOCL=%d\r\n", socket);
mazgch 52:8071747a7cb3 850 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 851 return false;
mazgch 44:9d12223b78ff 852 _sockets[socket].state = SOCK_CREATED;
mazgch 21:c4d64830bf02 853 return true;
mazgch 21:c4d64830bf02 854 }
mazgch 21:c4d64830bf02 855
mazgch 21:c4d64830bf02 856 bool MDMParser::socketFree(int socket)
mazgch 21:c4d64830bf02 857 {
mazgch 47:9a89e5195721 858 TRACE("socketFree(%d)\r\n", socket);
mazgch 21:c4d64830bf02 859 socketClose(socket);
mazgch 21:c4d64830bf02 860 if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_CREATED))
mazgch 21:c4d64830bf02 861 return false;
mazgch 21:c4d64830bf02 862 _sockets[socket].state = SOCK_FREE;
mazgch 21:c4d64830bf02 863 return true;
mazgch 21:c4d64830bf02 864 }
mazgch 21:c4d64830bf02 865
mazgch 69:4d6fa520dfca 866 #define USO_MAX_WRITE 1024 //!< maximum number of bytes to write to socket
mazgch 69:4d6fa520dfca 867
mazgch 21:c4d64830bf02 868 int MDMParser::socketSend(int socket, const char * buf, int len)
mazgch 21:c4d64830bf02 869 {
mazgch 47:9a89e5195721 870 TRACE("socketSend(%d,,%d)\r\n", socket,len);
mazgch 68:33a96cf64986 871 int cnt = len;
mazgch 68:33a96cf64986 872 while (cnt > 0) {
mazgch 69:4d6fa520dfca 873 int blk = USO_MAX_WRITE;
mazgch 68:33a96cf64986 874 if (cnt < blk)
mazgch 68:33a96cf64986 875 blk = cnt;
mazgch 68:33a96cf64986 876 sendFormated("AT+USOWR=%d,%d\r\n",socket,blk);
mazgch 52:8071747a7cb3 877 if (RESP_PROMPT != waitFinalResp())
mazgch 21:c4d64830bf02 878 return SOCKET_ERROR;
mazgch 74:208e3e32d263 879 RELAX_MS(50);
mazgch 68:33a96cf64986 880 send(buf, blk);
mazgch 52:8071747a7cb3 881 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 882 return SOCKET_ERROR;
mazgch 68:33a96cf64986 883 buf += blk;
mazgch 68:33a96cf64986 884 cnt -= blk;
mazgch 21:c4d64830bf02 885 }
mazgch 68:33a96cf64986 886 return (len - cnt);
mazgch 21:c4d64830bf02 887 }
mazgch 21:c4d64830bf02 888
mazgch 21:c4d64830bf02 889 int MDMParser::socketSendTo(int socket, IP ip, int port, const char * buf, int len)
mazgch 21:c4d64830bf02 890 {
mazgch 65:dd94f920a762 891 TRACE("socketSendTo(%d," IPSTR ",%d,,%d)\r\n", socket, IPNUM(ip),port,len);
mazgch 68:33a96cf64986 892 int cnt = len;
mazgch 68:33a96cf64986 893 while (cnt > 0) {
mazgch 69:4d6fa520dfca 894 int blk = USO_MAX_WRITE;
mazgch 68:33a96cf64986 895 if (cnt < blk)
mazgch 68:33a96cf64986 896 blk = cnt;
mazgch 68:33a96cf64986 897 sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",socket,IPNUM(ip),port,blk);
mazgch 52:8071747a7cb3 898 if (RESP_PROMPT != waitFinalResp())
mazgch 21:c4d64830bf02 899 return SOCKET_ERROR;
mazgch 74:208e3e32d263 900 RELAX_MS(50);
mazgch 68:33a96cf64986 901 send(buf, blk);
mazgch 63:42cb563a25bc 902 if (RESP_OK != waitFinalResp())
mazgch 21:c4d64830bf02 903 return SOCKET_ERROR;
mazgch 68:33a96cf64986 904 buf += blk;
mazgch 68:33a96cf64986 905 cnt -= blk;
mazgch 21:c4d64830bf02 906 }
mazgch 68:33a96cf64986 907 return (len - cnt);
mazgch 21:c4d64830bf02 908 }
mazgch 21:c4d64830bf02 909
mazgch 21:c4d64830bf02 910 int MDMParser::socketReadable(int socket)
mazgch 21:c4d64830bf02 911 {
mazgch 47:9a89e5195721 912 TRACE("socketReadable(%d)\r\n", socket);
mazgch 21:c4d64830bf02 913 if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_CONNECTED))
mazgch 21:c4d64830bf02 914 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 915 // allow to receive unsolicited commands
mazgch 21:c4d64830bf02 916 waitFinalResp(NULL, NULL, 0);
mazgch 21:c4d64830bf02 917 if (_sockets[socket].state != SOCK_CONNECTED)
mazgch 21:c4d64830bf02 918 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 919 return _sockets[socket].pending;
mazgch 21:c4d64830bf02 920 }
mazgch 21:c4d64830bf02 921
mazgch 21:c4d64830bf02 922 int MDMParser::_cbUSORD(int type, const char* buf, int len, char* out)
mazgch 21:c4d64830bf02 923 {
mazgch 21:c4d64830bf02 924 if ((type == TYPE_PLUS) && out) {
mazgch 21:c4d64830bf02 925 int sz, sk;
mazgch 21:c4d64830bf02 926 if ((sscanf(buf, "\r\n+USORD: %d,%d,", &sk, &sz) == 2) &&
mazgch 21:c4d64830bf02 927 (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 21:c4d64830bf02 928 memcpy(out, &buf[len-1-sz], sz);
mazgch 21:c4d64830bf02 929 }
mazgch 21:c4d64830bf02 930 }
mazgch 21:c4d64830bf02 931 return WAIT;
mazgch 21:c4d64830bf02 932 }
mazgch 21:c4d64830bf02 933
mazgch 21:c4d64830bf02 934 int MDMParser::socketRecv(int socket, char* buf, int len)
mazgch 21:c4d64830bf02 935 {
mazgch 21:c4d64830bf02 936 int cnt = 0;
mazgch 47:9a89e5195721 937 TRACE("socketRecv(%d,,%d)\r\n", socket, len);
mazgch 21:c4d64830bf02 938 if (!ISSOCKET(socket))
mazgch 21:c4d64830bf02 939 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 940 memset(buf, '\0', len);
mazgch 44:9d12223b78ff 941 Timer timer;
mazgch 44:9d12223b78ff 942 timer.start();
mazgch 21:c4d64830bf02 943 while (len) {
mazgch 69:4d6fa520dfca 944 int blk = MAX_SIZE; // still need space for headers and unsolicited commands
mazgch 21:c4d64830bf02 945 if (_sockets[socket].pending < blk)
mazgch 21:c4d64830bf02 946 blk = _sockets[socket].pending;
mazgch 21:c4d64830bf02 947 if (len < blk) blk = len;
mazgch 21:c4d64830bf02 948 if (blk) {
mazgch 44:9d12223b78ff 949 sendFormated("AT+USORD=%d,%d\r\n",socket, blk);
mazgch 52:8071747a7cb3 950 if (RESP_OK != waitFinalResp(_cbUSORD, buf)) {
mazgch 44:9d12223b78ff 951 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 952 }
mazgch 21:c4d64830bf02 953 len -= blk;
mazgch 21:c4d64830bf02 954 cnt += blk;
mazgch 21:c4d64830bf02 955 buf += blk;
mazgch 21:c4d64830bf02 956 _sockets[socket].pending -= blk;
mazgch 75:ce6e12067d0c 957 } else if ((_sockets[socket].state == SOCK_CONNECTED) &&
mazgch 75:ce6e12067d0c 958 !TIMEOUT(timer, _sockets[socket].timeout_ms)) {
mazgch 21:c4d64830bf02 959 // allow to receive unsolicited commands
mazgch 21:c4d64830bf02 960 waitFinalResp(NULL, NULL, 10);
mazgch 44:9d12223b78ff 961 } else {
mazgch 44:9d12223b78ff 962 len = 0; // no more data and socket closed or timed-out
mazgch 21:c4d64830bf02 963 }
mazgch 21:c4d64830bf02 964 }
mazgch 21:c4d64830bf02 965 return cnt;
mazgch 21:c4d64830bf02 966 }
mazgch 21:c4d64830bf02 967
mazgch 21:c4d64830bf02 968 int MDMParser::_cbUSORF(int type, const char* buf, int len, USORFparam* param)
mazgch 21:c4d64830bf02 969 {
mazgch 21:c4d64830bf02 970 if ((type == TYPE_PLUS) && param) {
mazgch 21:c4d64830bf02 971 int sz, sk, p, a,b,c,d;
mazgch 21:c4d64830bf02 972 if ((sscanf(buf, "\r\n+USORF: %d,\""IPSTR"\",%d,%d,",
mazgch 21:c4d64830bf02 973 &sk,&a,&b,&c,&d,&p,&sz) == 7) &&
mazgch 21:c4d64830bf02 974 (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 21:c4d64830bf02 975 memcpy(param->buf, &buf[len-1-sz], sz);
mazgch 21:c4d64830bf02 976 param->ip = IPADR(a,b,c,d);
mazgch 21:c4d64830bf02 977 param->port = p;
mazgch 21:c4d64830bf02 978 }
mazgch 21:c4d64830bf02 979 }
mazgch 21:c4d64830bf02 980 return WAIT;
mazgch 21:c4d64830bf02 981 }
mazgch 21:c4d64830bf02 982
mazgch 63:42cb563a25bc 983 int MDMParser::socketRecvFrom(int socket, IP* ip, int* port, char* buf, int len)
mazgch 21:c4d64830bf02 984 {
mazgch 21:c4d64830bf02 985 int cnt = 0;
mazgch 63:42cb563a25bc 986 TRACE("socketRecvFrom(%d,,%d)\r\n", socket, len);
mazgch 21:c4d64830bf02 987 if (!ISSOCKET(socket))
mazgch 21:c4d64830bf02 988 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 989 memset(buf, '\0', len);
mazgch 44:9d12223b78ff 990 Timer timer;
mazgch 44:9d12223b78ff 991 timer.start();
mazgch 21:c4d64830bf02 992 while (len) {
mazgch 69:4d6fa520dfca 993 int blk = MAX_SIZE; // still need space for headers and unsolicited commands
mazgch 21:c4d64830bf02 994 if (_sockets[socket].pending < blk)
mazgch 21:c4d64830bf02 995 blk = _sockets[socket].pending;
mazgch 21:c4d64830bf02 996 if (len < blk) blk = len;
mazgch 21:c4d64830bf02 997 if (blk) {
mazgch 21:c4d64830bf02 998 sendFormated("AT+USORF=%d,%d\r\n",socket, blk);
mazgch 21:c4d64830bf02 999 USORFparam param;
mazgch 21:c4d64830bf02 1000 param.buf = buf;
mazgch 52:8071747a7cb3 1001 if (RESP_OK != waitFinalResp(_cbUSORF, &param)) {
mazgch 44:9d12223b78ff 1002 return SOCKET_ERROR;
mazgch 21:c4d64830bf02 1003 }
mazgch 21:c4d64830bf02 1004 *ip = param.ip;
mazgch 63:42cb563a25bc 1005 *port = param.port;
mazgch 21:c4d64830bf02 1006 len -= blk;
mazgch 21:c4d64830bf02 1007 cnt += blk;
mazgch 21:c4d64830bf02 1008 buf += blk;
mazgch 21:c4d64830bf02 1009 _sockets[socket].pending -= blk;
mazgch 75:ce6e12067d0c 1010 } else if (!TIMEOUT(timer, _sockets[socket].timeout_ms)) {
mazgch 21:c4d64830bf02 1011 // allow to receive unsolicited commands
mazgch 21:c4d64830bf02 1012 waitFinalResp(NULL, NULL, 10);
mazgch 75:ce6e12067d0c 1013 } else
mazgch 44:9d12223b78ff 1014 len = 0; // no more data and socket closed or timed-out
mazgch 21:c4d64830bf02 1015 }
mazgch 44:9d12223b78ff 1016 timer.stop();
mazgch 44:9d12223b78ff 1017 timer.reset();
mazgch 21:c4d64830bf02 1018 return cnt;
mazgch 21:c4d64830bf02 1019 }
mazgch 21:c4d64830bf02 1020
mazgch 21:c4d64830bf02 1021 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 1022
mazgch 31:a0bed6c1e05d 1023 int MDMParser::_cbCMGL(int type, const char* buf, int len, CMGLparam* param)
mazgch 21:c4d64830bf02 1024 {
mazgch 31:a0bed6c1e05d 1025 if ((type == TYPE_PLUS) && param && param->num) {
mazgch 31:a0bed6c1e05d 1026 // +CMGL: <ix>,...
mazgch 31:a0bed6c1e05d 1027 int ix;
mazgch 31:a0bed6c1e05d 1028 if (sscanf(buf, "\r\n+CMGL: %d,", &ix) == 1)
mazgch 31:a0bed6c1e05d 1029 {
mazgch 31:a0bed6c1e05d 1030 *param->ix++ = ix;
mazgch 31:a0bed6c1e05d 1031 param->num--;
mazgch 31:a0bed6c1e05d 1032 }
mazgch 29:53d346010624 1033 }
mazgch 29:53d346010624 1034 return WAIT;
mazgch 21:c4d64830bf02 1035 }
mazgch 21:c4d64830bf02 1036
mazgch 31:a0bed6c1e05d 1037 int MDMParser::smsList(const char* stat /*= "ALL"*/, int* ix /*=NULL*/, int num /*= 0*/) {
mazgch 31:a0bed6c1e05d 1038 sendFormated("AT+CMGL=\"%s\"\r\n", stat);
mazgch 31:a0bed6c1e05d 1039 CMGLparam param;
mazgch 31:a0bed6c1e05d 1040 param.ix = ix;
mazgch 31:a0bed6c1e05d 1041 param.num = num;
mazgch 52:8071747a7cb3 1042 if (RESP_OK != waitFinalResp(_cbCMGL, &param))
mazgch 31:a0bed6c1e05d 1043 return -1;
mazgch 31:a0bed6c1e05d 1044 return num - param.num;
mazgch 21:c4d64830bf02 1045 }
mazgch 21:c4d64830bf02 1046
mazgch 21:c4d64830bf02 1047 bool MDMParser::smsSend(const char* num, const char* buf)
mazgch 21:c4d64830bf02 1048 {
mazgch 80:34985b4d821e 1049 sendFormated("AT+CMGS=\"%s\"\r\n",num);
mazgch 58:e38a2e942fbb 1050 if (RESP_PROMPT != waitFinalResp(NULL,NULL,150*1000)) {
mazgch 21:c4d64830bf02 1051 return false;
mazgch 21:c4d64830bf02 1052 }
mazgch 21:c4d64830bf02 1053 send(buf, strlen(buf));
mazgch 21:c4d64830bf02 1054 const char ctrlZ = 0x1A;
mazgch 21:c4d64830bf02 1055 send(&ctrlZ, sizeof(ctrlZ));
mazgch 52:8071747a7cb3 1056 if (RESP_OK != waitFinalResp()) {
mazgch 21:c4d64830bf02 1057 return false;
mazgch 21:c4d64830bf02 1058 }
mazgch 21:c4d64830bf02 1059 return true;
mazgch 21:c4d64830bf02 1060 }
mazgch 21:c4d64830bf02 1061
mazgch 21:c4d64830bf02 1062 bool MDMParser::smsDelete(int ix)
mazgch 21:c4d64830bf02 1063 {
mazgch 21:c4d64830bf02 1064 sendFormated("AT+CMGD=%d\r\n",ix);
mazgch 52:8071747a7cb3 1065 if (RESP_OK != waitFinalResp()) {
mazgch 21:c4d64830bf02 1066 return false;
mazgch 21:c4d64830bf02 1067 }
mazgch 21:c4d64830bf02 1068 return true;
mazgch 21:c4d64830bf02 1069 }
mazgch 21:c4d64830bf02 1070
mazgch 21:c4d64830bf02 1071 int MDMParser::_cbCMGR(int type, const char* buf, int len, CMGRparam* param)
mazgch 21:c4d64830bf02 1072 {
mazgch 21:c4d64830bf02 1073 if (param) {
mazgch 21:c4d64830bf02 1074 if (type == TYPE_PLUS) {
mazgch 21:c4d64830bf02 1075 if (sscanf(buf, "\r\n+CMGR: \"%*[^\"]\",\"%[^\"]", param->num) == 1) {
mazgch 21:c4d64830bf02 1076 }
mazgch 37:cc3433329d66 1077 } else if ((type == TYPE_UNKNOWN) && (buf[len-2] == '\r') && (buf[len-1] == '\n')) {
mazgch 21:c4d64830bf02 1078 memcpy(param->buf, buf, len-2);
mazgch 21:c4d64830bf02 1079 param->buf[len-2] = '\0';
mazgch 21:c4d64830bf02 1080 }
mazgch 21:c4d64830bf02 1081 }
mazgch 21:c4d64830bf02 1082 return WAIT;
mazgch 21:c4d64830bf02 1083 }
mazgch 21:c4d64830bf02 1084
mazgch 21:c4d64830bf02 1085 bool MDMParser::smsRead(int ix, char* num, char* buf, int len)
mazgch 21:c4d64830bf02 1086 {
mazgch 21:c4d64830bf02 1087 CMGRparam param;
mazgch 21:c4d64830bf02 1088 param.num = num;
mazgch 21:c4d64830bf02 1089 param.buf = buf;
mazgch 21:c4d64830bf02 1090 sendFormated("AT+CMGR=%d\r\n",ix);
mazgch 52:8071747a7cb3 1091 if (RESP_OK != waitFinalResp(_cbCMGR, &param)) {
mazgch 21:c4d64830bf02 1092 return false;
mazgch 21:c4d64830bf02 1093 }
mazgch 21:c4d64830bf02 1094 return true;
mazgch 21:c4d64830bf02 1095 }
mazgch 54:7ba8e4c218e2 1096
mazgch 54:7ba8e4c218e2 1097 // ----------------------------------------------------------------
mazgch 70:0a87d256cd24 1098
mazgch 70:0a87d256cd24 1099 int MDMParser::_cbCUSD(int type, const char* buf, int len, char* resp)
mazgch 70:0a87d256cd24 1100 {
mazgch 70:0a87d256cd24 1101 if ((type == TYPE_PLUS) && resp) {
mazgch 70:0a87d256cd24 1102 // +USD: \"%*[^\"]\",\"%[^\"]\",,\"%*[^\"]\",%d,%d,%d,%d,\"*[^\"]\",%d,%d"..);
mazgch 70:0a87d256cd24 1103 if (sscanf(buf, "\r\n+CUSD: %*d,\"%[^\"]\",%*d", resp) == 1) {
mazgch 70:0a87d256cd24 1104 /*nothing*/
mazgch 70:0a87d256cd24 1105 }
mazgch 70:0a87d256cd24 1106 }
mazgch 70:0a87d256cd24 1107 return WAIT;
mazgch 70:0a87d256cd24 1108 }
mazgch 70:0a87d256cd24 1109
mazgch 70:0a87d256cd24 1110 bool MDMParser::ussdCommand(const char* cmd, char* buf)
mazgch 70:0a87d256cd24 1111 {
mazgch 80:34985b4d821e 1112 if (_dev.dev == DEV_LISA_C200)
mazgch 80:34985b4d821e 1113 return false;
mazgch 70:0a87d256cd24 1114 *buf = '\0';
mazgch 70:0a87d256cd24 1115 sendFormated("AT+CUSD=1,\"%s\"\r\n",cmd);
mazgch 70:0a87d256cd24 1116 if (RESP_OK != waitFinalResp(_cbCUSD, buf)) {
mazgch 70:0a87d256cd24 1117 return false;
mazgch 70:0a87d256cd24 1118 }
mazgch 70:0a87d256cd24 1119 return true;
mazgch 70:0a87d256cd24 1120 }
mazgch 80:34985b4d821e 1121
mazgch 80:34985b4d821e 1122 // ----------------------------------------------------------------
mazgch 70:0a87d256cd24 1123
mazgch 80:34985b4d821e 1124 bool MDMParser::delFile(const char* filename)
mazgch 80:34985b4d821e 1125 {
mazgch 80:34985b4d821e 1126 sendFormated("AT+UDELFILE=\"%s\"\r\n", filename);
mazgch 80:34985b4d821e 1127 if (RESP_OK != waitFinalResp())
mazgch 80:34985b4d821e 1128 return false;
mazgch 80:34985b4d821e 1129 return true;
mazgch 80:34985b4d821e 1130 }
mazgch 80:34985b4d821e 1131
mazgch 80:34985b4d821e 1132 int MDMParser::writeFile(const char* filename, const char* buf, int len)
mazgch 80:34985b4d821e 1133 {
mazgch 80:34985b4d821e 1134 sendFormated("AT+UDWNFILE=\"%s\",%d\r\n", filename, len);
mazgch 80:34985b4d821e 1135 if (RESP_PROMPT != waitFinalResp())
mazgch 80:34985b4d821e 1136 return 0;
mazgch 80:34985b4d821e 1137 send(buf, len);
mazgch 80:34985b4d821e 1138 if (RESP_OK != waitFinalResp())
mazgch 80:34985b4d821e 1139 return 0;
mazgch 80:34985b4d821e 1140 return len;
mazgch 80:34985b4d821e 1141 }
mazgch 80:34985b4d821e 1142
mazgch 80:34985b4d821e 1143 int MDMParser::readFile(const char* filename, char* buf, int len)
mazgch 80:34985b4d821e 1144 {
mazgch 80:34985b4d821e 1145 sendFormated("AT+URDFILE=\"%s\"\r\n", filename, len);
mazgch 80:34985b4d821e 1146 URDFILEparam param;
mazgch 80:34985b4d821e 1147 param.filename = filename;
mazgch 80:34985b4d821e 1148 param.buf = buf;
mazgch 80:34985b4d821e 1149 param.sz = len;
mazgch 80:34985b4d821e 1150 param.len = 0;
mazgch 80:34985b4d821e 1151 if (RESP_OK != waitFinalResp(_cbURDFILE, &param))
mazgch 80:34985b4d821e 1152 return -1;
mazgch 80:34985b4d821e 1153 return param.len;
mazgch 80:34985b4d821e 1154 }
mazgch 80:34985b4d821e 1155
mazgch 80:34985b4d821e 1156 int MDMParser::_cbURDFILE(int type, const char* buf, int len, URDFILEparam* param)
mazgch 80:34985b4d821e 1157 {
mazgch 80:34985b4d821e 1158 if ((type == TYPE_PLUS) && param && param->filename && param->buf) {
mazgch 80:34985b4d821e 1159 char filename[48];
mazgch 80:34985b4d821e 1160 int sz;
mazgch 80:34985b4d821e 1161 if ((sscanf(buf, "\r\n+URDFILE: \"%[^\"]\",%d,", filename, &sz) == 2) &&
mazgch 80:34985b4d821e 1162 (0 == strcmp(param->filename, filename)) &&
mazgch 80:34985b4d821e 1163 (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 80:34985b4d821e 1164 param->len = (sz < param->sz) ? sz : param->sz;
mazgch 80:34985b4d821e 1165 memcpy(param->buf, &buf[len-1-sz], param->len);
mazgch 80:34985b4d821e 1166 }
mazgch 80:34985b4d821e 1167 }
mazgch 80:34985b4d821e 1168 return WAIT;
mazgch 80:34985b4d821e 1169 }
mazgch 80:34985b4d821e 1170
mazgch 70:0a87d256cd24 1171 // ----------------------------------------------------------------
mazgch 74:208e3e32d263 1172 bool MDMParser::setDebug(int level)
mazgch 74:208e3e32d263 1173 {
mazgch 74:208e3e32d263 1174 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 1175 if ((_debugLevel >= 0) && (level >= 0)) {
mazgch 74:208e3e32d263 1176 _debugLevel = level;
mazgch 74:208e3e32d263 1177 return true;
mazgch 74:208e3e32d263 1178 }
mazgch 74:208e3e32d263 1179 #endif
mazgch 74:208e3e32d263 1180 return false;
mazgch 74:208e3e32d263 1181 }
mazgch 74:208e3e32d263 1182
mazgch 73:2b32e0a21df2 1183 void MDMParser::dumpDevStatus(MDMParser::DevStatus* status,
mazgch 73:2b32e0a21df2 1184 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 1185 {
mazgch 75:ce6e12067d0c 1186 dprint(param, "Modem::devStatus\r\n");
mazgch 54:7ba8e4c218e2 1187 const char* txtDev[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200" };
mazgch 54:7ba8e4c218e2 1188 if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != MDMParser::DEV_UNKNOWN))
mazgch 73:2b32e0a21df2 1189 dprint(param, " Device: %s\r\n", txtDev[status->dev]);
mazgch 54:7ba8e4c218e2 1190 const char* txtLpm[] = { "Disabled", "Enabled", "Active" };
mazgch 54:7ba8e4c218e2 1191 if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm))
mazgch 73:2b32e0a21df2 1192 dprint(param, " Power Save: %s\r\n", txtLpm[status->lpm]);
mazgch 75:ce6e12067d0c 1193 const char* txtSim[] = { "Unknown", "Missing", "Pin", "Ready" };
mazgch 54:7ba8e4c218e2 1194 if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != MDMParser::SIM_UNKNOWN))
mazgch 73:2b32e0a21df2 1195 dprint(param, " SIM: %s\r\n", txtSim[status->sim]);
mazgch 54:7ba8e4c218e2 1196 if (*status->ccid)
mazgch 73:2b32e0a21df2 1197 dprint(param, " CCID: %s\r\n", status->ccid);
mazgch 54:7ba8e4c218e2 1198 if (*status->imei)
mazgch 73:2b32e0a21df2 1199 dprint(param, " IMEI: %s\r\n", status->imei);
mazgch 54:7ba8e4c218e2 1200 if (*status->imsi)
mazgch 73:2b32e0a21df2 1201 dprint(param, " IMSI: %s\r\n", status->imsi);
mazgch 54:7ba8e4c218e2 1202 if (*status->meid)
mazgch 73:2b32e0a21df2 1203 dprint(param, " MEID: %s\r\n", status->meid); // LISA-C
mazgch 54:7ba8e4c218e2 1204 if (*status->manu)
mazgch 73:2b32e0a21df2 1205 dprint(param, " Manufacturer: %s\r\n", status->manu);
mazgch 54:7ba8e4c218e2 1206 if (*status->model)
mazgch 73:2b32e0a21df2 1207 dprint(param, " Model: %s\r\n", status->model);
mazgch 54:7ba8e4c218e2 1208 if (*status->ver)
mazgch 73:2b32e0a21df2 1209 dprint(param, " Version: %s\r\n", status->ver);
mazgch 54:7ba8e4c218e2 1210 }
mazgch 54:7ba8e4c218e2 1211
mazgch 73:2b32e0a21df2 1212 void MDMParser::dumpNetStatus(MDMParser::NetStatus *status,
mazgch 73:2b32e0a21df2 1213 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 1214 {
mazgch 75:ce6e12067d0c 1215 dprint(param, "Modem::netStatus\r\n");
mazgch 54:7ba8e4c218e2 1216 const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
mazgch 79:291df065e345 1217 if (status->csd < sizeof(txtReg)/sizeof(*txtReg) && (status->csd != MDMParser::REG_UNKNOWN))
mazgch 79:291df065e345 1218 dprint(param, " CSD Registration: %s\r\n", txtReg[status->csd]);
mazgch 79:291df065e345 1219 if (status->psd < sizeof(txtReg)/sizeof(*txtReg) && (status->psd != MDMParser::REG_UNKNOWN))
mazgch 79:291df065e345 1220 dprint(param, " PSD Registration: %s\r\n", txtReg[status->psd]);
mazgch 54:7ba8e4c218e2 1221 const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" };
mazgch 54:7ba8e4c218e2 1222 if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != MDMParser::ACT_UNKNOWN))
mazgch 73:2b32e0a21df2 1223 dprint(param, " Access Technology: %s\r\n", txtAct[status->act]);
mazgch 54:7ba8e4c218e2 1224 if (status->rssi)
mazgch 73:2b32e0a21df2 1225 dprint(param, " Signal Strength: %d dBm\r\n", status->rssi);
mazgch 54:7ba8e4c218e2 1226 if (status->ber)
mazgch 73:2b32e0a21df2 1227 dprint(param, " Bit Error Rate: %d\r\n", status->ber);
mazgch 54:7ba8e4c218e2 1228 if (*status->opr)
mazgch 73:2b32e0a21df2 1229 dprint(param, " Operator: %s\r\n", status->opr);
mazgch 54:7ba8e4c218e2 1230 if (status->lac != 0xFFFF)
mazgch 73:2b32e0a21df2 1231 dprint(param, " Location Area Code: %04X\r\n", status->lac);
mazgch 54:7ba8e4c218e2 1232 if (status->ci != 0xFFFFFFFF)
mazgch 73:2b32e0a21df2 1233 dprint(param, " Cell ID: %08X\r\n", status->ci);
mazgch 54:7ba8e4c218e2 1234 if (*status->num)
mazgch 73:2b32e0a21df2 1235 dprint(param, " Phone Number: %s\r\n", status->num);
mazgch 54:7ba8e4c218e2 1236 }
mazgch 54:7ba8e4c218e2 1237
mazgch 73:2b32e0a21df2 1238 void MDMParser::dumpIp(MDMParser::IP ip,
mazgch 73:2b32e0a21df2 1239 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 1240 {
mazgch 57:869bd35f44cc 1241 if (ip != NOIP)
mazgch 75:ce6e12067d0c 1242 dprint(param, "Modem:IP " IPSTR "\r\n", IPNUM(ip));
mazgch 54:7ba8e4c218e2 1243 }
mazgch 70:0a87d256cd24 1244
mazgch 21:c4d64830bf02 1245 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 1246 int MDMParser::_parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end)
mazgch 18:e5697801df29 1247 {
mazgch 18:e5697801df29 1248 int o = 0;
mazgch 21:c4d64830bf02 1249 if (sta) {
mazgch 21:c4d64830bf02 1250 while (*sta) {
mazgch 21:c4d64830bf02 1251 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 1252 char ch = pipe->next();
mazgch 21:c4d64830bf02 1253 if (*sta++ != ch) return NOT_FOUND;
mazgch 21:c4d64830bf02 1254 }
mazgch 21:c4d64830bf02 1255 }
mazgch 21:c4d64830bf02 1256 if (!end) return o; // no termination
mazgch 35:9275215a3a5b 1257 // at least any char
mazgch 35:9275215a3a5b 1258 if (++o > len) return WAIT;
mazgch 35:9275215a3a5b 1259 pipe->next();
mazgch 35:9275215a3a5b 1260 // check the end
mazgch 21:c4d64830bf02 1261 int x = 0;
mazgch 21:c4d64830bf02 1262 while (end[x]) {
mazgch 21:c4d64830bf02 1263 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 1264 char ch = pipe->next();
mazgch 21:c4d64830bf02 1265 x = (end[x] == ch) ? x + 1 :
mazgch 21:c4d64830bf02 1266 (end[0] == ch) ? 1 :
mazgch 21:c4d64830bf02 1267 0;
mazgch 21:c4d64830bf02 1268 }
mazgch 21:c4d64830bf02 1269 return o;
mazgch 21:c4d64830bf02 1270 }
mazgch 21:c4d64830bf02 1271
mazgch 21:c4d64830bf02 1272 int MDMParser::_parseFormated(Pipe<char>* pipe, int len, const char* fmt)
mazgch 21:c4d64830bf02 1273 {
mazgch 21:c4d64830bf02 1274 int o = 0;
mazgch 21:c4d64830bf02 1275 int num = 0;
mazgch 21:c4d64830bf02 1276 if (fmt) {
mazgch 21:c4d64830bf02 1277 while (*fmt) {
mazgch 21:c4d64830bf02 1278 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 1279 char ch = pipe->next();
mazgch 21:c4d64830bf02 1280 if (*fmt == '%') {
mazgch 21:c4d64830bf02 1281 fmt++;
mazgch 21:c4d64830bf02 1282 if (*fmt == 'd') { // numeric
mazgch 21:c4d64830bf02 1283 fmt ++;
mazgch 21:c4d64830bf02 1284 num = 0;
mazgch 21:c4d64830bf02 1285 while (ch >= '0' && ch <= '9') {
mazgch 21:c4d64830bf02 1286 num = num * 10 + (ch - '0');
mazgch 21:c4d64830bf02 1287 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 1288 ch = pipe->next();
mazgch 21:c4d64830bf02 1289 }
mazgch 21:c4d64830bf02 1290 }
mazgch 21:c4d64830bf02 1291 else if (*fmt == 'c') { // char buffer (takes last numeric as length)
mazgch 21:c4d64830bf02 1292 fmt ++;
mazgch 21:c4d64830bf02 1293 while (num --) {
mazgch 21:c4d64830bf02 1294 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 1295 ch = pipe->next();
mazgch 21:c4d64830bf02 1296 }
mazgch 21:c4d64830bf02 1297 }
mazgch 80:34985b4d821e 1298 else if (*fmt == 's') {
mazgch 80:34985b4d821e 1299 fmt ++;
mazgch 80:34985b4d821e 1300 if (ch != '\"') return NOT_FOUND;
mazgch 80:34985b4d821e 1301 do {
mazgch 80:34985b4d821e 1302 if (++o > len) return WAIT;
mazgch 80:34985b4d821e 1303 ch = pipe->next();
mazgch 80:34985b4d821e 1304 } while (ch != '\"');
mazgch 80:34985b4d821e 1305 if (++o > len) return WAIT;
mazgch 80:34985b4d821e 1306 ch = pipe->next();
mazgch 80:34985b4d821e 1307 }
mazgch 21:c4d64830bf02 1308 }
mazgch 21:c4d64830bf02 1309 if (*fmt++ != ch) return NOT_FOUND;
mazgch 18:e5697801df29 1310 }
mazgch 18:e5697801df29 1311 }
mazgch 21:c4d64830bf02 1312 return o;
mazgch 21:c4d64830bf02 1313 }
mazgch 21:c4d64830bf02 1314
mazgch 21:c4d64830bf02 1315 int MDMParser::_getLine(Pipe<char>* pipe, char* buf, int len)
mazgch 21:c4d64830bf02 1316 {
mazgch 21:c4d64830bf02 1317 int unkn = 0;
mazgch 21:c4d64830bf02 1318 int sz = pipe->size();
mazgch 21:c4d64830bf02 1319 int fr = pipe->free();
mazgch 21:c4d64830bf02 1320 if (len > sz)
mazgch 21:c4d64830bf02 1321 len = sz;
mazgch 21:c4d64830bf02 1322 while (len > 0)
mazgch 21:c4d64830bf02 1323 {
mazgch 21:c4d64830bf02 1324 static struct {
mazgch 21:c4d64830bf02 1325 const char* fmt; int type;
mazgch 21:c4d64830bf02 1326 } lutF[] = {
mazgch 21:c4d64830bf02 1327 { "\r\n+USORD: %d,%d,\"%c\"", TYPE_PLUS },
mazgch 21:c4d64830bf02 1328 { "\r\n+USORF: %d,\""IPSTR"\",%d,%d,\"%c\"", TYPE_PLUS },
mazgch 80:34985b4d821e 1329 { "\r\n+URDFILE: %s,%d,\"%c\"", TYPE_PLUS },
mazgch 21:c4d64830bf02 1330 };
mazgch 21:c4d64830bf02 1331 static struct {
mazgch 21:c4d64830bf02 1332 const char* sta; const char* end; int type;
mazgch 21:c4d64830bf02 1333 } lut[] = {
mazgch 21:c4d64830bf02 1334 { "\r\nOK\r\n", NULL, TYPE_OK },
mazgch 21:c4d64830bf02 1335 { "\r\nERROR\r\n", NULL, TYPE_ERROR },
mazgch 31:a0bed6c1e05d 1336 { "\r\n+CME ERROR:", "\r\n", TYPE_ERROR },
mazgch 21:c4d64830bf02 1337 { "\r\n+CMS ERROR:", "\r\n", TYPE_ERROR },
mazgch 21:c4d64830bf02 1338 { "\r\nRING\r\n", NULL, TYPE_RING },
mazgch 21:c4d64830bf02 1339 { "\r\nCONNECT\r\n", NULL, TYPE_CONNECT },
mazgch 21:c4d64830bf02 1340 { "\r\nNO CARRIER\r\n", NULL, TYPE_NOCARRIER },
mazgch 21:c4d64830bf02 1341 { "\r\nNO DIALTONE\r\n", NULL, TYPE_NODIALTONE },
mazgch 21:c4d64830bf02 1342 { "\r\nBUSY\r\n", NULL, TYPE_BUSY },
mazgch 21:c4d64830bf02 1343 { "\r\nNO ANSWER\r\n", NULL, TYPE_NOANSWER },
mazgch 21:c4d64830bf02 1344 { "\r\n+", "\r\n", TYPE_PLUS },
mazgch 21:c4d64830bf02 1345 { "\r\n@", NULL, TYPE_PROMPT }, // Sockets
mazgch 21:c4d64830bf02 1346 { "\r\n>", NULL, TYPE_PROMPT }, // SMS
mazgch 80:34985b4d821e 1347 { "\n>", NULL, TYPE_PROMPT }, // File
mazgch 21:c4d64830bf02 1348 };
mazgch 21:c4d64830bf02 1349 for (int i = 0; i < sizeof(lutF)/sizeof(*lutF); i ++) {
mazgch 21:c4d64830bf02 1350 pipe->set(unkn);
mazgch 21:c4d64830bf02 1351 int ln = _parseFormated(pipe, len, lutF[i].fmt);
mazgch 21:c4d64830bf02 1352 if (ln == WAIT && fr)
mazgch 21:c4d64830bf02 1353 return WAIT;
mazgch 21:c4d64830bf02 1354 if ((ln != NOT_FOUND) && (unkn > 0))
mazgch 31:a0bed6c1e05d 1355 return TYPE_UNKNOWN | pipe->get(buf, unkn);
mazgch 21:c4d64830bf02 1356 if (ln > 0)
mazgch 21:c4d64830bf02 1357 return lutF[i].type | pipe->get(buf, ln);
mazgch 21:c4d64830bf02 1358 }
mazgch 21:c4d64830bf02 1359 for (int i = 0; i < sizeof(lut)/sizeof(*lut); i ++) {
mazgch 21:c4d64830bf02 1360 pipe->set(unkn);
mazgch 21:c4d64830bf02 1361 int ln = _parseMatch(pipe, len, lut[i].sta, lut[i].end);
mazgch 21:c4d64830bf02 1362 if (ln == WAIT && fr)
mazgch 21:c4d64830bf02 1363 return WAIT;
mazgch 21:c4d64830bf02 1364 if ((ln != NOT_FOUND) && (unkn > 0))
mazgch 31:a0bed6c1e05d 1365 return TYPE_UNKNOWN | pipe->get(buf, unkn);
mazgch 21:c4d64830bf02 1366 if (ln > 0)
mazgch 21:c4d64830bf02 1367 return lut[i].type | pipe->get(buf, ln);
mazgch 21:c4d64830bf02 1368 }
mazgch 21:c4d64830bf02 1369 // UNKNOWN
mazgch 21:c4d64830bf02 1370 unkn ++;
mazgch 21:c4d64830bf02 1371 len--;
mazgch 21:c4d64830bf02 1372 }
mazgch 18:e5697801df29 1373 return WAIT;
mazgch 18:e5697801df29 1374 }
mazgch 18:e5697801df29 1375
mazgch 18:e5697801df29 1376 // ----------------------------------------------------------------
mazgch 18:e5697801df29 1377 // Serial Implementation
mazgch 18:e5697801df29 1378 // ----------------------------------------------------------------
mazgch 18:e5697801df29 1379
mazgch 76:f7c3dd568dae 1380 /*! Helper Dev Null Device
mazgch 76:f7c3dd568dae 1381 Small helper class used to shut off stderr/stdout. Sometimes stdin/stdout
mazgch 76:f7c3dd568dae 1382 is shared with the serial port of the modem. Having printfs inbetween the
mazgch 76:f7c3dd568dae 1383 AT commands you cause a failure of the modem.
mazgch 76:f7c3dd568dae 1384 */
mazgch 76:f7c3dd568dae 1385 class DevNull : public Stream {
mazgch 76:f7c3dd568dae 1386 public:
mazgch 76:f7c3dd568dae 1387 DevNull() : Stream(_name+1) { } //!< Constructor
mazgch 76:f7c3dd568dae 1388 void claim(const char* mode, FILE* file)
mazgch 76:f7c3dd568dae 1389 { freopen(_name, mode, file); } //!< claim a stream
mazgch 76:f7c3dd568dae 1390 protected:
mazgch 76:f7c3dd568dae 1391 virtual int _getc() { return EOF; } //!< Nothing
mazgch 76:f7c3dd568dae 1392 virtual int _putc(int c) { return c; } //!< Discard
mazgch 76:f7c3dd568dae 1393 static const char* _name; //!< File name
mazgch 76:f7c3dd568dae 1394 };
mazgch 76:f7c3dd568dae 1395 const char* DevNull::_name = "/null"; //!< the null device name
mazgch 76:f7c3dd568dae 1396 static DevNull null; //!< the null device
mazgch 76:f7c3dd568dae 1397
mazgch 19:2b5d097ca15d 1398 MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/,
mazgch 19:2b5d097ca15d 1399 int baudrate /*= MDMBAUD*/,
mazgch 43:a89a7a505991 1400 #if DEVICE_SERIAL_FC
mazgch 19:2b5d097ca15d 1401 PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/,
mazgch 43:a89a7a505991 1402 #endif
mazgch 18:e5697801df29 1403 int rxSize /*= 256*/, int txSize /*= 128*/) :
mazgch 35:9275215a3a5b 1404 SerialPipe(tx, rx, rxSize, txSize)
mazgch 18:e5697801df29 1405 {
mazgch 76:f7c3dd568dae 1406 if (rx == USBRX)
mazgch 76:f7c3dd568dae 1407 null.claim("r", stdin);
mazgch 76:f7c3dd568dae 1408 if (tx == USBTX) {
mazgch 76:f7c3dd568dae 1409 null.claim("w", stdout);
mazgch 76:f7c3dd568dae 1410 null.claim("w", stderr);
mazgch 74:208e3e32d263 1411 #ifdef MDM_DEBUG
mazgch 76:f7c3dd568dae 1412 _debugLevel = -1;
mazgch 76:f7c3dd568dae 1413 #endif
mazgch 76:f7c3dd568dae 1414 }
mazgch 74:208e3e32d263 1415 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 1416 _onboard = (tx == MDMTXD) && (rx == MDMRXD);
mazgch 74:208e3e32d263 1417 if (_onboard)
mazgch 74:208e3e32d263 1418 c027_mdm_powerOn(false);
mazgch 74:208e3e32d263 1419 #endif
mazgch 18:e5697801df29 1420 baud(baudrate);
mazgch 35:9275215a3a5b 1421 #if DEVICE_SERIAL_FC
mazgch 35:9275215a3a5b 1422 if ((rts != NC) || (cts != NC))
mazgch 35:9275215a3a5b 1423 {
mazgch 35:9275215a3a5b 1424 Flow flow = (cts == NC) ? RTS :
mazgch 35:9275215a3a5b 1425 (rts == NC) ? CTS : RTSCTS ;
mazgch 35:9275215a3a5b 1426 set_flow_control(flow, rts, cts);
mazgch 35:9275215a3a5b 1427 if (cts != NC) _dev.lpm = LPM_ENABLED;
mazgch 35:9275215a3a5b 1428 }
mazgch 35:9275215a3a5b 1429 #endif
mazgch 18:e5697801df29 1430 }
mazgch 18:e5697801df29 1431
mazgch 76:f7c3dd568dae 1432 MDMSerial::~MDMSerial(void)
mazgch 76:f7c3dd568dae 1433 {
mazgch 76:f7c3dd568dae 1434 powerOff();
mazgch 76:f7c3dd568dae 1435 #ifdef TARGET_UBLOX_C027
mazgch 76:f7c3dd568dae 1436 if (_onboard)
mazgch 76:f7c3dd568dae 1437 c027_mdm_powerOff();
mazgch 76:f7c3dd568dae 1438 #endif
mazgch 76:f7c3dd568dae 1439 }
mazgch 76:f7c3dd568dae 1440
mazgch 18:e5697801df29 1441 int MDMSerial::_send(const void* buf, int len)
mazgch 18:e5697801df29 1442 {
mazgch 35:9275215a3a5b 1443 return put((const char*)buf, len, true/*=blocking*/);
mazgch 18:e5697801df29 1444 }
mazgch 18:e5697801df29 1445
mazgch 18:e5697801df29 1446 int MDMSerial::getLine(char* buffer, int length)
mazgch 18:e5697801df29 1447 {
mazgch 18:e5697801df29 1448 return _getLine(&_pipeRx, buffer, length);
mazgch 18:e5697801df29 1449 }
mazgch 18:e5697801df29 1450
mazgch 18:e5697801df29 1451 // ----------------------------------------------------------------
mazgch 18:e5697801df29 1452 // USB Implementation
mazgch 18:e5697801df29 1453 // ----------------------------------------------------------------
mazgch 18:e5697801df29 1454
mazgch 18:e5697801df29 1455 #ifdef HAVE_MDMUSB
mazgch 76:f7c3dd568dae 1456 MDMUsb::MDMUsb(void)
mazgch 74:208e3e32d263 1457 {
mazgch 74:208e3e32d263 1458 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 1459 _debugLevel = 1;
mazgch 74:208e3e32d263 1460 #endif
mazgch 74:208e3e32d263 1461 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 1462 _onboard = true;
mazgch 74:208e3e32d263 1463 c027_mdm_powerOn(true);
mazgch 74:208e3e32d263 1464 #endif
mazgch 74:208e3e32d263 1465 }
mazgch 76:f7c3dd568dae 1466
mazgch 76:f7c3dd568dae 1467 MDMUsb::~MDMUsb(void)
mazgch 76:f7c3dd568dae 1468 {
mazgch 76:f7c3dd568dae 1469 powerOff();
mazgch 76:f7c3dd568dae 1470 #ifdef TARGET_UBLOX_C027
mazgch 76:f7c3dd568dae 1471 if (_onboard)
mazgch 76:f7c3dd568dae 1472 c027_mdm_powerOff();
mazgch 76:f7c3dd568dae 1473 #endif
mazgch 76:f7c3dd568dae 1474 }
mazgch 76:f7c3dd568dae 1475
mazgch 76:f7c3dd568dae 1476 int MDMUsb::_send(const void* buf, int len) { return 0; }
mazgch 76:f7c3dd568dae 1477
mazgch 18:e5697801df29 1478 int MDMUsb::getLine(char* buffer, int length) { return NOT_FOUND; }
mazgch 76:f7c3dd568dae 1479
mazgch 35:9275215a3a5b 1480 #endif