Exemplo n.º 1
0
IOKitDevice::IOKitDevice(const IOKitDevice& device)
:
   usBusNumber(device.usBusNumber),
	usAddress(device.usAddress),
   usVid(device.usVid),
   usPid(device.usPid),
	usConfigurationNum(device.usConfigurationNum),
   ulSerialNumber(device.ulSerialNumber),
	ulLocation(device.ulLocation),
   hSavedService(NULL)
{

   if (device.hSavedService)
   {
      IOObjectRetain(device.hSavedService);
      hSavedService = device.hSavedService;
   }

   STRNCPY(szProductDescription, device.szProductDescription, sizeof(szProductDescription));
   memcpy(szSerialString, device.szSerialString, sizeof(szSerialString));
   memcpy(&stDeviceDescriptor, &(device.stDeviceDescriptor), sizeof(device.stDeviceDescriptor));
   memcpy(szSystemPath, device.szSystemPath, sizeof(device.szSystemPath));

   return;
}
Exemplo n.º 2
0
void FindWholeMedia(io_service_t service)
{
  kern_return_t kernResult;
  io_iterator_t iter;

  // Create an iterator across all parents of the service object passed in.
  kernResult = IORegistryEntryCreateIterator(service, kIOServicePlane, kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);

  if (KERN_SUCCESS != kernResult) {
    printf("IORegistryEntryCreateIterator returned %d\n", kernResult);
  } else if (NULL == iter) {
    printf("IORegistryEntryCreateIterator returned a NULL iterator\n");
  } else {
    Boolean isWholeMedia;

    // A reference on the initial service object is released in the do-while loop below,
    // so add a reference to balance
    IOObjectRetain(service);

    do {
      isWholeMedia = IsWholeMedia(service);
      IOObjectRelease(service);
    } while ((service = IOIteratorNext(iter)) && !isWholeMedia);

    IOObjectRelease(iter);
  }
}
Exemplo n.º 3
0
IOKitDevice& IOKitDevice::operator=(const IOKitDevice& clDevice_)
{
   if(this == &clDevice_)
      return *this;
      
   if (hSavedService)
   {
      IOObjectRelease(hSavedService);
      hSavedService = NULL;
   }   

   if (clDevice_.hSavedService)
   {
      IOObjectRetain(clDevice_.hSavedService);
      hSavedService = clDevice_.hSavedService;
   }

   usBusNumber = clDevice_.usBusNumber;
	usAddress = clDevice_.usAddress;
   usVid = clDevice_.usVid;
   usPid = clDevice_.usPid;
	usConfigurationNum = clDevice_.usConfigurationNum;
   ulSerialNumber = clDevice_.ulSerialNumber;
	ulLocation = clDevice_.ulLocation;
    
   STRNCPY(szProductDescription, clDevice_.szProductDescription, sizeof(szProductDescription));
   memcpy(szSerialString, clDevice_.szSerialString, sizeof(szSerialString));
   memcpy(&stDeviceDescriptor, &(clDevice_.stDeviceDescriptor), sizeof(clDevice_.stDeviceDescriptor));
   memcpy(szSystemPath, clDevice_.szSystemPath, sizeof(clDevice_.szSystemPath));

   return *this;
}
  void matched_callback(io_iterator_t iterator) {
    bool connected = false;

    while (auto service = IOIteratorNext(iterator)) {
      std::lock_guard<std::mutex> guard(connect_mutex_);

      // Use first matched service.
      if (!service_) {
        service_ = service;
        IOObjectRetain(service_);

        auto kr = IOServiceOpen(service_, mach_task_self(), kIOHIDServerConnectType, &connect_);
        if (kr != KERN_SUCCESS) {
          logger_.error("IOServiceOpen error: {1} @ {0}", __PRETTY_FUNCTION__, kr);
          connect_ = IO_OBJECT_NULL;
        }

        logger_.info("IOServiceOpen is succeeded @ {0}", __PRETTY_FUNCTION__);
        connected = true;
      }

      IOObjectRelease(service);
    }

    // We have to call callback after connect_mutex_ is unlocked.
    if (connected && connected_callback_) {
      connected_callback_(*this);
    }
  }
