summaryrefslogtreecommitdiff
path: root/src/FNetBt_BluetoothOppClientImpl.cpp
blob: 7ca302985160a0ef212c4966a2fbcbb83e1fc78d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//
// Open Service Platform
// Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// @file    FNetBtBluetoothOppClientImpl.cpp
// @brief   This is the implementation file for the _BluetoothOppClientImpl class.
//

#include <FNetBtBluetoothTypes.h>
#include <FNetBtBluetoothDevice.h>
#include <FNetBtIBluetoothOppClientEventListener.h>
#include <FBaseSysLog.h>
#include <FIo_FileImpl.h>
#include <FApp_AppInfo.h>
#include "FNetBt_BluetoothOppClientImpl.h"
#include "FNetBt_BluetoothOppSystemAdapter.h"
#include "FNetBt_BluetoothGapSystemAdapter.h"
#include "FNetBt_BluetoothOppClientEvent.h"
#include "FNetBt_BluetoothOppClientEventArg.h"

using namespace std;
using namespace Tizen::App;
using namespace Tizen::Base;
using namespace Tizen::Base::Runtime;

namespace Tizen { namespace Net { namespace Bluetooth
{

_BluetoothOppClientImpl::_BluetoothOppClientImpl(void)
	: __pGapAdapter(null)
	, __pOppAdapter(null)
	, __pEvent(null)
	, __stateMutex()
	, __currentState(_BT_OPP_CLI_STATE_DISABLED)
	, __pairingAddress()
	, __pushedFilePath()
	, __minProgressInterval(__defaultProgressInterval)
	, __previousProgress(0)
{
}

_BluetoothOppClientImpl::~_BluetoothOppClientImpl(void)
{
	if (__pOppAdapter != null)
	{
		if ((__currentState == _BT_OPP_CLI_STATE_PUSH_READY)
			|| (__currentState == _BT_OPP_CLI_STATE_ON_TRANSFER))
		{
			(void) CancelPush();
		}
	}

	if (__pGapAdapter != null)
	{
		__pGapAdapter->UnregisterManagerEventListener(*this);
		__pGapAdapter->UnregisterDeviceEventListener(*this);
	}
}

result
_BluetoothOppClientImpl::Construct(IBluetoothOppClientEventListener& listener)
{
	result r = E_SUCCESS;
	std::unique_ptr<_BluetoothOppClientEvent> pEvent;

	r = __stateMutex.Create();
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create the state mutex.");

	r = __pairingAddress.Construct(_BT_ADDRESS_LENGTH);
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct the address for pairing.");

	__pGapAdapter = _BluetoothGapSystemAdapter::GetInstance();
	SysTryReturn(NID_NET_BT, __pGapAdapter != null, E_SYSTEM, E_SYSTEM,
				"[E_SYSTEM] Failed to invoke _BluetoothGapSystemAdapter::GetInstance().");

	__pOppAdapter = _BluetoothOppSystemAdapter::GetInstance();

	pEvent.reset(new (std::nothrow) _BluetoothOppClientEvent());
	SysTryReturn(NID_NET_BT, pEvent != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
	r = pEvent->Construct();
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to construct the event.");
	// add the IBluetoothOppClientEventListener instance to a new created _BluetoothOppClientEvent.
	r = pEvent->AddListener(listener, true);
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
			"[%s] Propagating. Failed to add the application listener for OPP client", GetErrorMessage(r));

	// registers this callback listener to the system adapter for activation/deactivation event
	r = __pGapAdapter->RegisterManagerEventListener(*this, true);
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
			   "[E_SYSTEM] Failed to register the callback listener to _BluetoothGapSystemAdapter.");

	// registers this callback listener to the system adapter for paired event
	r = __pGapAdapter->RegisterDeviceEventListener(*this);
	if (r != E_SUCCESS)
	{
		__pGapAdapter->UnregisterManagerEventListener(*this);
	}
	SysTryReturn(NID_NET_BT, r == E_SUCCESS, E_SYSTEM, E_SYSTEM,
			   "[E_SYSTEM] Failed to register the callback listener to _BluetoothGapSystemAdapter.");

	// checks whether the Bluetooth is available
	__stateMutex.Acquire();

	if (__pGapAdapter->IsActivated() == true)
	{
		__currentState = _BT_OPP_CLI_STATE_IDLE;
	}

