qtmobility/src/bearer/qnetworksession_maemo.cpp
branchRCL_3
changeset 3 eb34711bcc75
parent 1 5822d84012fb
child 6 4203353e74ea
equal deleted inserted replaced
2:87be51aa5b5b 3:eb34711bcc75
    48 #include <proxyconf.h>
    48 #include <proxyconf.h>
    49 
    49 
    50 #include <ifaddrs.h>
    50 #include <ifaddrs.h>
    51 #include <netinet/in.h>
    51 #include <netinet/in.h>
    52 #include <arpa/inet.h>
    52 #include <arpa/inet.h>
       
    53 
       
    54 
       
    55 QDBusArgument &operator<<(QDBusArgument &argument,
       
    56                           const QtMobility::ICd2DetailsDBusStruct &icd2)
       
    57 {
       
    58     argument.beginStructure();
       
    59     argument << icd2.serviceType;
       
    60     argument << icd2.serviceAttributes;
       
    61     argument << icd2.setviceId;
       
    62     argument << icd2.networkType;
       
    63     argument << icd2.networkAttributes;
       
    64     argument << icd2.networkId;
       
    65     argument.endStructure();
       
    66     return argument;
       
    67 }
       
    68 
       
    69 const QDBusArgument &operator>>(const QDBusArgument &argument,
       
    70                                 QtMobility::ICd2DetailsDBusStruct &icd2)
       
    71 {
       
    72     argument.beginStructure();
       
    73     argument >> icd2.serviceType;
       
    74     argument >> icd2.serviceAttributes;
       
    75     argument >> icd2.setviceId;
       
    76     argument >> icd2.networkType;
       
    77     argument >> icd2.networkAttributes;
       
    78     argument >> icd2.networkId;
       
    79     argument.endStructure();
       
    80     return argument;
       
    81 }
       
    82 
       
    83 const QDBusArgument &operator>>(const QDBusArgument &argument,
       
    84                                 QtMobility::ICd2DetailsList &detailsList)
       
    85 {
       
    86      argument.beginArray();
       
    87      detailsList.clear();
       
    88 
       
    89      while (!argument.atEnd()) {
       
    90          QtMobility::ICd2DetailsDBusStruct element;
       
    91          argument >> element;
       
    92          detailsList.append(element);
       
    93      }
       
    94 
       
    95      argument.endArray();
       
    96      return argument;
       
    97 }
       
    98 
       
    99 QDBusArgument &operator<<(QDBusArgument &argument,
       
   100                           const QtMobility::ICd2DetailsList &detailsList)
       
   101 {
       
   102      argument.beginArray(qMetaTypeId<QtMobility::ICd2DetailsDBusStruct>());
       
   103      for (int i = 0; i < detailsList.count(); ++i)
       
   104          argument << detailsList[i];
       
   105      argument.endArray();
       
   106      return argument;
       
   107 }
    53 
   108 
    54 QTM_BEGIN_NAMESPACE
   109 QTM_BEGIN_NAMESPACE
    55 
   110 
    56 static QHash<QString, QVariant> properties;
   111 static QHash<QString, QVariant> properties;
    57 
   112 
   603         opened = true;
   658         opened = true;
   604 
   659 
   605         if ((activeConfig.state() & QNetworkConfiguration::Active) != QNetworkConfiguration::Active) {
   660         if ((activeConfig.state() & QNetworkConfiguration::Active) != QNetworkConfiguration::Active) {
   606             state = QNetworkSession::Connecting;
   661             state = QNetworkSession::Connecting;
   607             emit q->stateChanged(state);
   662             emit q->stateChanged(state);
   608 
   663             QTimer::singleShot(0, this, SLOT(do_open()));
   609 	    QTimer::singleShot(0, this, SLOT(do_open()));
   664             return;
   610 	    return;
       
   611         }
   665         }
   612 
       
   613         isOpen = (activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active;
   666         isOpen = (activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active;
   614         if (isOpen)
   667         if (isOpen)
   615             emit quitPendingWaitsForOpened();
   668             emit quitPendingWaitsForOpened();
   616     } else {
   669     } else {
   617 	/* We seem to be active so inform caller */
   670         /* We seem to be active so inform caller */
   618 	emit quitPendingWaitsForOpened();
   671         emit quitPendingWaitsForOpened();
   619     }
   672     }
   620 }
   673 }
   621 
       
   622 
   674 
   623 void QNetworkSessionPrivate::do_open()
   675 void QNetworkSessionPrivate::do_open()
   624 {
   676 {
   625     icd_connection_flags flags = connectFlags;
   677     icd_connection_flags flags = connectFlags;
   626     bool st;
       
   627     QString result;
       
   628     QString iap = publicConfig.identifier();
   678     QString iap = publicConfig.identifier();
   629 
   679 
   630     if (state == QNetworkSession::Connected) {
   680     if (state == QNetworkSession::Connected) {
   631 #ifdef BEARER_MANAGEMENT_DEBUG
   681 #ifdef BEARER_MANAGEMENT_DEBUG
   632 	qDebug() << "Already connected to" << activeConfig.identifier();
   682         qDebug() << "Already connected to" << activeConfig.identifier();
   633 #endif
   683 #endif
   634         emit q->stateChanged(QNetworkSession::Connected);
   684         emit q->stateChanged(QNetworkSession::Connected);
   635 	emit quitPendingWaitsForOpened();
   685         emit quitPendingWaitsForOpened();
   636 	return;
   686 	return;
   637     }
   687     }
   638 
   688 
   639     Maemo::IcdConnectResult connect_result;
       
   640     Maemo::Icd icd(ICD_LONG_CONNECT_TIMEOUT);
       
   641     QNetworkConfiguration config;
       
   642     if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   689     if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   643 	config = activeConfig;
   690         config = activeConfig;
   644     else
   691     else
   645 	config = publicConfig;
   692         config = publicConfig;
   646 
   693 
   647     if (iap == OSSO_IAP_ANY) {
   694     if (iap == OSSO_IAP_ANY) {
   648 #ifdef BEARER_MANAGEMENT_DEBUG
   695 #ifdef BEARER_MANAGEMENT_DEBUG
   649 	qDebug() << "connecting to default IAP" << iap;
   696         qDebug() << "connecting to default IAP" << iap;
   650 #endif
   697 #endif
   651 	st = icd.connect(flags, connect_result);
   698         m_connectRequestTimer.start(ICD_LONG_CONNECT_TIMEOUT);
   652 
   699         m_dbusInterface->asyncCall(ICD_DBUS_API_CONNECT_REQ, (uint)flags); // Return value ignored
       
   700         m_asynchCallActive = true;
   653     } else {
   701     } else {
   654 
   702         ICd2DetailsDBusStruct icd2;
   655 	QList<Maemo::ConnectParams> params;
   703         icd2.serviceType = config.d->service_type;
   656 	Maemo::ConnectParams param;
   704         icd2.serviceAttributes = config.d->service_attrs;
   657 	param.connect.service_type = config.d->service_type;
   705         icd2.setviceId = config.d->service_id;
   658 	param.connect.service_attrs = config.d->service_attrs;
   706         icd2.networkType = config.d->iap_type;
   659 	param.connect.service_id = config.d->service_id;
   707         icd2.networkAttributes = config.d->network_attrs;
   660 	param.connect.network_type = config.d->iap_type;
   708         if (config.d->network_attrs & ICD_NW_ATTR_IAPNAME) {
   661 	param.connect.network_attrs = config.d->network_attrs;
   709             icd2.networkId  = QByteArray(iap.toLatin1());
   662 	if (config.d->network_attrs & ICD_NW_ATTR_IAPNAME)
   710         } else {
   663 	    param.connect.network_id = QByteArray(iap.toLatin1());
   711             icd2.networkId  = config.d->network_id;
   664 	else
   712         }
   665 	    param.connect.network_id = config.d->network_id;
   713 
   666 	params.append(param);
   714 #ifdef BEARER_MANAGEMENT_DEBUG
   667 
   715     qDebug("connecting to %s/%s/0x%x/%s/0x%x/%s",
   668 #ifdef BEARER_MANAGEMENT_DEBUG
   716         icd2.networkId.data(),
   669 	qDebug("connecting to %s/%s/0x%x/%s/0x%x/%s",
   717         icd2.networkType.toAscii().constData(),
   670 	    param.connect.network_id.data(),
   718         icd2.networkAttributes,
   671 	    param.connect.network_type.toAscii().constData(),
   719         icd2.serviceType.toAscii().constData(),
   672 	    param.connect.network_attrs,
   720         icd2.serviceAttributes,
   673 	    param.connect.service_type.toAscii().constData(),
   721         icd2.setviceId.toAscii().constData());
   674 	    param.connect.service_attrs,
   722 #endif
   675 	    param.connect.service_id.toAscii().constData());
   723 
   676 #endif
   724         ICd2DetailsList paramArray;
   677 	st = icd.connect(flags, params, connect_result);
   725         paramArray.append(icd2);
   678     }
   726         m_connectRequestTimer.start(ICD_LONG_CONNECT_TIMEOUT);
   679 
   727         m_dbusInterface->asyncCall(ICD_DBUS_API_CONNECT_REQ, (uint)flags, QVariant::fromValue(paramArray)); // Return value ignored
   680     if (st) {
   728         m_asynchCallActive = true;
   681 	result = connect_result.connect.network_id.data();
   729     }
   682 	QString connected_iap = result;
   730 }
   683 
   731 
   684 	if (connected_iap.isEmpty()) {
   732  void QNetworkSessionPrivate::stateChange(const QDBusMessage& rep)
   685 #ifdef BEARER_MANAGEMENT_DEBUG
   733 {
   686 	    qDebug() << "connect to"<< iap << "failed, result is empty";
   734      if (m_asynchCallActive == true) {
   687 #endif
   735         if (m_connectRequestTimer.isActive())
   688 	    updateState(QNetworkSession::Disconnected);
   736             m_connectRequestTimer.stop();
   689 	    emit q->error(QNetworkSession::InvalidConfigurationError);
   737         m_asynchCallActive = false;
   690 	    if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   738 
   691                 copyConfig(publicConfig, activeConfig);
   739         QString result = rep.arguments().at(5).toString(); // network id or empty string
   692 	    return;
   740         QString connected_iap = result;
   693 	}
   741         if (connected_iap.isEmpty()) {
   694 
   742 #ifdef BEARER_MANAGEMENT_DEBUG
   695 	/* If the user tried to connect to some specific connection (foo)
   743             qDebug() << "connect to"<< publicConfig.identifier() << "failed, result is empty";
   696 	 * and we were already connected to some other connection (bar),
   744 #endif
   697 	 * then we cannot activate this session although icd has a valid
   745             updateState(QNetworkSession::Disconnected);
   698 	 * connection to somewhere.
   746             emit q->error(QNetworkSession::InvalidConfigurationError);
   699 	 */
   747             if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   700 	if ((publicConfig.type() != QNetworkConfiguration::UserChoice) &&
   748                     copyConfig(publicConfig, activeConfig);
   701 	    (connected_iap != config.identifier())) {
   749             return;
   702 	    updateState(QNetworkSession::Disconnected);
   750         }
   703 	    emit q->error(QNetworkSession::InvalidConfigurationError);
   751 
   704 	    return;
   752          /* If the user tried to connect to some specific connection (foo)
   705 	}
   753          * and we were already connected to some other connection (bar),
   706 
   754          * then we cannot activate this session although icd has a valid
   707 
   755          * connection to somewhere.
   708 	/* Did we connect to non saved IAP? */
   756          */
   709 	if (!(config.d->network_attrs & ICD_NW_ATTR_IAPNAME)) {
   757         if ((publicConfig.type() != QNetworkConfiguration::UserChoice) &&
   710 	    /* Because the connection succeeded, the IAP is now known.
   758             (connected_iap != config.identifier())) {
   711 	     */
   759             updateState(QNetworkSession::Disconnected);
   712 	    config.d->network_attrs |= ICD_NW_ATTR_IAPNAME;
   760             emit q->error(QNetworkSession::InvalidConfigurationError);
   713 	    config.d->id = connected_iap;
   761             return;
   714 	}
   762         }
   715 
   763 
   716 	/* User might have changed the IAP name when a new IAP was saved */
   764         /* Did we connect to non saved IAP? */
   717 	Maemo::IAPConf iap_name(config.d->id);
   765         if (!(config.d->network_attrs & ICD_NW_ATTR_IAPNAME)) {
   718 	QString name = iap_name.value("name").toString();
   766             /* Because the connection succeeded, the IAP is now known.
   719 	if (!name.isEmpty())
   767              */
   720 	    config.d->name = name;
   768             config.d->network_attrs |= ICD_NW_ATTR_IAPNAME;
   721 
   769             config.d->id = connected_iap;
   722         config.d->iap_type = connect_result.connect.network_type;
   770         }
   723 
   771 
   724 	config.d->isValid = true;
   772         /* User might have changed the IAP name when a new IAP was saved */
   725 	config.d->state = QNetworkConfiguration::Active;
   773         Maemo::IAPConf iap_name(config.d->id);
   726 	config.d->type = QNetworkConfiguration::InternetAccessPoint;
   774         QString name = iap_name.value("name").toString();
   727 
   775         if (!name.isEmpty())
   728 	startTime = QDateTime::currentDateTime();
   776             config.d->name = name;
   729 	updateState(QNetworkSession::Connected);
   777 
   730 
   778         config.d->iap_type = rep.arguments().at(3).toString(); // connect_result.connect.network_type;
   731 	currentNetworkInterface = get_network_interface();
   779         config.d->isValid = true;
   732 
   780         config.d->state = QNetworkConfiguration::Active;
   733 #ifdef BEARER_MANAGEMENT_DEBUG
   781         config.d->type = QNetworkConfiguration::InternetAccessPoint;
   734 	qDebug() << "connected to" << result << config.d->name << "at" << currentNetworkInterface;
   782 
   735 #endif
   783         startTime = QDateTime::currentDateTime();
   736 
   784         updateState(QNetworkSession::Connected);
   737 	/* We first check if the configuration already exists in the manager
   785         //currentNetworkInterface = get_network_interface();
   738 	 * and if it is not found there, we then insert it. Note that this
   786 #ifdef BEARER_MANAGEMENT_DEBUG
   739 	 * is only done for user choice config only because it can be missing
   787         //qDebug() << "connected to" << result << config.d->name << "at" << currentNetworkInterface;
   740 	 * from config manager list.
   788 #endif
   741 	 */
   789 
   742 
   790         /* We first check if the configuration already exists in the manager
   743 	if (publicConfig.d->type == QNetworkConfiguration::UserChoice) {
   791          * and if it is not found there, we then insert it. Note that this
   744 
   792          * is only done for user choice config only because it can be missing
       
   793          * from config manager list.
       
   794          */
       
   795         if (publicConfig.d->type == QNetworkConfiguration::UserChoice) {
   745 #ifdef BEARER_MANAGEMENT_DEBUG
   796 #ifdef BEARER_MANAGEMENT_DEBUG
   746 #if 0
   797 #if 0
   747 	    QList<QNetworkConfiguration> configs;
   798             QList<QNetworkConfiguration> configs;
   748 	    QNetworkConfigurationManagerPrivate *conPriv = (QNetworkConfigurationManagerPrivate*)config.d.data()->manager;
   799             QNetworkConfigurationManagerPrivate *conPriv = (QNetworkConfigurationManagerPrivate*)config.d.data()->manager;
   749 	    QList<QString> cpsIdents = conPriv->accessPointConfigurations.keys();
   800             QList<QString> cpsIdents = conPriv->accessPointConfigurations.keys();
   750 	    foreach( QString ii, cpsIdents) {
   801             foreach( QString ii, cpsIdents) {
   751 		QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> p = 
   802                 QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> p =
   752 		    conPriv->accessPointConfigurations.value(ii);
   803                         conPriv->accessPointConfigurations.value(ii);
   753 		QNetworkConfiguration pt;
   804                 QNetworkConfiguration pt;
   754 		pt.d = conPriv->accessPointConfigurations.value(ii);
   805                 pt.d = conPriv->accessPointConfigurations.value(ii);
   755 		configs << pt;
   806                 configs << pt;
   756 	    }
   807             }
   757 
   808 
   758 	    int all = configs.count();
   809             int all = configs.count();
   759 	    qDebug() << "All configurations:" << all;
   810             qDebug() << "All configurations:" << all;
   760 	    foreach(QNetworkConfiguration p, configs) {
   811             foreach(QNetworkConfiguration p, configs) {
   761 		qDebug() << p.name() <<":  isvalid->" <<p.isValid() << " type->"<< p.type() << 
   812                 qDebug() << p.name() <<":  isvalid->" <<p.isValid() << " type->"<< p.type() <<
   762 		    " roaming->" << p.isRoamingAvailable() << "identifier->" << p.identifier() <<
   813                         " roaming->" << p.isRoamingAvailable() << "identifier->" << p.identifier() <<
   763 		    " purpose->" << p.purpose() << " state->" << p.state();
   814                         " purpose->" << p.purpose() << " state->" << p.state();
   764 	    }
   815             }
   765 #endif
   816 #endif
   766 #endif
   817 #endif
   767             QNetworkConfigurationManagerPrivate *mgr = (QNetworkConfigurationManagerPrivate*)publicConfig.d.data()->manager;
   818             QNetworkConfigurationManagerPrivate *mgr = (QNetworkConfigurationManagerPrivate*)publicConfig.d.data()->manager;
   768             if (!mgr->accessPointConfigurations.contains(result)) {
   819             if (!mgr->accessPointConfigurations.contains(result)) {
   769 		QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> ptr = config.d;
   820                 QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> ptr = config.d;
   770 		mgr->accessPointConfigurations.insert(result, ptr);
   821                 mgr->accessPointConfigurations.insert(result, ptr);
   771 
   822 
   772 		QNetworkConfiguration item;
   823                 QNetworkConfiguration item;
   773 		item.d = ptr;
   824                 item.d = ptr;
   774 		emit mgr->configurationAdded(item);
   825                 emit mgr->configurationAdded(item);
   775 
   826 
   776 #ifdef BEARER_MANAGEMENT_DEBUG
   827 #ifdef BEARER_MANAGEMENT_DEBUG
   777 		//qDebug()<<"New configuration"<<result<<"added to manager in open";
   828             qDebug()<<"New configuration"<<result<<"added to manager in open";
   778 #endif
   829 #endif
   779 
   830 
   780 	    } else {
   831             } else {
   781                 QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> priv = mgr->accessPointConfigurations.value(result);
   832                 QExplicitlySharedDataPointer<QNetworkConfigurationPrivate> priv = mgr->accessPointConfigurations.value(result);
   782 		QNetworkConfiguration reference;
   833                 QNetworkConfiguration reference;
   783 		reference.d = priv;
   834                 reference.d = priv;
   784                 copyConfig(config, reference, false);
   835                 copyConfig(config, reference, false);
   785                 reference.d.data()->id = result; // Note: Id was not copied in copyConfig() function
   836                 reference.d.data()->id = result; // Note: Id was not copied in copyConfig() function
   786                 config = reference;
   837                 config = reference;
   787                 activeConfig = reference;
   838                 activeConfig = reference;
   788 
   839 
   789 #ifdef BEARER_MANAGEMENT_DEBUG
   840 #ifdef BEARER_MANAGEMENT_DEBUG
   790 		//qDebug()<<"Existing configuration"<<result<<"updated in manager in open";
   841                 qDebug()<<"Existing configuration"<<result<<"updated in manager in open";
   791 #endif
   842 #endif
   792 	    }
   843             }
   793 	}
   844         }
   794 
   845 
   795 	emit quitPendingWaitsForOpened();
   846         emit quitPendingWaitsForOpened();
   796 
   847     }
   797     } else {
   848 }
   798 #ifdef BEARER_MANAGEMENT_DEBUG
   849 
   799 	qDebug() << "connect to"<< iap << "failed, status:" << connect_result.status;
   850 void QNetworkSessionPrivate::connectTimeout()
   800 #endif
   851 {
   801 	updateState(QNetworkSession::Disconnected);
   852     updateState(QNetworkSession::Disconnected);
   802 	if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   853     if (publicConfig.type() == QNetworkConfiguration::UserChoice)
   803             copyConfig(publicConfig, activeConfig);
   854             copyConfig(publicConfig, activeConfig);
   804 	emit q->error(QNetworkSession::UnknownSessionError);
   855         emit q->error(QNetworkSession::UnknownSessionError);
   805     }
       
   806 }
   856 }
   807 
   857 
   808 void QNetworkSessionPrivate::close()
   858 void QNetworkSessionPrivate::close()
   809 {
   859 {
       
   860     if (m_connectRequestTimer.isActive())
       
   861         m_connectRequestTimer.stop();
       
   862 
   810     if (serviceConfig.isValid()) {
   863     if (serviceConfig.isValid()) {
   811         lastError = QNetworkSession::OperationNotSupportedError;
   864         lastError = QNetworkSession::OperationNotSupportedError;
   812         emit q->error(lastError);
   865         emit q->error(lastError);
   813     } else if (isOpen) {
   866     } else if (isOpen) {
   814         opened = false;
   867         opened = false;
   818 }
   871 }
   819 
   872 
   820 
   873 
   821 void QNetworkSessionPrivate::stop()
   874 void QNetworkSessionPrivate::stop()
   822 {
   875 {
       
   876     if (m_connectRequestTimer.isActive())
       
   877         m_connectRequestTimer.stop();
       
   878 
   823     if (serviceConfig.isValid()) {
   879     if (serviceConfig.isValid()) {
   824         lastError = QNetworkSession::OperationNotSupportedError;
   880         lastError = QNetworkSession::OperationNotSupportedError;
   825         emit q->error(lastError);
   881         emit q->error(lastError);
   826     } else {
   882     } else {
   827         if ((activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {
   883         if ((activeConfig.state() & QNetworkConfiguration::Active) == QNetworkConfiguration::Active) {