blob: 087bfcfcc26c529ee3551f8343afc62ef5e0578b [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/config80211.h"
8#include "shill/ieee80211.h"
9#include "shill/link_monitor.h"
10#include "shill/logging.h"
11#include "shill/metrics.h"
repo syncdc085c82012-12-28 08:54:41 -080012#include "shill/nl80211_message.h"
Wade Guthrie5020b572012-10-12 15:51:14 -070013
Wade Guthrie5020b572012-10-12 15:51:14 -070014namespace shill {
15
Wade Guthriec6c81962013-03-06 15:47:13 -080016Callback80211Metrics::Callback80211Metrics(const Config80211 &config80211,
17 Metrics *metrics)
18 : metrics_(metrics),
19 nl80211_message_type_(NetlinkMessage::kIllegalMessageType) {}
20
21void Callback80211Metrics::InitNl80211FamilyId(
22 const Config80211 &config80211) {
23 nl80211_message_type_ =
24 config80211.GetMessageType(Nl80211Message::kMessageTypeString);
Wade Guthrie5020b572012-10-12 15:51:14 -070025}
26
Wade Guthriec6c81962013-03-06 15:47:13 -080027void Callback80211Metrics::CollectDisconnectStatistics(
28 const NetlinkMessage &netlink_message) {
29 if (nl80211_message_type_ == NetlinkMessage::kIllegalMessageType) {
30 LOG(ERROR) << "Somehow, nl80211_message_type_ didn't get set correctly";
31 return;
32 }
33
34 // We only handle deauthenticate messages, which are nl80211 messages.
35 if (netlink_message.message_type() != nl80211_message_type_) {
36 return;
37 }
38 const Nl80211Message &message =
39 * reinterpret_cast<const Nl80211Message *>(&netlink_message);
40
Wade Guthriebdcdaa72013-03-04 12:47:12 -080041 SLOG(WiFi, 3) << "Received " << message.command_string()
42 << " (" << + message.command() << ")";
Christopher Wiley764538d2012-11-09 10:58:23 -080043 if (metrics_ &&
Wade Guthriebdcdaa72013-03-04 12:47:12 -080044 message.command() == DeauthenticateMessage::kCommand) {
Wade Guthrie5020b572012-10-12 15:51:14 -070045 Metrics::WiFiDisconnectByWhom by_whom =
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080046 message.const_attributes()->IsFlagAttributeTrue(
repo sync90ee0fa2012-12-18 10:08:08 -080047 NL80211_ATTR_DISCONNECTED_BY_AP) ?
Wade Guthrie5020b572012-10-12 15:51:14 -070048 Metrics::kDisconnectedByAp : Metrics::kDisconnectedNotByAp;
49 uint16_t reason = static_cast<uint16_t>(
50 IEEE_80211::kReasonCodeInvalid);
Wade Guthrie8343f7f2012-12-04 13:52:32 -080051 ByteString rawdata;
Wade Guthrieefe1f0c2013-02-26 17:42:01 -080052 if (message.const_attributes()->GetRawAttributeValue(NL80211_ATTR_FRAME,
Wade Guthriec6c81962013-03-06 15:47:13 -080053 &rawdata)) {
Wade Guthrie8343f7f2012-12-04 13:52:32 -080054 Nl80211Frame frame(rawdata);
Wade Guthrie5020b572012-10-12 15:51:14 -070055 reason = frame.reason();
56 }
57 IEEE_80211::WiFiReasonCode reason_enum =
58 static_cast<IEEE_80211::WiFiReasonCode>(reason);
59 metrics_->Notify80211Disconnect(by_whom, reason_enum);
60 }
61}
62
Wade Guthrie5020b572012-10-12 15:51:14 -070063} // namespace shill.