support library for C027 helper functions for Buffer Pipes, Buffered Serial Port (rtos capable) and GPS parsing. It includes modem APIs for USSD, SMS and Sockets.
Dependents: HTTPClient_Cellular_HelloWorld Cellular_HelloMQTT MbedSmartRestMain Car_Bon_car_module ... more
This library is intended to be used with u-blox products such as the C027 or a shield with u-blox cellular and GPS modules like the cellular and positioning shield from Embedded Artist.
For 2G/GSM and 3G/UMTS you need to:
- have a SIM card and know its PIN number
- need to know you network operators APN setting These setting should be passed to the connect or init and join functions. You can also extend the APN database in MDMAPN.h.
For CDMA products you need to make sure that you have provisioned and activated the modem with either Sprint or Verizon.
Revision 26:07be5faf8925, committed 2014-04-08
- Comitter:
- mazgch
- Date:
- Tue Apr 08 13:52:07 2014 +0000
- Parent:
- 25:4045d02e44f1
- Child:
- 27:e35f2118368f
- Commit message:
- extract more device data from modem, use templates to simplify type checking
Changed in this revision
MDM.cpp | Show annotated file Show diff for this revision Revisions of this file |
MDM.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MDM.cpp Tue Apr 08 11:59:28 2014 +0000 +++ b/MDM.cpp Tue Apr 08 13:52:07 2014 +0000 @@ -3,7 +3,7 @@ #include "MDM.h" #define TRACE (0)?:printf -//#define DEBUG +#define DEBUG #define PROFILE "0" // this is the psd profile used #define MAX_SIZE 256 // max expected messages // some helper @@ -27,14 +27,21 @@ MDMParser::MDMParser(void) { + + // device info _model = MODEL_UNKNOWN; _sim = SIM_UNKNOWN; + *_ccid = '\0'; + *_imsi = '\0'; + *_imei = '\0'; + // network info _net = NET_UNKNOWN; _act = ACT_UNKNOWN; - _ip = 0; _rssi = 0; *_num = '\0'; *_opr = '\0'; + // data network info + _ip = 0; for (int socket = 0; socket < sizeof(_sockets)/sizeof(*_sockets); socket++) { _sockets[socket].state = SOCK_FREE; _sockets[socket].pending = 0; @@ -60,7 +67,10 @@ return send(buf, len); } -int MDMParser::waitFinalResp(_CB cb /*= NULL*/, void* param /*= NULL*/, int timeout_ms /*= 5000*/) { +int MDMParser::waitFinalResp(_CALLBACKPTR cb /* = NULL*/, + void* param /* = NULL*/, + int timeout_ms /*= 5000*/) +{ char buf[MAX_SIZE]; Timer timer; timer.start(); @@ -187,10 +197,13 @@ if ((type == TYPE_UNKNOWN) && model) { if (strstr(buf, "SARA-G350")) { *model = MODEL_SARA_G350; + TRACE("Identified Model: SARA-G350 2G\n"); } else if (strstr(buf, "LISA-U200")) { *model = MODEL_LISA_U200; + TRACE("Identified Model: LISA-U200 2G/3G\n"); } else if (strstr(buf, "LISA-C200")) { *model= MODEL_LISA_C200; + TRACE("Identified Model: LISA-C200 CDMA\n"); } } return WAIT; @@ -217,23 +230,27 @@ if (OK != waitFinalResp()) return false; wait_ms(40); - // disable flow control - sendFormated("AT&K0\r\n"); - if (OK != waitFinalResp()) - return false; // identify the module sendFormated("ATI\r\n"); - if (OK != waitFinalResp((_CB)_cbATI, &_model)) + if (OK != waitFinalResp(_cbATI, &_model)) return false; if (_model == MODEL_UNKNOWN) return false; // model specific init if (_model == MODEL_LISA_C200) { + // disable flow control + sendFormated("AT+IFC=0,0\r\n"); + if (OK != waitFinalResp()) + return false; // Return the pseudo ESN or MEID sendFormated("AT+GSN\r\n"); + if (OK != waitFinalResp(_cbGSN, _imei)) + return false; + } else { + // disable flow control + sendFormated("AT&K0\r\n"); if (OK != waitFinalResp()) return false; - } else { // enable power saving sendFormated("AT+UPSV=1\r\n"); if (OK != waitFinalResp()) @@ -269,11 +286,11 @@ // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card. // ICCID is a serial number identifying the SIM. sendFormated("AT+CCID\r\n"); - if (OK != waitFinalResp()) + if (OK != waitFinalResp(_cbCCID, _ccid)) return false; // Returns the product serial number, IMEI (International Mobile Equipment Identity) sendFormated("AT+CGSN\r\n"); - if (OK != waitFinalResp()) + if (OK != waitFinalResp(_cbCGSN, _imei)) return false; // Setup SMS in text mode sendFormated("AT+CMGF=1\r\n"); @@ -287,11 +304,47 @@ } // Request IMSI (International Mobile Subscriber Identification) sendFormated("AT+CIMI\r\n"); - if (OK != waitFinalResp()) + if (OK != waitFinalResp(_cbCIMI, _imsi)) return false; return true; } +int MDMParser::_cbCCID(int type, const char* buf, int len, char* ccid) +{ + if ((type == TYPE_PLUS) && ccid){ + if (sscanf(buf, "\r\n+CCID: %[^\r]\r\n", ccid) == 1) + TRACE("Got CCID: %s\n", ccid); + } + return WAIT; +} + +int MDMParser::_cbGSN(int type, const char* buf, int len, char* imei) +{ + if ((type == TYPE_UNKNOWN) && imei){ + if (sscanf(buf, "\r\n%[^\r]\r\n%*[^\r]\r\n", imei) == 1) + TRACE("Got IMEI: %s\n", imei); + } + return WAIT; +} + +int MDMParser::_cbCGSN(int type, const char* buf, int len, char* imei) +{ + if ((type == TYPE_UNKNOWN) && imei){ + if (sscanf(buf, "\r\n%[^\r]\r\n", imei) == 1) + TRACE("Got IMEI: %s\n", imei); + } + return WAIT; +} + +int MDMParser::_cbCIMI(int type, const char* buf, int len, char* imsi) +{ + if ((type == TYPE_UNKNOWN) && imsi) { + if (sscanf(buf, "\r\n%[^\r]\r\n", imsi) == 1) + TRACE("Got IMSI: %s\n", imsi); + } + return WAIT; +} + bool MDMParser::checkNetStatus(Status* info /*= NULL*/) { // check registration @@ -450,7 +503,7 @@ return true; } sendFormated("AT+UDNSRN=0,\"%s\"\r\n", host); - if (OK != waitFinalResp((_CB)_cbUDNSRN, ip)) + if (OK != waitFinalResp(_cbUDNSRN, ip)) return false; return *ip != 0; } @@ -480,7 +533,7 @@ } sendFormated(cmd); int socket = -1; - if (OK != waitFinalResp((_CB)_cbUSOCR, &socket)) + if (OK != waitFinalResp(_cbUSOCR, &socket)) return SOCKET_ERROR; if (!ISSOCKET(socket) || (_sockets[socket].state != SOCK_FREE)) return SOCKET_ERROR; @@ -590,7 +643,7 @@ if (len < blk) blk = len; if (blk) { sendFormated("AT+USORD=%d,%d\r\n",socket, blk); - if (OK != waitFinalResp((_CB)_cbUSORD, buf)) { + if (OK != waitFinalResp(_cbUSORD, buf)) { return cnt ? cnt : SOCKET_ERROR; } len -= blk; @@ -637,7 +690,7 @@ sendFormated("AT+USORF=%d,%d\r\n",socket, blk); USORFparam param; param.buf = buf; - if (OK != waitFinalResp((_CB)_cbUSORF, ¶m)) { + if (OK != waitFinalResp(_cbUSORF, ¶m)) { return cnt ? cnt : SOCKET_ERROR; } *ip = param.ip; @@ -665,7 +718,7 @@ { int num = 0; sendFormated("AT+CMGL=ALL\r\n"); - if (OK != waitFinalResp((_CB)_cbCMGL,&num)) { + if (OK != waitFinalResp(_cbCMGL,&num)) { return false; } return true; @@ -716,7 +769,7 @@ param.num = num; param.buf = buf; sendFormated("AT+CMGR=%d\r\n",ix); - if (OK != waitFinalResp((_CB)_cbCMGR, ¶m)) { + if (OK != waitFinalResp(_cbCMGR, ¶m)) { return false; } return true; @@ -738,13 +791,12 @@ { *buf = '\0'; sendFormated("AT+CUSD=1,\"%s\"\r\n",cmd); - if (OK != waitFinalResp((_CB)_cbCUSD, buf)) { + if (OK != waitFinalResp(_cbCUSD, buf)) { return -1; } return strlen(buf); } - - + // ---------------------------------------------------------------- int MDMParser::_parseMatch(Pipe<char>* pipe, int len, const char* sta, const char* end) {
--- a/MDM.h Tue Apr 08 11:59:28 2014 +0000 +++ b/MDM.h Tue Apr 08 13:52:07 2014 +0000 @@ -52,9 +52,18 @@ virtual int getLine(char* buf, int len) = 0; virtual int send(const char* buf, int len); int sendFormated(const char* format, ...); - typedef int (*_CB)(int type, const char* buf, int len, void* param); - int waitFinalResp(_CB cb = NULL, void* param = NULL, int timeout_ms = 5000); - + + typedef int (*_CALLBACKPTR)(int type, const char* buf, int len, void* param); + int waitFinalResp(_CALLBACKPTR = NULL, + void* param = NULL, + int timeout_ms = 5000); + template<class T> + int waitFinalResp(int (*cb)(int type, const char* buf, int len, + T* param), + T* param, int timeout_ms = 5000) + { + return waitFinalResp((_CALLBACKPTR)cb, (void*)param, timeout_ms); + } // network bool init(const char* pin = NULL); bool checkNetStatus(Status* info = NULL); @@ -89,6 +98,7 @@ typedef enum { MODEL_UNKNOWN, MODEL_SARA_G350, MODEL_LISA_U200, MODEL_LISA_C200 } Model; typedef enum { SIM_UNKNOWN, SIM_PIN, SIM_READY } Sim; static int _cbATI(int type, const char* buf, int len, Model* model); + static int _cbCIMI(int type, const char* buf, int len, char* imsi); static int _cbUDNSRN(int type, const char* buf, int len, IP* ip); static int _cbUSOCR(int type, const char* buf, int len, int* socket); static int _cbUSORD(int type, const char* buf, int len, char* out); @@ -97,6 +107,9 @@ typedef struct { char* buf; char* num; } CMGRparam; static int _cbCUSD(int type, const char* buf, int len, char* buf); static int _cbCMGR(int type, const char* buf, int len, CMGRparam* param); + static int _cbCGSN(int type, const char* buf, int len, char* imei); + static int _cbGSN(int type, const char* buf, int len, char* imei); + static int _cbCCID(int type, const char* buf, int len, char* ccid); static IP strToIp(const char* str); IP _ip; Model _model; @@ -106,6 +119,9 @@ char _num[32]; char _opr[32]; int _rssi; + char _imsi[32]; + char _imei[32]; + char _ccid[32]; private: typedef enum { SOCK_FREE, SOCK_CREATED, SOCK_CONNECTED } SockState; typedef struct { SockState state; int pending; } SockCtrl;