blob: 589125b633afc30eb5a7938a500379c3fa7ab5f3 [file] [log] [blame]
Wade Guthrie64b4c142012-08-20 15:21:01 -07001// 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 Guthried4977f22012-08-22 12:37:54 -070012#include "shill/ieee80211.h"
Wade Guthrie64b4c142012-08-20 15:21:01 -070013#include "shill/link_monitor.h"
14#include "shill/logging.h"
Wade Guthried4977f22012-08-22 12:37:54 -070015#include "shill/metrics.h"
Wade Guthrie64b4c142012-08-20 15:21:01 -070016#include "shill/scope_logger.h"
17#include "shill/user_bound_nlmessage.h"
18
19using base::Bind;
20using base::LazyInstance;
21using std::string;
22
23namespace shill {
24
25namespace {
26LazyInstance<Callback80211Object> g_callback80211 = LAZY_INSTANCE_INITIALIZER;
27} // namespace
28
Wade Guthried4977f22012-08-22 12:37:54 -070029Callback80211Object::Callback80211Object()
30 : config80211_(NULL), metrics_(NULL), weak_ptr_factory_(this) {
Wade Guthrie64b4c142012-08-20 15:21:01 -070031}
32
33Callback80211Object::~Callback80211Object() {
34 DeinstallAsCallback();
35}
36
37void Callback80211Object::Config80211MessageCallback(
Wade Guthried4977f22012-08-22 12:37:54 -070038 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 Guthrie64b4c142012-08-20 15:21:01 -070062 scoped_ptr<UserBoundNlMessage::AttributeNameIterator> i;
63
Wade Guthried4977f22012-08-22 12:37:54 -070064 for (i.reset(message.GetAttributeNameIterator()); !i->AtEnd(); i->Advance()) {
Wade Guthrie64b4c142012-08-20 15:21:01 -070065 string value = "<unknown>";
Wade Guthried4977f22012-08-22 12:37:54 -070066 message.GetAttributeString(i->GetName(), &value);
67 SLOG(WiFi, 2) << " Attr:" << message.StringFromAttributeName(i->GetName())
Wade Guthrie64b4c142012-08-20 15:21:01 -070068 << "=" << value
Wade Guthried4977f22012-08-22 12:37:54 -070069 << " Type:" << message.GetAttributeTypeString(i->GetName());
Wade Guthrie64b4c142012-08-20 15:21:01 -070070 }
71}
72
73bool 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
84bool Callback80211Object::DeinstallAsCallback() {
85 if (config80211_) {
86 config80211_->UnsetDefaultCallback();
87 return true;
88 }
89 return false;
90}
91
92Callback80211Object *Callback80211Object::GetInstance() {
93 return g_callback80211.Pointer();
94}
95
96} // namespace shill.