summaryrefslogtreecommitdiff
path: root/net/nimble/host/src/ble_l2cap_sig_cmd.c
blob: a93833c6c36b46466c7f27126f18cedf64766300 (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
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

#include <string.h>
#include "ble_hs_priv.h"

int
ble_l2cap_sig_init_cmd(uint8_t op, uint8_t id, uint8_t payload_len,
                       struct os_mbuf **out_om, void **out_payload_buf)
{
    struct ble_l2cap_sig_hdr hdr;
    struct os_mbuf *txom;
    void *v;

    *out_om = NULL;
    *out_payload_buf = NULL;

    txom = ble_hs_misc_pkthdr();
    if (txom == NULL) {
        return BLE_HS_ENOMEM;
    }

    v = os_mbuf_extend(txom, BLE_L2CAP_SIG_HDR_SZ + payload_len);
    if (v == NULL) {
        return BLE_HS_ENOMEM;
    }

    hdr.op = op;
    hdr.identifier = id;
    hdr.length = TOFROMLE16(payload_len);

    ble_l2cap_sig_hdr_write(v, BLE_L2CAP_SIG_HDR_SZ, &hdr);

    *out_om = txom;
    *out_payload_buf = (uint8_t *)v + BLE_L2CAP_SIG_HDR_SZ;

    return 0;
}

static void
ble_l2cap_sig_hdr_swap(struct ble_l2cap_sig_hdr *dst,
                       struct ble_l2cap_sig_hdr *src)
{
    dst->op = src->op;
    dst->identifier = src->identifier;
    dst->length = TOFROMLE16(src->length);
}

void
ble_l2cap_sig_hdr_parse(void *payload, uint16_t len,
                        struct ble_l2cap_sig_hdr *dst)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_HDR_SZ);
    ble_l2cap_sig_hdr_swap(dst, payload);
}

void
ble_l2cap_sig_hdr_write(void *payload, uint16_t len,
                        struct ble_l2cap_sig_hdr *src)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_HDR_SZ);
    ble_l2cap_sig_hdr_swap(payload, src);
}
static void
ble_l2cap_sig_reject_swap(struct ble_l2cap_sig_reject *dst,
                          struct ble_l2cap_sig_reject *src)
{
    dst->reason = TOFROMLE16(src->reason);
}

static void
ble_l2cap_sig_reject_write(void *payload, uint16_t len,
                           struct ble_l2cap_sig_reject *src,
                           void *data, int data_len)
{
    uint8_t *u8ptr;

    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_REJECT_MIN_SZ + data_len);

    ble_l2cap_sig_reject_swap(payload, src);

    u8ptr = payload;
    u8ptr += BLE_L2CAP_SIG_REJECT_MIN_SZ;
    memcpy(u8ptr, data, data_len);
}

int
ble_l2cap_sig_reject_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
                        uint8_t id, uint16_t reason,
                        void *data, int data_len)
{
    struct ble_l2cap_sig_reject cmd;
    struct os_mbuf *txom;
    void *payload_buf;
    int rc;

    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_REJECT, id,
                                BLE_L2CAP_SIG_REJECT_MIN_SZ + data_len, &txom,
                                &payload_buf);

    cmd.reason = reason;
    ble_l2cap_sig_reject_write(payload_buf, txom->om_len, &cmd,
                               data, data_len);

    STATS_INC(ble_l2cap_stats, sig_rx);
    rc = ble_l2cap_tx(conn, chan, txom);
    return rc;
}

int
ble_l2cap_sig_reject_invalid_cid_tx(struct ble_hs_conn *conn,
                                    struct ble_l2cap_chan *chan, uint8_t id,
                                    uint16_t src_cid, uint16_t dst_cid)
{
    int rc;

    struct {
        uint16_t local_cid;
        uint16_t remote_cid;
    } data = {
        .local_cid = dst_cid,
        .remote_cid = src_cid,
    };

    rc = ble_l2cap_sig_reject_tx(conn, chan, id,
                                 BLE_L2CAP_SIG_ERR_INVALID_CID,
                                 &data, sizeof data);
    return rc;
}

static void
ble_l2cap_sig_update_req_swap(struct ble_l2cap_sig_update_req *dst,
                              struct ble_l2cap_sig_update_req *src)
{
    dst->itvl_min = TOFROMLE16(src->itvl_min);
    dst->itvl_max = TOFROMLE16(src->itvl_max);
    dst->slave_latency = TOFROMLE16(src->slave_latency);
    dst->timeout_multiplier = TOFROMLE16(src->timeout_multiplier);
}

void
ble_l2cap_sig_update_req_parse(void *payload, int len,
                               struct ble_l2cap_sig_update_req *dst)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
    ble_l2cap_sig_update_req_swap(dst, payload);
}

void
ble_l2cap_sig_update_req_write(void *payload, int len,
                               struct ble_l2cap_sig_update_req *src)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_REQ_SZ);
    ble_l2cap_sig_update_req_swap(payload, src);
}

int
ble_l2cap_sig_update_req_tx(struct ble_hs_conn *conn,
                            struct ble_l2cap_chan *chan, uint8_t id,
                            struct ble_l2cap_sig_update_req *req)
{
    struct os_mbuf *txom;
    void *payload_buf;
    int rc;

    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &txom,
                                &payload_buf);
    if (rc != 0) {
        return rc;
    }

    ble_l2cap_sig_update_req_write(payload_buf, BLE_L2CAP_SIG_UPDATE_REQ_SZ,
                                   req);

    rc = ble_l2cap_tx(conn, chan, txom);
    if (rc != 0) {
        return rc;
    }

    return 0;
}

static void
ble_l2cap_sig_update_rsp_swap(struct ble_l2cap_sig_update_rsp *dst,
                              struct ble_l2cap_sig_update_rsp *src)
{
    dst->result = TOFROMLE16(src->result);
}

void
ble_l2cap_sig_update_rsp_parse(void *payload, int len,
                               struct ble_l2cap_sig_update_rsp *dst)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
    ble_l2cap_sig_update_rsp_swap(dst, payload);
}

void
ble_l2cap_sig_update_rsp_write(void *payload, int len,
                               struct ble_l2cap_sig_update_rsp *src)
{
    BLE_HS_DBG_ASSERT(len >= BLE_L2CAP_SIG_UPDATE_RSP_SZ);
    ble_l2cap_sig_update_rsp_swap(payload, src);
}

int
ble_l2cap_sig_update_rsp_tx(struct ble_hs_conn *conn,
                            struct ble_l2cap_chan *chan, uint8_t id,
                            uint16_t result)
{
    struct ble_l2cap_sig_update_rsp rsp;
    struct os_mbuf *txom;
    void *payload_buf;
    int rc;

    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &txom,
                                &payload_buf);
    if (rc != 0) {
        return rc;
    }

    rsp.result = result;
    ble_l2cap_sig_update_rsp_write(payload_buf, BLE_L2CAP_SIG_UPDATE_RSP_SZ,
                                   &rsp);

    rc = ble_l2cap_tx(conn, chan, txom);
    if (rc != 0) {
        return rc;
    }

    return 0;
}