aboutsummaryrefslogtreecommitdiffstats
path: root/src/direct_bt/L2CAPComm.cpp
blob: e23ffa746dcb9a41aacadfabe0d3e8d7da2e971f (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
/*
 * Author: Sven Gothel <sgothel@jausoft.com>
 * Copyright (c) 2020 Gothel Software e.K.
 * Copyright (c) 2020 ZAFENA AB
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <cstring>
#include <string>
#include <memory>
#include <cstdint>
#include <vector>
#include <cstdio>

#include  <algorithm>

#include "BTIoctl.hpp"
#include "HCIIoctl.hpp"
#include "L2CAPIoctl.hpp"

#include "HCIComm.hpp"
#include "L2CAPComm.hpp"

extern "C" {
    #include <unistd.h>
    #include <sys/socket.h>
    #include <poll.h>
}

#include "dbt_debug.hpp"

using namespace direct_bt;

int L2CAPComm::l2cap_open_dev(const EUI48 & adapterAddress, const uint16_t psm, const uint16_t cid, const bool pubaddrAdapter, const bool blocking) {
    sockaddr_l2 a;
    int dd, err;

    // Create a loose L2CAP socket
    dd = ::socket(AF_BLUETOOTH, // AF_BLUETOOTH == PF_BLUETOOTH
                  blocking ? SOCK_SEQPACKET : SOCK_SEQPACKET | SOCK_NONBLOCK,
                  BTPROTO_L2CAP);

    if( 0 > dd ) {
        perror("L2CAPComm::l2cap_open_dev: socket failed");
        return dd;
    }

    // Bind socket to the L2CAP adapter
    // BT Core Spec v5.2: Vol 3, Part A: L2CAP_CONNECTION_REQ
    bzero((void *)&a, sizeof(a));
    a.l2_family=AF_BLUETOOTH;
    a.l2_psm = htobs(psm);
    a.l2_bdaddr = adapterAddress;
    a.l2_cid = htobs(cid);
    a.l2_bdaddr_type = pubaddrAdapter ? L2CAPADDR_LE_PUBLIC : L2CAPADDR_LE_RANDOM;
    if ( bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0 ) {
        perror("L2CAPComm::l2cap_open_dev: bind failed");
        goto failed;
    }

    return dd;

failed:
    err = errno;
    close(dd);
    errno = err;

    return -1;
}

int L2CAPComm::l2cap_close_dev(int dd)
{
    return close(dd);
}


// *************************************************
// *************************************************
// *************************************************

#define STATE_ENUM(X) \
    X(Error) \
    X(Disconnected) \
    X(Connecting) \
    X(Connected)

#define CASE_TO_STRING(V) case V: return #V;

std::string L2CAPComm::getStateString(const State state) {
    switch(state) {
        STATE_ENUM(CASE_TO_STRING)
        default: ; // fall through intended
    }
    return "Unknown State";
}

L2CAPComm::State L2CAPComm::connect() {
    /** BT Core Spec v5.2: Vol 3, Part A: L2CAP_CONNECTION_REQ */

    sockaddr_l2 req;
    int err, res;

    if( 0 <= _dd ) {
        return state; // already open
    }

    _dd = l2cap_open_dev(device->getAdapter().getAddress(), psm, cid, true /* pubaddrAdapter */, blocking);
    if( 0 > _dd ) {
        goto failure; // open failed
    }

    // actual request to connect to remote device
    bzero((void *)&req, sizeof(req));
    req.l2_family = AF_BLUETOOTH;
    req.l2_psm = htobs(psm);
    req.l2_bdaddr = device->getAddress();
    req.l2_cid = htobs(cid);
    req.l2_bdaddr_type = pubaddr ? L2CAPADDR_LE_PUBLIC : L2CAPADDR_LE_RANDOM;

    // may block if O_NONBLOCK has not been specified in open_dev(..)
    res = ::connect(_dd, (struct sockaddr*)&req, sizeof(req));

    if( !res )
    {
        state = Connected;

    } else if( EINPROGRESS == errno ) {
        // non-blocking connection in progress (O_NONBLOCK), check via select / poll later
        state = Connecting;

    } else  {
        // EALREADY == errno || ENETUNREACH == errno || EHOSTUNREACH == errno || ..
        perror("L2CAPComm::connect: connect failed");
        goto failure;
    }

    return state;

failure:
    err = errno;
    disconnect();
    errno = err;
    state = Error;
    return state;
}

bool L2CAPComm::disconnect() {
    if( 0 > _dd ) {
        return false;
    }
    interruptReadFlag = true;
    l2cap_close_dev(_dd);
    _dd = -1;
    state = Disconnected;
    return true;
}

int L2CAPComm::read(uint8_t* buffer, const int capacity, const int timeoutMS) {
    int len = 0;
    if( 0 > _dd || 0 > capacity ) {
        goto errout;
    }
    if( 0 == capacity ) {
        goto done;
    }
    interruptReadFlag = false;

    if( timeoutMS ) {
        struct pollfd p;
        int n;

        p.fd = _dd; p.events = POLLIN;
        while ( !interruptReadFlag && (n = poll(&p, 1, timeoutMS)) < 0 ) {
            if ( !interruptReadFlag && ( errno == EAGAIN || errno == EINTR ) ) {
                // cont temp unavail or interruption
                continue;
            }
            goto errout;
        }
        if (!n) {
            errno = ETIMEDOUT;
            goto errout;
        }
    }

    while ((len = ::read(_dd, buffer, capacity)) < 0) {
        if (errno == EAGAIN || errno == EINTR ) {
            // cont temp unavail or interruption
            continue;
        }
        goto errout;
    }

done:
    return len;

errout:
    state = Error;
    return -1;

}

int L2CAPComm::write(const uint8_t * buffer, const int length) {
    int len = 0;
    if( 0 > _dd || 0 > length ) {
        goto errout;
    }
    if( 0 == length ) {
        goto done;
    }

    while( ( len = ::write(_dd, buffer, length) ) < 0 ) {
        if( EAGAIN == errno || EINTR == errno ) {
            continue;
        }
        goto errout;
    }

done:
    return len;

errout:
    state = Error;
    return -1;
}