]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src2/bt_hw.c
00d18f6bae02f74f8957a2bd2063f6edcd177241
[tiny-bt.git] / src2 / bt_hw.c
1 /*
2 *  C Implementation: bt_hw
3 *
4 * Description: hardweare function for sockets
5 *
6 *
7 * Author: root <root@ubuntu>, (C) 2008
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12 #include<sys/socket.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/ioctl.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <stdint.h>
21 #include "bt_hw.h"
22 #include "hciembeded.h"
23
24 static int hw_dd;
25
26 int hw_bt_open_device(__u8 dev_id){
27         struct sockaddr_hci address;
28         int dd;
29         int oldflag;
30         if((dd=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
31                 perror("socket no created");
32                 return dd; //it return -1 when socket isn't created
33         }
34         
35         oldflag=fcntl(dd, F_GETFL, 0);
36         if(fcntl(dd, F_SETFL, oldflag | O_NONBLOCK) < 0){
37                 perror("problem with socket flag setting");
38                 return -1;
39         }
40         
41         memset(&address,0,sizeof(address));
42         address.hci_family=AF_BLUETOOTH;
43         address.hci_dev=dev_id;
44         if(bind(dd,(struct sockaddr *) &address, sizeof(address))<0){
45                 perror("Socket not binded to hci device");
46                 close(dd);
47                 return -1;
48         }
49         
50         if(ioctl(dd, HCISETRAW,1)<0){
51                 perror("error in ioctl HCISETRAW");
52                 return -1;
53                         
54         }
55         hw_dd=dd;
56         return dd;
57 }
58
59 int hw_bt_close_dev(){
60         return close(hw_dd);
61 }
62
63 int hw_bt_write(__u8 *p_array, __u16 length){
64         while(write(hw_dd, p_array, length)<0){
65                 perror("write interrupted");
66                 if(errno == EAGAIN || errno == EINTR)
67                         continue;
68                 return -1;
69                 
70         }
71         return 0;
72 }
73
74 int hw_bt_read(__u8 *p_recbuf){
75         int len;
76         while((len=read(hw_dd, p_recbuf, sizeof(p_recbuf)*HCI_MAX_EVENT_SIZE))<0){
77                 if(errno == EINTR)
78                         continue;
79                 if(errno == EAGAIN)
80                         return 0;
81                 perror("Problem with reading \n");
82                         return -1;
83         }
84         return len;
85 }
86