amq amq / CNManager

Fork of CNManager by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CNLib.cpp Source File

CNLib.cpp

00001 #include "CNLib.h"
00002 #include "CNUtil.h"
00003 #include "CNManager.h"
00004 
00005 #define PROFILE  "0"
00006 
00007 int  CNLib::powerOnModem(){
00008     TRACE("%s enter \r\n", __FUNCTION__);
00009     int res =  RESP_OK;
00010     PinName pn MDM_IF( = MDMPWRON, = D4);
00011 
00012 #ifdef TARGET_UBLOX_C027
00013     int i = 20;
00014     if (pn != NC) {
00015         INFO("Modem::wakeup\r\n");
00016         DigitalOut pin(pn, 1);
00017         while (i--) {
00018             // SARA-U2/LISA-U2 50..80us
00019             pin = 0; ::wait_us(50);
00020             pin = 1; ::wait_ms(10);
00021 
00022             // SARA-G35 >5ms, LISA-C2 > 150ms, LEON-G2 >5ms
00023             pin = 0; ::wait_ms(150);
00024             pin = 1; ::wait_ms(100);
00025 
00026             // purge any messages
00027             purge();
00028 
00029             // check interface
00030             sendFormated("AT\r\n");
00031             res = waitFinalResp(NULL,NULL,1000);
00032 
00033             if (RESPOK(res))
00034                 break;
00035         }
00036         if (i < 0) {
00037             ERROR("No Reply from Modem\r\n");
00038             goto failure;
00039         }
00040     }
00041 #endif
00042 
00043     TRACE("%s exit Ok \r\n", __FUNCTION__);
00044     return res;
00045 
00046 failure:
00047     ERROR("%s exit Error code %d\r\n", __FUNCTION__, res);
00048     return res;
00049 }
00050 
00051 int CNLib::initModem()
00052 {
00053     int res;
00054     TRACE("CNLib::%s enter \r\n", __FUNCTION__);
00055 
00056     // echo off
00057     sendFormated("AT E0\r\n");
00058     res = waitFinalResp();
00059     if (!RESPOK(res))
00060         goto failure;
00061     // enable verbose error messages
00062     sendFormated("AT+CMEE=2\r\n");
00063     res = waitFinalResp();
00064     if (!RESPOK(res))
00065         goto failure;
00066     // set baud rate
00067     sendFormated("AT+IPR=115200\r\n");
00068     res = waitFinalResp();
00069     if (!RESPOK(res))
00070         goto failure;
00071 
00072     // wait some time until baudrate is applied
00073     wait_ms(200); // SARA-G > 40ms
00074 
00075     // get the manufacturer
00076     sendFormated("AT+CGMI\r\n");
00077     res = waitFinalResp(_cbString, _dev.manu);
00078     if (!RESPOK(res))
00079         goto failure;
00080     // get the model identification
00081     sendFormated("AT+CGMM\r\n");
00082     res =  waitFinalResp(_cbString, _dev.model);
00083     if (!RESPOK(res))
00084        goto failure;
00085     // get the
00086     sendFormated("AT+CGMR\r\n");
00087     res =  waitFinalResp(_cbString, _dev.ver);
00088     if (!RESPOK(res))
00089        goto failure;
00090     // Returns the product serial number, IMEI (International Mobile Equipment Identity)
00091     sendFormated("AT+CGSN\r\n");
00092     res =  waitFinalResp(_cbString, _dev.imei);
00093     if (!RESPOK(res))
00094         goto failure;
00095     // enable power saving
00096     if (_dev.lpm != LPM_DISABLED) {
00097          // enable power saving (requires flow control, cts at least)
00098         sendFormated("AT+UPSV=1\r\n");
00099         res = waitFinalResp();
00100         if (!RESPOK(res))
00101             goto failure;
00102        _dev.lpm = LPM_ACTIVE;
00103     }
00104     // identify the module
00105     sendFormated("ATI\r\n");
00106     res = waitFinalResp(_cbATI, &_dev.dev );
00107     if (!RESPOK(res))
00108         goto failure;
00109     if (_dev.dev == DEV_UNKNOWN){
00110         ERROR("CNLib::%s UNKOWN device \r\n", __FUNCTION__);
00111         goto failure;
00112     }
00113     if ((_dev.dev == DEV_LISA_U2) || (_dev.dev == DEV_LEON_G2)) {
00114        // enable the network identification feature
00115      sendFormated("AT+UGPIOC=20,2\r\n");
00116        res = waitFinalResp(NULL,NULL,20000);
00117        if (!RESPOK(res))
00118            goto failure;
00119        }
00120    else if ((_dev.dev == DEV_SARA_U2) || (_dev.dev == DEV_SARA_U2) || (_dev.dev == DEV_SARA_G35)) {
00121        // enable the network identification feature
00122     sendFormated("AT+UGPIOC=16,2\r\n");
00123        res = waitFinalResp(NULL,NULL,20000);
00124        if (!RESPOK(res))
00125            goto failure;
00126        }
00127     //enable rat technology unsolicited
00128     sendFormated("AT+UREG=1\r\n");
00129     res = waitFinalResp();
00130     if (!RESPOK(res))
00131      goto failure;
00132     // enable the psd registration unsolicited result code
00133     sendFormated("AT+CGREG=2\r\n");
00134     res = waitFinalResp();
00135     if (!RESPOK(res))
00136         goto failure;
00137     // enable the network registration unsolicited result code
00138     sendFormated("AT+CREG=2\r\n");
00139     res = waitFinalResp();
00140     if (!RESPOK(res))
00141      goto failure;
00142     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00143     return res;
00144 
00145 failure:
00146     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00147     return res;
00148 }
00149 
00150 /*************************************************************************/
00151 
00152 int CNLib::_mcbCPIN(int type,  const char* buf, int len, Sim* sim)
00153 {
00154     int ret = WAIT;
00155     if (sim) {
00156         if (type == TYPE_PLUS){
00157             char s[16];
00158             if (sscanf(buf, "\r\n+CPIN: %[^\r]\r\n", s) >= 1){
00159                 if (!strcmp("READY", s)){
00160                     *sim = SIM_READY;
00161                 }
00162                 else if (!strcmp("SIM PIN", s))
00163                     *sim = SIM_PIN;
00164                 else if (!strcmp("SIM PUK", s))
00165                      *sim = SIM_PUK;
00166                      
00167                      
00168             }
00169         } else if (type == TYPE_ERROR_CME) {
00170                 if (strstr(buf, "+CME ERROR: SIM not inserted")){
00171                      *sim = SIM_MISSING;
00172                      ret = RESP_OK;
00173                 }
00174                 else if (strstr(buf, "+CME ERROR: incorrect password\r\n")){
00175                     *sim = WRONG_PIN;
00176                      ret = RESP_OK;
00177                 }
00178                 else{
00179                     *sim = SIM_UNKNOWN;
00180                      ret = RESP_OK;
00181                 }
00182         }
00183     }
00184     return ret;
00185 }
00186 
00187 int CNLib::simInit(const char* simpin)
00188 {
00189     int res;
00190     TRACE("CNLib::%s Enter \r\n", __FUNCTION__);
00191     if (simpin == NULL){
00192         sendFormated("AT+CPIN?\r\n");
00193     }
00194     else if (simpin != NULL && *simpin != NULL)
00195         sendFormated("AT+CPIN=\"%s\"\r\n", simpin);
00196     res = waitFinalResp(_mcbCPIN, &_dev.sim, 20000);
00197     if (!RESPOK(res))
00198         goto failure;
00199 
00200     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00201     return res;
00202 
00203 failure:
00204     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00205     return res;
00206  }
00207 
00208 
00209 int CNLib::getSimInfo(){
00210     int res;
00211     // Returns the ICCID (Integrated Circuit Card ID) of the SIM-card.
00212     // ICCID is a serial number identifying the SIM.
00213     sendFormated("AT+CCID\r\n");
00214     res = waitFinalResp(_cbCCID, _dev.ccid);
00215     if (res != RESP_OK)
00216         goto failure;
00217     // Request IMSI (International Mobile Subscriber Identification)
00218     sendFormated("AT+CIMI\r\n");
00219     res = waitFinalResp(_cbString, _dev.imsi);
00220     if (RESP_OK != res)
00221         goto failure;
00222     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00223     return res;
00224 
00225 failure:
00226     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00227     return res;
00228 }
00229 
00230 /*****************************************************************************/
00231 
00232 int CNLib::getNetworkInfo()
00233 {
00234     int res;
00235     TRACE("CNLib::%s Enter \r\n", __FUNCTION__);
00236     sendFormated("AT+COPS?\r\n");
00237     res = waitFinalResp(_cbCOPS, &_net);
00238     if (!RESPOK(res))
00239        goto failure;
00240     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00241     return res;
00242 
00243 failure:
00244     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00245     return res;
00246 }
00247 
00248 int CNLib::setNetAutoReg()
00249 {
00250     int res;
00251     TRACE("CNLib::%s Enter \r\n", __FUNCTION__);
00252     sendFormated("AT+COPS=0\r\n");
00253     res = waitFinalResp();
00254     if (!RESPOK(res))
00255        goto failure;
00256     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00257     return res;
00258 
00259 failure:
00260     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00261     return res;
00262 }
00263 
00264 /****************************************************************/
00265 
00266 int CNLib::getInternalStatusContext()
00267 {
00268     int res;
00269     TRACE("CNLib::%s Enter \r\n", __FUNCTION__);
00270 
00271     int context;
00272     sendFormated("AT+UPSND=" PROFILE ",8\r\n");
00273     res = waitFinalResp(_cbUPSND, &context);
00274     if (!RESPOK(res))
00275         goto failure;
00276     if (context == 0)
00277         _ip = NOIP;
00278     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00279     return res;
00280 
00281 failure:
00282   TRACE("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00283   _ip = NOIP;
00284   return res;
00285 }
00286 
00287 int CNLib::disableInternalContext()
00288 {
00289     int res;
00290     TRACE("CNLib::%s Enter \r\n", __FUNCTION__);
00291     _ip = NOIP;
00292     sendFormated("AT+UPSDA=" PROFILE ",4\r\n");
00293     res = waitFinalResp(NULL,NULL,40*1000);
00294     if (!RESPOK(res))
00295            goto failure;
00296     TRACE("CNLib::%s exit Ok \r\n", __FUNCTION__);
00297     return res;
00298 
00299 failure:
00300     ERROR("CNLib::%s exit Error code %d\r\n", __FUNCTION__, res);
00301     return res;
00302 }