wlanapitest/wlanhaitest/wlan/src/T_RSocketServData.cpp
branchGCC_SURGE
changeset 37 1ff9bf2737cf
parent 29 d1f0fe5eccbe
parent 36 1c425781161e
equal deleted inserted replaced
29:d1f0fe5eccbe 37:1ff9bf2737cf
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include "t_rsocketservdata.h"
       
    21 #include <commdb.h>				
       
    22 #include <wdbifwlansettings.h>			
       
    23 #include <apselect.h>     
       
    24 #include <aplistitem.h>
       
    25 #include <apdatahandler.h>
       
    26 #include <apaccesspointitem.h>
       
    27 /*@{*/
       
    28 //LIT's params fron the ini file
       
    29 _LIT(KWlanIap,							"WLANIAP");
       
    30 _LIT(KWlanSsid,							"DEFAULT_SSID");
       
    31 _LIT(KCommsDbTableView,     			"commsdbtableview");
       
    32 _LIT(KCommsDatabase,     			    "commsdatabase");
       
    33 /*@}*/
       
    34 
       
    35 /*@{*/
       
    36 //LIT's commands
       
    37 _LIT(KCmdSetOutgoingIap,				"SetOutgoingIap");
       
    38 _LIT(KCmdConnect,						"Connect");
       
    39 _LIT(KCmdClose,							"Close");
       
    40 /*@}*/
       
    41 
       
    42 
       
    43 /**
       
    44  * Two phase constructor
       
    45  *
       
    46  * @leave	system wide error
       
    47  */
       
    48 CT_RSocketServData* CT_RSocketServData::NewL()
       
    49 	{
       
    50 	CT_RSocketServData * ret = new (ELeave) CT_RSocketServData();
       
    51 	CleanupStack::PushL(ret);
       
    52 	ret->ConstructL();
       
    53 	CleanupStack::Pop(ret);
       
    54 	return ret;
       
    55 	}
       
    56 
       
    57 /*
       
    58  * public destructor
       
    59  */
       
    60 CT_RSocketServData::~CT_RSocketServData()
       
    61 	{
       
    62 	if(iSocketServConnected)
       
    63 		{
       
    64 		Close();
       
    65 		}
       
    66 	if(iSocketServ)
       
    67 		{
       
    68 		 delete iSocketServ;
       
    69 		 iSocketServ = NULL;
       
    70 		}
       
    71 	}
       
    72 
       
    73 /**
       
    74  * Private constructor. First phase construction
       
    75  */
       
    76 CT_RSocketServData::CT_RSocketServData()
       
    77 :	iSocketServ(NULL),
       
    78 	iSocketServConnected(EFalse),
       
    79 	iIapID(0)
       
    80 	{
       
    81 	}
       
    82 
       
    83 /**
       
    84  * Second phase construction
       
    85  *
       
    86  * @internalComponent
       
    87  *
       
    88  * @return	N/A
       
    89  *
       
    90  * @pre		None
       
    91  * @post	None
       
    92  *
       
    93  * @leave	system wide error
       
    94  */
       
    95 void CT_RSocketServData::ConstructL()
       
    96 	{
       
    97 	iSocketServ = new (ELeave)RSocketServ();
       
    98 	}
       
    99 
       
   100 
       
   101 /**
       
   102  * Return a pointer to the object that the data wraps
       
   103  *
       
   104  * @return	pointer to the object that the data wraps
       
   105  */
       
   106 TAny* CT_RSocketServData::GetObject()
       
   107 	{
       
   108 	return iSocketServ;
       
   109 	}
       
   110 
       
   111 void CT_RSocketServData::SetIapID(TUint32 aIapID)
       
   112 	{
       
   113 	iIapID = aIapID;
       
   114 	}
       
   115 
       
   116 /**
       
   117  * Process a command read from the Ini file
       
   118  * @param aCommand 			The command to process
       
   119  * @param aSection			The section get from the *.ini file of the project T_Wlan
       
   120  * @param aAsyncErrorIndex	Command index dor async calls to returns errors to
       
   121  * @return TBool			ETrue if the command is process
       
   122  * @leave					system wide error
       
   123  */
       
   124 TBool CT_RSocketServData::DoCommandL(const TTEFFunction& aCommand, const TTEFSectionName& aSection, const TInt /*aAsyncErrorIndex*/)
       
   125 	{
       
   126 	TBool ret = ETrue;
       
   127 	
       
   128 	if(aCommand == KCmdSetOutgoingIap)
       
   129 		{
       
   130 		DoCmdSetOutgoingIap(aSection);
       
   131 		}
       
   132 	else if(aCommand == KCmdConnect)
       
   133 		{
       
   134 		DoCmdConnect();
       
   135 		}
       
   136 	else if(aCommand == KCmdClose)
       
   137 		{
       
   138 		DoCmdClose();
       
   139 		}
       
   140 	else
       
   141 		{
       
   142 		ERR_PRINTF1(_L("Unknown command."));
       
   143 		ret = EFalse;
       
   144 		}
       
   145 	return ret;
       
   146 	}
       
   147 
       
   148 
       
   149 /**
       
   150  * Get IAP, matching the name given (KWlanIap parameter read from the ini file). Set SSID of the
       
   151  * IAP to the given value (KWlanSsid parameter read from the ini file).
       
   152  * Store the ID of the IAP locally to allow using the IAP for connecting.
       
   153  * If there are errors, are management for SetBlockResult() and SetError()
       
   154  * @param aSection				Section to review in the ini file for this command
       
   155  * @return void
       
   156  */
       
   157 void CT_RSocketServData::DoCmdSetOutgoingIap(const TTEFSectionName& aSection)
       
   158 	{
       
   159 	INFO_PRINTF1(_L("*START* CT_RSocketServData::DoCmdSetOutgoingIap"));
       
   160 	TBool dataOk = ETrue;
       
   161 	
       
   162     TPtrC aIapName;
       
   163 	if(!GetStringFromConfig(aSection, KWlanIap, aIapName))
       
   164 		{
       
   165         ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KWlanIap);        
       
   166         SetBlockResult(EFail);
       
   167         dataOk = EFalse;
       
   168 		}
       
   169 	
       
   170     TPtrC aSsid;    
       
   171 	if(!GetStringFromConfig(aSection, KWlanSsid, aSsid))
       
   172 		{
       
   173         ERR_PRINTF2(_L("Error in getting parameter %S from INI file"), &KWlanSsid);
       
   174         SetBlockResult(EFail);
       
   175         dataOk = EFalse;
       
   176 		}	
       
   177 	
       
   178     TPtrC commsdbtableName;
       
   179 	if(!GetStringFromConfig(aSection, KCommsDbTableView, commsdbtableName))
       
   180 		{
       
   181         ERR_PRINTF2(_L("Error in getting parameter %S from INI file"),&KCommsDbTableView);
       
   182         SetBlockResult(EFail);
       
   183         dataOk = EFalse;
       
   184 		}
       
   185 	
       
   186     TPtrC commsdbName;
       
   187 	if(!GetStringFromConfig(aSection, KCommsDatabase, commsdbName))
       
   188 		{
       
   189         ERR_PRINTF2(_L("Error in getting parameter %S from INI file"),&KCommsDatabase);
       
   190         SetBlockResult(EFail);
       
   191         dataOk = EFalse;
       
   192 		}
       
   193 	
       
   194     if (dataOk)
       
   195 		{
       
   196 		// reset IAP id
       
   197 		SetIapID(0);	
       
   198 		
       
   199         TBool apFound = EFalse;
       
   200 		// Open view to IAP table, select all outgoing IAPs.	
       
   201 		CCommsDbTableView* searchView = static_cast<CCommsDbTableView*>(GetDataObjectL(commsdbtableName));
       
   202         CCommsDatabase* commsDatabase =  static_cast<CCommsDatabase*>(GetDataObjectL(commsdbName));
       
   203 		
       
   204 		// Make sure the view was available.
       
   205 		if( searchView != NULL && commsDatabase != NULL )
       
   206 			{
       
   207 			CleanupStack::PushL(searchView);
       
   208 			INFO_PRINTF1(_L("Start browsing through the IAPs."));
       
   209 			TInt error = searchView->GotoFirstRecord();
       
   210 			
       
   211 			if( error == KErrNone )
       
   212 				{
       
   213 				// Buffer for reading IAP names from CommsDat. Buffer size is set to
       
   214 				// maximum readable value from CommsDat.
       
   215 				TBuf<KCommsDbSvrMaxColumnNameLength> iapName;
       
   216 				TUint32 iapID = 0;
       
   217 				INFO_PRINTF1(_L("CT_RSocketServData: CommsDat ready for searching, going through all outgoing IAPs"));
       
   218 				TUint32 iapservice = 0;
       
   219 				
       
   220 				TBool failed = EFalse;
       
   221 				
       
   222 				// Go through all IAPs.
       
   223 				while( error == KErrNone )
       
   224 					{
       
   225 					iapName.FillZ();
       
   226 					
       
   227 					// Read IAP ID and name from IAP table in CommsDat.
       
   228 					TRAPD(err, searchView->ReadTextL( TPtrC( COMMDB_NAME ), iapName ));
       
   229 					
       
   230 				    if(err == KErrNone)
       
   231 				    	{
       
   232 						TRAP(err, searchView->ReadUintL( TPtrC( COMMDB_ID ), iapID ));
       
   233 						
       
   234 					    if(err == KErrNone)
       
   235 					    	{
       
   236 							INFO_PRINTF3(_L("CT_RSocketServData: IAP (ID = [%d]): %S"), iapID, &iapName );
       
   237 							
       
   238 							// Try to match the name with user input in the ini file.
       
   239 							if( iapName.Match( aIapName ) == KErrNone )
       
   240 								{
       
   241 								INFO_PRINTF2(_L("CT_RSocketServData: Matching IAP name found with IAP ID = [%d]"), iapID );
       
   242 								
       
   243                                 apFound = ETrue;
       
   244                                 
       
   245 								// Return the found IAP ID			
       
   246 								SetIapID(iapID);
       
   247 								
       
   248 								// Read IAP service from IAP table in CommsDat.
       
   249 								TRAPD(err, searchView->ReadUintL( TPtrC( IAP_SERVICE ), iapservice ));
       
   250 								
       
   251 							    if(err == KErrNone)
       
   252 							    	{
       
   253 									INFO_PRINTF2(_L("Service of the AP: %d"),iapservice);
       
   254 									
       
   255 									// Write the ssid given as a parameter in WLANServiceTable in CommsDat
       
   256 									INFO_PRINTF1(_L("CT_RSocketServData: Get WlanSettings from WLANServiceTable"));
       
   257 									CWLanSettings* wlanset = new (ELeave) CWLanSettings();
       
   258 									CleanupStack::PushL( wlanset );
       
   259 									
       
   260 									// Connect to CommsDat
       
   261 									err = wlanset->Connect();
       
   262 									
       
   263 									if( err == KErrNone )
       
   264 										{
       
   265 										// Get wlan settings corresponding IAP service info from IAP table
       
   266 										SWLANSettings wlanSettings;
       
   267 										err = wlanset->GetWlanSettings( iapservice, wlanSettings );
       
   268 										
       
   269 										if( err == KErrNone )
       
   270 											{
       
   271 											INFO_PRINTF2(_L("CT_RSocketServData: CommsDat: wlanSettings.Name = %S"), &wlanSettings.Name );
       
   272 											INFO_PRINTF2(_L("CT_RSocketServData: CommsDat: wlanSettings.SSID = %S"), &wlanSettings.SSID );								
       
   273 											
       
   274 											// Set the new ssid from the ini file
       
   275 											wlanSettings.SSID = aSsid;
       
   276 											INFO_PRINTF2(_L("CT_RSocketServData: New value for wlanSettings.SSID = %S"), &wlanSettings.SSID );
       
   277 											
       
   278 											// Write the new settings in CommsDat
       
   279 											err = wlanset->WriteWlanSettings(wlanSettings );
       
   280 											
       
   281 											if( err == KErrNone )
       
   282 												{
       
   283 												INFO_PRINTF1(_L("CT_RSocketServData: WlanSettings saved in CommsDat"));
       
   284 												wlanset->Disconnect();
       
   285 												CleanupStack::PopAndDestroy( wlanset );
       
   286 												}
       
   287 											else
       
   288 												{
       
   289 												ERR_PRINTF2(_L("CT_RSocketServData: WriteWlanSettings error: [%d]"), err );
       
   290 												SetError(err);
       
   291 												failed = ETrue;
       
   292 												break;
       
   293 												}
       
   294 											}
       
   295 										else
       
   296 											{
       
   297 											ERR_PRINTF2(_L("CT_RSocketServData: Get WlanSettings error: [%d]"), err );			
       
   298 											SetError(err);
       
   299 											failed = ETrue;
       
   300 											break;
       
   301 											}
       
   302 										}
       
   303 									else
       
   304 										{
       
   305 										ERR_PRINTF2(_L("CT_RSocketServData: WLanSettings connect failed! [%d]"), err );				
       
   306 										SetError(err);
       
   307 										failed = ETrue;
       
   308 										break;
       
   309 										}
       
   310 							    	}
       
   311 							    else
       
   312 									{
       
   313 									ERR_PRINTF2(_L("searchView->ReadUintL left with error %d"), err);
       
   314 									SetError(err);
       
   315 									failed = ETrue;
       
   316 									break;
       
   317 									}	
       
   318 								}
       
   319 							
       
   320 							error = searchView->GotoNextRecord();
       
   321 							if(error == KErrNotFound)
       
   322 								{
       
   323 								INFO_PRINTF2(_L("searchView->GotoNextRecord() not found [%d]"), error);
       
   324 								INFO_PRINTF1(_L("No more records to look for"));
       
   325 								}
       
   326 							else if(error != KErrNone)
       
   327 								{
       
   328 								ERR_PRINTF2(_L("searchView->GotoNextRecord() Failed with error = %d"),error);
       
   329 								SetError(err);
       
   330 								failed = ETrue;
       
   331 								break;
       
   332 								}
       
   333 					    	}
       
   334 					    else
       
   335 							{
       
   336 							ERR_PRINTF2(_L("searchView->ReadUintL left with error %d"), err);
       
   337 							SetError(err);
       
   338 							failed = ETrue;
       
   339 							break;
       
   340 							}	
       
   341 				    	}
       
   342 				    else
       
   343 						{
       
   344 						ERR_PRINTF2(_L("searchView->ReadTextL left with error %d"), err);
       
   345 						SetError(err);
       
   346 						failed = ETrue;
       
   347 						break;
       
   348 						}
       
   349 					}	
       
   350 				
       
   351 				CleanupStack::Pop( searchView );
       
   352 				
       
   353 				//if( !failed && GetIapID() == 0 )
       
   354 				//	{
       
   355 				//	ERR_PRINTF1(_L("No valid IAP found"));
       
   356 				//	SetBlockResult(EFail);
       
   357 				//	}
       
   358 				}
       
   359 			else
       
   360 				{
       
   361 				INFO_PRINTF2(_L("CT_RSocketServData: No IAPs found [%d]"), error );		
       
   362 				}
       
   363                 
       
   364             if(apFound == EFalse)
       
   365                 {
       
   366                 CApAccessPointItem *wlan = CApAccessPointItem::NewLC();
       
   367                 wlan->SetNamesL(aIapName);
       
   368                 wlan->SetBearerTypeL(EApBearerTypeWLAN);
       
   369                 wlan->WriteTextL(EApWlanNetworkName, aSsid);
       
   370                 CApDataHandler *handler = CApDataHandler::NewLC(*commsDatabase);
       
   371                 TUint32 apId = handler->CreateFromDataL(*wlan);
       
   372                 INFO_PRINTF4(_L("Add new IAP ID: %d, name:%S, SSID: %S"), apId,&aIapName,&aSsid);
       
   373                 SetIapID(apId);
       
   374                 CleanupStack::PopAndDestroy(2);
       
   375                 }
       
   376 			}
       
   377 		else
       
   378 			{
       
   379 			ERR_PRINTF1(_L("CT_RSocketServData: No IAPs found"));
       
   380 			ERR_PRINTF1(_L("CommsDat, no view and database were available."));
       
   381 			SetBlockResult(EFail);
       
   382 			}	
       
   383 		}
       
   384 	
       
   385 	INFO_PRINTF1(_L("*END* CT_RSocketServData::DoCmdSetOutgoingIap"));
       
   386 	}
       
   387 
       
   388 /**
       
   389  * Command to calls RSocketServ::Connect. The error is management for SetError() helper
       
   390  * @param
       
   391  * @return
       
   392  */
       
   393 void CT_RSocketServData::DoCmdConnect()
       
   394 	{
       
   395 	INFO_PRINTF1(_L("*START* CT_RSocketServData::DoCmdConnect"));
       
   396 	
       
   397 	TInt err = iSocketServ->Connect();	
       
   398 	if(err != KErrNone)
       
   399  		{
       
   400  		ERR_PRINTF1(_L("iSocketServ->Connect() Fail"));
       
   401 		SetError(err);
       
   402  		}
       
   403 	else
       
   404 		{
       
   405 		iSocketServConnected = ETrue;
       
   406 		}
       
   407 	
       
   408 	INFO_PRINTF1(_L("*END* CT_RSocketServData::DoCmdConnect"));
       
   409 	}
       
   410 /**
       
   411  * Command to close RSocketServ instance
       
   412  * @param
       
   413  * @return
       
   414  */
       
   415 void CT_RSocketServData::DoCmdClose()
       
   416 	{
       
   417 	INFO_PRINTF1(_L("*START* CT_RSocketServData::DoCmdClose"));
       
   418 	Close();
       
   419 	INFO_PRINTF1(_L("*END* CT_RSocketServData::DoCmdClose"));
       
   420 	}
       
   421 
       
   422 /**
       
   423  * Helper for the command DoCmdCloseSocketServ: RSocketServ::Close
       
   424  * @param
       
   425  * @return
       
   426  */
       
   427 void CT_RSocketServData::Close()
       
   428 	{
       
   429 	iSocketServ->Close();
       
   430  	iSocketServConnected = EFalse; 		
       
   431 	}