blob: 9b20966ebd4301e643f21d691ba30956faa3989c [file] [log] [blame]
Wade Guthrie5020b572012-10-12 15:51:14 -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_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
19using base::Bind;
20using std::string;
21
22namespace shill {
23
24Callback80211Metrics::Callback80211Metrics(Config80211 *config80211,
25 Metrics *metrics)
26 : Callback80211Object(config80211), metrics_(metrics),
27 weak_ptr_factory_(this) {
28}
29
30void Callback80211Metrics::Config80211MessageCallback(
31 const UserBoundNlMessage &message) {
Wade Guthrieb1ec8602012-10-18 17:26:14 -070032 SLOG(WiFi, 3) << "Received " << message.GetMessageTypeString()
33 << " (" << + message.GetMessageType() << ")";
Wade Guthrie5020b572012-10-12 15:51:14 -070034 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 Guthrieb1ec8602012-10-18 17:26:14 -070054bool Callback80211Metrics::InstallAsBroadcastCallback() {
Wade Guthrie5020b572012-10-12 15:51:14 -070055 if (config80211_) {
56 callback_ = Bind(&Callback80211Metrics::Config80211MessageCallback,
57 weak_ptr_factory_.GetWeakPtr());
Wade Guthrieb1ec8602012-10-18 17:26:14 -070058 return config80211_->AddBroadcastCallback(callback_);
Wade Guthrie5020b572012-10-12 15:51:14 -070059 }
60 return false;
61}
62
63} // namespace shill.