javaextensions/bluetooth/omjbluetooth/src.s60/discoveryagent.cpp
changeset 21 2a9601315dfc
child 72 1f0034e370aa
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     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 "discoveryagent.h"
       
    20 #include "bluetoothdevicediscoverer.h"
       
    21 #include "fs_methodcall.h"
       
    22 #include "javajniutils.h"
       
    23 #include "exceptionbase.h"
       
    24 
       
    25 using namespace java::util;
       
    26 
       
    27 namespace java
       
    28 {
       
    29 namespace bluetooth
       
    30 {
       
    31 
       
    32 DiscoveryAgent::DiscoveryAgent(BluetoothFunctionServer* aServer)
       
    33 {
       
    34     JELOG2(EJavaBluetooth);
       
    35     // Initializing discovery agent data
       
    36     mFunctionServer = aServer;
       
    37     mSocketServerStatus = SOCKETSERVER_DISCONNECTED;
       
    38 
       
    39     // Initializing device discovery data
       
    40     mDeviceDiscoverer = NULL;
       
    41 }
       
    42 
       
    43 DiscoveryAgent::~DiscoveryAgent()
       
    44 {
       
    45     JELOG2(EJavaBluetooth);
       
    46     cleanupDeviceDiscoverer();
       
    47 }
       
    48 
       
    49 /**
       
    50  * Starts the device inquiry. After starts the inquiry, call made to the java
       
    51  * indicates that inquiry started.
       
    52  *
       
    53  * @param aJni
       
    54  * @param aPeer
       
    55  * @param aAccessCode Inquiry code
       
    56  * @param aMonitor    Monitor which waits for this operation to complete
       
    57  */
       
    58 int DiscoveryAgent::startDeviceDiscovery(JNIEnv* aJni, jobject& aPeer,
       
    59         int aAccessCode)
       
    60 {
       
    61     JELOG2(EJavaBluetooth);
       
    62 
       
    63     jclass peerClass = (*aJni).GetObjectClass(aPeer);
       
    64 
       
    65     //Get Method ID of Device Discovered Callback
       
    66     mDeviceDiscoveredMethod = aJni->GetMethodID(peerClass,
       
    67                               "deviceDiscoveredCallBack",
       
    68                               "(Ljava/lang/String;Ljava/lang/String;I)V");
       
    69 
       
    70     //We call Device Inquiry Started Callback in BluetoothStackS60 itself
       
    71     mDeviceDiscoveryStartedMethod = aJni->GetMethodID(peerClass,
       
    72                                     "deviceInquiryStartedCallback", "()V");
       
    73 
       
    74     //Check if all the JNI inits have succeeded
       
    75     if (NULL == mDeviceDiscoveredMethod || NULL
       
    76             == mDeviceDiscoveryStartedMethod)
       
    77     {
       
    78         //Error in initialization
       
    79         ELOG(EJavaBluetooth,
       
    80              "- DiscoveryAgent::startDeviceDiscovery Error in JNIInit");
       
    81         return -1;
       
    82     }
       
    83 
       
    84     TRAPD(err, CallMethodL(this,
       
    85                            &java::bluetooth::DiscoveryAgent::discoverDevicesFsL, aAccessCode,
       
    86                            mFunctionServer));
       
    87 
       
    88     if (KErrNone != err)
       
    89     {
       
    90         cleanupDeviceDiscoverer();
       
    91 
       
    92         ELOG1(EJavaBluetooth,
       
    93               "- DiscoveryAgent::startDeviceDiscovery Error %d", err);
       
    94         return err;
       
    95     }
       
    96 
       
    97     LOG(EJavaBluetooth, EInfo,
       
    98         " DiscoveryAgent::startDeviceDiscovery Started Callback");
       
    99 
       
   100     startedDeviceDiscovery(aJni, aPeer);
       
   101 
       
   102     return err;
       
   103 }
       
   104 
       
   105 void DiscoveryAgent::discoverDevicesFsL(int aAccessCode)
       
   106 {
       
   107     mDeviceDiscoverer = BluetoothDeviceDiscoverer::NewL(mFunctionServer);
       
   108     CleanupStack::PushL(mDeviceDiscoverer);
       
   109     mDeviceDiscoverer->DiscoverDevicesL(aAccessCode);
       
   110     CleanupStack::Pop(mDeviceDiscoverer);
       
   111 }
       
   112 
       
   113 /**
       
   114  * Cancels the device inquiry
       
   115  */
       
   116 bool DiscoveryAgent::cancelDeviceDiscovery()
       
   117 {
       
   118     JELOG2(EJavaBluetooth);
       
   119 
       
   120     if (mDeviceDiscoverer)
       
   121     {
       
   122         mDeviceDiscoverer->cancel();
       
   123     }
       
   124     return true;
       
   125 }
       
   126 
       
   127 /**
       
   128  * Makes callback to Java indicates that the inquiry started.
       
   129  */
       
   130 void DiscoveryAgent::startedDeviceDiscovery(JNIEnv* aJni, jobject& aPeer)
       
   131 {
       
   132     JELOG2(EJavaBluetooth);
       
   133     //Call method in Java here to notify that device discovery has been started
       
   134     //This too happens in Function Server Context.
       
   135 
       
   136     (*aJni).CallVoidMethod(aPeer, mDeviceDiscoveryStartedMethod);
       
   137 
       
   138 }
       
   139 
       
   140 /**
       
   141  * Searching for the next device.
       
   142  * Returns immediately, if already any device discovered;
       
   143  * otherwise waits for the new device.
       
   144  */
       
   145 int DiscoveryAgent::getNextDevice(DiscoveredDevice &aDiscoveredDevice)
       
   146 {
       
   147     JELOG2(EJavaBluetooth);
       
   148     int status = mDeviceDiscoverer->getNextDevice(aDiscoveredDevice);
       
   149     if (STATUS_DEVICE_FOUND != status)
       
   150     {
       
   151         cleanupDeviceDiscoverer();
       
   152     }
       
   153     return status;
       
   154 }
       
   155 
       
   156 /**
       
   157  * Makes callback to the Java only when new device found.
       
   158  * Returns the inquiry status.
       
   159  */
       
   160 int DiscoveryAgent::doDeviceDiscoveryCallback(JNIEnv* aJni, jobject& aPeer,
       
   161         DiscoveredDevice &aDiscoveredDevice)
       
   162 {
       
   163     JELOG2(EJavaBluetooth);
       
   164 
       
   165     jstring devAddr = NULL;
       
   166     jstring devName = NULL;
       
   167     int deviceClass = 0;
       
   168 
       
   169     if (STATUS_DEVICE_FOUND == aDiscoveredDevice.mInquiryStatus)
       
   170     {
       
   171         LOG(EJavaBluetooth, EInfo,
       
   172             "  DiscoveryAgent::doDeviceDiscoveryCallback: Device discovered callback");
       
   173 
       
   174         if (NULL != aDiscoveredDevice.mDeviceAddr)
       
   175         {
       
   176             try
       
   177             {
       
   178                 devAddr = java::util::JniUtils::wstringToJstring(aJni,
       
   179                           *(aDiscoveredDevice.mDeviceAddr));
       
   180             }
       
   181             catch (ExceptionBase ex)
       
   182             {
       
   183                 // Nothing to handle
       
   184             }
       
   185         }
       
   186         else
       
   187         {
       
   188             ELOG(EJavaBluetooth,
       
   189                  "  DiscoveryAgent Really Serious Error: Address is NULL !!");
       
   190         }
       
   191 
       
   192         if (NULL != aDiscoveredDevice.mDeviceName)
       
   193         {
       
   194             try
       
   195             {
       
   196                 devName = java::util::JniUtils::wstringToJstring(aJni,
       
   197                           *(aDiscoveredDevice.mDeviceName));
       
   198             }
       
   199             catch (ExceptionBase ex)
       
   200             {
       
   201                 // Nothing to handle
       
   202             }
       
   203         }
       
   204         else
       
   205         {
       
   206             ELOG(EJavaBluetooth,
       
   207                  "  DiscoveryAgent::doDeviceDiscoveryCallback: Device Name is NULL");
       
   208         }
       
   209 
       
   210         deviceClass = aDiscoveredDevice.mDeviceClass;
       
   211 
       
   212         (*aJni).CallVoidMethod(aPeer, mDeviceDiscoveredMethod, devAddr,
       
   213                                devName, deviceClass);
       
   214     }
       
   215 
       
   216     LOG1(EJavaBluetooth, EInfo,
       
   217          "- DiscoveryAgent::doDeviceDiscoveryCallback: %d",
       
   218          aDiscoveredDevice.mInquiryStatus);
       
   219     return aDiscoveredDevice.mInquiryStatus;
       
   220 }
       
   221 
       
   222 /**
       
   223  * Deletes the BluetoothDeviceDiscoverer
       
   224  */
       
   225 void DiscoveryAgent::cleanupDeviceDiscoverer()
       
   226 {
       
   227     JELOG2(EJavaBluetooth);
       
   228     delete mDeviceDiscoverer;
       
   229     mDeviceDiscoverer = NULL;
       
   230 }
       
   231 
       
   232 /**
       
   233  * Starts lookup for the friendly name of the device
       
   234  * On success, returns the friendly name of the device
       
   235  * Otherwise NULL
       
   236  *
       
   237  * Note: Must free resultant friendly name, whenever not required.
       
   238  */
       
   239 std::wstring* DiscoveryAgent::lookupFriendlyNameL(long long aDevAddr)
       
   240 {
       
   241     JELOG2(EJavaBluetooth);
       
   242     LOG1(EJavaBluetooth, EInfo,
       
   243          "+ DiscoveryAgent::lookupFriendlyName DeviceAddress:%x", aDevAddr);
       
   244 
       
   245     TInt64 devAddr = (TInt64) aDevAddr;
       
   246     std::wstring* result = NULL;
       
   247 
       
   248     CallMethodL(result, this, &java::bluetooth::DiscoveryAgent::nameLookupL,
       
   249                 devAddr, mFunctionServer);
       
   250 
       
   251     return result;
       
   252 }
       
   253 
       
   254 /**
       
   255  * Creates the BluetoothNameLookup object
       
   256  */
       
   257 std::wstring* DiscoveryAgent::nameLookupL(TInt64 aDevAddr)
       
   258 {
       
   259     JELOG2(EJavaBluetooth);
       
   260     std::wstring* result = NULL;
       
   261     BluetoothNameLookup * nameLookup = BluetoothNameLookup::NewL();
       
   262     CleanupStack::PushL(nameLookup);
       
   263     result = nameLookup->doDeviceNameLookupL(aDevAddr);
       
   264     CleanupStack::Pop(nameLookup);
       
   265     delete nameLookup;
       
   266     return result;
       
   267 }
       
   268 
       
   269 /*
       
   270  *   ------- Service Search Methods --------
       
   271  */
       
   272 int DiscoveryAgent::StartSearchServices(JNIEnv* aJni, jobject& aPeer,
       
   273                                         TInt64 aRemoteAddress, TPtrC8 aUuidsDes, TPtrC16 aAttrIdsDes,
       
   274                                         java::util::Monitor* aMonitor)
       
   275 {
       
   276     JELOG2(EJavaBluetooth);
       
   277 
       
   278     mServiceSearchMonitor = aMonitor;
       
   279     mServiceSearchStatus = -1;
       
   280 
       
   281     jclass peerClass = (*aJni).GetObjectClass(aPeer);
       
   282 
       
   283     //Get Method ID of Service Search Started Callback
       
   284     mServiceSearchStartedMethod = aJni->GetMethodID(peerClass,
       
   285                                   "serviceSearchStartedCallback", "()V");
       
   286 
       
   287     //Check if all the JNI inits have succeeded
       
   288     if (NULL == mServiceSearchStartedMethod)
       
   289     {
       
   290         //Error in initialization
       
   291         ELOG(EJavaBluetooth,
       
   292              "- DiscoveryAgent::StartSearchServices: Error in JNIInit ");
       
   293         return -1;
       
   294     }
       
   295 
       
   296     LOG(EJavaBluetooth, EInfo,
       
   297         "  DiscoveryAgent::StartSearchServices: Starting service search ");
       
   298 
       
   299     TRAPD(err, CallMethodL(this,
       
   300                            &java::bluetooth::DiscoveryAgent::searchServiceFsL, aRemoteAddress,
       
   301                            aUuidsDes, aAttrIdsDes, mFunctionServer));
       
   302 
       
   303     if (KErrNone == err)
       
   304     {
       
   305         StartedServiceSearch(aJni, aPeer);
       
   306     }
       
   307 
       
   308     return err;
       
   309 }
       
   310 
       
   311 void DiscoveryAgent::searchServiceFsL(TInt64 aRemoteAddress, TPtrC8 aUuidsDes,
       
   312                                       TPtrC16 aAttrIdsDes)
       
   313 {
       
   314     mServiceSearcher = BluetoothServiceSearcher::New(this, mFunctionServer);
       
   315 
       
   316     TRAPD(err, mServiceSearcher->SearchServicesL(aRemoteAddress, aUuidsDes,
       
   317             aAttrIdsDes));
       
   318     if (KErrNone != err)
       
   319     {
       
   320         ELOG(EJavaBluetooth,
       
   321              "  DiscoveryAgent::SearchServiceFsL: Error occured!! ");
       
   322         CleanupServiceSearcher();
       
   323         User::LeaveIfError(err);
       
   324     }
       
   325 }
       
   326 
       
   327 void DiscoveryAgent::StartedServiceSearch(JNIEnv* aJni, jobject& aPeer)
       
   328 {
       
   329     JELOG2(EJavaBluetooth);
       
   330     mIsServiceSearchOn = true;
       
   331 
       
   332     (*aJni).CallVoidMethod(aPeer, mServiceSearchStartedMethod);
       
   333 
       
   334 }
       
   335 
       
   336 void DiscoveryAgent::ServiceSearchCompleted(int aStatus)
       
   337 {
       
   338     JELOG2(EJavaBluetooth);
       
   339     mIsServiceSearchOn = false;
       
   340     mServiceSearchStatus = aStatus;
       
   341     mServiceSearchMonitor->notify();
       
   342     CleanupServiceSearcher();
       
   343 }
       
   344 
       
   345 bool DiscoveryAgent::CancelServiceSearch()
       
   346 {
       
   347     JELOG2(EJavaBluetooth);
       
   348     if (NULL != mServiceSearcher)
       
   349     {
       
   350         CallMethod(
       
   351             this,
       
   352             &java::bluetooth::DiscoveryAgent::cancelAndCleanupServiceSearchFs,
       
   353             mFunctionServer);
       
   354     }
       
   355     CleanupAllServiceSearcherInfo();
       
   356     mIsServiceSearchOn = false;
       
   357     return true;
       
   358 }
       
   359 
       
   360 void DiscoveryAgent::cancelAndCleanupServiceSearchFs()
       
   361 {
       
   362     mServiceSearcher->cancelServiceSearch();
       
   363     CleanupServiceSearcher();
       
   364 }
       
   365 
       
   366 int DiscoveryAgent::getStatusOfCompletion()
       
   367 {
       
   368     JELOG2(EJavaBluetooth);
       
   369     LOG1(EJavaBluetooth, EInfo, "+ DiscoveryAgent::getStatusOfCompletion: %d",
       
   370          mServiceSearchStatus);
       
   371     return mServiceSearchStatus;
       
   372 }
       
   373 
       
   374 int DiscoveryAgent::PopulateServiceRecordAttrValue(JNIEnv* aJni,
       
   375         jobject& aPeer, TInt64 aRemoteAddress, long aHandle,
       
   376         TPtrC16 aAttrIdsDes, jobject aServiceRecordImpl,
       
   377         java::util::Monitor* aMonitor)
       
   378 {
       
   379     JELOG2(EJavaBluetooth);
       
   380 
       
   381     mServiceSearchMonitor = aMonitor;
       
   382 
       
   383     jclass peerClass = (*aJni).GetObjectClass(aPeer);
       
   384 
       
   385     LOG(EJavaBluetooth, EInfo,
       
   386         "  DiscoveryAgent::PopulateServiceRecord: Starting populating service records ");
       
   387     TRAPD(err, CallMethodL(this,
       
   388                            &java::bluetooth::DiscoveryAgent::populateServiceRecordAttrValueFs,
       
   389                            aRemoteAddress, aHandle, aAttrIdsDes, aServiceRecordImpl,
       
   390                            mFunctionServer));
       
   391 
       
   392     return err;
       
   393 }
       
   394 
       
   395 void DiscoveryAgent::populateServiceRecordAttrValueFs(TInt64 aRemoteAddress,
       
   396         long aHandle, TPtrC16 aAttrIdsDes, jobject aServiceRecordImpl)
       
   397 {
       
   398     mServiceSearcher = BluetoothServiceSearcher::New(this, mFunctionServer);
       
   399     mServiceSearcher->PopulateServiceRecordsL(aRemoteAddress, aHandle,
       
   400             aAttrIdsDes, aServiceRecordImpl);
       
   401 }
       
   402 
       
   403 void DiscoveryAgent::PopulateRecordCompleted(int aStatus)
       
   404 {
       
   405     JELOG2(EJavaBluetooth);
       
   406     mPopulateRecordStatus = aStatus;
       
   407     mServiceSearchMonitor->notify();
       
   408     CleanupServiceSearcher();
       
   409 }
       
   410 
       
   411 int DiscoveryAgent::GetPopulateServiceRecordStatus()
       
   412 {
       
   413     JELOG2(EJavaBluetooth);
       
   414     return mPopulateRecordStatus;
       
   415 }
       
   416 
       
   417 bool DiscoveryAgent::CancelPopulateServiceRecordAttrValue()
       
   418 {
       
   419     JELOG2(EJavaBluetooth);
       
   420     mIsServiceSearchOn = false;
       
   421     TRAP_IGNORE(CallMethodL(mServiceSearcher,
       
   422                             &java::bluetooth::BluetoothServiceSearcher::cancelServiceSearch,
       
   423                             mFunctionServer));
       
   424 
       
   425     return true;
       
   426 }
       
   427 
       
   428 void DiscoveryAgent::CleanupServiceSearcher()
       
   429 {
       
   430     JELOG2(EJavaBluetooth);
       
   431     //Delete all contained objects
       
   432     if (mServiceSearcher)
       
   433     {
       
   434         delete mServiceSearcher;
       
   435         mServiceSearcher = NULL;
       
   436     }
       
   437 }
       
   438 
       
   439 void DiscoveryAgent::CleanupAllServiceSearcherInfo()
       
   440 {
       
   441     JELOG2(EJavaBluetooth);
       
   442     // Clear all realted data
       
   443     mServiceSearchStartedMethod = NULL;
       
   444     mIsServiceSearchOn = false;
       
   445     mPopulateRecordStatus = KErrNone;
       
   446     mServiceSearchMonitor = NULL;
       
   447 }
       
   448 
       
   449 } //end namespace bluetooth
       
   450 } //end namespace java