--- /dev/null
+/*
+* C Implementation: slavetest
+*
+* Description:
+*
+*
+* Author: root <root@ubuntu>, (C) 2008
+*
+* Copyright: See COPYING file that comes with this distribution
+*
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/poll.h>
+#include <sys/ioctl.h>
+#include "hcidriver.h"
+#include "hil2cap.h"
+#include "slavetest.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
+connection_hci connection,*p_connection=&connection;
+
+
+int factorial(int num){
+ if(num==1) return 1;
+ return num*factorial(num-1);
+}
+
+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;
+ sig=1;
+ global_index=i;
+ break;
+ }
+ if((evt_array[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
+ the_oldest = evt_array[i];
+ index=i;
+
+ }
+ }
+ if(!sig){
+
+ evt_array[index]=*p_evt;
+ global_index=index;
+ }
+ return 0;
+}
+
+int look_up_inarray(__u8 evt_code){
+ int i,index=-1,oldestid=65535;
+
+ if(evt_array[global_index].evt_code == evt_code && evt_array[global_index].actual_status != DONE){
+ return global_index;
+ }
+ else{
+ for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
+ if(evt_array[i].evt_code == evt_code && evt_array[i].actual_status != DONE){
+ if(evt_array[i].id<oldestid){
+ oldestid=evt_array[i].id;
+ index=i;
+ }
+ }
+ }
+ return index;
+ }
+}
+
+int rutine_con_req(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
+ __u16 sw_opcode;
+ incoming_evt new_evt,*p_new_evt=&new_evt;
+ evt_conn_request *p_evt;
+
+ p_evt = (void *) p_recbuf;
+
+ p_connection->master.bdaddr=p_evt->bdaddr;
+
+ p_new_evt->id=evt_id++;
+ p_new_evt->actual_status=ESTABLISHED;
+ p_new_evt->evt_code = EVT_CONN_COMPLETE;
+ p_new_evt->p_serv_rutine=&rutine_connection_complete;
+ add_evt_toarray(p_new_evt,p_pf,p_fchanged);
+
+ sw_opcode=swap_2_bytes(ACCEPT_CONNECTION_REQ_OP);
+ hci_filter_set_opcode(sw_opcode, &nnf);
+ if(*p_fchanged){
+ if(setsockopt(dd, SOL_HCI, HCI_FILTER, &nnf, sizeof(nnf))<0){
+ printf("some problem with setsockopt: %d",dd);
+ return -1;
+ }
+ *p_fchanged=0;
+ }
+
+ if(call_hci_accept_conn_req_cmd(dd, &p_evt->bdaddr, &sw_opcode)<0){
+ printf("error with accepting connection\n");
+ return -1;
+ }
+ printf("accept connection command was sended\n");
+ return 0;
+}
+
+int rutine_cmd_status(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
+ evt_cmd_status *p_evt;
+ p_evt = (void *) p_recbuf;
+
+ if(p_evt->status){
+ printf("command status error\n");
+ return -1;
+ }
+
+ printf("command status with opcode: %d",p_evt->opcode);
+ return 0;
+
+}
+
+int rutine_connection_complete(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
+ __u16 sw_opcode;
+ incoming_evt new_evt,*p_new_evt=&new_evt;
+ evt_conn_complete *p_evt;
+ p_evt = (void *) p_recbuf;
+
+ if(p_evt->status){
+ printf("connection was not setted\n");
+ return -1;
+ }
+ p_connection->con_state=CONNECTED;
+ p_connection->handle=p_evt->handle;
+ printf("connection was setted: %d\n",p_connection->handle);
+
+ p_new_evt->id=evt_id++;
+ p_new_evt->actual_status=ESTABLISHED;
+ p_new_evt->evt_code = EVT_DISCONN_COMPLETE;
+ p_new_evt->p_serv_rutine=&rutine_disconnect;
+ add_evt_toarray(p_new_evt,p_pf,p_fchanged);
+
+ sw_opcode=swap_2_bytes(ACCEPT_CONNECTION_REQ_OP);
+ *p_pf=nnf;
+ *p_fchanged=1;
+ hci_filter_clear_event(EVT_CONN_REQUEST,&nnf);
+ hci_filter_clear_opcode(&nnf);
+
+
+
+ return 0;
+}
+
+int rutine_disconnect(void *p_recbuf, int dd, struct hci_filter *p_pf, int *p_fchanged){
+ evt_disconn_complete *p_evt;
+ incoming_evt new_evt,*p_new_evt=&new_evt;
+
+ p_evt = (void *) p_recbuf;
+ if(p_evt->status){
+ printf("disconnection error: %d\n",p_evt->status);
+ return -1;
+ }
+ *p_pf=nnf;
+ *p_fchanged=1;
+ hci_filter_clear_event(EVT_CONN_COMPLETE,&nnf);
+
+ p_connection->con_state=DISCONNECTED;
+ printf("connection was canceled: %d\n",p_connection->handle);
+ printf("disconnection reason: %d\n",p_evt->reason);
+
+ 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,p_pf,p_fchanged);
+
+ return 0;
+}
+
+int check_socket(int dd,struct hci_filter *p_pf, int *p_fchanged){
+ int len,j,index;
+ hci_event_hdr *p_hdr;
+ __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
+
+ while((len=read(dd, recbuf, sizeof(recbuf)))<0){
+ if(errno == EINTR)
+ continue;
+ if(errno == EAGAIN)
+ return 0;
+ 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");
+ if(recbuf[0]==4){ // konstanty
+ p_hdr = (void *) (recbuf + 1);
+ p_recbuf = (void *) (recbuf + (1 + HCI_EVENT_HDR_SIZE));
+ if((index=look_up_inarray(p_hdr->evt))<0){
+ printf("There is no awaiting evt like this\n");
+ return -1;
+ }
+ else{
+ if(evt_array[index].p_serv_rutine(p_recbuf, dd, p_pf, p_fchanged)<0){
+ printf("error with service rutine");
+ return -1;
+ }
+ if(evt_array[index].actual_status != PERMANENT){
+ evt_array[index].actual_status = DONE;
+ }
+ }
+ return 1;
+ }
+ else if(recbuf[0]==2){
+
+ }
+ else return 0; // 0 or -1
+
+ }
+ 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;
+ 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_connection->con_id=0;
+ p_connection->con_state=DISCONNECTED;
+
+ int dd=hci_open_device_nonblock(0); // only for testig 1
+
+ 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(&nnf);
+ hci_filter_set_ptype(HCI_EVENT_PKT, &nnf);
+ //hci_filter_set_opcode(*(p_req->p_OCF_OGF), &nf);
+
+ 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, &pf, &fchanged)<0){
+ printf("problem with sock checking \n");
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &pf, sizeof(pf));
+ continue;
+ }
+
+
+ }
+ setsockopt(dd, SOL_HCI, HCI_FILTER, &oof, sizeof(oof));
+ hci_close_dev(dd);
+
+return 0;
+}
+
+