]> rtime.felk.cvut.cz Git - tiny-bt.git/blobdiff - src2/bt_hw.c
new version of callback functions and new was added
[tiny-bt.git] / src2 / bt_hw.c
index af5e50d615cb5953fe85344191d1fe3b916da41b..00d18f6bae02f74f8957a2bd2063f6edcd177241 100644 (file)
@@ -1,7 +1,7 @@
 /*
 *  C Implementation: bt_hw
 *
-* Description: 
+* Description: hardweare function for sockets
 *
 *
 * Author: root <root@ubuntu>, (C) 2008
 * Copyright: See COPYING file that comes with this distribution
 *
 */
-int hw_dd;
-__u8 hw_type;
+#include<sys/socket.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdint.h>
+#include "bt_hw.h"
+#include "hciembeded.h"
 
+static int hw_dd;
 
-int hw_bt_open_device(__u8 type, __u8 dev_id){
-       hw_type=type;
-       
-       switch(type){
-
-       case 0:{
-               struct sockaddr_hci address;
-               int dd;
-               int oldflag;
+int hw_bt_open_device(__u8 dev_id){
+       struct sockaddr_hci address;
+       int dd;
+       int oldflag;
+       if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
+               perror("socket no created");
+               return dd; //it return -1 when socket isn't created
+       }
        
-               if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
-                       perror("socket no created");
-                       return dd; //it return -1 when socket isn't created
-               }
-               
-               oldflag=fcntl(dd, F_GETFL, 0);
-               if(fcntl(dd, F_SETFL, oldflag | O_NONBLOCK) < 0){
-                       perror("problem with socket flag setting");
-                       return -1;
-               }
+       oldflag=fcntl(dd, F_GETFL, 0);
+       if(fcntl(dd, F_SETFL, oldflag | O_NONBLOCK) < 0){
+               perror("problem with socket flag setting");
+               return -1;
+       }
        
-               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;
-               }
+       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;
+       }
        
-               if(ioctl(dd, HCISETRAW,1)<0){
-                       perror("error in ioctl HCISETRAW");
-                       return -1;
+       if(ioctl(dd, HCISETRAW,1)<0){
+               perror("error in ioctl HCISETRAW");
+               return -1;
                        
-               }
-               hw_dd=dd;
-               return dd;
-               }
-       case 1:{
-
-               }
        }
-
-       return -1;
+       hw_dd=dd;
+       return dd;
 }
 
 int hw_bt_close_dev(){
-       switch(hw_type){
-       case 0:{
-               return close(hw_dd);
-       }
-       case 1:{
-
-       }
-       
-       }
-               
-       return -1;
+       return close(hw_dd);
 }
 
 int hw_bt_write(__u8 *p_array, __u16 length){
-       switch(hw_type){
-       case 0:{
-               while(write(hw_dd, p_array, length)<0){
-                       perror("write interrupted");
-                       if(errno == EAGAIN || errno == EINTR)
-                               continue;
-                       return -1;
-               }
-       }
-       case 1:{
-
-       }
+       while(write(hw_dd, p_array, length)<0){
+               perror("write interrupted");
+               if(errno == EAGAIN || errno == EINTR)
+                       continue;
+               return -1;
+               
        }
        return 0;
 }
 
 int hw_bt_read(__u8 *p_recbuf){
        int len;
-       switch(hw_type){        
-       case 0:{
-               while((len=read(hw_dd, p_recbuf, sizeof(p_recbuf*HCI_MAX_EVENT_SIZE)))<0){
-                               if(errno == EINTR)
-                                       continue;
-                               if(errno == EAGAIN)
-                                       return 0;
-                               perror("Problem with reading \n");
-                               return -1;
-                       }
-               return len;
-       }
-       case 1:{
-
-       }
+       while((len=read(hw_dd, p_recbuf, sizeof(p_recbuf)*HCI_MAX_EVENT_SIZE))<0){
+               if(errno == EINTR)
+                       continue;
+               if(errno == EAGAIN)
+                       return 0;
+               perror("Problem with reading \n");
+                       return -1;
        }
-
+       return len;
 }
+