u-blox / CNManager

Dependents:   C027_demo_ConnMan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CNReg.cpp Source File

CNReg.cpp

00001 #include "CNReg.h"
00002 #include "CNLib.h"
00003 
00004 #define ISREG(r)        ((r == MDMParser::REG_HOME) || (r == MDMParser::REG_ROAMING))
00005 #define ISROMAING(r)    (r == MDMParser::REG_ROAMING)
00006 #define ISUNKKOWN(r)    (r == MDMParser::REG_UNKNOWN)
00007 
00008 static MDMParser::Reg stateCS;      //!< CS registration status
00009 static MDMParser::Reg statePS;      //!< PS registration status
00010 static CNTimer tmStatusPull;       //!< timer for polling registration status
00011 static bool isRomaingEnabled;       //!< roaming is allowed
00012 
00013 void cnRegInit(){
00014     TRACE("%s enter \r\n", __FUNCTION__);
00015     stateCS = MDMParser::REG_UNKNOWN;
00016     statePS = MDMParser::REG_UNKNOWN;
00017     tmStatusPull.setExpireTimeout(TIMEOUT_POLLING_NETWORK_STATUS);
00018     tmStatusPull.start();
00019 }
00020 
00021 void cnRegSetRoaming(bool enabled){
00022     isRomaingEnabled = enabled;
00023 }
00024 
00025 void cnRegReset(){
00026     TRACE("%s enter \r\n", __FUNCTION__);
00027     stateCS = MDMParser::REG_UNKNOWN;
00028     statePS = MDMParser::REG_UNKNOWN;
00029 }
00030 
00031 CNResp cnRegLoop(CNLib* cnLib, RegStatus* regStatus){
00032     MDMParser::Reg newStateCS = cnLib->getNet()->csd;
00033     MDMParser::Reg newStatePS = cnLib->getNet()->psd;
00034     bool isRoamingRegister, isCSRegistered, hasCsRegister, hasCsDeRegister, isPSRegistered, hasPsRegister, hasPsDeRegister;
00035     *regStatus = REG_NO_CHANGES;
00036     int res;
00037     
00038     TRACE("%s enter \r\n", __FUNCTION__); 
00039     //read and set COPS if necessary
00040     if (cnLib->getNet()->regStatus == MDMParser::COPS_UNKOWN){
00041         res = cnLib->getNetworkInfo();
00042         if (!RESPOK(res)) return RES_ERROR;
00043     }
00044     if (cnLib->getNet()->regStatus != MDMParser::COPS_AUTOMATIC_REG)
00045         cnLib->setNetAutoReg();    
00046     // poll registration info when timer expires
00047     if (tmStatusPull.isExpired()){
00048         INFO("%s Polling Net Info timer Expired, checking net status\r\n", __FUNCTION__);
00049         cnLib->checkNetStatus();
00050          newStateCS = cnLib->getNet()->csd;
00051          newStatePS = cnLib->getNet()->psd;
00052         if (!tmStatusPull.isOn())
00053             tmStatusPull.start();
00054     }
00055     //recover an old value in case of read errors
00056     if (ISUNKKOWN(newStateCS)) newStateCS = stateCS;
00057     if (ISUNKKOWN(newStatePS)) newStatePS = statePS;        
00058     //process registration changes
00059     isRoamingRegister = ISROMAING(stateCS) || ISROMAING(statePS);
00060     isCSRegistered = ISREG(stateCS);
00061     hasCsRegister = (!isCSRegistered && ISREG(newStateCS));
00062     hasCsDeRegister = ( isCSRegistered && !ISREG(newStateCS));
00063     isPSRegistered = ISREG(statePS);
00064     hasPsRegister = ( !isPSRegistered && ISREG(newStatePS));
00065     hasPsDeRegister = ( isPSRegistered && !ISREG(newStatePS));
00066     //verify roaming
00067     if (!isRoamingRegister || (isRoamingRegister && isRomaingEnabled)){
00068         //verify registration
00069         if ((isCSRegistered && hasPsRegister) || (isPSRegistered &&  hasCsRegister) || \
00070                 (hasPsRegister && hasCsRegister) ){
00071             TRACE("%s CS and PS have just registered\r\n", __FUNCTION__);
00072             *regStatus = REG_REGISTERED;
00073         }
00074         else if (hasPsDeRegister || hasCsDeRegister){
00075             TRACE("%s CS or PS are not registered\r\n", __FUNCTION__);
00076             *regStatus = REG_NOT_REGISTERED;
00077         }
00078     }else{
00079         INFO("%s Roaming not allowed\r\n", __FUNCTION__);
00080         *regStatus = REG_NOT_REGISTERED;
00081     }
00082     //Perform action in base of registration status
00083     if (hasCsRegister){
00084         //cnLib->getNetMccMnc();
00085     } else if (hasCsDeRegister){
00086         //cnLib->getNet()->netMccMnc = 0;
00087         memset(cnLib->getNet()->opr, 0, strlen(cnLib->getNet()->opr));
00088     }
00089     //Dump info
00090     if (getUtilDebugLevel() > 1 && (newStatePS != statePS || newStateCS != stateCS )){
00091         const char* txtStatus[] = { "unchanged", "registered", "NOT registered"};
00092         INFO("cn registration Dump\r\n");
00093         if (*regStatus < sizeof(txtStatus)/sizeof(*txtStatus))
00094                 INFO("  Status is now %s\r\n", txtStatus[*regStatus]);
00095         INFO("  has CS Registered = %s, has CS DeRegistered = %s\r\n", BOOLTOSTR(hasCsRegister), BOOLTOSTR(hasCsDeRegister));
00096         INFO("  is Roaming active = %s, polling Timer %d\r\n", \
00097                 BOOLTOSTR(isRoamingRegister), tmStatusPull.read());
00098         //cnLib->dumpNetStatus(cnLib->getNet());
00099     }
00100     //update status
00101     stateCS = newStateCS;
00102     statePS = newStatePS;
00103     
00104     return RES_OK;    
00105 }