~increscent/sxmo-utils

0cb3e1e028c185daed6f1c66406d3350b950f3d1 — Robert Williams 4 months ago de7ce70
trying out new suspend
4 files changed, 52 insertions(+), 72 deletions(-)

M Makefile
M configs/openrc/sxmo-pinephone
M programs/sxmo_suspend.c
M scripts/core/sxmo_lock.sh
M Makefile => Makefile +4 -4
@@ 70,13 70,13 @@ install: $(PROGRAMS)

	install -D -m 0755 programs/sxmo_screenlock $(DESTDIR)$(PREFIX)/bin/

	install -D programs/sxmo_megiaudioroute $(DESTDIR)$(PREFIX)/bin/
	install -D -m 0755 programs/sxmo_megiaudioroute $(DESTDIR)$(PREFIX)/bin/

	install -D programs/sxmo_vibratepine $(DESTDIR)$(PREFIX)/bin/
	install -D -m 0755 programs/sxmo_vibratepine $(DESTDIR)$(PREFIX)/bin/

	install -D programs/sxmo_ring $(DESTDIR)$(PREFIX)/bin/
	install -D -m 0755 programs/sxmo_ring $(DESTDIR)$(PREFIX)/bin/

	install -D programs/sxmo_suspend $(DESTDIR)$(PREFIX)/bin/
	install -D -m 0755 programs/sxmo_suspend $(DESTDIR)$(PREFIX)/bin/

	# Fonts
	install -D -m 0644 -t $(DESTDIR)$(PREFIX)/share/fonts fonts/Terminess.ttf

M configs/openrc/sxmo-pinephone => configs/openrc/sxmo-pinephone +1 -1
@@ 3,4 3,4 @@
description="Enable user access to pinephone hardware kernel interface"

command="chmod"
command_args="a+rw /sys/module/8723cs/parameters/rtw_scan_interval_thr /sys/devices/platform/backlight/backlight/backlight/brightness /sys/power/state /sys/devices/platform/soc/1f00000.rtc/power/wakeup /sys/power/mem_sleep /sys/bus/usb/drivers/usb/unbind /sys/bus/usb/drivers/usb/bind /sys/class/leds/red:indicator/brightness /sys/class/leds/blue:indicator/brightness /sys/class/leds/green:indicator/brightness /sys/class/leds/white:flash/brightness /dev/rtc0"
command_args="a+rw /sys/module/8723cs/parameters/rtw_scan_interval_thr /sys/devices/platform/backlight/backlight/backlight/brightness /sys/power/state /sys/devices/platform/soc/1f00000.rtc/power/wakeup /sys/power/mem_sleep /sys/bus/usb/drivers/usb/unbind /sys/bus/usb/drivers/usb/bind /sys/class/leds/red:indicator/brightness /sys/class/leds/blue:indicator/brightness /sys/class/leds/green:indicator/brightness /sys/class/leds/white:flash/brightness /dev/rtc0 /sys/devices/platform/soc/1f03400.rsb/sunxi-rsb-3a3/axp221-pek/power/wakeup"

M programs/sxmo_suspend.c => programs/sxmo_suspend.c +46 -66
@@ 18,6 18,7 @@

const int WAKE_INTERVAL = 60; // wake every 60 seconds
const char *POWER_STATE_FILE = "/sys/power/state";
const char *RTC_DEVICE = "/dev/rtc0";

