uitgy

Dependents:   App_Pese_Ruche_SYSBEE

Fork of C027_Support by u-blox

Committer:
maxelior
Date:
Wed Apr 11 08:43:42 2018 +0000
Revision:
140:8ad0ff04c73c
Parent:
137:6a7a5c4f35f6
version fonctionnelle sans fonction essaimage et gps

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