免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 25104 | 回复: 29
打印 上一主题 下一主题

MAC中如何访问usb 设备 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-31 15:07 |只看该作者 |倒序浏览
在MAC中, 访问设备是用IOKit这个通讯层, 可我就是在找到设备,打开接口的时候失败。
err = (*intf)->USBIterfaceOpen(intf);  //err 返回0xe00002c5, 是“The device is already ope
                                                           //n with exclusive access (0xe00002c5).”

我查了好多进程的信息,也没有发现哪个进程占用了这个设备。
这个在MAC中如何访问USB设备呢? 不能象UNIX一样访问,是因为我现在没有找到对应的 /dev/xxx.
谢谢!!

论坛徽章:
0
2 [报告]
发表于 2007-05-31 15:40 |只看该作者
是什么USB设备,你要做什么,没头没尾的,只给出一个返回的错误值,怎么回答你呢?

论坛徽章:
0
3 [报告]
发表于 2007-05-31 16:20 |只看该作者
访问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, %d\n", (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 = %08x\n", 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 = %08x\n", err);
        CFRunLoopRemoveSource(CFRunLoopGetCurrent(), cfSource, kCFRunLoopDefaultMode);
        return;
    }
    printf("transferData: calling CFRunLoopRun\n");
    CFRunLoopRun();
    printf("transferData: returned from  CFRunLoopRun\n");
    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 = %08x\n", i, err);
            return;
        }
        if (transferType != kUSBBulk)
        {
            printf("dealWithPipes: skipping pipe %d because it is not a bulk pipe\n", i);
            continue;
        }
        if ((direction == kUSBIn) && !inPipeRef)
        {
            printf("dealWithPipes: grabbing BULK IN pipe index %d, number %d\n",i, number);
            inPipeRef = i;
        }
        if ((direction == kUSBOut) && !outPipeRef)
        {
            printf("dealWithPipes: grabbing BULK OUT pipe index %d, number %d\n", 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 = %p\n", 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 = %p\n", err, intf);
                return;
    }
       
        //doLockUnlockMedia(1);
        /*err = (*intf)->USBInterfaceClose(intf);
        if (err)
    {
                printf("dealWithInterface: unable to close interface. ret = %08x\n", 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 = %08x\n", err);
                (void)(*intf)->Release(intf);
                return;
    }
    err = (*intf)->GetNumEndpoints(intf, &numPipes);
    if (err)
    {
                printf("dealWithInterface: unable to get number of endpoints. ret = %08x\n", err);
                (*intf)->USBInterfaceClose(intf);
                (*intf)->Release(intf);
                return;
    }
       
    printf("dealWithInterface: found %d pipes\n", numPipes);
    if (numPipes == 0)
    {
                // try alternate setting 1
                err = (*intf)->SetAlternateInterface(intf, 1);
                if (err)
                {
                        printf("dealWithInterface: unable to set alternate interface 1. ret = %08x\n", 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 = %08x\n", 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 = %08x\n", err);
                return;
    }
    err = (*intf)->Release(intf);
    if (err)
    {
                printf("dealWithInterface: unable to release interface. ret = %08x\n", 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 = %p\n", 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 = %p\n", err, dev);
                return;
    }
       
        err = (*dev)->ResetDevice(dev);//(*iodev)->

       
    err = (*dev)->USBDeviceOpen(dev);
    if (err)
    {
                printf("dealWithDevice: unable to open device. ret = %08x\n", err);
                return;
    }       
       
        if (err)
    {
                printf("dealWithDevice: unable to reset device. ret = %08x\n", err);
                //return;
    }
   
        err = (*dev)->GetNumberOfConfigurations(dev, &numConf);
    if (err || !numConf)
    {
                printf("dealWithDevice: unable to obtain the number of configurations. ret = %08x\n", err);
        (*dev)->USBDeviceClose(dev);
        (*dev)->Release(dev);
                return;
    }
    printf("dealWithDevice: found %d configurations\n", 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 0\n");
        (*dev)->USBDeviceClose(dev);
        (*dev)->Release(dev);
                return;
    }/*
    err = (*dev)->SetConfiguration(dev, confDesc->bConfigurationValue);
    if (err)
    {
                printf("dealWithDevice: unable to set the configuration\n");
        (*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 iterator\n");
        (*dev)->USBDeviceClose(dev);
        (*dev)->Release(dev);
                return;
    }
   
    while ( (usbInterfaceRef = IOIteratorNext(iterator)) )
    {
                printf("found interface: %p\n", (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 - %08x\n", err);
                (*dev)->Release(dev);
                return;
    }
    err = (*dev)->Release(dev);
    if (err)
    {
                printf("dealWithDevice: error releasing device - %08x\n", 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 = %08x\n", err);
        return err;
    }
        //kIOUSBDeviceClassNam = 08;
    matchingDictionary = IOServiceMatching(kIOUSBDeviceClassName);        // requires <IOKit/usb/IOUSBLib.h>
    if (!matchingDictionary)
    {
        printf("USBSimpleExample: could not create matching dictionary\n");
        return -1;
    }
    numberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor);
    if (!numberRef)
    {
        printf("USBSimpleExample: could not create CFNumberRef for vendor\n");
        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 product\n");
        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 %p\n", (void*)usbDeviceRef);
                dealWithDevice(usbDeviceRef);
                IOObjectRelease(usbDeviceRef);                        // no longer need this reference
    }
   
    IOObjectRelease(iterator);
    iterator = 0;
   
    mach_port_deallocate(mach_task_self(), masterPort);
    return 0;
}

论坛徽章:
0
4 [报告]
发表于 2007-05-31 16:25 |只看该作者
就是不知道可不可以象UNIX那样,只访问文件就可以了。 通过IOKit还是很有难度的。恳请哪位大侠指教。谢谢

论坛徽章:
0
5 [报告]
发表于 2007-06-01 14:28 |只看该作者
什么设备?

论坛徽章:
0
6 [报告]
发表于 2007-06-01 14:31 |只看该作者
interface的open是独占的,hid、mass storage设备插入后相应的interface会被
系统open,不清楚你这个属于哪种情况

论坛徽章:
0
7 [报告]
发表于 2007-06-01 14:32 |只看该作者
:),sigh,我看贴不仔细~~;
U盘不能用你这种方式访问的,能说说你想干啥么?

论坛徽章:
0
8 [报告]
发表于 2007-06-02 09:05 |只看该作者
是一个mass storage设备, 我们在windows里面就可以直接用createFile访问的。可在MAC OX中找不到descriptor,也就无法访问了。

论坛徽章:
0
9 [报告]
发表于 2007-06-04 13:08 |只看该作者
你在windows下是createfile的string是什么?
你这样做的目的是什么呢?read data?

论坛徽章:
0
10 [报告]
发表于 2007-06-05 10:09 |只看该作者
createFile的string是“\\\\.\\A:”, 在windows2000以上。

这个USB设备不全是一个U盘,它里面有个单片机,我只是通过USB线来和单片机通信。通过USB线发送自定义的命令让单片机执行。我们有很多的交互过程,所以即有读也有写。我现在知道的是USB是个标准的。看MAC的本机信息象是一个HID设备。谢谢你的顶力相助。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP