bearermanagement/mpm/src/mpmcommsdataccess.cpp
changeset 20 9c97ad6591ae
parent 3 f7816ffc66ed
child 32 5c4486441ae6
equal deleted inserted replaced
18:fcbbe021d614 20:9c97ad6591ae
   232 
   232 
   233                 MPMLOGSSID( "CMPMCommsDatAccess<0x%x>::ValidateIapL: SSID = %S", 
   233                 MPMLOGSSID( "CMPMCommsDatAccess<0x%x>::ValidateIapL: SSID = %S", 
   234                     aSession.ConnectionId(), &ssid )
   234                     aSession.ConnectionId(), &ssid )
   235 
   235 
   236                 TUint32 presumedIapId = aIapId; 
   236                 TUint32 presumedIapId = aIapId; 
   237 
       
   238                 // Find and change Iap and Net Id if SSID exist
       
   239                 //
       
   240                 if ( !CheckEasyWLanL( aIapId ) )
       
   241                     {
       
   242                     aSession.MyServer().Events()->FindSSID( ssid, aIapId, aNetId );
       
   243                     }
       
   244 
   237 
   245                 // Store Iap Id, Net Id and empty APN to TConnInfo, 
   238                 // Store Iap Id, Net Id and empty APN to TConnInfo, 
   246                 // so that FindAPN can utilise the information 
   239                 // so that FindAPN can utilise the information 
   247                 // during the validation of next IAP to support 
   240                 // during the validation of next IAP to support 
   248                 // using existing WLAN connection SSID.
   241                 // using existing WLAN connection SSID.
   411         }
   404         }
   412 
   405 
   413     CleanupStack::PopAndDestroy(buf1);
   406     CleanupStack::PopAndDestroy(buf1);
   414     CleanupStack::PopAndDestroy(commsDbIapTableView);    
   407     CleanupStack::PopAndDestroy(commsDbIapTableView);    
   415     CleanupStack::PopAndDestroy(commsDatabase);
   408     CleanupStack::PopAndDestroy(commsDatabase);
   416     }
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CMPMCommsDatAccess::CheckEasyWLanL
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 TBool CMPMCommsDatAccess::CheckEasyWLanL( TUint32 aIapId ) const
       
   423     {
       
   424     MPMLOGSTRING2( "CMPMCommsDatAccess::CheckEasyWLanL: IAP Id = %i", aIapId)
       
   425 
       
   426     TBool easyWlan = EFalse;
       
   427     CCommsDatabase* commsDatabase = CCommsDatabase::NewL();
       
   428     CleanupStack::PushL( commsDatabase );
       
   429 
       
   430     // Make hidden records visible
       
   431     // 
       
   432     commsDatabase->ShowHiddenRecords();
       
   433 
       
   434     CCommsDbTableView* commsDbIapTableView = 
       
   435         commsDatabase->OpenViewMatchingUintLC( TPtrC( IAP ),
       
   436                                                TPtrC( COMMDB_ID ),
       
   437                                                aIapId );
       
   438 
       
   439     User::LeaveIfError( commsDbIapTableView->GotoFirstRecord() );
       
   440 
       
   441     TInt length1( 0 );
       
   442 
       
   443     commsDbIapTableView->ReadColumnLengthL( TPtrC( IAP_SERVICE_TYPE ), 
       
   444                                             length1 );
       
   445 
       
   446     HBufC* buf1 = HBufC::NewLC( length1 );
       
   447     TPtr ptr1( buf1->Des() );
       
   448 
       
   449     // Read IAP's service type
       
   450     //
       
   451     commsDbIapTableView->ReadTextL(TPtrC( IAP_SERVICE_TYPE ), ptr1 );
       
   452     MPMLOGSTRING2(
       
   453         "CMPMCommsDatAccess::CheckEasyWLanL: IAP service type = %S", &ptr1 )
       
   454 
       
   455     if ( ptr1.Compare( TPtrC( LAN_SERVICE ) ) == KErrNone )
       
   456         {
       
   457         TUint32 service( 0 );
       
   458         commsDbIapTableView->ReadUintL( TPtrC( IAP_SERVICE ), service );
       
   459 
       
   460         // Now open a view to LAN_SERVICE table
       
   461         //
       
   462         CCommsDbTableView* commsDbLanTableView = 
       
   463             commsDatabase->OpenViewMatchingUintLC( TPtrC( LAN_SERVICE ),
       
   464                                                    TPtrC( COMMDB_ID ),
       
   465                                                    service );
       
   466 
       
   467         User::LeaveIfError( commsDbLanTableView->GotoFirstRecord() );
       
   468 
       
   469         TUint32 commDbId( 0 );
       
   470 
       
   471         commsDbLanTableView->ReadUintL( TPtrC( COMMDB_ID ), commDbId );
       
   472 
       
   473         // Now open a view to WLAN_SERVICE table
       
   474         //
       
   475         // If non wlan product, wlan tables won't exist and this will 
       
   476         // leave. In that case do not check WLAN related issues 
       
   477         //
       
   478         CCommsDbTableView* commsDbWlanTableView( NULL );
       
   479         TRAPD( err2, commsDbWlanTableView =
       
   480                      commsDatabase->OpenViewMatchingUintLC( TPtrC( WLAN_SERVICE ),
       
   481                                                             TPtrC( WLAN_SERVICE_ID ),
       
   482                                                             commDbId );
       
   483                      CleanupStack::Pop( commsDbWlanTableView ) );
       
   484         if ( err2 == KErrNone )
       
   485             {
       
   486             // Push back to cleanup stack. Object must be popped within 
       
   487             // TRAP, to avoid unbalance in cleanup stack and panic 71
       
   488             CleanupStack::PushL( commsDbWlanTableView );
       
   489 
       
   490             TInt err = commsDbWlanTableView->GotoFirstRecord();
       
   491 
       
   492             if ( err == KErrNone )
       
   493                 {
       
   494                 TInt ssidLength( 0 );
       
   495                 commsDbWlanTableView->ReadColumnLengthL( TPtrC( NU_WLAN_SSID ), 
       
   496                                                                 ssidLength );
       
   497                 if ( ssidLength == 0 )
       
   498                     {
       
   499                     MPMLOGSTRING(
       
   500                         "CMPMCommsDatAccess::CheckEasyWLanL: IAP is Easy WLAN" )
       
   501                     easyWlan = ETrue;
       
   502                     }
       
   503                 else
       
   504                     {
       
   505                     MPMLOGSTRING(
       
   506                         "CMPMCommsDatAccess::CheckEasyWLanL: IAP isn't Easy WLAN" )
       
   507                     easyWlan = EFalse;
       
   508                     }
       
   509                 }
       
   510             else
       
   511                 {
       
   512                 MPMLOGSTRING2( "CMPMCommsDatAccess::CheckEasyWLanL\
       
   513  GotoFirstRecord returned %d", err )
       
   514                 }
       
   515 
       
   516             // Release memory
       
   517             //
       
   518             CleanupStack::PopAndDestroy( commsDbWlanTableView );
       
   519             }
       
   520         else
       
   521             {
       
   522             MPMLOGSTRING2(
       
   523                         "CMPMCommsDatAccess::CheckEasyWLanL: WLAN table view failed, err %d", err2 )
       
   524             }    
       
   525         CleanupStack::PopAndDestroy( commsDbLanTableView );
       
   526         }
       
   527 
       
   528     // Release memory
       
   529     //
       
   530     CleanupStack::PopAndDestroy( buf1 );
       
   531     CleanupStack::PopAndDestroy( commsDbIapTableView );
       
   532     CleanupStack::PopAndDestroy( commsDatabase );
       
   533 
       
   534     return easyWlan;
       
   535     }
   409     }
   536 
   410 
   537 // -----------------------------------------------------------------------------
   411 // -----------------------------------------------------------------------------
   538 // CMPMCommsDatAccess::DumpIAPsL
   412 // CMPMCommsDatAccess::DumpIAPsL
   539 // -----------------------------------------------------------------------------
   413 // -----------------------------------------------------------------------------
  1032         }
   906         }
  1033 
   907 
  1034     CleanupStack::PopAndDestroy( table );
   908     CleanupStack::PopAndDestroy( table );
  1035     CleanupStack::PopAndDestroy( commsDatabase );
   909     CleanupStack::PopAndDestroy( commsDatabase );
  1036     }
   910     }
  1037         
       
  1038 // -----------------------------------------------------------------------------
       
  1039 // CMPMCommsDatAccess::CheckWLANIapWithSsidL
       
  1040 // -----------------------------------------------------------------------------
       
  1041 //
       
  1042 
       
  1043 TUint32 CMPMCommsDatAccess::CheckWLANIapWithSsidL( TWlanSsid& aSsid, 
       
  1044                                                    TUint32 aSecMode,
       
  1045                                                    TWlanNetMode aConnMode )
       
  1046     {
       
  1047     MPMLOGSTRING( "CMPMCommsDatAccess::CheckWLANIapWithSsidL" )
       
  1048     TUint iapId( 0 );
       
  1049 
       
  1050     // Creating a session with the latest version
       
  1051     //     
       
  1052     CMDBSession* db = CMDBSession::NewLC( KCDVersion1_1 );
       
  1053 
       
  1054     db->SetAttributeMask( ECDHidden | ECDProtectedWrite );
       
  1055 
       
  1056     // Find Wlan service record
       
  1057     //
       
  1058     TBuf<KWlanMaxSsidLength> ssid16;
       
  1059     CnvUtfConverter::ConvertToUnicodeFromUtf8( ssid16, aSsid );
       
  1060     MPMLOGSTRING4( "CMPMCommsDatAccess::CheckWLANIapWithSsidL sec:%d, conn:%d ssid %S", 
       
  1061                    aSecMode, 
       
  1062                    aConnMode, 
       
  1063                    &ssid16 )
       
  1064     CMDBRecordSet<CCDWlanServiceRecord>* wlanSet = 
       
  1065         new ( ELeave ) CMDBRecordSet<CCDWlanServiceRecord>( 
       
  1066          CCDWlanServiceRecord::TableIdL( *db ) );
       
  1067     CleanupStack::PushL( wlanSet ); 
       
  1068 
       
  1069     CCDWlanServiceRecord* wlanRecord = new ( ELeave ) 
       
  1070         CCDWlanServiceRecord( CCDWlanServiceRecord::TableIdL( *db ) );
       
  1071 
       
  1072     CleanupStack::PushL( wlanRecord ); 
       
  1073     wlanSet->LoadL( *db );
       
  1074     
       
  1075     MPMLOGSTRING2( "CMPMCommsDatAccess::CheckWLANIapWithSsidL, record count:%d", 
       
  1076                    wlanSet->iRecords.Count() )
       
  1077     TBool matchFound( EFalse );
       
  1078     for (TInt i = 0; ( i < wlanSet->iRecords.Count() ) && !matchFound; i++ )
       
  1079         {
       
  1080         wlanRecord->SetElementId( GetRealElementId( wlanSet->iRecords[i] ) );
       
  1081         wlanRecord->LoadL( *db );
       
  1082         TBuf<KWlanMaxSsidLength> ssid;
       
  1083         ssid = wlanRecord->iWLanSSID;
       
  1084         //if not easywlan, continue
       
  1085         //
       
  1086         if( ssid.Compare( KNullDesC() ) )
       
  1087             {
       
  1088             ssid = wlanRecord->iWLanSSID.GetL();
       
  1089             TUint32 secMode = wlanRecord->iWlanSecMode;
       
  1090             TUint32 connMode = wlanRecord->iWlanConnMode;
       
  1091         
       
  1092             if ( connMode )
       
  1093                 {
       
  1094                 connMode = 1;    
       
  1095                 }
       
  1096             MPMLOGSTRING3( "CMPMCommsDatAccess::CheckWLANIapWithSsidL CommsDat secmode:%d, connmode:%d ", 
       
  1097         				   secMode, 
       
  1098         				   connMode )
       
  1099 
       
  1100             if ( !ssid.Compare( ssid16 ) && ( aConnMode == connMode ) )
       
  1101         	    {
       
  1102         	    //compare sec mode
       
  1103         	    if ( aSecMode == EWlanSecModeOpen || aSecMode == EWlanSecModeWep )
       
  1104         		    {
       
  1105         		    if ( aSecMode == secMode )
       
  1106         		        {
       
  1107         		        MPMLOGSTRING( "CMPMCommsDatAccess::CheckWLANIapWithSsidL match found, open/wep " )
       
  1108         		        matchFound = ETrue;
       
  1109         		        }
       
  1110         			    
       
  1111         		    }
       
  1112             	//WPA PSK case
       
  1113             	else if ( aSecMode == EWlanSecModeWpa2 )
       
  1114             		{
       
  1115         	    	if ( ( secMode > EWlanSecModeWep ) && (wlanRecord->iWlanEnableWpaPsk) )
       
  1116         	    	    {
       
  1117         	    	    MPMLOGSTRING( "CMPMCommsDatAccess::CheckWLANIapWithSsidL match found, wpa psk " )
       
  1118         	    	    matchFound = ETrue;
       
  1119         	    	    }
       
  1120             		}
       
  1121             	//aSecMode = EWlanSecModeWpa
       
  1122             	else
       
  1123             		{
       
  1124         	    	if ( secMode > EWlanSecModeWep )
       
  1125         	    	    {
       
  1126         	    	    MPMLOGSTRING( "CMPMCommsDatAccess::CheckWLANIapWithSsidL match found, wpa " )
       
  1127         	    	    matchFound = ETrue;
       
  1128         	    	    }
       
  1129             		}
       
  1130         			
       
  1131         		}
       
  1132         	}
       
  1133         }
       
  1134     
       
  1135     if ( matchFound )
       
  1136         {
       
  1137         // load Iap record
       
  1138         //
       
  1139         CCDIAPRecord* iapRecord = static_cast<CCDIAPRecord*> ( 
       
  1140             CCDRecordBase::RecordFactoryL( KCDTIdIAPRecord ) );
       
  1141         CleanupStack::PushL( iapRecord );
       
  1142 
       
  1143         CCDLANServiceRecord* lanRecord = static_cast<CCDLANServiceRecord*> (
       
  1144             CCDRecordBase::RecordFactoryL( KCDTIdLANServiceRecord ));
       
  1145         CleanupStack::PushL( lanRecord );
       
  1146         
       
  1147         iapRecord->iServiceType.SetL( TPtrC( KCDTypeNameLANService ) );
       
  1148         
       
  1149         lanRecord->SetRecordId( wlanRecord->iWlanServiceId );
       
  1150         lanRecord->LoadL( *db );
       
  1151 
       
  1152         iapRecord->iService = lanRecord->ElementId();
       
  1153 
       
  1154 /*        iapRecord->iServiceType.SetL( TPtrC( KCDTypeNameLANService ) );
       
  1155         iapRecord->iService = wlanRecord->iWlanServiceId;
       
  1156 */
       
  1157         if( iapRecord->FindL( *db ) )
       
  1158             {
       
  1159             MPMLOGSTRING( "CMPMCommsDatAccess::CheckWLANIapWithSsidL iap found" )
       
  1160             // Iap record found
       
  1161             //
       
  1162             iapId = iapRecord->RecordId();
       
  1163             }
       
  1164         CleanupStack::PopAndDestroy( lanRecord );
       
  1165         CleanupStack::PopAndDestroy( iapRecord );
       
  1166         }
       
  1167     CleanupStack::PopAndDestroy( wlanRecord );
       
  1168     CleanupStack::PopAndDestroy( wlanSet );
       
  1169     CleanupStack::PopAndDestroy( db );
       
  1170     
       
  1171     return iapId;
       
  1172     }
       
  1173 
       
  1174 // -----------------------------------------------------------------------------
       
  1175 // CMPMCommsDatAccess::SetEasyWlanDataL
       
  1176 // -----------------------------------------------------------------------------
       
  1177 //
       
  1178 void CMPMCommsDatAccess::SetEasyWlanDataL( const TWlanSsid& aSsid,
       
  1179                                            TUint            aSecMode,
       
  1180                                            TWlanNetMode     aConnMode,
       
  1181                                            TWepKeyData      aWepData,
       
  1182                                            TUint            aEnableWpaPsk,
       
  1183                                            const TDesC8&    aWpaPsk,
       
  1184                                            TUint            aWpaKeyLen )
       
  1185     {
       
  1186     MPMLOGSTRING( "CMPMCommsDatAccess::SetEasyWlanDataL" )
       
  1187     CMDBSession* dbSession = CMDBSession::NewL( KCDVersion1_1 );
       
  1188     CleanupStack::PushL(dbSession);
       
  1189 
       
  1190     // Try to open a transaction to obtain write lock
       
  1191     dbSession->OpenTransactionL();
       
  1192 
       
  1193     TUint32 wlanId = FindEasyWlanServiceIdL( dbSession );
       
  1194     if( !wlanId )
       
  1195         {
       
  1196         MPMLOGSTRING( "CMPMCommsDatAccess::SetEasyWlanDataL unable to store \
       
  1197  easy wlan settings" )
       
  1198         dbSession->Close();		
       
  1199 		CleanupStack::PopAndDestroy( dbSession );
       
  1200         return;
       
  1201         }
       
  1202     
       
  1203     CMDBGenericRecord* record = static_cast<CMDBGenericRecord*> 
       
  1204 				    ( CCDRecordBase::RecordFactoryL( 0));
       
  1205     CleanupStack::PushL( record );
       
  1206     record->InitializeL( TPtrC( WLAN_SERVICE ),NULL );
       
  1207     record->SetRecordId( wlanId );
       
  1208     record->LoadL( *dbSession );
       
  1209 
       
  1210         TBuf<KWlanMaxSsidLength> ssid16;
       
  1211         CnvUtfConverter::ConvertToUnicodeFromUtf8( ssid16, aSsid );
       
  1212         CMDBField<TDesC>* textField = (CMDBField<TDesC>*)record->GetFieldByIdL(KCDTIdWlanUsedSSID);
       
  1213         textField->SetMaxLengthL( ssid16.Length());
       
  1214         textField->SetL( ssid16 );
       
  1215     
       
  1216         *((CMDBField<TUint32>*)record->GetFieldByIdL(KCDTIdWlanSecMode)) = aSecMode;
       
  1217         *((CMDBField<TUint32>*)record->GetFieldByIdL(KCDTIdWlanConnMode)) = aConnMode;
       
  1218     
       
  1219         if (aEnableWpaPsk)
       
  1220     	    {
       
  1221     	    MPMLOGSTRING( "CMPMCommsDatAccess::SetEasyWlanDataL wpa psk" )
       
  1222     	    CMDBField<TDesC8>* binField = (CMDBField<TDesC8>*)
       
  1223 		   									record->GetFieldByIdL(KCDTIdWlanWpaPreSharedKey);
       
  1224 		    binField->SetMaxLengthL( aWpaKeyLen );
       
  1225 		    binField->SetL( aWpaPsk );
       
  1226 
       
  1227 		    *((CMDBField<TUint32>*)record->GetFieldByIdL(KCDTIdWlanWpaKeyLength)) = aWpaKeyLen;
       
  1228 		    *((CMDBField<TUint32>*)record->GetFieldByIdL(KCDTIdWlanEnableWpaPsk)) = aEnableWpaPsk;
       
  1229     	    }
       
  1230         else if (aSecMode == EWlanSecModeWep)
       
  1231     	    {
       
  1232     	    MPMLOGSTRING( "CMPMCommsDatAccess::SetEasyWlanDataL wep psk" )
       
  1233      	    *((CMDBField<TUint32>*)record->GetFieldByIdL( KCDTIdWlanWepIndex )) = 
       
  1234      	        aWepData.iDefaultWep;
       
  1235  
       
  1236     	    SetWepKeyL( KCDTIdWlanFormatKey1, KCDTIdWlanWepKey1, 
       
  1237     	                aWepData.iWep1, aWepData.iWepFormat1, record );
       
  1238             SetWepKeyL( KCDTIdWlanFormatKey2, KCDTIdWlanWepKey2, 
       
  1239                         aWepData.iWep2, aWepData.iWepFormat2, record );
       
  1240             SetWepKeyL( KCDTIdWlanFormatKey3, KCDTIdWlanWepKey3, 
       
  1241                         aWepData.iWep3, aWepData.iWepFormat3, record );
       
  1242             SetWepKeyL( KCDTIdWlanFormatKey4, KCDTIdWlanWepKey4, 
       
  1243                         aWepData.iWep4, aWepData.iWepFormat4, record );
       
  1244     	    }
       
  1245     	else
       
  1246     	    {
       
  1247     	    MPMLOGSTRING2( "CMPMCommsDatAccess::SetEasyWlanDataL sec mode %d, no key data written",
       
  1248     	         aSecMode )
       
  1249     	    }
       
  1250     
       
  1251         //Finally save all to commsdat.
       
  1252         record->ModifyL( *dbSession );
       
  1253         dbSession->CommitTransactionL();
       
  1254         dbSession->Close();
       
  1255         CleanupStack::PopAndDestroy( record );
       
  1256         CleanupStack::PopAndDestroy( dbSession );
       
  1257     }
       
  1258 
       
  1259 // -----------------------------------------------------------------------------
       
  1260 // CMPMCommsDatAccess::SetWepKeyL
       
  1261 // -----------------------------------------------------------------------------
       
  1262 //
       
  1263 void CMPMCommsDatAccess::SetWepKeyL( TMDBElementId       aFormatId,
       
  1264                                      TMDBElementId       aKeyId, 
       
  1265                                      TWlanWepKey         aWepKey,
       
  1266                                      TUint               aWepFormat,
       
  1267                                      CMDBGenericRecord*  aRecord )
       
  1268     {
       
  1269     CMDBField<TUint32>* formatField = static_cast<CMDBField<TUint32>*>
       
  1270             ( aRecord->GetFieldByIdL( aFormatId ) );
       
  1271     formatField->SetL( aWepFormat );
       
  1272     CMDBField<TDesC8>* wepKey = static_cast<CMDBField<TDesC8>*>
       
  1273             ( aRecord->GetFieldByIdL( aKeyId ) );
       
  1274     if ( aWepFormat )
       
  1275         {
       
  1276         wepKey->SetL( aWepKey );
       
  1277         }
       
  1278     else
       
  1279         {
       
  1280         //Convert Ascii to Hex format, as only Hex value to be stored in CommsDat
       
  1281         HBufC8* buf8Conv = HBufC8::NewLC( aWepKey.Length() * KMpmDoubleLength );
       
  1282         ConvertAsciiToHex( aWepKey, buf8Conv );
       
  1283         wepKey->SetL( buf8Conv->Des() );
       
  1284         CleanupStack::PopAndDestroy( buf8Conv );
       
  1285         }
       
  1286     }
       
  1287 
       
  1288 // -----------------------------------------------------------------------------
       
  1289 // CMPMCommsDatAccess::ConvertAsciiToHex
       
  1290 // -----------------------------------------------------------------------------
       
  1291 //
       
  1292 void CMPMCommsDatAccess::ConvertAsciiToHex( const TDesC8& aSource, 
       
  1293         							        HBufC8* aDest )
       
  1294     {
       
  1295     MPMLOGSTRING( "CMPMCommsDatAccess::ConvertAsciiToHex" )
       
  1296     _LIT( hex, "0123456789ABCDEF" );
       
  1297     TInt size = aSource.Size();
       
  1298     TPtr8 ptr = aDest->Des();
       
  1299     for ( TInt ii = 0; ii < size; ii++ )
       
  1300     	{
       
  1301     	TText8 ch = aSource[ii];
       
  1302     	ptr.Append( hex()[(ch/16)&0x0f] );
       
  1303     	ptr.Append( hex()[ch&0x0f] );
       
  1304     	}
       
  1305     }
       
  1306 
       
  1307 // -----------------------------------------------------------------------------
       
  1308 // CMPMCommsDatAccess::FindEasyWlanServiceIdL
       
  1309 // -----------------------------------------------------------------------------
       
  1310 //
       
  1311 TUint32 CMPMCommsDatAccess::FindEasyWlanServiceIdL( CMDBSession* aDb )
       
  1312     {
       
  1313     // if the method returns the record container there is for 
       
  1314     // some reason garbage data in the key values
       
  1315     //
       
  1316     TUint32 wlanId( 0 );
       
  1317     MPMLOGSTRING( "CMPMCommsDatAccess::FindEasyWlanServiceIdL" )    
       
  1318 
       
  1319     CMDBRecordSet<CCDWlanServiceRecord>* wlanSet = 
       
  1320         new ( ELeave ) CMDBRecordSet<CCDWlanServiceRecord>( 
       
  1321          CCDWlanServiceRecord::TableIdL( *aDb ) );
       
  1322     CleanupStack::PushL( wlanSet ); 
       
  1323     wlanSet->LoadL( *aDb );
       
  1324     
       
  1325     TBool found( EFalse );
       
  1326     for ( TInt i = 0; i < wlanSet->iRecords.Count() && !found; i++ )
       
  1327         {
       
  1328         CCDWlanServiceRecord* wlanRecord = new ( ELeave ) 
       
  1329             CCDWlanServiceRecord( CCDWlanServiceRecord::TableIdL( *aDb ) );
       
  1330         CleanupStack::PushL( wlanRecord );
       
  1331 
       
  1332         wlanRecord->SetElementId( GetRealElementId( wlanSet->iRecords[i] ) );
       
  1333         wlanRecord->LoadL( *aDb );
       
  1334         
       
  1335         TDesC ssid = wlanRecord->iWLanSSID;
       
  1336         if( !ssid.Compare( KNullDesC() ) )
       
  1337             {
       
  1338             MPMLOGSTRING2( "CMPMCommsDatAccess::FindEasyWlanServiceIdL: \
       
  1339 found service entry %i", wlanRecord->RecordId() )
       
  1340             wlanId = wlanRecord->RecordId();
       
  1341             found = ETrue;
       
  1342             }
       
  1343         CleanupStack::PopAndDestroy( wlanRecord );
       
  1344         }
       
  1345     CleanupStack::PopAndDestroy( wlanSet );
       
  1346     return wlanId;
       
  1347     }
       
  1348 
   911 
  1349 // -----------------------------------------------------------------------------
   912 // -----------------------------------------------------------------------------
  1350 // CMPMCommsDatAccess::CheckHiddenAgentL
   913 // CMPMCommsDatAccess::CheckHiddenAgentL
  1351 // -----------------------------------------------------------------------------
   914 // -----------------------------------------------------------------------------
  1352 //
   915 //
  1751                 MPMLOGSTRING( "CMPMCommsDatAccess::CheckWlanL: IAP is WLAN" )
  1314                 MPMLOGSTRING( "CMPMCommsDatAccess::CheckWlanL: IAP is WLAN" )
  1752                 wlan = EWlanIap;
  1315                 wlan = EWlanIap;
  1753                 TInt ssidLength( 0 );
  1316                 TInt ssidLength( 0 );
  1754                 commsDbWlanTableView->ReadColumnLengthL( TPtrC( NU_WLAN_SSID ), 
  1317                 commsDbWlanTableView->ReadColumnLengthL( TPtrC( NU_WLAN_SSID ), 
  1755                                                      ssidLength );
  1318                                                      ssidLength );
  1756                 if ( ssidLength == 0 )
       
  1757                     {
       
  1758                     MPMLOGSTRING(
       
  1759                         "CMPMCommsDatAccess::CheckWlanL: IAP is Easy WLAN" )
       
  1760                     wlan = EEasyWlanIap;
       
  1761                     }
       
  1762                 }
  1319                 }
  1763             else
  1320             else
  1764                 {
  1321                 {
  1765                 MPMLOGSTRING2( "CMPMCommsDatAccess::CheckWlanL\
  1322                 MPMLOGSTRING2( "CMPMCommsDatAccess::CheckWlanL\
  1766  GotoFirstRecord returned %d", err )
  1323  GotoFirstRecord returned %d", err )
  2323             MPMLOGSTRING(
  1880             MPMLOGSTRING(
  2324                 "CMPMCommsDatAccess::FindRealIapL: Call ChooseBestIAPL" )
  1881                 "CMPMCommsDatAccess::FindRealIapL: Call ChooseBestIAPL" )
  2325 
  1882 
  2326             TMpmConnPref tempMpmConnPref;
  1883             TMpmConnPref tempMpmConnPref;
  2327             tempMpmConnPref.SetSnapId( snap );
  1884             tempMpmConnPref.SetSnapId( snap );
  2328             aSession.IapSelectionL()->ChooseBestIAPL( tempMpmConnPref, aAvailableIAPList );
  1885 
       
  1886             // Ensure that there will be no infinite loops by dropping
       
  1887             // this virtual iap out from the list of available iaps.
       
  1888             // E.g. next layer SNAP could be the same as current SNAP.
       
  1889             RAvailableIAPList tempList;
       
  1890             CleanupClosePushL( tempList );
       
  1891               
       
  1892             for( TInt i=0; i < aAvailableIAPList.Count(); i++ )
       
  1893                 {
       
  1894                 // drop this virtual iap out from the list of
       
  1895                 // available iaps to prevent from infinitely looping
       
  1896                 //	
       
  1897                 if ( aAvailableIAPList[i] != aVirtualIapId )
       
  1898                     {
       
  1899                     tempList.Append( aAvailableIAPList[i] );
       
  1900                     }
       
  1901                 }
       
  1902             aSession.IapSelectionL()->ChooseBestIAPL( tempMpmConnPref, tempList );
       
  1903             CleanupStack::PopAndDestroy( &tempList );
       
  1904 
  2329             aRealIapId = tempMpmConnPref.IapId();
  1905             aRealIapId = tempMpmConnPref.IapId();
  2330             }
  1906             }
  2331         else if ( virtualNext->iNextLayerIAP != 0 )
  1907         else if ( virtualNext->iNextLayerIAP != 0 )
  2332             {
  1908             {
  2333             // IAP
  1909             // IAP
  2342                     "CMPMCommsDatAccess::FindRealIapL: Virtual IAP: %i", 
  1918                     "CMPMCommsDatAccess::FindRealIapL: Virtual IAP: %i", 
  2343                     iap2->RecordId() )
  1919                     iap2->RecordId() )
  2344                 MPMLOGSTRING(
  1920                 MPMLOGSTRING(
  2345                     "CMPMCommsDatAccess::FindRealIapL: Call FindRealIapL" )
  1921                     "CMPMCommsDatAccess::FindRealIapL: Call FindRealIapL" )
  2346 
  1922 
  2347                 FindRealIapL( iap2->RecordId(), 
  1923                 if ( aVirtualIapId != virtualNext->iNextLayerIAP )
  2348                               aRealIapId, 
  1924                     {
  2349                               aAvailableIAPList,
  1925                     FindRealIapL( iap2->RecordId(), 
  2350                               aSession );
  1926                                   aRealIapId, 
       
  1927                                   aAvailableIAPList,
       
  1928                                   aSession );
       
  1929                     }
       
  1930                 else
       
  1931                     {
       
  1932                     aRealIapId = 0;
       
  1933                     MPMLOGSTRING( "CMPMCommsDatAccess::FindRealIapL: Virtual iap points to itself" )
       
  1934                     }
  2351                 }
  1935                 }
  2352             else
  1936             else
  2353                 {
  1937                 {
  2354                 aRealIapId = iap2->RecordId();
  1938                 aRealIapId = iap2->RecordId();
  2355 
  1939 
  2666     
  2250     
  2667     CleanupStack::PopAndDestroy( ptrLanBearerRecordSet );    
  2251     CleanupStack::PopAndDestroy( ptrLanBearerRecordSet );    
  2668     }
  2252     }
  2669 
  2253 
  2670 // -----------------------------------------------------------------------------
  2254 // -----------------------------------------------------------------------------
  2671 // CMPMCommsDatAccess::AreActiveIapsInSameSnapL
       
  2672 // -----------------------------------------------------------------------------
       
  2673 //
       
  2674 TBool CMPMCommsDatAccess::AreActiveIapsInSameSnapL ( RArray<TActiveBMConn>& aActiveBMConns,
       
  2675                                                      TUint32& aSnapId )
       
  2676     {
       
  2677     MPMLOGSTRING( "CMPMCommsDatAccess::AreActiveIapsInSameSnapL" )
       
  2678     TBool same = ETrue;
       
  2679     TUint32 snapId = 0;
       
  2680     TUint32 prevSnapId = 0;
       
  2681             
       
  2682     // Go through all active connections
       
  2683     for (TInt index = 0; index < aActiveBMConns.Count(); index++ )
       
  2684         {
       
  2685         // Do check only for active connections
       
  2686         if ( aActiveBMConns[index].iConnInfo.iState != EStarted )
       
  2687             {
       
  2688             continue;
       
  2689             }
       
  2690         
       
  2691         // Iap id must be nonzero
       
  2692         if ( aActiveBMConns[index].iConnInfo.iIapId )
       
  2693             {
       
  2694             prevSnapId = snapId;
       
  2695             // Get destination id for the iap
       
  2696             snapId = GetSnapIdL( aActiveBMConns[index].iConnInfo.iIapId );
       
  2697                         
       
  2698             // Check if previous iap's destination is different
       
  2699             if ( ( ( prevSnapId != snapId ) && prevSnapId && snapId ) ||
       
  2700                  !snapId )
       
  2701                 {
       
  2702                 // Set return value to false and leave loop
       
  2703                 same = EFalse;
       
  2704                 break;
       
  2705                 }
       
  2706             }
       
  2707         }
       
  2708     
       
  2709     // Return destId for later use
       
  2710     aSnapId = snapId;
       
  2711     
       
  2712     return same;
       
  2713     }
       
  2714 
       
  2715 // -----------------------------------------------------------------------------
       
  2716 // CMPMCommsDatAccess::SelectActiveConnectionL
       
  2717 // -----------------------------------------------------------------------------
       
  2718 //
       
  2719 void CMPMCommsDatAccess::SelectActiveConnectionL ( const TUint32 aSnapId,
       
  2720                                                    RArray<TActiveBMConn>& aActiveBMConns,
       
  2721                                                    TUint32& aActiveIapId,
       
  2722                                                    TUint32& aActiveSnapId,
       
  2723                                                    TMPMBearerType& aActiveBearerType,
       
  2724                                                    CMPMServerSession& aSession )
       
  2725     {
       
  2726     MPMLOGSTRING( "CMPMCommsDatAccess::SelectActiveConnectionL" )
       
  2727     RArray<TUint32> activeIaps;
       
  2728     RArray<TNetIap> iapIds;
       
  2729     
       
  2730     // Reset output parameters
       
  2731     aActiveIapId = 0;
       
  2732     aActiveSnapId = 0;
       
  2733     aActiveBearerType = EMPMBearerTypeNone;
       
  2734 
       
  2735     CleanupClosePushL( activeIaps );
       
  2736         
       
  2737     for (TInt index = 0; index < aActiveBMConns.Count(); index++ )
       
  2738         {
       
  2739         CMPMServerSession* serverSession = 
       
  2740             aSession.MyServer().GetServerSession(
       
  2741                 aActiveBMConns[index].iConnInfo.iConnId );
       
  2742 
       
  2743         // Add only started iaps to array
       
  2744         if ( aActiveBMConns[index].iConnInfo.iState == EStarted &&
       
  2745              serverSession->ChooseBestIapCalled() )
       
  2746             {
       
  2747             activeIaps.AppendL ( aActiveBMConns[index].iConnInfo.iIapId );
       
  2748             }
       
  2749         }
       
  2750         
       
  2751     CleanupClosePushL( iapIds );
       
  2752     SearchDNEntriesL( aSnapId, iapIds );
       
  2753     
       
  2754     // Remove any deactived IAP from iapIds
       
  2755     TInt ret        = KErrNotFound;
       
  2756     TInt destCount  = iapIds.Count();
       
  2757     
       
  2758     // Decrement by one, because count is n, 
       
  2759     // but indexes in array are 0 .. n-1.
       
  2760     // 
       
  2761     destCount--;
       
  2762 
       
  2763     // This time we are browsing the array from the end to the beginning, 
       
  2764     // because removing one element from array affects index numbering.
       
  2765     for ( TInt k = destCount; k >= 0; k-- )
       
  2766         {
       
  2767         ret = activeIaps.Find( iapIds[k].iIapId );
       
  2768         if ( ret == KErrNotFound )
       
  2769             {
       
  2770             MPMLOGSTRING2( "CMPMCommsDatAccess::SelectActiveConnectionL: \
       
  2771 Remove deactived IAP = %i", iapIds[k].iIapId )
       
  2772             iapIds.Remove( k );
       
  2773             }
       
  2774         }
       
  2775 
       
  2776     // Determine the actual priorities for IAPs
       
  2777     DeterminePrioritiesL( iapIds, activeIaps, aSession );
       
  2778 
       
  2779     // Check if a matching available IAP is found.
       
  2780     if( iapIds.Count() )
       
  2781         {
       
  2782         // Go through all iaps
       
  2783         for ( TInt index = 0; index < iapIds.Count(); index++ )
       
  2784             {
       
  2785             // Get bearer type
       
  2786             TMPMBearerType bearerType = GetBearerTypeL( iapIds[index].iIapId );
       
  2787         
       
  2788             // If iap is not vpn, wlan or packet then skip it
       
  2789             if ( bearerType == EMPMBearerTypeOther )
       
  2790                 {
       
  2791                 continue;
       
  2792                 }
       
  2793             
       
  2794             // Set bearer type, iap id and snap id
       
  2795             for (TInt index2 = 0; index2 < aActiveBMConns.Count(); index2++ )
       
  2796                 {
       
  2797                 if ( ( aActiveBMConns[index2].iConnInfo.iIapId ==
       
  2798                        iapIds[index].iIapId ) )
       
  2799                     {
       
  2800                     if ( aSession.MyServer().DedicatedClients().Find(
       
  2801                          aActiveBMConns[index2].iConnInfo.iAppUid ) != KErrNone )
       
  2802                         {
       
  2803                         aActiveSnapId = aActiveBMConns[index2].iConnInfo.iSnap;
       
  2804                         aActiveIapId = aActiveBMConns[index2].iConnInfo.iIapId;
       
  2805                         aActiveBearerType = bearerType;
       
  2806                         }
       
  2807                     break;
       
  2808                     }
       
  2809                 }
       
  2810             
       
  2811             break;
       
  2812             }
       
  2813         }
       
  2814 
       
  2815     MPMLOGSTRING4(
       
  2816         "CMPMCommsDatAccess::SelectActiveConnectionL: Iap id = %i, Snap id = %i, \
       
  2817 Bearer type = %i",
       
  2818         aActiveIapId, aActiveSnapId, aActiveBearerType )
       
  2819     
       
  2820     CleanupStack::PopAndDestroy( &iapIds );
       
  2821     CleanupStack::PopAndDestroy( &activeIaps );
       
  2822     }
       
  2823     
       
  2824 // -----------------------------------------------------------------------------
       
  2825 // CMPMCommsDatAccess::GetSnapIdL
       
  2826 // -----------------------------------------------------------------------------
       
  2827 //
       
  2828 TUint32 CMPMCommsDatAccess::GetSnapIdL( TUint32 aIapId )
       
  2829     {
       
  2830     MPMLOGSTRING( "CMPMCommsDatAccess::GetSnapIdL" )
       
  2831     TUint32 snapId = 0;
       
  2832     RArray<TUint> snapIds;
       
  2833         
       
  2834     CleanupClosePushL( snapIds );
       
  2835     // Get destinations
       
  2836     FindAllSnapsL( snapIds );
       
  2837 
       
  2838     for (TInt index = 0; index < snapIds.Count(); index++)
       
  2839         {
       
  2840         RArray<TNetIap> ids;
       
  2841         // Get iaps belonging to this snap
       
  2842         CleanupClosePushL( ids );
       
  2843         SearchDNEntriesL( snapIds[index], ids );
       
  2844             
       
  2845         for (TInt index2 = 0; index2 < ids.Count(); index2++)
       
  2846             {
       
  2847             // Check if iap belongs to this destination
       
  2848             if (ids[index2].iIapId == aIapId)
       
  2849                 {
       
  2850                 snapId = snapIds[index];
       
  2851                 CleanupStack::PopAndDestroy( &ids );                    
       
  2852                 CleanupStack::PopAndDestroy( &snapIds );
       
  2853                 return snapId;
       
  2854                 }
       
  2855             }
       
  2856         
       
  2857         CleanupStack::PopAndDestroy( &ids );
       
  2858         }
       
  2859 
       
  2860     CleanupStack::PopAndDestroy( &snapIds );
       
  2861     
       
  2862     return snapId;
       
  2863     }
       
  2864 
       
  2865 // -----------------------------------------------------------------------------
       
  2866 // CMPMCommsDatAccess::IsSnapEmptyL
  2255 // CMPMCommsDatAccess::IsSnapEmptyL
  2867 // -----------------------------------------------------------------------------
  2256 // -----------------------------------------------------------------------------
  2868 //
  2257 //
  2869 TBool CMPMCommsDatAccess::IsSnapEmptyL( TUint32 aSnapId )
  2258 TBool CMPMCommsDatAccess::IsSnapEmptyL( TUint32 aSnapId )
  2870     {
  2259     {