#include <sys/poll.h>
#include <sys/ioctl.h>
#include "hcidriver.h"
+#include "hil2cap.h"
incoming_evt evt_array[MAX_CMD_STATE_REQUEST];
int evt_id;
int global_index;
+struct hci_filter nnf, oof; // the oldest and newest filters
-int add_evt_toarray(incoming_evt *p_evt){ //fcn add the request at the end of array or replace the oldest request
+
+int factorial(int num){
+ if(num==1) return 1;
+ return num*factorial(num-1);
+}
+
+int hci_open_device(int dev_id){
+ struct sockaddr_hci address;
+ int dd;
+
+ if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
+ perror("socket no created");
+ return dd; //it return -1 when socket isn't created
+ }
+
+ memset(&address,0,sizeof(address));
+ address.hci_family=AF_BLUETOOTH;
+ address.hci_dev=dev_id;
+ if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){
+ perror("Socket not binded to hci device");
+ close(dd);
+ return -1;
+ }
+
+ return dd;
+}
+
+int hci_close_dev(int dd){
+ return close(dd);
+}
+
+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
int i,sig=0,index=0;
incoming_evt the_oldest=evt_array[0];
-
+
+
+ if(!hci_filter_test_event(p_evt->evt_code,&nnf)){
+ *p_pf=nnf;
+ *p_fchanged=1;
+ hci_filter_set_event(p_evt->evt_code,&nnf);
+ }
for(i=0;i<MAX_EVT_COUNT;i++){
if((evt_array[i].actual_status)==0 || evt_array[i].actual_status==3 ){
evt_array[i]=*p_evt;
}
if(!sig){
//return -1;
- evt_array[index]=*p_req;
+ evt_array[index]=*p_evt;
global_index=index;
}
return 0;
}
-void rutine_con_req(void *arg){
+
+void rutine_con_req(void){
}
-void rutine_cmd_status(void *arg){
+void rutine_cmd_status(void){
}
-int check_socket(int *p_dd){
-
+int check_socket(int dd){
+ int len,j;
+ __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
+
+ while((len=read(dd, recbuf, sizeof(recbuf)))<0){
+ if(errno == EAGAIN || errno == EINTR)
+ printf("error in read EAGAIN or EINTR \n");
+ continue;
+ perror("Problem with cmd reading \n");
+ return -1;
+ }
+
+ if(len){
+ printf("Count of received bytes %d \n ",len);
+ for(j=0;j<len;j++){
+ printf("%2.2X ",recbuf[j]);
+ }
+ printf("\n");
+
+ }
+ else{
+ printf("There is nothing on socket %d \n",dd);
+ return 0;
+ }
return 0;
}
int main(void){
incoming_evt new_evt,*p_new_evt=&new_evt;
- struct hci_filter nf, of;
- int len;
+ int fchanged=0;
+ socklen_t len;
+ struct hci_filter pf;
memset(evt_array,0,(sizeof(incoming_evt)*MAX_EVT_COUNT));
evt_id=0;
global_index=0;
- p_new_evt->id=evt_id++;
- p_new_evt->actual_status=PERMANENT;
- p_new_req->evt_code = EVT_CMD_STATUS;
- p_new_req->p_serv_rutine=&rutine_cmd_status;
-
+ int dd=hci_open_device(0); // only for testig 1
- int dd=hci_open_device(1); // only for testig 1
- add_evt_toarray(p_new_evt);
- p_new_evt->id=evt_id++;
- p_new_evt->actual_status=ESTABLISHED;
- p_new_req->evt_code = EVT_CONN_REQUEST;
- p_new_req->p_serv_rutine=&rutine_con_req;
-
- len = sizeof(of);
- if(getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len)<0){
+ len = sizeof(oof); //the oldest filter
+ if(getsockopt(dd, SOL_HCI, HCI_FILTER, &oof, &len)<0){
printf("some problem with getsockopt: %d",dd);
return -1;
}
-
- hci_filter_clear(&nf);
- hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
- hci_filter_set_event(EVT_CMD_STATUS, &nf);
- //hci_filter_set_event(EVT_INQUIRY_RESULT_WITH_RSSI, &nf);
- //hci_filter_set_event(p_req->event, &nf);
+ hci_filter_clear(&nnf);
+ hci_filter_set_ptype(HCI_EVENT_PKT, &nnf);
//hci_filter_set_opcode(*(p_req->p_OCF_OGF), &nf);
-
- if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf))<0){
- printf("some problem with setsockopt: %d",dd);
- return -1;
- }
-
+
+ p_new_evt->id=evt_id++;
+ p_new_evt->actual_status=PERMANENT;
+ p_new_evt->evt_code = EVT_CMD_STATUS;
+ p_new_evt->p_serv_rutine=&rutine_cmd_status;
+ add_evt_toarray(p_new_evt,&pf,&fchanged);
+
+ p_new_evt->id=evt_id++;
+ p_new_evt->actual_status=ESTABLISHED;
+ p_new_evt->evt_code = EVT_CONN_REQUEST;
+ p_new_evt->p_serv_rutine=&rutine_con_req;
+ add_evt_toarray(p_new_evt,&pf,&fchanged);
+
while(1){
+
+ if(fchanged){
+ if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
+ printf("some problem with setsockopt: %d",dd);
+ return -1;
+ }
+ fchanged=0;
+ }
if(check_socket(dd)<0){
printf("problem with sock checking \n");
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &pf, sizeof(pf));
break;
}
-
+ printf("I doing something %d \n",factorial(100));
+
}
- setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &oof, sizeof(oof));
hci_close_dev(dd);
return 0;