Author: titmuss
Date: Mon Mar 31 08:38:34 2008
New Revision: 2160

URL: http://svn.slimdevices.com?rev=2160&root=Jive&view=rev
Log:
Bug: N/A
Description:
Use ioctl to turn wlan power save on/off, not the iwconfig command.


Modified:
    trunk/squeezeplay/src/squeezeplay_jive/share/jive/net/Wireless.lua
    trunk/squeezeplay/src/squeezeplay_jive/src/jive_wireless.c

Modified: trunk/squeezeplay/src/squeezeplay_jive/share/jive/net/Wireless.lua
URL: 
http://svn.slimdevices.com/trunk/squeezeplay/src/squeezeplay_jive/share/jive/net/Wireless.lua?rev=2160&root=Jive&r1=2159&r2=2160&view=diff
==============================================================================
--- trunk/squeezeplay/src/squeezeplay_jive/share/jive/net/Wireless.lua 
(original)
+++ trunk/squeezeplay/src/squeezeplay_jive/share/jive/net/Wireless.lua Mon Mar 
31 08:38:34 2008
@@ -541,13 +541,17 @@
                return
        end
 
+       if not self.t_sock then
+               return
+       end
+
        self._powerSaveState = enable
        if enable then
                log:info("iwconfig power on")
-               os.execute("/usr/sbin/iwconfig eth0 power on")
+               self.t_sock:setPower(true)
        else
                log:info("iwconfig power off")
-               os.execute("/usr/sbin/iwconfig eth0 power off")
+               self.t_sock:setPower(false)
        end
 end
 

Modified: trunk/squeezeplay/src/squeezeplay_jive/src/jive_wireless.c
URL: 
http://svn.slimdevices.com/trunk/squeezeplay/src/squeezeplay_jive/src/jive_wireless.c?rev=2160&root=Jive&r1=2159&r2=2160&view=diff
==============================================================================
--- trunk/squeezeplay/src/squeezeplay_jive/src/jive_wireless.c (original)
+++ trunk/squeezeplay/src/squeezeplay_jive/src/jive_wireless.c Mon Mar 31 
08:38:34 2008
@@ -10,9 +10,21 @@
 #include "wpa_ctrl.h"
 
 
