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); } }
static io_service_t GetUsbDevice(io_service_t service) { IOReturn status; io_iterator_t iterator = 0; io_service_t device = 0; if (!service) { return device; } status = IORegistryEntryCreateIterator(service, kIOServicePlane, (kIORegistryIterateParents | kIORegistryIterateRecursively), &iterator); if (status == kIOReturnSuccess) { io_service_t currentService; while ((currentService = IOIteratorNext(iterator)) && device == 0) { io_name_t serviceName; status = IORegistryEntryGetNameInPlane(currentService, kIOServicePlane, serviceName); if (status == kIOReturnSuccess && IOObjectConformsTo(currentService, kIOUSBDeviceClassName)) { device = currentService; } else { // Release the service object which is no longer needed (void) IOObjectRelease(currentService); } } // Release the iterator (void) IOObjectRelease(iterator); } return device; }
/* * 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; }
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 */
void search_wifi_mac_callback(void** context, io_iterator_t iterator) { unsigned char macaddrBytes[6]; io_iterator_t iterator2=0; io_object_t obj2=0; io_name_t name; CFDataRef t1=0; io_object_t next; while ((next = IOIteratorNext(iterator)) != 0) { if (!IORegistryEntryCreateIterator(next, "IOService", 3, &iterator2)) { while((obj2 = IOIteratorNext(iterator2)) != 0) { if (!IORegistryEntryGetName(obj2,name)) { if (!strcmp(name, "sdio") || !strcmp(name, "wlan")) { if((t1 = IORegistryEntryCreateCFProperty(obj2, CFSTR("local-mac-address"), kCFAllocatorDefault, 0)) != 0) { CFDataGetBytes(t1, CFRangeMake(0,6), macaddrBytes); *context = (void*) CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%02x:%02x:%02x:%02x:%02x:%02x"), macaddrBytes[0], macaddrBytes[1], macaddrBytes[2], macaddrBytes[3], macaddrBytes[4], macaddrBytes[5]); CFRelease(t1); } } } } IOObjectRelease(iterator2); } IOObjectRelease(next); if (*context != NULL) break; } }
static void DeviceAdded(void *refCon, io_iterator_t iter1) { int oldMouseCount = g_MouseCount; io_service_t service; while ((service = IOIteratorNext(iter1))) { io_iterator_t iter2; kern_return_t kr = IORegistryEntryCreateIterator(service, kIOServicePlane, kIORegistryIterateRecursively, &iter2); if (KERN_SUCCESS != kr) { DEBUG_LOG("IORegistryEntryCreateIterator returned 0x%08x\n", kr); continue; } io_service_t temp; while ((temp = IOIteratorNext(iter2))) { io_name_t name; kr = IORegistryEntryGetName(temp, name); if (KERN_SUCCESS != kr) continue; if (0 == strcmp("IOHIDPointing", name) || 0 == strcmp("IOHIDPointingDevice", name)) { NotificationData* pData = (NotificationData*)malloc(sizeof(*pData)); if (pData == NULL) continue; kr = IOServiceAddInterestNotification(g_NotifyPort, temp, kIOGeneralInterest, DeviceNotification, pData, &pData->notification); if (KERN_SUCCESS != kr) { DEBUG_LOG("IOServiceAddInterestNotification returned 0x%08x\n", kr); continue; } ++g_MouseCount; DEBUG_LOG("mouse count is now: %d\n", g_MouseCount); } IOObjectRelease(temp); } IOObjectRelease(iter2); IOObjectRelease(service); } if (oldMouseCount != g_MouseCount) SendMouseCount(g_MouseCount); }
/** * 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; }
FskErr FskFSVolumeGetDeviceInfo(UInt32 volumeID, char **vendor, char **product, char **revision, char **vendorSpecific) { FskErr err = kFskErrNone; mach_port_t masterPort = MACH_PORT_NULL; io_service_t service; io_iterator_t iterator = 0; io_object_t obj; char bsdNode[256]; CFMutableDictionaryRef properties = NULL; Boolean isFound = false; char *p; if (vendor) *vendor = NULL; if (product) *product = NULL; if (revision) *revision = NULL; if (vendorSpecific) *vendorSpecific = NULL; if (volumeID >= (UInt32)gNumStatFS) return kFskErrInvalidParameter; p = gStatFS[volumeID].f_mntfromname; if (FskStrStr(p, "/dev/") == p) p += 5; FskStrCopy(bsdNode, p); err = IOMasterPort(MACH_PORT_NULL, &masterPort); if (err != kIOReturnSuccess) { BAIL(kFskErrUnknown); } err = IOServiceGetMatchingServices(masterPort, IOBSDNameMatching(masterPort, 0, bsdNode), &iterator); if (err != kIOReturnSuccess) { BAIL(kFskErrUnknown); } // There is a 1:1 map from bsd node to IOMedia, so just get the service from the iterator. obj = IOIteratorNext(iterator); IOObjectRelease(iterator); if (obj == 0) goto bail; // Create an iterator across all parents of the service object passed in. err = IORegistryEntryCreateIterator(obj, kIOServicePlane, kIORegistryIterateRecursively | kIORegistryIterateParents, &iterator); IOObjectRelease(obj); if (KERN_SUCCESS != err) { BAIL(kFskErrUnknown); } while ((service = IOIteratorNext(iterator)) != 0) { properties = NULL; err = IORegistryEntryCreateCFProperties(service, &properties, kCFAllocatorDefault, kNilOptions); if (err == noErr) { if (find_and_set_info(properties, CFSTR("Vendor Identification"), vendor)) { find_and_set_info(properties, CFSTR("Product Identification"), product); find_and_set_info(properties, CFSTR("Product Revision Level"), revision); isFound = true; goto NextService; } if (find_and_set_info(properties, CFSTR("device model"), product)) { find_and_set_info(properties, CFSTR("device revision"), revision); isFound = true; goto NextService; } } NextService: if (properties) CFRelease(properties); IOObjectRelease(service); if (isFound) break; } // while bail: if (iterator) IOObjectRelease(iterator); if (masterPort) mach_port_deallocate(mach_task_self(), masterPort); return err; }
/* Return an icom error */ int usb_get_paths( icompaths *p ) { kern_return_t kstat; CFMutableDictionaryRef sdict; /* USB Device dictionary */ io_iterator_t mit; /* Matching itterator */ int vid, pid; struct usb_idevice *usbd = NULL; a1logd(p->log, 6, "usb_get_paths: about to look through devices:\n"); /* Get dictionary of USB devices */ if ((sdict = IOServiceMatching(kIOUSBDeviceClassName)) == NULL) { a1loge(p->log, ICOM_SYS, "usb_get_paths() IOServiceMatching returned a NULL dictionary\n"); return ICOM_SYS; } /* Init itterator to find matching types. Consumes sdict reference */ if ((kstat = IOServiceGetMatchingServices(kIOMasterPortDefault, sdict, &mit)) != KERN_SUCCESS) { a1loge(p->log, ICOM_SYS, "usb_get_paths() IOServiceGetMatchingServices returned %d\n", kstat); return ICOM_SYS; } /* Find all the matching USB devices */ for (;;) { io_object_t ioob; /* USB object found */ io_iterator_t it1; /* Child level 1 */ CFMutableDictionaryRef usbprops = 0; /* USB Device properties */ CFNumberRef vref, pref; /* USB Vendor and Product ID propeties */ CFNumberRef nconfref; /* No configurations */ CFNumberRef nepref, lidpref; /* No ep's, Location ID properties */ unsigned int vid = 0, pid = 0, nep, tnep, nconfig = 0, lid = 0; instType itype; if ((ioob = IOIteratorNext(mit)) == 0) break; /* Get the two properies we need. */ if ((vref = IORegistryEntryCreateCFProperty(ioob, CFSTR(kUSBVendorID), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(vref, kCFNumberIntType, &vid); CFRelease(vref); } if ((pref = IORegistryEntryCreateCFProperty(ioob, CFSTR(kUSBProductID), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(pref, kCFNumberIntType, &pid); CFRelease(pref); } if ((nconfref = IORegistryEntryCreateCFProperty(ioob, CFSTR("bNumConfigurations"), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(nconfref, kCFNumberIntType, &nconfig); CFRelease(nconfref); } if ((lidpref = IORegistryEntryCreateCFProperty(ioob, CFSTR("locationID"), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(lidpref, kCFNumberIntType, &lid); CFRelease(lidpref); } a1logd(p->log, 6, "usb_get_paths: checking vid 0x%04x, pid 0x%04x, lid 0x%x\n",vid,pid,lid); /* Do a preliminary match */ if ((itype = inst_usb_match(vid, pid, 0)) == instUnknown) { a1logd(p->log, 6 , "usb_get_paths: 0x%04x 0x%04x not reconized\n",vid,pid); IOObjectRelease(ioob); /* Release found object */ continue; } /* Allocate an idevice so that we can fill in the end point information */ if ((usbd = (struct usb_idevice *) calloc(sizeof(struct usb_idevice), 1)) == NULL) { a1loge(p->log, ICOM_SYS, "usb_get_paths: calloc failed!\n"); return ICOM_SYS; } usbd->nconfig = nconfig; usbd->config = 1; /* We are only interested in config 1 */ /* Go through all the Interfaces, adding up the number of end points */ tnep = 0; if ((kstat = IORegistryEntryCreateIterator(ioob, kIOServicePlane, kIORegistryIterateRecursively, &it1)) != KERN_SUCCESS) { IOObjectRelease(ioob); IOObjectRelease(mit); a1loge(p->log, kstat, "usb_get_paths: IORegistryEntryCreateIterator() with %d\n",kstat); return ICOM_SYS; } usbd->nifce = 0; for (;;) { io_object_t ch1; /* Child object 1 */ if ((ch1 = IOIteratorNext(it1)) == 0) break; if (IOObjectConformsTo(ch1, "IOUSBInterface")) { unsigned int config = 0; /* Get the configuration number */ if ((nconfref = IORegistryEntryCreateCFProperty(ch1, CFSTR(kUSBNumEndpoints), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(nconfref, kCFNumberIntType, &config); CFRelease(nconfref); } if (config != 1) continue; usbd->nifce++; /* Get the number of end points */ if ((nepref = IORegistryEntryCreateCFProperty(ch1, CFSTR(kUSBNumEndpoints), kCFAllocatorDefault,kNilOptions)) != 0) { CFNumberGetValue(nepref, kCFNumberIntType, &nep); CFRelease(nepref); tnep += nep; } IOObjectRelease(ch1); } } IOObjectRelease(it1); if ((itype = inst_usb_match(vid, pid, tnep)) != instUnknown) { int i; char pname[400]; int rv; /* If this device is HID, it will have already added to the paths list, */ /* so check for this and skip this device if it is already there. */ for (i = 0; i < p->npaths; i++) { if (p->paths[i]->vid == vid && p->paths[i]->pid == pid && p->paths[i]->hidd != NULL && p->paths[i]->hidd->lid == lid) { a1logd(p->log, 1, "usb_get_paths: Ignoring device because it is already in list as HID\n"); break; } } if (i < p->npaths) { IOObjectRelease(ioob); /* Release found object */ free(usbd); } else { a1logd(p->log, 1, "usb_get_paths: found instrument vid 0x%04x, pid 0x%04x\n",vid,pid); usbd->lid = lid; usbd->ioob = ioob; /* Create a path/identification */ sprintf(pname,"usb%d: (%s)", lid >> 20, inst_name(itype)); /* Add the path and ep info to the list */ if ((rv = p->add_usb(p, pname, vid, pid, tnep, usbd, itype)) != ICOM_OK) { IOObjectRelease(ioob); /* Release found object */ free(usbd); IOObjectRelease(mit); /* Release the itterator */ return rv; } } } else {