Connection Manager library for u-blox cellular modules. It manages the modem for keeping data connection always active.
CNData.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 "CNData.h" |
msinig | 0:86284a262735 | 2 | #include "CNLib.h" |
msinig | 0:86284a262735 | 3 | #include "CNUtil.h" |
msinig | 0:86284a262735 | 4 | |
msinig | 0:86284a262735 | 5 | //! Data Connection State |
msinig | 0:86284a262735 | 6 | typedef enum { |
msinig | 0:86284a262735 | 7 | DATA_UNDEFINED, //!< undefined |
msinig | 0:86284a262735 | 8 | DATA_UP, //!< data is up |
msinig | 0:86284a262735 | 9 | DATA_DOWN, //!< data is down |
msinig | 0:86284a262735 | 10 | } DataState; |
msinig | 0:86284a262735 | 11 | |
msinig | 0:86284a262735 | 12 | #define IS_DATA_CONNECTED(s) (s == DATA_UP) |
msinig | 0:86284a262735 | 13 | #define IS_DATA_DISCONNECTED(s) (s == DATA_DOWN) |
msinig | 0:86284a262735 | 14 | #define HAS_DATA_ATTACHED(st, newSt) (st == DATA_DOWN && newSt == DATA_UP) |
msinig | 0:86284a262735 | 15 | #define HAS_DATA_DEATTACHED(st, newSt) (newSt== DATA_DOWN && st == DATA_UP) |
msinig | 0:86284a262735 | 16 | |
msinig | 0:86284a262735 | 17 | static DataState state = DATA_UNDEFINED; //!<data state |
msinig | 0:86284a262735 | 18 | static CNTimer tmDataRetry; //!<timer for data retry |
msinig | 0:86284a262735 | 19 | static CNTimer tmDataPullStatus; //!<timer for data pull status |
msinig | 0:86284a262735 | 20 | static bool dataIsEnable; //!<data is enabled |
msinig | 0:86284a262735 | 21 | |
msinig | 0:86284a262735 | 22 | static char dapn[20]; //!<apn of the network provider |
msinig | 0:86284a262735 | 23 | static char dusername[10]; //!<user name text string for the authentication phase |
msinig | 0:86284a262735 | 24 | static char dpassword[10]; //!<password text string for the authentication phase |
msinig | 0:86284a262735 | 25 | static MDMParser::Auth auth; //!<authentication mode (CHAP,PAP,NONE or DETECT) |
msinig | 0:86284a262735 | 26 | |
msinig | 0:86284a262735 | 27 | void cnDataInit(){ |
msinig | 1:29ad1d1ac1f9 | 28 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 29 | state = DATA_DOWN; |
msinig | 0:86284a262735 | 30 | dataIsEnable = false; |
msinig | 0:86284a262735 | 31 | tmDataPullStatus.setExpireTimeout(TIMEOUT_CHECK_STATUS_DATA_CONNECITION); |
msinig | 0:86284a262735 | 32 | tmDataRetry.setExpireTimeout(TIMEOUT_RETRY_DATA_CONNECTION); |
msinig | 0:86284a262735 | 33 | memset(dapn, 0, sizeof(dapn)); |
msinig | 0:86284a262735 | 34 | memset(dusername, 0, sizeof(dusername)); |
msinig | 0:86284a262735 | 35 | memset(dpassword, 0, sizeof(dpassword)); |
msinig | 0:86284a262735 | 36 | auth = MDMParser::AUTH_DETECT; |
msinig | 0:86284a262735 | 37 | } |
msinig | 0:86284a262735 | 38 | |
msinig | 0:86284a262735 | 39 | void cnDataEnable(bool enabled){ |
msinig | 1:29ad1d1ac1f9 | 40 | TRACE("%s enter enabled: %d\r\n", __FUNCTION__, enabled); |
msinig | 0:86284a262735 | 41 | dataIsEnable = enabled; |
msinig | 0:86284a262735 | 42 | } |
msinig | 0:86284a262735 | 43 | |
msinig | 0:86284a262735 | 44 | void cnDataReset(){ |
msinig | 0:86284a262735 | 45 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 46 | state = DATA_DOWN; |
msinig | 0:86284a262735 | 47 | dataIsEnable = false; |
msinig | 0:86284a262735 | 48 | tmDataRetry.stop(); |
msinig | 0:86284a262735 | 49 | } |
msinig | 0:86284a262735 | 50 | |
msinig | 0:86284a262735 | 51 | void cnDataSetupApn(const char* apn,const char* username,const char* password) |
msinig | 0:86284a262735 | 52 | { |
msinig | 0:86284a262735 | 53 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 54 | if (apn) |
msinig | 0:86284a262735 | 55 | strncpy(dapn, apn, sizeof(apn)); |
msinig | 0:86284a262735 | 56 | if (username) |
msinig | 0:86284a262735 | 57 | strncpy(dusername, username, sizeof(username)); |
msinig | 0:86284a262735 | 58 | if (password) |
msinig | 0:86284a262735 | 59 | strncpy(dpassword, password, sizeof(password)); |
msinig | 0:86284a262735 | 60 | } |
msinig | 0:86284a262735 | 61 | |
msinig | 0:86284a262735 | 62 | CNResp cnDataLoop(CNLib* const lib, RegStatus regStatus,DataConnStatus* const dataStatus) |
msinig | 0:86284a262735 | 63 | { |
msinig | 0:86284a262735 | 64 | DataState newState = DATA_UNDEFINED; |
msinig | 0:86284a262735 | 65 | *dataStatus = DATA_NO_CHANGES; |
msinig | 0:86284a262735 | 66 | MDMParser::IP ip; |
msinig | 0:86284a262735 | 67 | char *apn, *username, *password; |
msinig | 1:29ad1d1ac1f9 | 68 | |
msinig | 1:29ad1d1ac1f9 | 69 | TRACE("%s enter \r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 70 | //enable data connection |
msinig | 0:86284a262735 | 71 | if (dataIsEnable && IS_DATA_DISCONNECTED(state) && regStatus == REG_REGISTERED){ |
msinig | 0:86284a262735 | 72 | if (!tmDataRetry.isOn() || tmDataRetry.isExpired()){ |
msinig | 0:86284a262735 | 73 | INFO("%s: Trying to bring up Data\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 74 | apn = (dapn[0]=='\0') ? NULL : dapn; |
msinig | 0:86284a262735 | 75 | username = (dusername[0]=='\0') ? NULL : dusername; |
msinig | 0:86284a262735 | 76 | password = (dpassword[0]=='\0') ? NULL : dpassword; |
msinig | 0:86284a262735 | 77 | ip = lib->join(apn, username, password, auth); |
msinig | 0:86284a262735 | 78 | newState = (ip != NOIP)? DATA_UP : DATA_DOWN; |
msinig | 0:86284a262735 | 79 | //check result of data activation call |
msinig | 0:86284a262735 | 80 | if (IS_DATA_DISCONNECTED(newState)){ |
msinig | 0:86284a262735 | 81 | INFO("%s: Data has not been successfully activated\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 82 | tmDataRetry.start(); |
msinig | 0:86284a262735 | 83 | } |
msinig | 0:86284a262735 | 84 | else if (IS_DATA_CONNECTED(newState)){ |
msinig | 0:86284a262735 | 85 | INFO("%s: Data has been activated successfully\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 86 | tmDataRetry.stop(); |
msinig | 0:86284a262735 | 87 | } |
msinig | 0:86284a262735 | 88 | } |
msinig | 0:86284a262735 | 89 | } |
msinig | 0:86284a262735 | 90 | //disable data |
msinig | 0:86284a262735 | 91 | else if (IS_DATA_CONNECTED(state) && (!dataIsEnable || regStatus == REG_NOT_REGISTERED)){ |
msinig | 0:86284a262735 | 92 | INFO("%s: Disable data\n", __FUNCTION__); |
msinig | 0:86284a262735 | 93 | lib->disableInternalContext(); |
msinig | 0:86284a262735 | 94 | newState = DATA_DOWN; |
msinig | 0:86284a262735 | 95 | } |
msinig | 0:86284a262735 | 96 | //pull internal context status |
msinig | 0:86284a262735 | 97 | else if (tmDataPullStatus.isExpired() && IS_DATA_CONNECTED(state)){ |
msinig | 0:86284a262735 | 98 | INFO("%s: Polling Status..\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 99 | lib->getInternalStatusContext(); |
msinig | 0:86284a262735 | 100 | if (lib->getIpAddress() == NOIP) newState = DATA_DOWN; |
msinig | 0:86284a262735 | 101 | else newState = DATA_UP; |
msinig | 0:86284a262735 | 102 | } |
msinig | 0:86284a262735 | 103 | //data id attached |
msinig | 0:86284a262735 | 104 | if (HAS_DATA_ATTACHED(state, newState)){ |
msinig | 0:86284a262735 | 105 | INFO("%s: Notify data is up\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 106 | *dataStatus = DATA_IS_CONNECTED; |
msinig | 0:86284a262735 | 107 | tmDataRetry.stop(); |
msinig | 0:86284a262735 | 108 | tmDataPullStatus.start(); |
msinig | 0:86284a262735 | 109 | } |
msinig | 0:86284a262735 | 110 | //data is detached |
msinig | 0:86284a262735 | 111 | if (HAS_DATA_DEATTACHED(state, newState)){ |
msinig | 0:86284a262735 | 112 | INFO("%s: Notify data is down\r\n", __FUNCTION__); |
msinig | 0:86284a262735 | 113 | *dataStatus = DATA_IS_DISCONNECTED; |
msinig | 0:86284a262735 | 114 | tmDataPullStatus.stop(); |
msinig | 0:86284a262735 | 115 | } |
msinig | 1:29ad1d1ac1f9 | 116 | |
msinig | 0:86284a262735 | 117 | //dump info |
msinig | 0:86284a262735 | 118 | if (getUtilDebugLevel() > 1 && ( HAS_DATA_ATTACHED(state, newState) || HAS_DATA_DEATTACHED(state, newState))){ |
msinig | 0:86284a262735 | 119 | INFO("CNM Data Dump\r\n"); |
msinig | 0:86284a262735 | 120 | const char* txtState[] = { "Undefined", "Up", "Down"}; |
msinig | 0:86284a262735 | 121 | if (newState < sizeof(txtState)/sizeof(*txtState) && newState != DATA_UNDEFINED) |
msinig | 1:29ad1d1ac1f9 | 122 | INFO(" State is now: %s\r\n", txtState[newState]); |
msinig | 0:86284a262735 | 123 | INFO(" Data is enabled: %s, has Data Attached: %s, has Data Detached: %s\r\n", BOOLTOSTR(dataIsEnable),\ |
msinig | 0:86284a262735 | 124 | BOOLTOSTR(HAS_DATA_ATTACHED(state, newState)), BOOLTOSTR(HAS_DATA_DEATTACHED(state, newState))); |
msinig | 0:86284a262735 | 125 | INFO(" Timer for Data Retry: %d, Timer for Check Data %d \r\n", tmDataRetry.getRetryContunter(), \ |
msinig | 0:86284a262735 | 126 | tmDataPullStatus.getRetryContunter()); |
msinig | 0:86284a262735 | 127 | if (lib->getIpAddress() != NOIP) |
msinig | 0:86284a262735 | 128 | lib->dumpIp(lib->getIpAddress()); |
msinig | 0:86284a262735 | 129 | } |
msinig | 0:86284a262735 | 130 | //update status |
msinig | 0:86284a262735 | 131 | if (newState != DATA_UNDEFINED) |
msinig | 0:86284a262735 | 132 | state = newState; |
msinig | 0:86284a262735 | 133 | |
msinig | 0:86284a262735 | 134 | return RES_OK;; |
msinig | 0:86284a262735 | 135 | } |