libs: remove cbi folder
[project/luci.git] / libs / iwinfo / src / iwinfo_wext.c
index 7f44fd0..2a1b685 100644 (file)
@@ -22,6 +22,8 @@
 #include "iwinfo.h"
 #include "iwinfo_wext.h"
 
+#define LOG10_MAGIC    1.25892541179
+
 static int ioctl_socket = -1;
 
 static double wext_freq2float(const struct iw_freq *in)
@@ -32,6 +34,57 @@ static double wext_freq2float(const struct iw_freq *in)
        return res;
 }
 
+static int wext_freq2mhz(const struct iw_freq *in)
+{
+       int i, mhz;
+
+       if( in->e == 6 )
+       {
+               return in->m;
+       }
+       else
+       {
+               mhz = in->m;
+               for(i = 0; i < in->e; i++)
+                       mhz *= 10;
+
+               return (int)(mhz / 100000);
+       }
+}
+
+static int wext_dbm2mw(int in)
+{
+       double res = 1.0;
+       int ip = in / 10;
+       int fp = in % 10;
+       int k;
+
+       for(k = 0; k < ip; k++) res *= 10;
+       for(k = 0; k < fp; k++) res *= LOG10_MAGIC;
+
+       return (int)res;
+}
+
+static int wext_mw2dbm(int in)
+{
+       double fin = (double) in;
+       int res = 0;
+
+       while(fin > 10.0)
+       {
+               res += 10;
+               fin /= 10.0;
+       }
+
+       while(fin > 1.000001)
+       {
+               res += 1;
+               fin /= LOG10_MAGIC;
+       }
+
+       return res;
+}
+
 static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
 {
        /* prepare socket */
@@ -136,7 +189,7 @@ int wext_get_bitrate(const char *ifname, int *buf)
 
        if(wext_ioctl(ifname, SIOCGIWRATE, &wrq) >= 0)
        {
-               *buf = wrq.u.bitrate.value;
+               *buf = (wrq.u.bitrate.value / 1000);
                return 0;
        }
 
@@ -146,12 +199,74 @@ int wext_get_bitrate(const char *ifname, int *buf)
 int wext_get_channel(const char *ifname, int *buf)
 {
        struct iwreq wrq;
+       struct iw_range range;
+       double freq;
+       int i;
 
        if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
        {
-               /* FIXME: iwlib has some strange workarounds here, maybe we need them as well... */
-               *buf = (int) wext_freq2float(&wrq.u.freq);
-               return 0;
+               if( wrq.u.freq.m >= 1000 )
+               {
+                       freq = wext_freq2float(&wrq.u.freq);
+                       wrq.u.data.pointer = (caddr_t) &range;
+                       wrq.u.data.length  = sizeof(struct iw_range);
+                       wrq.u.data.flags   = 0;
+
+                       if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
+                       {
+                               for(i = 0; i < range.num_frequency; i++)
+                               {
+                                       if( wext_freq2float(&range.freq[i]) == freq )
+                                       {
+                                               *buf = range.freq[i].i;
+                                               return 0;
+                                       }
+                               }
+                       }
+               }
+               else
+               {
+                       *buf = wrq.u.freq.m;
+                       return 0;
+               }
+       }
+
+       return -1;      
+}
+
+int wext_get_frequency(const char *ifname, int *buf)
+{
+       struct iwreq wrq;
+       struct iw_range range;
+       int i, channel;
+
+       if(wext_ioctl(ifname, SIOCGIWFREQ, &wrq) >= 0)
+       {
+               /* We got a channel number instead ... */
+               if( wrq.u.freq.m < 1000 )
+               {
+                       channel = wrq.u.freq.m;
+                       wrq.u.data.pointer = (caddr_t) &range;
+                       wrq.u.data.length  = sizeof(struct iw_range);
+                       wrq.u.data.flags   = 0;
+
+                       if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
+                       {
+                               for(i = 0; i < range.num_frequency; i++)
+                               {
+                                       if( range.freq[i].i == channel )
+                                       {
+                                               *buf = wext_freq2mhz(&range.freq[i]);
+                                               return 0;
+                                       }
+                               }
+                       }
+               }
+               else
+               {
+                       *buf = wext_freq2mhz(&wrq.u.freq);
+                       return 0;
+               }
        }
 
        return -1;      
@@ -241,3 +356,79 @@ int wext_get_assoclist(const char *ifname, char *buf, int *len)
        return -1;
 }
 
+int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
+{
+       struct iwreq wrq;
+       struct iw_range range;
+       struct iwinfo_txpwrlist_entry entry;
+       int i;
+
+       wrq.u.data.pointer = (caddr_t) &range;
+       wrq.u.data.length  = sizeof(struct iw_range);
+       wrq.u.data.flags   = 0;
+
+       if( (wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0) &&
+           (range.num_txpower > 0) && (range.num_txpower <= IW_MAX_TXPOWER) &&
+           !(range.txpower_capa & IW_TXPOW_RELATIVE)
+       ) {
+               for( i = 0; i < range.num_txpower; i++ )
+               {
+                       if( range.txpower_capa & IW_TXPOW_DBM )
+                       {
+                               entry.dbm = range.txpower[i];
+                               entry.mw  = wext_dbm2mw(range.txpower[i]);
+                       }
+
+                       else if( range.txpower_capa & IW_TXPOW_MWATT )
+                       {
+                               entry.dbm = wext_mw2dbm(range.txpower[i]);
+                               entry.mw  = range.txpower[i];
+                       }
+
+                       memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
+               }
+
+               *len = i * sizeof(entry);
+               return 0;
+       }
+
+       return -1;      
+}
+
+int wext_get_freqlist(const char *ifname, char *buf, int *len)
+{
+       struct iwreq wrq;
+       struct iw_range range;
+       struct iwinfo_freqlist_entry entry;
+       int i, bl;
+
+       wrq.u.data.pointer = (caddr_t) &range;
+       wrq.u.data.length  = sizeof(struct iw_range);
+       wrq.u.data.flags   = 0;
+
+       if(wext_ioctl(ifname, SIOCGIWRANGE, &wrq) >= 0)
+       {
+               bl = 0;
+
+               for(i = 0; i < range.num_frequency; i++)
+               {
+                       entry.mhz     = wext_freq2mhz(&range.freq[i]);
+                       entry.channel = range.freq[i].i;
+
+                       memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
+                       bl += sizeof(struct iwinfo_freqlist_entry);
+               }
+
+               *len = bl;
+               return 0;
+       }
+
+       return -1;
+}
+
+int wext_get_mbssid_support(const char *ifname, int *buf)
+{
+       /* No multi bssid support atm */
+       return -1;
+}
+