Minor changes

Fork of CNManager by u-blox

Committer:
msinig
Date:
Tue Jan 12 09:08:15 2016 +0000
Revision:
0:86284a262735
Child:
1:29ad1d1ac1f9
first drop

Who changed what in which revision?

UserRevisionLine numberNew 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 0:86284a262735 28 state = DATA_DOWN;
msinig 0:86284a262735 29 dataIsEnable = false;
msinig 0:86284a262735 30 tmDataPullStatus.setExpireTimeout(TIMEOUT_CHECK_STATUS_DATA_CONNECITION);
msinig 0:86284a262735 31 tmDataRetry.setExpireTimeout(TIMEOUT_RETRY_DATA_CONNECTION);
msinig 0:86284a262735 32 memset(dapn, 0, sizeof(dapn));
msinig 0:86284a262735 33 memset(dusername, 0, sizeof(dusername));
msinig 0:86284a262735 34 memset(dpassword, 0, sizeof(dpassword));
msinig 0:86284a262735 35 auth = MDMParser::AUTH_DETECT;
msinig 0:86284a262735 36 }
msinig 0:86284a262735 37
msinig 0:86284a262735 38 void cnDataEnable(bool enabled){
msinig 0:86284a262735 39 dataIsEnable = enabled;
msinig 0:86284a262735 40 }
msinig 0:86284a262735 41
msinig 0:86284a262735 42 void cnDataReset(){
msinig 0:86284a262735 43 TRACE("%s enter \r\n", __FUNCTION__);
msinig 0:86284a262735 44 state = DATA_DOWN;
msinig 0:86284a262735 45 dataIsEnable = false;
msinig 0:86284a262735 46 tmDataRetry.stop();
msinig 0:86284a262735 47 }
msinig 0:86284a262735 48
msinig 0:86284a262735 49 void cnDataSetupApn(const char* apn,const char* username,const char* password)
msinig 0:86284a262735 50 {
msinig 0:86284a262735 51 TRACE("%s enter \r\n", __FUNCTION__);
msinig 0:86284a262735 52 if (apn)
msinig 0:86284a262735 53 strncpy(dapn, apn, sizeof(apn));
msinig 0:86284a262735 54 if (username)
msinig 0:86284a262735 55 strncpy(dusername, username, sizeof(username));
msinig 0:86284a262735 56 if (password)
msinig 0:86284a262735 57 strncpy(dpassword, password, sizeof(password));
msinig 0:86284a262735 58 }
msinig 0:86284a262735 59
msinig 0:86284a262735 60 CNResp cnDataLoop(CNLib* const lib, RegStatus regStatus,DataConnStatus* const dataStatus)
msinig 0:86284a262735 61 {
msinig 0:86284a262735 62 DataState newState = DATA_UNDEFINED;
msinig 0:86284a262735 63 *dataStatus = DATA_NO_CHANGES;
msinig 0:86284a262735 64 MDMParser::IP ip;
msinig 0:86284a262735 65 char *apn, *username, *password;
msinig 0:86284a262735 66
msinig 0:86284a262735 67 //enable data connection
msinig 0:86284a262735 68 if (dataIsEnable && IS_DATA_DISCONNECTED(state) && regStatus == REG_REGISTERED){
msinig 0:86284a262735 69 if (!tmDataRetry.isOn() || tmDataRetry.isExpired()){
msinig 0:86284a262735 70 INFO("%s: Trying to bring up Data\r\n", __FUNCTION__);
msinig 0:86284a262735 71 apn = (dapn[0]=='\0') ? NULL : dapn;
msinig 0:86284a262735 72 username = (dusername[0]=='\0') ? NULL : dusername;
msinig 0:86284a262735 73 password = (dpassword[0]=='\0') ? NULL : dpassword;
msinig 0:86284a262735 74 ip = lib->join(apn, username, password, auth);
msinig 0:86284a262735 75 newState = (ip != NOIP)? DATA_UP : DATA_DOWN;
msinig 0:86284a262735 76 //check result of data activation call
msinig 0:86284a262735 77 if (IS_DATA_DISCONNECTED(newState)){
msinig 0:86284a262735 78 INFO("%s: Data has not been successfully activated\r\n", __FUNCTION__);
msinig 0:86284a262735 79 tmDataRetry.start();
msinig 0:86284a262735 80 }
msinig 0:86284a262735 81 else if (IS_DATA_CONNECTED(newState)){
msinig 0:86284a262735 82 INFO("%s: Data has been activated successfully\r\n", __FUNCTION__);
msinig 0:86284a262735 83 tmDataRetry.stop();
msinig 0:86284a262735 84 }
msinig 0:86284a262735 85 }
msinig 0:86284a262735 86 }
msinig 0:86284a262735 87 //disable data
msinig 0:86284a262735 88 else if (IS_DATA_CONNECTED(state) && (!dataIsEnable || regStatus == REG_NOT_REGISTERED)){
msinig 0:86284a262735 89 INFO("%s: Disable data\n", __FUNCTION__);
msinig 0:86284a262735 90 lib->disableInternalContext();
msinig 0:86284a262735 91 newState = DATA_DOWN;
msinig 0:86284a262735 92 }
msinig 0:86284a262735 93 //pull internal context status
msinig 0:86284a262735 94 else if (tmDataPullStatus.isExpired() && IS_DATA_CONNECTED(state)){
msinig 0:86284a262735 95 INFO("%s: Polling Status..\r\n", __FUNCTION__);
msinig 0:86284a262735 96 lib->getInternalStatusContext();
msinig 0:86284a262735 97 if (lib->getIpAddress() == NOIP) newState = DATA_DOWN;
msinig 0:86284a262735 98 else newState = DATA_UP;
msinig 0:86284a262735 99 }
msinig 0:86284a262735 100 //data id attached
msinig 0:86284a262735 101 if (HAS_DATA_ATTACHED(state, newState)){
msinig 0:86284a262735 102 INFO("%s: Notify data is up\r\n", __FUNCTION__);
msinig 0:86284a262735 103 *dataStatus = DATA_IS_CONNECTED;
msinig 0:86284a262735 104 tmDataRetry.stop();
msinig 0:86284a262735 105 tmDataPullStatus.start();
msinig 0:86284a262735 106 }
msinig 0:86284a262735 107 //data is detached
msinig 0:86284a262735 108 if (HAS_DATA_DEATTACHED(state, newState)){
msinig 0:86284a262735 109 INFO("%s: Notify data is down\r\n", __FUNCTION__);
msinig 0:86284a262735 110 *dataStatus = DATA_IS_DISCONNECTED;
msinig 0:86284a262735 111 tmDataPullStatus.stop();
msinig 0:86284a262735 112 }
msinig 0:86284a262735 113 //dump info
msinig 0:86284a262735 114 if (getUtilDebugLevel() > 1 && ( HAS_DATA_ATTACHED(state, newState) || HAS_DATA_DEATTACHED(state, newState))){
msinig 0:86284a262735 115 INFO("CNM Data Dump\r\n");
msinig 0:86284a262735 116 const char* txtState[] = { "Undefined", "Up", "Down"};
msinig 0:86284a262735 117 if (newState < sizeof(txtState)/sizeof(*txtState) && newState != DATA_UNDEFINED)
msinig 0:86284a262735 118 INFO(" State is now: %s\r\n", txtState[newState]);
msinig 0:86284a262735 119 INFO(" Data is enabled: %s, has Data Attached: %s, has Data Detached: %s\r\n", BOOLTOSTR(dataIsEnable),\
msinig 0:86284a262735 120 BOOLTOSTR(HAS_DATA_ATTACHED(state, newState)), BOOLTOSTR(HAS_DATA_DEATTACHED(state, newState)));
msinig 0:86284a262735 121 INFO(" Timer for Data Retry: %d, Timer for Check Data %d \r\n", tmDataRetry.getRetryContunter(), \
msinig 0:86284a262735 122 tmDataPullStatus.getRetryContunter());
msinig 0:86284a262735 123 if (lib->getIpAddress() != NOIP)
msinig 0:86284a262735 124 lib->dumpIp(lib->getIpAddress());
msinig 0:86284a262735 125 }
msinig 0:86284a262735 126 //update status
msinig 0:86284a262735 127 if (newState != DATA_UNDEFINED)
msinig 0:86284a262735 128 state = newState;
msinig 0:86284a262735 129
msinig 0:86284a262735 130 return RES_OK;;
msinig 0:86284a262735 131 }