]> rtime.felk.cvut.cz Git - tiny-bt.git/blob - src/hil2cap.c
8162cc9b77d7d13e51399d6bea1f3a81e8c46d9f
[tiny-bt.git] / src / hil2cap.c
1 /*
2 *  C Implementation: testovaci
3 *
4 * Description: 
5 *
6 *
7 * Author: root <root@ubuntu>, (C) 2008
8 *
9 * Copyright: See COPYING file that comes with this distribution
10 *
11 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/ioctl.h>
15 #include <errno.h>
16 #include <stdint.h>
17 #include "hil2cap.h"
18 #include "hcidriver.h"
19
20
21 cmd_state_request array_req[MAX_CMD_STATE_REQUEST];
22 int cmd_req_id;
23 int global_index;
24
25
26
27
28 int create_master_connection(struct hci_dev_info *master){//establish connection and send a coutn of bytes every x second
29          //array of open request which are in waiting state for appropriate event or answer
30         connection_hci connection,*p_connection=&connection;
31         p_connection->con_id=0;
32         p_connection->con_state=DISCONNECTED;
33         
34         
35         while(1){
36                 if(!p_connection->con_state){ //there is no connection, create it
37                                 
38                 }
39
40
41
42         }
43         
44         return 0;
45 }
46
47 int show_all_local_dev(struct hci_dev_info *master){ // and choose a master device which will initiate the connection
48         int sock,i;
49         struct hci_dev_list_req dlist,*p_dlist=&dlist;
50         struct hci_dev_req *p_dreq;
51         
52         p_dlist->dev_num=HCI_MAX_DEV;
53         p_dreq=p_dlist->dev_req;
54         
55         if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){ 
56                 return -1;
57         }
58         
59         if((ioctl(sock, HCIGETDEVLIST, (void *) p_dlist))<0){
60                 return -1;
61         }
62         if(p_dlist->dev_num > 0){
63                 struct hci_dev_info dev_array[p_dlist->dev_num];
64                 for(i=0;i<p_dlist->dev_num;i++){
65                         dev_array[i].dev_id=(p_dreq+i)->dev_id;
66                         if(ioctl(sock, HCIGETDEVINFO, (void *) &dev_array[i])<0){
67                                 printf("No info");
68                                 continue;
69                         }
70                         int dd=hci_open_device(dev_array[i].dev_id);
71                         if(l2cap_call_cmd(READ_BD_ADDR_CMD_OP,dd,&dev_array[i].bdaddr)< 0){
72                                 printf("Error in call hci bd addr cmd \n");
73                                 return -1;
74                         }
75                         if(l2cap_call_cmd(READ_LOCAL_NAME_CMD_OP,dd,&dev_array[i].name)< 0){
76                                 printf("Error in call hci bd addr cmd \n");
77                                 return -1;
78                         }
79                 }
80                 *master = dev_array[0]; //I will take a first dev like a master
81                 
82         }
83         else{
84                 printf("There is no hci device \n");
85                 return -1;
86         }
87         return 0;
88                 
89 }
90
91 int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){
92         cmd_state_request new_req,*p_new_req=&new_req;
93         __u16 sw_opcode;
94         sw_opcode=swap_2_bytes(ocf_ogf);        
95         switch(ocf_ogf){
96         
97         case READ_BD_ADDR_CMD_OP:{
98                 p_new_req->actual_status=ESTABLISHED;
99                 p_new_req->id=cmd_req_id++;
100                 p_new_req->req_opcode = ocf_ogf;
101                 p_new_req->evt_code = EVT_CMD_COMPLETE;
102                 p_new_req->p_serv_rutine=rutine_catch_bd_addr;
103
104                 if(req_fcn_add(p_new_req)<0){
105                         printf("Mistake with req array adding \n ");
106                         return -1;
107                 }
108                 
109                 if(call_hci_read_bd_addr_cmd(dd,(bt_address *)p_param,&sw_opcode)){
110                         printf("Mistake with bt addr cmd \n ");
111                         return -1;
112                 }
113                 req_fcn_remove();
114                 return 0;
115                 
116         
117         }
118         case READ_LOCAL_NAME_CMD_OP:{
119                 p_new_req->actual_status=ESTABLISHED;
120                 p_new_req->id=cmd_req_id++;
121                 p_new_req->req_opcode = ocf_ogf;
122                 p_new_req->evt_code = EVT_CMD_COMPLETE;
123                 p_new_req->p_serv_rutine=rutine_catch_bd_addr;
124
125                 if(req_fcn_add(p_new_req)<0){
126                         printf("Mistake with req array adding \n ");
127                         return -1;
128                 }
129                 if(call_hci_read_local_name_cmd(dd,p_param,&sw_opcode)){
130                         printf("Mistake with read local name cmd \n ");
131                         return -1;
132                 }
133                 req_fcn_remove();
134                 return 0;
135         }
136         }
137         
138         return 0;
139 }
140 /* request array support functions */
141 int req_fcn_add(cmd_state_request *p_req){ //fcn add the request at the end of array or replace the oldest request
142         int i,sig=0,index=0;
143         cmd_state_request the_oldest=array_req[0];
144         
145         for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
146                 if((array_req[i].actual_status)==0 || array_req[i].actual_status==3 ){
147                         array_req[i]=*p_req;
148                         sig=1;
149                         global_index=i;
150                         break;
151                 }
152                 if((array_req[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
153                         the_oldest = array_req[i];
154                         index=i;
155                         
156                 }
157         }
158         if(!sig){
159                 //return -1;
160                 array_req[index]=*p_req;
161                 global_index=index;     
162         }
163         return 0;
164 }
165
166 void req_fcn_remove(void){
167         array_req[global_index].actual_status=DONE;
168 }
169
170 /* rutines for each command */
171 //todo:
172 void *rutine_catch_bd_addr(void *arg){
173
174 return (void*)0;
175 }
176
177
178 int main(void){
179         struct hci_dev_info master,*p_master=&master;
180         cmd_req_id=0;
181         global_index=0;
182         memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST));
183         if(show_all_local_dev(p_master)<0){
184                 printf("error no dev \n");
185                 return -1;
186         }
187         
188         
189         
190         
191 return 0;
192 }
193
194
195 //void call_hci_send_data(unsigned short con_handle, __u8 pb_flag, __u8 b_flag, unsigned short data_lenght)
196
197 //void call_hci_create_connection(bt_address *dev_adress,unsigned short pkt_type,__u8 sc_mod){//
198
199 //}
200
201 //Socket functions
202 /* */
203         
204         
205