2 * Copyright (c) 2003 EISLAB, Lulea University of Technology.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwBT Bluetooth stack.
29 * Author: Conny Ohult <conny@sm.luth.se>
33 /*-----------------------------------------------------------------------------------*/
36 * Implementation of the Host Controller Interface (HCI). A command interface to the
37 * baseband controller and link manager, and gives access to hardware status and
41 /*-----------------------------------------------------------------------------------*/
55 #include "physbusif.h"
57 struct hci_pcb
*hci_dev
= NULL
;
58 struct hci_link
*hci_active_links
= NULL
;
59 struct hci_link
*hci_tmp_link
= NULL
;
60 struct hci_link_key
*hci_tmp_key
= NULL
;
62 MEMB(hci_pcbs
,sizeof(struct hci_pcb
),MEMB_NUM_HCI_PCB
);
63 MEMB(hci_links
,sizeof(struct hci_link
),MEMB_NUM_HCI_LINK
);
64 MEMB(hci_inq_results
,sizeof(struct hci_inq_res
),MEMB_NUM_HCI_INQ
);
65 MEMB(hci_link_key_results
,sizeof(struct hci_link_key
),MEMB_NUM_HCI_LINK_KEY
);
72 btmemb_init(&hci_pcbs
);
73 btmemb_init(&hci_links
);
74 btmemb_init(&hci_inq_results
);
75 btmemb_init(&hci_link_key_results
);
77 if((hci_dev
=btmemb_alloc(&hci_pcbs
))==NULL
) {
78 ERROR("hci_init: Could not allocate memory for hci_dev\n");
81 memset(hci_dev
,0,sizeof(struct hci_pcb
));
83 hci_active_links
= NULL
;
89 struct hci_link
* hci_new(void)
91 struct hci_link
*link
;
93 link
= btmemb_alloc(&hci_links
);
94 if(link
==NULL
) return NULL
;
96 memset(link
,0,sizeof(struct hci_link
));
100 struct hci_link
* hci_get_link(struct bd_addr
*bdaddr
)
102 struct hci_link
*link
;
104 for(link
=hci_active_links
;link
!=NULL
;link
=link
->next
) {
105 if(bd_addr_cmp(&(link
->bdaddr
),bdaddr
)) break;
110 /*-----------------------------------------------------------------------------------*/
114 * Close the link control block.
116 /*-----------------------------------------------------------------------------------*/
117 err_t
hci_close(struct hci_link
*link
)
119 if(link
->p
!= NULL
) {
120 btpbuf_free(link
->p
);
123 HCI_RMV(&(hci_active_links
), link
);
124 btmemb_free(&hci_links
, link
);
129 /*-----------------------------------------------------------------------------------*/
133 * Closes all active link control blocks.
135 /*-----------------------------------------------------------------------------------*/
136 void hci_reset_all(void)
138 struct hci_link
*link
,*tlink
;
139 struct hci_inq_res
*ires
,*tires
;
140 struct hci_link_key
*ikeys
,*tikeys
;
142 for(link
=hci_active_links
;link
!=NULL
;) {
147 hci_active_links
= NULL
;
149 for(ires
=hci_dev
->ires
;ires
!=NULL
;) {
151 btmemb_free(&hci_inq_results
,ires
);
155 for(ikeys
=hci_dev
->keyres
;ikeys
!=NULL
;) {
156 tikeys
= ikeys
->next
;
157 btmemb_free(&hci_inq_results
,ikeys
);
160 btmemb_free(&hci_pcbs
,hci_dev
);
165 void hci_arg(void *arg
)
167 hci_dev
->cbarg
= arg
;
170 void hci_cmd_complete(err_t (*cmd_complete
)(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
))
172 hci_dev
->cmd_complete
= cmd_complete
;
175 /*-----------------------------------------------------------------------------------*/
179 * Used to specify the function that should be called when HCI has received a
180 * PIN code request event.
182 /*-----------------------------------------------------------------------------------*/
183 void hci_pin_req(err_t (* pin_req
)(void *arg
, struct bd_addr
*bdaddr
))
185 hci_dev
->pin_req
= pin_req
;
187 /*-----------------------------------------------------------------------------------*/
189 * hci_link_key_not():
191 * Used to specify the function that should be called when HCI has received a
192 * link key notification event.
194 /*-----------------------------------------------------------------------------------*/
195 void hci_link_key_not(err_t (* link_key_not
)(void *arg
, struct bd_addr
*bdaddr
, u8_t
*key
))
197 hci_dev
->link_key_not
= link_key_not
;
200 /*-----------------------------------------------------------------------------------*/
202 * hci_connection_complete():
204 * Used to specify the function that should be called when HCI has received a
205 * connection complete event.
207 /*-----------------------------------------------------------------------------------*/
208 void hci_connection_complete(err_t (* conn_complete
)(void *arg
, struct bd_addr
*bdaddr
))
210 hci_dev
->conn_complete
= conn_complete
;
213 /*-----------------------------------------------------------------------------------*/
215 * hci_wlp_complete():
217 * Used to specify the function that should be called when HCI has received a
218 * successful write link policy complete event.
220 /*-----------------------------------------------------------------------------------*/
221 void hci_wlp_complete(err_t (* wlp_complete
)(void *arg
, struct bd_addr
*bdaddr
))
223 hci_dev
->wlp_complete
= wlp_complete
;
226 void hci_conn_req(err_t (*conn_req
)(void *arg
,struct bd_addr
*bdaddr
,u8_t
*cod
,u8_t link_type
))
228 hci_dev
->conn_req
= conn_req
;
231 err_t
hci_reg_dev_info(struct bd_addr
*bdaddr
,u8_t
*cod
,u8_t psrm
,u8_t psm
,u16_t co
)
233 struct hci_inq_res
*ires
;
235 if(hci_dev
==NULL
) return ERR_VAL
;
237 if((ires
=btmemb_alloc(&hci_inq_results
))!=NULL
) {
238 bd_addr_set(&(ires
->bdaddr
),bdaddr
);
239 memcpy(ires
->cod
,cod
,3);
245 HCI_REG(&(hci_dev
->ires
),ires
);
251 struct pbuf
* hci_cmd_ass(struct pbuf
*p
,u8_t ocf
,u8_t ogf
,u8_t len
)
253 ((u8_t
*)p
->payload
)[0] = HCI_COMMAND_DATA_PACKET
; /* cmd packet type */
254 ((u8_t
*)p
->payload
)[1] = (ocf
&0xff); /* OCF & OGF */
255 ((u8_t
*)p
->payload
)[2] = ((ocf
>>8)|(ogf
<<2));
256 ((u8_t
*)p
->payload
)[3] = len
-HCI_CMD_HDR_LEN
-1; /* Param len = plen - cmd hdr - ptype */
258 if(hci_dev
->num_cmd
>0) hci_dev
->num_cmd
--;
262 err_t
hci_reset(void)
264 struct pbuf
*p
= NULL
;
266 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_RESET_PLEN
,PBUF_RAM
))==NULL
) {
267 ERROR("hci_reset: Could not allocate memory for pbuf\n");
271 p
= hci_cmd_ass(p
,HCI_RESET_OCF
,HCI_HC_BB_OGF
,HCI_RESET_PLEN
);
273 physbusif_output(p
,p
->tot_len
);
279 err_t
hci_read_buffer_size(void)
281 struct pbuf
*p
= NULL
;
283 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_BUF_SIZE_PLEN
,PBUF_RAM
))==NULL
) {
284 ERROR("hci_read_buffer_size: Could not allocate memory for pbuf\n");
288 p
= hci_cmd_ass(p
,HCI_R_BUF_SIZE_OCF
,HCI_INFO_PARAM_OGF
,HCI_R_BUF_SIZE_PLEN
);
290 physbusif_output(p
,p
->tot_len
);
296 err_t
hci_read_bd_addr(void)
298 struct pbuf
*p
= NULL
;
300 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_BD_ADDR_PLEN
,PBUF_RAM
))==NULL
) {
301 ERROR("hci_read_bd_addr: Could not allocate memory for pbuf\n");
305 p
= hci_cmd_ass(p
,HCI_R_BD_ADDR_OCF
,HCI_INFO_PARAM_OGF
,HCI_R_BD_ADDR_PLEN
);
307 physbusif_output(p
,p
->tot_len
);
313 err_t
hci_read_local_version(void)
315 struct pbuf
*p
= NULL
;
317 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_LOC_VERS_SIZE_PLEN
,PBUF_RAM
))==NULL
) {
318 ERROR("hci_read_local_version: Could not allocate memory for pbuf\n");
322 p
= hci_cmd_ass(p
,HCI_R_LOC_VERSION_OCF
,HCI_INFO_PARAM_OGF
,HCI_R_LOC_VERS_SIZE_PLEN
);
324 physbusif_output(p
,p
->tot_len
);
330 err_t
hci_read_local_features(void)
332 struct pbuf
*p
= NULL
;
334 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_LOC_FEAT_SIZE_PLEN
,PBUF_RAM
))==NULL
) {
335 ERROR("hci_read_local_features: Could not allocate memory for pbuf\n");
339 p
= hci_cmd_ass(p
,HCI_R_LOC_FEATURES_OCF
,HCI_INFO_PARAM_OGF
,HCI_R_LOC_FEAT_SIZE_PLEN
);
341 physbusif_output(p
,p
->tot_len
);
347 err_t
hci_read_stored_link_key()
349 struct pbuf
*p
= NULL
;
350 struct hci_link_key
*tmpres
;
352 /* Free any previous link key result list */
353 while(hci_dev
->keyres
!= NULL
) {
354 tmpres
= hci_dev
->keyres
;
355 hci_dev
->keyres
= hci_dev
->keyres
->next
;
356 btmemb_free(&hci_link_key_results
,tmpres
);
360 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_STORED_LINK_KEY_PLEN
,PBUF_RAM
))==NULL
) {
361 ERROR("hci_read_stored_link_keys: Could not allocate memory for pbuf\n");
365 p
= hci_cmd_ass(p
,HCI_R_STORED_LINK_KEY_OCF
,HCI_HC_BB_OGF
,HCI_R_STORED_LINK_KEY_PLEN
);
367 memcpy((void*)((u8_t
*)p
->payload
+ 4),hci_dev
->bdaddr
.addr
,6);
368 ((u8_t
*)p
->payload
)[10] = 1;
370 physbusif_output(p
,p
->tot_len
);
376 err_t
hci_set_event_filter(u8_t filter_type
,u8_t filter_cond_type
,u8_t
*cond
)
379 struct pbuf
*p
= NULL
;
381 switch(filter_type
) {
386 switch(filter_cond_type
) {
401 switch(filter_cond_type
) {
419 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_SET_EV_FILTER_PLEN
+cond_len
,PBUF_RAM
))==NULL
) {
420 ERROR("hci_set_event_filter: Could not allocate memory for pbuf\n");
424 p
= hci_cmd_ass(p
,HCI_SET_EV_FILTER_OCF
,HCI_HC_BB_OGF
,HCI_SET_EV_FILTER_PLEN
+cond_len
);
425 ((u8_t
*)p
->payload
)[4] = filter_type
;
426 ((u8_t
*)p
->payload
)[5] = filter_cond_type
;
427 if(cond_len
>0) memcpy(p
->payload
+6,cond
,cond_len
);
429 physbusif_output(p
,p
->tot_len
);
435 err_t
hci_write_page_timeout(u16_t timeout
)
437 struct pbuf
*p
= NULL
;
439 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_PAGE_TIMEOUT_PLEN
,PBUF_RAM
))==NULL
) {
440 ERROR("hci_set_write_page_timeout: Could not allocate memory for pbuf\n");
444 p
= hci_cmd_ass(p
,HCI_W_PAGE_TIMEOUT_OCF
,HCI_HC_BB_OGF
,HCI_W_PAGE_TIMEOUT_PLEN
);
445 ((u16_t
*)p
->payload
)[2] = htole16(timeout
);
447 physbusif_output(p
,p
->tot_len
);
453 err_t
hci_write_scan_enable(u8_t scan_enable
)
455 struct pbuf
*p
= NULL
;
457 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_SCAN_EN_PLEN
,PBUF_RAM
))==NULL
) {
458 ERROR("hci_set_write_page_timeout: Could not allocate memory for pbuf\n");
462 p
= hci_cmd_ass(p
,HCI_W_SCAN_EN_OCF
,HCI_HC_BB_OGF
,HCI_W_SCAN_EN_PLEN
);
463 ((u8_t
*)p
->payload
)[4] = scan_enable
;
465 physbusif_output(p
,p
->tot_len
);
471 err_t
hci_inquiry(u32_t lap
,u8_t inq_len
,u8_t num_resp
,err_t (*inq_complete
)(void *arg
,struct hci_pcb
*pcb
,struct hci_inq_res
*ires
,u16_t result
))
473 struct pbuf
*p
= NULL
;
474 struct hci_inq_res
*tmpres
;
476 /* Free any previous inquiry result list */
477 while(hci_dev
->ires
!= NULL
) {
478 tmpres
= hci_dev
->ires
;
479 hci_dev
->ires
= hci_dev
->ires
->next
;
480 btmemb_free(&hci_inq_results
,tmpres
);
483 hci_dev
->inq_complete
= inq_complete
;
484 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_INQUIRY_PLEN
,PBUF_RAM
))==NULL
) {
485 ERROR("hci_inquiry: Could not allocate memory for pbuf\n");
489 p
= hci_cmd_ass(p
,HCI_INQUIRY_OCF
,HCI_LINK_CTRL_OGF
,HCI_INQUIRY_PLEN
);
490 ((u8_t
*)p
->payload
)[4] = (lap
&0xff);
491 ((u8_t
*)p
->payload
)[5] = (lap
>>8);
492 ((u8_t
*)p
->payload
)[6] = (lap
>>16);
494 ((u8_t
*)p
->payload
)[7] = inq_len
;
495 ((u8_t
*)p
->payload
)[8] = num_resp
;
497 physbusif_output(p
,p
->tot_len
);
503 err_t
hci_periodic_inquiry(u32_t lap
,u16_t min_period
,u16_t max_period
,u8_t inq_len
,u8_t num_resp
,err_t (*inq_complete
)(void *arg
,struct hci_pcb
*pcb
,struct hci_inq_res
*ires
,u16_t result
))
505 struct pbuf
*p
= NULL
;
506 struct hci_inq_res
*tmpres
;
508 /* Free any previous inquiry result list */
509 while(hci_dev
->ires
!= NULL
) {
510 tmpres
= hci_dev
->ires
;
511 hci_dev
->ires
= hci_dev
->ires
->next
;
512 btmemb_free(&hci_inq_results
,tmpres
);
515 hci_dev
->inq_complete
= inq_complete
;
516 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_PERIODIC_INQUIRY_PLEN
,PBUF_RAM
))==NULL
) {
517 ERROR("hci_periodic_inquiry: Could not allocate memory for pbuf\n");
521 /* Assembling command packet */
522 p
= hci_cmd_ass(p
,HCI_PERIODIC_INQUIRY_OCF
,HCI_LINK_CTRL_OGF
,HCI_PERIODIC_INQUIRY_PLEN
);
524 /* Assembling cmd prameters */
525 ((u16_t
*)p
->payload
)[2] = htole16(max_period
);
526 ((u16_t
*)p
->payload
)[3] = htole16(min_period
);
527 ((u8_t
*)p
->payload
)[8] = (lap
&0xff);
528 ((u8_t
*)p
->payload
)[9] = (lap
>>8);
529 ((u8_t
*)p
->payload
)[10] = (lap
>>16);
531 ((u8_t
*)p
->payload
)[11] = inq_len
;
532 ((u8_t
*)p
->payload
)[12] = num_resp
;
534 physbusif_output(p
,p
->tot_len
);
540 err_t
hci_exit_periodic_inquiry()
542 struct pbuf
*p
= NULL
;
544 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_EXIT_PERIODIC_INQUIRY_PLEN
,PBUF_RAM
))==NULL
) {
545 ERROR("hci_exit_periodic_inquiry: Could not allocate memory for pbuf\n");
549 /* Assembling command packet */
550 p
= hci_cmd_ass(p
,HCI_EXIT_PERIODIC_INQUIRY_OCF
,HCI_LINK_CTRL_OGF
,HCI_EXIT_PERIODIC_INQUIRY_PLEN
);
552 physbusif_output(p
,p
->tot_len
);
558 err_t
hci_accecpt_conn_request(struct bd_addr
*bdaddr
,u8_t role
)
560 struct pbuf
*p
= NULL
;
562 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_ACCEPT_CONN_REQ_PLEN
,PBUF_RAM
))==NULL
) {
563 ERROR("hci_exit_periodic_inquiry: Could not allocate memory for pbuf\n");
567 /* Assembling command packet */
568 p
= hci_cmd_ass(p
,HCI_ACCEPT_CONN_REQ_OCF
,HCI_LINK_CTRL_OGF
,HCI_ACCEPT_CONN_REQ_PLEN
);
570 /* Assembling cmd prameters */
571 memcpy((void*)(((u8_t
*)p
->payload
)+4),bdaddr
,6);
572 ((u8_t
*)p
->payload
)[10] = role
;
574 physbusif_output(p
,p
->tot_len
);
580 err_t
hci_set_event_mask(u64_t ev_mask
)
583 struct pbuf
*p
= NULL
;
585 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_SET_EV_MASK_PLEN
,PBUF_RAM
))==NULL
) {
586 ERROR("hci_set_event_mask: Could not allocate memory for pbuf\n");
590 /* Assembling command packet */
591 p
= hci_cmd_ass(p
,HCI_SET_EV_MASK_OCF
,HCI_HC_BB_OGF
,HCI_SET_EV_MASK_PLEN
);
593 mask
= htole64(ev_mask
);
594 memcpy(((u8_t
*)p
->payload
)+4,&mask
,8);
596 physbusif_output(p
,p
->tot_len
);
602 err_t
hci_write_local_name(u8_t
*name
,u8_t len
)
604 struct pbuf
*p
= NULL
;
606 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_LOCAL_NAME_PLEN
,PBUF_RAM
))==NULL
) {
607 ERROR("hci_write_local_name: Could not allocate memory for pbuf\n");
611 /* Assembling command packet */
612 p
= hci_cmd_ass(p
,HCI_W_LOCAL_NAME_OCF
,HCI_HC_BB_OGF
,HCI_W_LOCAL_NAME_PLEN
);
613 /* Assembling cmd prameters */
614 memcpy(((u8_t
*)p
->payload
) + 4, name
, len
);
616 physbusif_output(p
, p
->tot_len
);
622 err_t
hci_write_pin_type(u8_t type
)
624 struct pbuf
*p
= NULL
;
626 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_PIN_TYPE_PLEN
,PBUF_RAM
))==NULL
) {
627 ERROR("hci_write_local_name: Could not allocate memory for pbuf\n");
631 /* Assembling command packet */
632 p
= hci_cmd_ass(p
,HCI_W_PIN_TYPE_OCF
,HCI_HC_BB_OGF
,HCI_W_PIN_TYPE_PLEN
);
633 /* Assembling cmd prameters */
634 ((u8_t
*)p
->payload
)[4] = type
;
636 physbusif_output(p
, p
->tot_len
);
642 err_t
hci_read_remote_name(struct bd_addr
*bdaddr
)
645 struct pbuf
*p
= NULL
;
646 struct hci_inq_res
*ires
;
647 u8_t page_scan_repetition_mode
, page_scan_mode
;
649 for(ires
=hci_dev
->ires
;ires
!=NULL
;ires
=ires
->next
) {
650 if(bd_addr_cmp(&(ires
->bdaddr
),bdaddr
)) {
651 page_scan_repetition_mode
= ires
->psrm
;
652 page_scan_mode
= ires
->psm
;
653 clock_offset
= ires
->co
;
659 page_scan_repetition_mode
= 0x01;
660 page_scan_mode
= 0x00;
664 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_R_REMOTE_NAME_PLEN
,PBUF_RAM
))==NULL
) {
665 ERROR("hci_read_remote_name: Could not allocate memory for pbuf\n");
669 /* Assembling command packet */
670 p
= hci_cmd_ass(p
,HCI_R_REMOTE_NAME_OCF
,HCI_LINK_CTRL_OGF
,HCI_R_REMOTE_NAME_PLEN
);
671 /* Assembling cmd prameters */
672 memcpy(((u8_t
*)p
->payload
+4),bdaddr
->addr
,6);
673 ((u8_t
*)p
->payload
)[10] = page_scan_repetition_mode
;
674 ((u8_t
*)p
->payload
)[11] = page_scan_mode
;
675 ((u16_t
*)p
->payload
)[6] = htole16(clock_offset
);
677 physbusif_output(p
, p
->tot_len
);
684 err_t
hci_write_inquiry_mode(u8_t mode
)
686 struct pbuf
*p
= NULL
;
688 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_INQUIRY_MODE_PLEN
,PBUF_RAM
))==NULL
) {
689 ERROR("hci_write_inquiry_mode: Could not allocate memory for pbuf\n");
693 /* Assembling command packet */
694 p
= hci_cmd_ass(p
,HCI_W_INQUIRY_MODE_OCF
,HCI_HC_BB_OGF
,HCI_W_INQUIRY_MODE_PLEN
);
695 /* Assembling cmd prameters */
696 ((u8_t
*)p
->payload
)[4] = mode
;
698 physbusif_output(p
, p
->tot_len
);
704 err_t
hci_write_page_scan_type(u8_t type
)
706 struct pbuf
*p
= NULL
;
708 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_PAGE_SCAN_TYPE_PLEN
,PBUF_RAM
))==NULL
) {
709 ERROR("hci_write_inquiry_mode: Could not allocate memory for pbuf\n");
713 /* Assembling command packet */
714 p
= hci_cmd_ass(p
,HCI_W_PAGE_SCAN_TYPE_OCF
,HCI_HC_BB_OGF
,HCI_W_PAGE_SCAN_TYPE_PLEN
);
715 /* Assembling cmd prameters */
716 ((u8_t
*)p
->payload
)[4] = type
;
718 physbusif_output(p
, p
->tot_len
);
724 err_t
hci_write_inquiry_scan_type(u8_t type
)
726 struct pbuf
*p
= NULL
;
728 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_INQUIRY_SCAN_TYPE_PLEN
,PBUF_RAM
))==NULL
) {
729 ERROR("hci_write_inquiry_mode: Could not allocate memory for pbuf\n");
733 /* Assembling command packet */
734 p
= hci_cmd_ass(p
,HCI_W_INQUIRY_SCAN_TYPE_OCF
,HCI_HC_BB_OGF
,HCI_W_INQUIRY_SCAN_TYPE_PLEN
);
735 /* Assembling cmd prameters */
736 ((u8_t
*)p
->payload
)[4] = type
;
738 physbusif_output(p
, p
->tot_len
);
744 err_t
hci_vendor_specific_command(u8_t ocf
,u8_t ogf
,void *data
,u8_t len
)
746 struct pbuf
*p
= NULL
;
748 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_W_VENDOR_CMD_PLEN
+ len
,PBUF_RAM
))==NULL
) {
749 ERROR("hci_vendor_specific_patch: Could not allocate memory for pbuf\n");
753 /* Assembling command packet */
754 p
= hci_cmd_ass(p
,ocf
,ogf
,HCI_W_VENDOR_CMD_PLEN
+ len
);
755 /* Assembling cmd prameters */
756 memcpy(((u8_t
*)p
->payload
+ 4),data
,len
);
758 physbusif_output(p
, p
->tot_len
);
763 /*-----------------------------------------------------------------------------------*/
766 * Sets an ACL connection to low power Sniff mode.
768 /*-----------------------------------------------------------------------------------*/
769 err_t
hci_sniff_mode(struct bd_addr
*bdaddr
, u16_t max_interval
, u16_t min_interval
, u16_t attempt
, u16_t timeout
)
772 struct hci_link
*link
;
774 /* Check if an ACL connection exists */
775 link
= hci_get_link(bdaddr
);
778 ERROR("hci_sniff_mode: ACL connection does not exist\n");
782 if((p
= btpbuf_alloc(PBUF_TRANSPORT
, HCI_SNIFF_PLEN
, PBUF_RAM
)) == NULL
) { /* Alloc len of packet */
783 ERROR("hci_sniff_mode: Could not allocate memory for pbuf\n");
787 /* Assembling command packet */
788 p
= hci_cmd_ass(p
, HCI_SNIFF_MODE_OCF
, HCI_LINK_POLICY_OGF
, HCI_SNIFF_PLEN
);
789 /* Assembling cmd prameters */
790 ((u16_t
*)p
->payload
)[2] = htole16(link
->connhdl
);
791 ((u16_t
*)p
->payload
)[3] = htole16(max_interval
);
792 ((u16_t
*)p
->payload
)[4] = htole16(min_interval
);
793 ((u16_t
*)p
->payload
)[5] = htole16(attempt
);
794 ((u16_t
*)p
->payload
)[6] = htole16(timeout
);
796 physbusif_output(p
, p
->tot_len
);
801 /*-----------------------------------------------------------------------------------*/
802 /* hci_write_link_policy_settings():
804 * Control the modes (park, sniff, hold) that an ACL connection can take.
807 /*-----------------------------------------------------------------------------------*/
808 err_t
hci_write_link_policy_settings(struct bd_addr
*bdaddr
, u16_t link_policy
)
811 struct hci_link
*link
;
813 /* Check if an ACL connection exists */
814 link
= hci_get_link(bdaddr
);
817 ERROR("hci_write_link_policy_settings: ACL connection does not exist\n");
821 if( (p
= btpbuf_alloc(PBUF_TRANSPORT
, HCI_W_LINK_POLICY_PLEN
, PBUF_RAM
)) == NULL
) { /* Alloc len of packet */
822 ERROR("hci_write_link_policy_settings: Could not allocate memory for pbuf\n");
825 /* Assembling command packet */
826 p
= hci_cmd_ass(p
, HCI_W_LINK_POLICY_OCF
, HCI_LINK_POLICY_OGF
, HCI_W_LINK_POLICY_PLEN
);
828 /* Assembling cmd prameters */
829 ((u16_t
*)p
->payload
)[2] = htole16(link
->connhdl
);
830 ((u16_t
*)p
->payload
)[3] = htole16(link_policy
);
832 physbusif_output(p
, p
->tot_len
);
837 /*-----------------------------------------------------------------------------------*/
838 /* hci_pin_code_request_reply():
840 * Used to reply to a PIN Code Request event from the Host Controller and specifies
841 * the PIN code to use for a connection.
843 /*-----------------------------------------------------------------------------------*/
844 err_t
hci_pin_code_request_reply(struct bd_addr
*bdaddr
, u8_t pinlen
, u8_t
*pincode
)
848 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_PIN_CODE_REQ_REP_PLEN
, PBUF_RAM
)) == NULL
) {
849 ERROR("hci_pin_code_request_reply: Could not allocate memory for pbuf\n");
853 /* Reset buffer content just to make sure */
854 memset((u8_t
*)p
->payload
, 0, HCI_PIN_CODE_REQ_REP_PLEN
);
856 /* Assembling command packet */
857 p
= hci_cmd_ass(p
, HCI_PIN_CODE_REQ_REP
, HCI_LINK_CTRL_OGF
, HCI_PIN_CODE_REQ_REP_PLEN
);
858 /* Assembling cmd prameters */
859 memcpy(((u8_t
*)p
->payload
) + 4, bdaddr
->addr
, 6);
860 ((u8_t
*)p
->payload
)[10] = pinlen
;
861 memcpy(((u8_t
*)p
->payload
) + 11, pincode
, pinlen
);
863 physbusif_output(p
, p
->tot_len
);
869 /*-----------------------------------------------------------------------------------*/
870 /* hci_pin_code_request_neg_reply():
872 * Used to reply to a PIN Code Request event from the Host Controller when the Host
873 * cannot specify a PIN code to use for a connection.
875 /*-----------------------------------------------------------------------------------*/
876 err_t
hci_pin_code_request_neg_reply(struct bd_addr
*bdaddr
)
880 if((p
=btpbuf_alloc(PBUF_RAW
,HCI_PIN_CODE_REQ_NEG_REP_PLEN
,PBUF_RAM
)) == NULL
) {
881 ERROR("hci_pin_code_request_neg_reply: Could not allocate memory for pbuf\n");
885 p
= hci_cmd_ass(p
,HCI_PIN_CODE_REQ_NEG_REP
,HCI_LINK_CTRL_OGF
,HCI_PIN_CODE_REQ_NEG_REP_PLEN
);
886 memcpy(((u8_t
*)p
->payload
)+4, bdaddr
->addr
, 6);
888 physbusif_output(p
,p
->tot_len
);
894 /*-----------------------------------------------------------------------------------*/
897 * Used to terminate an existing connection.
899 /*-----------------------------------------------------------------------------------*/
900 err_t
hci_disconnect(struct bd_addr
*bdaddr
, u8_t reason
)
903 struct hci_link
*link
;
905 link
= hci_get_link(bdaddr
);
908 ERROR("hci_disconnect: Connection does not exist\n");
909 return ERR_CONN
; /* Connection does not exist */
911 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_DISCONN_PLEN
, PBUF_RAM
)) == NULL
) {
912 ERROR("hci_disconnect: Could not allocate memory for pbuf\n");
913 return ERR_MEM
; /* Could not allocate memory for pbuf */
915 /* Assembling command packet */
916 p
= hci_cmd_ass(p
, HCI_DISCONN_OCF
, HCI_LINK_CTRL_OGF
, HCI_DISCONN_PLEN
);
918 /* Assembling cmd prameters */
919 ((u16_t
*)p
->payload
)[2] = htole16(link
->connhdl
);
920 ((u8_t
*)p
->payload
)[6] = reason
;
922 physbusif_output(p
, p
->tot_len
);
928 /*-----------------------------------------------------------------------------------*/
929 /* hci_reject_connection_request():
931 * Used to decline a new incoming connection request.
933 /*-----------------------------------------------------------------------------------*/
934 err_t
hci_reject_connection_request(struct bd_addr
*bdaddr
, u8_t reason
)
938 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_REJECT_CONN_REQ_PLEN
, PBUF_RAM
)) == NULL
) {
939 ERROR("hci_reject_connection_request: Could not allocate memory for pbuf\n");
942 /* Assembling command packet */
943 p
= hci_cmd_ass(p
, HCI_REJECT_CONN_REQ_OCF
, HCI_LINK_CTRL_OGF
, HCI_REJECT_CONN_REQ_PLEN
);
944 /* Assembling cmd prameters */
945 memcpy(((u8_t
*)p
->payload
) + 4, bdaddr
->addr
, 6);
946 ((u8_t
*)p
->payload
)[10] = reason
;
948 physbusif_output(p
, p
->tot_len
);
954 /*-----------------------------------------------------------------------------------*/
955 /* hci_write_stored_link_key():
957 * Writes a link key to be stored in the Bluetooth host controller.
959 /*-----------------------------------------------------------------------------------*/
960 err_t
hci_write_stored_link_key(struct bd_addr
*bdaddr
, u8_t
*link
)
964 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_WRITE_STORED_LINK_KEY_PLEN
, PBUF_RAM
)) == NULL
) {
965 ERROR("hci_write_stored_link_key: Could not allocate memory for pbuf\n");
968 /* Assembling command packet */
969 p
= hci_cmd_ass(p
, HCI_WRITE_STORED_LINK_KEY
, HCI_HC_BB_OGF
, HCI_WRITE_STORED_LINK_KEY_PLEN
);
970 /* Assembling cmd prameters */
971 ((u8_t
*)p
->payload
)[4] = 0x01;
972 memcpy(((u8_t
*)p
->payload
) + 5, bdaddr
->addr
, 6);
973 memcpy(((u8_t
*)p
->payload
) + 11, link
, 16);
975 physbusif_output(p
, p
->tot_len
);
981 /*-----------------------------------------------------------------------------------*/
984 * Write the value for the Class_of_Device parameter, which is used to indicate its
985 * capabilities to other devices.
987 /*-----------------------------------------------------------------------------------*/
988 err_t
hci_write_cod(u8_t
*cod
)
992 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_W_COD_PLEN
, PBUF_RAM
)) == NULL
) {
993 ERROR("hci_write_cod: Could not allocate memory for pbuf\n");
996 /* Assembling command packet */
997 p
= hci_cmd_ass(p
, HCI_W_COD_OCF
, HCI_HC_BB_OGF
, HCI_W_COD_PLEN
);
998 /* Assembling cmd prameters */
999 memcpy(((u8_t
*)p
->payload
)+4, cod
, 3);
1001 physbusif_output(p
, p
->tot_len
);
1007 err_t
hci_read_current_lap(void)
1011 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_R_CUR_IACLAP_PLEN
, PBUF_RAM
)) == NULL
) {
1012 ERROR("hci_read_current_lap: Could not allocate memory for pbuf\n");
1015 /* Assembling command packet */
1016 p
= hci_cmd_ass(p
, HCI_R_CUR_IACLAP_OCF
, HCI_HC_BB_OGF
, HCI_R_CUR_IACLAP_PLEN
);
1018 physbusif_output(p
, p
->tot_len
);
1024 /*-----------------------------------------------------------------------------------*/
1025 /* hci_set_hc_to_h_fc():
1027 * Used by the Host to turn flow control on or off in the direction from the Host
1028 * Controller to the Host.
1030 /*-----------------------------------------------------------------------------------*/
1031 err_t
hci_set_hc_to_h_fc(void)
1035 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_SET_HC_TO_H_FC_PLEN
, PBUF_RAM
)) == NULL
) {
1036 ERROR("hci_set_hc_to_h_fc: Could not allocate memory for pbuf\n");
1039 /* Assembling command packet */
1040 p
= hci_cmd_ass(p
, HCI_SET_HC_TO_H_FC_OCF
, HCI_HC_BB_OGF
, HCI_SET_HC_TO_H_FC_PLEN
);
1041 /* Assembling cmd prameters */
1042 ((u8_t
*)p
->payload
)[4] = 0x01; /* Flow control on for HCI ACL Data Packets and off for HCI
1043 SCO Data Packets in direction from Host Controller to
1045 physbusif_output(p
, p
->tot_len
);
1051 /*-----------------------------------------------------------------------------------*/
1052 /* hci_host_buffer_size():
1054 * Used by the Host to notify the Host Controller about the maximum size of the data
1055 * portion of HCI ACL Data Packets sent from the Host Controller to the Host.
1057 /*-----------------------------------------------------------------------------------*/
1058 err_t
hci_host_buffer_size(void)
1061 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_H_BUF_SIZE_PLEN
, PBUF_RAM
)) == NULL
) {
1062 ERROR("hci_host_buffer_size: Could not allocate memory for pbuf\n");
1065 /* Assembling command packet */
1066 p
= hci_cmd_ass(p
, HCI_H_BUF_SIZE_OCF
, HCI_HC_BB_OGF
, HCI_H_BUF_SIZE_PLEN
);
1067 ((u16_t
*)p
->payload
)[2] = htole16(HCI_HOST_ACL_MAX_LEN
); /* Host ACL data packet maximum length */
1068 ((u8_t
*)p
->payload
)[6] = 255; /* Host SCO Data Packet Length */
1069 *((u16_t
*)(((u8_t
*)p
->payload
)+7)) = htole16(HCI_HOST_MAX_NUM_ACL
); /* Host max total num ACL data packets */
1070 ((u16_t
*)p
->payload
)[4] = htole16(1); /* Host Total Num SCO Data Packets */
1071 physbusif_output(p
, p
->tot_len
);
1074 hci_dev
->host_num_acl
= HCI_HOST_MAX_NUM_ACL
;
1079 /*-----------------------------------------------------------------------------------*/
1080 /* hci_host_num_comp_packets():
1082 * Used by the Host to indicate to the Host Controller the number of HCI Data Packets
1083 * that have been completed for each Connection Handle since the previous
1084 * Host_Number_Of_Completed_Packets command was sent to the Host Controller.
1086 /*-----------------------------------------------------------------------------------*/
1087 err_t
hci_host_num_comp_packets(u16_t conhdl
, u16_t num_complete
)
1091 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_H_NUM_COMPL_PLEN
, PBUF_RAM
)) == NULL
) {
1092 ERROR("hci_host_num_comp_packets: Could not allocate memory for pbuf\n");
1095 /* Assembling command packet */
1096 p
= hci_cmd_ass(p
, HCI_H_NUM_COMPL_OCF
, HCI_HC_BB_OGF
, HCI_H_NUM_COMPL_PLEN
);
1097 ((u8_t
*)p
->payload
)[4] = 1;
1098 *(u16_t
*)(p
->payload
+5) = htole16(conhdl
);
1099 *(u16_t
*)(p
->payload
+7) = htole16(num_complete
); /* Number of completed acl packets */
1101 physbusif_output(p
, p
->tot_len
);
1104 hci_dev
->host_num_acl
+= num_complete
;
1109 /*-----------------------------------------------------------------------------------*/
1110 /* lp_pdu_maxsize():
1112 * Called by L2CAP to check the maxsize of the PDU. In this case it is the largest
1113 * ACL packet that the Host Controller can buffer.
1115 /*-----------------------------------------------------------------------------------*/
1116 u16_t
lp_pdu_maxsize()
1118 return hci_dev
->acl_mtu
;
1121 /*-----------------------------------------------------------------------------------*/
1122 /* lp_is_connected():
1124 * Called by L2CAP to check if an active ACL connection exists for the specified
1125 * Bluetooth address.
1127 /*-----------------------------------------------------------------------------------*/
1128 u8_t
lp_is_connected(struct bd_addr
*bdaddr
)
1130 struct hci_link
*link
;
1132 link
= hci_get_link(bdaddr
);
1140 /*-----------------------------------------------------------------------------------*/
1143 * Called by L2CAP to send data to the Host Controller that will be transfered over
1144 * the ACL link from there.
1146 /*-----------------------------------------------------------------------------------*/
1147 err_t
lp_acl_write(struct bd_addr
*bdaddr
,struct pbuf
*p
,u16_t len
,u8_t pb
)
1150 struct hci_link
*link
;
1151 struct hci_acl_hdr
*aclhdr
;
1154 link
= hci_get_link(bdaddr
);
1156 ERROR("lp_acl_write: ACL connection does not exist\n");
1160 if(hci_dev
->acl_max_pkt
==0) {
1162 /* Packet can be queued? */
1163 if(link
->p
!= NULL
) {
1164 LOG("lp_acl_write: Host buffer full. Dropped packet\n");
1165 return ERR_OK
; /* Drop packet */
1167 /* Copy PBUF_REF referenced payloads into PBUF_RAM */
1169 /* Remember pbuf to queue, if any */
1173 /* Pbufs are queued, increase the reference count */
1175 LOG("lp_acl_write: Host queued packet %p\n", (void *)p
);
1180 if((q
=btpbuf_alloc(PBUF_RAW
,HCI_ACL_HDR_LEN
+1,PBUF_RAM
))==NULL
) {
1181 ERROR("lp_acl_write: Could not allocate memory for pbuf\n");
1186 ((u8_t
*)q
->payload
)[0] = HCI_ACL_DATA_PACKET
;
1188 aclhdr
= (void*)((u8_t
*)q
->payload
+1);
1189 //aclhdr->connhdl_pb_bc = CONNPBBC(link->connhdl,pb,0);
1190 connhdlpbbc
= link
->connhdl
; /* Received from connection complete event */
1191 connhdlpbbc
|= (pb
<<12); /* Packet boundary flag */
1192 connhdlpbbc
&= 0x3FFF; /* Point-to-point */
1193 aclhdr
->connhdl_pb_bc
= htole16(connhdlpbbc
);
1194 aclhdr
->len
= htole16(len
);
1196 physbusif_output(q
,(q
->len
+len
));
1197 --hci_dev
->acl_max_pkt
;
1199 p
= btpbuf_dechain(q
);
1204 /*-----------------------------------------------------------------------------------*/
1205 /* lp_write_flush_timeout():
1207 * Called by L2CAP to set the flush timeout for the ACL link.
1209 /*-----------------------------------------------------------------------------------*/
1210 err_t
lp_write_flush_timeout(struct bd_addr
*bdaddr
, u16_t flushto
)
1212 struct hci_link
*link
;
1215 /* Check if an ACL connection exists */
1216 link
= hci_get_link(bdaddr
);
1219 ERROR("lp_write_flush_timeout: ACL connection does not exist\n");
1223 if((p
= btpbuf_alloc(PBUF_TRANSPORT
, HCI_W_FLUSHTO_PLEN
, PBUF_RAM
)) == NULL
) { /* Alloc len of packet */
1224 ERROR("lp_write_flush_timeout: Could not allocate memory for pbuf\n");
1228 /* Assembling command packet */
1229 p
= hci_cmd_ass(p
, HCI_W_FLUSHTO
, HCI_HC_BB_OGF
, HCI_W_FLUSHTO_PLEN
);
1230 /* Assembling cmd prameters */
1231 ((u16_t
*)p
->payload
)[2] = htole16(link
->connhdl
);
1232 ((u16_t
*)p
->payload
)[3] = htole16(flushto
);
1234 physbusif_output(p
, p
->tot_len
);
1239 /*-----------------------------------------------------------------------------------*/
1240 /* lp_connect_req():
1242 * Called by L2CAP to cause the Link Manager to create a connection to the
1243 * Bluetooth device with the BD_ADDR specified by the command parameters.
1245 /*-----------------------------------------------------------------------------------*/
1246 err_t
lp_connect_req(struct bd_addr
*bdaddr
, u8_t allow_role_switch
)
1248 u8_t page_scan_repetition_mode
, page_scan_mode
;
1251 struct hci_link
*link
= hci_new();
1252 struct hci_inq_res
*inqres
;
1255 ERROR("lp_connect_req: Could not allocate memory for link\n");
1256 return ERR_MEM
; /* Could not allocate memory for link */
1259 bd_addr_set(&(link
->bdaddr
), bdaddr
);
1260 HCI_REG(&(hci_active_links
), link
);
1263 /* Check if module has been discovered in a recent inquiry */
1264 for(inqres
= hci_dev
->ires
; inqres
!= NULL
; inqres
= inqres
->next
) {
1265 if(bd_addr_cmp(&inqres
->bdaddr
, bdaddr
)) {
1266 page_scan_repetition_mode
= inqres
->psrm
;
1267 page_scan_mode
= inqres
->psm
;
1268 clock_offset
= inqres
->co
;
1272 if(inqres
== NULL
) {
1273 /* No information on parameters from an inquiry. Using default values */
1274 page_scan_repetition_mode
= 0x01; /* Assuming worst case: time between
1275 successive page scans starting
1277 page_scan_mode
= 0x00; /* Assumes the device uses mandatory scanning, most
1278 devices use this. If no conn is established, try
1279 again w this parm set to optional page scanning */
1280 clock_offset
= 0x00; /* If the device was not found in a recent inquiry
1281 this information is irrelevant */
1284 if((p
= btpbuf_alloc(PBUF_RAW
, HCI_CREATE_CONN_PLEN
, PBUF_RAM
)) == NULL
) {
1285 ERROR("lp_connect_req: Could not allocate memory for pbuf\n");
1286 return ERR_MEM
; /* Could not allocate memory for pbuf */
1289 /* Assembling command packet */
1290 p
= hci_cmd_ass(p
, HCI_CREATE_CONN_OCF
, HCI_LINK_CTRL_OGF
, HCI_CREATE_CONN_PLEN
);
1291 /* Assembling cmd prameters */
1292 memcpy(((u8_t
*)p
->payload
)+4, bdaddr
->addr
, 6);
1293 ((u16_t
*)p
->payload
)[5] = htole16(hci_dev
->pkt_type
);
1294 ((u8_t
*)p
->payload
)[12] = page_scan_repetition_mode
;
1295 ((u8_t
*)p
->payload
)[13] = page_scan_mode
;
1296 ((u16_t
*)p
->payload
)[7] = htole16(clock_offset
);
1297 ((u8_t
*)p
->payload
)[16] = allow_role_switch
;
1299 physbusif_output(p
, p
->tot_len
);
1305 static void hci_cc_info_param(u8_t ocf
,struct pbuf
*p
)
1307 struct bd_addr
*bdaddr
;
1310 case HCI_READ_LOCAL_VERSION
:
1311 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1312 hci_dev
->info
.hci_version
= *((u8_t
*)p
->payload
+ 1);
1313 hci_dev
->info
.hci_revision
= le16toh(*(u16_t
*)((u8_t
*)p
->payload
+ 2));
1314 hci_dev
->info
.lmp_version
= *((u8_t
*)p
->payload
+ 4);
1315 hci_dev
->info
.manufacturer
= le16toh(*(u16_t
*)((u8_t
*)p
->payload
+ 5));
1316 hci_dev
->info
.lmp_subversion
= le16toh(*(u16_t
*)((u8_t
*)p
->payload
+ 7));
1317 LOG("hci_cc_info_param(HCI_READ_LOCAL_VERSION): hci_version = %02x, hci_revision = %04x, lmp_version = %02x, manufacturer = %04x, lmp_suversion = %04x\n",hci_dev
->info
.hci_version
,hci_dev
->info
.hci_revision
,hci_dev
->info
.lmp_version
,hci_dev
->info
.manufacturer
,hci_dev
->info
.lmp_subversion
);
1320 case HCI_READ_LOCAL_FEATURES
:
1321 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1322 memcpy(hci_dev
->features
,(void*)((u8_t
*)p
->payload
+1),sizeof(hci_dev
->features
));
1324 if(hci_dev
->features
[0]&LMP_3SLOT
)
1325 hci_dev
->pkt_type
|= (HCI_DM3
|HCI_DH3
);
1326 if(hci_dev
->features
[0]&LMP_5SLOT
)
1327 hci_dev
->pkt_type
|= (HCI_DM5
|HCI_DH5
);
1328 if(hci_dev
->features
[1]&LMP_HV2
)
1329 hci_dev
->pkt_type
|= HCI_HV2
;
1330 if(hci_dev
->features
[1]&LMP_HV3
)
1331 hci_dev
->pkt_type
|= HCI_HV3
;
1332 LOG("hci_cc_info_param(HCI_READ_LOCAL_FEATURES): %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",hci_dev
->features
[0],hci_dev
->features
[1],hci_dev
->features
[2],hci_dev
->features
[3],
1333 hci_dev
->features
[4],hci_dev
->features
[5],hci_dev
->features
[6],hci_dev
->features
[7]);
1336 case HCI_READ_BUFFER_SIZE
:
1337 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1338 hci_dev
->acl_mtu
= le16toh(*(u16_t
*)(((u8_t
*)p
->payload
)+1));
1339 hci_dev
->sco_mtu
= *((u8_t
*)p
->payload
+3);
1340 hci_dev
->acl_max_pkt
= le16toh(*(u16_t
*)(((u8_t
*)p
->payload
)+4));
1341 hci_dev
->sco_max_pkt
= le16toh(*(u16_t
*)(((u8_t
*)p
->payload
)+5));
1342 LOG("hci_cc_info_param(HCI_READ_BUFFER_SIZE): acl_mt = %d, sco_mt = %d, acl_max_pkt = %d, sco_max_pkt = %d\n",hci_dev
->acl_mtu
,hci_dev
->sco_mtu
,hci_dev
->acl_max_pkt
,hci_dev
->sco_max_pkt
);
1345 case HCI_READ_BD_ADDR
:
1346 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1347 bdaddr
= (void*)((u8_t
*)p
->payload
+1);
1348 LOG("hci_cc_info_param(HCI_READ_BD_ADDR): %02x:%02x:%02x:%02x:%02x:%02x",bdaddr
->addr
[0],bdaddr
->addr
[1],bdaddr
->addr
[2],bdaddr
->addr
[3],bdaddr
->addr
[4],bdaddr
->addr
[5]);
1349 bd_addr_set(&(hci_dev
->bdaddr
),bdaddr
);
1355 static void hci_cc_host_ctrl(u8_t ocf
,struct pbuf
*p
)
1360 //printf("hci_cc_host_ctrl(%02x)\n",ocf);
1362 case HCI_SET_HC_TO_H_FC
:
1363 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) hci_dev
->flow
= 1;
1365 case HCI_READ_CUR_IACLAP
:
1366 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1367 for(i
=0;i
<((u8_t
*)p
->payload
)[1];i
++) {
1369 lap
= (void*)(((u8_t
*)p
->payload
)+(2+resp_off
));
1370 printf("lap = 00%02x%02x%02x\n",lap
[2],lap
[1],lap
[0]);
1377 static void hci_cc_link_policy(u8_t ocf
,struct pbuf
*p
)
1380 struct hci_link
*link
;
1383 case HCI_W_LINK_POLICY
:
1384 if(((u8_t
*)p
->payload
)[0]==HCI_SUCCESS
) {
1385 for(link
=hci_active_links
;link
!=NULL
;link
=link
->next
) {
1386 if(link
->connhdl
==le16toh(*((u16_t
*)(((u8_t
*)p
->payload
)+1)))) break;
1389 LOG("hci_cc_link_policy: Connection does not exist\n");
1392 HCI_EVENT_WLP_COMPLETE(hci_dev
,&link
->bdaddr
,ret
);
1394 LOG("Unsuccessful HCI_W_LINK_POLICY.\n");
1400 static void hci_conn_request_evt(struct pbuf
*p
)
1405 struct bd_addr
*bdaddr
;
1406 struct hci_link
*link
;
1408 LOG("hci_conn_request_evt()\n");
1409 bdaddr
= (void*)((u8_t
*)p
->payload
);
1410 cod
= (((u8_t
*)p
->payload
)+6);
1411 link_type
= *(((u8_t
*)p
->payload
)+9);
1413 HCI_EVENT_CONN_REQ(hci_dev
,bdaddr
,cod
,link_type
,ret
);
1415 link
= hci_get_link(bdaddr
);
1417 if((link
=hci_new())==NULL
) {
1418 ERROR("hci_conn_request_evt: Could not allocate memory for link. Disconnect\n");
1422 bd_addr_set(&(link
->bdaddr
),bdaddr
);
1423 HCI_REG(&(hci_active_links
),link
);
1425 hci_accecpt_conn_request(bdaddr
,0x00);
1430 static void hci_conn_complete_evt(struct pbuf
*p
)
1433 struct bd_addr
*bdaddr
;
1434 struct hci_link
*link
;
1436 bdaddr
= (void*)(((u8_t
*)p
->payload
)+3);
1437 link
= hci_get_link(bdaddr
);
1438 LOG("hci_conn_complete_evt(%p,%02x - %02x:%02x:%02x:%02x:%02x:%02x)\n",link
,((u8_t
*)p
->payload
)[0],bdaddr
->addr
[0],bdaddr
->addr
[1],bdaddr
->addr
[2],bdaddr
->addr
[3],bdaddr
->addr
[4],bdaddr
->addr
[5]);
1439 switch(((u8_t
*)p
->payload
)[0]) {
1442 if((link
=hci_new())==NULL
) {
1443 ERROR("hci_conn_complete_evt: Could not allocate memory for link. Disconnect\n");
1444 hci_disconnect(bdaddr
, HCI_OTHER_END_TERMINATED_CONN_LOW_RESOURCES
);
1445 lp_disconnect_ind(bdaddr
,HCI_CONN_TERMINATED_BY_LOCAL_HOST
);
1448 bd_addr_set(&(link
->bdaddr
),bdaddr
);
1449 link
->connhdl
= le16toh(*((u16_t
*)(((u8_t
*)p
->payload
)+1)));
1450 HCI_REG(&(hci_active_links
),link
);
1451 HCI_EVENT_CONN_COMPLETE(hci_dev
,bdaddr
,ret
);
1452 lp_connect_ind(&(link
->bdaddr
));
1454 link
->connhdl
= le16toh(*((u16_t
*)(((u8_t
*)p
->payload
)+1)));
1455 HCI_EVENT_CONN_COMPLETE(hci_dev
,bdaddr
,ret
);
1456 lp_connect_cfm(&(link
->bdaddr
),((u8_t
*)p
->payload
)[10],ERR_OK
);
1459 case HCI_PAGE_TIMEOUT
:
1464 lp_connect_cfm(bdaddr
,((u8_t
*)p
->payload
)[10],ERR_CONN
);
1470 static void hci_inquiry_result_evt(struct pbuf
*p
)
1474 struct bd_addr
*bdaddr
;
1475 struct hci_inq_res
*ires
;
1477 num_resp
= ((u8_t
*)p
->payload
)[0];
1478 //printf("hci_inquriy_result_evt(%d)\n",num_resp);
1479 for(i
=0;i
<num_resp
&& i
<MEMB_NUM_HCI_INQ
;i
++) {
1481 bdaddr
= (void*)(((u8_t
*)p
->payload
)+(1+resp_off
));
1482 if((ires
=btmemb_alloc(&hci_inq_results
))!=NULL
) {
1483 bd_addr_set(&(ires
->bdaddr
),bdaddr
);
1484 ires
->psrm
= ((u8_t
*)p
->payload
)[7+resp_off
];
1485 ires
->psm
= ((u8_t
*)p
->payload
)[8+resp_off
];
1486 memcpy(ires
->cod
,((u8_t
*)p
->payload
)+10+resp_off
,3);
1487 ires
->co
= le16toh(*((u16_t
*)(((u8_t
*)p
->payload
)+13+resp_off
)));
1490 HCI_REG(&(hci_dev
->ires
),ires
);
1492 ERROR("hci_inquriy_result_evt: Could not allocate memory for inquiry result\n");
1497 static void hci_return_link_key_evt(struct pbuf
*p
)
1501 struct bd_addr
*bdaddr
;
1502 struct hci_link_key
*keyres
;
1504 num_keys
= ((u8_t
*)p
->payload
)[0];
1505 //printf("hci_return_link_key_evt(%d)\n",num_keys);
1506 for(i
=0;i
<num_keys
&& i
<MEMB_NUM_HCI_LINK_KEY
;i
++) {
1508 bdaddr
= (void*)(((u8_t
*)p
->payload
)+1+resp_off
);
1509 if((keyres
=btmemb_alloc(&hci_link_key_results
))!=NULL
) {
1510 bd_addr_set(&(keyres
->bdaddr
),bdaddr
);
1511 memcpy(keyres
->key
,((u8_t
*)p
->payload
)+7+resp_off
,16);
1512 keyres
->next
= NULL
;
1514 //printf("link key evt: %02x:%02x:%02x:%02x:%02x:%02x\n",bdaddr->addr[0],bdaddr->addr[1],bdaddr->addr[2],bdaddr->addr[3],bdaddr->addr[4],bdaddr->addr[5]);
1515 HCI_REG(&(hci_dev
->keyres
),keyres
);
1517 ERROR("hci_return_link_key_evt: Could not allocate memory for link key result\n");
1522 void hci_event_handler(struct pbuf
*p
)
1529 struct hci_link
*link
;
1530 struct bd_addr
*bdaddr
;
1531 struct hci_evt_hdr
*evthdr
;
1533 evthdr
= p
->payload
;
1534 btpbuf_header(p
,-HCI_EVENT_HDR_LEN
);
1536 switch(evthdr
->code
) {
1537 case HCI_INQUIRY_COMPLETE
:
1538 //printf("HCI_INQUIRY_COMPLETE\n");
1539 HCI_EVENT_INQ_COMPLETE(hci_dev
,((u8_t
*)p
->payload
)[0],ret
);
1541 case HCI_INQUIRY_RESULT
:
1542 hci_inquiry_result_evt(p
);
1544 case HCI_CONNECTION_COMPLETE
:
1545 hci_conn_complete_evt(p
);
1547 case HCI_CONNECTION_REQUEST
:
1548 hci_conn_request_evt(p
);
1550 case HCI_DISCONNECTION_COMPLETE
:
1551 switch(((u8_t
*)p
->payload
)[0]) {
1553 for(link
=hci_active_links
;link
!=NULL
;link
=link
->next
) {
1554 if(link
->connhdl
==le16toh(*((u16_t
*)(((u8_t
*)p
->payload
)+1)))) break;
1557 lp_disconnect_ind(&(link
->bdaddr
),((u8_t
*)p
->payload
)[3]);
1565 case HCI_ENCRYPTION_CHANGE
:
1567 case HCI_QOS_SETUP_COMPLETE
:
1569 case HCI_COMMAND_COMPLETE
:
1570 hci_dev
->num_cmd
+= ((u8_t
*)p
->payload
)[0];
1571 btpbuf_header(p
,-1);
1573 opc
= le16toh(((u16_t
*)p
->payload
)[0]);
1576 btpbuf_header(p
,-2);
1579 case HCI_INFO_PARAM
:
1580 hci_cc_info_param(ocf
,p
);
1582 case HCI_HOST_C_N_BB
:
1583 hci_cc_host_ctrl(ocf
,p
);
1585 case HCI_LINK_POLICY
:
1586 hci_cc_link_policy(ocf
,p
);
1589 HCI_EVENT_CMD_COMPLETE(hci_dev
,ogf
,ocf
,((u8_t
*)p
->payload
)[0],ret
);
1591 case HCI_COMMAND_STATUS
:
1592 if(((u8_t
*)p
->payload
)[0]!=HCI_SUCCESS
) {
1593 btpbuf_header(p
,-2);
1595 opc
= le16toh(((u16_t
*)p
->payload
)[0]);
1598 btpbuf_header(p
,-2);
1600 HCI_EVENT_CMD_COMPLETE(hci_dev
,ogf
,ocf
,((u8_t
*)p
->payload
)[0],ret
);
1603 hci_dev
->num_cmd
+= ((u8_t
*)p
->payload
)[1];
1605 case HCI_HARDWARE_ERROR
:
1606 //TODO: IS THIS FATAL??
1608 case HCI_ROLE_CHANGE
:
1610 case HCI_NBR_OF_COMPLETED_PACKETS
:
1611 for(i
=0;i
<((u8_t
*)p
->payload
)[0];i
++) {
1613 hci_dev
->acl_max_pkt
+= le16toh(*((u16_t
*)(((u8_t
*)p
->payload
) + 3 + resp_off
)));
1614 connhdl
= le16toh(*((u16_t
*)(((u8_t
*)p
->payload
) + 1 + resp_off
)));
1616 for(link
= hci_active_links
; link
!= NULL
; link
= link
->next
) {
1617 if(link
->connhdl
== connhdl
) break;
1620 q
= link
== NULL
? NULL
: link
->p
;
1621 /* Queued packet present? */
1623 /* NULL attached buffer immediately */
1625 /* Send the queued packet */
1626 lp_acl_write(&link
->bdaddr
, q
, link
->len
, link
->pb
);
1627 /* Free the queued packet */
1632 case HCI_MODE_CHANGE
:
1633 printf("HCI_MODE_CHANGE\n");
1635 case HCI_DATA_BUFFER_OVERFLOW
:
1636 //TODO: IS THIS FATAL????
1638 case HCI_MAX_SLOTS_CHANGE
:
1640 case HCI_RETURN_LINK_KEYS
:
1641 hci_return_link_key_evt(p
);
1643 case HCI_PIN_CODE_REQUEST
:
1644 bdaddr
= (void *)((u8_t
*)p
->payload
); /* Get the Bluetooth address */
1645 HCI_EVENT_PIN_REQ(hci_dev
, bdaddr
, ret
); /* Notify application. If event is not registered,
1646 send a negative reply */
1648 case HCI_LINK_KEY_NOTIFICATION
:
1649 bdaddr
= (void *)((u8_t
*)p
->payload
); /* Get the Bluetooth address */
1651 HCI_EVENT_LINK_KEY_NOT(hci_dev
, bdaddr
, ((u8_t
*)p
->payload
) + 6, ret
); /* Notify application.*/
1654 LOG("hci_event_input: Undefined event code 0x%x\n", evthdr
->code
);
1659 void hci_acldata_handler(struct pbuf
*p
)
1661 struct hci_acl_hdr
*aclhdr
;
1662 struct hci_link
*link
;
1665 aclhdr
= p
->payload
;
1666 btpbuf_header(p
, -HCI_ACL_HDR_LEN
);
1668 conhdl
= le16toh(aclhdr
->connhdl_pb_bc
) & 0x0FFF; /* Get the connection handle from the first
1671 //TODO: XXX??? DO WE SAVE NUMACL PACKETS COMPLETED IN LINKS LIST?? SHOULD WE CALL
1672 //hci_host_num_comp_packets from the main loop when no data has been received from the
1674 --hci_dev
->host_num_acl
;
1675 if(hci_dev
->host_num_acl
== 0) {
1676 hci_host_num_comp_packets(conhdl
, HCI_HOST_MAX_NUM_ACL
);
1677 hci_dev
->host_num_acl
= HCI_HOST_MAX_NUM_ACL
;
1681 for(link
= hci_active_links
; link
!= NULL
; link
= link
->next
) {
1682 if(link
->connhdl
== conhdl
) {
1688 if(le16toh(aclhdr
->len
)) {
1689 //LOG("hci_acl_input: Forward ACL packet to higher layer p->tot_len = %d\n", p->tot_len);
1690 l2cap_input(p
, &(link
->bdaddr
));
1692 btpbuf_free(p
); /* If length of ACL packet is zero, we silently discard it */
1695 btpbuf_free(p
); /* If no acitve ACL link was found, we silently discard the packet */