blob: 82951ec7bb9bfb0a93a9a0fee05387c5e4ffcc31 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07006#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07007
Chris Masone8fe2c7e2011-06-09 15:51:19 -07008#include <map>
Paul Stewart75897df2011-04-27 09:05:53 -07009#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -070010#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070011
Chris Masoneee929b72011-05-10 10:02:18 -070012#include <base/logging.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070013#include <base/memory/scoped_ptr.h>
14#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070015
Paul Stewart75897df2011-04-27 09:05:53 -070016#include "shill/control_interface.h"
Chris Masonec1e50412011-06-07 13:04:53 -070017#include "shill/device_config_interface.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070018#include "shill/error.h"
Chris Masone3bd3c8c2011-06-13 08:20:26 -070019#include "shill/property_accessor.h"
Paul Stewart75897df2011-04-27 09:05:53 -070020#include "shill/service.h"
Chris Masoned7732e42011-05-20 11:08:56 -070021#include "shill/service_dbus_adaptor.h"
Paul Stewart75897df2011-04-27 09:05:53 -070022
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070024using std::string;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070026
27namespace shill {
28Service::Service(ControlInterface *control_interface,
mukesh agrawalb54601c2011-06-07 17:39:22 -070029 EventDispatcher *dispatcher,
Chris Masonec1e50412011-06-07 13:04:53 -070030 DeviceConfigInterfaceRefPtr config_interface,
Chris Masonea82b7112011-05-25 15:16:29 -070031 const string& name)
Chris Masone3bd3c8c2011-06-13 08:20:26 -070032 : auto_connect_(false),
33 connectable_(false),
34 favorite_(false),
35 priority_(0),
36 save_credentials_(true),
37 dispatcher_(dispatcher),
mukesh agrawalb54601c2011-06-07 17:39:22 -070038 name_(name),
Chris Masonea82b7112011-05-25 15:16:29 -070039 available_(false),
40 configured_(false),
Chris Masonea82b7112011-05-25 15:16:29 -070041 configuration_(NULL),
42 connection_(NULL),
Chris Masonec1e50412011-06-07 13:04:53 -070043 config_interface_(config_interface),
Chris Masonea82b7112011-05-25 15:16:29 -070044 adaptor_(control_interface->CreateServiceAdaptor(this)) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -070045
46 RegisterBool(flimflam::kAutoConnectProperty, &auto_connect_);
47 RegisterString(flimflam::kCheckPortalProperty, &check_portal_);
48 RegisterConstBool(flimflam::kConnectableProperty, &connectable_);
49 RegisterDerivedString(flimflam::kDeviceProperty, &Service::DeviceRPCID, NULL);
50
51 RegisterString(flimflam::kEapIdentityProperty, &eap_.identity);
52 RegisterString(flimflam::kEAPEAPProperty, &eap_.eap);
53 RegisterString(flimflam::kEapPhase2AuthProperty, &eap_.inner_eap);
54 RegisterString(flimflam::kEapAnonymousIdentityProperty,
55 &eap_.anonymous_identity);
56 RegisterString(flimflam::kEAPClientCertProperty, &eap_.client_cert);
57 RegisterString(flimflam::kEAPCertIDProperty, &eap_.cert_id);
58 RegisterString(flimflam::kEapPrivateKeyProperty, &eap_.private_key);
59 RegisterString(flimflam::kEapPrivateKeyPasswordProperty,
60 &eap_.private_key_password);
61 RegisterString(flimflam::kEAPKeyIDProperty, &eap_.key_id);
62 RegisterString(flimflam::kEapCaCertProperty, &eap_.ca_cert);
63 RegisterString(flimflam::kEapCaCertIDProperty, &eap_.ca_cert_id);
64 RegisterString(flimflam::kEAPPINProperty, &eap_.pin);
65 RegisterString(flimflam::kEapPasswordProperty, &eap_.password);
66 RegisterString(flimflam::kEapKeyMgmtProperty, &eap_.key_management);
67 RegisterBool(flimflam::kEapUseSystemCAsProperty, &eap_.use_system_cas);
68
69 RegisterConstString(flimflam::kErrorProperty, &error_);
70 RegisterConstBool(flimflam::kFavoriteProperty, &favorite_);
71 RegisterDerivedBool(flimflam::kIsActiveProperty, &Service::IsActive, NULL);
72 RegisterConstString(flimflam::kNameProperty, &name_);
73 RegisterInt32(flimflam::kPriorityProperty, &priority_);
74 RegisterDerivedString(flimflam::kProfileProperty,
75 &Service::ProfileRPCID,
76 NULL);
77 RegisterString(flimflam::kProxyConfigProperty, &proxy_config_);
78 RegisterBool(flimflam::kSaveCredentialsProperty, &save_credentials_);
79 RegisterDerivedString(flimflam::kStateProperty,
80 &Service::CalculateState,
81 NULL);
82
83 // TODO(cmasone): Create VPN Service with this property
84 // RegisterConstStringmap(flimflam::kProviderProperty, &provider_);
85
86 // TODO(cmasone): Add support for R/O properties that return DBus object paths
87 // flimflam::kDeviceProperty, flimflam::kProfileProperty
88
Chris Masoneb07006b2011-05-14 16:10:04 -070089 VLOG(2) << "Service initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070090}
91
Paul Stewartba41b992011-05-26 07:02:46 -070092Service::~Service() {}
Paul Stewart75897df2011-04-27 09:05:53 -070093
Chris Masone3bd3c8c2011-06-13 08:20:26 -070094bool Service::Contains(const string &property) {
95 return (bool_properties_.find(property) != bool_properties_.end() ||
96 int32_properties_.find(property) != int32_properties_.end() ||
97 string_properties_.find(property) != string_properties_.end());
Chris Masone8fe2c7e2011-06-09 15:51:19 -070098}
99
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700100bool Service::SetBoolProperty(const string& name, bool value, Error *error) {
101 VLOG(2) << "Setting " << name << " as a bool.";
102 bool set = bool_properties_[name]->Set(value);
103 if (!set && error)
104 error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
105 return set;
106}
107
108bool Service::SetInt32Property(const string& name, int32 value, Error *error) {
109 VLOG(2) << "Setting " << name << " as an int32";
110 bool set = int32_properties_[name]->Set(value);
111 if (!set && error)
112 error->Populate(Error::kInvalidArguments, name + " is not a R/W int32.");
113 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700114}
115
116bool Service::SetStringProperty(const string& name,
117 const string& value,
118 Error *error) {
119 VLOG(2) << "Setting " << name << " as a string.";
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700120 bool set = string_properties_[name]->Set(value);
121 if (!set && error)
122 error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
123 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700124}
125
126bool Service::SetStringmapProperty(const string& name,
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700127 const map<string, string>& value,
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700128 Error *error) {
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700129 VLOG(2) << "Setting " << name << " as a string map.";
130 bool set = stringmap_properties_[name]->Set(value);
131 if (!set && error)
132 error->Populate(Error::kInvalidArguments, name + " is not a R/W strmap.");
133 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700134}
135
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700136void Service::RegisterBool(const string &name, bool *prop) {
137 bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop));
138}
139
140void Service::RegisterConstBool(const string &name, const bool *prop) {
141 bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop));
142}
143
144void Service::RegisterDerivedBool(const string &name,
145 bool(Service::*get)(void),
146 bool(Service::*set)(const bool&)) {
147 bool_properties_[name] =
148 BoolAccessor(new CustomAccessor<Service, bool>(this, get, set));
149}
150
151void Service::RegisterInt32(const string &name, int32 *prop) {
152 int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop));
153}
154
155void Service::RegisterString(const string &name, string *prop) {
156 string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop));
157}
158
159void Service::RegisterConstString(const string &name, const string *prop) {
160 string_properties_[name] =
161 StringAccessor(new ConstPropertyAccessor<string>(prop));
162}
163
164void Service::RegisterDerivedString(const string &name,
165 string(Service::*get)(void),
166 bool(Service::*set)(const string&)) {
167 string_properties_[name] =
168 StringAccessor(new CustomAccessor<Service, string>(this, get, set));
169}
170
171void Service::RegisterStringmap(const string &name,
172 map<string, string> *prop) {
173 stringmap_properties_[name] =
174 StringmapAccessor(new PropertyAccessor<map<string, string> >(prop));
175}
176
177void Service::RegisterConstStringmap(const string &name,
178 const map<string, string> *prop) {
179 stringmap_properties_[name] =
180 StringmapAccessor(new ConstPropertyAccessor<map<string, string> >(prop));
181}
182
183void Service::RegisterConstUint8(const string &name, const uint8 *prop) {
184 uint8_properties_[name] =
185 Uint8Accessor(new ConstPropertyAccessor<uint8>(prop));
186}
187
188void Service::RegisterConstUint16(const string &name, const uint16 *prop) {
189 uint16_properties_[name] =
190 Uint16Accessor(new ConstPropertyAccessor<uint16>(prop));
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700191}
192
Paul Stewart75897df2011-04-27 09:05:53 -0700193} // namespace shill