2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "wificond/net/mlme_event.h"
21 #include <linux/nl80211.h>
23 #include <android-base/logging.h>
25 #include "wificond/net/nl80211_packet.h"
27 using std::unique_ptr;
35 bool GetCommonFields(const NL80211Packet* packet,
37 vector<uint8_t>* bssid) {
38 if (!packet->GetAttributeValue(NL80211_ATTR_IFINDEX, if_index)) {
39 LOG(ERROR) << "Failed to get NL80211_ATTR_IFINDEX";
42 // Some MLME events do not contain MAC address.
43 if (!packet->GetAttributeValue(NL80211_ATTR_MAC, bssid)) {
44 LOG(DEBUG) << "Failed to get NL80211_ATTR_MAC";
51 unique_ptr<MlmeAssociateEvent> MlmeAssociateEvent::InitFromPacket(
52 const NL80211Packet* packet) {
53 if (packet->GetCommand() != NL80211_CMD_ASSOCIATE) {
56 unique_ptr<MlmeAssociateEvent> associate_event(new MlmeAssociateEvent());
58 if (!GetCommonFields(packet,
59 &(associate_event->interface_index_),
60 &(associate_event->bssid_))){
63 // According to wpa_supplicant, status code of an ASSOCIATE event should be
64 // parsed from NL80211_ATTR_FRAME attribute.
65 // TODO(nywang): Parse NL80211_ATTR_FRAME 80211 management frame and get
67 associate_event->status_code_ = 0;
68 associate_event->is_timeout_ = packet->HasAttribute(NL80211_ATTR_TIMED_OUT);
70 return associate_event;
73 unique_ptr<MlmeConnectEvent> MlmeConnectEvent::InitFromPacket(
74 const NL80211Packet* packet) {
75 if (packet->GetCommand() != NL80211_CMD_CONNECT) {
78 unique_ptr<MlmeConnectEvent> connect_event(new MlmeConnectEvent());
79 if (!GetCommonFields(packet,
80 &(connect_event->interface_index_),
81 &(connect_event->bssid_))){
85 if (!packet->GetAttributeValue(NL80211_ATTR_STATUS_CODE,
86 &(connect_event->status_code_))) {
87 LOG(WARNING) << "Failed to get NL80211_ATTR_STATUS_CODE";
88 connect_event->status_code_ = 0;
90 connect_event->is_timeout_ = packet->HasAttribute(NL80211_ATTR_TIMED_OUT);
95 unique_ptr<MlmeRoamEvent> MlmeRoamEvent::InitFromPacket(
96 const NL80211Packet* packet) {
97 if (packet->GetCommand() != NL80211_CMD_ROAM) {
100 unique_ptr<MlmeRoamEvent> roam_event(new MlmeRoamEvent());
101 if (!GetCommonFields(packet,
102 &(roam_event->interface_index_),
103 &(roam_event->bssid_))){
107 if (!packet->GetAttributeValue(NL80211_ATTR_STATUS_CODE,
108 &(roam_event->status_code_))) {
109 LOG(WARNING) << "Failed to get NL80211_ATTR_STATUS_CODE";
110 roam_event->status_code_ = 0;
116 unique_ptr<MlmeDisconnectEvent> MlmeDisconnectEvent::InitFromPacket(
117 const NL80211Packet* packet) {
118 if (packet->GetCommand() != NL80211_CMD_DISCONNECT) {
121 unique_ptr<MlmeDisconnectEvent> disconnect_event(new MlmeDisconnectEvent());
122 if (!GetCommonFields(packet,
123 &(disconnect_event->interface_index_),
124 &(disconnect_event->bssid_))){
127 return disconnect_event;
130 unique_ptr<MlmeDisassociateEvent> MlmeDisassociateEvent::InitFromPacket(
131 const NL80211Packet* packet) {
132 if (packet->GetCommand() != NL80211_CMD_DISASSOCIATE) {
135 unique_ptr<MlmeDisassociateEvent> disassociate_event(new MlmeDisassociateEvent());
136 if (!GetCommonFields(packet,
137 &(disassociate_event->interface_index_),
138 &(disassociate_event->bssid_))){
141 return disassociate_event;
144 } // namespace wificond
145 } // namespace android