javaextensions/bluetooth/bluetoothplugins/btjavacaptainplugin/src.s60/btdeviceclassmanager.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bt_subscribe.h>
       
    20 #include <btmanclient.h>
       
    21 #include <e32property.h>
       
    22 #include "comms.h"
       
    23 #include "commsendpoint.h"
       
    24 #include "commsmessage.h"
       
    25 #include "logger.h"
       
    26 
       
    27 #include "coreinterface.h"
       
    28 #include "rtcinterface.h"
       
    29 
       
    30 #include "btdeviceclassmanager.h"
       
    31 
       
    32 #ifdef __SYMBIAN32__
       
    33 java::captain::ExtensionPluginInterface* getExtensionPlugin()
       
    34 {
       
    35 #else
       
    36 extern "C" java::captain::ExtensionPluginInterface* getExtensionPlugin()
       
    37 {
       
    38 #endif
       
    39     return new java::bluetooth::BtDeviceClassManager();
       
    40 }
       
    41 
       
    42 using namespace java::comms;
       
    43 using namespace java::captain;
       
    44 
       
    45 namespace java
       
    46 {
       
    47 namespace bluetooth
       
    48 {
       
    49 
       
    50 BtDeviceClassManager:: BtDeviceClassManager() : mCore(0)
       
    51 {
       
    52     JELOG2(EJavaBluetooth);
       
    53     mInitialServiceClassBits = 0;
       
    54     LOG(EJavaBluetooth, EInfo, "Loaded BT service plugin");
       
    55 }
       
    56 
       
    57 BtDeviceClassManager::~BtDeviceClassManager()
       
    58 {
       
    59     JELOG2(EJavaBluetooth);
       
    60 }
       
    61 
       
    62 void BtDeviceClassManager::startPlugin(CoreInterface* aCore)
       
    63 {
       
    64     JELOG2(EJavaBluetooth);
       
    65     mCore = aCore;
       
    66     mCore->getComms()->registerListener(PLUGIN_ID_BT_DEVICE_CLASS_MANAGER_C,
       
    67                                         this);
       
    68 }
       
    69 
       
    70 void BtDeviceClassManager::stopPlugin()
       
    71 {
       
    72     JELOG2(EJavaBluetooth);
       
    73     mCore->getComms()->unregisterListener(PLUGIN_ID_BT_DEVICE_CLASS_MANAGER_C,
       
    74                                           this);
       
    75     mCore = 0;
       
    76 }
       
    77 
       
    78 java::comms::CommsListener* BtDeviceClassManager::getCommsListener()
       
    79 {
       
    80     JELOG2(EJavaBluetooth);
       
    81     return this;
       
    82 }
       
    83 
       
    84 // CommsListener methods
       
    85 void BtDeviceClassManager::processMessage(CommsMessage& aMessage)
       
    86 {
       
    87     JELOG2(EJavaBluetooth);
       
    88     switch (aMessage.getMessageId())
       
    89     {
       
    90     case JSR_82_MESSAGE_ID_RANGE_START_C:
       
    91         int isServiceBitsPersistent;
       
    92         std::wstring midletId;
       
    93         int srvClassVal;
       
    94 
       
    95         aMessage >> isServiceBitsPersistent;
       
    96         aMessage >> midletId;
       
    97         aMessage >> srvClassVal;
       
    98 
       
    99         LOG1(EJavaBluetooth, EInfo,
       
   100              "  BtDeviceClassManager::processMessage Persistent:%d",
       
   101              isServiceBitsPersistent);
       
   102         LOG1(EJavaBluetooth, EInfo,
       
   103              "  BtDeviceClassManager::processMessage midletId:%S",
       
   104              midletId.c_str());
       
   105         LOG1(EJavaBluetooth, EInfo,
       
   106              "  BtDeviceClassManager::processMessage srvClassVal:0x%X",
       
   107              srvClassVal);
       
   108 
       
   109         if (!srvClassVal)
       
   110         {
       
   111             LOG(EJavaBluetooth, EInfo,
       
   112                 "   BtDeviceClassManager::processMessage Removing");
       
   113             mServiceListHandler.remove(midletId);
       
   114         }
       
   115         else
       
   116         {
       
   117             if (true == isServiceBitsPersistent)
       
   118             {
       
   119                 srvClassVal |= mServiceListHandler.get(midletId);
       
   120             }
       
   121 
       
   122             LOG(EJavaBluetooth, EInfo,
       
   123                 "   BtDeviceClassManager::processMessage Adding");
       
   124             mServiceListHandler.add(midletId, srvClassVal);
       
   125         }
       
   126 
       
   127         int retVal = setServiceClassBits(mServiceListHandler.getCollectiveData());
       
   128 
       
   129         CommsMessage replyMsg;
       
   130         replyMsg.replyTo(aMessage);
       
   131         replyMsg << retVal;
       
   132         mCore->getComms()->send(replyMsg);
       
   133         break;
       
   134     }
       
   135 }
       
   136 
       
   137 unsigned int BtDeviceClassManager::getServiceClassBits()
       
   138 {
       
   139     JELOG2(EJavaBluetooth);
       
   140     TInt currentCODVal = 0;
       
   141 
       
   142     TInt error = RProperty::Get(KPropertyUidBluetoothCategory,
       
   143                                 KPropertyKeyBluetoothGetDeviceClass, currentCODVal);
       
   144 
       
   145     if (KErrNone != error)
       
   146     {
       
   147         ELOG1(EJavaBluetooth, "- BtDeviceClassManager::getServiceClass Err:%d",
       
   148               error);
       
   149         return 0;
       
   150     }
       
   151 
       
   152     LOG1(EJavaBluetooth, EInfo,
       
   153          "  BtDeviceClassManager::getServiceClass COD:0x%x", currentCODVal);
       
   154 
       
   155     TBTDeviceClass currentCOD(currentCODVal);
       
   156     return currentCOD.MajorServiceClass();
       
   157 }
       
   158 
       
   159 int BtDeviceClassManager::setServiceClassBits(unsigned int aDeviceServiceClass)
       
   160 {
       
   161     JELOG2(EJavaBluetooth);
       
   162 
       
   163     if (0 == mInitialServiceClassBits)
       
   164     {
       
   165         mInitialServiceClassBits = getServiceClassBits();
       
   166         LOG1(EJavaBluetooth, EInfo, "mInitialServiceClassBits : %d",
       
   167              mInitialServiceClassBits);
       
   168     }
       
   169 
       
   170     TInt currentCODVal = 0;
       
   171 
       
   172     TInt error = RProperty::Get(KPropertyUidBluetoothCategory,
       
   173                                 KPropertyKeyBluetoothGetDeviceClass, currentCODVal);
       
   174 
       
   175     if (KErrNone != error)
       
   176     {
       
   177         ELOG1(EJavaBluetooth,
       
   178               "   BtDeviceClassManager::setServiceClass Err:%d", error);
       
   179         return error;
       
   180     }
       
   181 
       
   182     TBTDeviceClass currentCOD(currentCODVal);
       
   183 
       
   184     // Extracting the service class bits only.
       
   185     TBTDeviceClass requestedCOD(aDeviceServiceClass);
       
   186 
       
   187     TBTDeviceClass newCOD(mInitialServiceClassBits
       
   188                           | requestedCOD.MajorServiceClass(), currentCOD.MajorDeviceClass(),
       
   189                           currentCOD.MinorDeviceClass());
       
   190 
       
   191     LOG1(EJavaBluetooth, EInfo,
       
   192          "   BtDeviceClassManager::setServiceClass new service bits:0x%x",
       
   193          ((mInitialServiceClassBits | aDeviceServiceClass) << 13));
       
   194 
       
   195     error = RProperty::Set(KPropertyUidBluetoothCategory,
       
   196                            KPropertyKeyBluetoothSetDeviceClass, newCOD.DeviceClass());
       
   197 
       
   198     LOG3(
       
   199         EJavaBluetooth,
       
   200         EInfo,
       
   201         "   BtDeviceClassManager::setServiceClass 0x%X  Err:%d NewServCOD:0x%x",
       
   202         aDeviceServiceClass, error, getServiceClassBits());
       
   203 
       
   204     return error;
       
   205 }
       
   206 
       
   207 } // namespace bluetooth
       
   208 } // namespace java
       
   209