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"
24 incoming_evt evt_array[MAX_CMD_STATE_REQUEST];
27 struct hci_filter nnf, oof; // the oldest and newest filters
30 int factorial(int num){
32 return num*factorial(num-1);
35 int hci_open_device(int dev_id){
36 struct sockaddr_hci address;
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
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");
56 int hci_close_dev(int dd){
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
62 incoming_evt the_oldest=evt_array[0];
65 if(!hci_filter_test_event(p_evt->evt_code,&nnf)){
68 hci_filter_set_event(p_evt->evt_code,&nnf);
70 for(i=0;i<MAX_EVT_COUNT;i++){
71 if((evt_array[i].actual_status)==0 || evt_array[i].actual_status==3 ){
77 if((evt_array[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
78 the_oldest = evt_array[i];
85 evt_array[index]=*p_evt;
92 void rutine_con_req(void){
97 void rutine_cmd_status(void){
102 int check_socket(int dd){
104 __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
106 while((len=read(dd, recbuf, sizeof(recbuf)))<0){
107 if(errno == EAGAIN || errno == EINTR)
108 printf("error in read EAGAIN or EINTR \n");
110 perror("Problem with cmd reading \n");
115 printf("Count of received bytes %d \n ",len);
117 printf("%2.2X ",recbuf[j]);
123 printf("There is nothing on socket %d \n",dd);
135 incoming_evt new_evt,*p_new_evt=&new_evt;
138 struct hci_filter pf;
139 memset(evt_array,0,(sizeof(incoming_evt)*MAX_EVT_COUNT));
143 int dd=hci_open_device(0); // only for testig 1
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);
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);
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);
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);
169 if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
170 printf("some problem with setsockopt: %d",dd);
175 if(check_socket(dd)<0){
176 printf("problem with sock checking \n");
177 setsockopt(dd, SOL_HCI, HCI_FILTER, &pf, sizeof(pf));
181 printf("I doing something %d \n",factorial(100));
185 setsockopt(dd, SOL_HCI, HCI_FILTER, &oof, sizeof(oof));