Using IOPMSchedulePowerEvent

Discussion in 'Mac Programming' started by Blarged, Apr 17, 2008.

  1. macrumors newbie

    Apr 14, 2008
    I am attempting to schedule my machine to go to sleep after 35 seconds. Here is the code I am using

    CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
    CFDateRef wakeFromSleepAt = CFDateCreate(NULL, currentTime + 35);
    IOReturn returnCode = IOPMSchedulePowerEvent(wakeFromSleepAt, NULL, CFSTR(kIOPMAutoSleep));
    This returns returnCode = -536870207 and doesn't put the machine to sleep. Seems to me like a buffer overflow... and I'm sure the issue with my computation of the CFDateRef... Any help would be great.

    EDIT: I just determined that that returnCode is kIOReturnNotPrivileged

    EDIT2: In order to bypass the privilege nonsense... I ran from the terminal with a preceding sudo... works well
  2. thread starter macrumors newbie

    Apr 14, 2008
    Sorry for the double post... but hopefully a slightly different issue will get some help being resolved:

    I am having issues with scheduling my machine to sleep and wake with IOPMSchedulePowerEvent. I am using the code from above...

    What results is the machine going to sleep and waking up at some point <= 35 seconds from runtime. I have disconnected all network cables and closed all other applications just to be safe that it is being 'woken up' by some outside process.

    Am I generating a wake up time incorrectly, or are there other aspects to the IOPMSchedulePowerEvent that I need to consider?

    **EDIT: I realize the above code isn't really complete for putting the machine to sleep... so here's more
    	CFAbsoluteTime wakeTime = CFAbsoluteTimeGetCurrent() + 120.0;	// time must be greater than 35 seconds
    	CFDateRef wakeFromSleepAt = CFDateCreate(NULL, wakeTime);
    //	CFShow(wakeFromSleepAt);
    	returnCode = (int)IOPMSchedulePowerEvent(wakeFromSleepAt, NULL, CFSTR(kIOPMAutoWake));	//Set time to wake from sleep
    	if (!returnCode)
    		io_connect_t portComWithKernel;
    		portComWithKernel = IOPMFindPowerManagement(MACH_PORT_NULL);
    		returnCode = (int)IOPMSleepSystem(portComWithKernel);	// Also reference kIOReturnSuccess
  3. thread starter macrumors newbie

    Apr 14, 2008
    From another source I have figured out the problem.

    The way IOPMSchedulePowerEvent works is off the system clock... but rounds down to the nearest 30 seconds.

    Example (for all these let's assume I have set the computer to sleep for 120 seconds)
    Time on system clock || actual sleep time
    3:23:00 || 120 seconds
    3:23:10 || 110 seconds
    3:23:20 || 100 seconds
    3:23:30 || 120 seconds
    3:23:28 || 92 seconds

    See the pattern? The computer will only wake on the minute or half minute according to the system clock. I'm sure this is due to some low power 'features' during sleep... but what are ya gonna do...

    Workaround: What I plan to do for a work around is add 30 seconds to the requested sleep time. With this being done the computer will sleep for at least the needed time and up to 29 seconds longer. For me, this is what I needed, not idea, but what will work.

Share This Page