blob: b19268d69ae87d437f4175ab6f5876de61528a32 [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/manager.h"
#include <time.h>
#include <stdio.h>
#include <string>
#include <base/logging.h>
#include <base/memory/ref_counted.h>
#include <chromeos/dbus/service_constants.h>
#include "shill/adaptor_interfaces.h"
#include "shill/control_interface.h"
#include "shill/dbus_adaptor.h"
#include "shill/device.h"
#include "shill/device_info.h"
#include "shill/error.h"
#include "shill/shill_event.h"
#include "shill/service.h"
using std::string;
using std::vector;
namespace shill {
Manager::Manager(ControlInterface *control_interface,
EventDispatcher *dispatcher)
: adaptor_(control_interface->CreateManagerAdaptor(this)),
device_info_(control_interface, dispatcher, this),
running_(false),
offline_mode_(false),
state_(flimflam::kStateOffline) {
// Initialize Interface monitor, so we can detect new interfaces
known_properties_.push_back(flimflam::kStateProperty);
known_properties_.push_back(flimflam::kAvailableTechnologiesProperty);
known_properties_.push_back(flimflam::kEnabledTechnologiesProperty);
known_properties_.push_back(flimflam::kConnectedTechnologiesProperty);
known_properties_.push_back(flimflam::kDefaultTechnologyProperty);
known_properties_.push_back(flimflam::kCheckPortalListProperty);
known_properties_.push_back(flimflam::kCountryProperty);
known_properties_.push_back(flimflam::kOfflineModeProperty);
known_properties_.push_back(flimflam::kPortalURLProperty);
known_properties_.push_back(flimflam::kActiveProfileProperty);
known_properties_.push_back(flimflam::kProfilesProperty);
known_properties_.push_back(flimflam::kDevicesProperty);
known_properties_.push_back(flimflam::kServicesProperty);
known_properties_.push_back(flimflam::kServiceWatchListProperty);
VLOG(2) << "Manager initialized.";
}
Manager::~Manager() {}
void Manager::Start() {
LOG(INFO) << "Manager started.";
running_ = true;
adaptor_->UpdateRunning();
device_info_.Start();
}
void Manager::Stop() {
running_ = false;
adaptor_->UpdateRunning();
}
void Manager::RegisterDevice(DeviceRefPtr to_manage) {
vector<DeviceRefPtr>::iterator it;
for (it = devices_.begin(); it != devices_.end(); ++it) {
if (to_manage.get() == it->get())
return;
}
devices_.push_back(to_manage);
// TODO(pstew): Should check configuration
if (running_)
to_manage->Start();
}
void Manager::DeregisterDevice(DeviceConstRefPtr to_forget) {
vector<DeviceRefPtr>::iterator it;
for (it = devices_.begin(); it != devices_.end(); ++it) {
if (to_forget.get() == it->get()) {
devices_.erase(it);
return;
}
}
}
void Manager::RegisterService(ServiceRefPtr to_manage) {
vector<ServiceRefPtr>::iterator it;
for (it = services_.begin(); it != services_.end(); ++it) {
if (to_manage.get() == it->get())
return;
}
services_.push_back(to_manage);
}
void Manager::DeregisterService(ServiceConstRefPtr to_forget) {
vector<ServiceRefPtr>::iterator it;
for (it = services_.begin(); it != services_.end(); ++it) {
if (to_forget.get() == it->get()) {
services_.erase(it);
return;
}
}
}
void Manager::FilterByTechnology(Device::Technology tech,
vector<DeviceRefPtr> *found) {
CHECK(found);
vector<DeviceRefPtr>::iterator it;
for (it = devices_.begin(); it != devices_.end(); ++it) {
if ((*it)->TechnologyIs(tech))
found->push_back(*it);
}
}
ServiceRefPtr Manager::FindService(const std::string& name) {
vector<ServiceRefPtr>::iterator it;
for (it = services_.begin(); it != services_.end(); ++it) {
if (name == (*it)->UniqueName()) {
return *it;
}
}
return NULL;
}
bool Manager::Contains(const std::string &property) {
vector<string>::iterator it;
for (it = known_properties_.begin(); it != known_properties_.end(); ++it) {
if (property == *it)
return true;
}
return false;
}
bool Manager::SetBoolProperty(const string& name, bool value, Error *error) {
VLOG(2) << "Setting " << name << " as a bool.";
bool set = false;
if (name == flimflam::kOfflineModeProperty) {
offline_mode_ = value;
set = true;
}
if (!set && error)
error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
return set;
}
bool Manager::SetStringProperty(const string& name,
const string& value,
Error *error) {
VLOG(2) << "Setting " << name << " as a string.";
bool set = false;
if (name == flimflam::kActiveProfileProperty) {
active_profile_ = value;
set = true;
} else if (name == flimflam::kCountryProperty) {
country_ = value;
set = true;
} else if (name == flimflam::kPortalURLProperty) {
portal_url_ = value;
set = true;
} else if (name == flimflam::kCheckPortalListProperty) {
// parse string, update all services of specified technologies.
set = true;
}
if (!set && error)
error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
return set;
}
} // namespace shill