//-------------------------------------------------------------------------------------------------- static void StopCellularNetwork ( void ) { le_onoff_t radioState; le_result_t result; result=le_mrc_GetRadioPower(&radioState); if ((result == LE_OK) && (radioState == LE_OFF)) { return; } else { // Try to shutdown the radio anyway le_mrc_SetRadioPower(LE_OFF); // Set a timer that gets the current position. le_timer_Ref_t stopCellNetTimer = le_timer_Create("StopCellNetTimer"); le_clk_Time_t interval = {5, 0}; // 5 seconds if ( (le_timer_SetHandler(stopCellNetTimer, StopCellNetTimerHandler) != LE_OK) || (le_timer_SetRepeat(stopCellNetTimer, 0) != LE_OK) || (le_timer_SetInterval(stopCellNetTimer, interval) != LE_OK) || (le_timer_Start(stopCellNetTimer) != LE_OK) ) { LE_ERROR("Could not start the StopCellNet timer!"); } } }
//-------------------------------------------------------------------------------------------------- static void StopCellNetTimerHandler ( le_timer_Ref_t timerRef ) { le_onoff_t radioState; le_result_t result; if(RequestCount != 0) { // Request has been requested in the meantime, I must cancel the Release command process le_timer_Delete(timerRef); } else { result=le_mrc_GetRadioPower(&radioState); if ((result == LE_OK) && (radioState == LE_OFF)) { // The radio is OFF, stop and delete the Timer. le_timer_Delete(timerRef); } else { le_mrc_SetRadioPower(LE_OFF); // TODO: find a solution to get off of this infinite loop } } }
//-------------------------------------------------------------------------------------------------- static le_cellnet_State_t TranslateToCellNetState ( le_mrc_NetRegState_t state ) { le_sim_Id_t simSelected = le_sim_GetSelectedCard(); // Check if the SIM card is present if (!le_sim_IsPresent(simSelected)) { // SIM card absent return LE_CELLNET_SIM_ABSENT; } // SIM card present, translate the MRC network state switch(state) { case LE_MRC_REG_NONE: { le_onoff_t radioState; le_result_t result; // In this state, the radio should be OFF. if ((result = le_mrc_GetRadioPower(&radioState)) != LE_OK) { LE_WARN("Failed to get the radio power. Result: %d", result); return LE_CELLNET_REG_UNKNOWN; } else { if (radioState == LE_OFF) { // The radio is OFF return LE_CELLNET_RADIO_OFF; } else { // The radio is ON return LE_CELLNET_REG_EMERGENCY; } } } case LE_MRC_REG_SEARCHING: case LE_MRC_REG_DENIED: return LE_CELLNET_REG_EMERGENCY; case LE_MRC_REG_HOME: return LE_CELLNET_REG_HOME; case LE_MRC_REG_ROAMING: return LE_CELLNET_REG_ROAMING; default: return LE_CELLNET_REG_UNKNOWN; } }
//-------------------------------------------------------------------------------------------------- static le_cellnet_State_t TranslateToCellNetState ( le_mrc_NetRegState_t state ) { switch(state) { case LE_MRC_REG_NONE: { le_onoff_t radioState; le_result_t result; // In this state, the radio should be OFF. if ((result = le_mrc_GetRadioPower(&radioState)) != LE_OK) { LE_WARN("Failed to get the radio power. Result: %d", result); return LE_CELLNET_REG_UNKNOWN; } else { if (radioState == LE_OFF) { // The radio is OFF return LE_CELLNET_RADIO_OFF; } else { // The radio is ON return LE_CELLNET_REG_EMERGENCY; } } } case LE_MRC_REG_SEARCHING: case LE_MRC_REG_DENIED: return LE_CELLNET_REG_EMERGENCY; case LE_MRC_REG_HOME: return LE_CELLNET_REG_HOME; case LE_MRC_REG_ROAMING: return LE_CELLNET_REG_ROAMING; default: return LE_CELLNET_REG_UNKNOWN; } }
//------------------------------------------------------------------------------------------------- int cm_mrc_SetRadioPower ( le_onoff_t power ///< [IN] Radio power switch ) { le_result_t res; le_onoff_t currPower; res = le_mrc_GetRadioPower(&currPower); if (res != LE_OK) { return EXIT_FAILURE; } // Don't set if the radio power if it's the same if (currPower == power) { switch (currPower) { case 0: printf("Radio power is already set to OFF.\n"); break; case 1: printf("Radio power is already set to ON.\n"); break; default: break; } } else { res = le_mrc_SetRadioPower(power); if (res != LE_OK) { return EXIT_FAILURE; } } return EXIT_SUCCESS; }
//-------------------------------------------------------------------------------------------------- static void StartCellularNetwork ( void ) { le_onoff_t radioState; le_result_t result; result=le_mrc_GetRadioPower(&radioState); if ((result == LE_OK) && (radioState == LE_ON)) { // Load SIM configuration from secure storage le_sim_Id_t simSelected = le_sim_GetSelectedCard(); if (le_sim_IsPresent(simSelected)) { LoadSimFromSecStore(simSelected); } // Notify the applications even if the SIM is absent GetAndSendCellNetStateEvent(); } else { // Try to power ON the radio anyway le_mrc_SetRadioPower(LE_ON); // Set a timer that gets the current position. le_timer_Ref_t startCellNetTimer = le_timer_Create("StartCellNetTimer"); le_clk_Time_t interval = {15, 0}; // 15 seconds if ( (le_timer_SetHandler(startCellNetTimer, StartCellNetTimerHandler) != LE_OK) || (le_timer_SetRepeat(startCellNetTimer, 0) != LE_OK) || (le_timer_SetInterval(startCellNetTimer, interval) != LE_OK) || (le_timer_Start(startCellNetTimer) != LE_OK) ) { LE_ERROR("Could not start the StartCellNet timer!"); } } }
//-------------------------------------------------------------------------------------------------- static void StartCellNetTimerHandler ( le_timer_Ref_t timerRef ) { le_onoff_t radioState; le_result_t result; if(RequestCount == 0) { // Release has been requested in the meantime, I must cancel the Request command process le_timer_Delete(timerRef); } else { result=le_mrc_GetRadioPower(&radioState); if ((result == LE_OK) && (radioState == LE_ON)) { // The radio is ON, stop and delete the Timer. le_timer_Delete(timerRef); // Load SIM configuration from secure storage le_sim_Id_t simSelected = le_sim_GetSelectedCard(); if (le_sim_IsPresent(simSelected)) { LoadSimFromSecStore(simSelected); } // Notify the applications even if the SIM is absent GetAndSendCellNetStateEvent(); } else { le_mrc_SetRadioPower(LE_ON); // TODO: find a solution to get off of this infinite loop } } }
// ------------------------------------------------------------------------------------------------- static bool ProcessCommand ( const char* textPtr, ///< [IN] Check this text to see if it's a valid command. const char* requesterPtr ///< [IN] If not NULL, then any response text is SMSed to this target. ) { char buffer[10240]; // Start looking for a match... if (strcmp(textPtr, "Crash") == 0) { // As the name implies, we are going to be crashing the application. So simply append to // the output log and crash the app. This is done to allow demonstration of the supervisor // policies. int x = 10; int y = 0; LE_ERROR("Something wicked this way comes..."); LE_ERROR("Data result: %d", x / y); } else if (strcmp(textPtr, "Status") == 0) { // The status command allows the caller to query the current state of the modem. A friendly // version of this status is constructed and returned to the caller. le_onoff_t radioStatus; const char * radioStatusPtr; le_mrc_NetRegState_t netRegState; uint32_t signalQuality; if(le_mrc_GetRadioPower(&radioStatus) != LE_OK) { radioStatusPtr = "in an unknown state"; } else { radioStatusPtr = (radioStatus == LE_OFF) ? "off" : "on"; } if(le_mrc_GetNetRegState(&netRegState) != LE_OK) { netRegState = LE_MRC_REG_UNKNOWN; } if(le_mrc_GetSignalQual(&signalQuality) != LE_OK) { signalQuality = 0; } sprintf(buffer, "The radio is %s and is %s. The signal strength is %s.", radioStatusPtr, GetNetStateString(netRegState), GetSignalString(signalQuality)); } else if (strcmp(textPtr, "Sim") == 0) { // Like the status command, this command queries the underling hardware for information. // This information is turned into a string that can then be returned to the caller. le_sim_Id_t simId; le_sim_States_t simState; char iccid[100]; char imsi[100]; int pos = 0; simId = le_sim_GetSelectedCard(); simState = le_sim_GetState(simId); pos += snprintf(buffer + pos, sizeof(buffer) - pos, "SIM %u is %s.", simId, GetSimStateString(simState)); if(le_sim_GetICCID(simId, iccid, sizeof(iccid)) == LE_OK) { pos += snprintf(buffer + pos, sizeof(buffer) - pos, " ICCID=%s", iccid); } if(le_sim_GetIMSI(simId, imsi, sizeof(imsi)) == LE_OK) { pos += snprintf(buffer + pos, sizeof(buffer) - pos, " IMSI=%s", imsi); } } else if (strcmp(textPtr, "Online") == 0) { le_utf8_Copy(buffer, "Requesting data connection.", sizeof(buffer), NULL); GoOnline(); } else if (strcmp(textPtr, "Offline") == 0) { le_utf8_Copy(buffer, "Releasing data connection.", sizeof(buffer), NULL); GoOffline(buffer); } else if (strcmp(textPtr, "TestDataConnectionV4") == 0) { TestDataConnectionV4(buffer); } else if (strcmp(textPtr, "TestDataConnectionV6") == 0) { TestDataConnectionV6(buffer); } else if (strcmp(textPtr, "Netinfo") == 0) { Netinfo(buffer); } else if (strcmp(textPtr, "DataInfo") == 0) { Datainfo(buffer); } else if (strcmp(textPtr, "DataReset") == 0) { DataReset(buffer); } else if (strcmp(textPtr, "Scan") == 0) { PerformScan(buffer, sizeof(buffer), requesterPtr); } else { return false; } // Check to see if any processing has occurred. If so, check to see if the request came from a // local or remote requester. // If the requester was local, (requesterPtr == NULL) then simply log our response to the SMS log. // Otherwise, attempt to SMS the response string to the original caller. if (requesterPtr != NULL) { SendMessage(requesterPtr, buffer); } else if (OutputFilePtr != NULL) { fprintf(OutputFilePtr, "## %s ##\n", buffer); fflush(OutputFilePtr); } // Let the calling function know if we did any processing or not. return true; }