static void PrintConnectionSpeed(const void *key,
const void *value,
void *context)
// A CFDictionaryApplyFunction callback. The key is the SCF
// dynamic store key for a modem network service entity. The
// value is the entity dictionary. This extracts the connection
// speed property from that entity and prints it.
{
CFStringRef dynStoreKey;
CFDictionaryRef entityDict;
CFNumberRef connectionSpeed;
#pragma unused(context)
dynStoreKey = (CFStringRef) key;
entityDict = (CFDictionaryRef) value;
assert( CFGetTypeID(dynStoreKey) == CFStringGetTypeID() );
assert( CFGetTypeID(entityDict) == CFDictionaryGetTypeID() );
connectionSpeed = (CFNumberRef) CFDictionaryGetValue(entityDict,
kSCPropNetModemConnectSpeed);
if (connectionSpeed == NULL) {
// A disconnected modem entity has no connection speed
// property. In this example, we simply ignore it.
} else {
CFStringRef str;
assert( CFGetTypeID(connectionSpeed) == CFNumberGetTypeID() );
str = CFStringCreateWithFormat(NULL, NULL,
CFSTR("%@ = %@"),
dynStoreKey,
connectionSpeed);
assert(str != NULL);
CFShow(str);
CFQRelease(str);
}
}
int main (int argc, const char * argv[])
{
OSStatus err;
SCDynamicStoreRef store;
CFStringRef pattern;
CFArrayRef patterns;
CFDictionaryRef dictOfModemEntities;
dictOfModemEntities = NULL;
pattern = NULL;
patterns = NULL;
// Connect to the dynamic store.
store = SCDynamicStoreCreate(NULL, CFSTR("ConnectionSpeedTest"),
NULL, NULL);
err = MoreSCError(store);
// Set pattern to "State:/Network/Service/[^/]+/Modem" and
// then put it into a CFArray.
if (err == noErr) {
pattern = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL,
kSCDynamicStoreDomainState,
kSCCompAnyRegex,
kSCEntNetModem);
err = MoreSCError(pattern);
}
if (err == noErr) {
patterns = CFArrayCreate(NULL, (const void **) &pattern, 1,
&kCFTypeArrayCallBacks);
err = CFQError(patterns);
}
// Get all of the modem network service entities from the
// dynamic store and print the results.
if (err == noErr) {
dictOfModemEntities = SCDynamicStoreCopyMultiple(store,
NULL, patterns);
err = MoreSCError(dictOfModemEntities);
}
if (err == noErr) {
CFDictionaryApplyFunction(dictOfModemEntities,
PrintConnectionSpeed, NULL);
}
// Clean up.
CFQRelease(store);
CFQRelease(pattern);
CFQRelease(patterns);
CFQRelease(dictOfModemEntities);
return (err != noErr);
}
|