@ -1,9 +1,11 @@ 
			
		
	
		
		
			
				
					
					# include  <algorithm> # include  <algorithm>  
			
		
	
		
		
			
				
					
					# include  <set> # include  <set>  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					# include  "wifiManager.hpp" # include  "wifiManager.hpp"  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					
/**
  
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					 *  We  are  using  a  NetworkManager  DBUS  API  :  https : //developer.gnome.org/NetworkManager/1.26/spec.html
   
			
		
	
		
		
			
				
					
					 *  */   
			
		
	
		
		
			
				
					
					 
  
			
		
	
		
		
			
				
					
					QString  nm_path                 =  " /org/freedesktop/NetworkManager " ; QString  nm_path                 =  " /org/freedesktop/NetworkManager " ;  
			
		
	
		
		
			
				
					
					QString  nm_settings_path        =  " /org/freedesktop/NetworkManager/Settings " ; QString  nm_settings_path        =  " /org/freedesktop/NetworkManager/Settings " ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -18,6 +20,9 @@ QString connection_iface       = "org.freedesktop.NetworkManager.Connection.Acti 
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					QString  nm_service              =  " org.freedesktop.NetworkManager " ; QString  nm_service              =  " org.freedesktop.NetworkManager " ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					const  int  state_connected  =  100 ;  
			
		
	
		
		
			
				
					
					const  int  state_need_auth  =  60 ;  
			
		
	
		
		
			
				
					
					const  int  reason_wrong_password  =  8 ;  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					template  < typename  T > template  < typename  T >  
			
		
	
		
		
			
				
					
					T  get_response ( QDBusMessage  response ) { T  get_response ( QDBusMessage  response ) {  
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -28,16 +33,27 @@ T get_response(QDBusMessage response){ 
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					bool  compare_by_strength ( const  Network  & a ,  const  Network  & b ) { bool  compare_by_strength ( const  Network  & a ,  const  Network  & b ) {  
			
		
	
		
		
			
				
					
					  if  ( a . connected )  return  true ;    if  ( a . connected  = =  ConnectedType : : CONNECTED )  return  true ;   
			
				
				
			
		
	
		
		
			
				
					
					  if  ( b . connected )  return  false ;    if  ( b . connected  = =  ConnectedType : : CONNECTED )  return  false ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					  if  ( a . connected  = =  ConnectedType : : CONNECTING )  return  true ;   
			
		
	
		
		
			
				
					
					  if  ( b . connected  = =  ConnectedType : : CONNECTING )  return  false ;   
			
		
	
		
		
			
				
					
					  return  a . strength  >  b . strength ;    return  a . strength  >  b . strength ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					WifiManager : : WifiManager ( ) { WifiManager : : WifiManager ( ) {  
			
		
	
		
		
			
				
					
					  qDBusRegisterMetaType < Connection > ( ) ;    qDBusRegisterMetaType < Connection > ( ) ;   
			
		
	
		
		
			
				
					
					
   connecting_to_network  =  " " ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					  adapter  =  get_adapter ( ) ;    adapter  =  get_adapter ( ) ;   
			
		
	
		
		
			
				
					
					  has_adapter  =  adapter  ! =  " " ;    has_adapter  =  adapter  ! =  " " ;   
			
		
	
		
		
			
				
					
					  if ( has_adapter ) {   
			
		
	
		
		
			
				
					
					    QDBusInterface  nm ( nm_service ,  adapter ,  device_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					    bus . connect ( nm_service ,  adapter ,  device_iface ,  " StateChanged " ,  this ,  SLOT ( change ( unsigned  int ,  unsigned  int ,  unsigned  int ) ) ) ;   
			
		
	
		
		
			
				
					
					    
  
			
		
	
		
		
			
				
					
					    QDBusInterface  device_props ( nm_service ,  adapter ,  props_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					    QDBusMessage  response  =  device_props . call ( " Get " ,  device_iface ,  " State " ) ;   
			
		
	
		
		
			
				
					
					    raw_adapter_state  =  get_response < uint > ( response ) ;   
			
		
	
		
		
			
				
					
					    change ( raw_adapter_state ,  0 ,  0 ) ;   
			
		
	
		
		
			
				
					
					  }   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  WifiManager : : refreshNetworks ( ) { void  WifiManager : : refreshNetworks ( ) {  
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -63,7 +79,6 @@ QList<Network> WifiManager::get_networks(){ 
			
		
	
		
		
			
				
					
					  QVariant  first  =   response . arguments ( ) . at ( 0 ) ;    QVariant  first  =   response . arguments ( ) . at ( 0 ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  QString  active_ap  =  get_active_ap ( ) ;    QString  active_ap  =  get_active_ap ( ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  const  QDBusArgument  & args  =  first . value < QDBusArgument > ( ) ;    const  QDBusArgument  & args  =  first . value < QDBusArgument > ( ) ;   
			
		
	
		
		
			
				
					
					  args . beginArray ( ) ;    args . beginArray ( ) ;   
			
		
	
		
		
			
				
					
					  while  ( ! args . atEnd ( ) )  {    while  ( ! args . atEnd ( ) )  {   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -73,7 +88,17 @@ QList<Network> WifiManager::get_networks(){ 
			
		
	
		
		
			
				
					
					    QByteArray  ssid  =  get_property ( path . path ( ) ,  " Ssid " ) ;      QByteArray  ssid  =  get_property ( path . path ( ) ,  " Ssid " ) ;   
			
		
	
		
		
			
				
					
					    unsigned  int  strength  =  get_ap_strength ( path . path ( ) ) ;      unsigned  int  strength  =  get_ap_strength ( path . path ( ) ) ;   
			
		
	
		
		
			
				
					
					    SecurityType  security  =  getSecurityType ( path . path ( ) ) ;      SecurityType  security  =  getSecurityType ( path . path ( ) ) ;   
			
		
	
		
		
			
				
					
					    Network  network  =  { path . path ( ) ,  ssid ,  strength ,  path . path ( ) = = active_ap ,  security } ;      ConnectedType  ctype ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					    if ( path . path ( )  ! =  active_ap ) {   
			
		
	
		
		
			
				
					
					      ctype  =  ConnectedType : : DISCONNECTED ;   
			
		
	
		
		
			
				
					
					    } else {   
			
		
	
		
		
			
				
					
					      if ( ssid  = =  connecting_to_network ) {   
			
		
	
		
		
			
				
					
					        ctype  =  ConnectedType : : CONNECTING ;   
			
		
	
		
		
			
				
					
					      } else {   
			
		
	
		
		
			
				
					
					        ctype  =  ConnectedType : : CONNECTED ;   
			
		
	
		
		
			
				
					
					      }   
			
		
	
		
		
			
				
					
					    }   
			
		
	
		
		
			
				
					
					    Network  network  =  { path . path ( ) ,  ssid ,  strength ,  ctype ,  security } ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					    if  ( ssid . length ( ) ) {      if  ( ssid . length ( ) ) {   
			
		
	
		
		
			
				
					
					      r . push_back ( network ) ;        r . push_back ( network ) ;   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -96,10 +121,6 @@ SecurityType WifiManager::getSecurityType(QString path){ 
			
		
	
		
		
			
				
					
					  }  else  if ( ( sflag  &  0x1 )  & &  ( wpa_props  &  ( 0x333 )  & &  ! ( wpa_props  &  0x200 ) ) )  {    }  else  if ( ( sflag  &  0x1 )  & &  ( wpa_props  &  ( 0x333 )  & &  ! ( wpa_props  &  0x200 ) ) )  {   
			
		
	
		
		
			
				
					
					    return  SecurityType : : WPA ;      return  SecurityType : : WPA ;   
			
		
	
		
		
			
				
					
					  }  else  {    }  else  {   
			
		
	
		
		
			
				
					
					    // qDebug() << "Cannot determine security type for " << get_property(path, "Ssid") << " with flags";
   
			
		
	
		
		
			
				
					
					    // qDebug() << "flag    " << sflag;
   
			
		
	
		
		
			
				
					
					    // qDebug() << "WpaFlag " << wpaflag;
   
			
		
	
		
		
			
				
					
					    // qDebug() << "RsnFlag " << rsnflag;
   
			
		
	
		
		
			
				
					
					    return  SecurityType : : UNSUPPORTED ;      return  SecurityType : : UNSUPPORTED ;   
			
		
	
		
		
			
				
					
					  }    }   
			
		
	
		
		
			
				
					
					} }  
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -113,12 +134,12 @@ void WifiManager::connect(Network n, QString password){ 
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  WifiManager : : connect ( Network  n ,  QString  username ,  QString  password ) { void  WifiManager : : connect ( Network  n ,  QString  username ,  QString  password ) {  
			
		
	
		
		
			
				
					
					  connecting_to_network  =  n . ssid ;   
			
		
	
		
		
			
				
					
					  QString  active_ap  =  get_active_ap ( ) ;    QString  active_ap  =  get_active_ap ( ) ;   
			
		
	
		
		
			
				
					
					  if   ( active_ap ! = " " )  {    if ( active_ap ! = " "  & &  active_ap ! = " / " )  {   
			
				
				
			
		
	
		
		
			
				
					
					    clear _connections( get_property ( active_ap ,  " Ssid " ) ) ;      deactivate _connections( get_property ( active_ap ,  " Ssid " ) ) ;  //Disconnect from any connected networks 
    
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					  }    }   
			
		
	
		
		
			
				
					
					  clear_connections ( n . ssid ) ;    clear_connections ( n . ssid ) ;  //Clear all connections that may already exist to the network we are connecting
   
			
				
				
			
		
	
		
		
			
				
					
					  qDebug ( )  < <  " Connecting to " < <  n . ssid  < <  " with username, password = "  < <  username  < <  " , "  < < password ;   
			
		
	
		
		
	
		
		
			
				
					
					  connect ( n . ssid ,  username ,  password ,  n . security_type ) ;    connect ( n . ssid ,  username ,  password ,  n . security_type ) ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -127,7 +148,7 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S 
			
		
	
		
		
			
				
					
					  connection [ " connection " ] [ " type " ]  =  " 802-11-wireless " ;    connection [ " connection " ] [ " type " ]  =  " 802-11-wireless " ;   
			
		
	
		
		
			
				
					
					  connection [ " connection " ] [ " uuid " ]  =  QUuid : : createUuid ( ) . toString ( ) . remove ( ' { ' ) . remove ( ' } ' ) ;    connection [ " connection " ] [ " uuid " ]  =  QUuid : : createUuid ( ) . toString ( ) . remove ( ' { ' ) . remove ( ' } ' ) ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  connection [ " connection " ] [ " id " ]  =  " OpenPilot connection  " + QString : : fromStdString ( ssid . toStdString ( ) ) ;  //TODO Add security type
     connection [ " connection " ] [ " id " ]  =  " OpenPilot connection  " + QString : : fromStdString ( ssid . toStdString ( ) ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  connection [ " 802-11-wireless " ] [ " ssid " ]  =  ssid ;    connection [ " 802-11-wireless " ] [ " ssid " ]  =  ssid ;   
			
		
	
		
		
			
				
					
					  connection [ " 802-11-wireless " ] [ " mode " ]  =  " infrastructure " ;    connection [ " 802-11-wireless " ] [ " mode " ]  =  " infrastructure " ;   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -143,28 +164,34 @@ void WifiManager::connect(QByteArray ssid, QString username, QString password, S 
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  QDBusInterface  nm_settings ( nm_service ,  nm_settings_path ,  nm_settings_iface ,  bus ) ;    QDBusInterface  nm_settings ( nm_service ,  nm_settings_path ,  nm_settings_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					  QDBusReply < QDBusObjectPath >  result  =  nm_settings . call ( " AddConnection " ,  QVariant : : fromValue ( connection ) ) ;    QDBusReply < QDBusObjectPath >  result  =  nm_settings . call ( " AddConnection " ,  QVariant : : fromValue ( connection ) ) ;   
			
		
	
		
		
			
				
					
					  if  ( ! result . isValid ( ) )  {  }  
			
				
				
			
		
	
		
		
			
				
					
					    qDebug ( )  < <  result . error ( ) . name ( )  < <  result . error ( ) . message ( ) ;  
 
			
				
				
			
		
	
		
		
			
				
					
					  }  else  {  void  WifiManager : : deactivate_connections ( QString  ssid ) {  
			
				
				
			
		
	
		
		
			
				
					
					    qDebug ( )  < <  result . value ( ) . path ( ) ;    for ( QDBusObjectPath  active_connection_raw  :  get_active_connections ( ) ) {   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					    QString  active_connection  =  active_connection_raw . path ( ) ;   
			
		
	
		
		
			
				
					
					    QDBusInterface  nm ( nm_service ,  active_connection ,  props_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					    QDBusObjectPath  pth  =  get_response < QDBusObjectPath > ( nm . call ( " Get " ,  connection_iface ,  " SpecificObject " ) ) ;   
			
		
	
		
		
			
				
					
					    QString  Ssid  =  get_property ( pth . path ( ) ,  " Ssid " ) ;   
			
		
	
		
		
			
				
					
					    if ( Ssid  = =  ssid ) {   
			
		
	
		
		
			
				
					
					      QDBusInterface  nm2 ( nm_service ,  nm_path ,  nm_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					      nm2 . call ( " DeactivateConnection " ,  QVariant : : fromValue ( active_connection_raw ) ) ;   
			
		
	
		
		
			
				
					
					    }   
			
		
	
		
		
			
				
					
					  }    }   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  WifiManager : : print_active_connections ( ) { QVector < QDBusObjectPath >  WifiManager : : get_active_connections ( ) {  
			
				
				
			
		
	
		
		
			
				
					
					  //TO-DO clean up, the code is not currently in use.
   
			
		
	
		
		
	
		
		
			
				
					
					  QDBusInterface  nm ( nm_service ,  nm_path ,  props_iface ,  bus ) ;    QDBusInterface  nm ( nm_service ,  nm_path ,  props_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					  QDBusMessage  response  =  nm . call ( " Get " ,  nm_iface ,  " ActiveConnections " ) ;    QDBusMessage  response  =  nm . call ( " Get " ,  nm_iface ,  " ActiveConnections " ) ;   
			
		
	
		
		
			
				
					
					  QVariant  first  =  response . arguments ( ) . at ( 0 ) ;    QDBusArgument  arr  =  get_response < QDBusArgument > ( response ) ;   
			
				
				
			
		
	
		
		
			
				
					
					  QDBusVariant  dbvFirst  =  first . value < QDBusVariant > ( ) ;    QVector < QDBusObjectPath >  conns ;   
			
				
				
			
		
	
		
		
			
				
					
					  QVariant  vFirst  =  dbvFirst . variant ( ) ;  
 
			
				
				
			
		
	
		
		
			
				
					
					  QDBusArgument  step4  =  vFirst . value < QDBusArgument > ( ) ;   
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					  QDBusObjectPath  path ;    QDBusObjectPath  path ;   
			
		
	
		
		
			
				
					
					  step4 . beginArray ( ) ;    arr . beginArray ( ) ;   
			
				
				
			
		
	
		
		
			
				
					
					  while  ( ! step4 . atEnd ( ) ) {    while  ( ! arr . atEnd ( ) ) {   
			
				
				
			
		
	
		
		
			
				
					
					    step4 > >  path ;      arr > >  path ;   
			
				
				
			
		
	
		
		
			
				
					
					    qDebug ( ) < < path . path ( ) ;      conns . push_back ( path ) ;   
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					  }    }   
			
		
	
		
		
			
				
					
					  step4 . endArray ( ) ;    return  conns ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  WifiManager : : clear_connections ( QString  ssid ) { void  WifiManager : : clear_connections ( QString  ssid ) {  
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -189,7 +216,6 @@ void WifiManager::clear_connections(QString ssid){ 
			
		
	
		
		
			
				
					
					        if ( inner_key  = =  " ssid " ) {          if ( inner_key  = =  " ssid " ) {   
			
		
	
		
		
			
				
					
					          QString  value  =  innerMap . value ( inner_key ) . value < QString > ( ) ;            QString  value  =  innerMap . value ( inner_key ) . value < QString > ( ) ;   
			
		
	
		
		
			
				
					
					          if ( value  = =  ssid ) {            if ( value  = =  ssid ) {   
			
		
	
		
		
			
				
					
					            // qDebug()<<"Deleting "<<value;
   
			
		
	
		
		
			
				
					
					            nm2 . call ( " Delete " ) ;              nm2 . call ( " Delete " ) ;   
			
		
	
		
		
			
				
					
					          }            }   
			
		
	
		
		
			
				
					
					        }          }   
			
		
	
	
		
		
			
				
					
						
						
						
							
								 
						
					 
					@ -202,7 +228,7 @@ void WifiManager::request_scan(){ 
			
		
	
		
		
			
				
					
					  if  ( ! has_adapter )  return ;    if  ( ! has_adapter )  return ;   
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  QDBusInterface  nm ( nm_service ,  adapter ,  wireless_device_iface ,  bus ) ;    QDBusInterface  nm ( nm_service ,  adapter ,  wireless_device_iface ,  bus ) ;   
			
		
	
		
		
			
				
					
					  QDBusMessage  response  =  nm . call ( " RequestScan " ,   QVariantMap ( ) ) ;    nm . call ( " RequestScan " ,   QVariantMap ( ) ) ;   
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					uint  WifiManager : : get_wifi_device_state ( ) { uint  WifiManager : : get_wifi_device_state ( ) {  
			
		
	
	
		
		
			
				
					
						
							
								 
						
						
							
								 
						
						
					 
					@ -259,3 +285,12 @@ QString WifiManager::get_adapter(){ 
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					  return  adapter_path ;    return  adapter_path ;   
			
		
	
		
		
			
				
					
					} }  
			
		
	
		
		
			
				
					
					
 
			
		
	
		
		
			
				
					
					void  WifiManager : : change ( unsigned  int  new_state , unsigned  int  previous_state , unsigned  int  change_reason ) {  
			
		
	
		
		
			
				
					
					  raw_adapter_state  =  new_state ;   
			
		
	
		
		
			
				
					
					  if ( new_state  = =  state_need_auth  & &  change_reason  = =  reason_wrong_password ) {   
			
		
	
		
		
			
				
					
					    emit  wrongPassword ( connecting_to_network ) ;   
			
		
	
		
		
			
				
					
					  } else  if ( new_state  = =  state_connected ) {   
			
		
	
		
		
			
				
					
					    connecting_to_network = " " ;   
			
		
	
		
		
			
				
					
					  }   
			
		
	
		
		
			
				
					
					}