#include "powerwatch.h"
#include <IOKit/IOMessage.h>
namespace Security {
namespace MachPlusPlus {
PowerWatcher::PowerWatcher()
{
if (!(mKernelPort = IORegisterForSystemPower(this, &mPortRef, ioCallback, &mHandle)))
UnixError::throwMe(EINVAL); }
PowerWatcher::~PowerWatcher()
{
if (mKernelPort)
IODeregisterForSystemPower(&mHandle);
}
void PowerWatcher::ioCallback(void *refCon, io_service_t service,
natural_t messageType, void *argument)
{
PowerWatcher *me = (PowerWatcher *)refCon;
switch (messageType) {
case kIOMessageSystemWillSleep:
debug("powerwatch", "system will sleep");
me->systemWillSleep();
break;
case kIOMessageSystemHasPoweredOn:
debug("powerwatch", "system has powered on");
me->systemIsWaking();
break;
case kIOMessageSystemWillPowerOff:
debug("powerwatch", "system will power off");
me->systemWillPowerDown();
break;
#if !defined(NDEBUG)
case kIOMessageSystemWillNotPowerOff:
debug("powerwatch", "system will not power off");
break;
case kIOMessageCanSystemSleep:
debug("powerwatch", "can system sleep");
break;
case kIOMessageSystemWillNotSleep:
debug("powerwatch", "system will not sleep");
break;
case kIOMessageCanSystemPowerOff:
debug("powerwatch", "can system power off");
break;
default:
debug("powerwatch",
"type 0x%x message received (ignored)", messageType);
break;
#endif //NDEBUG
}
IOAllowPowerChange(me->mKernelPort, long(argument));
}
void PowerWatcher::systemWillSleep()
{ }
void PowerWatcher::systemIsWaking()
{ }
void PowerWatcher::systemWillPowerDown()
{ }
PortPowerWatcher::PortPowerWatcher()
{
port(IONotificationPortGetMachPort(mPortRef));
}
PortPowerWatcher::~PortPowerWatcher()
{
}
boolean_t PortPowerWatcher::handle(mach_msg_header_t *in)
{
IODispatchCalloutFromMessage(NULL, in, mPortRef);
return TRUE;
}
}
}