void Nanostack::Interface::network_handler(mesh_connection_status_t status) { if ((status == MESH_CONNECTED || status == MESH_CONNECTED_LOCAL || status == MESH_CONNECTED_GLOBAL) && _blocking) { connect_semaphore.release(); } if (status == MESH_CONNECTED) { uint8_t temp_ipv6_global[16]; uint8_t temp_ipv6_local[16]; if (arm_net_address_get(interface_id, ADDR_IPV6_LL, temp_ipv6_local) == 0) { _connect_status = NSAPI_STATUS_LOCAL_UP; } if (arm_net_address_get(interface_id, ADDR_IPV6_GP, temp_ipv6_global) == 0 && (memcmp(temp_ipv6_global, temp_ipv6_local, 16) != 0)) { _connect_status = NSAPI_STATUS_GLOBAL_UP; } } else if (status == MESH_CONNECTED_LOCAL) { _connect_status = NSAPI_STATUS_LOCAL_UP; } else if (status == MESH_CONNECTED_GLOBAL) { _connect_status = NSAPI_STATUS_GLOBAL_UP; } else if (status == MESH_BOOTSTRAP_STARTED || status == MESH_BOOTSTRAP_FAILED) { _connect_status = NSAPI_STATUS_CONNECTING; } else { _connect_status = NSAPI_STATUS_DISCONNECTED; } if (_connection_status_cb && _previous_connection_status != _connect_status) { _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connect_status); } _previous_connection_status = _connect_status; }
void PPPCellularInterface::ppp_status_cb(nsapi_event_t event, intptr_t parameter) { _connect_status = (nsapi_connection_status_t)parameter; if (_connection_status_cb) { _connection_status_cb(event, parameter); } }
nsapi_error_t PPPCellularInterface::connect() { nsapi_error_t retcode; bool success; bool did_init = false; const char *apn_config = NULL; bool user_specified_apn = false; /* If the user has specified the APN then use that or, * if we are not using the APN database, set _apn to * "internet" as a best guess */ if (_apn) { user_specified_apn = true; } else { #ifndef MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP _apn = "internet"; user_specified_apn = true; #endif } if (is_connected()) { return NSAPI_ERROR_IS_CONNECTED; } else if (_connect_status == NSAPI_STATUS_CONNECTING) { return NSAPI_ERROR_ALREADY; } _connect_status = NSAPI_STATUS_CONNECTING; if (_connection_status_cb) { _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_CONNECTING); } do { retry_init: retcode = NSAPI_ERROR_OK; /* 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()) { retcode = NSAPI_ERROR_DEVICE_ERROR; break; } retcode = initialize_sim_card(); if (retcode != NSAPI_ERROR_OK) { break; } 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) { retcode = NSAPI_ERROR_NO_CONNECTION; break; } #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) { break; } /* 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) { break; } /* 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 (!user_specified_apn && 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) { break; } if (!success) { shutdown_at_parser(); retcode = NSAPI_ERROR_NO_CONNECTION; break; } 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"); } tr_info("The APN being used is %s.\n", _apn); /* 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(); retcode = NSAPI_ERROR_NO_CONNECTION; break; } /* 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, callback(this, &PPPCellularInterface::ppp_status_cb), _uname, _pwd, _stack); if (retcode == NSAPI_ERROR_OK && _connect_is_blocking) { _connect_status = NSAPI_STATUS_GLOBAL_UP; } } while ((_connect_status == NSAPI_STATUS_CONNECTING && _connect_is_blocking) && apn_config && *apn_config); if (retcode != NSAPI_ERROR_OK) { _connect_status = NSAPI_STATUS_DISCONNECTED; if (_connection_status_cb) { _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); } } return retcode; }