	__stateMutex.Release();

	__pEvent = move(pEvent);

	return E_SUCCESS;
}


result
_BluetoothOppClientImpl::PushFile(const BluetoothDevice& remoteDevice, const Base::String& filePath)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());

	result r = E_FAILURE;
	String physicalPath;

	__stateMutex.Acquire();

	switch (__currentState)
	{
	case _BT_OPP_CLI_STATE_DISABLED:
		r = E_INVALID_STATE;
		break;

	case _BT_OPP_CLI_STATE_IDLE:
		if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
		{
			if (Tizen::Io::_FileImpl::ConvertVirtualToPhysicalPath(filePath, physicalPath) != E_SUCCESS)
			{
				r = E_INACCESSIBLE_PATH;
				break;
			}
		}
		else
		{
			physicalPath = filePath;
		}

		if (__pGapAdapter->IsPaired(*(remoteDevice.GetAddress())) == true)
		{
			r = __pOppAdapter->PushOppFile(*this, *(remoteDevice.GetAddress()), physicalPath);
			if (r == E_SUCCESS)
			{
				__currentState = _BT_OPP_CLI_STATE_PUSH_READY;
			}
		}
		else
		{
			// start the pairing process
			if (__pGapAdapter->Pair(*(remoteDevice.GetAddress())) == E_SUCCESS)
			{
				// copy the address of the pairing device to the local variable
				__pairingAddress.SetArray(remoteDevice.GetAddress()->GetPointer(), 0, _BT_ADDRESS_LENGTH);
				__pairingAddress.SetPosition(0);
				// copy the file path to the local variable
				__pushedFilePath.Clear();
				__pushedFilePath = physicalPath;

				r = E_SUCCESS;
				__currentState = _BT_OPP_CLI_STATE_BONDING;
			}
			else
			{
				r = E_FAILURE;
			}
		}
		break;

	case _BT_OPP_CLI_STATE_BONDING:
	case _BT_OPP_CLI_STATE_PUSH_READY:
	case _BT_OPP_CLI_STATE_REQ_CANCELING:   //TODO: this state should return E_INVALID_OPERATION. (Versioning)
	case _BT_OPP_CLI_STATE_ON_TRANSFER:
	case _BT_OPP_CLI_STATE_TRANS_CANCELING:   //TODO: this state should return E_INVALID_OPERATION. (Versioning)
		r = E_IN_PROGRESS;
		break;

	default:
		// ignore other cases
		break;
	}

	__stateMutex.Release();

	if (r != E_SUCCESS)
	{
		SysLogException(NID_NET_BT, r, "[%s] exception occurred on pushing a file.", GetErrorMessage(r));
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	return r;
}

result
_BluetoothOppClientImpl::CancelPush()
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s]", GetStringOfCurrentState());

	result r = E_FAILURE;

	__stateMutex.Acquire();

	switch (__currentState)
	{
	case _BT_OPP_CLI_STATE_DISABLED:
		r = E_INVALID_STATE;
		break;

	case _BT_OPP_CLI_STATE_IDLE:
		r = E_INVALID_OPERATION;
		break;

	case _BT_OPP_CLI_STATE_REQ_CANCELING:
	case _BT_OPP_CLI_STATE_TRANS_CANCELING:
		r = E_IN_PROGRESS;
		break;

	case _BT_OPP_CLI_STATE_BONDING: //TODO: Versioning check
		if (__pGapAdapter->CancelPair() == E_SUCCESS)
		{
			r = E_SUCCESS;
			__currentState = _BT_OPP_CLI_STATE_REQ_CANCELING;
		}
		// It will be changed to IDLE after receiving the OnBluetoothPaired event.
		break;

	case _BT_OPP_CLI_STATE_PUSH_READY:
		r = __pOppAdapter->CancelOppPush();

		if (r == E_SUCCESS)
		{
			__currentState = _BT_OPP_CLI_STATE_REQ_CANCELING;
		}
		// It will be changed to IDLE after receiving the OnOppTransferDone event.
		break;

	case _BT_OPP_CLI_STATE_ON_TRANSFER:
		r = __pOppAdapter->CancelOppPush();

		if (r == E_SUCCESS)
		{
			__currentState = _BT_OPP_CLI_STATE_TRANS_CANCELING;
		}
		// It will be changed to IDLE after receiving the OnOppTransferDone event.
		break;

	default:
		// ignore other cases
		break;
	}

	__stateMutex.Release();

	if (r != E_SUCCESS)
	{
		SysLogException(NID_NET_BT, r, "[%s] exception occurred on canceling the push.", GetErrorMessage(r));
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	return r;
}

