From cc82ea14d10dd73cf7fb9c7eb17d564705d8e9c3 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Mon, 3 Nov 2014 15:24:45 +0100 Subject: [PATCH] Trigger networkChanged notification: do not trigger if SSID did not change --- Classes/LinphoneManager.h | 4 ++-- Classes/LinphoneManager.m | 43 +++++++++++++++++++++++++++++++++------ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Classes/LinphoneManager.h b/Classes/LinphoneManager.h index ab76faeec..890e3763e 100644 --- a/Classes/LinphoneManager.h +++ b/Classes/LinphoneManager.h @@ -106,7 +106,7 @@ typedef struct _LinphoneManagerSounds { @interface LinphoneManager : NSObject { @protected SCNetworkReachabilityRef proxyReachability; - + @private NSTimer* mIterateTimer; NSMutableArray* pendindCallIdFromRemoteNotif; @@ -132,7 +132,6 @@ typedef struct _LinphoneManagerSounds { + (NSString *)getUserAgent; + (int)unreadMessageCount; - - (void)resetLinphoneCore; - (void)startLibLinphone; - (void)destroyLibLinphone; @@ -186,6 +185,7 @@ typedef struct _LinphoneManagerSounds { @property (readonly) NetworkType network; @property (readonly) const char* frontCamId; @property (readonly) const char* backCamId; +@property (assign) NSString* SSID; @property (readonly) sqlite3* database; @property (nonatomic, retain) NSData *pushNotificationToken; @property (readonly) LinphoneManagerSounds sounds; diff --git a/Classes/LinphoneManager.m b/Classes/LinphoneManager.m index 39714f6ee..c23caa84a 100644 --- a/Classes/LinphoneManager.m +++ b/Classes/LinphoneManager.m @@ -27,6 +27,7 @@ #import #import #import +#import #import "LinphoneManager.h" #import "LinphoneCoreSettingsStore.h" @@ -974,6 +975,21 @@ static void linphone_iphone_is_composing_received(LinphoneCore *lc, LinphoneChat }); } ++ (NSString*)getCurrentWifiSSID { +#if TARGET_IPHONE_SIMULATOR + return @"Sim_err_SSID_NotSupported"; +#else + NSString *data = nil; + CFDictionaryRef dict = CNCopyCurrentNetworkInfo((CFStringRef)@"en0"); + if(dict) { + [LinphoneLogger log:LinphoneLoggerDebug format:@"AP Wifi: %@", dict]; + data = [NSString stringWithString:(NSString*) CFDictionaryGetValue(dict, @"SSID")]; + CFRelease(dict); + } + return data; +#endif +} + static void showNetworkFlags(SCNetworkReachabilityFlags flags){ [LinphoneLogger logc:LinphoneLoggerLog format:"Network connection flags:"]; if (flags==0) [LinphoneLogger logc:LinphoneLoggerLog format:"no flags."]; @@ -998,6 +1014,14 @@ static void showNetworkFlags(SCNetworkReachabilityFlags flags){ static void networkReachabilityNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { LinphoneManager *mgr = [LinphoneManager instance]; SCNetworkReachabilityFlags flags; + + // for an unknown reason, we are receiving multiple time the notification, so + // we will skip each time the SSID did not change + NSString *newSSID = [LinphoneManager getCurrentWifiSSID]; + if ([newSSID compare:mgr.SSID] == NSOrderedSame) return; + + mgr.SSID = [newSSID retain]; + if (SCNetworkReachabilityGetFlags([mgr getProxyReachability], &flags)) { networkReachabilityCallBack([mgr getProxyReachability],flags,nil); } @@ -1084,13 +1108,20 @@ void networkReachabilityCallBack(SCNetworkReachabilityRef target, SCNetworkReach } // This notification is used to detect SSID change (switch of Wifi network). The ReachabilityCallback is - // not triggered when switching between 2 private Wifi... + // not triggered when switching between 2 private Wifi... + // Since we cannot be sure we were already observer, remove ourself each time... to be improved + _SSID = [[LinphoneManager getCurrentWifiSSID] retain]; + CFNotificationCenterRemoveObserver( + CFNotificationCenterGetDarwinNotifyCenter(), + self, + CFSTR("com.apple.system.config.network_change"), + NULL); CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), - NULL, - networkReachabilityNotification, - CFSTR("com.apple.system.config.network_change"), - NULL, - CFNotificationSuspensionBehaviorDeliverImmediately); + self, + networkReachabilityNotification, + CFSTR("com.apple.system.config.network_change"), + NULL, + CFNotificationSuspensionBehaviorDeliverImmediately); proxyReachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress);