bluetoothengine/btnotif/btnotifsrv/src/btnotifdeviceselector.cpp
changeset 63 bcf742120177
parent 52 4545c04e61e1
equal deleted inserted replaced
52:4545c04e61e1 63:bcf742120177
    23 #include "btnotifdeviceselector.h"
    23 #include "btnotifdeviceselector.h"
    24 
    24 
    25 #include "btnotifserver.h"
    25 #include "btnotifserver.h"
    26 #include "btnotificationmanager.h"
    26 #include "btnotificationmanager.h"
    27 #include "btnotifclientserver.h"
    27 #include "btnotifclientserver.h"
       
    28 
    28 // Key description length
    29 // Key description length
    29 const TInt KMaxKeyDesCLength  = 20;
    30 const TInt KMaxKeyDesCLength  = 20;
       
    31 const TInt KLastUsedDevices = 5;
    30 
    32 
    31 // ======== MEMBER FUNCTIONS ========
    33 // ======== MEMBER FUNCTIONS ========
    32 
    34 
    33 // ---------------------------------------------------------------------------
    35 // ---------------------------------------------------------------------------
    34 // C++ default constructor
    36 // C++ default constructor
    78         iNotification = NULL;
    80         iNotification = NULL;
    79         }
    81         }
    80     iDevices.ResetAndDestroy();
    82     iDevices.ResetAndDestroy();
    81     iDevices.Close();
    83     iDevices.Close();
    82     delete iDiscoverer;
    84     delete iDiscoverer;
    83     
       
    84     }
    85     }
    85 
    86 
    86 // ---------------------------------------------------------------------------
    87 // ---------------------------------------------------------------------------
    87 // Process a client message related to notifiers.
    88 // Process a client message related to notifiers.
    88 // ---------------------------------------------------------------------------
    89 // ---------------------------------------------------------------------------
   129             
   130             
   130             iLoadDevices = EFalse;
   131             iLoadDevices = EFalse;
   131             if(iServer.DevRepository().IsInitialized())
   132             if(iServer.DevRepository().IsInitialized())
   132                 {
   133                 {
   133                 iLoadDevices = ETrue;
   134                 iLoadDevices = ETrue;
       
   135                 iDevices.ResetAndDestroy();
   134                 if(iServer.DevRepository().AllDevices().Count()==0)
   136                 if(iServer.DevRepository().AllDevices().Count()==0)
   135                      {
   137                      {
   136                      PrepareNotificationL(TBluetoothDialogParams::EDeviceSearch, ENoResource);
   138                      PrepareNotificationL(TBluetoothDialogParams::EDeviceSearch, ENoResource);
   137                      iDevices.ResetAndDestroy();
       
   138                      iDiscoverer->DiscoverDeviceL();
   139                      iDiscoverer->DiscoverDeviceL();
   139                      }
   140                      }
   140                 else
   141                 else
   141                      {
   142                      {
   142                      iDevices.ResetAndDestroy();
       
   143                      PrepareNotificationL(TBluetoothDialogParams::EMoreDevice, ENoResource);
   143                      PrepareNotificationL(TBluetoothDialogParams::EMoreDevice, ENoResource);
   144                      LoadUsedDevicesL();
   144                      LoadUsedDevicesL();
   145                      }
   145                      }
   146                 }
   146                 }
   147             iMessage = aMessage;
   147             iMessage = aMessage;
   178         TInt val = *(static_cast<TInt*>(aData.Get(_L("selectedindex"))->Data()));
   178         TInt val = *(static_cast<TInt*>(aData.Get(_L("selectedindex"))->Data()));
   179         BOstrace1( TRACE_DEBUG, TNAME_DEVLIST_2, "MBRDataReceived, val %d", val );
   179         BOstrace1( TRACE_DEBUG, TNAME_DEVLIST_2, "MBRDataReceived, val %d", val );
   180 
   180 
   181         if ( !iMessage.IsNull() )
   181         if ( !iMessage.IsNull() )
   182             {
   182             {
   183            // TInt sel = val;// - TBluetoothDialogParams::EDialogExt;
       
   184             TBTDeviceResponseParamsPckg devParams;    
   183             TBTDeviceResponseParamsPckg devParams;    
   185             if (  val > -1 && val < iDevices.Count() )
   184             if (  val > -1 && val < iDevices.Count() )
   186                 {
   185                 {
   187                 TRAP(err,SendSelectedDeviceL(aData));
   186                 TRAP(err,SendSelectedDeviceL(aData));
   188              /*   devParams().SetDeviceAddress( iDevices[val]->Addr() );
       
   189                 devParams().SetDeviceClass(iDevices[val]->Device().DeviceClass());
       
   190                 devParams().SetDeviceName(iDevices[val]->Alias());
       
   191                 err = iMessage.Write( EBTNotifSrvReplySlot, devParams );*/
       
   192                 iNotification->RemoveObserver();
   187                 iNotification->RemoveObserver();
   193                 iNotification->Close(); // Also dequeues the notification from the queue.
   188                 iNotification->Close(); // Also dequeues the notification from the queue.
   194                 iNotification = NULL;                
   189                 iNotification = NULL;                
   195                 }
   190                 }
   196             iMessage.Complete( err );
   191             iMessage.Complete( err );
   239 // ---------------------------------------------------------------------------
   234 // ---------------------------------------------------------------------------
   240 //
   235 //
   241 void CBTNotifDeviceSelector::HandleNextDiscoveryResultL( 
   236 void CBTNotifDeviceSelector::HandleNextDiscoveryResultL( 
   242         const TInquirySockAddr& aAddr, const TDesC& aName )
   237         const TInquirySockAddr& aAddr, const TDesC& aName )
   243     {
   238     {
   244     // Todo: look for this device in repository before creating it.
       
   245     TBuf<KBTDevAddrSize * 2> addr; 
       
   246     
       
   247     
       
   248     
       
   249     CBtDevExtension* devext = GetDeviceFromRepositoryL(aAddr.BTAddr());
   239     CBtDevExtension* devext = GetDeviceFromRepositoryL(aAddr.BTAddr());
   250     
   240     
   251     if(!devext)
   241     if(!devext)
   252         {
   242         {
   253         devext = CBtDevExtension::NewLC( aAddr, aName );
   243         devext = CBtDevExtension::NewLC( aAddr, aName );
   261     
   251     
   262     if(iNotification)
   252     if(iNotification)
   263         {// conditional check required as CAdvanceDevDiscoverer sends discovered devices at times
   253         {// conditional check required as CAdvanceDevDiscoverer sends discovered devices at times
   264          // even after canceldiscovery is issued and notification is set to NULL
   254          // even after canceldiscovery is issued and notification is set to NULL
   265          // this causes EExcDataAbort
   255          // this causes EExcDataAbort
   266     CHbSymbianVariantMap* map = iNotification->Data();
   256         LoadDeviceDetailsL(*devext);
   267 /*    TBuf<KMaxKeyDesCLength> keyStr;
       
   268     CHbSymbianVariant* devEntry;
       
   269 
       
   270     keyStr.Num( TBluetoothDialogParams::EDialogExt + iDevices.Count() - 1 );
       
   271     devEntry = CHbSymbianVariant::NewL( (TAny*) &(devext->Alias()), 
       
   272             CHbSymbianVariant::EDes );
       
   273     map->Add( keyStr, devEntry );*/
       
   274     
       
   275     User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EDeviceName,
       
   276             devext->Alias()));
       
   277     
       
   278     devext->Addr().GetReadable(addr);
       
   279     User::LeaveIfError(iNotification->SetData(TBluetoothDialogParams::EAddress,addr));
       
   280     
       
   281     TInt classOfDevice;
       
   282     classOfDevice =  devext->Device().DeviceClass().DeviceClass();
       
   283     User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EDeviceClass,classOfDevice));
       
   284 
       
   285     TBool status;
       
   286     status = isBonded( devext->Device());
       
   287     
       
   288  //   setMajorProperty(majorProperty, _L("Bonded"), isBonded( devArray[i]->Device() ));
       
   289     AddDataL(map,_L("Bonded"),&status,CHbSymbianVariant::EBool);
       
   290     status = devext->Device().GlobalSecurity().Banned();
       
   291     AddDataL(map,_L("Blocked"),&status,
       
   292             CHbSymbianVariant::EBool);
       
   293     status = devext->Device().GlobalSecurity().NoAuthorise();
       
   294     AddDataL(map,_L("Trusted"),&status,
       
   295             CHbSymbianVariant::EBool);
       
   296     status = devext->ServiceConnectionStatus() == EBTEngConnected;
       
   297     AddDataL(map,_L("Connected"),&status,
       
   298             CHbSymbianVariant::EBool);
       
   299  
       
   300     iNotification->Update();
       
   301         }
   257         }
   302     }
   258     }
   303 
   259 
   304 // ---------------------------------------------------------------------------
   260 // ---------------------------------------------------------------------------
   305 // HandleDiscoveryCompleted
   261 // HandleDiscoveryCompleted
   336     iRepositoryInitialized = ETrue;
   292     iRepositoryInitialized = ETrue;
   337     TInt err(KErrNone);
   293     TInt err(KErrNone);
   338     if(!iLoadDevices)
   294     if(!iLoadDevices)
   339         {
   295         {
   340         iLoadDevices = ETrue;
   296         iLoadDevices = ETrue;
       
   297         iDevices.ResetAndDestroy();
   341         if(iServer.DevRepository().AllDevices().Count()==0)
   298         if(iServer.DevRepository().AllDevices().Count()==0)
   342              {
   299              {
   343              iDevices.ResetAndDestroy();
       
   344              TRAP(err, {
   300              TRAP(err, {
   345              PrepareNotificationL(TBluetoothDialogParams::EDeviceSearch, ENoResource);
   301              PrepareNotificationL(TBluetoothDialogParams::EDeviceSearch, ENoResource);
   346              iDiscoverer->DiscoverDeviceL(); } );
   302              iDiscoverer->DiscoverDeviceL(); } );
   347              }
   303              }
   348         else
   304         else
   349              {
   305              {
   350              iDevices.ResetAndDestroy();
       
   351              TRAP( err, 
   306              TRAP( err, 
   352                      {PrepareNotificationL(
   307                      {PrepareNotificationL(
   353                              TBluetoothDialogParams::EMoreDevice, ENoResource);
   308                              TBluetoothDialogParams::EMoreDevice, ENoResource);
   354                       LoadUsedDevicesL();
   309                       LoadUsedDevicesL();
   355                      } );
   310                      } );
   408     }
   363     }
   409 
   364 
   410 void CBTNotifDeviceSelector::LoadUsedDevicesL()
   365 void CBTNotifDeviceSelector::LoadUsedDevicesL()
   411     {
   366     {
   412     const RDevExtensionArray& devArray= iServer.DevRepository().AllDevices();
   367     const RDevExtensionArray& devArray= iServer.DevRepository().AllDevices();
   413     TBuf<KBTDevAddrSize * 2> addr; 
       
   414     for(TInt i=0; i< devArray.Count(); i++ )
   368     for(TInt i=0; i< devArray.Count(); i++ )
   415         {
   369         {
   416       const TTime& usedTime = devArray[i]->Device().Used();
   370         iDevices.AppendL( devArray[i]->CopyL() );
   417         TTime monthBack;
   371         }
   418         monthBack.HomeTime();
   372         SortUsedDevicesL();
   419         monthBack -= TTimeIntervalDays(30);
   373         RemoveUnWantedDevices();
   420         if(usedTime >= monthBack)
   374         SendLastUsedDevicesL();
   421             {
   375         SendPairedDevicesL();
   422             iDevices.AppendL( devArray[i]->CopyL() );
   376         
   423             CHbSymbianVariantMap* map = iNotification->Data();
   377     }
   424  //           TBuf<KMaxKeyDesCLength> keyStr;
   378 
   425  //           CHbSymbianVariant* devEntry;
   379 
   426 
   380 void CBTNotifDeviceSelector::SortUsedDevicesL() 
   427   //          keyStr.Num( TBluetoothDialogParams::EDialogExt + iDevices.Count() - 1 );
   381     { 
   428 //            devEntry = CHbSymbianVariant::NewL( (TAny*) &(devArray[i]->Alias()), 
   382     TInt count  = iDevices.Count();
   429   //                  CHbSymbianVariant::EDes );
   383     for(TInt i=0;i<(count-1);i++) 
   430   //          map->Add( keyStr, devEntry, );
   384         {
   431             User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EDeviceName,
   385         for(TInt j=0;j<(count-(i+1));j++)
   432                     devArray[i]->Alias()));
   386             {
   433 //            AddDataL(map,keyStr,&(devArray[i]->Alias()),CHbSymbianVariant::EDes);
   387             if(iDevices[j]->Device().Used().Int64() < iDevices[j+1]->Device().Used().Int64())
   434             devArray[i]->Addr().GetReadable(addr);
   388                 {
   435             User::LeaveIfError(iNotification->SetData(TBluetoothDialogParams::EAddress,addr));
   389                 CBtDevExtension* devextension = iDevices[j+1];
   436             addr.Zero();
   390                 iDevices.Remove(j+1);
   437             TInt classOfDevice;
   391                 iDevices.InsertL(devextension,j);
   438             classOfDevice =  devArray[i]->Device().DeviceClass().DeviceClass();
   392                 }
   439             User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EDeviceClass,classOfDevice));
   393             }
   440             
   394         }
   441             TBool status;
   395     } 
   442             status = isBonded( devArray[i]->Device());
   396 
   443             
   397 
   444          //   setMajorProperty(majorProperty, _L("Bonded"), isBonded( devArray[i]->Device() ));
   398 void CBTNotifDeviceSelector::RemoveUnWantedDevices()
   445             AddDataL(map,_L("Bonded"),&status,CHbSymbianVariant::EBool);
   399     {
   446             status = devArray[i]->Device().GlobalSecurity().Banned();
   400     for(TInt i=(iDevices.Count()-1);i>-1;i--)
   447             AddDataL(map,_L("Blocked"),&status,
   401         {
   448                     CHbSymbianVariant::EBool);
   402         TInt classOfDevice = iDevices[i]->Device().DeviceClass().DeviceClass();
   449             status = devArray[i]->Device().GlobalSecurity().NoAuthorise();
   403         TBTDeviceClass codClass = TBTDeviceClass( classOfDevice );
   450             AddDataL(map,_L("Trusted"),&status,
   404         TUint8 majorDevCls = codClass.MajorDeviceClass();
   451                     CHbSymbianVariant::EBool);
   405         if((majorDevCls ==EMajorDeviceComputer)|| (majorDevCls ==EMajorDevicePhone))
   452             status = devArray[i]->ServiceConnectionStatus() == EBTEngConnected;
   406             {
   453             AddDataL(map,_L("Connected"),&status,
   407             }
   454                     CHbSymbianVariant::EBool);
   408         else
   455              // set blocked status:
   409             {
   456 /*             setMajorProperty(majorProperty, BtuiDevProperty::Blocked, 
   410             CBtDevExtension* devextension = iDevices[i];
   457                      devArray[i]->Device().GlobalSecurity().Banned() );
   411             iDevices.Remove(i);
   458              // set trusted status:
   412             delete devextension;
   459              setMajorProperty(majorProperty, BtuiDevProperty::Trusted, 
   413             devextension = NULL;
   460                      devArray[i]->Device().GlobalSecurity().NoAuthorise() );
   414             iDevices.Compress();
   461              // set connected status:
   415             }
   462              // EBTEngConnecting is an intermediate state between connected and not-connected, 
   416         }
   463              // we do not treat it as connected:         
   417     }
   464              setMajorProperty(majorProperty, BtuiDevProperty::Connected, devArray[i]->ServiceConnectionStatus() == EBTEngConnected);
   418 
   465 
   419 void CBTNotifDeviceSelector::SendLastUsedDevicesL()
   466   */          
   420     {
   467 
   421     TInt Count = iDevices.Count();
   468  //           AddDataL(map,keyStr,&(devArray[i]->Alias()),CHbSymbianVariant::EDes);
   422     if(Count > KLastUsedDevices)
   469             iNotification->Update();
   423         Count = KLastUsedDevices;
   470             }
   424     for(TInt i=0;i < Count;i++)
   471         }
   425         {
   472     }
   426         LoadDeviceDetailsL(*(iDevices[i]));
   473 
   427         }
   474 
   428     }
       
   429 
       
   430 
       
   431 void CBTNotifDeviceSelector::SendPairedDevicesL()
       
   432     {
       
   433     TInt count = iDevices.Count();
       
   434     if(count > KLastUsedDevices)
       
   435         {
       
   436         for(TInt i = KLastUsedDevices; i< count; i++)
       
   437             {
       
   438             if(isBonded( iDevices[i]->Device()))
       
   439                 {
       
   440                 LoadDeviceDetailsL(*(iDevices[i]));
       
   441                 }
       
   442             }
       
   443         }
       
   444         
       
   445     }
       
   446 
       
   447 void CBTNotifDeviceSelector::LoadDeviceDetailsL(const CBtDevExtension& aDevice)
       
   448     {
       
   449     TBuf<KBTDevAddrSize * 2> addr;
       
   450     TUint32 classOfDevice;
       
   451     TBool status;
       
   452 
       
   453     //TODO Need to create string constants Also try Enum value for device name rather 
       
   454     //than hadrdcoded string for key. It was not working previously
       
   455     CHbSymbianVariantMap* map = iNotification->CreateNotificationDataL();
       
   456     AddDataL(map,_L("Name"),(TAny*) &(aDevice.Alias()),CHbSymbianVariant::EDes);
       
   457     
       
   458     aDevice.Addr().GetReadable(addr);
       
   459     User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EAddress,addr));
       
   460     
       
   461     classOfDevice =  aDevice.Device().DeviceClass().DeviceClass();
       
   462     User::LeaveIfError(iNotification->SetData(TBluetoothDeviceDialog::EDeviceClass,classOfDevice));
       
   463     
       
   464     status = isBonded( aDevice.Device());
       
   465     AddDataL(map,_L("Bonded"),&status,CHbSymbianVariant::EBool);
       
   466     
       
   467     status = aDevice.Device().GlobalSecurity().Banned();
       
   468     AddDataL(map,_L("Blocked"),&status,CHbSymbianVariant::EBool);
       
   469     
       
   470     status = aDevice.Device().GlobalSecurity().NoAuthorise();
       
   471     AddDataL(map,_L("Trusted"),&status,CHbSymbianVariant::EBool);
       
   472     
       
   473     status = aDevice.ServiceConnectionStatus() == EBTEngConnected;
       
   474     AddDataL(map,_L("Connected"),&status,CHbSymbianVariant::EBool);
       
   475     iNotification->Update();
       
   476     }
   475 
   477 
   476 /*!
   478 /*!
   477   Tells if the given device is bonded.
   479   Tells if the given device is bonded.
   478 */
   480 */
   479 TBool CBTNotifDeviceSelector::isBonded( const CBTDevice &dev )
   481 TBool CBTNotifDeviceSelector::isBonded( const CBTDevice &dev ) const
   480 {
   482 {
   481     // todo: this has not addresses Just Works pairing mode yet.
   483     // todo: this has not addresses Just Works pairing mode yet.
   482     return dev.IsValidPaired() && dev.IsPaired() &&
   484     return dev.IsValidPaired() && dev.IsPaired() &&
   483         dev.LinkKeyType() != ELinkKeyUnauthenticatedUpgradable;
   485         dev.LinkKeyType() != ELinkKeyUnauthenticatedUpgradable;
   484 }
   486 }
   510     if(pos > -1)
   512     if(pos > -1)
   511         {
   513         {
   512         return devArray[pos]->CopyL();
   514         return devArray[pos]->CopyL();
   513         }
   515         }
   514     return NULL;
   516     return NULL;
   515 /*    addrSymbianToReadbleString( addrStr, addr );
       
   516     for (int i = 0; i < mData.count(); ++i ) {
       
   517         if ( mData.at( i ).value( BtDeviceModel::ReadableBdaddrRole ) 
       
   518                 == addrStr ) {
       
   519             return i;
       
   520         }
       
   521     }
       
   522     return -1;*/
       
   523 }
   517 }
   524 
   518 
   525 
   519 
   526 void CBTNotifDeviceSelector::SendSelectedDeviceL( CHbSymbianVariantMap& aData )
   520 void CBTNotifDeviceSelector::SendSelectedDeviceL( CHbSymbianVariantMap& aData )
   527     {
   521     {
   528     TInt err; 
       
   529     TBTDeviceResponseParamsPckg devParams;
   522     TBTDeviceResponseParamsPckg devParams;
   530     TBTDevAddr address; 
   523     TBTDevAddr address; 
   531     User::LeaveIfError(address.SetReadable(
   524     User::LeaveIfError(address.SetReadable(
   532                                     *(static_cast<TDesC*>(aData.Get(_L("deviceaddress"))->Data()))));
   525                                     *(static_cast<TDesC*>(aData.Get(_L("deviceaddress"))->Data()))));
   533     devParams().SetDeviceAddress( address );
   526     devParams().SetDeviceAddress( address );