2 * C Implementation: testovaci
7 * Author: root <root@ubuntu>, (C) 2008
9 * Copyright: See COPYING file that comes with this distribution
14 #include <sys/ioctl.h>
18 #include "hcidriver.h"
21 expect_evt evt_array[MAX_CMD_STATE_REQUEST];
24 connection_hci connection,*p_connection=&connection;
27 void add_evt_toarray(expect_evt *p_evt){ //fcn add the request at the end of array or replace the oldest request
29 incoming_evt the_oldest=evt_array[0];
31 for(i=0;i<MAX_EVT_COUNT;i++){
32 if((evt_array[i].actual_status)==0 || evt_array[i].actual_status==3 ){
38 if((evt_array[i].id) < the_oldest.id){ //the smallest id (oldest request) will stay here
39 the_oldest = evt_array[i];
46 evt_array[index]=*p_evt;
51 int look_up_inarray(__u8 evt_code){
52 int i,index=-1,oldestid=65535;
54 if(evt_array[global_index].evt_code == evt_code && evt_array[global_index].actual_status != DONE){
58 for(i=0;i<MAX_CMD_STATE_REQUEST;i++){
59 if(evt_array[i].evt_code == evt_code && evt_array[i].actual_status != DONE){
60 if(evt_array[i].id<oldestid){
61 oldestid=evt_array[i].id;
72 int tiny_bt_init(bt_device *p_device){
73 expect_evt evt,*p_evt=&evt;
74 req_state status = PERMANENT;
77 p_connection->con_id=0;
78 p_connection->con_state=DISCONNECTED;
80 memset(p_evt,0,sizeof(evt));
81 p_evt->actual_status=status;
83 p_evt->evt_type=EVT_CMD_STATUS;
85 add_evt_toarray(p_evt);
89 int tiny_bt_process(){
92 __u8 recbuf[HCI_MAX_EVENT_SIZE],*p_recbuf;
94 len = hw_bt_read(recbuf);
97 printf("Count of received bytes %d \n ",len);
99 printf("%2.2X ",recbuf[j]);
102 if(recbuf[0]==HCI_EVENT_PKT){
103 p_hdr = (void *) (recbuf + 1);
104 p_recbuf = (void *) (recbuf + (1 + HCI_EVENT_HDR_SIZE));
105 if((index=look_up_inarray(p_hdr->evt))<0){
106 printf("There is no awaiting evt like this\n");
110 if(evt_array[index].p_callback(evt_array[index].p_data, p_recbuf)<0){
111 printf("error with service rutine");
114 if(evt_array[index].actual_status != PERMANENT){
115 evt_array[index].actual_status = DONE;
120 else if(recbuf[0]==2){
123 else return 0; // 0 or -1
127 printf("There is nothing on socket %d \n",dd);
135 int tiny_bt_read_bd_addr(bd_address *p_dest, void (*callback_app_read_bd_addr)(bt_address *p_address)){
136 expect_evt evt,*p_evt=&evt;
137 req_state status = ESTABLISHED;
138 read_bd_addr_data *p_data;
140 memset(p_evt,0,sizeof(evt));
141 p_evt->actual_status=status;
143 p_evt->evt_type=EVT_CMD_COMPLETE;
144 p_evt->req_opcode=READ_BD_ADDR_CMD_OP;
145 p_evt->p_callback=&callback_hci_read_bd_addr;
147 p_data=malloc(sizeof(read_bd_addr_data));
148 p_data->p_address=p_dest;
149 p_data->callback_app_read_bd_addr=callback_app_read_bd_addr;
150 p_evt->p_data=(void *)p_data;
152 add_evt_toarray(p_evt);
153 if(send_hci_read_bd_addr_cmd()<0){
154 perror("send_hci_cmd error");
160 int tiny_bt_read_local_name(char *p_name, void (*callback_app_read_local_name)(char *p_name)){
161 expect_evt evt,*p_evt=&evt;
162 req_state status = ESTABLISHED;
163 read_local_name_data *p_data;
165 memset(p_evt,0,sizeof(evt));
166 p_evt->actual_status=status;
168 p_evt->evt_type=EVT_CMD_COMPLETE;
169 p_evt->req_opcode=READ_LOCAL_NAME_CMD_OP;
170 p_evt->p_callback=callback_hci_read_local_name;
172 p_data=malloc(sizeof(read_local_name_data));
173 p_data->p_name=p_name;
174 p_data->callback_hci_read_local_name=callback_app_read_local_name;
175 p_evt->p_data=(void *)p_data;
177 add_evt_toarray(p_evt);
178 if(send_hci_read_local_name_cmd()<0){
179 perror("send_hci_cmd error");
185 int tiny_bt_connect(bt_address *p_dest_addr, __u16 *p_handle, void (*callback_app_connection_complete)(__u16 *p_handle, bt_address *p_address)){
186 expect_evt evt,*p_evt=&evt;
187 req_state status = ESTABLISHED;
188 connection_complete_data *p_data;
190 memset(p_evt,0,sizeof(evt));
191 p_evt->actual_status=status;
193 p_evt->evt_type=EVT_CONN_COMPLETE;
194 p_evt->req_opcode=CREATE_CONNECTION_CMD_OP;
195 p_evt->p_callback=callback_hci_connection_complete;
197 p_data=malloc(sizeof(connection_complete_data));
198 p_data->p_handle=p_handle;
199 p_data->callback_app_connection_complete=callback_app_connection_complete;
200 p_evt->p_data=(void *)p_data;
202 add_evt_toarray(p_evt);
203 if(send_hci_create_connection_cmd(p_dest_addr)<0){
204 perror("send_hci_cmd error");
211 int tiny_bt_wait_for_connection(){
220 int create_master_connection(connection_hci *p_connection){//establish connection and send a coutn of bytes every x second
221 //array of open request which are in waiting state for appropriate event or answer
222 p_connection->con_id=0;
223 p_connection->con_state=DISCONNECTED;
225 p_connection->socket_fd=hci_open_device_sraw(p_connection->master.dev_id);
226 if(l2cap_call_cmd(CREATE_CONNECTION_CMD_OP,p_connection->socket_fd,p_connection)<0){
227 printf("connection problem \n");
235 int show_all_local_dev(struct hci_dev_info *master){ // and choose a master device which will initiate the connection
237 struct hci_dev_list_req dlist,*p_dlist=&dlist;
238 struct hci_dev_req *p_dreq;
240 p_dlist->dev_num=HCI_MAX_DEV;
241 p_dreq=p_dlist->dev_req;
243 if((sock=socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI))<0){
247 if((ioctl(sock, HCIGETDEVLIST, (void *) p_dlist))<0){
250 if(p_dlist->dev_num > 0){
251 struct hci_dev_info dev_array[p_dlist->dev_num];
252 for(i=0;i<p_dlist->dev_num;i++){
253 dev_array[i].dev_id=(p_dreq+i)->dev_id;
254 if(ioctl(sock, HCIGETDEVINFO, (void *) &dev_array[i])<0){
258 int dd=hci_open_device(dev_array[i].dev_id);
259 if(l2cap_call_cmd(READ_BD_ADDR_CMD_OP,dd,&dev_array[i].bdaddr)< 0){
260 printf("Error in call hci bd addr cmd \n");
263 if(l2cap_call_cmd(READ_LOCAL_NAME_CMD_OP,dd,&dev_array[i].name)< 0){
264 printf("Error in call hci bd addr cmd \n");
267 //hci_close_dev(dev_array[i].dev_id);
269 *master = dev_array[0]; //I will take a first dev like a master
273 printf("There is no hci device \n");
280 int show_all_remote_dev(struct hci_dev_info *p_master, bt_address *p_remadrar, int *p_remd_count){
281 int dd=hci_open_device(p_master->dev_id);
283 if((*p_remd_count=l2cap_call_cmd(INQUIRY_CMD_OP, dd, p_remadrar))< 0){
284 printf("Error in call inquiry command \n");
291 int l2cap_call_cmd(__u16 ocf_ogf,int dd, void *p_param){
292 cmd_state_request new_req,*p_new_req=&new_req;
294 int remd_count,ret_val;
295 sw_opcode=swap_2_bytes(ocf_ogf);
296 p_new_req->actual_status=ESTABLISHED;
297 p_new_req->id=cmd_req_id++;
298 p_new_req->req_opcode = ocf_ogf;
302 case READ_BD_ADDR_CMD_OP:
303 p_new_req->evt_code = EVT_CMD_COMPLETE;
304 p_new_req->p_serv_rutine=NULL;
306 if(req_fcn_add(p_new_req)<0){
307 printf("Mistake with req array adding \n ");
311 if(call_hci_read_bd_addr_cmd(dd,(bt_address *)p_param,&sw_opcode)){
312 printf("Mistake with bt addr cmd \n ");
318 case READ_LOCAL_NAME_CMD_OP:
319 p_new_req->evt_code = EVT_CMD_COMPLETE;
320 p_new_req->p_serv_rutine=NULL;
322 if(req_fcn_add(p_new_req)<0){
323 printf("Mistake with req array adding \n ");
326 if(call_hci_read_local_name_cmd(dd,p_param,&sw_opcode)){
327 printf("Mistake with read local name cmd \n ");
335 p_new_req->evt_code = EVT_INQUIRY_COMPLETE;
336 p_new_req->p_serv_rutine=NULL;
338 if(req_fcn_add(p_new_req)<0){
339 printf("Mistake with req array adding \n ");
342 if((remd_count=call_hci_inquiry_cmd(dd,p_param,&sw_opcode)) < 0){
343 printf("Mistake with inquiry cmd \n ");
349 case CREATE_CONNECTION_CMD_OP:
350 p_new_req->evt_code = EVT_CONN_COMPLETE;
351 p_new_req->p_serv_rutine=NULL;
353 if(req_fcn_add(p_new_req)<0){
354 printf("Mistake with req array adding \n ");
357 if((ret_val=call_hci_create_connection_cmd(dd, &(((connection_hci *)p_param)->slave.bdaddr), &sw_opcode)) < 0){
358 printf("Mistake with inquiry cmd \n ");
361 ((connection_hci *)p_param)->handle=ret_val;
362 ((connection_hci *)p_param)->con_state=CONNECTED;
372 struct hci_dev_info master,*p_master=&master;
373 bt_address remote_bd_addr_array[HCI_MAX_DEV];
374 connection_hci connection,*p_connection=&connection;
375 memset(remote_bd_addr_array,0,(sizeof(bt_address)*HCI_MAX_DEV));
380 memset(array_req,0,(sizeof(cmd_state_request)*MAX_CMD_STATE_REQUEST));
381 if(show_all_local_dev(p_master)<0){
382 printf("error no dev \n");
385 if(show_all_remote_dev(p_master, remote_bd_addr_array, &remd_count)<0){
386 printf("error no remote dev \n");
389 printf("All remote devices:\n");
390 for(i=0;i<remd_count;i++){
391 printba(&remote_bd_addr_array[i]);
393 p_connection->master=master;
394 p_connection->slave.bdaddr=remote_bd_addr_array[0];
395 //fill_add(&prefered,0x31,0x16,0xCA,0x72,0x02,0x00);
396 //p_connection->slave.bdaddr=prefered;
397 if(create_master_connection(p_connection)<0){
398 printf("connection error");