MAC中如何访问usb 设备
在MAC中, 访问设备是用IOKit这个通讯层, 可我就是在找到设备,打开接口的时候失败。
err = (*intf)->USBIterfaceOpen(intf); //err 返回0xe00002c5, 是“The device is already ope
//n with exclusive access (0xe00002c5).”
我查了好多进程的信息,也没有发现哪个进程占用了这个设备。
这个在MAC中如何访问USB设备呢? 不能象UNIX一样访问,是因为我现在没有找到对应的 /dev/xxx.
谢谢!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
createFile的string是“\\.\A:”, 在windows2000以上。
这个USB设备不全是一个U盘,它里面有个单片机,我只是通过USB线来和单片机通信。通过USB线发送自定义的命令让单片机执行。我们有很多的交互过程,所以即有读也有写。我现在知道的是USB是个标准的。看MAC的本机信息象是一个HID设备。谢谢你的顶力相助。
你在windows下是createfile的string是什么?
你这样做的目的是什么呢?read data?
是一个mass storage设备, 我们在windows里面就可以直接用createFile访问的。可在MAC OX中找不到descriptor,也就无法访问了。
:),sigh,我看贴不仔细~~;
U盘不能用你这种方式访问的,能说说你想干啥么?
interface的open是独占的,hid、mass storage设备插入后相应的interface会被
系统open,不清楚你这个属于哪种情况
什么设备?
就是不知道可不可以象UNIX那样,只访问文件就可以了。 通过IOKit还是很有难度的。恳请哪位大侠指教。谢谢
访问U盘, 下面是源码: U盘的vid是0x10d6, pid:0x1100. 这个是MAC上面的一个例子在 /Developer/Example/IOKit/usb/USBSimple Example.
#include <stdio.h>
#include <mach/mach.h>
#include <CoreFoundation/CFNumber.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
mach_port_t masterPort = 0; // requires <mach/mach.h>
char outBuf[8096];
char inBuf[8096];
void
MyCallBackFunction(void *dummy, IOReturn result, void *arg0)
{
// UInt8 inPipeRef = (UInt32)dummy;
printf("MyCallbackfunction: %d, %d, %dn", (int)dummy, (int)result, (int)arg0);
CFRunLoopStop(CFRunLoopGetCurrent());
}
void transferData(IOUSBInterfaceInterface **intf, UInt8 inPipeRef, UInt8 outPipeRef)
{
IOReturn err;
CFRunLoopSourceRef cfSource;
int i;
err = (*intf)->CreateInterfaceAsyncEventSource(intf, &cfSource);
if (err)
{
printf("transferData: unable to create event source, err = %08xn", err);
return;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), cfSource, kCFRunLoopDefaultMode);
for (i=0; i < 12; i++)
outBuf[i] = 'R';
err = (*intf)->WritePipeAsync(intf, outPipeRef, outBuf, 12, (IOAsyncCallback1)MyCallBackFunction, (void*)(UInt32)inPipeRef);
if (err)
{
printf("transferData: WritePipeAsyncFailed, err = %08xn", err);
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource, kCFRunLoopDefaultMode);
return;
}
printf("transferData: calling CFRunLoopRunn");
CFRunLoopRun();
printf("transferData: returned from CFRunLoopRunn");
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource, kCFRunLoopDefaultMode);
}
void dealWithPipes(IOUSBInterfaceInterface **intf, UInt8 numPipes)
{
int i;
IOReturn err;
UInt8 inPipeRef = 0;
UInt8 outPipeRef = 0;
UInt8 direction, number, transferType, interval;
UInt16 maxPacketSize;
// pipes are one based, since zero is the default control pipe
for (i=1; i <= numPipes; i++)
{
err = (*intf)->GetPipeProperties(intf, i, &direction, &number, &transferType, &maxPacketSize, &interval);
if (err)
{
printf("dealWithPipes: unable to get pipe properties for pipe %d, err = %08xn", i, err);
return;
}
if (transferType != kUSBBulk)
{
printf("dealWithPipes: skipping pipe %d because it is not a bulk pipen", i);
continue;
}
if ((direction == kUSBIn) && !inPipeRef)
{
printf("dealWithPipes: grabbing BULK IN pipe index %d, number %dn",i, number);
inPipeRef = i;
}
if ((direction == kUSBOut) && !outPipeRef)
{
printf("dealWithPipes: grabbing BULK OUT pipe index %d, number %dn", i, number);
outPipeRef = i;
}
}
// if (inPipeRef && outPipeRef)
// transferData(intf, inPipeRef, outPipeRef);
}
//#include <IOBlockStorageDevice.h>
void dealWithInterface(io_service_t usbInterfaceRef)
{
IOReturn err;
IOCFPlugInInterface **iodev = NULL; // requires <IOKit/IOCFPlugIn.h>
IOUSBInterfaceInterface **intf;
SInt32 score;
UInt8 numPipes;
UInt8 interfaceClass;
UInt8 interfaceSubClass;
err = IOCreatePlugInInterfaceForService(usbInterfaceRef, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
err = IOObjectRelease(usbInterfaceRef);
if (err || !iodev)
{
printf("dealWithInterface: unable to create plugin. ret = %08x, iodev = %pn", err, iodev);
return;
}
err = (*iodev)->QueryInterface(iodev, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID)&intf);
err = IODestroyPlugInInterface(iodev); // done with this
//err = (*iodev)->Release(iodev);
if (err || !intf)
{
printf("dealWithInterface: unable to create a device interface. ret = %08x, intf = %pn", err, intf);
return;
}
//doLockUnlockMedia(1);
/*err = (*intf)->USBInterfaceClose(intf);
if (err)
{
printf("dealWithInterface: unable to close interface. ret = %08xn", err);
return;
}
*/
err = (*intf)->GetInterfaceClass(intf, &interfaceClass);
err = (*intf)->GetInterfaceSubClass(intf, &interfaceSubClass);
err = (*intf)->USBInterfaceOpen(intf); //就是在这里出错的。
if (err != kIOReturnSuccess)
{
printf("dealWithInterface: unable to open interface. ret = %08xn", err);
(void)(*intf)->Release(intf);
return;
}
err = (*intf)->GetNumEndpoints(intf, &numPipes);
if (err)
{
printf("dealWithInterface: unable to get number of endpoints. ret = %08xn", err);
(*intf)->USBInterfaceClose(intf);
(*intf)->Release(intf);
return;
}
printf("dealWithInterface: found %d pipesn", numPipes);
if (numPipes == 0)
{
// try alternate setting 1
err = (*intf)->SetAlternateInterface(intf, 1);
if (err)
{
printf("dealWithInterface: unable to set alternate interface 1. ret = %08xn", err);
(*intf)->USBInterfaceClose(intf);
(*intf)->Release(intf);
return;
}
err = (*intf)->GetNumEndpoints(intf, &numPipes);
if (err)
{
printf("dealWithInterface: unable to get number of endpoints - alt setting 1. ret = %08xn", err);
(*intf)->USBInterfaceClose(intf);
(*intf)->Release(intf);
return;
}
numPipes = 13; // workaround. GetNumEndpoints does not work after SetAlternateInterface
}
if (numPipes)
dealWithPipes(intf, numPipes);
err = (*intf)->USBInterfaceClose(intf);
if (err)
{
printf("dealWithInterface: unable to close interface. ret = %08xn", err);
return;
}
err = (*intf)->Release(intf);
if (err)
{
printf("dealWithInterface: unable to release interface. ret = %08xn", err);
return;
}
}
void dealWithDevice(io_service_t usbDeviceRef)
{
IOReturn err;
IOCFPlugInInterface **iodev; // requires <IOKit/IOCFPlugIn.h>
IOUSBDeviceInterface **dev;
SInt32 score;
UInt8 numConf;
IOUSBConfigurationDescriptorPtr confDesc;
IOUSBFindInterfaceRequest interfaceRequest;
io_iterator_t iterator;
io_service_t usbInterfaceRef;
err = IOCreatePlugInInterfaceForService(usbDeviceRef, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
if (err || !iodev)
{
printf("dealWithDevice: unable to create plugin. ret = %08x, iodev = %pn", err, iodev);
return;
}
err = (*iodev)->QueryInterface(iodev, CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID)&dev);
IODestroyPlugInInterface(iodev); // done with this
if (err || !dev)
{
printf("dealWithDevice: unable to create a device interface. ret = %08x, dev = %pn", err, dev);
return;
}
err = (*dev)->ResetDevice(dev);//(*iodev)->
err = (*dev)->USBDeviceOpen(dev);
if (err)
{
printf("dealWithDevice: unable to open device. ret = %08xn", err);
return;
}
if (err)
{
printf("dealWithDevice: unable to reset device. ret = %08xn", err);
//return;
}
err = (*dev)->GetNumberOfConfigurations(dev, &numConf);
if (err || !numConf)
{
printf("dealWithDevice: unable to obtain the number of configurations. ret = %08xn", err);
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return;
}
printf("dealWithDevice: found %d configurationsn", numConf);
err = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &confDesc); // get the first config desc (index 0)
if (err)
{
printf("dealWithDevice:unable to get config descriptor for index 0n");
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return;
}/*
err = (*dev)->SetConfiguration(dev, confDesc->bConfigurationValue);
if (err)
{
printf("dealWithDevice: unable to set the configurationn");
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return;
}*/
interfaceRequest.bInterfaceClass = kIOUSBFindInterfaceDontCare; // requested class
interfaceRequest.bInterfaceSubClass = kIOUSBFindInterfaceDontCare; // requested subclass
interfaceRequest.bInterfaceProtocol = kIOUSBFindInterfaceDontCare; // requested protocol
interfaceRequest.bAlternateSetting = kIOUSBFindInterfaceDontCare; // requested alt setting
err = (*dev)->CreateInterfaceIterator(dev, &interfaceRequest, &iterator);
if (err)
{
printf("dealWithDevice: unable to create interface iteratorn");
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return;
}
while ( (usbInterfaceRef = IOIteratorNext(iterator)) )
{
printf("found interface: %pn", (void*)usbInterfaceRef);
dealWithInterface(usbInterfaceRef);
//IOObjectRelease(usbInterfaceRef); // no longer need this reference
}
IOObjectRelease(iterator);
iterator = 0;
err = (*dev)->USBDeviceClose(dev);
if (err)
{
printf("dealWithDevice: error closing device - %08xn", err);
(*dev)->Release(dev);
return;
}
err = (*dev)->Release(dev);
if (err)
{
printf("dealWithDevice: error releasing device - %08xn", err);
return;
}
}
int main (int argc, const char * argv[])
{
kern_return_t err;
CFMutableDictionaryRef matchingDictionary = 0; // requires <IOKit/IOKitLib.h>
SInt32 idVendor = 0x10d6;//0x1403;//1351;
SInt32 idProduct = 0x1100;//0x0001;//8193;
CFNumberRef numberRef;
io_iterator_t iterator = 0;
io_service_t usbDeviceRef;
err = IOMasterPort(MACH_PORT_NULL, &masterPort);
if (err)
{
printf("USBSimpleExample: could not create master port, err = %08xn", err);
return err;
}
//kIOUSBDeviceClassNam = 08;
matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName); // requires <IOKit/usb/IOUSBLib.h>
if (!matchingDictionary)
{
printf("USBSimpleExample: could not create matching dictionaryn");
return -1;
}
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor);
if (!numberRef)
{
printf("USBSimpleExample: could not create CFNumberRef for vendorn");
return -1;
}
//kUSBVendorID = 1403;
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBVendorID), numberRef);
CFRelease(numberRef);
numberRef = 0;
numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct);
if (!numberRef)
{
printf("USBSimpleExample: could not create CFNumberRef for productn");
return -1;
}
//kUSBProductID = 0001;
CFDictionaryAddValue(matchingDictionary, CFSTR(kUSBProductID), numberRef);
CFRelease(numberRef);
numberRef = 0;
err = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
matchingDictionary = 0; // this was consumed by the above call
while ( (usbDeviceRef = IOIteratorNext(iterator)) )
{
printf("Found device %pn", (void*)usbDeviceRef);
dealWithDevice(usbDeviceRef);
IOObjectRelease(usbDeviceRef); // no longer need this reference
}
IOObjectRelease(iterator);
iterator = 0;
mach_port_deallocate(mach_task_self(), masterPort);
return 0;
}
是什么USB设备,你要做什么,没头没尾的,只给出一个返回的错误值,怎么回答你呢?