Beispiel #1
0
nsapi_error_t EasyCellularConnection::connect()
{
    nsapi_error_t err = check_connect();
    if (err) {
        return err;
    }
#if USE_APN_LOOKUP
    if (!_credentials_set) {
        _target_state = CellularConnectionFSM::STATE_SIM_PIN;
        err = _cellularConnectionFSM->continue_to_state(_target_state);
        if (err == NSAPI_ERROR_OK) {
            int sim_wait = _cellularSemaphore.wait(60 * 1000); // reserve 60 seconds to access to SIM
            if (sim_wait != 1) {
                tr_error("NO SIM ACCESS");
                err = NSAPI_ERROR_NO_CONNECTION;
            } else {
                char imsi[MAX_IMSI_LENGTH + 1];
                wait(1); // need to wait to access SIM in some modems
                err = _cellularConnectionFSM->get_sim()->get_imsi(imsi);
                if (err == NSAPI_ERROR_OK) {
                    const char *apn_config = apnconfig(imsi);
                    if (apn_config) {
                        const char* apn = _APN_GET(apn_config);
                        const char* uname = _APN_GET(apn_config);
                        const char* pwd = _APN_GET(apn_config);
                        tr_info("Looked up APN %s", apn);
                        err = _cellularConnectionFSM->get_network()->set_credentials(apn, uname, pwd);
                    }
                }
            }
        }
        if (err) {
            tr_error("APN lookup failed");
            return err;
        }
    }
#endif // USE_APN_LOOKUP

    _target_state = CellularConnectionFSM::STATE_CONNECTED;
    err = _cellularConnectionFSM->continue_to_state(_target_state);
    if (err == NSAPI_ERROR_OK) {
        int ret_wait = _cellularSemaphore.wait(10 * 60 * 1000); // cellular network searching may take several minutes
        if (ret_wait != 1) {
            tr_info("No cellular connection");
            err = NSAPI_ERROR_NO_CONNECTION;
        }
    }

    return err;
}
nsapi_error_t PPPCellularInterface::connect()
{
    nsapi_error_t retcode;
    bool success;
    bool did_init = false;
    const char *apn_config = NULL;

    if (dev_info.ppp_connection_up) {
        return NSAPI_ERROR_IS_CONNECTED;
    }

    do {
        retry_init:

        /* setup AT parser */
        setup_at_parser();

        if (!initialized) {

            /* If we have hangup (eg DCD) detection, we don't want it active
             * as long as we are using ATCmdParser.
             * As soon as we get into data mode, we will turn it back on. */
            enable_hup(false);

            if (!power_up()) {
                return NSAPI_ERROR_DEVICE_ERROR;
            }

            retcode = initialize_sim_card();
            if (retcode != NSAPI_ERROR_OK) {
                return retcode;
            }

            success = nwk_registration(PACKET_SWITCHED) //perform network registration
            && get_CCID(_at)//get integrated circuit ID of the SIM
            && get_IMSI(_at)//get international mobile subscriber information
            && get_IMEI(_at)//get international mobile equipment identifier
            && get_MEID(_at)//its same as IMEI
            && set_CMGF(_at)//set message format for SMS
            && set_CNMI(_at);//set new SMS indication

            if (!success) {
                return NSAPI_ERROR_NO_CONNECTION;
            }

#if MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP
            if (!apn_config) {
                apn_config = apnconfig(dev_info.imsi);
            }
#endif

            /* Check if user want skip SIM pin checking on boot up */
            if (set_sim_pin_check_request) {
                retcode = do_sim_pin_check(_at, _pin);
                if (retcode != NSAPI_ERROR_OK) {
                    return retcode;
                }
                /* set this request to false, as it is unnecessary to repeat in case of retry */
                set_sim_pin_check_request = false;
            }

            /* check if the user requested a sim pin change */
            if (change_pin) {
                retcode = do_change_sim_pin(_at, _pin, _new_pin);
                if (retcode != NSAPI_ERROR_OK) {
                    return retcode;
                }
                /* set this request to false, as it is unnecessary to repeat in case of retry */
                change_pin = false;
            }

#if MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP
            if (apn_config) {
                _apn = _APN_GET(apn_config);
                _uname = _APN_GET(apn_config);
                _pwd = _APN_GET(apn_config);
                tr_info("Looked up APN %s.", _apn);
            }
#endif

            //sets up APN and IP protocol for external PDP context
            retcode = setup_context_and_credentials();
            if (retcode != NSAPI_ERROR_OK) {
                return retcode;
            }

            if (!success) {
                shutdown_at_parser();
                return NSAPI_ERROR_NO_CONNECTION;
            }

            initialized = true;
            did_init = true;
        } else {
            /* If we were already initialized, we expect to receive NO_CARRIER response
             * from the modem as we were kicked out of Data mode */
            _at->recv("NO CARRIER");
            success = _at->send("AT") && _at->recv("OK");
        }

        /* Attempt to enter data mode */
        success = set_atd(_at); //enter into Data mode with the modem
        if (!success) {
            power_down();
            initialized = false;

            /* if we were previously initialized , i.e., not in this particular attempt,
             * we want to re-initialize */
            if (!did_init) {
                goto retry_init;
            }

            /* shutdown AT parser before notifying application of the failure */
            shutdown_at_parser();

            return NSAPI_ERROR_NO_CONNECTION;
        }

        /* This is the success case.
         * Save RAM, discard AT Parser as we have entered Data mode. */
        shutdown_at_parser();

        /* We now want hangup (e.g., DCD) detection if available */
        enable_hup(true);

        /* Initialize PPP
         * mbed_ppp_init() is a blocking call, it will block until
         * connected, or timeout after 30 seconds*/
        retcode = nsapi_ppp_connect(_fh, _connection_status_cb, _uname, _pwd, _stack);
        if (retcode == NSAPI_ERROR_OK) {
            dev_info.ppp_connection_up = true;
        }

    }while(!dev_info.ppp_connection_up && apn_config && *apn_config);

    return retcode;
}