Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 17, 2008, 02:05 PM   #1
Blarged
macrumors newbie
 
Join Date: Apr 2008
Send a message via AIM to Blarged
Using IOPMSchedulePowerEvent

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

Code:
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
http://gemma.apple.com/documentation...urn/index.html

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

Last edited by Blarged; Apr 17, 2008 at 06:09 PM.
Blarged is offline   0 Reply With Quote
Old May 14, 2008, 01:51 PM   #2
Blarged
Thread Starter
macrumors newbie
 
Join Date: Apr 2008
Send a message via AIM to Blarged
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
Code:
	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

Last edited by Blarged; May 14, 2008 at 05:14 PM.
Blarged is offline   0 Reply With Quote
Old May 14, 2008, 06:17 PM   #3
Blarged
Thread Starter
macrumors newbie
 
Join Date: Apr 2008
Send a message via AIM to Blarged
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.
Blarged is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump

All times are GMT -5. The time now is 07:20 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC