]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src2/testapp.c
2d44ade465acfd56b0603e322e752c49e2c1dd75
[tiny-bt.git] / src2 / testapp.c
1 /*
2 *  C Implementation: testapp
3 *
4 * Description: 
5 *
6 *
7 * Author: root <root@ubuntu>, (C) 2008
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <stdint.h>
17 #include "tiny_bt_hci_core.h"
18 #include "tiny_bt_hci_cmd.h"
19 #include "testapp.h"
20
21 bt_address rem_bd_addr_array[HCI_MAX_DEV];
22 bt_address rem_addr;
23 int glob_count = 0;
24 int stop = 0;
25 connection_hci connection,*p_connection=&connection;
26
27 void callback_app_read_bd_addr(bt_address *p_address)
28 {
29         printf("Read address is :");
30         printba(p_address);
31         tiny_bt_read_local_name(&(p_connection->master.name), callback_app_read_local_name);
32 }
33
34 void callback_app_read_local_name(char *p_name)
35 {
36         int i;
37         
38         printf("read name is:");
39         
40         for (i = 0; i < 8; i++) {
41                 printf("%c", *(p_name+i));
42         }
43         printf("\n");
44         tiny_bt_inquiry(&rem_addr, callback_app_inquiry_RSSI, callback_app_inquiry_complete);
45 }
46
47 void callback_app_inquiry_RSSI(bt_address *p_address)
48 {
49         static int count = 0;
50         int i, sign = 0;
51         
52         if (count == 0) {
53                 rem_bd_addr_array[0] = *p_address;
54                 count++;
55         } else {
56                 for (i = 0; i < count; i++) {
57                         if (compare_bda(p_address, &rem_bd_addr_array[i])) {
58                                 sign = 1;
59                                 break;
60                         }
61                 }
62                 if (sign == 0) {
63                         if (count < HCI_MAX_DEV) {      
64                                 rem_bd_addr_array[count] = *p_address;
65                                 printf("There is device:");
66                                 printba(p_address);
67                                 count++;
68                         } else {
69                                 printf("Array full, there is more than 16 devices in surroundig area\n");
70                         }
71                 }
72         }
73         glob_count = count;
74         tiny_bt_inquiry_register_again(&rem_addr, callback_app_inquiry_RSSI);
75 }
76
77 void callback_app_inquiry_complete(void)
78 {
79         int i;
80         
81         printf("inquiry complete\n");
82         
83         for (i = 0; i < glob_count; i++) {
84                 printf("remote dev");
85                 printba(&rem_bd_addr_array[i]);
86         }
87         
88         if (glob_count > 0) {
89                 p_connection->slave.bdaddr = rem_bd_addr_array[0];
90                 tiny_bt_connect(&rem_bd_addr_array[0], 
91                         &p_connection->handle, callback_app_connection_complete);
92         } else {
93                 printf("There is no remote device suitable for connect\n");
94                 stop=1;
95         }
96 }
97
98 void callback_app_connection_complete(__u16 *p_handle)
99 {
100         char ar[6] = {"hallo "};
101
102         connection_state state = CONNECT;
103         p_connection->con_state = state;
104         printf("Connection complete handle:%d\n", *p_handle);
105
106         if (tiny_bt_send_data(ar, 6, p_handle) < 0) {
107                 printf("unable to send data\n");
108         }
109         
110         tiny_bt_disconnect(p_handle, callback_app_disconnect);
111 }
112
113 void callback_app_disconnect(__u16 *p_handle, __u8 *p_reason) 
114 {
115         printf("disconnection handle %d was completed\n", *p_handle);
116         stop = 1;
117 }
118
119
120 int main(void)
121 {
122         int ii = 30000;
123         
124         if (tiny_bt_init(0) < 0) {
125                 perror("device impossible to ititialize\n");
126                 return -1;
127         }
128         printf("begin of main\n");
129         
130         if (tiny_bt_read_bd_addr(&p_connection->master.bdaddr, callback_app_read_bd_addr) < 0) {
131                 perror("tiny_bt_read_bd_addr error\n");
132                 return -1;
133         }
134         
135         while (1) {
136                 if (stop) 
137                         return -1;
138                 //ii--;
139                 if (tiny_bt_process() < 0) {
140                         perror("process error");
141                         return -1;
142                 }
143
144         }
145         return 0;
146 }
147