Sample code for Network Scan

//--------------------------------------------------------------------------------------------------
/**
* Read scan information
*
*/
//--------------------------------------------------------------------------------------------------
static void ReadScanInfo
(
le_mrc_ScanInformationRef_t scanInfoRef
)
{
le_mrc_Rat_t rat;
bool boolTest;
char mcc[LE_MRC_MCC_BYTES] = {0};
char mnc[LE_MRC_MNC_BYTES] = {0};
char nameStr[100] = {0};
 
mcc,
LE_MRC_MCC_BYTES,
mnc,
LE_MRC_MNC_BYTES);
LE_ASSERT(res == LE_OK);
 
res = le_mrc_GetCellularNetworkName(scanInfoRef, nameStr, 1);
res = le_mrc_GetCellularNetworkName(scanInfoRef, nameStr, 100);;
LE_ASSERT(res == LE_OK);
LE_INFO("cellular network name.%s (mcc=%s, mnc=%s)", nameStr, mcc, mnc);
 
rat = le_mrc_GetCellularNetworkRat(scanInfoRef);
LE_ASSERT((rat>=LE_MRC_RAT_UNKNOWN) && (rat<=LE_MRC_RAT_LTE));
LE_INFO("le_mrc_GetCellularNetworkRat returns rat %d", rat);
 
boolTest = le_mrc_IsCellularNetworkInUse(scanInfoRef);
LE_INFO("IsCellularNetworkInUse is %s", ( (boolTest) ? "true" : "false"));
 
boolTest = le_mrc_IsCellularNetworkAvailable(scanInfoRef);
LE_INFO("le_mrc_IsCellularNetworkAvailable is %s", ( (boolTest) ? "true" : "false"));
 
boolTest = le_mrc_IsCellularNetworkHome(scanInfoRef);
LE_INFO("le_mrc_IsCellularNetworkHome is %s", ( (boolTest) ? "true" : "false"));
 
