31
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 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 |
*/
|
47
|
17 |
#include "btdelegatedevsecurity.h"
|
|
18 |
#include "btqtconstants.h"
|
31
|
19 |
#include <QModelIndex>
|
|
20 |
#include <btsettingmodel.h>
|
|
21 |
#include <btdevicemodel.h>
|
42
|
22 |
#include <btdelegatefactory.h>
|
31
|
23 |
#include <hbnotificationdialog.h>
|
57
|
24 |
#include "btqtconstants.h"
|
|
25 |
#include "btuiutil.h"
|
|
26 |
#include <bluetoothuitrace.h>
|
|
27 |
#include <btengconnman.h>
|
31
|
28 |
|
|
29 |
BtDelegateDevSecurity::BtDelegateDevSecurity(
|
|
30 |
BtSettingModel* settingModel,
|
|
31 |
BtDeviceModel* deviceModel,
|
|
32 |
QObject *parent) :
|
57
|
33 |
BtAbstractDelegate(settingModel, deviceModel, parent), mBtEngDevMan(0),
|
|
34 |
mDisconnectDelegate(0), mBtEngAddr(0), mRegDevArray(0), mOperation(0),
|
|
35 |
mDevice(0),mNewDev(0), mActiveHandling(false),mAddingBlockedDev(false)
|
31
|
36 |
{
|
57
|
37 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
|
38 |
mRegDevArray = new CBTDeviceArray(1);
|
|
39 |
BTUI_ASSERT_X( mRegDevArray, "BtDelegateDevSecurity::BtDelegateDevSecurity()",
|
|
40 |
"can't allocate regdevarray");
|
|
41 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
31
|
42 |
}
|
|
43 |
|
|
44 |
BtDelegateDevSecurity::~BtDelegateDevSecurity()
|
|
45 |
{
|
57
|
46 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
31
|
47 |
delete mBtEngDevMan;
|
42
|
48 |
delete mDisconnectDelegate;
|
57
|
49 |
delete mRegDevArray;
|
|
50 |
delete mNewDev;
|
|
51 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
31
|
52 |
}
|
|
53 |
|
|
54 |
|
57
|
55 |
/*!
|
|
56 |
Returns the supported editor types.
|
|
57 |
\return the sum of supported editor types
|
|
58 |
*/
|
|
59 |
int BtDelegateDevSecurity::supportedEditorTypes() const
|
|
60 |
{
|
|
61 |
return BtDelegate::UnpairDevice
|
|
62 |
| BtDelegate::BlockDevice
|
|
63 |
| BtDelegate::UnblockDevice
|
|
64 |
| BtDelegate::TrustDevice
|
|
65 |
| BtDelegate::UntrustDevice;
|
|
66 |
}
|
|
67 |
|
|
68 |
/*!
|
|
69 |
* performs operations on remote device: unpair, authorize/unauthorize, block/unblock
|
|
70 |
* params of type QList<QVariant>:
|
|
71 |
* 1) remote device address (QString)
|
|
72 |
* 2) DeviceSecurityService operation
|
|
73 |
*/
|
31
|
74 |
void BtDelegateDevSecurity::exec( const QVariant ¶ms )
|
|
75 |
{
|
57
|
76 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
|
77 |
// check if in use already
|
|
78 |
if ( mActiveHandling ) {
|
|
79 |
emit delegateCompleted(KErrInUse, this);
|
|
80 |
BOstraceFunctionExitExt( DUMMY_DEVLIST, this, KErrInUse );
|
|
81 |
return;
|
|
82 |
}
|
|
83 |
mActiveHandling = true;
|
31
|
84 |
|
57
|
85 |
// check parameters
|
|
86 |
QList<QVariant> paramList = params.value< QList<QVariant> >();
|
|
87 |
if (paramList.count() != 2) {
|
|
88 |
// wrong parameters
|
|
89 |
emitCommandComplete( KErrArgument );
|
|
90 |
BOstraceFunctionExitExt( DUMMY_DEVLIST, this, KErrArgument );
|
|
91 |
return;
|
|
92 |
}
|
42
|
93 |
|
57
|
94 |
int error = 0;
|
|
95 |
TRAP( error, {
|
|
96 |
if( !mBtEngDevMan) {
|
|
97 |
mBtEngDevMan = CBTEngDevMan::NewL( this );
|
|
98 |
}
|
|
99 |
});
|
|
100 |
if (error) {
|
|
101 |
emitCommandComplete( KErrNoMemory );
|
|
102 |
BOstraceFunctionExitExt( DUMMY_DEVLIST, this, KErrNoMemory );
|
|
103 |
return;
|
42
|
104 |
}
|
57
|
105 |
|
|
106 |
mStrBtAddr = paramList.at(0).value<QString>(); // device to operate on
|
|
107 |
mOperation = paramList.at(1).toInt(); // operation
|
|
108 |
|
|
109 |
addrReadbleStringToSymbian( mStrBtAddr, mBtEngAddr );
|
|
110 |
BtTraceBtAddr1( TRACE_DEBUG, DUMMY_DEVLIST, "device addr=", mBtEngAddr );
|
|
111 |
|
|
112 |
|
|
113 |
// get device from registry since it is needed for all operations
|
|
114 |
mSearchPattern.FindAddress( mBtEngAddr );
|
|
115 |
mRegDevArray->ResetAndDestroy();
|
|
116 |
mBtEngDevMan->GetDevices(mSearchPattern, mRegDevArray); // callback is HandleGetDevicesComplete()
|
|
117 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
|
118 |
}
|
|
119 |
|
|
120 |
|
|
121 |
void BtDelegateDevSecurity::unpair()
|
|
122 |
{
|
|
123 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
|
124 |
int error = KErrNone;
|
|
125 |
|
|
126 |
// unpair first since malicious device might try to connect/pair again
|
|
127 |
// immediately after disconnecting
|
|
128 |
mDevice->SetPaired(EFalse);
|
|
129 |
mDevice->DeleteLinkKey();
|
|
130 |
|
|
131 |
// untrust the device also
|
|
132 |
TBTDeviceSecurity security = mDevice->GlobalSecurity();
|
|
133 |
security.SetNoAuthorise( EFalse );
|
|
134 |
mDevice->SetGlobalSecurity( security );
|
|
135 |
|
|
136 |
error = mBtEngDevMan->ModifyDevice( *mDevice ); // see callback for possible disconnect
|
|
137 |
|
|
138 |
if ( error ) {
|
|
139 |
emitCommandComplete(error);
|
|
140 |
}
|
|
141 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
|
142 |
}
|
|
143 |
|
|
144 |
void BtDelegateDevSecurity::authorizeOrBlock()
|
|
145 |
{
|
|
146 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
|
147 |
BOstraceExt1( TRACE_NORMAL, DUMMY_DEVLIST, "operation (Unpair|Block|Unblock|Authorize|Unauthorize)=%d",
|
|
148 |
mOperation);
|
|
149 |
int error = KErrNone;
|
|
150 |
|
|
151 |
TBTDeviceSecurity security = mDevice->GlobalSecurity();
|
|
152 |
switch ( mOperation ) {
|
|
153 |
case BtAuthorize:
|
|
154 |
security.SetNoAuthorise( ETrue ); // set trust status to true
|
|
155 |
security.SetBanned( EFalse );
|
|
156 |
break;
|
|
157 |
case BtUnauthorize:
|
|
158 |
security.SetNoAuthorise( EFalse );
|
|
159 |
break;
|
|
160 |
case BtUnblock:
|
|
161 |
security.SetBanned( EFalse );
|
|
162 |
break;
|
|
163 |
case BtBlock:
|
|
164 |
security.SetBanned( ETrue );
|
|
165 |
security.SetNoAuthorise( EFalse ); // set trust status to false
|
|
166 |
break;
|
|
167 |
}
|
|
168 |
|
|
169 |
mDevice->SetGlobalSecurity( security );
|
|
170 |
if ( (mOperation == BtBlock) || (mOperation == BtUnblock) ) {
|
|
171 |
// deleting link key for executing unblock is safe as no
|
|
172 |
// link key shall exist if the device has been blocked.
|
|
173 |
mDevice->DeleteLinkKey();
|
|
174 |
if ( mOperation == BtBlock ) {
|
|
175 |
mDevice->SetPaired(EFalse);
|
42
|
176 |
}
|
|
177 |
}
|
57
|
178 |
error = mBtEngDevMan->ModifyDevice( *mDevice );
|
31
|
179 |
|
|
180 |
if ( error ) {
|
|
181 |
emitCommandComplete(error);
|
|
182 |
}
|
57
|
183 |
BOstraceFunctionExitExt( DUMMY_DEVLIST, this, error );
|
31
|
184 |
}
|
|
185 |
|
|
186 |
void BtDelegateDevSecurity::cancel()
|
|
187 |
{
|
57
|
188 |
BOstraceFunctionEntry1( DUMMY_DEVLIST, this );
|
|
189 |
if ( mActiveHandling ) {
|
|
190 |
mAddingBlockedDev = false;
|
|
191 |
emitCommandComplete(KErrNone);
|
|
192 |
}
|
|
193 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
31
|
194 |
}
|
|
195 |
|
42
|
196 |
void BtDelegateDevSecurity::disconnectDelegateCompleted( int err )
|
|
197 |
{
|
57
|
198 |
BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, err );
|
|
199 |
if (mDisconnectDelegate) {
|
|
200 |
delete mDisconnectDelegate;
|
|
201 |
mDisconnectDelegate = 0;
|
|
202 |
}
|
|
203 |
emitCommandComplete(err);
|
|
204 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
|
205 |
}
|
|
206 |
|
|
207 |
void BtDelegateDevSecurity::HandleDevManComplete( TInt err )
|
|
208 |
{
|
|
209 |
BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, err );
|
|
210 |
|
|
211 |
if ( !mActiveHandling ) {
|
|
212 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
|
213 |
return;
|
|
214 |
}
|
|
215 |
if ( !err ) {
|
|
216 |
if ( mAddingBlockedDev ) {
|
|
217 |
// blocked a device which was not in the registry originally
|
|
218 |
mAddingBlockedDev = false;
|
|
219 |
delete mNewDev;
|
|
220 |
mNewDev = 0;
|
|
221 |
}
|
|
222 |
else if ( mOperation == BtBlock || mOperation == BtUnpair) {
|
|
223 |
// disconnect after blocking/unpairing if device is connected;
|
|
224 |
// disconnect done after block/unpair, instead of before, to prevent a malicious device
|
|
225 |
// from reconnecting/"re-pairing"
|
|
226 |
CBTEngConnMan *connMan(0);
|
|
227 |
TRAP( err, connMan = CBTEngConnMan::NewL() );
|
|
228 |
TBTEngConnectionStatus connstatus(EBTEngNotConnected);
|
|
229 |
if (!err) {
|
|
230 |
err = connMan->IsConnected(mBtEngAddr, connstatus );
|
|
231 |
delete connMan;
|
|
232 |
}
|
|
233 |
if ( !err && connstatus == EBTEngConnected ) {
|
|
234 |
if (! mDisconnectDelegate){
|
|
235 |
mDisconnectDelegate = BtDelegateFactory::newDelegate(
|
|
236 |
BtDelegate::DisconnectService, settingModel(), deviceModel());
|
|
237 |
connect( mDisconnectDelegate, SIGNAL(delegateCompleted(int,BtAbstractDelegate*)), this,
|
|
238 |
SLOT(disconnectDelegateCompleted(int)) );
|
|
239 |
}
|
|
240 |
QList<QVariant>list;
|
|
241 |
list.append(QVariant(ServiceLevel));
|
|
242 |
list.append(QVariant(mStrBtAddr));
|
|
243 |
mDisconnectDelegate->exec(QVariant(list)); // see callback for continuation
|
|
244 |
return;
|
|
245 |
}
|
|
246 |
}
|
|
247 |
}
|
|
248 |
emitCommandComplete(err);
|
|
249 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
42
|
250 |
}
|
|
251 |
|
57
|
252 |
void BtDelegateDevSecurity::HandleGetDevicesComplete( TInt err, CBTDeviceArray* aDeviceArray )
|
31
|
253 |
{
|
57
|
254 |
BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, err );
|
|
255 |
|
|
256 |
if ( mActiveHandling ) {
|
|
257 |
if ( !err && aDeviceArray->Count() ) {
|
|
258 |
mDevice = aDeviceArray->At( 0 );
|
|
259 |
switch ( mOperation ) {
|
|
260 |
case BtUnpair:
|
|
261 |
unpair();
|
|
262 |
break;
|
|
263 |
case BtAuthorize:
|
|
264 |
case BtUnauthorize:
|
|
265 |
case BtUnblock:
|
|
266 |
case BtBlock:
|
|
267 |
authorizeOrBlock();
|
|
268 |
break;
|
|
269 |
default:
|
|
270 |
// wrong parameter
|
|
271 |
emitCommandComplete( KErrArgument );
|
|
272 |
}
|
|
273 |
}
|
|
274 |
else if ( err == KErrNotFound && mOperation == BtBlock) { // device not in registry, need to add it
|
|
275 |
mAddingBlockedDev = true;
|
|
276 |
TRAP( err, {
|
|
277 |
mNewDev = CBTDevice::NewL( mBtEngAddr );
|
|
278 |
});
|
|
279 |
if ( !err ) {
|
|
280 |
// get needed info about device from model, e.g. name, cod
|
|
281 |
QString btStringAddr;
|
|
282 |
addrSymbianToReadbleString(btStringAddr, mBtEngAddr);
|
|
283 |
QModelIndex start = deviceModel()->index(0,0);
|
|
284 |
QModelIndexList indexList = deviceModel()->match(start,
|
|
285 |
BtDeviceModel::ReadableBdaddrRole, btStringAddr);
|
|
286 |
// ToDo: can we be sure that device will always be found in the model?
|
|
287 |
QModelIndex index = indexList.at(0);
|
|
288 |
QString devName = deviceModel()->data(index,BtDeviceModel::NameAliasRole).toString();
|
|
289 |
BtTraceQString1( TRACE_DEBUG, DUMMY_DEVLIST, "device name=", devName);
|
|
290 |
TBuf<KMaxBCBluetoothNameLen> buf( devName.utf16() );
|
|
291 |
TRAP( err, mNewDev->SetDeviceNameL( BTDeviceNameConverter::ToUTF8L( buf ) ));
|
|
292 |
if( !err ) {
|
|
293 |
int cod = (index.data(BtDeviceModel::CoDRole)).toInt();
|
|
294 |
mNewDev->SetDeviceClass(cod);
|
|
295 |
TBTDeviceSecurity security = mNewDev->GlobalSecurity();
|
|
296 |
security.SetBanned( ETrue );
|
|
297 |
security.SetNoAuthorise( EFalse ); // set trust status to false
|
|
298 |
mNewDev->SetGlobalSecurity( security );
|
|
299 |
mNewDev->DeleteLinkKey();
|
|
300 |
mNewDev->SetPaired(EFalse);
|
|
301 |
err = mBtEngDevMan->AddDevice( *mNewDev ); // see callback HandleDevManComplete()
|
|
302 |
}
|
|
303 |
}
|
|
304 |
}
|
|
305 |
if (err) {
|
|
306 |
mAddingBlockedDev = false;
|
|
307 |
emitCommandComplete( err );
|
|
308 |
}
|
|
309 |
}
|
|
310 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
31
|
311 |
}
|
|
312 |
|
|
313 |
void BtDelegateDevSecurity::emitCommandComplete(int error)
|
|
314 |
{
|
57
|
315 |
BOstraceFunctionEntryExt( DUMMY_DEVLIST, this, error );
|
42
|
316 |
// no dialogs here since stack provides "unpaired to %1" dialog
|
|
317 |
// and failures are not reported
|
57
|
318 |
mActiveHandling = false;
|
|
319 |
mAddingBlockedDev = false;
|
|
320 |
if ( mNewDev ) {
|
|
321 |
delete mNewDev;
|
|
322 |
mNewDev = 0;
|
|
323 |
}
|
|
324 |
emit delegateCompleted(error, this);
|
|
325 |
BOstraceFunctionExit1( DUMMY_DEVLIST, this );
|
31
|
326 |
}
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|