bluetoothengine/btsap/src/BTSapStateConnect.cpp
changeset 0 f63038272f30
child 65 001a94c71129
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2004-2006 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 *     This class is a BTSapServer's state for setting up connection with BTSap client
       
    16 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include <e32property.h>
       
    24 #include <ctsydomainpskeys.h>
       
    25 #include <PSVariables.h>
       
    26 #include "BTSapServerState.h"
       
    27 #include "BTSapSocketHandler.h"
       
    28 #include "BTSapSimCardStatusNotifier.h"
       
    29 #include "debug.h"
       
    30 
       
    31 
       
    32 CBTSapServerState::TStateConnect::TStateConnect(CBTSapServerState& aServerState)
       
    33     : TStateIdle(aServerState), iConnectRequestOK(EFalse), iCardStatus(ECardStatusReserved)
       
    34     {
       
    35     }
       
    36 
       
    37 // ---------------------------------------------------------
       
    38 // Enter
       
    39 // ---------------------------------------------------------
       
    40 void CBTSapServerState::TStateConnect:: Enter(TRequestStatus& aStatus)
       
    41     {
       
    42     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: Enter")));
       
    43 
       
    44     iStatus = &aStatus;
       
    45     TConnectionStatus connectionStatus = EConnectionErrReject;
       
    46     if(!IsCallOngoing())
       
    47         {   // SAP cannot be accepted if a call is ongoing or if no SIM is present
       
    48         connectionStatus = EConnectionOK;
       
    49         }
       
    50 
       
    51     if (connectionStatus == EConnectionOK)
       
    52         {
       
    53         CheckMaxMsgSize(connectionStatus);
       
    54         }
       
    55 
       
    56     if (connectionStatus == EConnectionOK)
       
    57         {
       
    58         iConnectRequestOK = ETrue;
       
    59 
       
    60         // init value which is impossible to be received.
       
    61         // by checking the value, BTSap knows if status_ind is received
       
    62         iCardStatus = ECardStatusReserved;
       
    63         // listen to status_ind (card_reset is expected)
       
    64         iServerState.BTSapSimCardStatusNotifier().Start();
       
    65 
       
    66         NotifySapState(ESapConnecting);
       
    67 
       
    68         // Waiting for Accept/RejectSapConnection()
       
    69         aStatus = KRequestPending; 
       
    70 
       
    71         if(!IsSimPresent())
       
    72             {
       
    73             // If there is no SIM present, we bypass the accept/reject for the connection.
       
    74             // Instead this will be signalled in the connection reply, followed by disconnection.
       
    75             iCardStatus = ECardStatusRemoved;
       
    76             User::RequestComplete(iStatus, EUserAccepted);
       
    77             }
       
    78 #ifdef __WINS__        
       
    79 
       
    80         //Doesn't wait for any confirmation (used in WINS)        
       
    81         BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: Enter Accept")));
       
    82         User::RequestComplete(iStatus, EUserAccepted); 
       
    83         
       
    84 #endif //__WINS__
       
    85 
       
    86         }
       
    87     else
       
    88         {
       
    89         iConnectRequestOK = EFalse;
       
    90 
       
    91         iResponseMessage.SetMsgID(EConnectResponse);
       
    92         iResponseMessage.AddParameter(EParaConnectionStatus, connectionStatus);
       
    93 
       
    94         if (connectionStatus == EConnectionErrNotSupported)
       
    95             {
       
    96             iResponseMessage.AddParameter(EParaMaxMsgSize, KMaxMsgSize, 2);
       
    97             }
       
    98 
       
    99         iServerState.BTSapSocketHandler().Send(iResponseMessage.Data());
       
   100 
       
   101         if (connectionStatus == EConnectionErrNotSupported)
       
   102             {
       
   103             // waiting for the client to send another conn_req with my KMaxMsgSize
       
   104             aStatus = KRequestPending; 
       
   105             }
       
   106         else
       
   107             {
       
   108             // too small or a phone call ongoing
       
   109             User::RequestComplete(iStatus, EConnectionError);
       
   110             }
       
   111         }
       
   112     }
       
   113 
       
   114 // --------------------------------------------------------------------------------------------
       
   115 // Complete
       
   116 // set next state
       
   117 // --------------------------------------------------------------------------------------------
       
   118 //
       
   119 TBTSapServerState CBTSapServerState::TStateConnect::Complete(TInt aReason)
       
   120     {
       
   121     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: Complete")));
       
   122 
       
   123     TBTSapServerState nextState = EStateNotConnected;
       
   124 
       
   125     if (aReason == EUserAccepted || aReason == EUserRejected)
       
   126         {
       
   127         TInt connectionStatus = (aReason == EUserAccepted) ? EConnectionOK : EConnectionErrReject;
       
   128 
       
   129         iResponseMessage.SetMsgID(EConnectResponse);
       
   130         iResponseMessage.AddParameter(EParaConnectionStatus, connectionStatus);
       
   131         iServerState.BTSapSocketHandler().Send(iResponseMessage.Data());
       
   132 
       
   133         if (connectionStatus == EConnectionOK)
       
   134             {
       
   135             BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: Complete: iCardStatus: %d"), iCardStatus));
       
   136 
       
   137             if (iCardStatus != ECardStatusReserved)
       
   138                 {
       
   139                 // send status_ind
       
   140                 TStateIdle::SimCardStatusChanged(iCardStatus);
       
   141                 }
       
   142 
       
   143             if(iCardStatus != ECardStatusRemoved)
       
   144                 {
       
   145                 // There is no SIM, so the connection will be disconnected by the client.
       
   146                 // We therefore do not signal a connection complete.
       
   147                 NotifySapState(ESapConnected);
       
   148                 }
       
   149             nextState = EStateIdle;
       
   150             }
       
   151         }
       
   152 
       
   153     return nextState;
       
   154     }
       
   155 
       
   156 void CBTSapServerState::TStateConnect::Cancel()
       
   157     {
       
   158     NotifySapState(ESapNotConnected);
       
   159     User::RequestComplete(iStatus, KErrCancel);
       
   160     }
       
   161 
       
   162 TBool CBTSapServerState::TStateConnect::IsCallOngoing()
       
   163     {
       
   164 #ifdef __WINS__
       
   165 
       
   166 	BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapServerState::TStateConnect::IsCallOngoing ON THE WINS SIDE")));
       
   167 	return EFalse;
       
   168 	
       
   169 #else    
       
   170     
       
   171     TBool retVal = EFalse;
       
   172     
       
   173     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapServerState::TStateConnect::IsCallOngoing")));
       
   174 
       
   175     TInt callState;
       
   176     // Try to get the call state property
       
   177     TInt err = RProperty::Get(KPSUidCtsyCallInformation, KCTsyCallState, callState);
       
   178 
       
   179     // Check if retrieving the property value succeeded
       
   180     if(err == KErrNone)
       
   181         {
       
   182         BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  TStateConnect: callState: %d"), callState));
       
   183 
       
   184         // If callState is EPSTelephonyCallStateNone or EPSTelephonyCallStateUninitialized, there's no ongoing call
       
   185         retVal = (callState != EPSCTsyCallStateNone) && (callState != EPSCTsyCallStateUninitialized);
       
   186         }
       
   187     else
       
   188         {
       
   189         // Couldn't retrieve call state property
       
   190         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR, BTSapPrintTrace(_L("[BTSap]  TStateConnect: Couldn't get callState!!! (err = %d)"), err));
       
   191         retVal = ETrue; // Assume there was an ongoing call to abort the SAP connection
       
   192         }
       
   193 
       
   194     return retVal;
       
   195 #endif // __WINS__
       
   196     }
       
   197 
       
   198 void CBTSapServerState::TStateConnect::CheckMaxMsgSize(TConnectionStatus& aMsgSizeStatus)
       
   199     {
       
   200     TInt msgSize;
       
   201     iServerState.BTSapRequestMessage().GetParameter(EParaMaxMsgSize, msgSize);
       
   202     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  TStateConnect: msg size: %d"), msgSize));
       
   203 
       
   204     if (msgSize < KMinMsgSize)
       
   205         {
       
   206         BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: msg size too small ***")));
       
   207         aMsgSizeStatus = EConnectionErrTooSmall;
       
   208         }
       
   209     else if (msgSize > KMaxMsgSize)
       
   210         {
       
   211         BTSAP_TRACE_OPT(KBTSAP_TRACE_INFO, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: msg size too big ***")));
       
   212         aMsgSizeStatus = EConnectionErrNotSupported;
       
   213         }
       
   214     else
       
   215         {
       
   216         aMsgSizeStatus = EConnectionOK;
       
   217         }
       
   218     }
       
   219 
       
   220 TInt CBTSapServerState::TStateConnect::AcceptSapConnection()
       
   221     {
       
   222     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: AcceptSapConnection")));
       
   223 
       
   224     if (*iStatus == KRequestPending)
       
   225         {
       
   226         User::RequestComplete(iStatus, EUserAccepted);
       
   227         }
       
   228 
       
   229     return KErrNone;
       
   230     }
       
   231 
       
   232 TInt CBTSapServerState::TStateConnect::RejectSapConnection(TBTSapRejectReason /*aReason*/)
       
   233     {
       
   234     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  SM: TStateConnect: RejectSapConnection")));
       
   235 
       
   236     if (*iStatus == KRequestPending)
       
   237         {
       
   238         User::RequestComplete(iStatus, EUserRejected);
       
   239         }
       
   240 
       
   241     return KErrNone;
       
   242     }
       
   243 
       
   244 TInt CBTSapServerState::TStateConnect::ChangeState(TBTSapServerState& aNextState)
       
   245     {
       
   246     TInt retVal = KErrNotSupported;
       
   247     TBTSapServerState nextState = EStateNotConnected;
       
   248 
       
   249     if ((aNextState == EStateConnect &&
       
   250         *iStatus == KRequestPending &&
       
   251         !iConnectRequestOK) ||
       
   252         aNextState == EStateDisconnect)
       
   253         {
       
   254         retVal = KErrNone;
       
   255         nextState = aNextState;
       
   256         }
       
   257 
       
   258     aNextState = nextState;
       
   259     return retVal;
       
   260     }
       
   261 
       
   262 void CBTSapServerState::TStateConnect::SimCardStatusChanged(TCardStatus aCardStatus)
       
   263     {
       
   264     iCardStatus = aCardStatus;
       
   265     }
       
   266 
       
   267 //  End of File