1/*
2 *
3 *  BlueZ - Bluetooth protocol stack for Linux
4 *
5 *  Copyright (C) 2000-2005  CSR Ltd.
6 *
7 *
8 *  Permission is hereby granted, free of charge, to any person obtaining
9 *  a copy of this software and associated documentation files (the
10 *  "Software"), to deal in the Software without restriction, including
11 *  without limitation the rights to use, copy, modify, merge, publish,
12 *  distribute, sublicense, and/or sell copies of the Software, and to
13 *  permit persons to whom the Software is furnished to do so, subject to
14 *  the following conditions:
15 *
16 *  The above copyright notice and this permission notice shall be included
17 *  in all copies or substantial portions of the Software.
18 *
19 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 *  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 *  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28
29#ifndef UBCSP_INCLUDE_H
30#define UBCSP_INCLUDE_H
31
32/*****************************************************************************/
33/*****************************************************************************/
34/*****************************************************************************/
35/**                                                                         **/
36/** ubcsp.h                                                                 **/
37/**                                                                         **/
38/** MicroBCSP - a very low cost implementation of the BCSP protocol         **/
39/**                                                                         **/
40/*****************************************************************************/
41
42/* If we wish to use CRC's, then change 0 to 1 in the next line */
43#define UBCSP_CRC 1
44
45/* Define some basic types - change these for your architecture */
46typedef unsigned char uint8;
47typedef unsigned short uint16;
48typedef unsigned int uint32;
49typedef signed char int8;
50typedef signed short int16;
51typedef signed int int32;
52
53/* The defines below require a printf function to be available */
54
55/* Do we want to show packet errors in debug output */
56#define SHOW_PACKET_ERRORS	0
57
58/* Do we want to show Link Establishment State transitions in debug output */
59#define SHOW_LE_STATES		0
60
61/*****************************************************************************/
62/**                                                                         **/
63/** ubcsp_packet                                                            **/
64/**                                                                         **/
65/** This is description of a bcsp packet for the upper layer                **/
66/**                                                                         **/
67/*****************************************************************************/
68
69struct ubcsp_packet
70{
71	uint8 channel;		/* Which Channel this packet is to/from */
72	uint8 reliable;		/* Is this packet reliable */
73	uint8 use_crc;		/* Does this packet use CRC data protection */
74	uint16 length;		/* What is the length of the payload data */
75	uint8 *payload;		/* The payload data itself - size of length */
76};
77
78/*****************************************************************************/
79/**                                                                         **/
80/** ubcsp_configuration                                                     **/
81/**                                                                         **/
82/** This is the main configuration of the ubcsp engine                      **/
83/** All state variables are stored in this structure                        **/
84/**                                                                         **/
85/*****************************************************************************/
86
87enum ubcsp_link_establishment_state
88{
89	ubcsp_le_uninitialized,
90	ubcsp_le_initialized,
91	ubcsp_le_active
92};
93
94enum ubcsp_link_establishment_packet
95{
96	ubcsp_le_sync,
97	ubcsp_le_sync_resp,
98	ubcsp_le_conf,
99	ubcsp_le_conf_resp,
100	ubcsp_le_none
101};
102
103struct ubcsp_configuration
104{
105	uint8 link_establishment_state;
106	uint8 link_establishment_resp;
107	uint8 link_establishment_packet;
108
109	uint8 sequence_number:3;
110	uint8 ack_number:3;
111	uint8 send_ack;
112	struct ubcsp_packet *send_packet;
113	struct ubcsp_packet *receive_packet;
114
115	uint8 receive_header_checksum;
116	uint8 receive_slip_escape;
117	int32 receive_index;
118
119	uint8 send_header_checksum;
120#ifdef UBCSP_CRC
121	uint8 need_send_crc;
122	uint16 send_crc;
123#endif
124	uint8 send_slip_escape;
125
126	uint8 *send_ptr;
127	int32 send_size;
128	uint8 *next_send_ptr;
129	int32 next_send_size;
130
131	int8 delay;
132};
133
134/*****************************************************************************/
135/**                                                                         **/
136/** ubcsp_poll sets activity from an OR of these flags                      **/
137/**                                                                         **/
138/*****************************************************************************/
139
140#define UBCSP_PACKET_SENT 0x01
141#define UBCSP_PACKET_RECEIVED 0x02
142#define UBCSP_PEER_RESET 0x04
143#define UBCSP_PACKET_ACK 0x08
144
145/*****************************************************************************/
146/**                                                                         **/
147/** This is the functional interface for ucbsp                              **/
148/**                                                                         **/
149/*****************************************************************************/
150
151void ubcsp_initialize (void);
152void ubcsp_send_packet (struct ubcsp_packet *send_packet);
153void ubcsp_receive_packet (struct ubcsp_packet *receive_packet);
154uint8 ubcsp_poll (uint8 *activity);
155
156/*****************************************************************************/
157/**                                                                         **/
158/** Slip Escape Values                                                      **/
159/**                                                                         **/
160/*****************************************************************************/
161
162#define SLIP_FRAME 0xC0
163#define SLIP_ESCAPE 0xDB
164#define SLIP_ESCAPE_FRAME 0xDC
165#define SLIP_ESCAPE_ESCAPE 0xDD
166
167/*****************************************************************************/
168/*****************************************************************************/
169/*****************************************************************************/
170
171/*****************************************************************************/
172/**                                                                         **/
173/** These functions need to be linked into your system                      **/
174/**                                                                         **/
175/*****************************************************************************/
176
177/*****************************************************************************/
178/**                                                                         **/
179/** put_uart outputs a single octet over the UART Tx line                   **/
180/**                                                                         **/
181/*****************************************************************************/
182
183extern void put_uart (uint8);
184
185/*****************************************************************************/
186/**                                                                         **/
187/** get_uart receives a single octet over the UART Rx line                  **/
188/** if no octet is available, then this returns 0                           **/
189/** if an octet was read, then this is returned in the argument and         **/
190/**   the function returns 1                                                **/
191/**                                                                         **/
192/*****************************************************************************/
193
194extern uint8 get_uart (uint8 *);
195
196/*****************************************************************************/
197/**                                                                         **/
198/** These defines should be changed to your systems concept of 100ms        **/
199/**                                                                         **/
200/*****************************************************************************/
201
202#define UBCSP_POLL_TIME_IMMEDIATE   0
203#define UBCSP_POLL_TIME_DELAY       25
204
205/*****************************************************************************/
206/*****************************************************************************/
207/*****************************************************************************/
208#endif
209