--- /dev/null
+/*
+* C Implementation: bt_hw
+*
+* Description: hardweare function for sockets
+*
+*
+* Author: root <root@ubuntu>, (C) 2008
+*
+* Copyright: See COPYING file that comes with this distribution
+*
+*/
+#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 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
+ }
+
+ 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;
+ }
+
+ if(ioctl(dd, HCISETRAW,1)<0){
+ perror("error in ioctl HCISETRAW");
+ return -1;
+
+ }
+ hw_dd=dd;
+ return dd;
+}
+
+int hw_bt_close_dev(){
+ return close(hw_dd);
+}
+
+int hw_bt_write(__u8 *p_array, __u16 length){
+ 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;
+ 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;
+}
+