ublox-cellular-base_r4
Diff: UbloxCellularBase.cpp
- Revision:
- 24:e26a6ab0dd75
- Parent:
- 23:eaab8e812a5d
- Child:
- 25:e67d3d9d2e7e
diff -r eaab8e812a5d -r e26a6ab0dd75 UbloxCellularBase.cpp --- a/UbloxCellularBase.cpp Fri May 03 13:43:49 2019 +0500 +++ b/UbloxCellularBase.cpp Tue May 28 15:39:51 2019 +0500 @@ -802,29 +802,35 @@ if (pin != NULL) { _pin = pin; } - if (initialise_sim_card()) { +#ifdef TARGET_UBLOX_C027 + if (set_functionality_mode(FUNC_MIN)) { +#else + if (set_functionality_mode(FUNC_AIRPLANE)) { +#endif + if (initialise_sim_card()) { #ifdef TARGET_UBLOX_C030_R412M - if (_psm_status == false) { //psm is not enabled by application yet so disable it at start-up - set_power_saving_mode(0, 0); - } + if (_psm_status == false) { //psm is not enabled by application yet so disable it at start-up + set_power_saving_mode(0, 0); + } #endif - if (set_device_identity(&_dev_info.dev) && // Set up device identity - device_init(_dev_info.dev)) {// Initialise this device - // Get the integrated circuit ID of the SIM - if (get_iccid()) { - // Try a few times to get the IMSI (since on some modems this can - // take a while to be retrieved, especially if a SIM PIN - // was set) - for (x = 0; (x < 3) && !get_imsi(); x++) { - wait_ms(1000); - } - - if (x < 3) { // If we got the IMSI, can get the others - if (get_imei() && // Get international mobile equipment identifier - get_meid() && // Probably the same as the IMEI - set_sms()) { // And set up SMS - // The modem is initialised. - _modem_initialised = true; + if (set_device_identity(&_dev_info.dev) && // Set up device identity + device_init(_dev_info.dev)) {// Initialise this device + // Get the integrated circuit ID of the SIM + if (get_iccid()) { + // Try a few times to get the IMSI (since on some modems this can + // take a while to be retrieved, especially if a SIM PIN + // was set) + for (x = 0; (x < 3) && !get_imsi(); x++) { + wait_ms(1000); + } + + if (x < 3) { // If we got the IMSI, can get the others + if (get_imei() && // Get international mobile equipment identifier + get_meid() && // Probably the same as the IMEI + set_sms()) { // And set up SMS + // The modem is initialised. + _modem_initialised = true; + } } } } @@ -850,58 +856,60 @@ MBED_ASSERT(_at != NULL); if (!is_registered_psd() && !is_registered_csd() && !is_registered_eps()) { - tr_info("Searching Network..."); - // Enable the packet switched and network registration unsolicited result codes - if (_at->send("AT+CREG=1") && _at->recv("OK") && - _at->send("AT+CGREG=1") && _at->recv("OK")) { - atSuccess = true; - if (_at->send("AT+CEREG=1")) { - _at->recv("OK"); - // Don't check return value as this works for LTE only - } - - if (atSuccess) { - // See if we are already in automatic mode - if (_at->send("AT+COPS?") && _at->recv("+COPS: %d", &status) && - _at->recv("OK")) { - // If not, set it - if (status != 0) { - // Don't check return code here as there's not much - // we can do if this fails. - _at->send("AT+COPS=0") && _at->recv("OK"); - } - } - - // Query the registration status directly as well, - // just in case - if (_at->send("AT+CREG?") && _at->recv("OK")) { - // Answer will be processed by URC - } - if (_at->send("AT+CGREG?") && _at->recv("OK")) { - // Answer will be processed by URC - } - if (_at->send("AT+CEREG?")) { + if (set_functionality_mode(FUNC_FULL)) { + tr_info("Searching Network..."); + // Enable the packet switched and network registration unsolicited result codes + if (_at->send("AT+CREG=1") && _at->recv("OK") && + _at->send("AT+CGREG=1") && _at->recv("OK")) { + atSuccess = true; + if (_at->send("AT+CEREG=1")) { _at->recv("OK"); // Don't check return value as this works for LTE only } + + if (atSuccess) { + // See if we are already in automatic mode + if (_at->send("AT+COPS?") && _at->recv("+COPS: %d", &status) && + _at->recv("OK")) { + // If not, set it + if (status != 0) { + // Don't check return code here as there's not much + // we can do if this fails. + _at->send("AT+COPS=0") && _at->recv("OK"); + } + } + + // Query the registration status directly as well, + // just in case + if (_at->send("AT+CREG?") && _at->recv("OK")) { + // Answer will be processed by URC + } + if (_at->send("AT+CGREG?") && _at->recv("OK")) { + // Answer will be processed by URC + } + if (_at->send("AT+CEREG?")) { + _at->recv("OK"); + // Don't check return value as this works for LTE only + } + } } - } - // Wait for registration to succeed - at_set_timeout(1000); - for (int waitSeconds = 0; !registered && (waitSeconds < 180); waitSeconds++) { - registered = is_registered_psd() || is_registered_csd() || is_registered_eps(); - _at->recv(UNNATURAL_STRING); - } - at_set_timeout(at_timeout); - - if (registered) { - // This should return quickly but sometimes the status field is not returned - // so make the timeout short + // Wait for registration to succeed at_set_timeout(1000); - if (_at->send("AT+COPS?") && _at->recv("+COPS: %*d,%*d,\"%*[^\"]\",%d\n", &status)) { - set_rat(status); + for (int waitSeconds = 0; !registered && (waitSeconds < 180); waitSeconds++) { + registered = is_registered_psd() || is_registered_csd() || is_registered_eps(); + _at->recv(UNNATURAL_STRING); } at_set_timeout(at_timeout); + + if (registered) { + // This should return quickly but sometimes the status field is not returned + // so make the timeout short + at_set_timeout(1000); + if (_at->send("AT+COPS?") && _at->recv("+COPS: %*d,%*d,\"%*[^\"]\",%d\n", &status)) { + set_rat(status); + } + at_set_timeout(at_timeout); + } } } else { registered = true; @@ -1190,6 +1198,11 @@ //application should call init() or connect() in order to initialize the modem bool UbloxCellularBase::reboot_modem() { + return (set_functionality_mode(FUNC_RESET)); +} + +bool UbloxCellularBase::set_functionality_mode(FunctionalityMode mode) +{ bool return_val = false; int at_timeout; LOCK(); @@ -1198,23 +1211,40 @@ at_timeout = _at_timeout; // Has to be inside LOCK()s at_set_timeout(3*60*1000); //command has 3 minutes timeout - tr_info("rebooting modem..."); - if (_at->send("AT+CFUN=15") && _at->recv("OK")) { - tr_info("reboot successful"); + if (_at->send("AT+CFUN=%d", mode) && _at->recv("OK")) { return_val = true; } - _dev_info.reg_status_csd = CSD_NOT_REGISTERED_NOT_SEARCHING; - _dev_info.reg_status_psd = PSD_NOT_REGISTERED_NOT_SEARCHING; - _dev_info.reg_status_eps = EPS_NOT_REGISTERED_NOT_SEARCHING; - _modem_initialised = false; + if (mode == FUNC_RESET || mode == FUNC_RESET_WITH_SIM) { + _modem_initialised = false; + } + at_set_timeout(at_timeout); UNLOCK(); return return_val; } +bool UbloxCellularBase::get_functionality_mode(int *mode) +{ + bool return_val = false; + + if (mode == NULL) { + return false; + } + + LOCK(); + MBED_ASSERT(_at != NULL); + + if ( (_at->send("AT+CFUN?") && _at->recv("+CFUN: %d", mode) && _at->recv("OK")) ) { + return_val = true; + } + + UNLOCK(); + return return_val; +} + #ifdef TARGET_UBLOX_C030_R41XM bool UbloxCellularBase::set_mno_profile(MNOProfile profile) {