summaryrefslogtreecommitdiffstats
path: root/src/bluetooth/osx/osxbtobexsession.mm
blob: b8e604c8ec75c1bc6670e8f8b27825efd4e6350c (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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtBluetooth module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "osxbtobexsession_p.h"
#include "qbluetoothaddress.h"
#include "osxbtutility_p.h"

#include <QtCore/qloggingcategory.h>
#include <QtCore/qvector.h>
#include <QtCore/qdebug.h>
#include <QtCore/qlist.h>

#include <algorithm>
#include <cstddef>
#include <limits>

QT_BEGIN_NAMESPACE

namespace OSXBluetooth
{

OBEXSessionDelegate::~OBEXSessionDelegate()
{
}

namespace {

struct OBEXHeader
{
    OBEXHeader() : headerID(0)
    {
    }

    quint8 headerID;
    QVariant value;
};

enum {
    // Bits 7 and 8 == header's format.
    OBEXHeaderFormatMask = 0xc0,
    //
    OBEXHeaderFormatUnicode = 0,         // 87
    OBEXHeaderFormatByteSequence = 0x40, // 0100 0000
    OBEXHeaderFormat1Byte = 0x80,        // 1000 0000
    OBEXHeaderFormat4Byte = 0xc0,        // 1100 0000

};

quint32 extract_uint32(const uint8_t *bytes)
{
    // Four byte value, high byte first.
    Q_ASSERT_X(bytes, Q_FUNC_INFO, "invalid input data (null)");

    uint32_t value = uint32_t();
    std::copy(bytes, bytes + sizeof value, reinterpret_cast<uint8_t *>(&value));

    return NSSwapBigIntToHost(value);
}

quint16 extract_uint16(const uint8_t *bytes)
{
    // Two byte value, high byte first.
    Q_ASSERT_X(bytes, Q_FUNC_INFO, "invalid input data (null)");

    uint16_t value = uint16_t();
    std::copy(bytes, bytes + sizeof value, reinterpret_cast<uint8_t *>(&value));

    return NSSwapBigShortToHost(value);
}

QString extract_qstring(const uint8_t *bytes, quint16 stringLength)
{
    if (bytes && stringLength) {
        NSString * const nsString = [[NSString alloc] initWithBytes:bytes
                                                             length:stringLength
                                                           encoding:NSUnicodeStringEncoding];
        if (nsString)
            return QString::fromNSString(nsString);
    }

    // Empty string is an error, "valid" empty strings are
    // handled separately.
    return QString();
}

QList<OBEXHeader> qt_bluetooth_headers(const uint8_t *data, std::size_t length)
{
    // Convert a data from IOBluetooth into something, Qt understands.
    // Possible formats (bits 7 and 8):
    // 1. 00 Two bytes of length folowed by a null-terminated
    //    Unicode text string (length is unsigned integer;
    //    it covers the header ID and the whole of the header
    //    value, including the length bytes and the two bytes
    //    of null terminator.
    // 2. 01 Two bytes of length followed by a byte sequence (length
    //    is an unsigned integer sent high byte first; it covers
    //    the header ID and the whole of the header value).
    // 3. 10 A single byte value.
    // 4. 11 A four byte value, sent high byte first.

    Q_ASSERT_X(data, Q_FUNC_INFO, "invalid data (null)");
    Q_ASSERT_X(length >= 2, Q_FUNC_INFO, "invalid data length");

    Q_UNUSED(data)
    Q_UNUSED(length)

    QList<OBEXHeader> empty;
    QList<OBEXHeader> qtHeaders;

    for (std::size_t i = 0; i < length;) {
        std::size_t headerLength = 0;
        OBEXHeader header;
        header.headerID = data[i];

        switch (data[i] & OBEXHeaderFormatMask) {
        case OBEXHeaderFormatUnicode:
        {
            if (i + 3 > length)
                return empty;
            headerLength = extract_uint16(data + i + 1);
            // Invalid length or input data:
            if (headerLength < 3 || i + headerLength > length)
                return empty;
            if (headerLength == 3 || headerLength == 5) { // Can 5 ever happen?
                header.value.fromValue<QString>(QString());
            } else if (headerLength > 5) {// TODO: We do not check now, that the string actually valid.
                const QString value(extract_qstring(data + i + 3, headerLength - 5));
                if (!value.length()) // Some error?
                    return empty;
                header.value.setValue<QString>(value);
            } else // Still something weird.
                return empty;
            break;
        }
        case OBEXHeaderFormatByteSequence:
        {
            if (i + 3 > length)
                return empty;
            headerLength = extract_uint16(data + i + 1);
            // Something is wrong:
            if (headerLength < 3 || i + headerLength > length)
                return empty;
            QVector<unsigned char> value;
            if (headerLength > 3) {
                value.resize(headerLength - 3);
                std::copy(data, data + headerLength, value.begin());
            }
            header.value.setValue<QVector<unsigned char> >(value);
            break;
        }
        case OBEXHeaderFormat1Byte:
        {
            // 1 byte integer + 1 byte headerID == 2
            if (i + 2 > length)
                return empty;
            headerLength = 2;
            header.value.setValue<quint8>(data[i + 1]);
            break;
        }
        case OBEXHeaderFormat4Byte:
        {
            // 4 byte integer + 1 byte headerID == 5
            if (i + 5 > length)
                return empty;
            headerLength = 5;
            header.value.setValue<quint32>(extract_uint32(data + i + 1));
            break;
        }
        default:
            qCWarning(QT_BT_OSX) << "invalid header format";
            return empty;
        }

        i += headerLength;
        qtHeaders.push_back(header);
    }

    return qtHeaders;
}

bool append_uint16(ObjCStrongReference<NSMutableData> headers, uint16_t value)
{
    if (!headers)
        return false;

    const NSUInteger length = [headers length];
    const uint16_t valueSwapped = NSSwapHostShortToBig(value);
    [headers appendBytes:&valueSwapped length:sizeof valueSwapped];

    return [headers length] - length == 2;
}


bool append_four_byte_header(ObjCStrongReference<NSMutableData> headers, uint8_t headerID,
                             uint32_t headerValue)
{
    if (!headers)
        return false;

    const NSUInteger length = [headers length];
    // Header ID (1 byte)
    [headers appendBytes:&headerID length:1];
    // Header value (4 bytes)
    const uint32_t valueSwapped(NSSwapHostIntToBig(headerValue));
    [headers appendBytes:&valueSwapped length:sizeof valueSwapped];

    return [headers length] - length == 5;
}

bool append_unicode_header(ObjCStrongReference<NSMutableData> headers, uint8_t headerID,
                           const QString &string)
{
    // Two bytes of length followed by a null-terminated
    // Unicode text string. Length is unsigned integer,
    // it covers the header ID and the whole of the header
    // value, including the length bytes and the two bytes
    // of null terminator.
    // All Obj-C objects are autoreleased.

    if (!headers)
        return false;

    QT_BT_MAC_AUTORELEASEPOOL;

    const NSUInteger initialLength = [headers length];
    [headers appendBytes:&headerID length:1];

    if (!string.length()) {
        // Empty string. The length is 3
        // (header ID + length value itself).
        return append_uint16(headers, 3);
    }

    NSString *const nsString = string.toNSString();
    if (!nsString)
        return false;

    // TODO: check if the encodings is right. It was NSUnicodeStringEncoding but
    // byte order was wrong. Also, I do not need BOM check anymore?
    NSData *const data = [nsString dataUsingEncoding:NSUTF16BigEndianStringEncoding];
    if (!data)
        return false;

    // This data can include byte-order marker (BOM) and does not include
    // a null terminator. Anyway, the length must be >= 2.
    NSUInteger length = [data length];
    if (length < 2)
        return false;

    const uint8_t *dataPtr = static_cast<const uint8_t *>([data bytes]);
    if ((dataPtr[0] == 0xff && dataPtr[1] == 0xfe)
        || (dataPtr[0] == 0xfe && dataPtr[1] == 0xff)) {
        if (length == 2) //Something weird?
            return false;
        // Skip a BOM.
        dataPtr += 2;
        length -= 2;
    }

    // headerID + length == 3, string's length + 2
    // bytes for a null terminator.
    if (!append_uint16(headers, length + 3 + 2))
        return false;

    [headers appendBytes:dataPtr length:length];
    const uint8_t nullTerminator[2] = {};
    [headers appendBytes:nullTerminator length:2];

    return [headers length] - initialLength == length + 3 + 2;
}

ObjCStrongReference<NSMutableData> next_data_chunk(QIODevice &inputStream, IOBluetoothOBEXSession *session,
                                                   NSUInteger headersLength, bool &isLast)
{
    // Work only for OBEX put (we request a specific payload length).
    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid OBEX session (nil)");

    const OBEXMaxPacketLength packetSize = [session getAvailableCommandPayloadLength:kOBEXOpCodePut];
    if (!packetSize || headersLength >= packetSize)
        return ObjCStrongReference<NSMutableData>();

    const OBEXMaxPacketLength maxBodySize = packetSize - headersLength;

    QVector<char> block(maxBodySize);
    const int realSize = inputStream.read(block.data(), block.size());
    if (realSize <= 0) {
        // Well, either the last or an error.
        isLast = true;
        return ObjCStrongReference<NSMutableData>();
    }

    ObjCStrongReference<NSMutableData> chunk([NSMutableData dataWithBytes:block.data()
                                              length:realSize], true);
    if (chunk && [chunk length]) {
        // If it actually was the last chunk
        // of a length == maxBodySize, we'll
        // send one more packet (empty though)?
        isLast = [chunk length] < maxBodySize;
    }

    return chunk;
}

bool check_connect_event(const OBEXSessionEvent *e, OBEXError &error, OBEXOpCode &response)
{
    Q_ASSERT_X(e, Q_FUNC_INFO, "invalid event (null)");

    // This function tries to extract either an error code or a
    // server response code. "Good" event has type connect command respond
    // and reponse code 0XA0. Everything else is a "bad" event and
    // means connect failed.

    // If it's an error event - return the error.
    // If it's connect response - extract the response code.
    // If it's something else (is it possible?) - set general error.

    if (e->type == kOBEXSessionEventTypeError) {
        error = e->u.errorData.error;
        return false;
    } if (e->type == kOBEXSessionEventTypeConnectCommandResponseReceived) {
        // We can read response code only for such an event.
        response = e->u.connectCommandResponseData.serverResponseOpCode;
        return response == kOBEXResponseCodeSuccessWithFinalBit;
    } else {
        qCWarning(QT_BT_OSX) << "unexpected event type";
        error = kOBEXGeneralError;
        return false;
    }
}

bool check_put_event(const OBEXSessionEvent *e, OBEXError &error, OBEXOpCode &response)
{
    Q_ASSERT_X(e, Q_FUNC_INFO, "invalid event (null)");

    // See the comments above.

    if (e->type == kOBEXSessionEventTypeError) {
        error = e->u.errorData.error;
        return false;
    } else if (e->type == kOBEXSessionEventTypePutCommandResponseReceived) {
        response = e->u.putCommandResponseData.serverResponseOpCode;
        return response == kOBEXResponseCodeContinueWithFinalBit ||
               response == kOBEXResponseCodeSuccessWithFinalBit;
    } else {
        qCWarning(QT_BT_OSX) << "unexpected event type";
        error = kOBEXGeneralError;
        return false;
    }
}

bool check_abort_event(const OBEXSessionEvent *e, OBEXError &error, OBEXOpCode &response)
{
    Q_ASSERT_X(e, Q_FUNC_INFO, "invalid event (null)");

    if (e->type == kOBEXSessionEventTypeError) {
        error = e->u.errorData.error;
        return false;
    } else if (e->type == kOBEXSessionEventTypeAbortCommandResponseReceived) {
        response = e->u.abortCommandResponseData.serverResponseOpCode;
        return response == kOBEXResponseCodeSuccessWithFinalBit;
    } else {
        qCWarning(QT_BT_OSX) << "unexpected event type";
        return false;
    }
}

} // Unnamed namespace.
} // OSXBluetooth.

QT_END_NAMESPACE

QT_USE_NAMESPACE

@interface QT_MANGLE_NAMESPACE(OSXBTOBEXSession) (PrivateAPI)

// OBEXDisconnect returns void - it's considered to be always
// successful. These methods are "private API" - no need to expose them,
// for internal use only.
- (void)OBEXDisconnect;
- (void)OBEXDisconnectHandler:(const OBEXSessionEvent*)event;

@end

@implementation QT_MANGLE_NAMESPACE(OSXBTOBEXSession)

+ (OBEXMaxPacketLength) maxPacketLength
{
    // Some arbitrary number, we'll adjust it as soon as
    // we connected, asking a session about packet size for
    // a particular command.
    return 0x1000;
}

- (id)initWithDelegate:(QT_PREPEND_NAMESPACE(OSXBluetooth::OBEXSessionDelegate) *)aDelegate
      remoteDevice:(const QBluetoothAddress &)deviceAddress channelID:(quint16)port
{
    Q_ASSERT_X(aDelegate, Q_FUNC_INFO, "invalid delegate (null)");
    Q_ASSERT_X(!deviceAddress.isNull(), Q_FUNC_INFO, "invalid remote device address");
    Q_ASSERT_X(port, Q_FUNC_INFO, "invalid port (0)");

    if (self = [super init]) {
        connected = false;
        currentRequest = OSXBluetooth::OBEXNoop;
        connectionID = 0;
        connectionIDFound = false;

        const BluetoothDeviceAddress addr(OSXBluetooth::iobluetooth_address(deviceAddress));
        device = [[IOBluetoothDevice deviceWithAddress:&addr] retain];
        if (!device) {
            qCWarning(QT_BT_OSX) << "failed to create an IOBluetoothDevice";
            return self;
        }

        session = [[IOBluetoothOBEXSession alloc] initWithDevice:device channelID:port];
        if (!session) {
            qCWarning(QT_BT_OSX) << "failed to create an OBEX session";
            return self;
        }

        delegate = aDelegate;
        channelID = port;
    }

    return self;
}

- (void)dealloc
{
    [device release];
    [session release];

    [headersData release];
    [bodyData release];

    [super dealloc];
}

- (OBEXError)OBEXConnect
{
    if (!session) {
        qCWarning(QT_BT_OSX) << "invalid session (nil)";
        return kOBEXGeneralError;
    }

    // That's a "single-shot" operation:
    Q_ASSERT_X(currentRequest == OSXBluetooth::OBEXNoop, Q_FUNC_INFO,
               "can not connect in this state (another request is active)");

    connected = false;
    connectionIDFound = false;
    connectionID = 0;
    currentRequest = OSXBluetooth::OBEXConnect;

    const OBEXError status = [session OBEXConnect:kOBEXConnectFlagNone
                                      maxPacketLength:[QT_MANGLE_NAMESPACE(OSXBTOBEXSession) maxPacketLength]
                                      optionalHeaders:Q_NULLPTR
                                      optionalHeadersLength:0
                                      eventSelector:@selector(OBEXConnectHandler:)
                                      selectorTarget:self
                                      refCon:Q_NULLPTR];

    if (status != kOBEXSuccess) {
        currentRequest = OSXBluetooth::OBEXNoop;
        // Already connected is still ok for us?
        connected = status == kOBEXSessionAlreadyConnectedError;
    }

    return status;
}

- (void)OBEXConnectHandler:(const OBEXSessionEvent*)event
{
    using namespace OSXBluetooth;

    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid session (nil)");

    if (pendingAbort) {
        currentRequest = OBEXNoop;
        [self OBEXAbort];
        return;
    }

    if (currentRequest != OBEXConnect) {
        qCWarning(QT_BT_OSX) << "called while there is no "
                                "active connect request";
        return;
    }

    currentRequest = OBEXNoop;

    OBEXError errorCode = kOBEXSuccess;
    OBEXOpCode responseCode = kOBEXResponseCodeSuccessWithFinalBit;

    if (!check_connect_event(event, errorCode, responseCode)) {
        // OBEX connect failed.
        if (delegate)
            delegate->OBEXConnectError(errorCode, responseCode);
        return;
    }

    const OBEXConnectCommandResponseData *const response = &event->u.connectCommandResponseData;
    if (response->headerDataPtr && response->headerDataLength >= 2) {
        // 2 == 1 byte headerID + at least 1 byte headerValue ...
        QList<OBEXHeader> headers(qt_bluetooth_headers(static_cast<const uint8_t *>(response->headerDataPtr),
                                  response->headerDataLength));
        // ConnectionID is used when multiplexing OBEX connections
        // to identify which particular connection this object is
        // being sent on. When used, this _must_ be the first
        // header sent.

        foreach (const OBEXHeader &header, headers) {
            if (header.headerID == kOBEXHeaderIDConnectionID) {
                connectionID = header.value.value<quint32>();
                connectionIDFound = true;
                break;
            }
        }
    }

    connected = true;

    if (delegate)
        delegate->OBEXConnectSuccess();
}

- (OBEXError)OBEXAbort
{
    using namespace OSXBluetooth;

    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid OBEX session (nil)");

    if (currentRequest == OBEXNoop) {
        pendingAbort = false;

        if (![self isConnected])
            return kOBEXSessionNotConnectedError;

        currentRequest = OBEXAbort;
        const OBEXError status = [session OBEXAbort:Q_NULLPTR
                                          optionalHeadersLength:0
                                          eventSelector:@selector(OBEXAbortHandler:)
                                          selectorTarget:self
                                          refCon:Q_NULLPTR];
        if (status != kOBEXSuccess)
            currentRequest = OBEXNoop;

        return status;
    } else {
        // We're in the middle of some request, wait
        // for any handler to be called first.
        pendingAbort = true;
        return kOBEXSuccess;
    }
}

- (void)OBEXAbortHandler:(const OBEXSessionEvent*)event
{
    using namespace OSXBluetooth;

    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid OBEX session (nil)");

    if (currentRequest != OBEXAbort) {
        qCWarning(QT_BT_OSX) << "called while there "
                                "is no ABORT request";
        return;
    }

    pendingAbort = false;
    currentRequest = OBEXNoop;

    if (delegate) {
        OBEXError error = kOBEXSuccess;
        OBEXOpCode response = kOBEXResponseCodeSuccessWithFinalBit;
        if (check_abort_event(event, error, response))
            delegate->OBEXAbortSuccess();
    }
}

- (OBEXError)OBEXPutFile:(QT_PREPEND_NAMESPACE(QIODevice) *)input withName:(const QString &)name
{
    using namespace OSXBluetooth;

    if (!session || ![self isConnected])
        return kOBEXSessionNotConnectedError;

    Q_ASSERT_X(currentRequest == OBEXNoop, Q_FUNC_INFO,
               "the current session has an active request already");
    Q_ASSERT_X(input, Q_FUNC_INFO, "invalid input stream (null)");
    Q_ASSERT_X(input->isReadable(), Q_FUNC_INFO, "invalid input stream (not readable)");

    // We send a put request with a couple of headers (size/file name/may be connection ID) +
    // a payload.
    const qint64 fileSize = input->size();
    if (fileSize <= 0 || fileSize >= std::numeric_limits<uint32_t>::max()) {
        qCWarning(QT_BT_OSX) << "invalid input file size";
        return kOBEXBadArgumentError;
    }

    ObjCStrongReference<NSMutableData> headers([[NSMutableData alloc] init], false);
    if (!headers) {
        qCWarning(QT_BT_OSX) << "failed to allocate headers";
        return kOBEXNoResourcesError;
    }

    // Now we append headers with: Connection ID (if any),
    // file name, file size, the first (and probably the only) chunk of data
    // from the input stream and send a put request.

    if (connectionIDFound) {
        if (!append_four_byte_header(headers, kOBEXHeaderIDConnectionID, connectionID)) {
            qCWarning(QT_BT_OSX) << "failed to append connection ID header";
            return kOBEXNoResourcesError;
        }
    }

    if (name.length()) {
        if (!append_unicode_header(headers, kOBEXHeaderIDName, name)) {
            qCWarning(QT_BT_OSX) << "failed to append a unicode string";
            return kOBEXNoResourcesError;
        }
    }

    if (fileSize && !input->isSequential())
        append_four_byte_header(headers, kOBEXHeaderIDLength, uint32_t(fileSize));

    bool lastChunk = false;
    ObjCStrongReference<NSMutableData> chunk(next_data_chunk(*input, session, [headers length], lastChunk));
    if (!chunk || ![chunk length]) {
        // We do not support PUT-DELETE (?)
        // At least the first chunk is expected to be non-empty.
        qCWarning(QT_BT_OSX) << "invalid input stream";
        return kOBEXBadArgumentError;
    }

    currentRequest = OBEXPut;

    const OBEXError status = [session OBEXPut:lastChunk
                                      headersData:[headers mutableBytes]
                                      headersDataLength:[headers length]
                                      bodyData:[chunk mutableBytes]
                                      bodyDataLength:[chunk length]
                                      eventSelector:@selector(OBEXPutHandler:)
                                      selectorTarget:self
                                      refCon:Q_NULLPTR];

    if (status == kOBEXSuccess) {
        if (delegate && fileSize && !input->isSequential())
            delegate->OBEXPutDataSent([chunk length], fileSize);

        bytesSent = [chunk length];
        headersData = headers.take();
        bodyData = chunk.take();
        inputStream = input;
    } else {
        // PUT request failed and we now
        // want to close a connection/session.
        currentRequest = OBEXNoop;
        // Try to cleanup (disconnect).
        [self OBEXDisconnect];
    }

    return status;
}

- (void)OBEXPutHandler:(const OBEXSessionEvent*)event
{
    using namespace OSXBluetooth;

    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid OBEX session (nil)");

    if (pendingAbort) {
        currentRequest = OBEXNoop;
        [self OBEXAbort];
        return;
    }

    if (currentRequest != OBEXPut) {
        qCWarning(QT_BT_OSX) << "called while the current "
                                "request is not a put request";
        return;
    }

    OBEXError error = kOBEXSuccess;
    OBEXOpCode responseCode = kOBEXResponseCodeSuccessWithFinalBit;
    if (!check_put_event(event, error, responseCode)) {
        currentRequest = OBEXNoop;
        if (delegate)
            delegate->OBEXPutError(error, responseCode);
        [self OBEXDisconnect];
        return;
    }

    // Now try to send more data if we have any.
    if (responseCode == kOBEXResponseCodeContinueWithFinalBit) {
        // Send more data.
        bool lastChunk = false;
        // 0 for the headers length, no more headers.
        ObjCStrongReference<NSMutableData> chunk(next_data_chunk(*inputStream, session, 0, lastChunk));
        if (!chunk && !lastChunk) {
            qCWarning(QT_BT_OSX) << "failed to allocate the next memory chunk";
            return;
        }

        void *dataPtr = chunk ? [chunk mutableBytes] : Q_NULLPTR;
        const NSUInteger dataSize = chunk ? [chunk length] : 0;
        const OBEXError status = [session OBEXPut:lastChunk
                                          headersData:Q_NULLPTR
                                          headersDataLength:0
                                          bodyData:dataPtr
                                          bodyDataLength:dataSize
                                          eventSelector:@selector(OBEXPutHandler:)
                                          selectorTarget:self
                                          refCon:Q_NULLPTR];

        if (status != kOBEXSuccess) {
            qCWarning(QT_BT_OSX) << "failed to send the next memory chunk";
            currentRequest = OBEXNoop;
            if (delegate) // Response code is not important here.
                delegate->OBEXPutError(kOBEXNoResourcesError, 0);

            [self OBEXDisconnect];
        } else {
            [bodyData release];
            bytesSent += [chunk length];
            bodyData = chunk.take();//retained already.

            if (delegate && !inputStream->isSequential())
                delegate->OBEXPutDataSent(bytesSent, inputStream->size());
        }
    } else if (responseCode == kOBEXResponseCodeSuccessWithFinalBit) {
        currentRequest = OBEXNoop;
        if (delegate)
            delegate->OBEXPutSuccess();

        [self OBEXDisconnect];
    }
}

- (void)OBEXDisconnect
{
    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid session (nil)");

    currentRequest = OSXBluetooth::OBEXDisconnect;

    [session OBEXDisconnect:Q_NULLPTR
             optionalHeadersLength:0
             eventSelector:@selector(OBEXDisconnectHandler:)
             selectorTarget:self
             refCon:Q_NULLPTR];
}

- (void)OBEXDisconnectHandler:(const OBEXSessionEvent*)event
{
    Q_UNUSED(event)

    Q_ASSERT_X(session, Q_FUNC_INFO, "invalid session (nil)");

    // Event can have an error type, but there's nothing
    // we can do - even "cleanup" failed.
    connected = false;
}

- (bool)isConnected
{
    return device && session && connected;
}

- (void)closeSession
{
    // Clear the delegate and reset the request,
    // do not try any of OBEX commands - the session will be deleted
    // immediately.
    delegate = Q_NULLPTR;
    // This will stop any handler (callback) preventing
    // any read/write to potentially deleted objects.
    currentRequest = OSXBluetooth::OBEXNoop;
}

- (bool)hasActiveRequest
{
    return currentRequest != OSXBluetooth::OBEXNoop && !pendingAbort;
}

@end