javaextensions/bluetooth/omjbluetooth/src.s60/bluetoothstacks60native.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 <e32cmn.h>
       
    20 #include <bttypes.h>
       
    21 
       
    22 #include "functionserver.h"
       
    23 #include "javajniutils.h"
       
    24 #include "jniarrayutils.h"
       
    25 #include "jstringutils.h"
       
    26 #include "logger.h"
       
    27 #include "fs_methodcall.h"
       
    28 #include "monitor.h"
       
    29 #include "exceptionbase.h"
       
    30 
       
    31 #include "bluetoothconsts.h"
       
    32 #include "bluetoothfunctionserver.h"
       
    33 #include "discoveryagent.h"
       
    34 #include "bluetoothclientconnection.h"
       
    35 #include "btl2capserverconnection.h"
       
    36 #include "btrfcommserverconnection.h"
       
    37 #include "bluetoothstructs.h"
       
    38 #include "servicerecord.h"
       
    39 #include "bluetoothremotedevice.h"
       
    40 
       
    41 //Push related stuff
       
    42 #include "btl2cappushserverconnection.h"
       
    43 #include "btl2capserverconnectionfactory.h"
       
    44 #include "btrfcommpushserverconnection.h"
       
    45 #include "btrfcommserverconnectionfactory.h"
       
    46 #include "btobexserverconnectionfactory.h"
       
    47 #include "pushexception.h"
       
    48 #include "pusherrorcodes.h"
       
    49 
       
    50 #include "com_nokia_mj_impl_bluetooth_BluetoothStackS60.h"
       
    51 
       
    52 using namespace std;
       
    53 using namespace java::util;
       
    54 using namespace java::push;
       
    55 using namespace java::bluetooth;
       
    56 
       
    57 //
       
    58 // JNI functions specific to Bluetooth Stack
       
    59 //
       
    60 
       
    61 JNIEXPORT jlong JNICALL
       
    62 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1createFunctionServer
       
    63 (JNIEnv *aJni, jobject aPeer)
       
    64 {
       
    65     JELOG2(EJavaBluetooth);
       
    66     java::bluetooth::BluetoothFunctionServer* server =
       
    67         new java::bluetooth::BluetoothFunctionServer(*aJni, aPeer);
       
    68     return reinterpret_cast<jlong>(server);
       
    69 }
       
    70 
       
    71 
       
    72 //
       
    73 // JNI functions specific to Discovery Agent
       
    74 //
       
    75 
       
    76 JNIEXPORT jlong JNICALL
       
    77 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1createNativeDiscoveryAgent
       
    78 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aServer)
       
    79 {
       
    80     JELOG2(EJavaBluetooth);
       
    81     java::bluetooth::BluetoothFunctionServer *server =
       
    82         reinterpret_cast<java::bluetooth::BluetoothFunctionServer *>(
       
    83             static_cast<long>(aServer));
       
    84 
       
    85     java::bluetooth::DiscoveryAgent* agent =
       
    86         new java::bluetooth::DiscoveryAgent(server);
       
    87 
       
    88     jlong ret = reinterpret_cast<jlong>(agent);
       
    89     return ret;
       
    90 }
       
    91 
       
    92 
       
    93 //
       
    94 // JNI functions specific to Device Discovery
       
    95 //
       
    96 
       
    97 JNIEXPORT jint JNICALL
       
    98 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1runDeviceInquiry
       
    99 (JNIEnv *aJni, jobject aPeer, jint aAccessCode, jlong aDiscAgent)
       
   100 {
       
   101     JELOG2(EJavaBluetooth);
       
   102     LOG1(EJavaBluetooth, EInfo, "+ JNI::runDeviceInquiry %X", aAccessCode);
       
   103 
       
   104     java::bluetooth::DiscoveryAgent* agent =
       
   105         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   106             static_cast<long>(aDiscAgent));
       
   107 
       
   108     int result = KErrNone;
       
   109 
       
   110     result = agent->startDeviceDiscovery(aJni, aPeer, aAccessCode);
       
   111 
       
   112     if (KErrNone != result)
       
   113     {
       
   114         ELOG1(EJavaBluetooth,
       
   115               "- JNI::runDeviceInquiry Error before starting inquiry %d",
       
   116               result);
       
   117 
       
   118         java::util::JniUtils::throwNewException(aJni,
       
   119                                                 "javax/bluetooth/BluetoothStateException",
       
   120                                                 "Device inquiry failed. Symbian OS error: "
       
   121                                                 + JavaCommonUtils::intToString(result));
       
   122         return INQUIRY_ERROR;
       
   123     }
       
   124 
       
   125     int discStatus = 0;
       
   126 
       
   127     do
       
   128     {
       
   129         //Wait for discovery agent to unlock this.
       
   130         LOG(EJavaBluetooth, EInfo,
       
   131             "  JNI::runDeviceInquiry: Getting next device");
       
   132 
       
   133         DiscoveredDevice device;
       
   134 
       
   135         discStatus = agent->getNextDevice(device);
       
   136 
       
   137         agent->doDeviceDiscoveryCallback(aJni, aPeer, device);
       
   138 
       
   139         device.clean();
       
   140 
       
   141         LOG1(EJavaBluetooth, EInfo,
       
   142              "  JNI::runDeviceInquiry doDeviceDiscoveryCallback return: %d",
       
   143              discStatus);
       
   144 
       
   145     }
       
   146     while ((INQUIRY_ERROR != discStatus) &&
       
   147             (INQUIRY_COMPLETED != discStatus) &&
       
   148             (INQUIRY_TERMINATED != discStatus));
       
   149 
       
   150     return discStatus;
       
   151 }
       
   152 
       
   153 JNIEXPORT jboolean JNICALL
       
   154 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1cancelInquiry
       
   155 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aDiscAgent)
       
   156 {
       
   157     JELOG2(EJavaBluetooth);
       
   158     java::bluetooth::DiscoveryAgent* agent =
       
   159         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   160             static_cast<long>(aDiscAgent));
       
   161 
       
   162     jboolean ret = agent->cancelDeviceDiscovery();
       
   163     return ret;
       
   164 }
       
   165 
       
   166 JNIEXPORT jstring JNICALL
       
   167 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1getRemoteDeviceFriendlyName
       
   168 (JNIEnv* aJni, jobject /*aPeer*/, jlong aAgent, jlong aAddr)
       
   169 {
       
   170     JELOG2(EJavaBluetooth);
       
   171     LOG1(EJavaBluetooth, EInfo,
       
   172          "+ JNI::getRemoteDeviceFriendlyName DeviceAddress:%ld",
       
   173          aAddr);
       
   174 
       
   175     java::bluetooth::DiscoveryAgent* dAgent =
       
   176         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   177             static_cast<long>(aAgent));
       
   178 
       
   179     std::wstring* friendlyName = NULL;
       
   180     TRAPD(err,
       
   181           friendlyName = dAgent->lookupFriendlyNameL(aAddr));
       
   182 
       
   183     if (KErrNone != err)
       
   184     {
       
   185         ELOG1(EJavaBluetooth,
       
   186               "- JNI::getRemoteDeviceFriendlyName while lookupFriendlyNameL:%d",
       
   187               err);
       
   188         java::util::JniUtils::throwNewException(aJni,
       
   189                                                 "java/io/IOException",
       
   190                                                 "Retrieving remote device friendly name failed. Symbian OS error: "
       
   191                                                 + JavaCommonUtils::intToString(err));
       
   192         return NULL;
       
   193     }
       
   194 
       
   195     jstring ret;
       
   196     try
       
   197     {
       
   198         if (NULL != friendlyName)
       
   199         {
       
   200             ret = java::util::JniUtils::wstringToJstring(aJni, *friendlyName);
       
   201             delete friendlyName;
       
   202         }
       
   203         else
       
   204         {
       
   205             std::wstring str(L"");
       
   206             ret = java::util::JniUtils::wstringToJstring(aJni, str);
       
   207         }
       
   208     }
       
   209     catch (ExceptionBase ex)
       
   210     {
       
   211         ELOG1(EJavaBluetooth,
       
   212               "- JNI::getRemoteDeviceFriendlyName Exception:%S", ex.what());
       
   213         java::util::JniUtils::throwNewException(aJni,
       
   214                                                 "java/lang/RunTimeException", "Retrieving friendly name failed");
       
   215         delete friendlyName;
       
   216         return NULL;
       
   217     }
       
   218     return ret;
       
   219 }
       
   220 
       
   221 //
       
   222 // JNI functions specific to Service Search & Populate Records
       
   223 //
       
   224 
       
   225 JNIEXPORT jint JNICALL
       
   226 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1runSearchServices
       
   227 (JNIEnv *aEnv, jobject aPeer, jlong aRemoteAddress, jbyteArray aUuidSet,
       
   228  jintArray aAttrSet, jlong aDiscAgent)
       
   229 {
       
   230     JELOG2(EJavaBluetooth);
       
   231     java::bluetooth::DiscoveryAgent* agent =
       
   232         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   233             static_cast<long>(aDiscAgent));
       
   234 
       
   235     // ...convert the UUID bytes into an 8-bit descriptor...
       
   236     jbyte* byteArray = aEnv->GetByteArrayElements(aUuidSet, NULL);
       
   237     TInt numUuidBytes = aEnv->GetArrayLength(aUuidSet);
       
   238     TPtrC8 uuidsDes(reinterpret_cast<TUint8*>(byteArray), numUuidBytes);
       
   239 
       
   240     // ...convert the Attribute IDs into pairs of 16-bits in a descriptor...
       
   241     int numAttrIds = aEnv->GetArrayLength(aAttrSet);
       
   242     jint* intArray = aEnv->GetIntArrayElements(aAttrSet, NULL);
       
   243     TPtrC16 attrIdsDes(reinterpret_cast<TUint16*>(intArray), numAttrIds*2);
       
   244 
       
   245     java::util::Monitor* serviceSearchMonitor =
       
   246         java::util::Monitor::createMonitor();
       
   247 
       
   248     LOG(EJavaBluetooth, EInfo,
       
   249         "   JNI::runSearchServices calling StartSearchServices");
       
   250     int result = KErrNone;
       
   251 
       
   252     result = agent->StartSearchServices(
       
   253                  aEnv, aPeer, aRemoteAddress, uuidsDes, attrIdsDes,
       
   254                  serviceSearchMonitor);
       
   255     LOG(EJavaBluetooth, EInfo,
       
   256         "  JNI::runSearchServices returned from StartSearchServices");
       
   257 
       
   258     if (KErrNone != result)
       
   259     {
       
   260         LOG(EJavaBluetooth, EInfo,
       
   261             "  JNI::runSearchServices Error occurred before starting service search!");
       
   262         delete serviceSearchMonitor;
       
   263         serviceSearchMonitor = NULL;
       
   264         return SERVICE_SEARCH_ERROR;
       
   265     }
       
   266 
       
   267     int searchStatus = -1;
       
   268 
       
   269     LOG(EJavaBluetooth, EInfo,
       
   270         "  JNI::runSearchServices waiting for search complete");
       
   271 
       
   272     while ((SERVICE_SEARCH_ERROR != searchStatus) &&
       
   273             (SERVICE_SEARCH_COMPLETED != searchStatus) &&
       
   274             (SERVICE_SEARCH_TERMINATED != searchStatus) &&
       
   275             (SERVICE_SEARCH_NO_RECORDS != searchStatus) &&
       
   276             (SERVICE_SEARCH_DEVICE_NOT_REACHABLE != searchStatus))
       
   277     {
       
   278         //Wait for discoveryagent to unlock this.
       
   279         serviceSearchMonitor->wait();
       
   280         searchStatus = agent->getStatusOfCompletion();
       
   281         LOG1(EJavaBluetooth, EInfo,
       
   282              "  JNI::runSearchServices doServiceDiscoveredCallback return: %d",
       
   283              searchStatus);
       
   284     }
       
   285 
       
   286     delete serviceSearchMonitor;
       
   287     serviceSearchMonitor = NULL;
       
   288     LOG1(EJavaBluetooth, EInfo,
       
   289          "- JNI::runSearchServices status:%d", searchStatus);
       
   290     return searchStatus;
       
   291 }
       
   292 
       
   293 JNIEXPORT jboolean JNICALL
       
   294 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1cancelServiceSearch
       
   295 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aDiscAgent)
       
   296 {
       
   297     JELOG2(EJavaBluetooth);
       
   298     java::bluetooth::DiscoveryAgent* agent =
       
   299         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   300             static_cast<long>(aDiscAgent));
       
   301 
       
   302     jboolean ret = agent->CancelServiceSearch();
       
   303     return ret;
       
   304 }
       
   305 
       
   306 JNIEXPORT jint JNICALL
       
   307 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1populateServiceRecordAttributeValues
       
   308 (JNIEnv *aEnv, jobject aPeer, jlong aRemoteDevice, jlong aHandle,
       
   309  jintArray aAttrSet, jobject aServiceRecordImpl, jlong aDiscAgent)
       
   310 {
       
   311     JELOG2(EJavaBluetooth);
       
   312     java::bluetooth::DiscoveryAgent* agent =
       
   313         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   314             static_cast<long>(aDiscAgent));
       
   315 
       
   316     // ...convert the Attribute IDs into pairs of 16-bits in a descriptor...
       
   317     int numAttrIds = aEnv->GetArrayLength(aAttrSet);
       
   318     jint* intArray = aEnv->GetIntArrayElements(aAttrSet, NULL);
       
   319     TPtrC16 attrIdsDes(reinterpret_cast<TUint16*>(intArray), numAttrIds*2);
       
   320 
       
   321     java::util::Monitor* serviceSearchMonitor =
       
   322         java::util::Monitor::createMonitor();
       
   323 
       
   324     jobject serviceRecGlobalRef = aEnv->NewGlobalRef(aServiceRecordImpl);
       
   325 
       
   326     LOG(EJavaBluetooth, EInfo,
       
   327         "   JNI::populateServiceRecordAttributeValues calling PopulateServiceRecord");
       
   328     int result = KErrNone;
       
   329     result = agent->PopulateServiceRecordAttrValue(
       
   330                  aEnv, aPeer, aRemoteDevice, aHandle, attrIdsDes, serviceRecGlobalRef,
       
   331                  serviceSearchMonitor);
       
   332 
       
   333     if (KErrNone != result)
       
   334     {
       
   335         aEnv->DeleteGlobalRef(serviceRecGlobalRef);
       
   336         return SERVICE_SEARCH_ERROR;
       
   337     }
       
   338 
       
   339     // Waiting for populateServiceRecord to finish
       
   340     serviceSearchMonitor->wait();
       
   341 
       
   342     aEnv->DeleteGlobalRef(serviceRecGlobalRef);
       
   343 
       
   344     return agent->GetPopulateServiceRecordStatus();
       
   345 }
       
   346 
       
   347 JNIEXPORT void JNICALL
       
   348 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1cancelPopulateServicesRecordAttributeValues
       
   349 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aDiscAgent)
       
   350 {
       
   351     JELOG2(EJavaBluetooth);
       
   352     java::bluetooth::DiscoveryAgent* agent =
       
   353         reinterpret_cast<java::bluetooth::DiscoveryAgent *>(
       
   354             static_cast<long>(aDiscAgent));
       
   355 
       
   356     jboolean result = agent->CancelPopulateServiceRecordAttrValue();
       
   357     LOG1(EJavaBluetooth, EInfo,
       
   358          "- JNI::cancelPopulateServicesRecordAttributeValues Result:%d",
       
   359          result);
       
   360     return;
       
   361 }
       
   362 
       
   363 JNIEXPORT jlong JNICALL
       
   364 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1getRemoteAddress
       
   365 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aHandle)
       
   366 {
       
   367     JELOG2(EJavaBluetooth);
       
   368     BluetoothClientConnection *conn =
       
   369         reinterpret_cast<BluetoothClientConnection *>(
       
   370             static_cast<long>(aHandle));
       
   371 
       
   372     return conn->getRemoteAddress();
       
   373 }
       
   374 
       
   375 JNIEXPORT jlong JNICALL
       
   376 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2OpenClientConnection
       
   377 (JNIEnv* aJni, jobject /*peer*/, jlong aBtAddress, jint aChannel,
       
   378  jboolean aAuthenticate, jboolean aEncrypt, jint aReceiveMTU,
       
   379  jint aTransmitMTU, jlong aServer)
       
   380 {
       
   381     JELOG2(EJavaBluetooth);
       
   382     LOG1(EJavaBluetooth, EInfo,
       
   383          "+ JNI:l2OpenClientConnection: btAddress = %x", aBtAddress);
       
   384 
       
   385     BluetoothFunctionServer *server =
       
   386         reinterpret_cast<BluetoothFunctionServer *>(
       
   387             static_cast<long>(aServer));
       
   388 
       
   389     BluetoothClientConnection* conn = new BluetoothClientConnection(server);
       
   390 
       
   391     //Initialize Bluetooth Connection to L2CAP
       
   392     int error = conn->init(PROTOCOL_L2CAP);
       
   393     if (error != 0)
       
   394     {
       
   395         // If initialization was not successful,
       
   396         // then delete BluetoothClientConnection.
       
   397         conn->close();
       
   398         delete conn;
       
   399         ELOG1(EJavaBluetooth,
       
   400               "JNI:l2OpenClientConnection: Error during init: %d", error);
       
   401 
       
   402         jclass cls = aJni->FindClass(
       
   403                          "javax/bluetooth/BluetoothConnectionException");
       
   404 
       
   405         jmethodID constructorID = aJni->GetMethodID(cls, "<init>", "(I)V");
       
   406         jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   407                                      cls, constructorID, BC_FAILED_NOINFO);
       
   408 
       
   409         aJni->Throw(bcexception);
       
   410         // free the local ref
       
   411         aJni->DeleteLocalRef(cls);
       
   412         return error;
       
   413     }
       
   414 
       
   415     //Connect to the server
       
   416     error = conn->connect(aBtAddress, aChannel, aAuthenticate, aEncrypt,
       
   417                           aReceiveMTU, aTransmitMTU);
       
   418 
       
   419     if (error<0)
       
   420     {
       
   421         ELOG1(EJavaBluetooth,
       
   422               "JNI:l2OpenClientConnection: Error during connect: %d",
       
   423               error);
       
   424         jclass cls = aJni->FindClass(
       
   425                          "javax/bluetooth/BluetoothConnectionException");
       
   426         jmethodID constructorID = aJni->GetMethodID(cls, "<init>", "(I)V");
       
   427         if (error == HCI_TIMEOUT)
       
   428         {
       
   429             jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   430                                          cls, constructorID, BC_TIMEOUT);
       
   431             aJni->Throw(bcexception);
       
   432         }
       
   433         else
       
   434         {
       
   435             jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   436                                          cls, constructorID, BC_FAILED_NOINFO);
       
   437             aJni->Throw(bcexception);
       
   438         }
       
   439         // free the local ref
       
   440         aJni->DeleteLocalRef(cls);
       
   441         conn->close();
       
   442         delete conn;
       
   443         return error;
       
   444     }
       
   445 
       
   446     LOG1(EJavaBluetooth, EInfo,
       
   447          "- JNI:l2OpenClientConnection: Handle = %ld", conn);
       
   448     return reinterpret_cast<jlong>(conn);
       
   449 }
       
   450 
       
   451 JNIEXPORT jlong JNICALL
       
   452 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfClientOpen
       
   453 (JNIEnv* aJni, jobject /*peer*/, jlong aBtAddress, jint aChannel,
       
   454  jboolean aAuthenticate, jboolean aEncrypt, jlong aServer)
       
   455 {
       
   456     JELOG2(EJavaBluetooth);
       
   457     LOG1(EJavaBluetooth, EInfo,
       
   458          "+ JNI:rfClientOpen: btAddress = %ld", aBtAddress);
       
   459     BluetoothFunctionServer *server =
       
   460         reinterpret_cast<BluetoothFunctionServer *>(
       
   461             static_cast<long>(aServer));
       
   462 
       
   463     BluetoothClientConnection* conn = new BluetoothClientConnection(server);
       
   464 
       
   465     //Initialize Bluetooth Connection to RFCOMM
       
   466     int error = conn->init(PROTOCOL_RFCOMM);
       
   467     if (error != 0)
       
   468     {
       
   469         // If initialization was not successful,
       
   470         // delete BluetoothClientConnection.
       
   471         conn->close();
       
   472         delete conn;
       
   473         ELOG1(EJavaBluetooth,
       
   474               "JNI:rfClientOpen: Error during init: %d", error);
       
   475 
       
   476         jclass cls = aJni->FindClass(
       
   477                          "javax/bluetooth/BluetoothConnectionException");
       
   478         jmethodID constructorID = aJni->GetMethodID(cls, "<init>", "(I)V");
       
   479         jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   480                                      cls, constructorID, BC_FAILED_NOINFO);
       
   481         aJni->Throw(bcexception);
       
   482         // free the local ref
       
   483         aJni->DeleteLocalRef(cls);
       
   484         return error;
       
   485     }
       
   486 
       
   487     //Connect to the server
       
   488     error = conn->connect(aBtAddress, aChannel, aAuthenticate, aEncrypt);
       
   489     if (error<0)
       
   490     {
       
   491         ELOG1(EJavaBluetooth,
       
   492               "JNI:rfClientOpen: Error during connect: %d", error);
       
   493         jclass cls = aJni->FindClass(
       
   494                          "javax/bluetooth/BluetoothConnectionException");
       
   495         jmethodID constructorID = aJni->GetMethodID(cls, "<init>", "(I)V");
       
   496         if (error == HCI_TIMEOUT)
       
   497         {
       
   498             jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   499                                          cls, constructorID, BC_TIMEOUT);
       
   500             aJni->Throw(bcexception);
       
   501         }
       
   502         else
       
   503         {
       
   504             jthrowable bcexception = (jthrowable) aJni->NewObject(
       
   505                                          cls, constructorID, BC_FAILED_NOINFO);
       
   506             aJni->Throw(bcexception);
       
   507         }
       
   508         // free the local ref
       
   509         aJni->DeleteLocalRef(cls);
       
   510         conn->close();
       
   511         delete conn;
       
   512         return error;
       
   513     }
       
   514 
       
   515     LOG1(EJavaBluetooth, EInfo, "- JNI:rfClientOpen: Handle %ld", conn);
       
   516     return reinterpret_cast<jlong>(conn);
       
   517 }
       
   518 
       
   519 JNIEXPORT jint JNICALL
       
   520 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2GetTransmitMTU
       
   521 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   522 {
       
   523     JELOG2(EJavaBluetooth);
       
   524     BluetoothClientConnection *conn =
       
   525         reinterpret_cast<BluetoothClientConnection *>(
       
   526             static_cast<long>(aHandle));
       
   527 
       
   528     int retVal = conn->getTransmitMTU();
       
   529     return retVal;
       
   530 }
       
   531 
       
   532 JNIEXPORT jint JNICALL
       
   533 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2GetReceiveMTU
       
   534 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   535 {
       
   536     JELOG2(EJavaBluetooth);
       
   537     BluetoothClientConnection *conn =
       
   538         reinterpret_cast<BluetoothClientConnection *>(
       
   539             static_cast<long>(aHandle));
       
   540 
       
   541     int retVal = conn->getReceiveMTU();
       
   542     return retVal;
       
   543 }
       
   544 
       
   545 JNIEXPORT jint JNICALL
       
   546 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1available
       
   547 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   548 {
       
   549     JELOG2(EJavaBluetooth);
       
   550     BluetoothClientConnection *conn =
       
   551         reinterpret_cast<BluetoothClientConnection *>(
       
   552             static_cast<long>(aHandle));
       
   553 
       
   554     int retVal = conn->available();
       
   555     return retVal;
       
   556 }
       
   557 
       
   558 JNIEXPORT jint JNICALL
       
   559 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1clientReceive
       
   560 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle, jbyteArray aBuffer)
       
   561 {
       
   562     JELOG2(EJavaBluetooth);
       
   563     BluetoothClientConnection *conn =
       
   564         reinterpret_cast<BluetoothClientConnection *>(
       
   565             static_cast<long>(aHandle));
       
   566 
       
   567     // Memory needs to be allocated inside ClientConnection implementation.
       
   568     // Reason to keep this here is to prevent passing JNI parameters to client.
       
   569     char* bufferToFill = NULL;
       
   570     int length = 0;
       
   571 
       
   572     // Length returned will be positive if no errors resent.
       
   573     // Else it will be error code.
       
   574     length = conn->receive(bufferToFill);
       
   575 
       
   576     if (length>0)
       
   577     {
       
   578         if (NULL == bufferToFill)
       
   579         {
       
   580             LOG(EJavaBluetooth, EInfo,
       
   581                 "JNI_BluetoothStackS60_clientReceive: Buffer NULL");
       
   582             return 0; //EOF
       
   583         }
       
   584 
       
   585         LOG1(EJavaBluetooth, EInfo,
       
   586              "JNI_BluetoothStackS60_clientReceive: Data %s",
       
   587              bufferToFill);
       
   588 
       
   589         JNIArrayUtils::CopyToJava((*aJni),
       
   590                                   bufferToFill, length, aBuffer, 0, length);
       
   591     }
       
   592     LOG1(EJavaBluetooth, EInfo,
       
   593          "JNI_BluetoothStackS60_clientReceive: Returning %d", length);
       
   594 
       
   595     delete[] bufferToFill;
       
   596     return length;
       
   597 }
       
   598 
       
   599 JNIEXPORT jint JNICALL
       
   600 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1clientSend
       
   601 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle,
       
   602  jbyteArray aBuffer, jlong aLength)
       
   603 {
       
   604     JELOG2(EJavaBluetooth);
       
   605     BluetoothClientConnection *conn =
       
   606         reinterpret_cast<BluetoothClientConnection *>(
       
   607             static_cast<long>(aHandle));
       
   608 
       
   609     char* bufferToSend = new char[aLength+1];
       
   610 
       
   611     JNIArrayUtils::CopyToNative((*aJni), aBuffer, 0, aLength, bufferToSend);
       
   612     bufferToSend[aLength] = '\0';
       
   613     int res = conn->send(bufferToSend, aLength);
       
   614     LOG1(EJavaBluetooth,EInfo,
       
   615          "Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1clientSend: Sent %d"
       
   616          , res);
       
   617 
       
   618     delete[] bufferToSend;
       
   619 
       
   620     return res;
       
   621 }
       
   622 
       
   623 JNIEXPORT void JNICALL
       
   624 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1clientClose
       
   625 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   626 {
       
   627     JELOG2(EJavaBluetooth);
       
   628     BluetoothClientConnection *conn =
       
   629         reinterpret_cast<BluetoothClientConnection *>(
       
   630             static_cast<long>(aHandle));
       
   631 
       
   632     conn->close();
       
   633     delete conn;
       
   634 }
       
   635 
       
   636 /*
       
   637  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   638  * Method:    registerForCallback
       
   639  * Signature: (J)V
       
   640  */
       
   641 JNIEXPORT void JNICALL
       
   642 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1registerForCallback
       
   643 (JNIEnv *aJni, jobject peer, jlong aHandle)
       
   644 {
       
   645     JELOG2(EJavaBluetooth);
       
   646     BluetoothClientConnection *conn =
       
   647         reinterpret_cast<BluetoothClientConnection *>(
       
   648             static_cast<long>(aHandle));
       
   649 
       
   650     conn->registerCallback(aJni, peer);
       
   651 }
       
   652 
       
   653 //------------------------------------------------------------------------------
       
   654 //    L2CAP Server Implementations
       
   655 //------------------------------------------------------------------------------
       
   656 
       
   657 JNIEXPORT jboolean JNICALL
       
   658 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1isL2ServerAlreadyRunning
       
   659 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   660 {
       
   661     JELOG2(EJavaBluetooth);
       
   662     L2CapPushServerConnection* server =
       
   663         reinterpret_cast<L2CapPushServerConnection *>(
       
   664             static_cast<long>(aHandle));
       
   665 
       
   666     return server->isActive();
       
   667 }
       
   668 
       
   669 JNIEXPORT void JNICALL
       
   670 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1restoreL2ServiceRecord(
       
   671     JNIEnv *aJni, jobject /*peer*/,
       
   672     jlong aHandle, jobject aServiceRecordImpl)
       
   673 {
       
   674     JELOG2(EJavaBluetooth);
       
   675     L2CapPushServerConnection* server =
       
   676         reinterpret_cast<L2CapPushServerConnection *>(
       
   677             static_cast<long>(aHandle));
       
   678 
       
   679     L2CAPServerConnection *srvHandle = server->getServerObject();
       
   680 
       
   681     ServiceRecord* srvRecHandle = srvHandle->getServiceRecordHandle();
       
   682     if (NULL != srvRecHandle)
       
   683     {
       
   684         TRAP_IGNORE(
       
   685             srvRecHandle->restoreJavaServiceRecordL(aJni, aServiceRecordImpl));
       
   686     }
       
   687 }
       
   688 
       
   689 /*
       
   690  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   691  * Method:    l2ServerOpen
       
   692  * Signature: (ZZZZZII)J
       
   693  */
       
   694 JNIEXPORT jlong JNICALL
       
   695 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2ServerOpen
       
   696 (JNIEnv *aJni, jobject /*peer*/, jstring aUrl, jboolean aAuthorize,
       
   697  jboolean aAuthenticate, jboolean aEncrypt, jboolean aMaster,
       
   698  jint aReceiveMTU, jint aTransmitMTU)
       
   699 {
       
   700     JELOG2(EJavaBluetooth);
       
   701     LOG2(EJavaBluetooth, EInfo,
       
   702          "+ JNI::l2ServerOpen TxMTU:%d RxMTU:%d",
       
   703          aTransmitMTU, aReceiveMTU);
       
   704 
       
   705     std::wstring connectionUrl;
       
   706     try
       
   707     {
       
   708         connectionUrl = JniUtils::jstringToWstring(aJni, aUrl);
       
   709     }
       
   710     catch (ExceptionBase ex)
       
   711     {
       
   712         ELOG1(EJavaBluetooth,
       
   713               "- JNI::l2ServerOpen Exception:%S", ex.what());
       
   714         java::util::JniUtils::throwNewException(aJni,
       
   715                                                 "java/lang/RunTimeException",
       
   716                                                 "Opening L2CAP server connection failed");
       
   717         return NULL;
       
   718     }
       
   719 
       
   720     //Get handle to ServerConnectionFactory object.
       
   721     BtL2CapServerConnectionFactory& connectionFactory =
       
   722         BtL2CapServerConnectionFactory::getFactory();
       
   723 
       
   724     L2CapPushServerConnection *l2CapPushServer;
       
   725 
       
   726     ServerConnection *serverConn = connectionFactory.create(connectionUrl);
       
   727     l2CapPushServer =
       
   728         reinterpret_cast<L2CapPushServerConnection*>(serverConn);
       
   729 
       
   730     if (l2CapPushServer->isActive())
       
   731     {
       
   732         //if we have already accepted connection or are listening, we need not
       
   733         //go any further. return handle.
       
   734         LOG(EJavaBluetooth, EInfo,
       
   735             "- JNI::l2ServerOpen: Bluetooth server already running ");
       
   736         return reinterpret_cast<jlong>(l2CapPushServer);
       
   737     }
       
   738     else
       
   739     {
       
   740         LOG(EJavaBluetooth, EInfo,
       
   741             "  JNI::l2ServerOpen: Trying to create and run a bluetooth server ");
       
   742 
       
   743         L2CAPServerConnection* srvHandle = l2CapPushServer->getServerObject();
       
   744 
       
   745         LOG(EJavaBluetooth, EInfo,
       
   746             "  JNI::l2ServerOpen: Calling open on Server Object");
       
   747         TInt err;
       
   748 
       
   749         err = srvHandle->ServerOpen(aAuthorize, aAuthenticate, aEncrypt,
       
   750                                     aMaster, aReceiveMTU, aTransmitMTU);
       
   751 
       
   752         if (err != KErrNone)
       
   753         {
       
   754             ELOG1(EJavaBluetooth,
       
   755                   "- JNI::l2ServerOpen Error on L2CAPServerConnection.ServerOpen: %d",
       
   756                   err);
       
   757             if (l2CapPushServer->isCreatedByPush())
       
   758             {
       
   759                 connectionFactory.releaseConnection(connectionUrl);
       
   760             }
       
   761             else
       
   762             {
       
   763                 l2CapPushServer->close();
       
   764                 delete l2CapPushServer;
       
   765             }
       
   766             java::util::JniUtils::throwNewException(aJni,
       
   767                                                     "java/io/IOException",
       
   768                                                     "Opening L2CAP server connection failed. Symbian OS error: "
       
   769                                                     + JavaCommonUtils::intToString(err));
       
   770             return NULL;
       
   771         }
       
   772         return reinterpret_cast<jlong>(l2CapPushServer);
       
   773     }
       
   774 }
       
   775 
       
   776 /*
       
   777  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   778  * Method:    l2ServerGetPSM
       
   779  * Signature: (J)I
       
   780  */
       
   781 JNIEXPORT jint JNICALL
       
   782 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2ServerGetPSM
       
   783 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle)
       
   784 {
       
   785     JELOG2(EJavaBluetooth);
       
   786     L2CapPushServerConnection* server =
       
   787         reinterpret_cast<L2CapPushServerConnection *>(
       
   788             static_cast<long>(aHandle));
       
   789 
       
   790     L2CAPServerConnection *srvHandle = server->getServerObject();
       
   791 
       
   792     //We should never be here if push plugin is already running.
       
   793     int psmVal = srvHandle->GetServerPSM();
       
   794 
       
   795     if (psmVal < 0)
       
   796     {
       
   797         ELOG1(EJavaBluetooth,
       
   798               "- JNI::l2ServerGetPSM Error while GetServerPSM: %d",
       
   799               psmVal);
       
   800 
       
   801         java::util::JniUtils::throwNewException(aJni,
       
   802                                                 "java/io/IOException",
       
   803                                                 "Opening L2CAP server connection failed. Symbian OS error: "
       
   804                                                 + JavaCommonUtils::intToString(psmVal));
       
   805     }
       
   806 
       
   807     LOG1(EJavaBluetooth, EInfo, "- JNI::l2ServerGetPSM PSM value:%d ", psmVal);
       
   808     return psmVal;
       
   809 }
       
   810 
       
   811 /*
       
   812  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   813  * Method:    l2ServerGetServiceRecord
       
   814  * Signature: (J)I
       
   815  */
       
   816 JNIEXPORT jint JNICALL
       
   817 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2ServerGetServiceRecord
       
   818 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   819 {
       
   820     JELOG2(EJavaBluetooth);
       
   821 
       
   822     L2CapPushServerConnection* server =
       
   823         reinterpret_cast<L2CapPushServerConnection *>(
       
   824             static_cast<long>(aHandle));
       
   825 
       
   826     L2CAPServerConnection *srvHandle = server->getServerObject();
       
   827     int servRecId = 0;
       
   828     ServiceRecord* srvRecHandle = srvHandle->getServiceRecordHandle();
       
   829 
       
   830     if (NULL != srvRecHandle)
       
   831     {
       
   832         servRecId = srvRecHandle->getServiceRecordID();
       
   833     }
       
   834     LOG1(EJavaBluetooth, EInfo,
       
   835          "- JNI::l2ServerGetServiceRecord: %d", servRecId);
       
   836     return servRecId;
       
   837 }
       
   838 
       
   839 /*
       
   840  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   841  * Method:    l2capRegisterSDPRecord
       
   842  * Signature: (JI[BLjava/lang/String;)I
       
   843  */
       
   844 JNIEXPORT void JNICALL
       
   845 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2capRegisterSDPRecord
       
   846 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle, jint aPsmVal,
       
   847  jbyteArray aUuid, jstring aServerName)
       
   848 {
       
   849     JELOG2(EJavaBluetooth);
       
   850     TInt err;
       
   851 
       
   852     L2CapPushServerConnection* server =
       
   853         reinterpret_cast<L2CapPushServerConnection *>(
       
   854             static_cast<long>(aHandle));
       
   855 
       
   856     if (server->isActive())
       
   857     {
       
   858         //If the server is active it means that it has already done registration
       
   859         //and has started advertising.
       
   860         //If connection has already been accepted, then no issues at all.
       
   861         return;
       
   862     }
       
   863 
       
   864     L2CAPServerConnection *srvHandle = server->getServerObject();
       
   865 
       
   866     jbyte* byteArray = aJni->GetByteArrayElements(aUuid, NULL);
       
   867 
       
   868     TPtrC8 uuidDes(reinterpret_cast<TUint8*>(byteArray),
       
   869                    aJni->GetArrayLength(aUuid));
       
   870     TUUID uuid;
       
   871 
       
   872     TRAP(err, uuid.SetL(uuidDes));
       
   873     if (err != KErrNone)
       
   874     {
       
   875         ELOG1(EJavaBluetooth,
       
   876               "- JNI::l2capRegisterSDPRecord Error while converting UUID: %d",
       
   877               err);
       
   878         java::util::JniUtils::throwNewException(aJni,
       
   879                                                 "java/io/IOException",
       
   880                                                 "Opening L2CAP server connection failed. Symbian OS error: "
       
   881                                                 + JavaCommonUtils::intToString(err));
       
   882         return;
       
   883     }
       
   884 
       
   885     JStringUtils servName(*aJni, aServerName);
       
   886     TPtrC8 servNameP(
       
   887         reinterpret_cast<const TUint8*>(servName.Ptr()),
       
   888         servName.Size());
       
   889 
       
   890     err = srvHandle->initializeServiceRecord(aPsmVal, uuid, servNameP);
       
   891 
       
   892     if (err != KErrNone)
       
   893     {
       
   894         ELOG1(EJavaBluetooth,
       
   895               "- JNI::l2capRegisterSDPRecord Error while initializeServiceRecord: %d",
       
   896               err);
       
   897         java::util::JniUtils::throwNewException(aJni,
       
   898                                                 "java/io/IOException",
       
   899                                                 "Opening L2CAP server connection failed. Symbian OS error: "
       
   900                                                 + JavaCommonUtils::intToString(err));
       
   901     }
       
   902 
       
   903 }
       
   904 
       
   905 /*
       
   906  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   907  * Method:    l2ServerAcceptAndOpenServerConnection
       
   908  * Signature: (J)J
       
   909  */
       
   910 JNIEXPORT jlong JNICALL
       
   911 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2ServerAcceptAndOpenServerConnection
       
   912 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle, jintArray aReceiveMtu)
       
   913 {
       
   914     JELOG2(EJavaBluetooth);
       
   915 
       
   916     BluetoothClientConnection *clientHandle;
       
   917     int receiveMtu = 0;
       
   918 
       
   919     L2CapPushServerConnection* server =
       
   920         reinterpret_cast<L2CapPushServerConnection *>(
       
   921             static_cast<long>(aHandle));
       
   922 
       
   923     if (server->isConnectionAccepted())
       
   924     {
       
   925         LOG(EJavaBluetooth, EInfo,
       
   926             "  JNI::l2ServerAcceptAndOpenServerConnection: returning already accepted push client ");
       
   927         //Since connection has already been accepted, return the client object.
       
   928         clientHandle = server->getConnectedClient();
       
   929         receiveMtu = clientHandle->getReceiveMTU();
       
   930         aJni->SetIntArrayRegion(aReceiveMtu, 0, 1, &receiveMtu);
       
   931         return reinterpret_cast<jlong>(clientHandle);
       
   932     }
       
   933 
       
   934     if (server->isListening())
       
   935     {
       
   936         //Wait till connect and then return.
       
   937         //Then we return client here.
       
   938         java::util::Monitor* acceptMonitor =
       
   939             java::util::Monitor::createMonitor();
       
   940 
       
   941         server->setAcceptMonitor(acceptMonitor);
       
   942         LOG(EJavaBluetooth, EInfo,
       
   943             "  JNI::l2ServerAcceptAndOpenServerConnection: waiting for connection accept ");
       
   944         acceptMonitor->wait();
       
   945         server->unsetAcceptMonitor();
       
   946         delete acceptMonitor;
       
   947 
       
   948         clientHandle = server->getConnectedClient();
       
   949         receiveMtu = clientHandle->getReceiveMTU();
       
   950         aJni->SetIntArrayRegion(aReceiveMtu, 0, 1, &receiveMtu);
       
   951         return reinterpret_cast<jlong>(clientHandle);
       
   952     }
       
   953 
       
   954     // NOTE: In case we reached here, it means that there was no corresponding
       
   955     // push plugin active when the midlet was launched.
       
   956     // So we go ahead with usual stuff.
       
   957 
       
   958     L2CAPServerConnection *srvHandle = server->getServerObject();
       
   959 
       
   960     long retVal = srvHandle->Accept();
       
   961 
       
   962     if (retVal < KErrNone)
       
   963     {
       
   964         ELOG1(EJavaBluetooth,
       
   965               "  JNI::l2ServerAcceptAndOpenServerConnection Error while Accept: %d ",
       
   966               retVal);
       
   967 
       
   968         if (KErrCancel == retVal)
       
   969         {
       
   970             java::util::JniUtils::throwNewException(aJni,
       
   971                                                     "java/io/InterruptedIOException",
       
   972                                                     "acceptAndOpen() failed: L2CAP server connection is closed");
       
   973         }
       
   974         else
       
   975         {
       
   976             java::util::JniUtils::throwNewException(aJni,
       
   977                                                     "java/io/IOException",
       
   978                                                     "acceptAndOpen() on L2CAP server connection failed. Symbian OS error: "
       
   979                                                     + JavaCommonUtils::intToString(retVal));
       
   980         }
       
   981     }
       
   982     else
       
   983     {
       
   984         clientHandle = reinterpret_cast<BluetoothClientConnection *>(retVal);
       
   985         receiveMtu = clientHandle->getReceiveMTU();
       
   986         aJni->SetIntArrayRegion(aReceiveMtu, 0, 1, &receiveMtu);
       
   987     }
       
   988     return retVal;
       
   989 }
       
   990 
       
   991 /*
       
   992  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
   993  * Method:    l2ServerClose
       
   994  * Signature: (J)I
       
   995  */
       
   996 JNIEXPORT jint JNICALL
       
   997 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1l2ServerClose
       
   998 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
   999 {
       
  1000     JELOG2(EJavaBluetooth);
       
  1001     L2CapPushServerConnection* server =
       
  1002         reinterpret_cast<L2CapPushServerConnection *>(
       
  1003             static_cast<long>(aHandle));
       
  1004 
       
  1005     BtL2CapServerConnectionFactory& connectionFactory =
       
  1006         BtL2CapServerConnectionFactory::getFactory();
       
  1007 
       
  1008     std::wstring uri = server->getUri();
       
  1009 
       
  1010     server->unsetClearServiceClassBitsFlag();
       
  1011     if (server->isCreatedByPush())
       
  1012     {
       
  1013         connectionFactory.releaseConnection(uri);
       
  1014     }
       
  1015     else
       
  1016     {
       
  1017         server->close();
       
  1018         delete server;
       
  1019     }
       
  1020 
       
  1021     return 0;
       
  1022 }
       
  1023 
       
  1024 //-----------------------------------------------------------------------------
       
  1025 //    RFCOMM Server Implementations
       
  1026 //-----------------------------------------------------------------------------
       
  1027 
       
  1028 
       
  1029 JNIEXPORT jboolean JNICALL
       
  1030 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1isRfServerAlreadyRunning
       
  1031 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
  1032 {
       
  1033     JELOG2(EJavaBluetooth);
       
  1034     RFCOMMPushServerConnection* server =
       
  1035         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1036             static_cast<long>(aHandle));
       
  1037 
       
  1038     return server->isActive();
       
  1039 }
       
  1040 
       
  1041 JNIEXPORT void JNICALL
       
  1042 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1restoreRfServiceRecord(
       
  1043     JNIEnv *aJni, jobject /*peer*/,
       
  1044     jlong aHandle, jobject aServiceRecordImpl)
       
  1045 {
       
  1046     JELOG2(EJavaBluetooth);
       
  1047 
       
  1048     RFCOMMPushServerConnection* server =
       
  1049         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1050             static_cast<long>(aHandle));
       
  1051 
       
  1052     RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1053 
       
  1054     ServiceRecord* srvRecHandle = srvHandle->getServiceRecordHandle();
       
  1055 
       
  1056     if (NULL != srvRecHandle)
       
  1057     {
       
  1058         TRAP_IGNORE(
       
  1059             srvRecHandle->restoreJavaServiceRecordL(aJni, aServiceRecordImpl));
       
  1060     }
       
  1061 }
       
  1062 
       
  1063 /*
       
  1064  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1065  * Method:    rfServerOpen
       
  1066  * Signature: (ZZZJ)J
       
  1067  */
       
  1068 JNIEXPORT jlong JNICALL
       
  1069 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfServerOpen
       
  1070 (JNIEnv *aJni, jobject /*peer*/, jstring aUrl, jboolean aAuthorize,
       
  1071  jboolean aAuthenticate, jboolean aEncrypt, jboolean aIsGOEP)
       
  1072 {
       
  1073     JELOG2(EJavaBluetooth);
       
  1074 
       
  1075     std::wstring connectionUrl;
       
  1076     try
       
  1077     {
       
  1078         connectionUrl = JniUtils::jstringToWstring(aJni, aUrl);
       
  1079     }
       
  1080     catch (ExceptionBase ex)
       
  1081     {
       
  1082         ELOG1(EJavaBluetooth,
       
  1083               "- JNI::rfServerOpen Exception:%S", ex.what());
       
  1084         java::util::JniUtils::throwNewException(aJni,
       
  1085                                                 "java/lang/RunTimeException",
       
  1086                                                 "Opening server connection failed");
       
  1087         return NULL;
       
  1088     }
       
  1089 
       
  1090     //Get handle to ServerConnectionFactory object.
       
  1091     ServerConnectionFactory* connectionFactory = 0;
       
  1092     if (!aIsGOEP)
       
  1093     {
       
  1094         connectionFactory = &BTRFCOMMServerConnectionFactory::getFactory();
       
  1095     }
       
  1096     else
       
  1097     {
       
  1098         connectionFactory = &BTOBEXServerConnectionFactory::getFactory();
       
  1099     }
       
  1100 
       
  1101     RFCOMMPushServerConnection *rfcommPushServer;
       
  1102 
       
  1103     ServerConnection *serverConn =
       
  1104         connectionFactory->create(connectionUrl);
       
  1105     rfcommPushServer =
       
  1106         reinterpret_cast<RFCOMMPushServerConnection*>(serverConn);
       
  1107 
       
  1108     if (rfcommPushServer->isActive())
       
  1109     {
       
  1110         //if we have already accepted connection or are listening, we need not
       
  1111         //go any further. return handle.
       
  1112         LOG(EJavaBluetooth, EInfo,
       
  1113             "- JNI::rfServerOpen: Bluetooth server already running ");
       
  1114         return reinterpret_cast<jlong>(rfcommPushServer);
       
  1115     }
       
  1116     else
       
  1117     {
       
  1118         LOG(EJavaBluetooth, EInfo,
       
  1119             "  JNI::rfServerOpen: Opening server ");
       
  1120         RFCOMMServerConnection* srvHandle =
       
  1121             rfcommPushServer->getServerObject();
       
  1122 
       
  1123         TInt err;
       
  1124 
       
  1125         err = srvHandle->ServerOpen(aAuthorize, aAuthenticate, aEncrypt);
       
  1126 
       
  1127         if (err != KErrNone)
       
  1128         {
       
  1129             ELOG1(EJavaBluetooth,
       
  1130                   "- JNI::rfServerOpen Error on RFCOMMServerConnection.ServerOpen: %d",
       
  1131                   err);
       
  1132             if (rfcommPushServer->isCreatedByPush())
       
  1133             {
       
  1134                 connectionFactory->releaseConnection(connectionUrl);
       
  1135             }
       
  1136             else
       
  1137             {
       
  1138                 rfcommPushServer->close();
       
  1139                 delete rfcommPushServer;
       
  1140             }
       
  1141 
       
  1142             java::util::JniUtils::throwNewException(aJni,
       
  1143                                                     "java/io/IOException",
       
  1144                                                     "Opening server connection failed. Symbian OS error: "
       
  1145                                                     + JavaCommonUtils::intToString(err));
       
  1146             return NULL;
       
  1147         }
       
  1148     }
       
  1149     return reinterpret_cast<jlong>(rfcommPushServer);
       
  1150 }
       
  1151 
       
  1152 /*
       
  1153  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1154  * Method:    rfcommRegisterSDPRecord
       
  1155  * Signature: (JI[BLjava/lang/String;)V
       
  1156  */
       
  1157 JNIEXPORT void JNICALL
       
  1158 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfcommRegisterSDPRecord
       
  1159 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle, jint aChannel,
       
  1160  jbyteArray aUuid, jstring aServerName, jboolean aIsObex)
       
  1161 {
       
  1162     JELOG2(EJavaBluetooth);
       
  1163     TInt err;
       
  1164 
       
  1165     RFCOMMPushServerConnection* server =
       
  1166         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1167             static_cast<long>(aHandle));
       
  1168 
       
  1169     if (server->isActive())
       
  1170     {
       
  1171         //If the server is active it means that it has already done
       
  1172         //registration and has started advertising.
       
  1173         //If connection has already been accepted, then no issues at all.
       
  1174         return;
       
  1175     }
       
  1176 
       
  1177     LOG(EJavaBluetooth, EInfo,
       
  1178         "  JNI::rfcommRegisterSDPRecord Getting server object");
       
  1179 
       
  1180     RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1181 
       
  1182     jbyte* byteArray = aJni->GetByteArrayElements(aUuid, NULL);
       
  1183     TPtrC8 uuidDes(
       
  1184         reinterpret_cast<TUint8*>(byteArray), aJni->GetArrayLength(aUuid));
       
  1185     TUUID uuid;
       
  1186 
       
  1187     TRAP(err, uuid.SetL(uuidDes));
       
  1188     if (err != KErrNone)
       
  1189     {
       
  1190         ELOG1(EJavaBluetooth,
       
  1191               "- JNI::rfcommRegisterSDPRecord Error while converting UUID: %d",
       
  1192               err);
       
  1193         java::util::JniUtils::throwNewException(aJni,
       
  1194                                                 "java/io/IOException",
       
  1195                                                 "Opening server connection failed. Symbian OS error: "
       
  1196                                                 + JavaCommonUtils::intToString(err));
       
  1197         return;
       
  1198     }
       
  1199 
       
  1200     JStringUtils servName(*aJni, aServerName);
       
  1201     TPtrC8 servNameP(
       
  1202         reinterpret_cast<const TUint8*>(servName.Ptr()), servName.Size());
       
  1203 
       
  1204     err = srvHandle->initializeServiceRecord(
       
  1205               aChannel, uuid, servNameP, aIsObex);
       
  1206 
       
  1207     if (err != KErrNone)
       
  1208     {
       
  1209         ELOG1(EJavaBluetooth,
       
  1210               "- JNI::rfcommRegisterSDPRecord Error while initializeServiceRecord: %d",
       
  1211               err);
       
  1212 
       
  1213         java::util::JniUtils::throwNewException(aJni,
       
  1214                                                 "java/io/IOException",
       
  1215                                                 "Opening server connection failed. Symbian OS error: "
       
  1216                                                 + JavaCommonUtils::intToString(err));
       
  1217     }
       
  1218 }
       
  1219 
       
  1220 /*
       
  1221  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1222  * Method:    rfGetChannel
       
  1223  * Signature: (J)I
       
  1224  */
       
  1225 JNIEXPORT jint JNICALL
       
  1226 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfGetChannel
       
  1227 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
  1228 {
       
  1229     JELOG2(EJavaBluetooth);
       
  1230 
       
  1231     RFCOMMPushServerConnection* server =
       
  1232         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1233             static_cast<long>(aHandle));
       
  1234 
       
  1235     RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1236     int channel = srvHandle->GetRfListeningChannel();
       
  1237 
       
  1238     LOG1(EJavaBluetooth, EInfo, "- JNI::rfGetChannel: %d", channel);
       
  1239     return channel;
       
  1240 }
       
  1241 
       
  1242 /*
       
  1243  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1244  * Method:    rfServerGetServiceRecord
       
  1245  * Signature: (J)I
       
  1246  */
       
  1247 JNIEXPORT jint JNICALL
       
  1248 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfServerGetServiceRecord
       
  1249 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
  1250 {
       
  1251     JELOG2(EJavaBluetooth);
       
  1252 
       
  1253     RFCOMMPushServerConnection* server =
       
  1254         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1255             static_cast<long>(aHandle));
       
  1256 
       
  1257     RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1258     int servRecId = 0;
       
  1259     ServiceRecord* srvRecHandle = srvHandle->getServiceRecordHandle();
       
  1260 
       
  1261     if (NULL != srvRecHandle)
       
  1262     {
       
  1263         srvRecHandle->getServiceRecordID();
       
  1264     }
       
  1265     LOG1(EJavaBluetooth, EInfo,
       
  1266          "- JNI::rfServerGetServiceRecord: %d", srvRecHandle);
       
  1267     return servRecId;
       
  1268 }
       
  1269 
       
  1270 /*
       
  1271  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1272  * Method:    rfServerAcceptAndOpenServerConnection
       
  1273  * Signature: (J)J
       
  1274  */
       
  1275 JNIEXPORT jlong JNICALL
       
  1276 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfServerAcceptAndOpenServerConnection
       
  1277 (JNIEnv *aJni, jobject /*peer*/, jlong aHandle)
       
  1278 {
       
  1279     JELOG2(EJavaBluetooth);
       
  1280 
       
  1281     RFCOMMPushServerConnection* server =
       
  1282         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1283             static_cast<long>(aHandle));
       
  1284 
       
  1285     if (server->isConnectionAccepted())
       
  1286     {
       
  1287         LOG(EJavaBluetooth, EInfo,
       
  1288             "  JNI::rfServerAcceptAndOpenServerConnection: returning already accepted push client ");
       
  1289         //Since connection has already been accepted, return the client object.
       
  1290         return reinterpret_cast<jlong>(server->getConnectedClient());
       
  1291     }
       
  1292 
       
  1293     if (server->isListening())
       
  1294     {
       
  1295         //Wait till connect and then return.
       
  1296         //Then we return client here.
       
  1297         java::util::Monitor* acceptMonitor =
       
  1298             java::util::Monitor::createMonitor();
       
  1299 
       
  1300         server->setAcceptMonitor(acceptMonitor);
       
  1301         LOG(EJavaBluetooth, EInfo,
       
  1302             "  JNI::rfServerAcceptAndOpenServerConnection: waiting for connection accept ");
       
  1303         acceptMonitor->wait();
       
  1304         server->unsetAcceptMonitor();
       
  1305         delete acceptMonitor;
       
  1306 
       
  1307         return reinterpret_cast<jlong>(server->getConnectedClient());
       
  1308     }
       
  1309 
       
  1310     LOG(EJavaBluetooth, EInfo,
       
  1311         "  JNI::rfServerAcceptAndOpenServerConnection Getting server object");
       
  1312 
       
  1313     //NOTE: In case we reached here, it means that there was no corresponding
       
  1314     //push plugin active when the midlet was launched.
       
  1315     //So we go ahead with usual stuff.
       
  1316 
       
  1317     RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1318 
       
  1319     long clientHandle;
       
  1320 
       
  1321     clientHandle = srvHandle->Accept();
       
  1322 
       
  1323     if (clientHandle < KErrNone)
       
  1324     {
       
  1325         ELOG1(EJavaBluetooth,
       
  1326               "- JNI::rfServerAcceptAndOpenServerConnection Error while Accept: %d ",
       
  1327               clientHandle);
       
  1328 
       
  1329         if (KErrCancel == clientHandle)
       
  1330         {
       
  1331             java::util::JniUtils::throwNewException(aJni,
       
  1332                                                     "java/io/InterruptedIOException",
       
  1333                                                     "acceptAndOpen() on server connection failed: connection is closed ");
       
  1334         }
       
  1335         else
       
  1336         {
       
  1337             java::util::JniUtils::throwNewException(aJni,
       
  1338                                                     "java/io/IOException",
       
  1339                                                     "acceptAndOpen() on server connection failed. Symbian OS error: "
       
  1340                                                     + JavaCommonUtils::intToString(clientHandle));
       
  1341         }
       
  1342     }
       
  1343     return (clientHandle);
       
  1344 }
       
  1345 
       
  1346 /*
       
  1347  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1348  * Method:    rfServerClose
       
  1349  * Signature: (J)I
       
  1350  */
       
  1351 JNIEXPORT jint JNICALL
       
  1352 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1rfServerClose
       
  1353 (JNIEnv* /*aJni*/, jobject /*peer*/, jlong aHandle)
       
  1354 {
       
  1355     JELOG2(EJavaBluetooth);
       
  1356 
       
  1357     RFCOMMPushServerConnection* server =
       
  1358         reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1359             static_cast<long>(aHandle));
       
  1360 
       
  1361     ServerConnectionFactory* connectionFactory = 0;
       
  1362     if (!(server->isGOEPConnection()))
       
  1363     {
       
  1364         connectionFactory = &BTRFCOMMServerConnectionFactory::getFactory();
       
  1365     }
       
  1366     else
       
  1367     {
       
  1368         connectionFactory = &BTOBEXServerConnectionFactory::getFactory();
       
  1369     }
       
  1370     std::wstring uri = server->getUri();
       
  1371 
       
  1372     server->unsetClearServiceClassBitsFlag();
       
  1373     if (server->isCreatedByPush())
       
  1374     {
       
  1375         connectionFactory->releaseConnection(uri);
       
  1376     }
       
  1377     else
       
  1378     {
       
  1379         server->close();
       
  1380         delete server;
       
  1381     }
       
  1382     return 0;
       
  1383 }
       
  1384 
       
  1385 /*
       
  1386  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1387  * Method:    initializeUpdateRecord
       
  1388  * Signature: (Ljava/lang/String;J)V
       
  1389  */
       
  1390 JNIEXPORT void JNICALL
       
  1391 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1initializeUpdateRecord
       
  1392 (JNIEnv *aJni, jobject /*peer*/,
       
  1393  jstring aProtocol, jlong aHandle, jint aDeviceServiceClasses)
       
  1394 {
       
  1395     JELOG2(EJavaBluetooth);
       
  1396     ServiceRecord* srvRec;
       
  1397 
       
  1398     std::wstring protocol;
       
  1399 
       
  1400     try
       
  1401     {
       
  1402         protocol = java::util::JniUtils::jstringToWstring(aJni, aProtocol);
       
  1403     }
       
  1404     catch (ExceptionBase ex)
       
  1405     {
       
  1406         ELOG1(EJavaBluetooth,
       
  1407               "- JNI::initializeUpdateRecord Exception:%S", ex.what());
       
  1408         java::util::JniUtils::throwNewException(aJni,
       
  1409                                                 "java/lang/RunTimeException", "Updating service record failed");
       
  1410         return;
       
  1411     }
       
  1412 
       
  1413     if (0 == protocol.compare(L"btl2cap"))
       
  1414     {
       
  1415         L2CapPushServerConnection* server =
       
  1416             reinterpret_cast<L2CapPushServerConnection *>(
       
  1417                 static_cast<long>(aHandle));
       
  1418 
       
  1419         L2CAPServerConnection *srvHandle = server->getServerObject();
       
  1420         srvRec = srvHandle->getServiceRecordHandle();
       
  1421     }
       
  1422     else //if  protocol is "btspp"
       
  1423     {
       
  1424         RFCOMMPushServerConnection* server =
       
  1425             reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1426                 static_cast<long>(aHandle));
       
  1427 
       
  1428         RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1429         srvRec = srvHandle->getServiceRecordHandle();
       
  1430     }
       
  1431 
       
  1432     srvRec->initializeUpdateRecord(aDeviceServiceClasses);
       
  1433 }
       
  1434 
       
  1435 /*
       
  1436  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1437  * Method:    completesUpdateRecord
       
  1438  * Signature: (Ljava/lang/String;J)V
       
  1439  */
       
  1440 JNIEXPORT void JNICALL
       
  1441 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1completesUpdateRecord
       
  1442 (JNIEnv *aJni, jobject /*peer*/, jstring aProtocol, jlong aHandle)
       
  1443 {
       
  1444     JELOG2(EJavaBluetooth);
       
  1445     ServiceRecord* srvRec;
       
  1446 
       
  1447     std::wstring protocol;
       
  1448 
       
  1449     try
       
  1450     {
       
  1451         protocol = java::util::JniUtils::jstringToWstring(aJni, aProtocol);
       
  1452     }
       
  1453     catch (ExceptionBase ex)
       
  1454     {
       
  1455         ELOG1(EJavaBluetooth,
       
  1456               "- JNI::completesUpdateRecord Exception:%S", ex.what());
       
  1457         java::util::JniUtils::throwNewException(aJni,
       
  1458                                                 "java/lang/RunTimeException", "Updating service record failed");
       
  1459         return;
       
  1460     }
       
  1461     if (0 == protocol.compare(L"btl2cap"))
       
  1462     {
       
  1463         L2CapPushServerConnection* server =
       
  1464             reinterpret_cast<L2CapPushServerConnection *>(
       
  1465                 static_cast<long>(aHandle));
       
  1466 
       
  1467         L2CAPServerConnection *srvHandle = server->getServerObject();
       
  1468 
       
  1469         srvRec = srvHandle->getServiceRecordHandle();
       
  1470     }
       
  1471     else // if protocol is "btspp"
       
  1472     {
       
  1473         RFCOMMPushServerConnection* server =
       
  1474             reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1475                 static_cast<long>(aHandle));
       
  1476 
       
  1477         RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1478 
       
  1479         srvRec = srvHandle->getServiceRecordHandle();
       
  1480     }
       
  1481 
       
  1482     srvRec->completesUpdateRecord();
       
  1483 }
       
  1484 
       
  1485 /*
       
  1486  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1487  * Method:    sdpAddAttribute
       
  1488  * Signature: (Ljava/lang/String;JIIJ[B)V
       
  1489  */
       
  1490 JNIEXPORT void JNICALL
       
  1491 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1sdpAddAttribute
       
  1492 (JNIEnv *aJni, jobject /*peer*/, jstring aProtocol, jlong aHandle,
       
  1493  jint aAttrID, jint aAttrType, jlong aNumValue, jbyteArray aBytesValue)
       
  1494 {
       
  1495     JELOG2(EJavaBluetooth);
       
  1496     ServiceRecord* srvRec;
       
  1497     int err = 0;
       
  1498 
       
  1499     std::wstring protocol;
       
  1500 
       
  1501     try
       
  1502     {
       
  1503         protocol = java::util::JniUtils::jstringToWstring(aJni, aProtocol);
       
  1504     }
       
  1505     catch (ExceptionBase ex)
       
  1506     {
       
  1507         ELOG1(EJavaBluetooth,
       
  1508               "- JNI::sdpAddAttribute Exception:%S", ex.what());
       
  1509         java::util::JniUtils::throwNewException(aJni,
       
  1510                                                 "java/lang/RunTimeException", "Updating service record failed");
       
  1511         return;
       
  1512     }
       
  1513     if (0 == protocol.compare(L"btl2cap"))
       
  1514     {
       
  1515         L2CapPushServerConnection* server =
       
  1516             reinterpret_cast<L2CapPushServerConnection *>(
       
  1517                 static_cast<long>(aHandle));
       
  1518 
       
  1519         L2CAPServerConnection *srvHandle = server->getServerObject();
       
  1520 
       
  1521         srvRec = srvHandle->getServiceRecordHandle();
       
  1522     }
       
  1523     else // if protocol is "btspp"
       
  1524     {
       
  1525         RFCOMMPushServerConnection* server =
       
  1526             reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1527                 static_cast<long>(aHandle));
       
  1528 
       
  1529         RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1530 
       
  1531         srvRec = srvHandle->getServiceRecordHandle();
       
  1532     }
       
  1533 
       
  1534     LOG1(EJavaBluetooth, EInfo,
       
  1535          "  JNI::sdpAddAttribute aAttrType:%d", aAttrType);
       
  1536     switch (aAttrType)
       
  1537     {
       
  1538     case DataElement_NULL:
       
  1539     {
       
  1540         err = srvRec->setAttributeNil(aAttrID);
       
  1541         break;
       
  1542     }
       
  1543     case DataElement_BOOL:
       
  1544     {
       
  1545         TBool val = (aNumValue) ? ETrue : EFalse;
       
  1546 
       
  1547         err = srvRec->setAttributeBool(aAttrID, val);
       
  1548         break;
       
  1549     }
       
  1550     case DataElement_U_INT_1:
       
  1551     case DataElement_INT_1:
       
  1552     case DataElement_U_INT_2:
       
  1553     case DataElement_INT_2:
       
  1554     case DataElement_U_INT_4:
       
  1555     case DataElement_INT_4:
       
  1556     {
       
  1557         ELOG(EJavaBluetooth, "  JNI::sdpAddAttribute UINT/INT/1/2/4 element.");
       
  1558         TSdpIntBuf<TUint> val(aNumValue);
       
  1559 
       
  1560         err = srvRec->setAttributeInt(aAttrID, aAttrType, val);
       
  1561         break;
       
  1562     }
       
  1563     case DataElement_INT_8:
       
  1564     {
       
  1565         TSdpIntBuf<TUint64> val(aNumValue);
       
  1566 
       
  1567         err = srvRec->setAttributeInt(aAttrID, aAttrType, val);
       
  1568         break;
       
  1569     }
       
  1570     case DataElement_U_INT_8:
       
  1571     case DataElement_U_INT_16:
       
  1572     case DataElement_INT_16:
       
  1573     {
       
  1574         jbyte* bytes = aJni->GetByteArrayElements(aBytesValue, NULL);
       
  1575         TPtrC8 val(reinterpret_cast<TUint8*>(bytes),
       
  1576                    aJni->GetArrayLength(aBytesValue));
       
  1577 
       
  1578         err = srvRec->setAttributeUint(aAttrID, aAttrType, val);
       
  1579         break;
       
  1580     }
       
  1581     case DataElement_UUID:
       
  1582     {
       
  1583         jbyte* bytes = aJni->GetByteArrayElements(aBytesValue, NULL);
       
  1584         TPtrC8 uuidDes(reinterpret_cast<TUint8*>(bytes),
       
  1585                        aJni->GetArrayLength(aBytesValue));
       
  1586 
       
  1587         TUUID val;
       
  1588 
       
  1589         TRAP(err, val.SetL(uuidDes));
       
  1590         if (err != KErrNone)
       
  1591         {
       
  1592             ELOG1(EJavaBluetooth,
       
  1593                   "- JNI::sdpAddAttribute Error while converting UUID: %d",
       
  1594                   err);
       
  1595             java::util::JniUtils::throwNewException(aJni,
       
  1596                                                     "javax/bluetooth/ServiceRegistrationException",
       
  1597                                                     "Updating service record failed. Symbian OS error: "
       
  1598                                                     + JavaCommonUtils::intToString(err));
       
  1599             return;
       
  1600         }
       
  1601         err = srvRec->setAttributeUUID(aAttrID, val);
       
  1602         break;
       
  1603     }
       
  1604     case DataElement_STRING:
       
  1605     {
       
  1606         jbyte* bytes = aJni->GetByteArrayElements(aBytesValue, NULL);
       
  1607         TPtrC8 val(reinterpret_cast<TUint8*>(bytes),
       
  1608                    aJni->GetArrayLength(aBytesValue));
       
  1609 
       
  1610         err = srvRec->setAttributeString(aAttrID, val);
       
  1611         break;
       
  1612     }
       
  1613     case DataElement_URL:
       
  1614     {
       
  1615         jbyte* bytes = aJni->GetByteArrayElements(aBytesValue, NULL);
       
  1616         TPtrC8 val(reinterpret_cast<TUint8*>(bytes),
       
  1617                    aJni->GetArrayLength(aBytesValue));
       
  1618 
       
  1619         err = srvRec->setAttributeUrl(aAttrID, val);
       
  1620         break;
       
  1621     }
       
  1622     default:
       
  1623         break;
       
  1624     }
       
  1625 
       
  1626     if (KErrNone != err)
       
  1627     {
       
  1628 
       
  1629         ELOG1(EJavaBluetooth,
       
  1630               "- JNI::sdpAddAttribute Failed to update service record %d",
       
  1631               err);
       
  1632         java::util::JniUtils::throwNewException(aJni,
       
  1633                                                 "javax/bluetooth/ServiceRegistrationException",
       
  1634                                                 "Updating service record failed. Symbian OS error: "
       
  1635                                                 + JavaCommonUtils::intToString(err));
       
  1636     }
       
  1637 }
       
  1638 
       
  1639 /*
       
  1640  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1641  * Method:    sdpAttributeListStart
       
  1642  * Signature: (Ljava/lang/String;JII)V
       
  1643  */
       
  1644 JNIEXPORT void JNICALL
       
  1645 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1sdpAttributeListStart
       
  1646 (JNIEnv *aJni, jobject /*peer*/, jstring aProtocol, jlong aHandle,
       
  1647  jint aAttrID, jint aAttrType)
       
  1648 {
       
  1649     JELOG2(EJavaBluetooth);
       
  1650     ServiceRecord* srvRec;
       
  1651     int err = 0;
       
  1652     std::wstring protocol;
       
  1653 
       
  1654     try
       
  1655     {
       
  1656         protocol = java::util::JniUtils::jstringToWstring(aJni, aProtocol);
       
  1657     }
       
  1658     catch (ExceptionBase ex)
       
  1659     {
       
  1660         ELOG1(EJavaBluetooth,
       
  1661               "- JNI::sdpAttributeListStart Exception:%S", ex.what());
       
  1662         java::util::JniUtils::throwNewException(aJni,
       
  1663                                                 "java/lang/RunTimeException", "Updating service record failed");
       
  1664         return;
       
  1665     }
       
  1666     if (0 == protocol.compare(L"btl2cap"))
       
  1667     {
       
  1668         L2CapPushServerConnection* server =
       
  1669             reinterpret_cast<L2CapPushServerConnection *>(
       
  1670                 static_cast<long>(aHandle));
       
  1671 
       
  1672         L2CAPServerConnection *srvHandle = server->getServerObject();
       
  1673 
       
  1674         srvRec = srvHandle->getServiceRecordHandle();
       
  1675     }
       
  1676     else // if protocol is "btspp"
       
  1677     {
       
  1678         RFCOMMPushServerConnection* server =
       
  1679             reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1680                 static_cast<long>(aHandle));
       
  1681 
       
  1682         RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1683 
       
  1684         srvRec = srvHandle->getServiceRecordHandle();
       
  1685     }
       
  1686 
       
  1687     err = srvRec->attributeListStart(aAttrID, aAttrType);
       
  1688 
       
  1689     if (KErrNone != err)
       
  1690     {
       
  1691         ELOG1(EJavaBluetooth,
       
  1692               "- JNI::sdpAttributeListStart Error while attributeListStart: %d",
       
  1693               err);
       
  1694         java::util::JniUtils::throwNewException(aJni,
       
  1695                                                 "javax/bluetooth/ServiceRegistrationException",
       
  1696                                                 "Updating service record failed. Symbian OS error: "
       
  1697                                                 + JavaCommonUtils::intToString(err));
       
  1698     }
       
  1699 }
       
  1700 
       
  1701 /*
       
  1702  * Class:     com_nokia_mj_impl_bluetooth_BluetoothStackS60
       
  1703  * Method:    sdpAttributeListEnd
       
  1704  * Signature: (Ljava/lang/String;JI)V
       
  1705  */
       
  1706 JNIEXPORT void JNICALL
       
  1707 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1sdpAttributeListEnd
       
  1708 (JNIEnv *aJni, jobject /*peer*/, jstring aProtocol, jlong aHandle,
       
  1709  jint aAttrID)
       
  1710 {
       
  1711     JELOG2(EJavaBluetooth);
       
  1712     ServiceRecord* srvRec;
       
  1713     int err = 0;
       
  1714     std::wstring protocol;
       
  1715 
       
  1716     try
       
  1717     {
       
  1718         protocol = java::util::JniUtils::jstringToWstring(aJni, aProtocol);
       
  1719     }
       
  1720     catch (ExceptionBase ex)
       
  1721     {
       
  1722         ELOG1(EJavaBluetooth,
       
  1723               "- JNI::sdpAttributeListEnd Exception:%S", ex.what());
       
  1724         java::util::JniUtils::throwNewException(aJni,
       
  1725                                                 "java/lang/RunTimeException", "Updating service record failed");
       
  1726         return;
       
  1727     }
       
  1728     if (0 == protocol.compare(L"btl2cap"))
       
  1729     {
       
  1730         L2CapPushServerConnection* server =
       
  1731             reinterpret_cast<L2CapPushServerConnection *>(
       
  1732                 static_cast<long>(aHandle));
       
  1733 
       
  1734         L2CAPServerConnection *srvHandle = server->getServerObject();
       
  1735 
       
  1736         srvRec = srvHandle->getServiceRecordHandle();
       
  1737     }
       
  1738     else // if protocol is "btspp"
       
  1739     {
       
  1740         RFCOMMPushServerConnection* server =
       
  1741             reinterpret_cast<RFCOMMPushServerConnection *>(
       
  1742                 static_cast<long>(aHandle));
       
  1743 
       
  1744         RFCOMMServerConnection *srvHandle = server->getServerObject();
       
  1745 
       
  1746         srvRec = srvHandle->getServiceRecordHandle();
       
  1747     }
       
  1748 
       
  1749     err = srvRec->attributeListEnd(aAttrID);
       
  1750 
       
  1751     if (KErrNone != err)
       
  1752     {
       
  1753         ELOG1(EJavaBluetooth,
       
  1754               "- JNI::sdpAttributeListEnd Error while attributeListEnd: %d",
       
  1755               err);
       
  1756 
       
  1757         java::util::JniUtils::throwNewException(aJni,
       
  1758                                                 "javax/bluetooth/ServiceRegistrationException",
       
  1759                                                 "Updating service record failed. Symbian OS error: "
       
  1760                                                 + JavaCommonUtils::intToString(err));
       
  1761     }
       
  1762 }
       
  1763 
       
  1764 //
       
  1765 // JNI functions specific to Remote Device operations
       
  1766 //
       
  1767 
       
  1768 
       
  1769 JNIEXPORT jboolean JNICALL
       
  1770 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1isAuthenticated
       
  1771 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aRemoteAddress)
       
  1772 {
       
  1773     JELOG2(EJavaBluetooth);
       
  1774 
       
  1775     jboolean ret = java::bluetooth::BluetoothRemoteDevice::getSecurityProperty(
       
  1776                        REMOTE_AUTHENTICATED, aRemoteAddress);
       
  1777 
       
  1778     return ret;
       
  1779 }
       
  1780 
       
  1781 JNIEXPORT jboolean JNICALL
       
  1782 Java_com_nokia_mj_impl_bluetooth_BluetoothStackS60__1isTrusted
       
  1783 (JNIEnv* /*aJni*/, jobject /*aPeer*/, jlong aRemoteAddress)
       
  1784 {
       
  1785     JELOG2(EJavaBluetooth);
       
  1786 
       
  1787     jboolean ret = java::bluetooth::BluetoothRemoteDevice::getSecurityProperty(
       
  1788                        REMOTE_TRUSTED, aRemoteAddress);
       
  1789 
       
  1790     return ret;
       
  1791 }