Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // 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 Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 6 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 7 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 8 | #include <map> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 10 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 11 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 12 | #include <base/logging.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 13 | #include <base/memory/scoped_ptr.h> |
| 14 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 16 | #include "shill/control_interface.h" |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 17 | #include "shill/device_config_interface.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 18 | #include "shill/error.h" |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 19 | #include "shill/property_accessor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 20 | #include "shill/service.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 21 | #include "shill/service_dbus_adaptor.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 22 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | using std::map; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 24 | using std::string; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 25 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 26 | |
| 27 | namespace shill { |
| 28 | Service::Service(ControlInterface *control_interface, |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 29 | EventDispatcher *dispatcher, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 30 | DeviceConfigInterfaceRefPtr config_interface, |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 31 | const string& name) |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 32 | : auto_connect_(false), |
| 33 | connectable_(false), |
| 34 | favorite_(false), |
| 35 | priority_(0), |
| 36 | save_credentials_(true), |
| 37 | dispatcher_(dispatcher), |
mukesh agrawal | b54601c | 2011-06-07 17:39:22 -0700 | [diff] [blame] | 38 | name_(name), |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 39 | available_(false), |
| 40 | configured_(false), |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 41 | configuration_(NULL), |
| 42 | connection_(NULL), |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 43 | config_interface_(config_interface), |
Chris Masone | a82b711 | 2011-05-25 15:16:29 -0700 | [diff] [blame] | 44 | adaptor_(control_interface->CreateServiceAdaptor(this)) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 45 | |
| 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 Masone | b07006b | 2011-05-14 16:10:04 -0700 | [diff] [blame] | 89 | VLOG(2) << "Service initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Paul Stewart | ba41b99 | 2011-05-26 07:02:46 -0700 | [diff] [blame] | 92 | Service::~Service() {} |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 93 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 94 | bool 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 100 | bool 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 | |
| 108 | bool 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool Service::SetStringProperty(const string& name, |
| 117 | const string& value, |
| 118 | Error *error) { |
| 119 | VLOG(2) << "Setting " << name << " as a string."; |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 120 | 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | bool Service::SetStringmapProperty(const string& name, |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 127 | const map<string, string>& value, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 128 | Error *error) { |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 129 | 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 Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 136 | void Service::RegisterBool(const string &name, bool *prop) { |
| 137 | bool_properties_[name] = BoolAccessor(new PropertyAccessor<bool>(prop)); |
| 138 | } |
| 139 | |
| 140 | void Service::RegisterConstBool(const string &name, const bool *prop) { |
| 141 | bool_properties_[name] = BoolAccessor(new ConstPropertyAccessor<bool>(prop)); |
| 142 | } |
| 143 | |
| 144 | void 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 | |
| 151 | void Service::RegisterInt32(const string &name, int32 *prop) { |
| 152 | int32_properties_[name] = Int32Accessor(new PropertyAccessor<int32>(prop)); |
| 153 | } |
| 154 | |
| 155 | void Service::RegisterString(const string &name, string *prop) { |
| 156 | string_properties_[name] = StringAccessor(new PropertyAccessor<string>(prop)); |
| 157 | } |
| 158 | |
| 159 | void Service::RegisterConstString(const string &name, const string *prop) { |
| 160 | string_properties_[name] = |
| 161 | StringAccessor(new ConstPropertyAccessor<string>(prop)); |
| 162 | } |
| 163 | |
| 164 | void 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 | |
| 171 | void Service::RegisterStringmap(const string &name, |
| 172 | map<string, string> *prop) { |
| 173 | stringmap_properties_[name] = |
| 174 | StringmapAccessor(new PropertyAccessor<map<string, string> >(prop)); |
| 175 | } |
| 176 | |
| 177 | void 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 | |
| 183 | void Service::RegisterConstUint8(const string &name, const uint8 *prop) { |
| 184 | uint8_properties_[name] = |
| 185 | Uint8Accessor(new ConstPropertyAccessor<uint8>(prop)); |
| 186 | } |
| 187 | |
| 188 | void Service::RegisterConstUint16(const string &name, const uint16 *prop) { |
| 189 | uint16_properties_[name] = |
| 190 | Uint16Accessor(new ConstPropertyAccessor<uint16>(prop)); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 193 | } // namespace shill |