Exemplo n.º 5
0
const void * _IOObjectCFRetain( CFAllocatorRef          allocator  __unused, 
                                const void *            value)
{
    if (kIOReturnSuccess != IOObjectRetain((io_object_t)(uintptr_t)value))
        return NULL;
    
    return value;
}
Exemplo n.º 6
0
/*
 * Given disk2s1, look up "disk2" is IOKit and attempt to determine if
 * it is an optical device.
 */
int is_optical_media(const char *bsdname)
{
	CFMutableDictionaryRef matchingDict;
	int ret = 0;
	io_service_t service, start;
    kern_return_t   kernResult;
    io_iterator_t   iter;

	if ((matchingDict = IOBSDNameMatching(kIOMasterPortDefault, 0, bsdname))  == NULL)
        return(0);

	start = IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);
	if (IO_OBJECT_NULL == start)
		return (0);

	service = start;

	// Create an iterator across all parents of the service object passed in.
	// since only disk2 would match with ConfirmsTo, and not disk2s1, so
    // we search the parents until we find "Whole", ie, disk2.
	kernResult = IORegistryEntryCreateIterator(service,
                       kIOServicePlane,
                       kIORegistryIterateRecursively | kIORegistryIterateParents,
                       &iter);

	if (KERN_SUCCESS == kernResult) {
        Boolean isWholeMedia = false;
        IOObjectRetain(service);
        do {

			// Lookup "Whole" if we can
			if (IOObjectConformsTo(service, kIOMediaClass)) {
				CFTypeRef wholeMedia;
				wholeMedia = IORegistryEntryCreateCFProperty(service,
													 CFSTR(kIOMediaWholeKey),
                                                     kCFAllocatorDefault,
                                                     0);
				if (wholeMedia) {
					isWholeMedia = CFBooleanGetValue(wholeMedia);
					CFRelease(wholeMedia);
				}
			}

			// If we found "Whole", check the service type.
			if (isWholeMedia &&
				( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
				  (IOObjectConformsTo(service, kIODVDMediaClass)) )) {
				ret = 1; // Is optical, skip
			}

            IOObjectRelease(service);
        } while ((service = IOIteratorNext(iter)) && !isWholeMedia);
        IOObjectRelease(iter);
	}

	IOObjectRelease(start);
	return ret;
}
Exemplo n.º 7
0
IOKitDevice::IOKitDevice(const io_service_t& hService_)
:
   usBusNumber(0),
	usAddress(0),
	usConfigurationNum(0),
   ulSerialNumber(0),
	ulLocation(0),
   hSavedService(NULL)
{
   UCHAR ucSerialIndex = 0;
   
   szProductDescription[0] = '\0';
   szSerialString[0] = '\0';
	memset(&stDeviceDescriptor, 0, sizeof(stDeviceDescriptor));  //!! Can initialize in stDeviceDescriptor constructor instead
	memset(szSystemPath, 0, sizeof(szSystemPath));

   if(hService_ == 0)
      throw IOKitError::INVALID_PARAM;

   if(!IOObjectConformsTo(hService_, "IOUSBDevice"))
      throw IOKitError::INVALID_PARAM;
      
   IOObjectRetain(hService_);
   hSavedService = hService_;
   
   
   //IOKitError::Enum eReturn;
   
   
   
   //!!eReturn = IOKitDevice::GetNewDescriptor(ppstDeviceInterface, stDeviceDescriptor);
   //if(eReturn != IOKitError::SUCCESS)
   //   throw eReturn;
   
   IOKitDevice::GetDeviceString(hService_, CFSTR("USB Product Name"), (UCHAR*)szProductDescription, sizeof(szProductDescription));  //could also use "Product Name" (with a search) to get from drivers

   usVid = IOKitDevice::GetDeviceNumber(hService_, CFSTR("idVendor"));
   usPid = IOKitDevice::GetDeviceNumber(hService_, CFSTR("idProduct"));
   usConfigurationNum = IOKitDevice::GetDeviceNumber(hService_, CFSTR("bNumConfigurations"));
   ulLocation = IOKitDevice::GetDeviceNumber(hService_, CFSTR("locationID"));
   usAddress = IOKitDevice::GetDeviceNumber(hService_, CFSTR("USB Address"));
   usBusNumber = ulLocation >> 24;
   
   UCHAR bDeviceClass = IOKitDevice::GetDeviceNumber(hService_, CFSTR("bDeviceClass"));
   UCHAR bDeviceSubClass = IOKitDevice::GetDeviceNumber(hService_, CFSTR("bDeviceSubClass"));

   ulSerialNumber = IOKitDevice::GetSerialNumber(hService_, (UCHAR*) szSerialString, sizeof(szSerialString), ucSerialIndex);
   stDeviceDescriptor.iSerialNumber = (UInt8)ucSerialIndex;  //update descriptor with serial string index

   SNPRINTF(szSystemPath, sizeof(szSystemPath), "%03i-%04x-%04x-%02x-%02x", usAddress, usVid, usPid, bDeviceClass, bDeviceSubClass);
      
   //!!eReturn = CheckDeviceSanity();
   //if(eReturn != IOKitError::SUCCESS)
   //   throw eReturn;
	
   return;
}
Exemplo n.º 8
0
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort)
{
    int retval = 0;
    CFMutableDictionaryRef matchingDict;
    kern_return_t rc;
    io_iterator_t iter;
    io_service_t service;

    if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL)
        return(0);

    rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter);
    if ((rc != KERN_SUCCESS) || (!iter))
        return(0);

    service = IOIteratorNext(iter);
    IOObjectRelease(iter);
    if (!service)
        return(0);

    rc = IORegistryEntryCreateIterator(service, kIOServicePlane,
             kIORegistryIterateRecursively | kIORegistryIterateParents, &iter);
    
    if (!iter)
        return(0);

    if (rc != KERN_SUCCESS)
    {
        IOObjectRelease(iter);
        return(0);
    } /* if */

    IOObjectRetain(service);  /* add an extra object reference... */

    do
    {
        if (darwinIsWholeMedia(service))
        {
            if ( (IOObjectConformsTo(service, kIOCDMediaClass)) ||
                 (IOObjectConformsTo(service, kIODVDMediaClass)) )
            {
                retval = 1;
            } /* if */
        } /* if */
        IOObjectRelease(service);
    } while ((service = IOIteratorNext(iter)) && (!retval));
                
    IOObjectRelease(iter);
    IOObjectRelease(service);

    return(retval);
} /* darwinIsMountedDisc */
Exemplo n.º 9
0
io_registry_entry_t lookService(uint32_t segment,
                                uint32_t bus, uint32_t device, uint32_t function)
{
    kern_return_t status;
    io_iterator_t iter;
    io_service_t service;

    IOPCIAddressSpace space;
    space.bits           = 0;
    space.es.busNum      = bus;
    space.es.deviceNum   = device;
    space.es.functionNum = function;
    
    status = IOServiceGetMatchingServices(kIOMasterPortDefault, 
                                            IOServiceMatching("IOPCIDevice"), &iter);
    assert(kIOReturnSuccess == status);

    while ((service = IOIteratorNext(iter)))
    {
        CFDataRef reg;
        UInt32    bits;

        reg = IORegistryEntryCreateCFProperty(service, CFSTR("reg"), 
                    kCFAllocatorDefault, kNilOptions);
        bits = 0;

        if (reg)
        {
            if (CFDataGetTypeID() == CFGetTypeID(reg))
                bits = ((UInt32 *)CFDataGetBytePtr(reg))[0];
            CFRelease(reg);
        }
        if (bits == space.bits)
        {
            IOObjectRetain(service);
            break;
        }
    }
    IOObjectRelease(iter);

    return (service);
}
Exemplo n.º 10
0
int
MacHaptic_MaybeAddDevice( io_object_t device )
{
    IOReturn result;
    CFMutableDictionaryRef hidProperties;
    CFTypeRef refCF;
    SDL_hapticlist_item *item;

    if (numhaptics == -1) {
        return -1; /* not initialized. We'll pick these up on enumeration if we init later. */
    }

    /* Check for force feedback. */
    if (FFIsForceFeedback(device) != FF_OK) {
        return -1;
    }

    /* Make sure we don't already have it */
    for (item = SDL_hapticlist; item ; item = item->next)
    {
        if (IOObjectIsEqualTo((io_object_t) item->dev, device)) {
            /* Already added */
            return -1;
        }
    }

    item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
    if (item == NULL) {
        return SDL_SetError("Could not allocate haptic storage");
    }

    /* retain it as we are going to keep it around a while */
    IOObjectRetain(device);

    /* Set basic device data. */
    HIDGetDeviceProduct(device, item->name);
    item->dev = device;

    /* Set usage pages. */
    hidProperties = 0;
    refCF = 0;
    result = IORegistryEntryCreateCFProperties(device,
                                               &hidProperties,
                                               kCFAllocatorDefault,
                                               kNilOptions);
    if ((result == KERN_SUCCESS) && hidProperties) {
        refCF = CFDictionaryGetValue(hidProperties,
                                     CFSTR(kIOHIDPrimaryUsagePageKey));
        if (refCF) {
            if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usagePage)) {
                SDL_SetError("Haptic: Receiving device's usage page.");
            }
            refCF = CFDictionaryGetValue(hidProperties,
                                         CFSTR(kIOHIDPrimaryUsageKey));
            if (refCF) {
                if (!CFNumberGetValue(refCF, kCFNumberLongType, &item->usage)) {
                    SDL_SetError("Haptic: Receiving device's usage.");
                }
            }
        }
        CFRelease(hidProperties);
    }

    if (SDL_hapticlist_tail == NULL) {
        SDL_hapticlist = SDL_hapticlist_tail = item;
    } else {
        SDL_hapticlist_tail->next = item;
        SDL_hapticlist_tail = item;
    }

    /* Device has been added. */
    ++numhaptics;

    return numhaptics;
}
Exemplo n.º 11
0
/**
 * Guess the media that a volume/partition is on
 */
