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