Sample code for Data Profiles

static const char DefaultCid[] = "default";
static const char automaticApn[] = "auto";
static const char PdpIpv4[] = "ipv4";
static const char PdpIpv6[] = "ipv6";
static const char PdpIpv4v6[] = "ipv4v6";
static const char AuthPap[] = "pap";
static const char AuthChap[] = "chap";
static const char AuthPapChap[] = "pap-chap";
static const char Map[] = "map";
static const char Cid[] = "cid";
static const char Rmnet[] = "rmnet";
 
// Structure used to set the configuration
typedef struct
{
char cid[10]; // profile identifier
char pdp[10]; // packet data protocol
char apn[LE_MDC_APN_NAME_MAX_LEN]; // access point network
char auth[10]; // authentication level
char userName[LE_MDC_USER_NAME_MAX_LEN]; // username for authentication
char password[LE_MDC_PASSWORD_NAME_MAX_LEN]; // password for authentication
}
Configuration_t;
 
// Tests cases
typedef enum
{
TEST_SYNC,
TEST_ASYNC,
TEST_MAX
} Testcase_t;
 
// Tests definition
struct
{
char testName[20];
Testcase_t testCase;
} testsDef[] = {
TEST_DEF(TEST_SYNC),
TEST_DEF(TEST_ASYNC),
TEST_DEF(TEST_MAX)
};
 
