From: David Plotek Date: Thu, 24 Jul 2008 17:27:25 +0000 (+0200) Subject: part of master l2cap functions was implemented. Master initiate connection. Next... X-Git-Url: http://rtime.felk.cvut.cz/gitweb/tiny-bt.git/commitdiff_plain/5052f5ac1ddd2ac70ffcd62dcb0a4c334f849125 part of master l2cap functions was implemented. Master initiate connection. Next will be a slave pooling part. --- diff --git a/src/hcidriver.c b/src/hcidriver.c index 1dfa3c4..8f74f41 100644 --- a/src/hcidriver.c +++ b/src/hcidriver.c @@ -12,34 +12,28 @@ /************************** hci command functions**************************/ -int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout){ +int call_hci_inquiry_cmd(int dd, void *p_addressarray,__u16 *p_ocf_ogf){ inquiry_cp cmdp,*p_cmdp=&cmdp; hci_request req,*p_req=&req; - //hci_inquiry_complete_ev com_ev,*p_com_ev=&com_ev; - __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; - p_cmdp->lap[0]=0x33; p_cmdp->lap[1]=0x8b; p_cmdp->lap[2]=0x9e; - p_cmdp->length=5; - p_cmdp->num_rsp=5; + p_cmdp->length=INQ_TIME_14s40; + p_cmdp->num_rsp=NUM_RSP_20; memset(p_req,0,sizeof(req)); - assemble_ocf_ogf(0x01,0x01,p_OCF_OGF); - p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_OCF_OGF=p_ocf_ogf; p_req->p_retcmdp=p_addressarray; p_req->p_cmdp=p_cmdp; - p_req->retcmdp_len=INQUIRY_INFO_SIZE; + p_req->retcmdp_len=0; p_req->cmdp_len=INQUIRY_CP_SIZE; - p_req->event=EVT_INQUIRY_RESULT; + p_req->event=EVT_INQUIRY_COMPLETE; - if(hci_send_request(dd,p_req,timeout)<0) + if(hci_send_request(dd,p_req,INQ_TIME_4s24*1280)<0) return -1; -// if(p_com_ev->status) -// return -1; - - - return 0; + + + return p_req->retcmdp_len; } // fill up the input parameter address pointer by one address int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address,__u16 *p_ocf_ogf){ @@ -53,7 +47,7 @@ int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address,__u16 *p_ocf_ogf){ p_req->cmdp_len=0; p_req->event=EVT_CMD_COMPLETE; - if(hci_send_request(dd,p_req,1000)<0) + if(hci_send_request(dd,p_req,2000)<0) return -1; if(p_cmdp->status) return -1; @@ -82,11 +76,11 @@ int call_hci_read_local_name_cmd(int dd, void *p_name,__u16 *p_ocf_ogf){ } -int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout){ +int call_hci_create_connection_cmd(int dd, bt_address *p_address, __u16 *p_ocf_ogf){ create_conn_cp cmdp,*p_cmdp=&cmdp; hci_request req,*p_req=&req; - __u16 OCF_OGF,*p_OCF_OGF=&OCF_OGF; - + evt_conn_complete cc_evp,*p_cc_evp=&cc_evp; + p_cmdp->bdaddr=*p_address; p_cmdp->pkt_type=0x0010; p_cmdp->pscan_rep_mode=0x01; @@ -94,18 +88,18 @@ int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout){ p_cmdp->clock_offset=0xf000; p_cmdp->role_switch=0x00; memset(p_req,0,sizeof(req)); - assemble_ocf_ogf(0x05,0x01,p_OCF_OGF); - p_req->p_OCF_OGF=p_OCF_OGF; + p_req->p_OCF_OGF=p_ocf_ogf; p_req->p_cmdp=p_cmdp; p_req->cmdp_len=CREATE_CONN_CP_SIZE; + p_req->p_retcmdp=p_cc_evp; + p_req->retcmdp_len=EVT_CONN_COMPLETE_SIZE; p_req->event=EVT_CONN_COMPLETE; - if(hci_send_request(dd,p_req,timeout)<0) + if(hci_send_request(dd,p_req,2000)<0) return -1; - //if(p_com_ev->status) - // return -1; - - return 0; + + return p_cc_evp->handle; + } @@ -138,9 +132,7 @@ int hci_close_dev(int dd){ } int hci_send_command(int dd, hci_request *p_req){ - __u8 array[p_req->cmdp_len+4]; //type + OCF+OGF+plen - //__u16 correct_opcode,*p_cor_opcode=&correct_opcode; - //correct_opcode=swap_2_bytes(*(p_req->p_OCF_OGF)); + __u8 array[p_req->cmdp_len+4]; int ii; array[0]=0x01; memcpy(&array[1],p_req->p_OCF_OGF,2); @@ -170,10 +162,7 @@ int hci_send_request(int dd, hci_request *p_req,int timeout){ socklen_t len; hci_event_hdr *p_hdr; struct hci_filter nf, of; - int try_count, sign=0;; - - - + int try_count; len = sizeof(of); if(getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len)<0){ @@ -184,6 +173,7 @@ int hci_send_request(int dd, hci_request *p_req,int timeout){ 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_set_opcode(*(p_req->p_OCF_OGF), &nf); @@ -197,15 +187,17 @@ int hci_send_request(int dd, hci_request *p_req,int timeout){ goto fail; } - try_count= 10; + try_count= 30; while(try_count--){ + int sign=0; printf("try %d \n",try_count); evt_cmd_complete *cc; evt_cmd_status *cs; evt_remote_name_req_complete *rnrc; remote_name_req_cp *cpar; inquiry_info_with_rssi *p_res_ev; + bt_address *p_actual; if(timeout){ struct pollfd p; @@ -275,18 +267,7 @@ int hci_send_request(int dd, hci_request *p_req,int timeout){ memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); printf("retcmdp lenght: %d \n",p_req->retcmdp_len); goto succes; - // __u8 first,*p_first; - // p_first=&first; - // for(j=0;jretcmdp_len;j++){ - // memcpy(p_first,p_req->p_retcmdp+j,1); - // printf("%d. byte is: %X \n",j,first); - // printf(" %X",*((unsigned int *)p_req->p_retcmdp+j)); - //printf("address of p_req: %d \n",p_req); - //printf("address of p_req: %d \n",p_req->p_retcmdp); - // } - // printf("Ok\n"); - - + case EVT_REMOTE_NAME_REQ_COMPLETE: if(p_hdr->evt != p_req->event) break; @@ -300,44 +281,58 @@ int hci_send_request(int dd, hci_request *p_req,int timeout){ memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); goto succes; - case EVT_INQUIRY_RESULT: - printf("bingo inquiry result event \n"); - break; - //goto succes; - -/* case EVT_INQUIRY_RESULT_WITH_RSSI: + case EVT_INQUIRY_RESULT_WITH_RSSI: printf("bingo inquiry result RSSI event \n"); + p_recbuf = recbuf + (1 + HCI_EVENT_HDR_SIZE + 1);// one byte set forward p_res_ev = (void *) p_recbuf; - p_actual = &(p_res_ev->ba_array); - for(j=0;jp_retcmdp+(j*sizeof(bt_address)))) == 1){ - sign=1; - } //im looking for all array members an compare with actual address - } - if(!sign){ // if in array address doesnt exist i will add it to array - *(bt_address*)(p_req->p_retcmdp+(count*sizeof(bt_address)))= *p_actual; - count++; - } - if(count==0){ + p_actual = &(p_res_ev->bdaddr); + //todo:involve signal strength + if(count==0){ //array is empty *(bt_address*)(p_req->p_retcmdp)= *p_actual; count++; } + else{ + for(j=0;jp_retcmdp+(j*sizeof(bt_address)))) == 1){ + sign=1; + } //im looking for all array members an compare with actual address + } + if(!sign){ // if in array address doesnt exist i will add it to array + *(bt_address*)(p_req->p_retcmdp+(count*sizeof(bt_address)))= *p_actual; + count++; + } + } //printf("type %2.2X \n",((hci_inquiry_result_RSSI_ev *)p_req->p_retcmdp)->RSSI_array); break; //goto succes; -*/ + case EVT_INQUIRY_COMPLETE: + if(p_hdr->evt != p_req->event) + break; + if(*p_recbuf != 0) + goto fail; printf("bingo inquiry complete event \n"); + p_req->retcmdp_len=count; //count of diferent remote decices + goto succes; - - default: if(p_hdr->evt != p_req->event) + case EVT_CONN_COMPLETE: + if(p_hdr->evt != p_req->event) + break; + if(*p_recbuf != 0) + goto fail; + printf("bingo two devices were connected"); + p_req->retcmdp_len = min(len, p_req->retcmdp_len); + memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); + goto succes; + + default: + if(p_hdr->evt != p_req->event) break; p_req->retcmdp_len = min(len, p_req->retcmdp_len); memcpy(p_req->p_retcmdp, p_recbuf, p_req->retcmdp_len); goto succes; - } diff --git a/src/hcidriver.h b/src/hcidriver.h index 5a1704f..8002b68 100644 --- a/src/hcidriver.h +++ b/src/hcidriver.h @@ -36,6 +36,17 @@ #define SOL_SCO 17 #define SOL_RFCOMM 18 +#define INQ_TIME_1s28 1 +#define INQ_TIME_2s56 2 +#define INQ_TIME_4s24 3 +#define INQ_TIME_5s52 4 +#define INQ_TIME_7s20 5 +#define INQ_TIME_14s40 10 + +#define NUM_RSP_5 5 +#define NUM_RSP_10 10 +#define NUM_RSP_20 20 + #define max(a, b) ((a) > (b) ? (a) : (b)) #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -70,10 +81,10 @@ typedef struct{ /* next function structures*/ -extern int call_hci_inquiry_cmd(int dd, bt_address *p_addressarray,int timeout); +extern int call_hci_inquiry_cmd(int dd, void *p_addressarray,__u16 *p_ocf_ogf); extern int call_hci_read_bd_addr_cmd(int dd,bt_address *p_address,__u16 *p_ocf_ogf); extern int call_hci_read_local_name_cmd(int dd, void *p_name,__u16 *p_ocf_ogf); -extern int call_hci_create_connection_cmd(int dd, bt_address *p_address, int timeout); +extern int call_hci_create_connection_cmd(int dd, bt_address *p_address, __u16 *p_ocf_ogf); extern int hci_open_device(int dev_id); extern int hci_send_command(int dd, hci_request *p_req); extern int hci_send_request(int dd, hci_request *p_req,int timeout); diff --git a/src/hciembeded.h b/src/hciembeded.h index 2a5c9cd..af4cd23 100644 --- a/src/hciembeded.h +++ b/src/hciembeded.h @@ -1499,6 +1499,7 @@ typedef struct { } __attribute__ ((packed)) evt_flow_spec_complete; #define EVT_FLOW_SPEC_COMPLETE_SIZE (5 + HCI_QOS_CP_SIZE) +//todo:this solution is for number of responcies equal to 1 only!!! #define EVT_INQUIRY_RESULT_WITH_RSSI 0x22 typedef struct { bt_address bdaddr; diff --git a/src/hil2cap.c b/src/hil2cap.c index 8162cc9..02a4a44 100644 --- a/src/hil2cap.c +++ b/src/hil2cap.c @@ -25,21 +25,17 @@ int global_index; -int create_master_connection(struct hci_dev_info *master){//establish connection and send a coutn of bytes every x second +int create_master_connection(connection_hci *p_connection){//establish connection and send a coutn of bytes every x second //array of open request which are in waiting state for appropriate event or answer - connection_hci connection,*p_connection=&connection; p_connection->con_id=0; p_connection->con_state=DISCONNECTED; + p_connection->socket_fd=hci_open_device(p_connection->master.dev_id); + if(l2cap_call_cmd(CREATE_CONNECTION_CMD_OP,p_connection->socket_fd,p_connection)<0){ + printf("connection problem \n"); + return -1; + } - while(1){ - if(!p_connection->con_state){ //there is no connection, create it - - } - - - - } return 0; } @@ -76,6 +72,7 @@ int show_all_local_dev(struct hci_dev_info *master){ // and choose a master devi printf("Error in call hci bd addr cmd \n"); return -1; } + //hci_close_dev(dev_array[i].dev_id); } *master = dev_array[0]; //I will take a first dev like a master @@ -88,18 +85,31 @@ int show_all_local_dev(struct hci_dev_info *master){ // and choose a master devi } +int show_all_remote_dev(struct hci_dev_info *p_master, bt_address *p_remadrar, int *p_remd_count){ + int dd=hci_open_device(p_master->dev_id); + + if((*p_remd_count=l2cap_call_cmd(INQUIRY_CMD_OP, dd, p_remadrar))< 0){ + printf("Error in call inquiry command \n"); + return -1; + } + return 0; + +} + int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){ cmd_state_request new_req,*p_new_req=&new_req; __u16 sw_opcode; - sw_opcode=swap_2_bytes(ocf_ogf); + int remd_count; + sw_opcode=swap_2_bytes(ocf_ogf); + p_new_req->actual_status=ESTABLISHED; + p_new_req->id=cmd_req_id++; + p_new_req->req_opcode = ocf_ogf; + switch(ocf_ogf){ - case READ_BD_ADDR_CMD_OP:{ - p_new_req->actual_status=ESTABLISHED; - p_new_req->id=cmd_req_id++; - p_new_req->req_opcode = ocf_ogf; + case READ_BD_ADDR_CMD_OP: p_new_req->evt_code = EVT_CMD_COMPLETE; - p_new_req->p_serv_rutine=rutine_catch_bd_addr; + p_new_req->p_serv_rutine=NULL; if(req_fcn_add(p_new_req)<0){ printf("Mistake with req array adding \n "); @@ -113,14 +123,9 @@ int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){ req_fcn_remove(); return 0; - - } - case READ_LOCAL_NAME_CMD_OP:{ - p_new_req->actual_status=ESTABLISHED; - p_new_req->id=cmd_req_id++; - p_new_req->req_opcode = ocf_ogf; + case READ_LOCAL_NAME_CMD_OP: p_new_req->evt_code = EVT_CMD_COMPLETE; - p_new_req->p_serv_rutine=rutine_catch_bd_addr; + p_new_req->p_serv_rutine=NULL; if(req_fcn_add(p_new_req)<0){ printf("Mistake with req array adding \n "); @@ -132,7 +137,38 @@ int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){ } req_fcn_remove(); return 0; - } + + case INQUIRY_CMD_OP: + + p_new_req->evt_code = EVT_INQUIRY_COMPLETE; + p_new_req->p_serv_rutine=NULL; + + if(req_fcn_add(p_new_req)<0){ + printf("Mistake with req array adding \n "); + return -1; + } + if((remd_count=call_hci_inquiry_cmd(dd,p_param,&sw_opcode)) < 0){ + printf("Mistake with inquiry cmd \n "); + return -1; + } + req_fcn_remove(); + return remd_count; + + case CREATE_CONNECTION_CMD_OP: + p_new_req->evt_code = EVT_CONN_COMPLETE; + p_new_req->p_serv_rutine=NULL; + + if(req_fcn_add(p_new_req)<0){ + printf("Mistake with req array adding \n "); + return -1; + } + if((((connection_hci *)p_param)->handle=call_hci_create_connection_cmd(dd, &(((connection_hci *)p_param)->slave.bdaddr), &sw_opcode)) < 0){ + printf("Mistake with inquiry cmd \n "); + return -1; + } + ((connection_hci *)p_param)->con_state=CONNECTED; + req_fcn_remove(); + return 0; } return 0; @@ -177,6 +213,10 @@ return (void*)0; int main(void){ struct hci_dev_info master,*p_master=&master; + bt_address remote_bd_addr_array[HCI_MAX_DEV]; + memset(remote_bd_addr_array,0,(sizeof(bt_address)*HCI_MAX_DEV)); + connection_hci connection,*p_connection=&connection; + int remd_count,i; cmd_req_id=0; global_index=0; memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST)); @@ -184,22 +224,22 @@ int main(void){ printf("error no dev \n"); return -1; } + if(show_all_remote_dev(p_master, remote_bd_addr_array, &remd_count)<0){ + printf("error no remote dev \n"); + return -1; + } + printf("All remote devices:\n"); + for(i=0;imaster=master; + p_connection->slave.bdaddr=remote_bd_addr_array[0]; - - - + if(create_master_connection(p_connection)<0){ + printf("connection error"); + return -1; + } + return 0; } - -//void call_hci_send_data(unsigned short con_handle, __u8 pb_flag, __u8 b_flag, unsigned short data_lenght) - -//void call_hci_create_connection(bt_address *dev_adress,unsigned short pkt_type,__u8 sc_mod){// - -//} - -//Socket functions -/* */ - - - diff --git a/src/hil2cap.h b/src/hil2cap.h index 8f47a3c..1434a4e 100644 --- a/src/hil2cap.h +++ b/src/hil2cap.h @@ -19,8 +19,7 @@ /* Connection states */ #define DISCONNECTED 0 -#define CONNECTED 1 -#define PENDING 2 +#define CONNECTED 1 /* CMD request states*/ #define FREE 0 @@ -46,20 +45,25 @@ typedef struct{ __u8 actual_status; __u8 id; __u16 req_opcode; - __u16 evt_code; // code of expected ansver event , command status event will be accepted everytime - void *p_serv_rutine; + __u16 evt_code;//codes of expected ansver event,command status event will be accepted everytime + void (*p_serv_rutine)(void); } __attribute__((packed)) cmd_state_request; + typedef struct{ __u16 con_id; __u8 con_state; + struct hci_dev_info master; + struct hci_dev_info slave; + int handle; int socket_fd; }connection_hci; -extern int create_master_connection(struct hci_dev_info *master); +extern int create_master_connection(connection_hci *p_connection); extern int show_all_local_dev(struct hci_dev_info *master); +extern int show_all_remote_dev(struct hci_dev_info *master, bt_address *p_remadrar,int *p_remd_count); extern int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param); diff --git a/testf/t1/rectest b/testf/t1/rectest new file mode 100755 index 0000000..49e0e35 Binary files /dev/null and b/testf/t1/rectest differ diff --git a/testf/t2/slavetest.c b/testf/t2/slavetest.c new file mode 100644 index 0000000..f1c1ac2 --- /dev/null +++ b/testf/t2/slavetest.c @@ -0,0 +1,80 @@ +/* +* C Implementation: slavetest +* +* Description: +* +* +* Author: root , (C) 2008 +* +* Copyright: See COPYING file that comes with this distribution +* +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "hcidriver.h" + +cmd_state_request array_req[MAX_CMD_STATE_REQUEST]; +int cmd_req_id; +int global_index; + +int req_fcn_add(cmd_state_request *p_req){ //fcn add the request at the end of array or replace the oldest request + int i,sig=0,index=0; + cmd_state_request the_oldest=array_req[0]; + + for(i=0;iid=cmd_req_id++; + //p_new_req->req_opcode = ocf_ogf; + p_new_req->evt_code = EVT_CONN_REQUEST; + p_new_req->p_serv_rutine=&rutine_con_req; + + + + while(1){ + + + } + +return 0; +}