Exemplo n.º 1
0
bool Power::doSpecialTestAction(bool slept){

    qDebug() << Q_FUNC_INFO << "#### BATTERY_TEST"<< BATTERY_TEST << "####";

    bool suspendDisabled = Battery::getInstance()->getLevel() > START_POINT;

    if(BATTERY_TEST == TEST_SLEEP && slept){
        while(Battery::getInstance()->getLevel() > START_POINT){
            sleep(1);
            qDebug() << Q_FUNC_INFO << "Current:" << Battery::getInstance()->getLevel()
                        << "% > threshold" << QString::number(START_POINT) << "%";
            Screen::getInstance()->refreshScreen(); // Refresh screen to empty battery faster
        }
        return true;
    }

    // Empty battery waiting for start level
    for(int i = 0; i < 5 && suspendDisabled ; i++){ // Refresh screen to empty battery faster
        qDebug() << Q_FUNC_INFO << "Current:" << Battery::getInstance()->getLevel()
                    << "% > threshold" << QString::number(START_POINT) << "%";
        Screen::getInstance()->refreshScreen();
        sleep(1);
    }

    if(!slept)
        checkAndResetLight();

    return suspendDisabled;
}
Exemplo n.º 2
0
bool PowerMx508::suspend(int autoWakeUpSecs)
{
    qDebug() << Q_FUNC_INFO;

    int ret;
    bool has_slept = false;

    qDebug() << Q_FUNC_INFO << " autoWakeUpSecs: " << autoWakeUpSecs;
    b_processCanceled = false;

    // If power key is pressed no action to allow forced reset
    if(isPowerKeyPressed()){
        qDebug() << Q_FUNC_INFO << "Suspend prevented to ensure forced power off";
        return false;
    }

    if (wakeupActive(wSources, numSources))
        return false;


    /* Initial read of the current wakeup_count
     * It blocks when a wakeup event is processed, so we should call processEvents after this
     */
    QString wakeup_count1;
    QString wakeup_count2;
    QFile *wakeFile = new QFile("/sys/power/wakeup_count");
    if (wakeFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
        wakeup_count1 = QString(wakeFile->readLine()).trimmed();
        wakeFile->close();
        qDebug() << "read wakeup_count" << wakeup_count1.toInt();
    } else {
        qDebug() << Q_FUNC_INFO << "read of wakeup_count failed";
    }

    // set autosleep alarm if requested
    if(autoWakeUpSecs > 0)
        RTCManager::setRTCAlarm(autoWakeUpSecs);

#ifdef BATTERY_TEST
    if(doSpecialTestAction(false))
        return true;
#endif

#ifdef SHOWCASE
    /**
    Re-switch on light periodically when
    exposed in closed showcase
    */
    checkAndResetLight();
#endif

    // Check whether it was canceled and then rollback
    QCoreApplication::processEvents();

    /* must write the wake_count again, for the kernel to check for new ones during suspending */
    if (wakeFile->open(QIODevice::ReadOnly | QIODevice::Text)) {
        wakeup_count2 = QString(wakeFile->readLine()).trimmed();
        wakeFile->close();
        qDebug() << "read wakeup_count" << wakeup_count2.toInt();

        if (wakeup_count1.toInt() != wakeup_count2.toInt()) {
            qDebug() << "wakeup_counts differ";
            b_processCanceled = true;
        } else if (wakeFile->open(QIODevice::WriteOnly | QIODevice::Text)) {
            qDebug() << "writing wakeup_count" << wakeup_count2.toLatin1();
            /* if the write does not succeed, do not suspend */
            if(wakeFile->write(wakeup_count2.toLatin1()) < 0)
                b_processCanceled = true;
            wakeFile->close();
        }
    } else {
        qDebug() << Q_FUNC_INFO << "read of wakeup_count failed";
    }

    if(!b_processCanceled) {
        qDebug() << "really suspending";

        //suspend
        FILE *output = fopen("/sys/power/state","w");
        fprintf( output, "mem");

        /* Writing to sys/power/state generates a close-error
         * when the suspend was aborted by an error.
         */
        ret = fclose(output);
        qDebug() << "suspend fclose returned" << ret;
        has_slept = !ret;

/*-----------------SUSPENDED---------------------------------*/

/* has_slept is true, when the system really slept */
    }

    /* If the suspend was canceled or failed, simply rollback */
    if (b_processCanceled || !has_slept) {
        qDebug() << Q_FUNC_INFO << "Rolling back from suspend";
        if(b_debugOn)
            setLed(true); // Automatically off at suspend/sleep
        return false;
    }

    if(b_debugOn)
        setLed(true); // Automatically off at suspend/sleep
    return true;
}