powermgt_macosx.c

00001 /*
00002  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
00003  *
00004  * @APPLE_LICENSE_HEADER_START@
00005  *
00006  * The contents of this file constitute Original Code as defined in and
00007  * are subject to the Apple Public Source License Version 1.1 (the
00008  * "License").  You may not use this file except in compliance with the
00009  * License.  Please obtain a copy of the License at
00010  * http://www.apple.com/publicsource and read it before using this file.
00011  *
00012  * This Original Code and all software distributed under the License are
00013  * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
00014  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
00015  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
00017  * License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * @APPLE_LICENSE_HEADER_END@
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,&notify,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  /* __APPLE__ */

Generated on Mon Aug 17 01:00:10 2009 for pcsc-lite by  doxygen 1.5.9