//! [Data_session]
//--------------------------------------------------------------------------------------------------
/**
* Session handler response for connection and disconnection.
*/
//--------------------------------------------------------------------------------------------------
static void SessionHandlerFunc
(
le_mdc_ProfileRef_t profileRef,
le_result_t result,
void* contextPtr
)
{
le_result_t* activationPtr = contextPtr;
*activationPtr = result;
 
LE_INFO("Session result %d for profile %d", result, le_mdc_GetProfileIndex(profileRef));
 
le_sem_Post(AsyncTestSemaphore);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Start asynchronous session.
*/
//--------------------------------------------------------------------------------------------------
static void SessionStartAsync
(
void* param1Ptr,
void* param2Ptr
)
{
le_mdc_ProfileRef_t profileRef = param1Ptr;
 
le_mdc_StartSessionAsync(profileRef, SessionHandlerFunc, param2Ptr);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Stop asynchronous session.
*/
//--------------------------------------------------------------------------------------------------
static void SessionStopAsync
(
void* param1Ptr,
void* param2Ptr
)
{
le_mdc_ProfileRef_t profileRef = param1Ptr;
 
le_mdc_StopSessionAsync(profileRef, SessionHandlerFunc, param2Ptr);
}
//! [Data_session]
//--------------------------------------------------------------------------------------------------
/**
* Set the configuration.
*
*/
//--------------------------------------------------------------------------------------------------
static void SetConfiguration
(
le_mdc_ProfileRef_t* profileRefPtr
)
{
// Check the configuration file
FILE* configFilePtr = fopen("/tmp/config.txt","r");
Configuration_t configuration;
 
memset(&configuration, 0, sizeof(Configuration_t));
 
// if configuration file absent, use default settings
if (NULL == configFilePtr)
{
le_utf8_Copy(configuration.cid, DefaultCid, sizeof(configuration.cid), NULL);
le_utf8_Copy(configuration.pdp, PdpIpv4, sizeof(configuration.pdp), NULL);
le_utf8_Copy(configuration.apn, automaticApn, sizeof(configuration.apn), NULL);
}
else
{
// Get the configuration from the file
fseek(configFilePtr, 0, SEEK_END);
uint32_t len = ftell(configFilePtr);
char cmdLine[ len + 1 ];
char* saveLinePtr;
char* saveParamPtr;
char* cmdLinePtr;
 
memset(cmdLine,0,len+1);
fseek(configFilePtr, 0, SEEK_SET);
fread(cmdLine, 1, len, configFilePtr);
fclose(configFilePtr);
 
// Get first line
cmdLinePtr = strtok_r(cmdLine, "\r\n", &saveLinePtr);
 
// Get profile number
char* cidPtr = strtok_r(cmdLinePtr, " ", &saveParamPtr);
LE_ASSERT(NULL != cidPtr);
le_utf8_Copy(configuration.cid, cidPtr, sizeof(configuration.cid), NULL);
 
// Get pdp type
char* pdpPtr = strtok_r(NULL, " ", &saveParamPtr);
LE_ASSERT(NULL != pdpPtr);
le_utf8_Copy(configuration.pdp, pdpPtr, sizeof(configuration.pdp), NULL);
 
// Get apn
char* apnPtr = strtok_r(NULL, " ", &saveParamPtr);
LE_ASSERT(NULL != apnPtr);
le_utf8_Copy(configuration.apn, apnPtr, sizeof(configuration.apn), NULL);
 
// Get authentication
char* authPtr = strtok_r(NULL, " ", &saveParamPtr);
if (NULL != authPtr)
{
le_utf8_Copy(configuration.auth, authPtr, sizeof(configuration.auth), NULL);
}
 
// Get username
char* userNamePtr = strtok_r(NULL, " ", &saveParamPtr);
if (NULL != userNamePtr)
{
le_utf8_Copy(configuration.userName, userNamePtr, sizeof(configuration.userName), NULL);
}
 
// Get password
char* passwordPtr = strtok_r(NULL, " ", &saveParamPtr);
if (NULL != passwordPtr)
{
le_utf8_Copy(configuration.password, passwordPtr, sizeof(configuration.password), NULL);
}
 
// Get optional lines
cmdLinePtr = strtok_r(NULL, "\r\n", &saveLinePtr);
 
while (cmdLinePtr)
{
char* optionPtr = strtok_r(cmdLinePtr, " ", &saveParamPtr);
 
// Check mapping cid
if (strncmp(optionPtr, Map, strlen(Map)) == 0)
{
LE_INFO("mapping cid");
char* cidPtr = strtok_r(NULL, " ", &saveParamPtr);
 
if (strncmp(cidPtr, Cid, strlen(Cid)) == 0)
{
int cid = strtol((const char*) cidPtr+strlen(Cid), NULL, 10);
 
if (0 != errno)
{
LE_ERROR("Bad cid %d %m", errno);
exit(EXIT_FAILURE);
}
 
LE_INFO("map cid %d", cid);
 
char* rmnetPtr = strtok_r(NULL, " ", &saveParamPtr);
 
if (0 == strncmp(rmnetPtr, Rmnet, strlen(Rmnet)))
{
int rmnet = strtol(rmnetPtr+strlen(Rmnet), NULL, 10);
 
if (0 != errno)
{
LE_ERROR("Bad rmnet %d %m", errno);
exit(EXIT_FAILURE);
}
 
le_mdc_ProfileRef_t profileRef = le_mdc_GetProfile(cid);
 
char string[20];
memset(string,0,sizeof(string));
snprintf(string,sizeof(string),"rmnet_data%d", rmnet);
LE_INFO("cid %d rmnet: %s", cid, string);
 
}
}
}
 
cmdLinePtr = strtok_r(NULL, "\r\n", &saveLinePtr);
}
}
 
uint32_t profile;
 
if ( strncmp(configuration.cid, DefaultCid, sizeof(DefaultCid)) == 0 )
{
profile = LE_MDC_DEFAULT_PROFILE;
}
else
{
profile = strtol(configuration.cid, NULL, 10);
 
if (errno != 0)
{
LE_ERROR("Bad profile %d %m", errno);
exit(EXIT_FAILURE);
}
}
//! [Profile_parameters]
// Get the profile reference
*profileRefPtr = le_mdc_GetProfile(profile);
LE_ASSERT(NULL != *profileRefPtr);
 
// Check the current state of the cid
le_mdc_ConState_t state = LE_MDC_DISCONNECTED;
 
// Check the state
LE_ASSERT(LE_OK == le_mdc_GetSessionState(*profileRefPtr, &state));
 
// If already connected, disconnect the session
if (LE_MDC_CONNECTED == state)
{
LE_ASSERT(le_mdc_StopSession(*profileRefPtr) == LE_OK);
}
 
// Set pdp type
le_mdc_Pdp_t pdp = LE_MDC_PDP_UNKNOWN;
 
if (0 == strncmp(configuration.pdp, PdpIpv4, sizeof(PdpIpv4)))
{
pdp = LE_MDC_PDP_IPV4;
}
else if (0 == strncmp(configuration.pdp, PdpIpv6, sizeof(PdpIpv6)))
{
pdp = LE_MDC_PDP_IPV6;
}
else if (0 == strncmp(configuration.pdp, PdpIpv4v6, sizeof(PdpIpv4v6)))
{
pdp = LE_MDC_PDP_IPV4V6;
}
 
LE_ASSERT(LE_OK == le_mdc_SetPDP(*profileRefPtr, pdp));
 
// Set APN
if (0 == strncmp(configuration.apn, automaticApn, sizeof(automaticApn)))
{
#if LE_CONFIG_ENABLE_DEFAULT_APN_SWITCHING
// Set default APN
LE_ASSERT(LE_OK == le_mdc_SetDefaultAPN(*profileRefPtr));
#else
// Default APN switching should be disabled. Thus we should use SetApn.
LE_ASSERT(LE_OK == le_mdc_SetAPN(*profileRefPtr, configuration.apn));
#endif
}
else
{
LE_ASSERT(LE_OK == le_mdc_SetAPN(*profileRefPtr, configuration.apn));
}
 
le_mdc_Auth_t auth = LE_MDC_AUTH_NONE;
if ('\0' != configuration.auth[0])
{
// Set the authentication, username and password
if (0 == strncmp(configuration.auth, AuthPapChap, sizeof(AuthPapChap)))
{
auth = LE_MDC_AUTH_PAP | LE_MDC_AUTH_CHAP;
}
// Set the authentication, username and password
else if (0 == strncmp(configuration.auth, AuthPap, sizeof(AuthPap)))
{
auth = LE_MDC_AUTH_PAP;
}
else if (0 == strncmp(configuration.auth, AuthChap, sizeof(AuthChap)))
{
auth = LE_MDC_AUTH_CHAP;
}
 
if (LE_MDC_AUTH_NONE != auth)
{
auth,
configuration.userName,
configuration.password));
}
}
 
LE_INFO("cid: %d pdp: %d apn: %s auth: %d username: %s password: %s",
le_mdc_GetProfileIndex(*profileRefPtr), pdp, configuration.apn, auth,
configuration.userName, configuration.password);
}