result
_BluetoothOppClientImpl::SetMinProgressInterval(int percent)
{
	result r = E_SUCCESS;

	if ((percent < 1) || (percent > 100))
	{
		r = E_OUT_OF_RANGE;
		SysLogException(NID_NET_BT, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The input argument - percent - is outside the valid range.");
	}
	else
	{
		__minProgressInterval = percent;
	}

	return r;
}

void
_BluetoothOppClientImpl::OnBluetoothActivated(result r)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	__stateMutex.Acquire();

	if ((__currentState == _BT_OPP_CLI_STATE_DISABLED)
		&& (r == E_SUCCESS))
	{
		__currentState = _BT_OPP_CLI_STATE_IDLE;
	}

	__stateMutex.Release();

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s]", GetStringOfCurrentState());
}

void
_BluetoothOppClientImpl::OnBluetoothDeactivated(result r)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	__stateMutex.Acquire();

	if (r == E_SUCCESS)
	{
		__currentState = _BT_OPP_CLI_STATE_DISABLED;
	}

	__stateMutex.Release();

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s]", GetStringOfCurrentState());
}

void
_BluetoothOppClientImpl::OnOppPushResponded(result r)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	bool isFired = false;

	__stateMutex.Acquire();

	if ((__currentState == _BT_OPP_CLI_STATE_PUSH_READY) || (__currentState == _BT_OPP_CLI_STATE_REQ_CANCELING))
	{
		if (r == E_SUCCESS)
		{
			__currentState = _BT_OPP_CLI_STATE_ON_TRANSFER;
		}
		else // including both cases of being canceled by the user and rejected by the remote device
		{
			__currentState = _BT_OPP_CLI_STATE_IDLE;
			SysLogException(NID_NET_BT, r, "[%s] exception occurred on the response of the OPP push request.", GetErrorMessage(r));
		}

		isFired = true;
	}

	__stateMutex.Release();

	if (isFired)
	{
		_BluetoothOppClientEventArg* pArg = new (std::nothrow) _BluetoothOppClientEventArg(r);
		if (pArg == null)
		{
			SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
			isFired = false;
		}
		else
		{
			__pEvent->FireAsync(*pArg);
		}
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PUSH_RESPONDED_Event:%s]",
				GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
}

void
_BluetoothOppClientImpl::OnOppTransferInProgress(int percent)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [Progress:%d]", GetStringOfCurrentState(), percent);

	bool isFired = false;

	__stateMutex.Acquire();

	if ((__currentState == _BT_OPP_CLI_STATE_ON_TRANSFER) || (__currentState == _BT_OPP_CLI_STATE_TRANS_CANCELING))
	{
		if (((percent - __previousProgress) >= __minProgressInterval) || (percent >= 100))
		{
			isFired = true;
			__previousProgress = percent;
		}
	}

	__stateMutex.Release();

	if (isFired)
	{
		_BluetoothOppClientEventArg* pArg = new (std::nothrow) _BluetoothOppClientEventArg(percent);
		if (pArg == null)
		{
			SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
			isFired = false;
		}
		else
		{
			__pEvent->FireAsync(*pArg);
		}
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [TRANSFER_IN_PROGRESS_Event:%s]",
				GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
}

