2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 uint8_t ao_btm_running;
22 __xdata uint8_t ao_btm_connected;
25 __xdata char ao_btm_buffer[1024];
28 #define AO_BTM_MAX_REPLY 16
29 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
31 extern volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
34 * Read a line of data from the serial port, truncating
35 * it after a few characters.
41 uint8_t ao_btm_reply_len = 0;
46 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
47 if (ao_btm_reply_len < sizeof (ao_btm_reply))
48 ao_btm_reply[ao_btm_reply_len++] = c;
49 if (ao_btm_ptr < sizeof (ao_btm_buffer))
50 ao_btm_buffer[ao_btm_ptr++] = c;
51 if (c == '\r' || c == '\n')
54 for (c = 0; c < 10; c++) {
55 ao_delay(AO_MS_TO_TICKS(10));
56 if (!ao_fifo_empty(ao_usart1_rx_fifo))
63 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
64 ao_btm_reply[c++] = '\0';
65 return ao_btm_reply_len;
69 * Drain the serial port completely
74 while (ao_btm_get_line())
79 ao_btm_echo(uint8_t echo)
81 ao_stdios[ao_btm_stdio].echo = echo;
85 * A command line pre-processor to detect connect/disconnect messages
86 * and update the internal state
93 while (ao_cmd_lex_c != '\n') {
94 if (ao_match_word("CONNECT")) {
97 ao_wakeup(&ao_btm_connected);
100 if (ao_match_word("DISCONNECT")) {
101 ao_btm_connected = 0;
103 ao_wakeup(&ao_btm_connected);
106 if (ao_match_word("ERROR"))
108 if (ao_match_word("OK"))
113 return !ao_btm_connected;
117 * A wrapper for ao_serial_pollchar that
118 * doesn't return any characters while we're
119 * initializing the bluetooth device
122 ao_btm_pollchar(void)
126 return AO_READ_AGAIN;
127 c = ao_serial_pollchar();
128 if (c != AO_READ_AGAIN)
129 if (ao_btm_ptr < sizeof (ao_btm_buffer))
130 ao_btm_buffer[ao_btm_ptr++] = c;
135 * Wait for the bluetooth device to return
136 * status from the previously executed command
139 ao_btm_wait_reply(void)
143 if (!strcmp(ao_btm_reply, "OK"))
145 if (!strcmp(ao_btm_reply, "ERROR"))
147 if (ao_btm_reply[0] == '\0')
153 ao_btm_cmd(__code char *cmd)
155 ao_cur_stdio = ao_btm_stdio;
161 * A thread to initialize the bluetooth device and
162 * hang around to blink the LED when connected
167 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
168 ao_add_stdio(ao_btm_pollchar,
171 ao_btm_stdio = ao_num_stdios - 1;
172 ao_cur_stdio = ao_btm_stdio;
175 ao_delay(AO_SEC_TO_TICKS(1));
178 ao_delay(AO_SEC_TO_TICKS(1));
181 ao_btm_cmd("ATQ0\r");
182 ao_btm_cmd("ATE0\r");
184 ao_delay(AO_SEC_TO_TICKS(1));
185 ao_btm_cmd("ATC0\r");
186 ao_btm_cmd("ATL4\r");
187 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
189 printf("ATN=TeleBT-%d\r", ao_serial_number);
193 while (!ao_btm_connected && !ao_btm_chat)
194 ao_sleep(&ao_btm_connected);
197 while (ao_btm_chat) {
199 c = ao_serial_pollchar();
200 if (c != AO_READ_AGAIN)
204 ao_sleep(&ao_usart1_rx_fifo);
209 while (ao_btm_connected) {
210 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
211 ao_delay(AO_SEC_TO_TICKS(3));
216 __xdata struct ao_task ao_btm_task;
219 * Connect directly to the bluetooth device, mostly
228 ao_wakeup(&ao_btm_connected);
230 while ((c = ao_usb_getchar()) != '~') {
231 if (c == '\n') c = '\r';
232 ao_serial_putchar(c);
235 while (!ao_btm_running) {
236 ao_wakeup(&ao_usart1_rx_fifo);
237 ao_delay(AO_MS_TO_TICKS(10));
242 * Dump everything received from the bluetooth device during startup
249 for (i = 0; i < ao_btm_ptr; i++)
250 putchar(ao_btm_buffer[i]);
254 __code struct ao_cmds ao_btm_cmds[] = {
255 { ao_btm_forward, "B\0BTM serial link." },
256 { ao_btm_dump, "d\0Dump btm buffer." },
264 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
265 ao_add_task(&ao_btm_task, ao_btm, "bt");
266 ao_cmd_register(&ao_btm_cmds[0]);