Browse Source

add wireless functions to check supported channels/frequencies/bands (bands not complete yet, only A/B)

master
Brad Parker 11 years ago
parent
commit
d9e4c9378e
  1. 8
      iptest/iptest.cpp
  2. 55
      wireless.cpp
  3. 22
      wireless.h

8
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<int, int> 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()));

55
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<Wireless::Bands> 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<int> Wireless::channels() const {
QList<int> channels = m_channels.keys();
return channels;
}
QList<int> Wireless::frequencies() const {
QList<int> 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<int> 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;
}

22
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<Bands> SupportedBands;
const QString& name() const;
const QMap<int, int>& channelMap() const;
QVector<int> channels() const;
QVector<int> frequencies() const;
QList<int> channels() const;
QList<int> 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<int, int> 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);

Loading…
Cancel
Save