C027_Support library plus AT Comand for dialing.
Fork of C027_Support_New by
MDM.cpp@121:3e1d84b2d34c, 2015-10-11 (annotated)
- Committer:
- irsanjul
- Date:
- Sun Oct 11 09:56:57 2015 +0000
- Revision:
- 121:3e1d84b2d34c
- Parent:
- 120:353754a2bcd9
C027_Support library from mbed plus AT Command for dialing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 18:e5697801df29 | 1 | #include "mbed.h" |
mazgch | 18:e5697801df29 | 2 | #include "MDM.h" |
mazgch | 74:208e3e32d263 | 3 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 4 | #include "C027_api.h" |
mazgch | 74:208e3e32d263 | 5 | #endif |
mazgch | 85:dd8f4f0d0ca9 | 6 | #include "MDMAPN.h" |
irsanjul | 120:353754a2bcd9 | 7 | #include <sstream> |
irsanjul | 120:353754a2bcd9 | 8 | |
irsanjul | 120:353754a2bcd9 | 9 | extern Serial dbg; |
irsanjul | 121:3e1d84b2d34c | 10 | extern time_t t; |
mazgch | 84:a05edb010176 | 11 | |
mazgch | 74:208e3e32d263 | 12 | #define PROFILE "0" //!< this is the psd profile used |
mazgch | 74:208e3e32d263 | 13 | #define MAX_SIZE 128 //!< max expected messages |
mazgch | 103:197fa7920ad8 | 14 | // num sockets |
mazgch | 104:c64ba749a422 | 15 | #define NUMSOCKETS (sizeof(_sockets)/sizeof(*_sockets)) |
mazgch | 104:c64ba749a422 | 16 | //! test if it is a socket is ok to use |
mazgch | 104:c64ba749a422 | 17 | #define ISSOCKET(s) (((s) >= 0) && ((s) < NUMSOCKETS) && (_sockets[s].handle != SOCKET_ERROR)) |
mazgch | 75:ce6e12067d0c | 18 | //! check for timeout |
mazgch | 75:ce6e12067d0c | 19 | #define TIMEOUT(t, ms) ((ms != TIMEOUT_BLOCKING) && (ms < t.read_ms())) |
mazgch | 80:34985b4d821e | 20 | //! registration ok check helper |
mazgch | 79:291df065e345 | 21 | #define REG_OK(r) ((r == REG_HOME) || (r == REG_ROAMING)) |
mazgch | 80:34985b4d821e | 22 | //! registration done check helper (no need to poll further) |
mazgch | 79:291df065e345 | 23 | #define REG_DONE(r) ((r == REG_HOME) || (r == REG_ROAMING) || (r == REG_DENIED)) |
mazgch | 95:8282dbbe1492 | 24 | //! helper to make sure that lock unlock pair is always balaced |
mazgch | 95:8282dbbe1492 | 25 | #define LOCK() { lock() |
mazgch | 95:8282dbbe1492 | 26 | //! helper to make sure that lock unlock pair is always balaced |
mazgch | 95:8282dbbe1492 | 27 | #define UNLOCK() } unlock() |
mazgch | 79:291df065e345 | 28 | |
mazgch | 74:208e3e32d263 | 29 | #ifdef MDM_DEBUG |
mazgch | 95:8282dbbe1492 | 30 | #if 1 // colored terminal output using ANSI escape sequences |
mazgch | 95:8282dbbe1492 | 31 | #define COL(c) "\033[" c |
mazgch | 95:8282dbbe1492 | 32 | #else |
mazgch | 95:8282dbbe1492 | 33 | #define COL(c) |
mazgch | 95:8282dbbe1492 | 34 | #endif |
mazgch | 95:8282dbbe1492 | 35 | #define DEF COL("39m") |
mazgch | 95:8282dbbe1492 | 36 | #define BLA COL("30m") |
mazgch | 95:8282dbbe1492 | 37 | #define RED COL("31m") |
mazgch | 95:8282dbbe1492 | 38 | #define GRE COL("32m") |
mazgch | 95:8282dbbe1492 | 39 | #define YEL COL("33m") |
mazgch | 95:8282dbbe1492 | 40 | #define BLU COL("34m") |
mazgch | 95:8282dbbe1492 | 41 | #define MAG COL("35m") |
mazgch | 95:8282dbbe1492 | 42 | #define CYA COL("36m") |
mazgch | 95:8282dbbe1492 | 43 | #define WHY COL("37m") |
mazgch | 95:8282dbbe1492 | 44 | |
mazgch | 74:208e3e32d263 | 45 | void dumpAtCmd(const char* buf, int len) |
mazgch | 21:c4d64830bf02 | 46 | { |
mazgch | 75:ce6e12067d0c | 47 | ::printf(" %3d \"", len); |
mazgch | 21:c4d64830bf02 | 48 | while (len --) { |
mazgch | 21:c4d64830bf02 | 49 | char ch = *buf++; |
mazgch | 75:ce6e12067d0c | 50 | if ((ch > 0x1F) && (ch != 0x7F)) { // is printable |
mazgch | 75:ce6e12067d0c | 51 | if (ch == '%') ::printf("%%"); |
mazgch | 75:ce6e12067d0c | 52 | else if (ch == '"') ::printf("\\\""); |
mazgch | 75:ce6e12067d0c | 53 | else if (ch == '\\') ::printf("\\\\"); |
mazgch | 75:ce6e12067d0c | 54 | else putchar(ch); |
mazgch | 75:ce6e12067d0c | 55 | } else { |
mazgch | 75:ce6e12067d0c | 56 | if (ch == '\a') ::printf("\\a"); // BEL (0x07) |
mazgch | 75:ce6e12067d0c | 57 | else if (ch == '\b') ::printf("\\b"); // Backspace (0x08) |
mazgch | 75:ce6e12067d0c | 58 | else if (ch == '\t') ::printf("\\t"); // Horizontal Tab (0x09) |
mazgch | 75:ce6e12067d0c | 59 | else if (ch == '\n') ::printf("\\n"); // Linefeed (0x0A) |
mazgch | 75:ce6e12067d0c | 60 | else if (ch == '\v') ::printf("\\v"); // Vertical Tab (0x0B) |
mazgch | 75:ce6e12067d0c | 61 | else if (ch == '\f') ::printf("\\f"); // Formfeed (0x0C) |
mazgch | 75:ce6e12067d0c | 62 | else if (ch == '\r') ::printf("\\r"); // Carriage Return (0x0D) |
mazgch | 76:f7c3dd568dae | 63 | else ::printf("\\x%02x", (unsigned char)ch); |
mazgch | 75:ce6e12067d0c | 64 | } |
mazgch | 21:c4d64830bf02 | 65 | } |
mazgch | 75:ce6e12067d0c | 66 | ::printf("\"\r\n"); |
mazgch | 21:c4d64830bf02 | 67 | } |
mazgch | 74:208e3e32d263 | 68 | |
mazgch | 117:74e4e0109a9e | 69 | void MDMParser::_debugPrint(int level, const char* color, const char* format, ...) |
mazgch | 117:74e4e0109a9e | 70 | { |
mazgch | 117:74e4e0109a9e | 71 | if (_debugLevel >= level) |
mazgch | 117:74e4e0109a9e | 72 | { |
mazgch | 117:74e4e0109a9e | 73 | va_list args; |
mazgch | 117:74e4e0109a9e | 74 | va_start (args, format); |
mazgch | 117:74e4e0109a9e | 75 | if (color) ::printf(color); |
mazgch | 117:74e4e0109a9e | 76 | ::vprintf(format, args); |
mazgch | 117:74e4e0109a9e | 77 | if (color) ::printf(DEF); |
mazgch | 117:74e4e0109a9e | 78 | va_end (args); |
mazgch | 117:74e4e0109a9e | 79 | } |
mazgch | 117:74e4e0109a9e | 80 | } |
mazgch | 117:74e4e0109a9e | 81 | |
mazgch | 117:74e4e0109a9e | 82 | #define ERROR(...) _debugPrint(0, RED, __VA_ARGS__) |
mazgch | 117:74e4e0109a9e | 83 | #define INFO(...) _debugPrint(1, GRE, __VA_ARGS__) |
mazgch | 117:74e4e0109a9e | 84 | #define TRACE(...) _debugPrint(2, DEF, __VA_ARGS__) |
mazgch | 117:74e4e0109a9e | 85 | #define TEST(...) _debugPrint(3, CYA, __VA_ARGS__) |
mazgch | 74:208e3e32d263 | 86 | |
mazgch | 74:208e3e32d263 | 87 | #else |
mazgch | 74:208e3e32d263 | 88 | |
mazgch | 75:ce6e12067d0c | 89 | #define ERROR(...) (void)0 // no tracing |
mazgch | 95:8282dbbe1492 | 90 | #define TEST(...) (void)0 // no tracing |
mazgch | 74:208e3e32d263 | 91 | #define INFO(...) (void)0 // no tracing |
mazgch | 74:208e3e32d263 | 92 | #define TRACE(...) (void)0 // no tracing |
mazgch | 74:208e3e32d263 | 93 | |
mazgch | 31:a0bed6c1e05d | 94 | #endif |
mazgch | 21:c4d64830bf02 | 95 | |
mazgch | 44:9d12223b78ff | 96 | MDMParser* MDMParser::inst; |
mazgch | 44:9d12223b78ff | 97 | |
mazgch | 21:c4d64830bf02 | 98 | MDMParser::MDMParser(void) |
mazgch | 21:c4d64830bf02 | 99 | { |
mazgch | 44:9d12223b78ff | 100 | inst = this; |
mazgch | 31:a0bed6c1e05d | 101 | memset(&_dev, 0, sizeof(_dev)); |
mazgch | 31:a0bed6c1e05d | 102 | memset(&_net, 0, sizeof(_net)); |
mazgch | 54:7ba8e4c218e2 | 103 | _net.lac = 0xFFFF; |
mazgch | 54:7ba8e4c218e2 | 104 | _net.ci = 0xFFFFFFFF; |
mazgch | 35:9275215a3a5b | 105 | _ip = NOIP; |
mazgch | 76:f7c3dd568dae | 106 | _init = false; |
mazgch | 31:a0bed6c1e05d | 107 | memset(_sockets, 0, sizeof(_sockets)); |
mazgch | 104:c64ba749a422 | 108 | for (int socket = 0; socket < NUMSOCKETS; socket ++) |
mazgch | 103:197fa7920ad8 | 109 | _sockets[socket].handle = SOCKET_ERROR; |
mazgch | 74:208e3e32d263 | 110 | #ifdef MDM_DEBUG |
mazgch | 76:f7c3dd568dae | 111 | _debugLevel = 1; |
mazgch | 74:208e3e32d263 | 112 | _debugTime.start(); |
mazgch | 74:208e3e32d263 | 113 | #endif |
mazgch | 74:208e3e32d263 | 114 | } |
mazgch | 74:208e3e32d263 | 115 | |
mazgch | 18:e5697801df29 | 116 | int MDMParser::send(const char* buf, int len) |
mazgch | 18:e5697801df29 | 117 | { |
mazgch | 74:208e3e32d263 | 118 | #ifdef MDM_DEBUG |
mazgch | 74:208e3e32d263 | 119 | if (_debugLevel >= 3) { |
mazgch | 75:ce6e12067d0c | 120 | ::printf("%10.3f AT send ", _debugTime.read_ms()*0.001); |
mazgch | 74:208e3e32d263 | 121 | dumpAtCmd(buf,len); |
mazgch | 74:208e3e32d263 | 122 | } |
mazgch | 21:c4d64830bf02 | 123 | #endif |
mazgch | 18:e5697801df29 | 124 | return _send(buf, len); |
mazgch | 18:e5697801df29 | 125 | } |
mazgch | 18:e5697801df29 | 126 | |
mazgch | 21:c4d64830bf02 | 127 | int MDMParser::sendFormated(const char* format, ...) { |
mazgch | 21:c4d64830bf02 | 128 | char buf[MAX_SIZE]; |
mazgch | 21:c4d64830bf02 | 129 | va_list args; |
mazgch | 21:c4d64830bf02 | 130 | va_start(args, format); |
mazgch | 21:c4d64830bf02 | 131 | int len = vsnprintf(buf,sizeof(buf), format, args); |
mazgch | 21:c4d64830bf02 | 132 | va_end(args); |
mazgch | 21:c4d64830bf02 | 133 | return send(buf, len); |
mazgch | 21:c4d64830bf02 | 134 | } |
mazgch | 21:c4d64830bf02 | 135 | |
mazgch | 26:07be5faf8925 | 136 | int MDMParser::waitFinalResp(_CALLBACKPTR cb /* = NULL*/, |
mazgch | 26:07be5faf8925 | 137 | void* param /* = NULL*/, |
mazgch | 26:07be5faf8925 | 138 | int timeout_ms /*= 5000*/) |
mazgch | 26:07be5faf8925 | 139 | { |
mazgch | 69:4d6fa520dfca | 140 | char buf[MAX_SIZE + 64 /* add some more space for framing */]; |
mazgch | 21:c4d64830bf02 | 141 | Timer timer; |
mazgch | 21:c4d64830bf02 | 142 | timer.start(); |
mazgch | 21:c4d64830bf02 | 143 | do { |
mazgch | 21:c4d64830bf02 | 144 | int ret = getLine(buf, sizeof(buf)); |
mazgch | 74:208e3e32d263 | 145 | #ifdef MDM_DEBUG |
mazgch | 74:208e3e32d263 | 146 | if ((_debugLevel >= 3) && (ret != WAIT) && (ret != NOT_FOUND)) |
mazgch | 21:c4d64830bf02 | 147 | { |
mazgch | 31:a0bed6c1e05d | 148 | int len = LENGTH(ret); |
mazgch | 31:a0bed6c1e05d | 149 | int type = TYPE(ret); |
mazgch | 95:8282dbbe1492 | 150 | const char* s = (type == TYPE_UNKNOWN)? YEL "UNK" DEF : |
mazgch | 95:8282dbbe1492 | 151 | (type == TYPE_TEXT) ? MAG "TXT" DEF : |
mazgch | 95:8282dbbe1492 | 152 | (type == TYPE_OK ) ? GRE "OK " DEF : |
mazgch | 95:8282dbbe1492 | 153 | (type == TYPE_ERROR) ? RED "ERR" DEF : |
mazgch | 95:8282dbbe1492 | 154 | (type == TYPE_PLUS) ? CYA " + " DEF : |
irsanjul | 120:353754a2bcd9 | 155 | (type == TYPE_PROMPT) ? BLU " > " DEF : |
irsanjul | 120:353754a2bcd9 | 156 | (type == TYPE_NOCARRIER)? CYA "NO" DEF : |
irsanjul | 120:353754a2bcd9 | 157 | (type == TYPE_RING) ? CYA "RING" DEF : |
mazgch | 95:8282dbbe1492 | 158 | "..." ; |
mazgch | 75:ce6e12067d0c | 159 | ::printf("%10.3f AT read %s", _debugTime.read_ms()*0.001, s); |
mazgch | 74:208e3e32d263 | 160 | dumpAtCmd(buf, len); |
mazgch | 21:c4d64830bf02 | 161 | } |
mazgch | 21:c4d64830bf02 | 162 | #endif |
mazgch | 21:c4d64830bf02 | 163 | if ((ret != WAIT) && (ret != NOT_FOUND)) |
mazgch | 18:e5697801df29 | 164 | { |
mazgch | 21:c4d64830bf02 | 165 | int type = TYPE(ret); |
mazgch | 21:c4d64830bf02 | 166 | // handle unsolicited commands here |
mazgch | 21:c4d64830bf02 | 167 | if (type == TYPE_PLUS) { |
mazgch | 21:c4d64830bf02 | 168 | const char* cmd = buf+3; |
mazgch | 54:7ba8e4c218e2 | 169 | int a, b, c, d, r; |
mazgch | 23:05a1aeeb5fd9 | 170 | char s[32]; |
mazgch | 21:c4d64830bf02 | 171 | |
mazgch | 31:a0bed6c1e05d | 172 | // SMS Command --------------------------------- |
mazgch | 31:a0bed6c1e05d | 173 | // +CNMI: <mem>,<index> |
mazgch | 31:a0bed6c1e05d | 174 | if (sscanf(cmd, "CMTI: \"%*[^\"]\",%d", &a) == 1) { |
mazgch | 31:a0bed6c1e05d | 175 | TRACE("New SMS at index %d\r\n", a); |
mazgch | 21:c4d64830bf02 | 176 | // Socket Specific Command --------------------------------- |
mazgch | 21:c4d64830bf02 | 177 | // +UUSORD: <socket>,<length> |
mazgch | 103:197fa7920ad8 | 178 | } else if ((sscanf(cmd, "UUSORD: %d,%d", &a, &b) == 2)) { |
mazgch | 103:197fa7920ad8 | 179 | int socket = _findSocket(a); |
mazgch | 103:197fa7920ad8 | 180 | TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b); |
mazgch | 103:197fa7920ad8 | 181 | if (socket != SOCKET_ERROR) |
mazgch | 103:197fa7920ad8 | 182 | _sockets[socket].pending = b; |
mazgch | 69:4d6fa520dfca | 183 | // +UUSORF: <socket>,<length> |
mazgch | 103:197fa7920ad8 | 184 | } else if ((sscanf(cmd, "UUSORF: %d,%d", &a, &b) == 2)) { |
mazgch | 103:197fa7920ad8 | 185 | int socket = _findSocket(a); |
mazgch | 103:197fa7920ad8 | 186 | TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b); |
mazgch | 103:197fa7920ad8 | 187 | if (socket != SOCKET_ERROR) |
mazgch | 103:197fa7920ad8 | 188 | _sockets[socket].pending = b; |
mazgch | 21:c4d64830bf02 | 189 | // +UUSOCL: <socket> |
mazgch | 103:197fa7920ad8 | 190 | } else if ((sscanf(cmd, "UUSOCL: %d", &a) == 1)) { |
mazgch | 103:197fa7920ad8 | 191 | int socket = _findSocket(a); |
mazgch | 103:197fa7920ad8 | 192 | TRACE("Socket %d: handle %d closed by remote host\r\n", socket, a); |
mazgch | 103:197fa7920ad8 | 193 | if ((socket != SOCKET_ERROR) && _sockets[socket].connected) |
mazgch | 103:197fa7920ad8 | 194 | _sockets[socket].connected = false; |
mazgch | 103:197fa7920ad8 | 195 | } |
mazgch | 32:8f12ac182bbb | 196 | if (_dev.dev == DEV_LISA_C200) { |
mazgch | 21:c4d64830bf02 | 197 | // CDMA Specific ------------------------------------------- |
mazgch | 21:c4d64830bf02 | 198 | // +CREG: <n><SID>,<NID>,<stat> |
mazgch | 54:7ba8e4c218e2 | 199 | if (sscanf(cmd, "CREG: %*d,%d,%d,%d",&a,&b,&c) == 3) { |
mazgch | 54:7ba8e4c218e2 | 200 | // _net.sid = a; |
mazgch | 54:7ba8e4c218e2 | 201 | // _net.nid = b; |
mazgch | 79:291df065e345 | 202 | if (c == 0) _net.csd = REG_NONE; // not registered, home network |
mazgch | 79:291df065e345 | 203 | else if (c == 1) _net.csd = REG_HOME; // registered, home network |
mazgch | 79:291df065e345 | 204 | else if (c == 2) _net.csd = REG_NONE; // not registered, but MT is currently searching a new operator to register to |
mazgch | 79:291df065e345 | 205 | else if (c == 3) _net.csd = REG_DENIED; // registration denied |
mazgch | 79:291df065e345 | 206 | else if (c == 5) _net.csd = REG_ROAMING; // registered, roaming |
mazgch | 79:291df065e345 | 207 | _net.psd = _net.csd; // fake PSD registration (CDMA is always registered) |
mazgch | 31:a0bed6c1e05d | 208 | _net.act = ACT_CDMA; |
mazgch | 84:a05edb010176 | 209 | // +CSS: <mode>[,<format>,<oper>[,<AcT>]] |
mazgch | 21:c4d64830bf02 | 210 | } else if (sscanf(cmd, "CSS %*c,%2s,%*d",s) == 1) { |
mazgch | 31:a0bed6c1e05d | 211 | //_net.reg = (strcmp("Z", s) == 0) ? REG_UNKNOWN : REG_HOME; |
mazgch | 21:c4d64830bf02 | 212 | } |
mazgch | 21:c4d64830bf02 | 213 | } else { |
mazgch | 21:c4d64830bf02 | 214 | // GSM/UMTS Specific ------------------------------------------- |
mazgch | 79:291df065e345 | 215 | // +UUPSDD: <profile_id> |
mazgch | 79:291df065e345 | 216 | if (sscanf(cmd, "UUPSDD: %d",&a) == 1) { |
mazgch | 79:291df065e345 | 217 | if (*PROFILE == a) _ip = NOIP; |
mazgch | 79:291df065e345 | 218 | } else { |
mazgch | 79:291df065e345 | 219 | // +CREG|CGREG: <n>,<stat>[,<lac>,<ci>[,AcT[,<rac>]]] // reply to AT+CREG|AT+CGREG |
mazgch | 79:291df065e345 | 220 | // +CREG|CGREG: <stat>[,<lac>,<ci>[,AcT[,<rac>]]] // URC |
mazgch | 79:291df065e345 | 221 | b = 0xFFFF; c = 0xFFFFFFFF; d = -1; |
mazgch | 79:291df065e345 | 222 | r = sscanf(cmd, "%s %*d,%d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d); |
mazgch | 79:291df065e345 | 223 | if (r <= 1) |
mazgch | 79:291df065e345 | 224 | r = sscanf(cmd, "%s %d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d); |
mazgch | 79:291df065e345 | 225 | if (r >= 2) { |
mazgch | 79:291df065e345 | 226 | Reg *reg = !strcmp(s, "CREG:") ? &_net.csd : |
mazgch | 79:291df065e345 | 227 | !strcmp(s, "CGREG:") ? &_net.psd : NULL; |
mazgch | 79:291df065e345 | 228 | if (reg) { |
mazgch | 79:291df065e345 | 229 | // network status |
mazgch | 79:291df065e345 | 230 | if (a == 0) *reg = REG_NONE; // 0: not registered, home network |
mazgch | 79:291df065e345 | 231 | else if (a == 1) *reg = REG_HOME; // 1: registered, home network |
mazgch | 79:291df065e345 | 232 | else if (a == 2) *reg = REG_NONE; // 2: not registered, but MT is currently searching a new operator to register to |
mazgch | 79:291df065e345 | 233 | else if (a == 3) *reg = REG_DENIED; // 3: registration denied |
mazgch | 79:291df065e345 | 234 | else if (a == 4) *reg = REG_UNKNOWN; // 4: unknown |
mazgch | 79:291df065e345 | 235 | else if (a == 5) *reg = REG_ROAMING; // 5: registered, roaming |
mazgch | 79:291df065e345 | 236 | if ((r >= 3) && (b != 0xFFFF)) _net.lac = b; // location area code |
mazgch | 79:291df065e345 | 237 | if ((r >= 4) && (c != 0xFFFFFFFF)) _net.ci = c; // cell ID |
mazgch | 79:291df065e345 | 238 | // access technology |
mazgch | 79:291df065e345 | 239 | if (r >= 5) { |
mazgch | 79:291df065e345 | 240 | if (d == 0) _net.act = ACT_GSM; // 0: GSM |
mazgch | 79:291df065e345 | 241 | else if (d == 1) _net.act = ACT_GSM; // 1: GSM COMPACT |
mazgch | 79:291df065e345 | 242 | else if (d == 2) _net.act = ACT_UTRAN; // 2: UTRAN |
mazgch | 79:291df065e345 | 243 | else if (d == 3) _net.act = ACT_EDGE; // 3: GSM with EDGE availability |
mazgch | 79:291df065e345 | 244 | else if (d == 4) _net.act = ACT_UTRAN; // 4: UTRAN with HSDPA availability |
mazgch | 79:291df065e345 | 245 | else if (d == 5) _net.act = ACT_UTRAN; // 5: UTRAN with HSUPA availability |
mazgch | 79:291df065e345 | 246 | else if (d == 6) _net.act = ACT_UTRAN; // 6: UTRAN with HSDPA and HSUPA availability |
mazgch | 79:291df065e345 | 247 | } |
mazgch | 79:291df065e345 | 248 | } |
mazgch | 54:7ba8e4c218e2 | 249 | } |
mazgch | 21:c4d64830bf02 | 250 | } |
mazgch | 21:c4d64830bf02 | 251 | } |
mazgch | 21:c4d64830bf02 | 252 | } |
mazgch | 21:c4d64830bf02 | 253 | if (cb) { |
mazgch | 21:c4d64830bf02 | 254 | int len = LENGTH(ret); |
mazgch | 21:c4d64830bf02 | 255 | int ret = cb(type, buf, len, param); |
mazgch | 21:c4d64830bf02 | 256 | if (WAIT != ret) |
mazgch | 21:c4d64830bf02 | 257 | return ret; |
mazgch | 21:c4d64830bf02 | 258 | } |
mazgch | 95:8282dbbe1492 | 259 | if (type == TYPE_OK) |
mazgch | 95:8282dbbe1492 | 260 | return RESP_OK; |
mazgch | 95:8282dbbe1492 | 261 | if (type == TYPE_ERROR) |
mazgch | 95:8282dbbe1492 | 262 | return RESP_ERROR; |
mazgch | 95:8282dbbe1492 | 263 | if (type == TYPE_PROMPT) |
mazgch | 95:8282dbbe1492 | 264 | return RESP_PROMPT; |
mazgch | 21:c4d64830bf02 | 265 | } |
mazgch | 21:c4d64830bf02 | 266 | // relax a bit |
mazgch | 95:8282dbbe1492 | 267 | wait_ms(10); |
mazgch | 21:c4d64830bf02 | 268 | } |
mazgch | 75:ce6e12067d0c | 269 | while (!TIMEOUT(timer, timeout_ms)); |
mazgch | 21:c4d64830bf02 | 270 | return WAIT; |
mazgch | 21:c4d64830bf02 | 271 | } |
mazgch | 21:c4d64830bf02 | 272 | |
mazgch | 31:a0bed6c1e05d | 273 | int MDMParser::_cbString(int type, const char* buf, int len, char* str) |
mazgch | 21:c4d64830bf02 | 274 | { |
mazgch | 37:cc3433329d66 | 275 | if (str && (type == TYPE_UNKNOWN)) { |
mazgch | 31:a0bed6c1e05d | 276 | if (sscanf(buf, "\r\n%s\r\n", str) == 1) |
mazgch | 31:a0bed6c1e05d | 277 | /*nothing*/; |
mazgch | 21:c4d64830bf02 | 278 | } |
mazgch | 21:c4d64830bf02 | 279 | return WAIT; |
mazgch | 21:c4d64830bf02 | 280 | } |
mazgch | 21:c4d64830bf02 | 281 | |
mazgch | 31:a0bed6c1e05d | 282 | int MDMParser::_cbInt(int type, const char* buf, int len, int* val) |
mazgch | 31:a0bed6c1e05d | 283 | { |
mazgch | 37:cc3433329d66 | 284 | if (val && (type == TYPE_UNKNOWN)) { |
mazgch | 31:a0bed6c1e05d | 285 | if (sscanf(buf, "\r\n%d\r\n", val) == 1) |
mazgch | 31:a0bed6c1e05d | 286 | /*nothing*/; |
mazgch | 31:a0bed6c1e05d | 287 | } |
mazgch | 31:a0bed6c1e05d | 288 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 289 | } |
mazgch | 31:a0bed6c1e05d | 290 | |
mazgch | 31:a0bed6c1e05d | 291 | // ---------------------------------------------------------------- |
mazgch | 31:a0bed6c1e05d | 292 | |
mazgch | 57:869bd35f44cc | 293 | bool MDMParser::connect( |
mazgch | 57:869bd35f44cc | 294 | const char* simpin, |
mazgch | 80:34985b4d821e | 295 | const char* apn, const char* username, |
mazgch | 80:34985b4d821e | 296 | const char* password, Auth auth, |
mazgch | 74:208e3e32d263 | 297 | PinName pn) |
mazgch | 57:869bd35f44cc | 298 | { |
mazgch | 75:ce6e12067d0c | 299 | bool ok = init(simpin, NULL, pn); |
mazgch | 74:208e3e32d263 | 300 | #ifdef MDM_DEBUG |
mazgch | 75:ce6e12067d0c | 301 | if (_debugLevel >= 1) dumpDevStatus(&_dev); |
mazgch | 74:208e3e32d263 | 302 | #endif |
mazgch | 75:ce6e12067d0c | 303 | if (!ok) |
mazgch | 57:869bd35f44cc | 304 | return false; |
mazgch | 75:ce6e12067d0c | 305 | ok = registerNet(); |
mazgch | 74:208e3e32d263 | 306 | #ifdef MDM_DEBUG |
mazgch | 75:ce6e12067d0c | 307 | if (_debugLevel >= 1) dumpNetStatus(&_net); |
mazgch | 74:208e3e32d263 | 308 | #endif |
mazgch | 75:ce6e12067d0c | 309 | if (!ok) |
mazgch | 57:869bd35f44cc | 310 | return false; |
mazgch | 80:34985b4d821e | 311 | IP ip = join(apn,username,password,auth); |
mazgch | 74:208e3e32d263 | 312 | #ifdef MDM_DEBUG |
mazgch | 74:208e3e32d263 | 313 | if (_debugLevel >= 1) dumpIp(ip); |
mazgch | 74:208e3e32d263 | 314 | #endif |
mazgch | 57:869bd35f44cc | 315 | if (ip == NOIP) |
mazgch | 57:869bd35f44cc | 316 | return false; |
mazgch | 57:869bd35f44cc | 317 | return true; |
mazgch | 57:869bd35f44cc | 318 | } |
mazgch | 57:869bd35f44cc | 319 | |
mazgch | 74:208e3e32d263 | 320 | bool MDMParser::init(const char* simpin, DevStatus* status, PinName pn) |
mazgch | 21:c4d64830bf02 | 321 | { |
mazgch | 74:208e3e32d263 | 322 | int i = 10; |
mazgch | 95:8282dbbe1492 | 323 | LOCK(); |
mazgch | 76:f7c3dd568dae | 324 | memset(&_dev, 0, sizeof(_dev)); |
mazgch | 74:208e3e32d263 | 325 | if (pn != NC) { |
mazgch | 74:208e3e32d263 | 326 | INFO("Modem::wakeup\r\n"); |
mazgch | 74:208e3e32d263 | 327 | DigitalOut pin(pn, 1); |
mazgch | 74:208e3e32d263 | 328 | while (i--) { |
mazgch | 79:291df065e345 | 329 | // SARA-U2/LISA-U2 50..80us |
irsanjul | 120:353754a2bcd9 | 330 | /*pin = 0; ::wait_us(50); |
mazgch | 95:8282dbbe1492 | 331 | pin = 1; ::wait_ms(10); |
irsanjul | 120:353754a2bcd9 | 332 | */ |
mazgch | 98:c786461edd40 | 333 | // SARA-G35 >5ms, LISA-C2 > 150ms, LEON-G2 >5ms |
mazgch | 95:8282dbbe1492 | 334 | pin = 0; ::wait_ms(150); |
mazgch | 95:8282dbbe1492 | 335 | pin = 1; ::wait_ms(100); |
mazgch | 79:291df065e345 | 336 | |
mazgch | 79:291df065e345 | 337 | // purge any messages |
mazgch | 101:edfeb8af206e | 338 | purge(); |
mazgch | 95:8282dbbe1492 | 339 | |
mazgch | 101:edfeb8af206e | 340 | // check interface |
mazgch | 74:208e3e32d263 | 341 | sendFormated("AT\r\n"); |
mazgch | 101:edfeb8af206e | 342 | int r = waitFinalResp(NULL,NULL,1000); |
mazgch | 95:8282dbbe1492 | 343 | if(RESP_OK == r) break; |
mazgch | 74:208e3e32d263 | 344 | } |
mazgch | 75:ce6e12067d0c | 345 | if (i < 0) { |
mazgch | 95:8282dbbe1492 | 346 | ERROR("No Reply from Modem\r\n"); |
mazgch | 95:8282dbbe1492 | 347 | goto failure; |
mazgch | 75:ce6e12067d0c | 348 | } |
mazgch | 21:c4d64830bf02 | 349 | } |
mazgch | 76:f7c3dd568dae | 350 | _init = true; |
mazgch | 75:ce6e12067d0c | 351 | |
mazgch | 74:208e3e32d263 | 352 | INFO("Modem::init\r\n"); |
mazgch | 21:c4d64830bf02 | 353 | // echo off |
mazgch | 21:c4d64830bf02 | 354 | sendFormated("AT E0\r\n"); |
mazgch | 52:8071747a7cb3 | 355 | if(RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 356 | goto failure; |
mazgch | 21:c4d64830bf02 | 357 | // enable verbose error messages |
mazgch | 21:c4d64830bf02 | 358 | sendFormated("AT+CMEE=2\r\n"); |
mazgch | 52:8071747a7cb3 | 359 | if(RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 360 | goto failure; |
mazgch | 21:c4d64830bf02 | 361 | // set baud rate |
mazgch | 21:c4d64830bf02 | 362 | sendFormated("AT+IPR=115200\r\n"); |
mazgch | 52:8071747a7cb3 | 363 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 364 | goto failure; |
mazgch | 112:89b5b21db71e | 365 | // wait some time until baudrate is applied |
mazgch | 112:89b5b21db71e | 366 | wait_ms(200); // SARA-G > 40ms |
mazgch | 21:c4d64830bf02 | 367 | // identify the module |
mazgch | 21:c4d64830bf02 | 368 | sendFormated("ATI\r\n"); |
mazgch | 52:8071747a7cb3 | 369 | if (RESP_OK != waitFinalResp(_cbATI, &_dev.dev)) |
mazgch | 95:8282dbbe1492 | 370 | goto failure; |
mazgch | 32:8f12ac182bbb | 371 | if (_dev.dev == DEV_UNKNOWN) |
mazgch | 95:8282dbbe1492 | 372 | goto failure; |
mazgch | 32:8f12ac182bbb | 373 | // device specific init |
mazgch | 32:8f12ac182bbb | 374 | if (_dev.dev == DEV_LISA_C200) { |
mazgch | 32:8f12ac182bbb | 375 | // get the manufacturer |
mazgch | 32:8f12ac182bbb | 376 | sendFormated("AT+GMI\r\n"); |
mazgch | 52:8071747a7cb3 | 377 | if (RESP_OK != waitFinalResp(_cbString, _dev.manu)) |
mazgch | 95:8282dbbe1492 | 378 | goto failure; |
mazgch | 32:8f12ac182bbb | 379 | // get the model identification |
mazgch | 32:8f12ac182bbb | 380 | sendFormated("AT+GMM\r\n"); |
mazgch | 52:8071747a7cb3 | 381 | if (RESP_OK != waitFinalResp(_cbString, _dev.model)) |
mazgch | 95:8282dbbe1492 | 382 | goto failure; |
mazgch | 32:8f12ac182bbb | 383 | // get the sw version |
mazgch | 32:8f12ac182bbb | 384 | sendFormated("AT+GMR\r\n"); |
mazgch | 52:8071747a7cb3 | 385 | if (RESP_OK != waitFinalResp(_cbString, _dev.ver)) |
mazgch | 95:8282dbbe1492 | 386 | goto failure; |
mazgch | 95:8282dbbe1492 | 387 | // get the pseudo ESN or MEID |
mazgch | 21:c4d64830bf02 | 388 | sendFormated("AT+GSN\r\n"); |
mazgch | 52:8071747a7cb3 | 389 | if (RESP_OK != waitFinalResp(_cbString, _dev.meid)) |
mazgch | 95:8282dbbe1492 | 390 | goto failure; |
mazgch | 50:d76aece8038f | 391 | #if 0 |
mazgch | 50:d76aece8038f | 392 | // enable power saving |
mazgch | 50:d76aece8038f | 393 | if (_dev.lpm != LPM_DISABLED) { |
mazgch | 50:d76aece8038f | 394 | // enable power saving (requires flow control, cts at least) |
mazgch | 50:d76aece8038f | 395 | sendFormated("AT+UPSV=1,1280\r\n"); |
mazgch | 52:8071747a7cb3 | 396 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 397 | goto failure; |
mazgch | 50:d76aece8038f | 398 | _dev.lpm = LPM_ACTIVE; |
mazgch | 50:d76aece8038f | 399 | } |
mazgch | 50:d76aece8038f | 400 | #endif |
mazgch | 26:07be5faf8925 | 401 | } else { |
mazgch | 98:c786461edd40 | 402 | if ((_dev.dev == DEV_LISA_U200) || (_dev.dev == DEV_LEON_G200)) { |
mazgch | 35:9275215a3a5b | 403 | // enable the network identification feature |
mazgch | 21:c4d64830bf02 | 404 | sendFormated("AT+UGPIOC=20,2\r\n"); |
mazgch | 52:8071747a7cb3 | 405 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 406 | goto failure; |
mazgch | 98:c786461edd40 | 407 | } else if ((_dev.dev == DEV_SARA_U260) || (_dev.dev == DEV_SARA_U270) || |
mazgch | 98:c786461edd40 | 408 | (_dev.dev == DEV_SARA_G350)) { |
mazgch | 35:9275215a3a5b | 409 | // enable the network identification feature |
mazgch | 21:c4d64830bf02 | 410 | sendFormated("AT+UGPIOC=16,2\r\n"); |
mazgch | 52:8071747a7cb3 | 411 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 412 | goto failure; |
mazgch | 21:c4d64830bf02 | 413 | } |
mazgch | 21:c4d64830bf02 | 414 | // check the sim card |
mazgch | 31:a0bed6c1e05d | 415 | for (int i = 0; (i < 5) && (_dev.sim != SIM_READY); i++) { |
mazgch | 21:c4d64830bf02 | 416 | sendFormated("AT+CPIN?\r\n"); |
mazgch | 31:a0bed6c1e05d | 417 | int ret = waitFinalResp(_cbCPIN, &_dev.sim); |
mazgch | 62:a02f026bdd7c | 418 | // having an error here is ok (sim may still be initializing) |
mazgch | 52:8071747a7cb3 | 419 | if ((RESP_OK != ret) && (RESP_ERROR != ret)) |
mazgch | 95:8282dbbe1492 | 420 | goto failure; |
mazgch | 31:a0bed6c1e05d | 421 | // Enter PIN if needed |
mazgch | 77:55788e619858 | 422 | if (_dev.sim == SIM_PIN) { |
mazgch | 57:869bd35f44cc | 423 | if (!simpin) { |
mazgch | 75:ce6e12067d0c | 424 | ERROR("SIM PIN not available\r\n"); |
mazgch | 95:8282dbbe1492 | 425 | goto failure; |
mazgch | 31:a0bed6c1e05d | 426 | } |
mazgch | 57:869bd35f44cc | 427 | sendFormated("AT+CPIN=%s\r\n", simpin); |
mazgch | 52:8071747a7cb3 | 428 | if (RESP_OK != waitFinalResp(_cbCPIN, &_dev.sim)) |
mazgch | 95:8282dbbe1492 | 429 | goto failure; |
mazgch | 62:a02f026bdd7c | 430 | } else if (_dev.sim != SIM_READY) { |
mazgch | 95:8282dbbe1492 | 431 | wait_ms(1000); |
mazgch | 62:a02f026bdd7c | 432 | } |
mazgch | 21:c4d64830bf02 | 433 | } |
mazgch | 77:55788e619858 | 434 | if (_dev.sim != SIM_READY) { |
mazgch | 77:55788e619858 | 435 | if (_dev.sim == SIM_MISSING) |
mazgch | 77:55788e619858 | 436 | ERROR("SIM not inserted\r\n"); |
mazgch | 95:8282dbbe1492 | 437 | goto failure; |
mazgch | 77:55788e619858 | 438 | } |
mazgch | 32:8f12ac182bbb | 439 | // get the manufacturer |
mazgch | 32:8f12ac182bbb | 440 | sendFormated("AT+CGMI\r\n"); |
mazgch | 52:8071747a7cb3 | 441 | if (RESP_OK != waitFinalResp(_cbString, _dev.manu)) |
mazgch | 95:8282dbbe1492 | 442 | goto failure; |
mazgch | 32:8f12ac182bbb | 443 | // get the model identification |
mazgch | 32:8f12ac182bbb | 444 | sendFormated("AT+CGMM\r\n"); |
mazgch | 52:8071747a7cb3 | 445 | if (RESP_OK != waitFinalResp(_cbString, _dev.model)) |
mazgch | 95:8282dbbe1492 | 446 | goto failure; |
mazgch | 32:8f12ac182bbb | 447 | // get the |
mazgch | 32:8f12ac182bbb | 448 | sendFormated("AT+CGMR\r\n"); |
mazgch | 52:8071747a7cb3 | 449 | if (RESP_OK != waitFinalResp(_cbString, _dev.ver)) |
irsanjul | 120:353754a2bcd9 | 450 | goto failure; |
mazgch | 21:c4d64830bf02 | 451 | // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card. |
mazgch | 21:c4d64830bf02 | 452 | // ICCID is a serial number identifying the SIM. |
mazgch | 21:c4d64830bf02 | 453 | sendFormated("AT+CCID\r\n"); |
mazgch | 52:8071747a7cb3 | 454 | if (RESP_OK != waitFinalResp(_cbCCID, _dev.ccid)) |
mazgch | 95:8282dbbe1492 | 455 | goto failure; |
mazgch | 21:c4d64830bf02 | 456 | // Returns the product serial number, IMEI (International Mobile Equipment Identity) |
mazgch | 21:c4d64830bf02 | 457 | sendFormated("AT+CGSN\r\n"); |
mazgch | 52:8071747a7cb3 | 458 | if (RESP_OK != waitFinalResp(_cbString, _dev.imei)) |
mazgch | 95:8282dbbe1492 | 459 | goto failure; |
mazgch | 50:d76aece8038f | 460 | // enable power saving |
mazgch | 50:d76aece8038f | 461 | if (_dev.lpm != LPM_DISABLED) { |
mazgch | 50:d76aece8038f | 462 | // enable power saving (requires flow control, cts at least) |
mazgch | 50:d76aece8038f | 463 | sendFormated("AT+UPSV=1\r\n"); |
mazgch | 52:8071747a7cb3 | 464 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 465 | goto failure; |
mazgch | 50:d76aece8038f | 466 | _dev.lpm = LPM_ACTIVE; |
mazgch | 50:d76aece8038f | 467 | } |
mazgch | 79:291df065e345 | 468 | // enable the psd registration unsolicited result code |
mazgch | 79:291df065e345 | 469 | sendFormated("AT+CGREG=2\r\n"); |
mazgch | 79:291df065e345 | 470 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 471 | goto failure; |
mazgch | 21:c4d64830bf02 | 472 | } |
mazgch | 79:291df065e345 | 473 | // enable the network registration unsolicited result code |
mazgch | 79:291df065e345 | 474 | sendFormated("AT+CREG=%d\r\n", (_dev.dev == DEV_LISA_C200) ? 1 : 2); |
mazgch | 79:291df065e345 | 475 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 476 | goto failure; |
mazgch | 31:a0bed6c1e05d | 477 | // Setup SMS in text mode |
mazgch | 31:a0bed6c1e05d | 478 | sendFormated("AT+CMGF=1\r\n"); |
mazgch | 52:8071747a7cb3 | 479 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 480 | goto failure; |
mazgch | 31:a0bed6c1e05d | 481 | // setup new message indication |
mazgch | 75:ce6e12067d0c | 482 | sendFormated("AT+CNMI=2,1\r\n"); |
mazgch | 52:8071747a7cb3 | 483 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 484 | goto failure; |
irsanjul | 120:353754a2bcd9 | 485 | // setup audio path mode |
irsanjul | 121:3e1d84b2d34c | 486 | //sendFormated("AT+USPM=0,0,0,0,0\r\n"); |
irsanjul | 121:3e1d84b2d34c | 487 | //if (RESP_OK != waitFinalResp()) |
irsanjul | 121:3e1d84b2d34c | 488 | //goto failure; |
irsanjul | 120:353754a2bcd9 | 489 | // setup loudness speaker |
irsanjul | 121:3e1d84b2d34c | 490 | //sendFormated("ATL2\r\n"); |
irsanjul | 121:3e1d84b2d34c | 491 | //if (RESP_OK != waitFinalResp()) |
irsanjul | 121:3e1d84b2d34c | 492 | //goto failure; |
irsanjul | 120:353754a2bcd9 | 493 | // setup report |
irsanjul | 120:353754a2bcd9 | 494 | sendFormated("AT+CMER=1,0,0,2,1\r\n"); |
irsanjul | 120:353754a2bcd9 | 495 | if (RESP_OK != waitFinalResp()) |
irsanjul | 120:353754a2bcd9 | 496 | goto failure; |
irsanjul | 120:353754a2bcd9 | 497 | // setup call status |
irsanjul | 120:353754a2bcd9 | 498 | sendFormated("AT+UCALLSTAT=1\r\n"); |
irsanjul | 120:353754a2bcd9 | 499 | if (RESP_OK != waitFinalResp()) |
irsanjul | 120:353754a2bcd9 | 500 | goto failure; |
irsanjul | 120:353754a2bcd9 | 501 | // setup calling line identification |
irsanjul | 120:353754a2bcd9 | 502 | sendFormated("AT+CLIP=1\r\n"); |
irsanjul | 120:353754a2bcd9 | 503 | if (RESP_OK != waitFinalResp()) |
irsanjul | 120:353754a2bcd9 | 504 | goto failure; |
mazgch | 21:c4d64830bf02 | 505 | // Request IMSI (International Mobile Subscriber Identification) |
mazgch | 21:c4d64830bf02 | 506 | sendFormated("AT+CIMI\r\n"); |
mazgch | 52:8071747a7cb3 | 507 | if (RESP_OK != waitFinalResp(_cbString, _dev.imsi)) |
mazgch | 95:8282dbbe1492 | 508 | goto failure; |
irsanjul | 120:353754a2bcd9 | 509 | |
mazgch | 28:4d9509e3b1cf | 510 | if (status) |
mazgch | 31:a0bed6c1e05d | 511 | memcpy(status, &_dev, sizeof(DevStatus)); |
mazgch | 95:8282dbbe1492 | 512 | UNLOCK(); |
mazgch | 31:a0bed6c1e05d | 513 | return true; |
mazgch | 95:8282dbbe1492 | 514 | failure: |
mazgch | 95:8282dbbe1492 | 515 | unlock(); |
mazgch | 95:8282dbbe1492 | 516 | return false; |
mazgch | 31:a0bed6c1e05d | 517 | } |
mazgch | 31:a0bed6c1e05d | 518 | |
mazgch | 74:208e3e32d263 | 519 | bool MDMParser::powerOff(void) |
mazgch | 74:208e3e32d263 | 520 | { |
mazgch | 95:8282dbbe1492 | 521 | bool ok = false; |
mazgch | 76:f7c3dd568dae | 522 | if (_init) { |
mazgch | 95:8282dbbe1492 | 523 | LOCK(); |
mazgch | 76:f7c3dd568dae | 524 | INFO("Modem::powerOff\r\n"); |
mazgch | 76:f7c3dd568dae | 525 | sendFormated("AT+CPWROFF\r\n"); |
mazgch | 95:8282dbbe1492 | 526 | if (RESP_OK == waitFinalResp(NULL,NULL,120*1000)) { |
mazgch | 95:8282dbbe1492 | 527 | _init = false; |
mazgch | 95:8282dbbe1492 | 528 | ok = true; |
mazgch | 95:8282dbbe1492 | 529 | } |
mazgch | 95:8282dbbe1492 | 530 | UNLOCK(); |
mazgch | 76:f7c3dd568dae | 531 | } |
mazgch | 95:8282dbbe1492 | 532 | return ok; |
mazgch | 74:208e3e32d263 | 533 | } |
mazgch | 74:208e3e32d263 | 534 | |
mazgch | 32:8f12ac182bbb | 535 | int MDMParser::_cbATI(int type, const char* buf, int len, Dev* dev) |
mazgch | 31:a0bed6c1e05d | 536 | { |
mazgch | 37:cc3433329d66 | 537 | if ((type == TYPE_UNKNOWN) && dev) { |
mazgch | 98:c786461edd40 | 538 | if (strstr(buf, "SARA-G350")) *dev = DEV_SARA_G350; |
mazgch | 98:c786461edd40 | 539 | else if (strstr(buf, "LISA-U200")) *dev = DEV_LISA_U200; |
mazgch | 98:c786461edd40 | 540 | else if (strstr(buf, "LISA-C200")) *dev = DEV_LISA_C200; |
mazgch | 98:c786461edd40 | 541 | else if (strstr(buf, "SARA-U260")) *dev = DEV_SARA_U260; |
mazgch | 98:c786461edd40 | 542 | else if (strstr(buf, "SARA-U270")) *dev = DEV_SARA_U270; |
mazgch | 98:c786461edd40 | 543 | else if (strstr(buf, "LEON-G200")) *dev = DEV_LEON_G200; |
mazgch | 28:4d9509e3b1cf | 544 | } |
mazgch | 31:a0bed6c1e05d | 545 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 546 | } |
mazgch | 31:a0bed6c1e05d | 547 | |
mazgch | 31:a0bed6c1e05d | 548 | int MDMParser::_cbCPIN(int type, const char* buf, int len, Sim* sim) |
mazgch | 31:a0bed6c1e05d | 549 | { |
mazgch | 75:ce6e12067d0c | 550 | if (sim) { |
mazgch | 75:ce6e12067d0c | 551 | if (type == TYPE_PLUS){ |
mazgch | 75:ce6e12067d0c | 552 | char s[16]; |
mazgch | 75:ce6e12067d0c | 553 | if (sscanf(buf, "\r\n+CPIN: %[^\r]\r\n", s) >= 1) |
mazgch | 75:ce6e12067d0c | 554 | *sim = (0 == strcmp("READY", s)) ? SIM_READY : SIM_PIN; |
mazgch | 75:ce6e12067d0c | 555 | } else if (type == TYPE_ERROR) { |
mazgch | 75:ce6e12067d0c | 556 | if (strstr(buf, "+CME ERROR: SIM not inserted")) |
mazgch | 75:ce6e12067d0c | 557 | *sim = SIM_MISSING; |
mazgch | 31:a0bed6c1e05d | 558 | } |
mazgch | 31:a0bed6c1e05d | 559 | } |
mazgch | 31:a0bed6c1e05d | 560 | return WAIT; |
mazgch | 21:c4d64830bf02 | 561 | } |
mazgch | 21:c4d64830bf02 | 562 | |
mazgch | 26:07be5faf8925 | 563 | int MDMParser::_cbCCID(int type, const char* buf, int len, char* ccid) |
mazgch | 26:07be5faf8925 | 564 | { |
mazgch | 26:07be5faf8925 | 565 | if ((type == TYPE_PLUS) && ccid){ |
mazgch | 26:07be5faf8925 | 566 | if (sscanf(buf, "\r\n+CCID: %[^\r]\r\n", ccid) == 1) |
mazgch | 31:a0bed6c1e05d | 567 | /*TRACE("Got CCID: %s\r\n", ccid)*/; |
mazgch | 26:07be5faf8925 | 568 | } |
mazgch | 26:07be5faf8925 | 569 | return WAIT; |
mazgch | 26:07be5faf8925 | 570 | } |
mazgch | 26:07be5faf8925 | 571 | |
mazgch | 75:ce6e12067d0c | 572 | bool MDMParser::registerNet(NetStatus* status /*= NULL*/, int timeout_ms /*= 180000*/) |
mazgch | 75:ce6e12067d0c | 573 | { |
mazgch | 75:ce6e12067d0c | 574 | Timer timer; |
mazgch | 75:ce6e12067d0c | 575 | timer.start(); |
mazgch | 75:ce6e12067d0c | 576 | INFO("Modem::register\r\n"); |
mazgch | 75:ce6e12067d0c | 577 | while (!checkNetStatus(status) && !TIMEOUT(timer, timeout_ms)) |
mazgch | 95:8282dbbe1492 | 578 | wait_ms(1000); |
mazgch | 79:291df065e345 | 579 | if (_net.csd == REG_DENIED) ERROR("CSD Registration Denied\r\n"); |
mazgch | 79:291df065e345 | 580 | if (_net.psd == REG_DENIED) ERROR("PSD Registration Denied\r\n"); |
mazgch | 79:291df065e345 | 581 | return REG_OK(_net.csd) || REG_OK(_net.psd); |
mazgch | 75:ce6e12067d0c | 582 | } |
mazgch | 75:ce6e12067d0c | 583 | |
mazgch | 28:4d9509e3b1cf | 584 | bool MDMParser::checkNetStatus(NetStatus* status /*= NULL*/) |
mazgch | 21:c4d64830bf02 | 585 | { |
mazgch | 95:8282dbbe1492 | 586 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 587 | LOCK(); |
mazgch | 75:ce6e12067d0c | 588 | memset(&_net, 0, sizeof(_net)); |
mazgch | 75:ce6e12067d0c | 589 | _net.lac = 0xFFFF; |
mazgch | 75:ce6e12067d0c | 590 | _net.ci = 0xFFFFFFFF; |
mazgch | 21:c4d64830bf02 | 591 | // check registration |
mazgch | 21:c4d64830bf02 | 592 | sendFormated("AT+CREG?\r\n"); |
mazgch | 88:135fb4bb7aac | 593 | waitFinalResp(); // don't fail as service could be not subscribed |
mazgch | 79:291df065e345 | 594 | if (_dev.dev != DEV_LISA_C200) { |
mazgch | 79:291df065e345 | 595 | // check PSD registration |
mazgch | 79:291df065e345 | 596 | sendFormated("AT+CGREG?\r\n"); |
mazgch | 88:135fb4bb7aac | 597 | waitFinalResp(); // don't fail as service could be not subscribed |
mazgch | 79:291df065e345 | 598 | } |
mazgch | 79:291df065e345 | 599 | if (REG_OK(_net.csd) || REG_OK(_net.psd)) |
mazgch | 75:ce6e12067d0c | 600 | { |
mazgch | 75:ce6e12067d0c | 601 | // check modem specific status messages |
mazgch | 75:ce6e12067d0c | 602 | if (_dev.dev == DEV_LISA_C200) { |
mazgch | 75:ce6e12067d0c | 603 | sendFormated("AT+CSS?\r\n"); |
mazgch | 75:ce6e12067d0c | 604 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 605 | goto failure; |
mazgch | 81:3966a5c17037 | 606 | while (1) { |
mazgch | 81:3966a5c17037 | 607 | // get the Telephone number |
mazgch | 81:3966a5c17037 | 608 | sendFormated("AT$MDN?\r\n"); |
mazgch | 81:3966a5c17037 | 609 | if (RESP_OK != waitFinalResp(_cbString, _net.num)) |
mazgch | 95:8282dbbe1492 | 610 | goto failure; |
mazgch | 81:3966a5c17037 | 611 | // check if we have a Mobile Directory Number |
mazgch | 81:3966a5c17037 | 612 | if (*_net.num && (memcmp(_net.num, "000000", 6) != 0)) |
mazgch | 81:3966a5c17037 | 613 | break; |
mazgch | 81:3966a5c17037 | 614 | |
mazgch | 81:3966a5c17037 | 615 | INFO("Device not yet activated\r\n"); |
mazgch | 81:3966a5c17037 | 616 | INFO("Make sure you have a valid contract with the network operator for this device.\r\n"); |
mazgch | 81:3966a5c17037 | 617 | // Check if the the version contains a V for Verizon |
mazgch | 81:3966a5c17037 | 618 | // Verizon: E0.V.xx.00.xxR, |
mazgch | 81:3966a5c17037 | 619 | // Sprint E0.S.xx.00.xxR |
mazgch | 81:3966a5c17037 | 620 | if (_dev.ver[3] == 'V') { |
mazgch | 81:3966a5c17037 | 621 | int i; |
mazgch | 81:3966a5c17037 | 622 | INFO("Start device over-the-air activation (this can take a few minutes)\r\n"); |
mazgch | 81:3966a5c17037 | 623 | sendFormated("AT+CDV=*22899\r\n"); |
mazgch | 81:3966a5c17037 | 624 | i = 1; |
mazgch | 82:055dcfcf9dcc | 625 | if ((RESP_OK != waitFinalResp(_cbUACTIND, &i, 120*1000)) || (i == 1)) { |
mazgch | 81:3966a5c17037 | 626 | ERROR("Device over-the-air activation failed\r\n"); |
mazgch | 95:8282dbbe1492 | 627 | goto failure; |
mazgch | 81:3966a5c17037 | 628 | } |
mazgch | 81:3966a5c17037 | 629 | INFO("Device over-the-air activation successful\r\n"); |
mazgch | 81:3966a5c17037 | 630 | |
mazgch | 81:3966a5c17037 | 631 | INFO("Start PRL over-the-air update (this can take a few minutes)\r\n"); |
mazgch | 81:3966a5c17037 | 632 | sendFormated("AT+CDV=*22891\r\n"); |
mazgch | 81:3966a5c17037 | 633 | i = 1; |
mazgch | 82:055dcfcf9dcc | 634 | if ((RESP_OK != waitFinalResp(_cbUACTIND, &i, 120*1000)) || (i == 1)) { |
mazgch | 81:3966a5c17037 | 635 | ERROR("PRL over-the-air update failed\r\n"); |
mazgch | 95:8282dbbe1492 | 636 | goto failure; |
mazgch | 81:3966a5c17037 | 637 | } |
mazgch | 81:3966a5c17037 | 638 | INFO("PRL over-the-air update successful\r\n"); |
mazgch | 81:3966a5c17037 | 639 | |
mazgch | 81:3966a5c17037 | 640 | } else { |
mazgch | 81:3966a5c17037 | 641 | // Sprint or Aeris |
mazgch | 81:3966a5c17037 | 642 | INFO("Wait for OMA-DM over-the-air activation (this can take a few minutes)\r\n"); |
mazgch | 95:8282dbbe1492 | 643 | wait_ms(120*1000); |
mazgch | 81:3966a5c17037 | 644 | } |
mazgch | 81:3966a5c17037 | 645 | } |
mazgch | 75:ce6e12067d0c | 646 | // get the the Network access identifier string |
mazgch | 75:ce6e12067d0c | 647 | char nai[64]; |
mazgch | 75:ce6e12067d0c | 648 | sendFormated("AT$QCMIPNAI?\r\n"); |
mazgch | 75:ce6e12067d0c | 649 | if (RESP_OK != waitFinalResp(_cbString, nai)) |
mazgch | 95:8282dbbe1492 | 650 | goto failure; |
mazgch | 75:ce6e12067d0c | 651 | } else { |
mazgch | 75:ce6e12067d0c | 652 | sendFormated("AT+COPS?\r\n"); |
mazgch | 75:ce6e12067d0c | 653 | if (RESP_OK != waitFinalResp(_cbCOPS, &_net)) |
mazgch | 95:8282dbbe1492 | 654 | goto failure; |
mazgch | 95:8282dbbe1492 | 655 | // get the MSISDNs related to this subscriber |
mazgch | 75:ce6e12067d0c | 656 | sendFormated("AT+CNUM\r\n"); |
mazgch | 75:ce6e12067d0c | 657 | if (RESP_OK != waitFinalResp(_cbCNUM, _net.num)) |
mazgch | 95:8282dbbe1492 | 658 | goto failure; |
mazgch | 75:ce6e12067d0c | 659 | } |
mazgch | 95:8282dbbe1492 | 660 | // get the signal strength indication |
mazgch | 75:ce6e12067d0c | 661 | sendFormated("AT+CSQ\r\n"); |
mazgch | 75:ce6e12067d0c | 662 | if (RESP_OK != waitFinalResp(_cbCSQ, &_net)) |
mazgch | 95:8282dbbe1492 | 663 | goto failure; |
mazgch | 75:ce6e12067d0c | 664 | } |
mazgch | 28:4d9509e3b1cf | 665 | if (status) { |
mazgch | 31:a0bed6c1e05d | 666 | memcpy(status, &_net, sizeof(NetStatus)); |
mazgch | 25:4045d02e44f1 | 667 | } |
mazgch | 95:8282dbbe1492 | 668 | ok = REG_DONE(_net.csd) && REG_DONE(_net.psd); |
mazgch | 95:8282dbbe1492 | 669 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 670 | return ok; |
mazgch | 95:8282dbbe1492 | 671 | failure: |
mazgch | 95:8282dbbe1492 | 672 | unlock(); |
mazgch | 95:8282dbbe1492 | 673 | return false; |
mazgch | 31:a0bed6c1e05d | 674 | } |
mazgch | 31:a0bed6c1e05d | 675 | |
mazgch | 31:a0bed6c1e05d | 676 | int MDMParser::_cbCOPS(int type, const char* buf, int len, NetStatus* status) |
mazgch | 31:a0bed6c1e05d | 677 | { |
mazgch | 31:a0bed6c1e05d | 678 | if ((type == TYPE_PLUS) && status){ |
mazgch | 31:a0bed6c1e05d | 679 | int act = 99; |
mazgch | 31:a0bed6c1e05d | 680 | // +COPS: <mode>[,<format>,<oper>[,<AcT>]] |
mazgch | 31:a0bed6c1e05d | 681 | if (sscanf(buf, "\r\n+COPS: %*d,%*d,\"%[^\"]\",%d",status->opr,&act) >= 1) { |
mazgch | 31:a0bed6c1e05d | 682 | if (act == 0) status->act = ACT_GSM; // 0: GSM, |
mazgch | 31:a0bed6c1e05d | 683 | else if (act == 2) status->act = ACT_UTRAN; // 2: UTRAN |
mazgch | 31:a0bed6c1e05d | 684 | } |
mazgch | 31:a0bed6c1e05d | 685 | } |
mazgch | 31:a0bed6c1e05d | 686 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 687 | } |
mazgch | 31:a0bed6c1e05d | 688 | |
mazgch | 31:a0bed6c1e05d | 689 | int MDMParser::_cbCNUM(int type, const char* buf, int len, char* num) |
mazgch | 31:a0bed6c1e05d | 690 | { |
mazgch | 31:a0bed6c1e05d | 691 | if ((type == TYPE_PLUS) && num){ |
mazgch | 31:a0bed6c1e05d | 692 | int a; |
mazgch | 31:a0bed6c1e05d | 693 | if ((sscanf(buf, "\r\n+CNUM: \"My Number\",\"%31[^\"]\",%d", num, &a) == 2) && |
mazgch | 31:a0bed6c1e05d | 694 | ((a == 129) || (a == 145))) { |
mazgch | 31:a0bed6c1e05d | 695 | } |
mazgch | 31:a0bed6c1e05d | 696 | } |
mazgch | 31:a0bed6c1e05d | 697 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 698 | } |
mazgch | 31:a0bed6c1e05d | 699 | |
mazgch | 54:7ba8e4c218e2 | 700 | int MDMParser::_cbCSQ(int type, const char* buf, int len, NetStatus* status) |
mazgch | 31:a0bed6c1e05d | 701 | { |
mazgch | 54:7ba8e4c218e2 | 702 | if ((type == TYPE_PLUS) && status){ |
mazgch | 54:7ba8e4c218e2 | 703 | int a,b; |
mazgch | 54:7ba8e4c218e2 | 704 | char _ber[] = { 49, 43, 37, 25, 19, 13, 7, 0 }; // see 3GPP TS 45.008 [20] subclause 8.2.4 |
mazgch | 31:a0bed6c1e05d | 705 | // +CSQ: <rssi>,<qual> |
mazgch | 54:7ba8e4c218e2 | 706 | if (sscanf(buf, "\r\n+CSQ: %d,%d",&a,&b) == 2) { |
mazgch | 54:7ba8e4c218e2 | 707 | if (a != 99) status->rssi = -113 + 2*a; // 0: -113 1: -111 ... 30: -53 dBm with 2 dBm steps |
mazgch | 54:7ba8e4c218e2 | 708 | if ((b != 99) && (b < sizeof(_ber))) status->ber = _ber[b]; // |
mazgch | 31:a0bed6c1e05d | 709 | } |
mazgch | 31:a0bed6c1e05d | 710 | } |
mazgch | 31:a0bed6c1e05d | 711 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 712 | } |
mazgch | 21:c4d64830bf02 | 713 | |
mazgch | 81:3966a5c17037 | 714 | int MDMParser::_cbUACTIND(int type, const char* buf, int len, int* i) |
mazgch | 81:3966a5c17037 | 715 | { |
mazgch | 81:3966a5c17037 | 716 | if ((type == TYPE_PLUS) && i){ |
mazgch | 82:055dcfcf9dcc | 717 | int a; |
mazgch | 82:055dcfcf9dcc | 718 | if (sscanf(buf, "\r\n+UACTIND: %d", &a) == 1) { |
mazgch | 82:055dcfcf9dcc | 719 | *i = a; |
mazgch | 81:3966a5c17037 | 720 | } |
mazgch | 81:3966a5c17037 | 721 | } |
mazgch | 81:3966a5c17037 | 722 | return WAIT; |
mazgch | 81:3966a5c17037 | 723 | } |
mazgch | 81:3966a5c17037 | 724 | |
mazgch | 21:c4d64830bf02 | 725 | // ---------------------------------------------------------------- |
mazgch | 21:c4d64830bf02 | 726 | // internet connection |
mazgch | 21:c4d64830bf02 | 727 | |
mazgch | 79:291df065e345 | 728 | MDMParser::IP MDMParser::join(const char* apn /*= NULL*/, const char* username /*= NULL*/, |
mazgch | 79:291df065e345 | 729 | const char* password /*= NULL*/, Auth auth /*= AUTH_DETECT*/) |
mazgch | 21:c4d64830bf02 | 730 | { |
mazgch | 95:8282dbbe1492 | 731 | LOCK(); |
mazgch | 78:caf0014375cb | 732 | INFO("Modem::join\r\n"); |
mazgch | 32:8f12ac182bbb | 733 | _ip = NOIP; |
mazgch | 32:8f12ac182bbb | 734 | if (_dev.dev == DEV_LISA_C200) { |
mazgch | 76:f7c3dd568dae | 735 | // make a dumy dns lookup (which will fail, so ignore the result) |
mazgch | 76:f7c3dd568dae | 736 | sendFormated("AT+UDNSRN=0,\"u-blox.com\"\r\n"); |
mazgch | 76:f7c3dd568dae | 737 | waitFinalResp(); |
mazgch | 76:f7c3dd568dae | 738 | // This fake lookup will enable the IP connection and we |
mazgch | 76:f7c3dd568dae | 739 | // should have an IP after this, so we check it |
mazgch | 76:f7c3dd568dae | 740 | |
mazgch | 21:c4d64830bf02 | 741 | //Get local IP address |
mazgch | 21:c4d64830bf02 | 742 | sendFormated("AT+CMIP?\r\n"); |
mazgch | 52:8071747a7cb3 | 743 | if (RESP_OK != waitFinalResp(_cbCMIP, &_ip)) |
mazgch | 95:8282dbbe1492 | 744 | goto failure; |
mazgch | 21:c4d64830bf02 | 745 | } else { |
mazgch | 21:c4d64830bf02 | 746 | // check gprs attach status |
mazgch | 72:d1e943ad6558 | 747 | sendFormated("AT+CGATT=1\r\n"); |
mazgch | 74:208e3e32d263 | 748 | if (RESP_OK != waitFinalResp(NULL,NULL,3*60*1000)) |
mazgch | 95:8282dbbe1492 | 749 | goto failure; |
mazgch | 31:a0bed6c1e05d | 750 | |
mazgch | 31:a0bed6c1e05d | 751 | // Check the profile |
mazgch | 31:a0bed6c1e05d | 752 | int a = 0; |
mazgch | 79:291df065e345 | 753 | bool force = true; |
mazgch | 31:a0bed6c1e05d | 754 | sendFormated("AT+UPSND=" PROFILE ",8\r\n"); |
mazgch | 52:8071747a7cb3 | 755 | if (RESP_OK != waitFinalResp(_cbUPSND, &a)) |
mazgch | 95:8282dbbe1492 | 756 | goto failure; |
mazgch | 79:291df065e345 | 757 | if (a == 1 && force) { |
mazgch | 31:a0bed6c1e05d | 758 | // disconnect the profile already if it is connected |
mazgch | 31:a0bed6c1e05d | 759 | sendFormated("AT+UPSDA=" PROFILE ",4\r\n"); |
mazgch | 58:e38a2e942fbb | 760 | if (RESP_OK != waitFinalResp(NULL,NULL,40*1000)) |
mazgch | 95:8282dbbe1492 | 761 | goto failure; |
mazgch | 79:291df065e345 | 762 | a = 0; |
mazgch | 31:a0bed6c1e05d | 763 | } |
mazgch | 79:291df065e345 | 764 | if (a == 0) { |
mazgch | 84:a05edb010176 | 765 | bool ok = false; |
mazgch | 84:a05edb010176 | 766 | // try to lookup the apn settings from our local database by mccmnc |
mazgch | 84:a05edb010176 | 767 | const char* config = NULL; |
mazgch | 88:135fb4bb7aac | 768 | if (!apn && !username && !password) |
mazgch | 88:135fb4bb7aac | 769 | config = apnconfig(_dev.imsi); |
mazgch | 84:a05edb010176 | 770 | |
mazgch | 79:291df065e345 | 771 | // Set up the dynamic IP address assignment. |
mazgch | 79:291df065e345 | 772 | sendFormated("AT+UPSD=" PROFILE ",7,\"0.0.0.0\"\r\n"); |
mazgch | 52:8071747a7cb3 | 773 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 774 | goto failure; |
mazgch | 84:a05edb010176 | 775 | |
mazgch | 84:a05edb010176 | 776 | do { |
mazgch | 84:a05edb010176 | 777 | if (config) { |
mazgch | 85:dd8f4f0d0ca9 | 778 | apn = _APN_GET(config); |
mazgch | 85:dd8f4f0d0ca9 | 779 | username = _APN_GET(config); |
mazgch | 85:dd8f4f0d0ca9 | 780 | password = _APN_GET(config); |
mazgch | 84:a05edb010176 | 781 | TRACE("Testing APN Settings(\"%s\",\"%s\",\"%s\")\r\n", apn, username, password); |
mazgch | 79:291df065e345 | 782 | } |
mazgch | 84:a05edb010176 | 783 | // Set up the APN |
mazgch | 90:3915192f6d7e | 784 | if (apn && *apn) { |
mazgch | 90:3915192f6d7e | 785 | sendFormated("AT+UPSD=" PROFILE ",1,\"%s\"\r\n", apn); |
mazgch | 90:3915192f6d7e | 786 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 787 | goto failure; |
mazgch | 90:3915192f6d7e | 788 | } |
mazgch | 90:3915192f6d7e | 789 | if (username && *username) { |
mazgch | 90:3915192f6d7e | 790 | sendFormated("AT+UPSD=" PROFILE ",2,\"%s\"\r\n", username); |
mazgch | 90:3915192f6d7e | 791 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 792 | goto failure; |
mazgch | 90:3915192f6d7e | 793 | } |
mazgch | 90:3915192f6d7e | 794 | if (password && *password) { |
mazgch | 90:3915192f6d7e | 795 | sendFormated("AT+UPSD=" PROFILE ",3,\"%s\"\r\n", password); |
mazgch | 90:3915192f6d7e | 796 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 797 | goto failure; |
mazgch | 90:3915192f6d7e | 798 | } |
mazgch | 84:a05edb010176 | 799 | // try different Authentication Protocols |
mazgch | 84:a05edb010176 | 800 | // 0 = none |
mazgch | 84:a05edb010176 | 801 | // 1 = PAP (Password Authentication Protocol) |
mazgch | 84:a05edb010176 | 802 | // 2 = CHAP (Challenge Handshake Authentication Protocol) |
mazgch | 84:a05edb010176 | 803 | for (int i = AUTH_NONE; i <= AUTH_CHAP && !ok; i ++) { |
mazgch | 84:a05edb010176 | 804 | if ((auth == AUTH_DETECT) || (auth == i)) { |
mazgch | 84:a05edb010176 | 805 | // Set up the Authentication Protocol |
mazgch | 84:a05edb010176 | 806 | sendFormated("AT+UPSD=" PROFILE ",6,%d\r\n", i); |
mazgch | 84:a05edb010176 | 807 | if (RESP_OK != waitFinalResp()) |
mazgch | 95:8282dbbe1492 | 808 | goto failure; |
mazgch | 84:a05edb010176 | 809 | // Activate the profile and make connection |
mazgch | 84:a05edb010176 | 810 | sendFormated("AT+UPSDA=" PROFILE ",3\r\n"); |
mazgch | 84:a05edb010176 | 811 | if (RESP_OK == waitFinalResp(NULL,NULL,150*1000)) |
mazgch | 84:a05edb010176 | 812 | ok = true; |
mazgch | 84:a05edb010176 | 813 | } |
mazgch | 84:a05edb010176 | 814 | } |
mazgch | 99:3116d3e900ed | 815 | } while (!ok && config && *config); // maybe use next setting ? |
mazgch | 84:a05edb010176 | 816 | if (!ok) { |
mazgch | 79:291df065e345 | 817 | ERROR("Your modem APN/password/username may be wrong\r\n"); |
mazgch | 95:8282dbbe1492 | 818 | goto failure; |
mazgch | 79:291df065e345 | 819 | } |
mazgch | 74:208e3e32d263 | 820 | } |
mazgch | 21:c4d64830bf02 | 821 | //Get local IP address |
mazgch | 21:c4d64830bf02 | 822 | sendFormated("AT+UPSND=" PROFILE ",0\r\n"); |
mazgch | 52:8071747a7cb3 | 823 | if (RESP_OK != waitFinalResp(_cbUPSND, &_ip)) |
mazgch | 95:8282dbbe1492 | 824 | goto failure; |
mazgch | 31:a0bed6c1e05d | 825 | } |
mazgch | 95:8282dbbe1492 | 826 | UNLOCK(); |
mazgch | 32:8f12ac182bbb | 827 | return _ip; |
mazgch | 95:8282dbbe1492 | 828 | failure: |
mazgch | 95:8282dbbe1492 | 829 | unlock(); |
mazgch | 95:8282dbbe1492 | 830 | return NOIP; |
mazgch | 31:a0bed6c1e05d | 831 | } |
mazgch | 31:a0bed6c1e05d | 832 | |
mazgch | 84:a05edb010176 | 833 | int MDMParser::_cbUDOPN(int type, const char* buf, int len, char* mccmnc) |
mazgch | 84:a05edb010176 | 834 | { |
mazgch | 84:a05edb010176 | 835 | if ((type == TYPE_PLUS) && mccmnc) { |
mazgch | 84:a05edb010176 | 836 | if (sscanf(buf, "\r\n+UDOPN: 0,\"%[^\"]\"", mccmnc) == 1) |
mazgch | 84:a05edb010176 | 837 | ; |
mazgch | 84:a05edb010176 | 838 | } |
mazgch | 84:a05edb010176 | 839 | return WAIT; |
mazgch | 84:a05edb010176 | 840 | } |
mazgch | 84:a05edb010176 | 841 | |
mazgch | 32:8f12ac182bbb | 842 | int MDMParser::_cbCMIP(int type, const char* buf, int len, IP* ip) |
mazgch | 32:8f12ac182bbb | 843 | { |
mazgch | 102:fb982ff9da82 | 844 | if ((type == TYPE_UNKNOWN) && ip) { |
mazgch | 32:8f12ac182bbb | 845 | int a,b,c,d; |
mazgch | 102:fb982ff9da82 | 846 | if (sscanf(buf, "\r\n" IPSTR, &a,&b,&c,&d) == 4) |
mazgch | 32:8f12ac182bbb | 847 | *ip = IPADR(a,b,c,d); |
mazgch | 32:8f12ac182bbb | 848 | } |
mazgch | 32:8f12ac182bbb | 849 | return WAIT; |
mazgch | 32:8f12ac182bbb | 850 | } |
mazgch | 32:8f12ac182bbb | 851 | |
mazgch | 31:a0bed6c1e05d | 852 | int MDMParser::_cbUPSND(int type, const char* buf, int len, int* act) |
mazgch | 31:a0bed6c1e05d | 853 | { |
mazgch | 31:a0bed6c1e05d | 854 | if ((type == TYPE_PLUS) && act) { |
mazgch | 31:a0bed6c1e05d | 855 | if (sscanf(buf, "\r\n+UPSND: %*d,%*d,%d", act) == 1) |
mazgch | 31:a0bed6c1e05d | 856 | /*nothing*/; |
mazgch | 21:c4d64830bf02 | 857 | } |
mazgch | 31:a0bed6c1e05d | 858 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 859 | } |
mazgch | 31:a0bed6c1e05d | 860 | |
mazgch | 31:a0bed6c1e05d | 861 | int MDMParser::_cbUPSND(int type, const char* buf, int len, IP* ip) |
mazgch | 31:a0bed6c1e05d | 862 | { |
mazgch | 31:a0bed6c1e05d | 863 | if ((type == TYPE_PLUS) && ip) { |
mazgch | 31:a0bed6c1e05d | 864 | int a,b,c,d; |
mazgch | 31:a0bed6c1e05d | 865 | // +UPSND=<profile_id>,<param_tag>[,<dynamic_param_val>] |
mazgch | 31:a0bed6c1e05d | 866 | if (sscanf(buf, "\r\n+UPSND: " PROFILE ",0,\"" IPSTR "\"", &a,&b,&c,&d) == 4) |
mazgch | 31:a0bed6c1e05d | 867 | *ip = IPADR(a,b,c,d); |
mazgch | 31:a0bed6c1e05d | 868 | } |
mazgch | 31:a0bed6c1e05d | 869 | return WAIT; |
mazgch | 31:a0bed6c1e05d | 870 | } |
mazgch | 31:a0bed6c1e05d | 871 | |
mazgch | 31:a0bed6c1e05d | 872 | int MDMParser::_cbUDNSRN(int type, const char* buf, int len, IP* ip) |
mazgch | 31:a0bed6c1e05d | 873 | { |
mazgch | 31:a0bed6c1e05d | 874 | if ((type == TYPE_PLUS) && ip) { |
mazgch | 31:a0bed6c1e05d | 875 | int a,b,c,d; |
mazgch | 95:8282dbbe1492 | 876 | if (sscanf(buf, "\r\n+UDNSRN: \"" IPSTR "\"", &a,&b,&c,&d) == 4) |
mazgch | 31:a0bed6c1e05d | 877 | *ip = IPADR(a,b,c,d); |
mazgch | 31:a0bed6c1e05d | 878 | } |
mazgch | 31:a0bed6c1e05d | 879 | return WAIT; |
mazgch | 21:c4d64830bf02 | 880 | } |
mazgch | 21:c4d64830bf02 | 881 | |
mazgch | 21:c4d64830bf02 | 882 | bool MDMParser::disconnect(void) |
mazgch | 21:c4d64830bf02 | 883 | { |
mazgch | 95:8282dbbe1492 | 884 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 885 | LOCK(); |
mazgch | 74:208e3e32d263 | 886 | INFO("Modem::disconnect\r\n"); |
mazgch | 95:8282dbbe1492 | 887 | if (_ip != NOIP) { |
mazgch | 95:8282dbbe1492 | 888 | if (_dev.dev == DEV_LISA_C200) { |
mazgch | 95:8282dbbe1492 | 889 | // There something to do here |
mazgch | 95:8282dbbe1492 | 890 | _ip = NOIP; |
mazgch | 95:8282dbbe1492 | 891 | ok = true; |
mazgch | 95:8282dbbe1492 | 892 | } else { |
mazgch | 95:8282dbbe1492 | 893 | sendFormated("AT+UPSDA=" PROFILE ",4\r\n"); |
mazgch | 95:8282dbbe1492 | 894 | if (RESP_OK != waitFinalResp()) { |
mazgch | 95:8282dbbe1492 | 895 | _ip = NOIP; |
mazgch | 95:8282dbbe1492 | 896 | ok = true; |
mazgch | 95:8282dbbe1492 | 897 | } |
mazgch | 95:8282dbbe1492 | 898 | } |
mazgch | 21:c4d64830bf02 | 899 | } |
mazgch | 95:8282dbbe1492 | 900 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 901 | return ok; |
mazgch | 21:c4d64830bf02 | 902 | } |
mazgch | 21:c4d64830bf02 | 903 | |
mazgch | 31:a0bed6c1e05d | 904 | MDMParser::IP MDMParser::gethostbyname(const char* host) |
mazgch | 21:c4d64830bf02 | 905 | { |
mazgch | 31:a0bed6c1e05d | 906 | IP ip = NOIP; |
mazgch | 31:a0bed6c1e05d | 907 | int a,b,c,d; |
mazgch | 31:a0bed6c1e05d | 908 | if (sscanf(host, IPSTR, &a,&b,&c,&d) == 4) |
mazgch | 31:a0bed6c1e05d | 909 | ip = IPADR(a,b,c,d); |
mazgch | 31:a0bed6c1e05d | 910 | else { |
mazgch | 95:8282dbbe1492 | 911 | LOCK(); |
mazgch | 31:a0bed6c1e05d | 912 | sendFormated("AT+UDNSRN=0,\"%s\"\r\n", host); |
mazgch | 52:8071747a7cb3 | 913 | if (RESP_OK != waitFinalResp(_cbUDNSRN, &ip)) |
mazgch | 95:8282dbbe1492 | 914 | ip = NOIP; |
mazgch | 95:8282dbbe1492 | 915 | UNLOCK(); |
mazgch | 21:c4d64830bf02 | 916 | } |
mazgch | 31:a0bed6c1e05d | 917 | return ip; |
mazgch | 21:c4d64830bf02 | 918 | } |
mazgch | 21:c4d64830bf02 | 919 | |
mazgch | 21:c4d64830bf02 | 920 | // ---------------------------------------------------------------- |
mazgch | 21:c4d64830bf02 | 921 | // sockets |
mazgch | 21:c4d64830bf02 | 922 | |
mazgch | 103:197fa7920ad8 | 923 | int MDMParser::_cbUSOCR(int type, const char* buf, int len, int* handle) |
mazgch | 21:c4d64830bf02 | 924 | { |
mazgch | 103:197fa7920ad8 | 925 | if ((type == TYPE_PLUS) && handle) { |
mazgch | 108:254bf037f83f | 926 | // +USOCR: socket |
mazgch | 108:254bf037f83f | 927 | if (sscanf(buf, "\r\n+USOCR: %d", handle) == 1) |
mazgch | 108:254bf037f83f | 928 | /*nothing*/; |
mazgch | 21:c4d64830bf02 | 929 | } |
mazgch | 21:c4d64830bf02 | 930 | return WAIT; |
mazgch | 21:c4d64830bf02 | 931 | } |
mazgch | 21:c4d64830bf02 | 932 | |
mazgch | 63:42cb563a25bc | 933 | int MDMParser::socketSocket(IpProtocol ipproto, int port) |
mazgch | 21:c4d64830bf02 | 934 | { |
mazgch | 103:197fa7920ad8 | 935 | int socket; |
mazgch | 95:8282dbbe1492 | 936 | LOCK(); |
mazgch | 103:197fa7920ad8 | 937 | // find an free socket |
mazgch | 103:197fa7920ad8 | 938 | socket = _findSocket(); |
mazgch | 47:9a89e5195721 | 939 | TRACE("socketSocket(%d)\r\n", ipproto); |
mazgch | 95:8282dbbe1492 | 940 | if (socket != SOCKET_ERROR) { |
mazgch | 106:18aeacdae391 | 941 | if (ipproto == IPPROTO_UDP) { |
mazgch | 106:18aeacdae391 | 942 | // sending port can only be set on 2G/3G modules |
mazgch | 106:18aeacdae391 | 943 | if ((port != -1) && (_dev.dev != DEV_LISA_C200)) { |
mazgch | 106:18aeacdae391 | 944 | sendFormated("AT+USOCR=17,%d\r\n", port); |
mazgch | 106:18aeacdae391 | 945 | } else { |
mazgch | 106:18aeacdae391 | 946 | sendFormated("AT+USOCR=17\r\n"); |
mazgch | 106:18aeacdae391 | 947 | } |
mazgch | 103:197fa7920ad8 | 948 | } else /*(ipproto == IPPROTO_TCP)*/ { |
mazgch | 103:197fa7920ad8 | 949 | sendFormated("AT+USOCR=6\r\n"); |
mazgch | 103:197fa7920ad8 | 950 | } |
mazgch | 103:197fa7920ad8 | 951 | int handle = SOCKET_ERROR; |
mazgch | 103:197fa7920ad8 | 952 | if ((RESP_OK == waitFinalResp(_cbUSOCR, &handle)) && |
mazgch | 103:197fa7920ad8 | 953 | (handle != SOCKET_ERROR)) { |
mazgch | 103:197fa7920ad8 | 954 | TRACE("Socket %d: handle %d was created\r\n", socket, handle); |
mazgch | 103:197fa7920ad8 | 955 | _sockets[socket].handle = handle; |
mazgch | 103:197fa7920ad8 | 956 | _sockets[socket].timeout_ms = TIMEOUT_BLOCKING; |
mazgch | 103:197fa7920ad8 | 957 | _sockets[socket].connected = false; |
mazgch | 103:197fa7920ad8 | 958 | _sockets[socket].pending = 0; |
mazgch | 103:197fa7920ad8 | 959 | } |
mazgch | 103:197fa7920ad8 | 960 | else |
mazgch | 103:197fa7920ad8 | 961 | socket = SOCKET_ERROR; |
mazgch | 95:8282dbbe1492 | 962 | } |
mazgch | 95:8282dbbe1492 | 963 | UNLOCK(); |
mazgch | 21:c4d64830bf02 | 964 | return socket; |
mazgch | 21:c4d64830bf02 | 965 | } |
mazgch | 21:c4d64830bf02 | 966 | |
mazgch | 21:c4d64830bf02 | 967 | bool MDMParser::socketConnect(int socket, const char * host, int port) |
mazgch | 21:c4d64830bf02 | 968 | { |
mazgch | 31:a0bed6c1e05d | 969 | IP ip = gethostbyname(host); |
mazgch | 31:a0bed6c1e05d | 970 | if (ip == NOIP) |
mazgch | 21:c4d64830bf02 | 971 | return false; |
mazgch | 21:c4d64830bf02 | 972 | // connect to socket |
mazgch | 95:8282dbbe1492 | 973 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 974 | LOCK(); |
mazgch | 103:197fa7920ad8 | 975 | if (ISSOCKET(socket) && (!_sockets[socket].connected)) { |
mazgch | 95:8282dbbe1492 | 976 | TRACE("socketConnect(%d,%s,%d)\r\n", socket,host,port); |
mazgch | 103:197fa7920ad8 | 977 | sendFormated("AT+USOCO=%d,\"" IPSTR "\",%d\r\n", _sockets[socket].handle, IPNUM(ip), port); |
mazgch | 95:8282dbbe1492 | 978 | if (RESP_OK == waitFinalResp()) |
mazgch | 103:197fa7920ad8 | 979 | ok = _sockets[socket].connected = true; |
mazgch | 95:8282dbbe1492 | 980 | } |
mazgch | 95:8282dbbe1492 | 981 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 982 | return ok; |
mazgch | 21:c4d64830bf02 | 983 | } |
mazgch | 21:c4d64830bf02 | 984 | |
mazgch | 44:9d12223b78ff | 985 | bool MDMParser::socketIsConnected(int socket) |
mazgch | 44:9d12223b78ff | 986 | { |
mazgch | 95:8282dbbe1492 | 987 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 988 | LOCK(); |
mazgch | 103:197fa7920ad8 | 989 | ok = ISSOCKET(socket) && _sockets[socket].connected; |
mazgch | 107:436ee320efd6 | 990 | TRACE("socketIsConnected(%d) %s\r\n", socket, ok?"yes":"no"); |
mazgch | 95:8282dbbe1492 | 991 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 992 | return ok; |
mazgch | 44:9d12223b78ff | 993 | } |
mazgch | 44:9d12223b78ff | 994 | |
mazgch | 66:69072b3c5bca | 995 | bool MDMParser::socketSetBlocking(int socket, int timeout_ms) |
mazgch | 44:9d12223b78ff | 996 | { |
mazgch | 95:8282dbbe1492 | 997 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 998 | LOCK(); |
mazgch | 103:197fa7920ad8 | 999 | TRACE("socketSetBlocking(%d,%d)\r\n", socket,timeout_ms); |
mazgch | 95:8282dbbe1492 | 1000 | if (ISSOCKET(socket)) { |
mazgch | 95:8282dbbe1492 | 1001 | _sockets[socket].timeout_ms = timeout_ms; |
mazgch | 95:8282dbbe1492 | 1002 | ok = true; |
mazgch | 95:8282dbbe1492 | 1003 | } |
mazgch | 95:8282dbbe1492 | 1004 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1005 | return ok; |
mazgch | 44:9d12223b78ff | 1006 | } |
mazgch | 44:9d12223b78ff | 1007 | |
mazgch | 21:c4d64830bf02 | 1008 | bool MDMParser::socketClose(int socket) |
mazgch | 21:c4d64830bf02 | 1009 | { |
mazgch | 95:8282dbbe1492 | 1010 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1011 | LOCK(); |
mazgch | 103:197fa7920ad8 | 1012 | if (ISSOCKET(socket) && _sockets[socket].connected) { |
mazgch | 95:8282dbbe1492 | 1013 | TRACE("socketClose(%d)\r\n", socket); |
mazgch | 103:197fa7920ad8 | 1014 | sendFormated("AT+USOCL=%d\r\n", _sockets[socket].handle); |
mazgch | 95:8282dbbe1492 | 1015 | if (RESP_OK == waitFinalResp()) { |
mazgch | 103:197fa7920ad8 | 1016 | _sockets[socket].connected = false; |
mazgch | 95:8282dbbe1492 | 1017 | ok = true; |
mazgch | 95:8282dbbe1492 | 1018 | } |
mazgch | 95:8282dbbe1492 | 1019 | } |
mazgch | 95:8282dbbe1492 | 1020 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1021 | return ok; |
mazgch | 21:c4d64830bf02 | 1022 | } |
mazgch | 21:c4d64830bf02 | 1023 | |
mazgch | 21:c4d64830bf02 | 1024 | bool MDMParser::socketFree(int socket) |
mazgch | 21:c4d64830bf02 | 1025 | { |
mazgch | 95:8282dbbe1492 | 1026 | // make sure it is closed |
mazgch | 21:c4d64830bf02 | 1027 | socketClose(socket); |
mazgch | 95:8282dbbe1492 | 1028 | bool ok = true; |
mazgch | 95:8282dbbe1492 | 1029 | LOCK(); |
mazgch | 103:197fa7920ad8 | 1030 | if (ISSOCKET(socket)) { |
mazgch | 103:197fa7920ad8 | 1031 | TRACE("socketFree(%d)\r\n", socket); |
mazgch | 103:197fa7920ad8 | 1032 | _sockets[socket].handle = SOCKET_ERROR; |
mazgch | 103:197fa7920ad8 | 1033 | _sockets[socket].timeout_ms = TIMEOUT_BLOCKING; |
mazgch | 103:197fa7920ad8 | 1034 | _sockets[socket].connected = false; |
mazgch | 103:197fa7920ad8 | 1035 | _sockets[socket].pending = 0; |
mazgch | 95:8282dbbe1492 | 1036 | ok = true; |
mazgch | 95:8282dbbe1492 | 1037 | } |
mazgch | 95:8282dbbe1492 | 1038 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1039 | return ok; |
mazgch | 21:c4d64830bf02 | 1040 | } |
mazgch | 21:c4d64830bf02 | 1041 | |
mazgch | 69:4d6fa520dfca | 1042 | #define USO_MAX_WRITE 1024 //!< maximum number of bytes to write to socket |
mazgch | 69:4d6fa520dfca | 1043 | |
mazgch | 21:c4d64830bf02 | 1044 | int MDMParser::socketSend(int socket, const char * buf, int len) |
mazgch | 21:c4d64830bf02 | 1045 | { |
mazgch | 47:9a89e5195721 | 1046 | TRACE("socketSend(%d,,%d)\r\n", socket,len); |
mazgch | 68:33a96cf64986 | 1047 | int cnt = len; |
mazgch | 68:33a96cf64986 | 1048 | while (cnt > 0) { |
mazgch | 69:4d6fa520dfca | 1049 | int blk = USO_MAX_WRITE; |
mazgch | 68:33a96cf64986 | 1050 | if (cnt < blk) |
mazgch | 68:33a96cf64986 | 1051 | blk = cnt; |
mazgch | 95:8282dbbe1492 | 1052 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1053 | LOCK(); |
mazgch | 103:197fa7920ad8 | 1054 | if (ISSOCKET(socket)) { |
mazgch | 103:197fa7920ad8 | 1055 | sendFormated("AT+USOWR=%d,%d\r\n",_sockets[socket].handle,blk); |
mazgch | 103:197fa7920ad8 | 1056 | if (RESP_PROMPT == waitFinalResp()) { |
mazgch | 103:197fa7920ad8 | 1057 | wait_ms(50); |
mazgch | 103:197fa7920ad8 | 1058 | send(buf, blk); |
mazgch | 103:197fa7920ad8 | 1059 | if (RESP_OK == waitFinalResp()) |
mazgch | 103:197fa7920ad8 | 1060 | ok = true; |
mazgch | 103:197fa7920ad8 | 1061 | } |
mazgch | 95:8282dbbe1492 | 1062 | } |
mazgch | 95:8282dbbe1492 | 1063 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1064 | if (!ok) |
mazgch | 21:c4d64830bf02 | 1065 | return SOCKET_ERROR; |
mazgch | 68:33a96cf64986 | 1066 | buf += blk; |
mazgch | 68:33a96cf64986 | 1067 | cnt -= blk; |
mazgch | 21:c4d64830bf02 | 1068 | } |
mazgch | 68:33a96cf64986 | 1069 | return (len - cnt); |
mazgch | 21:c4d64830bf02 | 1070 | } |
mazgch | 21:c4d64830bf02 | 1071 | |
mazgch | 21:c4d64830bf02 | 1072 | int MDMParser::socketSendTo(int socket, IP ip, int port, const char * buf, int len) |
mazgch | 21:c4d64830bf02 | 1073 | { |
mazgch | 103:197fa7920ad8 | 1074 | TRACE("socketSendTo(%d," IPSTR ",%d,,%d)\r\n", socket,IPNUM(ip),port,len); |
mazgch | 68:33a96cf64986 | 1075 | int cnt = len; |
mazgch | 68:33a96cf64986 | 1076 | while (cnt > 0) { |
mazgch | 69:4d6fa520dfca | 1077 | int blk = USO_MAX_WRITE; |
mazgch | 68:33a96cf64986 | 1078 | if (cnt < blk) |
mazgch | 68:33a96cf64986 | 1079 | blk = cnt; |
mazgch | 95:8282dbbe1492 | 1080 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1081 | LOCK(); |
mazgch | 103:197fa7920ad8 | 1082 | if (ISSOCKET(socket)) { |
mazgch | 103:197fa7920ad8 | 1083 | sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",_sockets[socket].handle,IPNUM(ip),port,blk); |
mazgch | 103:197fa7920ad8 | 1084 | if (RESP_PROMPT == waitFinalResp()) { |
mazgch | 103:197fa7920ad8 | 1085 | wait_ms(50); |
mazgch | 103:197fa7920ad8 | 1086 | send(buf, blk); |
mazgch | 103:197fa7920ad8 | 1087 | if (RESP_OK == waitFinalResp()) |
mazgch | 103:197fa7920ad8 | 1088 | ok = true; |
mazgch | 103:197fa7920ad8 | 1089 | } |
mazgch | 95:8282dbbe1492 | 1090 | } |
mazgch | 95:8282dbbe1492 | 1091 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1092 | if (!ok) |
mazgch | 21:c4d64830bf02 | 1093 | return SOCKET_ERROR; |
mazgch | 68:33a96cf64986 | 1094 | buf += blk; |
mazgch | 68:33a96cf64986 | 1095 | cnt -= blk; |
mazgch | 21:c4d64830bf02 | 1096 | } |
mazgch | 68:33a96cf64986 | 1097 | return (len - cnt); |
mazgch | 21:c4d64830bf02 | 1098 | } |
mazgch | 21:c4d64830bf02 | 1099 | |
mazgch | 21:c4d64830bf02 | 1100 | int MDMParser::socketReadable(int socket) |
mazgch | 21:c4d64830bf02 | 1101 | { |
mazgch | 95:8282dbbe1492 | 1102 | int pending = SOCKET_ERROR; |
mazgch | 95:8282dbbe1492 | 1103 | LOCK(); |
mazgch | 103:197fa7920ad8 | 1104 | if (ISSOCKET(socket) && _sockets[socket].connected) { |
mazgch | 95:8282dbbe1492 | 1105 | TRACE("socketReadable(%d)\r\n", socket); |
mazgch | 95:8282dbbe1492 | 1106 | // allow to receive unsolicited commands |
mazgch | 95:8282dbbe1492 | 1107 | waitFinalResp(NULL, NULL, 0); |
mazgch | 103:197fa7920ad8 | 1108 | if (_sockets[socket].connected) |
mazgch | 95:8282dbbe1492 | 1109 | pending = _sockets[socket].pending; |
mazgch | 95:8282dbbe1492 | 1110 | } |
mazgch | 95:8282dbbe1492 | 1111 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1112 | return pending; |
mazgch | 21:c4d64830bf02 | 1113 | } |
mazgch | 21:c4d64830bf02 | 1114 | |
mazgch | 21:c4d64830bf02 | 1115 | int MDMParser::_cbUSORD(int type, const char* buf, int len, char* out) |
mazgch | 21:c4d64830bf02 | 1116 | { |
mazgch | 21:c4d64830bf02 | 1117 | if ((type == TYPE_PLUS) && out) { |
mazgch | 21:c4d64830bf02 | 1118 | int sz, sk; |
mazgch | 21:c4d64830bf02 | 1119 | if ((sscanf(buf, "\r\n+USORD: %d,%d,", &sk, &sz) == 2) && |
mazgch | 21:c4d64830bf02 | 1120 | (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) { |
mazgch | 21:c4d64830bf02 | 1121 | memcpy(out, &buf[len-1-sz], sz); |
mazgch | 21:c4d64830bf02 | 1122 | } |
mazgch | 21:c4d64830bf02 | 1123 | } |
mazgch | 21:c4d64830bf02 | 1124 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1125 | } |
mazgch | 21:c4d64830bf02 | 1126 | |
mazgch | 21:c4d64830bf02 | 1127 | int MDMParser::socketRecv(int socket, char* buf, int len) |
mazgch | 21:c4d64830bf02 | 1128 | { |
mazgch | 21:c4d64830bf02 | 1129 | int cnt = 0; |
mazgch | 47:9a89e5195721 | 1130 | TRACE("socketRecv(%d,,%d)\r\n", socket, len); |
mazgch | 95:8282dbbe1492 | 1131 | #ifdef MDM_DEBUG |
mazgch | 21:c4d64830bf02 | 1132 | memset(buf, '\0', len); |
mazgch | 95:8282dbbe1492 | 1133 | #endif |
mazgch | 44:9d12223b78ff | 1134 | Timer timer; |
mazgch | 44:9d12223b78ff | 1135 | timer.start(); |
mazgch | 21:c4d64830bf02 | 1136 | while (len) { |
mazgch | 69:4d6fa520dfca | 1137 | int blk = MAX_SIZE; // still need space for headers and unsolicited commands |
mazgch | 21:c4d64830bf02 | 1138 | if (len < blk) blk = len; |
mazgch | 95:8282dbbe1492 | 1139 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1140 | LOCK(); |
mazgch | 95:8282dbbe1492 | 1141 | if (ISSOCKET(socket)) { |
mazgch | 103:197fa7920ad8 | 1142 | if (_sockets[socket].connected) { |
mazgch | 95:8282dbbe1492 | 1143 | if (_sockets[socket].pending < blk) |
mazgch | 95:8282dbbe1492 | 1144 | blk = _sockets[socket].pending; |
mazgch | 95:8282dbbe1492 | 1145 | if (blk > 0) { |
mazgch | 103:197fa7920ad8 | 1146 | sendFormated("AT+USORD=%d,%d\r\n",_sockets[socket].handle, blk); |
mazgch | 95:8282dbbe1492 | 1147 | if (RESP_OK == waitFinalResp(_cbUSORD, buf)) { |
mazgch | 95:8282dbbe1492 | 1148 | _sockets[socket].pending -= blk; |
mazgch | 95:8282dbbe1492 | 1149 | len -= blk; |
mazgch | 95:8282dbbe1492 | 1150 | cnt += blk; |
mazgch | 95:8282dbbe1492 | 1151 | buf += blk; |
mazgch | 95:8282dbbe1492 | 1152 | ok = true; |
mazgch | 95:8282dbbe1492 | 1153 | } |
mazgch | 95:8282dbbe1492 | 1154 | } else if (!TIMEOUT(timer, _sockets[socket].timeout_ms)) { |
mazgch | 95:8282dbbe1492 | 1155 | ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs |
mazgch | 95:8282dbbe1492 | 1156 | } else { |
mazgch | 95:8282dbbe1492 | 1157 | len = 0; |
mazgch | 95:8282dbbe1492 | 1158 | ok = true; |
mazgch | 95:8282dbbe1492 | 1159 | } |
mazgch | 103:197fa7920ad8 | 1160 | } else { |
mazgch | 95:8282dbbe1492 | 1161 | len = 0; |
mazgch | 95:8282dbbe1492 | 1162 | ok = true; |
mazgch | 21:c4d64830bf02 | 1163 | } |
mazgch | 21:c4d64830bf02 | 1164 | } |
mazgch | 95:8282dbbe1492 | 1165 | UNLOCK(); |
mazgch | 107:436ee320efd6 | 1166 | if (!ok) { |
mazgch | 107:436ee320efd6 | 1167 | TRACE("socketRecv: ERROR\r\n"); |
mazgch | 95:8282dbbe1492 | 1168 | return SOCKET_ERROR; |
mazgch | 107:436ee320efd6 | 1169 | } |
mazgch | 21:c4d64830bf02 | 1170 | } |
mazgch | 107:436ee320efd6 | 1171 | TRACE("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt); |
mazgch | 21:c4d64830bf02 | 1172 | return cnt; |
mazgch | 21:c4d64830bf02 | 1173 | } |
mazgch | 21:c4d64830bf02 | 1174 | |
mazgch | 21:c4d64830bf02 | 1175 | int MDMParser::_cbUSORF(int type, const char* buf, int len, USORFparam* param) |
mazgch | 21:c4d64830bf02 | 1176 | { |
mazgch | 21:c4d64830bf02 | 1177 | if ((type == TYPE_PLUS) && param) { |
mazgch | 21:c4d64830bf02 | 1178 | int sz, sk, p, a,b,c,d; |
mazgch | 95:8282dbbe1492 | 1179 | int r = sscanf(buf, "\r\n+USORF: %d,\"" IPSTR "\",%d,%d,", |
mazgch | 95:8282dbbe1492 | 1180 | &sk,&a,&b,&c,&d,&p,&sz); |
mazgch | 95:8282dbbe1492 | 1181 | if ((r == 7) && (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) { |
mazgch | 21:c4d64830bf02 | 1182 | memcpy(param->buf, &buf[len-1-sz], sz); |
mazgch | 21:c4d64830bf02 | 1183 | param->ip = IPADR(a,b,c,d); |
mazgch | 21:c4d64830bf02 | 1184 | param->port = p; |
mazgch | 21:c4d64830bf02 | 1185 | } |
mazgch | 21:c4d64830bf02 | 1186 | } |
mazgch | 21:c4d64830bf02 | 1187 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1188 | } |
mazgch | 21:c4d64830bf02 | 1189 | |
mazgch | 63:42cb563a25bc | 1190 | int MDMParser::socketRecvFrom(int socket, IP* ip, int* port, char* buf, int len) |
mazgch | 21:c4d64830bf02 | 1191 | { |
mazgch | 21:c4d64830bf02 | 1192 | int cnt = 0; |
mazgch | 63:42cb563a25bc | 1193 | TRACE("socketRecvFrom(%d,,%d)\r\n", socket, len); |
mazgch | 95:8282dbbe1492 | 1194 | #ifdef MDM_DEBUG |
mazgch | 21:c4d64830bf02 | 1195 | memset(buf, '\0', len); |
mazgch | 95:8282dbbe1492 | 1196 | #endif |
mazgch | 44:9d12223b78ff | 1197 | Timer timer; |
mazgch | 44:9d12223b78ff | 1198 | timer.start(); |
mazgch | 21:c4d64830bf02 | 1199 | while (len) { |
mazgch | 69:4d6fa520dfca | 1200 | int blk = MAX_SIZE; // still need space for headers and unsolicited commands |
mazgch | 21:c4d64830bf02 | 1201 | if (len < blk) blk = len; |
mazgch | 95:8282dbbe1492 | 1202 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1203 | LOCK(); |
mazgch | 95:8282dbbe1492 | 1204 | if (ISSOCKET(socket)) { |
mazgch | 95:8282dbbe1492 | 1205 | if (_sockets[socket].pending < blk) |
mazgch | 95:8282dbbe1492 | 1206 | blk = _sockets[socket].pending; |
mazgch | 95:8282dbbe1492 | 1207 | if (blk > 0) { |
mazgch | 103:197fa7920ad8 | 1208 | sendFormated("AT+USORF=%d,%d\r\n",_sockets[socket].handle, blk); |
mazgch | 95:8282dbbe1492 | 1209 | USORFparam param; |
mazgch | 95:8282dbbe1492 | 1210 | param.buf = buf; |
mazgch | 95:8282dbbe1492 | 1211 | if (RESP_OK == waitFinalResp(_cbUSORF, ¶m)) { |
mazgch | 95:8282dbbe1492 | 1212 | _sockets[socket].pending -= blk; |
mazgch | 95:8282dbbe1492 | 1213 | *ip = param.ip; |
mazgch | 95:8282dbbe1492 | 1214 | *port = param.port; |
mazgch | 95:8282dbbe1492 | 1215 | len -= blk; |
mazgch | 95:8282dbbe1492 | 1216 | cnt += blk; |
mazgch | 95:8282dbbe1492 | 1217 | buf += blk; |
mazgch | 95:8282dbbe1492 | 1218 | len = 0; // done |
mazgch | 95:8282dbbe1492 | 1219 | ok = true; |
mazgch | 95:8282dbbe1492 | 1220 | } |
mazgch | 95:8282dbbe1492 | 1221 | } else if (!TIMEOUT(timer, _sockets[socket].timeout_ms)) { |
mazgch | 95:8282dbbe1492 | 1222 | ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs |
mazgch | 95:8282dbbe1492 | 1223 | } else { |
mazgch | 95:8282dbbe1492 | 1224 | len = 0; // no more data and socket closed or timed-out |
mazgch | 95:8282dbbe1492 | 1225 | ok = true; |
mazgch | 21:c4d64830bf02 | 1226 | } |
mazgch | 95:8282dbbe1492 | 1227 | } |
mazgch | 95:8282dbbe1492 | 1228 | UNLOCK(); |
mazgch | 107:436ee320efd6 | 1229 | if (!ok) { |
mazgch | 107:436ee320efd6 | 1230 | TRACE("socketRecv: ERROR\r\n"); |
mazgch | 95:8282dbbe1492 | 1231 | return SOCKET_ERROR; |
mazgch | 107:436ee320efd6 | 1232 | } |
mazgch | 21:c4d64830bf02 | 1233 | } |
mazgch | 44:9d12223b78ff | 1234 | timer.stop(); |
mazgch | 44:9d12223b78ff | 1235 | timer.reset(); |
mazgch | 107:436ee320efd6 | 1236 | TRACE("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt); |
mazgch | 21:c4d64830bf02 | 1237 | return cnt; |
mazgch | 21:c4d64830bf02 | 1238 | } |
mazgch | 21:c4d64830bf02 | 1239 | |
mazgch | 103:197fa7920ad8 | 1240 | int MDMParser::_findSocket(int handle) { |
mazgch | 104:c64ba749a422 | 1241 | for (int socket = 0; socket < NUMSOCKETS; socket ++) { |
mazgch | 103:197fa7920ad8 | 1242 | if (_sockets[socket].handle == handle) |
mazgch | 103:197fa7920ad8 | 1243 | return socket; |
mazgch | 103:197fa7920ad8 | 1244 | } |
mazgch | 103:197fa7920ad8 | 1245 | return SOCKET_ERROR; |
mazgch | 103:197fa7920ad8 | 1246 | } |
mazgch | 103:197fa7920ad8 | 1247 | |
mazgch | 21:c4d64830bf02 | 1248 | // ---------------------------------------------------------------- |
mazgch | 31:a0bed6c1e05d | 1249 | |
mazgch | 31:a0bed6c1e05d | 1250 | int MDMParser::_cbCMGL(int type, const char* buf, int len, CMGLparam* param) |
mazgch | 21:c4d64830bf02 | 1251 | { |
mazgch | 31:a0bed6c1e05d | 1252 | if ((type == TYPE_PLUS) && param && param->num) { |
mazgch | 31:a0bed6c1e05d | 1253 | // +CMGL: <ix>,... |
mazgch | 31:a0bed6c1e05d | 1254 | int ix; |
mazgch | 31:a0bed6c1e05d | 1255 | if (sscanf(buf, "\r\n+CMGL: %d,", &ix) == 1) |
mazgch | 31:a0bed6c1e05d | 1256 | { |
mazgch | 31:a0bed6c1e05d | 1257 | *param->ix++ = ix; |
mazgch | 31:a0bed6c1e05d | 1258 | param->num--; |
mazgch | 31:a0bed6c1e05d | 1259 | } |
mazgch | 29:53d346010624 | 1260 | } |
mazgch | 29:53d346010624 | 1261 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1262 | } |
mazgch | 21:c4d64830bf02 | 1263 | |
mazgch | 31:a0bed6c1e05d | 1264 | int MDMParser::smsList(const char* stat /*= "ALL"*/, int* ix /*=NULL*/, int num /*= 0*/) { |
mazgch | 95:8282dbbe1492 | 1265 | int ret = -1; |
mazgch | 95:8282dbbe1492 | 1266 | LOCK(); |
mazgch | 31:a0bed6c1e05d | 1267 | sendFormated("AT+CMGL=\"%s\"\r\n", stat); |
mazgch | 31:a0bed6c1e05d | 1268 | CMGLparam param; |
mazgch | 31:a0bed6c1e05d | 1269 | param.ix = ix; |
mazgch | 31:a0bed6c1e05d | 1270 | param.num = num; |
mazgch | 95:8282dbbe1492 | 1271 | if (RESP_OK == waitFinalResp(_cbCMGL, ¶m)) |
mazgch | 95:8282dbbe1492 | 1272 | ret = num - param.num; |
mazgch | 95:8282dbbe1492 | 1273 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1274 | return ret; |
mazgch | 21:c4d64830bf02 | 1275 | } |
mazgch | 21:c4d64830bf02 | 1276 | |
mazgch | 21:c4d64830bf02 | 1277 | bool MDMParser::smsSend(const char* num, const char* buf) |
mazgch | 21:c4d64830bf02 | 1278 | { |
mazgch | 95:8282dbbe1492 | 1279 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1280 | LOCK(); |
mazgch | 80:34985b4d821e | 1281 | sendFormated("AT+CMGS=\"%s\"\r\n",num); |
mazgch | 95:8282dbbe1492 | 1282 | if (RESP_PROMPT == waitFinalResp(NULL,NULL,150*1000)) { |
mazgch | 95:8282dbbe1492 | 1283 | send(buf, strlen(buf)); |
mazgch | 95:8282dbbe1492 | 1284 | const char ctrlZ = 0x1A; |
mazgch | 95:8282dbbe1492 | 1285 | send(&ctrlZ, sizeof(ctrlZ)); |
mazgch | 95:8282dbbe1492 | 1286 | ok = (RESP_OK == waitFinalResp()); |
mazgch | 21:c4d64830bf02 | 1287 | } |
mazgch | 95:8282dbbe1492 | 1288 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1289 | return ok; |
mazgch | 21:c4d64830bf02 | 1290 | } |
mazgch | 21:c4d64830bf02 | 1291 | |
mazgch | 21:c4d64830bf02 | 1292 | bool MDMParser::smsDelete(int ix) |
mazgch | 21:c4d64830bf02 | 1293 | { |
mazgch | 95:8282dbbe1492 | 1294 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1295 | LOCK(); |
mazgch | 21:c4d64830bf02 | 1296 | sendFormated("AT+CMGD=%d\r\n",ix); |
mazgch | 95:8282dbbe1492 | 1297 | ok = (RESP_OK == waitFinalResp()); |
mazgch | 95:8282dbbe1492 | 1298 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1299 | return ok; |
mazgch | 21:c4d64830bf02 | 1300 | } |
mazgch | 21:c4d64830bf02 | 1301 | |
mazgch | 21:c4d64830bf02 | 1302 | int MDMParser::_cbCMGR(int type, const char* buf, int len, CMGRparam* param) |
mazgch | 21:c4d64830bf02 | 1303 | { |
mazgch | 21:c4d64830bf02 | 1304 | if (param) { |
mazgch | 21:c4d64830bf02 | 1305 | if (type == TYPE_PLUS) { |
mazgch | 21:c4d64830bf02 | 1306 | if (sscanf(buf, "\r\n+CMGR: \"%*[^\"]\",\"%[^\"]", param->num) == 1) { |
mazgch | 21:c4d64830bf02 | 1307 | } |
mazgch | 37:cc3433329d66 | 1308 | } else if ((type == TYPE_UNKNOWN) && (buf[len-2] == '\r') && (buf[len-1] == '\n')) { |
mazgch | 21:c4d64830bf02 | 1309 | memcpy(param->buf, buf, len-2); |
mazgch | 21:c4d64830bf02 | 1310 | param->buf[len-2] = '\0'; |
mazgch | 21:c4d64830bf02 | 1311 | } |
mazgch | 21:c4d64830bf02 | 1312 | } |
mazgch | 21:c4d64830bf02 | 1313 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1314 | } |
mazgch | 21:c4d64830bf02 | 1315 | |
mazgch | 21:c4d64830bf02 | 1316 | bool MDMParser::smsRead(int ix, char* num, char* buf, int len) |
mazgch | 21:c4d64830bf02 | 1317 | { |
mazgch | 95:8282dbbe1492 | 1318 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1319 | LOCK(); |
mazgch | 21:c4d64830bf02 | 1320 | CMGRparam param; |
mazgch | 21:c4d64830bf02 | 1321 | param.num = num; |
mazgch | 21:c4d64830bf02 | 1322 | param.buf = buf; |
mazgch | 21:c4d64830bf02 | 1323 | sendFormated("AT+CMGR=%d\r\n",ix); |
mazgch | 95:8282dbbe1492 | 1324 | ok = (RESP_OK == waitFinalResp(_cbCMGR, ¶m)); |
mazgch | 95:8282dbbe1492 | 1325 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1326 | return ok; |
mazgch | 21:c4d64830bf02 | 1327 | } |
irsanjul | 120:353754a2bcd9 | 1328 | |
irsanjul | 120:353754a2bcd9 | 1329 | //----------------------------------------------------------------- |
irsanjul | 120:353754a2bcd9 | 1330 | bool MDMParser::dialStart(const char* num) |
irsanjul | 120:353754a2bcd9 | 1331 | { |
irsanjul | 120:353754a2bcd9 | 1332 | bool ok = false; |
irsanjul | 120:353754a2bcd9 | 1333 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1334 | //sendFormated("AT\r\n"); |
irsanjul | 120:353754a2bcd9 | 1335 | //wait(0.5); |
irsanjul | 120:353754a2bcd9 | 1336 | sendFormated("ATD%s;\r\n",num); |
irsanjul | 120:353754a2bcd9 | 1337 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 120:353754a2bcd9 | 1338 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1339 | return ok; |
irsanjul | 120:353754a2bcd9 | 1340 | } |
irsanjul | 120:353754a2bcd9 | 1341 | |
irsanjul | 120:353754a2bcd9 | 1342 | bool MDMParser::dialStop() |
irsanjul | 120:353754a2bcd9 | 1343 | { |
irsanjul | 120:353754a2bcd9 | 1344 | bool ok = false; |
irsanjul | 120:353754a2bcd9 | 1345 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1346 | sendFormated("ATH\r\n"); |
irsanjul | 120:353754a2bcd9 | 1347 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 120:353754a2bcd9 | 1348 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1349 | return ok; |
irsanjul | 120:353754a2bcd9 | 1350 | } |
irsanjul | 120:353754a2bcd9 | 1351 | |
irsanjul | 120:353754a2bcd9 | 1352 | bool MDMParser::dialAns() |
irsanjul | 120:353754a2bcd9 | 1353 | { |
irsanjul | 120:353754a2bcd9 | 1354 | bool ok = false; |
irsanjul | 120:353754a2bcd9 | 1355 | LOCK(); |
irsanjul | 121:3e1d84b2d34c | 1356 | sendFormated("ATA\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1357 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 120:353754a2bcd9 | 1358 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1359 | return ok; |
irsanjul | 120:353754a2bcd9 | 1360 | } |
irsanjul | 120:353754a2bcd9 | 1361 | |
irsanjul | 120:353754a2bcd9 | 1362 | int MDMParser::_cbATA(int type, const char* buf, int len, ATAparam* param) |
irsanjul | 120:353754a2bcd9 | 1363 | { |
irsanjul | 120:353754a2bcd9 | 1364 | if (param) |
irsanjul | 120:353754a2bcd9 | 1365 | { |
irsanjul | 120:353754a2bcd9 | 1366 | if (type == TYPE_PLUS) |
irsanjul | 120:353754a2bcd9 | 1367 | { |
irsanjul | 120:353754a2bcd9 | 1368 | if (sscanf(buf, "\r\n+CLIP: %31[^\"]\",%d,,,,%*d", param->num, param->stat) == 2) { |
irsanjul | 120:353754a2bcd9 | 1369 | dbg.printf("number %s call you\r\n",param->num); |
irsanjul | 120:353754a2bcd9 | 1370 | } |
irsanjul | 120:353754a2bcd9 | 1371 | } |
irsanjul | 120:353754a2bcd9 | 1372 | } |
irsanjul | 120:353754a2bcd9 | 1373 | return WAIT; |
irsanjul | 120:353754a2bcd9 | 1374 | } |
irsanjul | 120:353754a2bcd9 | 1375 | |
irsanjul | 120:353754a2bcd9 | 1376 | bool MDMParser::dialStatus(const char* number) |
irsanjul | 120:353754a2bcd9 | 1377 | { |
irsanjul | 120:353754a2bcd9 | 1378 | bool ok = false; |
irsanjul | 120:353754a2bcd9 | 1379 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1380 | ATAparam param; |
irsanjul | 120:353754a2bcd9 | 1381 | //ok = (RESP_OK == waitFinalResp(_cbATA, ¶m)); |
irsanjul | 120:353754a2bcd9 | 1382 | waitFinalResp(_cbATA, ¶m); |
irsanjul | 120:353754a2bcd9 | 1383 | ok = (number == param.num); |
irsanjul | 120:353754a2bcd9 | 1384 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1385 | return ok; |
irsanjul | 120:353754a2bcd9 | 1386 | } |
irsanjul | 120:353754a2bcd9 | 1387 | |
irsanjul | 120:353754a2bcd9 | 1388 | // ---------------------------------------------------------------- |
irsanjul | 120:353754a2bcd9 | 1389 | int MDMParser::_cbDBG(int type, const char* buf, int len, DBGparam* param) |
irsanjul | 120:353754a2bcd9 | 1390 | { |
irsanjul | 120:353754a2bcd9 | 1391 | if(type == TYPE_PLUS) |
irsanjul | 120:353754a2bcd9 | 1392 | { |
irsanjul | 120:353754a2bcd9 | 1393 | int a, b; |
irsanjul | 120:353754a2bcd9 | 1394 | if(sscanf(buf, "\r\n+CLIP: \"%[^\"],%*d",param->num) == 1) |
irsanjul | 120:353754a2bcd9 | 1395 | { |
irsanjul | 120:353754a2bcd9 | 1396 | return RESP_OK; |
irsanjul | 120:353754a2bcd9 | 1397 | } |
irsanjul | 120:353754a2bcd9 | 1398 | else if(sscanf(buf, "\r\n+UCALLSTAT: %*d,%d",&a) == 1) |
irsanjul | 120:353754a2bcd9 | 1399 | { |
irsanjul | 120:353754a2bcd9 | 1400 | /* |
irsanjul | 120:353754a2bcd9 | 1401 | get call status format: |
irsanjul | 120:353754a2bcd9 | 1402 | +UCALLSTAT: <id>,<status> |
irsanjul | 120:353754a2bcd9 | 1403 | <id> : id call; |
irsanjul | 120:353754a2bcd9 | 1404 | <status> : 0 = active (saat terhubung) |
irsanjul | 120:353754a2bcd9 | 1405 | 1 = hold |
irsanjul | 120:353754a2bcd9 | 1406 | 2 = dialling (melakukan panggilan namun blm terhubung) |
irsanjul | 120:353754a2bcd9 | 1407 | 3 = alerting (melakukan panggilan dan dalam kedalam menunggu) |
irsanjul | 120:353754a2bcd9 | 1408 | 4 = ringing (saat ada panggilan) |
irsanjul | 120:353754a2bcd9 | 1409 | 5 = waiting |
irsanjul | 120:353754a2bcd9 | 1410 | 6 = disconected (dialling dihentikan) |
irsanjul | 120:353754a2bcd9 | 1411 | 7 = connected |
irsanjul | 120:353754a2bcd9 | 1412 | */ |
irsanjul | 120:353754a2bcd9 | 1413 | *param->status = a; |
irsanjul | 120:353754a2bcd9 | 1414 | return RESP_OK; |
irsanjul | 120:353754a2bcd9 | 1415 | } |
irsanjul | 120:353754a2bcd9 | 1416 | else if(sscanf(buf, "\r\n+CIEV: %d,%d", &a, &b) == 2) |
irsanjul | 120:353754a2bcd9 | 1417 | { |
irsanjul | 120:353754a2bcd9 | 1418 | if(a == 2) |
irsanjul | 120:353754a2bcd9 | 1419 | { |
irsanjul | 120:353754a2bcd9 | 1420 | *param->signal = b; |
irsanjul | 120:353754a2bcd9 | 1421 | return RESP_OK; |
irsanjul | 120:353754a2bcd9 | 1422 | } |
irsanjul | 120:353754a2bcd9 | 1423 | else return WAIT; |
irsanjul | 120:353754a2bcd9 | 1424 | } |
irsanjul | 120:353754a2bcd9 | 1425 | else if(sscanf(buf, "\r\n+CMTI: \"%*[^\"]\",%d", &a) == 1) |
irsanjul | 120:353754a2bcd9 | 1426 | { |
irsanjul | 120:353754a2bcd9 | 1427 | // a is indeks for sms |
irsanjul | 120:353754a2bcd9 | 1428 | if(a == 0) |
irsanjul | 120:353754a2bcd9 | 1429 | return WAIT; |
irsanjul | 120:353754a2bcd9 | 1430 | |
irsanjul | 120:353754a2bcd9 | 1431 | *param->indeks = a; |
irsanjul | 120:353754a2bcd9 | 1432 | return RESP_OK; |
irsanjul | 120:353754a2bcd9 | 1433 | } |
irsanjul | 120:353754a2bcd9 | 1434 | else return WAIT; |
irsanjul | 120:353754a2bcd9 | 1435 | } |
irsanjul | 120:353754a2bcd9 | 1436 | else return WAIT; |
irsanjul | 120:353754a2bcd9 | 1437 | } |
irsanjul | 120:353754a2bcd9 | 1438 | |
irsanjul | 120:353754a2bcd9 | 1439 | bool MDMParser::readDbg(int* status, char* num, int *signal, int* idx) |
irsanjul | 120:353754a2bcd9 | 1440 | { |
irsanjul | 120:353754a2bcd9 | 1441 | bool ok = 0; |
irsanjul | 120:353754a2bcd9 | 1442 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1443 | DBGparam param; |
irsanjul | 120:353754a2bcd9 | 1444 | param.status = status; |
irsanjul | 120:353754a2bcd9 | 1445 | param.num = num; |
irsanjul | 120:353754a2bcd9 | 1446 | param.signal = signal; |
irsanjul | 120:353754a2bcd9 | 1447 | param.indeks = idx; |
irsanjul | 120:353754a2bcd9 | 1448 | ok = (RESP_OK == waitFinalResp(_cbDBG, ¶m, 1000)); |
irsanjul | 120:353754a2bcd9 | 1449 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1450 | return ok; |
irsanjul | 120:353754a2bcd9 | 1451 | } |
irsanjul | 120:353754a2bcd9 | 1452 | |
irsanjul | 120:353754a2bcd9 | 1453 | bool MDMParser::mute(bool act) |
irsanjul | 120:353754a2bcd9 | 1454 | { |
irsanjul | 120:353754a2bcd9 | 1455 | if(act) |
irsanjul | 120:353754a2bcd9 | 1456 | { |
irsanjul | 120:353754a2bcd9 | 1457 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1458 | //sendFormated("AT+CALM=1\r\n"); |
irsanjul | 120:353754a2bcd9 | 1459 | sendFormated("AT+CRSL=2\r\n"); |
irsanjul | 120:353754a2bcd9 | 1460 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1461 | return true; |
irsanjul | 120:353754a2bcd9 | 1462 | } |
irsanjul | 120:353754a2bcd9 | 1463 | else |
irsanjul | 120:353754a2bcd9 | 1464 | { |
irsanjul | 120:353754a2bcd9 | 1465 | LOCK(); |
irsanjul | 120:353754a2bcd9 | 1466 | //sendFormated("AT+CALM=0\r\n"); |
irsanjul | 120:353754a2bcd9 | 1467 | sendFormated("AT+CRSL=5\r\n"); |
irsanjul | 120:353754a2bcd9 | 1468 | UNLOCK(); |
irsanjul | 120:353754a2bcd9 | 1469 | return false; |
irsanjul | 120:353754a2bcd9 | 1470 | } |
irsanjul | 120:353754a2bcd9 | 1471 | } |
irsanjul | 121:3e1d84b2d34c | 1472 | //----------------------------------------------------------------- |
irsanjul | 121:3e1d84b2d34c | 1473 | |
irsanjul | 121:3e1d84b2d34c | 1474 | bool MDMParser::CopsCtzu() |
irsanjul | 121:3e1d84b2d34c | 1475 | { |
irsanjul | 121:3e1d84b2d34c | 1476 | bool ok = 0; |
irsanjul | 121:3e1d84b2d34c | 1477 | LOCK(); |
irsanjul | 121:3e1d84b2d34c | 1478 | sendFormated("AT+COPS=2\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1479 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 121:3e1d84b2d34c | 1480 | sendFormated("AT+CTZU=1\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1481 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 121:3e1d84b2d34c | 1482 | sendFormated("AT+COPS=0\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1483 | ok = (RESP_OK == waitFinalResp()); |
irsanjul | 121:3e1d84b2d34c | 1484 | UNLOCK(); |
irsanjul | 121:3e1d84b2d34c | 1485 | return ok; |
irsanjul | 121:3e1d84b2d34c | 1486 | } |
irsanjul | 121:3e1d84b2d34c | 1487 | |
irsanjul | 121:3e1d84b2d34c | 1488 | int MDMParser::_cbCCLK(int type, const char* buf, int len, CCLKparam* param) |
irsanjul | 121:3e1d84b2d34c | 1489 | { |
irsanjul | 121:3e1d84b2d34c | 1490 | if(type == TYPE_PLUS) |
irsanjul | 121:3e1d84b2d34c | 1491 | { |
irsanjul | 121:3e1d84b2d34c | 1492 | dbg.printf("masuk _cbCCLK\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1493 | if(sscanf(buf, "\r\n+CCLK: \"%[^\"]", param->buf) == 1) |
irsanjul | 121:3e1d84b2d34c | 1494 | { |
irsanjul | 121:3e1d84b2d34c | 1495 | dbg.printf("masuk if nya\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1496 | return RESP_OK; |
irsanjul | 121:3e1d84b2d34c | 1497 | } |
irsanjul | 121:3e1d84b2d34c | 1498 | else return WAIT; |
irsanjul | 121:3e1d84b2d34c | 1499 | } |
irsanjul | 121:3e1d84b2d34c | 1500 | else return WAIT; |
irsanjul | 121:3e1d84b2d34c | 1501 | } |
irsanjul | 121:3e1d84b2d34c | 1502 | |
irsanjul | 121:3e1d84b2d34c | 1503 | bool MDMParser::clk(char *buf) |
irsanjul | 121:3e1d84b2d34c | 1504 | { |
irsanjul | 121:3e1d84b2d34c | 1505 | bool ok = 0; |
irsanjul | 121:3e1d84b2d34c | 1506 | LOCK(); |
irsanjul | 121:3e1d84b2d34c | 1507 | CCLKparam param; |
irsanjul | 121:3e1d84b2d34c | 1508 | param.buf = buf; |
irsanjul | 121:3e1d84b2d34c | 1509 | sendFormated("AT+CCLK?\r\n"); |
irsanjul | 121:3e1d84b2d34c | 1510 | ok = (RESP_OK == waitFinalResp(_cbCCLK, ¶m, 1000)); |
irsanjul | 121:3e1d84b2d34c | 1511 | UNLOCK(); |
irsanjul | 121:3e1d84b2d34c | 1512 | return ok; |
irsanjul | 121:3e1d84b2d34c | 1513 | } |
irsanjul | 121:3e1d84b2d34c | 1514 | |
mazgch | 54:7ba8e4c218e2 | 1515 | // ---------------------------------------------------------------- |
mazgch | 70:0a87d256cd24 | 1516 | |
mazgch | 70:0a87d256cd24 | 1517 | int MDMParser::_cbCUSD(int type, const char* buf, int len, char* resp) |
mazgch | 70:0a87d256cd24 | 1518 | { |
mazgch | 70:0a87d256cd24 | 1519 | if ((type == TYPE_PLUS) && resp) { |
mazgch | 70:0a87d256cd24 | 1520 | // +USD: \"%*[^\"]\",\"%[^\"]\",,\"%*[^\"]\",%d,%d,%d,%d,\"*[^\"]\",%d,%d"..); |
mazgch | 70:0a87d256cd24 | 1521 | if (sscanf(buf, "\r\n+CUSD: %*d,\"%[^\"]\",%*d", resp) == 1) { |
mazgch | 70:0a87d256cd24 | 1522 | /*nothing*/ |
mazgch | 70:0a87d256cd24 | 1523 | } |
mazgch | 70:0a87d256cd24 | 1524 | } |
mazgch | 70:0a87d256cd24 | 1525 | return WAIT; |
mazgch | 70:0a87d256cd24 | 1526 | } |
mazgch | 70:0a87d256cd24 | 1527 | |
mazgch | 70:0a87d256cd24 | 1528 | bool MDMParser::ussdCommand(const char* cmd, char* buf) |
mazgch | 70:0a87d256cd24 | 1529 | { |
mazgch | 95:8282dbbe1492 | 1530 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1531 | LOCK(); |
mazgch | 70:0a87d256cd24 | 1532 | *buf = '\0'; |
mazgch | 95:8282dbbe1492 | 1533 | if (_dev.dev != DEV_LISA_C200) { |
mazgch | 95:8282dbbe1492 | 1534 | sendFormated("AT+CUSD=1,\"%s\"\r\n",cmd); |
mazgch | 95:8282dbbe1492 | 1535 | ok = (RESP_OK == waitFinalResp(_cbCUSD, buf)); |
mazgch | 70:0a87d256cd24 | 1536 | } |
mazgch | 95:8282dbbe1492 | 1537 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1538 | return ok; |
mazgch | 70:0a87d256cd24 | 1539 | } |
mazgch | 80:34985b4d821e | 1540 | |
mazgch | 80:34985b4d821e | 1541 | // ---------------------------------------------------------------- |
mazgch | 70:0a87d256cd24 | 1542 | |
mazgch | 115:d8d94b73c725 | 1543 | int MDMParser::_cbUDELFILE(int type, const char* buf, int len, void*) |
mazgch | 115:d8d94b73c725 | 1544 | { |
mazgch | 115:d8d94b73c725 | 1545 | if ((type == TYPE_ERROR) && strstr(buf, "+CME ERROR: FILE NOT FOUND")) |
mazgch | 115:d8d94b73c725 | 1546 | return RESP_OK; // file does not exist, so all ok... |
mazgch | 115:d8d94b73c725 | 1547 | return WAIT; |
mazgch | 115:d8d94b73c725 | 1548 | } |
mazgch | 115:d8d94b73c725 | 1549 | |
mazgch | 80:34985b4d821e | 1550 | bool MDMParser::delFile(const char* filename) |
mazgch | 80:34985b4d821e | 1551 | { |
mazgch | 95:8282dbbe1492 | 1552 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1553 | LOCK(); |
mazgch | 80:34985b4d821e | 1554 | sendFormated("AT+UDELFILE=\"%s\"\r\n", filename); |
mazgch | 115:d8d94b73c725 | 1555 | ok = (RESP_OK == waitFinalResp(_cbUDELFILE)); |
mazgch | 95:8282dbbe1492 | 1556 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1557 | return ok; |
mazgch | 80:34985b4d821e | 1558 | } |
mazgch | 80:34985b4d821e | 1559 | |
mazgch | 80:34985b4d821e | 1560 | int MDMParser::writeFile(const char* filename, const char* buf, int len) |
mazgch | 80:34985b4d821e | 1561 | { |
mazgch | 95:8282dbbe1492 | 1562 | bool ok = false; |
mazgch | 95:8282dbbe1492 | 1563 | LOCK(); |
mazgch | 80:34985b4d821e | 1564 | sendFormated("AT+UDWNFILE=\"%s\",%d\r\n", filename, len); |
mazgch | 95:8282dbbe1492 | 1565 | if (RESP_PROMPT == waitFinalResp()) { |
mazgch | 95:8282dbbe1492 | 1566 | send(buf, len); |
mazgch | 95:8282dbbe1492 | 1567 | ok = (RESP_OK == waitFinalResp()); |
mazgch | 95:8282dbbe1492 | 1568 | } |
mazgch | 95:8282dbbe1492 | 1569 | UNLOCK(); |
mazgch | 95:8282dbbe1492 | 1570 | return ok ? len : -1; |
mazgch | 80:34985b4d821e | 1571 | } |
mazgch | 80:34985b4d821e | 1572 | |
mazgch | 80:34985b4d821e | 1573 | int MDMParser::readFile(const char* filename, char* buf, int len) |
mazgch | 80:34985b4d821e | 1574 | { |
mazgch | 80:34985b4d821e | 1575 | URDFILEparam param; |
mazgch | 80:34985b4d821e | 1576 | param.filename = filename; |
mazgch | 80:34985b4d821e | 1577 | param.buf = buf; |
mazgch | 80:34985b4d821e | 1578 | param.sz = len; |
mazgch | 80:34985b4d821e | 1579 | param.len = 0; |
mazgch | 95:8282dbbe1492 | 1580 | LOCK(); |
mazgch | 95:8282dbbe1492 | 1581 | sendFormated("AT+URDFILE=\"%s\"\r\n", filename, len); |
mazgch | 105:f6bb2a20de70 | 1582 | if (RESP_OK != waitFinalResp(_cbURDFILE, ¶m)) |
mazgch | 95:8282dbbe1492 | 1583 | param.len = -1; |
mazgch | 95:8282dbbe1492 | 1584 | UNLOCK(); |
mazgch | 80:34985b4d821e | 1585 | return param.len; |
mazgch | 80:34985b4d821e | 1586 | } |
mazgch | 80:34985b4d821e | 1587 | |
mazgch | 80:34985b4d821e | 1588 | int MDMParser::_cbURDFILE(int type, const char* buf, int len, URDFILEparam* param) |
mazgch | 80:34985b4d821e | 1589 | { |
mazgch | 80:34985b4d821e | 1590 | if ((type == TYPE_PLUS) && param && param->filename && param->buf) { |
mazgch | 80:34985b4d821e | 1591 | char filename[48]; |
mazgch | 80:34985b4d821e | 1592 | int sz; |
mazgch | 80:34985b4d821e | 1593 | if ((sscanf(buf, "\r\n+URDFILE: \"%[^\"]\",%d,", filename, &sz) == 2) && |
mazgch | 80:34985b4d821e | 1594 | (0 == strcmp(param->filename, filename)) && |
mazgch | 80:34985b4d821e | 1595 | (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) { |
mazgch | 80:34985b4d821e | 1596 | param->len = (sz < param->sz) ? sz : param->sz; |
mazgch | 80:34985b4d821e | 1597 | memcpy(param->buf, &buf[len-1-sz], param->len); |
mazgch | 80:34985b4d821e | 1598 | } |
mazgch | 80:34985b4d821e | 1599 | } |
mazgch | 80:34985b4d821e | 1600 | return WAIT; |
mazgch | 80:34985b4d821e | 1601 | } |
mazgch | 80:34985b4d821e | 1602 | |
mazgch | 70:0a87d256cd24 | 1603 | // ---------------------------------------------------------------- |
mazgch | 74:208e3e32d263 | 1604 | bool MDMParser::setDebug(int level) |
mazgch | 74:208e3e32d263 | 1605 | { |
mazgch | 74:208e3e32d263 | 1606 | #ifdef MDM_DEBUG |
mazgch | 117:74e4e0109a9e | 1607 | if ((_debugLevel >= -1) && (level >= -1) && |
mazgch | 117:74e4e0109a9e | 1608 | (_debugLevel <= 3) && (level <= 3)) { |
mazgch | 74:208e3e32d263 | 1609 | _debugLevel = level; |
mazgch | 74:208e3e32d263 | 1610 | return true; |
mazgch | 74:208e3e32d263 | 1611 | } |
mazgch | 74:208e3e32d263 | 1612 | #endif |
mazgch | 74:208e3e32d263 | 1613 | return false; |
mazgch | 74:208e3e32d263 | 1614 | } |
mazgch | 74:208e3e32d263 | 1615 | |
mazgch | 73:2b32e0a21df2 | 1616 | void MDMParser::dumpDevStatus(MDMParser::DevStatus* status, |
mazgch | 73:2b32e0a21df2 | 1617 | _DPRINT dprint, void* param) |
mazgch | 54:7ba8e4c218e2 | 1618 | { |
mazgch | 75:ce6e12067d0c | 1619 | dprint(param, "Modem::devStatus\r\n"); |
mazgch | 98:c786461edd40 | 1620 | const char* txtDev[] = { "Unknown", "SARA-G350", "LISA-U200", "LISA-C200", "SARA-U260", "SARA-U270", "LEON-G200" }; |
mazgch | 98:c786461edd40 | 1621 | if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != DEV_UNKNOWN)) |
mazgch | 73:2b32e0a21df2 | 1622 | dprint(param, " Device: %s\r\n", txtDev[status->dev]); |
mazgch | 54:7ba8e4c218e2 | 1623 | const char* txtLpm[] = { "Disabled", "Enabled", "Active" }; |
mazgch | 54:7ba8e4c218e2 | 1624 | if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm)) |
mazgch | 73:2b32e0a21df2 | 1625 | dprint(param, " Power Save: %s\r\n", txtLpm[status->lpm]); |
mazgch | 75:ce6e12067d0c | 1626 | const char* txtSim[] = { "Unknown", "Missing", "Pin", "Ready" }; |
mazgch | 98:c786461edd40 | 1627 | if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != SIM_UNKNOWN)) |
mazgch | 73:2b32e0a21df2 | 1628 | dprint(param, " SIM: %s\r\n", txtSim[status->sim]); |
mazgch | 54:7ba8e4c218e2 | 1629 | if (*status->ccid) |
mazgch | 73:2b32e0a21df2 | 1630 | dprint(param, " CCID: %s\r\n", status->ccid); |
mazgch | 54:7ba8e4c218e2 | 1631 | if (*status->imei) |
mazgch | 73:2b32e0a21df2 | 1632 | dprint(param, " IMEI: %s\r\n", status->imei); |
mazgch | 54:7ba8e4c218e2 | 1633 | if (*status->imsi) |
mazgch | 73:2b32e0a21df2 | 1634 | dprint(param, " IMSI: %s\r\n", status->imsi); |
mazgch | 54:7ba8e4c218e2 | 1635 | if (*status->meid) |
mazgch | 73:2b32e0a21df2 | 1636 | dprint(param, " MEID: %s\r\n", status->meid); // LISA-C |
mazgch | 54:7ba8e4c218e2 | 1637 | if (*status->manu) |
mazgch | 73:2b32e0a21df2 | 1638 | dprint(param, " Manufacturer: %s\r\n", status->manu); |
mazgch | 54:7ba8e4c218e2 | 1639 | if (*status->model) |
mazgch | 73:2b32e0a21df2 | 1640 | dprint(param, " Model: %s\r\n", status->model); |
mazgch | 54:7ba8e4c218e2 | 1641 | if (*status->ver) |
mazgch | 73:2b32e0a21df2 | 1642 | dprint(param, " Version: %s\r\n", status->ver); |
mazgch | 54:7ba8e4c218e2 | 1643 | } |
mazgch | 54:7ba8e4c218e2 | 1644 | |
mazgch | 73:2b32e0a21df2 | 1645 | void MDMParser::dumpNetStatus(MDMParser::NetStatus *status, |
mazgch | 73:2b32e0a21df2 | 1646 | _DPRINT dprint, void* param) |
mazgch | 54:7ba8e4c218e2 | 1647 | { |
mazgch | 75:ce6e12067d0c | 1648 | dprint(param, "Modem::netStatus\r\n"); |
mazgch | 54:7ba8e4c218e2 | 1649 | const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" }; |
mazgch | 98:c786461edd40 | 1650 | if (status->csd < sizeof(txtReg)/sizeof(*txtReg) && (status->csd != REG_UNKNOWN)) |
mazgch | 79:291df065e345 | 1651 | dprint(param, " CSD Registration: %s\r\n", txtReg[status->csd]); |
mazgch | 98:c786461edd40 | 1652 | if (status->psd < sizeof(txtReg)/sizeof(*txtReg) && (status->psd != REG_UNKNOWN)) |
mazgch | 79:291df065e345 | 1653 | dprint(param, " PSD Registration: %s\r\n", txtReg[status->psd]); |
mazgch | 54:7ba8e4c218e2 | 1654 | const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA" }; |
mazgch | 98:c786461edd40 | 1655 | if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != ACT_UNKNOWN)) |
mazgch | 73:2b32e0a21df2 | 1656 | dprint(param, " Access Technology: %s\r\n", txtAct[status->act]); |
mazgch | 54:7ba8e4c218e2 | 1657 | if (status->rssi) |
mazgch | 73:2b32e0a21df2 | 1658 | dprint(param, " Signal Strength: %d dBm\r\n", status->rssi); |
mazgch | 54:7ba8e4c218e2 | 1659 | if (status->ber) |
mazgch | 73:2b32e0a21df2 | 1660 | dprint(param, " Bit Error Rate: %d\r\n", status->ber); |
mazgch | 54:7ba8e4c218e2 | 1661 | if (*status->opr) |
mazgch | 73:2b32e0a21df2 | 1662 | dprint(param, " Operator: %s\r\n", status->opr); |
mazgch | 54:7ba8e4c218e2 | 1663 | if (status->lac != 0xFFFF) |
mazgch | 73:2b32e0a21df2 | 1664 | dprint(param, " Location Area Code: %04X\r\n", status->lac); |
mazgch | 54:7ba8e4c218e2 | 1665 | if (status->ci != 0xFFFFFFFF) |
mazgch | 73:2b32e0a21df2 | 1666 | dprint(param, " Cell ID: %08X\r\n", status->ci); |
mazgch | 54:7ba8e4c218e2 | 1667 | if (*status->num) |
mazgch | 73:2b32e0a21df2 | 1668 | dprint(param, " Phone Number: %s\r\n", status->num); |
mazgch | 54:7ba8e4c218e2 | 1669 | } |
mazgch | 54:7ba8e4c218e2 | 1670 | |
mazgch | 73:2b32e0a21df2 | 1671 | void MDMParser::dumpIp(MDMParser::IP ip, |
mazgch | 73:2b32e0a21df2 | 1672 | _DPRINT dprint, void* param) |
mazgch | 54:7ba8e4c218e2 | 1673 | { |
mazgch | 57:869bd35f44cc | 1674 | if (ip != NOIP) |
mazgch | 75:ce6e12067d0c | 1675 | dprint(param, "Modem:IP " IPSTR "\r\n", IPNUM(ip)); |
mazgch | 54:7ba8e4c218e2 | 1676 | } |
mazgch | 70:0a87d256cd24 | 1677 | |
mazgch | 21:c4d64830bf02 | 1678 | // ---------------------------------------------------------------- |
mazgch | 21:c4d64830bf02 | 1679 | int MDMParser::_parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end) |
mazgch | 18:e5697801df29 | 1680 | { |
mazgch | 18:e5697801df29 | 1681 | int o = 0; |
mazgch | 21:c4d64830bf02 | 1682 | if (sta) { |
mazgch | 21:c4d64830bf02 | 1683 | while (*sta) { |
mazgch | 21:c4d64830bf02 | 1684 | if (++o > len) return WAIT; |
mazgch | 21:c4d64830bf02 | 1685 | char ch = pipe->next(); |
mazgch | 21:c4d64830bf02 | 1686 | if (*sta++ != ch) return NOT_FOUND; |
mazgch | 21:c4d64830bf02 | 1687 | } |
mazgch | 21:c4d64830bf02 | 1688 | } |
mazgch | 21:c4d64830bf02 | 1689 | if (!end) return o; // no termination |
mazgch | 35:9275215a3a5b | 1690 | // at least any char |
mazgch | 35:9275215a3a5b | 1691 | if (++o > len) return WAIT; |
mazgch | 35:9275215a3a5b | 1692 | pipe->next(); |
mazgch | 35:9275215a3a5b | 1693 | // check the end |
mazgch | 21:c4d64830bf02 | 1694 | int x = 0; |
mazgch | 21:c4d64830bf02 | 1695 | while (end[x]) { |
mazgch | 21:c4d64830bf02 | 1696 | if (++o > len) return WAIT; |
mazgch | 21:c4d64830bf02 | 1697 | char ch = pipe->next(); |
mazgch | 21:c4d64830bf02 | 1698 | x = (end[x] == ch) ? x + 1 : |
mazgch | 21:c4d64830bf02 | 1699 | (end[0] == ch) ? 1 : |
mazgch | 21:c4d64830bf02 | 1700 | 0; |
mazgch | 21:c4d64830bf02 | 1701 | } |
mazgch | 21:c4d64830bf02 | 1702 | return o; |
mazgch | 21:c4d64830bf02 | 1703 | } |
mazgch | 21:c4d64830bf02 | 1704 | |
mazgch | 21:c4d64830bf02 | 1705 | int MDMParser::_parseFormated(Pipe<char>* pipe, int len, const char* fmt) |
mazgch | 21:c4d64830bf02 | 1706 | { |
mazgch | 21:c4d64830bf02 | 1707 | int o = 0; |
mazgch | 21:c4d64830bf02 | 1708 | int num = 0; |
mazgch | 21:c4d64830bf02 | 1709 | if (fmt) { |
mazgch | 21:c4d64830bf02 | 1710 | while (*fmt) { |
mazgch | 21:c4d64830bf02 | 1711 | if (++o > len) return WAIT; |
mazgch | 21:c4d64830bf02 | 1712 | char ch = pipe->next(); |
mazgch | 21:c4d64830bf02 | 1713 | if (*fmt == '%') { |
mazgch | 21:c4d64830bf02 | 1714 | fmt++; |
mazgch | 21:c4d64830bf02 | 1715 | if (*fmt == 'd') { // numeric |
mazgch | 21:c4d64830bf02 | 1716 | fmt ++; |
mazgch | 21:c4d64830bf02 | 1717 | num = 0; |
mazgch | 21:c4d64830bf02 | 1718 | while (ch >= '0' && ch <= '9') { |
mazgch | 21:c4d64830bf02 | 1719 | num = num * 10 + (ch - '0'); |
mazgch | 21:c4d64830bf02 | 1720 | if (++o > len) return WAIT; |
mazgch | 21:c4d64830bf02 | 1721 | ch = pipe->next(); |
mazgch | 21:c4d64830bf02 | 1722 | } |
mazgch | 21:c4d64830bf02 | 1723 | } |
mazgch | 21:c4d64830bf02 | 1724 | else if (*fmt == 'c') { // char buffer (takes last numeric as length) |
mazgch | 21:c4d64830bf02 | 1725 | fmt ++; |
mazgch | 21:c4d64830bf02 | 1726 | while (num --) { |
mazgch | 21:c4d64830bf02 | 1727 | if (++o > len) return WAIT; |
mazgch | 21:c4d64830bf02 | 1728 | ch = pipe->next(); |
mazgch | 21:c4d64830bf02 | 1729 | } |
mazgch | 21:c4d64830bf02 | 1730 | } |
mazgch | 80:34985b4d821e | 1731 | else if (*fmt == 's') { |
mazgch | 80:34985b4d821e | 1732 | fmt ++; |
mazgch | 80:34985b4d821e | 1733 | if (ch != '\"') return NOT_FOUND; |
mazgch | 80:34985b4d821e | 1734 | do { |
mazgch | 80:34985b4d821e | 1735 | if (++o > len) return WAIT; |
mazgch | 80:34985b4d821e | 1736 | ch = pipe->next(); |
mazgch | 80:34985b4d821e | 1737 | } while (ch != '\"'); |
mazgch | 80:34985b4d821e | 1738 | if (++o > len) return WAIT; |
mazgch | 80:34985b4d821e | 1739 | ch = pipe->next(); |
mazgch | 80:34985b4d821e | 1740 | } |
mazgch | 21:c4d64830bf02 | 1741 | } |
mazgch | 21:c4d64830bf02 | 1742 | if (*fmt++ != ch) return NOT_FOUND; |
mazgch | 18:e5697801df29 | 1743 | } |
mazgch | 18:e5697801df29 | 1744 | } |
mazgch | 21:c4d64830bf02 | 1745 | return o; |
mazgch | 21:c4d64830bf02 | 1746 | } |
mazgch | 21:c4d64830bf02 | 1747 | |
mazgch | 21:c4d64830bf02 | 1748 | int MDMParser::_getLine(Pipe<char>* pipe, char* buf, int len) |
mazgch | 21:c4d64830bf02 | 1749 | { |
mazgch | 21:c4d64830bf02 | 1750 | int unkn = 0; |
mazgch | 21:c4d64830bf02 | 1751 | int sz = pipe->size(); |
mazgch | 21:c4d64830bf02 | 1752 | int fr = pipe->free(); |
mazgch | 21:c4d64830bf02 | 1753 | if (len > sz) |
mazgch | 21:c4d64830bf02 | 1754 | len = sz; |
mazgch | 21:c4d64830bf02 | 1755 | while (len > 0) |
mazgch | 21:c4d64830bf02 | 1756 | { |
mazgch | 21:c4d64830bf02 | 1757 | static struct { |
mazgch | 21:c4d64830bf02 | 1758 | const char* fmt; int type; |
mazgch | 21:c4d64830bf02 | 1759 | } lutF[] = { |
mazgch | 21:c4d64830bf02 | 1760 | { "\r\n+USORD: %d,%d,\"%c\"", TYPE_PLUS }, |
mazgch | 95:8282dbbe1492 | 1761 | { "\r\n+USORF: %d,\"" IPSTR "\",%d,%d,\"%c\"", TYPE_PLUS }, |
mazgch | 80:34985b4d821e | 1762 | { "\r\n+URDFILE: %s,%d,\"%c\"", TYPE_PLUS }, |
mazgch | 21:c4d64830bf02 | 1763 | }; |
mazgch | 21:c4d64830bf02 | 1764 | static struct { |
mazgch | 21:c4d64830bf02 | 1765 | const char* sta; const char* end; int type; |
mazgch | 21:c4d64830bf02 | 1766 | } lut[] = { |
mazgch | 21:c4d64830bf02 | 1767 | { "\r\nOK\r\n", NULL, TYPE_OK }, |
mazgch | 21:c4d64830bf02 | 1768 | { "\r\nERROR\r\n", NULL, TYPE_ERROR }, |
mazgch | 31:a0bed6c1e05d | 1769 | { "\r\n+CME ERROR:", "\r\n", TYPE_ERROR }, |
mazgch | 21:c4d64830bf02 | 1770 | { "\r\n+CMS ERROR:", "\r\n", TYPE_ERROR }, |
mazgch | 21:c4d64830bf02 | 1771 | { "\r\nRING\r\n", NULL, TYPE_RING }, |
mazgch | 21:c4d64830bf02 | 1772 | { "\r\nCONNECT\r\n", NULL, TYPE_CONNECT }, |
mazgch | 21:c4d64830bf02 | 1773 | { "\r\nNO CARRIER\r\n", NULL, TYPE_NOCARRIER }, |
mazgch | 21:c4d64830bf02 | 1774 | { "\r\nNO DIALTONE\r\n", NULL, TYPE_NODIALTONE }, |
mazgch | 21:c4d64830bf02 | 1775 | { "\r\nBUSY\r\n", NULL, TYPE_BUSY }, |
mazgch | 21:c4d64830bf02 | 1776 | { "\r\nNO ANSWER\r\n", NULL, TYPE_NOANSWER }, |
mazgch | 21:c4d64830bf02 | 1777 | { "\r\n+", "\r\n", TYPE_PLUS }, |
mazgch | 21:c4d64830bf02 | 1778 | { "\r\n@", NULL, TYPE_PROMPT }, // Sockets |
mazgch | 21:c4d64830bf02 | 1779 | { "\r\n>", NULL, TYPE_PROMPT }, // SMS |
mazgch | 80:34985b4d821e | 1780 | { "\n>", NULL, TYPE_PROMPT }, // File |
mazgch | 21:c4d64830bf02 | 1781 | }; |
mazgch | 21:c4d64830bf02 | 1782 | for (int i = 0; i < sizeof(lutF)/sizeof(*lutF); i ++) { |
mazgch | 21:c4d64830bf02 | 1783 | pipe->set(unkn); |
mazgch | 21:c4d64830bf02 | 1784 | int ln = _parseFormated(pipe, len, lutF[i].fmt); |
mazgch | 21:c4d64830bf02 | 1785 | if (ln == WAIT && fr) |
mazgch | 21:c4d64830bf02 | 1786 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1787 | if ((ln != NOT_FOUND) && (unkn > 0)) |
mazgch | 31:a0bed6c1e05d | 1788 | return TYPE_UNKNOWN | pipe->get(buf, unkn); |
mazgch | 21:c4d64830bf02 | 1789 | if (ln > 0) |
mazgch | 21:c4d64830bf02 | 1790 | return lutF[i].type | pipe->get(buf, ln); |
mazgch | 21:c4d64830bf02 | 1791 | } |
mazgch | 21:c4d64830bf02 | 1792 | for (int i = 0; i < sizeof(lut)/sizeof(*lut); i ++) { |
mazgch | 21:c4d64830bf02 | 1793 | pipe->set(unkn); |
mazgch | 21:c4d64830bf02 | 1794 | int ln = _parseMatch(pipe, len, lut[i].sta, lut[i].end); |
mazgch | 21:c4d64830bf02 | 1795 | if (ln == WAIT && fr) |
mazgch | 21:c4d64830bf02 | 1796 | return WAIT; |
mazgch | 21:c4d64830bf02 | 1797 | if ((ln != NOT_FOUND) && (unkn > 0)) |
mazgch | 31:a0bed6c1e05d | 1798 | return TYPE_UNKNOWN | pipe->get(buf, unkn); |
mazgch | 21:c4d64830bf02 | 1799 | if (ln > 0) |
mazgch | 21:c4d64830bf02 | 1800 | return lut[i].type | pipe->get(buf, ln); |
mazgch | 21:c4d64830bf02 | 1801 | } |
mazgch | 21:c4d64830bf02 | 1802 | // UNKNOWN |
mazgch | 21:c4d64830bf02 | 1803 | unkn ++; |
mazgch | 21:c4d64830bf02 | 1804 | len--; |
mazgch | 21:c4d64830bf02 | 1805 | } |
mazgch | 18:e5697801df29 | 1806 | return WAIT; |
mazgch | 18:e5697801df29 | 1807 | } |
mazgch | 18:e5697801df29 | 1808 | |
mazgch | 18:e5697801df29 | 1809 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 1810 | // Serial Implementation |
mazgch | 18:e5697801df29 | 1811 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 1812 | |
mazgch | 76:f7c3dd568dae | 1813 | /*! Helper Dev Null Device |
mazgch | 76:f7c3dd568dae | 1814 | Small helper class used to shut off stderr/stdout. Sometimes stdin/stdout |
mazgch | 76:f7c3dd568dae | 1815 | is shared with the serial port of the modem. Having printfs inbetween the |
mazgch | 76:f7c3dd568dae | 1816 | AT commands you cause a failure of the modem. |
mazgch | 76:f7c3dd568dae | 1817 | */ |
mazgch | 76:f7c3dd568dae | 1818 | class DevNull : public Stream { |
mazgch | 76:f7c3dd568dae | 1819 | public: |
mazgch | 76:f7c3dd568dae | 1820 | DevNull() : Stream(_name+1) { } //!< Constructor |
mazgch | 76:f7c3dd568dae | 1821 | void claim(const char* mode, FILE* file) |
mazgch | 76:f7c3dd568dae | 1822 | { freopen(_name, mode, file); } //!< claim a stream |
mazgch | 76:f7c3dd568dae | 1823 | protected: |
mazgch | 76:f7c3dd568dae | 1824 | virtual int _getc() { return EOF; } //!< Nothing |
mazgch | 76:f7c3dd568dae | 1825 | virtual int _putc(int c) { return c; } //!< Discard |
mazgch | 76:f7c3dd568dae | 1826 | static const char* _name; //!< File name |
mazgch | 76:f7c3dd568dae | 1827 | }; |
mazgch | 76:f7c3dd568dae | 1828 | const char* DevNull::_name = "/null"; //!< the null device name |
mazgch | 76:f7c3dd568dae | 1829 | static DevNull null; //!< the null device |
mazgch | 76:f7c3dd568dae | 1830 | |
mazgch | 19:2b5d097ca15d | 1831 | MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, |
mazgch | 19:2b5d097ca15d | 1832 | int baudrate /*= MDMBAUD*/, |
mazgch | 43:a89a7a505991 | 1833 | #if DEVICE_SERIAL_FC |
mazgch | 19:2b5d097ca15d | 1834 | PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/, |
mazgch | 43:a89a7a505991 | 1835 | #endif |
mazgch | 18:e5697801df29 | 1836 | int rxSize /*= 256*/, int txSize /*= 128*/) : |
mazgch | 35:9275215a3a5b | 1837 | SerialPipe(tx, rx, rxSize, txSize) |
mazgch | 18:e5697801df29 | 1838 | { |
mazgch | 76:f7c3dd568dae | 1839 | if (rx == USBRX) |
mazgch | 76:f7c3dd568dae | 1840 | null.claim("r", stdin); |
mazgch | 76:f7c3dd568dae | 1841 | if (tx == USBTX) { |
mazgch | 76:f7c3dd568dae | 1842 | null.claim("w", stdout); |
mazgch | 76:f7c3dd568dae | 1843 | null.claim("w", stderr); |
mazgch | 74:208e3e32d263 | 1844 | #ifdef MDM_DEBUG |
mazgch | 76:f7c3dd568dae | 1845 | _debugLevel = -1; |
mazgch | 76:f7c3dd568dae | 1846 | #endif |
mazgch | 76:f7c3dd568dae | 1847 | } |
mazgch | 74:208e3e32d263 | 1848 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 1849 | _onboard = (tx == MDMTXD) && (rx == MDMRXD); |
mazgch | 74:208e3e32d263 | 1850 | if (_onboard) |
mazgch | 74:208e3e32d263 | 1851 | c027_mdm_powerOn(false); |
mazgch | 74:208e3e32d263 | 1852 | #endif |
mazgch | 18:e5697801df29 | 1853 | baud(baudrate); |
mazgch | 35:9275215a3a5b | 1854 | #if DEVICE_SERIAL_FC |
mazgch | 35:9275215a3a5b | 1855 | if ((rts != NC) || (cts != NC)) |
mazgch | 35:9275215a3a5b | 1856 | { |
mazgch | 35:9275215a3a5b | 1857 | Flow flow = (cts == NC) ? RTS : |
mazgch | 35:9275215a3a5b | 1858 | (rts == NC) ? CTS : RTSCTS ; |
mazgch | 35:9275215a3a5b | 1859 | set_flow_control(flow, rts, cts); |
mazgch | 35:9275215a3a5b | 1860 | if (cts != NC) _dev.lpm = LPM_ENABLED; |
mazgch | 35:9275215a3a5b | 1861 | } |
mazgch | 35:9275215a3a5b | 1862 | #endif |
mazgch | 18:e5697801df29 | 1863 | } |
mazgch | 18:e5697801df29 | 1864 | |
mazgch | 76:f7c3dd568dae | 1865 | MDMSerial::~MDMSerial(void) |
mazgch | 76:f7c3dd568dae | 1866 | { |
irsanjul | 120:353754a2bcd9 | 1867 | /*powerOff(); |
mazgch | 76:f7c3dd568dae | 1868 | #ifdef TARGET_UBLOX_C027 |
mazgch | 76:f7c3dd568dae | 1869 | if (_onboard) |
mazgch | 76:f7c3dd568dae | 1870 | c027_mdm_powerOff(); |
irsanjul | 120:353754a2bcd9 | 1871 | #endif*/ |
mazgch | 76:f7c3dd568dae | 1872 | } |
mazgch | 76:f7c3dd568dae | 1873 | |
mazgch | 18:e5697801df29 | 1874 | int MDMSerial::_send(const void* buf, int len) |
mazgch | 18:e5697801df29 | 1875 | { |
mazgch | 35:9275215a3a5b | 1876 | return put((const char*)buf, len, true/*=blocking*/); |
mazgch | 18:e5697801df29 | 1877 | } |
mazgch | 18:e5697801df29 | 1878 | |
mazgch | 18:e5697801df29 | 1879 | int MDMSerial::getLine(char* buffer, int length) |
mazgch | 18:e5697801df29 | 1880 | { |
mazgch | 18:e5697801df29 | 1881 | return _getLine(&_pipeRx, buffer, length); |
mazgch | 18:e5697801df29 | 1882 | } |
mazgch | 18:e5697801df29 | 1883 | |
mazgch | 18:e5697801df29 | 1884 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 1885 | // USB Implementation |
mazgch | 18:e5697801df29 | 1886 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 1887 | |
mazgch | 18:e5697801df29 | 1888 | #ifdef HAVE_MDMUSB |
mazgch | 76:f7c3dd568dae | 1889 | MDMUsb::MDMUsb(void) |
mazgch | 74:208e3e32d263 | 1890 | { |
mazgch | 74:208e3e32d263 | 1891 | #ifdef MDM_DEBUG |
mazgch | 74:208e3e32d263 | 1892 | _debugLevel = 1; |
mazgch | 74:208e3e32d263 | 1893 | #endif |
mazgch | 74:208e3e32d263 | 1894 | #ifdef TARGET_UBLOX_C027 |
mazgch | 74:208e3e32d263 | 1895 | _onboard = true; |
mazgch | 74:208e3e32d263 | 1896 | c027_mdm_powerOn(true); |
mazgch | 74:208e3e32d263 | 1897 | #endif |
mazgch | 74:208e3e32d263 | 1898 | } |
mazgch | 76:f7c3dd568dae | 1899 | |
mazgch | 76:f7c3dd568dae | 1900 | MDMUsb::~MDMUsb(void) |
mazgch | 76:f7c3dd568dae | 1901 | { |
irsanjul | 120:353754a2bcd9 | 1902 | /*powerOff(); |
mazgch | 76:f7c3dd568dae | 1903 | #ifdef TARGET_UBLOX_C027 |
mazgch | 76:f7c3dd568dae | 1904 | if (_onboard) |
mazgch | 76:f7c3dd568dae | 1905 | c027_mdm_powerOff(); |
irsanjul | 120:353754a2bcd9 | 1906 | #endif*/ |
mazgch | 76:f7c3dd568dae | 1907 | } |
mazgch | 76:f7c3dd568dae | 1908 | |
mazgch | 76:f7c3dd568dae | 1909 | int MDMUsb::_send(const void* buf, int len) { return 0; } |
mazgch | 76:f7c3dd568dae | 1910 | |
mazgch | 18:e5697801df29 | 1911 | int MDMUsb::getLine(char* buffer, int length) { return NOT_FOUND; } |
mazgch | 76:f7c3dd568dae | 1912 | |
mazgch | 35:9275215a3a5b | 1913 | #endif |