For all physical devices, adding a device entry to the Name Registry is handled by the device's expert. Device drivers normally do not need to add their devices to the Registry.
Adding a new device to the system consists of entering a new name entry in the Registry and setting the appropriate property values. The example shown in Listing 10-12 adds a new name entry to the Registry with a single property.
Listing 10-12 Adding a name entry to the Name Registry
#include <NameRegistry.h>
OSStatus JoePro_AddName(
const RegCStrPathName *name,
const RegPropertyName *prop,
const void *val,
const RegPropertyValueSize len
)
{
OSStatus err = noErr;
RegEntryID where, new_entry;
err = JoePro_FigureOutWhere(&where);
if (err == noErr) {
err = JoePro_EnterName(&where, name, &new_entry);
RegistryEntryIDDispose(&where);
}
if (err == noErr) {
err = JoePro_AddProperties(&new_entry, prop, val, len);
RegistryEntryIDDispose(&new_entry);
}
return err;
}
OSStatus
JoePro_FigureOutWhere(RegEntryID *where)
{
OSErr err = noErr;
RegEntryIter cookie;
Boolean done = FALSE;
/*
* We want to search all the names, which is
* the default, so we just need to continue.
*/
RegEntryIterationOp op = kRegIterContinue;
/*
* For this example, the existence of the
* "Joe Pro Root" property is used to find
* out where to put the "Joe Pro" devices.
* Initialization code will need to have
* created this entry.
*/
RegPropertyNameBuf name;
RegPropertyValue val = NULL;
RegPropertyValueSize siz = 0;
strncpy(name, "Joe Pro Root", sizeof(name));
/*
* Figure out where to put the driver.
* By convention, there is one "Joe Pro Root"
* so we don't need to loop.
*/
err = RegistryEntryIterateCreate(&cookie);
if (err == noErr) {
err = RegistyEntrySearch(&cookie, op, &where, &done,
name, val, siz);
}
RegistryEntryIterateDispose(&cookie);
/*
* Check if we completed the search without
* finding the "Joe Pro Root".
*/
assert(err != noErr || !done);
return err;
}
OSStatus JoePro_EnterName(
const RegEntryID *where,
const RegCStrPathName *name,
RegEntryID *entry
)
{
/*
* Assumption: This call will return an error
* if the name entry is already in the Registry.
*/
return RegistryCStrEntryCreate(where, name, entry);
}
OSStatus
JoePro_AddProperties(
const RegEntryID *entry,
const RegPropertyName *prop,
const void *val,
const RegPropertyValueSize siz
)
{
return RegistryPropertyCreate(entry, prop, val, siz);
}
Since all name entries in the registry are connected to at least one other entry, either an existing name entry must be provided when creating a new entry or it will be assumed that the path is specified relative to the root entry.
The creator of a name entry must determine where in the tree it should appear. This is typically determined by convention.