2016-08-17 23:25:09 +05:30
|
|
|
/* rfcomm.c - RFCOMM handling */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 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 <arch/cpu.h>
|
|
|
|
#include <toolchain.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <atomic.h>
|
|
|
|
#include <misc/byteorder.h>
|
|
|
|
#include <misc/util.h>
|
|
|
|
|
|
|
|
#include <bluetooth/log.h>
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/conn.h>
|
|
|
|
#include <bluetooth/driver.h>
|
|
|
|
#include <bluetooth/l2cap.h>
|
2016-08-18 17:19:33 +05:30
|
|
|
#include <bluetooth/rfcomm.h>
|
2016-08-17 23:25:09 +05:30
|
|
|
|
|
|
|
#include "hci_core.h"
|
|
|
|
#include "conn_internal.h"
|
|
|
|
#include "l2cap_internal.h"
|
|
|
|
#include "rfcomm_internal.h"
|
|
|
|
|
|
|
|
#if !defined(CONFIG_BLUETOOTH_DEBUG_RFCOMM)
|
|
|
|
#undef BT_DBG
|
|
|
|
#define BT_DBG(fmt, ...)
|
|
|
|
#endif
|
|
|
|
|
2016-08-18 17:19:33 +05:30
|
|
|
#define RFCOMM_CHANNEL_START 0x01
|
|
|
|
#define RFCOMM_CHANNEL_END 0x1e
|
|
|
|
|
2016-08-17 23:25:09 +05:30
|
|
|
#define RFCOMM_DEFAULT_MTU 127
|
|
|
|
|
2016-08-18 17:19:33 +05:30
|
|
|
static struct bt_rfcomm_server *servers;
|
|
|
|
|
2016-08-17 23:25:09 +05:30
|
|
|
#define RFCOMM_SESSION(_ch) CONTAINER_OF(_ch, \
|
|
|
|
struct bt_rfcomm_session, br_chan.chan)
|
|
|
|
|
|
|
|
static struct bt_rfcomm_session bt_rfcomm_pool[CONFIG_BLUETOOTH_MAX_CONN];
|
|
|
|
|
2016-08-18 17:19:33 +05:30
|
|
|
static struct bt_rfcomm_server *rfcomm_server_lookup_channel(uint8_t channel)
|
|
|
|
{
|
|
|
|
struct bt_rfcomm_server *server;
|
|
|
|
|
|
|
|
for (server = servers; server; server = server->_next) {
|
|
|
|
if (server->channel == channel) {
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int bt_rfcomm_server_register(struct bt_rfcomm_server *server)
|
|
|
|
{
|
|
|
|
if (server->channel < RFCOMM_CHANNEL_START ||
|
|
|
|
server->channel > RFCOMM_CHANNEL_END || !server->accept) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if given channel is already in use */
|
|
|
|
if (rfcomm_server_lookup_channel(server->channel)) {
|
|
|
|
BT_DBG("Channel already registered");
|
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("Channel 0x%02x", server->channel);
|
|
|
|
|
|
|
|
server->_next = servers;
|
|
|
|
servers = server;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:25:09 +05:30
|
|
|
static void rfcomm_connected(struct bt_l2cap_chan *chan)
|
|
|
|
{
|
|
|
|
BT_DBG("Session %p", RFCOMM_SESSION(chan));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_disconnected(struct bt_l2cap_chan *chan)
|
|
|
|
{
|
|
|
|
BT_DBG("Session %p", RFCOMM_SESSION(chan));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rfcomm_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rfcomm_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static struct bt_l2cap_chan_ops ops = {
|
|
|
|
.connected = rfcomm_connected,
|
|
|
|
.disconnected = rfcomm_disconnected,
|
|
|
|
.recv = rfcomm_recv,
|
|
|
|
};
|
|
|
|
|
|
|
|
BT_DBG("conn %p handle %u", conn, conn->handle);
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(bt_rfcomm_pool); i++) {
|
|
|
|
struct bt_rfcomm_session *session = &bt_rfcomm_pool[i];
|
|
|
|
|
|
|
|
if (session->br_chan.chan.conn) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_DBG("session %p initialized", session);
|
|
|
|
|
|
|
|
session->br_chan.chan.ops = &ops;
|
|
|
|
session->br_chan.rx.mtu = RFCOMM_DEFAULT_MTU;
|
|
|
|
session->state = BT_RFCOMM_STATE_INIT;
|
|
|
|
|
|
|
|
*chan = &session->br_chan.chan;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BT_ERR("No available RFCOMM context for conn %p", conn);
|
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_rfcomm_init(void)
|
|
|
|
{
|
|
|
|
static struct bt_l2cap_server server = {
|
|
|
|
.psm = BT_L2CAP_PSM_RFCOMM,
|
|
|
|
.accept = rfcomm_accept,
|
|
|
|
};
|
|
|
|
|
|
|
|
bt_l2cap_br_server_register(&server);
|
|
|
|
}
|