implemented wiiuse_io_write for mac

This commit is contained in:
Lysann Schlegel
2012-11-10 21:26:15 +01:00
parent a39cfd4374
commit 8c2f22089a
2 changed files with 75 additions and 28 deletions

View File

@@ -151,7 +151,18 @@ int wiiuse_os_read(struct wiimote_t* wm) {
} }
int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) { int wiiuse_os_write(struct wiimote_t* wm, byte* buf, int len) {
if(!wm) {
WIIUSE_ERROR("Attempting to write to NULL Wiimote");
return 0; return 0;
}
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
WiiuseWiimote* objc_wm = (WiiuseWiimote*) wm->objc_wm;
int result = [objc_wm writeBuffer: buf length: (NSUInteger)len];
[pool drain];
return result;
} }
#pragma mark - #pragma mark -
@@ -180,6 +191,8 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
@implementation WiiuseWiimote @implementation WiiuseWiimote
#pragma mark init, dealloc
- (id) initWithPtr: (wiimote*) wm_ device:(IOBluetoothDevice *)device_ { - (id) initWithPtr: (wiimote*) wm_ device:(IOBluetoothDevice *)device_ {
self = [super init]; self = [super init];
if(self) { if(self) {
@@ -206,6 +219,19 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
[super dealloc]; [super dealloc];
} }
#pragma mark connect, disconnect
- (BOOL) connectChannel: (IOBluetoothL2CAPChannel**) pChannel PSM: (BluetoothL2CAPPSM) psm {
if ([device openL2CAPChannelSync:pChannel withPSM:psm delegate:self] != kIOReturnSuccess) {
WIIUSE_ERROR("Unable to open L2CAP channel [id %i].", wm->unid);
*pChannel = nil;
return NO;
} else {
[*pChannel retain];
return YES;
}
}
- (IOReturn) connect { - (IOReturn) connect {
if(!device) { if(!device) {
WIIUSE_ERROR("Missing device."); WIIUSE_ERROR("Missing device.");
@@ -213,14 +239,11 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
} }
// open channels // open channels
if ([device openL2CAPChannelSync:&controlChannel withPSM:WM_OUTPUT_CHANNEL delegate:self] != kIOReturnSuccess) { if(![self connectChannel:&controlChannel PSM:WM_OUTPUT_CHANNEL]) {
WIIUSE_ERROR("Unable to open L2CAP control channel [id %i].", wm->unid); [self disconnect];
[device closeConnection];
return kIOReturnNotOpen; return kIOReturnNotOpen;
} else if([device openL2CAPChannelSync:&interruptChannel withPSM:WM_INPUT_CHANNEL delegate:self] != kIOReturnSuccess) { } else if(![self connectChannel:&interruptChannel PSM:WM_INPUT_CHANNEL]) {
WIIUSE_ERROR("Unable to open L2CAP interrupt channel [id %i].", wm->unid); [self disconnect];
[controlChannel closeChannel];
[device closeConnection];
return kIOReturnNotOpen; return kIOReturnNotOpen;
} }
@@ -228,31 +251,26 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
disconnectNotification = [device registerForDisconnectNotification:self selector:@selector(disconnected:fromDevice:)]; disconnectNotification = [device registerForDisconnectNotification:self selector:@selector(disconnected:fromDevice:)];
if(!disconnectNotification) { if(!disconnectNotification) {
WIIUSE_ERROR("Unable to register disconnection handler [id %i].", wm->unid); WIIUSE_ERROR("Unable to register disconnection handler [id %i].", wm->unid);
[interruptChannel closeChannel]; [self disconnect];
[controlChannel closeChannel];
[device closeConnection];
return kIOReturnNotOpen; return kIOReturnNotOpen;
} }
// retain channels
[controlChannel retain];
[interruptChannel retain];
return kIOReturnSuccess; return kIOReturnSuccess;
} }
- (void) disconnect { - (void) disconnectChannel: (IOBluetoothL2CAPChannel**) pChannel {
// interrupt channel if(!pChannel) return;
if([interruptChannel closeChannel] != kIOReturnSuccess)
WIIUSE_ERROR("Unable to close interrupt channel [id %i].", wm ? wm->unid : -1);
[interruptChannel release];
interruptChannel = nil;
// control channel if([*pChannel closeChannel] != kIOReturnSuccess)
if([controlChannel closeChannel] != kIOReturnSuccess) WIIUSE_ERROR("Unable to close channel [id %i].", wm ? wm->unid : -1);
WIIUSE_ERROR("Unable to close control channel [id %i].", wm ? wm->unid : -1); [*pChannel release];
[controlChannel release]; *pChannel = nil;
controlChannel = nil; }
- (void) disconnect {
// channels
[self disconnectChannel:&interruptChannel];
[self disconnectChannel:&controlChannel];
// device // device
if([device closeConnection] != kIOReturnSuccess) if([device closeConnection] != kIOReturnSuccess)
@@ -261,13 +279,40 @@ void wiiuse_cleanup_platform_fields(struct wiimote_t* wm) {
device = nil; device = nil;
} }
#pragma mark IOBluetoothL2CAPChannel delegates
- (void) disconnected:(IOBluetoothUserNotification*) notification fromDevice:(IOBluetoothDevice*) device { - (void) disconnected:(IOBluetoothUserNotification*) notification fromDevice:(IOBluetoothDevice*) device {
wiiuse_disconnected(wm); wiiuse_disconnected(wm);
} }
#pragma mark read, write
- (int) writeBuffer: (byte*) buffer length: (NSUInteger) length {
if(controlChannel == nil) {
WIIUSE_ERROR("Attempted to write to nil control channel [id %i].", wm->unid);
return 0;
}
IOReturn error = [controlChannel writeSync:buffer length:length];
if (error != kIOReturnSuccess) {
WIIUSE_ERROR("Error writing to control channel [id %i].", wm->unid);
WIIUSE_DEBUG("Attempting to reopen the control channel [id %i].", wm->unid);
[self disconnectChannel:&controlChannel];
[self connectChannel:&controlChannel PSM:WM_OUTPUT_CHANNEL];
if(!controlChannel) {
WIIUSE_ERROR("Error reopening the control channel [id %i].", wm->unid);
[self disconnect];
} else {
WIIUSE_DEBUG("Attempting to write again to the control channel [id %i].", wm->unid);
error = [controlChannel writeSync:buffer length:length];
if (error != kIOReturnSuccess)
WIIUSE_ERROR("Unable to write again to the control channel [id %i].", wm->unid);
}
}
return (error == kIOReturnSuccess) ? length : 0;
}
@end @end
#endif // __APPLE__ #endif // __APPLE__

View File

@@ -66,6 +66,8 @@
- (IOReturn) connect; - (IOReturn) connect;
- (void) disconnect; - (void) disconnect;
- (int) writeBuffer: (byte*) buffer length: (NSUInteger) length;
@end @end