void
_BluetoothOppClientImpl::OnOppTransferDone(const Tizen::Base::String& filePath, int fileSize, bool isCompleted)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [filePath:%ls], [fileSize:%d], [%s]",
				GetStringOfCurrentState(), filePath.GetPointer(), fileSize, isCompleted ? "Completed" : "Incompleted");

	bool isFired = false;
	String pushedFilePath;
	result r = E_SUCCESS;

	__stateMutex.Acquire();

	if ((__currentState == _BT_OPP_CLI_STATE_ON_TRANSFER) || (__currentState == _BT_OPP_CLI_STATE_TRANS_CANCELING))
	{
		__currentState = _BT_OPP_CLI_STATE_IDLE;
		isFired = true;
		__previousProgress = 0;        // initialization for the next push transaction
	}

	__stateMutex.Release();

	if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
	{
		r = Tizen::Io::_FileImpl::ConvertPhysicalToVirtualPath(__pushedFilePath, pushedFilePath);
		if(r != E_SUCCESS)
		{
			SysLogException(NID_NET_BT, r, "[%s] exception occurred on converting the file path from physical to virtual.", GetErrorMessage(r));
			pushedFilePath = __pushedFilePath;
		}
	}
	else
	{
		pushedFilePath = __pushedFilePath;
	}

	if (isFired)
	{
		_BluetoothOppClientEventArg* pArg = new (std::nothrow) _BluetoothOppClientEventArg(pushedFilePath, fileSize, isCompleted);
		if (pArg == null)
		{
			SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
			isFired = false;
		}
		else
		{
			__pEvent->FireAsync(*pArg);
		}
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [TRANSFER_DONE_Event:%s]",
				GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
}

void
_BluetoothOppClientImpl::OnBluetoothPaired(const BluetoothDevice* pPairedDevice, result r)
{
	SysLog(NID_NET_BT, "EntryPoint, [CurrentState:%s], [ActionResult:%s]", GetStringOfCurrentState(), GetErrorMessage(r));

	bool isFired = false;
	result responseResult = E_SYSTEM;

	__stateMutex.Acquire();

	if (pPairedDevice->GetAddress()->Equals(__pairingAddress))
	{
		if (__currentState == _BT_OPP_CLI_STATE_BONDING)
		{
			if (r == E_SUCCESS)
			{
				if (__pOppAdapter->PushOppFile(*this, __pairingAddress, __pushedFilePath) == E_SUCCESS)
				{
					__currentState = _BT_OPP_CLI_STATE_PUSH_READY;
				}
				else
				{
					// callback responded with E_SYSTEM
					isFired = true;
					__currentState = _BT_OPP_CLI_STATE_IDLE;
					responseResult = E_SYSTEM;
				}
			}
			else
			{
				// callback responded with r
				isFired = true;
				__currentState = _BT_OPP_CLI_STATE_IDLE;
				responseResult = r;
			}

			__pairingAddress.Clear();
		}
		else if (__currentState == _BT_OPP_CLI_STATE_REQ_CANCELING)
		{
			// callback responded with E_SYSTEM or E_OPERATION_CANCELED
			isFired = true;
			__currentState = _BT_OPP_CLI_STATE_IDLE;
			responseResult = E_SYSTEM;
		}
	}
	// ignored otherwise

	__stateMutex.Release();

	if (isFired)
	{
		_BluetoothOppClientEventArg* pArg = new (std::nothrow) _BluetoothOppClientEventArg(responseResult);
		if (pArg == null)
		{
			SysLogException(NID_NET_BT, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
			isFired = false;
		}
		else
		{
			__pEvent->FireAsync(*pArg);
		}
	}

	SysLog(NID_NET_BT, "ExitPoint, [CurrentState:%s], [PUSH_RESPONDED_Event:%s]",
				GetStringOfCurrentState(), isFired ? "Fired" : "NotFired");
}

const char*
_BluetoothOppClientImpl::GetStringOfCurrentState(void) const
{
	const char* pStateString = null;

	switch (__currentState)
	{
	case _BT_OPP_CLI_STATE_DISABLED:
		pStateString = "DISABLED";
		break;

	case _BT_OPP_CLI_STATE_IDLE:
		pStateString = "IDLE";
		break;

	case _BT_OPP_CLI_STATE_BONDING:
		pStateString = "BONDING";
		break;

	case _BT_OPP_CLI_STATE_PUSH_READY:
		pStateString = "PUSH_READY";
		break;

	case _BT_OPP_CLI_STATE_REQ_CANCELING:
		pStateString = "REQ_CANCELING";
		break;

	case _BT_OPP_CLI_STATE_ON_TRANSFER:
		pStateString = "ON_TRANSFER";
		break;

	case _BT_OPP_CLI_STATE_TRANS_CANCELING:
		pStateString = "TRANS_CANCELING";
		break;

	default:
		pStateString = "Unknown";
		break;
	}

	return pStateString;
}

} } } // Tizen::Net::Bluetooth