]> rtime.felk.cvut.cz Git - mirosot.git/blob - bth_tests/app/test.c
An unsucesfull attempt to clean bluetooth library and make it working.
[mirosot.git] / bth_tests / app / test.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <bth_fce_out.h>
5 #include <bth_inface.h>
6 #include <stdio.h>
7
8 /* #include <cmd_proc.h> */
9 /* #include "cmd_bth.h" */
10 /* #include "cmd_pxmc.h" */
11 #include <bth_h8s2638.h>
12
13 #include <bluetooth/bluetooth.h>
14 #include <bluetooth/hci.h>
15 #include <bluetooth/hci_lib.h>
16 #include <sys/ioctl.h>
17 #include <errno.h>
18
19 static int open_socket(int dev, unsigned long flags)
20 {
21         struct sockaddr_hci addr;
22         struct hci_filter flt;
23         struct hci_dev_info di;
24         int sk, dd, opt;
25
26         if (dev != HCI_DEV_NONE) {
27                 dd = hci_open_dev(dev);
28                 if (dd < 0) {
29                         perror("Can't open device");
30                         exit(1);
31                 }
32
33                 if (hci_devinfo(dev, &di) < 0) {
34                         perror("Can't get device info");
35                         exit(1);
36                 }
37
38                 opt = hci_test_bit(HCI_RAW, &di.flags);
39                 if (ioctl(dd, HCISETRAW, opt) < 0) {
40                         if (errno == EACCES) {
41                                 perror("Can't access device");
42                                 exit(1);
43                         }
44                 }
45
46                 hci_close_dev(dd);
47         }
48
49         /* Create HCI socket */
50         sk = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
51         if (sk < 0) {
52                 perror("Can't create raw socket");
53                 exit(1);
54         }
55
56         opt = 1;
57         if (setsockopt(sk, SOL_HCI, HCI_DATA_DIR, &opt, sizeof(opt)) < 0) {
58                 perror("Can't enable data direction info");
59                 exit(1);
60         }
61
62         opt = 1;
63         if (setsockopt(sk, SOL_HCI, HCI_TIME_STAMP, &opt, sizeof(opt)) < 0) {
64                 perror("Can't enable time stamp");
65                 exit(1);
66         }
67
68         /* Setup filter */
69         hci_filter_clear(&flt);
70         hci_filter_all_ptypes(&flt);
71         hci_filter_all_events(&flt);
72         if (setsockopt(sk, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
73                 perror("Can't set filter");
74                 exit(1);
75         }
76
77         /* Bind socket to the HCI device */
78         addr.hci_family = AF_BLUETOOTH;
79         addr.hci_dev = dev;
80         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
81                 printf("Can't attach to device hci%d. %s(%d)\n", 
82                                         dev, strerror(errno), errno);
83                 exit(1);
84         }
85
86         return sk;
87 }
88
89
90 int main()
91 {
92   int zn_bth;
93
94   printf("I'm here\n");
95
96   extern int hci_socket;
97   //hci_socket = open_socket(0, 0);
98   hci_socket = 1;
99
100   bth_init();  /* bluetooth initialization */
101
102   /*inicializace komunikacnich datovych I/O bufferu */
103   bth_inface_setup(0);
104
105   /*TPU channel 1 - inicialize (kontrola paketu + pocatecni zpozdeni)*/
106   bth_init_pkt_controll();
107
108   /*nastaveni zarizeni Bth do slave role*/
109   bth_parametr_slave();
110
111   /*kratka cas. pouza pro bth zarizeni - mazani zasilanych dat*/
112   sleep(1);
113 //  bth_start_TPU_counter();
114   bth_start();
115
116   /*nekonecna smycka obsluhujici bth, pc ...*/
117   do {
118     int ret;
119     ret = read(hci_socket, &zn_bth, sizeof(zn_bth));
120     if(ret > 0)
121     {
122       bth_recieve_packet(zn_bth);
123     };
124
125     l2cap_send_data(0, 0);
126     bth_send_queue();           /* send built packets if there are any */
127   } while(1);
128   return 0;
129 };