boolTest = le_mrc_IsCellularNetworkForbidden(scanInfoRef);
LE_INFO("le_mrc_IsCellularNetworkForbidden is %s", ( (boolTest) ? "true" : "false"));
}
 
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Cellular Network Scan.
*
* le_mrc_PerformCellularNetworkScan() API test
*/
//--------------------------------------------------------------------------------------------------
static void Testle_mrc_PerformCellularNetworkScan
(
void
)
{
le_mrc_RatBitMask_t bitMaskOrigin = 0;
le_mrc_ScanInformationListRef_t scanInfoListRef = NULL;
le_mrc_ScanInformationRef_t scanInfoRef = NULL;
 
// Get the current rat preference.
res = le_mrc_GetRatPreferences(&bitMaskOrigin);
LE_ASSERT(res == LE_OK);
 
scanInfoListRef = le_mrc_PerformCellularNetworkScan(bitMaskOrigin);
if (scanInfoListRef != NULL)
{
scanInfoRef = le_mrc_GetFirstCellularNetworkScan(scanInfoListRef);
LE_ASSERT(scanInfoRef != NULL);
ReadScanInfo(scanInfoRef);
 
while ((scanInfoRef = le_mrc_GetNextCellularNetworkScan(scanInfoListRef)) != NULL)
{
ReadScanInfo(scanInfoRef);
}
 
}
res = le_mrc_SetRatPreferences(bitMaskOrigin);
LE_ASSERT(LE_OK == res);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Cellular Network Scan handler function.
*
*/
//--------------------------------------------------------------------------------------------------
static void MyNetworkPciScanHandler
(
le_mrc_PciScanInformationListRef_t listRef,
void* contextPtr
)
{
char mcc[LE_MRC_MCC_BYTES] = {0};
char mnc[LE_MRC_MNC_BYTES] = {0};
le_mrc_PciScanInformationRef_t scanInfoRef = NULL;
le_mrc_PlmnInformationRef_t PlmnInfoRef = NULL;
 
if (NULL != listRef)
{
scanInfoRef = le_mrc_GetFirstPciScanInfo(listRef);
LE_ASSERT(scanInfoRef != NULL);
uint16_t physicalCellId = le_mrc_GetPciScanCellId(scanInfoRef);
uint32_t globalCellId = le_mrc_GetPciScanGlobalCellId(scanInfoRef);
LE_INFO("First cell ID: physical %"PRIu16" global %"PRIu32"", physicalCellId, globalCellId);
//Get first plmninfo reference
PlmnInfoRef = le_mrc_GetFirstPlmnInfo(scanInfoRef);
if(PlmnInfoRef == NULL)
{
LE_INFO("FAIL to get ref to plmn info");
}
else
{
LE_INFO(" SUCCESS to get ref to plmn info");
res = le_mrc_GetPciScanMccMnc(PlmnInfoRef,
mcc,
LE_MRC_MCC_BYTES,
mnc,
LE_MRC_MNC_BYTES);
if (res == LE_OK )
{
LE_INFO("the first value of mcc in the first cell is :%s",mcc);
LE_INFO("the first value of mnc in the first cell is :%s",mnc);
}
}
while ((PlmnInfoRef = le_mrc_GetNextPlmnInfo(scanInfoRef)) != NULL)
{
res = le_mrc_GetPciScanMccMnc(PlmnInfoRef,
mcc,
LE_MRC_MCC_BYTES,
mnc,
LE_MRC_MNC_BYTES);
if (res == LE_OK )
{
LE_INFO("The Next value of mcc in the first cell is :%s",mcc);
LE_INFO("The Next value of mnc in the first cell is :%s",mnc);
}
}
 
while ((scanInfoRef = le_mrc_GetNextPciScanInfo(listRef)) != NULL)
{
physicalCellId = le_mrc_GetPciScanCellId(scanInfoRef);
globalCellId = le_mrc_GetPciScanGlobalCellId(scanInfoRef);
LE_INFO("Next cell ID: physical %"PRIu16" global %"PRIu32"", physicalCellId,
globalCellId);
PlmnInfoRef = le_mrc_GetFirstPlmnInfo(scanInfoRef);
 
if(PlmnInfoRef == NULL)
{
LE_INFO("FAIL to get ref to plmn info");
}
else
{
LE_INFO(" SUCCESS to get ref to plmn info");
res = le_mrc_GetPciScanMccMnc(PlmnInfoRef,
mcc,
LE_MRC_MCC_BYTES,
mnc,
LE_MRC_MNC_BYTES);
if (res == LE_OK )
{
LE_INFO("the first value of mcc in the next cell is :%s",mcc);
LE_INFO("the first value of mnc in the next cell is :%s",mnc);
}
}
 
while ((PlmnInfoRef = le_mrc_GetNextPlmnInfo(scanInfoRef)) != NULL)
{
res = le_mrc_GetPciScanMccMnc(PlmnInfoRef,
mcc,
LE_MRC_MCC_BYTES,
mnc,
LE_MRC_MNC_BYTES);
if (res == LE_OK )
{
LE_INFO("The Next value of mcc in the Next cell is :%s",mcc);
LE_INFO("The Next value of mnc in the Next cell is :%s",mnc);
}
}
}
}
// Deleting the scan results
 
le_sem_Post(ThreadSemaphore);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Thread for asynchronous Pci scan test.
*
* Test API: le_mrc_PerformPciNetworkScanAsync() API test
*/
//--------------------------------------------------------------------------------------------------
static void* MyNetworkPciScanAsyncThread
(
void* context ///< See parameter documentation above.
)
{
le_mrc_RatBitMask_t bitMaskOrigin;
 
 
// Get the current rat preference.
le_result_t res = le_mrc_GetRatPreferences(&bitMaskOrigin);
LE_ASSERT(res == LE_OK);
 
le_mrc_PerformPciNetworkScanAsync(bitMaskOrigin, MyNetworkPciScanHandler, NULL);
return NULL;
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Cellular Network Scan asynchronous.
*
*/
//--------------------------------------------------------------------------------------------------
static void Testle_mrc_PerformPciNetworkScanAsync
(
void
)
{
le_clk_Time_t time = {0,0};
le_mrc_RatBitMask_t bitMaskOrigin = 0;
time.sec = 120000;
 
res = le_mrc_GetRatPreferences(&bitMaskOrigin);
LE_ASSERT(LE_OK == res);
 
// Init the semaphore for asynchronous callback
ThreadSemaphore = le_sem_Create("HandlerSem",0);
 
RegistrationThreadRef = le_thread_Create("CallBack", MyNetworkPciScanAsyncThread, NULL);
le_thread_Start(RegistrationThreadRef);
 
// Wait for complete asynchronous registration
res = le_sem_WaitWithTimeOut(ThreadSemaphore, time);
LE_ERROR_IF(res != LE_OK, "SYNC FAILED");
le_thread_Cancel(RegistrationThreadRef);
 
le_sem_Delete(ThreadSemaphore);
 
le_mrc_SetRatPreferences(bitMaskOrigin);
 
le_thread_Sleep(5);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Test: Cellular Network Scan handler function.
*
*/
//--------------------------------------------------------------------------------------------------
static void MyNetworkScanHandler
(
le_mrc_ScanInformationListRef_t listRef,
void* contextPtr
)
{
le_mrc_ScanInformationRef_t scanInfoRef = NULL;
 
if (NULL != listRef)
{
scanInfoRef = le_mrc_GetFirstCellularNetworkScan(listRef);
LE_ASSERT(scanInfoRef != NULL);
 
if (scanInfoRef != NULL)
{
ReadScanInfo(scanInfoRef);
 
while ((scanInfoRef = le_mrc_GetNextCellularNetworkScan(listRef)) != NULL)
{
ReadScanInfo(scanInfoRef);
}
}
}
le_sem_Post(ThreadSemaphore);
}
 
//--------------------------------------------------------------------------------------------------
/**
* Thread for asynchronous Network scan test.
*
* Test API: le_mrc_PerformCellularNetworkScanAsync() API test
*/
//--------------------------------------------------------------------------------------------------
static void* MyNetworkScanAsyncThread
(
void* context ///< See parameter documentation above.
)
{
le_mrc_RatBitMask_t bitMaskOrigin;
 
 
// Get the current rat preference.
le_result_t res = le_mrc_GetRatPreferences(&bitMaskOrigin);
LE_ASSERT(res == LE_OK);
 
le_mrc_PerformCellularNetworkScanAsync(bitMaskOrigin, MyNetworkScanHandler, NULL);
return NULL;
}