Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -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_object.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/memory/weak_ptr.h> |
| 10 | |
| 11 | #include "shill/config80211.h" |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 12 | #include "shill/ieee80211.h" |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 13 | #include "shill/link_monitor.h" |
| 14 | #include "shill/logging.h" |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 15 | #include "shill/metrics.h" |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 16 | #include "shill/scope_logger.h" |
| 17 | #include "shill/user_bound_nlmessage.h" |
| 18 | |
| 19 | using base::Bind; |
| 20 | using base::LazyInstance; |
| 21 | using std::string; |
| 22 | |
| 23 | namespace shill { |
| 24 | |
| 25 | namespace { |
| 26 | LazyInstance<Callback80211Object> g_callback80211 = LAZY_INSTANCE_INITIALIZER; |
| 27 | } // namespace |
| 28 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 29 | Callback80211Object::Callback80211Object() |
| 30 | : config80211_(NULL), metrics_(NULL), weak_ptr_factory_(this) { |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | Callback80211Object::~Callback80211Object() { |
| 34 | DeinstallAsCallback(); |
| 35 | } |
| 36 | |
| 37 | void Callback80211Object::Config80211MessageCallback( |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 38 | const UserBoundNlMessage &message) { |
| 39 | if (metrics_ && message.GetMessageType() == DeauthenticateMessage::kCommand) { |
| 40 | Metrics::WiFiDisconnectByWhom by_whom = |
| 41 | message.AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP) ? |
| 42 | Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp; |
| 43 | uint16_t reason = static_cast<uint16_t>( |
| 44 | IEEE_80211::kReasonCodeInvalid); |
| 45 | void *rawdata = NULL; |
| 46 | int frame_byte_count = 0; |
| 47 | if (message.GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata, |
| 48 | &frame_byte_count)) { |
| 49 | const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata); |
| 50 | Nl80211Frame frame(frame_data, frame_byte_count); |
| 51 | reason = frame.reason(); |
| 52 | } |
| 53 | IEEE_80211::WiFiReasonCode reason_enum = |
| 54 | static_cast<IEEE_80211::WiFiReasonCode>(reason); |
| 55 | metrics_->Notify80211Disconnect(by_whom, reason_enum); |
| 56 | } |
| 57 | |
| 58 | // Now, print out the message. |
| 59 | |
| 60 | SLOG(WiFi, 2) << "Received " << message.GetMessageTypeString() |
| 61 | << " (" << + message.GetMessageType() << ")"; |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 62 | scoped_ptr<UserBoundNlMessage::AttributeNameIterator> i; |
| 63 | |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 64 | for (i.reset(message.GetAttributeNameIterator()); !i->AtEnd(); i->Advance()) { |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 65 | string value = "<unknown>"; |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 66 | message.GetAttributeString(i->GetName(), &value); |
| 67 | SLOG(WiFi, 2) << " Attr:" << message.StringFromAttributeName(i->GetName()) |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 68 | << "=" << value |
Wade Guthrie | d4977f2 | 2012-08-22 12:37:54 -0700 | [diff] [blame] | 69 | << " Type:" << message.GetAttributeTypeString(i->GetName()); |
Wade Guthrie | 64b4c14 | 2012-08-20 15:21:01 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | bool Callback80211Object::InstallAsCallback() { |
| 74 | if (config80211_) { |
| 75 | Config80211::Callback callback = |
| 76 | Bind(&Callback80211Object::Config80211MessageCallback, |
| 77 | weak_ptr_factory_.GetWeakPtr()); |
| 78 | config80211_->SetDefaultCallback(callback); |
| 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | bool Callback80211Object::DeinstallAsCallback() { |
| 85 | if (config80211_) { |
| 86 | config80211_->UnsetDefaultCallback(); |
| 87 | return true; |
| 88 | } |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | Callback80211Object *Callback80211Object::GetInstance() { |
| 93 | return g_callback80211.Pointer(); |
| 94 | } |
| 95 | |
| 96 | } // namespace shill. |