+#include <sys/socket.h>
+#include <net/if.h>
+#include <linux/types.h>
+#include <linux/wireless.h>
+
+
+struct wlan_data {
+       struct wpa_ctrl *ctrl;
+       int fd;
+};
+
+
 static int jive_net_wpa_ctrl_open(lua_State *L) {
        const char *ctrl_path;
-       struct wpa_ctrl **ctrl;
+       struct wlan_data *data;
        int err;
 
        /* stack is:
@@ -23,28 +35,37 @@
        // FIXME allow variable control path
        ctrl_path = "/var/run/wpa_supplicant/eth0";
 
-       ctrl = lua_newuserdata(L, sizeof(struct wpa_ctrl **));
-
-       *ctrl = wpa_ctrl_open(ctrl_path);
-       if (*ctrl == NULL) {
+       data = lua_newuserdata(L, sizeof(struct wlan_data));
+
+       data->ctrl = wpa_ctrl_open(ctrl_path);
+       if (data->ctrl == NULL) {
                lua_pushnil(L);
                lua_pushfstring(L, "cannot open wpa_cli %s", ctrl_path);
                return 2;
        }
 
-       err = wpa_ctrl_attach(*ctrl);
+       err = wpa_ctrl_attach(data->ctrl);
        if (err == -1) {
-               wpa_ctrl_close(*ctrl);
+               wpa_ctrl_close(data->ctrl);
 
                lua_pushnil(L);
                lua_pushstring(L, "wpa_ctrl attach error");
                return 2;
        }
        if (err == -2) {
-               wpa_ctrl_close(*ctrl);
-
+               wpa_ctrl_close(data->ctrl);
+               
                lua_pushnil(L);
                lua_pushstring(L, "wpa_ctrl attach timeout");
+               return 2;
+       }
+
+       data->fd = socket(AF_INET, SOCK_DGRAM, 0);
+       if (data->fd < 0) {
+               wpa_ctrl_close(data->ctrl);
+               
+               lua_pushnil(L);
+               lua_pushfstring(L, "wlan socket: ", strerror(errno));
                return 2;
        }
 
@@ -56,12 +77,17 @@
 
 
 static int jive_net_wpa_ctrl_close(lua_State *L) {
-       struct wpa_ctrl **ctrl;
-
-       ctrl = (struct wpa_ctrl **)lua_touserdata(L, 1);
-       if (*ctrl) {
-               wpa_ctrl_close(*ctrl);
-               *ctrl = NULL;
+       struct wlan_data *data;
+
+       data = (struct wlan_data *)lua_touserdata(L, 1);
+       if (data->ctrl) {
+               wpa_ctrl_close(data->ctrl);
+               data->ctrl = NULL;
+       }
+
+       if (data->fd) {
+               close(data->fd);
+               data->fd = 0;
        }
 
        return 0;
@@ -69,21 +95,22 @@
 
 
 static int jive_net_wpa_ctrl_request(lua_State *L) {
-       struct wpa_ctrl *ctrl;
+       struct wlan_data *data;
        const char *cmd;
        size_t cmd_len;
        int err;
 
-       ctrl = *(struct wpa_ctrl **)lua_touserdata(L, 1);
+
+       data = (struct wlan_data *)lua_touserdata(L, 1);
        cmd = lua_tolstring(L, 2, &cmd_len);
 
-       if (!ctrl) {
+       if (!data->ctrl) {
                lua_pushnil(L);
                lua_pushstring(L, "wpa_ctrl closed");
                return 2;
        }
 
-       err = wpa_ctrl_request(ctrl, cmd, cmd_len, NULL, NULL, NULL);
+       err = wpa_ctrl_request(data->ctrl, cmd, cmd_len, NULL, NULL, NULL);
        if (err == -1) {
                jive_net_wpa_ctrl_close(L);
 
@@ -98,43 +125,43 @@
 
 
 static int jive_net_wpa_ctrl_get_fd(lua_State *L) {
-       struct wpa_ctrl *ctrl;
-
-       ctrl = *(struct wpa_ctrl **)lua_touserdata(L, 1);
-
-       if (!ctrl) {
+       struct wlan_data *data;
+
+       data = (struct wlan_data *)lua_touserdata(L, 1);
+
+       if (!data->ctrl) {
                lua_pushnil(L);
                lua_pushstring(L, "wpa_ctrl closed");
                return 2;
        }
 
-       lua_pushinteger(L, wpa_ctrl_get_fd(ctrl));
+       lua_pushinteger(L, wpa_ctrl_get_fd(data->ctrl));
 
        return 1;
 }
 
 
 static int jive_net_wpa_ctrl_recv(lua_State *L) {
-       struct wpa_ctrl *ctrl;
+       struct wlan_data *data;
        char reply[2048];
        size_t reply_len = sizeof(reply);
        int err;
 
-       ctrl = *(struct wpa_ctrl **)lua_touserdata(L, 1);
-
-       if (!ctrl) {
+       data = (struct wlan_data *)lua_touserdata(L, 1);
+
+       if (!data->ctrl) {
                lua_pushnil(L);
                lua_pushstring(L, "wpa_ctrl closed");
                return 2;
        }
 
-       if (!wpa_ctrl_pending(ctrl)) {
+       if (!wpa_ctrl_pending(data->ctrl)) {
                lua_pushnil(L);
                lua_pushstring(L, "timeout");
                return 2;
        }
 
-       err = wpa_ctrl_recv(ctrl, reply, &reply_len);
+       err = wpa_ctrl_recv(data->ctrl, reply, &reply_len);
        if (err == -1) {
                jive_net_wpa_ctrl_close(L);
 
@@ -146,6 +173,72 @@
        lua_pushlstring(L, reply, reply_len);
        return 1;
 }
+
+
+static int jive_net_wlan_get_power(lua_State *L) {
+       struct wlan_data *data;
+       struct iwreq wrq;
+
+       data = (struct wlan_data *)lua_touserdata(L, 1);
+
+       if (!data->fd) {
+               lua_pushnil(L);
+               lua_pushstring(L, "wlan closed");
+               return 2;
+       }
+
+       strncpy(wrq.ifr_ifrn.ifrn_name, "eth0", IFNAMSIZ);
+       wrq.u.power.flags = 0;
+
+       if (ioctl(data->fd, SIOCGIWPOWER, &wrq) < 0) {
+               lua_pushnil(L);
+               lua_pushfstring(L, "ioctl error: %s", strerror(errno));
+               return 2;
+       }
+
+       if (wrq.u.power.disabled) {
+               lua_pushboolean(L, 0);
+       }
+       else {
+               lua_pushboolean(L, 1);
+       }
+
+       return 1;
+};
+
+
+static int jive_net_wlan_set_power(lua_State *L) {
+       struct wlan_data *data;
+       struct iwreq wrq;
+
+       data = (struct wlan_data *)lua_touserdata(L, 1);
+
+       if (!data->fd) {
+               lua_pushnil(L);
+               lua_pushstring(L, "wlan closed");
+               return 2;
+       }
+
+       strncpy(wrq.ifr_ifrn.ifrn_name, "eth0", IFNAMSIZ);
+
+       if (lua_toboolean(L, 2)) {
+               wrq.u.power.disabled = 0;
+               wrq.u.power.flags = IW_POWER_ON;
+       }
+       else {
+               wrq.u.power.disabled = 1;
+               wrq.u.power.flags = 0;
+       }
+
+       if (ioctl(data->fd, SIOCSIWPOWER, &wrq) < 0) {
+               lua_pushnil(L);
+               lua_pushfstring(L, "ioctl error: %s", strerror(errno));
+               return 2;
+       }
+
+       lua_pushboolean(L, 1);
+       return 1;
+};
 
 
 static const struct luaL_Reg jive_net_wpa_ctrl_lib[] = {
@@ -172,6 +265,12 @@
        lua_pushcfunction(L, jive_net_wpa_ctrl_get_fd);
        lua_setfield(L, -2, "getfd");
 
+       lua_pushcfunction(L, jive_net_wlan_get_power);
+       lua_setfield(L, -2, "getPower");
+
+       lua_pushcfunction(L, jive_net_wlan_set_power);
+       lua_setfield(L, -2, "setPower");
+
        lua_pushvalue(L, -1);
        lua_setfield(L, -2, "__index");
 

_______________________________________________
Jive-checkins mailing list
[email protected]
http://lists.slimdevices.com/cgi-bin/mailman/listinfo/jive-checkins

Reply via email to