| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies). |
| 4 |
** Contact: http://www.qt-project.org/legal |
| 5 |
** |
| 6 |
** This file is part of the QtSystems module of the Qt Toolkit. |
| 7 |
** |
| 8 |
** $QT_BEGIN_LICENSE:LGPL$ |
| 9 |
** Commercial License Usage |
| 10 |
** Licensees holding valid commercial Qt licenses may use this file in |
| 11 |
** accordance with the commercial license agreement provided with the |
| 12 |
** Software or, alternatively, in accordance with the terms contained in |
| 13 |
** a written agreement between you and The Qt Company. For licensing terms |
| 14 |
** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 |
** information use the contact form at https://www.qt.io/contact-us. |
| 16 |
** |
| 17 |
** GNU Lesser General Public License Usage |
| 18 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 |
** General Public License version 3 as published by the Free Software |
| 20 |
** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 |
** packaging of this file. Please review the following information to |
| 22 |
** ensure the GNU Lesser General Public License version 3 requirements |
| 23 |
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 |
** |
| 25 |
** GNU General Public License Usage |
| 26 |
** Alternatively, this file may be used under the terms of the GNU |
| 27 |
** General Public License version 2.0 or (at your option) the GNU General |
| 28 |
** Public license version 3 or any later version approved by the KDE Free |
| 29 |
** Qt Foundation. The licenses are as published by the Free Software |
| 30 |
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 |
** included in the packaging of this file. Please review the following |
| 32 |
** information to ensure the GNU General Public License requirements will |
| 33 |
** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 |
** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 |
** |
| 36 |
** $QT_END_LICENSE$ |
| 37 |
** |
| 38 |
****************************************************************************/ |
| 39 |
|
| 40 |
#include "qnetworkinfo_mac_p.h" |
| 41 |
|
| 42 |
#include <QtCore/private/qcore_mac_p.h> |
| 43 |
#include <QStringList> |
| 44 |
#include <QTimer> |
| 45 |
#include <QHostInfo> |
| 46 |
#include <QtCore/qmetaobject.h> |
| 47 |
|
| 48 |
|
| 49 |
#include <CoreWLAN/CoreWLAN.h> |
| 50 |
#include <SystemConfiguration/SystemConfiguration.h> |
| 51 |
#include <IOBluetooth/IOBluetooth.h> |
| 52 |
|
| 53 |
#include <net/if.h> |
| 54 |
#include <net/if_types.h> |
| 55 |
#include <net/if_media.h> |
| 56 |
#include <sys/ioctl.h> |
| 57 |
#include <sys/socket.h> |
| 58 |
|
| 59 |
inline QString nsstringToQString(const NSString *nsstr) |
| 60 |
{ return QLatin1String([nsstr UTF8String]);} |
| 61 |
|
| 62 |
inline QStringList nsarrayToQStringList(void *nsarray) |
| 63 |
{ |
| 64 |
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 65 |
QStringList result; |
| 66 |
NSArray *array = static_cast<NSArray *>(nsarray); |
| 67 |
for (NSUInteger i=0; i<[array count]; ++i) |
| 68 |
result << nsstringToQString([array objectAtIndex:i]); |
| 69 |
[autoreleasepool release]; |
| 70 |
return result; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
@interface QtMNSListener : NSObject |
| 75 |
{ |
| 76 |
NSNotificationCenter *center; |
| 77 |
CWInterface * currentInterface; |
| 78 |
} |
| 79 |
- (void)notificationHandler:(NSNotification *)notification; |
| 80 |
- (void)remove; |
| 81 |
@end |
| 82 |
|
| 83 |
@implementation QtMNSListener |
| 84 |
- (id) init |
| 85 |
{ |
| 86 |
[super init]; |
| 87 |
center = [NSNotificationCenter defaultCenter]; |
| 88 |
currentInterface = [CWInterface interfaceWithName:nil]; |
| 89 |
|
| 90 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWModeDidChangeNotification object:nil]; |
| 91 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWSSIDDidChangeNotification object:nil]; |
| 92 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWBSSIDDidChangeNotification object:nil]; |
| 93 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWCountryCodeDidChangeNotification object:nil]; |
| 94 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWLinkDidChangeNotification object:nil]; |
| 95 |
[center addObserver:self selector:@selector(notificationHandler:) name:kCWPowerDidChangeNotification object:nil]; |
| 96 |
|
| 97 |
return self; |
| 98 |
} |
| 99 |
|
| 100 |
-(void)dealloc |
| 101 |
{ |
| 102 |
[center release]; |
| 103 |
[super dealloc]; |
| 104 |
} |
| 105 |
|
| 106 |
-(void)remove |
| 107 |
{ |
| 108 |
[center removeObserver:self]; |
| 109 |
} |
| 110 |
|
| 111 |
- (void)notificationHandler:(NSNotification *)notification |
| 112 |
{ |
| 113 |
QNetworkInfoPrivate::instance()->wifiNetworkChanged(QCFString::toQString([notification name]), QCFString::toQString([[notification object]name])); |
| 114 |
} |
| 115 |
@end |
| 116 |
@interface RemoteDeviceRSSIHostControllerDelegate : NSObject |
| 117 |
{ |
| 118 |
} |
| 119 |
// See IOBluetoothHostControllerDelegate |
| 120 |
- (void)readRSSIForDeviceComplete:(id)controller device:(IOBluetoothDevice*)device info:(BluetoothHCIRSSIInfo*)info error:(IOReturn)error; |
| 121 |
@end |
| 122 |
|
| 123 |
@implementation RemoteDeviceRSSIHostControllerDelegate |
| 124 |
- (id) init |
| 125 |
{ |
| 126 |
[super init]; |
| 127 |
return self; |
| 128 |
} |
| 129 |
|
| 130 |
- (void)readRSSIForDeviceComplete:(id)controller device:(IOBluetoothDevice*)device info:(BluetoothHCIRSSIInfo*)info error:(IOReturn)error |
| 131 |
{ |
| 132 |
Q_UNUSED(controller); |
| 133 |
Q_UNUSED(device); |
| 134 |
|
| 135 |
if ((error != kIOReturnSuccess) || (info == NULL)) { |
| 136 |
qDebug() << "ERROR: readRSSIForDeviceComplete return error"; |
| 137 |
} else if (info->handle == kBluetoothConnectionHandleNone) { |
| 138 |
qDebug() << "ERROR: readRSSIForDeviceComplete no connection"; |
| 139 |
} else { |
| 140 |
} |
| 141 |
} |
| 142 |
@end |
| 143 |
|
| 144 |
NSObject* delegate; |
| 145 |
@interface QtBtConnectListener : NSObject |
| 146 |
{ |
| 147 |
} |
| 148 |
@end |
| 149 |
|
| 150 |
QNetworkInfoPrivate *QNetworkInfoPrivate::self = 0; |
| 151 |
|
| 152 |
#ifndef MAC_SDK_10_5 |
| 153 |
QtMNSListener *listener; |
| 154 |
#endif |
| 155 |
|
| 156 |
QRunLoopThread::QRunLoopThread(QObject *parent) |
| 157 |
:QObject(parent) |
| 158 |
{ |
| 159 |
moveToThread(&t); |
| 160 |
t.start(); |
| 161 |
} |
| 162 |
|
| 163 |
QRunLoopThread::~QRunLoopThread() |
| 164 |
{ |
| 165 |
} |
| 166 |
|
| 167 |
void QRunLoopThread::stop() |
| 168 |
{ |
| 169 |
QMutexLocker locker(&mutex); |
| 170 |
CFRunLoopStop(CFRunLoopGetCurrent()); |
| 171 |
keepRunning = false; |
| 172 |
//#ifndef MAC_SDK_10_5 |
| 173 |
// [listener release]; |
| 174 |
// [delegate release]; |
| 175 |
//#endif |
| 176 |
t.quit(); |
| 177 |
t.wait(); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
void QRunLoopThread::doWork() |
| 182 |
{ |
| 183 |
#ifndef MAC_SDK_10_5 |
| 184 |
if (QThread::currentThread() != &t) { |
| 185 |
QMetaObject::invokeMethod(this, "doWork", |
| 186 |
Qt::QueuedConnection); |
| 187 |
} else { |
| 188 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 189 |
startNetworkChangeLoop(); |
| 190 |
delegate = [[[RemoteDeviceRSSIHostControllerDelegate alloc] init] autorelease]; |
| 191 |
|
| 192 |
mutex.lock(); |
| 193 |
keepRunning = true; |
| 194 |
mutex.unlock(); |
| 195 |
|
| 196 |
listener = [[[QtMNSListener alloc] init] autorelease]; |
| 197 |
|
| 198 |
SInt32 result; |
| 199 |
while (keepRunning && |
| 200 |
(result = CFRunLoopRunInMode(kCFRunLoopDefaultMode ,1, YES))) { |
| 201 |
} |
| 202 |
[pool release]; |
| 203 |
} |
| 204 |
#endif |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
void networkChangeCallback(SCDynamicStoreRef, CFArrayRef changedKeys, void *) |
| 209 |
{ |
| 210 |
// NSLog(@"changed keys %@", changedKeys); |
| 211 |
QStringList keyList = nsarrayToQStringList((void*)changedKeys); |
| 212 |
if (keyList.contains("State:/Network/Global/DNS")) { |
| 213 |
} |
| 214 |
if (keyList.contains("State:/Network/Global/IPv4")) { |
| 215 |
QNetworkInfoPrivate::instance()->ethernetChanged(); |
| 216 |
QNetworkInfoPrivate::instance()->getDefaultInterface(); |
| 217 |
} |
| 218 |
|
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
void QRunLoopThread::startNetworkChangeLoop() |
| 223 |
{ |
| 224 |
storeSession = NULL; |
| 225 |
|
| 226 |
SCDynamicStoreContext dynStoreContext = { 0, this /*(void *)storeSession*/, NULL, NULL, NULL }; |
| 227 |
storeSession = SCDynamicStoreCreate(NULL, |
| 228 |
CFSTR("networkChangeCallback"), |
| 229 |
networkChangeCallback, |
| 230 |
&dynStoreContext); |
| 231 |
if (!storeSession ) { |
| 232 |
qDebug()<< "could not open dynamic store: error:" << SCErrorString(SCError()); |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
CFMutableArrayRef notificationKeys; |
| 237 |
notificationKeys = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 238 |
CFMutableArrayRef patternsArray; |
| 239 |
patternsArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 240 |
|
| 241 |
CFStringRef storeKey; |
| 242 |
|
| 243 |
storeKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, |
| 244 |
kSCDynamicStoreDomainState, |
| 245 |
kSCEntNetLink); |
| 246 |
CFArrayAppendValue(notificationKeys, storeKey); |
| 247 |
CFRelease(storeKey); |
| 248 |
|
| 249 |
storeKey = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, |
| 250 |
kSCDynamicStoreDomainState, |
| 251 |
kSCCompAnyRegex, |
| 252 |
kSCEntNetLink); |
| 253 |
CFArrayAppendValue(patternsArray, storeKey); |
| 254 |
CFRelease(storeKey); |
| 255 |
|
| 256 |
storeKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, |
| 257 |
kSCDynamicStoreDomainState, |
| 258 |
kSCEntNetDNS); |
| 259 |
CFArrayAppendValue(notificationKeys, storeKey); |
| 260 |
CFRelease(storeKey); |
| 261 |
|
| 262 |
storeKey = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, |
| 263 |
kSCDynamicStoreDomainState, |
| 264 |
kSCCompAnyRegex, |
| 265 |
kSCEntNetDNS); |
| 266 |
CFArrayAppendValue(patternsArray, storeKey); |
| 267 |
CFRelease(storeKey); |
| 268 |
|
| 269 |
storeKey = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, |
| 270 |
kSCDynamicStoreDomainState, |
| 271 |
kSCEntNetIPv4); |
| 272 |
CFArrayAppendValue(notificationKeys, storeKey); |
| 273 |
CFRelease(storeKey); |
| 274 |
|
| 275 |
storeKey = SCDynamicStoreKeyCreateNetworkServiceEntity(NULL, |
| 276 |
kSCDynamicStoreDomainState, |
| 277 |
kSCCompAnyRegex, |
| 278 |
kSCEntNetIPv4); |
| 279 |
CFArrayAppendValue(patternsArray, storeKey); |
| 280 |
CFRelease(storeKey); |
| 281 |
|
| 282 |
if (!SCDynamicStoreSetNotificationKeys(storeSession , notificationKeys, patternsArray)) { |
| 283 |
qDebug()<< "register notification error:"<< SCErrorString(SCError()); |
| 284 |
CFRelease(storeSession ); |
| 285 |
CFRelease(notificationKeys); |
| 286 |
CFRelease(patternsArray); |
| 287 |
return; |
| 288 |
} |
| 289 |
CFRelease(notificationKeys); |
| 290 |
CFRelease(patternsArray); |
| 291 |
|
| 292 |
runloopSource = SCDynamicStoreCreateRunLoopSource(NULL, storeSession , 0); |
| 293 |
if (!runloopSource) { |
| 294 |
qDebug()<< "runloop source error:"<< SCErrorString(SCError()); |
| 295 |
CFRelease(storeSession); |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
CFRunLoopAddSource(CFRunLoopGetCurrent(), runloopSource, kCFRunLoopDefaultMode); |
| 300 |
CFRelease(runloopSource); |
| 301 |
CFRelease(storeSession); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
QT_BEGIN_NAMESPACE |
| 307 |
|
| 308 |
|
| 309 |
QNetworkInfoPrivate::QNetworkInfoPrivate(QNetworkInfo *parent) |
| 310 |
: QObject(parent) |
| 311 |
, q_ptr(parent) |
| 312 |
{ |
| 313 |
#ifndef MAC_SDK_10_5 |
| 314 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 315 |
if ([[CWInterface supportedInterfaces] count] > 0 ) { |
| 316 |
hasWifi = true; |
| 317 |
} else { |
| 318 |
hasWifi = false; |
| 319 |
} |
| 320 |
[pool release]; |
| 321 |
#endif |
| 322 |
rssiTimer = new QTimer(this); |
| 323 |
runloopThread = NULL; |
| 324 |
|
| 325 |
if (!self) |
| 326 |
self = this; |
| 327 |
resetInterfaceCount(); |
| 328 |
QTimer::singleShot(200, this, SLOT(primaryInterface())); |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
QNetworkInfoPrivate::~QNetworkInfoPrivate() |
| 333 |
{ |
| 334 |
#ifndef MAC_SDK_10_5 |
| 335 |
if (runloopThread && hasWifi && networkThreadOk && runloopThread->keepRunning) { |
| 336 |
runloopThread->stop(); |
| 337 |
delete runloopThread; |
| 338 |
} |
| 339 |
#endif |
| 340 |
} |
| 341 |
|
| 342 |
void QNetworkInfoPrivate::primaryInterface() |
| 343 |
{ |
| 344 |
defaultInterface = getDefaultInterface(); |
| 345 |
} |
| 346 |
|
| 347 |
void QNetworkInfoPrivate::resetInterfaceCount() |
| 348 |
{ |
| 349 |
networkInterfaceCounts.clear(); |
| 350 |
networkInterfaceCounts.insert(QNetworkInfo::GsmMode,0); |
| 351 |
networkInterfaceCounts.insert(QNetworkInfo::CdmaMode,0); |
| 352 |
networkInterfaceCounts.insert(QNetworkInfo::WcdmaMode,0); |
| 353 |
// networkInterfaceCounts.insert(QNetworkInfo::WlanMode,0); |
| 354 |
// networkInterfaceCounts.insert(QNetworkInfo::EthernetMode,0); |
| 355 |
// networkInterfaceCounts.insert(QNetworkInfo::BluetoothMode,0); |
| 356 |
networkInterfaceCounts.insert(QNetworkInfo::WimaxMode,0); |
| 357 |
networkInterfaceCounts.insert(QNetworkInfo::LteMode,0); |
| 358 |
networkInterfaceCounts.insert(QNetworkInfo::TdscdmaMode,0); |
| 359 |
|
| 360 |
CFArrayRef interfaceArray = SCNetworkInterfaceCopyAll(); //10.4 |
| 361 |
CFStringRef iName; |
| 362 |
CFStringRef type; |
| 363 |
|
| 364 |
for ( long i = 0; i < CFArrayGetCount(interfaceArray); i++) { |
| 365 |
SCNetworkInterfaceRef thisInterface = (SCNetworkInterfaceRef ) CFArrayGetValueAtIndex(interfaceArray, i); |
| 366 |
type = SCNetworkInterfaceGetInterfaceType(thisInterface); |
| 367 |
iName = SCNetworkInterfaceGetBSDName(thisInterface); |
| 368 |
if (type != NULL) { |
| 369 |
if (CFEqual(type, kSCNetworkInterfaceTypeBluetooth)) { |
| 370 |
++networkInterfaceCounts[QNetworkInfo::BluetoothMode]; |
| 371 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeEthernet)) { |
| 372 |
++networkInterfaceCounts[QNetworkInfo::EthernetMode]; |
| 373 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeIEEE80211)) { |
| 374 |
++networkInterfaceCounts[QNetworkInfo::WlanMode]; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
CFRelease(interfaceArray); |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
int QNetworkInfoPrivate::networkInterfaceCount(QNetworkInfo::NetworkMode mode) |
| 383 |
{ |
| 384 |
return networkInterfaceCounts.value(mode); |
| 385 |
} |
| 386 |
|
| 387 |
int QNetworkInfoPrivate::networkSignalStrength(QNetworkInfo::NetworkMode mode, int interfaceNumber) |
| 388 |
{ |
| 389 |
switch (mode) { |
| 390 |
case QNetworkInfo::GsmMode: |
| 391 |
break; |
| 392 |
case QNetworkInfo::CdmaMode: |
| 393 |
break; |
| 394 |
case QNetworkInfo::WcdmaMode: |
| 395 |
break; |
| 396 |
case QNetworkInfo::WlanMode: |
| 397 |
{ |
| 398 |
int signalQuality = 0; |
| 399 |
if (hasWifi) { |
| 400 |
#ifndef MAC_SDK_10_5 |
| 401 |
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 402 |
QString name = interfaceForMode(mode, interfaceNumber).name(); |
| 403 |
CWInterface *wifiInterface = [CWInterface interfaceWithName:QCFString::toNSString(name)]; |
| 404 |
|
| 405 |
if ([wifiInterface power]) { |
| 406 |
if (!rssiTimer->isActive()) |
| 407 |
rssiTimer->start(5000); |
| 408 |
} else { |
| 409 |
if (rssiTimer->isActive()) |
| 410 |
rssiTimer->stop(); |
| 411 |
} |
| 412 |
|
| 413 |
int rssiSignal = [[wifiInterface rssi] intValue]; |
| 414 |
|
| 415 |
if (rssiSignal !=0 ) { |
| 416 |
int maxRssi = -40; |
| 417 |
int minRssi = [[wifiInterface noise] intValue]; |
| 418 |
signalQuality = ( 100 * (maxRssi - minRssi) * (maxRssi - minRssi) - (maxRssi - rssiSignal) * |
| 419 |
(15 * (maxRssi - minRssi) + 62 * (maxRssi - rssiSignal)) ) / |
| 420 |
((maxRssi - minRssi) * (maxRssi - minRssi)); |
| 421 |
|
| 422 |
} else { |
| 423 |
signalQuality = 0; |
| 424 |
} |
| 425 |
|
| 426 |
if (signalStrengthCache != signalQuality) { |
| 427 |
if (signalStrengthCache == 0) { |
| 428 |
networkStatus(QNetworkInfo::WlanMode, 0); |
| 429 |
} |
| 430 |
signalStrengthCache = signalQuality; |
| 431 |
Q_EMIT networkSignalStrengthChanged(mode, 0, signalQuality); |
| 432 |
} |
| 433 |
[autoreleasepool release]; |
| 434 |
#endif |
| 435 |
} |
| 436 |
return signalQuality; |
| 437 |
} |
| 438 |
break; |
| 439 |
case QNetworkInfo::EthernetMode: |
| 440 |
{ |
| 441 |
int percent = (isInterfaceActive(interfaceForMode(mode, interfaceNumber).name().toLatin1())) ? 100 : 0; |
| 442 |
return percent; |
| 443 |
} |
| 444 |
break; |
| 445 |
case QNetworkInfo::BluetoothMode: |
| 446 |
{ |
| 447 |
#ifdef MAC_SDK_10_6 |
| 448 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 449 |
IOBluetoothHostController* controller = IOBluetoothHostController.defaultController; |
| 450 |
|
| 451 |
if (controller != NULL) { |
| 452 |
|
| 453 |
NSArray *devices = [IOBluetoothDevice recentDevices:0]; |
| 454 |
for ( IOBluetoothDevice *btDevice in devices ) { |
| 455 |
if ([btDevice isConnected]) { |
| 456 |
qDebug()<<"IOBluetoothDevice connected"<< QCFString::toString([btDevice getName]); |
| 457 |
// delegate = [[RemoteDeviceRSSIHostControllerDelegate alloc] init]; |
| 458 |
[delegate retain]; |
| 459 |
[controller setDelegate:delegate]; |
| 460 |
IOReturn rc = [controller readRSSIForDevice:btDevice]; |
| 461 |
if (rc != noErr) { |
| 462 |
qDebug()<< "ERROR: call readRSSIForDevice failed"; |
| 463 |
} |
| 464 |
//[delegate release]; |
| 465 |
} |
| 466 |
} |
| 467 |
// [devices release]; |
| 468 |
// [controller release]; |
| 469 |
} |
| 470 |
[pool release]; |
| 471 |
#endif |
| 472 |
} |
| 473 |
case QNetworkInfo::WimaxMode: |
| 474 |
break; |
| 475 |
default: |
| 476 |
break; |
| 477 |
}; |
| 478 |
return -1; |
| 479 |
} |
| 480 |
|
| 481 |
QNetworkInfo::CellDataTechnology QNetworkInfoPrivate::currentCellDataTechnology(int) |
| 482 |
{ |
| 483 |
return QNetworkInfo::UnknownDataTechnology; |
| 484 |
} |
| 485 |
|
| 486 |
QNetworkInfo::NetworkMode QNetworkInfoPrivate::currentNetworkMode() |
| 487 |
{ |
| 488 |
return modeForInterface(getDefaultInterface()); |
| 489 |
return QNetworkInfo::UnknownMode; |
| 490 |
} |
| 491 |
|
| 492 |
QNetworkInfo::NetworkStatus QNetworkInfoPrivate::networkStatus(QNetworkInfo::NetworkMode mode, int interfaceNumber) |
| 493 |
{ |
| 494 |
QNetworkInfo::NetworkStatus status = QNetworkInfo::NoNetworkAvailable; |
| 495 |
switch (mode) { |
| 496 |
case QNetworkInfo::GsmMode: |
| 497 |
break; |
| 498 |
case QNetworkInfo::CdmaMode: |
| 499 |
break; |
| 500 |
case QNetworkInfo::WcdmaMode: |
| 501 |
break; |
| 502 |
case QNetworkInfo::WlanMode: |
| 503 |
{ |
| 504 |
#ifndef MAC_SDK_10_5 |
| 505 |
if (hasWifi) { |
| 506 |
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 507 |
CWInterface *wifiInterface = [CWInterface interfaceWithName: QCFString::toNSString(interfaceForMode(mode,interfaceNumber).name())]; |
| 508 |
|
| 509 |
if ([wifiInterface power]) { |
| 510 |
if (!rssiTimer->isActive()) |
| 511 |
rssiTimer->start(5000); |
| 512 |
} else { |
| 513 |
if (rssiTimer->isActive()) |
| 514 |
rssiTimer->stop(); |
| 515 |
} |
| 516 |
|
| 517 |
switch ([[wifiInterface interfaceState]intValue]) { |
| 518 |
case kCWInterfaceStateInactive: |
| 519 |
status = QNetworkInfo::NoNetworkAvailable; |
| 520 |
break; |
| 521 |
case kCWInterfaceStateScanning: |
| 522 |
case kCWInterfaceStateAuthenticating: |
| 523 |
case kCWInterfaceStateAssociating: |
| 524 |
status = QNetworkInfo::Searching; |
| 525 |
break; |
| 526 |
case kCWInterfaceStateRunning: |
| 527 |
status = QNetworkInfo::HomeNetwork; //Connected |
| 528 |
break; |
| 529 |
}; |
| 530 |
[autoreleasepool release]; |
| 531 |
} |
| 532 |
|
| 533 |
#else |
| 534 |
if (isInterfaceActive(interfaceForMode(mode, interfaceNumber).name().toLatin1())) { |
| 535 |
status = QNetworkInfo::HomeNetwork; |
| 536 |
} |
| 537 |
#endif |
| 538 |
} |
| 539 |
break; |
| 540 |
case QNetworkInfo::EthernetMode: |
| 541 |
{ |
| 542 |
if (isInterfaceActive(interfaceForMode(mode, interfaceNumber).name().toLatin1())) { |
| 543 |
return QNetworkInfo::HomeNetwork; //Connected |
| 544 |
} else { |
| 545 |
return QNetworkInfo::NoNetworkAvailable; |
| 546 |
} |
| 547 |
} |
| 548 |
break; |
| 549 |
case QNetworkInfo::BluetoothMode: |
| 550 |
{ |
| 551 |
|
| 552 |
} |
| 553 |
break; |
| 554 |
case QNetworkInfo::WimaxMode: |
| 555 |
break; |
| 556 |
default: |
| 557 |
break; |
| 558 |
}; |
| 559 |
return status; |
| 560 |
return QNetworkInfo::UnknownStatus; |
| 561 |
} |
| 562 |
|
| 563 |
QString QNetworkInfoPrivate::cellId(int) |
| 564 |
{ |
| 565 |
return QString(); |
| 566 |
} |
| 567 |
|
| 568 |
QString QNetworkInfoPrivate::currentMobileCountryCode(int) |
| 569 |
{ |
| 570 |
return QString(); |
| 571 |
} |
| 572 |
|
| 573 |
QString QNetworkInfoPrivate::currentMobileNetworkCode(int) |
| 574 |
{ |
| 575 |
return QString(); |
| 576 |
} |
| 577 |
|
| 578 |
QString QNetworkInfoPrivate::homeMobileCountryCode(int) |
| 579 |
{ |
| 580 |
return QString(); |
| 581 |
} |
| 582 |
|
| 583 |
QString QNetworkInfoPrivate::homeMobileNetworkCode(int) |
| 584 |
{ |
| 585 |
return QString(); |
| 586 |
} |
| 587 |
|
| 588 |
QString QNetworkInfoPrivate::imsi(int) |
| 589 |
{ |
| 590 |
return QString(); |
| 591 |
} |
| 592 |
|
| 593 |
QString QNetworkInfoPrivate::locationAreaCode(int) |
| 594 |
{ |
| 595 |
return QString(); |
| 596 |
} |
| 597 |
|
| 598 |
QString QNetworkInfoPrivate::macAddress(QNetworkInfo::NetworkMode mode, int interfaceNumber) |
| 599 |
{ |
| 600 |
return interfaceForMode(mode,interfaceNumber).hardwareAddress(); |
| 601 |
} |
| 602 |
|
| 603 |
QString QNetworkInfoPrivate::networkName(QNetworkInfo::NetworkMode mode, int interfaceNumber) |
| 604 |
{ |
| 605 |
if (networkStatus(mode, interfaceNumber) == QNetworkInfo::NoNetworkAvailable) { |
| 606 |
return QString(); |
| 607 |
} |
| 608 |
switch (mode) { |
| 609 |
case QNetworkInfo::GsmMode: |
| 610 |
break; |
| 611 |
case QNetworkInfo::CdmaMode: |
| 612 |
break; |
| 613 |
case QNetworkInfo::WcdmaMode: |
| 614 |
break; |
| 615 |
case QNetworkInfo::WlanMode: |
| 616 |
{ |
| 617 |
QString name = interfaceForMode(mode,interfaceNumber).name(); |
| 618 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 619 |
#ifndef MAC_SDK_10_5 |
| 620 |
if (hasWifi) { |
| 621 |
CWInterface *wifiInterface = [CWInterface interfaceWithName:QCFString::toNSString(name)]; |
| 622 |
QString netname = nsstringToQString([wifiInterface ssid]); |
| 623 |
[pool release]; |
| 624 |
return netname; |
| 625 |
} |
| 626 |
#else |
| 627 |
SCDynamicStoreRef theDynamicStore; |
| 628 |
theDynamicStore = SCDynamicStoreCreate(nil, CFSTR("FindCurrentInterfaceAndIP"), nil, nil); |
| 629 |
|
| 630 |
NSMutableString *interfaceName = [NSMutableString string]; |
| 631 |
NSString *airportPath = [NSString stringWithFormat:@"State:/Network/Interface/%@/AirPort", QCFString::toNSString(name)]; |
| 632 |
|
| 633 |
CFDictionaryRef airportPlist = (const __CFDictionary*)SCDynamicStoreCopyValue(theDynamicStore, (CFStringRef)airportPath); |
| 634 |
|
| 635 |
CFRelease(theDynamicStore); |
| 636 |
QString netname = QCFString::ToQString([(NSDictionary *)airportPlist valueForKey:@"SSID_STR"]); |
| 637 |
[pool release]; |
| 638 |
return netname; |
| 639 |
#endif |
| 640 |
} |
| 641 |
break; |
| 642 |
case QNetworkInfo::EthernetMode: |
| 643 |
{ |
| 644 |
QString netname; |
| 645 |
if (isInterfaceActive(interfaceForMode(mode,interfaceNumber).name().toLocal8Bit())) { |
| 646 |
int inNumber = 0; |
| 647 |
// QString netname = interfaceForMode(mode,interfaceNumber).humanReadableName(); |
| 648 |
CFArrayRef interfaceArray = SCNetworkInterfaceCopyAll(); //10.4 |
| 649 |
CFStringRef iName; |
| 650 |
CFStringRef type; |
| 651 |
|
| 652 |
for ( long i = 0; i < CFArrayGetCount(interfaceArray); i++) { |
| 653 |
SCNetworkInterfaceRef thisInterface = (SCNetworkInterfaceRef ) CFArrayGetValueAtIndex(interfaceArray, i); |
| 654 |
type = SCNetworkInterfaceGetInterfaceType(thisInterface); |
| 655 |
iName = SCNetworkInterfaceGetBSDName(thisInterface); |
| 656 |
// if (interfaceName == QCFString::toQString(iName)) { |
| 657 |
if (type != NULL) { |
| 658 |
if (CFEqual(type, kSCNetworkInterfaceTypeEthernet)) { |
| 659 |
if (inNumber == interfaceNumber) { |
| 660 |
netname = QCFString::toQString(SCNetworkInterfaceGetLocalizedDisplayName(thisInterface)); |
| 661 |
break; |
| 662 |
} else { |
| 663 |
inNumber++; |
| 664 |
} |
| 665 |
} |
| 666 |
// } |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
return netname;//QHostInfo::localDomainName(); //FIXME |
| 671 |
} |
| 672 |
break; |
| 673 |
case QNetworkInfo::BluetoothMode: |
| 674 |
break; |
| 675 |
case QNetworkInfo::WimaxMode: |
| 676 |
break; |
| 677 |
default: |
| 678 |
break; |
| 679 |
}; |
| 680 |
return QString(); |
| 681 |
} |
| 682 |
|
| 683 |
void QNetworkInfoPrivate::connectNotify(const QMetaMethod &signal) |
| 684 |
{ |
| 685 |
static const QMetaMethod networkSignalStrengthChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkSignalStrengthChanged); |
| 686 |
static const QMetaMethod networkNameChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkNameChanged); |
| 687 |
static const QMetaMethod networkStatusChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkStatusChanged); |
| 688 |
|
| 689 |
if (signal == networkSignalStrengthChangedSignal) { |
| 690 |
connect(rssiTimer, SIGNAL(timeout()), this, SLOT(rssiTimeout())); |
| 691 |
rssiTimer->start(5000); |
| 692 |
} |
| 693 |
if (signal == networkNameChangedSignal |
| 694 |
|| signal == networkStatusChangedSignal) { |
| 695 |
#ifndef MAC_SDK_10_5 |
| 696 |
if (hasWifi) { |
| 697 |
runloopThread = new QRunLoopThread(); |
| 698 |
runloopThread->doWork(); |
| 699 |
networkThreadOk = true; |
| 700 |
} |
| 701 |
#endif |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
void QNetworkInfoPrivate::disconnectNotify(const QMetaMethod &signal) |
| 706 |
{ |
| 707 |
|
| 708 |
static const QMetaMethod networkSignalStrengthChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkSignalStrengthChanged); |
| 709 |
static const QMetaMethod networkNameChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkNameChanged); |
| 710 |
static const QMetaMethod networkStatusChangedSignal = QMetaMethod::fromSignal(&QNetworkInfoPrivate::networkStatusChanged); |
| 711 |
|
| 712 |
|
| 713 |
if (signal == networkSignalStrengthChangedSignal) { |
| 714 |
rssiTimer->stop(); |
| 715 |
disconnect(rssiTimer, SIGNAL(timeout()), this, SLOT(rssiTimeout())); |
| 716 |
} |
| 717 |
if (signal == networkNameChangedSignal |
| 718 |
|| signal == networkStatusChangedSignal) { |
| 719 |
#ifndef MAC_SDK_10_5 |
| 720 |
if (hasWifi && networkThreadOk) { |
| 721 |
runloopThread->stop(); |
| 722 |
[delegate release]; |
| 723 |
} |
| 724 |
#endif |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
#ifndef QT_NO_NETWORKINTERFACE |
| 729 |
QNetworkInterface QNetworkInfoPrivate::interfaceForMode(QNetworkInfo::NetworkMode mode, int interface) |
| 730 |
{ |
| 731 |
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; |
| 732 |
QNetworkInterface netInterface; |
| 733 |
CFArrayRef interfaceArray = SCNetworkInterfaceCopyAll(); //10.4 |
| 734 |
CFStringRef iName; |
| 735 |
CFStringRef type; |
| 736 |
|
| 737 |
int interfaceTypeCount = 0; |
| 738 |
for (long i = 0; i < CFArrayGetCount(interfaceArray); i++) { |
| 739 |
SCNetworkInterfaceRef thisInterface = (SCNetworkInterfaceRef ) CFArrayGetValueAtIndex(interfaceArray, i); |
| 740 |
type = SCNetworkInterfaceGetInterfaceType(thisInterface); |
| 741 |
iName = SCNetworkInterfaceGetBSDName(thisInterface); |
| 742 |
|
| 743 |
if (type != NULL) { |
| 744 |
if (CFEqual(type, kSCNetworkInterfaceTypeBluetooth) && mode == QNetworkInfo::BluetoothMode) { |
| 745 |
|
| 746 |
netInterface = QNetworkInterface::interfaceFromName(QCFString::toQString(iName)); |
| 747 |
|
| 748 |
// workaround for null MAC from SCNetworkInterfaceGetHardwareAddressString and bogus BSD name here |
| 749 |
#ifdef MAC_SDK_10_6 |
| 750 |
IOBluetoothHostController* controller = IOBluetoothHostController.defaultController; |
| 751 |
QString macbtMac = QCFString::toQString([controller addressAsString]).replace("-",":").toUpper(); |
| 752 |
if (!macbtMac.isEmpty()) { |
| 753 |
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); |
| 754 |
Q_FOREACH (const QNetworkInterface thisNetInterface, interfaces) { |
| 755 |
if ( thisNetInterface.hardwareAddress() == macbtMac) { |
| 756 |
if (interfaceTypeCount == interface) { |
| 757 |
netInterface = thisNetInterface; |
| 758 |
break; |
| 759 |
} else { |
| 760 |
interfaceTypeCount++; |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
} |
| 765 |
#endif |
| 766 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeEthernet) && mode == QNetworkInfo::EthernetMode) { |
| 767 |
if (interfaceTypeCount == interface) { |
| 768 |
netInterface = QNetworkInterface::interfaceFromName(QCFString::toQString(iName)); |
| 769 |
break; |
| 770 |
} else { |
| 771 |
interfaceTypeCount++; |
| 772 |
} |
| 773 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeIEEE80211) && mode == QNetworkInfo::WlanMode) { |
| 774 |
if (interfaceTypeCount == interface) { |
| 775 |
netInterface = QNetworkInterface::interfaceFromName(QCFString::toQString(iName)); |
| 776 |
break; |
| 777 |
} else { |
| 778 |
interfaceTypeCount++; |
| 779 |
} |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
CFRelease(interfaceArray); |
| 784 |
[autoreleasepool release]; |
| 785 |
return netInterface; |
| 786 |
} |
| 787 |
#endif // QT_NO_NETWORKINTERFACE |
| 788 |
|
| 789 |
QNetworkInfo::NetworkMode QNetworkInfoPrivate::modeForInterface(QString interfaceName) |
| 790 |
{ |
| 791 |
QNetworkInfo::NetworkMode mode = QNetworkInfo::UnknownMode; |
| 792 |
CFArrayRef interfaceArray = SCNetworkInterfaceCopyAll(); //10.4 |
| 793 |
CFStringRef iName; |
| 794 |
CFStringRef type; |
| 795 |
|
| 796 |
for ( long i = 0; i < CFArrayGetCount(interfaceArray); i++) { |
| 797 |
SCNetworkInterfaceRef thisInterface = (SCNetworkInterfaceRef ) CFArrayGetValueAtIndex(interfaceArray, i); |
| 798 |
type = SCNetworkInterfaceGetInterfaceType(thisInterface); |
| 799 |
iName = SCNetworkInterfaceGetBSDName(thisInterface); |
| 800 |
if ( interfaceName == QCFString::toQString(iName)) { |
| 801 |
if (type != NULL) { |
| 802 |
if (CFEqual(type, kSCNetworkInterfaceTypeBluetooth)) { |
| 803 |
mode = QNetworkInfo::BluetoothMode; |
| 804 |
break; |
| 805 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeEthernet)) { |
| 806 |
mode = QNetworkInfo::EthernetMode; |
| 807 |
break; |
| 808 |
} else if (CFEqual(type, kSCNetworkInterfaceTypeIEEE80211)) { |
| 809 |
mode = QNetworkInfo::WlanMode; |
| 810 |
break; |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
} |
| 815 |
CFRelease(interfaceArray); |
| 816 |
return mode; |
| 817 |
} |
| 818 |
|
| 819 |
QString QNetworkInfoPrivate::getDefaultInterface() |
| 820 |
{ |
| 821 |
// resetInterfaceCount(); |
| 822 |
|
| 823 |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
| 824 |
SCDynamicStoreRef storeSession2; |
| 825 |
CFStringRef key; |
| 826 |
CFDictionaryRef globalDict; |
| 827 |
CFStringRef primaryInterface = NULL; |
| 828 |
QString interfaceName; |
| 829 |
|
| 830 |
storeSession2 = SCDynamicStoreCreate(NULL, CFSTR("getPrimary"), NULL, NULL); |
| 831 |
// if (!storeSession2) { |
| 832 |
// } |
| 833 |
key = SCDynamicStoreKeyCreateNetworkGlobalEntity(NULL, kSCDynamicStoreDomainState,kSCEntNetIPv4); |
| 834 |
globalDict = (const __CFDictionary*)SCDynamicStoreCopyValue(storeSession2, key); |
| 835 |
CFRelease(key); |
| 836 |
if (globalDict) { |
| 837 |
|
| 838 |
primaryInterface = (CFStringRef )CFDictionaryGetValue(globalDict, |
| 839 |
kSCDynamicStorePropNetPrimaryInterface); |
| 840 |
if (primaryInterface) { |
| 841 |
CFRetain(primaryInterface); |
| 842 |
} |
| 843 |
CFRelease(globalDict); |
| 844 |
} |
| 845 |
|
| 846 |
CFRelease(storeSession2); |
| 847 |
if (primaryInterface) { |
| 848 |
interfaceName = QCFString::toQString(primaryInterface); |
| 849 |
if (interfaceName != defaultInterface) { |
| 850 |
Q_EMIT currentNetworkModeChanged(modeForInterface(interfaceName)); |
| 851 |
defaultInterface = interfaceName; |
| 852 |
} |
| 853 |
} |
| 854 |
[pool release]; |
| 855 |
return interfaceName; |
| 856 |
} |
| 857 |
|
| 858 |
void QNetworkInfoPrivate::rssiTimeout() |
| 859 |
{ |
| 860 |
networkSignalStrength(QNetworkInfo::WlanMode,0); |
| 861 |
} |
| 862 |
|
| 863 |
bool QNetworkInfoPrivate::isInterfaceActive(const char* netInterface) |
| 864 |
{ |
| 865 |
struct ifmediareq ifm; |
| 866 |
|
| 867 |
memset(&ifm, 0, sizeof(struct ifmediareq)); |
| 868 |
strncpy(ifm.ifm_name, netInterface, IFNAMSIZ); |
| 869 |
int s = socket(AF_INET, SOCK_DGRAM, 0); |
| 870 |
ioctl(s, SIOCGIFMEDIA, &ifm); |
| 871 |
if (ifm.ifm_status & IFM_ACTIVE) { |
| 872 |
return true; |
| 873 |
} |
| 874 |
return false; |
| 875 |
} |
| 876 |
|
| 877 |
void QNetworkInfoPrivate::ethernetChanged() |
| 878 |
{ |
| 879 |
resetInterfaceCount(); |
| 880 |
QNetworkInfo::NetworkStatus status = QNetworkInfo::NoNetworkAvailable; |
| 881 |
int carrier = 0; |
| 882 |
|
| 883 |
if (isInterfaceActive(interfaceForMode(QNetworkInfo::EthernetMode,0).name().toLocal8Bit()),0) { |
| 884 |
status = QNetworkInfo::HomeNetwork/*Connected*/; |
| 885 |
carrier = 100; |
| 886 |
} |
| 887 |
Q_EMIT networkStatusChanged(QNetworkInfo::EthernetMode,0, status); |
| 888 |
Q_EMIT networkSignalStrengthChanged(QNetworkInfo::EthernetMode,0,carrier); |
| 889 |
Q_EMIT networkNameChanged(QNetworkInfo::EthernetMode, 0,networkName(QNetworkInfo::EthernetMode,0)); |
| 890 |
Q_EMIT currentNetworkModeChanged(modeForInterface(getDefaultInterface())); |
| 891 |
} |
| 892 |
|
| 893 |
void QNetworkInfoPrivate::wifiNetworkChanged(const QString ¬ification, const QString interfaceName) |
| 894 |
{ |
| 895 |
resetInterfaceCount(); |
| 896 |
if (notification == QLatin1String("SSID_CHANGED_NOTIFICATION")) { |
| 897 |
Q_EMIT networkNameChanged(QNetworkInfo::WlanMode, 0, networkName(QNetworkInfo::WlanMode, 0)); |
| 898 |
} |
| 899 |
|
| 900 |
if (notification == QLatin1String("BSSID_CHANGED_NOTIFICATION")) { |
| 901 |
QNetworkInfo::NetworkStatus status = networkStatus(QNetworkInfo::WlanMode,0); |
| 902 |
Q_EMIT networkStatusChanged(QNetworkInfo::WlanMode, 0, status); |
| 903 |
} |
| 904 |
if (notification == QLatin1String("POWER_CHANGED_NOTIFICATION")) { |
| 905 |
#ifndef MAC_SDK_10_5 |
| 906 |
CWInterface *wifiInterface = [CWInterface interfaceWithName: QCFString::toNSString(interfaceName)]; |
| 907 |
if ([wifiInterface power]) { |
| 908 |
if (!rssiTimer->isActive()) { |
| 909 |
rssiTimer->start(5000); |
| 910 |
} |
| 911 |
} else { |
| 912 |
if (rssiTimer->isActive()) { |
| 913 |
rssiTimer->stop(); |
| 914 |
} |
| 915 |
int strength = [[wifiInterface rssi] intValue]; |
| 916 |
Q_EMIT networkSignalStrengthChanged(QNetworkInfo::WlanMode, 0, strength); |
| 917 |
} |
| 918 |
#endif |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
QT_END_NAMESPACE |