// Types
enum Color {


@@ 36,7 37,7 @@ struct fbuf {
// Fn declarations
time_t  convert_rtc_time(struct rtc_time *rtc);
void    die(const char *err, ...);
void    hook_post_suspend();
int     hook_post_suspend();
int     hook_pre_suspend();
int     hook_rtc_wake(time_t waketime);
void    lock_touch(Display *dpy, int screen);


@@ 57,34 58,6 @@ void    writefile(char *filepath, char *str);
// Variables
int rtc_fd = 0;

// Fn declarations
//int checkrtcwake();
//void configuresuspendsettingsandwakeupsources();
//void lockscreen(Display *dpy, int screen, int blank);
//void unblankscreen();
//void blankscreen();
//void readinputloop(Display *dpy, int screen);
//void postwake();
//void sigterm();
//void syncstate();
//void usage();
//void writefile(char *filepath, char *str);

// Variables
//enum State state = StateNoInput;
//int suspendtimeouts = 35;
//int suspendpendingsceenon = 0;
//int suspendpendingtimeouts = 0;
//KeySym lastkeysym = XK_Cancel;
//int lastkeyn = 0;
//char oldbrightness[10] = "200";
//char * brightnessfile = 
//char * powerstatefile = "/sys/power/state";
//time_t waketime = 0; //next wakeup time according to the RTC clock
//int blanked = 0; //indicated whether the display blanked or not

#define RTC_DEVICE      "/dev/rtc0"

time_t convert_rtc_time(struct rtc_time * rtc) {
    struct tm         tm;
    memset(&tm, 0, sizeof tm);


@@ 133,7 106,7 @@ time_t rtc_set_alarm() {
        fprintf(stderr, "error enabling rtc alarm\n");
        return -1;
    }
    return 0;
    return waketime;
}

void set_wakeup_sources() {


@@ 158,12 131,12 @@ void set_wakeup_sources() {
//    }
//    closedir(wakeupsources);
//
//    // Enable powerbutton wakeup source
//    fprintf(stderr, "Enable powerbutton wakeup source\n");
//    writefile(
//        "/sys/devices/platform/soc/1f03400.rsb/sunxi-rsb-3a3/axp221-pek/power/wakeup",
//        "enabled"
//    );
    // Enable powerbutton wakeup source
    fprintf(stderr, "Enable powerbutton wakeup source\n");
    writefile(
        "/sys/devices/platform/soc/1f03400.rsb/sunxi-rsb-3a3/axp221-pek/power/wakeup",
        "enabled"
    );
//
//    // Enable IRQ wakeup source (incoming call) 5.8
//    fprintf(stderr, "Enable 5.8 IRQ wakeup source\n");


@@ 323,55 296,64 @@ int hook_pre_suspend() {
    return system("sxmo_presuspend.sh");
}

void hook_post_suspend() {
int hook_post_suspend() {
    //called after fully waking up (not used for temporary rtc wakeups)
    system("sxmo_postwake.sh");
    return system("sxmo_postwake.sh");
}

// Returns 0 for rtc wake and 1 for everything else
// Returns 1 for rtc wake and 0 for any other kind of wake
int rtc_check_wake(time_t waketime) {
    struct rtc_time now;
    if (ioctl(rtc_fd, RTC_RD_TIME, &now) < 0) {
        fprintf(stderr, "Error reading rtc time\n");
        return 1;
        return 0;
    }

    const long int timediff = convert_rtc_time(&now) - waketime;
    fprintf(stderr, "Checking rtc wake? timediff=%ld\n", timediff);
    return !(timediff >= 0 && timediff <= 3);
    return timediff >= 0 && timediff <= 3;
}

int hook_rtc_wake(time_t waketime) {
    fprintf(stderr, "Calling RTC wake script\n");
    set_pine_led(Blue);
    return system("sxmo_rtcwake.sh");
}

void suspend() {
    fprintf(stderr, "Suspend entering suspend state (pred mode)\n");

    set_wakeup_sources();

    time_t waketime = rtc_set_alarm();

    die("don't sleep :)");
    writefile((char *)POWER_STATE_FILE, "mem");

    //---- program blocks here due to sleep ----- //
    // Just woke up again
    fprintf(stderr, "Suspend woke up\n");
    fprintf(stderr, "Resetting usb connection to the modem\n");
    writefile("/sys/bus/usb/drivers/usb/unbind", "3-1");
    writefile("/sys/bus/usb/drivers/usb/bind", "3-1");
    fprintf(stderr, "Lower scan interval for quicker reconnection to wireless network\n");
    writefile("/sys/module/8723cs/parameters/rtw_scan_interval_thr", "1200"); //ms
    //^-- this will be undone again by a networkmanager hook after connection has been established
    //    or by a delayed script if no connection can be established after a while (to conserve battery)
    while (1) {
        fprintf(stderr, "Suspend entering suspend state\n");

        set_wakeup_sources();

        time_t waketime = rtc_set_alarm();

//        die("don't sleep :)");
        writefile((char *)POWER_STATE_FILE, "mem");

        //---- program blocks here due to sleep ----- //
        // Just woke up again
        fprintf(stderr, "Suspend woke up\n");
        fprintf(stderr, "Resetting usb connection to the modem\n");
        writefile("/sys/bus/usb/drivers/usb/unbind", "3-1");
        writefile("/sys/bus/usb/drivers/usb/bind", "3-1");
        fprintf(stderr, "Lower scan interval for quicker reconnection to wireless network\n");
        writefile("/sys/module/8723cs/parameters/rtw_scan_interval_thr", "1200"); //ms
        //^-- this will be undone again by a networkmanager hook after connection has been established
        //    or by a delayed script if no connection can be established after a while (to conserve battery)

        fprintf(stderr, "Suspend exiting suspend state\n");

        if (rtc_check_wake(waketime)) {
            set_pine_led(Blue);
            hook_rtc_wake(waketime);
            sleep(1);
            set_pine_led(Off);
            continue;
        }

    if (rtc_check_wake)
        hook_rtc_wake(waketime);
        break;
    }

    fprintf(stderr, "Suspend exiting suspend state (pred mode)\n");
}

void writefile(char *filepath, char *str) {


@@ 402,11 384,9 @@ int rtc_init() {
void suspend_init() {
    rtc_fd = rtc_init();
    screen_off();
    set_pine_led(Green);
}

void suspend_reset() {
    set_pine_led(Off);
    screen_on();
    rtc_close(rtc_fd);
}

M scripts/core/sxmo_lock.sh => scripts/core/sxmo_lock.sh +1 -1
@@ 3,7 3,7 @@ if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/lock" ]; then
	"$XDG_CONFIG_HOME/sxmo/hooks/lock"
fi
pkill -9 lisgd
sxmo_screenlock "$@"
sxmo_suspend
sxmo_lisgdstart.sh &
if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/unlock" ]; then
	"$XDG_CONFIG_HOME/sxmo/hooks/unlock"