1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <string.h>
21 #include <errno.h>
22 #include "nimble/ble.h"
23 #include "nimble/nimble_opt.h"
24 #include "host/ble_sm.h"
25 #include "ble_hs_priv.h"
26
ble_sm_cmd_get(uint8_t opcode,size_t len,struct os_mbuf ** txom)27 void *ble_sm_cmd_get(uint8_t opcode, size_t len, struct os_mbuf **txom)
28 {
29 struct ble_sm_hdr *hdr;
30 *txom = ble_hs_mbuf_l2cap_pkt();
31 if (*txom == NULL) {
32 return NULL;
33 }
34
35 if (os_mbuf_extend(*txom, sizeof(*hdr) + len) == NULL) {
36 os_mbuf_free_chain(*txom);
37 return NULL;
38 }
39
40 hdr = (struct ble_sm_hdr *)(*txom)->om_data;
41 hdr->opcode = opcode;
42 return hdr->data;
43 }
44
45 /* this function consumes tx os_mbuf */
ble_sm_tx(uint16_t conn_handle,struct os_mbuf * txom)46 int ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)
47 {
48 struct ble_l2cap_chan *chan;
49 struct ble_hs_conn *conn;
50 BLE_HS_DBG_ASSERT(ble_hs_locked_by_cur_task());
51 STATS_INC(ble_l2cap_stats, sm_tx);
52 ble_hs_misc_conn_chan_find_reqd(conn_handle, BLE_L2CAP_CID_SM,
53 &conn, &chan);
54 return ble_l2cap_tx(conn, chan, txom);
55 }