-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbt_h.cpp
418 lines (385 loc) · 16 KB
/
bt_h.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*****************************************************************************
*
* Copyright (C) 2010 Circuits At Home, LTD. All rights reserved.
*
* This software may be distributed and modified under the terms of the GNU
* General Public License version 2 (GPL) as published by the Free Software
* Foundation and appearing in the file GPL.TXT included in the packaging of
* this file. Please note that GPL Section 2[b] requires that all works based
* on this software must also be made publicly available under the terms of
* the GPL ("Copyleft").
*
* Contact information:
* Circuits At Home Web site: http://www.circuitsathome.com
* e-mail: [email protected]
*****************************************************************************/
#include "bt_h.h"
#include "serial.h"
#include <avr/pgmspace.h>
const int8_t hci_reset_cmd[] PROGMEM = {0x03, 0x0c, 0x00};
const int8_t hci_read_bd_addr_cmd[] PROGMEM = {0x09, 0x10, 0x00};
const int8_t hci_set_event_filter_cmd[] PROGMEM = {0x05, 0x0c, 0x03, 0x02, 0x00, 0x03};
const int8_t hci_write_cod_cmd[] PROGMEM = {0x24, 0x0c, 0x03, 0x04, 0x02, 0x58};//0x08, 0x04, 0x24};
const int8_t hci_set_local_name_cmd[] PROGMEM = {0x13, 0x0c};
const int8_t hci_set_page_timeout_cmd[] PROGMEM = {0x18, 0x0c, 0x02, 0x00, 0x40};
const int8_t hci_inquiry_cmd[] PROGMEM = {0x01, 0x04, 0x05, 0x33, 0x8b, 0x9e, 0x04, 0x01};
const int8_t hci_connect_cmd[] PROGMEM = {0x05, 0x04, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0x01, 0x00, 0x8d, 0x2a, 0x01};
const int8_t hci_scan_enable_cmd[] PROGMEM = {0x1a, 0x0c, 0x01, 0x03};
const int8_t hci_pin_reply_cmd[] PROGMEM = {0x0d, 0x04, 0x17};
const int8_t hci_reset_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
const int8_t hci_read_bd_addr_resp[] PROGMEM = {0x0e, 0x0a, 0x01, 0x09, 0x10, 0x00};
const int8_t hci_set_event_filter_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x05, 0x0c, 0x00};
const int8_t hci_write_cod_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x24, 0x0c, 0x00};
const int8_t hci_set_local_name_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x13, 0x0c, 0x00};
const int8_t hci_set_page_timeout_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x18, 0x0c, 0x00};
const int8_t hci_inquiry_resp[] PROGMEM = {0x01, 0x01, 0x00};
const int8_t hci_inquiry_result_resp[] PROGMEM = {0x02, 0x0f, 0x01};
const int8_t hci_connection_result_resp[] PROGMEM = {0x03, 0x0b, 0x00};
const int8_t hci_scan_enable_resp[] PROGMEM = {0x0e, 0x04, 0x01, 0x1a, 0x0c, 0x00};
const int8_t hci_pin_request_event[] PROGMEM = {0x16, 0x06};
const int8_t hci_role_change_event[] PROGMEM = {0x12, 0x08, 0x00};
/////////////////////////////////////////////////////////////////////////////////////
TBT::TBT(){
uint8_t i;
const int8_t lname[]="Arduino", lpin[]="1234";
UsbState = USB_STATE_DETACHED;
cState = BT_STATE_INIT;
ep_addr[0] = 0x01;
ep_addr[1] = 0x02;
ep_addr[2] = 0x02;
my_name_len = 7;
my_pin_len = 4;
for(i = 0; i < my_name_len; i++)
my_name[i] = lname[i];
for(i = 0; i < my_pin_len; i++)
my_pin[i] = lpin[i];
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::InitServer(void){
cBehaviour = BT_SERVER;
Init();
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::InitClient(void){
cBehaviour = BT_CLIENT;
Init();
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::InitClientAddr(Tbd_addr Aaddr){
user_addr = Aaddr;
cBehaviour = BT_CLIENT_ADDR;
Init();
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::Init(void){
hci_state = HCI_STATE_INIT;
buf_count = 0;
cState_p = cState;
cState = BT_STATE_MAX;
Max.powerOn();
delay(200);
};
/////////////////////////////////////////////////////////////////////////////////////
uint8_t TBT::Task(int8_t * Abuf){
uint8_t res, i;
res = 0;
UsbState = Usb.getUsbTaskState();
Max.Task();
Usb.Task();
delay(1);
if(UsbState == USB_STATE_CONFIGURING){
Spp_Init();
Usb.setUsbTaskState(USB_STATE_RUNNING);
}
if(UsbState == USB_STATE_RUNNING){
if(cState == BT_STATE_USB) {
cState_p = cState;
cState = BT_STATE_HCI;
}
res = ACL_Poll();
if(res != 0){
if(cState != BT_STATE_CONNECTED) res = 0;
else {
for(i = 4; i < res; i++)
Abuf[i-4] = buf[i];
res -= 4;
}
}
HCI_Poll();
HCI_Task();
}
return res;
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::Spp_Init(void){
uint8_t rcode = 0; //return code
cState_p = cState;
cState = BT_STATE_USB;
// Initialize data structures for endpoints of device
ep_record[ CONTROL_PIPE ] = *( Usb.getDevTableEntry( 0,0 )); //copy endpoint 0 parameters
ep_record[ EVENT_PIPE ].epAddr = ep_addr[0]; // Bluetooth event endpoint
ep_record[ EVENT_PIPE ].Attr = EP_INTERRUPT;
ep_record[ EVENT_PIPE ].MaxPktSize = INT_MAXPKTSIZE;
ep_record[ EVENT_PIPE ].Interval = EP_POLL;
ep_record[ EVENT_PIPE ].sndToggle = bmSNDTOG0;
ep_record[ EVENT_PIPE ].rcvToggle = bmRCVTOG0;
ep_record[ DATAIN_PIPE ].epAddr = ep_addr[1]; // Bluetooth data endpoint
ep_record[ DATAIN_PIPE ].Attr = EP_BULK;
ep_record[ DATAIN_PIPE ].MaxPktSize = BULK_MAXPKTSIZE;
ep_record[ DATAIN_PIPE ].Interval = 0;
ep_record[ DATAIN_PIPE ].sndToggle = bmSNDTOG0;
ep_record[ DATAIN_PIPE ].rcvToggle = bmRCVTOG0;
ep_record[ DATAOUT_PIPE ].epAddr = ep_addr[2]; // Bluetooth data endpoint
ep_record[ DATAOUT_PIPE ].Attr = EP_BULK;
ep_record[ DATAOUT_PIPE ].MaxPktSize = BULK_MAXPKTSIZE;
ep_record[ DATAOUT_PIPE ].Interval = 0;
ep_record[ DATAOUT_PIPE ].sndToggle = bmSNDTOG0;
ep_record[ DATAOUT_PIPE ].rcvToggle = bmRCVTOG0;
Usb.setDevTableEntry(BT_ADDR, ep_record);
//Configure the device
rcode = Usb.setConf(BT_ADDR, ep_record[ CONTROL_PIPE ].epAddr, BT_CONFIGURATION);
if( rcode ) {
//dbgn("Err Set Conf");
while(1); //stop
}
delay(200);
//dbgn("usb init\n");
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::HCI_Poll(void){
uint8_t rcode, i;
rcode = Usb.inTransfer(BT_ADDR, ep_record[EVENT_PIPE].epAddr, MAX_BUFFER_SIZE, (char*)buf, USB_NAK_NOWAIT);
if(!rcode){
buf_count = buf[1] + 2;
/*Serial.print("HCI_evt:");
Serial.print(buf_count, HEX);
Serial.print(":");
for(i = 0; i < buf_count; i++){
Serial.print((buf)[i], HEX);
Serial.print(" ");
}
Serial.print("\r\n");*/
}
};
/////////////////////////////////////////////////////////////////////////////////////
uint8_t TBT::ACL_Poll(void){
uint8_t rcode, i;
rcode = Usb.inTransfer(BT_ADDR, ep_record[DATAIN_PIPE].epAddr, MAX_BUFFER_SIZE, (char*)buf, USB_NAK_NOWAIT);
if(!rcode){
buf_count = buf[2] + 4;
/*Serial.print("ACL_evt:");
Serial.print(buf_count, HEX);
Serial.print(":");
for(i = 0; i < buf_count; i++){
Serial.print((buf)[i], HEX);
Serial.print(" ");
}
Serial.print("\r\n");*/
return buf_count;
}
return 0;
};
/////////////////////////////////////////////////////////////////////////////////////
uint8_t TBT::HCI_Send(uint8_t Abytes_count, int8_t * Abytes) {
uint8_t i;
buf_count = 0;
/*Serial.print("HCI_com:");
Serial.print(Abytes_count, HEX);
Serial.print(":");
for(i=0;i<Abytes_count;i++){
Serial.print(Abytes[i], HEX);
Serial.print(" ");
}
Serial.print("\r\n");*/
return(Usb.ctrlReq(BT_ADDR, ep_record[CONTROL_PIPE].epAddr, bmREQ_HCI_OUT, HCI_COMMAND_REQ, 0x00, 0x00 ,0, Abytes_count, (char*)Abytes));
};
/////////////////////////////////////////////////////////////////////////////////////
uint8_t TBT::ACL_Send(uint8_t Abytes_count, int8_t * Abytes) {
uint8_t i;
buf_count = 0;
/*Serial.print("ACL_com:");
Serial.print(Abytes_count, HEX);
Serial.print(":");
for(i=0;i<Abytes_count;i++){
Serial.print(Abytes[i], HEX);
Serial.print(" ");
}
Serial.print("\r\n");*/
return(Usb.outTransfer(BT_ADDR, ep_record[DATAOUT_PIPE].epAddr, Abytes_count, (char*)Abytes));
};
/////////////////////////////////////////////////////////////////////////////////////
uint8_t TBT::GetState(void){
return cState;
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::Convert(uint8_t Abytes_count, const int8_t * Abytes1, int8_t * Abytes2){
uint8_t i;
for(i = 0; i < Abytes_count; i++){
Abytes2[i] = pgm_read_byte(Abytes1++);
}
};
/////////////////////////////////////////////////////////////////////////////////////
void TBT::HCI_Task(void){
uint8_t i;
if(cState >= BT_STATE_HCI){
if(hci_state == HCI_STATE_INIT) {
hci_state = HCI_STATE_RESET_WAIT;
Convert(3, hci_reset_cmd, buf);
HCI_Send(3, buf);
}
if(hci_state == HCI_STATE_RESET_WAIT) {
if((buf_count == 6) && strcmp_p(buf, hci_reset_resp, 6)){
hci_state = HCI_STATE_READ_BD_ADDR_WAIT;
Convert(3, hci_read_bd_addr_cmd, buf);
HCI_Send(3, buf);
}
}//else dbgn("could not reset device");
if(hci_state == HCI_STATE_READ_BD_ADDR_WAIT) {
if((buf_count == 12) && strcmp_p(buf, hci_read_bd_addr_resp, 6)){
for(i = 0; i < 6; i++)
my_addr.bytes[i] = buf[i + 6];
hci_state = HCI_STATE_SET_EVENT_FILTER_WAIT;
Convert(6, hci_set_event_filter_cmd, buf);
HCI_Send(6, buf);
}
}//else dbgn("could not read own bd address");
if(hci_state == HCI_STATE_SET_EVENT_FILTER_WAIT) {
if((buf_count == 6) && strcmp_p(buf, hci_set_event_filter_resp, 6)){
hci_state = HCI_STATE_WRITE_COD_WAIT;
Convert(6, hci_write_cod_cmd, buf);
HCI_Send(6, buf);
}
}//else dbgn("could not set event filter");
if(hci_state == HCI_STATE_WRITE_COD_WAIT){
if((buf_count == 6) && strcmp_p(buf, hci_write_cod_resp, 6)){
hci_state = HCI_STATE_SET_LOCAL_NAME_WAIT;
Convert(2, hci_set_local_name_cmd, buf);
buf[2] = my_name_len+1;
for(i = 0; i < my_name_len; i++)
buf[i + 3] = my_name[i];
buf[i + 3] = 0;
HCI_Send(4 + my_name_len, buf);
}
}//else dbgn("could not write code of device");
if(hci_state == HCI_STATE_SET_LOCAL_NAME_WAIT) {
if((buf_count == 6) && strcmp_p(buf, hci_set_local_name_resp, 6)){
hci_state = HCI_STATE_SET_PAGE_TIMEOUT_WAIT;
Convert(5, hci_set_page_timeout_cmd, buf);
HCI_Send(5, buf);
}
}//else dbgn("could not set own local name");
if(hci_state == HCI_STATE_SET_PAGE_TIMEOUT_WAIT) {
if((buf_count == 6) && strcmp_p(buf, hci_set_page_timeout_resp, 6)){
if(cBehaviour == BT_CLIENT){// scan for devices
hci_state = HCI_STATE_INQUIRY_RESULT_WAIT;
Convert(8, hci_inquiry_cmd, buf);
HCI_Send(8, buf);
} else if(cBehaviour == BT_SERVER){// be visible for incoming connection
hci_state = HCI_STATE_SCAN_ENABLE_WAIT;
Convert(4, hci_scan_enable_cmd, buf);
HCI_Send(4, buf);
} else {// connect to predefined address
Convert(16, hci_connect_cmd, buf);
for(i=0;i<6;i++)
buf[i+3] = user_addr.bytes[i];
for(i=0;i<2;i++)
buf[i+13] = buf[i+15];
hci_state = HCI_STATE_CONNECT_WAIT;
HCI_Send(16, buf);
}
}
}//else dbgn("could not set page timeout");
if(hci_state == HCI_STATE_INQUIRY_RESULT_WAIT) {
if((buf_count == 3) && strcmp_p(buf, hci_inquiry_resp, 3)){
hci_state = HCI_STATE_INQUIRY_RESULT_WAIT;
Convert(8, hci_inquiry_cmd, buf);
HCI_Send(8, buf);
}
if((buf_count == 17) && strcmp_p(buf, hci_inquiry_result_resp, 3)){
for(i=0;i<6;i++)
dest_addr.bytes[i] = buf[i+3];
Convert(16, hci_connect_cmd, buf);
for(i=0;i<6;i++)
buf[i+3] = dest_addr.bytes[i];
for(i=0;i<2;i++)
buf[i+13] = buf[i+15];
hci_state = HCI_STATE_CONNECT_WAIT;
HCI_Send(16, buf);
}
}
if((hci_state == HCI_STATE_CONNECT_WAIT) || (hci_state == HCI_STATE_PIN_REQUEST_WAIT) || (hci_state == HCI_STATE_CONNECTED)){
if((buf_count == 13) && (strcmp_p(buf, hci_connection_result_resp, 3)) && (buf[12] == 0x00)){
hci_state = HCI_STATE_CONNECTED;
buf_count = 0;
conn_handle[0] = buf[3];
conn_handle[1] = buf[4] | 0x20;
cState_p = cState;
cState = BT_STATE_CONNECTED;
}
if((buf_count == 8) && strcmp_p(buf, hci_pin_request_event, 2)){
for(i=0;i<6;i++)
dest_addr.bytes[i] = buf[i+2];
if(hci_state != HCI_STATE_CONNECTED) hci_state = HCI_STATE_CONNECT_WAIT;
Convert(3, hci_pin_reply_cmd, buf);
for(i=0;i<6;i++)
buf[i+3] = dest_addr.bytes[i];
buf[9] = my_pin_len;
for(i=0;i<16;i++)
buf[i+10] = 0;
for(i=0;i<my_pin_len;i++)
buf[i+10] = my_pin[i];
HCI_Send(26, buf);
}
}
if(hci_state == HCI_STATE_SCAN_ENABLE_WAIT) {
if((buf_count == 6) && strcmp_p(buf, hci_scan_enable_resp, 6)){
hci_state = HCI_STATE_PIN_REQUEST_WAIT;
}
}
}
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
void TBT::Send(int8_t * Astr, uint8_t Acnt){
uint8_t i;
buf[0] = conn_handle[0];
buf[1] = conn_handle[1];
buf[2] = Acnt;
buf[3] = 0;
for(i = 0; i < Acnt; i++)
buf[i+4] = Astr[i];
ACL_Send(Acnt+4, buf);
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
void TBT::SetName(int8_t * Astr, uint8_t Acnt){
uint8_t i;
my_name_len = Acnt;
if(my_name_len > 16) my_name_len = 16;
for(i=0;i<my_name_len;i++)
my_name[i] = Astr[i];
};
////////////////////////////////////////////////////////////////////////////////////////////////////////
void TBT::SetPin(int8_t * Astr, uint8_t Acnt){
uint8_t i;
my_pin_len = Acnt;
if(my_pin_len > 8) my_pin_len = 8;
for(i=0;i<my_pin_len;i++)
my_pin[i] = Astr[i];
};
///////////////////////////////////////////////////////////////////////////////////////
void TBT::SetEP(uint8_t Aep_addr1, uint8_t Aep_addr2, uint8_t Aep_addr3){
ep_addr[0] = Aep_addr1;
ep_addr[1] = Aep_addr2;
ep_addr[2] = Aep_addr3;
};
///////////////////////////////////////////////////////////////////////////////////////
Tbd_addr TBT::GetAddr(void){
return my_addr;
};
///////////////////////////////////////////////////////////////////////////////////////
int8_t TBT::strcmp_p(int8_t Astr1[], const int8_t Astr2[], uint16_t Acnt){
uint16_t i;
for(i = 0; i < Acnt; i++){
if(Astr1[i] != pgm_read_byte(Astr2++)) return 0;
}
return 1;
};