javaextensions/bluetooth/bluetoothplugins/btl2cappushplugin/src/btl2cappushserverconnection.cpp
branchRCL_3
changeset 19 04becd199f91
child 57 59b3b4473dc8
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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 #include "btl2cappushserverconnection.h"
       
    20 #include "btl2capserverconnectionfactory.h"
       
    21 #include "fs_methodcall.h"
       
    22 #include "pushexception.h"
       
    23 #include "pusherrorcodes.h"
       
    24 #include "serviceclasshandler.h"
       
    25 #include "bluetoothnamelookup.h"
       
    26 #include "javacommonutils.h"
       
    27 
       
    28 using namespace std;
       
    29 using namespace java::util;
       
    30 using namespace java::push;
       
    31 
       
    32 namespace java
       
    33 {
       
    34 namespace bluetooth
       
    35 {
       
    36 
       
    37 OS_EXPORT L2CapPushServerConnection::L2CapPushServerConnection(
       
    38     const wstring aUri, const wstring aFilter):
       
    39         mAcceptMonitor(NULL),
       
    40         mConnectionUri(aUri),
       
    41         mConnectionFilter(aFilter),
       
    42         mL2CAPServer(NULL),
       
    43         mPendingConnection(false),
       
    44         mListening(false),
       
    45         mCreatedByPush(false),
       
    46         mClearServiceClassBitsFlag(true)
       
    47 {
       
    48     JELOG2(EJavaBluetooth);
       
    49 
       
    50     mFunctionServer = new BluetoothFunctionServer();
       
    51     // Create Bluetooth Parameters
       
    52     mBtUrlParams = new BtUrlParams(mConnectionUri, mConnectionFilter);
       
    53 
       
    54     // Create a Server Object and store it as member.
       
    55     mL2CAPServer = new L2CAPServerConnection(mFunctionServer);
       
    56 }
       
    57 
       
    58 OS_EXPORT L2CapPushServerConnection::~L2CapPushServerConnection()
       
    59 {
       
    60     JELOG2(EJavaBluetooth);
       
    61     delete mBtUrlParams;
       
    62     deleteServer();
       
    63     delete mFunctionServer;
       
    64 }
       
    65 
       
    66 /**
       
    67  * This method is called by Push framework to start listening.
       
    68  * Steps to be followed to start listening.
       
    69  * 1. Prompt user to switch in Bluetooth if it is switched off.
       
    70  * 2. Create Btl2capServer and ask it to start listening.
       
    71  *
       
    72  *
       
    73  */
       
    74 OS_EXPORT void L2CapPushServerConnection::open(ConnectionListener* aListener)
       
    75 {
       
    76     JELOG2(EJavaBluetooth);
       
    77     mConnectionListener = aListener;
       
    78 
       
    79     bool authenticate = mBtUrlParams->getParamAuthenticate();
       
    80     bool authorize = mBtUrlParams->getParamAuthorize();
       
    81     bool encrypt = mBtUrlParams->getParamEncrypt();
       
    82     bool master = mBtUrlParams->getParamMaster();
       
    83     int recvMtu = mBtUrlParams->getParamReceiveMtu();
       
    84     int transMtu = mBtUrlParams->getParamTransmitMtu();
       
    85 
       
    86     int error = 0;
       
    87 
       
    88     // Open L2CAP Server.
       
    89     error = mL2CAPServer->openServer(authorize, authenticate, encrypt, master,
       
    90                                      recvMtu, transMtu);
       
    91     if (error < 0)
       
    92     {
       
    93         std::string errTxt("ERROR!!! Unable to Open L2CAP Server.");
       
    94         throw PushException(COMMON_SRV_CONN_PLUGIN_ERROR, errTxt, __FILE__,
       
    95                             __FUNCTION__, __LINE__);
       
    96     }
       
    97 
       
    98     int psmValue = mL2CAPServer->GetServerPSM();
       
    99 
       
   100     std::wstring uuid = mBtUrlParams->getServiceUuid();
       
   101     std::wstring serviceName = mBtUrlParams->getParamName();
       
   102 
       
   103     LOG1(EJavaBluetooth, EInfo, "  L2CapPushServerConnection::open uuid:%S",
       
   104          uuid.c_str());
       
   105 
       
   106     // Initializing the Service record
       
   107     error = mL2CAPServer->initializeServiceRecord(psmValue, uuid, serviceName);
       
   108 
       
   109     if (error < 0)
       
   110     {
       
   111         std::string errTxt("ERROR: Unable to Initialize L2CAP Server");
       
   112         throw PushException(COMMON_SRV_CONN_PLUGIN_ERROR, errTxt, __FILE__,
       
   113                             __FUNCTION__, __LINE__);
       
   114     }
       
   115 
       
   116     // Restore Persistent Record (if any)
       
   117     mL2CAPServer->restorePersistentRecord();
       
   118 
       
   119     // Ask server to start
       
   120     mL2CAPServer->asyncAccept(this, mBtUrlParams);
       
   121 
       
   122     // We are listening now.
       
   123     mListening = true;
       
   124 
       
   125     // Reset the flag
       
   126     mClearServiceClassBitsFlag = true;
       
   127 }
       
   128 
       
   129 /*
       
   130  * When server is push registered, and manual launched server is closed,
       
   131  * it is not required to clear the service class bits
       
   132  * associated with this service.
       
   133  */
       
   134 OS_EXPORT void L2CapPushServerConnection::unsetClearServiceClassBitsFlag()
       
   135 {
       
   136     JELOG2(EJavaBluetooth);
       
   137     mClearServiceClassBitsFlag = false;
       
   138 }
       
   139 
       
   140 OS_EXPORT void L2CapPushServerConnection::close()
       
   141 {
       
   142     JELOG2(EJavaBluetooth);
       
   143     if (true == mClearServiceClassBitsFlag)
       
   144     {
       
   145         ServiceClassHandler::setDeviceServiceClass(0);
       
   146     }
       
   147     mAcceptMonitor = NULL;
       
   148     mPendingConnection = false;
       
   149     mListening = false;
       
   150     mL2CAPServer->CloseServer();
       
   151 }
       
   152 
       
   153 /*
       
   154  * Gets the connection URI used to open this server connection.
       
   155  */
       
   156 OS_EXPORT std::wstring L2CapPushServerConnection::getUri() const
       
   157 {
       
   158     JELOG2(EJavaBluetooth);
       
   159     return mConnectionUri;
       
   160 }
       
   161 
       
   162 /*
       
   163  * Gets the connection Filter string associate with this connection.
       
   164  */
       
   165 OS_EXPORT std::wstring L2CapPushServerConnection::getFilter() const
       
   166 {
       
   167     JELOG2(EJavaBluetooth);
       
   168     return mConnectionFilter;
       
   169 }
       
   170 
       
   171 OS_EXPORT
       
   172 void L2CapPushServerConnection::setFilter(const std::wstring& aFilter)
       
   173 {
       
   174     JELOG2(EJavaBluetooth);
       
   175     mConnectionFilter = aFilter;
       
   176 }
       
   177 
       
   178 void L2CapPushServerConnection::handleConnectionRequest(
       
   179     BluetoothClientConnection* aClientConnection, TInt err)
       
   180 {
       
   181     JELOG2(EJavaBluetooth);
       
   182     //Here we handle checking of parameters and deciding to invoke push.
       
   183     //Check all parameters
       
   184     if (KErrNone != err)
       
   185     {
       
   186         if (NULL != mAcceptMonitor)
       
   187         {
       
   188             mAcceptMonitor->notify();
       
   189         }
       
   190         mListening = false;
       
   191         return;
       
   192     }
       
   193     BtL2CapServerConnectionFactory& connectionFactory =
       
   194         BtL2CapServerConnectionFactory::getFactory();
       
   195     connectionFactory.setPendingMsgFlag(mConnectionUri, true);
       
   196 
       
   197     std::wstring* remoteDeviceName = NULL;
       
   198 
       
   199     BluetoothNameLookup * nameLookup = NULL;
       
   200 
       
   201     long long remoteDevAddr = aClientConnection->getRemoteAddress();
       
   202 
       
   203     TRAPD(lookupErr,
       
   204     {
       
   205         nameLookup = BluetoothNameLookup::NewL();
       
   206         remoteDeviceName = nameLookup->doDeviceNameLookupL(remoteDevAddr);
       
   207         delete nameLookup;
       
   208     }
       
   209          );
       
   210 
       
   211     if (KErrNone != lookupErr)
       
   212     {
       
   213         remoteDeviceName = new std::wstring(
       
   214             JavaCommonUtils::longLongToWstring(remoteDevAddr));
       
   215     }
       
   216 
       
   217     mConnectionListener->msgArrived(*remoteDeviceName);
       
   218 
       
   219     delete remoteDeviceName;
       
   220 
       
   221     // This means that when jsr gets this object, it must complete
       
   222     // acceptAndOpen immediately
       
   223     mPendingConnection = true;
       
   224     mListening = false;
       
   225 
       
   226     // Store this as member. We can later return this when
       
   227     // queried from jsr.
       
   228     mClientConnection = aClientConnection;
       
   229 
       
   230     if (NULL != mAcceptMonitor)
       
   231     {
       
   232         mAcceptMonitor->notify();
       
   233     }
       
   234 
       
   235 }
       
   236 
       
   237 OS_EXPORT void L2CapPushServerConnection::setAcceptMonitor(
       
   238     java::util::Monitor* aMonitor)
       
   239 {
       
   240     mAcceptMonitor = aMonitor;
       
   241     mL2CAPServer->avoidFilter();
       
   242 }
       
   243 
       
   244 OS_EXPORT void L2CapPushServerConnection::unsetAcceptMonitor()
       
   245 {
       
   246     mAcceptMonitor = NULL;
       
   247 }
       
   248 
       
   249 OS_EXPORT bool L2CapPushServerConnection::isActive()
       
   250 {
       
   251     JELOG2(EJavaBluetooth);
       
   252     bool result = (mPendingConnection || mListening);
       
   253     LOG1(EJavaBluetooth, EInfo,
       
   254          "+ L2CapPushServerConnection::isActive result: %d", result);
       
   255     return result;
       
   256 }
       
   257 
       
   258 OS_EXPORT bool L2CapPushServerConnection::isConnectionAccepted()
       
   259 {
       
   260     return mPendingConnection;
       
   261 }
       
   262 
       
   263 OS_EXPORT bool L2CapPushServerConnection::isListening()
       
   264 {
       
   265     return mListening;
       
   266 }
       
   267 
       
   268 OS_EXPORT void L2CapPushServerConnection::setCreatedByPush()
       
   269 {
       
   270     mCreatedByPush = true;
       
   271 }
       
   272 
       
   273 OS_EXPORT bool L2CapPushServerConnection::isCreatedByPush()
       
   274 {
       
   275     return mCreatedByPush;
       
   276 }
       
   277 OS_EXPORT L2CAPServerConnection* L2CapPushServerConnection::getServerObject()
       
   278 {
       
   279     JELOG2(EJavaBluetooth);
       
   280     if (mL2CAPServer)
       
   281     {
       
   282         LOG(EJavaBluetooth, EInfo,
       
   283             "- L2CapPushServerConnection::getServerObject Returning existing L2CAP Server");
       
   284         return mL2CAPServer;
       
   285     }
       
   286     else
       
   287     {
       
   288         mL2CAPServer = new L2CAPServerConnection(mFunctionServer);
       
   289         LOG(EJavaBluetooth, EInfo,
       
   290             "- L2CapPushServerConnection::getServerObject Returning new L2CAP Server");
       
   291         return mL2CAPServer;
       
   292     }
       
   293 }
       
   294 
       
   295 OS_EXPORT void L2CapPushServerConnection::deleteServer()
       
   296 {
       
   297     JELOG2(EJavaBluetooth);
       
   298     if (mL2CAPServer)
       
   299     {
       
   300         delete mL2CAPServer;
       
   301         mL2CAPServer = NULL;
       
   302     }
       
   303 }
       
   304 
       
   305 OS_EXPORT
       
   306 BluetoothClientConnection* L2CapPushServerConnection::getConnectedClient()
       
   307 {
       
   308     JELOG2(EJavaBluetooth);
       
   309     //NOTE: ISSUE: IS there a chance that there can be multiple clients
       
   310     //connecting to the push midlet before launch of midlet?
       
   311     //In that case we need to return clients in the order in which they were
       
   312     //accepted.
       
   313     //Code will be something like return acceptedConnections->deQueue()
       
   314     //and in handleConnectionRequest we must
       
   315     //have acceptedConnection->enQueue(client);
       
   316     //
       
   317     BtL2CapServerConnectionFactory& connectionFactory =
       
   318         BtL2CapServerConnectionFactory::getFactory();
       
   319     connectionFactory.setPendingMsgFlag(mConnectionUri, false);
       
   320 
       
   321     // Handed over the accepted connection. And hence, resetting the flag  back
       
   322     mPendingConnection = false;
       
   323 
       
   324     return mClientConnection;
       
   325 }
       
   326 
       
   327 } //end namespace bluetooth
       
   328 } //end namespace java