Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1515 | - | 1 | /** |
2 | * source for the Bluetooth driver |
||
3 | * @file bluetooth.c |
||
4 | * @author Linus Lotz<lotz@in.tum.de> |
||
5 | * @author Salomon Sickert |
||
6 | */ |
||
7 | |||
8 | |||
9 | |||
10 | #include "cpu.h" |
||
11 | #include <string.h> |
||
12 | #include <util/delay.h> |
||
13 | #include "bluetooth.h" |
||
14 | #include "main.h" |
||
15 | #ifdef HWVERSION3_9 |
||
16 | #include "uart1.h" |
||
17 | #include "usart.h" |
||
18 | #include "fifo.h" |
||
19 | #include "error.h" |
||
20 | #include "lcd.h" |
||
21 | #include "eeprom.h" |
||
22 | #include "error.h" |
||
23 | #include "setup.h" |
||
24 | |||
25 | |||
26 | //#define SaveMem |
||
27 | |||
28 | // |
||
29 | // Baudrate for the UART-connection to the BTM-222 on SQUIRREL |
||
30 | // |
||
31 | |||
32 | #define SQUIRREL |
||
33 | |||
34 | #ifdef SQUIRREL |
||
35 | #define UART_BAUD_RATE 19200 |
||
36 | #endif |
||
37 | |||
38 | #ifdef NUT |
||
39 | #define UART_BAUD_RATE 19200 |
||
40 | #endif |
||
41 | |||
42 | |||
43 | typedef enum { |
||
44 | BT_RAW, |
||
45 | BT_DATA, |
||
46 | BT_CMD, |
||
47 | BT_NOECHO, |
||
48 | BT_NOANSWER |
||
49 | } communication_mode_t; |
||
50 | |||
51 | #define BT_CMD_TIMEOUT_MS 2000 |
||
52 | |||
53 | typedef enum { |
||
54 | BT_TEST, // AT |
||
55 | BT_CONNECT, // ATA |
||
56 | BT_DISCONNECT, // ATH |
||
57 | BT_CLEAR_ADDRESS, // ATD0 |
||
58 | BT_SET_ADDRESS, // ATD=_____ |
||
59 | BT_FIND_DEVICES, // ATF? |
||
60 | BT_DISABLE_AUTOCONNECT, // ATO1 |
||
61 | BT_SET_MASTER, // ATR0 |
||
62 | BT_SET_SLAVE, // ATR1 |
||
63 | BT_SET_PIN, // ATP=1234 |
||
64 | BT_SET_57600, // ATL4 Baudrate 57600 |
||
65 | BT_SET_NOANSWER, // ATQ1 Rückmeldungen aus |
||
66 | BT_SET_NOECHO, // ATE0 ECHO deaktivieren |
||
67 | BT_SET_ANSWER, // ATQ0 Rückmeldungen |
||
68 | BT_SET_ECHO, // ATE1 ECHO aktivieren |
||
69 | BT_SET_DEFAULT, // Defaultwerte setzen |
||
70 | BT_SET_NAME, // Devicename |
||
71 | BT_SET_DISPWRDOWN // disable auto Powerdown |
||
72 | } bt_cmd_t; |
||
73 | |||
74 | #ifdef SQUIRREL |
||
75 | #define IN_FIFO_SIZE 100 |
||
76 | #endif |
||
77 | |||
78 | #ifdef NUT |
||
79 | #define IN_FIFO_SIZE 65 |
||
80 | #endif |
||
81 | |||
82 | static uint8_t bt_buffer[IN_FIFO_SIZE]; |
||
83 | static fifo_t in_fifo; |
||
84 | |||
85 | static bt_mode_t bt_mode = BLUETOOTH_SLAVE; |
||
86 | static communication_mode_t comm_mode = BT_CMD; |
||
87 | |||
88 | uint8_t i = 0; |
||
89 | uint8_t NoEcho = 0; |
||
90 | uint8_t NoAnswer = 0; |
||
91 | |||
92 | |||
93 | |||
94 | // Set a timeout of Y ms and a Conditon X, which have to be true while timeout |
||
95 | #define while_timeout(X, Y) for(uint16_t __timeout = 0; __timeout++ <= Y && (X); Delay_MS(Y ? 1 : 0)) |
||
96 | |||
97 | //-------------------------------------------------------------- |
||
98 | void Delay_MS(int count) |
||
99 | { |
||
100 | for (int i = 0; i < count; i++) |
||
101 | _delay_ms(1); |
||
102 | } |
||
103 | |||
104 | |||
105 | //-------------------------------------------------------------- |
||
106 | static void uart_receive(void) |
||
107 | { |
||
108 | unsigned int uart_data; |
||
109 | |||
110 | while (!fifo_is_full(&in_fifo)) |
||
111 | { |
||
112 | uart_data = uart1_getc(); |
||
113 | |||
114 | // USART_puts("."); |
||
115 | |||
116 | switch (uart_data & 0xFF00) { |
||
117 | // Framing Error detected, i.e no stop bit detected |
||
118 | case UART_FRAME_ERROR: |
||
119 | #ifdef DEBUG |
||
120 | warn_pgm(PSTR("FRM ERR")); |
||
121 | #endif |
||
122 | return; |
||
123 | |||
124 | // Overrun, a character already presend in the UART UDR register was |
||
125 | // not read by the interrupt handler before the next character arrived, |
||
126 | // one or more received characters have been dropped |
||
127 | // |
||
128 | case UART_OVERRUN_ERROR: |
||
129 | #ifdef DEBUG |
||
130 | warn_pgm(PSTR("OVR ERR")); |
||
131 | #endif |
||
132 | return; |
||
133 | |||
134 | // We are not reading the receive buffer fast enough, |
||
135 | // one or more received character have been dropped |
||
136 | // |
||
137 | case UART_BUFFER_OVERFLOW: |
||
138 | #ifdef DEBUG |
||
139 | warn_pgm(PSTR("BUF ERR")); |
||
140 | #endif |
||
141 | return; |
||
142 | |||
143 | // UART Inputbuffer empty, nothing to do |
||
144 | case UART_NO_DATA: |
||
145 | return; |
||
146 | |||
147 | default: |
||
148 | { |
||
149 | fifo_write(&in_fifo, uart_data); |
||
150 | // USART_putc(uart_data); |
||
151 | } |
||
152 | } |
||
153 | } |
||
154 | #ifdef DEBUG |
||
155 | warn_pgm(PSTR("FIFO OVR ERR")); |
||
156 | #endif |
||
157 | } |
||
158 | |||
159 | |||
160 | //-------------------------------------------------------------- |
||
161 | static void uart_send(const char *data, const uint8_t length) |
||
162 | { |
||
163 | #ifdef DEBUG |
||
164 | debug_pgm(PSTR("bt_uart_send")); |
||
165 | #endif |
||
166 | |||
167 | char echo; |
||
168 | |||
169 | lcd_printp_at (i++, 1, PSTR("."), 0); |
||
170 | for (uint8_t i = 0; i < length; i++) |
||
171 | { |
||
172 | |||
173 | #ifdef DEBUG |
||
174 | USART_putc((data[i])); //test |
||
175 | #endif |
||
176 | // debug_pgm(PSTR("bt_init_S")); |
||
177 | |||
178 | if (uart1_putc(data[i]) == 0) |
||
179 | { |
||
180 | #ifdef DEBUG |
||
181 | warn_pgm(PSTR("UART: Remote not ready")); |
||
182 | #endif |
||
183 | return; |
||
184 | } |
||
185 | |||
186 | if (comm_mode == BT_RAW) |
||
187 | _delay_ms(50); |
||
188 | |||
189 | if (comm_mode == BT_DATA) |
||
190 | _delay_ms(1); |
||
191 | |||
192 | if (comm_mode == BT_NOECHO) |
||
193 | _delay_ms(1); |
||
194 | |||
195 | if (comm_mode == BT_CMD) |
||
196 | { |
||
197 | uint8_t x = 0; |
||
198 | for (; x < 3; x++) |
||
199 | { |
||
200 | // // while_timeout(X, Y) for(uint16_t __timeout = 0; __timeout++ <= Y && (X); _delay_ms(Y ? 1 : 0)) |
||
201 | // while_timeout(fifo_is_empty(&in_fifo), 200) |
||
202 | for(uint16_t __timeout = 0; __timeout++ <= 200 && (fifo_is_empty(&in_fifo)); _delay_ms(200 ? 1 : 0)) |
||
203 | { |
||
204 | uart_receive(); |
||
205 | } |
||
206 | |||
207 | fifo_read(&in_fifo, &echo); |
||
208 | |||
209 | if (echo != data[i]) { |
||
210 | if (uart1_putc(data[i]) == 0) |
||
211 | { |
||
212 | warn_pgm(PSTR ("UART: Remote not ready")); |
||
213 | return; |
||
214 | } |
||
215 | } |
||
216 | else |
||
217 | break; |
||
218 | } |
||
219 | |||
220 | if (x == 3) |
||
221 | { |
||
222 | error_putc(data[i]); |
||
223 | error_pgm(PSTR("BT: WRONG ECHO")); |
||
224 | } |
||
225 | } |
||
226 | } |
||
227 | } |
||
228 | |||
229 | |||
230 | //-------------------------------------------------------------- |
||
231 | static uint16_t send_cmd(const bt_cmd_t command, const char *data) |
||
232 | { |
||
233 | _delay_ms(500); // org 500 300 zu wenig |
||
234 | char full_command[20]; // Maximum command size |
||
235 | |||
236 | switch (command) |
||
237 | { |
||
238 | case BT_SET_PIN: |
||
239 | strcpy_P(full_command, PSTR("ATP=")); |
||
240 | for (uint8_t i = 0; i < bt_pin_length; i++) |
||
241 | { |
||
242 | full_command[i+4] = bt_pin[i]; |
||
243 | } |
||
244 | full_command[(bt_pin_length+4)] =0; |
||
245 | break; |
||
246 | |||
247 | case BT_SET_DEFAULT: |
||
248 | strcpy_P(full_command, PSTR("ATZ0")); |
||
249 | break; |
||
250 | |||
251 | case BT_SET_57600: |
||
252 | strcpy_P(full_command, PSTR("ATL4")); |
||
253 | break; |
||
254 | |||
255 | case BT_SET_NOANSWER: |
||
256 | strcpy_P(full_command, PSTR("ATQ1")); |
||
257 | break; |
||
258 | |||
259 | case BT_SET_NOECHO: |
||
260 | strcpy_P(full_command, PSTR("ATE0")); |
||
261 | break; |
||
262 | |||
263 | case BT_SET_ANSWER: |
||
264 | strcpy_P(full_command, PSTR("ATQ0")); |
||
265 | break; |
||
266 | |||
267 | case BT_SET_ECHO: |
||
268 | strcpy_P(full_command, PSTR("ATE1")); |
||
269 | break; |
||
270 | |||
271 | case BT_TEST: |
||
272 | strcpy_P(full_command, PSTR("AT")); |
||
273 | break; |
||
274 | |||
275 | case BT_CONNECT: |
||
276 | strcpy_P(full_command, PSTR("ATA")); |
||
277 | break; |
||
278 | |||
279 | case BT_DISCONNECT: |
||
280 | strcpy_P(full_command, PSTR("ATH")); |
||
281 | break; |
||
282 | |||
283 | case BT_CLEAR_ADDRESS: |
||
284 | strcpy_P(full_command, PSTR("ATD0")); |
||
285 | break; |
||
286 | |||
287 | case BT_SET_ADDRESS: |
||
288 | strcpy_P(full_command, PSTR("ATD=")); |
||
289 | memcpy((full_command + strlen(full_command)), data, 12); |
||
290 | full_command[16] = 0; |
||
291 | break; |
||
292 | |||
293 | case BT_FIND_DEVICES: |
||
294 | strcpy_P(full_command, PSTR("ATF?")); |
||
295 | break; |
||
296 | |||
297 | case BT_DISABLE_AUTOCONNECT: |
||
298 | strcpy_P(full_command, PSTR("ATO1")); |
||
299 | break; |
||
300 | |||
301 | case BT_SET_MASTER: |
||
302 | strcpy_P(full_command, PSTR("ATR0")); |
||
303 | break; |
||
304 | |||
305 | case BT_SET_SLAVE: |
||
306 | strcpy_P(full_command, PSTR("ATR1")); |
||
307 | break; |
||
308 | |||
309 | case BT_SET_NAME: |
||
310 | strcpy_P(full_command, PSTR("ATN=")); |
||
311 | for (uint8_t i = 0; i < bt_name_len; i++) |
||
312 | { |
||
313 | full_command[i + 4] = bt_name[i]; |
||
314 | } |
||
315 | full_command[(bt_name_len + 4)] = 0; |
||
316 | break; |
||
317 | |||
318 | case BT_SET_DISPWRDOWN: |
||
319 | strcpy_P(full_command, PSTR("ATS1")); |
||
320 | break; |
||
321 | |||
322 | default: |
||
323 | warn_pgm(PSTR("CMD UNK")); |
||
324 | return false; |
||
325 | } |
||
326 | |||
327 | strcat_P(full_command, PSTR("\r")); |
||
328 | |||
329 | // throw away your television |
||
330 | uart_receive(); |
||
331 | fifo_clear(&in_fifo); |
||
332 | // debug_pgm(PSTR("bt_init3")); |
||
333 | // send command |
||
334 | uart_send(full_command, strlen(full_command)); |
||
335 | |||
336 | if (command== BT_SET_ECHO) |
||
337 | return true; |
||
338 | |||
339 | if (command== BT_SET_ANSWER) |
||
340 | return true; |
||
341 | |||
342 | // get response |
||
343 | while_timeout(true, BT_CMD_TIMEOUT_MS) |
||
344 | { |
||
345 | uart_receive(); |
||
346 | if (fifo_strstr_pgm(&in_fifo, PSTR("OK\r\n"))) |
||
347 | { |
||
348 | info_pgm(PSTR("CMD SEND: OK")); |
||
349 | return true; |
||
350 | } |
||
351 | |||
352 | if (fifo_strstr_pgm(&in_fifo, PSTR("ERROR\r\n"))) |
||
353 | { |
||
354 | #ifdef DEBUG |
||
355 | info_pgm(PSTR("CMD SEND: Error")); |
||
356 | #endif |
||
357 | return false; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | #ifdef DEBUG |
||
362 | if (command != BT_TEST) |
||
363 | warn_pgm(PSTR("CMD SEND: TIMEOUT")); |
||
364 | #endif |
||
365 | |||
366 | |||
367 | return false; |
||
368 | } |
||
369 | |||
370 | |||
371 | //-------------------------------------------------------------- |
||
372 | void test(void) |
||
373 | { |
||
374 | comm_mode = BT_RAW; |
||
375 | for (uint8_t i = 0; i < 3; i++) |
||
376 | if (send_cmd(BT_TEST, NULL)) |
||
377 | break; |
||
378 | comm_mode = BT_CMD; |
||
379 | } |
||
380 | |||
381 | |||
382 | #ifndef SaveMem |
||
383 | |||
384 | //-------------------------------------------------------------- |
||
385 | static void clean_line(void) |
||
386 | { |
||
387 | while_timeout(true, 50) |
||
388 | uart_receive(); |
||
389 | fifo_strstr_pgm(&in_fifo, PSTR("\r\n")); |
||
390 | } |
||
391 | |||
392 | static communication_mode_t update_comm_mode(uint16_t timeout_ms) |
||
393 | { |
||
394 | while_timeout(true, timeout_ms) |
||
395 | { |
||
396 | uart_receive(); |
||
397 | |||
398 | if (fifo_strstr_pgm(&in_fifo, PSTR("DISCONNECT"))) |
||
399 | { |
||
400 | clean_line(); |
||
401 | test(); |
||
402 | comm_mode = BT_CMD; |
||
403 | return comm_mode; |
||
404 | } |
||
405 | |||
406 | if (fifo_strstr_pgm(&in_fifo, PSTR("CONNECT"))) |
||
407 | { |
||
408 | _delay_ms(100); //don't delete this, else there will be no success!!!!!!!!! |
||
409 | comm_mode = BT_DATA; |
||
410 | return comm_mode; |
||
411 | } |
||
412 | |||
413 | if (fifo_strstr_pgm (&in_fifo, PSTR("Time out,Fail to connect!"))) |
||
414 | { |
||
415 | clean_line(); |
||
416 | #ifdef DEBUG |
||
417 | debug_pgm(PSTR("CONNECT FAILED")); |
||
418 | #endif |
||
419 | test(); |
||
420 | comm_mode = BT_CMD; |
||
421 | return comm_mode; |
||
422 | } |
||
423 | } |
||
424 | |||
425 | return comm_mode; |
||
426 | } |
||
427 | #endif |
||
428 | |||
429 | |||
430 | //-------------------------------------------------------------- |
||
431 | uint16_t bt_init(void) |
||
432 | { |
||
433 | uint8_t init_error = false; |
||
434 | uint8_t BT_found = 0; |
||
435 | i = 0; |
||
436 | |||
437 | set_BTOn(); |
||
438 | |||
439 | lcd_cls(); |
||
440 | lcd_printp_at (0, 0, PSTR("BT initialisieren.."), 0); |
||
441 | _delay_ms(200); |
||
442 | |||
443 | for (uint8_t z = (bt_name_length); z > 0; z--) |
||
444 | { |
||
445 | if (bt_name[z - 1] != ' ') |
||
446 | { |
||
447 | bt_name_len = z; |
||
448 | break; |
||
449 | } |
||
450 | } |
||
451 | |||
452 | uart1_init(UART_BAUD_SELECT(57600, F_CPU)); |
||
453 | fifo_init(&in_fifo, bt_buffer, IN_FIFO_SIZE); |
||
454 | _delay_ms(100); |
||
455 | // debug_pgm(PSTR("bt_init")); |
||
456 | uart_receive(); |
||
457 | // debug_pgm(PSTR("bt_init1")); |
||
458 | fifo_clear(&in_fifo); |
||
459 | send_cmd(BT_TEST, NULL); |
||
460 | comm_mode = BT_NOECHO; |
||
461 | send_cmd(BT_SET_ECHO, NULL); |
||
462 | send_cmd(BT_SET_ANSWER, NULL); |
||
463 | |||
464 | // debug_pgm(PSTR("bt_init2")); |
||
465 | #ifdef DEBUG |
||
466 | debug_pgm(PSTR("Check with 57600")); |
||
467 | #endif |
||
468 | // send_cmd(BT_TEST, NULL); // Schrott löschen |
||
469 | |||
470 | if (send_cmd(BT_TEST, NULL)) // Test mit 57600 |
||
471 | { |
||
472 | #ifdef DEBUG |
||
473 | debug_pgm(PSTR("BT found 57600 Baud")); |
||
474 | #endif |
||
475 | BT_found = 1; |
||
476 | } |
||
477 | |||
478 | if (BT_found == 0) |
||
479 | { |
||
480 | #ifdef DEBUG |
||
481 | debug_pgm(PSTR("Check with 19200")); |
||
482 | #endif |
||
483 | uart1_init(UART_BAUD_SELECT(19200, F_CPU));// Test mit 19200 |
||
484 | _delay_ms(100); |
||
485 | send_cmd(BT_TEST, NULL); // Schrott löschen |
||
486 | send_cmd(BT_SET_ANSWER, NULL); |
||
487 | send_cmd(BT_SET_ECHO, NULL); |
||
488 | |||
489 | if (send_cmd(BT_TEST, NULL)) |
||
490 | { |
||
491 | debug_pgm(PSTR("19200 OK")); |
||
492 | if (send_cmd(BT_TEST, NULL)) |
||
493 | { |
||
494 | #ifdef DEBUG |
||
495 | debug_pgm(PSTR("BT found 19200 Baud")); |
||
496 | #endif |
||
497 | BT_found = 2; |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | |||
502 | if (BT_found == 0) |
||
503 | { |
||
504 | #ifdef DEBUG |
||
505 | debug_pgm(PSTR("Check with 9600")); |
||
506 | #endif |
||
507 | uart1_init(UART_BAUD_SELECT(9600, F_CPU));//test mit 9600 |
||
508 | _delay_ms(100); |
||
509 | send_cmd(BT_TEST, NULL); |
||
510 | send_cmd(BT_SET_ANSWER, NULL); |
||
511 | send_cmd(BT_SET_ECHO, NULL); |
||
512 | if (send_cmd(BT_TEST, NULL)); |
||
513 | { |
||
514 | #ifdef DEBUG |
||
515 | debug_pgm(PSTR("9600 OK")); |
||
516 | #endif |
||
517 | if (send_cmd(BT_TEST, NULL)) |
||
518 | { |
||
519 | #ifdef DEBUG |
||
520 | debug_pgm(PSTR("BT found 9600 Baud")); |
||
521 | #endif |
||
522 | BT_found = 3; |
||
523 | } |
||
524 | } |
||
525 | } |
||
526 | |||
527 | if (BT_found > 0) |
||
528 | { |
||
529 | /* Set comm_mode to CMD */ |
||
530 | comm_mode = BT_CMD; |
||
531 | // test(); |
||
532 | /* Set BTM Baudrate */ |
||
533 | if (!(send_cmd(BT_SET_57600, NULL))) |
||
534 | init_error = true; |
||
535 | uart1_init(UART_BAUD_SELECT(57600, F_CPU)); |
||
536 | _delay_ms(100); |
||
537 | // test(); |
||
538 | /* Clear remote address */ |
||
539 | if(!(send_cmd(BT_CLEAR_ADDRESS, NULL))) |
||
540 | init_error = true; |
||
541 | // test(); |
||
542 | /* Set BTM to SLAVE */ |
||
543 | if (!(send_cmd(BT_SET_SLAVE, NULL))) |
||
544 | init_error = true; |
||
545 | // test(); |
||
546 | /* Set BTM PIN */ |
||
547 | if(!(send_cmd(BT_SET_PIN, NULL))) |
||
548 | init_error = true; |
||
549 | // test(); |
||
550 | /* Set BTM Name */ |
||
551 | if(!(send_cmd(BT_SET_NAME, NULL))) |
||
552 | init_error = true; |
||
553 | _delay_ms(300); |
||
554 | // test(); |
||
555 | if(!(send_cmd(BT_SET_DISPWRDOWN, NULL))) |
||
556 | init_error = true; |
||
557 | // test(); |
||
558 | /* Set BTM Echo aus */ |
||
559 | send_cmd(BT_SET_NOECHO, NULL); |
||
560 | // test(); |
||
561 | comm_mode = BT_NOECHO; |
||
562 | /* Set BTM Answer aus */ |
||
563 | send_cmd(BT_SET_NOANSWER, NULL); |
||
564 | // test(); |
||
565 | |||
566 | bt_mode = BLUETOOTH_SLAVE; |
||
567 | |||
568 | set_USBOn(); |
||
569 | |||
570 | if (!init_error) |
||
571 | { |
||
572 | WriteBTInitFlag(); // Init merken |
||
573 | return true; |
||
574 | } |
||
575 | else |
||
576 | return false; |
||
577 | } |
||
578 | else |
||
579 | { |
||
580 | set_USBOn(); |
||
581 | return false; |
||
582 | } |
||
583 | |||
584 | } |
||
585 | |||
586 | |||
587 | #ifndef SaveMem |
||
588 | |||
589 | //-------------------------------------------------------------- |
||
590 | uint16_t bt_set_mode(const bt_mode_t mode) |
||
591 | { |
||
592 | if (update_comm_mode(0) == BT_DATA) |
||
593 | return false; |
||
594 | |||
595 | if (mode == bt_mode) |
||
596 | return true; |
||
597 | |||
598 | if (mode == BLUETOOTH_MASTER) |
||
599 | if (send_cmd(BT_SET_MASTER, NULL)) |
||
600 | { |
||
601 | bt_mode = BLUETOOTH_MASTER; |
||
602 | test(); |
||
603 | send_cmd(BT_DISABLE_AUTOCONNECT, NULL); |
||
604 | } |
||
605 | |||
606 | if (mode == BLUETOOTH_SLAVE) |
||
607 | if (send_cmd(BT_SET_SLAVE, NULL)) |
||
608 | { |
||
609 | bt_mode = BLUETOOTH_SLAVE; |
||
610 | } |
||
611 | |||
612 | test(); |
||
613 | return mode == bt_mode; |
||
614 | } |
||
615 | |||
616 | |||
617 | //-------------------------------------------------------------- |
||
618 | uint16_t bt_receive(void *data, uint8_t length, uint16_t timeout_ms) |
||
619 | { |
||
620 | uint8_t rec_length = 0; |
||
621 | uint8_t i = 0; |
||
622 | |||
623 | // while_timeout(true, timeout_ms); |
||
624 | for(uint16_t __timeout = 0; __timeout++ <= true && (timeout_ms); _delay_ms(true ? 1 : 0)) |
||
625 | { |
||
626 | if (i == length) |
||
627 | return true; |
||
628 | |||
629 | uart_receive(); |
||
630 | |||
631 | if (fifo_is_empty(&in_fifo)) |
||
632 | continue; |
||
633 | |||
634 | if (update_comm_mode(0) != BT_DATA) |
||
635 | { |
||
636 | #ifdef DEBUG |
||
637 | debug_pgm(PSTR("not connected")); |
||
638 | #endif |
||
639 | return false; |
||
640 | } |
||
641 | // We have a connection |
||
642 | if (timeout_ms == 0) |
||
643 | timeout_ms += 2000; |
||
644 | |||
645 | if (fifo_is_empty(&in_fifo)) |
||
646 | continue; |
||
647 | |||
648 | // Find starting point of packet |
||
649 | if (!rec_length) |
||
650 | { |
||
651 | fifo_read(&in_fifo, (char *)&rec_length); |
||
652 | |||
653 | if (rec_length != length) |
||
654 | { |
||
655 | rec_length = 0; |
||
656 | } |
||
657 | else |
||
658 | { |
||
659 | // You've got mail! |
||
660 | timeout_ms += 2000; |
||
661 | } |
||
662 | } |
||
663 | else |
||
664 | { |
||
665 | fifo_read(&in_fifo, (char *)data + i); |
||
666 | i++; |
||
667 | } |
||
668 | } |
||
669 | return false; |
||
670 | } |
||
671 | |||
672 | #endif |
||
673 | |||
674 | #ifndef SaveMem |
||
675 | |||
676 | |||
677 | //-------------------------------------------------------------- |
||
678 | uint16_t bt_send(void *data, const uint8_t length) |
||
679 | { |
||
680 | if (update_comm_mode(0) == BT_CMD) |
||
681 | return false; |
||
682 | |||
683 | uart_send((const char *)&length, 1); |
||
684 | uart_send((char *)data, length); |
||
685 | return (update_comm_mode(0) == BT_DATA); |
||
686 | } |
||
687 | |||
688 | |||
689 | #ifdef SQUIRREL |
||
690 | |||
691 | //-------------------------------------------------------------- |
||
692 | uint16_t bt_connect(const char *address) |
||
693 | { |
||
694 | // Maybe we already disconnected??? |
||
695 | if (BT_DATA == update_comm_mode(0)) |
||
696 | { |
||
697 | #ifdef DEBUG |
||
698 | debug_pgm(PSTR("We are still connected...")); |
||
699 | #endif |
||
700 | return false; |
||
701 | } |
||
702 | test(); |
||
703 | |||
704 | if (!send_cmd(BT_DISABLE_AUTOCONNECT, address)) |
||
705 | return false; |
||
706 | |||
707 | test(); |
||
708 | #ifdef DEBUG |
||
709 | debug_pgm (PSTR ("SET_ADD")); |
||
710 | #endif |
||
711 | |||
712 | if (!send_cmd(BT_SET_ADDRESS, address)) |
||
713 | return false; |
||
714 | |||
715 | test(); |
||
716 | #ifdef DEBUG |
||
717 | debug_pgm (PSTR ("CONNECT")); |
||
718 | #endif |
||
719 | |||
720 | if (!send_cmd(BT_CONNECT, NULL)) |
||
721 | return false; |
||
722 | #ifdef DEBUG |
||
723 | debug_pgm (PSTR ("WAIT FOR COMM")); |
||
724 | #endif |
||
725 | return (BT_DATA == update_comm_mode(60000)); |
||
726 | } |
||
727 | |||
728 | |||
729 | //-------------------------------------------------------------- |
||
730 | uint16_t bt_disconnect(void) |
||
731 | { |
||
732 | /* Bluetooth reseten */ |
||
733 | // set_bit(PORTC.DIR, 4); |
||
734 | // set_bit(PORTC.OUT, 4); |
||
735 | _delay_ms(500); |
||
736 | // clear_bit(PORTC.OUT, 4); |
||
737 | // return bt_init(); |
||
738 | |||
739 | #if 1 |
||
740 | if (BT_CMD == update_comm_mode(0)) |
||
741 | { |
||
742 | fifo_clear(&in_fifo); |
||
743 | return true; |
||
744 | } |
||
745 | |||
746 | // Switch to online cmd mode |
||
747 | for (uint8_t i = 0; i < 4; i++) |
||
748 | { |
||
749 | const char plus = '+'; |
||
750 | uart_send(&plus, 1); |
||
751 | _delay_ms(1500); |
||
752 | } |
||
753 | |||
754 | //comm_mode = BT_CMD; |
||
755 | |||
756 | if (!send_cmd(BT_DISCONNECT, NULL)) |
||
757 | return false; |
||
758 | |||
759 | test(); |
||
760 | if (!send_cmd(BT_CLEAR_ADDRESS, NULL)) |
||
761 | return false; |
||
762 | test(); |
||
763 | |||
764 | if (BT_CMD == update_comm_mode(10000)) |
||
765 | { |
||
766 | fifo_clear(&in_fifo); |
||
767 | return true; |
||
768 | } |
||
769 | #ifdef DEBUG |
||
770 | debug_pgm(PSTR("Still in DATA??")); |
||
771 | #endif |
||
772 | return false; |
||
773 | #endif |
||
774 | } |
||
775 | |||
776 | |||
777 | //-------------------------------------------------------------- |
||
778 | void copy_address(const char *src, char *dst) |
||
779 | { |
||
780 | uint8_t off = 0; |
||
781 | |||
782 | for (uint8_t i = 0; i < 14; i++) |
||
783 | { |
||
784 | if (src[i + off] == '-') |
||
785 | off++; |
||
786 | |||
787 | dst[i] = src[i + off]; |
||
788 | } |
||
789 | } |
||
790 | |||
791 | |||
792 | //-------------------------------------------------------------- |
||
793 | uint16_t bt_discover(char result[8][12]) |
||
794 | |||
795 | // 14.8.2011 ist noch nicht getestet, wird für PKT auch nicht benötigt, Cebra |
||
796 | { |
||
797 | // update_callback(20); |
||
798 | test(); |
||
799 | if (!bt_set_mode(BLUETOOTH_MASTER)) |
||
800 | return false; |
||
801 | |||
802 | if (!send_cmd(BT_FIND_DEVICES, NULL)) |
||
803 | return false; |
||
804 | |||
805 | char buffer[255]; //oversized, but who cares? |
||
806 | char *bufferhead = buffer; |
||
807 | uint8_t pos = 0; |
||
808 | uint16_t Timeout = 20000; |
||
809 | uint8_t pos1 = 0; |
||
810 | |||
811 | do |
||
812 | { |
||
813 | uart_receive(); |
||
814 | Timeout--; |
||
815 | pos1++; |
||
816 | _delay_ms(1); |
||
817 | |||
818 | } |
||
819 | while ((Timeout > 0) ||(!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n")))); |
||
820 | |||
821 | // byte_to_hex(Timeout); |
||
822 | |||
823 | assert_pgm((!fifo_strstr_pgm(&in_fifo, PSTR("Inquiry Results:\r\n"))),PSTR("INQ Result false")); |
||
824 | |||
825 | info_pgm (PSTR ("Rec1")); |
||
826 | |||
827 | for (uint16_t i = 0; i < 60000; i++) |
||
828 | { |
||
829 | //if ((i % 1000) == 0) |
||
830 | //update_callback(40 + i / 1000); |
||
831 | uart_receive(); |
||
832 | // lcd_printp(".", 0); |
||
833 | _delay_ms(1); |
||
834 | } |
||
835 | |||
836 | info_pgm (PSTR ("Rec2")); |
||
837 | |||
838 | //update_callback(100); |
||
839 | |||
840 | while (!fifo_is_empty(&in_fifo)) |
||
841 | { |
||
842 | // Get next line |
||
843 | while (!fifo_cmp_pgm(&in_fifo, PSTR("\r\n"))) |
||
844 | { |
||
845 | fifo_read(&in_fifo, bufferhead); |
||
846 | bufferhead++; |
||
847 | } |
||
848 | // terminate string |
||
849 | *bufferhead = 0; |
||
850 | |||
851 | //reset bufferhead |
||
852 | bufferhead = buffer; |
||
853 | |||
854 | if (strlen(buffer) == 0) |
||
855 | continue; //the empty line before end of inquiry |
||
856 | |||
857 | if (strstr_P(buffer, PSTR("Inquiry End"))) |
||
858 | { |
||
859 | fifo_clear(&in_fifo); |
||
860 | test(); |
||
861 | return true; |
||
862 | } |
||
863 | |||
864 | if (strncmp_P(PSTR("0012"), &buffer[21], 4)) |
||
865 | { |
||
866 | copy_address(&buffer[21], result[pos]); |
||
867 | pos++; |
||
868 | } |
||
869 | } |
||
870 | |||
871 | return false; |
||
872 | } |
||
873 | |||
874 | #endif |
||
875 | #endif |
||
876 | #endif /* SQUIRREL */ |