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.

Dependents:   C027Interface C027Interface C027_SupportTest

Fork of C027_Support by u-blox

Committer:
sarahmarshy
Date:
Thu Feb 02 21:15:50 2017 +0000
Revision:
142:38b7588c624d
Parent:
141:0d91c7fe072b
Use the new SerialBase constructor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mazgch 18:e5697801df29 1 #include "mbed.h"
mazgch 18:e5697801df29 2 #include "MDM.h"
mazgch 74:208e3e32d263 3 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 4 #include "C027_api.h"
mazgch 74:208e3e32d263 5 #endif
mazgch 85:dd8f4f0d0ca9 6 #include "MDMAPN.h"
mazgch 125:25a292afbac6 7
mazgch 74:208e3e32d263 8 #define PROFILE "0" //!< this is the psd profile used
Christopher Haster 140:b5614db52fc4 9 #define MAX_SIZE 1024 //!< max expected messages
mazgch 103:197fa7920ad8 10 // num sockets
mazgch 104:c64ba749a422 11 #define NUMSOCKETS (sizeof(_sockets)/sizeof(*_sockets))
mazgch 104:c64ba749a422 12 //! test if it is a socket is ok to use
mazgch 104:c64ba749a422 13 #define ISSOCKET(s) (((s) >= 0) && ((s) < NUMSOCKETS) && (_sockets[s].handle != SOCKET_ERROR))
mazgch 75:ce6e12067d0c 14 //! check for timeout
mazgch 75:ce6e12067d0c 15 #define TIMEOUT(t, ms) ((ms != TIMEOUT_BLOCKING) && (ms < t.read_ms()))
fdilenarda 135:cbccf4052d45 16 // num HTTP profiles
fdilenarda 135:cbccf4052d45 17 #define NUMPROFILES (sizeof(_httpProfiles)/sizeof(*_httpProfiles))
fdilenarda 135:cbccf4052d45 18 //! test if it is an HTTP profile is ok to use
fdilenarda 135:cbccf4052d45 19 #define ISPROFILE(p) (((p) >= 0) && ((p) < NUMPROFILES) && (_httpProfiles[p].handle != HTTP_PROF_ERROR))
mazgch 80:34985b4d821e 20 //! registration ok check helper
mazgch 79:291df065e345 21 #define REG_OK(r) ((r == REG_HOME) || (r == REG_ROAMING))
mazgch 80:34985b4d821e 22 //! registration done check helper (no need to poll further)
mazgch 79:291df065e345 23 #define REG_DONE(r) ((r == REG_HOME) || (r == REG_ROAMING) || (r == REG_DENIED))
mazgch 95:8282dbbe1492 24 //! helper to make sure that lock unlock pair is always balaced
mazgch 95:8282dbbe1492 25 #define LOCK() { lock()
mazgch 95:8282dbbe1492 26 //! helper to make sure that lock unlock pair is always balaced
mazgch 95:8282dbbe1492 27 #define UNLOCK() } unlock()
mazgch 79:291df065e345 28
mazgch 74:208e3e32d263 29 #ifdef MDM_DEBUG
mazgch 95:8282dbbe1492 30 #if 1 // colored terminal output using ANSI escape sequences
mazgch 95:8282dbbe1492 31 #define COL(c) "\033[" c
mazgch 95:8282dbbe1492 32 #else
mazgch 95:8282dbbe1492 33 #define COL(c)
mazgch 95:8282dbbe1492 34 #endif
mazgch 95:8282dbbe1492 35 #define DEF COL("39m")
mazgch 95:8282dbbe1492 36 #define BLA COL("30m")
mazgch 95:8282dbbe1492 37 #define RED COL("31m")
mazgch 95:8282dbbe1492 38 #define GRE COL("32m")
mazgch 95:8282dbbe1492 39 #define YEL COL("33m")
mazgch 95:8282dbbe1492 40 #define BLU COL("34m")
mazgch 95:8282dbbe1492 41 #define MAG COL("35m")
mazgch 95:8282dbbe1492 42 #define CYA COL("36m")
mazgch 95:8282dbbe1492 43 #define WHY COL("37m")
mazgch 95:8282dbbe1492 44
mazgch 74:208e3e32d263 45 void dumpAtCmd(const char* buf, int len)
mazgch 21:c4d64830bf02 46 {
mazgch 75:ce6e12067d0c 47 ::printf(" %3d \"", len);
mazgch 21:c4d64830bf02 48 while (len --) {
mazgch 21:c4d64830bf02 49 char ch = *buf++;
mazgch 75:ce6e12067d0c 50 if ((ch > 0x1F) && (ch != 0x7F)) { // is printable
mazgch 75:ce6e12067d0c 51 if (ch == '%') ::printf("%%");
mazgch 75:ce6e12067d0c 52 else if (ch == '"') ::printf("\\\"");
mazgch 75:ce6e12067d0c 53 else if (ch == '\\') ::printf("\\\\");
mazgch 75:ce6e12067d0c 54 else putchar(ch);
mazgch 75:ce6e12067d0c 55 } else {
mazgch 75:ce6e12067d0c 56 if (ch == '\a') ::printf("\\a"); // BEL (0x07)
mazgch 75:ce6e12067d0c 57 else if (ch == '\b') ::printf("\\b"); // Backspace (0x08)
mazgch 75:ce6e12067d0c 58 else if (ch == '\t') ::printf("\\t"); // Horizontal Tab (0x09)
mazgch 75:ce6e12067d0c 59 else if (ch == '\n') ::printf("\\n"); // Linefeed (0x0A)
mazgch 75:ce6e12067d0c 60 else if (ch == '\v') ::printf("\\v"); // Vertical Tab (0x0B)
mazgch 75:ce6e12067d0c 61 else if (ch == '\f') ::printf("\\f"); // Formfeed (0x0C)
mazgch 75:ce6e12067d0c 62 else if (ch == '\r') ::printf("\\r"); // Carriage Return (0x0D)
mazgch 76:f7c3dd568dae 63 else ::printf("\\x%02x", (unsigned char)ch);
mazgch 75:ce6e12067d0c 64 }
mazgch 21:c4d64830bf02 65 }
mazgch 75:ce6e12067d0c 66 ::printf("\"\r\n");
mazgch 21:c4d64830bf02 67 }
mazgch 74:208e3e32d263 68
mazgch 117:74e4e0109a9e 69 void MDMParser::_debugPrint(int level, const char* color, const char* format, ...)
mazgch 117:74e4e0109a9e 70 {
mazgch 117:74e4e0109a9e 71 if (_debugLevel >= level)
mazgch 117:74e4e0109a9e 72 {
mazgch 117:74e4e0109a9e 73 va_list args;
mazgch 117:74e4e0109a9e 74 va_start (args, format);
mazgch 117:74e4e0109a9e 75 if (color) ::printf(color);
mazgch 117:74e4e0109a9e 76 ::vprintf(format, args);
mazgch 117:74e4e0109a9e 77 if (color) ::printf(DEF);
mazgch 117:74e4e0109a9e 78 va_end (args);
mazgch 117:74e4e0109a9e 79 }
mazgch 117:74e4e0109a9e 80 }
mazgch 117:74e4e0109a9e 81
mazgch 117:74e4e0109a9e 82 #define ERROR(...) _debugPrint(0, RED, __VA_ARGS__)
mazgch 117:74e4e0109a9e 83 #define INFO(...) _debugPrint(1, GRE, __VA_ARGS__)
mazgch 117:74e4e0109a9e 84 #define TRACE(...) _debugPrint(2, DEF, __VA_ARGS__)
mazgch 117:74e4e0109a9e 85 #define TEST(...) _debugPrint(3, CYA, __VA_ARGS__)
mazgch 74:208e3e32d263 86
mazgch 74:208e3e32d263 87 #else
mazgch 74:208e3e32d263 88
mazgch 75:ce6e12067d0c 89 #define ERROR(...) (void)0 // no tracing
mazgch 95:8282dbbe1492 90 #define TEST(...) (void)0 // no tracing
mazgch 74:208e3e32d263 91 #define INFO(...) (void)0 // no tracing
mazgch 74:208e3e32d263 92 #define TRACE(...) (void)0 // no tracing
mazgch 74:208e3e32d263 93
mazgch 31:a0bed6c1e05d 94 #endif
mazgch 21:c4d64830bf02 95
mazgch 44:9d12223b78ff 96 MDMParser* MDMParser::inst;
mazgch 44:9d12223b78ff 97
mazgch 21:c4d64830bf02 98 MDMParser::MDMParser(void)
mazgch 21:c4d64830bf02 99 {
mazgch 44:9d12223b78ff 100 inst = this;
mazgch 31:a0bed6c1e05d 101 memset(&_dev, 0, sizeof(_dev));
mazgch 31:a0bed6c1e05d 102 memset(&_net, 0, sizeof(_net));
mazgch 54:7ba8e4c218e2 103 _net.lac = 0xFFFF;
mazgch 54:7ba8e4c218e2 104 _net.ci = 0xFFFFFFFF;
mazgch 35:9275215a3a5b 105 _ip = NOIP;
mazgch 76:f7c3dd568dae 106 _init = false;
mazgch 31:a0bed6c1e05d 107 memset(_sockets, 0, sizeof(_sockets));
mazgch 104:c64ba749a422 108 for (int socket = 0; socket < NUMSOCKETS; socket ++)
mazgch 103:197fa7920ad8 109 _sockets[socket].handle = SOCKET_ERROR;
fdilenarda 135:cbccf4052d45 110 memset(_httpProfiles, 0, sizeof(_httpProfiles));
fdilenarda 135:cbccf4052d45 111 for (int profile = 0; profile < NUMPROFILES; profile ++)
fdilenarda 135:cbccf4052d45 112 _httpProfiles[profile].handle = HTTP_PROF_ERROR;
mazgch 74:208e3e32d263 113 #ifdef MDM_DEBUG
mazgch 76:f7c3dd568dae 114 _debugLevel = 1;
mazgch 74:208e3e32d263 115 _debugTime.start();
mazgch 74:208e3e32d263 116 #endif
mazgch 74:208e3e32d263 117 }
mazgch 74:208e3e32d263 118
mazgch 18:e5697801df29 119 int MDMParser::send(const char* buf, int len)
mazgch 18:e5697801df29 120 {
mazgch 74:208e3e32d263 121 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 122 if (_debugLevel >= 3) {
mazgch 75:ce6e12067d0c 123 ::printf("%10.3f AT send ", _debugTime.read_ms()*0.001);
mazgch 74:208e3e32d263 124 dumpAtCmd(buf,len);
mazgch 74:208e3e32d263 125 }
mazgch 21:c4d64830bf02 126 #endif
mazgch 18:e5697801df29 127 return _send(buf, len);
mazgch 18:e5697801df29 128 }
mazgch 18:e5697801df29 129
mazgch 21:c4d64830bf02 130 int MDMParser::sendFormated(const char* format, ...) {
Christopher Haster 140:b5614db52fc4 131 char *buf = (char *)malloc(MAX_SIZE);
mazgch 21:c4d64830bf02 132 va_list args;
mazgch 21:c4d64830bf02 133 va_start(args, format);
Christopher Haster 140:b5614db52fc4 134 int len = vsnprintf(buf,MAX_SIZE, format, args);
mazgch 21:c4d64830bf02 135 va_end(args);
Christopher Haster 140:b5614db52fc4 136 int ret = send(buf, len);
Christopher Haster 140:b5614db52fc4 137 free(buf);
Christopher Haster 140:b5614db52fc4 138 return ret;
mazgch 21:c4d64830bf02 139 }
mazgch 21:c4d64830bf02 140
mazgch 26:07be5faf8925 141 int MDMParser::waitFinalResp(_CALLBACKPTR cb /* = NULL*/,
mazgch 26:07be5faf8925 142 void* param /* = NULL*/,
mazgch 26:07be5faf8925 143 int timeout_ms /*= 5000*/)
mazgch 26:07be5faf8925 144 {
Christopher Haster 140:b5614db52fc4 145 char *buf = (char*)malloc(MAX_SIZE + 64);
mazgch 21:c4d64830bf02 146 Timer timer;
mazgch 21:c4d64830bf02 147 timer.start();
mazgch 21:c4d64830bf02 148 do {
Christopher Haster 140:b5614db52fc4 149 int ret = getLine(buf, MAX_SIZE + 64);
mazgch 74:208e3e32d263 150 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 151 if ((_debugLevel >= 3) && (ret != WAIT) && (ret != NOT_FOUND))
mazgch 21:c4d64830bf02 152 {
mazgch 31:a0bed6c1e05d 153 int len = LENGTH(ret);
mazgch 31:a0bed6c1e05d 154 int type = TYPE(ret);
mazgch 95:8282dbbe1492 155 const char* s = (type == TYPE_UNKNOWN)? YEL "UNK" DEF :
mazgch 95:8282dbbe1492 156 (type == TYPE_TEXT) ? MAG "TXT" DEF :
mazgch 95:8282dbbe1492 157 (type == TYPE_OK ) ? GRE "OK " DEF :
mazgch 95:8282dbbe1492 158 (type == TYPE_ERROR) ? RED "ERR" DEF :
mazgch 95:8282dbbe1492 159 (type == TYPE_PLUS) ? CYA " + " DEF :
mazgch 95:8282dbbe1492 160 (type == TYPE_PROMPT) ? BLU " > " DEF :
mazgch 95:8282dbbe1492 161 "..." ;
mazgch 75:ce6e12067d0c 162 ::printf("%10.3f AT read %s", _debugTime.read_ms()*0.001, s);
mazgch 74:208e3e32d263 163 dumpAtCmd(buf, len);
mazgch 21:c4d64830bf02 164 }
mazgch 21:c4d64830bf02 165 #endif
mazgch 21:c4d64830bf02 166 if ((ret != WAIT) && (ret != NOT_FOUND))
mazgch 18:e5697801df29 167 {
mazgch 21:c4d64830bf02 168 int type = TYPE(ret);
mazgch 21:c4d64830bf02 169 // handle unsolicited commands here
mazgch 21:c4d64830bf02 170 if (type == TYPE_PLUS) {
mazgch 21:c4d64830bf02 171 const char* cmd = buf+3;
mazgch 54:7ba8e4c218e2 172 int a, b, c, d, r;
mazgch 23:05a1aeeb5fd9 173 char s[32];
msinig 133:57b208dd96fb 174
mazgch 31:a0bed6c1e05d 175 // SMS Command ---------------------------------
mazgch 31:a0bed6c1e05d 176 // +CNMI: <mem>,<index>
mazgch 31:a0bed6c1e05d 177 if (sscanf(cmd, "CMTI: \"%*[^\"]\",%d", &a) == 1) {
mazgch 31:a0bed6c1e05d 178 TRACE("New SMS at index %d\r\n", a);
mazgch 21:c4d64830bf02 179 // Socket Specific Command ---------------------------------
mazgch 21:c4d64830bf02 180 // +UUSORD: <socket>,<length>
mazgch 103:197fa7920ad8 181 } else if ((sscanf(cmd, "UUSORD: %d,%d", &a, &b) == 2)) {
mazgch 103:197fa7920ad8 182 int socket = _findSocket(a);
mazgch 103:197fa7920ad8 183 TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b);
mazgch 103:197fa7920ad8 184 if (socket != SOCKET_ERROR)
mazgch 103:197fa7920ad8 185 _sockets[socket].pending = b;
mazgch 69:4d6fa520dfca 186 // +UUSORF: <socket>,<length>
mazgch 103:197fa7920ad8 187 } else if ((sscanf(cmd, "UUSORF: %d,%d", &a, &b) == 2)) {
mazgch 103:197fa7920ad8 188 int socket = _findSocket(a);
mazgch 103:197fa7920ad8 189 TRACE("Socket %d: handle %d has %d bytes pending\r\n", socket, a, b);
mazgch 103:197fa7920ad8 190 if (socket != SOCKET_ERROR)
mazgch 103:197fa7920ad8 191 _sockets[socket].pending = b;
mazgch 21:c4d64830bf02 192 // +UUSOCL: <socket>
mazgch 103:197fa7920ad8 193 } else if ((sscanf(cmd, "UUSOCL: %d", &a) == 1)) {
mazgch 103:197fa7920ad8 194 int socket = _findSocket(a);
mazgch 103:197fa7920ad8 195 TRACE("Socket %d: handle %d closed by remote host\r\n", socket, a);
mazgch 103:197fa7920ad8 196 if ((socket != SOCKET_ERROR) && _sockets[socket].connected)
msinig 136:8dc8f48275fc 197 _sockets[socket].connected = false;
msinig 133:57b208dd96fb 198 // +UULOC: <date>,<time>,<lat>,<long>,<alt>,<uncertainty>,<speed>, <direction>,<vertical_acc>,<sensor_used>,<SV_used>,<antenna_status>, <jamming_status>
msinig 133:57b208dd96fb 199 }else if (sscanf(cmd, "UULOC: %d/%d/%d,%d:%d:%d.%*d,%f,%f,%d,%d,%d,%d,%d,%d,%d,%*d,%*d",\
msinig 136:8dc8f48275fc 200 &_loc[0].time.tm_mday, &_loc[0].time.tm_mon, &_loc[0].time.tm_year, &_loc[0].time.tm_hour, &_loc[0].time.tm_min, &_loc[0].time.tm_sec,\
msinig 136:8dc8f48275fc 201 &_loc[0].latitude, &_loc[0].longitude, &_loc[0].altitutude, &_loc[0].uncertainty, &_loc[0].speed, &_loc[0].direction, &_loc[0].verticalAcc, \
msinig 136:8dc8f48275fc 202 &b, &_loc[0].svUsed) == 15) {
msinig 136:8dc8f48275fc 203 TRACE("Parsed UULOC position at index 0\r\n");
msinig 136:8dc8f48275fc 204 _loc[0].sensor = (b==0)? CELL_LAST : (b==1)? CELL_GNSS : (b==2)? CELL_LOCATE : (b==3)? CELL_HYBRID : CELL_LAST;
msinig 136:8dc8f48275fc 205 _loc[0].time.tm_mon -= 1;
msinig 136:8dc8f48275fc 206 _loc[0].time.tm_wday=0;
msinig 136:8dc8f48275fc 207 _loc[0].time.tm_yday=0;
msinig 136:8dc8f48275fc 208 _loc[0].validData = true;
msinig 136:8dc8f48275fc 209 _locExpPos=1;
msinig 136:8dc8f48275fc 210 _locRcvPos++;
msinig 136:8dc8f48275fc 211 // +UULOC: <sol>,<num>,<sensor_used>,<date>,<time>,<lat>,<long>,<alt>,<uncertainty>,<speed>, <direction>,<vertical_acc>,,<SV_used>,<antenna_status>, <jamming_status>
msinig 136:8dc8f48275fc 212 }else if (sscanf(cmd, "UULOC: %d,%d,%d,%d/%d/%d,%d:%d:%d.%*d,%f,%f,%d,%d,%d,%d,%d,%d,%*d,%*d",\
msinig 136:8dc8f48275fc 213 &a,&_locExpPos,&b, \
msinig 136:8dc8f48275fc 214 &_loc[CELL_MAX_HYP-1].time.tm_mday, &_loc[CELL_MAX_HYP-1].time.tm_mon, &_loc[CELL_MAX_HYP-1].time.tm_year, &_loc[CELL_MAX_HYP-1].time.tm_hour, &_loc[CELL_MAX_HYP-1].time.tm_min, &_loc[CELL_MAX_HYP-1].time.tm_sec,\
msinig 136:8dc8f48275fc 215 &_loc[CELL_MAX_HYP-1].latitude, &_loc[CELL_MAX_HYP-1].longitude, &_loc[CELL_MAX_HYP-1].altitutude, &_loc[CELL_MAX_HYP-1].uncertainty, &_loc[CELL_MAX_HYP-1].speed, &_loc[CELL_MAX_HYP-1].direction, &_loc[CELL_MAX_HYP-1].verticalAcc, \
msinig 136:8dc8f48275fc 216 &_loc[CELL_MAX_HYP-1].svUsed) == 17) {
msinig 136:8dc8f48275fc 217 if (--a>=0){
msinig 136:8dc8f48275fc 218 TRACE("Parsed UULOC position at index %d\r\n",a);
msinig 136:8dc8f48275fc 219 memcpy(&_loc[a], &_loc[CELL_MAX_HYP-1], sizeof(*_loc));
msinig 136:8dc8f48275fc 220 _loc[a].sensor = (b==0)? CELL_LAST : (b==1)? CELL_GNSS : (b==2)? CELL_LOCATE : (b==3)? CELL_HYBRID : CELL_LAST;
msinig 136:8dc8f48275fc 221 _loc[a].time.tm_mon -= 1;
msinig 136:8dc8f48275fc 222 _loc[a].time.tm_wday=0;
msinig 136:8dc8f48275fc 223 _loc[a].time.tm_yday=0;
msinig 136:8dc8f48275fc 224 _loc[a].validData = true;
msinig 136:8dc8f48275fc 225 _locRcvPos++;
msinig 136:8dc8f48275fc 226 }
msinig 136:8dc8f48275fc 227 //+UULOC: <sol>,<num>,<sensor_used>,<date>,<time>,<lat>,<long>,<alt>,<lat50>,<long50>,<major50>,<minor50>,<orientation50>,<confidence50>[,<lat95>,<long95>,<major95>,<minor95>,<orientation95>,<confidence95>]
msinig 136:8dc8f48275fc 228 }else if (sscanf(cmd, "UULOC: %d,%d,%d,%d/%d/%d,%d:%d:%d.%*d,%f,%f,%d,%*f,%*f,%d,%*d,%*d,%*d",\
msinig 136:8dc8f48275fc 229 &a,&_locExpPos,&b, \
msinig 136:8dc8f48275fc 230 &_loc[CELL_MAX_HYP-1].time.tm_mday, &_loc[CELL_MAX_HYP-1].time.tm_mon, &_loc[CELL_MAX_HYP-1].time.tm_year, &_loc[CELL_MAX_HYP-1].time.tm_hour, &_loc[CELL_MAX_HYP-1].time.tm_min, &_loc[CELL_MAX_HYP-1].time.tm_sec,\
msinig 136:8dc8f48275fc 231 &_loc[CELL_MAX_HYP-1].latitude, &_loc[CELL_MAX_HYP-1].longitude, &_loc[CELL_MAX_HYP-1].altitutude, &_loc[CELL_MAX_HYP-1].uncertainty) == 13) {
msinig 136:8dc8f48275fc 232 if (--a>=0){
msinig 136:8dc8f48275fc 233 TRACE("Parsed UULOC position at index %d\r\n",a);
msinig 136:8dc8f48275fc 234 memcpy(&_loc[a], &_loc[CELL_MAX_HYP-1], sizeof(*_loc));
msinig 136:8dc8f48275fc 235 _loc[a].sensor = (b==0)? CELL_LAST : (b==1)? CELL_GNSS : (b==2)? CELL_LOCATE : (b==3)? CELL_HYBRID : CELL_LAST;
msinig 136:8dc8f48275fc 236 _loc[a].time.tm_mon -= 1;
msinig 136:8dc8f48275fc 237 _loc[a].time.tm_wday=0;
msinig 136:8dc8f48275fc 238 _loc[a].time.tm_yday=0;
msinig 136:8dc8f48275fc 239 _loc[a].validData = true;
msinig 136:8dc8f48275fc 240 _locRcvPos++;
msinig 136:8dc8f48275fc 241 }
fdilenarda 135:cbccf4052d45 242 // +UHTTPCR: <profile_id>,<op_code>,<param_val>
fdilenarda 135:cbccf4052d45 243 } else if ((sscanf(cmd, "UUHTTPCR: %d,%d,%d", &a, &b, &c) == 3)) {
fdilenarda 135:cbccf4052d45 244 _httpProfiles[a].cmd = b; //command
fdilenarda 135:cbccf4052d45 245 _httpProfiles[a].result = c; //result
fdilenarda 135:cbccf4052d45 246 TRACE("%s for profile %d: result code is %d\r\n", getHTTPcmd(b), a, c);
fdilenarda 135:cbccf4052d45 247 }
fdilenarda 135:cbccf4052d45 248 if (_dev.dev == DEV_LISA_C2) {
mazgch 21:c4d64830bf02 249 // CDMA Specific -------------------------------------------
mazgch 21:c4d64830bf02 250 // +CREG: <n><SID>,<NID>,<stat>
mazgch 54:7ba8e4c218e2 251 if (sscanf(cmd, "CREG: %*d,%d,%d,%d",&a,&b,&c) == 3) {
mazgch 54:7ba8e4c218e2 252 // _net.sid = a;
mazgch 54:7ba8e4c218e2 253 // _net.nid = b;
mazgch 79:291df065e345 254 if (c == 0) _net.csd = REG_NONE; // not registered, home network
mazgch 79:291df065e345 255 else if (c == 1) _net.csd = REG_HOME; // registered, home network
mazgch 79:291df065e345 256 else if (c == 2) _net.csd = REG_NONE; // not registered, but MT is currently searching a new operator to register to
mazgch 79:291df065e345 257 else if (c == 3) _net.csd = REG_DENIED; // registration denied
mazgch 79:291df065e345 258 else if (c == 5) _net.csd = REG_ROAMING; // registered, roaming
mazgch 79:291df065e345 259 _net.psd = _net.csd; // fake PSD registration (CDMA is always registered)
mazgch 31:a0bed6c1e05d 260 _net.act = ACT_CDMA;
mazgch 84:a05edb010176 261 // +CSS: <mode>[,<format>,<oper>[,<AcT>]]
mazgch 21:c4d64830bf02 262 } else if (sscanf(cmd, "CSS %*c,%2s,%*d",s) == 1) {
mazgch 31:a0bed6c1e05d 263 //_net.reg = (strcmp("Z", s) == 0) ? REG_UNKNOWN : REG_HOME;
mazgch 21:c4d64830bf02 264 }
mazgch 21:c4d64830bf02 265 } else {
mazgch 21:c4d64830bf02 266 // GSM/UMTS Specific -------------------------------------------
mazgch 79:291df065e345 267 // +UUPSDD: <profile_id>
mazgch 79:291df065e345 268 if (sscanf(cmd, "UUPSDD: %d",&a) == 1) {
mazgch 79:291df065e345 269 if (*PROFILE == a) _ip = NOIP;
mazgch 79:291df065e345 270 } else {
mazgch 79:291df065e345 271 // +CREG|CGREG: <n>,<stat>[,<lac>,<ci>[,AcT[,<rac>]]] // reply to AT+CREG|AT+CGREG
mazgch 79:291df065e345 272 // +CREG|CGREG: <stat>[,<lac>,<ci>[,AcT[,<rac>]]] // URC
mazgch 79:291df065e345 273 b = 0xFFFF; c = 0xFFFFFFFF; d = -1;
mazgch 79:291df065e345 274 r = sscanf(cmd, "%s %*d,%d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d);
mazgch 79:291df065e345 275 if (r <= 1)
mazgch 79:291df065e345 276 r = sscanf(cmd, "%s %d,\"%X\",\"%X\",%d",s,&a,&b,&c,&d);
mazgch 79:291df065e345 277 if (r >= 2) {
mazgch 79:291df065e345 278 Reg *reg = !strcmp(s, "CREG:") ? &_net.csd :
mazgch 123:66cef6353b13 279 !strcmp(s, "CGREG:") ? &_net.psd :
mazgch 123:66cef6353b13 280 !strcmp(s, "CEREG:") ? &_net.eps : NULL;
mazgch 79:291df065e345 281 if (reg) {
mazgch 79:291df065e345 282 // network status
mazgch 79:291df065e345 283 if (a == 0) *reg = REG_NONE; // 0: not registered, home network
mazgch 79:291df065e345 284 else if (a == 1) *reg = REG_HOME; // 1: registered, home network
mazgch 79:291df065e345 285 else if (a == 2) *reg = REG_NONE; // 2: not registered, but MT is currently searching a new operator to register to
mazgch 79:291df065e345 286 else if (a == 3) *reg = REG_DENIED; // 3: registration denied
mazgch 79:291df065e345 287 else if (a == 4) *reg = REG_UNKNOWN; // 4: unknown
mazgch 79:291df065e345 288 else if (a == 5) *reg = REG_ROAMING; // 5: registered, roaming
mazgch 123:66cef6353b13 289 else if (a == 6) *reg = REG_HOME; // 6: registered, sms only, home
mazgch 79:291df065e345 290 if ((r >= 3) && (b != 0xFFFF)) _net.lac = b; // location area code
msinig 136:8dc8f48275fc 291 if ((r >= 4) && (c != 0xFFFFFFFF)) _net.ci = c; // cell ID
mazgch 79:291df065e345 292 // access technology
mazgch 79:291df065e345 293 if (r >= 5) {
mazgch 79:291df065e345 294 if (d == 0) _net.act = ACT_GSM; // 0: GSM
mazgch 79:291df065e345 295 else if (d == 1) _net.act = ACT_GSM; // 1: GSM COMPACT
mazgch 79:291df065e345 296 else if (d == 2) _net.act = ACT_UTRAN; // 2: UTRAN
mazgch 79:291df065e345 297 else if (d == 3) _net.act = ACT_EDGE; // 3: GSM with EDGE availability
mazgch 79:291df065e345 298 else if (d == 4) _net.act = ACT_UTRAN; // 4: UTRAN with HSDPA availability
mazgch 79:291df065e345 299 else if (d == 5) _net.act = ACT_UTRAN; // 5: UTRAN with HSUPA availability
mazgch 79:291df065e345 300 else if (d == 6) _net.act = ACT_UTRAN; // 6: UTRAN with HSDPA and HSUPA availability
mazgch 123:66cef6353b13 301 else if (d == 7) _net.act = ACT_LTE; // 7: LTE
mazgch 79:291df065e345 302 }
mazgch 79:291df065e345 303 }
mazgch 54:7ba8e4c218e2 304 }
mazgch 21:c4d64830bf02 305 }
mazgch 21:c4d64830bf02 306 }
mazgch 21:c4d64830bf02 307 }
mazgch 21:c4d64830bf02 308 if (cb) {
mazgch 21:c4d64830bf02 309 int len = LENGTH(ret);
mazgch 21:c4d64830bf02 310 int ret = cb(type, buf, len, param);
Christopher Haster 140:b5614db52fc4 311 if (WAIT != ret) {
Christopher Haster 140:b5614db52fc4 312 free(buf);
mazgch 21:c4d64830bf02 313 return ret;
Christopher Haster 140:b5614db52fc4 314 }
mazgch 21:c4d64830bf02 315 }
Christopher Haster 140:b5614db52fc4 316 if (type == TYPE_OK) {
Christopher Haster 140:b5614db52fc4 317 free(buf);
mazgch 95:8282dbbe1492 318 return RESP_OK;
Christopher Haster 140:b5614db52fc4 319 }
Christopher Haster 140:b5614db52fc4 320 if (type == TYPE_ERROR) {
Christopher Haster 140:b5614db52fc4 321 free(buf);
mazgch 95:8282dbbe1492 322 return RESP_ERROR;
Christopher Haster 140:b5614db52fc4 323 }
Christopher Haster 140:b5614db52fc4 324 if (type == TYPE_ERROR_CME) {
Christopher Haster 140:b5614db52fc4 325 free(buf);
Christopher Haster 140:b5614db52fc4 326 return RESP_ERROR_CME;
Christopher Haster 140:b5614db52fc4 327 }
Christopher Haster 140:b5614db52fc4 328 if (type == TYPE_PROMPT) {
Christopher Haster 140:b5614db52fc4 329 free(buf);
mazgch 95:8282dbbe1492 330 return RESP_PROMPT;
Christopher Haster 140:b5614db52fc4 331 }
mazgch 21:c4d64830bf02 332 }
mazgch 21:c4d64830bf02 333 // relax a bit
mazgch 95:8282dbbe1492 334 wait_ms(10);
mazgch 21:c4d64830bf02 335 }
mazgch 75:ce6e12067d0c 336 while (!TIMEOUT(timer, timeout_ms));
Christopher Haster 140:b5614db52fc4 337 free(buf);
mazgch 21:c4d64830bf02 338 return WAIT;
mazgch 21:c4d64830bf02 339 }
mazgch 21:c4d64830bf02 340
mazgch 31:a0bed6c1e05d 341 int MDMParser::_cbString(int type, const char* buf, int len, char* str)
mazgch 21:c4d64830bf02 342 {
mazgch 37:cc3433329d66 343 if (str && (type == TYPE_UNKNOWN)) {
mazgch 31:a0bed6c1e05d 344 if (sscanf(buf, "\r\n%s\r\n", str) == 1)
mazgch 31:a0bed6c1e05d 345 /*nothing*/;
mazgch 21:c4d64830bf02 346 }
mazgch 21:c4d64830bf02 347 return WAIT;
mazgch 21:c4d64830bf02 348 }
mazgch 21:c4d64830bf02 349
mazgch 31:a0bed6c1e05d 350 int MDMParser::_cbInt(int type, const char* buf, int len, int* val)
mazgch 31:a0bed6c1e05d 351 {
mazgch 37:cc3433329d66 352 if (val && (type == TYPE_UNKNOWN)) {
mazgch 31:a0bed6c1e05d 353 if (sscanf(buf, "\r\n%d\r\n", val) == 1)
mazgch 31:a0bed6c1e05d 354 /*nothing*/;
mazgch 31:a0bed6c1e05d 355 }
mazgch 31:a0bed6c1e05d 356 return WAIT;
mazgch 31:a0bed6c1e05d 357 }
mazgch 31:a0bed6c1e05d 358
mazgch 31:a0bed6c1e05d 359 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 360
mazgch 57:869bd35f44cc 361 bool MDMParser::connect(
mazgch 57:869bd35f44cc 362 const char* simpin,
mazgch 80:34985b4d821e 363 const char* apn, const char* username,
mazgch 80:34985b4d821e 364 const char* password, Auth auth,
mazgch 74:208e3e32d263 365 PinName pn)
mazgch 57:869bd35f44cc 366 {
mazgch 75:ce6e12067d0c 367 bool ok = init(simpin, NULL, pn);
mazgch 74:208e3e32d263 368 #ifdef MDM_DEBUG
mazgch 75:ce6e12067d0c 369 if (_debugLevel >= 1) dumpDevStatus(&_dev);
mazgch 74:208e3e32d263 370 #endif
mazgch 75:ce6e12067d0c 371 if (!ok)
mazgch 57:869bd35f44cc 372 return false;
mazgch 75:ce6e12067d0c 373 ok = registerNet();
mazgch 74:208e3e32d263 374 #ifdef MDM_DEBUG
mazgch 75:ce6e12067d0c 375 if (_debugLevel >= 1) dumpNetStatus(&_net);
mazgch 74:208e3e32d263 376 #endif
mazgch 75:ce6e12067d0c 377 if (!ok)
mazgch 57:869bd35f44cc 378 return false;
mazgch 80:34985b4d821e 379 IP ip = join(apn,username,password,auth);
mazgch 74:208e3e32d263 380 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 381 if (_debugLevel >= 1) dumpIp(ip);
mazgch 74:208e3e32d263 382 #endif
mazgch 57:869bd35f44cc 383 if (ip == NOIP)
mazgch 57:869bd35f44cc 384 return false;
mazgch 57:869bd35f44cc 385 return true;
mazgch 57:869bd35f44cc 386 }
mazgch 57:869bd35f44cc 387
mazgch 74:208e3e32d263 388 bool MDMParser::init(const char* simpin, DevStatus* status, PinName pn)
mazgch 21:c4d64830bf02 389 {
mazgch 74:208e3e32d263 390 int i = 10;
mazgch 95:8282dbbe1492 391 LOCK();
mazgch 76:f7c3dd568dae 392 memset(&_dev, 0, sizeof(_dev));
mazgch 74:208e3e32d263 393 if (pn != NC) {
mazgch 74:208e3e32d263 394 INFO("Modem::wakeup\r\n");
mazgch 74:208e3e32d263 395 DigitalOut pin(pn, 1);
mazgch 74:208e3e32d263 396 while (i--) {
mazgch 79:291df065e345 397 // SARA-U2/LISA-U2 50..80us
mazgch 95:8282dbbe1492 398 pin = 0; ::wait_us(50);
mazgch 95:8282dbbe1492 399 pin = 1; ::wait_ms(10);
mazgch 79:291df065e345 400
mazgch 98:c786461edd40 401 // SARA-G35 >5ms, LISA-C2 > 150ms, LEON-G2 >5ms
mazgch 95:8282dbbe1492 402 pin = 0; ::wait_ms(150);
mazgch 95:8282dbbe1492 403 pin = 1; ::wait_ms(100);
mazgch 79:291df065e345 404
mazgch 79:291df065e345 405 // purge any messages
mazgch 101:edfeb8af206e 406 purge();
mazgch 95:8282dbbe1492 407
mazgch 101:edfeb8af206e 408 // check interface
mazgch 74:208e3e32d263 409 sendFormated("AT\r\n");
mazgch 101:edfeb8af206e 410 int r = waitFinalResp(NULL,NULL,1000);
mazgch 95:8282dbbe1492 411 if(RESP_OK == r) break;
mazgch 74:208e3e32d263 412 }
mazgch 75:ce6e12067d0c 413 if (i < 0) {
mazgch 95:8282dbbe1492 414 ERROR("No Reply from Modem\r\n");
mazgch 95:8282dbbe1492 415 goto failure;
mazgch 75:ce6e12067d0c 416 }
mazgch 21:c4d64830bf02 417 }
mazgch 76:f7c3dd568dae 418 _init = true;
mazgch 75:ce6e12067d0c 419
mazgch 74:208e3e32d263 420 INFO("Modem::init\r\n");
mazgch 21:c4d64830bf02 421 // echo off
mazgch 21:c4d64830bf02 422 sendFormated("AT E0\r\n");
mazgch 52:8071747a7cb3 423 if(RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 424 goto failure;
mazgch 21:c4d64830bf02 425 // enable verbose error messages
mazgch 21:c4d64830bf02 426 sendFormated("AT+CMEE=2\r\n");
mazgch 52:8071747a7cb3 427 if(RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 428 goto failure;
mazgch 21:c4d64830bf02 429 // set baud rate
mazgch 21:c4d64830bf02 430 sendFormated("AT+IPR=115200\r\n");
mazgch 52:8071747a7cb3 431 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 432 goto failure;
mazgch 112:89b5b21db71e 433 // wait some time until baudrate is applied
mazgch 112:89b5b21db71e 434 wait_ms(200); // SARA-G > 40ms
mazgch 21:c4d64830bf02 435 // identify the module
mazgch 21:c4d64830bf02 436 sendFormated("ATI\r\n");
mazgch 52:8071747a7cb3 437 if (RESP_OK != waitFinalResp(_cbATI, &_dev.dev))
mazgch 95:8282dbbe1492 438 goto failure;
mazgch 32:8f12ac182bbb 439 if (_dev.dev == DEV_UNKNOWN)
mazgch 95:8282dbbe1492 440 goto failure;
mazgch 32:8f12ac182bbb 441 // device specific init
mazgch 125:25a292afbac6 442 if (_dev.dev == DEV_LISA_C2) {
mazgch 32:8f12ac182bbb 443 // get the manufacturer
mazgch 32:8f12ac182bbb 444 sendFormated("AT+GMI\r\n");
mazgch 52:8071747a7cb3 445 if (RESP_OK != waitFinalResp(_cbString, _dev.manu))
mazgch 95:8282dbbe1492 446 goto failure;
mazgch 32:8f12ac182bbb 447 // get the model identification
mazgch 32:8f12ac182bbb 448 sendFormated("AT+GMM\r\n");
mazgch 52:8071747a7cb3 449 if (RESP_OK != waitFinalResp(_cbString, _dev.model))
mazgch 95:8282dbbe1492 450 goto failure;
mazgch 32:8f12ac182bbb 451 // get the sw version
mazgch 32:8f12ac182bbb 452 sendFormated("AT+GMR\r\n");
mazgch 52:8071747a7cb3 453 if (RESP_OK != waitFinalResp(_cbString, _dev.ver))
mazgch 95:8282dbbe1492 454 goto failure;
mazgch 95:8282dbbe1492 455 // get the pseudo ESN or MEID
mazgch 21:c4d64830bf02 456 sendFormated("AT+GSN\r\n");
mazgch 52:8071747a7cb3 457 if (RESP_OK != waitFinalResp(_cbString, _dev.meid))
mazgch 95:8282dbbe1492 458 goto failure;
mazgch 50:d76aece8038f 459 #if 0
mazgch 50:d76aece8038f 460 // enable power saving
mazgch 50:d76aece8038f 461 if (_dev.lpm != LPM_DISABLED) {
mazgch 50:d76aece8038f 462 // enable power saving (requires flow control, cts at least)
mazgch 50:d76aece8038f 463 sendFormated("AT+UPSV=1,1280\r\n");
mazgch 52:8071747a7cb3 464 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 465 goto failure;
mazgch 50:d76aece8038f 466 _dev.lpm = LPM_ACTIVE;
mazgch 50:d76aece8038f 467 }
mazgch 50:d76aece8038f 468 #endif
mazgch 26:07be5faf8925 469 } else {
mazgch 125:25a292afbac6 470 if ((_dev.dev == DEV_LISA_U2) || (_dev.dev == DEV_LEON_G2) ||
mazgch 125:25a292afbac6 471 (_dev.dev == DEV_TOBY_L2)) {
mazgch 35:9275215a3a5b 472 // enable the network identification feature
mazgch 21:c4d64830bf02 473 sendFormated("AT+UGPIOC=20,2\r\n");
mazgch 52:8071747a7cb3 474 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 475 goto failure;
mazgch 125:25a292afbac6 476 } else if ((_dev.dev == DEV_SARA_U2) || (_dev.dev == DEV_SARA_G35)) {
mazgch 35:9275215a3a5b 477 // enable the network identification feature
mazgch 21:c4d64830bf02 478 sendFormated("AT+UGPIOC=16,2\r\n");
mazgch 52:8071747a7cb3 479 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 480 goto failure;
mazgch 21:c4d64830bf02 481 }
mazgch 21:c4d64830bf02 482 // check the sim card
mazgch 31:a0bed6c1e05d 483 for (int i = 0; (i < 5) && (_dev.sim != SIM_READY); i++) {
mazgch 21:c4d64830bf02 484 sendFormated("AT+CPIN?\r\n");
mazgch 31:a0bed6c1e05d 485 int ret = waitFinalResp(_cbCPIN, &_dev.sim);
mazgch 62:a02f026bdd7c 486 // having an error here is ok (sim may still be initializing)
mazgch 52:8071747a7cb3 487 if ((RESP_OK != ret) && (RESP_ERROR != ret))
mazgch 95:8282dbbe1492 488 goto failure;
mazgch 31:a0bed6c1e05d 489 // Enter PIN if needed
mazgch 77:55788e619858 490 if (_dev.sim == SIM_PIN) {
mazgch 57:869bd35f44cc 491 if (!simpin) {
mazgch 75:ce6e12067d0c 492 ERROR("SIM PIN not available\r\n");
mazgch 95:8282dbbe1492 493 goto failure;
mazgch 31:a0bed6c1e05d 494 }
mazgch 130:3189949981ec 495 sendFormated("AT+CPIN=\"%s\"\r\n", simpin);
mazgch 52:8071747a7cb3 496 if (RESP_OK != waitFinalResp(_cbCPIN, &_dev.sim))
mazgch 95:8282dbbe1492 497 goto failure;
mazgch 62:a02f026bdd7c 498 } else if (_dev.sim != SIM_READY) {
mazgch 95:8282dbbe1492 499 wait_ms(1000);
mazgch 62:a02f026bdd7c 500 }
mazgch 21:c4d64830bf02 501 }
mazgch 77:55788e619858 502 if (_dev.sim != SIM_READY) {
mazgch 77:55788e619858 503 if (_dev.sim == SIM_MISSING)
mazgch 77:55788e619858 504 ERROR("SIM not inserted\r\n");
mazgch 95:8282dbbe1492 505 goto failure;
mazgch 77:55788e619858 506 }
mazgch 32:8f12ac182bbb 507 // get the manufacturer
mazgch 32:8f12ac182bbb 508 sendFormated("AT+CGMI\r\n");
mazgch 52:8071747a7cb3 509 if (RESP_OK != waitFinalResp(_cbString, _dev.manu))
mazgch 95:8282dbbe1492 510 goto failure;
mazgch 32:8f12ac182bbb 511 // get the model identification
mazgch 32:8f12ac182bbb 512 sendFormated("AT+CGMM\r\n");
mazgch 52:8071747a7cb3 513 if (RESP_OK != waitFinalResp(_cbString, _dev.model))
mazgch 95:8282dbbe1492 514 goto failure;
mazgch 126:bfbb9e19f6e0 515 // get the version
mazgch 126:bfbb9e19f6e0 516 sendFormated("ATI9\r\n");
mazgch 52:8071747a7cb3 517 if (RESP_OK != waitFinalResp(_cbString, _dev.ver))
mazgch 126:bfbb9e19f6e0 518 goto failure;
mazgch 21:c4d64830bf02 519 // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card.
mazgch 21:c4d64830bf02 520 // ICCID is a serial number identifying the SIM.
mazgch 21:c4d64830bf02 521 sendFormated("AT+CCID\r\n");
mazgch 52:8071747a7cb3 522 if (RESP_OK != waitFinalResp(_cbCCID, _dev.ccid))
mazgch 95:8282dbbe1492 523 goto failure;
mazgch 21:c4d64830bf02 524 // Returns the product serial number, IMEI (International Mobile Equipment Identity)
mazgch 21:c4d64830bf02 525 sendFormated("AT+CGSN\r\n");
mazgch 52:8071747a7cb3 526 if (RESP_OK != waitFinalResp(_cbString, _dev.imei))
mazgch 95:8282dbbe1492 527 goto failure;
mazgch 50:d76aece8038f 528 // enable power saving
mazgch 50:d76aece8038f 529 if (_dev.lpm != LPM_DISABLED) {
mazgch 50:d76aece8038f 530 // enable power saving (requires flow control, cts at least)
mazgch 50:d76aece8038f 531 sendFormated("AT+UPSV=1\r\n");
mazgch 52:8071747a7cb3 532 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 533 goto failure;
mazgch 50:d76aece8038f 534 _dev.lpm = LPM_ACTIVE;
mazgch 50:d76aece8038f 535 }
mazgch 79:291df065e345 536 // enable the psd registration unsolicited result code
mazgch 79:291df065e345 537 sendFormated("AT+CGREG=2\r\n");
mazgch 79:291df065e345 538 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 539 goto failure;
mazgch 21:c4d64830bf02 540 }
mazgch 79:291df065e345 541 // enable the network registration unsolicited result code
mazgch 125:25a292afbac6 542 sendFormated("AT+CREG=%d\r\n", (_dev.dev == DEV_LISA_C2) ? 1 : 2);
mazgch 79:291df065e345 543 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 544 goto failure;
mazgch 31:a0bed6c1e05d 545 // Setup SMS in text mode
mazgch 31:a0bed6c1e05d 546 sendFormated("AT+CMGF=1\r\n");
mazgch 52:8071747a7cb3 547 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 548 goto failure;
mazgch 31:a0bed6c1e05d 549 // setup new message indication
mazgch 75:ce6e12067d0c 550 sendFormated("AT+CNMI=2,1\r\n");
mazgch 52:8071747a7cb3 551 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 552 goto failure;
mazgch 21:c4d64830bf02 553 // Request IMSI (International Mobile Subscriber Identification)
mazgch 21:c4d64830bf02 554 sendFormated("AT+CIMI\r\n");
mazgch 52:8071747a7cb3 555 if (RESP_OK != waitFinalResp(_cbString, _dev.imsi))
mazgch 95:8282dbbe1492 556 goto failure;
mazgch 28:4d9509e3b1cf 557 if (status)
mazgch 31:a0bed6c1e05d 558 memcpy(status, &_dev, sizeof(DevStatus));
mazgch 95:8282dbbe1492 559 UNLOCK();
mazgch 31:a0bed6c1e05d 560 return true;
mazgch 95:8282dbbe1492 561 failure:
mazgch 95:8282dbbe1492 562 unlock();
mazgch 95:8282dbbe1492 563 return false;
mazgch 31:a0bed6c1e05d 564 }
mazgch 31:a0bed6c1e05d 565
mazgch 74:208e3e32d263 566 bool MDMParser::powerOff(void)
mazgch 74:208e3e32d263 567 {
mazgch 95:8282dbbe1492 568 bool ok = false;
mazgch 76:f7c3dd568dae 569 if (_init) {
mazgch 95:8282dbbe1492 570 LOCK();
mazgch 76:f7c3dd568dae 571 INFO("Modem::powerOff\r\n");
mazgch 76:f7c3dd568dae 572 sendFormated("AT+CPWROFF\r\n");
mazgch 95:8282dbbe1492 573 if (RESP_OK == waitFinalResp(NULL,NULL,120*1000)) {
mazgch 95:8282dbbe1492 574 _init = false;
mazgch 95:8282dbbe1492 575 ok = true;
mazgch 95:8282dbbe1492 576 }
mazgch 95:8282dbbe1492 577 UNLOCK();
mazgch 76:f7c3dd568dae 578 }
mazgch 95:8282dbbe1492 579 return ok;
mazgch 74:208e3e32d263 580 }
mazgch 74:208e3e32d263 581
mazgch 32:8f12ac182bbb 582 int MDMParser::_cbATI(int type, const char* buf, int len, Dev* dev)
mazgch 31:a0bed6c1e05d 583 {
mazgch 37:cc3433329d66 584 if ((type == TYPE_UNKNOWN) && dev) {
mazgch 125:25a292afbac6 585 if (strstr(buf, "SARA-G35")) *dev = DEV_SARA_G35;
msinig 133:57b208dd96fb 586 else if (strstr(buf, "LISA-U200-03S")) *dev = DEV_LISA_U2_03S;
msinig 133:57b208dd96fb 587 else if (strstr(buf, "LISA-U2")) *dev = DEV_LISA_U2;
mazgch 125:25a292afbac6 588 else if (strstr(buf, "LISA-C2")) *dev = DEV_LISA_C2;
mazgch 125:25a292afbac6 589 else if (strstr(buf, "SARA-U2")) *dev = DEV_SARA_U2;
mazgch 125:25a292afbac6 590 else if (strstr(buf, "LEON-G2")) *dev = DEV_LEON_G2;
mazgch 125:25a292afbac6 591 else if (strstr(buf, "TOBY-L2")) *dev = DEV_TOBY_L2;
mazgch 125:25a292afbac6 592 else if (strstr(buf, "MPCI-L2")) *dev = DEV_MPCI_L2;
mazgch 28:4d9509e3b1cf 593 }
mazgch 31:a0bed6c1e05d 594 return WAIT;
mazgch 31:a0bed6c1e05d 595 }
mazgch 31:a0bed6c1e05d 596
mazgch 31:a0bed6c1e05d 597 int MDMParser::_cbCPIN(int type, const char* buf, int len, Sim* sim)
mazgch 31:a0bed6c1e05d 598 {
mazgch 75:ce6e12067d0c 599 if (sim) {
mazgch 75:ce6e12067d0c 600 if (type == TYPE_PLUS){
mazgch 75:ce6e12067d0c 601 char s[16];
mazgch 75:ce6e12067d0c 602 if (sscanf(buf, "\r\n+CPIN: %[^\r]\r\n", s) >= 1)
mazgch 75:ce6e12067d0c 603 *sim = (0 == strcmp("READY", s)) ? SIM_READY : SIM_PIN;
mazgch 75:ce6e12067d0c 604 } else if (type == TYPE_ERROR) {
mazgch 75:ce6e12067d0c 605 if (strstr(buf, "+CME ERROR: SIM not inserted"))
mazgch 75:ce6e12067d0c 606 *sim = SIM_MISSING;
mazgch 31:a0bed6c1e05d 607 }
mazgch 31:a0bed6c1e05d 608 }
mazgch 31:a0bed6c1e05d 609 return WAIT;
mazgch 21:c4d64830bf02 610 }
mazgch 21:c4d64830bf02 611
mazgch 26:07be5faf8925 612 int MDMParser::_cbCCID(int type, const char* buf, int len, char* ccid)
mazgch 26:07be5faf8925 613 {
mazgch 26:07be5faf8925 614 if ((type == TYPE_PLUS) && ccid){
mazgch 26:07be5faf8925 615 if (sscanf(buf, "\r\n+CCID: %[^\r]\r\n", ccid) == 1)
mazgch 31:a0bed6c1e05d 616 /*TRACE("Got CCID: %s\r\n", ccid)*/;
mazgch 26:07be5faf8925 617 }
mazgch 26:07be5faf8925 618 return WAIT;
mazgch 26:07be5faf8925 619 }
mazgch 26:07be5faf8925 620
mazgch 75:ce6e12067d0c 621 bool MDMParser::registerNet(NetStatus* status /*= NULL*/, int timeout_ms /*= 180000*/)
mazgch 75:ce6e12067d0c 622 {
mazgch 75:ce6e12067d0c 623 Timer timer;
mazgch 75:ce6e12067d0c 624 timer.start();
mazgch 75:ce6e12067d0c 625 INFO("Modem::register\r\n");
mazgch 75:ce6e12067d0c 626 while (!checkNetStatus(status) && !TIMEOUT(timer, timeout_ms))
mazgch 95:8282dbbe1492 627 wait_ms(1000);
mazgch 79:291df065e345 628 if (_net.csd == REG_DENIED) ERROR("CSD Registration Denied\r\n");
mazgch 79:291df065e345 629 if (_net.psd == REG_DENIED) ERROR("PSD Registration Denied\r\n");
mazgch 123:66cef6353b13 630 if (_net.eps == REG_DENIED) ERROR("EPS Registration Denied\r\n");
mazgch 123:66cef6353b13 631 return REG_OK(_net.csd) || REG_OK(_net.psd) || REG_OK(_net.eps);
mazgch 75:ce6e12067d0c 632 }
mazgch 75:ce6e12067d0c 633
mazgch 28:4d9509e3b1cf 634 bool MDMParser::checkNetStatus(NetStatus* status /*= NULL*/)
mazgch 21:c4d64830bf02 635 {
mazgch 95:8282dbbe1492 636 bool ok = false;
mazgch 95:8282dbbe1492 637 LOCK();
mazgch 75:ce6e12067d0c 638 memset(&_net, 0, sizeof(_net));
mazgch 75:ce6e12067d0c 639 _net.lac = 0xFFFF;
mazgch 75:ce6e12067d0c 640 _net.ci = 0xFFFFFFFF;
mazgch 21:c4d64830bf02 641 // check registration
mazgch 21:c4d64830bf02 642 sendFormated("AT+CREG?\r\n");
mazgch 88:135fb4bb7aac 643 waitFinalResp(); // don't fail as service could be not subscribed
mazgch 125:25a292afbac6 644 if (_dev.dev != DEV_LISA_C2) {
mazgch 79:291df065e345 645 // check PSD registration
mazgch 79:291df065e345 646 sendFormated("AT+CGREG?\r\n");
mazgch 88:135fb4bb7aac 647 waitFinalResp(); // don't fail as service could be not subscribed
mazgch 125:25a292afbac6 648 if ((_dev.dev == DEV_TOBY_L2) || (_dev.dev == DEV_MPCI_L2)) {
mazgch 125:25a292afbac6 649 // check EPS network registration
mazgch 125:25a292afbac6 650 sendFormated("AT+CEREG?\r\n");
mazgch 125:25a292afbac6 651 waitFinalResp(); // don't fail as service could be not subscribed
mazgch 125:25a292afbac6 652 }
mazgch 79:291df065e345 653 }
mazgch 123:66cef6353b13 654 if (REG_OK(_net.csd) || REG_OK(_net.psd) || REG_OK(_net.eps))
mazgch 75:ce6e12067d0c 655 {
mazgch 75:ce6e12067d0c 656 // check modem specific status messages
mazgch 125:25a292afbac6 657 if (_dev.dev == DEV_LISA_C2) {
mazgch 75:ce6e12067d0c 658 sendFormated("AT+CSS?\r\n");
mazgch 75:ce6e12067d0c 659 if (RESP_OK != waitFinalResp())
mazgch 95:8282dbbe1492 660 goto failure;
mazgch 81:3966a5c17037 661 while (1) {
mazgch 81:3966a5c17037 662 // get the Telephone number
mazgch 81:3966a5c17037 663 sendFormated("AT$MDN?\r\n");
mazgch 81:3966a5c17037 664 if (RESP_OK != waitFinalResp(_cbString, _net.num))
mazgch 95:8282dbbe1492 665 goto failure;
mazgch 81:3966a5c17037 666 // check if we have a Mobile Directory Number
mazgch 81:3966a5c17037 667 if (*_net.num && (memcmp(_net.num, "000000", 6) != 0))
mazgch 81:3966a5c17037 668 break;
mazgch 81:3966a5c17037 669
mazgch 81:3966a5c17037 670 INFO("Device not yet activated\r\n");
mazgch 81:3966a5c17037 671 INFO("Make sure you have a valid contract with the network operator for this device.\r\n");
mazgch 81:3966a5c17037 672 // Check if the the version contains a V for Verizon
mazgch 81:3966a5c17037 673 // Verizon: E0.V.xx.00.xxR,
mazgch 81:3966a5c17037 674 // Sprint E0.S.xx.00.xxR
mazgch 81:3966a5c17037 675 if (_dev.ver[3] == 'V') {
mazgch 81:3966a5c17037 676 int i;
mazgch 81:3966a5c17037 677 INFO("Start device over-the-air activation (this can take a few minutes)\r\n");
mazgch 81:3966a5c17037 678 sendFormated("AT+CDV=*22899\r\n");
mazgch 81:3966a5c17037 679 i = 1;
mazgch 82:055dcfcf9dcc 680 if ((RESP_OK != waitFinalResp(_cbUACTIND, &i, 120*1000)) || (i == 1)) {
mazgch 81:3966a5c17037 681 ERROR("Device over-the-air activation failed\r\n");
mazgch 95:8282dbbe1492 682 goto failure;
mazgch 81:3966a5c17037 683 }
mazgch 81:3966a5c17037 684 INFO("Device over-the-air activation successful\r\n");
mazgch 81:3966a5c17037 685
mazgch 81:3966a5c17037 686 INFO("Start PRL over-the-air update (this can take a few minutes)\r\n");
mazgch 81:3966a5c17037 687 sendFormated("AT+CDV=*22891\r\n");
mazgch 81:3966a5c17037 688 i = 1;
mazgch 82:055dcfcf9dcc 689 if ((RESP_OK != waitFinalResp(_cbUACTIND, &i, 120*1000)) || (i == 1)) {
mazgch 81:3966a5c17037 690 ERROR("PRL over-the-air update failed\r\n");
mazgch 95:8282dbbe1492 691 goto failure;
mazgch 81:3966a5c17037 692 }
mazgch 81:3966a5c17037 693 INFO("PRL over-the-air update successful\r\n");
mazgch 81:3966a5c17037 694
mazgch 81:3966a5c17037 695 } else {
mazgch 81:3966a5c17037 696 // Sprint or Aeris
mazgch 81:3966a5c17037 697 INFO("Wait for OMA-DM over-the-air activation (this can take a few minutes)\r\n");
mazgch 95:8282dbbe1492 698 wait_ms(120*1000);
mazgch 81:3966a5c17037 699 }
mazgch 81:3966a5c17037 700 }
mazgch 75:ce6e12067d0c 701 // get the the Network access identifier string
mazgch 75:ce6e12067d0c 702 char nai[64];
mazgch 75:ce6e12067d0c 703 sendFormated("AT$QCMIPNAI?\r\n");
mazgch 75:ce6e12067d0c 704 if (RESP_OK != waitFinalResp(_cbString, nai))
mazgch 95:8282dbbe1492 705 goto failure;
mazgch 75:ce6e12067d0c 706 } else {
mazgch 75:ce6e12067d0c 707 sendFormated("AT+COPS?\r\n");
mazgch 75:ce6e12067d0c 708 if (RESP_OK != waitFinalResp(_cbCOPS, &_net))
mazgch 95:8282dbbe1492 709 goto failure;
mazgch 95:8282dbbe1492 710 // get the MSISDNs related to this subscriber
mazgch 75:ce6e12067d0c 711 sendFormated("AT+CNUM\r\n");
mazgch 75:ce6e12067d0c 712 if (RESP_OK != waitFinalResp(_cbCNUM, _net.num))
mazgch 95:8282dbbe1492 713 goto failure;
mazgch 75:ce6e12067d0c 714 }
mazgch 95:8282dbbe1492 715 // get the signal strength indication
mazgch 75:ce6e12067d0c 716 sendFormated("AT+CSQ\r\n");
mazgch 75:ce6e12067d0c 717 if (RESP_OK != waitFinalResp(_cbCSQ, &_net))
mazgch 95:8282dbbe1492 718 goto failure;
mazgch 75:ce6e12067d0c 719 }
mazgch 28:4d9509e3b1cf 720 if (status) {
mazgch 31:a0bed6c1e05d 721 memcpy(status, &_net, sizeof(NetStatus));
mazgch 25:4045d02e44f1 722 }
mazgch 123:66cef6353b13 723 ok = REG_DONE(_net.csd) &&
mazgch 123:66cef6353b13 724 (REG_DONE(_net.psd) || REG_DONE(_net.eps));
mazgch 95:8282dbbe1492 725 UNLOCK();
mazgch 95:8282dbbe1492 726 return ok;
mazgch 95:8282dbbe1492 727 failure:
mazgch 95:8282dbbe1492 728 unlock();
mazgch 95:8282dbbe1492 729 return false;
mazgch 31:a0bed6c1e05d 730 }
mazgch 31:a0bed6c1e05d 731
mazgch 31:a0bed6c1e05d 732 int MDMParser::_cbCOPS(int type, const char* buf, int len, NetStatus* status)
mazgch 31:a0bed6c1e05d 733 {
mazgch 31:a0bed6c1e05d 734 if ((type == TYPE_PLUS) && status){
mazgch 31:a0bed6c1e05d 735 int act = 99;
msinig 134:2fbd5723e063 736 int mode = 99;
mazgch 31:a0bed6c1e05d 737 // +COPS: <mode>[,<format>,<oper>[,<AcT>]]
msinig 134:2fbd5723e063 738 if (sscanf(buf, "\r\n+COPS: %d,%*d,\"%[^\"]\",%d",&mode,status->opr,&act) >= 1) {
mazgch 31:a0bed6c1e05d 739 if (act == 0) status->act = ACT_GSM; // 0: GSM,
mazgch 31:a0bed6c1e05d 740 else if (act == 2) status->act = ACT_UTRAN; // 2: UTRAN
mazgch 123:66cef6353b13 741 else if (act == 7) status->act = ACT_LTE; // 2: UTRAN
msinig 134:2fbd5723e063 742 if (mode == 0) status->regStatus = COPS_AUTOMATIC_REG;
msinig 134:2fbd5723e063 743 else if (mode == 1) status->regStatus = COPS_MANUAL_REG;
msinig 134:2fbd5723e063 744 else if (mode == 2) status->regStatus = COPS_DISABLED_REG;
mazgch 31:a0bed6c1e05d 745 }
mazgch 31:a0bed6c1e05d 746 }
mazgch 31:a0bed6c1e05d 747 return WAIT;
mazgch 31:a0bed6c1e05d 748 }
mazgch 31:a0bed6c1e05d 749
mazgch 31:a0bed6c1e05d 750 int MDMParser::_cbCNUM(int type, const char* buf, int len, char* num)
mazgch 31:a0bed6c1e05d 751 {
mazgch 31:a0bed6c1e05d 752 if ((type == TYPE_PLUS) && num){
mazgch 31:a0bed6c1e05d 753 int a;
mazgch 31:a0bed6c1e05d 754 if ((sscanf(buf, "\r\n+CNUM: \"My Number\",\"%31[^\"]\",%d", num, &a) == 2) &&
mazgch 31:a0bed6c1e05d 755 ((a == 129) || (a == 145))) {
mazgch 31:a0bed6c1e05d 756 }
mazgch 31:a0bed6c1e05d 757 }
mazgch 31:a0bed6c1e05d 758 return WAIT;
mazgch 31:a0bed6c1e05d 759 }
mazgch 31:a0bed6c1e05d 760
mazgch 54:7ba8e4c218e2 761 int MDMParser::_cbCSQ(int type, const char* buf, int len, NetStatus* status)
mazgch 31:a0bed6c1e05d 762 {
mazgch 54:7ba8e4c218e2 763 if ((type == TYPE_PLUS) && status){
mazgch 54:7ba8e4c218e2 764 int a,b;
mazgch 54:7ba8e4c218e2 765 char _ber[] = { 49, 43, 37, 25, 19, 13, 7, 0 }; // see 3GPP TS 45.008 [20] subclause 8.2.4
mazgch 31:a0bed6c1e05d 766 // +CSQ: <rssi>,<qual>
mazgch 54:7ba8e4c218e2 767 if (sscanf(buf, "\r\n+CSQ: %d,%d",&a,&b) == 2) {
mazgch 121:8da935c2c08c 768 if (a != 99) status->rssi = -113 + 2*a; // 0: -113 1: -111 ... 30: -53 dBm with 2 dBm steps, 31: >-51 dBm
mazgch 54:7ba8e4c218e2 769 if ((b != 99) && (b < sizeof(_ber))) status->ber = _ber[b]; //
mazgch 31:a0bed6c1e05d 770 }
mazgch 31:a0bed6c1e05d 771 }
mazgch 31:a0bed6c1e05d 772 return WAIT;
mazgch 31:a0bed6c1e05d 773 }
mazgch 21:c4d64830bf02 774
mazgch 124:65eb7d58f2da 775
mazgch 124:65eb7d58f2da 776 int MDMParser::_cbUACTIND(int type, const char* buf, int len, int* i)
mazgch 124:65eb7d58f2da 777 {
mazgch 124:65eb7d58f2da 778 if ((type == TYPE_PLUS) && i){
mazgch 124:65eb7d58f2da 779 int a;
mazgch 124:65eb7d58f2da 780 if (sscanf(buf, "\r\n+UACTIND: %d", &a) == 1) {
mazgch 124:65eb7d58f2da 781 *i = a;
mazgch 124:65eb7d58f2da 782 }
mazgch 124:65eb7d58f2da 783 }
mazgch 124:65eb7d58f2da 784 return WAIT;
mazgch 124:65eb7d58f2da 785 }
mazgch 124:65eb7d58f2da 786
mazgch 21:c4d64830bf02 787 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 788 // internet connection
mazgch 21:c4d64830bf02 789
mazgch 128:0415646a9934 790 bool MDMParser::_activateProfile(const char* apn, const char* username, const char* password, Auth auth)
mazgch 128:0415646a9934 791 {
mazgch 128:0415646a9934 792 // Set up the APN
mazgch 128:0415646a9934 793 if (*apn) {
mazgch 128:0415646a9934 794 sendFormated("AT+UPSD=" PROFILE ",1,\"%s\"\r\n", apn);
mazgch 128:0415646a9934 795 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 796 return false;
mazgch 128:0415646a9934 797 }
mazgch 128:0415646a9934 798 if (*username) {
mazgch 128:0415646a9934 799 sendFormated("AT+UPSD=" PROFILE ",2,\"%s\"\r\n", username);
mazgch 128:0415646a9934 800 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 801 return false;
mazgch 128:0415646a9934 802 }
mazgch 128:0415646a9934 803 if (*password) {
mazgch 128:0415646a9934 804 sendFormated("AT+UPSD=" PROFILE ",3,\"%s\"\r\n", password);
mazgch 128:0415646a9934 805 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 806 return false;
mazgch 128:0415646a9934 807 }
mazgch 128:0415646a9934 808 // Set up the dynamic IP address assignment.
mazgch 128:0415646a9934 809 sendFormated("AT+UPSD=" PROFILE ",7,\"0.0.0.0\"\r\n");
mazgch 128:0415646a9934 810 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 811 return false;
mazgch 128:0415646a9934 812 // try different Authentication Protocols
mazgch 128:0415646a9934 813 // 0 = none
mazgch 128:0415646a9934 814 // 1 = PAP (Password Authentication Protocol)
mazgch 128:0415646a9934 815 // 2 = CHAP (Challenge Handshake Authentication Protocol)
mazgch 128:0415646a9934 816 for (int i = AUTH_NONE; i <= AUTH_CHAP; i ++) {
mazgch 128:0415646a9934 817 if ((auth == AUTH_DETECT) || (auth == i)) {
mazgch 128:0415646a9934 818 // Set up the Authentication Protocol
mazgch 128:0415646a9934 819 sendFormated("AT+UPSD=" PROFILE ",6,%d\r\n",i);
mazgch 128:0415646a9934 820 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 821 return false;
mazgch 128:0415646a9934 822 // Activate the profile and make connection
mazgch 128:0415646a9934 823 sendFormated("AT+UPSDA=" PROFILE ",3\r\n");
mazgch 128:0415646a9934 824 if (RESP_OK == waitFinalResp(NULL,NULL,150*1000))
mazgch 128:0415646a9934 825 return true;
mazgch 128:0415646a9934 826 }
mazgch 128:0415646a9934 827 }
mazgch 128:0415646a9934 828 return false;
mazgch 128:0415646a9934 829 }
mazgch 128:0415646a9934 830
mazgch 131:965a7cbc1e58 831 bool MDMParser::_activateProfileReuseExternal(void)
mazgch 131:965a7cbc1e58 832 {
mazgch 131:965a7cbc1e58 833 int cid = -1;
mazgch 131:965a7cbc1e58 834 sendFormated("AT+CGDCONT?\r\n");
mazgch 131:965a7cbc1e58 835 if (RESP_OK != waitFinalResp(_cbCGDCONT, &cid))
mazgch 131:965a7cbc1e58 836 return false;
mazgch 131:965a7cbc1e58 837 if (cid == -1)
mazgch 131:965a7cbc1e58 838 return false;
mazgch 131:965a7cbc1e58 839 // we found a context that provides us a valid IP so lets reuse it for the internal IP stack
mazgch 131:965a7cbc1e58 840 sendFormated("AT+UPSD=" PROFILE ",100,%d\r\n", cid);
mazgch 131:965a7cbc1e58 841 if (RESP_OK != waitFinalResp())
mazgch 131:965a7cbc1e58 842 return false;
mazgch 131:965a7cbc1e58 843 // Activate the profile and make connection
mazgch 131:965a7cbc1e58 844 sendFormated("AT+UPSDA=" PROFILE ",3\r\n");
mazgch 131:965a7cbc1e58 845 return (RESP_OK == waitFinalResp(NULL,NULL,150*1000));
mazgch 131:965a7cbc1e58 846 }
mazgch 131:965a7cbc1e58 847
mazgch 128:0415646a9934 848 bool MDMParser::_activateProfileByCid(int cid, const char* apn, const char* username, const char* password, Auth auth)
mazgch 128:0415646a9934 849 {
mazgch 128:0415646a9934 850 sendFormated("AT+CGDCONT=%d,\"IP\",\"%s\"\r\n", cid, apn);
mazgch 128:0415646a9934 851 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 852 return false;
mazgch 128:0415646a9934 853 sendFormated("AT+UAUTHREQ=%d,%d,\"%s\",\"%s\"\r\n", cid, auth, username, password);
mazgch 128:0415646a9934 854 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 855 return false;
mazgch 128:0415646a9934 856 sendFormated("AT+UPSD=" PROFILE ",100,%d\r\n", cid);
mazgch 128:0415646a9934 857 if (RESP_OK != waitFinalResp())
mazgch 128:0415646a9934 858 return false;
mazgch 128:0415646a9934 859 // Activate the profile and make connection
mazgch 128:0415646a9934 860 sendFormated("AT+UPSDA=" PROFILE ",3\r\n");
mazgch 128:0415646a9934 861 return (RESP_OK == waitFinalResp(NULL,NULL,150*1000));
mazgch 128:0415646a9934 862 }
mazgch 128:0415646a9934 863
mazgch 131:965a7cbc1e58 864 int MDMParser::_cbCGDCONT(int type, const char* buf, int len, int* cid)
mazgch 131:965a7cbc1e58 865 {
mazgch 131:965a7cbc1e58 866 // accept with and without leading \r\n in +CGDCONT:
mazgch 131:965a7cbc1e58 867 if ((type == TYPE_PLUS) && (buf[0] == '\r') && (buf[1] == '\n') && (len >= 2))
mazgch 131:965a7cbc1e58 868 buf += 2, len -= 2, type = TYPE_UNKNOWN;
mazgch 131:965a7cbc1e58 869 if (type == TYPE_UNKNOWN) {
mazgch 131:965a7cbc1e58 870 int a,b,c,d,t;
mazgch 131:965a7cbc1e58 871 //+CGDCONT: <cid>,"IP","<apn name>","<ip adr>",0,0,0,0,0,0
mazgch 131:965a7cbc1e58 872 if (sscanf(buf, "+CGDCONT: %d,\"IP\",\"%*[^\"]\",\"" IPSTR "\",%*d,%*d,%*d,%*d,%*d,%*d", &t, &a,&b,&c,&d) == 5) {
mazgch 131:965a7cbc1e58 873 if (IPADR(a,b,c,d) != NOIP)
mazgch 131:965a7cbc1e58 874 *cid = t;
mazgch 131:965a7cbc1e58 875 }
mazgch 131:965a7cbc1e58 876 }
mazgch 131:965a7cbc1e58 877 return WAIT;
mazgch 131:965a7cbc1e58 878 }
mazgch 131:965a7cbc1e58 879
mazgch 79:291df065e345 880 MDMParser::IP MDMParser::join(const char* apn /*= NULL*/, const char* username /*= NULL*/,
mazgch 79:291df065e345 881 const char* password /*= NULL*/, Auth auth /*= AUTH_DETECT*/)
mazgch 21:c4d64830bf02 882 {
mazgch 95:8282dbbe1492 883 LOCK();
mazgch 78:caf0014375cb 884 INFO("Modem::join\r\n");
mazgch 32:8f12ac182bbb 885 _ip = NOIP;
mazgch 125:25a292afbac6 886 if (_dev.dev == DEV_LISA_C2) {
mazgch 76:f7c3dd568dae 887 // make a dumy dns lookup (which will fail, so ignore the result)
mazgch 76:f7c3dd568dae 888 sendFormated("AT+UDNSRN=0,\"u-blox.com\"\r\n");
mazgch 76:f7c3dd568dae 889 waitFinalResp();
mazgch 76:f7c3dd568dae 890 // This fake lookup will enable the IP connection and we
mazgch 76:f7c3dd568dae 891 // should have an IP after this, so we check it
mazgch 76:f7c3dd568dae 892
mazgch 21:c4d64830bf02 893 //Get local IP address
mazgch 21:c4d64830bf02 894 sendFormated("AT+CMIP?\r\n");
mazgch 52:8071747a7cb3 895 if (RESP_OK != waitFinalResp(_cbCMIP, &_ip))
mazgch 95:8282dbbe1492 896 goto failure;
mazgch 125:25a292afbac6 897
mazgch 21:c4d64830bf02 898 } else {
mazgch 21:c4d64830bf02 899 // check gprs attach status
mazgch 72:d1e943ad6558 900 sendFormated("AT+CGATT=1\r\n");
mazgch 74:208e3e32d263 901 if (RESP_OK != waitFinalResp(NULL,NULL,3*60*1000))
mazgch 95:8282dbbe1492 902 goto failure;
mazgch 31:a0bed6c1e05d 903 // Check the profile
mazgch 31:a0bed6c1e05d 904 int a = 0;
mazgch 79:291df065e345 905 bool force = true;
mazgch 31:a0bed6c1e05d 906 sendFormated("AT+UPSND=" PROFILE ",8\r\n");
mazgch 52:8071747a7cb3 907 if (RESP_OK != waitFinalResp(_cbUPSND, &a))
mazgch 95:8282dbbe1492 908 goto failure;
mazgch 79:291df065e345 909 if (a == 1 && force) {
mazgch 31:a0bed6c1e05d 910 // disconnect the profile already if it is connected
mazgch 31:a0bed6c1e05d 911 sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
mazgch 58:e38a2e942fbb 912 if (RESP_OK != waitFinalResp(NULL,NULL,40*1000))
mazgch 95:8282dbbe1492 913 goto failure;
mazgch 79:291df065e345 914 a = 0;
mazgch 31:a0bed6c1e05d 915 }
mazgch 79:291df065e345 916 if (a == 0) {
mazgch 84:a05edb010176 917 bool ok = false;
mazgch 84:a05edb010176 918 // try to lookup the apn settings from our local database by mccmnc
mazgch 84:a05edb010176 919 const char* config = NULL;
mazgch 88:135fb4bb7aac 920 if (!apn && !username && !password)
mazgch 88:135fb4bb7aac 921 config = apnconfig(_dev.imsi);
mazgch 84:a05edb010176 922 do {
mazgch 84:a05edb010176 923 if (config) {
mazgch 85:dd8f4f0d0ca9 924 apn = _APN_GET(config);
mazgch 85:dd8f4f0d0ca9 925 username = _APN_GET(config);
mazgch 85:dd8f4f0d0ca9 926 password = _APN_GET(config);
mazgch 79:291df065e345 927 }
mazgch 125:25a292afbac6 928 // convert pointer to empty strings
mazgch 125:25a292afbac6 929 apn = apn ? apn : "";
mazgch 125:25a292afbac6 930 username = username ? username : "";
mazgch 125:25a292afbac6 931 password = password ? password : "";
mazgch 125:25a292afbac6 932 auth = (*username && *password) ? auth : AUTH_NONE;
mazgch 125:25a292afbac6 933 TRACE("Testing APN Settings(\"%s\",\"%s\",\"%s\",%d)\r\n", apn, username, password, auth);
mazgch 128:0415646a9934 934 if ((_dev.dev != DEV_TOBY_L2) && (_dev.dev != DEV_MPCI_L2))
mazgch 128:0415646a9934 935 ok = _activateProfile(apn, username, password, auth);
mazgch 131:965a7cbc1e58 936 else {
mazgch 131:965a7cbc1e58 937 ok = _activateProfileReuseExternal();
mazgch 131:965a7cbc1e58 938 if (ok)
mazgch 131:965a7cbc1e58 939 TRACE("Reusing External Context\r\n");
mazgch 131:965a7cbc1e58 940 else
mazgch 131:965a7cbc1e58 941 ok = _activateProfileByCid(1, apn, username, password, auth);
mazgch 131:965a7cbc1e58 942 }
mazgch 99:3116d3e900ed 943 } while (!ok && config && *config); // maybe use next setting ?
mazgch 84:a05edb010176 944 if (!ok) {
mazgch 79:291df065e345 945 ERROR("Your modem APN/password/username may be wrong\r\n");
mazgch 95:8282dbbe1492 946 goto failure;
mazgch 79:291df065e345 947 }
mazgch 74:208e3e32d263 948 }
mazgch 21:c4d64830bf02 949 //Get local IP address
mazgch 21:c4d64830bf02 950 sendFormated("AT+UPSND=" PROFILE ",0\r\n");
mazgch 52:8071747a7cb3 951 if (RESP_OK != waitFinalResp(_cbUPSND, &_ip))
mazgch 95:8282dbbe1492 952 goto failure;
mazgch 31:a0bed6c1e05d 953 }
mazgch 95:8282dbbe1492 954 UNLOCK();
mazgch 32:8f12ac182bbb 955 return _ip;
mazgch 95:8282dbbe1492 956 failure:
mazgch 95:8282dbbe1492 957 unlock();
mazgch 95:8282dbbe1492 958 return NOIP;
mazgch 31:a0bed6c1e05d 959 }
mazgch 31:a0bed6c1e05d 960
mazgch 84:a05edb010176 961 int MDMParser::_cbUDOPN(int type, const char* buf, int len, char* mccmnc)
mazgch 84:a05edb010176 962 {
mazgch 84:a05edb010176 963 if ((type == TYPE_PLUS) && mccmnc) {
mazgch 84:a05edb010176 964 if (sscanf(buf, "\r\n+UDOPN: 0,\"%[^\"]\"", mccmnc) == 1)
mazgch 84:a05edb010176 965 ;
mazgch 84:a05edb010176 966 }
mazgch 84:a05edb010176 967 return WAIT;
mazgch 84:a05edb010176 968 }
mazgch 84:a05edb010176 969
mazgch 32:8f12ac182bbb 970 int MDMParser::_cbCMIP(int type, const char* buf, int len, IP* ip)
mazgch 32:8f12ac182bbb 971 {
mazgch 102:fb982ff9da82 972 if ((type == TYPE_UNKNOWN) && ip) {
mazgch 32:8f12ac182bbb 973 int a,b,c,d;
mazgch 102:fb982ff9da82 974 if (sscanf(buf, "\r\n" IPSTR, &a,&b,&c,&d) == 4)
mazgch 32:8f12ac182bbb 975 *ip = IPADR(a,b,c,d);
mazgch 32:8f12ac182bbb 976 }
mazgch 32:8f12ac182bbb 977 return WAIT;
mazgch 32:8f12ac182bbb 978 }
mazgch 32:8f12ac182bbb 979
mazgch 31:a0bed6c1e05d 980 int MDMParser::_cbUPSND(int type, const char* buf, int len, int* act)
mazgch 31:a0bed6c1e05d 981 {
mazgch 31:a0bed6c1e05d 982 if ((type == TYPE_PLUS) && act) {
mazgch 31:a0bed6c1e05d 983 if (sscanf(buf, "\r\n+UPSND: %*d,%*d,%d", act) == 1)
mazgch 31:a0bed6c1e05d 984 /*nothing*/;
mazgch 21:c4d64830bf02 985 }
mazgch 31:a0bed6c1e05d 986 return WAIT;
mazgch 31:a0bed6c1e05d 987 }
mazgch 31:a0bed6c1e05d 988
mazgch 31:a0bed6c1e05d 989 int MDMParser::_cbUPSND(int type, const char* buf, int len, IP* ip)
mazgch 31:a0bed6c1e05d 990 {
mazgch 31:a0bed6c1e05d 991 if ((type == TYPE_PLUS) && ip) {
mazgch 31:a0bed6c1e05d 992 int a,b,c,d;
mazgch 31:a0bed6c1e05d 993 // +UPSND=<profile_id>,<param_tag>[,<dynamic_param_val>]
mazgch 31:a0bed6c1e05d 994 if (sscanf(buf, "\r\n+UPSND: " PROFILE ",0,\"" IPSTR "\"", &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 995 *ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 996 }
mazgch 31:a0bed6c1e05d 997 return WAIT;
mazgch 31:a0bed6c1e05d 998 }
mazgch 31:a0bed6c1e05d 999
mazgch 31:a0bed6c1e05d 1000 int MDMParser::_cbUDNSRN(int type, const char* buf, int len, IP* ip)
mazgch 31:a0bed6c1e05d 1001 {
mazgch 31:a0bed6c1e05d 1002 if ((type == TYPE_PLUS) && ip) {
mazgch 31:a0bed6c1e05d 1003 int a,b,c,d;
mazgch 95:8282dbbe1492 1004 if (sscanf(buf, "\r\n+UDNSRN: \"" IPSTR "\"", &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 1005 *ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 1006 }
mazgch 31:a0bed6c1e05d 1007 return WAIT;
mazgch 21:c4d64830bf02 1008 }
mazgch 21:c4d64830bf02 1009
mazgch 21:c4d64830bf02 1010 bool MDMParser::disconnect(void)
mazgch 21:c4d64830bf02 1011 {
mazgch 95:8282dbbe1492 1012 bool ok = false;
mazgch 95:8282dbbe1492 1013 LOCK();
mazgch 74:208e3e32d263 1014 INFO("Modem::disconnect\r\n");
mazgch 95:8282dbbe1492 1015 if (_ip != NOIP) {
mazgch 125:25a292afbac6 1016 if (_dev.dev == DEV_LISA_C2) {
mazgch 95:8282dbbe1492 1017 // There something to do here
mazgch 95:8282dbbe1492 1018 _ip = NOIP;
mazgch 95:8282dbbe1492 1019 ok = true;
mazgch 95:8282dbbe1492 1020 } else {
mazgch 95:8282dbbe1492 1021 sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
mazgch 95:8282dbbe1492 1022 if (RESP_OK != waitFinalResp()) {
mazgch 95:8282dbbe1492 1023 _ip = NOIP;
mazgch 95:8282dbbe1492 1024 ok = true;
mazgch 95:8282dbbe1492 1025 }
mazgch 95:8282dbbe1492 1026 }
mazgch 21:c4d64830bf02 1027 }
mazgch 95:8282dbbe1492 1028 UNLOCK();
mazgch 95:8282dbbe1492 1029 return ok;
mazgch 21:c4d64830bf02 1030 }
mazgch 21:c4d64830bf02 1031
mazgch 31:a0bed6c1e05d 1032 MDMParser::IP MDMParser::gethostbyname(const char* host)
mazgch 21:c4d64830bf02 1033 {
mazgch 31:a0bed6c1e05d 1034 IP ip = NOIP;
mazgch 31:a0bed6c1e05d 1035 int a,b,c,d;
mazgch 31:a0bed6c1e05d 1036 if (sscanf(host, IPSTR, &a,&b,&c,&d) == 4)
mazgch 31:a0bed6c1e05d 1037 ip = IPADR(a,b,c,d);
mazgch 31:a0bed6c1e05d 1038 else {
mazgch 95:8282dbbe1492 1039 LOCK();
mazgch 31:a0bed6c1e05d 1040 sendFormated("AT+UDNSRN=0,\"%s\"\r\n", host);
mazgch 52:8071747a7cb3 1041 if (RESP_OK != waitFinalResp(_cbUDNSRN, &ip))
mazgch 95:8282dbbe1492 1042 ip = NOIP;
mazgch 95:8282dbbe1492 1043 UNLOCK();
mazgch 21:c4d64830bf02 1044 }
mazgch 31:a0bed6c1e05d 1045 return ip;
mazgch 21:c4d64830bf02 1046 }
mazgch 21:c4d64830bf02 1047
mazgch 21:c4d64830bf02 1048 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 1049 // sockets
mazgch 21:c4d64830bf02 1050
mazgch 103:197fa7920ad8 1051 int MDMParser::_cbUSOCR(int type, const char* buf, int len, int* handle)
mazgch 21:c4d64830bf02 1052 {
mazgch 103:197fa7920ad8 1053 if ((type == TYPE_PLUS) && handle) {
mazgch 108:254bf037f83f 1054 // +USOCR: socket
mazgch 108:254bf037f83f 1055 if (sscanf(buf, "\r\n+USOCR: %d", handle) == 1)
mazgch 108:254bf037f83f 1056 /*nothing*/;
mazgch 21:c4d64830bf02 1057 }
mazgch 21:c4d64830bf02 1058 return WAIT;
mazgch 21:c4d64830bf02 1059 }
mazgch 21:c4d64830bf02 1060
mazgch 63:42cb563a25bc 1061 int MDMParser::socketSocket(IpProtocol ipproto, int port)
mazgch 21:c4d64830bf02 1062 {
mazgch 103:197fa7920ad8 1063 int socket;
mazgch 95:8282dbbe1492 1064 LOCK();
mazgch 103:197fa7920ad8 1065 // find an free socket
mazgch 103:197fa7920ad8 1066 socket = _findSocket();
mazgch 47:9a89e5195721 1067 TRACE("socketSocket(%d)\r\n", ipproto);
mazgch 95:8282dbbe1492 1068 if (socket != SOCKET_ERROR) {
mazgch 106:18aeacdae391 1069 if (ipproto == IPPROTO_UDP) {
mazgch 106:18aeacdae391 1070 // sending port can only be set on 2G/3G modules
mazgch 125:25a292afbac6 1071 if ((port != -1) && (_dev.dev != DEV_LISA_C2)) {
mazgch 106:18aeacdae391 1072 sendFormated("AT+USOCR=17,%d\r\n", port);
mazgch 106:18aeacdae391 1073 } else {
mazgch 106:18aeacdae391 1074 sendFormated("AT+USOCR=17\r\n");
mazgch 106:18aeacdae391 1075 }
mazgch 103:197fa7920ad8 1076 } else /*(ipproto == IPPROTO_TCP)*/ {
mazgch 103:197fa7920ad8 1077 sendFormated("AT+USOCR=6\r\n");
mazgch 103:197fa7920ad8 1078 }
mazgch 103:197fa7920ad8 1079 int handle = SOCKET_ERROR;
mazgch 103:197fa7920ad8 1080 if ((RESP_OK == waitFinalResp(_cbUSOCR, &handle)) &&
mazgch 103:197fa7920ad8 1081 (handle != SOCKET_ERROR)) {
mazgch 103:197fa7920ad8 1082 TRACE("Socket %d: handle %d was created\r\n", socket, handle);
mazgch 103:197fa7920ad8 1083 _sockets[socket].handle = handle;
mazgch 103:197fa7920ad8 1084 _sockets[socket].timeout_ms = TIMEOUT_BLOCKING;
mazgch 103:197fa7920ad8 1085 _sockets[socket].connected = false;
mazgch 103:197fa7920ad8 1086 _sockets[socket].pending = 0;
mazgch 103:197fa7920ad8 1087 }
mazgch 103:197fa7920ad8 1088 else
mazgch 103:197fa7920ad8 1089 socket = SOCKET_ERROR;
mazgch 95:8282dbbe1492 1090 }
mazgch 95:8282dbbe1492 1091 UNLOCK();
mazgch 21:c4d64830bf02 1092 return socket;
mazgch 21:c4d64830bf02 1093 }
mazgch 21:c4d64830bf02 1094
mazgch 21:c4d64830bf02 1095 bool MDMParser::socketConnect(int socket, const char * host, int port)
mazgch 21:c4d64830bf02 1096 {
mazgch 31:a0bed6c1e05d 1097 IP ip = gethostbyname(host);
mazgch 31:a0bed6c1e05d 1098 if (ip == NOIP)
mazgch 21:c4d64830bf02 1099 return false;
mazgch 21:c4d64830bf02 1100 // connect to socket
mazgch 95:8282dbbe1492 1101 bool ok = false;
mazgch 95:8282dbbe1492 1102 LOCK();
mazgch 103:197fa7920ad8 1103 if (ISSOCKET(socket) && (!_sockets[socket].connected)) {
mazgch 95:8282dbbe1492 1104 TRACE("socketConnect(%d,%s,%d)\r\n", socket,host,port);
mazgch 103:197fa7920ad8 1105 sendFormated("AT+USOCO=%d,\"" IPSTR "\",%d\r\n", _sockets[socket].handle, IPNUM(ip), port);
mazgch 95:8282dbbe1492 1106 if (RESP_OK == waitFinalResp())
mazgch 103:197fa7920ad8 1107 ok = _sockets[socket].connected = true;
mazgch 95:8282dbbe1492 1108 }
mazgch 95:8282dbbe1492 1109 UNLOCK();
mazgch 95:8282dbbe1492 1110 return ok;
mazgch 21:c4d64830bf02 1111 }
mazgch 21:c4d64830bf02 1112
mazgch 44:9d12223b78ff 1113 bool MDMParser::socketIsConnected(int socket)
mazgch 44:9d12223b78ff 1114 {
mazgch 95:8282dbbe1492 1115 bool ok = false;
mazgch 95:8282dbbe1492 1116 LOCK();
mazgch 103:197fa7920ad8 1117 ok = ISSOCKET(socket) && _sockets[socket].connected;
mazgch 107:436ee320efd6 1118 TRACE("socketIsConnected(%d) %s\r\n", socket, ok?"yes":"no");
mazgch 95:8282dbbe1492 1119 UNLOCK();
mazgch 95:8282dbbe1492 1120 return ok;
mazgch 44:9d12223b78ff 1121 }
mazgch 44:9d12223b78ff 1122
mazgch 66:69072b3c5bca 1123 bool MDMParser::socketSetBlocking(int socket, int timeout_ms)
mazgch 44:9d12223b78ff 1124 {
mazgch 95:8282dbbe1492 1125 bool ok = false;
mazgch 95:8282dbbe1492 1126 LOCK();
mazgch 103:197fa7920ad8 1127 TRACE("socketSetBlocking(%d,%d)\r\n", socket,timeout_ms);
mazgch 95:8282dbbe1492 1128 if (ISSOCKET(socket)) {
mazgch 95:8282dbbe1492 1129 _sockets[socket].timeout_ms = timeout_ms;
mazgch 95:8282dbbe1492 1130 ok = true;
mazgch 95:8282dbbe1492 1131 }
mazgch 95:8282dbbe1492 1132 UNLOCK();
mazgch 95:8282dbbe1492 1133 return ok;
mazgch 44:9d12223b78ff 1134 }
mazgch 44:9d12223b78ff 1135
mazgch 21:c4d64830bf02 1136 bool MDMParser::socketClose(int socket)
mazgch 21:c4d64830bf02 1137 {
mazgch 95:8282dbbe1492 1138 bool ok = false;
mazgch 95:8282dbbe1492 1139 LOCK();
mazgch 103:197fa7920ad8 1140 if (ISSOCKET(socket) && _sockets[socket].connected) {
mazgch 95:8282dbbe1492 1141 TRACE("socketClose(%d)\r\n", socket);
mazgch 103:197fa7920ad8 1142 sendFormated("AT+USOCL=%d\r\n", _sockets[socket].handle);
mazgch 95:8282dbbe1492 1143 if (RESP_OK == waitFinalResp()) {
mazgch 103:197fa7920ad8 1144 _sockets[socket].connected = false;
mazgch 95:8282dbbe1492 1145 ok = true;
mazgch 95:8282dbbe1492 1146 }
mazgch 95:8282dbbe1492 1147 }
mazgch 95:8282dbbe1492 1148 UNLOCK();
mazgch 95:8282dbbe1492 1149 return ok;
mazgch 21:c4d64830bf02 1150 }
mazgch 21:c4d64830bf02 1151
mazgch 21:c4d64830bf02 1152 bool MDMParser::socketFree(int socket)
mazgch 21:c4d64830bf02 1153 {
mazgch 95:8282dbbe1492 1154 // make sure it is closed
mazgch 21:c4d64830bf02 1155 socketClose(socket);
mazgch 95:8282dbbe1492 1156 bool ok = true;
mazgch 95:8282dbbe1492 1157 LOCK();
mazgch 103:197fa7920ad8 1158 if (ISSOCKET(socket)) {
mazgch 103:197fa7920ad8 1159 TRACE("socketFree(%d)\r\n", socket);
mazgch 103:197fa7920ad8 1160 _sockets[socket].handle = SOCKET_ERROR;
mazgch 103:197fa7920ad8 1161 _sockets[socket].timeout_ms = TIMEOUT_BLOCKING;
mazgch 103:197fa7920ad8 1162 _sockets[socket].connected = false;
mazgch 103:197fa7920ad8 1163 _sockets[socket].pending = 0;
mazgch 95:8282dbbe1492 1164 ok = true;
mazgch 95:8282dbbe1492 1165 }
mazgch 95:8282dbbe1492 1166 UNLOCK();
mazgch 95:8282dbbe1492 1167 return ok;
mazgch 21:c4d64830bf02 1168 }
mazgch 21:c4d64830bf02 1169
mazgch 69:4d6fa520dfca 1170 #define USO_MAX_WRITE 1024 //!< maximum number of bytes to write to socket
mazgch 69:4d6fa520dfca 1171
mazgch 21:c4d64830bf02 1172 int MDMParser::socketSend(int socket, const char * buf, int len)
mazgch 21:c4d64830bf02 1173 {
mazgch 47:9a89e5195721 1174 TRACE("socketSend(%d,,%d)\r\n", socket,len);
mazgch 68:33a96cf64986 1175 int cnt = len;
mazgch 68:33a96cf64986 1176 while (cnt > 0) {
mazgch 69:4d6fa520dfca 1177 int blk = USO_MAX_WRITE;
mazgch 68:33a96cf64986 1178 if (cnt < blk)
mazgch 68:33a96cf64986 1179 blk = cnt;
mazgch 95:8282dbbe1492 1180 bool ok = false;
mazgch 95:8282dbbe1492 1181 LOCK();
mazgch 103:197fa7920ad8 1182 if (ISSOCKET(socket)) {
mazgch 103:197fa7920ad8 1183 sendFormated("AT+USOWR=%d,%d\r\n",_sockets[socket].handle,blk);
mazgch 103:197fa7920ad8 1184 if (RESP_PROMPT == waitFinalResp()) {
mazgch 103:197fa7920ad8 1185 wait_ms(50);
mazgch 103:197fa7920ad8 1186 send(buf, blk);
mazgch 103:197fa7920ad8 1187 if (RESP_OK == waitFinalResp())
mazgch 103:197fa7920ad8 1188 ok = true;
mazgch 103:197fa7920ad8 1189 }
mazgch 95:8282dbbe1492 1190 }
mazgch 95:8282dbbe1492 1191 UNLOCK();
mazgch 95:8282dbbe1492 1192 if (!ok)
mazgch 21:c4d64830bf02 1193 return SOCKET_ERROR;
mazgch 68:33a96cf64986 1194 buf += blk;
mazgch 68:33a96cf64986 1195 cnt -= blk;
mazgch 21:c4d64830bf02 1196 }
mazgch 68:33a96cf64986 1197 return (len - cnt);
mazgch 21:c4d64830bf02 1198 }
mazgch 21:c4d64830bf02 1199
mazgch 21:c4d64830bf02 1200 int MDMParser::socketSendTo(int socket, IP ip, int port, const char * buf, int len)
mazgch 21:c4d64830bf02 1201 {
mazgch 103:197fa7920ad8 1202 TRACE("socketSendTo(%d," IPSTR ",%d,,%d)\r\n", socket,IPNUM(ip),port,len);
mazgch 68:33a96cf64986 1203 int cnt = len;
mazgch 68:33a96cf64986 1204 while (cnt > 0) {
mazgch 69:4d6fa520dfca 1205 int blk = USO_MAX_WRITE;
mazgch 68:33a96cf64986 1206 if (cnt < blk)
mazgch 68:33a96cf64986 1207 blk = cnt;
mazgch 95:8282dbbe1492 1208 bool ok = false;
mazgch 95:8282dbbe1492 1209 LOCK();
mazgch 103:197fa7920ad8 1210 if (ISSOCKET(socket)) {
mazgch 103:197fa7920ad8 1211 sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",_sockets[socket].handle,IPNUM(ip),port,blk);
mazgch 103:197fa7920ad8 1212 if (RESP_PROMPT == waitFinalResp()) {
mazgch 103:197fa7920ad8 1213 wait_ms(50);
mazgch 103:197fa7920ad8 1214 send(buf, blk);
mazgch 103:197fa7920ad8 1215 if (RESP_OK == waitFinalResp())
mazgch 103:197fa7920ad8 1216 ok = true;
mazgch 103:197fa7920ad8 1217 }
mazgch 95:8282dbbe1492 1218 }
mazgch 95:8282dbbe1492 1219 UNLOCK();
mazgch 95:8282dbbe1492 1220 if (!ok)
mazgch 21:c4d64830bf02 1221 return SOCKET_ERROR;
mazgch 68:33a96cf64986 1222 buf += blk;
mazgch 68:33a96cf64986 1223 cnt -= blk;
mazgch 21:c4d64830bf02 1224 }
mazgch 68:33a96cf64986 1225 return (len - cnt);
mazgch 21:c4d64830bf02 1226 }
mazgch 21:c4d64830bf02 1227
mazgch 21:c4d64830bf02 1228 int MDMParser::socketReadable(int socket)
mazgch 21:c4d64830bf02 1229 {
mazgch 95:8282dbbe1492 1230 int pending = SOCKET_ERROR;
mazgch 95:8282dbbe1492 1231 LOCK();
mazgch 103:197fa7920ad8 1232 if (ISSOCKET(socket) && _sockets[socket].connected) {
mazgch 95:8282dbbe1492 1233 TRACE("socketReadable(%d)\r\n", socket);
mazgch 95:8282dbbe1492 1234 // allow to receive unsolicited commands
mazgch 95:8282dbbe1492 1235 waitFinalResp(NULL, NULL, 0);
mazgch 103:197fa7920ad8 1236 if (_sockets[socket].connected)
mazgch 95:8282dbbe1492 1237 pending = _sockets[socket].pending;
mazgch 95:8282dbbe1492 1238 }
mazgch 95:8282dbbe1492 1239 UNLOCK();
mazgch 95:8282dbbe1492 1240 return pending;
mazgch 21:c4d64830bf02 1241 }
mazgch 21:c4d64830bf02 1242
mazgch 21:c4d64830bf02 1243 int MDMParser::_cbUSORD(int type, const char* buf, int len, char* out)
mazgch 21:c4d64830bf02 1244 {
mazgch 21:c4d64830bf02 1245 if ((type == TYPE_PLUS) && out) {
mazgch 21:c4d64830bf02 1246 int sz, sk;
mazgch 21:c4d64830bf02 1247 if ((sscanf(buf, "\r\n+USORD: %d,%d,", &sk, &sz) == 2) &&
mazgch 21:c4d64830bf02 1248 (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 21:c4d64830bf02 1249 memcpy(out, &buf[len-1-sz], sz);
mazgch 21:c4d64830bf02 1250 }
mazgch 21:c4d64830bf02 1251 }
mazgch 21:c4d64830bf02 1252 return WAIT;
mazgch 21:c4d64830bf02 1253 }
mazgch 21:c4d64830bf02 1254
mazgch 21:c4d64830bf02 1255 int MDMParser::socketRecv(int socket, char* buf, int len)
mazgch 21:c4d64830bf02 1256 {
mazgch 21:c4d64830bf02 1257 int cnt = 0;
mazgch 47:9a89e5195721 1258 TRACE("socketRecv(%d,,%d)\r\n", socket, len);
mazgch 95:8282dbbe1492 1259 #ifdef MDM_DEBUG
mazgch 21:c4d64830bf02 1260 memset(buf, '\0', len);
mazgch 95:8282dbbe1492 1261 #endif
mazgch 44:9d12223b78ff 1262 Timer timer;
mazgch 44:9d12223b78ff 1263 timer.start();
mazgch 21:c4d64830bf02 1264 while (len) {
mazgch 69:4d6fa520dfca 1265 int blk = MAX_SIZE; // still need space for headers and unsolicited commands
mazgch 21:c4d64830bf02 1266 if (len < blk) blk = len;
mazgch 95:8282dbbe1492 1267 bool ok = false;
mazgch 95:8282dbbe1492 1268 LOCK();
mazgch 95:8282dbbe1492 1269 if (ISSOCKET(socket)) {
mazgch 103:197fa7920ad8 1270 if (_sockets[socket].connected) {
mazgch 95:8282dbbe1492 1271 if (_sockets[socket].pending < blk)
mazgch 95:8282dbbe1492 1272 blk = _sockets[socket].pending;
mazgch 95:8282dbbe1492 1273 if (blk > 0) {
mazgch 103:197fa7920ad8 1274 sendFormated("AT+USORD=%d,%d\r\n",_sockets[socket].handle, blk);
mazgch 95:8282dbbe1492 1275 if (RESP_OK == waitFinalResp(_cbUSORD, buf)) {
mazgch 95:8282dbbe1492 1276 _sockets[socket].pending -= blk;
mazgch 95:8282dbbe1492 1277 len -= blk;
mazgch 95:8282dbbe1492 1278 cnt += blk;
mazgch 95:8282dbbe1492 1279 buf += blk;
mazgch 95:8282dbbe1492 1280 ok = true;
mazgch 95:8282dbbe1492 1281 }
mazgch 95:8282dbbe1492 1282 } else if (!TIMEOUT(timer, _sockets[socket].timeout_ms)) {
mazgch 95:8282dbbe1492 1283 ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
mazgch 95:8282dbbe1492 1284 } else {
mazgch 95:8282dbbe1492 1285 len = 0;
mazgch 95:8282dbbe1492 1286 ok = true;
mazgch 95:8282dbbe1492 1287 }
mazgch 103:197fa7920ad8 1288 } else {
mazgch 95:8282dbbe1492 1289 len = 0;
mazgch 95:8282dbbe1492 1290 ok = true;
mazgch 21:c4d64830bf02 1291 }
mazgch 21:c4d64830bf02 1292 }
mazgch 95:8282dbbe1492 1293 UNLOCK();
mazgch 107:436ee320efd6 1294 if (!ok) {
mazgch 107:436ee320efd6 1295 TRACE("socketRecv: ERROR\r\n");
mazgch 95:8282dbbe1492 1296 return SOCKET_ERROR;
mazgch 107:436ee320efd6 1297 }
mazgch 21:c4d64830bf02 1298 }
mazgch 107:436ee320efd6 1299 TRACE("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt);
mazgch 21:c4d64830bf02 1300 return cnt;
mazgch 21:c4d64830bf02 1301 }
mazgch 21:c4d64830bf02 1302
mazgch 21:c4d64830bf02 1303 int MDMParser::_cbUSORF(int type, const char* buf, int len, USORFparam* param)
mazgch 21:c4d64830bf02 1304 {
mazgch 21:c4d64830bf02 1305 if ((type == TYPE_PLUS) && param) {
mazgch 21:c4d64830bf02 1306 int sz, sk, p, a,b,c,d;
mazgch 95:8282dbbe1492 1307 int r = sscanf(buf, "\r\n+USORF: %d,\"" IPSTR "\",%d,%d,",
mazgch 95:8282dbbe1492 1308 &sk,&a,&b,&c,&d,&p,&sz);
mazgch 95:8282dbbe1492 1309 if ((r == 7) && (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 21:c4d64830bf02 1310 memcpy(param->buf, &buf[len-1-sz], sz);
mazgch 21:c4d64830bf02 1311 param->ip = IPADR(a,b,c,d);
mazgch 21:c4d64830bf02 1312 param->port = p;
mazgch 21:c4d64830bf02 1313 }
mazgch 21:c4d64830bf02 1314 }
mazgch 21:c4d64830bf02 1315 return WAIT;
mazgch 21:c4d64830bf02 1316 }
mazgch 21:c4d64830bf02 1317
mazgch 63:42cb563a25bc 1318 int MDMParser::socketRecvFrom(int socket, IP* ip, int* port, char* buf, int len)
mazgch 21:c4d64830bf02 1319 {
mazgch 21:c4d64830bf02 1320 int cnt = 0;
mazgch 63:42cb563a25bc 1321 TRACE("socketRecvFrom(%d,,%d)\r\n", socket, len);
mazgch 95:8282dbbe1492 1322 #ifdef MDM_DEBUG
mazgch 21:c4d64830bf02 1323 memset(buf, '\0', len);
mazgch 95:8282dbbe1492 1324 #endif
mazgch 44:9d12223b78ff 1325 Timer timer;
mazgch 44:9d12223b78ff 1326 timer.start();
mazgch 21:c4d64830bf02 1327 while (len) {
mazgch 69:4d6fa520dfca 1328 int blk = MAX_SIZE; // still need space for headers and unsolicited commands
mazgch 21:c4d64830bf02 1329 if (len < blk) blk = len;
mazgch 95:8282dbbe1492 1330 bool ok = false;
mazgch 95:8282dbbe1492 1331 LOCK();
mazgch 95:8282dbbe1492 1332 if (ISSOCKET(socket)) {
mazgch 95:8282dbbe1492 1333 if (_sockets[socket].pending < blk)
mazgch 95:8282dbbe1492 1334 blk = _sockets[socket].pending;
mazgch 95:8282dbbe1492 1335 if (blk > 0) {
mazgch 103:197fa7920ad8 1336 sendFormated("AT+USORF=%d,%d\r\n",_sockets[socket].handle, blk);
mazgch 95:8282dbbe1492 1337 USORFparam param;
mazgch 95:8282dbbe1492 1338 param.buf = buf;
mazgch 95:8282dbbe1492 1339 if (RESP_OK == waitFinalResp(_cbUSORF, &param)) {
mazgch 95:8282dbbe1492 1340 _sockets[socket].pending -= blk;
mazgch 95:8282dbbe1492 1341 *ip = param.ip;
mazgch 95:8282dbbe1492 1342 *port = param.port;
mazgch 95:8282dbbe1492 1343 len -= blk;
mazgch 95:8282dbbe1492 1344 cnt += blk;
mazgch 95:8282dbbe1492 1345 buf += blk;
mazgch 95:8282dbbe1492 1346 len = 0; // done
mazgch 95:8282dbbe1492 1347 ok = true;
mazgch 95:8282dbbe1492 1348 }
mazgch 95:8282dbbe1492 1349 } else if (!TIMEOUT(timer, _sockets[socket].timeout_ms)) {
mazgch 95:8282dbbe1492 1350 ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
mazgch 95:8282dbbe1492 1351 } else {
mazgch 95:8282dbbe1492 1352 len = 0; // no more data and socket closed or timed-out
mazgch 95:8282dbbe1492 1353 ok = true;
mazgch 21:c4d64830bf02 1354 }
mazgch 95:8282dbbe1492 1355 }
mazgch 95:8282dbbe1492 1356 UNLOCK();
mazgch 107:436ee320efd6 1357 if (!ok) {
mazgch 107:436ee320efd6 1358 TRACE("socketRecv: ERROR\r\n");
mazgch 95:8282dbbe1492 1359 return SOCKET_ERROR;
mazgch 107:436ee320efd6 1360 }
mazgch 21:c4d64830bf02 1361 }
mazgch 44:9d12223b78ff 1362 timer.stop();
mazgch 44:9d12223b78ff 1363 timer.reset();
mazgch 107:436ee320efd6 1364 TRACE("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt);
mazgch 21:c4d64830bf02 1365 return cnt;
mazgch 21:c4d64830bf02 1366 }
mazgch 21:c4d64830bf02 1367
mazgch 103:197fa7920ad8 1368 int MDMParser::_findSocket(int handle) {
mazgch 104:c64ba749a422 1369 for (int socket = 0; socket < NUMSOCKETS; socket ++) {
mazgch 103:197fa7920ad8 1370 if (_sockets[socket].handle == handle)
mazgch 103:197fa7920ad8 1371 return socket;
mazgch 103:197fa7920ad8 1372 }
mazgch 103:197fa7920ad8 1373 return SOCKET_ERROR;
mazgch 103:197fa7920ad8 1374 }
mazgch 103:197fa7920ad8 1375
mazgch 21:c4d64830bf02 1376 // ----------------------------------------------------------------
fdilenarda 135:cbccf4052d45 1377 // HTTP
fdilenarda 135:cbccf4052d45 1378
fdilenarda 135:cbccf4052d45 1379 int MDMParser::httpFindProfile()
fdilenarda 135:cbccf4052d45 1380 {
fdilenarda 135:cbccf4052d45 1381 int profile = HTTP_PROF_ERROR; //default value
fdilenarda 135:cbccf4052d45 1382 LOCK();
fdilenarda 135:cbccf4052d45 1383 // find a free HTTP profile
fdilenarda 135:cbccf4052d45 1384 profile = _findProfile();
fdilenarda 135:cbccf4052d45 1385 TRACE("httpFindProfile: profile is %d\r\n", profile);
fdilenarda 135:cbccf4052d45 1386 if (profile != HTTP_PROF_ERROR) {
fdilenarda 135:cbccf4052d45 1387 _httpProfiles[profile].handle = 1;
fdilenarda 135:cbccf4052d45 1388 _httpProfiles[profile].timeout_ms = TIMEOUT_BLOCKING;
fdilenarda 135:cbccf4052d45 1389 _httpProfiles[profile].pending = false;
fdilenarda 135:cbccf4052d45 1390 _httpProfiles[profile].cmd = -1;
fdilenarda 135:cbccf4052d45 1391 _httpProfiles[profile].result = -1;
fdilenarda 135:cbccf4052d45 1392 }
fdilenarda 135:cbccf4052d45 1393 UNLOCK();
fdilenarda 135:cbccf4052d45 1394 return profile;
fdilenarda 135:cbccf4052d45 1395 }
fdilenarda 135:cbccf4052d45 1396
fdilenarda 135:cbccf4052d45 1397 int MDMParser::_findProfile(int handle) {
fdilenarda 135:cbccf4052d45 1398 for (int profile = 0; profile < NUMPROFILES; profile++) {
fdilenarda 135:cbccf4052d45 1399 if (_httpProfiles[profile].handle == handle)
fdilenarda 135:cbccf4052d45 1400 return profile;
fdilenarda 135:cbccf4052d45 1401 }
fdilenarda 135:cbccf4052d45 1402 return HTTP_PROF_ERROR;
fdilenarda 135:cbccf4052d45 1403 }
fdilenarda 135:cbccf4052d45 1404
fdilenarda 135:cbccf4052d45 1405 bool MDMParser::httpSetBlocking(int profile, int timeout_ms)
fdilenarda 135:cbccf4052d45 1406 {
fdilenarda 135:cbccf4052d45 1407 bool ok = false;
fdilenarda 135:cbccf4052d45 1408 LOCK();
fdilenarda 135:cbccf4052d45 1409 TRACE("httpSetBlocking(%d,%d)\r\n", profile, timeout_ms);
fdilenarda 135:cbccf4052d45 1410 if (ISPROFILE(profile)) {
fdilenarda 135:cbccf4052d45 1411 _httpProfiles[profile].timeout_ms = timeout_ms;
fdilenarda 135:cbccf4052d45 1412 ok = true;
fdilenarda 135:cbccf4052d45 1413 }
fdilenarda 135:cbccf4052d45 1414 UNLOCK();
fdilenarda 135:cbccf4052d45 1415 return ok;
fdilenarda 135:cbccf4052d45 1416 }
fdilenarda 135:cbccf4052d45 1417
fdilenarda 135:cbccf4052d45 1418 bool MDMParser::httpSetProfileForCmdMng(int profile)
fdilenarda 135:cbccf4052d45 1419 {
fdilenarda 135:cbccf4052d45 1420 bool ok = false;
fdilenarda 135:cbccf4052d45 1421 LOCK();
fdilenarda 135:cbccf4052d45 1422 TRACE("httpSetProfileForCmdMng(%d)\r\n", profile);
fdilenarda 135:cbccf4052d45 1423 if (ISPROFILE(profile)) {
fdilenarda 135:cbccf4052d45 1424 _httpProfiles[profile].pending = true;
fdilenarda 135:cbccf4052d45 1425 _httpProfiles[profile].result = -1;
fdilenarda 135:cbccf4052d45 1426 ok = true;
fdilenarda 135:cbccf4052d45 1427 }
fdilenarda 135:cbccf4052d45 1428 UNLOCK();
fdilenarda 135:cbccf4052d45 1429 return ok;
fdilenarda 135:cbccf4052d45 1430 }
fdilenarda 135:cbccf4052d45 1431
fdilenarda 135:cbccf4052d45 1432 bool MDMParser::httpFreeProfile(int profile)
fdilenarda 135:cbccf4052d45 1433 {
fdilenarda 135:cbccf4052d45 1434 bool ok = true;
fdilenarda 135:cbccf4052d45 1435 LOCK();
fdilenarda 135:cbccf4052d45 1436 if (ISPROFILE(profile)) {
fdilenarda 135:cbccf4052d45 1437 TRACE("httpFreeProfile(%d)\r\n", profile);
fdilenarda 135:cbccf4052d45 1438 _httpProfiles[profile].handle = HTTP_PROF_ERROR;
fdilenarda 135:cbccf4052d45 1439 _httpProfiles[profile].timeout_ms = TIMEOUT_BLOCKING;
fdilenarda 135:cbccf4052d45 1440 _httpProfiles[profile].pending = false;
fdilenarda 135:cbccf4052d45 1441 _httpProfiles[profile].cmd = -1;
fdilenarda 135:cbccf4052d45 1442 _httpProfiles[profile].result = -1;
fdilenarda 135:cbccf4052d45 1443 ok = true;
fdilenarda 135:cbccf4052d45 1444 }
fdilenarda 135:cbccf4052d45 1445 UNLOCK();
fdilenarda 135:cbccf4052d45 1446 return ok;
fdilenarda 135:cbccf4052d45 1447 }
fdilenarda 135:cbccf4052d45 1448
fdilenarda 135:cbccf4052d45 1449 bool MDMParser::httpResetProfile(int httpProfile)
fdilenarda 135:cbccf4052d45 1450 {
fdilenarda 135:cbccf4052d45 1451 bool ok = false;
fdilenarda 135:cbccf4052d45 1452
fdilenarda 135:cbccf4052d45 1453 LOCK();
fdilenarda 135:cbccf4052d45 1454 TRACE("httpResetProfile(%d)\r\n", httpProfile);
fdilenarda 135:cbccf4052d45 1455 sendFormated("AT+UHTTP=%d\r\n", httpProfile);
fdilenarda 135:cbccf4052d45 1456 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1457 ok = true;
fdilenarda 135:cbccf4052d45 1458 UNLOCK();
fdilenarda 135:cbccf4052d45 1459
fdilenarda 135:cbccf4052d45 1460 return ok;
fdilenarda 135:cbccf4052d45 1461 }
fdilenarda 135:cbccf4052d45 1462
fdilenarda 135:cbccf4052d45 1463 bool MDMParser::httpSetPar(int httpProfile, HttpOpCode httpOpCode, const char * httpInPar)
fdilenarda 135:cbccf4052d45 1464 {
fdilenarda 135:cbccf4052d45 1465 bool ok = false;
fdilenarda 135:cbccf4052d45 1466 IP ip = NOIP;
fdilenarda 135:cbccf4052d45 1467 int httpInParNum = 0;
fdilenarda 135:cbccf4052d45 1468
fdilenarda 135:cbccf4052d45 1469 LOCK();
fdilenarda 135:cbccf4052d45 1470 TRACE("httpSetPar(%d,%d,\"%s\")\r\n", httpProfile, httpOpCode, httpInPar);
fdilenarda 135:cbccf4052d45 1471 switch(httpOpCode){
fdilenarda 135:cbccf4052d45 1472 case HTTP_IP_ADDRESS: //0
fdilenarda 135:cbccf4052d45 1473 ip = gethostbyname(httpInPar);
fdilenarda 135:cbccf4052d45 1474 if (ip == NOIP)
fdilenarda 135:cbccf4052d45 1475 return false;
fdilenarda 135:cbccf4052d45 1476
fdilenarda 135:cbccf4052d45 1477 sendFormated("AT+UHTTP=%d,%d,\"" IPSTR "\"\r\n", httpProfile, httpOpCode, IPNUM(ip));
fdilenarda 135:cbccf4052d45 1478 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1479 ok = true;
fdilenarda 135:cbccf4052d45 1480 break;
fdilenarda 135:cbccf4052d45 1481
fdilenarda 135:cbccf4052d45 1482 case HTTP_SERVER_NAME: //1
fdilenarda 135:cbccf4052d45 1483 case HTTP_USER_NAME: //2
fdilenarda 135:cbccf4052d45 1484 case HTTP_PASSWORD: //3
fdilenarda 135:cbccf4052d45 1485 sendFormated("AT+UHTTP=%d,%d,\"%s\"\r\n", httpProfile, httpOpCode, httpInPar);
fdilenarda 135:cbccf4052d45 1486 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1487 ok = true;
fdilenarda 135:cbccf4052d45 1488 break;
fdilenarda 135:cbccf4052d45 1489
fdilenarda 135:cbccf4052d45 1490 case HTTP_AUTH_TYPE: //4
fdilenarda 137:6a7a5c4f35f6 1491 case HTTP_SERVER_PORT: //5
fdilenarda 135:cbccf4052d45 1492 httpInParNum = atoi(httpInPar);
fdilenarda 135:cbccf4052d45 1493 sendFormated("AT+UHTTP=%d,%d,%d\r\n", httpProfile, httpOpCode, httpInParNum);
fdilenarda 135:cbccf4052d45 1494 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1495 ok = true;
fdilenarda 135:cbccf4052d45 1496 break;
fdilenarda 135:cbccf4052d45 1497
fdilenarda 135:cbccf4052d45 1498 case HTTP_SECURE: //6
fdilenarda 135:cbccf4052d45 1499 if(_dev.dev != DEV_LISA_C2)
fdilenarda 135:cbccf4052d45 1500 {
fdilenarda 135:cbccf4052d45 1501 httpInParNum = atoi(httpInPar);
fdilenarda 135:cbccf4052d45 1502 sendFormated("AT+UHTTP=%d,%d,%d\r\n", httpProfile, httpOpCode, httpInParNum);
fdilenarda 135:cbccf4052d45 1503 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1504 ok = true;
fdilenarda 135:cbccf4052d45 1505 } else {
fdilenarda 135:cbccf4052d45 1506 TRACE("httpSetPar: HTTP secure option not supported by module\r\n");
fdilenarda 135:cbccf4052d45 1507 ok = false;
fdilenarda 135:cbccf4052d45 1508 }
fdilenarda 135:cbccf4052d45 1509 break;
fdilenarda 135:cbccf4052d45 1510
fdilenarda 135:cbccf4052d45 1511 default:
fdilenarda 135:cbccf4052d45 1512 TRACE("httpSetPar: unknown httpOpCode %s\r\n", httpOpCode);
fdilenarda 135:cbccf4052d45 1513 ok = false;
fdilenarda 135:cbccf4052d45 1514 break;
fdilenarda 135:cbccf4052d45 1515 }
fdilenarda 135:cbccf4052d45 1516 UNLOCK();
fdilenarda 135:cbccf4052d45 1517 return ok;
fdilenarda 135:cbccf4052d45 1518 }
fdilenarda 135:cbccf4052d45 1519
fdilenarda 135:cbccf4052d45 1520 bool MDMParser::httpCommand(int httpProfile, HttpCmd httpCmdCode, const char* httpPath, const char* httpOut, \
fdilenarda 135:cbccf4052d45 1521 const char* httpIn, int httpContentType, const char* httpCustomPar, char* buf, int len)
fdilenarda 135:cbccf4052d45 1522 {
fdilenarda 135:cbccf4052d45 1523 bool ok = false;
fdilenarda 135:cbccf4052d45 1524 #ifdef MDM_DEBUG
fdilenarda 135:cbccf4052d45 1525 memset(buf, '\0', len);
fdilenarda 135:cbccf4052d45 1526 #endif
fdilenarda 135:cbccf4052d45 1527 LOCK();
fdilenarda 135:cbccf4052d45 1528 TRACE("%s\r\n", getHTTPcmd(httpCmdCode));
fdilenarda 135:cbccf4052d45 1529 switch (httpCmdCode)
fdilenarda 135:cbccf4052d45 1530 {
fdilenarda 135:cbccf4052d45 1531 case HTTP_HEAD:
fdilenarda 135:cbccf4052d45 1532 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\"\r\n", httpProfile, HTTP_HEAD, httpPath, httpOut);
fdilenarda 135:cbccf4052d45 1533 break;
fdilenarda 135:cbccf4052d45 1534
fdilenarda 135:cbccf4052d45 1535 case HTTP_GET:
fdilenarda 135:cbccf4052d45 1536 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\"\r\n", httpProfile, HTTP_GET, httpPath, httpOut);
fdilenarda 135:cbccf4052d45 1537 break;
fdilenarda 135:cbccf4052d45 1538
fdilenarda 135:cbccf4052d45 1539 case HTTP_DELETE:
fdilenarda 135:cbccf4052d45 1540 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\"\r\n", httpProfile, HTTP_DELETE, httpPath, httpOut);
fdilenarda 135:cbccf4052d45 1541 break;
fdilenarda 135:cbccf4052d45 1542
fdilenarda 135:cbccf4052d45 1543 case HTTP_PUT:
fdilenarda 135:cbccf4052d45 1544 //in this case the parameter httpIn is a filename
fdilenarda 135:cbccf4052d45 1545 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\"\r\n", httpProfile, HTTP_PUT, httpPath, httpOut, httpIn);
fdilenarda 135:cbccf4052d45 1546 break;
fdilenarda 135:cbccf4052d45 1547
fdilenarda 135:cbccf4052d45 1548 case HTTP_POST_FILE:
fdilenarda 135:cbccf4052d45 1549 //in this case the parameter httpIn is a filename
fdilenarda 135:cbccf4052d45 1550 if(_dev.dev != DEV_LISA_C2)
fdilenarda 135:cbccf4052d45 1551 {
fdilenarda 135:cbccf4052d45 1552 if(httpContentType != 6)
fdilenarda 135:cbccf4052d45 1553 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d\r\n", \
fdilenarda 135:cbccf4052d45 1554 httpProfile, HTTP_POST_FILE, httpPath, httpOut, httpIn, httpContentType);
fdilenarda 135:cbccf4052d45 1555 else
fdilenarda 135:cbccf4052d45 1556 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d,%d\r\n", \
fdilenarda 135:cbccf4052d45 1557 httpProfile, HTTP_POST_FILE, httpPath, httpOut, httpIn, httpContentType, httpCustomPar);
fdilenarda 135:cbccf4052d45 1558 }
fdilenarda 135:cbccf4052d45 1559 else{
fdilenarda 135:cbccf4052d45 1560 if((httpContentType != 5) && (httpContentType != 6) && (httpCustomPar == NULL))
fdilenarda 135:cbccf4052d45 1561 {
fdilenarda 135:cbccf4052d45 1562 //parameters values consistent with the AT commands specs of LISA-C200
fdilenarda 135:cbccf4052d45 1563 //(in particular httpCustomPar has to be not defined)
fdilenarda 135:cbccf4052d45 1564 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d\r\n", \
fdilenarda 135:cbccf4052d45 1565 httpProfile, HTTP_POST_FILE, httpPath, httpOut, httpIn, httpContentType);
fdilenarda 135:cbccf4052d45 1566 } else {
fdilenarda 135:cbccf4052d45 1567 TRACE("httpCommand: command not supported by module");
fdilenarda 135:cbccf4052d45 1568 return ok; //error
fdilenarda 135:cbccf4052d45 1569 }
fdilenarda 135:cbccf4052d45 1570 }
fdilenarda 135:cbccf4052d45 1571 break;
fdilenarda 135:cbccf4052d45 1572
fdilenarda 135:cbccf4052d45 1573 case HTTP_POST_DATA:
fdilenarda 135:cbccf4052d45 1574 //in this case the parameter httpIn is a string containing data
fdilenarda 135:cbccf4052d45 1575 if(_dev.dev != DEV_LISA_C2)
fdilenarda 135:cbccf4052d45 1576 {
fdilenarda 135:cbccf4052d45 1577 if(httpContentType != 6)
fdilenarda 135:cbccf4052d45 1578 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d\r\n", \
fdilenarda 135:cbccf4052d45 1579 httpProfile, HTTP_POST_DATA, httpPath, httpOut, httpIn, httpContentType);
fdilenarda 135:cbccf4052d45 1580 else
fdilenarda 135:cbccf4052d45 1581 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d,%d\r\n", \
fdilenarda 135:cbccf4052d45 1582 httpProfile, HTTP_POST_DATA, httpPath, httpOut, httpIn, httpContentType, httpCustomPar);
fdilenarda 135:cbccf4052d45 1583 } else {
fdilenarda 135:cbccf4052d45 1584 if((httpContentType != 5) && (httpContentType != 6) && (httpCustomPar == NULL))
fdilenarda 135:cbccf4052d45 1585 {
fdilenarda 135:cbccf4052d45 1586 //parameters values consistent with the AT commands specs of LISA-C200
fdilenarda 135:cbccf4052d45 1587 //(in particular httpCustomPar has to be not defined)
fdilenarda 135:cbccf4052d45 1588 sendFormated("AT+UHTTPC=%d,%d,\"%s\",\"%s\",\"%s\",%d\r\n", \
fdilenarda 135:cbccf4052d45 1589 httpProfile, HTTP_POST_DATA, httpPath, httpOut, httpIn, httpContentType);
fdilenarda 135:cbccf4052d45 1590 } else {
fdilenarda 135:cbccf4052d45 1591 TRACE("httpCommand: command not supported by module");
fdilenarda 135:cbccf4052d45 1592 return ok; //error
fdilenarda 135:cbccf4052d45 1593 }
fdilenarda 135:cbccf4052d45 1594 }
fdilenarda 135:cbccf4052d45 1595 break;
fdilenarda 135:cbccf4052d45 1596
fdilenarda 135:cbccf4052d45 1597 default:
fdilenarda 135:cbccf4052d45 1598 TRACE("HTTP command not recognized\r\n");
fdilenarda 135:cbccf4052d45 1599 return ok; //error
fdilenarda 135:cbccf4052d45 1600 }
fdilenarda 135:cbccf4052d45 1601
fdilenarda 135:cbccf4052d45 1602 if (RESP_OK == waitFinalResp())
fdilenarda 135:cbccf4052d45 1603 {
fdilenarda 135:cbccf4052d45 1604 Timer timer;
fdilenarda 135:cbccf4052d45 1605 timer.start();
fdilenarda 135:cbccf4052d45 1606 httpSetProfileForCmdMng(httpProfile);
fdilenarda 135:cbccf4052d45 1607 while (_httpProfiles[httpProfile].pending) //waiting for unsolicited
fdilenarda 135:cbccf4052d45 1608 {
fdilenarda 135:cbccf4052d45 1609 ok = false; //reset variable
fdilenarda 135:cbccf4052d45 1610 if(_httpProfiles[httpProfile].result != -1)
fdilenarda 135:cbccf4052d45 1611 {
fdilenarda 135:cbccf4052d45 1612 //received unsolicited: starting its analysis
fdilenarda 135:cbccf4052d45 1613 _httpProfiles[httpProfile].pending = false;
fdilenarda 135:cbccf4052d45 1614 if(_httpProfiles[httpProfile].result == 1)
fdilenarda 135:cbccf4052d45 1615 {
fdilenarda 135:cbccf4052d45 1616 //HTTP command successfully executed
fdilenarda 135:cbccf4052d45 1617 if(_dev.dev != DEV_LISA_C2)
fdilenarda 135:cbccf4052d45 1618 {
fdilenarda 135:cbccf4052d45 1619 TRACE("httpCommand: reading files with a dimension " \
fdilenarda 135:cbccf4052d45 1620 "also greater than MAX_SIZE bytes\r\n");
fdilenarda 135:cbccf4052d45 1621 if(readFileNew(httpOut,buf,len) >=0 )
fdilenarda 135:cbccf4052d45 1622 ok = true;
fdilenarda 135:cbccf4052d45 1623 } else {
fdilenarda 135:cbccf4052d45 1624 TRACE("httpCommand: reading files with a dimension " \
fdilenarda 135:cbccf4052d45 1625 "less than MAX_SIZE bytes, otherwise error\r\n");
fdilenarda 135:cbccf4052d45 1626 if(readFile(httpOut,buf,len) >=0 )
fdilenarda 135:cbccf4052d45 1627 ok = true;
fdilenarda 135:cbccf4052d45 1628 }
fdilenarda 135:cbccf4052d45 1629 } else {
fdilenarda 135:cbccf4052d45 1630 //HTTP command not successfully executed
fdilenarda 135:cbccf4052d45 1631 ok = false;
fdilenarda 135:cbccf4052d45 1632 }
fdilenarda 135:cbccf4052d45 1633 } else if (!TIMEOUT(timer, _httpProfiles[httpProfile].timeout_ms)) {
fdilenarda 135:cbccf4052d45 1634 ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
fdilenarda 135:cbccf4052d45 1635 } else {
fdilenarda 135:cbccf4052d45 1636 //not received unsolicited and expired timer
fdilenarda 135:cbccf4052d45 1637 TRACE("httpCommand: not received unsolicited and expired timer\r\n");
fdilenarda 135:cbccf4052d45 1638 ok = false;
fdilenarda 135:cbccf4052d45 1639 }
fdilenarda 135:cbccf4052d45 1640 if (!ok) {
fdilenarda 135:cbccf4052d45 1641 TRACE("%s: ERROR\r\n", getHTTPcmd(httpCmdCode));
fdilenarda 135:cbccf4052d45 1642 _httpProfiles[httpProfile].pending = false; //no more while loops
fdilenarda 135:cbccf4052d45 1643 }
fdilenarda 135:cbccf4052d45 1644 }
fdilenarda 135:cbccf4052d45 1645 }
fdilenarda 135:cbccf4052d45 1646 UNLOCK();
fdilenarda 135:cbccf4052d45 1647 return ok;
fdilenarda 135:cbccf4052d45 1648 }
fdilenarda 135:cbccf4052d45 1649
fdilenarda 135:cbccf4052d45 1650 const char* MDMParser::getHTTPcmd(int httpCmdCode)
fdilenarda 135:cbccf4052d45 1651 {
fdilenarda 135:cbccf4052d45 1652 switch (httpCmdCode)
fdilenarda 135:cbccf4052d45 1653 {
fdilenarda 135:cbccf4052d45 1654 case HTTP_HEAD:
fdilenarda 135:cbccf4052d45 1655 return "HTTP HEAD command";
fdilenarda 135:cbccf4052d45 1656 case HTTP_GET:
fdilenarda 135:cbccf4052d45 1657 return "HTTP GET command";
fdilenarda 135:cbccf4052d45 1658 case HTTP_DELETE:
fdilenarda 135:cbccf4052d45 1659 return "HTTP DELETE command";
fdilenarda 135:cbccf4052d45 1660 case HTTP_PUT:
fdilenarda 135:cbccf4052d45 1661 return "HTTP PUT command";
fdilenarda 135:cbccf4052d45 1662 case HTTP_POST_FILE:
fdilenarda 135:cbccf4052d45 1663 return "HTTP POST file command";
fdilenarda 135:cbccf4052d45 1664 case HTTP_POST_DATA:
fdilenarda 135:cbccf4052d45 1665 return "HTTP POST data command";
fdilenarda 135:cbccf4052d45 1666 default:
fdilenarda 135:cbccf4052d45 1667 return "HTTP command not recognized";
fdilenarda 135:cbccf4052d45 1668 }
fdilenarda 135:cbccf4052d45 1669 }
fdilenarda 135:cbccf4052d45 1670
fdilenarda 135:cbccf4052d45 1671 // ----------------------------------------------------------------
mazgch 31:a0bed6c1e05d 1672
mazgch 31:a0bed6c1e05d 1673 int MDMParser::_cbCMGL(int type, const char* buf, int len, CMGLparam* param)
mazgch 21:c4d64830bf02 1674 {
mazgch 31:a0bed6c1e05d 1675 if ((type == TYPE_PLUS) && param && param->num) {
mazgch 31:a0bed6c1e05d 1676 // +CMGL: <ix>,...
mazgch 31:a0bed6c1e05d 1677 int ix;
mazgch 31:a0bed6c1e05d 1678 if (sscanf(buf, "\r\n+CMGL: %d,", &ix) == 1)
mazgch 31:a0bed6c1e05d 1679 {
mazgch 31:a0bed6c1e05d 1680 *param->ix++ = ix;
mazgch 31:a0bed6c1e05d 1681 param->num--;
mazgch 31:a0bed6c1e05d 1682 }
mazgch 29:53d346010624 1683 }
mazgch 29:53d346010624 1684 return WAIT;
mazgch 21:c4d64830bf02 1685 }
mazgch 21:c4d64830bf02 1686
mazgch 31:a0bed6c1e05d 1687 int MDMParser::smsList(const char* stat /*= "ALL"*/, int* ix /*=NULL*/, int num /*= 0*/) {
mazgch 95:8282dbbe1492 1688 int ret = -1;
mazgch 95:8282dbbe1492 1689 LOCK();
mazgch 31:a0bed6c1e05d 1690 sendFormated("AT+CMGL=\"%s\"\r\n", stat);
mazgch 31:a0bed6c1e05d 1691 CMGLparam param;
mazgch 31:a0bed6c1e05d 1692 param.ix = ix;
mazgch 31:a0bed6c1e05d 1693 param.num = num;
mazgch 95:8282dbbe1492 1694 if (RESP_OK == waitFinalResp(_cbCMGL, &param))
mazgch 95:8282dbbe1492 1695 ret = num - param.num;
mazgch 95:8282dbbe1492 1696 UNLOCK();
mazgch 95:8282dbbe1492 1697 return ret;
mazgch 21:c4d64830bf02 1698 }
mazgch 21:c4d64830bf02 1699
mazgch 21:c4d64830bf02 1700 bool MDMParser::smsSend(const char* num, const char* buf)
mazgch 21:c4d64830bf02 1701 {
mazgch 95:8282dbbe1492 1702 bool ok = false;
mazgch 95:8282dbbe1492 1703 LOCK();
mazgch 80:34985b4d821e 1704 sendFormated("AT+CMGS=\"%s\"\r\n",num);
mazgch 95:8282dbbe1492 1705 if (RESP_PROMPT == waitFinalResp(NULL,NULL,150*1000)) {
mazgch 95:8282dbbe1492 1706 send(buf, strlen(buf));
mazgch 95:8282dbbe1492 1707 const char ctrlZ = 0x1A;
mazgch 95:8282dbbe1492 1708 send(&ctrlZ, sizeof(ctrlZ));
mazgch 95:8282dbbe1492 1709 ok = (RESP_OK == waitFinalResp());
mazgch 21:c4d64830bf02 1710 }
mazgch 95:8282dbbe1492 1711 UNLOCK();
mazgch 95:8282dbbe1492 1712 return ok;
mazgch 21:c4d64830bf02 1713 }
mazgch 21:c4d64830bf02 1714
mazgch 21:c4d64830bf02 1715 bool MDMParser::smsDelete(int ix)
mazgch 21:c4d64830bf02 1716 {
mazgch 95:8282dbbe1492 1717 bool ok = false;
mazgch 95:8282dbbe1492 1718 LOCK();
mazgch 21:c4d64830bf02 1719 sendFormated("AT+CMGD=%d\r\n",ix);
mazgch 95:8282dbbe1492 1720 ok = (RESP_OK == waitFinalResp());
mazgch 95:8282dbbe1492 1721 UNLOCK();
mazgch 95:8282dbbe1492 1722 return ok;
mazgch 21:c4d64830bf02 1723 }
mazgch 21:c4d64830bf02 1724
mazgch 21:c4d64830bf02 1725 int MDMParser::_cbCMGR(int type, const char* buf, int len, CMGRparam* param)
mazgch 21:c4d64830bf02 1726 {
mazgch 21:c4d64830bf02 1727 if (param) {
mazgch 21:c4d64830bf02 1728 if (type == TYPE_PLUS) {
mazgch 21:c4d64830bf02 1729 if (sscanf(buf, "\r\n+CMGR: \"%*[^\"]\",\"%[^\"]", param->num) == 1) {
mazgch 21:c4d64830bf02 1730 }
mazgch 37:cc3433329d66 1731 } else if ((type == TYPE_UNKNOWN) && (buf[len-2] == '\r') && (buf[len-1] == '\n')) {
mazgch 21:c4d64830bf02 1732 memcpy(param->buf, buf, len-2);
mazgch 21:c4d64830bf02 1733 param->buf[len-2] = '\0';
mazgch 21:c4d64830bf02 1734 }
mazgch 21:c4d64830bf02 1735 }
mazgch 21:c4d64830bf02 1736 return WAIT;
mazgch 21:c4d64830bf02 1737 }
mazgch 21:c4d64830bf02 1738
mazgch 21:c4d64830bf02 1739 bool MDMParser::smsRead(int ix, char* num, char* buf, int len)
mazgch 21:c4d64830bf02 1740 {
mazgch 95:8282dbbe1492 1741 bool ok = false;
mazgch 95:8282dbbe1492 1742 LOCK();
mazgch 21:c4d64830bf02 1743 CMGRparam param;
mazgch 21:c4d64830bf02 1744 param.num = num;
mazgch 21:c4d64830bf02 1745 param.buf = buf;
mazgch 21:c4d64830bf02 1746 sendFormated("AT+CMGR=%d\r\n",ix);
mazgch 95:8282dbbe1492 1747 ok = (RESP_OK == waitFinalResp(_cbCMGR, &param));
mazgch 95:8282dbbe1492 1748 UNLOCK();
mazgch 95:8282dbbe1492 1749 return ok;
mazgch 21:c4d64830bf02 1750 }
mazgch 54:7ba8e4c218e2 1751
mazgch 54:7ba8e4c218e2 1752 // ----------------------------------------------------------------
mazgch 70:0a87d256cd24 1753
mazgch 70:0a87d256cd24 1754 int MDMParser::_cbCUSD(int type, const char* buf, int len, char* resp)
mazgch 70:0a87d256cd24 1755 {
mazgch 70:0a87d256cd24 1756 if ((type == TYPE_PLUS) && resp) {
mazgch 70:0a87d256cd24 1757 // +USD: \"%*[^\"]\",\"%[^\"]\",,\"%*[^\"]\",%d,%d,%d,%d,\"*[^\"]\",%d,%d"..);
mazgch 70:0a87d256cd24 1758 if (sscanf(buf, "\r\n+CUSD: %*d,\"%[^\"]\",%*d", resp) == 1) {
mazgch 70:0a87d256cd24 1759 /*nothing*/
mazgch 70:0a87d256cd24 1760 }
mazgch 70:0a87d256cd24 1761 }
mazgch 70:0a87d256cd24 1762 return WAIT;
mazgch 70:0a87d256cd24 1763 }
mazgch 70:0a87d256cd24 1764
mazgch 70:0a87d256cd24 1765 bool MDMParser::ussdCommand(const char* cmd, char* buf)
mazgch 70:0a87d256cd24 1766 {
mazgch 95:8282dbbe1492 1767 bool ok = false;
mazgch 95:8282dbbe1492 1768 LOCK();
mazgch 70:0a87d256cd24 1769 *buf = '\0';
mazgch 125:25a292afbac6 1770 if (_dev.dev != DEV_LISA_C2) {
mazgch 95:8282dbbe1492 1771 sendFormated("AT+CUSD=1,\"%s\"\r\n",cmd);
mazgch 95:8282dbbe1492 1772 ok = (RESP_OK == waitFinalResp(_cbCUSD, buf));
mazgch 70:0a87d256cd24 1773 }
mazgch 95:8282dbbe1492 1774 UNLOCK();
mazgch 95:8282dbbe1492 1775 return ok;
mazgch 70:0a87d256cd24 1776 }
mazgch 80:34985b4d821e 1777
mazgch 80:34985b4d821e 1778 // ----------------------------------------------------------------
mazgch 70:0a87d256cd24 1779
mazgch 115:d8d94b73c725 1780 int MDMParser::_cbUDELFILE(int type, const char* buf, int len, void*)
mazgch 115:d8d94b73c725 1781 {
mazgch 115:d8d94b73c725 1782 if ((type == TYPE_ERROR) && strstr(buf, "+CME ERROR: FILE NOT FOUND"))
mazgch 115:d8d94b73c725 1783 return RESP_OK; // file does not exist, so all ok...
mazgch 115:d8d94b73c725 1784 return WAIT;
mazgch 115:d8d94b73c725 1785 }
mazgch 115:d8d94b73c725 1786
mazgch 80:34985b4d821e 1787 bool MDMParser::delFile(const char* filename)
mazgch 80:34985b4d821e 1788 {
mazgch 95:8282dbbe1492 1789 bool ok = false;
mazgch 95:8282dbbe1492 1790 LOCK();
mazgch 80:34985b4d821e 1791 sendFormated("AT+UDELFILE=\"%s\"\r\n", filename);
mazgch 115:d8d94b73c725 1792 ok = (RESP_OK == waitFinalResp(_cbUDELFILE));
mazgch 95:8282dbbe1492 1793 UNLOCK();
mazgch 95:8282dbbe1492 1794 return ok;
mazgch 80:34985b4d821e 1795 }
mazgch 80:34985b4d821e 1796
mazgch 80:34985b4d821e 1797 int MDMParser::writeFile(const char* filename, const char* buf, int len)
mazgch 80:34985b4d821e 1798 {
mazgch 95:8282dbbe1492 1799 bool ok = false;
mazgch 95:8282dbbe1492 1800 LOCK();
mazgch 80:34985b4d821e 1801 sendFormated("AT+UDWNFILE=\"%s\",%d\r\n", filename, len);
mazgch 95:8282dbbe1492 1802 if (RESP_PROMPT == waitFinalResp()) {
mazgch 95:8282dbbe1492 1803 send(buf, len);
mazgch 95:8282dbbe1492 1804 ok = (RESP_OK == waitFinalResp());
mazgch 95:8282dbbe1492 1805 }
mazgch 95:8282dbbe1492 1806 UNLOCK();
mazgch 95:8282dbbe1492 1807 return ok ? len : -1;
mazgch 80:34985b4d821e 1808 }
mazgch 80:34985b4d821e 1809
mazgch 80:34985b4d821e 1810 int MDMParser::readFile(const char* filename, char* buf, int len)
mazgch 80:34985b4d821e 1811 {
mazgch 80:34985b4d821e 1812 URDFILEparam param;
mazgch 80:34985b4d821e 1813 param.filename = filename;
mazgch 80:34985b4d821e 1814 param.buf = buf;
mazgch 80:34985b4d821e 1815 param.sz = len;
mazgch 80:34985b4d821e 1816 param.len = 0;
mazgch 95:8282dbbe1492 1817 LOCK();
mazgch 95:8282dbbe1492 1818 sendFormated("AT+URDFILE=\"%s\"\r\n", filename, len);
mazgch 105:f6bb2a20de70 1819 if (RESP_OK != waitFinalResp(_cbURDFILE, &param))
mazgch 95:8282dbbe1492 1820 param.len = -1;
mazgch 95:8282dbbe1492 1821 UNLOCK();
mazgch 80:34985b4d821e 1822 return param.len;
mazgch 80:34985b4d821e 1823 }
mazgch 80:34985b4d821e 1824
mazgch 80:34985b4d821e 1825 int MDMParser::_cbURDFILE(int type, const char* buf, int len, URDFILEparam* param)
mazgch 80:34985b4d821e 1826 {
mazgch 80:34985b4d821e 1827 if ((type == TYPE_PLUS) && param && param->filename && param->buf) {
mazgch 80:34985b4d821e 1828 char filename[48];
mazgch 80:34985b4d821e 1829 int sz;
mazgch 80:34985b4d821e 1830 if ((sscanf(buf, "\r\n+URDFILE: \"%[^\"]\",%d,", filename, &sz) == 2) &&
mazgch 80:34985b4d821e 1831 (0 == strcmp(param->filename, filename)) &&
mazgch 80:34985b4d821e 1832 (buf[len-sz-2] == '\"') && (buf[len-1] == '\"')) {
mazgch 80:34985b4d821e 1833 param->len = (sz < param->sz) ? sz : param->sz;
mazgch 80:34985b4d821e 1834 memcpy(param->buf, &buf[len-1-sz], param->len);
mazgch 80:34985b4d821e 1835 }
mazgch 80:34985b4d821e 1836 }
mazgch 80:34985b4d821e 1837 return WAIT;
mazgch 80:34985b4d821e 1838 }
fdilenarda 135:cbccf4052d45 1839
fdilenarda 135:cbccf4052d45 1840 //The following function is useful for reading files with a dimension greater than MAX_SIZE bytes
fdilenarda 135:cbccf4052d45 1841 int MDMParser::readFileNew(const char* filename, char* buf, int len)
fdilenarda 135:cbccf4052d45 1842 {
fdilenarda 135:cbccf4052d45 1843 int countBytes = -1; //counter for file reading (default value)
fdilenarda 135:cbccf4052d45 1844
fdilenarda 135:cbccf4052d45 1845 if(_dev.dev != DEV_LISA_C2)
fdilenarda 135:cbccf4052d45 1846 {
fdilenarda 135:cbccf4052d45 1847 //retrieve information about the file, in particular its size
fdilenarda 135:cbccf4052d45 1848 int filesize = infoFile(filename);
fdilenarda 135:cbccf4052d45 1849 TRACE("readFileNew: filename is %s; filesize is %d\r\n", filename, filesize);
fdilenarda 135:cbccf4052d45 1850
fdilenarda 135:cbccf4052d45 1851 if (len < filesize)
fdilenarda 135:cbccf4052d45 1852 TRACE("readFileNew: WARNING. Buffer dimension is %d bytes," \
fdilenarda 135:cbccf4052d45 1853 "while file size is %d bytes\r\n", len, filesize);
fdilenarda 135:cbccf4052d45 1854
fdilenarda 135:cbccf4052d45 1855 if (filesize > 0)
fdilenarda 135:cbccf4052d45 1856 {
fdilenarda 135:cbccf4052d45 1857 #ifdef MDM_DEBUG
fdilenarda 135:cbccf4052d45 1858 memset(buf, '\0', len);
fdilenarda 135:cbccf4052d45 1859 #endif
fdilenarda 135:cbccf4052d45 1860 int offset = 0; //start reading from 0
fdilenarda 135:cbccf4052d45 1861 int blockSize = MAX_SIZE; //still need space for headers and unsolicited commands
fdilenarda 135:cbccf4052d45 1862 int bytesToRead = filesize; //bytes to read
fdilenarda 135:cbccf4052d45 1863
fdilenarda 135:cbccf4052d45 1864 while (bytesToRead)
fdilenarda 135:cbccf4052d45 1865 {
fdilenarda 135:cbccf4052d45 1866 bool ok = false;
fdilenarda 135:cbccf4052d45 1867
fdilenarda 135:cbccf4052d45 1868 if (bytesToRead < blockSize)
fdilenarda 135:cbccf4052d45 1869 blockSize = bytesToRead;
fdilenarda 135:cbccf4052d45 1870
fdilenarda 135:cbccf4052d45 1871 LOCK();
fdilenarda 135:cbccf4052d45 1872 if (blockSize > 0) {
fdilenarda 135:cbccf4052d45 1873
fdilenarda 135:cbccf4052d45 1874 sendFormated("AT+URDBLOCK=\"%s\",%d,%d\r\n", filename, offset, blockSize);
fdilenarda 135:cbccf4052d45 1875
fdilenarda 135:cbccf4052d45 1876 if (RESP_OK == waitFinalResp(_cbURDBLOCK, buf)) {
fdilenarda 135:cbccf4052d45 1877 bytesToRead -= blockSize;
fdilenarda 135:cbccf4052d45 1878 offset += blockSize;
fdilenarda 135:cbccf4052d45 1879 buf += blockSize;
fdilenarda 135:cbccf4052d45 1880 ok = true;
fdilenarda 135:cbccf4052d45 1881 } else {
fdilenarda 135:cbccf4052d45 1882 //error condition
fdilenarda 135:cbccf4052d45 1883 countBytes = -1;
fdilenarda 135:cbccf4052d45 1884 ok = false;
fdilenarda 135:cbccf4052d45 1885 }
fdilenarda 135:cbccf4052d45 1886 }
fdilenarda 135:cbccf4052d45 1887 UNLOCK();
fdilenarda 135:cbccf4052d45 1888
fdilenarda 135:cbccf4052d45 1889 if (!ok) {
fdilenarda 135:cbccf4052d45 1890 TRACE("readFileNew: ERROR\r\n");
fdilenarda 135:cbccf4052d45 1891 return countBytes; //in this case countBytes is -1
fdilenarda 135:cbccf4052d45 1892 }
fdilenarda 135:cbccf4052d45 1893 }
fdilenarda 135:cbccf4052d45 1894
fdilenarda 135:cbccf4052d45 1895 countBytes = offset; //total read bytes
fdilenarda 135:cbccf4052d45 1896 return countBytes;
fdilenarda 135:cbccf4052d45 1897 }
fdilenarda 135:cbccf4052d45 1898 } else {
fdilenarda 135:cbccf4052d45 1899 TRACE("httpCommand: command not supported by module");
fdilenarda 135:cbccf4052d45 1900 }
fdilenarda 135:cbccf4052d45 1901 return countBytes; //it could be 0 or -1 (possible error)
fdilenarda 135:cbccf4052d45 1902 }
fdilenarda 135:cbccf4052d45 1903
fdilenarda 135:cbccf4052d45 1904 int MDMParser::_cbURDBLOCK(int type, const char* buf, int len, char* out)
fdilenarda 135:cbccf4052d45 1905 {
fdilenarda 135:cbccf4052d45 1906 char fileNameRes[48];
fdilenarda 135:cbccf4052d45 1907 int sizeRes;
fdilenarda 135:cbccf4052d45 1908
fdilenarda 135:cbccf4052d45 1909 if ((type == TYPE_PLUS) && out) {
fdilenarda 135:cbccf4052d45 1910 if ((sscanf(buf, "\r\n+URDBLOCK: \"%[^\"]\",%d,", fileNameRes, &sizeRes) == 2) &&
fdilenarda 135:cbccf4052d45 1911 (buf[len-sizeRes-2] == '\"') && (buf[len-1] == '\"')) {
fdilenarda 135:cbccf4052d45 1912 memcpy(out, &buf[len-1-sizeRes], sizeRes);
fdilenarda 135:cbccf4052d45 1913 }
fdilenarda 135:cbccf4052d45 1914 }
fdilenarda 135:cbccf4052d45 1915
fdilenarda 135:cbccf4052d45 1916 return WAIT;
fdilenarda 135:cbccf4052d45 1917 }
fdilenarda 135:cbccf4052d45 1918
fdilenarda 135:cbccf4052d45 1919 int MDMParser::infoFile(const char* filename)
fdilenarda 135:cbccf4052d45 1920 {
fdilenarda 135:cbccf4052d45 1921 int infoFile = 0; //default value
fdilenarda 135:cbccf4052d45 1922
fdilenarda 135:cbccf4052d45 1923 LOCK();
fdilenarda 135:cbccf4052d45 1924 sendFormated("AT+ULSTFILE=2,\"%s\"\r\n", filename);
fdilenarda 135:cbccf4052d45 1925 if (RESP_OK != waitFinalResp(_cbULSTFILE, &infoFile))
fdilenarda 135:cbccf4052d45 1926 infoFile = -1; //error condition
fdilenarda 135:cbccf4052d45 1927 UNLOCK();
fdilenarda 135:cbccf4052d45 1928
fdilenarda 135:cbccf4052d45 1929 return infoFile;
fdilenarda 135:cbccf4052d45 1930 }
fdilenarda 135:cbccf4052d45 1931
fdilenarda 135:cbccf4052d45 1932 int MDMParser::_cbULSTFILE(int type, const char* buf, int len, int* infoFile)
fdilenarda 135:cbccf4052d45 1933 {
fdilenarda 135:cbccf4052d45 1934 if (infoFile) {
fdilenarda 135:cbccf4052d45 1935 if (type == TYPE_PLUS) {
fdilenarda 135:cbccf4052d45 1936 if (sscanf(buf, "\r\n+ULSTFILE: %d\r\n", infoFile) == 1) {
fdilenarda 135:cbccf4052d45 1937 }
fdilenarda 135:cbccf4052d45 1938 }
fdilenarda 135:cbccf4052d45 1939 }
fdilenarda 135:cbccf4052d45 1940 return WAIT;
fdilenarda 135:cbccf4052d45 1941 }
fdilenarda 135:cbccf4052d45 1942
msinig 133:57b208dd96fb 1943 // ----------------------------------------------------------------
msinig 136:8dc8f48275fc 1944 int MDMParser::cellLocSrvTcp(const char* token, const char* server_1, const char* server_2, int days/* = 14*/, \
msinig 133:57b208dd96fb 1945 int period/* = 4*/, int resolution/* = 1*/)
msinig 133:57b208dd96fb 1946 {
msinig 133:57b208dd96fb 1947 bool ok = false;
msinig 133:57b208dd96fb 1948 LOCK();
msinig 133:57b208dd96fb 1949 if (_dev.dev == DEV_LISA_U2_03S || _dev.dev == DEV_SARA_U2 ){
msinig 133:57b208dd96fb 1950 sendFormated("AT+UGSRV=\"%s\",\"%s\",\"%s\"\r\n", server_1, server_2, token, days, period, resolution);
msinig 133:57b208dd96fb 1951 ok = (RESP_OK == waitFinalResp());
msinig 133:57b208dd96fb 1952 } else
msinig 136:8dc8f48275fc 1953 ERROR("Command not supported\r\n");
msinig 133:57b208dd96fb 1954 UNLOCK();
msinig 133:57b208dd96fb 1955 return ok;
msinig 133:57b208dd96fb 1956 }
msinig 133:57b208dd96fb 1957
msinig 133:57b208dd96fb 1958 int MDMParser::cellLocSrvUdp(const char* server_1 /*= "cell-live1.services.u-blox.com"*/, int port /*= 46434*/, \
msinig 133:57b208dd96fb 1959 int latency/* = 1000*/, int mode/* = 0*/)
msinig 133:57b208dd96fb 1960 {
msinig 133:57b208dd96fb 1961 bool ok = false;
msinig 133:57b208dd96fb 1962 LOCK();
msinig 133:57b208dd96fb 1963 if (_dev.dev != DEV_TOBY_L2){
msinig 133:57b208dd96fb 1964 sendFormated("AT+UGAOP=\"%s\",%d,%d,%d\r\n", server_1, port, latency, mode);
msinig 133:57b208dd96fb 1965 ok = (RESP_OK == waitFinalResp());
msinig 133:57b208dd96fb 1966 } else
msinig 136:8dc8f48275fc 1967 ERROR("Command not supported\r\n");
msinig 133:57b208dd96fb 1968 UNLOCK();
msinig 133:57b208dd96fb 1969 return ok;
msinig 133:57b208dd96fb 1970 }
msinig 133:57b208dd96fb 1971
msinig 136:8dc8f48275fc 1972 int MDMParser::cellLocUnsol(int mode)
msinig 133:57b208dd96fb 1973 {
msinig 133:57b208dd96fb 1974 bool ok = false;
msinig 133:57b208dd96fb 1975 LOCK();
msinig 133:57b208dd96fb 1976 if (_dev.dev == DEV_LISA_U2_03S){
msinig 133:57b208dd96fb 1977 sendFormated("AT+ULOCIND=%d\r\n", mode);
msinig 133:57b208dd96fb 1978 ok = (RESP_OK == waitFinalResp());
msinig 133:57b208dd96fb 1979 } else
msinig 136:8dc8f48275fc 1980 ERROR("Command not supported\r\n");
msinig 133:57b208dd96fb 1981 UNLOCK();
msinig 133:57b208dd96fb 1982 return ok;
msinig 133:57b208dd96fb 1983 }
msinig 133:57b208dd96fb 1984
msinig 136:8dc8f48275fc 1985 int MDMParser::cellLocConfig(int scanMode)
msinig 133:57b208dd96fb 1986 {
msinig 133:57b208dd96fb 1987 bool ok = false;
msinig 133:57b208dd96fb 1988 LOCK();
msinig 133:57b208dd96fb 1989 if (_dev.dev != DEV_TOBY_L2){
msinig 133:57b208dd96fb 1990 sendFormated("AT+ULOCCELL=%d\r\n", scanMode);
msinig 133:57b208dd96fb 1991 ok = (RESP_OK == waitFinalResp());
msinig 133:57b208dd96fb 1992 }else
msinig 136:8dc8f48275fc 1993 ERROR("Command not supported\r\n");
msinig 133:57b208dd96fb 1994 UNLOCK();
msinig 133:57b208dd96fb 1995 return ok;
msinig 133:57b208dd96fb 1996 }
msinig 133:57b208dd96fb 1997
msinig 136:8dc8f48275fc 1998 int MDMParser::cellLocRequest(CellSensType sensor, int timeout, int accuracy, CellRespType type/* =1*/, int hypotesis/* =1*/)
msinig 133:57b208dd96fb 1999 {
msinig 133:57b208dd96fb 2000 bool ok = false;
msinig 133:57b208dd96fb 2001
msinig 136:8dc8f48275fc 2002 if (hypotesis > 1 && type != CELL_MULTIHYP){
msinig 136:8dc8f48275fc 2003 ERROR("Num hypotesis is not set accordelying to CellRespType\r\n");
msinig 136:8dc8f48275fc 2004 return false;
msinig 136:8dc8f48275fc 2005 }
msinig 136:8dc8f48275fc 2006 if (hypotesis > CELL_MAX_HYP){
msinig 136:8dc8f48275fc 2007 ERROR("Number of hypotesis is too big\r\n");
msinig 136:8dc8f48275fc 2008 return false;
msinig 136:8dc8f48275fc 2009 }
msinig 136:8dc8f48275fc 2010 LOCK();
msinig 136:8dc8f48275fc 2011 _locRcvPos=0;
msinig 136:8dc8f48275fc 2012 _locExpPos=0;
msinig 136:8dc8f48275fc 2013 for (int i=0; i < hypotesis; i++)
msinig 136:8dc8f48275fc 2014 _loc[i].validData = false;
msinig 133:57b208dd96fb 2015 if (_dev.dev == DEV_LISA_U2_03S){
msinig 136:8dc8f48275fc 2016 sendFormated("AT+ULOC=2,%d,%d,%d,%d,%d\r\n", sensor, type, timeout, accuracy, hypotesis);
msinig 133:57b208dd96fb 2017 ok = (RESP_OK == waitFinalResp());
msinig 133:57b208dd96fb 2018 } else if (_dev.dev != DEV_TOBY_L2){
msinig 133:57b208dd96fb 2019 sendFormated("AT+ULOC=2,%d,1,%d,%d\r\n", sensor, timeout, accuracy);
msinig 133:57b208dd96fb 2020 ok = (RESP_OK == waitFinalResp());
msinig 136:8dc8f48275fc 2021 } else
msinig 136:8dc8f48275fc 2022 ERROR("Command not supported\r\n");
msinig 133:57b208dd96fb 2023 UNLOCK();
msinig 133:57b208dd96fb 2024 return ok;
msinig 133:57b208dd96fb 2025 }
msinig 136:8dc8f48275fc 2026 int MDMParser::cellLocGetRes()
msinig 136:8dc8f48275fc 2027 {
msinig 136:8dc8f48275fc 2028 return _locRcvPos;
msinig 136:8dc8f48275fc 2029 }
msinig 136:8dc8f48275fc 2030 int MDMParser::cellLocGetExpRes()
msinig 136:8dc8f48275fc 2031 {
msinig 136:8dc8f48275fc 2032 int res=0;
msinig 133:57b208dd96fb 2033 waitFinalResp(NULL,NULL,0);
msinig 136:8dc8f48275fc 2034 LOCK();
msinig 136:8dc8f48275fc 2035 if (_locRcvPos>0)
msinig 136:8dc8f48275fc 2036 res = _locExpPos;
msinig 136:8dc8f48275fc 2037 UNLOCK();
msinig 136:8dc8f48275fc 2038 return res;
msinig 136:8dc8f48275fc 2039 }
msinig 136:8dc8f48275fc 2040
msinig 136:8dc8f48275fc 2041 int MDMParser::cellLocGetData(CellLocData *data, int index/*=0*/){
msinig 136:8dc8f48275fc 2042
msinig 136:8dc8f48275fc 2043 if (!_loc[index].validData)
msinig 136:8dc8f48275fc 2044 return false;
msinig 136:8dc8f48275fc 2045 LOCK();
msinig 136:8dc8f48275fc 2046 memcpy(data, &_loc[index], sizeof(*_loc));
msinig 136:8dc8f48275fc 2047 UNLOCK();
msinig 136:8dc8f48275fc 2048 return true;
msinig 133:57b208dd96fb 2049 }
msinig 133:57b208dd96fb 2050
mazgch 70:0a87d256cd24 2051 // ----------------------------------------------------------------
mazgch 74:208e3e32d263 2052 bool MDMParser::setDebug(int level)
mazgch 74:208e3e32d263 2053 {
mazgch 74:208e3e32d263 2054 #ifdef MDM_DEBUG
mazgch 123:66cef6353b13 2055 _debugLevel = (level < -1) ? -1 :
mazgch 123:66cef6353b13 2056 (level > 3) ? 3 :
mazgch 123:66cef6353b13 2057 level;
Christopher Haster 141:0d91c7fe072b 2058 return _debugLevel == level;
Christopher Haster 141:0d91c7fe072b 2059 #else
Christopher Haster 141:0d91c7fe072b 2060 return false;
mazgch 74:208e3e32d263 2061 #endif
mazgch 74:208e3e32d263 2062 }
mazgch 74:208e3e32d263 2063
mazgch 73:2b32e0a21df2 2064 void MDMParser::dumpDevStatus(MDMParser::DevStatus* status,
mazgch 73:2b32e0a21df2 2065 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 2066 {
mazgch 75:ce6e12067d0c 2067 dprint(param, "Modem::devStatus\r\n");
msinig 133:57b208dd96fb 2068 const char* txtDev[] = { "Unknown", "SARA-G35", "LISA-U2", "LISA-U2-03S", "LISA-C2",
mazgch 125:25a292afbac6 2069 "SARA-U2", "LEON-G2", "TOBY-L2", "MPCI-L2" };
mazgch 98:c786461edd40 2070 if (status->dev < sizeof(txtDev)/sizeof(*txtDev) && (status->dev != DEV_UNKNOWN))
mazgch 73:2b32e0a21df2 2071 dprint(param, " Device: %s\r\n", txtDev[status->dev]);
mazgch 54:7ba8e4c218e2 2072 const char* txtLpm[] = { "Disabled", "Enabled", "Active" };
mazgch 54:7ba8e4c218e2 2073 if (status->lpm < sizeof(txtLpm)/sizeof(*txtLpm))
mazgch 73:2b32e0a21df2 2074 dprint(param, " Power Save: %s\r\n", txtLpm[status->lpm]);
mazgch 75:ce6e12067d0c 2075 const char* txtSim[] = { "Unknown", "Missing", "Pin", "Ready" };
mazgch 98:c786461edd40 2076 if (status->sim < sizeof(txtSim)/sizeof(*txtSim) && (status->sim != SIM_UNKNOWN))
mazgch 73:2b32e0a21df2 2077 dprint(param, " SIM: %s\r\n", txtSim[status->sim]);
mazgch 54:7ba8e4c218e2 2078 if (*status->ccid)
mazgch 73:2b32e0a21df2 2079 dprint(param, " CCID: %s\r\n", status->ccid);
mazgch 54:7ba8e4c218e2 2080 if (*status->imei)
mazgch 73:2b32e0a21df2 2081 dprint(param, " IMEI: %s\r\n", status->imei);
mazgch 54:7ba8e4c218e2 2082 if (*status->imsi)
mazgch 73:2b32e0a21df2 2083 dprint(param, " IMSI: %s\r\n", status->imsi);
mazgch 54:7ba8e4c218e2 2084 if (*status->meid)
mazgch 73:2b32e0a21df2 2085 dprint(param, " MEID: %s\r\n", status->meid); // LISA-C
mazgch 54:7ba8e4c218e2 2086 if (*status->manu)
mazgch 73:2b32e0a21df2 2087 dprint(param, " Manufacturer: %s\r\n", status->manu);
mazgch 54:7ba8e4c218e2 2088 if (*status->model)
mazgch 73:2b32e0a21df2 2089 dprint(param, " Model: %s\r\n", status->model);
mazgch 54:7ba8e4c218e2 2090 if (*status->ver)
mazgch 73:2b32e0a21df2 2091 dprint(param, " Version: %s\r\n", status->ver);
mazgch 54:7ba8e4c218e2 2092 }
mazgch 54:7ba8e4c218e2 2093
mazgch 73:2b32e0a21df2 2094 void MDMParser::dumpNetStatus(MDMParser::NetStatus *status,
mazgch 73:2b32e0a21df2 2095 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 2096 {
mazgch 75:ce6e12067d0c 2097 dprint(param, "Modem::netStatus\r\n");
mazgch 54:7ba8e4c218e2 2098 const char* txtReg[] = { "Unknown", "Denied", "None", "Home", "Roaming" };
mazgch 98:c786461edd40 2099 if (status->csd < sizeof(txtReg)/sizeof(*txtReg) && (status->csd != REG_UNKNOWN))
mazgch 79:291df065e345 2100 dprint(param, " CSD Registration: %s\r\n", txtReg[status->csd]);
mazgch 98:c786461edd40 2101 if (status->psd < sizeof(txtReg)/sizeof(*txtReg) && (status->psd != REG_UNKNOWN))
mazgch 79:291df065e345 2102 dprint(param, " PSD Registration: %s\r\n", txtReg[status->psd]);
mazgch 123:66cef6353b13 2103 if (status->eps < sizeof(txtReg)/sizeof(*txtReg) && (status->eps != REG_UNKNOWN))
mazgch 123:66cef6353b13 2104 dprint(param, " EPS Registration: %s\r\n", txtReg[status->eps]);
mazgch 123:66cef6353b13 2105 const char* txtAct[] = { "Unknown", "GSM", "Edge", "3G", "CDMA", "LTE" };
mazgch 98:c786461edd40 2106 if (status->act < sizeof(txtAct)/sizeof(*txtAct) && (status->act != ACT_UNKNOWN))
mazgch 73:2b32e0a21df2 2107 dprint(param, " Access Technology: %s\r\n", txtAct[status->act]);
mazgch 54:7ba8e4c218e2 2108 if (status->rssi)
mazgch 73:2b32e0a21df2 2109 dprint(param, " Signal Strength: %d dBm\r\n", status->rssi);
mazgch 54:7ba8e4c218e2 2110 if (status->ber)
mazgch 73:2b32e0a21df2 2111 dprint(param, " Bit Error Rate: %d\r\n", status->ber);
mazgch 54:7ba8e4c218e2 2112 if (*status->opr)
mazgch 73:2b32e0a21df2 2113 dprint(param, " Operator: %s\r\n", status->opr);
mazgch 54:7ba8e4c218e2 2114 if (status->lac != 0xFFFF)
mazgch 73:2b32e0a21df2 2115 dprint(param, " Location Area Code: %04X\r\n", status->lac);
mazgch 54:7ba8e4c218e2 2116 if (status->ci != 0xFFFFFFFF)
mazgch 73:2b32e0a21df2 2117 dprint(param, " Cell ID: %08X\r\n", status->ci);
mazgch 54:7ba8e4c218e2 2118 if (*status->num)
mazgch 73:2b32e0a21df2 2119 dprint(param, " Phone Number: %s\r\n", status->num);
mazgch 54:7ba8e4c218e2 2120 }
mazgch 54:7ba8e4c218e2 2121
mazgch 73:2b32e0a21df2 2122 void MDMParser::dumpIp(MDMParser::IP ip,
mazgch 73:2b32e0a21df2 2123 _DPRINT dprint, void* param)
mazgch 54:7ba8e4c218e2 2124 {
mazgch 57:869bd35f44cc 2125 if (ip != NOIP)
mazgch 75:ce6e12067d0c 2126 dprint(param, "Modem:IP " IPSTR "\r\n", IPNUM(ip));
mazgch 54:7ba8e4c218e2 2127 }
mazgch 70:0a87d256cd24 2128
mazgch 21:c4d64830bf02 2129 // ----------------------------------------------------------------
mazgch 21:c4d64830bf02 2130 int MDMParser::_parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end)
mazgch 18:e5697801df29 2131 {
mazgch 18:e5697801df29 2132 int o = 0;
mazgch 21:c4d64830bf02 2133 if (sta) {
mazgch 21:c4d64830bf02 2134 while (*sta) {
mazgch 21:c4d64830bf02 2135 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 2136 char ch = pipe->next();
mazgch 21:c4d64830bf02 2137 if (*sta++ != ch) return NOT_FOUND;
mazgch 21:c4d64830bf02 2138 }
mazgch 21:c4d64830bf02 2139 }
mazgch 21:c4d64830bf02 2140 if (!end) return o; // no termination
mazgch 35:9275215a3a5b 2141 // at least any char
mazgch 35:9275215a3a5b 2142 if (++o > len) return WAIT;
mazgch 35:9275215a3a5b 2143 pipe->next();
mazgch 35:9275215a3a5b 2144 // check the end
mazgch 21:c4d64830bf02 2145 int x = 0;
mazgch 21:c4d64830bf02 2146 while (end[x]) {
mazgch 21:c4d64830bf02 2147 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 2148 char ch = pipe->next();
mazgch 21:c4d64830bf02 2149 x = (end[x] == ch) ? x + 1 :
mazgch 21:c4d64830bf02 2150 (end[0] == ch) ? 1 :
mazgch 21:c4d64830bf02 2151 0;
mazgch 21:c4d64830bf02 2152 }
mazgch 21:c4d64830bf02 2153 return o;
mazgch 21:c4d64830bf02 2154 }
mazgch 21:c4d64830bf02 2155
mazgch 21:c4d64830bf02 2156 int MDMParser::_parseFormated(Pipe<char>* pipe, int len, const char* fmt)
mazgch 21:c4d64830bf02 2157 {
mazgch 21:c4d64830bf02 2158 int o = 0;
mazgch 21:c4d64830bf02 2159 int num = 0;
mazgch 21:c4d64830bf02 2160 if (fmt) {
mazgch 21:c4d64830bf02 2161 while (*fmt) {
mazgch 21:c4d64830bf02 2162 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 2163 char ch = pipe->next();
mazgch 21:c4d64830bf02 2164 if (*fmt == '%') {
mazgch 21:c4d64830bf02 2165 fmt++;
mazgch 21:c4d64830bf02 2166 if (*fmt == 'd') { // numeric
mazgch 21:c4d64830bf02 2167 fmt ++;
mazgch 21:c4d64830bf02 2168 num = 0;
mazgch 21:c4d64830bf02 2169 while (ch >= '0' && ch <= '9') {
mazgch 21:c4d64830bf02 2170 num = num * 10 + (ch - '0');
mazgch 21:c4d64830bf02 2171 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 2172 ch = pipe->next();
mazgch 21:c4d64830bf02 2173 }
mazgch 21:c4d64830bf02 2174 }
mazgch 21:c4d64830bf02 2175 else if (*fmt == 'c') { // char buffer (takes last numeric as length)
mazgch 21:c4d64830bf02 2176 fmt ++;
mazgch 21:c4d64830bf02 2177 while (num --) {
mazgch 21:c4d64830bf02 2178 if (++o > len) return WAIT;
mazgch 21:c4d64830bf02 2179 ch = pipe->next();
mazgch 21:c4d64830bf02 2180 }
mazgch 21:c4d64830bf02 2181 }
mazgch 80:34985b4d821e 2182 else if (*fmt == 's') {
mazgch 80:34985b4d821e 2183 fmt ++;
mazgch 80:34985b4d821e 2184 if (ch != '\"') return NOT_FOUND;
mazgch 80:34985b4d821e 2185 do {
mazgch 80:34985b4d821e 2186 if (++o > len) return WAIT;
mazgch 80:34985b4d821e 2187 ch = pipe->next();
mazgch 80:34985b4d821e 2188 } while (ch != '\"');
mazgch 80:34985b4d821e 2189 if (++o > len) return WAIT;
mazgch 80:34985b4d821e 2190 ch = pipe->next();
mazgch 80:34985b4d821e 2191 }
mazgch 21:c4d64830bf02 2192 }
mazgch 21:c4d64830bf02 2193 if (*fmt++ != ch) return NOT_FOUND;
mazgch 18:e5697801df29 2194 }
mazgch 18:e5697801df29 2195 }
mazgch 21:c4d64830bf02 2196 return o;
mazgch 21:c4d64830bf02 2197 }
mazgch 21:c4d64830bf02 2198
mazgch 21:c4d64830bf02 2199 int MDMParser::_getLine(Pipe<char>* pipe, char* buf, int len)
mazgch 21:c4d64830bf02 2200 {
mazgch 21:c4d64830bf02 2201 int unkn = 0;
mazgch 21:c4d64830bf02 2202 int sz = pipe->size();
mazgch 21:c4d64830bf02 2203 int fr = pipe->free();
mazgch 21:c4d64830bf02 2204 if (len > sz)
mazgch 21:c4d64830bf02 2205 len = sz;
mazgch 21:c4d64830bf02 2206 while (len > 0)
mazgch 21:c4d64830bf02 2207 {
mazgch 21:c4d64830bf02 2208 static struct {
mazgch 21:c4d64830bf02 2209 const char* fmt; int type;
mazgch 21:c4d64830bf02 2210 } lutF[] = {
mazgch 21:c4d64830bf02 2211 { "\r\n+USORD: %d,%d,\"%c\"", TYPE_PLUS },
mazgch 95:8282dbbe1492 2212 { "\r\n+USORF: %d,\"" IPSTR "\",%d,%d,\"%c\"", TYPE_PLUS },
mazgch 80:34985b4d821e 2213 { "\r\n+URDFILE: %s,%d,\"%c\"", TYPE_PLUS },
fdilenarda 135:cbccf4052d45 2214 { "\r\n+URDBLOCK: %s,%d,\"%c\"", TYPE_PLUS },
mazgch 21:c4d64830bf02 2215 };
mazgch 21:c4d64830bf02 2216 static struct {
mazgch 21:c4d64830bf02 2217 const char* sta; const char* end; int type;
mazgch 21:c4d64830bf02 2218 } lut[] = {
mazgch 21:c4d64830bf02 2219 { "\r\nOK\r\n", NULL, TYPE_OK },
mazgch 21:c4d64830bf02 2220 { "\r\nERROR\r\n", NULL, TYPE_ERROR },
msinig 134:2fbd5723e063 2221 { "\r\n+CME ERROR:", "\r\n", TYPE_ERROR_CME },
mazgch 21:c4d64830bf02 2222 { "\r\n+CMS ERROR:", "\r\n", TYPE_ERROR },
mazgch 21:c4d64830bf02 2223 { "\r\nRING\r\n", NULL, TYPE_RING },
mazgch 21:c4d64830bf02 2224 { "\r\nCONNECT\r\n", NULL, TYPE_CONNECT },
mazgch 21:c4d64830bf02 2225 { "\r\nNO CARRIER\r\n", NULL, TYPE_NOCARRIER },
mazgch 21:c4d64830bf02 2226 { "\r\nNO DIALTONE\r\n", NULL, TYPE_NODIALTONE },
mazgch 21:c4d64830bf02 2227 { "\r\nBUSY\r\n", NULL, TYPE_BUSY },
mazgch 21:c4d64830bf02 2228 { "\r\nNO ANSWER\r\n", NULL, TYPE_NOANSWER },
mazgch 21:c4d64830bf02 2229 { "\r\n+", "\r\n", TYPE_PLUS },
mazgch 21:c4d64830bf02 2230 { "\r\n@", NULL, TYPE_PROMPT }, // Sockets
mazgch 21:c4d64830bf02 2231 { "\r\n>", NULL, TYPE_PROMPT }, // SMS
mazgch 80:34985b4d821e 2232 { "\n>", NULL, TYPE_PROMPT }, // File
mazgch 21:c4d64830bf02 2233 };
mazgch 21:c4d64830bf02 2234 for (int i = 0; i < sizeof(lutF)/sizeof(*lutF); i ++) {
mazgch 21:c4d64830bf02 2235 pipe->set(unkn);
mazgch 21:c4d64830bf02 2236 int ln = _parseFormated(pipe, len, lutF[i].fmt);
mazgch 21:c4d64830bf02 2237 if (ln == WAIT && fr)
mazgch 21:c4d64830bf02 2238 return WAIT;
mazgch 21:c4d64830bf02 2239 if ((ln != NOT_FOUND) && (unkn > 0))
mazgch 31:a0bed6c1e05d 2240 return TYPE_UNKNOWN | pipe->get(buf, unkn);
mazgch 21:c4d64830bf02 2241 if (ln > 0)
mazgch 21:c4d64830bf02 2242 return lutF[i].type | pipe->get(buf, ln);
mazgch 21:c4d64830bf02 2243 }
mazgch 21:c4d64830bf02 2244 for (int i = 0; i < sizeof(lut)/sizeof(*lut); i ++) {
mazgch 21:c4d64830bf02 2245 pipe->set(unkn);
mazgch 21:c4d64830bf02 2246 int ln = _parseMatch(pipe, len, lut[i].sta, lut[i].end);
mazgch 21:c4d64830bf02 2247 if (ln == WAIT && fr)
mazgch 21:c4d64830bf02 2248 return WAIT;
mazgch 21:c4d64830bf02 2249 if ((ln != NOT_FOUND) && (unkn > 0))
mazgch 31:a0bed6c1e05d 2250 return TYPE_UNKNOWN | pipe->get(buf, unkn);
mazgch 21:c4d64830bf02 2251 if (ln > 0)
mazgch 21:c4d64830bf02 2252 return lut[i].type | pipe->get(buf, ln);
mazgch 21:c4d64830bf02 2253 }
mazgch 21:c4d64830bf02 2254 // UNKNOWN
mazgch 21:c4d64830bf02 2255 unkn ++;
mazgch 21:c4d64830bf02 2256 len--;
mazgch 21:c4d64830bf02 2257 }
mazgch 18:e5697801df29 2258 return WAIT;
mazgch 18:e5697801df29 2259 }
mazgch 18:e5697801df29 2260
mazgch 18:e5697801df29 2261 // ----------------------------------------------------------------
mazgch 18:e5697801df29 2262 // Serial Implementation
mazgch 18:e5697801df29 2263 // ----------------------------------------------------------------
mazgch 18:e5697801df29 2264
mazgch 76:f7c3dd568dae 2265 /*! Helper Dev Null Device
mazgch 76:f7c3dd568dae 2266 Small helper class used to shut off stderr/stdout. Sometimes stdin/stdout
mazgch 76:f7c3dd568dae 2267 is shared with the serial port of the modem. Having printfs inbetween the
mazgch 76:f7c3dd568dae 2268 AT commands you cause a failure of the modem.
mazgch 76:f7c3dd568dae 2269 */
mazgch 76:f7c3dd568dae 2270 class DevNull : public Stream {
mazgch 76:f7c3dd568dae 2271 public:
mazgch 76:f7c3dd568dae 2272 DevNull() : Stream(_name+1) { } //!< Constructor
mazgch 76:f7c3dd568dae 2273 void claim(const char* mode, FILE* file)
mazgch 76:f7c3dd568dae 2274 { freopen(_name, mode, file); } //!< claim a stream
mazgch 76:f7c3dd568dae 2275 protected:
mazgch 76:f7c3dd568dae 2276 virtual int _getc() { return EOF; } //!< Nothing
mazgch 76:f7c3dd568dae 2277 virtual int _putc(int c) { return c; } //!< Discard
mazgch 76:f7c3dd568dae 2278 static const char* _name; //!< File name
mazgch 76:f7c3dd568dae 2279 };
mazgch 76:f7c3dd568dae 2280 const char* DevNull::_name = "/null"; //!< the null device name
mazgch 76:f7c3dd568dae 2281 static DevNull null; //!< the null device
mazgch 76:f7c3dd568dae 2282
mazgch 19:2b5d097ca15d 2283 MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/,
mazgch 19:2b5d097ca15d 2284 int baudrate /*= MDMBAUD*/,
mazgch 43:a89a7a505991 2285 #if DEVICE_SERIAL_FC
mazgch 19:2b5d097ca15d 2286 PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/,
mazgch 43:a89a7a505991 2287 #endif
mazgch 18:e5697801df29 2288 int rxSize /*= 256*/, int txSize /*= 128*/) :
mazgch 35:9275215a3a5b 2289 SerialPipe(tx, rx, rxSize, txSize)
mazgch 18:e5697801df29 2290 {
mazgch 76:f7c3dd568dae 2291 if (rx == USBRX)
mazgch 76:f7c3dd568dae 2292 null.claim("r", stdin);
mazgch 76:f7c3dd568dae 2293 if (tx == USBTX) {
mazgch 76:f7c3dd568dae 2294 null.claim("w", stdout);
mazgch 76:f7c3dd568dae 2295 null.claim("w", stderr);
mazgch 74:208e3e32d263 2296 #ifdef MDM_DEBUG
mazgch 76:f7c3dd568dae 2297 _debugLevel = -1;
mazgch 76:f7c3dd568dae 2298 #endif
mazgch 76:f7c3dd568dae 2299 }
mazgch 74:208e3e32d263 2300 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 2301 _onboard = (tx == MDMTXD) && (rx == MDMRXD);
mazgch 74:208e3e32d263 2302 if (_onboard)
mazgch 74:208e3e32d263 2303 c027_mdm_powerOn(false);
mazgch 74:208e3e32d263 2304 #endif
mazgch 18:e5697801df29 2305 baud(baudrate);
mazgch 35:9275215a3a5b 2306 #if DEVICE_SERIAL_FC
mazgch 35:9275215a3a5b 2307 if ((rts != NC) || (cts != NC))
mazgch 35:9275215a3a5b 2308 {
mazgch 35:9275215a3a5b 2309 Flow flow = (cts == NC) ? RTS :
mazgch 35:9275215a3a5b 2310 (rts == NC) ? CTS : RTSCTS ;
mazgch 35:9275215a3a5b 2311 set_flow_control(flow, rts, cts);
mazgch 35:9275215a3a5b 2312 if (cts != NC) _dev.lpm = LPM_ENABLED;
mazgch 35:9275215a3a5b 2313 }
mazgch 35:9275215a3a5b 2314 #endif
mazgch 18:e5697801df29 2315 }
mazgch 18:e5697801df29 2316
mazgch 76:f7c3dd568dae 2317 MDMSerial::~MDMSerial(void)
mazgch 76:f7c3dd568dae 2318 {
mazgch 76:f7c3dd568dae 2319 powerOff();
mazgch 76:f7c3dd568dae 2320 #ifdef TARGET_UBLOX_C027
mazgch 76:f7c3dd568dae 2321 if (_onboard)
mazgch 76:f7c3dd568dae 2322 c027_mdm_powerOff();
mazgch 76:f7c3dd568dae 2323 #endif
mazgch 76:f7c3dd568dae 2324 }
mazgch 76:f7c3dd568dae 2325
mazgch 18:e5697801df29 2326 int MDMSerial::_send(const void* buf, int len)
mazgch 18:e5697801df29 2327 {
mazgch 35:9275215a3a5b 2328 return put((const char*)buf, len, true/*=blocking*/);
mazgch 18:e5697801df29 2329 }
mazgch 18:e5697801df29 2330
mazgch 18:e5697801df29 2331 int MDMSerial::getLine(char* buffer, int length)
mazgch 18:e5697801df29 2332 {
mazgch 18:e5697801df29 2333 return _getLine(&_pipeRx, buffer, length);
mazgch 18:e5697801df29 2334 }
mazgch 18:e5697801df29 2335
mazgch 18:e5697801df29 2336 // ----------------------------------------------------------------
mazgch 18:e5697801df29 2337 // USB Implementation
mazgch 18:e5697801df29 2338 // ----------------------------------------------------------------
mazgch 18:e5697801df29 2339
mazgch 18:e5697801df29 2340 #ifdef HAVE_MDMUSB
mazgch 76:f7c3dd568dae 2341 MDMUsb::MDMUsb(void)
mazgch 74:208e3e32d263 2342 {
mazgch 74:208e3e32d263 2343 #ifdef MDM_DEBUG
mazgch 74:208e3e32d263 2344 _debugLevel = 1;
mazgch 74:208e3e32d263 2345 #endif
mazgch 74:208e3e32d263 2346 #ifdef TARGET_UBLOX_C027
mazgch 74:208e3e32d263 2347 _onboard = true;
mazgch 74:208e3e32d263 2348 c027_mdm_powerOn(true);
mazgch 74:208e3e32d263 2349 #endif
mazgch 74:208e3e32d263 2350 }
mazgch 76:f7c3dd568dae 2351
mazgch 76:f7c3dd568dae 2352 MDMUsb::~MDMUsb(void)
mazgch 76:f7c3dd568dae 2353 {
mazgch 76:f7c3dd568dae 2354 powerOff();
mazgch 76:f7c3dd568dae 2355 #ifdef TARGET_UBLOX_C027
mazgch 76:f7c3dd568dae 2356 if (_onboard)
mazgch 76:f7c3dd568dae 2357 c027_mdm_powerOff();
mazgch 76:f7c3dd568dae 2358 #endif
mazgch 76:f7c3dd568dae 2359 }
mazgch 76:f7c3dd568dae 2360
mazgch 76:f7c3dd568dae 2361 int MDMUsb::_send(const void* buf, int len) { return 0; }
mazgch 76:f7c3dd568dae 2362
mazgch 18:e5697801df29 2363 int MDMUsb::getLine(char* buffer, int length) { return NOT_FOUND; }
mazgch 76:f7c3dd568dae 2364
Christopher Haster 140:b5614db52fc4 2365 #endif