OpticalMessageListenerTool.c [plain text]
#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOMessage.h>
#include <IOKit/scsi/IOSCSIMultimediaCommandsDevice.h>
#include <CoreFoundation/CoreFoundation.h>
#define DEBUG 0
#define DEBUG_ASSERT_COMPONENT_NAME_STRING "OpticalMessageListenerTool"
#if DEBUG
#define DEBUG_ASSERT_MESSAGE(componentNameString, \
assertionString, \
exceptionLabelString, \
errorString, \
fileName, \
lineNumber, \
errorCode) \
DebugAssert(componentNameString, \
assertionString, \
exceptionLabelString, \
errorString, \
fileName, \
lineNumber, \
errorCode) \
static void
DebugAssert ( const char * componentNameString,
const char * assertionString,
const char * exceptionLabelString,
const char * errorString,
const char * fileName,
long lineNumber,
int errorCode )
{
if ( ( assertionString != NULL ) && ( *assertionString != '\0' ) )
printf ( "Assertion failed: %s: %s\n", componentNameString, assertionString );
else
printf ( "Check failed: %s:\n", componentNameString );
if ( exceptionLabelString != NULL )
printf ( " %s\n", exceptionLabelString );
if ( errorString != NULL )
printf ( " %s\n", errorString );
if ( fileName != NULL )
printf ( " file: %s\n", fileName );
if ( lineNumber != 0 )
printf ( " line: %ld\n", lineNumber );
if ( errorCode != 0 )
printf ( " error: %d\n", errorCode );
}
#endif
#include <AssertMacros.h>
#define kIOCDBlockStorageDeviceString "IOCDBlockStorageDevice"
static void
ServiceMatched ( void * refcon, io_iterator_t iterator );
static void
ServiceInterest ( void * refcon, io_service_t service, natural_t type, void * arg );
static IONotificationPortRef gNotifyPort = NULL;
int
main ( int argc, const char * argv[] )
{
io_iterator_t iterator = MACH_PORT_NULL;
gNotifyPort = IONotificationPortCreate ( kIOMasterPortDefault );
CFRunLoopAddSource ( CFRunLoopGetCurrent ( ),
IONotificationPortGetRunLoopSource ( gNotifyPort ),
kCFRunLoopCommonModes );
IOServiceAddMatchingNotification ( gNotifyPort,
kIOFirstMatchNotification,
IOServiceMatching ( kIOCDBlockStorageDeviceString ),
&ServiceMatched,
NULL,
&iterator );
ServiceMatched ( NULL, iterator );
printf ( "OpticalMessageListener\n" );
printf ( "Copyright 2002-2003, Apple Computer, Inc.\n\n" );
fflush ( stdout );
CFRunLoopRun ( );
if ( iterator != 0 )
{
IOObjectRelease ( iterator );
iterator = 0;
}
CFRunLoopRemoveSource ( CFRunLoopGetCurrent ( ),
IONotificationPortGetRunLoopSource ( gNotifyPort ),
kCFRunLoopCommonModes );
if ( gNotifyPort != 0 )
{
IONotificationPortDestroy ( gNotifyPort );
gNotifyPort = 0;
}
return 0;
}
void
ServiceMatched ( void * refcon, io_iterator_t iterator )
{
io_service_t service = MACH_PORT_NULL;
io_iterator_t ignored = MACH_PORT_NULL;
while ( ( service = IOIteratorNext ( iterator ) ) != MACH_PORT_NULL )
{
IOServiceAddInterestNotification ( gNotifyPort,
service,
kIOGeneralInterest,
ServiceInterest,
NULL,
&ignored );
}
}
void
ServiceInterest ( void * refcon, io_service_t service, natural_t type, void * arg )
{
printf ( "type = %08lX, arg = %08lX\n", ( UInt32 ) type, ( UInt32 ) arg );
switch ( type )
{
case kIOMessageTrayStateChange:
{
printf ( "Tray state change " );
if ( ( UInt32 ) arg == kMessageTrayStateChangeRequestAccepted )
{
printf ( "accepted.\n" );
}
else if ( ( UInt32 ) arg == kMessageTrayStateChangeRequestRejected )
{
printf ( "rejected - exclusive user client has control.\n" );
}
}
break;
case kIOMessageMediaAccessChange:
{
printf ( "Media access change " );
if ( ( UInt32 ) arg == kMessageDeterminingMediaPresence )
{
printf ( "determining media presence.\n" );
}
else if ( ( UInt32 ) arg == kMessageFoundMedia )
{
printf ( "media found in drive.\n" );
}
else if ( ( UInt32 ) arg == kMessageFoundMedia )
{
printf ( "media type determined.\n" );
}
}
break;
case kIOMessageServiceIsTerminated:
{
printf ( "Service terminated\n" );
}
break;
case kIOMessageServiceIsSuspended:
{
printf ( "Service suspended\n" );
}
break;
case kIOMessageServiceIsResumed:
{
printf ( "Service resumed\n" );
}
break;
case kIOMessageServiceIsRequestingClose:
{
printf ( "Requesting close\n" );
}
break;
case kIOMessageServiceIsAttemptingOpen:
{
printf ( "Attempting open\n" );
}
break;
case kIOMessageServiceWasClosed:
{
printf ( "Service closed\n" );
}
break;
case kIOMessageServiceBusyStateChange:
{
printf ( "Busy state change\n" );
}
break;
default:
break;
}
printf ( "\n" );
fflush ( stdout );
}