pimprotocols/pbap/client/pbapcli.cpp
changeset 0 e686773b3f54
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Bluetooth Phone Book Access Profile (PBAP) client side API implementation. This file
       
    15 // implements the methods used to start and stop the PBAP server listening for 
       
    16 // phone book client equipment (PCE) requests. 
       
    17 // 
       
    18 //
       
    19 
       
    20 /**
       
    21  @file
       
    22  @publishedPartner
       
    23  @released
       
    24 */
       
    25 #include "pbapcli.h"
       
    26 #include "clientserver.h"
       
    27 
       
    28 
       
    29 /**
       
    30  Start the server process which lives in its own executable and rendezvous with it.
       
    31 
       
    32  @return  KErrNone if successful, or an error code if not.
       
    33  */
       
    34 static TInt StartServer()
       
    35 	{
       
    36 	const TUidType serverUid(KNullUid, KNullUid, KPbapServerUid);
       
    37 
       
    38 	RProcess server;
       
    39 	TInt r=server.Create(KPbapServerImg,KNullDesC,serverUid);
       
    40 
       
    41 	if (r==KErrNone)
       
    42 		{
       
    43 		TRequestStatus stat;
       
    44 		server.Rendezvous(stat);
       
    45 		if (stat!=KRequestPending)
       
    46 			{
       
    47 			server.Kill(0);		// abort startup
       
    48 			}
       
    49 		else
       
    50 			{
       
    51 			server.Resume();	// logon OK - start the server
       
    52 			}	
       
    53 		User::WaitForRequest(stat);		// wait for start or death
       
    54 
       
    55 		// we can't use the 'exit reason' if the server panicked as this
       
    56 		// is the panic 'reason' and may be '0' which cannot be distinguished
       
    57 		// from KErrNone
       
    58 		r=(server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();	
       
    59 	    server.Close(); 
       
    60 		}
       
    61 	return r;
       
    62 	}
       
    63 
       
    64 
       
    65 /**
       
    66  Standard constructor.
       
    67  */
       
    68 EXPORT_C RPbapSession::RPbapSession()
       
    69 	{
       
    70 	}
       
    71 
       
    72 
       
    73 /**
       
    74  Connects the client to the Bluetooth PBAP Server. 
       
    75 
       
    76  This must be used before any of the other methods listed in this API
       
    77  section. The first client to call this method will cause the initiation of
       
    78  the PBAP server within its own executable process. The PBAP Server will
       
    79  only allow at most one session to be connected at any one time and any
       
    80  subsequent attempts to Connect() to the server will result in KErrInUse 
       
    81  being returned.
       
    82 
       
    83  Once a connection to the server has completed successfully Start() must be
       
    84  called to allow PCE requests.
       
    85 
       
    86  @return  KErrNone if successful, a system-wide error code if not. 
       
    87  @capability None
       
    88  */
       
    89 EXPORT_C TInt RPbapSession::Connect()
       
    90 	{
       
    91 	TInt err = CreateSession(KPbapServerName, Version());
       
    92 
       
    93 	// we will get an error if the server has not been started yet so check
       
    94 	if ((err == KErrNotFound) || (err == KErrServerTerminated))
       
    95 		{
       
    96 		err = StartServer();
       
    97 		if ((err == KErrNone) || (err == KErrAlreadyExists))
       
    98 			{
       
    99 			// attempt to create the session again
       
   100 			err = CreateSession(KPbapServerName, Version());
       
   101 			}
       
   102 		}
       
   103 		
       
   104 	return err;
       
   105 	}
       
   106 
       
   107 
       
   108 /**
       
   109  Get the PBAP server version.
       
   110  */
       
   111 TVersion RPbapSession::Version() const
       
   112 	{
       
   113 	return TVersion(KPbapServerMajorVersionNumber,
       
   114 					KPbapServerMinorVersionNumber,
       
   115 					KPbapServerBuildVersionNumber);
       
   116 	}
       
   117 
       
   118 
       
   119 /**
       
   120  Start listening for PCE requests (via Obex over Bluetooth) in the PBAP server.
       
   121  If this request is successful sessions with a PCE can be established
       
   122 
       
   123  @return  KErrNone if successful, or an error code if not.
       
   124  
       
   125  @capability LocalServices
       
   126  @capability ReadUserData
       
   127  */
       
   128 EXPORT_C TInt RPbapSession::Start()
       
   129 	{
       
   130 	return SendReceive(EPbapStartListening);
       
   131 	}
       
   132 
       
   133 
       
   134 /**
       
   135  Stop listening for PCE requests to the PBAP server.
       
   136  Once this function is called all PCE requests will fail until the next
       
   137  successful Start() call. Calling this function during a request
       
   138  will result in an Obex disconnect being reported to the client.
       
   139  
       
   140  @capability LocalServices
       
   141  @capability ReadUserData
       
   142  */	
       
   143 EXPORT_C void RPbapSession::Stop()
       
   144 	{
       
   145 	SendReceive(EPbapStopListening);
       
   146 	}
       
   147 
       
   148 
       
   149 /**
       
   150   Sets the password required to access the PBAP server, this can be done 
       
   151   before or after a call to RPbapSession::Start.
       
   152   When a password is set, a PCE must specify it to access the server. To 
       
   153   remove the need for the password, the PBAP server must be stopped and
       
   154   re-started.
       
   155 
       
   156  @return  KErrNone if successful, or an error code if not.
       
   157 
       
   158  @capability LocalServices
       
   159  @capability ReadUserData
       
   160  */
       
   161 EXPORT_C TInt RPbapSession::SetPassword(const TDesC& aPassword)
       
   162 	{
       
   163 	TIpcArgs args(&aPassword);
       
   164 	return SendReceive(EPbapSetPassword, args);
       
   165 	}
       
   166 
       
   167 
       
   168 /**
       
   169  Set a heap mark in the PBAP server thread.
       
   170  
       
   171  @capability None
       
   172  */
       
   173 EXPORT_C TInt RPbapSession::__DbgMarkHeap()
       
   174 	{
       
   175 	return DEBUG_SEND_RECIEVE(EPbapDbgMarkHeap, TIpcArgs(TIpcArgs::ENothing));
       
   176 	}
       
   177 
       
   178 
       
   179 /**
       
   180  Performs a heap mark check in the PBAP Server thread.
       
   181  
       
   182  @param aCount  The number of heap cells expected to be allocated at
       
   183                 the current nest level.
       
   184  @capability None
       
   185  */
       
   186 EXPORT_C TInt RPbapSession::__DbgCheckHeap(TInt aCount)
       
   187 	{
       
   188 	TIpcArgs args(aCount);
       
   189 	return DEBUG_SEND_RECIEVE(EPbapDbgCheckHeap, args);
       
   190 	}
       
   191 
       
   192 
       
   193 /**
       
   194  Perfom a heap mark end check in the PBAP server thread.
       
   195  
       
   196  @param aCount  The number of heap cells expected to remain allocated
       
   197                 at the current nest level.
       
   198  @capability None
       
   199  */
       
   200 EXPORT_C TInt RPbapSession::__DbgMarkEnd(TInt aCount)
       
   201 	{
       
   202 	TIpcArgs args(aCount);
       
   203 	return DEBUG_SEND_RECIEVE(EPbapDbgMarkEnd, args);
       
   204 	}
       
   205 
       
   206 
       
   207 /**
       
   208  Set a heap fail next condition in the PBAP server thread.
       
   209  
       
   210  @param aCount  Determines when the allocation will fail. 
       
   211  @capability None
       
   212  */
       
   213 EXPORT_C TInt RPbapSession::__DbgFailNext(TInt aCount)
       
   214 	{
       
   215 	TIpcArgs args(aCount);
       
   216 	return DEBUG_SEND_RECIEVE(EPbapDbgFailNext, args);
       
   217 	}