Ticket #242: neod_dynamicwifiinterface_and_useoftempdaemontotogglewifi.diff

File neod_dynamicwifiinterface_and_useoftempdaemontotogglewifi.diff, 6.5 KB (added by prahal, 3 years ago)

neod use of tempdaemon

  • configure.ac

     
    1818AC_SUBST(GETTEXT_PACKAGE) 
    1919AM_GLIB_GNU_GETTEXT 
    2020 
    21 PKG_CHECK_MODULES(NEOD, gtk+-2.0 gthread-2.0 gconf-2.0 libh1settings libnotify) 
     21PKG_CHECK_MODULES(NEOD, gtk+-2.0 gthread-2.0 gconf-2.0 libh1settings libnotify dbus-glib-1) 
    2222 
     23AC_MSG_CHECKING([for wireless-tools >= 28pre9]) 
     24AC_TRY_COMPILE([#include <iwlib.h>], 
     25               [#ifndef IWEVGENIE 
     26                #error "not found" 
     27                #endif], 
     28               [ac_have_iwevgenie=yes], 
     29               [ac_have_iwevgenie=no]) 
     30AC_MSG_RESULT($ac_have_iwevgenie) 
     31if test "$ac_have_iwevgenie" = no; then 
     32        AC_MSG_ERROR(wireless-tools >= 28pre9 not installed or not functional) 
     33fi 
     34IWLIB=-liw 
     35AC_SUBST(IWLIB) 
     36 
     37 
    2338AC_ARG_WITH([platform], 
    2439            AC_HELP_STRING([--with-platform], [Which platform to use [[default=vanilla]]]), 
    2540            [neod_platform=$with_platform]) 
  • src/wifi.c

     
    1414 
    1515 
    1616#include "wifi.h" 
     17#include <dbus/dbus-glib.h> 
    1718 
    18 gboolean 
    19 wifi_radio_is_on (const gchar *iface) 
     19static gchar * found_ifname = NULL; 
     20 
     21static int 
     22get_ifwireless(int                    skfd, 
     23         char *                 ifname, 
     24         struct wireless_info * info) 
    2025{ 
    21   struct iwreq wrq; 
     26 
     27  memset((char *) info, 0, sizeof(struct wireless_info)); 
     28 
     29  /* Get basic information */ 
     30  if(iw_get_basic_config(skfd, ifname, &(info->b)) < 0) 
     31    { 
     32      /* If no wireless name : no wireless extensions */ 
     33      /* But let's check if the interface exists at all */ 
     34      struct ifreq ifr; 
     35 
     36      strncpy(ifr.ifr_name, ifname, IFNAMSIZ); 
     37      if(ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) 
     38        return(-ENODEV); 
     39      else 
     40        return(-ENOTSUP); 
     41    } 
     42 
     43    return 0; 
     44} 
     45 
     46static int 
     47fill_wireless(int          skfd, 
     48           char *       ifname, 
     49           char *       args[], 
     50           int          count) 
     51{ 
     52  struct wireless_info  info; 
     53  int rc; 
     54 
     55  rc = get_ifwireless(skfd, ifname, &info); 
     56  if (!rc) { 
     57    if (found_ifname == NULL) 
     58      found_ifname = g_strdup(ifname); 
     59  } 
     60  return rc; 
     61} 
     62 
     63 
     64 
     65gchar * 
     66wifi_first_interface () 
     67{ 
    2268  int sock = 0; /* socket */ 
    2369 
    2470  /* Open socket to perform ioctl() on */ 
     
    2975    return FALSE; 
    3076  } 
    3177 
    32   /* Clear our request and set the interface name */ 
    33   memset (&wrq, 0, sizeof (struct iwreq)); 
    34   strncpy ((char *)&wrq.ifr_name, iface, IFNAMSIZ); 
    35  
    36   /* Feel the power, uhh, do the ioctl() */ 
    37   if (ioctl (sock, SIOCGIWTXPOW, &wrq) != 0) 
     78  if(found_ifname == NULL) 
     79    iw_enum_devices(sock, &fill_wireless ,NULL, 0); 
     80  if(found_ifname == NULL) 
    3881  { 
    39     g_warning ("Error performing ioctl: %s", g_strerror (errno)); 
    40     close (sock); 
     82    g_warning ("No wireless interface found."); 
    4183    return FALSE; 
    4284  } 
    4385 
    4486  close (sock); 
    4587 
    46   return !wrq.u.txpower.disabled; 
     88  return g_strdup(found_ifname); 
    4789} 
    4890 
    4991gboolean 
    50 wifi_radio_control (const gchar *iface, gboolean enable) 
     92wifi_radio_is_on (const gchar *iface) 
    5193{ 
    5294  struct iwreq wrq; 
    5395  int sock = 0; /* socket */ 
     
    62104 
    63105  /* Clear our request and set the interface name */ 
    64106  memset (&wrq, 0, sizeof (struct iwreq)); 
    65  
    66107  strncpy ((char *)&wrq.ifr_name, iface, IFNAMSIZ); 
    67108 
    68109  /* Feel the power, uhh, do the ioctl() */ 
     
    73114    return FALSE; 
    74115  } 
    75116 
    76   wrq.u.txpower.disabled = !enable; 
     117  close (sock); 
    77118 
    78   /* Feel the power, uhh, do the ioctl() */ 
    79   if (ioctl (sock, SIOCSIWTXPOW, &wrq) != 0) 
     119  return !wrq.u.txpower.disabled; 
     120} 
     121 
     122gboolean 
     123wifi_radio_control (const gchar *iface, gboolean enable) 
     124{ 
     125  DBusGProxy *proxy; 
     126  DBusGConnection *connection; 
     127  GError *error = NULL; 
     128  gboolean result; 
     129 
     130  connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); 
     131  if (connection == NULL) 
    80132  { 
    81     g_warning ("Error performing ioctl: %s", g_strerror (errno)); 
    82     close (sock); 
    83     return FALSE; 
     133        g_warning("Unable to connect to dbus: %sn", error->message); 
     134        g_error_free (error); 
     135        /* Basically here, there is a problem, since there is no dbus :)  */ 
     136        return FALSE; 
    84137  } 
    85138 
    86   close (sock); 
     139  /* This won't trigger activation! */ 
     140  proxy = dbus_g_proxy_new_for_name (connection, 
     141                "org.prahal.TempDaemon", 
     142                "/org/prahal/TempDaemon/Wifi", 
     143                "org.prahal.TempDaemon.Wifi"); 
    87144 
    88   return TRUE; 
     145  /* The method call will trigger activation, more on that later */ 
     146  if (!dbus_g_proxy_call (proxy, "WifiRadioControl", &error, G_TYPE_STRING, iface, G_TYPE_BOOLEAN, enable, G_TYPE_INVALID, 
     147                          G_TYPE_BOOLEAN, &result, G_TYPE_INVALID)) 
     148  { 
     149        /* Method failed, the GError is set, let's warn everyone */ 
     150        g_warning ("Woops remote method failed: %s", error->message); 
     151        g_error_free (error); 
     152        return FALSE; 
     153  } 
     154 
     155  g_print ("We got the folowing result: %d", result); 
     156 
     157  /* Cleanup */ 
     158  g_object_unref (proxy); 
     159 
     160  return result; 
    89161} 
  • src/neod-device.c

     
    8787#define HEADPHONE_INSERTION_SWITCHCODE 0x02 
    8888#define CHARGER_INSERTION_BUTTON 0x164 
    8989 
    90 #define WIFI_IFACE "eth1" 
    91  
    9290#define BIT_MASK( name, numbits )                                        \ 
    9391    unsigned short  name[ ((numbits) - 1) / (sizeof( short ) * 8) + 1 ];    \ 
    9492    memset( name, 0, sizeof( name ) ) 
     
    240238void 
    241239device_wifi_enable(gboolean enable) 
    242240{ 
    243     wifi_radio_control(WIFI_IFACE, enable); 
     241    wifi_radio_control(wifi_first_interface(), enable); 
    244242} 
    245243 
    246244gboolean  
    247245device_wifi_is_enabled() 
    248246{ 
    249     return wifi_radio_is_on ( WIFI_IFACE ); 
     247    return wifi_radio_is_on ( wifi_first_interface() ); 
    250248} 
    251249 
    252250void  
  • src/wifi.h

     
    2525#include <netdb.h> 
    2626#include <unistd.h> 
    2727 
    28 #include <linux/if.h> 
    29 #include <linux/wireless.h> 
     28#include <iwlib.h> 
    3029 
     30gchar *wifi_first_interface (); 
    3131gboolean wifi_radio_is_on (const gchar *iface); 
    3232gboolean wifi_radio_control (const gchar *iface, gboolean enable); 
    3333#endif /* __WIFI_H_ */ 
  • src/Makefile.am

     
    2525  wifi.c \ 
    2626  wifi.h 
    2727 
    28 neod_LDADD = @NEOD_LIBS@ -lapm 
     28neod_LDADD = @NEOD_LIBS@ @IWLIB@ -lapm 
    2929 
    3030MAINTAINERCLEANFILES  = config.h.in Makefile.in 
    3131