blob: 3d658764a8d5b1a5e552434decf83c554a40bf59 [file] [log] [blame]
// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "shill/cellular.h"
#include <netinet/in.h>
#include <linux/if.h> // Needs definitions from netinet/in.h
#include <string>
#include <utility>
#include <vector>
#include <base/logging.h>
#include <base/stringprintf.h>
#include <chromeos/dbus/service_constants.h>
#include <mm/mm-modem.h>
#include <mobile_provider.h>
#include "shill/cellular_capability_cdma.h"
#include "shill/cellular_capability_gsm.h"
#include "shill/cellular_service.h"
#include "shill/control_interface.h"
#include "shill/device.h"
#include "shill/device_info.h"
#include "shill/error.h"
#include "shill/event_dispatcher.h"
#include "shill/manager.h"
#include "shill/modem_simple_proxy_interface.h"
#include "shill/profile.h"
#include "shill/property_accessor.h"
#include "shill/proxy_factory.h"
#include "shill/rtnl_handler.h"
using std::make_pair;
using std::map;
using std::string;
using std::vector;
namespace shill {
const char Cellular::kConnectPropertyPhoneNumber[] = "number";
const char Cellular::kPhoneNumberCDMA[] = "#777";
const char Cellular::kPhoneNumberGSM[] = "*99#";
Cellular::Operator::Operator() {
SetName("");
SetCode("");
SetCountry("");
}
Cellular::Operator::~Operator() {}
void Cellular::Operator::CopyFrom(const Operator &oper) {
dict_ = oper.dict_;
}
const string &Cellular::Operator::GetName() const {
return dict_.find(flimflam::kOperatorNameKey)->second;
}
void Cellular::Operator::SetName(const string &name) {
dict_[flimflam::kOperatorNameKey] = name;
}
const string &Cellular::Operator::GetCode() const {
return dict_.find(flimflam::kOperatorCodeKey)->second;
}
void Cellular::Operator::SetCode(const string &code) {
dict_[flimflam::kOperatorCodeKey] = code;
}
const string &Cellular::Operator::GetCountry() const {
return dict_.find(flimflam::kOperatorCountryKey)->second;
}
void Cellular::Operator::SetCountry(const string &country) {
dict_[flimflam::kOperatorCountryKey] = country;
}
const Stringmap &Cellular::Operator::ToDict() const {
return dict_;
}
Cellular::CDMA::CDMA()
: activation_state(MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED),
prl_version(0) {}
Cellular::GSM::GSM()
: access_technology(MM_MODEM_GSM_ACCESS_TECH_UNKNOWN) {}
Cellular::Cellular(ControlInterface *control_interface,
EventDispatcher *dispatcher,
Manager *manager,
const string &link_name,
const string &address,
int interface_index,
Type type,
const string &owner,
const string &path,
mobile_provider_db *provider_db)
: Device(control_interface,
dispatcher,
manager,
link_name,
address,
interface_index),
proxy_factory_(ProxyFactory::GetInstance()),
type_(type),
state_(kStateDisabled),
modem_state_(kModemStateUnknown),
dbus_owner_(owner),
dbus_path_(path),
provider_db_(provider_db),
task_factory_(this),
allow_roaming_(false) {
PropertyStore *store = this->mutable_store();
store->RegisterConstString(flimflam::kCarrierProperty, &carrier_);
store->RegisterConstString(flimflam::kDBusConnectionProperty, &dbus_owner_);
store->RegisterConstString(flimflam::kDBusObjectProperty, &dbus_path_);
store->RegisterBool(flimflam::kCellularAllowRoamingProperty, &allow_roaming_);
store->RegisterConstString(flimflam::kEsnProperty, &esn_);
store->RegisterConstString(flimflam::kFirmwareRevisionProperty,
&firmware_revision_);
store->RegisterConstString(flimflam::kHardwareRevisionProperty,
&hardware_revision_);
store->RegisterConstStringmap(flimflam::kHomeProviderProperty,
&home_provider_.ToDict());
store->RegisterConstString(flimflam::kImeiProperty, &imei_);
store->RegisterConstString(flimflam::kImsiProperty, &imsi_);
store->RegisterConstString(flimflam::kManufacturerProperty, &manufacturer_);
store->RegisterConstString(flimflam::kMdnProperty, &mdn_);
store->RegisterConstString(flimflam::kMeidProperty, &meid_);
store->RegisterConstString(flimflam::kMinProperty, &min_);
store->RegisterConstString(flimflam::kModelIDProperty, &model_id_);
store->RegisterConstUint16(flimflam::kPRLVersionProperty, &cdma_.prl_version);
HelpRegisterDerivedStrIntPair(flimflam::kSIMLockStatusProperty,
&Cellular::SimLockStatusToProperty,
NULL);
VLOG(2) << "Cellular device " << this->link_name() << " initialized: "
<< GetTypeString();
}
Cellular::~Cellular() {}
string Cellular::GetTypeString() const {
switch (type_) {
case kTypeGSM: return "CellularTypeGSM";
case kTypeCDMA: return "CellularTypeCDMA";
default: NOTREACHED();
}
return StringPrintf("CellularTypeUnknown-%d", type_);
}
// static
string Cellular::GetStateString(State state) {
switch (state) {
case kStateDisabled: return "CellularStateDisabled";
case kStateEnabled: return "CellularStateEnabled";
case kStateRegistered: return "CellularStateRegistered";
case kStateConnected: return "CellularStateConnected";
case kStateLinked: return "CellularStateLinked";
default: NOTREACHED();
}
return StringPrintf("CellularStateUnknown-%d", state);
}
// static
string Cellular::GetCDMAActivationStateString(uint32 state) {
switch (state) {
case MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATED:
return flimflam::kActivationStateActivated;
case MM_MODEM_CDMA_ACTIVATION_STATE_ACTIVATING:
return flimflam::kActivationStateActivating;
case MM_MODEM_CDMA_ACTIVATION_STATE_NOT_ACTIVATED:
return flimflam::kActivationStateNotActivated;
case MM_MODEM_CDMA_ACTIVATION_STATE_PARTIALLY_ACTIVATED:
return flimflam::kActivationStatePartiallyActivated;
default:
return flimflam::kActivationStateUnknown;
}
}
// static
string Cellular::GetCDMAActivationErrorString(uint32 error) {
switch (error) {
case MM_MODEM_CDMA_ACTIVATION_ERROR_WRONG_RADIO_INTERFACE:
return flimflam::kErrorNeedEvdo;
case MM_MODEM_CDMA_ACTIVATION_ERROR_ROAMING:
return flimflam::kErrorNeedHomeNetwork;
case MM_MODEM_CDMA_ACTIVATION_ERROR_COULD_NOT_CONNECT:
case MM_MODEM_CDMA_ACTIVATION_ERROR_SECURITY_AUTHENTICATION_FAILED:
case MM_MODEM_CDMA_ACTIVATION_ERROR_PROVISIONING_FAILED:
return flimflam::kErrorOtaspFailed;
case MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR:
return "";
case MM_MODEM_CDMA_ACTIVATION_ERROR_NO_SIGNAL:
default:
return flimflam::kErrorActivationFailed;
}
}
void Cellular::SetState(State state) {
VLOG(2) << GetStateString(state_) << " -> " << GetStateString(state);
state_ = state;
}
void Cellular::Start() {
LOG(INFO) << __func__ << ": " << GetStateString(state_);
Device::Start();
InitCapability(); // For now, only a single capability is supported.
InitProxies();
EnableModem();
capability_->Register();
GetModemStatus();
capability_->GetIdentifiers();
capability_->GetProperties();
GetModemInfo();
capability_->GetRegistrationState();
}
void Cellular::Stop() {
capability_.reset();
proxy_.reset();
simple_proxy_.reset();
manager()->DeregisterService(service_);
service_ = NULL; // Breaks a reference cycle.
SetState(kStateDisabled);
Device::Stop();
}
void Cellular::InitCapability() {
VLOG(2) << __func__;
switch (type_) {
case kTypeGSM:
capability_.reset(new CellularCapabilityGSM(this));
break;
case kTypeCDMA:
capability_.reset(new CellularCapabilityCDMA(this));
break;
default: NOTREACHED();
}
}
void Cellular::InitProxies() {
VLOG(2) << __func__;
proxy_.reset(proxy_factory_->CreateModemProxy(this, dbus_path_, dbus_owner_));
simple_proxy_.reset(
proxy_factory_->CreateModemSimpleProxy(dbus_path_, dbus_owner_));
capability_->InitProxies();
}
void Cellular::EnableModem() {
CHECK_EQ(kStateDisabled, state_);
// TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
proxy_->Enable(true);
SetState(kStateEnabled);
}
void Cellular::GetModemStatus() {
CHECK_EQ(kStateEnabled, state_);
// TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
DBusPropertiesMap properties = simple_proxy_->GetStatus();
if (DBusProperties::GetString(properties, "carrier", &carrier_) &&
type_ == kTypeCDMA) {
home_provider_.SetName(carrier_);
home_provider_.SetCode("");
home_provider_.SetCountry("us");
}
DBusProperties::GetString(properties, "meid", &meid_);
DBusProperties::GetString(properties, "imei", &imei_);
if (DBusProperties::GetString(properties, "imsi", &imsi_) &&
type_ == kTypeGSM) {
// TODO(petkov): Set GSM provider based on IMSI and SPN.
}
DBusProperties::GetString(properties, "esn", &esn_);
DBusProperties::GetString(properties, "mdn", &mdn_);
DBusProperties::GetString(properties, "min", &min_);
DBusProperties::GetString(
properties, "firmware_revision", &firmware_revision_);
uint32 state = 0;
if (DBusProperties::GetUint32(properties, "state", &state)) {
modem_state_ = static_cast<ModemState>(state);
}
if (type_ == kTypeCDMA) {
DBusProperties::GetUint32(
properties, "activation_state", &cdma_.activation_state);
DBusProperties::GetUint16(properties, "prl_version", &cdma_.prl_version);
// TODO(petkov): For now, get the payment and usage URLs from ModemManager
// to match flimflam. In the future, provide a plugin API to get these
// directly from the modem driver.
DBusProperties::GetString(properties, "payment_url", &cdma_.payment_url);
DBusProperties::GetString(properties, "usage_url", &cdma_.usage_url);
}
}
void Cellular::Activate(const std::string &carrier, Error *error) {
capability_->Activate(carrier, error);
}
void Cellular::RegisterOnNetwork(const string &network_id, Error *error) {
capability_->RegisterOnNetwork(network_id, error);
}
void Cellular::RequirePIN(const string &pin, bool require, Error *error) {
capability_->RequirePIN(pin, require, error);
}
void Cellular::EnterPIN(const string &pin, Error *error) {
capability_->EnterPIN(pin, error);
}
void Cellular::UnblockPIN(
const string &unblock_code, const string &pin, Error *error) {
capability_->UnblockPIN(unblock_code, pin, error);
}
void Cellular::ChangePIN(
const string &old_pin, const string &new_pin, Error *error) {
capability_->ChangePIN(old_pin, new_pin, error);
}
void Cellular::Scan(Error *error) {
capability_->Scan(error);
}
void Cellular::GetModemInfo() {
// TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
ModemProxyInterface::Info info = proxy_->GetInfo();
manufacturer_ = info._1;
model_id_ = info._2;
hardware_revision_ = info._3;
VLOG(2) << "ModemInfo: " << manufacturer_ << ", " << model_id_ << ", "
<< hardware_revision_;
}
void Cellular::HandleNewRegistrationState() {
dispatcher()->PostTask(
task_factory_.NewRunnableMethod(
&Cellular::HandleNewRegistrationStateTask));
}
void Cellular::HandleNewRegistrationStateTask() {
VLOG(2) << __func__;
const string network_tech = capability_->GetNetworkTechnologyString();
if (network_tech.empty()) {
if (state_ == kStateLinked) {
manager()->DeregisterService(service_);
}
service_ = NULL;
if (state_ == kStateLinked ||
state_ == kStateConnected ||
state_ == kStateRegistered) {
SetState(kStateEnabled);
}
return;
}
if (state_ == kStateEnabled) {
SetState(kStateRegistered);
}
if (!service_.get()) {
// For now, no endpoint is created. Revisit if necessary.
CreateService();
}
capability_->GetSignalQuality();
if (state_ == kStateRegistered && modem_state_ == kModemStateConnected) {
SetState(kStateConnected);
EstablishLink();
}
service_->set_network_tech(network_tech);
service_->set_roaming_state(capability_->GetRoamingStateString());
}
void Cellular::HandleNewSignalQuality(uint32 strength) {
VLOG(2) << "Signal strength: " << strength;
if (service_.get()) {
service_->set_strength(strength);
}
}
void Cellular::CreateService() {
VLOG(2) << __func__;
CHECK(!service_.get());
service_ =
new CellularService(control_interface(), dispatcher(), manager(), this);
switch (type_) {
case kTypeGSM:
service_->set_activation_state(flimflam::kActivationStateActivated);
UpdateServingOperator();
break;
case kTypeCDMA:
service_->set_payment_url(cdma_.payment_url);
service_->set_usage_url(cdma_.usage_url);
UpdateServingOperator();
HandleNewCDMAActivationState(MM_MODEM_CDMA_ACTIVATION_ERROR_NO_ERROR);
break;
default:
NOTREACHED();
}
}
bool Cellular::TechnologyIs(const Technology::Identifier type) const {
return type == Technology::kCellular;
}
void Cellular::Connect(Error *error) {
VLOG(2) << __func__;
if (state_ == kStateConnected ||
state_ == kStateLinked) {
Error::PopulateAndLog(error, Error::kAlreadyConnected,
"Already connected; connection request ignored.");
return;
}
CHECK_EQ(kStateRegistered, state_);
if (!allow_roaming_ &&
service_->roaming_state() == flimflam::kRoamingStateRoaming) {
Error::PopulateAndLog(error, Error::kNotOnHomeNetwork,
"Roaming disallowed; connection request ignored.");
CHECK(error);
return;
}
DBusPropertiesMap properties;
const char *phone_number = NULL;
switch (type_) {
case kTypeGSM:
phone_number = kPhoneNumberGSM;
break;
case kTypeCDMA:
phone_number = kPhoneNumberCDMA;
break;
default: NOTREACHED();
}
properties[kConnectPropertyPhoneNumber].writer().append_string(phone_number);
// TODO(petkov): Setup apn and "home_only".
// Defer connect because we may be in a dbus-c++ callback.
dispatcher()->PostTask(
task_factory_.NewRunnableMethod(&Cellular::ConnectTask, properties));
}
void Cellular::ConnectTask(const DBusPropertiesMap &properties) {
VLOG(2) << __func__;
// TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
simple_proxy_->Connect(properties);
SetState(kStateConnected);
EstablishLink();
}
void Cellular::EstablishLink() {
VLOG(2) << __func__;
CHECK_EQ(kStateConnected, state_);
unsigned int flags = 0;
if (manager()->device_info()->GetFlags(interface_index(), &flags) &&
(flags & IFF_UP) != 0) {
LinkEvent(flags, IFF_UP);
return;
}
// TODO(petkov): Provide a timeout for a failed link-up request.
rtnl_handler()->SetInterfaceFlags(interface_index(), IFF_UP, IFF_UP);
}
void Cellular::LinkEvent(unsigned int flags, unsigned int change) {
Device::LinkEvent(flags, change);
if ((flags & IFF_UP) != 0 && state_ == kStateConnected) {
LOG(INFO) << link_name() << " is up.";
SetState(kStateLinked);
manager()->RegisterService(service_);
// TODO(petkov): For GSM, remember the APN.
if (AcquireDHCPConfig()) {
SelectService(service_);
SetServiceState(Service::kStateConfiguring);
} else {
LOG(ERROR) << "Unable to acquire DHCP config.";
}
} else if ((flags & IFF_UP) == 0 && state_ == kStateLinked) {
SetState(kStateConnected);
manager()->DeregisterService(service_);
SelectService(NULL);
DestroyIPConfig();
}
}
void Cellular::HandleNewCDMAActivationState(uint32 error) {
if (!service_.get()) {
return;
}
service_->set_activation_state(
GetCDMAActivationStateString(cdma_.activation_state));
service_->set_error(GetCDMAActivationErrorString(error));
}
void Cellular::OnModemStateChanged(uint32 /*old_state*/,
uint32 /*new_state*/,
uint32 /*reason*/) {
// TODO(petkov): Implement this.
NOTIMPLEMENTED();
}
void Cellular::SetGSMAccessTechnology(uint32 access_technology) {
CHECK_EQ(kTypeGSM, type_);
gsm_.access_technology = access_technology;
if (service_.get()) {
service_->set_network_tech(capability_->GetNetworkTechnologyString());
}
}
void Cellular::UpdateGSMOperatorInfo() {
if (!gsm_.network_id.empty()) {
VLOG(2) << "Looking up network id: " << gsm_.network_id;
mobile_provider *provider =
mobile_provider_lookup_by_network(provider_db_,
gsm_.network_id.c_str());
if (provider) {
const char *provider_name = mobile_provider_get_name(provider);
if (provider_name && *provider_name) {
gsm_.operator_name = provider_name;
gsm_.operator_country = provider->country;
VLOG(2) << "Operator name: " << gsm_.operator_name
<< ", country: " << gsm_.operator_country;
}
} else {
VLOG(2) << "GSM provider not found.";
}
}
UpdateServingOperator();
}
void Cellular::UpdateServingOperator() {
if (!service_.get()) {
return;
}
switch (type_) {
case kTypeGSM: {
Operator oper;
oper.SetName(gsm_.operator_name);
oper.SetCode(gsm_.network_id);
oper.SetCountry(gsm_.operator_country);
service_->set_serving_operator(oper);
break;
}
case kTypeCDMA:
service_->set_serving_operator(home_provider_);
break;
default: NOTREACHED();
}
}
StrIntPair Cellular::SimLockStatusToProperty(Error */*error*/) {
return StrIntPair(make_pair(flimflam::kSIMLockTypeProperty,
sim_lock_status_.lock_type),
make_pair(flimflam::kSIMLockRetriesLeftProperty,
sim_lock_status_.retries_left));
}
void Cellular::HelpRegisterDerivedStrIntPair(
const string &name,
StrIntPair(Cellular::*get)(Error *),
void(Cellular::*set)(const StrIntPair&, Error *)) {
mutable_store()->RegisterDerivedStrIntPair(
name,
StrIntPairAccessor(
new CustomAccessor<Cellular, StrIntPair>(this, get, set)));
}
} // namespace shill