Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Switching Airport of in code using SCF



Hi
Thanks Allan that seemed to get the setting applied in the system config but Airport remains up after I run the tool and only comes down after a reboot. Is there any way I can force the interface down? I tried SCNetworkInterfaceForceConfigurationRefresh at the end of the code but that didn't work.

Thanks again

Jonathan

This is what I've done:

gPrefsRef = SCPreferencesCreate(nil, CFSTR("InterfaceConfig2"), nil);
if (gPrefsRef == nil
errorAndExit("InterfaceConfig failure: SCPreferencesCreate failed");

if (!SCPreferencesLock(gPrefsRef, YES)) 
errorAndExit("InterfaceConfig failure: SCPreferencesLock could not get a lock");



NSEnumerator *e = [(NSArray *)SCNetworkSetCopyServices(SCNetworkSetCopyCurrent(gPrefsRef)) objectEnumerator];
SCNetworkServiceRef service;

while (service = (SCNetworkServiceRef)[e nextObject]) {
interfaceRef = SCNetworkServiceGetInterface(service);
if (isInterfaceOfType(interfaceRef, kSCNetworkInterfaceTypeIEEE80211)) {
CFDictionaryRef dict = (CFMutableDictionaryRef)[NSMutableDictionary dictionaryWithObjectsAndKeys:(NSString *)kSCValNetAirPortJoinModeAutomatic,
(NSString *)kSCPropNetAirPortJoinMode,
[NSNumber numberWithInt:0],
(NSString *)kSCPropNetAirPortPowerEnabled, nil];
if (SCNetworkInterfaceSetConfiguration(interfaceRef, dict) == NO) {
syslog(LOG_LOCAL7 | LOG_ALERT, "InterfaceConfig failure: Could not set Airport interface settings");
}
}
}


if (SCPreferencesCommitChanges(gPrefsRef) == NO)
errorAndExit("InterfaceConfig failure: SCPreferencesCommitChanges failed");
if (SCPreferencesApplyChanges(gPrefsRef) == NO)
errorAndExit("InterfaceConfig failure: SCPreferencesApplyChanges failed");

SCPreferencesUnlock(gPrefsRef);
CFRelease(gPrefsRef);




On 18 Feb 2008, at 14:25, Allan Nathanson wrote:

The SCNetworkInterface configuration is stored either per-set or per-service.  What this means is that you need to have an SCNetworkInterface that was derived from a service.  Instead of iterating over the interfaces from SCNetworkInterfaceCopyAll you'll want to call SCNetworkSetCopyCurrent to copy the current set, call SCNetworkSetCopyServices to copy the services in that set, find the service with the desired interface (SCNetworkServiceGetInterface, SCNetworkInterfaceGetInterfaceType), and than use that interface to update the configuration.

Note: you should have had a syslog() message telling you that the SCNetworkInterfaceSetConfiguration call returned an error.

- Allan

p.s. in your example code, there's no need for the SCDynamicStore calls.



On Feb 18, 2008, at 9:05 AM, Jonathan Guy wrote:

Hi
All I'm attempting to do is turn of Airport using the System Configuration Framework

I guessed the clue was in the key kSCPropNetAirPortPowerEnabled so I whipped up some code but it doesn't appear to work. If anyone can shed some light on this I'd be very grateful.

This is the code (there may be a few release functions missing). The program actually flows without any problems and returns zero but nothing in the SystemConfig changes.

static SCDynamicStoreRef storeRef = nil;
static SCPreferencesRef gPrefsRef = nil;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
SCNetworkInterfaceRef interfaceRef;
CFArrayRef interfaces = nil;
CFIndex interfaceCount;
CFIndex interfaceIndex;

storeRef = SCDynamicStoreCreate(nil, CFSTR("interfaceConfig"), nil, nil);
if (storeRef == nil
errorAndExit("InterfaceConfig failure: SCDynamicStoreCreate failed");

gPrefsRef = SCPreferencesCreate(nil, CFSTR("InterfaceConfig"), nil);
if (gPrefsRef == nil
errorAndExit("InterfaceConfig failure: SCPreferencesCreate failed");

if (!SCPreferencesLock(gPrefsRef, YES)) 
errorAndExit("InterfaceConfig failure: SCPreferencesLock could not get a lock");

interfaces = SCNetworkInterfaceCopyAll();
if (interfaces == nil)
errorAndExit("InterfaceConfig failure: SCNetworkInterfaceCopyAll return nil interfaces");

interfaceCount = CFArrayGetCount(interfaces);

for (interfaceIndex = 0; interfaceIndex < interfaceCount; interfaceIndex++) {
interfaceRef = CFArrayGetValueAtIndex(interfaces, interfaceIndex);

if (isInterfaceOfType(interfaceRef, kSCNetworkInterfaceTypeIEEE80211)) {
// Airport interface needs to be powered off
CFDictionaryRef dict = (CFMutableDictionaryRef)[NSMutableDictionary dictionaryWithObjectsAndKeys:(NSString *)kSCValNetAirPortJoinModeAutomatic,
(NSString *)kSCPropNetAirPortJoinMode,
[NSNumber numberWithInt:0],
(NSString *)kSCPropNetAirPortPowerEnabled, nil];
if (SCNetworkInterfaceSetConfiguration(interfaceRef, dict) == NO) {
syslog(LOG_LOCAL7 | LOG_ALERT, "InterfaceConfig failure: Could not set Airport interface settings");
}
}
}

if (SCPreferencesCommitChanges(gPrefsRef) == NO)
errorAndExit("InterfaceConfig failure: SCPreferencesCommitChanges failed");
if (SCPreferencesApplyChanges(gPrefsRef) == NO)
errorAndExit("InterfaceConfig failure: SCPreferencesApplyChanges failed");

SCPreferencesUnlock(gPrefsRef);
CFRelease(gPrefsRef);
CFRelease(storeRef);

[pool release];

    return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/macnetworkprog/email@hidden

This email sent to email@hidden

References: 
 >Switching Airport of in code using SCF (From: Jonathan Guy <email@hidden>)
 >Re: Switching Airport of in code using SCF (From: Allan Nathanson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.