MythMediaType FindMediaType(io_service_t service)
{
    kern_return_t  kernResult;
    io_iterator_t  iter;
    MythMediaType  mediaType = MEDIATYPE_UNKNOWN;
    QString        msg = QString("FindMediaType() - ");
    bool           isWholeMedia = false;

    // Create an iterator across all parents of the service object passed in.
    kernResult = IORegistryEntryCreateIterator(service,
                                               kIOServicePlane,
                                               kIORegistryIterateRecursively
                                               | kIORegistryIterateParents,
                                               &iter);

    if (KERN_SUCCESS != kernResult)
        LOG(VB_GENERAL, LOG_CRIT, msg +
            QString("IORegistryEntryCreateIterator returned %1")
                .arg(kernResult));
    else if (!iter)
        LOG(VB_GENERAL, LOG_CRIT, msg +
            "IORegistryEntryCreateIterator returned NULL iterator");
    else
    {
        // A reference on the initial service object is released in
        // the do-while loop below, so add a reference to balance
        IOObjectRetain(service);
        
        do
        {
            isWholeMedia = false;
            if (IOObjectConformsTo(service, kIOMediaClass))
            {
                CFTypeRef wholeMedia;

                wholeMedia = IORegistryEntryCreateCFProperty
                             (service, CFSTR(kIOMediaWholeKey), 
                              kCFAllocatorDefault, 0);

                if (!wholeMedia)
                    LOG(VB_GENERAL, LOG_ALERT, msg +
                        "Could not retrieve Whole property");
                else
                {
                    isWholeMedia = CFBooleanGetValue((CFBooleanRef)wholeMedia);
                    CFRelease(wholeMedia);
                }
            }

            if (isWholeMedia)
            {
                if (IOObjectConformsTo(service, kIODVDMediaClass))
                    mediaType = MEDIATYPE_DVD;
                else if (IOObjectConformsTo(service, kIOCDMediaClass))
                    mediaType = MEDIATYPE_AUDIO;
            }

            IOObjectRelease(service);

        } while ((service = IOIteratorNext(iter))
                 && (mediaType == MEDIATYPE_UNKNOWN));

        IOObjectRelease(iter);
    }
    return mediaType;
}