Loading...
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 | /* net_driver_bt.c - IP Bluetooth LE driver */ /* * Copyright (c) 2015 Intel Corporation * * 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. */ #include <nanokernel.h> #include <toolchain.h> #include <sections.h> #include <string.h> #include <errno.h> #if defined(CONFIG_STDOUT_CONSOLE) #include <stdio.h> #define PRINT printf #else #include <misc/printk.h> #define PRINT printk #endif #include <net/buf.h> #include <net/ip_buf.h> #include <net/net_core.h> #include <net/net_ip.h> #include <net/net_socket.h> #include "contiki/netstack.h" #include <net_driver_bt.h> #include <bluetooth/bluetooth.h> #include <bluetooth/uuid.h> #include <bluetooth/l2cap.h> #include <bluetooth/gatt.h> #define L2CAP_IPSP_PSM 0x0023 #define L2CAP_IPSP_MTU IP_BUF_MAX_DATA static inline void memswap(void *dst, const void *src, int len) { int i; for (i = 0; i < len; i++) { ((uint8_t *)dst)[i] = ((uint8_t *)src)[(len - 1) - i]; } } static void ipsp_connected(struct bt_l2cap_chan *chan) { struct bt_conn_info info; char src[BT_ADDR_LE_STR_LEN]; char dst[BT_ADDR_LE_STR_LEN]; linkaddr_t addr; bt_conn_get_info(chan->conn, &info); bt_addr_le_to_str(info.le.src, src, sizeof(src)); bt_addr_le_to_str(info.le.dst, dst, sizeof(dst)); NET_DBG("Channel %p Source %s connected to Destination %s\n", chan, src, dst); /* Swap bytes since net_set_mac expect big endian address */ memswap(addr.u8, info.le.src->a.val, sizeof(addr.u8)); net_set_mac(addr.u8, sizeof(addr)); } static void ipsp_disconnected(struct bt_l2cap_chan *chan) { NET_DBG("Channel %p disconnected\n", chan); } static void ipsp_recv(struct bt_l2cap_chan *chan, struct net_buf *buf) { struct bt_conn_info info; linkaddr_t src; linkaddr_t dst; NET_DBG("Incoming data channel %p len %u\n", chan, ip_buf_len(buf)); bt_conn_get_info(chan->conn, &info); /* Swap bytes since linkaddr_copy expect big endian address */ memswap(src.u8, info.le.src->a.val, sizeof(src)); memswap(dst.u8, info.le.dst->a.val, sizeof(dst)); /* Add MAC addresses to the buffer */ linkaddr_copy(&ip_buf_ll_dest(buf), &src); linkaddr_copy(&ip_buf_ll_src(buf), &dst); /* Initialize uip_len */ uip_len(buf) = ip_buf_len(buf); /* Uncompress data */ if (!NETSTACK_COMPRESS.uncompress(buf)) { NET_ERR("uncompression failed\n"); return; } /* net_recv takes ownership of the buffer */ net_buf_ref(buf); /* Add buffer to rx_queue */ if (net_recv(buf) < 0) { NET_ERR("input to IP stack failed\n"); net_buf_unref(buf); return; } } static struct net_buf *ipsp_alloc_buf(struct bt_l2cap_chan *chan) { NET_DBG("Channel %p requires buffer\n", chan); return ip_buf_get_reserve_rx(0); } static struct bt_l2cap_chan_ops ipsp_ops = { .alloc_buf = ipsp_alloc_buf, .recv = ipsp_recv, .connected = ipsp_connected, .disconnected = ipsp_disconnected, }; static struct bt_l2cap_chan ipsp_chan = { .ops = &ipsp_ops, .rx.mtu = L2CAP_IPSP_MTU, }; static int ipsp_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan) { NET_DBG("Incoming conn %p\n", conn); if (ipsp_chan.conn) { NET_ERR("No channels available"); return -ENOMEM; } *chan = &ipsp_chan; return 0; } static struct bt_l2cap_server server = { .psm = L2CAP_IPSP_PSM, .accept = ipsp_accept, }; static struct bt_gatt_attr attrs[] = { /* IPSS Service Declaration */ BT_GATT_PRIMARY_SERVICE(BT_UUID_IPSS), }; static int net_driver_bt_open(void) { bt_gatt_register(attrs, ARRAY_SIZE(attrs)); bt_l2cap_server_register(&server); return 0; } static int net_driver_bt_send(struct net_buf *buf) { #ifdef CONFIG_NETWORKING_WITH_LOGGING int orig_len = ip_buf_len(buf); #endif if (!NETSTACK_COMPRESS.compress(buf)) { NET_DBG("compression failed\n"); ip_buf_unref(buf); return -EINVAL; } NET_DBG("sending %d bytes (original len %d)\n", ip_buf_len(buf), orig_len); return bt_l2cap_chan_send(&ipsp_chan, buf); } static struct net_driver net_driver_bt = { .head_reserve = 0, .open = net_driver_bt_open, .send = net_driver_bt_send, }; int net_driver_bt_init(void) { NETSTACK_COMPRESS.init(); net_register_driver(&net_driver_bt); return 0; } |