Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(728)

Side by Side Diff: src/device.cc

Issue 5180003: cashew: add local byte counters (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cashew.git@master
Patch Set: Address jglasgow and zelidrag (offline) code review comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/device.h ('k') | src/policy.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/device.h" 5 #include "src/device.h"
6 6
7 #include <glog/logging.h> 7 #include <glog/logging.h>
8 8
9 #include "src/byte_counter.h"
9 #include "src/service.h" 10 #include "src/service.h"
10 11
11 namespace cashew { 12 namespace cashew {
12 13
13 const char* Device::kCarrierUnknown = "Unknown";
14
15 // Flimflam Device D-Bus identifiers 14 // Flimflam Device D-Bus identifiers
16 static const char* kFlimflamDeviceName = "org.chromium.flimflam"; 15 static const char *kFlimflamDeviceName = "org.chromium.flimflam";
17 16
18 // Flimflam Device property names 17 // Flimflam Device property names
19 static const char* kFlimflamDeviceCarrierProperty = "Cellular.Carrier"; 18 static const char *kFlimflamDeviceCarrierProperty = "Cellular.Carrier";
20 static const char* kFlimflamDeviceTypeProperty = "Type"; 19 static const char *kFlimflamDeviceInterfaceProperty = "Interface";
20 static const char *kFlimflamDeviceTypeProperty = "Type";
21 21
22 // Flimflam Device on-the-wire Type values 22 // Flimflam Device on-the-wire Type values
23 static const char* kFlimflamDeviceTypeEthernet = "ethernet"; 23 static const char *kFlimflamDeviceTypeEthernet = "ethernet";
24 static const char* kFlimflamDeviceTypeWifi = "wifi"; 24 static const char *kFlimflamDeviceTypeWifi = "wifi";
25 static const char* kFlimflamDeviceTypeWimax = "wimax"; 25 static const char *kFlimflamDeviceTypeWimax = "wimax";
26 static const char* kFlimflamDeviceTypeBluetooth = "bluetooth"; 26 static const char *kFlimflamDeviceTypeBluetooth = "bluetooth";
27 static const char* kFlimflamDeviceTypeGPS = "gps"; 27 static const char *kFlimflamDeviceTypeGPS = "gps";
28 static const char* kFlimflamDeviceTypeCellular = "cellular"; 28 static const char *kFlimflamDeviceTypeCellular = "cellular";
29 // TODO(vlaviano): what does vendor look like? 29 // TODO(vlaviano): what does vendor look like?
30 static const char* kFlimflamDeviceTypeVendor = ""; 30 static const char *kFlimflamDeviceTypeVendor = "";
31 31
32 // GetDeviceProperties retry interval 32 // GetDeviceProperties retry interval
33 static const guint kSecondsPerMinute = 60; 33 static const guint kSecondsPerMinute = 60;
34 static const guint kGetDevicePropertiesIntervalSeconds = 1 * kSecondsPerMinute; 34 static const guint kGetDevicePropertiesIntervalSeconds = 1 * kSecondsPerMinute;
35 35
36 Device::Device(Service * const parent, DBus::Connection& connection, // NOLINT 36 Device::Device(Service * const parent, DBus::Connection& connection, // NOLINT
37 const DBus::Path& path) 37 const DBus::Path& path)
38 : DBus::ObjectProxy(connection, path, kFlimflamDeviceName), 38 : DBus::ObjectProxy(connection, path, kFlimflamDeviceName),
39 parent_(CHECK_NOTNULL(parent)), path_(path), type_(kTypeUnknown), 39 parent_(CHECK_NOTNULL(parent)), path_(path), type_(kTypeUnknown),
40 carrier_(kCarrierUnknown), get_properties_source_id_(0), 40 get_properties_source_id_(0), retrying_get_properties_(false),
41 retrying_get_properties_(false) { 41 byte_counter_(NULL), byte_counter_running_(false) {
42 // schedule a GetProperties() call to our Flimflam service path to init state 42 // schedule a GetProperties() call to our Flimflam service path to init state
43 // we'll keep trying periodically until we succeed 43 // we'll keep trying periodically until we succeed
44 // we'll subsequently update this state by monitoring PropertyChanged signals 44 // we'll subsequently update this state by monitoring PropertyChanged signals
45 get_properties_source_id_ = 45 get_properties_source_id_ =
46 g_idle_add(StaticGetDevicePropertiesCallback, this); 46 g_idle_add(StaticGetDevicePropertiesCallback, this);
47 if (get_properties_source_id_ == 0) { 47 if (get_properties_source_id_ == 0) {
48 LOG(ERROR) << path_ << ": ctor: g_idle_add failed"; 48 LOG(ERROR) << path_ << ": ctor: g_idle_add failed";
49 } 49 }
50 } 50 }
51 51
52 Device::~Device() { 52 Device::~Device() {
53 StopByteCounter();
53 if (get_properties_source_id_ != 0 && 54 if (get_properties_source_id_ != 0 &&
54 !g_source_remove(get_properties_source_id_)) { 55 !g_source_remove(get_properties_source_id_)) {
55 DLOG(WARNING) << path_ << ": dtor: g_source_remove failed"; 56 DLOG(WARNING) << path_ << ": dtor: g_source_remove failed";
56 } 57 }
57 } 58 }
58 59
59 const DBus::Path& Device::GetPath() const { 60 const DBus::Path& Device::GetPath() const {
60 return path_; 61 return path_;
61 } 62 }
62 63
63 Device::Type Device::GetType() const { 64 Device::Type Device::GetType() const {
64 return type_; 65 return type_;
65 } 66 }
66 67
67 const std::string& Device::GetCarrier() const { 68 const std::string& Device::GetCarrier() const {
68 return carrier_; 69 return carrier_;
69 } 70 }
70 71
72 const std::string& Device::GetInterface() const {
73 return interface_;
74 }
75
71 // Flimflam Device D-Bus Proxy methods 76 // Flimflam Device D-Bus Proxy methods
72 77
73 void Device::PropertyChanged(const std::string& property_name, 78 void Device::PropertyChanged(const std::string& property_name,
74 const DBus::Variant& new_value) { 79 const DBus::Variant& new_value) {
75 DLOG(INFO) << path_ << ": PropertyChanged: property_name = " << property_name; 80 DLOG(INFO) << path_ << ": PropertyChanged: property_name = " << property_name;
76 if (property_name == kFlimflamDeviceCarrierProperty) { 81 if (property_name == kFlimflamDeviceCarrierProperty) {
77 OnCarrierUpdate(new_value.reader().get_string()); 82 OnCarrierUpdate(new_value.reader().get_string());
83 } else if (property_name == kFlimflamDeviceInterfaceProperty) {
84 OnInterfaceUpdate(new_value.reader().get_string());
78 } else if (property_name == kFlimflamDeviceTypeProperty) { 85 } else if (property_name == kFlimflamDeviceTypeProperty) {
79 OnTypeUpdate(new_value.reader().get_string()); 86 OnTypeUpdate(new_value.reader().get_string());
80 } else { 87 } else {
81 // we don't care about this property 88 // we don't care about this property
82 } 89 }
83 } 90 }
84 91
92 // Service methods
93
94 bool Device::StartByteCounter() {
95 if (byte_counter_running_) {
96 DLOG(WARNING) << path_ << ": StartByteCounter: counter already running";
97 return false;
98 }
99 byte_counter_running_ = true;
100 if (interface_.empty()) {
101 // if we don't yet know our interface name, we report success but hold off
102 // creating a byte counter object; we'll do so later in OnInterfaceUpdate
103 DLOG(INFO) << path_
104 << ": StartByteCounter: no interface name, delaying counter creation";
105 return true;
106 }
107 if (!CreateByteCounter()) {
108 DLOG(WARNING) << path_
109 << ": StartByteCounter: couldn't create byte counter for " << interface_;
110 byte_counter_running_ = false;
111 return false;
112 }
113 DLOG(INFO) << path_ << ": StartByteCounter: created byte counter for "
114 << interface_;
115 return true;
116 }
117
118 void Device::StopByteCounter() {
119 if (byte_counter_running_) {
120 DeleteByteCounter();
121 byte_counter_running_ = false;
122 }
123 DCHECK(byte_counter_ == NULL);
124 }
125
126 bool Device::ByteCounterRunning() const {
127 return byte_counter_running_;
128 }
129
85 // glib integration interface 130 // glib integration interface
86 131
87 guint Device::GetGetPropertiesSourceId() const { 132 guint Device::GetGetPropertiesSourceId() const {
88 return get_properties_source_id_; 133 return get_properties_source_id_;
89 } 134 }
90 135
91 void Device::SetGetPropertiesSourceId(guint source_id) { 136 void Device::SetGetPropertiesSourceId(guint source_id) {
92 get_properties_source_id_ = source_id; 137 get_properties_source_id_ = source_id;
93 } 138 }
94 139
95 bool Device::RetryingGetProperties() const { 140 bool Device::RetryingGetProperties() const {
96 return retrying_get_properties_; 141 return retrying_get_properties_;
97 } 142 }
98 143
99 void Device::OnRetryingGetProperties(bool retrying) { 144 void Device::OnRetryingGetProperties(bool retrying) {
100 retrying_get_properties_ = retrying; 145 retrying_get_properties_ = retrying;
101 } 146 }
102 147
148 // ByteCounterDelegate methods
149
150 void Device::OnByteCounterUpdate(const ByteCounter *counter, uint64 rx_bytes,
151 uint64 tx_bytes) {
152 DCHECK(byte_counter_running_);
153 DCHECK(byte_counter_ != NULL);
154 DCHECK(counter == byte_counter_);
155 DLOG(INFO) << path_ << ": OnByteCounterUpdate: rx_bytes = " << rx_bytes
156 << ", tx_bytes = " << tx_bytes;
157 parent_->OnByteCounterUpdate(rx_bytes, tx_bytes);
158 }
159
103 // Private methods 160 // Private methods
104 161
105 Device::Type Device::TypeFromString(const std::string& type) const { 162 Device::Type Device::TypeFromString(const std::string& type) const {
106 if (type == kFlimflamDeviceTypeEthernet) { 163 if (type == kFlimflamDeviceTypeEthernet) {
107 return kTypeEthernet; 164 return kTypeEthernet;
108 } 165 }
109 if (type == kFlimflamDeviceTypeWifi) { 166 if (type == kFlimflamDeviceTypeWifi) {
110 return kTypeWifi; 167 return kTypeWifi;
111 } 168 }
112 if (type == kFlimflamDeviceTypeWimax) { 169 if (type == kFlimflamDeviceTypeWimax) {
(...skipping 11 matching lines...) Expand all
124 if (type == kFlimflamDeviceTypeVendor) { 181 if (type == kFlimflamDeviceTypeVendor) {
125 return kTypeVendor; 182 return kTypeVendor;
126 } 183 }
127 return kTypeUnknown; 184 return kTypeUnknown;
128 } 185 }
129 186
130 void Device::OnCarrierUpdate(const std::string& carrier) { 187 void Device::OnCarrierUpdate(const std::string& carrier) {
131 DLOG(INFO) << path_ << ": OnCarrierUpdate: carrier = " << carrier; 188 DLOG(INFO) << path_ << ": OnCarrierUpdate: carrier = " << carrier;
132 // only propagate this to parent Service if there's been a change 189 // only propagate this to parent Service if there's been a change
133 // we expect to see only one such change (from "Unknown") 190 // we expect to see only one such change (from "Unknown")
134 if (carrier_ != carrier) { 191 if (carrier_ == carrier) {
135 if (carrier_ != kCarrierUnknown) { 192 return;
136 LOG(WARNING) << path_ << ": OnCarrierUpdate: carrier change from " 193 }
137 << carrier_ << " to " << carrier << "!"; 194 if (!carrier_.empty()) {
138 } 195 LOG(WARNING) << path_ << ": OnCarrierUpdate: carrier change from "
139 carrier_ = carrier; 196 << carrier_ << " to " << carrier << "!";
140 parent_->OnCarrierUpdate(carrier); 197 }
198 carrier_ = carrier;
199 parent_->OnCarrierUpdate(carrier);
200 }
201
202 void Device::OnInterfaceUpdate(const std::string& interface) {
203 DLOG(INFO) << path_ << ": OnInterfaceUpdate: interface = " << interface;
204 if (interface_ == interface) {
205 return;
206 }
207 if (!interface_.empty()) {
208 LOG(WARNING) << path_ << ": OnInterfaceUpdate: interface change from "
209 << interface_ << " to " << interface << "!";
210 }
211 const std::string old_interface = interface_;
212 interface_ = interface;
213 if (!byte_counter_running_) {
214 return;
215 }
216 // get rid of byte counter for old interface
217 StopByteCounter();
218 // if we don't have new interface info, we can't make a new byte counter yet
219 if (interface_.empty()) {
220 return;
221 }
222 // create byte counter for new interface
223 // TODO(vlaviano): creating new counter on interface change may confuse our
224 // parent service.
225 if (!StartByteCounter()) {
226 LOG(ERROR) << path_
227 << ": OnInterfaceUpdate: could not create byte counter for "
228 << interface_;
229 return;
141 } 230 }
142 } 231 }
143 232
144 void Device::OnTypeUpdate(const std::string& type) { 233 void Device::OnTypeUpdate(const std::string& type) {
145 DLOG(INFO) << path_ << ": OnTypeUpdate: type = " << type; 234 DLOG(INFO) << path_ << ": OnTypeUpdate: type = " << type;
146 type_ = TypeFromString(type); 235 type_ = TypeFromString(type);
147 if (type_ != kTypeCellular) { 236 if (type_ != kTypeCellular) {
148 LOG(WARNING) << path_ << ": OnTypeUpdate: type is not cellular!"; 237 LOG(WARNING) << path_ << ": OnTypeUpdate: type is not cellular!";
149 } 238 }
150 } 239 }
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 return false; 290 return false;
202 } catch (...) { // NOLINT 291 } catch (...) { // NOLINT
203 LOG(WARNING) << path_ 292 LOG(WARNING) << path_
204 << ": GetDeviceProperties: GetProperties() -> Exception"; 293 << ": GetDeviceProperties: GetProperties() -> Exception";
205 return false; 294 return false;
206 } 295 }
207 DLOG(INFO) << "Received " << properties.size() << " properties"; 296 DLOG(INFO) << "Received " << properties.size() << " properties";
208 297
209 // grab the properties in which we're interested 298 // grab the properties in which we're interested
210 PropertyMap::const_iterator it; 299 PropertyMap::const_iterator it;
300 it = properties.find(kFlimflamDeviceInterfaceProperty);
301 if (it != properties.end()) {
302 const DBus::Variant& value = static_cast<DBus::Variant>(it->second);
303 OnInterfaceUpdate(value.reader().get_string());
304 } else {
305 DLOG(WARNING) << path_ << ": GetDeviceProperties: no Interface property";
306 }
211 it = properties.find(kFlimflamDeviceTypeProperty); 307 it = properties.find(kFlimflamDeviceTypeProperty);
212 if (it != properties.end()) { 308 if (it != properties.end()) {
213 const DBus::Variant& value = static_cast<DBus::Variant>(it->second); 309 const DBus::Variant& value = static_cast<DBus::Variant>(it->second);
214 OnTypeUpdate(value.reader().get_string()); 310 OnTypeUpdate(value.reader().get_string());
215 } else { 311 } else {
216 DLOG(WARNING) << path_ << ": GetDeviceProperties: no Type property"; 312 DLOG(WARNING) << path_ << ": GetDeviceProperties: no Type property";
217 } 313 }
218 // don't expect to find Cellular.* properties if we're not a cellular device 314 // don't expect to find Cellular.* properties if we're not a cellular device
219 if (type_ != kTypeCellular) { 315 if (type_ != kTypeCellular) {
220 return true; 316 return true;
221 } 317 }
222 it = properties.find(kFlimflamDeviceCarrierProperty); 318 it = properties.find(kFlimflamDeviceCarrierProperty);
223 if (it != properties.end()) { 319 if (it != properties.end()) {
224 const DBus::Variant& value = static_cast<DBus::Variant>(it->second); 320 const DBus::Variant& value = static_cast<DBus::Variant>(it->second);
225 OnCarrierUpdate(value.reader().get_string()); 321 OnCarrierUpdate(value.reader().get_string());
226 } else { 322 } else {
227 DLOG(WARNING) << path_ 323 DLOG(WARNING) << path_
228 << ": GetDeviceProperties: no Cellular.Carrier property"; 324 << ": GetDeviceProperties: no Cellular.Carrier property";
229 } 325 }
230 return true; 326 return true;
231 } 327 }
232 328
329 bool Device::CreateByteCounter() {
330 if (byte_counter_ != NULL) {
331 DLOG(WARNING) << path_ << ": CreateByteCounter: counter already exists";
332 return false;
333 }
334 if (interface_.empty()) {
335 DLOG(WARNING) << path_ << ": CreateByteCounter: interface name unknown";
336 return false;
337 }
338 byte_counter_ = ByteCounter::NewByteCounter(interface_);
339 if (byte_counter_ == NULL) {
340 LOG(ERROR) << path_
341 << ": CreateByteCounter: could not create byte counter for "
342 << interface_;
343 return false;
344 }
345 byte_counter_->SetDelegate(this);
346 return true;
347 }
348
349 void Device::DeleteByteCounter() {
350 DLOG(INFO) << path_ << ": DeleteByteCounter";
351 if (byte_counter_ != NULL) {
352 delete byte_counter_;
353 byte_counter_ = NULL;
354 }
355 }
356
233 } // namespace cashew 357 } // namespace cashew
OLDNEW
« no previous file with comments | « src/device.h ('k') | src/policy.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698