⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 capi.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    CMTP implementation for Linux Bluetooth stack (BlueZ).   Copyright (C) 2002-2003 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.*/#include <linux/config.h>#include <linux/module.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/slab.h>#include <linux/poll.h>#include <linux/fcntl.h>#include <linux/skbuff.h>#include <linux/socket.h>#include <linux/ioctl.h>#include <linux/file.h>#include <linux/wait.h>#include <net/sock.h>#include <linux/isdn/capilli.h>#include <linux/isdn/capicmd.h>#include <linux/isdn/capiutil.h>#include "cmtp.h"#ifndef CONFIG_BT_CMTP_DEBUG#undef  BT_DBG#define BT_DBG(D...)#endif#define CAPI_INTEROPERABILITY		0x20#define CAPI_INTEROPERABILITY_REQ	CAPICMD(CAPI_INTEROPERABILITY, CAPI_REQ)#define CAPI_INTEROPERABILITY_CONF	CAPICMD(CAPI_INTEROPERABILITY, CAPI_CONF)#define CAPI_INTEROPERABILITY_IND	CAPICMD(CAPI_INTEROPERABILITY, CAPI_IND)#define CAPI_INTEROPERABILITY_RESP	CAPICMD(CAPI_INTEROPERABILITY, CAPI_RESP)#define CAPI_INTEROPERABILITY_REQ_LEN	(CAPI_MSG_BASELEN + 2)#define CAPI_INTEROPERABILITY_CONF_LEN	(CAPI_MSG_BASELEN + 4)#define CAPI_INTEROPERABILITY_IND_LEN	(CAPI_MSG_BASELEN + 2)#define CAPI_INTEROPERABILITY_RESP_LEN	(CAPI_MSG_BASELEN + 2)#define CAPI_FUNCTION_REGISTER		0#define CAPI_FUNCTION_RELEASE		1#define CAPI_FUNCTION_GET_PROFILE	2#define CAPI_FUNCTION_GET_MANUFACTURER	3#define CAPI_FUNCTION_GET_VERSION	4#define CAPI_FUNCTION_GET_SERIAL_NUMBER	5#define CAPI_FUNCTION_MANUFACTURER	6#define CAPI_FUNCTION_LOOPBACK		7#define CMTP_MSGNUM	1#define CMTP_APPLID	2#define CMTP_MAPPING	3static struct cmtp_application *cmtp_application_add(struct cmtp_session *session, __u16 appl){	struct cmtp_application *app = kmalloc(sizeof(*app), GFP_KERNEL);	BT_DBG("session %p application %p appl %d", session, app, appl);	if (!app)		return NULL;	memset(app, 0, sizeof(*app));	app->state = BT_OPEN;	app->appl = appl;	list_add_tail(&app->list, &session->applications);	return app;}static void cmtp_application_del(struct cmtp_session *session, struct cmtp_application *app){	BT_DBG("session %p application %p", session, app);	if (app) {		list_del(&app->list);		kfree(app);	}}static struct cmtp_application *cmtp_application_get(struct cmtp_session *session, int pattern, __u16 value){	struct cmtp_application *app;	struct list_head *p, *n;	list_for_each_safe(p, n, &session->applications) {		app = list_entry(p, struct cmtp_application, list);		switch (pattern) {		case CMTP_MSGNUM:			if (app->msgnum == value)				return app;			break;		case CMTP_APPLID:			if (app->appl == value)				return app;			break;		case CMTP_MAPPING:			if (app->mapping == value)				return app;			break;		}	}	return NULL;}static int cmtp_msgnum_get(struct cmtp_session *session){	session->msgnum++;	if ((session->msgnum & 0xff) > 200)		session->msgnum = CMTP_INITIAL_MSGNUM + 1;	return session->msgnum;}static void cmtp_send_capimsg(struct cmtp_session *session, struct sk_buff *skb){	struct cmtp_scb *scb = (void *) skb->cb;	BT_DBG("session %p skb %p len %d", session, skb, skb->len);	scb->id = -1;	scb->data = (CAPIMSG_COMMAND(skb->data) == CAPI_DATA_B3);	skb_queue_tail(&session->transmit, skb);	cmtp_schedule(session);}static void cmtp_send_interopmsg(struct cmtp_session *session,					__u8 subcmd, __u16 appl, __u16 msgnum,					__u16 function, unsigned char *buf, int len){	struct sk_buff *skb;	unsigned char *s;	BT_DBG("session %p subcmd 0x%02x appl %d msgnum %d", session, subcmd, appl, msgnum);	if (!(skb = alloc_skb(CAPI_MSG_BASELEN + 6 + len, GFP_ATOMIC))) {		BT_ERR("Can't allocate memory for interoperability packet");		return;	}	s = skb_put(skb, CAPI_MSG_BASELEN + 6 + len);	capimsg_setu16(s, 0, CAPI_MSG_BASELEN + 6 + len);	capimsg_setu16(s, 2, appl);	capimsg_setu8 (s, 4, CAPI_INTEROPERABILITY);	capimsg_setu8 (s, 5, subcmd);	capimsg_setu16(s, 6, msgnum);	/* Interoperability selector (Bluetooth Device Management) */	capimsg_setu16(s, 8, 0x0001);	capimsg_setu8 (s, 10, 3 + len);	capimsg_setu16(s, 11, function);	capimsg_setu8 (s, 13, len);	if (len > 0)		memcpy(s + 14, buf, len);	cmtp_send_capimsg(session, skb);}static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *skb){	struct capi_ctr *ctrl = &session->ctrl;	struct cmtp_application *application;	__u16 appl, msgnum, func, info;	__u32 controller;	BT_DBG("session %p skb %p len %d", session, skb, skb->len);	switch (CAPIMSG_SUBCOMMAND(skb->data)) {	case CAPI_CONF:		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 5);		info = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 8);		switch (func) {		case CAPI_FUNCTION_REGISTER:			msgnum = CAPIMSG_MSGID(skb->data);			application = cmtp_application_get(session, CMTP_MSGNUM, msgnum);			if (application) {				application->state = BT_CONNECTED;				application->msgnum = 0;				application->mapping = CAPIMSG_APPID(skb->data);				wake_up_interruptible(&session->wait);			}			break;		case CAPI_FUNCTION_RELEASE:			appl = CAPIMSG_APPID(skb->data);			application = cmtp_application_get(session, CMTP_MAPPING, appl);			if (application) {				application->state = BT_CLOSED;				application->msgnum = 0;				wake_up_interruptible(&session->wait);			}			break;		case CAPI_FUNCTION_GET_PROFILE:			controller = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 11);			msgnum = CAPIMSG_MSGID(skb->data);			if (!info && (msgnum == CMTP_INITIAL_MSGNUM)) {				session->ncontroller = controller;				wake_up_interruptible(&session->wait);				break;			}			if (!info && ctrl) {				memcpy(&ctrl->profile,					skb->data + CAPI_MSG_BASELEN + 11,					sizeof(capi_profile));				session->state = BT_CONNECTED;				capi_ctr_ready(ctrl);			}			break;		case CAPI_FUNCTION_GET_MANUFACTURER:			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 10);			if (!info && ctrl) {				strncpy(ctrl->manu,					skb->data + CAPI_MSG_BASELEN + 15,					skb->data[CAPI_MSG_BASELEN + 14]);			}			break;		case CAPI_FUNCTION_GET_VERSION:			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);			if (!info && ctrl) {				ctrl->version.majorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 16);				ctrl->version.minorversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 20);				ctrl->version.majormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 24);				ctrl->version.minormanuversion = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 28);			}			break;		case CAPI_FUNCTION_GET_SERIAL_NUMBER:			controller = CAPIMSG_U32(skb->data, CAPI_MSG_BASELEN + 12);			if (!info && ctrl) {				memset(ctrl->serial, 0, CAPI_SERIAL_LEN);				strncpy(ctrl->serial,					skb->data + CAPI_MSG_BASELEN + 17,					skb->data[CAPI_MSG_BASELEN + 16]);			}			break;		}		break;	case CAPI_IND:		func = CAPIMSG_U16(skb->data, CAPI_MSG_BASELEN + 3);		if (func == CAPI_FUNCTION_LOOPBACK) {			appl = CAPIMSG_APPID(skb->data);			msgnum = CAPIMSG_MSGID(skb->data);			cmtp_send_interopmsg(session, CAPI_RESP, appl, msgnum, func,						skb->data + CAPI_MSG_BASELEN + 6,						skb->data[CAPI_MSG_BASELEN + 5]);		}		break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -