powermgt_macosx.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "config.h"
00024 #include <ctype.h>
00025
00026 #ifdef __APPLE__
00027 #include <stdlib.h>
00028 #include <stdio.h>
00029
00030 #include <mach/mach_port.h>
00031 #include <mach/mach_interface.h>
00032 #include <mach/mach_init.h>
00033
00034 #include <IOKit/pwr_mgt/IOPMLib.h>
00035 #include <IOKit/IOMessage.h>
00036
00037 #include "misc.h"
00038 #include "pcsclite.h"
00039 #include "pcscd.h"
00040 #include "debuglog.h"
00041 #include "readerfactory.h"
00042 #include "thread_generic.h"
00043 #include "hotplug.h"
00044
00045 static io_connect_t root_port;
00046 static IONotificationPortRef notify;
00047 static io_object_t anIterator;
00048
00049 PCSCLITE_THREAD_T pmgmtThread;
00050 extern PCSCLITE_MUTEX usbNotifierMutex;
00051
00052 void PMPowerRegistrationThread();
00053
00054 void PMPowerEventCallback(void * x,io_service_t y,natural_t messageType,void * messageArgument)
00055 {
00056
00057 switch ( messageType ) {
00058 case kIOMessageCanSystemSleep:
00059 IOAllowPowerChange(root_port,(long)messageArgument);
00060 break;
00061 case kIOMessageSystemWillSleep:
00062 Log1(PCSC_LOG_INFO, "system going into sleep");
00063 SYS_MutexLock(&usbNotifierMutex);
00064 RFSuspendAllReaders();
00065 IOAllowPowerChange(root_port,(long)messageArgument);
00066 Log1(PCSC_LOG_INFO, "system allowed to sleep");
00067 break;
00068 case kIOMessageSystemHasPoweredOn:
00069 Log1(PCSC_LOG_INFO, "system coming out of sleep");
00070 HPSearchHotPluggables();
00071 RFAwakeAllReaders();
00072 SYS_MutexUnLock(&usbNotifierMutex);
00073 break;
00074 }
00075
00076 }
00077
00078 void PMPowerRegistrationThread() {
00079
00080 root_port = IORegisterForSystemPower (0,¬ify,PMPowerEventCallback,&anIterator);
00081
00082 if ( root_port == 0 ) {
00083 Log1(PCSC_LOG_ERROR, "IORegisterForSystemPower failed");
00084 return;
00085 }
00086
00087 CFRunLoopAddSource(CFRunLoopGetCurrent(),
00088 IONotificationPortGetRunLoopSource(notify),
00089 kCFRunLoopDefaultMode);
00090
00091 CFRunLoopRun();
00092 }
00093
00094 ULONG PMRegisterForPowerEvents() {
00095
00096 LONG rv;
00097
00098 rv = SYS_ThreadCreate(&pmgmtThread, THREAD_ATTR_DEFAULT,
00099 (PCSCLITE_THREAD_FUNCTION( )) PMPowerRegistrationThread, NULL);
00100 return 0;
00101 }
00102
00103 #endif