7 #include <lwp_threads.h>
14 #include "physbusif.h"
17 #define STACKSIZE 32768
18 #define MQ_BOX_SIZE 256
20 /* Vendor specific OGF */
21 #define HCI_VENDOR_OGF 0x3f
23 /* Vendor specific OCF */
24 #define HCI_VENDOR_PATCH_START_OCF 0x4f
25 #define HCI_VENDOR_PATCH_CONT_OCF 0x4c
26 #define HCI_VENDOR_PATCH_END_OCF 0x4f
53 struct inquiry_info_ex
*info
;
65 s32 (*sent
)(void *arg
,struct bte_pcb
*pcb
,u8 err
);
67 struct ctrl_req_t
*next
;
70 static struct bt_state btstate
;
71 static u8_t bte_patch0
[184] = {
72 0x70,0x99,0x08,0x00,0x88,0x43,0xd1,0x07,0x09,0x0c,0x08,0x43,0xa0,0x62,0x19,0x23,
73 0xdb,0x01,0x33,0x80,0x7c,0xf7,0x88,0xf8,0x28,0x76,0x80,0xf7,0x17,0xff,0x43,0x78,
74 0xeb,0x70,0x19,0x23,0xdb,0x01,0x33,0x87,0x7c,0xf7,0xbc,0xfb,0x0b,0x60,0xa3,0x7b,
75 0x01,0x49,0x0b,0x60,0x90,0xf7,0x96,0xfb,0xd8,0x1d,0x08,0x00,0x00,0xf0,0x04,0xf8,
76 0x00,0x23,0x79,0xf7,0xe3,0xfa,0x00,0x00,0x00,0xb5,0x00,0x23,0x11,0x49,0x0b,0x60,
77 0x1d,0x21,0xc9,0x03,0x0b,0x60,0x7d,0x20,0x80,0x01,0x01,0x38,0xfd,0xd1,0x0e,0x4b,
78 0x0e,0x4a,0x13,0x60,0x47,0x20,0x00,0x21,0x96,0xf7,0x96,0xff,0x46,0x20,0x00,0x21,
79 0x96,0xf7,0x92,0xff,0x0a,0x4a,0x13,0x68,0x0a,0x48,0x03,0x40,0x13,0x60,0x0a,0x4a,
80 0x13,0x68,0x0a,0x48,0x03,0x40,0x13,0x60,0x09,0x4a,0x13,0x68,0x09,0x48,0x03,0x40,
81 0x13,0x60,0x00,0xbd,0x24,0x80,0x0e,0x00,0x81,0x03,0x0f,0xfe,0x5c,0x00,0x0f,0x00,
82 0x60,0xfc,0x0e,0x00,0xfe,0xff,0x00,0x00,0xfc,0xfc,0x0e,0x00,0xff,0x9f,0x00,0x00,
83 0x30,0xfc,0x0e,0x00,0x7f,0xff,0x00,0x00
85 static u8_t bte_patch1
[92] = {
86 0x07,0x20,0xbc,0x65,0x01,0x00,0x84,0x42,0x09,0xd2,0x84,0x42,0x09,0xd1,0x21,0x84,
87 0x5a,0x00,0x00,0x83,0xf0,0x74,0xff,0x09,0x0c,0x08,0x43,0x22,0x00,0x61,0x00,0x00,
88 0x83,0xf0,0x40,0xfc,0x00,0x00,0x00,0x00,0x23,0xcc,0x9f,0x01,0x00,0x6f,0xf0,0xe4,
89 0xfc,0x03,0x28,0x7d,0xd1,0x24,0x3c,0x62,0x01,0x00,0x28,0x20,0x00,0xe0,0x60,0x8d,
90 0x23,0x68,0x25,0x04,0x12,0x01,0x00,0x20,0x1c,0x20,0x1c,0x24,0xe0,0xb0,0x21,0x26,
91 0x74,0x2f,0x00,0x00,0x86,0xf0,0x18,0xfd,0x21,0x4f,0x3b,0x60
94 static u8 ppc_stack
[STACKSIZE
] ATTRIBUTE_ALIGN(8);
96 err_t
acl_wlp_completed(void *arg
,struct bd_addr
*bdaddr
);
97 err_t
link_key_not(void *arg
,struct bd_addr
*bdaddr
,u8_t
*key
);
98 err_t
pin_req(void *arg
,struct bd_addr
*bdaddr
);
99 err_t
l2cap_connected(void *arg
,struct l2cap_pcb
*l2cappcb
,u16_t result
,u16_t status
);
100 err_t
l2cap_accepted(void *arg
,struct l2cap_pcb
*l2cappcb
,err_t err
);
101 err_t
acl_conn_complete(void *arg
,struct bd_addr
*bdaddr
);
102 err_t
l2cap_disconnect_cfm(void *arg
, struct l2cap_pcb
*pcb
);
103 err_t
l2cap_disconnected_ind(void *arg
, struct l2cap_pcb
*pcb
, err_t err
);
105 err_t
bte_input(void *arg
,struct l2cap_pcb
*pcb
,struct pbuf
*p
,err_t err
);
106 err_t
bte_callback(void (*f
)(void*),void *ctx
);
107 err_t
bte_hci_apply_patch_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
);
108 err_t
bte_hci_patch_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
);
109 err_t
bte_hci_initcore_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
);
110 err_t
bte_hci_initsub_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
);
111 err_t
bte_inquiry_complete(void *arg
,struct hci_pcb
*pcb
,struct hci_inq_res
*ires
,u16_t result
);
112 err_t
bte_read_stored_link_key_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
);
114 MEMB(bte_pcbs
,sizeof(struct bte_pcb
),MEMP_NUM_BTE_PCB
);
115 MEMB(bte_ctrl_reqs
,sizeof(struct ctrl_req_t
),MEMP_NUM_BTE_CTRLS
);
117 static void bte_reset_all()
119 btmemb_init(&bte_pcbs
);
120 btmemb_init(&bte_ctrl_reqs
);
122 if(btstate
.info
!=NULL
) free(btstate
.info
);
125 btstate
.hci_inited
= 0;
126 btstate
.hci_cmddone
= 0;
127 btstate
.num_founddevs
= 0;
128 btstate
.last_err
= ERR_OK
;
131 static void bt_alarmhandler(syswd_t alarm
,void *cbarg
)
133 __lwp_thread_dispatchdisable();
134 SYS_SwitchFiber(0,0,0,0,(u32
)l2cap_tmr
,(u32
)(&ppc_stack
[STACKSIZE
]));
135 __lwp_thread_dispatchunnest();
138 static inline s32
__bte_waitcmdfinish(struct bt_state
*state
)
143 if(!state
) return ERR_VAL
;
145 _CPU_ISR_Disable(level
);
146 while(!state
->hci_cmddone
)
147 LWP_ThreadSleep(state
->hci_cmdq
);
148 ret
= state
->last_err
;
149 _CPU_ISR_Restore(level
);
154 static inline s32
__bte_cmdfinish(struct bt_state
*state
,err_t err
)
158 if(!state
) return ERR_VAL
;
160 _CPU_ISR_Disable(level
);
161 state
->last_err
= err
;
162 state
->hci_cmddone
= 1;
164 state
->cb(err
,state
->usrdata
);
166 LWP_ThreadSignal(state
->hci_cmdq
);
167 _CPU_ISR_Restore(level
);
172 static inline s32
__bte_waitrequest(struct ctrl_req_t
*req
)
177 if(!req
|| !req
->pcb
) return ERR_VAL
;
179 _CPU_ISR_Disable(level
);
180 while(req
->state
!=STATE_SENT
181 && req
->state
!=STATE_FAILED
)
183 LWP_ThreadSleep(req
->pcb
->cmdq
);
186 _CPU_ISR_Restore(level
);
191 static inline void __bte_close_ctrl_queue(struct bte_pcb
*pcb
)
193 struct ctrl_req_t
*req
;
195 while(pcb
->ctrl_req_head
!=NULL
) {
196 req
= pcb
->ctrl_req_head
;
198 req
->state
= STATE_DISCONNECTED
;
199 if(req
->sent
!=NULL
) {
200 req
->sent(pcb
->cbarg
,pcb
,ERR_CLSD
);
201 btmemb_free(&bte_ctrl_reqs
,req
);
203 LWP_ThreadSignal(pcb
->cmdq
);
205 pcb
->ctrl_req_head
= req
->next
;
207 pcb
->ctrl_req_tail
= NULL
;
210 static s32
__bte_send_pending_request(struct bte_pcb
*pcb
)
213 struct ctrl_req_t
*req
;
215 if(pcb
->ctrl_req_head
==NULL
) return ERR_OK
;
216 if(pcb
->state
==STATE_DISCONNECTING
|| pcb
->state
==STATE_DISCONNECTED
) return ERR_CLSD
;
218 req
= pcb
->ctrl_req_head
;
219 req
->state
= STATE_SENDING
;
221 err
= l2ca_datawrite(pcb
->out_pcb
,req
->p
);
225 pcb
->ctrl_req_head
= req
->next
;
228 req
->state
= STATE_FAILED
;
230 req
->sent(pcb
->cbarg
,pcb
,err
);
231 btmemb_free(&bte_ctrl_reqs
,req
);
233 LWP_ThreadSignal(pcb
->cmdq
);
239 static s32
__bte_send_request(struct ctrl_req_t
*req
)
246 req
->state
= STATE_READY
;
248 _CPU_ISR_Disable(level
);
249 if(req
->pcb
->ctrl_req_head
==NULL
) {
250 req
->pcb
->ctrl_req_head
= req
->pcb
->ctrl_req_tail
= req
;
251 err
= __bte_send_pending_request(req
->pcb
);
253 req
->pcb
->ctrl_req_tail
->next
= req
;
254 req
->pcb
->ctrl_req_tail
= req
;
257 _CPU_ISR_Restore(level
);
262 static err_t
__bte_shutdown_finished(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
265 struct bt_state
*state
= (struct bt_state
*)arg
;
267 if(state
==NULL
) return ERR_OK
;
269 state
->hci_inited
= 0;
270 hci_cmd_complete(NULL
);
271 if(result
==HCI_SUCCESS
)
277 return __bte_cmdfinish(state
,err
);
280 static void bte_process_handshake(struct bte_pcb
*pcb
,u8_t param
,void *buf
,u16_t len
)
283 struct ctrl_req_t
*req
;
285 LOG("bte_process_handshake(%p)\n",pcb
);
288 case HIDP_HSHK_SUCCESSFULL
:
289 req
= pcb
->ctrl_req_head
;
290 pcb
->ctrl_req_head
= req
->next
;
293 req
->state
= STATE_SENT
;
295 req
->sent(pcb
->cbarg
,pcb
,ERR_OK
);
296 btmemb_free(&bte_ctrl_reqs
,req
);
298 LWP_ThreadSignal(pcb
->cmdq
);
300 err
= __bte_send_pending_request(pcb
);
302 case HIDP_HSHK_NOTREADY
:
303 case HIDP_HSHK_INV_REPORTID
:
304 case HIDP_HSHK_NOTSUPPORTED
:
305 case HIDP_HSHK_IVALIDPARAM
:
306 case HIDP_HSHK_UNKNOWNERROR
:
308 case HIDP_HSHK_FATALERROR
:
315 static void bte_process_data(struct bte_pcb
*pcb
,u8_t param
,void *buf
,u16_t len
)
317 LOG("bte_process_data(%p)\n",pcb
);
319 case HIDP_DATA_RTYPE_INPUT
:
320 if(pcb
->recv
!=NULL
) pcb
->recv(pcb
->cbarg
,buf
,len
);
322 case HIDP_DATA_RTYPE_OTHER
:
323 case HIDP_DATA_RTYPE_OUPUT
:
324 case HIDP_DATA_RTYPE_FEATURE
:
331 static err_t
bte_process_input(void *arg
,struct l2cap_pcb
*pcb
,struct pbuf
*p
,err_t err
)
336 struct bte_pcb
*bte
= (struct bte_pcb
*)arg
;
338 LOG("bte_process_input(%p,%p)\n",bte
,p
);
340 if(bte
->state
==STATE_DISCONNECTING
341 || bte
->state
==STATE_DISCONNECTED
) return ERR_CLSD
;
348 type
= (hdr
&HIDP_HDR_TRANS_MASK
);
349 param
= (hdr
&HIDP_HDR_PARAM_MASK
);
351 case HIDP_TRANS_HANDSHAKE
:
352 bte_process_handshake(bte
,param
,buf
,len
);
354 case HIDP_TRANS_HIDCONTROL
:
356 case HIDP_TRANS_DATA
:
357 bte_process_data(bte
,param
,buf
,len
);
372 memset(&btstate
,0,sizeof(struct bt_state
));
378 LWP_InitQueue(&btstate
.hci_cmdq
);
379 SYS_CreateAlarm(&btstate
.timer_svc
);
381 _CPU_ISR_Disable(level
);
385 physbusif_reset_all();
387 hci_wlp_complete(acl_wlp_completed
);
388 hci_connection_complete(acl_conn_complete
);
389 _CPU_ISR_Restore(level
);
393 SYS_SetPeriodicAlarm(btstate
.timer_svc
,&tb
,&tb
,bt_alarmhandler
, NULL
);
400 if(btstate
.hci_inited
==0) return;
402 LOG("BTE_Shutdown()\n");
404 _CPU_ISR_Disable(level
);
406 btstate
.usrdata
= NULL
;
407 btstate
.hci_cmddone
= 0;
409 hci_cmd_complete(__bte_shutdown_finished
);
411 __bte_waitcmdfinish(&btstate
);
412 _CPU_ISR_Restore(level
);
414 physbusif_shutdown();
417 s32
BTE_InitCore(btecallback cb
)
421 _CPU_ISR_Disable(level
);
423 btstate
.usrdata
= NULL
;
424 btstate
.hci_cmddone
= 0;
426 hci_cmd_complete(bte_hci_initcore_complete
);
428 _CPU_ISR_Restore(level
);
433 s32
BTE_ApplyPatch(btecallback cb
)
438 _CPU_ISR_Disable(level
);
440 btstate
.usrdata
= NULL
;
441 btstate
.hci_cmddone
= 0;
443 hci_cmd_complete(bte_hci_apply_patch_complete
);
444 hci_vendor_specific_command(HCI_VENDOR_PATCH_START_OCF
,HCI_VENDOR_OGF
,&kick
,1);
445 _CPU_ISR_Restore(level
);
450 s32
BTE_InitSub(btecallback cb
)
454 _CPU_ISR_Disable(level
);
456 btstate
.usrdata
= NULL
;
457 btstate
.hci_cmddone
= 0;
459 hci_cmd_complete(bte_hci_initsub_complete
);
460 hci_write_inquiry_mode(0x01);
461 _CPU_ISR_Restore(level
);
466 s32
BTE_ReadStoredLinkKey(struct linkkey_info
*keys
,u8 max_cnt
,btecallback cb
)
470 _CPU_ISR_Disable(level
);
472 btstate
.usrdata
= keys
;
473 btstate
.num_maxdevs
= max_cnt
;
474 btstate
.hci_cmddone
= 0;
476 hci_cmd_complete(bte_read_stored_link_key_complete
);
477 hci_read_stored_link_key();
478 _CPU_ISR_Restore(level
);
483 void (*BTE_SetDisconnectCallback(void (*callback
)(struct bd_addr
*bdaddr
,u8 reason
)))(struct bd_addr
*bdaddr
,u8 reason
)
485 return l2cap_disconnect_bb(callback
);
488 struct bte_pcb
* bte_new()
492 if((pcb
=btmemb_alloc(&bte_pcbs
))==NULL
) return NULL
;
494 memset(pcb
,0,sizeof(struct bte_pcb
));
496 pcb
->state
= (u32
)STATE_NOTREADY
;
497 LWP_InitQueue(&(pcb
->cmdq
));
502 s32
bte_registerdeviceasync(struct bte_pcb
*pcb
,struct bd_addr
*bdaddr
,s32 (*conn_cfm
)(void *arg
,struct bte_pcb
*pcb
,u8 err
))
506 struct l2cap_pcb
*l2capcb
= NULL
;
508 //printf("bte_registerdeviceasync()\n");
509 _CPU_ISR_Disable(level
);
513 pcb
->conn_cfm
= conn_cfm
;
514 pcb
->state
= (u32
)STATE_CONNECTING
;
516 bd_addr_set(&(pcb
->bdaddr
),bdaddr
);
517 if((l2capcb
=l2cap_new())==NULL
) {
521 l2cap_arg(l2capcb
,pcb
);
523 err
= l2cap_connect_ind(l2capcb
,bdaddr
,HIDP_OUTPUT_CHANNEL
,l2cap_accepted
);
525 l2cap_close(l2capcb
);
530 if((l2capcb
=l2cap_new())==NULL
) {
534 l2cap_arg(l2capcb
,pcb
);
536 err
= l2cap_connect_ind(l2capcb
,bdaddr
,HIDP_INPUT_CHANNEL
,l2cap_accepted
);
538 l2cap_close(l2capcb
);
543 _CPU_ISR_Restore(level
);
544 //printf("bte_registerdeviceasync(%02x)\n",err);
548 s32
bte_inquiry(struct inquiry_info
*info
,u8 max_cnt
,u8 flush
)
553 struct inquiry_info_ex
*pinfo
;
557 _CPU_ISR_Disable(level
);
558 if(btstate
.num_founddevs
==0 || flush
==1) {
559 btstate
.hci_cmddone
= 0;
560 btstate
.num_maxdevs
= max_cnt
;
561 hci_inquiry(0x009E8B33,0x03,max_cnt
,bte_inquiry_complete
);
562 last_err
= __bte_waitcmdfinish(&btstate
);
564 fnd
= btstate
.num_founddevs
;
565 pinfo
= btstate
.info
;
566 _CPU_ISR_Restore(level
);
568 if(last_err
==ERR_OK
) {
569 for(i
=0;i
<fnd
&& i
<max_cnt
;i
++) {
570 bd_addr_set(&(info
[i
].bdaddr
),&(pinfo
[i
].bdaddr
));
571 memcpy(info
[i
].cod
,pinfo
[i
].cod
,3);
574 return (s32
)((last_err
==ERR_OK
) ? fnd
: last_err
);
577 s32
bte_inquiry_ex(struct inquiry_info_ex
*info
,u8 max_cnt
,u8 flush
)
582 struct inquiry_info_ex
*pinfo
;
586 _CPU_ISR_Disable(level
);
587 if(btstate
.num_founddevs
==0 || flush
==1) {
588 btstate
.hci_cmddone
= 0;
589 btstate
.num_maxdevs
= max_cnt
;
590 hci_inquiry(0x009E8B33,0x03,max_cnt
,bte_inquiry_complete
);
591 last_err
= __bte_waitcmdfinish(&btstate
);
593 fnd
= btstate
.num_founddevs
;
594 pinfo
= btstate
.info
;
595 _CPU_ISR_Restore(level
);
597 if(last_err
==ERR_OK
) {
598 for(i
=0;i
<fnd
&& i
<max_cnt
;i
++) {
599 memcpy(info
[i
].cod
,pinfo
[i
].cod
,3);
600 bd_addr_set(&(info
[i
].bdaddr
),&(pinfo
[i
].bdaddr
));
601 info
[i
].psrm
= pinfo
[i
].psrm
;
602 info
[i
].psm
= pinfo
[i
].psm
;
603 info
[i
].co
= pinfo
[i
].co
;
606 return (s32
)((last_err
==ERR_OK
) ? fnd
: last_err
);
609 s32
bte_disconnect(struct bte_pcb
*pcb
)
614 if(pcb
==NULL
) return ERR_VAL
;
616 _CPU_ISR_Disable(level
);
617 pcb
->state
= (u32
)STATE_DISCONNECTING
;
618 if(pcb
->in_pcb
!=NULL
)
619 err
= l2ca_disconnect_req(pcb
->in_pcb
,l2cap_disconnect_cfm
);
620 else if(pcb
->out_pcb
!=NULL
)
621 err
= l2ca_disconnect_req(pcb
->out_pcb
,l2cap_disconnect_cfm
);
622 _CPU_ISR_Restore(level
);
628 s32 bte_connect(struct bte_pcb *pcb,struct bd_addr *bdaddr,u8 psm,s32 (*recv)(void *arg,void *buffer,u16 len))
633 if(pcb==NULL) return ERR_VAL;
635 if((pcb->l2capcb=l2cap_new())==NULL) return ERR_MEM;
639 bd_addr_set(&(pcb->bdaddr),bdaddr);
641 _CPU_ISR_Disable(level);
643 l2cap_arg(pcb->l2capcb,pcb);
644 err = l2ca_connect_req(pcb->l2capcb,bdaddr,psm,HCI_ALLOW_ROLE_SWITCH,l2cap_connected);
646 LWP_ThreadSleep(pcb->cmdq);
649 _CPU_ISR_Restore(level);
654 s32 bte_connect_ex(struct bte_pcb *pcb,struct inquiry_info_ex *info,u8 psm,s32 (*recv)(void *arg,void *buffer,u16 len))
658 if((err=hci_reg_dev_info(&(info->bdaddr),info->cod,info->psrm,info->psm,info->co))!=ERR_OK) return err;
659 return bte_connect(pcb,&(info->bdaddr),psm,recv);
662 s32 bte_listen(struct bte_pcb *pcb,struct bd_addr *bdaddr,u8 psm)
666 struct l2cap_pcb *l2capcb = NULL;
668 if(pcb==NULL) return ERR_VAL;
670 if((l2capcb=l2cap_new())==NULL) return ERR_MEM;
675 bd_addr_set(&(pcb->bdaddr),bdaddr);
677 _CPU_ISR_Disable(level);
679 l2cap_arg(l2capcb,pcb);
680 err = l2cap_connect_ind(l2capcb,psm,l2cap_accepted);
681 if(err!=ERR_OK) l2cap_close(l2capcb);
683 _CPU_ISR_Restore(level);
687 s32 bte_accept(struct bte_pcb *pcb,s32 (*recv)(void *arg,void *buffer,u16 len))
692 if(pcb==NULL) return ERR_VAL;
694 _CPU_ISR_Disable(level);
696 while(pcb->l2capcb==NULL)
697 LWP_ThreadSleep(pcb->cmdq);
699 _CPU_ISR_Restore(level);
705 s32
bte_senddata(struct bte_pcb
*pcb
,void *message
,u16 len
)
710 if(pcb
==NULL
|| message
==NULL
|| len
==0) return ERR_VAL
;
711 if(pcb
->state
==STATE_DISCONNECTING
|| pcb
->state
==STATE_DISCONNECTED
) return ERR_CLSD
;
713 if((p
=btpbuf_alloc(PBUF_RAW
,(1 + len
),PBUF_RAM
))==NULL
) {
714 ERROR("bte_senddata: Could not allocate memory for pbuf\n");
718 ((u8
*)p
->payload
)[0] = (HIDP_TRANS_DATA
|HIDP_DATA_RTYPE_OUPUT
);
719 memcpy(p
->payload
+1,message
,len
);
721 err
= l2ca_datawrite(pcb
->out_pcb
,p
);
727 s32
bte_sendmessageasync(struct bte_pcb
*pcb
,void *message
,u16 len
,s32 (*sent
)(void *arg
,struct bte_pcb
*pcb
,u8 err
))
730 struct ctrl_req_t
*req
;
732 //printf("bte_sendmessageasync()\n");
734 if(pcb
==NULL
|| message
==NULL
|| len
==0) return ERR_VAL
;
735 if(pcb
->state
==STATE_DISCONNECTING
|| pcb
->state
==STATE_DISCONNECTED
) return ERR_CLSD
;
737 if((req
=btmemb_alloc(&bte_ctrl_reqs
))==NULL
) {
738 ERROR("bte_sendmessageasync: Could not allocate memory for request\n");
742 if((p
=btpbuf_alloc(PBUF_RAW
,(1 + len
),PBUF_RAM
))==NULL
) {
743 ERROR("bte_sendmessageasync: Could not allocate memory for pbuf\n");
744 btmemb_free(&bte_ctrl_reqs
,req
);
748 ((u8
*)p
->payload
)[0] = (HIDP_TRANS_SETREPORT
|HIDP_DATA_RTYPE_OUPUT
);
749 memcpy(p
->payload
+1,message
,len
);
754 return __bte_send_request(req
);
757 s32
bte_sendmessage(struct bte_pcb
*pcb
,void *message
,u16 len
)
761 struct ctrl_req_t
*req
;
763 //printf("bte_sendmessage()\n");
765 if(pcb
==NULL
|| message
==NULL
|| len
==0) return ERR_VAL
;
766 if(pcb
->state
==STATE_DISCONNECTING
|| pcb
->state
==STATE_DISCONNECTED
) return ERR_CLSD
;
768 if((req
=btmemb_alloc(&bte_ctrl_reqs
))==NULL
) {
769 ERROR("bte_sendmessage: Could not allocate memory for request\n");
773 if((p
=btpbuf_alloc(PBUF_RAW
,(1 + len
),PBUF_RAM
))==NULL
) {
774 ERROR("bte_sendmessage: Could not allocate memory for pbuf\n");
775 btmemb_free(&bte_ctrl_reqs
,req
);
779 ((u8
*)p
->payload
)[0] = (HIDP_TRANS_SETREPORT
|HIDP_DATA_RTYPE_OUPUT
);
780 memcpy(p
->payload
+1,message
,len
);
785 err
= __bte_send_request(req
);
786 if(err
==ERR_OK
) err
= __bte_waitrequest(req
);
788 btmemb_free(&bte_ctrl_reqs
,req
);
792 void bte_arg(struct bte_pcb
*pcb
,void *arg
)
795 _CPU_ISR_Disable(level
);
797 _CPU_ISR_Restore(level
);
800 void bte_received(struct bte_pcb
*pcb
, s32 (*recv
)(void *arg
,void *buffer
,u16 len
))
803 _CPU_ISR_Disable(level
);
805 _CPU_ISR_Restore(level
);
808 void bte_disconnected(struct bte_pcb
*pcb
,s32 (disconn_cfm
)(void *arg
,struct bte_pcb
*pcb
,u8 err
))
811 _CPU_ISR_Disable(level
);
812 pcb
->disconn_cfm
= disconn_cfm
;
813 _CPU_ISR_Restore(level
);
816 err_t
acl_wlp_completed(void *arg
,struct bd_addr
*bdaddr
)
818 //hci_sniff_mode(bdaddr,200,100,10,10);
822 err_t
acl_conn_complete(void *arg
,struct bd_addr
*bdaddr
)
824 //printf("acl_conn_complete\n");
825 //memcpy(&(btstate.acl_bdaddr),bdaddr,6);
827 hci_write_link_policy_settings(bdaddr
,0x0005);
831 err_t
pin_req(void *arg
,struct bd_addr
*bdaddr
)
833 //printf("pin_req\n");
837 err_t
l2cap_disconnected_ind(void *arg
, struct l2cap_pcb
*pcb
, err_t err
)
839 struct bte_pcb
*bte
= (struct bte_pcb
*)arg
;
841 if(bte
==NULL
) return ERR_OK
;
843 bte
->state
= (u32
)STATE_DISCONNECTING
;
844 switch(l2cap_psm(pcb
)) {
845 case HIDP_OUTPUT_CHANNEL
:
846 l2cap_close(bte
->out_pcb
);
849 case HIDP_INPUT_CHANNEL
:
850 l2cap_close(bte
->in_pcb
);
854 if(bte
->in_pcb
==NULL
&& bte
->out_pcb
==NULL
) {
856 bte
->state
= (u32
)STATE_DISCONNECTED
;
857 __bte_close_ctrl_queue(bte
);
858 if(bte
->disconn_cfm
!=NULL
) bte
->disconn_cfm(bte
->cbarg
,bte
,ERR_OK
);
863 err_t
l2cap_disconnect_cfm(void *arg
, struct l2cap_pcb
*pcb
)
866 struct bte_pcb
*bte
= (struct bte_pcb
*)arg
;
868 if(bte
==NULL
) return ERR_OK
;
870 switch(l2cap_psm(pcb
)) {
871 case HIDP_OUTPUT_CHANNEL
:
872 l2cap_close(bte
->out_pcb
);
874 if(bte
->in_pcb
!=NULL
) err
= l2ca_disconnect_req(bte
->in_pcb
,l2cap_disconnect_cfm
);
876 case HIDP_INPUT_CHANNEL
:
877 l2cap_close(bte
->in_pcb
);
879 if(bte
->out_pcb
!=NULL
) err
= l2ca_disconnect_req(bte
->out_pcb
,l2cap_disconnect_cfm
);
882 if(bte
->in_pcb
==NULL
&& bte
->out_pcb
==NULL
) {
884 bte
->state
= (u32
)STATE_DISCONNECTED
;
885 __bte_close_ctrl_queue(bte
);
886 if(bte
->disconn_cfm
!=NULL
) bte
->disconn_cfm(bte
->cbarg
,bte
,ERR_OK
);
888 hci_cmd_complete(NULL
);
889 hci_disconnect(&(bte
->bdaddr
),HCI_OTHER_END_TERMINATED_CONN_USER_ENDED
);
895 err_t
link_key_not(void *arg
,struct bd_addr
*bdaddr
,u8_t
*key
)
897 //printf("link_key_not\n");
898 return hci_write_stored_link_key(bdaddr
,key
);
902 err_t l2cap_connected(void *arg,struct l2cap_pcb *l2cappcb,u16_t result,u16_t status)
904 struct bte_pcb *btepcb = (struct bte_pcb*)arg;
906 printf("l2cap_connected(%02x)\n",result);
907 if(result==L2CAP_CONN_SUCCESS) {
908 l2cap_recv(l2cappcb,bte_input);
909 l2cap_disconnect_ind(l2cappcb,l2cap_disconnected_ind);
910 btepcb->err = ERR_OK;
912 l2cap_close(l2cappcb);
913 btepcb->err = ERR_CONN;
916 if(btepcb->conn_cfm) btepcb->conn_cfm(btepcb->cbarg,btepcb,btepcb->err);
917 LWP_ThreadSignal(btepcb->cmdq);
921 err_t
l2cap_accepted(void *arg
,struct l2cap_pcb
*l2cappcb
,err_t err
)
923 struct bte_pcb
*btepcb
= (struct bte_pcb
*)arg
;
925 //printf("l2cap_accepted(%02x)\n",err);
927 l2cap_recv(l2cappcb
,bte_process_input
);
928 l2cap_disconnect_ind(l2cappcb
,l2cap_disconnected_ind
);
929 switch(l2cap_psm(l2cappcb
)) {
930 case HIDP_OUTPUT_CHANNEL
:
931 btepcb
->out_pcb
= l2cappcb
;
933 case HIDP_INPUT_CHANNEL
:
934 btepcb
->in_pcb
= l2cappcb
;
937 if(btepcb
->in_pcb
&& btepcb
->out_pcb
) {
938 btepcb
->err
= ERR_OK
;
939 btepcb
->state
= (u32
)STATE_CONNECTED
;
940 if(btepcb
->conn_cfm
) btepcb
->conn_cfm(btepcb
->cbarg
,btepcb
,ERR_OK
);
943 l2cap_close(l2cappcb
);
944 btepcb
->err
= ERR_CONN
;
945 btepcb
->conn_cfm(btepcb
->cbarg
,btepcb
,ERR_CONN
);
951 err_t
bte_inquiry_complete(void *arg
,struct hci_pcb
*pcb
,struct hci_inq_res
*ires
,u16_t result
)
954 struct hci_inq_res
*p
;
955 struct bt_state
*state
= (struct bt_state
*)arg
;
957 if(result
==HCI_SUCCESS
) {
960 if(btstate
.info
!=NULL
) free(btstate
.info
);
962 btstate
.num_maxdevs
= 0;
963 btstate
.num_founddevs
= 0;
967 btstate
.num_founddevs
++;
972 btstate
.info
= (struct inquiry_info_ex
*)malloc(sizeof(struct inquiry_info_ex
)*btstate
.num_founddevs
);
973 for(i
=0;i
<btstate
.num_founddevs
&& p
!=NULL
;i
++) {
974 bd_addr_set(&(btstate
.info
[i
].bdaddr
),&(p
->bdaddr
));
975 memcpy(btstate
.info
[i
].cod
,p
->cod
,3);
976 btstate
.info
[i
].psrm
= p
->psrm
;
977 btstate
.info
[i
].psm
= p
->psm
;
978 btstate
.info
[i
].co
= p
->co
;
980 printf("bdaddr: %02x:%02x:%02x:%02x:%02x:%02x\n",p
->bdaddr
.addr
[0],p
->bdaddr
.addr
[1],p
->bdaddr
.addr
[2],p
->bdaddr
.addr
[3],p
->bdaddr
.addr
[4],p
->bdaddr
.addr
[5]);
981 printf("cod: %02x%02x%02x\n",p
->cod
[0],p
->cod
[1],p
->cod
[2]);
982 printf("psrm: %02x\n",p
->psrm
);
983 printf("psm: %02x\n",p
->psm
);
984 printf("co: %04x\n",p
->co
);
987 __bte_cmdfinish(state
,ERR_OK
);
989 hci_inquiry(0x009E8B33,0x03,btstate
.num_maxdevs
,bte_inquiry_complete
);
994 err_t
bte_read_stored_link_key_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
997 struct hci_link_key
*p
;
998 struct linkkey_info
*keys
;
999 struct bt_state
*state
= (struct bt_state
*)arg
;
1001 if(!pcb
) return ERR_CONN
;
1003 LOG("bte_read_stored_link_key_complete(%02x,%p)\n",result
,pcb
->keyres
);
1005 if(state
==NULL
) return ERR_VAL
;
1006 if(!(ogf
==HCI_HC_BB_OGF
&& ocf
==HCI_R_STORED_LINK_KEY_OCF
)) return __bte_cmdfinish(state
,ERR_CONN
);
1008 if(result
==HCI_SUCCESS
) {
1009 keys
= (struct linkkey_info
*)state
->usrdata
;
1010 if(pcb
->keyres
!=NULL
&& keys
!=NULL
) {
1011 for(i
=0,p
=pcb
->keyres
;i
<state
->num_maxdevs
&& p
!=NULL
;i
++) {
1012 bd_addr_set(&(keys
[i
].bdaddr
),&(p
->bdaddr
));
1013 memcpy(keys
[i
].key
,p
->key
,16);
1018 LOG("bte_read_stored_link_key_complete(%02x,%p,%d)\n",result
,pcb
->keyres
,i
);
1019 __bte_cmdfinish(state
,i
);
1023 return __bte_cmdfinish(state
,ERR_VAL
);
1025 /* new init with patching */
1026 err_t
bte_hci_initcore_complete2(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
1029 u8_t dev_cod
[] = {0x04, 0x02,0x40};
1030 struct bt_state
*state
= (struct bt_state
*)arg
;
1032 LOG("bte_hci_initcore_complete2(%02x,%02x)\n",ogf
,ocf
);
1035 if(ocf
==HCI_WRITE_INQUIRY_MODE
) {
1036 if(result
==HCI_SUCCESS
) {
1037 hci_write_page_scan_type(0x01);
1040 } else if(ocf
==HCI_WRITE_PAGE_SCAN_TYPE
) {
1041 if(result
==HCI_SUCCESS
) {
1042 hci_write_inquiry_scan_type(0x01);
1045 } else if(ocf
==HCI_WRITE_INQUIRY_SCAN_TYPE
) {
1046 if(result
==HCI_SUCCESS
) {
1047 hci_write_cod(dev_cod
);
1050 } else if(ocf
==HCI_WRITE_COD
) {
1051 if(result
==HCI_SUCCESS
) {
1052 hci_write_page_timeout(0x2000);
1055 } else if(ocf
==HCI_WRITE_PAGE_TIMEOUT
) {
1056 if(result
==HCI_SUCCESS
) {
1057 state
->hci_inited
= 1;
1058 hci_cmd_complete(NULL
);
1059 return __bte_cmdfinish(state
,ERR_OK
);
1065 LOG("Unknown command complete event. OGF = 0x%x OCF = 0x%x\n", ogf
, ocf
);
1070 if(err
!=ERR_OK
) __bte_cmdfinish(state
,err
);
1074 err_t
bte_hci_initcore_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
1077 u8_t dev_cod
[] = {0x00, 0x1f,0x00};
1078 struct bt_state
*state
= (struct bt_state
*)arg
;
1080 LOG("bte_hci_initcore_complete(%02x,%02x)\n",ogf
,ocf
);
1082 case HCI_INFO_PARAM
:
1083 if(ocf
==HCI_READ_BUFFER_SIZE
) {
1084 if(result
==HCI_SUCCESS
) {
1085 hci_write_cod(dev_cod
);
1088 } else if(ocf
==HCI_READ_LOCAL_VERSION
) {
1089 if(result
==HCI_SUCCESS
) {
1093 } else if(ocf
==HCI_READ_BD_ADDR
) {
1094 if(result
==HCI_SUCCESS
) {
1095 hci_read_local_features();
1098 } else if(ocf
==HCI_READ_LOCAL_FEATURES
) {
1099 if(result
==HCI_SUCCESS
) {
1100 hci_cmd_complete(bte_hci_initcore_complete2
);
1101 hci_write_inquiry_mode(0x01);
1107 if(ocf
==HCI_RESET
) {
1108 if(result
==HCI_SUCCESS
) {
1109 hci_read_buffer_size();
1112 } else if(ocf
==HCI_WRITE_COD
) {
1113 if(result
==HCI_SUCCESS
) {
1114 hci_write_local_name((u8_t
*)"",1);
1117 } else if(ocf
==HCI_WRITE_LOCAL_NAME
) {
1118 if(result
==HCI_SUCCESS
) {
1119 hci_write_pin_type(0x00);
1122 } else if(ocf
==HCI_WRITE_PIN_TYPE
) {
1123 if(result
==HCI_SUCCESS
) {
1124 hci_host_buffer_size();
1127 } else if(ocf
==HCI_HOST_BUF_SIZE
) {
1128 if(result
==HCI_SUCCESS
) {
1129 hci_read_local_version();
1135 LOG("Unknown command complete event. OGF = 0x%x OCF = 0x%x\n", ogf
, ocf
);
1140 if(err
!=ERR_OK
) __bte_cmdfinish(state
,err
);
1144 err_t
bte_hci_apply_patch_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
1147 struct bt_state
*state
= (struct bt_state
*)arg
;
1149 LOG("bte_hci_apply_patch_complete(%02x,%02x,%02x)\n",ogf
,ocf
,result
);
1151 case HCI_VENDOR_OGF
:
1152 if(ocf
==HCI_VENDOR_PATCH_START_OCF
) {
1153 if(result
==HCI_SUCCESS
) {
1154 err
= hci_vendor_specific_command(HCI_VENDOR_PATCH_CONT_OCF
,HCI_VENDOR_OGF
,bte_patch0
,184);
1157 } else if(ocf
==HCI_VENDOR_PATCH_CONT_OCF
) {
1158 if(result
==HCI_SUCCESS
) {
1159 hci_cmd_complete(bte_hci_patch_complete
);
1160 err
= hci_vendor_specific_command(HCI_VENDOR_PATCH_END_OCF
,HCI_VENDOR_OGF
,bte_patch1
,92);
1166 LOG("Unknown command complete event. OGF = 0x%x OCF = 0x%x\n", ogf
, ocf
);
1171 if(err
!=ERR_OK
) __bte_cmdfinish(state
,err
);
1175 err_t
bte_hci_patch_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
1178 u8_t dev_cod
[] = {0x04, 0x02,0x40};
1179 struct bt_state
*state
= (struct bt_state
*)arg
;
1181 LOG("bte_hci_patch_complete(%02x,%02x,%02x)\n",ogf
,ocf
,result
);
1183 case HCI_INFO_PARAM
:
1184 if(ocf
==HCI_READ_BUFFER_SIZE
) {
1185 if(result
==HCI_SUCCESS
) {
1186 hci_write_cod(dev_cod
);
1189 } else if(ocf
==HCI_READ_LOCAL_VERSION
) {
1190 if(result
==HCI_SUCCESS
) {
1194 } else if(ocf
==HCI_READ_BD_ADDR
) {
1195 if(result
==HCI_SUCCESS
) {
1196 hci_read_local_features();
1199 } else if(ocf
==HCI_READ_LOCAL_FEATURES
) {
1200 if(result
==HCI_SUCCESS
) {
1201 hci_cmd_complete(NULL
);
1202 return __bte_cmdfinish(state
,ERR_OK
);
1208 if(ocf
==HCI_RESET
) {
1209 if(result
==HCI_SUCCESS
) {
1210 hci_read_buffer_size();
1213 } else if(ocf
==HCI_WRITE_COD
) {
1214 if(result
==HCI_SUCCESS
) {
1215 hci_write_local_name((u8_t
*)"",1);
1218 } else if(ocf
==HCI_WRITE_LOCAL_NAME
) {
1219 if(result
==HCI_SUCCESS
) {
1220 hci_write_pin_type(0x00);
1223 } else if(ocf
==HCI_WRITE_PIN_TYPE
) {
1224 if(result
==HCI_SUCCESS
) {
1225 hci_host_buffer_size();
1228 } else if(ocf
==HCI_HOST_BUF_SIZE
) {
1229 if(result
==HCI_SUCCESS
) {
1230 hci_read_local_version();
1235 case HCI_VENDOR_OGF
:
1236 if(ocf
==HCI_VENDOR_PATCH_END_OCF
) {
1237 if(result
==HCI_SUCCESS
) {
1244 LOG("Unknown command complete event. OGF = 0x%x OCF = 0x%x\n", ogf
, ocf
);
1249 if(err
!=ERR_OK
) __bte_cmdfinish(state
,err
);
1253 err_t
bte_hci_initsub_complete(void *arg
,struct hci_pcb
*pcb
,u8_t ogf
,u8_t ocf
,u8_t result
)
1256 u8_t dev_cod
[] = {0x00, 0x04,0x48};
1257 struct bt_state
*state
= (struct bt_state
*)arg
;
1259 LOG("bte_hci_initsub_complete(%02x,%02x)\n",ogf
,ocf
);
1262 if(ocf
==HCI_WRITE_INQUIRY_MODE
) {
1263 if(result
==HCI_SUCCESS
) {
1264 hci_write_page_scan_type(0x01);
1267 } else if(ocf
==HCI_WRITE_PAGE_SCAN_TYPE
) {
1268 if(result
==HCI_SUCCESS
) {
1269 hci_write_inquiry_scan_type(0x01);
1272 } else if(ocf
==HCI_WRITE_INQUIRY_SCAN_TYPE
) {
1273 if(result
==HCI_SUCCESS
) {
1274 hci_write_cod(dev_cod
);
1277 } else if(ocf
==HCI_WRITE_COD
) {
1278 if(result
==HCI_SUCCESS
) {
1279 hci_write_page_timeout(0x8000);
1282 } else if(ocf
==HCI_WRITE_PAGE_TIMEOUT
) {
1283 if(result
==HCI_SUCCESS
) {
1284 hci_write_local_name((u8_t
*)"Wii",4);
1287 } else if(ocf
==HCI_WRITE_LOCAL_NAME
) {
1288 if(result
==HCI_SUCCESS
) {
1289 hci_write_scan_enable(0x02);
1292 } else if(ocf
==HCI_WRITE_SCAN_ENABLE
) {
1293 if(result
==HCI_SUCCESS
) {
1294 hci_cmd_complete(NULL
);
1295 return __bte_cmdfinish(state
,ERR_OK
);
1301 LOG("Unknown command complete event. OGF = 0x%x OCF = 0x%x\n", ogf
, ocf
);
1307 if(err
!=ERR_OK
) __bte_cmdfinish(state
,err
);