blob: 9acc50120bcaabc1507db6dc5cf24285a624061d [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) {
Christopher Wiley764538d2012-11-09 10:58:23 -080032 SLOG(WiFi, 3) << "Received " << message.message_type_string()
33 << " (" << + message.message_type() << ")";
34 if (metrics_ &&
35 message.message_type() == DeauthenticateMessage::kCommand) {
Wade Guthrie5020b572012-10-12 15:51:14 -070036 Metrics::WiFiDisconnectByWhom by_whom =
37 message.AttributeExists(NL80211_ATTR_DISCONNECTED_BY_AP) ?
38 Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp;
39 uint16_t reason = static_cast<uint16_t>(
40 IEEE_80211::kReasonCodeInvalid);
41 void *rawdata = NULL;
42 int frame_byte_count = 0;
43 if (message.GetRawAttributeData(NL80211_ATTR_FRAME, &rawdata,
44 &frame_byte_count)) {
45 const uint8_t *frame_data = reinterpret_cast<const uint8_t *>(rawdata);
46 Nl80211Frame frame(frame_data, frame_byte_count);
47 reason = frame.reason();
48 }
49 IEEE_80211::WiFiReasonCode reason_enum =
50 static_cast<IEEE_80211::WiFiReasonCode>(reason);
51 metrics_->Notify80211Disconnect(by_whom, reason_enum);
52 }
53}
54
Wade Guthrieb1ec8602012-10-18 17:26:14 -070055bool Callback80211Metrics::InstallAsBroadcastCallback() {
Wade Guthrie5020b572012-10-12 15:51:14 -070056 if (config80211_) {
57 callback_ = Bind(&Callback80211Metrics::Config80211MessageCallback,
58 weak_ptr_factory_.GetWeakPtr());
Wade Guthrieb1ec8602012-10-18 17:26:14 -070059 return config80211_->AddBroadcastCallback(callback_);
Wade Guthrie5020b572012-10-12 15:51:14 -070060 }
61 return false;
62}
63
64} // namespace shill.