?? bcsp_slip.c
字號:
/* * bcsp_slip -- Implementation of the SLIP layer in the BCSP protocol stack * * Copyright (C) 2001 Axis Communications AB * * Author: Peter Kjellerstedt <peter.kjellerstedt@axis.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Exceptionally, Axis Communications AB grants discretionary and * conditional permissions for additional use of the text contained * in the company's release of the AXIS OpenBT Stack under the * provisions set forth hereunder. * * Provided that, if you use the AXIS OpenBT Stack with other files, * that do not implement functionality as specified in the Bluetooth * System specification, to produce an executable, this does not by * itself cause the resulting executable to be covered by the GNU * General Public License. Your use of that executable is in no way * restricted on account of using the AXIS OpenBT Stack code with it. * * This exception does not however invalidate any other reasons why * the executable file might be covered by the provisions of the GNU * General Public License. * * $Id: bcsp_slip.c,v 1.8 2001/09/18 13:04:27 pkj Exp $ * *//****************** INCLUDE FILES SECTION ***********************************/#define __NO_VERSION__ /* don't define kernel_version in module.h */#ifdef __KERNEL__#include <linux/malloc.h>#include <linux/bluetooth/btcommon.h>#include <linux/bluetooth/btmem.h>#include <linux/bluetooth/bcsp.h>#include <linux/bluetooth/bcsp_debug.h>#else#include <stdlib.h>#include <errno.h>#include "include/btcommon.h"#include "include/btmem.h"#include "include/bcsp.h"#include "include/bcsp_debug.h"#endif/****************** CONSTANT AND MACRO SECTION ******************************/#if SLIP_DEBUG#define D(fmt...) printk("SLIP: " fmt)#define PRINTPKT(data, len) print_data(NULL, data, len)#else#define D(fmt...)#define PRINTPKT(data, len)#endif/****************** TYPE DEFINITION SECTION *********************************//****************** LOCAL FUNCTION DECLARATION SECTION **********************/static void slip_send_add_byte(struct bcsp* bcsp, u8 data);#if BCSP_PARSELOWERstatic void slip_debug(const u8 *str, const struct bcsp *bcsp);#endif/****************** GLOBAL VARIABLE DECLARATION SECTION *********************//****************** LOCAL VARIABLE DECLARATION SECTION **********************//****************** FUNCTION DEFINITION SECTION *****************************/s32bcsp_slip_send(struct bcsp* bcsp){ s32 i; D(__FUNCTION__ ":\n"); PRINTPKT(bcsp->payload, bcsp->payload_length); if (!(bcsp->packet = kmalloc(2 * (4 + bcsp->payload_length) + 2, GFP_ATOMIC))) { return -ENOMEM; } bcsp->packet_length = 0; bcsp->packet[bcsp->packet_length++] = 0xC0; /* Send the BCSP header */ slip_send_add_byte(bcsp, bcsp->flags); slip_send_add_byte(bcsp, bcsp->identifier | ((bcsp->payload_length & 0x0F) << 4)); slip_send_add_byte(bcsp, bcsp->payload_length >> 4); slip_send_add_byte(bcsp, bcsp->checksum); /* Send the payload */ for (i = 0; i < bcsp->payload_length; i++) { slip_send_add_byte(bcsp, bcsp->payload[i]); } /* Send CRC if wanted */ if (BCSP_GET_CRC_PRESENT(bcsp)) { slip_send_add_byte(bcsp, bcsp->crc >> 8); slip_send_add_byte(bcsp, bcsp->crc & 0xFF); } bcsp->packet[bcsp->packet_length++] = 0xC0;#if BCSP_PARSELOWER slip_debug("<== ", bcsp);#endif bcsp_write_lower(bcsp->packet, bcsp->packet_length); kfree(bcsp->packet); return bcsp->packet_length;}s32bcsp_slip_receive(const u8* packet, u32 len){ static u8 buffer[4 + 4096 + 2 + 1]; static s32 length = 0; static s32 skipping = TRUE; struct bcsp bcsp; s32 i = 0; D(__FUNCTION__ "\n"); bcsp_init_packet(&bcsp); if (skipping) { while (i < len) { if (packet[i++] == 0xC0) { length = 0; skipping = FALSE; break; } } bcsp.packet = NULL; } for (; i < len && !skipping; i++) { switch (packet[i]) { case 0xC0: skipping = TRUE; if (!length) { i--; } else if ((bcsp.packet = kmalloc(length, GFP_ATOMIC))) { memcpy(bcsp.packet, buffer, length); bcsp.packet_length = length; } break; case 0xDB: switch (packet[++i]) { case 0xDC: buffer[length++] = 0xC0; break; case 0xDD: buffer[length++] = 0xDB; break; default: skipping = TRUE; break; } break; default: buffer[length++] = packet[i]; break; } if (length == sizeof(buffer)) { skipping = TRUE; } } if (bcsp.packet) {#if BCSP_PARSELOWER slip_debug("==> ", &bcsp);#endif bcsp_integrity_receive(&bcsp); kfree(bcsp.packet); } return i;}voidslip_send_add_byte(struct bcsp* bcsp, u8 data){ switch (data) { case 0xC0: bcsp->packet[bcsp->packet_length++] = 0xDB; bcsp->packet[bcsp->packet_length++] = 0xDC; break; case 0xDB: bcsp->packet[bcsp->packet_length++] = 0xDB; bcsp->packet[bcsp->packet_length++] = 0xDD; break; default: bcsp->packet[bcsp->packet_length++] = data; break; }}#if BCSP_PARSELOWER/* * Used to parse headers of sent/incoming BCSP packets * Also prints other potentially interesting stats */#include <linux/bluetooth/hci_internal.h>voidslip_debug(const u8 *str, const struct bcsp *bcsp){ extern hci_controller hci_ctrl; extern u8 winspace; extern u8 txseq; extern u8 txack; extern u8 rxack; extern u8 expected_rxseq; printk("%sseq: %ld, ack: %ld, winsize: %d, acl: %d, bufc: %d, length: %d\n", str, BCSP_GET_SEQ(bcsp), BCSP_GET_ACK(bcsp), winspace, hci_ctrl.hc_buf.acl_num, buf_byte_count(-1), bcsp->packet_length); printk("Status: txseq: %d, txack: %d, cur_rxack: %d, exp_rxseq: %d\n", txseq, txack, rxack, expected_rxseq);}#endif/****************** END OF FILE slip.c **************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -