Mac-OS 开发 与USB HID通信

macOS 开发

//
//  USBDeviceTool.h
//  Mac-USB-04
//
//  Created by nb616 on 2017/7/29.
//  Copyright © 2017年 CNDotaIsBestDota. All rights reserved.
//

#import 
#import 
#import 

@protocol USBDeviceDelegate 

- (void)robotPenUSBRecvData:(uint8_t*)recvData;

- (void)robotPenUSBConnectDevice:(IOHIDDeviceRef)deviceRef;

- (void)robotPenUSBRomveDevice;
@end

@interface USBDeviceTool : NSObject

+ (USBDeviceTool *)share;

@property (weak , nonatomic) id  delegate;
@property (assign , nonatomic) IOHIDManagerRef managerRef;
@property (assign , nonatomic) IOHIDDeviceRef deviceRef;

/**
 连接设备
 */
- (void)connectDevice;

/**
 断开设备
 */
- (void)disConnectDevice;

/**
 发送数据USB
 
 @param buffer buffer
 */
- (void)sendData:(char *)buffer;

@end


#import "USBDeviceTool.h"
#import "USBDeviceTool+robot.h"
static USBDeviceTool *_tool = nil;

void Handle_DeviceOutgoingData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength) {
    [[USBDeviceTool share].delegate robotPenUSBRecvData:report];
}

void Handle_DeviceMatchingCallback(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
    
    BOOL isRot = [[USBDeviceTool share] isRobotProdut:inIOHIDDeviceRef];
    if (isRot && ![USBDeviceTool share].deviceRef) {
        IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0L);
        [USBDeviceTool share].deviceRef = inIOHIDDeviceRef;
        [[USBDeviceTool share].delegate robotPenUSBConnectDevice:inIOHIDDeviceRef];
        if (ret == kIOReturnSuccess) {
           
            char *inputbuffer = malloc(64);
            IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, (uint8_t*)inputbuffer, 64, Handle_DeviceOutgoingData, NULL);
            
            [[USBDeviceTool share] enterUSBState];
        }
    }
}

void Handle_DeviceRemovalCallback(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
    
    BOOL isRot = [[USBDeviceTool share] isRobotProdut:inIOHIDDeviceRef];
    if (isRot) {
        [[USBDeviceTool share].delegate robotPenUSBRomveDevice];
         [USBDeviceTool share].deviceRef = nil;
    }
}

@interface USBDeviceTool ()


@end

@implementation USBDeviceTool

+ (USBDeviceTool *)share
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!_tool) {
            _tool = [[USBDeviceTool alloc] init];
        }
    });
    return _tool;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.managerRef = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
        IOHIDManagerScheduleWithRunLoop(self.managerRef, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
        IOReturn ret = IOHIDManagerOpen(self.managerRef, kIOHIDOptionsTypeNone);
        if (ret != kIOReturnSuccess) {
            NSLog(@"打开失败");
        }
        IOHIDManagerRegisterDeviceMatchingCallback(self.managerRef, &Handle_DeviceMatchingCallback, NULL);
        IOHIDManagerRegisterDeviceRemovalCallback(self.managerRef, &Handle_DeviceRemovalCallback, NULL);
        IOHIDManagerSetDeviceMatching(self.managerRef, NULL);
        
    }
    return self;
}



- (void)connectDevice
{
    if (!_deviceRef) {
         IOHIDManagerSetDeviceMatching(self.managerRef, NULL);
    }
}

- (void)disConnectDevice
{
    if (!_deviceRef) {
        return;
    }
    
    if ([self isRobotProdut:_deviceRef]) {
        
        [self exitOutUSBState];
        
        IOReturn ret = IOHIDDeviceClose(_deviceRef, 0L);
        if (ret == kIOReturnSuccess) {
            _deviceRef = nil;
            [[USBDeviceTool share].delegate robotPenUSBRomveDevice];
        }
    }
   
}

- (void)sendData:(char *)buffer
{
    if (!_deviceRef) {
        return ;
    }
    
    BOOL isRot = [self isRobotProdut:_deviceRef];
    if (isRot) {
        IOReturn ret = IOHIDDeviceSetReport(_deviceRef, kIOHIDReportTypeOutput, 0, (uint8_t*)buffer, 64);
        if (ret == kIOReturnSuccess) {
            NSLog(@"发送数据成功");
        }
    }
}
@end

你可能感兴趣的:(Mac-OS 开发 与USB HID通信)