IORegisterForSystemPower

Function IORegisterForSystemPower 

Source
pub unsafe extern "C-unwind" fn IORegisterForSystemPower(
    refcon: *mut c_void,
    the_port_ref: *mut IONotificationPortRef,
    callback: IOServiceInterestCallback,
    notifier: *mut io_object_t,
) -> io_connect_t
Available on crate features libc and pwr_mgt only.
Expand description

Connects the caller to the Root Power Domain IOService for the purpose of receiving sleep & wake notifications for the system. Does not provide system shutdown and restart notifications.

Provides sleep/wake notifications to applications. Requires that applications acknowledge some, but not all notifications. Register for sleep/wake notifications will deliver these messages over the sleep/wake lifecycle:

  • kIOMessageSystemWillSleep is delivered at the point the system is initiating a non-abortable sleep. Callers MUST acknowledge this event by calling
     IOAllowPowerChange

    . If a caller does not acknowledge the sleep notification, the sleep will continue anyway after a 30 second timeout (resulting in bad user experience). Delivered before any hardware is powered off.

  • kIOMessageSystemWillPowerOn is delivered at early wakeup time, before most hardware has been powered on. Be aware that any attempts to access disk, network, the display, etc. may result in errors or blocking your process until those resources become available. Caller must NOT acknowledge kIOMessageSystemWillPowerOn; the caller must simply return from its handler.
  • kIOMessageSystemHasPoweredOn is delivered at wakeup completion time, after all device drivers and hardware have handled the wakeup event. Expect this event 1-5 or more seconds after initiating system wakeup. Caller must NOT acknowledge kIOMessageSystemHasPoweredOn; the caller must simply return from its handler.
  • kIOMessageCanSystemSleep indicates the system is pondering an idle sleep, but gives apps the chance to veto that sleep attempt. Caller must acknowledge kIOMessageCanSystemSleep by calling
     IOAllowPowerChange

    or

     IOCancelPowerChange

    . Calling IOAllowPowerChange will not veto the sleep; any app that calls IOCancelPowerChange will veto the idle sleep. A kIOMessageCanSystemSleep notification will be followed up to 30 seconds later by a kIOMessageSystemWillSleep message. or a kIOMessageSystemWillNotSleep message.

  • kIOMessageSystemWillNotSleep is delivered when some app client has vetoed an idle sleep request. kIOMessageSystemWillNotSleep may follow a kIOMessageCanSystemSleep notification, but will not otherwise be sent. Caller must NOT acknowledge kIOMessageSystemWillNotSleep; the caller must simply return from its handler.
To deregister for sleep/wake notifications, the caller must make two calls, in this order:
  1. Call IODeregisterForSystemPower with the 'notifier' argument returned here.
  2. Then call IONotificationPortDestroy passing the 'thePortRef' argument returned here.

Parameter refcon: Caller may provide data to receive as an argument to ‘callback’ on power state changes.

Parameter thePortRef: On return, thePortRef is a pointer to an IONotificationPortRef, which will deliver the power notifications. The port is allocated by this function and must be later released by the caller (after calling

 IODeregisterForSystemPower
). The caller should also enable IONotificationPortRef by calling
 IONotificationPortGetRunLoopSource
, or
 IONotificationPortGetMachPort
, or
 IONotificationPortSetDispatchQueue
.

Parameter callback: A c-function which is called during the notification.

Parameter notifier: On success, returns a pointer to a unique notifier which caller must keep and pass to a subsequent call to IODeregisterForSystemPower.

Returns: Returns a io_connect_t session for the IOPMrootDomain or IO_OBJECT_NULL if request failed. Caller must close return value via IOServiceClose() after calling IODeregisterForSystemPower on the notifier argument.

§Safety

  • refcon must be a valid pointer.
  • the_port_ref must be a valid pointer.
  • callback must be implemented correctly.
  • notifier must be a valid pointer.