]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src2/testapp.c
outgoing data packet was modified. Now I can send packet and receive it on second...
[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 <unistd.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <stdint.h>
18 #include "tiny_bt_hci_core.h"
19 #include "tiny_bt_hci_cmd.h"
20 #include "testapp.h"
21
22 bt_address rem_bd_addr_array[HCI_MAX_DEV];
23 bt_address rem_addr;
24 int glob_count = 0;
25 int stop = 0;
26 connection_hci connection,*p_connection=&connection;
27
28 void callback_app_read_bd_addr(bt_address *p_address)
29 {
30         printf("Read address is :");
31         printba(p_address);
32         tiny_bt_read_local_name(&(p_connection->master.name), callback_app_read_local_name);
33 }
34
35 void callback_app_read_local_name(char *p_name)
36 {
37         int i;
38         
39         printf("read name is:");
40         
41         for (i = 0; i < 8; i++) {
42                 printf("%c", *(p_name+i));
43         }
44         printf("\n");
45         tiny_bt_inquiry(&rem_addr, callback_app_inquiry_RSSI, callback_app_inquiry_complete);
46 }
47
48 void callback_app_inquiry_RSSI(bt_address *p_address)
49 {
50         static int count = 0;
51         int i, sign = 0;
52         
53         if (count == 0) {
54                 rem_bd_addr_array[0] = *p_address;
55                 printf("There is first device:");
56                 printba(p_address);
57                 count++;
58         } else {
59                 for (i = 0; i < count; i++) {
60                         if (compare_bda(p_address, &rem_bd_addr_array[i])) {
61                                 sign = 1;
62                                 break;
63                         }
64                 }
65                 if (sign == 0) {
66                         if (count < HCI_MAX_DEV) {      
67                                 rem_bd_addr_array[count] = *p_address;
68                                 printf("There is device:");
69                                 printba(p_address);
70                                 count++;
71                         } else {
72                                 printf("Array full, there is more than 16 devices in surroundig area\n");
73                         }
74                 }
75         }
76         glob_count = count;
77         tiny_bt_inquiry_register_again(&rem_addr, callback_app_inquiry_RSSI);
78 }
79
80 void callback_app_inquiry_complete(void)
81 {
82         int i;
83         
84         printf("inquiry complete\n");
85         
86         for (i = 0; i < glob_count; i++) {
87                 printf("remote dev");
88                 printba(&rem_bd_addr_array[i]);
89         }
90         
91         if (glob_count > 0) {
92                 p_connection->slave.bdaddr = rem_bd_addr_array[0];
93                 tiny_bt_connect(&rem_bd_addr_array[0], 
94                         &p_connection->handle, callback_app_connection_complete);
95         } else {
96                 printf("There is no remote device suitable for connect\n");
97                 stop=1;
98         }
99         /*
100         tiny_bt_finish();
101         stop = 1;
102         printf("Application finished\n");
103         */
104 }
105
106 void callback_app_connection_complete(__u16 *p_handle)
107 {
108         char ar[11] = {"hallo karel"};
109         char w2[15] = {"second messagee"};    
110         
111         connection_state state = CONNECT;
112         p_connection->con_state = state;
113         printf("Connection complete handle:%d\n", *p_handle);
114
115         if (tiny_bt_send_data(ar, 11, p_handle, ACL_START, ACL_PPP) < 0) {
116                 printf("unable to send data\n");
117         }
118         
119         if (tiny_bt_send_data(w2, 15, p_handle, ACL_CONT, ACL_PPP) < 0) {
120                 printf("unable to send data\n");
121         }
122         sleep(1);
123         tiny_bt_disconnect(p_handle, callback_app_disconnect);
124 }
125
126 void callback_app_disconnect(__u16 *p_handle, __u8 *p_reason) 
127 {
128         printf("disconnection handle %d was completed\n", *p_handle);
129         tiny_bt_finish();
130         stop = 1;
131 }
132
133
134 int main(int argc, char* argv[])
135 {
136         //int ii = 30000;
137         
138         if (tiny_bt_init(atoi(argv[1])) < 0) {
139                 perror("device impossible to ititialize\n");
140                 tiny_bt_finish();
141                 exit(1);
142         }
143         printf("begin of main\n");
144         
145         if (tiny_bt_read_bd_addr(&p_connection->master.bdaddr, callback_app_read_bd_addr) < 0) {
146                 perror("tiny_bt_read_bd_addr error\n");
147                 tiny_bt_finish();
148                 exit(1);
149         }
150         
151         while (1) {
152                 if (stop) 
153                         break;
154                 if (tiny_bt_process() < 0) {
155                         perror("process error");
156                         tiny_bt_finish();
157                         stop = 1;
158                 }
159
160         }
161         printf("main finished\n");
162         return 0;
163 }
164