2 * C Implementation: slavetest
7 * Author: root <root@ubuntu>, (C) 2008
9 * Copyright: See COPYING file that comes with this distribution
20 #include <sys/ioctl.h>
21 #include "hcidriver.h"
23 #include "slavetest.h"
25 incoming_evt evt_array[MAX_CMD_STATE_REQUEST];
28 struct hci_filter nnf, oof; // the oldest and newest filters
29 connection_hci connection,*p_connection=&connection;
32 int factorial(int num){
34 return num*factorial(num-1);
37 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
39 incoming_evt the_oldest=evt_array[0];
42 if(!hci_filter_test_event(p_evt->evt_code,&nnf)){
45 hci_filter_set_event(p_evt->evt_code,&nnf);
47 for(i=0;i<MAX_EVT_COUNT;i++){
48 if((evt_array[i].actual_status)==0 || evt_array[i].actual_status==3 ){
54 if((evt_array[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
55 the_oldest = evt_array[i];
62 evt_array[index]=*p_evt;
68 int look_up_inarray(__u8 evt_code){
69 int i,index=-1,oldestid=65535;
71 if(evt_array[global_index].evt_code == evt_code && evt_array[global_index].actual_status != DONE){
75 for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
76 if(evt_array[i].evt_code == evt_code && evt_array[i].actual_status != DONE){
77 if(evt_array[i].id<oldestid){
78 oldestid=evt_array[i].id;
87 int rutine_con_req(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
89 incoming_evt new_evt,*p_new_evt=&new_evt;
90 evt_conn_request *p_evt;
92 p_evt = (void *) p_recbuf;
94 p_connection->master.bdaddr=p_evt->bdaddr;
96 p_new_evt->id=evt_id++;
97 p_new_evt->actual_status=ESTABLISHED;
98 p_new_evt->evt_code = EVT_CONN_COMPLETE;
99 p_new_evt->p_serv_rutine=&rutine_connection_complete;
100 add_evt_toarray(p_new_evt,p_pf,p_fchanged);
102 sw_opcode=swap_2_bytes(ACCEPT_CONNECTION_REQ_OP);
103 hci_filter_set_opcode(sw_opcode, &nnf);
105 if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
106 printf("some problem with setsockopt: %d",dd);
112 if(call_hci_accept_conn_req_cmd(dd, &p_evt->bdaddr, &sw_opcode)<0){
113 printf("error with accepting connection\n");
116 printf("accept connection command was sended\n");
120 int rutine_cmd_status(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
121 evt_cmd_status *p_evt;
122 p_evt = (void *) p_recbuf;
125 printf("command status error\n");
129 printf("command status with opcode: %d",p_evt->opcode);
134 int rutine_connection_complete(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
136 incoming_evt new_evt,*p_new_evt=&new_evt;
137 evt_conn_complete *p_evt;
138 p_evt = (void *) p_recbuf;
141 printf("connection was not setted\n");
144 p_connection->con_state=CONNECTED;
145 p_connection->handle=p_evt->handle;
146 printf("connection was setted: %d\n",p_connection->handle);
148 p_new_evt->id=evt_id++;
149 p_new_evt->actual_status=ESTABLISHED;
150 p_new_evt->evt_code = EVT_DISCONN_COMPLETE;
151 p_new_evt->p_serv_rutine=&rutine_disconnect;
152 add_evt_toarray(p_new_evt,p_pf,p_fchanged);
154 sw_opcode=swap_2_bytes(ACCEPT_CONNECTION_REQ_OP);
157 hci_filter_clear_event(EVT_CONN_REQUEST,&nnf);
158 hci_filter_clear_opcode(&nnf);
165 int rutine_disconnect(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
166 evt_disconn_complete *p_evt;
167 incoming_evt new_evt,*p_new_evt=&new_evt;
169 p_evt = (void *) p_recbuf;
171 printf("disconnection error: %d\n",p_evt->status);
176 hci_filter_clear_event(EVT_CONN_COMPLETE,&nnf);
178 p_connection->con_state=DISCONNECTED;
179 printf("connection was canceled: %d\n",p_connection->handle);
180 printf("disconnection reason: %d\n",p_evt->reason);
182 p_new_evt->id=evt_id++;
183 p_new_evt->actual_status=ESTABLISHED;
184 p_new_evt->evt_code = EVT_CONN_REQUEST;
185 p_new_evt->p_serv_rutine=&rutine_con_req;
186 add_evt_toarray(p_new_evt,p_pf,p_fchanged);
191 int check_socket(int dd,struct hci_filter *p_pf, int *p_fchanged){
193 hci_event_hdr *p_hdr;
194 __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
196 while((len=read(dd, recbuf, sizeof(recbuf)))<0){
201 perror("Problem with cmd reading \n");
206 printf("Count of received bytes %d \n ",len);
208 printf("%2.2X ",recbuf[j]);
211 if(recbuf[0]==4){ // konstanty
212 p_hdr = (void *) (recbuf + 1);
213 p_recbuf = (void *) (recbuf + (1 + HCI_EVENT_HDR_SIZE));
214 if((index=look_up_inarray(p_hdr->evt))<0){
215 printf("There is no awaiting evt like this\n");
219 if(evt_array[index].p_serv_rutine(p_recbuf, dd, p_pf, p_fchanged)<0){
220 printf("error with service rutine");
223 if(evt_array[index].actual_status != PERMANENT){
224 evt_array[index].actual_status = DONE;
229 else if(recbuf[0]==2){
232 else return 0; // 0 or -1
236 printf("There is nothing on socket %d \n",dd);
248 incoming_evt new_evt,*p_new_evt=&new_evt;
251 struct hci_filter pf;
252 memset(evt_array,0,(sizeof(incoming_evt)*MAX_EVT_COUNT));
255 p_connection->con_id=0;
256 p_connection->con_state=DISCONNECTED;
258 int dd=hci_open_device_nonblock(0); // only for testig 1
260 len = sizeof(oof); //the oldest filter
261 if(getsockopt(dd, SOL_HCI, HCI_FILTER, &oof, &len)<0){
262 printf("some problem with getsockopt: %d",dd);
265 hci_filter_clear(&nnf);
266 hci_filter_set_ptype(HCI_EVENT_PKT, &nnf);
267 //hci_filter_set_opcode(*(p_req->p_OCF_OGF), &nf);
269 p_new_evt->id=evt_id++;
270 p_new_evt->actual_status=PERMANENT;
271 p_new_evt->evt_code = EVT_CMD_STATUS;
272 p_new_evt->p_serv_rutine=&rutine_cmd_status;
273 add_evt_toarray(p_new_evt,&pf,&fchanged);
275 p_new_evt->id=evt_id++;
276 p_new_evt->actual_status=ESTABLISHED;
277 p_new_evt->evt_code = EVT_CONN_REQUEST;
278 p_new_evt->p_serv_rutine=&rutine_con_req;
279 add_evt_toarray(p_new_evt,&pf,&fchanged);
284 if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
285 printf("some problem with setsockopt: %d",dd);
290 if(check_socket(dd, &pf, &fchanged)<0){
291 printf("problem with sock checking \n");
292 setsockopt(dd, SOL_HCI, HCI_FILTER, &pf, sizeof(pf));
298 setsockopt(dd, SOL_HCI, HCI_FILTER, &oof, sizeof(oof));