]> rtime.felk.cvut.cz Git - tiny-bt.git/blobdiff - testf/t4/tiny_bt_hci_cmd.c
new version of callback functions and new was added
[tiny-bt.git] / testf / t4 / tiny_bt_hci_cmd.c
diff --git a/testf/t4/tiny_bt_hci_cmd.c b/testf/t4/tiny_bt_hci_cmd.c
new file mode 100644 (file)
index 0000000..2e7f5e6
--- /dev/null
@@ -0,0 +1,217 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <sys/poll.h>
+#include "tiny_bt_hci_cmd.h"
+#include "bt_hw.h"
+
+
+
+
+
+/************************** hci command functions**************************/
+int send_hci_inquiry_cmd(void){
+       inquiry_cp cmdp,*p_cmdp=&cmdp;
+       hci_cmd_request creq,*p_creq=&creq;
+       inq_time time=T14s40;
+       responce_num num=NUM_RSP_20; 
+       
+       memset(p_cmdp,0,sizeof(cmdp));
+       p_cmdp->lap[0]=0x33;
+       p_cmdp->lap[1]=0x8b;
+       p_cmdp->lap[2]=0x9e;
+       p_cmdp->length=time;
+       p_cmdp->num_rsp=num;
+       memset(p_creq,0,sizeof(creq));
+       p_creq->OCF_OGF=INQUIRY_CMD_OP;
+       p_creq->p_cmdp=p_cmdp;
+       p_creq->cmdp_len=INQUIRY_CP_SIZE;
+       
+       if(send_cmd(p_creq)<0){
+               perror("hci_inquiry_cmd wasn't sent");
+               return -1;
+       }
+       return 0;
+}
+// fill up the input parameter address pointer by one address
+int send_hci_read_bd_addr_cmd(void){
+       hci_cmd_request creq,*p_creq=&creq;
+               
+       memset(p_creq,0,sizeof(creq));
+       p_creq->OCF_OGF=READ_BD_ADDR_CMD_OP;
+       p_creq->cmdp_len=0;
+       
+       if(send_cmd(p_creq)<0){
+               perror("hci_read_bd_addr wasn't sent");
+               return -1;
+       }
+       return 0;
+}
+
+int send_hci_read_local_name_cmd(void){
+       hci_cmd_request creq,*p_creq=&creq;
+               
+       memset(p_creq,0,sizeof(creq));
+       p_creq->OCF_OGF=READ_LOCAL_NAME_CMD_OP;
+       p_creq->cmdp_len=0;
+       
+       if(send_cmd(p_creq)<0){
+               perror("hci_read_local_name_cmd wasn't sent");
+               return -1;
+       }
+       return 0;
+}
+
+int send_hci_create_connection_cmd(bt_address *p_dest_addr){
+       create_conn_cp cmdp,*p_cmdp=&cmdp;
+       hci_cmd_request creq,*p_creq=&creq;
+       
+       memset(p_cmdp,0,sizeof(cmdp));  
+       p_cmdp->bdaddr=*p_dest_addr;
+       p_cmdp->pkt_type=0x0010;
+       p_cmdp->pscan_rep_mode=0x01;
+       p_cmdp->pscan_mode=0x00;
+       p_cmdp->clock_offset=0xf000;
+       p_cmdp->role_switch=0x00;
+       memset(p_creq,0,sizeof(creq));
+       p_creq->OCF_OGF=CREATE_CONNECTION_CMD_OP;
+       p_creq->p_cmdp=p_cmdp;
+       p_creq->cmdp_len=CREATE_CONN_CP_SIZE;
+       
+       if(send_cmd(p_creq)<0){
+               perror("hci_create_connection_cmd wasn't sent");
+               return -1;
+       }
+       return 0;       
+}
+
+int send_hci_accept_conn_req_cmd(bt_address *p_address){
+       accept_conn_req_cp cmdp, *p_cmdp=&cmdp;
+       hci_cmd_request creq,*p_creq=&creq;
+       
+       memset(p_cmdp,0,sizeof(cmdp));
+       p_cmdp->bdaddr=*p_address;
+       p_cmdp->role=0x01;
+       memset(p_creq,0,sizeof(creq));
+       p_creq->OCF_OGF=ACCEPT_CONNECTION_REQ_OP;
+       p_creq->p_cmdp=p_cmdp;
+       p_creq->cmdp_len=ACCEPT_CONN_REQ_CP_SIZE;
+       
+       if(send_cmd(p_creq)<0){
+               perror("hci_accept_conn_req_cmd wasn't sent");
+               return -1;
+       }
+       return 0;
+}
+
+/*********************************main functions****************************/
+int send_cmd(hci_cmd_request *p_creq){
+       __u8 array[p_creq->cmdp_len+4];
+       __u16 sw_opcode;
+       int ii;
+       sw_opcode=swap_2_bytes(p_creq->OCF_OGF);
+       array[0]=0x01;
+       memcpy(&array[1],&sw_opcode,2);
+       array[3]= p_creq->cmdp_len;
+       if(p_creq->cmdp_len > 0){
+               memcpy(&array[4],p_creq->p_cmdp,p_creq->cmdp_len); // !!!!!!!!! segmentation fault
+       }
+
+       for(ii=0;ii<sizeof(array);ii++){
+               printf(" %x",array[ii]);
+       }
+       printf("\n");
+
+       if(hw_bt_write(array,sizeof(array))<0){
+               perror("hw_bt_write problem");
+               return -1;
+       }
+       return 0;
+}
+
+/*****************************HCI support functions*****************************/
+
+
+void assemble_ocf_ogf(__u8 ocf,__u8 ogf,__u16 *p_ocf_ogf){
+       __u16 var1;
+       __u16 result;
+       
+       var1=(ogf<<10);
+       result=ocf;
+       *p_ocf_ogf=(result|var1);
+}
+
+void printba(bt_address *ba){
+       printf("address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X: \n",ba->byte[0],ba->byte[1],ba->byte[2],ba->byte[3],ba->byte[4],ba->byte[5]);
+}
+
+int compare_bda(bt_address *p_first, bt_address *p_second){
+       __u8 i,k=0;
+       for(i=0;i<sizeof(bt_address);i++){
+               if(p_first->byte[i]==p_second->byte[i])
+                       k++;
+       }
+       if(k==sizeof(bt_address)) return 1; //all bytes are similar
+       
+       return 0; //addreses are different in one byte at least  
+}
+
+void fill_zero(bt_address *p_addr){
+       __u8 i;
+       for(i=0;i<sizeof(bt_address);i++){
+               p_addr->byte[i]=0x00;
+       }
+}
+void swap_addrbytes(bt_address *p_addr){
+       bt_address help,*p_help=&help;
+       __u8 i;
+       for(i=0;i<sizeof(bt_address);i++){
+               p_help->byte[i]=p_addr->byte[5-i];
+       }
+       *p_addr=*p_help;
+}
+__u16 swap_2_bytes(__u16 twobytes){
+       __u16 first,second,result=0;
+       first=twobytes&255;
+       second=twobytes&65280;
+       result=result|(first<<8);
+       result=result|(second>>8);
+       return result;
+}
+
+__u8 swap8(__u8 byte1){
+       __u8 i,mask=0,hvar1=0,resvar=0;
+       for(i=0;i<8;i++){
+               mask=1<<i; // 1,2,4,8,16,32,64,128
+               hvar1=byte1 & mask;
+               if(hvar1>0)
+               resvar=resvar + (128>>i);
+       }
+       return resvar;          
+}
+
+__u16 swap16(__u16 byte2){
+       __u8 i;
+       __u16 mask=0,hvar1=0,resvar=0;
+       for(i=0;i<16;i++){
+       mask=1<<i;
+       hvar1=byte2 & mask;
+       if(hvar1>0)
+       resvar=resvar + (32768>>i);
+       }
+       return resvar;
+}
+
+void fill_add(bt_address *addr,__u8 first, __u8 sec, __u8 third, __u8 forth, __u8 fifth, __u8 sixth){  
+       addr->byte[0]=first;
+       addr->byte[1]=sec;
+       addr->byte[2]=third;
+       addr->byte[3]=forth;
+       addr->byte[4]=fifth;
+       addr->byte[5]=sixth;
+}
+