Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 "shill/callback80211_metrics.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/memory/weak_ptr.h> |
| 10 | |
| 11 | #include "shill/config80211.h" |
| 12 | #include "shill/ieee80211.h" |
| 13 | #include "shill/link_monitor.h" |
| 14 | #include "shill/logging.h" |
| 15 | #include "shill/metrics.h" |
| 16 | #include "shill/scope_logger.h" |
| 17 | #include "shill/user_bound_nlmessage.h" |
| 18 | |
| 19 | using base::Bind; |
| 20 | using std::string; |
| 21 | |
| 22 | namespace shill { |
| 23 | |
| 24 | Callback80211Metrics::Callback80211Metrics(Config80211 *config80211, |
| 25 | Metrics *metrics) |
| 26 | : Callback80211Object(config80211), metrics_(metrics), |
| 27 | weak_ptr_factory_(this) { |
| 28 | } |
| 29 | |
| 30 | void Callback80211Metrics::Config80211MessageCallback( |
| 31 | const UserBoundNlMessage &message) { |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 32 | SLOG(WiFi, 3) << "Received " << message.GetMessageTypeString() |
| 33 | << " (" << + message.GetMessageType() << ")"; |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 34 | if (metrics_ && message.GetMessageType() == DeauthenticateMessage::kCommand) { |
| 35 | Metrics::WiFiDisconnectByWhom by_whom = |
| 36 | message.AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP) ? |
| 37 | Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp; |
| 38 | uint16_t reason = static_cast<uint16_t>( |
| 39 | IEEE_80211::kReasonCodeInvalid); |
| 40 | void *rawdata = NULL; |
| 41 | int frame_byte_count = 0; |
| 42 | if (message.GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata, |
| 43 | &frame_byte_count)) { |
| 44 | const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata); |
| 45 | Nl80211Frame frame(frame_data, frame_byte_count); |
| 46 | reason = frame.reason(); |
| 47 | } |
| 48 | IEEE_80211::WiFiReasonCode reason_enum = |
| 49 | static_cast<IEEE_80211::WiFiReasonCode>(reason); |
| 50 | metrics_->Notify80211Disconnect(by_whom, reason_enum); |
| 51 | } |
| 52 | } |
| 53 | |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 54 | bool Callback80211Metrics::InstallAsBroadcastCallback() { |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 55 | if (config80211_) { |
| 56 | callback_ = Bind(&Callback80211Metrics::Config80211MessageCallback, |
| 57 | weak_ptr_factory_.GetWeakPtr()); |
Wade Guthrie | b1ec860 | 2012-10-18 17:26:14 -0700 | [diff] [blame] | 58 | return config80211_->AddBroadcastCallback(callback_); |
Wade Guthrie | 5020b57 | 2012-10-12 15:51:14 -0700 | [diff] [blame] | 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | } // namespace shill. |