Connection Manager library for u-blox cellular modules. It manages the modem for keeping data connection always active.
CNReg.cpp@1:29ad1d1ac1f9, 2016-01-21 (annotated)
- Committer:
- msinig
- Date:
- Thu Jan 21 14:00:25 2016 +0000
- Revision:
- 1:29ad1d1ac1f9
- Parent:
- 0:86284a262735
fix bugs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
msinig | 0:86284a262735 | 1 | #include "CNReg.h" |
msinig | 0:86284a262735 | 2 | #include "CNLib.h" |
msinig | 0:86284a262735 | 3 | |
msinig | 0:86284a262735 | 4 | #define ISREG(r) ((r == MDMParser::REG_HOME) || (r == MDMParser::REG_ROAMING)) |
msinig | 0:86284a262735 | 5 | #define ISROMAING(r) (r == MDMParser::REG_ROAMING) |
msinig | 0:86284a262735 | 6 | #define ISUNKKOWN(r) (r == MDMParser::REG_UNKNOWN) |
msinig | 0:86284a262735 | 7 | |
msinig | 0:86284a262735 | 8 | static MDMParser::Reg stateCS; //!< CS registration status |
msinig | 0:86284a262735 | 9 | static MDMParser::Reg statePS; //!< PS registration status |
msinig | 0:86284a262735 | 10 | static CNTimer tmStatusPull; //!< timer for polling registration status |
msinig | 0:86284a262735 | 11 | static bool isRomaingEnabled; //!< roaming is allowed |
msinig | 0:86284a262735 | 12 | |
msinig | 0:86284a262735 | 13 | void cnRegInit(){ |
msinig | 0:86284a262735 | 14 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 15 | stateCS = MDMParser::REG_UNKNOWN; |
msinig | 0:86284a262735 | 16 | statePS = MDMParser::REG_UNKNOWN; |
msinig | 0:86284a262735 | 17 | tmStatusPull.setExpireTimeout(TIMEOUT_POLLING_NETWORK_STATUS); |
msinig | 0:86284a262735 | 18 | tmStatusPull.start(); |
msinig | 0:86284a262735 | 19 | } |
msinig | 0:86284a262735 | 20 | |
msinig | 0:86284a262735 | 21 | void cnRegSetRoaming(bool enabled){ |
msinig | 0:86284a262735 | 22 | isRomaingEnabled = enabled; |
msinig | 0:86284a262735 | 23 | } |
msinig | 0:86284a262735 | 24 | |
msinig | 0:86284a262735 | 25 | void cnRegReset(){ |
msinig | 0:86284a262735 | 26 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 27 | stateCS = MDMParser::REG_UNKNOWN; |
msinig | 0:86284a262735 | 28 | statePS = MDMParser::REG_UNKNOWN; |
msinig | 0:86284a262735 | 29 | } |
msinig | 0:86284a262735 | 30 | |
msinig | 1:29ad1d1ac1f9 | 31 | CNResp cnRegLoop(CNLib* cnLib, RegStatus* regStatus){ |
msinig | 0:86284a262735 | 32 | MDMParser::Reg newStateCS = cnLib->getNet()->csd; |
msinig | 0:86284a262735 | 33 | MDMParser::Reg newStatePS = cnLib->getNet()->psd; |
msinig | 0:86284a262735 | 34 | bool isRoamingRegister, isCSRegistered, hasCsRegister, hasCsDeRegister, isPSRegistered, hasPsRegister, hasPsDeRegister; |
msinig | 1:29ad1d1ac1f9 | 35 | *regStatus = REG_NO_CHANGES; |
msinig | 1:29ad1d1ac1f9 | 36 | int res; |
msinig | 0:86284a262735 | 37 | |
msinig | 1:29ad1d1ac1f9 | 38 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 39 | //read and set COPS if necessary |
msinig | 1:29ad1d1ac1f9 | 40 | if (cnLib->getNet()->regStatus == MDMParser::COPS_UNKOWN){ |
msinig | 1:29ad1d1ac1f9 | 41 | res = cnLib->getNetworkInfo(); |
msinig | 1:29ad1d1ac1f9 | 42 | if (!RESPOK(res)) return RES_ERROR; |
msinig | 1:29ad1d1ac1f9 | 43 | } |
msinig | 0:86284a262735 | 44 | if (cnLib->getNet()->regStatus != MDMParser::COPS_AUTOMATIC_REG) |
msinig | 0:86284a262735 | 45 | cnLib->setNetAutoReg(); |
msinig | 0:86284a262735 | 46 | // poll registration info when timer expires |
msinig | 0:86284a262735 | 47 | if (tmStatusPull.isExpired()){ |
msinig | 0:86284a262735 | 48 | INFO("%s Polling Net Info timer Expired, checking net status\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 49 | cnLib->checkNetStatus(); |
msinig | 0:86284a262735 | 50 | newStateCS = cnLib->getNet()->csd; |
msinig | 0:86284a262735 | 51 | newStatePS = cnLib->getNet()->psd; |
msinig | 0:86284a262735 | 52 | if (!tmStatusPull.isOn()) |
msinig | 0:86284a262735 | 53 | tmStatusPull.start(); |
msinig | 0:86284a262735 | 54 | } |
msinig | 0:86284a262735 | 55 | //recover an old value in case of read errors |
msinig | 0:86284a262735 | 56 | if (ISUNKKOWN(newStateCS)) newStateCS = stateCS; |
msinig | 0:86284a262735 | 57 | if (ISUNKKOWN(newStatePS)) newStatePS = statePS; |
msinig | 0:86284a262735 | 58 | //process registration changes |
msinig | 0:86284a262735 | 59 | isRoamingRegister = ISROMAING(stateCS) || ISROMAING(statePS); |
msinig | 0:86284a262735 | 60 | isCSRegistered = ISREG(stateCS); |
msinig | 0:86284a262735 | 61 | hasCsRegister = (!isCSRegistered && ISREG(newStateCS)); |
msinig | 0:86284a262735 | 62 | hasCsDeRegister = ( isCSRegistered && !ISREG(newStateCS)); |
msinig | 0:86284a262735 | 63 | isPSRegistered = ISREG(statePS); |
msinig | 0:86284a262735 | 64 | hasPsRegister = ( !isPSRegistered && ISREG(newStatePS)); |
msinig | 0:86284a262735 | 65 | hasPsDeRegister = ( isPSRegistered && !ISREG(newStatePS)); |
msinig | 0:86284a262735 | 66 | //verify roaming |
msinig | 0:86284a262735 | 67 | if (!isRoamingRegister || (isRoamingRegister && isRomaingEnabled)){ |
msinig | 0:86284a262735 | 68 | //verify registration |
msinig | 0:86284a262735 | 69 | if ((isCSRegistered && hasPsRegister) || (isPSRegistered && hasCsRegister) || \ |
msinig | 0:86284a262735 | 70 | (hasPsRegister && hasCsRegister) ){ |
msinig | 0:86284a262735 | 71 | TRACE("%s CS and PS have just registered\r\n", __FUNCTION__); |
msinig | 1:29ad1d1ac1f9 | 72 | *regStatus = REG_REGISTERED; |
msinig | 0:86284a262735 | 73 | } |
msinig | 0:86284a262735 | 74 | else if (hasPsDeRegister || hasCsDeRegister){ |
msinig | 0:86284a262735 | 75 | TRACE("%s CS or PS are not registered\r\n", __FUNCTION__); |
msinig | 1:29ad1d1ac1f9 | 76 | *regStatus = REG_NOT_REGISTERED; |
msinig | 0:86284a262735 | 77 | } |
msinig | 0:86284a262735 | 78 | }else{ |
msinig | 0:86284a262735 | 79 | INFO("%s Roaming not allowed\r\n", __FUNCTION__); |
msinig | 1:29ad1d1ac1f9 | 80 | *regStatus = REG_NOT_REGISTERED; |
msinig | 0:86284a262735 | 81 | } |
msinig | 0:86284a262735 | 82 | //Perform action in base of registration status |
msinig | 0:86284a262735 | 83 | if (hasCsRegister){ |
msinig | 0:86284a262735 | 84 | //cnLib->getNetMccMnc(); |
msinig | 0:86284a262735 | 85 | } else if (hasCsDeRegister){ |
msinig | 0:86284a262735 | 86 | //cnLib->getNet()->netMccMnc = 0; |
msinig | 0:86284a262735 | 87 | memset(cnLib->getNet()->opr, 0, strlen(cnLib->getNet()->opr)); |
msinig | 0:86284a262735 | 88 | } |
msinig | 0:86284a262735 | 89 | //Dump info |
msinig | 0:86284a262735 | 90 | if (getUtilDebugLevel() > 1 && (newStatePS != statePS || newStateCS != stateCS )){ |
msinig | 1:29ad1d1ac1f9 | 91 | const char* txtStatus[] = { "unchanged", "registered", "NOT registered"}; |
msinig | 0:86284a262735 | 92 | INFO("cn registration Dump\r\n"); |
msinig | 1:29ad1d1ac1f9 | 93 | if (*regStatus < sizeof(txtStatus)/sizeof(*txtStatus)) |
msinig | 1:29ad1d1ac1f9 | 94 | INFO(" Status is now %s\r\n", txtStatus[*regStatus]); |
msinig | 0:86284a262735 | 95 | INFO(" has CS Registered = %s, has CS DeRegistered = %s\r\n", BOOLTOSTR(hasCsRegister), BOOLTOSTR(hasCsDeRegister)); |
msinig | 0:86284a262735 | 96 | INFO(" is Roaming active = %s, polling Timer %d\r\n", \ |
msinig | 0:86284a262735 | 97 | BOOLTOSTR(isRoamingRegister), tmStatusPull.read()); |
msinig | 1:29ad1d1ac1f9 | 98 | //cnLib->dumpNetStatus(cnLib->getNet()); |
msinig | 0:86284a262735 | 99 | } |
msinig | 0:86284a262735 | 100 | //update status |
msinig | 0:86284a262735 | 101 | stateCS = newStateCS; |
msinig | 0:86284a262735 | 102 | statePS = newStatePS; |
msinig | 1:29ad1d1ac1f9 | 103 | |
msinig | 0:86284a262735 | 104 | return RES_OK; |
msinig | 0:86284a262735 | 105 | } |