isolationserver/isoserver/src/isoconnectionmanager.c
changeset 10 59927b2d3b75
parent 0 d0f3a028347a
equal deleted inserted replaced
0:d0f3a028347a 10:59927b2d3b75
     1 /*
       
     2 * ============================================================================
       
     3 *  Name        : isoconnectionmanager.c
       
     4 *  Part of     : isolation server.
       
     5 *  Version     : %version: 24 %
       
     6 *
       
     7 *  Copyright © 2007-2008 Nokia.  All rights reserved.
       
     8 *  All rights reserved.
       
     9 *  Redistribution and use in source and binary forms, with or without modification, 
       
    10 *  are permitted provided that the following conditions are met:
       
    11 *  Redistributions of source code must retain the above copyright notice, this list 
       
    12 *  of conditions and the following disclaimer.Redistributions in binary form must 
       
    13 *  reproduce the above copyright notice, this list of conditions and the following 
       
    14 *  disclaimer in the documentation and/or other materials provided with the distribution.
       
    15 *  Neither the name of the Nokia Corporation nor the names of its contributors may be used 
       
    16 *  to endorse or promote products derived from this software without specific prior written 
       
    17 *  permission.
       
    18 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
       
    19 *  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
       
    20 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
       
    21 *  SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
       
    22 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
       
    23 *  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
       
    24 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
       
    25 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
       
    26 *  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    27 * ============================================================================
       
    28 * Template version: 1.0
       
    29 */
       
    30 
       
    31 
       
    32 
       
    33 #include <glib.h>
       
    34 #include <gtypes.h>
       
    35 #include <sys/types.h> 
       
    36 #include <stdlib.h>
       
    37 #include <string.h>
       
    38 #include <stdio.h> 
       
    39 
       
    40 #include "isoconnectionmanager.h"
       
    41 #include "isomodifycontactlist.h" //For roster_members_changed_cb
       
    42 #include "isoutils.h" //for loger and othr func
       
    43 #include "isofetchcontactlist.h" //For request_roster
       
    44 
       
    45 #include "msgliterals.h"
       
    46 #include "isoservermain.h"
       
    47 #include "isopresence.h"
       
    48 #include "isosearch.h"
       
    49 #include "msg_enums.h"
       
    50 
       
    51 enum disconnect_reason
       
    52     {
       
    53     EConnection_Status_Reason_None_Specified = 0,
       
    54     EConnection_Status_Reason_Requested,
       
    55     EConnection_Status_Reason_Network_Error,
       
    56     EConnection_Status_Reason_Authentication_Failed,
       
    57     };
       
    58 //userConnection globalCon;
       
    59 
       
    60 /*!	\file 
       
    61 *	Impliments the functions in isoconnectionmanager.h
       
    62 */
       
    63 
       
    64 /*!	\brief Documented in the header file
       
    65 */
       
    66 static void check_conn_properties(TpConn *conn)
       
    67 {
       
    68     TpPropsIface *pres_iface;
       
    69 
       
    70   	pres_iface = TELEPATHY_PROPS_IFACE (tp_conn_get_interface (
       
    71         						conn, TELEPATHY_CONN_IFACE_PRESENCE_QUARK));
       
    72   	if (pres_iface != NULL)
       
    73   		{
       
    74   		globalCon.pres_iface = pres_iface;
       
    75 	  	dbus_g_proxy_connect_signal(DBUS_G_PROXY(pres_iface),
       
    76 					    "PresenceUpdate",
       
    77 					    G_CALLBACK(telepathy_presence_handler),
       
    78 					    NULL, NULL);
       
    79   		}
       
    80   	else 
       
    81   		{
       
    82     	g_warning ("The connection object does not support " TP_IFACE_PROPERTIES);  
       
    83   		}
       
    84 }
       
    85 
       
    86 /*! \brief This function is registered in action_login for statuschanged signal.
       
    87  *  This function is called by telepathygabble when the status of the user changes
       
    88  *  Once user is connected this function registers for NewChannel signal with telepathygabble 
       
    89  *  client is also informed of the status change..
       
    90  *
       
    91  *  \param proxy
       
    92  *  \param status status of the user ( connected/connecting/disconnected )
       
    93  *  \param reason reason for status change(server disconnected/ user requested etc.,)
       
    94  *  \param user_data  message header request
       
    95  *
       
    96  *  \return : Boolean True on success, false on failure
       
    97  */
       
    98 
       
    99 static gboolean status_changed_cb(DBusGProxy *proxy,
       
   100 				  guint status, guint reason,
       
   101 				  gpointer user_data)
       
   102 	{
       
   103 
       
   104 	
       
   105 	int err = 0;
       
   106 	
       
   107 	message_hdr_req* hdr_req = ( message_hdr_req* )user_data;
       
   108 	// create the msg queue
       
   109 	iso_logger( "%s", "In - status_changed_cb\n" );
       
   110 	
       
   111 	//switch to the connection status
       
   112 	switch ( status ) 
       
   113 		{
       
   114 		case EConnected:
       
   115 			{
       
   116 			//Connected..
       
   117 			iso_logger( "%s", "connected\n" );
       
   118 			//Get connection interfaces
       
   119 			check_conn_properties(TELEPATHY_CONN(proxy));
       
   120 			//Connect to the signal for new channels
       
   121 			dbus_g_proxy_connect_signal( DBUS_G_PROXY( globalCon.conn ), "NewChannel",
       
   122 			      G_CALLBACK( new_channel_handler ),
       
   123 			       NULL, NULL );
       
   124 		   	//send response to client..
       
   125 			err = send_response_to_client( hdr_req, reason, 1 );
       
   126 			//hdr_req would be freed here
       
   127 			//Disconnect the previous signal for connected
       
   128 			//This is to pass the diff msg hdr as cb userdata for disconnect
       
   129 			/*dbus_g_proxy_disconnect_signal( DBUS_G_PROXY( globalCon.conn ), StatusChangedStr,
       
   130 					G_CALLBACK( status_changed_cb ),
       
   131 					hdr_req );*/
       
   132 			
       
   133 			globalCon.conn_stat = connected;
       
   134 			break;
       
   135 			}
       
   136 		case EConnecting:
       
   137 			{//Connecting..
       
   138 			iso_logger( "%s", "connecting\n" );
       
   139 			//already done after creating the thread
       
   140 			//globalCon.conn_stat = connecting;
       
   141 			break;
       
   142 			}
       
   143 		case ENotConnected: 
       
   144 			{
       
   145 			//DisConnected....
       
   146 			//Set the reason for disconnection
       
   147 			iso_logger( "%s : %d : %s : %d", "disconnected and reason is", reason, "state is ", globalCon.conn_stat );
       
   148 			//reset the flag to not connected
       
   149 			
       
   150 			//should here be  not_connected != globalCon.conn_stat  &&  disconnected != globalCon.conn_stat 
       
   151 			//and all login errors should set the conn_stat to disconnected
       
   152 			if ( not_connected != globalCon.conn_stat &&  disconnected != globalCon.conn_stat ) 
       
   153 				{
       
   154 				int success = 1;
       
   155 				int error = 0;
       
   156 				
       
   157 				action_logedout();
       
   158 				
       
   159 				//switch for the reason of logout
       
   160 				switch ( reason )
       
   161 				    {
       
   162 				    //set appropriate message types and error if any
       
   163 				    case EConnection_Status_Reason_None_Specified:
       
   164 				        if ( connecting == globalCon.conn_stat )
       
   165 				            {
       
   166 				            //not yet connected and user has cancelled the a/p
       
   167 				            hdr_req->message_type = ELogin_Request;
       
   168 				            error = ELOGIN_NONE_SPECIFIED;
       
   169 				            success = 0;
       
   170 				            }
       
   171 				        else {
       
   172 				            //connected and user has cancelled the a/p
       
   173                             //state is going to be not connected as fr these cases ossadaptation is 
       
   174                             //going to send handle request terminated signal 
       
   175 				            hdr_req->message_type = EUserEndGprs;
       
   176 				            
       
   177 				            }
       
   178 				        
       
   179 				        break;
       
   180 				    case EConnection_Status_Reason_Requested:
       
   181 				        hdr_req->message_type = ELogout_Request;
       
   182 				        
       
   183 				        break;
       
   184 				    case EConnection_Status_Reason_Network_Error:
       
   185 				        if ( connecting == globalCon.conn_stat )
       
   186                             {
       
   187                             //not yet connected and user has cancelled the a/p
       
   188                             hdr_req->message_type = ELogin_Request;
       
   189                             error = ELOGIN_NETWORK_ERROR;
       
   190                             success = 0;
       
   191                             }
       
   192 				        else {
       
   193                             //state is going to be not connected as fr these cases ossadaptation is 
       
   194 				            //going to send handle request terminated signal
       
   195                             hdr_req->message_type = EServer_DisConnect_Request;   
       
   196                             
       
   197 				        }
       
   198 				        
       
   199 				        break;
       
   200 				    case EConnection_Status_Reason_Authentication_Failed:
       
   201 				        hdr_req->message_type = ELogin_Request;
       
   202 				        error = ELOGIN_AUTHENTICATION;
       
   203 				        success = 0;
       
   204 				        
       
   205 				        break;
       
   206 				    default:
       
   207 				        hdr_req->message_type = ELogin_Request;
       
   208                         error = ELOGIN_AUTHORIZATION;
       
   209                         success = 0;
       
   210                         
       
   211                         break;
       
   212 
       
   213 				    }
       
   214 				//to set to no more requests allowed
       
   215 				globalCon.conn_stat = not_connected;
       
   216 				
       
   217 				err = send_response_to_client( hdr_req, error, success );
       
   218 				
       
   219 				}
       
   220 						
       
   221 			break;
       
   222 			}
       
   223 		default:
       
   224 			{
       
   225 			break;	
       
   226 			}
       
   227 		
       
   228 		}
       
   229 
       
   230 	if ( 0 != err )
       
   231 		{
       
   232 		//failed
       
   233 		return ERROR;	
       
   234 		}
       
   235 	iso_logger( "%s", "out - status_changed_cb\n" );
       
   236 	return TRUE;
       
   237 	}
       
   238 	
       
   239 /*! \brief new_channel_handler is registered as a callback for any new 
       
   240  *  channels craetion request, in function status_changed_cb after status is changed 
       
   241  *  to log in. new_channel_handler creates channel and also registers for the signals
       
   242  *  MembersChanged for contact related channels and Recieved and SendError for IM
       
   243  *  related channels.
       
   244  *
       
   245  *  \param proxy unused
       
   246  *  \param object_path object path of the channel
       
   247  *  \param channel_type used to get a new channel
       
   248  *  \param handle_type used to get a new channel, channel handle type
       
   249  *  \param handle used to get a new channel, channel handle 
       
   250  *  \param suppress_handler unused
       
   251  *  \param user_data unused
       
   252  *
       
   253  *  \return void
       
   254  */
       
   255 static void new_channel_handler( DBusGProxy *proxy, const char *object_path,
       
   256 				const gchar *g_channel_type, guint handle_type,
       
   257 				guint handle, gboolean suppress_handler,
       
   258 				gpointer user_data )
       
   259 	{
       
   260 	
       
   261 	TpChan *new_chan = NULL;
       
   262 	UNUSED_FORMAL_PARAM(proxy);
       
   263 	UNUSED_FORMAL_PARAM(suppress_handler);
       
   264 	UNUSED_FORMAL_PARAM(user_data);
       
   265 	iso_logger( "%s", "In - new_channel_handler\n" );
       
   266 	
       
   267 	//create new channel
       
   268 	new_chan = tp_chan_new( globalCon.dbusConn,
       
   269 	       globalCon.connmgr_bus, object_path,
       
   270 		       g_channel_type, handle_type, handle );
       
   271 
       
   272 	if ( !new_chan ) 
       
   273 		{
       
   274 		return ;
       
   275 		}	
       
   276 		
       
   277 	
       
   278 	if ( strcmp( g_channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST  ) == 0 
       
   279 	 		&& ( g_strrstr(object_path, "RosterChannel/known" )  ) )
       
   280 		{
       
   281 		//channel type is contact rosters..
       
   282 		//request for the roster
       
   283 			
       
   284 		globalCon.group_iface_known =
       
   285 	     	tp_chan_get_interface( new_chan,
       
   286 					      TELEPATHY_CHAN_IFACE_GROUP_QUARK);
       
   287 		//register for the members changed signal			      			      
       
   288 		dbus_g_proxy_connect_signal (globalCon.group_iface_known, "MembersChanged",
       
   289 						     G_CALLBACK (roster_members_changed_cb),
       
   290 						     NULL, NULL);			      		       
       
   291 				       
       
   292 		}
       
   293 	else if ( strcmp( g_channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST  ) == 0 
       
   294 	 		&& ( g_strrstr(object_path, "RosterChannel/publish" )  ) )
       
   295 		{
       
   296 		//channel type is contact rosters..
       
   297 		//request for the roster
       
   298 			
       
   299 		globalCon.group_iface_publish =
       
   300 	     	tp_chan_get_interface( new_chan,
       
   301 					      TELEPATHY_CHAN_IFACE_GROUP_QUARK);
       
   302 		//register for the members changed signal			      			      
       
   303 		dbus_g_proxy_connect_signal (globalCon.group_iface_publish, "MembersChanged",
       
   304 						     G_CALLBACK (roster_members_changed_cb),
       
   305 						     NULL, NULL);
       
   306 					      		       
       
   307 		request_roster( EPublish_Channel );		       
       
   308 		} 		
       
   309 	else if ( strcmp( g_channel_type, TP_IFACE_CHANNEL_TYPE_CONTACT_LIST  ) == 0 
       
   310  		&& (g_strrstr(object_path, "RosterChannel/subscribe") ) )
       
   311  		  
       
   312 		{
       
   313 		//roster fetch should be done using subscribe, as it will give 
       
   314 		//current and remote pending members in differnet arrays, that can be sued on ui side
       
   315 		//local pending members can be fetched only using "RosterChannel/publish",that can be fetched if required
       
   316 		
       
   317 		globalCon.group_iface_subscribe =
       
   318 	     	tp_chan_get_interface( new_chan,
       
   319 					      TELEPATHY_CHAN_IFACE_GROUP_QUARK);
       
   320 					      
       
   321 			
       
   322 		//register for the members changed signal			      			      
       
   323 		dbus_g_proxy_connect_signal (globalCon.group_iface_subscribe, "MembersChanged",
       
   324 						     G_CALLBACK (roster_members_changed_cb),
       
   325 						     NULL, NULL);
       
   326 		request_roster( ESubscribe_Channel );
       
   327 	    				     
       
   328 					       
       
   329 		}
       
   330 				
       
   331 	//recieve message channel handlers
       
   332 	else if ( ( strcmp( g_channel_type, TP_IFACE_CHANNEL_TYPE_TEXT ) == 0 ) )
       
   333 		{
       
   334 		//recieve message 
       
   335 		if ( globalCon.text_channels == NULL )
       
   336 			{
       
   337 			globalCon.text_channels = g_hash_table_new_full( g_str_hash,
       
   338 				              g_str_equal,
       
   339 				             ( GDestroyNotify ) g_free,
       
   340 				             ( GDestroyNotify ) g_object_unref );	
       
   341 			}
       
   342 		if ( globalCon.text_channels ) 
       
   343 			{
       
   344 			g_hash_table_insert( globalCon.text_channels, 
       
   345 					g_strdup( object_path ), new_chan );		
       
   346 			}
       
   347 		
       
   348 		iso_logger( "b4 text_channel_init" );
       
   349 		//check for  new_chan objects for text channel should be loaclly stored/freed
       
   350 		text_channel_init( new_chan );
       
   351 		}
       
   352 	iso_logger( "%s", "Out - new_channel_handler\n" );
       
   353 	}    
       
   354 
       
   355 
       
   356 /*! \brief Is called after logout.. Any cleanup operations to
       
   357  *   be performed here. Setting isConnected to EFalse, cleaning up 
       
   358  *  of resources is done
       
   359  */
       
   360 void action_logedout() 
       
   361 	{
       
   362 	
       
   363 	iso_logger( "%s", "In - action_logedout\n" ); 
       
   364     //not quiting the main loop
       
   365     //release the connection manager
       
   366     if ( globalCon.connmgr ) 
       
   367 	    {
       
   368 	    g_object_unref ( globalCon.connmgr );
       
   369 		globalCon.connmgr = NULL;	
       
   370 	    }
       
   371 	//release the dbus connection
       
   372 	if ( globalCon.dbusConn ) 
       
   373 	    {
       
   374 	    dbus_g_connection_unref ( globalCon.dbusConn );
       
   375 		globalCon.dbusConn = NULL;	
       
   376 	    }
       
   377 	//release the connection object
       
   378 	if ( globalCon.conn ) 
       
   379 	    {
       
   380 	    g_object_unref ( globalCon.conn );
       
   381 		globalCon.conn = NULL;	
       
   382 	    }
       
   383 	//release the text channels
       
   384 	if ( globalCon.text_channels ) 
       
   385 	    {
       
   386 	    g_hash_table_destroy( globalCon.text_channels ); 
       
   387 		globalCon.text_channels = NULL;	
       
   388 	    }
       
   389 	//release the subscribe interface   
       
   390 	if ( globalCon.group_iface_subscribe ) 
       
   391 	    {
       
   392 	    g_object_unref ( globalCon.group_iface_subscribe );
       
   393 		globalCon.group_iface_subscribe = NULL;	
       
   394 	    }
       
   395 	//release the publish interface       
       
   396 	if ( globalCon.group_iface_publish ) 
       
   397 	    {
       
   398 	    g_object_unref ( globalCon.group_iface_publish );
       
   399 		globalCon.group_iface_publish = NULL;	
       
   400 	    }
       
   401 	//release the known interface       
       
   402 	if ( globalCon.group_iface_known ) 
       
   403 	    {
       
   404 	    g_object_unref ( globalCon.group_iface_known );
       
   405 		globalCon.group_iface_known = NULL;	
       
   406 	    }
       
   407 	
       
   408 	iso_logger( "%s", "Out - action_logedout\n" );
       
   409 	}
       
   410 	
       
   411 /*! \brief Callback for the logout 
       
   412  * 
       
   413  *  \param proxy : unused
       
   414  *  \param error : unused
       
   415  *  \param userdata : unused
       
   416  *  \return : void
       
   417  */
       
   418 void logout_cb( DBusGProxy *proxy, GError *error, gpointer userdata )
       
   419 	{
       
   420 
       
   421 	iso_logger( "%s", "In - logout_cb\n" );
       
   422         UNUSED_FORMAL_PARAM(proxy);
       
   423         UNUSED_FORMAL_PARAM(error);
       
   424         UNUSED_FORMAL_PARAM(userdata);
       
   425 	iso_logger( "%s", "Out - logout_cb\n" );
       
   426 	}
       
   427 	
       
   428 /*! \brief requests for closing of session. Also closes the search channel.
       
   429  *  \remark should not close the search channel
       
   430  * 
       
   431  *  Request for log - out
       
   432  *  \param pc - message  which has the request header
       
   433  *  \return : MEM_ALLOCATION_ERROR or 0
       
   434  */
       
   435 int action_logout( char* pc ) 
       
   436 	{
       
   437 	
       
   438 	TpConn* conn = globalCon.conn;
       
   439 	message_hdr_req *msg_hdr = NULL;
       
   440 
       
   441 	iso_logger( "%s", "In - action_logout\n" );
       
   442     
       
   443     if( globalCon.search_chan )
       
   444 	    {
       
   445 	    tp_chan_close_async( DBUS_G_PROXY( globalCon.search_chan ), search_chan_closed_cb, NULL );	
       
   446 	    }
       
   447 
       
   448 	//allocate memory and check for errors
       
   449 	msg_hdr = ( message_hdr_req* ) malloc( sizeof( message_hdr_req ) );
       
   450 	if ( NULL == msg_hdr ) 
       
   451 		{
       
   452 		return MEM_ALLOCATION_ERROR;	
       
   453 		}
       
   454 	//intialize the memory to 0s
       
   455     memset( msg_hdr, '\0', sizeof( message_hdr_req ) );
       
   456     //read message header from buffer
       
   457     memcpy( msg_hdr, pc, sizeof( message_hdr_req ) );
       
   458 	
       
   459 	//disconnect in async way as glib signal is not workin
       
   460 	tp_conn_disconnect_async( DBUS_G_PROXY( conn ), logout_cb, msg_hdr );
       
   461 	iso_logger( "%s", "Out - action_logout\n" );
       
   462 	
       
   463 	return 0;
       
   464 	}
       
   465 
       
   466 /*! \brief validates params, gets a dbus, creates a connection manager,
       
   467  *  and connection. Registers for the statuschange
       
   468  * 
       
   469  *  \param ls_hdr login struct
       
   470  *  \return Errors if invalid param or getting dbus, conn mgr or conn fails else 0
       
   471  */
       
   472 gint action_login( login_struct* ls_hdr ) 
       
   473 	{
       
   474 	
       
   475 	DBusGConnection *connection = NULL;
       
   476 	TpConnMgr *connmgr = NULL;
       
   477 	TpConn	*conn = NULL;
       
   478 	GError *error = NULL;
       
   479 	
       
   480 	gchar* def1 = NULL;
       
   481 	char* def2 = NULL;
       
   482 	char* def3 = NULL; 
       
   483 	
       
   484 	gchar* def4 = NULL;
       
   485 	guint def5 = 0;
       
   486 	gboolean def6 = 0;
       
   487 		
       
   488 	#ifdef __WINSCW__	
       
   489 	gchar* def8 = NULL;
       
   490 	guint def9 = 0; 
       
   491 	#endif
       
   492 		
       
   493     //Build hash table
       
   494     GHashTable *connection_parameters = NULL;
       
   495     //initalize the Gvalues
       
   496 	GValue *value1 = g_new0(GValue, 1);
       
   497 	GValue *value2 = g_new0(GValue, 1);
       
   498 	GValue *value3 = g_new0(GValue, 1);
       
   499 	GValue *value4 = g_new0(GValue, 1);
       
   500 	GValue *value5 = g_new0(GValue, 1);
       
   501 	GValue *value6 = g_new0(GValue, 1);
       
   502 	
       
   503 	#ifdef __WINSCW__	
       
   504 	GValue *value9 = g_new0(GValue, 1);
       
   505 	GValue *value10 = g_new0(GValue, 1);
       
   506 	#endif
       
   507 	
       
   508 	//log message
       
   509 	iso_logger( "%s", "In - action_login\n" );
       
   510 	
       
   511 	//Check if all the mandatory settings are there
       
   512 	if ( NULL == ls_hdr->strs[0] || NULL == ls_hdr->strs[1] || NULL == ls_hdr->strs[2]
       
   513 		|| NULL == ls_hdr->strs[4] || NULL == ls_hdr->strs[5] ||
       
   514 		NULL == ls_hdr->strs[7] || NULL == ls_hdr->strs[8] || NULL == ls_hdr->strs[9] ) 
       
   515 		{
       
   516 		return INVALID_PARAMETERES;
       
   517 		}
       
   518 	//Only for winscw 	
       
   519 	#ifdef __WINSCW__
       
   520 	if ( NULL == ls_hdr->strs[10] || NULL == ls_hdr->strs[11] ) 
       
   521 		{
       
   522 		return INVALID_PARAMETERES;
       
   523 		}
       
   524 	#endif
       
   525 	
       
   526 	//0 is username
       
   527 	def1 = g_strdup( ls_hdr->strs[0] );
       
   528 	//1 is passwd
       
   529 	def2 = g_strdup( ls_hdr->strs[1] ) ; 
       
   530 	//2 is server addr
       
   531 	def3 = g_strdup( ls_hdr->strs[2] ) ; 
       
   532 	if ( NULL != ls_hdr->strs[3] ) 
       
   533 		{
       
   534 		//3 is resource	
       
   535 		def4 = g_strdup( ls_hdr->strs[3] ) ;
       
   536 		} 
       
   537 	else 
       
   538 		{
       
   539 		//Fix required from lalitha
       
   540 		//Is this manadatory? or Can this be NULL
       
   541 		def4 = g_strdup( TestId );
       
   542 		}
       
   543 	//4 is ssl
       
   544 	def5 = atoi( ls_hdr->strs[4] );
       
   545 	//5 is server port
       
   546 	def6 = atoi( ls_hdr->strs[5] );
       
   547 	//6 is IapId is not used
       
   548 	//def7 = g_strdup( ls_hdr->strs[6] ) ;
       
   549 	
       
   550 	iso_logger( "username is %s", ls_hdr->strs[0] );
       
   551 	iso_logger( "server is %s", ls_hdr->strs[2] );
       
   552 	iso_logger( "resource is %s", ls_hdr->strs[3] );
       
   553 	iso_logger( "ssl is %s", ls_hdr->strs[4] );
       
   554 	iso_logger( "server port is %s", ls_hdr->strs[5] );
       
   555 	iso_logger( "connmgr bus is %s", ls_hdr->strs[7] );
       
   556 	iso_logger( "connmgr path is %s", ls_hdr->strs[8] );
       
   557 	iso_logger( "protocol is %s", ls_hdr->strs[9] );
       
   558 	
       
   559 	
       
   560 	globalCon.connmgr_bus = strdup( ls_hdr->strs[7]);
       
   561 	
       
   562 	if ( NULL == globalCon.connmgr_bus ) 
       
   563 		{
       
   564 		return MEM_ALLOCATION_ERROR;
       
   565 		}
       
   566 	
       
   567 	#ifdef __WINSCW__
       
   568 	//7 is ProxyServer
       
   569 	def8 = g_strdup( ls_hdr->strs[10] ) ;
       
   570 	//proxy port
       
   571 	def9 = atoi( ls_hdr->strs[11] );
       
   572 	#endif
       
   573 	
       
   574 	//Get DBus - do not change the order of following calls to 
       
   575 	//dbus, g_hash_table_new
       
   576 	
       
   577 
       
   578 	connection = dbus_g_bus_get( DBUS_BUS_SESSION , &error);
       
   579 	
       
   580 	//DBus Connection is NULL		
       
   581 	//return with error
       
   582 	if ( connection == NULL ) 
       
   583 		{
       
   584 		if ( error ) 
       
   585 			{
       
   586 			g_error_free(error);
       
   587 			}
       
   588 		iso_logger( "failed in  connection == NULL " );
       
   589 		return DBUS_CONNECTION_ERROR;		
       
   590 		}
       
   591 	/* printf("connected to DBus with connection %p\n", connection);*/
       
   592     globalCon.dbusConn = connection;
       
   593 
       
   594 	//Get connection manager
       
   595 	connmgr = tp_connmgr_new(connection, ls_hdr->strs[7], ls_hdr->strs[8],TP_IFACE_CONN_MGR_INTERFACE);
       
   596 	if ( connmgr == NULL ) 
       
   597 		{
       
   598 		iso_logger( "failed in tp_connmgr_new" );
       
   599 		//Free already allocated resources
       
   600 		//return with error
       
   601 		dbus_g_connection_unref ( globalCon.dbusConn );
       
   602 		globalCon.dbusConn = NULL;	
       
   603 		return TP_CONNMGR_ERROR;
       
   604 		}
       
   605 	
       
   606 	//Create values for hash table 
       
   607 	connection_parameters = g_hash_table_new( g_str_hash, g_str_equal );
       
   608 	if ( NULL == connection_parameters ) 
       
   609 		{
       
   610 		//Free already allocated resources
       
   611 		//return with error
       
   612 	    g_object_unref (connmgr);
       
   613 		connmgr = NULL;	
       
   614 		dbus_g_connection_unref ( globalCon.dbusConn );
       
   615 		globalCon.dbusConn = NULL;	
       
   616 		iso_logger( "failed in g_hash_table_new" );
       
   617 		return TP_HASHTABLE_ERROR;	
       
   618 		}
       
   619 		
       
   620 	//Add AccountStr values to hash table
       
   621 	g_value_init( value1, G_TYPE_STRING );
       
   622 	g_value_set_string ( value1, def1 );
       
   623 	g_hash_table_insert( connection_parameters, ( gpointer ) AccountStr, value1 ); 
       
   624 	
       
   625 	//Add PasswdStr values to hash table
       
   626 	g_value_init( value2, G_TYPE_STRING ); 
       
   627 	g_value_set_string ( value2, def2 );
       
   628 	g_hash_table_insert( connection_parameters, ( gpointer ) PasswdStr, value2 ); 
       
   629 	//Add ServerStr values to hash table
       
   630 	g_value_init(value3, G_TYPE_STRING); 
       
   631 	g_value_set_string (value3, def3);
       
   632 	g_hash_table_insert( connection_parameters, ( gpointer ) ServerStr, value3 ); 
       
   633 	//Add ResourceStr values to hash table
       
   634 	if ( NULL != def4 ) 
       
   635 		{
       
   636 		//This is not mandatory settings item
       
   637 		//so check for NULL value before useing it..
       
   638 		g_value_init( value4, G_TYPE_STRING ); 
       
   639 		g_value_set_string ( value4, def4 );
       
   640 		g_hash_table_insert( connection_parameters, ( gpointer ) ResourceStr, value4 ); 	
       
   641 		}
       
   642 	
       
   643 	//Add PortStr values to hash table
       
   644 	g_value_init(value5, G_TYPE_UINT); 
       
   645 	g_value_set_uint (value5, def6);
       
   646     g_hash_table_insert(connection_parameters, ( gpointer ) PortStr, value5); 
       
   647 	//Add OldsslStr values to hash table
       
   648     g_value_init(value6, G_TYPE_BOOLEAN);
       
   649 	g_value_set_boolean (value6, def5);
       
   650     g_hash_table_insert(connection_parameters, ( gpointer ) OldsslStr, value6 ); 
       
   651     //Settings Only for emulator
       
   652     #ifdef __WINSCW__   
       
   653     //Add proxy server values to hash table 
       
   654     g_value_init(value9, G_TYPE_STRING); 
       
   655 	g_value_set_string (value9, def8);
       
   656     g_hash_table_insert(connection_parameters, "https-proxy-server", value9); 
       
   657 
       
   658 	//Add proxy port values to hash table
       
   659     g_value_init(value10, G_TYPE_UINT); 
       
   660 	g_value_set_uint (value10, def9);
       
   661     g_hash_table_insert(connection_parameters, "https-proxy-port", value10); 
       
   662 	#endif
       
   663 
       
   664     
       
   665 	//Get connection from connection manager - i.e., Login
       
   666 	conn = tp_connmgr_new_connection( connmgr,connection_parameters, ls_hdr->strs[9] );
       
   667 	
       
   668 	if ( !conn )
       
   669 		{
       
   670 		iso_logger( "failed in tp_connmgr_new_connection" );
       
   671 		//Free already allocated resources
       
   672 		//return with error
       
   673 		g_object_unref (connmgr);
       
   674 		connmgr = NULL;	
       
   675 		dbus_g_connection_unref ( globalCon.dbusConn );
       
   676 		globalCon.dbusConn = NULL;
       
   677 		
       
   678 		g_hash_table_destroy( connection_parameters );
       
   679 		return TP_CONNECTION_ERROR;
       
   680 		}
       
   681 		
       
   682 	
       
   683 	//register for StatusChanged callback - login / logout			      
       
   684 	dbus_g_proxy_connect_signal( DBUS_G_PROXY( conn ), StatusChangedStr,
       
   685 					G_CALLBACK( status_changed_cb ),
       
   686 					ls_hdr->hdr_req, NULL );
       
   687 	//Assign to global variable struct				
       
   688 	globalCon.conn = conn;
       
   689 	globalCon.connmgr = connmgr;
       
   690 	globalCon.dbusConn = connection;
       
   691 	//Free 
       
   692 	g_hash_table_destroy( connection_parameters );
       
   693 	iso_logger( "%s", "Out - action_login\n" );
       
   694 	
       
   695 	return 0;	
       
   696 	}	
       
   697 
       
   698 /*! \brief parses the message buffer and validates the parameters. The values are written
       
   699  *  to ls_hdr
       
   700  *
       
   701  *  \param aMsgBuffer - buf to be parsed
       
   702  *  \param ls_hdr - login_struct struct to which values are written 
       
   703  *  \param msg_len - no. of bytes in the message buffer aMsgBuffer
       
   704  *
       
   705  */
       
   706 
       
   707 gint parse_for_login( gchar* msg_buffer, login_struct** msg_struct, int msg_len ) 
       
   708 	{
       
   709 
       
   710 	char* bufPtr = NULL;
       
   711 	gint index = 0;
       
   712 	gint arg_count = 0;
       
   713 	gint str_len = 0;
       
   714 	char *str1 = NULL;
       
   715 	
       
   716 	message_hdr_req *msg_hdr = NULL;
       
   717 	
       
   718 	iso_logger( "%s","In - parse_for_login\n" );
       
   719 	//Reading of the message_hdr_req is done twice..
       
   720 	//should be changed to read once..
       
   721 	//Allocate memory and check for the error
       
   722 	msg_hdr = ( message_hdr_req* ) malloc( sizeof( message_hdr_req ) );
       
   723 	if ( NULL == msg_hdr ) 
       
   724 		{
       
   725 		return MEM_ALLOCATION_ERROR;
       
   726 		}
       
   727 	//initalize memory to zero	
       
   728     memset( msg_hdr, '\0', sizeof( message_hdr_req ) );
       
   729     memcpy( msg_hdr, msg_buffer, sizeof( message_hdr_req ) );
       
   730     //ownership of msg_hdr is transefred to msg_struct
       
   731     ( *msg_struct )->hdr_req = msg_hdr;
       
   732 	
       
   733 	bufPtr = msg_buffer + sizeof( message_hdr_req );
       
   734 	
       
   735 	//validating each arg and copying it..
       
   736 	
       
   737 	while ( arg_count < LOGIN_ARG_COUNT ) 
       
   738 		{
       
   739 		//Get one string 
       
   740 		while(  index + str_len < msg_len  && '\0' != *( bufPtr  + index + str_len ) ) 
       
   741 			{
       
   742 			str_len++;
       
   743 			//Check if max param len no. of chars already read..
       
   744 			//if so return with error
       
   745 			if ( MAX_PARAM_LEN == index + str_len ) 
       
   746 				{
       
   747 				return INVALID_PARAMETERES;
       
   748 				}
       
   749 			}
       
   750 		//str_len == 0, means one setting attrib missing..
       
   751 		//Assign that to NULL..and continue		
       
   752 		if ( 0 == str_len ) 
       
   753 			{
       
   754 			( *msg_struct )->strs[arg_count] = NULL;
       
   755 			//Increment the no of args read by one
       
   756 			arg_count++;
       
   757 			index++;
       
   758 			continue;
       
   759 			}
       
   760 		//ownership of these strs is transfered to msg_struct
       
   761 		//Allocate memory and check for the error	
       
   762 		str1 = ( char* ) malloc ( str_len + 1 );
       
   763 		if ( NULL == str1 ) 
       
   764 			{
       
   765 			return MEM_ALLOCATION_ERROR;
       
   766 			}
       
   767 		//Copy the string from buffer
       
   768 		//copy '\0' as well
       
   769 		memcpy( str1, bufPtr + index, str_len + 1 ); 
       
   770 		//Assign to the array
       
   771 		( *msg_struct )->strs[arg_count] = str1;
       
   772 		//Skip the '\0' already read..
       
   773 		index += str_len + 1;
       
   774 		//reset the str_len to read next char
       
   775 		str_len = 0;
       
   776 		//Increment the no of args read by one
       
   777 		arg_count++;
       
   778 		
       
   779 		}
       
   780 	iso_logger( "%s", "Out - parse_for_login\n" );
       
   781 	return 0;
       
   782 	}
       
   783 /*! \brief Calls parse_for_login to parse and validate the arguments
       
   784  *  And then calls action_login for to do actual login
       
   785  *
       
   786  *  \param aMsgBuf message buffer
       
   787  *  \param msg_len - len of the message
       
   788  */
       
   789 gint action_parse_login( char* aMsgBuf, int msg_len ) 
       
   790 	{
       
   791 
       
   792 	gint message_type_err = 0;
       
   793 	login_struct* message_struct;
       
   794 	gint arg_count = 0;
       
   795 	
       
   796 	iso_logger( "%s", "In - action_parse_login\n" );
       
   797 	//Allocate memory and check for errors
       
   798 	message_struct = ( login_struct* ) malloc( sizeof ( login_struct ) );
       
   799 	if ( NULL == message_struct ) 
       
   800 		{
       
   801 		return MEM_ALLOCATION_ERROR;
       
   802 		}
       
   803 	//intialize memory to 0s
       
   804 	memset ( message_struct, '\0', sizeof ( login_struct ) );
       
   805 	
       
   806 	//Parse the message into login parameters
       
   807 	message_type_err = parse_for_login( aMsgBuf, &message_struct, msg_len );
       
   808 	//If no error in parsing do a login
       
   809 	if ( !message_type_err ) 
       
   810 		{
       
   811 		//parse passed
       
   812 		message_type_err = action_login( message_struct );
       
   813 		
       
   814 		}
       
   815 	//check if the pointer is not pointing to NULL
       
   816 	//And free the memory	
       
   817 	for( arg_count = 0; arg_count < LOGIN_ARG_COUNT; arg_count++ ) 
       
   818 		{
       
   819 		if ( NULL != message_struct->strs[arg_count] ) 
       
   820 			{
       
   821 			free ( message_struct->strs[arg_count] );
       
   822 			}
       
   823 		}
       
   824 	free ( message_struct );
       
   825 	iso_logger( "%s", "Out - action_parse_login\n" );
       
   826 	return message_type_err;
       
   827 	}
       
   828 	
       
   829