📄 core.c
字号:
/* RFCOMM implementation for Linux Bluetooth stack (BlueZ). Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com> Copyright (C) 2002 Marcel Holtmann <marcel@holtmann.org> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; 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 OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED.*//* * Bluetooth RFCOMM core. * * $Id: core.c,v 1.42 2002/10/01 23:26:25 maxk Exp $ */#include <linux/config.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/signal.h>#include <linux/init.h>#include <linux/wait.h>#include <linux/device.h>#include <linux/net.h>#include <net/sock.h>#include <asm/uaccess.h>#include <asm/unaligned.h>#include <net/bluetooth/bluetooth.h>#include <net/bluetooth/hci_core.h>#include <net/bluetooth/l2cap.h>#include <net/bluetooth/rfcomm.h>#define VERSION "1.6"#ifndef CONFIG_BT_RFCOMM_DEBUG#undef BT_DBG#define BT_DBG(D...)#endifstatic struct task_struct *rfcomm_thread;static DECLARE_MUTEX(rfcomm_sem);#define rfcomm_lock() down(&rfcomm_sem);#define rfcomm_unlock() up(&rfcomm_sem);static unsigned long rfcomm_event;static LIST_HEAD(session_list);static atomic_t terminate, running;static int rfcomm_send_frame(struct rfcomm_session *s, u8 *data, int len);static int rfcomm_send_sabm(struct rfcomm_session *s, u8 dlci);static int rfcomm_send_disc(struct rfcomm_session *s, u8 dlci);static int rfcomm_queue_disc(struct rfcomm_dlc *d);static int rfcomm_send_nsc(struct rfcomm_session *s, int cr, u8 type);static int rfcomm_send_pn(struct rfcomm_session *s, int cr, struct rfcomm_dlc *d);static int rfcomm_send_msc(struct rfcomm_session *s, int cr, u8 dlci, u8 v24_sig);static int rfcomm_send_test(struct rfcomm_session *s, int cr, u8 *pattern, int len);static int rfcomm_send_credits(struct rfcomm_session *s, u8 addr, u8 credits);static void rfcomm_make_uih(struct sk_buff *skb, u8 addr);static void rfcomm_process_connect(struct rfcomm_session *s);static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src, bdaddr_t *dst, int *err);static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst);static void rfcomm_session_del(struct rfcomm_session *s);/* ---- RFCOMM frame parsing macros ---- */#define __get_dlci(b) ((b & 0xfc) >> 2)#define __get_channel(b) ((b & 0xf8) >> 3)#define __get_dir(b) ((b & 0x04) >> 2)#define __get_type(b) ((b & 0xef))#define __test_ea(b) ((b & 0x01))#define __test_cr(b) ((b & 0x02))#define __test_pf(b) ((b & 0x10))#define __addr(cr, dlci) (((dlci & 0x3f) << 2) | (cr << 1) | 0x01)#define __ctrl(type, pf) (((type & 0xef) | (pf << 4)))#define __dlci(dir, chn) (((chn & 0x1f) << 1) | dir)#define __srv_channel(dlci) (dlci >> 1)#define __dir(dlci) (dlci & 0x01)#define __len8(len) (((len) << 1) | 1)#define __len16(len) ((len) << 1)/* MCC macros */#define __mcc_type(cr, type) (((type << 2) | (cr << 1) | 0x01))#define __get_mcc_type(b) ((b & 0xfc) >> 2)#define __get_mcc_len(b) ((b & 0xfe) >> 1)/* RPN macros */#define __rpn_line_settings(data, stop, parity) ((data & 0x3) | ((stop & 0x1) << 2) | ((parity & 0x7) << 3))#define __get_rpn_data_bits(line) ((line) & 0x3)#define __get_rpn_stop_bits(line) (((line) >> 2) & 0x1)#define __get_rpn_parity(line) (((line) >> 3) & 0x7)static inline void rfcomm_schedule(uint event){ if (!rfcomm_thread) return; //set_bit(event, &rfcomm_event); set_bit(RFCOMM_SCHED_WAKEUP, &rfcomm_event); wake_up_process(rfcomm_thread);}static inline void rfcomm_session_put(struct rfcomm_session *s){ if (atomic_dec_and_test(&s->refcnt)) rfcomm_session_del(s);}/* ---- RFCOMM FCS computation ---- *//* reversed, 8-bit, poly=0x07 */static unsigned char rfcomm_crc_table[256] = { 0x00, 0x91, 0xe3, 0x72, 0x07, 0x96, 0xe4, 0x75, 0x0e, 0x9f, 0xed, 0x7c, 0x09, 0x98, 0xea, 0x7b, 0x1c, 0x8d, 0xff, 0x6e, 0x1b, 0x8a, 0xf8, 0x69, 0x12, 0x83, 0xf1, 0x60, 0x15, 0x84, 0xf6, 0x67, 0x38, 0xa9, 0xdb, 0x4a, 0x3f, 0xae, 0xdc, 0x4d, 0x36, 0xa7, 0xd5, 0x44, 0x31, 0xa0, 0xd2, 0x43, 0x24, 0xb5, 0xc7, 0x56, 0x23, 0xb2, 0xc0, 0x51, 0x2a, 0xbb, 0xc9, 0x58, 0x2d, 0xbc, 0xce, 0x5f, 0x70, 0xe1, 0x93, 0x02, 0x77, 0xe6, 0x94, 0x05, 0x7e, 0xef, 0x9d, 0x0c, 0x79, 0xe8, 0x9a, 0x0b, 0x6c, 0xfd, 0x8f, 0x1e, 0x6b, 0xfa, 0x88, 0x19, 0x62, 0xf3, 0x81, 0x10, 0x65, 0xf4, 0x86, 0x17, 0x48, 0xd9, 0xab, 0x3a, 0x4f, 0xde, 0xac, 0x3d, 0x46, 0xd7, 0xa5, 0x34, 0x41, 0xd0, 0xa2, 0x33, 0x54, 0xc5, 0xb7, 0x26, 0x53, 0xc2, 0xb0, 0x21, 0x5a, 0xcb, 0xb9, 0x28, 0x5d, 0xcc, 0xbe, 0x2f, 0xe0, 0x71, 0x03, 0x92, 0xe7, 0x76, 0x04, 0x95, 0xee, 0x7f, 0x0d, 0x9c, 0xe9, 0x78, 0x0a, 0x9b, 0xfc, 0x6d, 0x1f, 0x8e, 0xfb, 0x6a, 0x18, 0x89, 0xf2, 0x63, 0x11, 0x80, 0xf5, 0x64, 0x16, 0x87, 0xd8, 0x49, 0x3b, 0xaa, 0xdf, 0x4e, 0x3c, 0xad, 0xd6, 0x47, 0x35, 0xa4, 0xd1, 0x40, 0x32, 0xa3, 0xc4, 0x55, 0x27, 0xb6, 0xc3, 0x52, 0x20, 0xb1, 0xca, 0x5b, 0x29, 0xb8, 0xcd, 0x5c, 0x2e, 0xbf, 0x90, 0x01, 0x73, 0xe2, 0x97, 0x06, 0x74, 0xe5, 0x9e, 0x0f, 0x7d, 0xec, 0x99, 0x08, 0x7a, 0xeb, 0x8c, 0x1d, 0x6f, 0xfe, 0x8b, 0x1a, 0x68, 0xf9, 0x82, 0x13, 0x61, 0xf0, 0x85, 0x14, 0x66, 0xf7, 0xa8, 0x39, 0x4b, 0xda, 0xaf, 0x3e, 0x4c, 0xdd, 0xa6, 0x37, 0x45, 0xd4, 0xa1, 0x30, 0x42, 0xd3, 0xb4, 0x25, 0x57, 0xc6, 0xb3, 0x22, 0x50, 0xc1, 0xba, 0x2b, 0x59, 0xc8, 0xbd, 0x2c, 0x5e, 0xcf};/* CRC on 2 bytes */#define __crc(data) (rfcomm_crc_table[rfcomm_crc_table[0xff ^ data[0]] ^ data[1]])/* FCS on 2 bytes */ static inline u8 __fcs(u8 *data){ return (0xff - __crc(data));}/* FCS on 3 bytes */ static inline u8 __fcs2(u8 *data){ return (0xff - rfcomm_crc_table[__crc(data) ^ data[2]]);}/* Check FCS */static inline int __check_fcs(u8 *data, int type, u8 fcs){ u8 f = __crc(data); if (type != RFCOMM_UIH) f = rfcomm_crc_table[f ^ data[2]]; return rfcomm_crc_table[f ^ fcs] != 0xcf;}/* ---- L2CAP callbacks ---- */static void rfcomm_l2state_change(struct sock *sk){ BT_DBG("%p state %d", sk, sk->sk_state); rfcomm_schedule(RFCOMM_SCHED_STATE);}static void rfcomm_l2data_ready(struct sock *sk, int bytes){ BT_DBG("%p bytes %d", sk, bytes); rfcomm_schedule(RFCOMM_SCHED_RX);}static int rfcomm_l2sock_create(struct socket **sock){ int err; BT_DBG(""); err = sock_create_kern(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP, sock); if (!err) { struct sock *sk = (*sock)->sk; sk->sk_data_ready = rfcomm_l2data_ready; sk->sk_state_change = rfcomm_l2state_change; } return err;}/* ---- RFCOMM DLCs ---- */static void rfcomm_dlc_timeout(unsigned long arg){ struct rfcomm_dlc *d = (void *) arg; BT_DBG("dlc %p state %ld", d, d->state); set_bit(RFCOMM_TIMED_OUT, &d->flags); rfcomm_dlc_put(d); rfcomm_schedule(RFCOMM_SCHED_TIMEO);}static void rfcomm_dlc_set_timer(struct rfcomm_dlc *d, long timeout){ BT_DBG("dlc %p state %ld timeout %ld", d, d->state, timeout); if (!mod_timer(&d->timer, jiffies + timeout)) rfcomm_dlc_hold(d);}static void rfcomm_dlc_clear_timer(struct rfcomm_dlc *d){ BT_DBG("dlc %p state %ld", d, d->state); if (timer_pending(&d->timer) && del_timer(&d->timer)) rfcomm_dlc_put(d);}static void rfcomm_dlc_clear_state(struct rfcomm_dlc *d){ BT_DBG("%p", d); d->state = BT_OPEN; d->flags = 0; d->mscex = 0; d->mtu = RFCOMM_DEFAULT_MTU; d->v24_sig = RFCOMM_V24_RTC | RFCOMM_V24_RTR | RFCOMM_V24_DV; d->cfc = RFCOMM_CFC_DISABLED; d->rx_credits = RFCOMM_DEFAULT_CREDITS;}struct rfcomm_dlc *rfcomm_dlc_alloc(gfp_t prio){ struct rfcomm_dlc *d = kmalloc(sizeof(*d), prio); if (!d) return NULL; memset(d, 0, sizeof(*d)); init_timer(&d->timer); d->timer.function = rfcomm_dlc_timeout; d->timer.data = (unsigned long) d; skb_queue_head_init(&d->tx_queue); spin_lock_init(&d->lock); atomic_set(&d->refcnt, 1); rfcomm_dlc_clear_state(d); BT_DBG("%p", d); return d;}void rfcomm_dlc_free(struct rfcomm_dlc *d){ BT_DBG("%p", d); skb_queue_purge(&d->tx_queue); kfree(d);}static void rfcomm_dlc_link(struct rfcomm_session *s, struct rfcomm_dlc *d){ BT_DBG("dlc %p session %p", d, s); rfcomm_session_hold(s); rfcomm_dlc_hold(d); list_add(&d->list, &s->dlcs); d->session = s;}static void rfcomm_dlc_unlink(struct rfcomm_dlc *d){ struct rfcomm_session *s = d->session; BT_DBG("dlc %p refcnt %d session %p", d, atomic_read(&d->refcnt), s); list_del(&d->list); d->session = NULL; rfcomm_dlc_put(d); rfcomm_session_put(s);}static struct rfcomm_dlc *rfcomm_dlc_get(struct rfcomm_session *s, u8 dlci){ struct rfcomm_dlc *d; struct list_head *p; list_for_each(p, &s->dlcs) { d = list_entry(p, struct rfcomm_dlc, list); if (d->dlci == dlci) return d; } return NULL;}static int __rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel){ struct rfcomm_session *s; int err = 0; u8 dlci; BT_DBG("dlc %p state %ld %s %s channel %d", d, d->state, batostr(src), batostr(dst), channel); if (channel < 1 || channel > 30) return -EINVAL; if (d->state != BT_OPEN && d->state != BT_CLOSED) return 0; s = rfcomm_session_get(src, dst); if (!s) { s = rfcomm_session_create(src, dst, &err); if (!s) return err; } dlci = __dlci(!s->initiator, channel); /* Check if DLCI already exists */ if (rfcomm_dlc_get(s, dlci)) return -EBUSY; rfcomm_dlc_clear_state(d); d->dlci = dlci; d->addr = __addr(s->initiator, dlci); d->priority = 7; d->state = BT_CONFIG; rfcomm_dlc_link(s, d); d->mtu = s->mtu; d->cfc = (s->cfc == RFCOMM_CFC_UNKNOWN) ? 0 : s->cfc; if (s->state == BT_CONNECTED) rfcomm_send_pn(s, 1, d); rfcomm_dlc_set_timer(d, RFCOMM_CONN_TIMEOUT); return 0;}int rfcomm_dlc_open(struct rfcomm_dlc *d, bdaddr_t *src, bdaddr_t *dst, u8 channel){ int r; rfcomm_lock(); r = __rfcomm_dlc_open(d, src, dst, channel); rfcomm_unlock(); return r;}static int __rfcomm_dlc_close(struct rfcomm_dlc *d, int err){ struct rfcomm_session *s = d->session; if (!s) return 0; BT_DBG("dlc %p state %ld dlci %d err %d session %p", d, d->state, d->dlci, err, s); switch (d->state) { case BT_CONNECTED: case BT_CONFIG: case BT_CONNECT: d->state = BT_DISCONN; if (skb_queue_empty(&d->tx_queue)) { rfcomm_send_disc(s, d->dlci); rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT); } else { rfcomm_queue_disc(d); rfcomm_dlc_set_timer(d, RFCOMM_DISC_TIMEOUT * 2); } break; default: rfcomm_dlc_clear_timer(d); rfcomm_dlc_lock(d); d->state = BT_CLOSED; d->state_change(d, err); rfcomm_dlc_unlock(d); skb_queue_purge(&d->tx_queue); rfcomm_dlc_unlink(d); } return 0;}int rfcomm_dlc_close(struct rfcomm_dlc *d, int err){ int r; rfcomm_lock(); r = __rfcomm_dlc_close(d, err); rfcomm_unlock(); return r;}int rfcomm_dlc_send(struct rfcomm_dlc *d, struct sk_buff *skb){ int len = skb->len; if (d->state != BT_CONNECTED) return -ENOTCONN; BT_DBG("dlc %p mtu %d len %d", d, d->mtu, len); if (len > d->mtu) return -EINVAL; rfcomm_make_uih(skb, d->addr); skb_queue_tail(&d->tx_queue, skb); if (!test_bit(RFCOMM_TX_THROTTLED, &d->flags)) rfcomm_schedule(RFCOMM_SCHED_TX); return len;}void fastcall __rfcomm_dlc_throttle(struct rfcomm_dlc *d){ BT_DBG("dlc %p state %ld", d, d->state); if (!d->cfc) { d->v24_sig |= RFCOMM_V24_FC; set_bit(RFCOMM_MSC_PENDING, &d->flags); } rfcomm_schedule(RFCOMM_SCHED_TX);}void fastcall __rfcomm_dlc_unthrottle(struct rfcomm_dlc *d){ BT_DBG("dlc %p state %ld", d, d->state); if (!d->cfc) { d->v24_sig &= ~RFCOMM_V24_FC; set_bit(RFCOMM_MSC_PENDING, &d->flags); } rfcomm_schedule(RFCOMM_SCHED_TX);}/* Set/get modem status functions use _local_ status i.e. what we report to the other side. Remote status is provided by dlc->modem_status() callback. */int rfcomm_dlc_set_modem_status(struct rfcomm_dlc *d, u8 v24_sig){ BT_DBG("dlc %p state %ld v24_sig 0x%x", d, d->state, v24_sig); if (test_bit(RFCOMM_RX_THROTTLED, &d->flags)) v24_sig |= RFCOMM_V24_FC; else v24_sig &= ~RFCOMM_V24_FC; d->v24_sig = v24_sig; if (!test_and_set_bit(RFCOMM_MSC_PENDING, &d->flags)) rfcomm_schedule(RFCOMM_SCHED_TX); return 0;}int rfcomm_dlc_get_modem_status(struct rfcomm_dlc *d, u8 *v24_sig){ BT_DBG("dlc %p state %ld v24_sig 0x%x", d, d->state, d->v24_sig); *v24_sig = d->v24_sig; return 0;}/* ---- RFCOMM sessions ---- */static struct rfcomm_session *rfcomm_session_add(struct socket *sock, int state){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -