]> rtime.felk.cvut.cz Git - mirosot.git/blob - bluetooth/hci.h
An unsucesfull attempt to clean bluetooth library and make it working.
[mirosot.git] / bluetooth / hci.h
1 /*******************************************************************
2   bluetooth library
3
4   hci.h - main structures for keeping an information about a queue,
5           definition of global val, and any macros and inline fce
6
7   Copyright (C) 2006 by Petr Kovacik petr_kovacik@gmail.com
8
9  *******************************************************************/
10
11
12
13
14 #ifndef HCI_H_H
15 #define HCI_H_H
16
17 #include <bth_config.h>
18 #include <types.h>
19 #ifdef CONFIG_BLUETOOTH_LINUX
20 #include <stddef.h>
21 #endif
22
23 /* Byte order conversions */
24 #if __BYTE_ORDER == __LITTLE_ENDIAN
25 #define htobs(d)  (d)
26 #define htobl(d)  (d)
27 #define btohs(d)  (d)
28 #define btohl(d)  (d)
29 #elif __BYTE_ORDER == __BIG_ENDIAN
30 #define htobs(d)  bswap_16(d)
31 #define htobl(d)  bswap_32(d)
32 #define btohs(d)  bswap_16(d)
33 #define btohl(d)  bswap_32(d)
34 #else
35 #error "Unknown byte order"
36 #endif
37
38
39 /* BD Address */
40 typedef struct {
41         uint8_t b[6];
42 } bdaddr_t;
43
44
45 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
46 #define BDADDR_ALL   (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
47 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
48
49 /* Copy, swap, convert BD Address */
50 // static inline int bacmp(const bdaddr_t *ba1, const bdaddr_t *ba2)
51 // {
52 //      return memcmp(ba1, ba2, sizeof(bdaddr_t));
53 // }
54 // static inline void bacpy(bdaddr_t *dst, const bdaddr_t *src)
55 // {
56 //      memcpy(dst, src, sizeof(bdaddr_t));
57 // }
58
59 /*pro typ char (nikoliv u16)*/
60 #define store8(field, val) field=val;
61
62
63 // Returns an offset of a field in a structure
64 #define OFFSETOF(struct_type, field) \
65   ((int)&(((struct_type *)0)->field))
66
67 /********************************************************************************/
68 /*----------------------------- DEFINICE MAKER -----------------------------*/
69 #define cmd_opcode_pack(ogf, ocf)       (uint16_t)((ocf & 0x03ff)|(ogf << 10))
70 #define cmd_opcode_ogf(op)              (op >> 10)
71 #define cmd_opcode_ocf(op)              (op & 0x03ff)
72 /********************************************************************************/
73
74 #define BDADDR_ANY   (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
75 #define BDADDR_ALL   (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}})
76 #define BDADDR_LOCAL (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff, 0xff}})
77
78 /*pocet L2cap spojeni, ktere muze byt navazano s jednim BT zarizenim*/
79 #define L2CAP_NUM_OF_CANAL 2 
80
81
82 /* HCI Packet types */
83 #define HCI_PKT_SIZE            0x01
84
85 #define HCI_COMMAND_PKT         0x01
86 #define HCI_ACLDATA_PKT         0x02
87 #define HCI_SCODATA_PKT         0x03
88 #define HCI_EVENT_PKT             0x04
89 #define HCI_VENDOR_PKT          0xff
90 /*----------------------------- check command by event -----------------------------*/
91
92 typedef struct bth_ch_com {
93   uint16_t obcod;
94   int len;
95   void *position;
96   struct bth_ch_com *next;
97 } bths_check_command;
98
99 /*-----------------------------buffer info -----------------------------*/
100 typedef struct {
101   int len;
102   uint8_t aktual;
103   uint8_t sent;
104 } bths_command_buf_info;
105 /*------------------------- local device info -------------------------*/
106 typedef struct {
107         char     name[8];
108         bdaddr_t bdaddr;
109         uint32_t flags;
110         uint8_t  type;
111         uint8_t  features[8];
112         uint32_t pkt_type;
113         uint32_t link_policy;
114         uint32_t link_mode;
115         uint16_t acl_mtu;
116         uint16_t acl_pkts;
117         uint8_t  sco_mtu;
118         uint16_t sco_pkts;
119         uint8_t  busy;
120 }bths_dev_info;
121
122 /*------------------------- connect device info -------------------------*/
123 typedef struct con_bluet {      
124         bdaddr_t bdaddr;
125         uint8_t  grupe[8];
126         uint16_t handle;
127         uint8_t  link_type;
128         uint8_t  encr_mode;
129         uint16_t max_slots;
130         uint16_t ptype;
131         uint16_t ident;
132         uint16_t scid[L2CAP_NUM_OF_CANAL];
133         uint16_t dcid[L2CAP_NUM_OF_CANAL];
134 }bths_connect_bluet;
135
136
137
138 /* --------  HCI Packet structures  -------- */
139 /*--------------------------------*/
140 typedef struct {
141         uint16_t        opcode;         /* OCF & OGF */
142         uint8_t         plen;
143 } hci_command_hdr;
144    #define HCI_COMMAND_HDR____opcode            0
145    #define HCI_COMMAND_HDR____plen     2
146 /*--------------------------------*/
147 #define HCI_COMMAND_HDR_SIZE    3
148 /****************************************************************/
149 /****************************************************************/
150 /*--------------------------------*/
151 typedef struct{
152         uint8_t         evt;    //event kod
153         uint8_t         plen;   //delka dat
154 } hci_event_hdr;
155    #define HCI_EVENT_HDR____evt         0
156    #define HCI_EVENT_HDR____plen        1
157 /*--------------------------------*/
158 #define HCI_EVENT_HDR_SIZE      2
159 /****************************************************************/
160 /****************************************************************/
161
162 /*--------------------------------*/
163 typedef struct {
164         uint16_t        handle;         /* Handle & Flags(PB, BC) */
165         uint16_t        dlen;
166 }       hci_acl_hdr;
167    #define HCI_ACL_HDR____handle                0
168    #define HCI_ACL_HDR____dlen          2
169 /*--------------------------------*/
170 #define HCI_ACL_HDR_SIZE        4
171 /****************************************************************/
172 /****************************************************************/
173
174 /*--------------------------------*/
175 typedef struct {
176         uint16_t        handle;
177         uint8_t         dlen;
178 }       hci_sco_hdr;
179    #define HCI_SCO_HDR____handle                0
180    #define HCI_SCO_HDR____dlen          2
181 /*--------------------------------*/
182 #define HCI_SCO_HDR_SIZE        3
183 /****************************************************************/
184 /****************************************************************/
185
186 /*--------------------------------*/
187 typedef struct {
188         uint16_t        device;
189         uint16_t        type;
190         uint16_t        plen;
191 }       hci_msg_hdr;
192    #define HCI_MSG_HDR____device                0
193    #define HCI_MSG_HDR____type          2
194    #define HCI_MSG_HDR____plen            4
195 /*--------------------------------*/
196 /****************************************************************/
197 /****************************************************************/
198
199
200 /*----------------local device info (master) -----------------------*/
201 extern bths_dev_info  bth_local_info;
202
203 /*----------------conect device info (slave) -----------------------*/
204 extern bths_connect_bluet *bth_connected[];
205 //extern void *bth_for_connecting[];
206
207 /*---------------- command and acknowledge queues -----------------------*/
208 #define LENCOMMAND 10
209 extern void  *bth_array_adrr_comm_packet[LENCOMMAND];
210 extern bths_command_buf_info bth_com_buf_info;
211
212 extern void  *bth_array_adrr_check_packet[LENCOMMAND];
213 extern bths_command_buf_info bth_check_buf_info;
214
215 extern uint8_t bth_controll_flag;
216
217 /*--------BD address whitch are accept for connection-------------*/
218 extern bdaddr_t bth_accept_bd_addr[]; //for future use
219         
220
221 inline static int bth_find_conected_dev(uint16_t handle)
222 {
223   int j;
224   
225   for(j=0; j<8;j++)  //8 moznych zarizeni, se kterymi lze komunikovat
226   {
227     if(bth_connected[j]!=NULL)
228     {
229       if(bth_connected[j]->handle==handle)
230       {return (j);};
231     };
232   }
233   return(-1);
234 };
235
236 inline static int bth_find_chanal(uint16_t num_dev, uint16_t chanal)
237 {
238   int j;
239   for(j=0; j<L2CAP_NUM_OF_CANAL;j++)  //8 moznych zarizeni, se kterymi lze komunikovat
240   {
241     if(bth_connected[num_dev]->scid[j]==chanal)
242     {
243       return(j);
244     };
245   }
246   return(-1);
247 };
248
249 inline static int bth_add_chanal_dcid(uint16_t num_dev, uint16_t chanal)
250 {
251   int j;
252   for(j=0; j<L2CAP_NUM_OF_CANAL;j++)  //8 moznych zarizeni, se kterymi lze komunikovat
253   {
254     if(bth_connected[num_dev]->dcid[j]==0)
255     {
256       (bth_connected[num_dev])->dcid[j]=chanal;
257       return(j);
258     };
259   }
260   return(-1);
261 };
262
263 inline static int bth_find_chanal_dcid(uint16_t num_dev, uint16_t chanal)
264 {
265   int j;
266   for(j=0; j<L2CAP_NUM_OF_CANAL;j++)  //8 moznych zarizeni, se kterymi lze komunikovat
267   {
268     if(bth_connected[num_dev]->dcid[j]==chanal)
269     {
270       return(j);
271     };
272   }
273   return(-1);
274 };
275
276 inline static int bth_find_chanal_scid(uint16_t num_dev, uint16_t chanal)
277 {
278   int j;
279   for(j=0; j<L2CAP_NUM_OF_CANAL;j++)  //8 moznych zarizeni, se kterymi lze komunikovat
280   {
281     if(bth_connected[num_dev]->scid[j]==chanal)
282     {
283       return(j);
284     };
285   }
286   return(-1);
287 };
288
289 /*-----------funkce ---------------*/
290 int bth_send_packet(uint8_t *hp, uint16_t hp_size);
291 int VypisHexa(uint8_t *s, int delka);
292 void bth_conv_char_text(uint8_t zn);
293
294 #endif