diff options
Diffstat (limited to '')
-rw-r--r-- | src/wakeup/waketab.cpp | 195 | ||||
-rw-r--r-- | src/wakeup/wakeup.cpp | 52 | ||||
-rw-r--r-- | src/wakeup/wakeup.h | 81 | ||||
-rw-r--r-- | src/wakeup/wakeup_ethernet.cpp | 111 | ||||
-rw-r--r-- | src/wakeup/wakeup_ethernet.h | 50 | ||||
-rw-r--r-- | src/wakeup/wakeup_usb.cpp | 111 | ||||
-rw-r--r-- | src/wakeup/wakeup_usb.h | 50 |
7 files changed, 650 insertions, 0 deletions
diff --git a/src/wakeup/waketab.cpp b/src/wakeup/waketab.cpp new file mode 100644 index 0000000..77d7567 --- /dev/null +++ b/src/wakeup/waketab.cpp @@ -0,0 +1,195 @@ +#include <algorithm> +#include <stdio.h> +#include <string.h> +#include <ncurses.h> +#include "wakeup.h" +#include <vector> +#include "../lib.h" +#include "../measurement/sysfs.h" +#include "../display.h" +#include "../report/report.h" +#include "../report/report-maker.h" +#include "../report/report-data-html.h" +#include "wakeup_ethernet.h" +#include "wakeup_usb.h" + +using namespace std; + +static bool should_clear = false; + +class wakeup_window *newtab_window; + +class wakeup_window: public tab_window { +public: + virtual void repaint(void); + virtual void cursor_enter(void); + virtual void expose(void); + virtual void window_refresh(void); +}; + +static void init_wakeup(void) +{ + add_ethernet_wakeup(); + add_usb_wakeup(); +} + +void initialize_wakeup(void) +{ + class wakeup_window *win; + + win = new wakeup_window(); + + create_tab("WakeUp", _("WakeUp"), win, _(" <ESC> Exit | <Enter> Toggle wakeup | <r> Window refresh")); + + init_wakeup(); + + win->cursor_max = wakeup_all.size() - 1; + + if (newtab_window) + delete newtab_window; + + newtab_window = win; +} + +static void __wakeup_update_display(int cursor_pos) +{ + WINDOW *win; + unsigned int i; + + win = get_ncurses_win("WakeUp"); + + if (!win) + return; + + if (should_clear) { + should_clear = false; + wclear(win); + } + + wmove(win, 1,0); + + for (i = 0; i < wakeup_all.size(); i++) { + char res[128]; + char desc[4096]; + pt_strcpy(res, wakeup_all[i]->wakeup_string()); + pt_strcpy(desc, wakeup_all[i]->description()); + while (strlen(res) < 12) + strcat(res, " "); + + while (strlen(desc) < 103) + strcat(desc, " "); + + if ((int)i != cursor_pos) { + wattrset(win, A_NORMAL); + wprintw(win, " "); + } else { + wattrset(win, A_REVERSE); + wprintw(win, ">> "); + } + wprintw(win, "%s %s\n", _(res), _(desc)); + } +} + +void wakeup_update_display(void) +{ + class tab_window *wt; + + wt = tab_windows["WakeUp"]; + if (!wt) + return; + wt->repaint(); +} + +void wakeup_window::repaint(void) +{ + __wakeup_update_display(cursor_pos); +} + +void wakeup_window::cursor_enter(void) +{ + class wakeup *wake; + const char *wakeup_toggle_script; + wake = wakeup_all[cursor_pos]; + if (!wake) + return; + wakeup_toggle_script = wake->wakeup_toggle_script(); + wake->wakeup_toggle(); + ui_notify_user(">> %s\n", wakeup_toggle_script); +} + +void report_show_wakeup(void) +{ + unsigned int i; + int idx, rows = 0, cols; + + /* div attr css_class and css_id */ + tag_attr div_attr; + init_div(&div_attr, "clear_block", "wakeup"); + + /* Set Title attributes */ + tag_attr title_attr; + init_title_attr(&title_attr); + + /* Set Table attributes, rows, and cols */ + table_attributes wakeup_table_css; + cols=2; + idx = cols; + + for (i = 0; i < wakeup_all.size(); i++) { + int tgb; + tgb = wakeup_all[i]->wakeup_value(); + if (tgb == WAKEUP_DISABLE) + rows+=1; + } + + /* add section */ + + report.add_div(&div_attr); + if (rows > 0){ + rows= rows + 1; + init_wakeup_table_attr(&wakeup_table_css, rows, cols); + + /* Set array of data in row Major order */ + string *wakeup_data = new string[cols * rows]; + + wakeup_data[0]=__("Description"); + wakeup_data[1]=__("Script"); + + for (i = 0; i < wakeup_all.size(); i++) { + int gb; + gb = wakeup_all[i]->wakeup_value(); + if (gb != WAKEUP_DISABLE) + continue; + wakeup_data[idx]=string(wakeup_all[i]->description()); + idx+=1; + wakeup_data[idx]=string(wakeup_all[i]->wakeup_toggle_script()); + idx+=1; + } + + /* Report Output */ + report.add_title(&title_attr,__("Wake status of the devices")); + report.add_table(wakeup_data, &wakeup_table_css); + delete [] wakeup_data; + } +} + +void wakeup_window::expose(void) +{ + cursor_pos = 0; + repaint(); +} + +void wakeup_window::window_refresh(void) +{ + clear_wakeup(); + should_clear = true; + init_wakeup(); +} + +void clear_wakeup() +{ + for (size_t i = 0; i < wakeup_all.size(); i++) { + delete wakeup_all[i]; + } + wakeup_all.clear(); +} diff --git a/src/wakeup/wakeup.cpp b/src/wakeup/wakeup.cpp new file mode 100644 index 0000000..9d83a9a --- /dev/null +++ b/src/wakeup/wakeup.cpp @@ -0,0 +1,52 @@ +/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ + +#include <string.h> +#include <ncurses.h> +#include "wakeup.h" +#include <vector> +#include "../lib.h" + +using namespace std; + +vector<class wakeup *> wakeup_all; + +wakeup::wakeup(const char *str, double _score, const char *enable, const char *disable) +{ + score = _score; + pt_strcpy(desc, str); + pt_strcpy(wakeup_enable, enable); + pt_strcpy(wakeup_disable, disable); +} + +wakeup::wakeup(void) +{ + score = 0; + desc[0] = 0; + pt_strcpy(wakeup_enable, _("Enabled")); + pt_strcpy(wakeup_disable, _("Disabled")); + pt_strcpy(wakeup_idle, _("Unknown")); +} + diff --git a/src/wakeup/wakeup.h b/src/wakeup/wakeup.h new file mode 100644 index 0000000..13f99a3 --- /dev/null +++ b/src/wakeup/wakeup.h @@ -0,0 +1,81 @@ +/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ +#ifndef _INCLUDE_GUARD_WAKEUP_H +#define _INCLUDE_GUARD_WAKEUP_H + +#include<vector> +#include <limits.h> + +using namespace std; + +#define WAKEUP_ENABLE 1 +#define WAKEUP_DISABLE 0 + +class wakeup { + char wakeup_enable[128]; + char wakeup_disable[128]; + char wakeup_idle[128]; +protected: + char toggle_enable[4096]; + char toggle_disable[4096]; +public: + char desc[4096]; + double score; + + wakeup(const char *str, double _score, const char *enable = "", const char *disable = ""); + wakeup(void); + + virtual ~wakeup () {}; + + virtual int wakeup_value(void) { return WAKEUP_DISABLE; } + + virtual char *wakeup_string(void) + { + switch (wakeup_value()) { + case WAKEUP_ENABLE: + return wakeup_enable; + case WAKEUP_DISABLE: + return wakeup_disable; + } + return wakeup_idle; + } + + + virtual const char *description(void) { return desc; }; + + virtual void wakeup_toggle(void) { }; + + virtual const char *wakeup_toggle_script(void) { return toggle_enable; } + +}; + +extern vector<class wakeup *> wakeup_all; + +extern void initialize_wakeup(void); +extern void wakeup_update_display(void); +extern void report_show_wakeup(void); +extern void clear_wakeup(void); + +#endif diff --git a/src/wakeup/wakeup_ethernet.cpp b/src/wakeup/wakeup_ethernet.cpp new file mode 100644 index 0000000..6ce1725 --- /dev/null +++ b/src/wakeup/wakeup_ethernet.cpp @@ -0,0 +1,111 @@ +;/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ + +#include "wakeup.h" +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <utility> +#include <iostream> +#include <fstream> +#include <sys/socket.h> +#include <errno.h> +#include <linux/types.h> +#include <net/if.h> +#include <linux/sockios.h> +#include <sys/ioctl.h> + +#include <linux/ethtool.h> + +#include "../lib.h" +#include "wakeup_ethernet.h" + +ethernet_wakeup::ethernet_wakeup(const char *path, const char *iface) : wakeup("", 0.5, _("Enabled"), _("Disabled")) +{ + memset(interf, 0, sizeof(interf)); + pt_strcpy(interf, iface); + sprintf(desc, _("Wake-on-lan status for device %s"), iface); + snprintf(eth_path, sizeof(eth_path), "/sys/class/net/%s/device/power/wakeup", iface); + snprintf(toggle_enable, sizeof(toggle_enable), "echo 'enabled' > '%s';", eth_path); + snprintf(toggle_disable, sizeof(toggle_disable), "echo 'disabled' > '%s';", eth_path); +} + +int ethernet_wakeup::wakeup_value(void) +{ + string content; + + content = read_sysfs_string(eth_path); + + if (strcmp(content.c_str(), "enabled") == 0) + return WAKEUP_ENABLE; + + return WAKEUP_DISABLE; +} + +void ethernet_wakeup::wakeup_toggle(void) +{ + int enable; + enable = wakeup_value(); + + if (enable == WAKEUP_ENABLE) { + write_sysfs(eth_path, "disabled"); + return; + } + + write_sysfs(eth_path, "enabled"); +} + +const char *ethernet_wakeup::wakeup_toggle_script(void) +{ + int enable; + enable = wakeup_value(); + + if (enable == WAKEUP_ENABLE) { + return toggle_disable; + } + + return toggle_enable; +} + +void wakeup_eth_callback(const char *d_name) +{ + class ethernet_wakeup *eth; + char filename[PATH_MAX]; + + snprintf(filename, sizeof(filename), "/sys/class/net/%s/device/power/wakeup", d_name); + if (access(filename, R_OK) != 0) + return; + + snprintf(filename, sizeof(filename), "/sys/class/net/%s/device/power/wakeup", d_name); + eth = new class ethernet_wakeup(filename, d_name); + wakeup_all.push_back(eth); +} + +void add_ethernet_wakeup(void) +{ + process_directory("/sys/class/net/", wakeup_eth_callback); +} diff --git a/src/wakeup/wakeup_ethernet.h b/src/wakeup/wakeup_ethernet.h new file mode 100644 index 0000000..682bf95 --- /dev/null +++ b/src/wakeup/wakeup_ethernet.h @@ -0,0 +1,50 @@ +/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ +#ifndef _INCLUDE_GUARD_ETHERNET_WAKEUP_H +#define _INCLUDE_GUARD_ETHERNET_WAKEUP_H + +#include <vector> + +#include "wakeup.h" + +using namespace std; + +class ethernet_wakeup : public wakeup { + char eth_path[PATH_MAX]; +public: + char interf[4096]; + ethernet_wakeup(const char *eth_path, const char *iface); + + virtual int wakeup_value(void); + + virtual void wakeup_toggle(void); + + virtual const char *wakeup_toggle_script(void); + +}; + +extern void add_ethernet_wakeup(void); + +#endif diff --git a/src/wakeup/wakeup_usb.cpp b/src/wakeup/wakeup_usb.cpp new file mode 100644 index 0000000..e0e4567 --- /dev/null +++ b/src/wakeup/wakeup_usb.cpp @@ -0,0 +1,111 @@ +;/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ + +#include "wakeup.h" +#include <sys/types.h> +#include <unistd.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <utility> +#include <iostream> +#include <fstream> +#include <sys/socket.h> +#include <errno.h> +#include <linux/types.h> +#include <net/if.h> +#include <linux/sockios.h> +#include <sys/ioctl.h> + +#include <linux/ethtool.h> + +#include "../lib.h" +#include "wakeup_usb.h" + +usb_wakeup::usb_wakeup(const char *path, const char *iface) : wakeup("", 0.5, _("Enabled"), _("Disabled")) +{ + memset(interf, 0, sizeof(interf)); + pt_strcpy(interf, iface); + sprintf(desc, _("Wake status for USB device %s"), iface); + snprintf(usb_path, sizeof(usb_path), "/sys/bus/usb/devices/%s/power/wakeup", iface); + snprintf(toggle_enable, sizeof(toggle_enable), "echo 'enabled' > '%s';", usb_path); + snprintf(toggle_disable, sizeof(toggle_disable), "echo 'disabled' > '%s';", usb_path); +} + +int usb_wakeup::wakeup_value(void) +{ + string content; + + content = read_sysfs_string(usb_path); + + if (strcmp(content.c_str(), "enabled") == 0) + return WAKEUP_ENABLE; + + return WAKEUP_DISABLE; +} + +void usb_wakeup::wakeup_toggle(void) +{ + int enable; + enable = wakeup_value(); + + if (enable == WAKEUP_ENABLE) { + write_sysfs(usb_path, "disabled"); + return; + } + + write_sysfs(usb_path, "enabled"); +} + +const char *usb_wakeup::wakeup_toggle_script(void) +{ + int enable; + enable = wakeup_value(); + + if (enable == WAKEUP_ENABLE) { + return toggle_disable; + } + + return toggle_enable; +} + +void wakeup_usb_callback(const char *d_name) +{ + class usb_wakeup *usb; + char filename[PATH_MAX]; + + snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s/power/wakeup", d_name); + if (access(filename, R_OK) != 0) + return; + + snprintf(filename, sizeof(filename), "/sys/bus/usb/devices/%s/power/wakeup", d_name); + usb = new class usb_wakeup(filename, d_name); + wakeup_all.push_back(usb); +} + +void add_usb_wakeup(void) +{ + process_directory("/sys/bus/usb/devices/", wakeup_usb_callback); +} diff --git a/src/wakeup/wakeup_usb.h b/src/wakeup/wakeup_usb.h new file mode 100644 index 0000000..f7a1f7e --- /dev/null +++ b/src/wakeup/wakeup_usb.h @@ -0,0 +1,50 @@ +/* + * Copyright 2018, Intel Corporation + * + * This file is part of PowerTOP + * + * This program file is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program in a file named COPYING; if not, write to the + * Free Software Foundation, Inc, + * 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA + * or just google for it. + * + * Authors: + * Gayatri Kammela <gayatri.kammela@intel.com> + */ +#ifndef _INCLUDE_GUARD_USB_WAKEUP_H +#define _INCLUDE_GUARD_USB_WAKEUP_H + +#include <vector> + +#include "wakeup.h" + +using namespace std; + +class usb_wakeup : public wakeup { + char usb_path[PATH_MAX]; +public: + char interf[4096]; + usb_wakeup(const char *usb_path, const char *iface); + + virtual int wakeup_value(void); + + virtual void wakeup_toggle(void); + + virtual const char *wakeup_toggle_script(void); + +}; + +extern void add_usb_wakeup(void); + +#endif |