libiwinfo: Fix frequency-to-MHz coversion
[project/luci.git] / contrib / package / iwinfo / src / iwinfo_wext.c
index 7bd1574..5b942ea 100644 (file)
 #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)
 {
        int             i;
@@ -34,9 +30,9 @@ static double wext_freq2float(const struct iw_freq *in)
        return res;
 }
 
-static int wext_freq2mhz(const struct iw_freq *in)
+static inline int wext_freq2mhz(const struct iw_freq *in)
 {
-       int i, mhz;
+       int i;
 
        if( in->e == 6 )
        {
@@ -44,62 +40,18 @@ static int wext_freq2mhz(const struct iw_freq *in)
        }
        else
        {
-               mhz = in->m;
-               for(i = 0; i < in->e; i++)
-                       mhz *= 10;
-
-               return (int)(mhz / 100000);
+               return (int)(wext_freq2float(in) / 1000000);
        }
 }
 
-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;
-}
-
-int wext_mw2dbm(int in)
+static inline int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
 {
-       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 (int)res;
-}
-
-static int wext_ioctl(const char *ifname, int cmd, struct iwreq *wrq)
-{
-       /* prepare socket */
-       if( ioctl_socket == -1 )
-       {
-               ioctl_socket = socket(AF_INET, SOCK_DGRAM, 0);
-               fcntl(ioctl_socket, F_SETFD, fcntl(ioctl_socket, F_GETFD) | FD_CLOEXEC);
-       }
-
        if( !strncmp(ifname, "mon.", 4) )
                strncpy(wrq->ifr_name, &ifname[4], IFNAMSIZ);
        else
                strncpy(wrq->ifr_name, ifname, IFNAMSIZ);
 
-       return ioctl(ioctl_socket, cmd, wrq);
+       return iwinfo_ioctl(cmd, wrq);
 }
 
 
@@ -113,6 +65,11 @@ int wext_probe(const char *ifname)
        return 0;
 }
 
+void wext_close(void)
+{
+       /* Nop */
+}
+
 int wext_get_mode(const char *ifname, char *buf)
 {
        struct iwreq wrq;
@@ -288,7 +245,7 @@ int wext_get_txpower(const char *ifname, int *buf)
        if(wext_ioctl(ifname, SIOCGIWTXPOW, &wrq) >= 0)
        {
                if(wrq.u.txpower.flags & IW_TXPOW_MWATT)
-                       *buf = wext_mw2dbm(wrq.u.txpower.value);
+                       *buf = iwinfo_mw2dbm(wrq.u.txpower.value);
                else
                        *buf = wrq.u.txpower.value;
 
@@ -397,16 +354,18 @@ int wext_get_txpwrlist(const char *ifname, char *buf, int *len)
        ) {
                for( i = 0; i < range.num_txpower; i++ )
                {
-                       if( range.txpower_capa & IW_TXPOW_DBM )
+                       if( range.txpower_capa & IW_TXPOW_MWATT )
                        {
-                               entry.dbm = range.txpower[i];
-                               entry.mw  = wext_dbm2mw(range.txpower[i]);
+                               entry.dbm = iwinfo_mw2dbm(range.txpower[i]);
+                               entry.mw  = range.txpower[i];
                        }
 
-                       else if( range.txpower_capa & IW_TXPOW_MWATT )
+                       /* Madwifi does neither set mW not dBm caps, also iwlist assumes
+                        * dBm if mW is not set, so don't check here... */
+                       else /* if( range.txpower_capa & IW_TXPOW_DBM ) */
                        {
-                               entry.dbm = wext_mw2dbm(range.txpower[i]);
-                               entry.mw  = range.txpower[i];
+                               entry.dbm = range.txpower[i];
+                               entry.mw  = iwinfo_dbm2mw(range.txpower[i]);
                        }
 
                        memcpy(&buf[i*sizeof(entry)], &entry, sizeof(entry));
@@ -436,8 +395,9 @@ int wext_get_freqlist(const char *ifname, char *buf, int *len)
 
                for(i = 0; i < range.num_frequency; i++)
                {
-                       entry.mhz     = wext_freq2mhz(&range.freq[i]);
-                       entry.channel = range.freq[i].i;
+                       entry.mhz        = wext_freq2mhz(&range.freq[i]);
+                       entry.channel    = range.freq[i].i;
+                       entry.restricted = 0;
 
                        memcpy(&buf[bl], &entry, sizeof(struct iwinfo_freqlist_entry));
                        bl += sizeof(struct iwinfo_freqlist_entry);
@@ -462,6 +422,33 @@ int wext_get_countrylist(const char *ifname, char *buf, int *len)
        return -1;
 }
 
+int wext_get_hwmodelist(const char *ifname, int *buf)
+{
+       char chans[IWINFO_BUFSIZE] = { 0 };
+       struct iwinfo_freqlist_entry *e = NULL;
+       int len = 0;
+
+       if( !wext_get_freqlist(ifname, chans, &len) )
+       {
+               for( e = (struct iwinfo_freqlist_entry *)chans; e->channel; e++ )
+               {
+                       if( e->channel <= 14 )
+                       {
+                               *buf |= IWINFO_80211_B;
+                               *buf |= IWINFO_80211_G;
+                       }
+                       else
+                       {
+                               *buf |= IWINFO_80211_A;
+                       }
+               }
+
+               return 0;
+       }
+
+       return -1;
+}
+
 int wext_get_encryption(const char *ifname, char *buf)
 {
        /* No reliable crypto info in wext */