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