blob: 28437ad76cd93d7d89b9dff7f2e09630e0a7a814 [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
Wade Guthrie5020b572012-10-12 15:51:14 -07007#include "shill/ieee80211.h"
Wade Guthrie5020b572012-10-12 15:51:14 -07008#include "shill/logging.h"
9#include "shill/metrics.h"
Wade Guthriebb9fca22013-04-10 17:21:42 -070010#include "shill/netlink_manager.h"
repo syncdc085c82012-12-28 08:54:41 -080011#include "shill/nl80211_message.h"
Wade Guthrie5020b572012-10-12 15:51:14 -070012
Wade Guthrie5020b572012-10-12 15:51:14 -070013namespace shill {
14
Wade Guthriebb9fca22013-04-10 17:21:42 -070015Callback80211Metrics::Callback80211Metrics(
16 const NetlinkManager &netlink_manager, Metrics *metrics)
Wade Guthriec6c81962013-03-06 15:47:13 -080017 : metrics_(metrics),
18 nl80211_message_type_(NetlinkMessage::kIllegalMessageType) {}
19
20void Callback80211Metrics::InitNl80211FamilyId(
Wade Guthriebb9fca22013-04-10 17:21:42 -070021 const NetlinkManager &netlink_manager) {
Wade Guthriec6c81962013-03-06 15:47:13 -080022 nl80211_message_type_ =
Wade Guthriebb9fca22013-04-10 17:21:42 -070023 netlink_manager.GetMessageType(Nl80211Message::kMessageTypeString);
Wade Guthrie5020b572012-10-12 15:51:14 -070024}
25
Wade Guthriec6c81962013-03-06 15:47:13 -080026void Callback80211Metrics::CollectDisconnectStatistics(
27 const NetlinkMessage &netlink_message) {
28 if (nl80211_message_type_ == NetlinkMessage::kIllegalMessageType) {
29 LOG(ERROR) << "Somehow, nl80211_message_type_ didn't get set correctly";
30 return;
31 }
32
33 // We only handle deauthenticate messages, which are nl80211 messages.
34 if (netlink_message.message_type() != nl80211_message_type_) {
35 return;
36 }
37 const Nl80211Message &message =
38 * reinterpret_cast<const Nl80211Message *>(&netlink_message);
39
Wade Guthriebdcdaa72013-03-04 12:47:12 -080040 SLOG(WiFi, 3) << "Received " << message.command_string()
41 << " (" << + message.command() << ")";
Christopher Wiley764538d2012-11-09 10:58:23 -080042 if (metrics_ &&
Wade Guthriebdcdaa72013-03-04 12:47:12 -080043 message.command() == DeauthenticateMessage::kCommand) {
Wade Guthrie5020b572012-10-12 15:51:14 -070044 Metrics::WiFiDisconnectByWhom by_whom =
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080045 message.const_attributes()->IsFlagAttributeTrue(
repo sync90ee0fa2012-12-18 10:08:08 -080046 NL80211_ATTR_DISCONNECTED_BY_AP) ?
Wade Guthrie5020b572012-10-12 15:51:14 -070047 Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp;
48 uint16_t reason = static_cast<uint16_t>(
49 IEEE_80211::kReasonCodeInvalid);
Wade Guthrie8343f7f2012-12-04 13:52:32 -080050 ByteString rawdata;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080051 if (message.const_attributes()->GetRawAttributeValue(NL80211_ATTR_FRAME,
Wade Guthriec6c81962013-03-06 15:47:13 -080052 &rawdata)) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -080053 Nl80211Frame frame(rawdata);
Wade Guthrie5020b572012-10-12 15:51:14 -070054 reason = frame.reason();
55 }
56 IEEE_80211::WiFiReasonCode reason_enum =
57 static_cast<IEEE_80211::WiFiReasonCode>(reason);
58 metrics_->Notify80211Disconnect(by_whom, reason_enum);
59 }
60}
61
Wade Guthrie5020b572012-10-12 15:51:14 -070062} // namespace shill.