diff --git a/iptest/iptest.cpp b/iptest/iptest.cpp index 4a8c304..46f2981 100644 --- a/iptest/iptest.cpp +++ b/iptest/iptest.cpp @@ -42,7 +42,7 @@ int main(int argc, char *argv[]) { //interface.deleteAddress("1.1.1.5/29"); - Wireless wi("enp0s3"); + Wireless wi("wlp0s11u1"); QMap channels = wi.channelMap(); @@ -52,6 +52,12 @@ int main(int argc, char *argv[]) { std::cout << "converting channel 1 to freq: " << qPrintable(Wireless::ChanToFreq("Channel: 1")) << std::endl; std::cout << "converting freq 5200 to channel: " << qPrintable(Wireless::FreqToChan("Frequency: 5.2GHz")) << std::endl; + std::cout << "is channel 11 supported? " << (wi.channelSupported(11) ? "yes" : "no") << std::endl; + std::cout << "is channel 165 supported? " << (wi.channelSupported(165) ? "yes" : "no") << std::endl; + std::cout << "is frequency 2.437GHz supported? " << (wi.frequencySupported(2437) ? "yes" : "no") << std::endl; + std::cout << "is frequency 5.825GHz supported? " << (wi.frequencySupported(5825) ? "yes" : "no") << std::endl; + std::cout << "is 802.11a supported? " << (((wi.supportedBands() & Wireless::BAND_80211_A) == Wireless::BAND_80211_A) ? "yes" : "no") << std::endl; + std::cout << "is 802.11b supported? " << (((wi.supportedBands() & Wireless::BAND_80211_B) == Wireless::BAND_80211_B) ? "yes" : "no") << std::endl; QTimer::singleShot(0, qApp, SLOT(quit())); diff --git a/wireless.cpp b/wireless.cpp index c02e2d9..886faf8 100644 --- a/wireless.cpp +++ b/wireless.cpp @@ -21,6 +21,9 @@ struct nl80211_channel_block { typedef struct nl80211_channel_block nl80211_channel_block_t; +// why is this necessary? I get "SupportedBands does not name a type" if I only declare the typedef in the class definition +typedef QFlags SupportedBands; + int IEEE80211Freq[][2] = { {1, 2412}, {2, 2417}, @@ -138,6 +141,20 @@ m_isNL80211(false) for(int i = 0; i < cblock.nfreqs; ++i) { m_channels.insert(cblock.channel_list[i], ChanToFreq(cblock.channel_list[i])); + + // check supported channels to see what bands we support (only makes sense for A and B) + // is there a better way to do this? + if(!((m_supportedBands & BAND_80211_A) == BAND_80211_A)) { + if(cblock.channel_list[i] >= 36) { // first US 5GHz channel + m_supportedBands |= BAND_80211_A; + } + } + + if(!((m_supportedBands & BAND_80211_B) == BAND_80211_B)) { + if(cblock.channel_list[i] >= 1) { // first 2.4GHz channel + m_supportedBands |= BAND_80211_B; + } + } } free(cblock.channel_list); @@ -145,11 +162,45 @@ m_isNL80211(false) m_isValid = true; } -bool Wireless::isValid() { +const SupportedBands Wireless::supportedBands() const { + return m_supportedBands; +} + +QList Wireless::channels() const { + QList channels = m_channels.keys(); + + return channels; +} + +QList Wireless::frequencies() const { + QList freqs = m_channels.values(); + + return freqs; +} + +bool Wireless::channelSupported(int chan) const { + if(m_channels.contains(chan)) { + return true; + }else{ + return false; + } +} + +bool Wireless::frequencySupported(int freq) const { + QList channels = m_channels.values(); + + if(channels.contains(freq)) { + return true; + }else{ + return false; + } +} + +bool Wireless::isValid() const { return m_isValid; } -bool Wireless::isNL80211() { +bool Wireless::isNL80211() const { return m_isNL80211; } diff --git a/wireless.h b/wireless.h index c3e4d54..7b8cf30 100644 --- a/wireless.h +++ b/wireless.h @@ -10,22 +10,36 @@ Q_OBJECT public: Wireless(QString interface = QString()); + + enum Bands { + BAND_80211_A = (1 << 0), + BAND_80211_B = (1 << 1), + BAND_80211_G = (1 << 2), + BAND_80211_N = (1 << 3) + }; + + typedef QFlags SupportedBands; + const QString& name() const; const QMap& channelMap() const; - QVector channels() const; - QVector frequencies() const; + QList channels() const; + QList frequencies() const; static int ChanToFreq(int in_chan); static QString ChanToFreq(QString in_chan, QString outputFormat = QString()); static int FreqToChan(int in_freq); static QString FreqToChan(QString in_freq, QString outputFormat = QString()); - bool isValid(); - bool isNL80211(); + bool isValid() const; + bool isNL80211() const; + bool channelSupported(int chan) const; + bool frequencySupported(int freq) const; + const SupportedBands supportedBands() const; private: QString m_interface; QMap m_channels; bool m_isValid; bool m_isNL80211; + SupportedBands m_supportedBands; static int nl80211_freqlist_cb(struct nl_msg *msg, void *arg); static int nl80211_error_cb(struct sockaddr_nl *nla, struct nlmsgerr *err, void *arg);