IOKit problems with USB IR device
IOKit problems with USB IR device
- Subject: IOKit problems with USB IR device
- From: Greg Hulands <email@hidden>
- Date: Sat, 25 Jan 2003 09:38:36 +1000
Hi,
I am trying to get the Polar Heart Rate Monitor Infra Red adaptor to
work on OS X. There is a library that was written on linux that I am
using as a reference point. I am able to find the device quite ok, but
when it comes to sending commands to the device, nothing happens. I am
new to this type of thing, so I am probably missing a fundamental
technique used to access usb devices.
The code is as follows
#import <Foundation/Foundation.h>
#import <IOKit/usb/IOUSBLib.h>
@class PPacket;
@interface PolarS710Device : NSObject
{
IOUSBDeviceInterface182 **_device;
IOUSBInterfaceInterface183 **_interface;
}
+ (id)sharedDevice;
- (BOOL)sendPacket:(PPacket *)packet;
- (PPacket *)receivePacket;
@end
@implementation PolarS710Device
+ (id)sharedDevice
{
if (!_sharedDevice)
_sharedDevice = [[PolarS710Device alloc] init];
return _sharedDevice;
}
- (id)init
{
if (self = [super init]) {
kern_return_t kr;
mach_port_t masterPort;
io_iterator_t iterator;
io_service_t usbDevice;
CFMutableDictionaryRef matchingDict;
SInt32 usbVendor = S710_USB_VENDOR_ID;
SInt32 usbProduct = S710_USB_PRODUCT_ID;
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if(KERN_SUCCESS != kr)
[NSException raise:PolarS710Exeception format:@"Failed to
get master device port!:%d", kr];
// set vendor and products ids
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict)
[NSException raise:PolarS710Exeception format:@"Could not
create USB Matching Dict"];
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBVendorID),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbVendor));
CFDictionarySetValue(
matchingDict,
CFSTR(kUSBProductID),
CFNumberCreate(kCFAllocatorDefault,
kCFNumberSInt32Type, &usbProduct));
kr = IOServiceGetMatchingServices(masterPort, matchingDict,
&iterator);
if(KERN_SUCCESS != kr)
[NSException raise:PolarS710Exeception format:@"Failed to
get service!:%d", kr];
if (usbDevice = IOIteratorNext(iterator)) {
IOCFPlugInInterface **plugInInterface = NULL;
SInt32 score;
kr = IOCreatePlugInInterfaceForService(usbDevice,
kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface,
&score);
if(KERN_SUCCESS != kr)
[NSException raise:PolarS710Exeception format:@"Failed
to create plugin!:%d", kr];
IOObjectRelease(usbDevice);
(*plugInInterface)->QueryInterface(plugInInterface,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID)&_device);
(*plugInInterface)->Release(plugInInterface);
(*_device)->USBDeviceOpen(_device);
} else
[NSException raise:PolarS710Exeception format:@"Failed to
find Polar IR USB Adaptor. Make sure it is connected"];
IOObjectRelease(iterator);
mach_port_deallocate(mach_task_self(), masterPort);
}
return self;
}
- (void)dealloc
{
(*_device)->USBDeviceClose(_device);
(*_device)->Release(_device);
[super dealloc];
}
- (BOOL)sendPacket:(char *)serialized length:(int)bytes
{
IOUSBDevRequestTO request;
kern_return_t result;
memset(&request, 0, sizeof(IOUSBDevRequestTO));
request.bmRequestType = USBmakebmRequestType(kUSBOut, kUSBVendor,
kUSBDevice);
request.bRequest = 0x01;
request.wValue = 0x0801;
request.wIndex = 0x0000;
request.wLength = bytes;
request.pData = serialized;
request.completionTimeout = 1000;
result = (*_device)->DeviceRequestTO(_device, &request);
if (result != kIOReturnSuccess)
{
NSLog(@"Error sending control message");
return NO;
}
/* i am pretty sure DeviceRequest modifies this */
return YES;
}
If anyone has had experience with this type of device and could provide
some pointers, it would be greatly appreciated.
Regards,
Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.