1 /* 2 * Copyright 2019, The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 #pragma once 18 19 #include "macaddress.h" 20 21 #include <stdint.h> 22 23 #include <array> 24 #include <vector> 25 26 #define IEEE80211_TX_MAX_RATES 4 27 28 struct hwsim_tx_rate { 29 signed char idx; 30 unsigned char count; 31 } __attribute__((__packed__)); 32 33 enum class FrameType : uint8_t { 34 Unknown, 35 Ack, 36 Data, 37 }; 38 39 enum class AccessCategory { 40 Voice, 41 Video, 42 BestEffort, 43 Background, 44 }; 45 46 FrameType frameTypeFromByte(uint8_t byte); 47 48 using Rates = std::array<hwsim_tx_rate, IEEE80211_TX_MAX_RATES>; 49 50 class FrameInfo { 51 public: 52 FrameInfo() = default; 53 FrameInfo(const FrameInfo&) = default; 54 FrameInfo(FrameInfo&&) = default; 55 FrameInfo(const MacAddress& transmitter, 56 uint64_t cookie, 57 uint32_t flags, 58 uint32_t channel, 59 const hwsim_tx_rate* rates, 60 size_t numRates); 61 62 FrameInfo& operator=(const FrameInfo&) = default; 63 FrameInfo& operator=(FrameInfo&&) = default; 64 rates()65 const Rates& rates() const { return mTxRates; } rates()66 Rates& rates() { return mTxRates; } 67 transmitter()68 const MacAddress& transmitter() const { return mTransmitter; } cookie()69 uint64_t cookie() const { return mCookie; } flags()70 uint32_t flags() const { return mFlags; } channel()71 uint32_t channel() const { return mChannel; } 72 73 bool shouldAck() const; 74 75 private: 76 Rates mTxRates; 77 MacAddress mTransmitter; 78 uint64_t mCookie = 0; 79 uint32_t mFlags = 0; 80 uint32_t mChannel = 0; 81 }; 82 83 class Frame { 84 public: 85 Frame() = default; 86 Frame(const uint8_t* data, size_t size); 87 Frame(const uint8_t* data, 88 size_t size, 89 const MacAddress& transmitter, 90 uint64_t cookie, 91 uint32_t flags, 92 uint32_t channel, 93 const hwsim_tx_rate* rates, 94 size_t numRates); 95 Frame(Frame&& other) = default; 96 97 Frame& operator=(Frame&& other) = default; 98 size()99 size_t size() const { return mData.size(); } data()100 const uint8_t* data() const { return mData.data(); } data()101 uint8_t* data() { return mData.data(); } cookie()102 uint64_t cookie() const { return mInfo.cookie(); } flags()103 uint32_t flags() const { return mInfo.flags(); } channel()104 uint32_t channel() const { return mInfo.channel(); } 105 rates()106 Rates& rates() { return mInfo.rates(); } rates()107 const Rates& rates() const { return mInfo.rates(); } 108 initialRates()109 const Rates& initialRates() const { return mInitialTxRates; } 110 111 // Increment the number of attempts made in the tx rates 112 bool incrementAttempts(); 113 bool hasRemainingAttempts(); 114 115 // The transmitter as indicated by hwsim transmitter()116 const MacAddress& transmitter() const { return mInfo.transmitter(); } 117 // The source as indicated by the 802.11 frame 118 const MacAddress& source() const; 119 // The destination as indicated by the 802.11 frame 120 const MacAddress& destination() const; 121 122 std::string str() const; 123 info()124 const FrameInfo& info() const { return mInfo; } info()125 FrameInfo& info() { return mInfo; } 126 127 bool isBeacon() const; 128 bool isData() const; 129 bool isDataQoS() const; 130 131 uint16_t getQoSControl() const; 132 shouldAck()133 bool shouldAck() const { return mInfo.shouldAck(); } 134 135 uint64_t calcNextTimeout(); 136 setRadioDestination(const MacAddress & destination)137 void setRadioDestination(const MacAddress& destination) { 138 mRadioDestination = destination; 139 } radioDestination()140 const MacAddress& radioDestination() const { return mRadioDestination; } 141 private: 142 Frame(const Frame&) = delete; 143 Frame& operator=(const Frame&) = delete; 144 145 AccessCategory getAc() const; 146 147 std::vector<uint8_t> mData; 148 FrameInfo mInfo; 149 MacAddress mRadioDestination; 150 Rates mInitialTxRates; 151 uint64_t mNextTimeout = 0; 152 // The contention winow determines how much time to back off on each retry. 153 // The contention window initial value and max value are determined by the 154 // access category of the frame. 155 uint32_t mContentionWindow = 0; 156 uint32_t mContentionWindowMax = 0; 157 }; 158 159