]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - testf/t2/slavetest.c
612d50120c65acb75286d0073cf3140d612e4a90
[tiny-bt.git] / testf / t2 / slavetest.c
1 /*
2 *  C Implementation: slavetest
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 <math.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <sys/poll.h>
20 #include <sys/ioctl.h>
21 #include "hcidriver.h"
22 #include "hil2cap.h"
23
24 incoming_evt evt_array[MAX_CMD_STATE_REQUEST];
25 int evt_id;
26 int global_index;
27 struct hci_filter nnf, oof; // the oldest and newest filters
28
29
30 int factorial(int num){
31         if(num==1) return 1;
32         return num*factorial(num-1);
33 }
34
35 int hci_open_device(int dev_id){
36         struct sockaddr_hci address;
37         int dd;
38
39         if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
40                 perror("socket no created");
41                 return dd; //it return -1 when socket isn't created
42         }
43
44         memset(&address,0,sizeof(address));
45         address.hci_family=AF_BLUETOOTH;
46         address.hci_dev=dev_id;
47         if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){
48                 perror("Socket not binded to hci device");
49                 close(dd);
50                 return -1;
51         }
52
53         return dd;
54 }
55
56 int hci_close_dev(int dd){
57         return close(dd);
58 }
59
60 int add_evt_toarray(incoming_evt *p_evt,struct hci_filter *p_pf,int *p_fchanged){ //fcn add the request at the end of array or replace the oldest request
61         int i,sig=0,index=0;
62         incoming_evt the_oldest=evt_array[0];
63
64
65         if(!hci_filter_test_event(p_evt->evt_code,&nnf)){
66                 *p_pf=nnf;
67                 *p_fchanged=1; 
68                 hci_filter_set_event(p_evt->evt_code,&nnf);
69         }
70         for(i=0;i<MAX_EVT_COUNT;i++){
71                 if((evt_array[i].actual_status)==0 || evt_array[i].actual_status==3 ){
72                         evt_array[i]=*p_evt;
73                         sig=1;
74                         global_index=i;
75                         break;
76                 }
77                 if((evt_array[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
78                         the_oldest = evt_array[i];
79                         index=i;
80                         
81                 }
82         }
83         if(!sig){
84                 //return -1;
85                 evt_array[index]=*p_evt;
86                 global_index=index;     
87         }
88         return 0;
89 }
90
91
92 void rutine_con_req(void){
93
94
95 }
96
97 void rutine_cmd_status(void){
98
99
100 }
101
102 int check_socket(int dd){
103         int len,j;
104         __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
105         
106         while((len=read(dd, recbuf, sizeof(recbuf)))<0){
107                         if(errno == EAGAIN || errno == EINTR)
108                                 printf("error in read EAGAIN or EINTR \n");
109                                 continue;
110                         perror("Problem with cmd reading \n");
111                         return -1;
112                 }
113         
114         if(len){
115                 printf("Count of received bytes %d \n ",len);
116                 for(j=0;j<len;j++){
117                         printf("%2.2X ",recbuf[j]);     
118                 }
119                 printf("\n");
120                 
121         }
122         else{
123                 printf("There is nothing on socket %d \n",dd);
124                 return 0;
125         }
126
127 return 0;
128 }
129
130
131
132
133 int main(void){
134
135         incoming_evt new_evt,*p_new_evt=&new_evt;
136         int fchanged=0;
137         socklen_t len;
138         struct hci_filter pf;
139         memset(evt_array,0,(sizeof(incoming_evt)*MAX_EVT_COUNT));
140         evt_id=0;
141         global_index=0;
142         
143         int dd=hci_open_device(0); // only for testig 1 
144         
145         len = sizeof(oof); //the oldest filter
146         if(getsockopt(dd, SOL_HCI, HCI_FILTER, &oof, &len)<0){
147                 printf("some problem with getsockopt: %d",dd);
148                 return -1;
149         }
150         hci_filter_clear(&nnf);
151         hci_filter_set_ptype(HCI_EVENT_PKT, &nnf);
152         //hci_filter_set_opcode(*(p_req->p_OCF_OGF), &nf);
153
154         p_new_evt->id=evt_id++;
155         p_new_evt->actual_status=PERMANENT;
156         p_new_evt->evt_code = EVT_CMD_STATUS;
157         p_new_evt->p_serv_rutine=&rutine_cmd_status;
158         add_evt_toarray(p_new_evt,&pf,&fchanged);
159                 
160         p_new_evt->id=evt_id++;
161         p_new_evt->actual_status=ESTABLISHED;
162         p_new_evt->evt_code = EVT_CONN_REQUEST;
163         p_new_evt->p_serv_rutine=&rutine_con_req;
164         add_evt_toarray(p_new_evt,&pf,&fchanged);
165                 
166         while(1){
167
168                 if(fchanged){
169                         if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
170                         printf("some problem with setsockopt: %d",dd);
171                         return -1;
172                         }
173                         fchanged=0;
174                 }
175                 if(check_socket(dd)<0){
176                         printf("problem with sock checking \n");
177                         setsockopt(dd, SOL_HCI, HCI_FILTER, &pf, sizeof(pf));
178                         break;
179                 }
180                 
181                 printf("I doing something %d \n",factorial(100));
182                                 
183
184         }
185         setsockopt(dd, SOL_HCI, HCI_FILTER, &oof, sizeof(oof));
186         hci_close_dev(dd);
187
188 return 0;
189 }