blob: 20cd6f6b67f89cb7e1313e4927e938a9ee3fbcd2 [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) {
32 if (metrics_ && message.GetMessageType() == DeauthenticateMessage::kCommand) {
33 Metrics::WiFiDisconnectByWhom by_whom =
34 message.AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP) ?
35 Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp;
36 uint16_t reason = static_cast<uint16_t>(
37 IEEE_80211::kReasonCodeInvalid);
38 void *rawdata = NULL;
39 int frame_byte_count = 0;
40 if (message.GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
41 &frame_byte_count)) {
42 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
43 Nl80211Frame frame(frame_data, frame_byte_count);
44 reason = frame.reason();
45 }
46 IEEE_80211::WiFiReasonCode reason_enum =
47 static_cast<IEEE_80211::WiFiReasonCode>(reason);
48 metrics_->Notify80211Disconnect(by_whom, reason_enum);
49 }
50}
51
52bool Callback80211Metrics::InstallAsDefaultCallback() {
53 if (config80211_) {
54 callback_ = Bind(&Callback80211Metrics::Config80211MessageCallback,
55 weak_ptr_factory_.GetWeakPtr());
56 config80211_->SetDefaultCallback(callback_);
57 return true;
58 }
59 return false;
60}
61
62} // namespace shill.