xref: /libbtbb/lib/src/bluetooth_packet.h (revision c4e05ee86950a475ea1edbc37c485b6879b60f4f)
1 /* -*- c -*- */
2 /*
3  * Copyright 2007 - 2013 Dominic Spill, Michael Ossmann, Will Code
4  *
5  * This file is part of libbtbb
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with libbtbb; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 #ifndef INCLUDED_BLUETOOTH_PACKET_H
23 #define INCLUDED_BLUETOOTH_PACKET_H
24 #include "btbb.h"
25 
26 /* maximum number of symbols */
27 #define MAX_SYMBOLS 3125
28 
29 /* maximum number of payload bits */
30 #define MAX_PAYLOAD_LENGTH 2744
31 
32 /* minimum header bit errors to indicate that this is an ID packet */
33 #define ID_THRESHOLD 5
34 
35 #define PACKET_TYPE_NULL 0
36 #define PACKET_TYPE_POLL 1
37 #define PACKET_TYPE_FHS 2
38 #define PACKET_TYPE_DM1 3
39 #define PACKET_TYPE_DH1 4
40 #define PACKET_TYPE_HV1 5
41 #define PACKET_TYPE_HV2 6
42 #define PACKET_TYPE_HV3 7
43 #define PACKET_TYPE_DV 8
44 #define PACKET_TYPE_AUX1 9
45 #define PACKET_TYPE_DM3 10
46 #define PACKET_TYPE_DH3 11
47 #define PACKET_TYPE_EV4 12
48 #define PACKET_TYPE_EV5 13
49 #define PACKET_TYPE_DM5 14
50 #define PACKET_TYPE_DH5 15
51 
52 struct btbb_packet {
53 
54 	uint32_t refcount;
55 
56 	uint32_t flags;
57 
58 	uint8_t channel; /* Bluetooth channel (0-79) */
59 	uint8_t UAP;     /* upper address part */
60 	uint16_t NAP;    /* non-significant address part */
61 	uint32_t LAP;    /* lower address part found in access code */
62 
63 	uint8_t modulation;
64 	uint8_t transport;
65 	uint8_t packet_type;
66 	uint8_t packet_lt_addr; /* LLID field of payload header (2 bits) */
67 	uint8_t packet_flags; /* Flags - FLOW/ARQN/SQEN */
68 	uint8_t packet_hec; /* Flags - FLOW/ARQN/SQEN */
69 
70 	/* packet header, one bit per char */
71 	char packet_header[18];
72 
73 	/* number of payload header bytes: 0, 1, 2, or -1 for
74 	 * unknown. payload is one bit per char. */
75 	int payload_header_length;
76 	char payload_header[16];
77 
78 	/* LLID field of payload header (2 bits) */
79 	uint8_t payload_llid;
80 
81 	/* flow field of payload header (1 bit) */
82 	uint8_t payload_flow;
83 
84 	/* payload length: the total length of the asynchronous data
85 	* in bytes.  This does not include the length of synchronous
86 	* data, such as the voice field of a DV packet.  If there is a
87 	* payload header, this payload length is payload body length
88 	* (the length indicated in the payload header's length field)
89 	* plus payload_header_length plus 2 bytes CRC (if present).
90 	*/
91 	int payload_length;
92 
93 	/* The actual payload data in host format
94 	* Ready for passing to wireshark
95 	* 2744 is the maximum length, but most packets are shorter.
96 	* Dynamic allocation would probably be better in the long run but is
97 	* problematic in the short run.
98 	*/
99 	char payload[MAX_PAYLOAD_LENGTH];
100 
101 	uint16_t crc;
102 	uint32_t clkn;     /* CLK1-27 of the packet */
103 	uint8_t ac_errors; /* Number of bit errors in the AC */
104 
105 	/* the raw symbol stream (less the preamble), one bit per char */
106 	//FIXME maybe this should be a vector so we can grow it only
107 	//to the size needed and later shrink it if we find we have
108 	//more symbols than necessary
109 	uint16_t length; /* number of symbols */
110 	char symbols[MAX_SYMBOLS];
111 
112 };
113 
114 /* type-specific CRC checks and decoding */
115 int fhs(int clock, btbb_packet* p);
116 int DM(int clock, btbb_packet* p);
117 int DH(int clock, btbb_packet* p);
118 int EV3(int clock, btbb_packet* p);
119 int EV4(int clock, btbb_packet* p);
120 int EV5(int clock, btbb_packet* p);
121 int HV(int clock, btbb_packet* p);
122 
123 /* check if the packet's CRC is correct for a given clock (CLK1-6) */
124 int crc_check(int clock, btbb_packet* p);
125 
126 /* format payload for tun interface */
127 char *tun_format(btbb_packet* p);
128 
129 /* try a clock value (CLK1-6) to unwhiten packet header,
130  * sets resultant d_packet_type and d_UAP, returns UAP.
131  */
132 uint8_t try_clock(int clock, btbb_packet* p);
133 
134 /* extract LAP from FHS payload */
135 uint32_t lap_from_fhs(btbb_packet* p);
136 
137 /* extract UAP from FHS payload */
138 uint8_t uap_from_fhs(btbb_packet* p);
139 
140 /* extract NAP from FHS payload */
141 uint16_t nap_from_fhs(btbb_packet* p);
142 
143 /* extract clock from FHS payload */
144 uint32_t clock_from_fhs(btbb_packet* p);
145 
146 #endif /* INCLUDED_BLUETOOTH_PACKET_H */
147