xref: /btstack/src/hci.c (revision 22909952ea76c45a8db20409374b80e7e0204e4c)
11f504dbdSmatthias.ringwald /*
21f504dbdSmatthias.ringwald  *  hci.c
31f504dbdSmatthias.ringwald  *
41f504dbdSmatthias.ringwald  *  Created by Matthias Ringwald on 4/29/09.
51f504dbdSmatthias.ringwald  *
61f504dbdSmatthias.ringwald  */
71f504dbdSmatthias.ringwald 
8475c8125Smatthias.ringwald #include <unistd.h>
993b8dc03Smatthias.ringwald #include <stdarg.h>
1093b8dc03Smatthias.ringwald #include <string.h>
1156fe0872Smatthias.ringwald #include <stdio.h>
121f504dbdSmatthias.ringwald #include "hci.h"
131f504dbdSmatthias.ringwald 
140a974e0cSmatthias.ringwald // calculate combined ogf/ocf value
150a974e0cSmatthias.ringwald #define OPCODE(ogf, ocf) (ocf | ogf << 10)
1602ea9861Smatthias.ringwald #define OGF_LINK_CONTROL 0x01
1702ea9861Smatthias.ringwald #define OGF_CONTROLLER_BASEBAND 0x03
1893b8dc03Smatthias.ringwald 
1993b8dc03Smatthias.ringwald hci_cmd_t hci_inquiry = {
203091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x01), "311"
213091b266Smatthias.ringwald     // LAP, Inquiry length, Num_responses
223091b266Smatthias.ringwald };
233091b266Smatthias.ringwald 
243091b266Smatthias.ringwald hci_cmd_t hci_link_key_request_negative_reply = {
253091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
263091b266Smatthias.ringwald };
273091b266Smatthias.ringwald 
283091b266Smatthias.ringwald hci_cmd_t hci_pin_code_request_reply = {
293091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
303091b266Smatthias.ringwald     // BD_ADDR, pin length, PIN: c-string
3193b8dc03Smatthias.ringwald };
3293b8dc03Smatthias.ringwald 
3393b8dc03Smatthias.ringwald hci_cmd_t hci_reset = {
3402ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
3502ea9861Smatthias.ringwald };
3602ea9861Smatthias.ringwald 
3702ea9861Smatthias.ringwald hci_cmd_t hci_create_connection = {
3802ea9861Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
3902ea9861Smatthias.ringwald     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
4002ea9861Smatthias.ringwald };
4102ea9861Smatthias.ringwald 
4202ea9861Smatthias.ringwald hci_cmd_t hci_write_page_timeout = {
4302ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
4402ea9861Smatthias.ringwald     // Page_Timeout * 0.625 ms
4502ea9861Smatthias.ringwald };
4602ea9861Smatthias.ringwald 
473091b266Smatthias.ringwald hci_cmd_t hci_write_authentication_enable = {
483091b266Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
493091b266Smatthias.ringwald     // Authentication_Enable
503091b266Smatthias.ringwald };
513091b266Smatthias.ringwald 
5202ea9861Smatthias.ringwald hci_cmd_t hci_host_buffer_size = {
5302ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
5402ea9861Smatthias.ringwald     // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
5593b8dc03Smatthias.ringwald };
5693b8dc03Smatthias.ringwald 
5793b8dc03Smatthias.ringwald 
5816833f0aSmatthias.ringwald // the stack is here
5916833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
6016833f0aSmatthias.ringwald 
61475c8125Smatthias.ringwald 
6243625864Smatthias.ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
6343625864Smatthias.ringwald     buffer[pos] = value & 0xff;
6443625864Smatthias.ringwald     buffer[pos+1] = value >> 8;
6543625864Smatthias.ringwald }
6643625864Smatthias.ringwald 
6756fe0872Smatthias.ringwald void hexdump(uint8_t *data, int size){
6856fe0872Smatthias.ringwald     int i;
6956fe0872Smatthias.ringwald     for (i=0; i<size;i++){
7056fe0872Smatthias.ringwald         printf("%02X ", data[i]);
7156fe0872Smatthias.ringwald     }
7256fe0872Smatthias.ringwald     printf("\n");
7356fe0872Smatthias.ringwald }
7456fe0872Smatthias.ringwald 
7556fe0872Smatthias.ringwald #if 0
7656fe0872Smatthias.ringwald static void *hci_daemon_thread(void *arg){
7756fe0872Smatthias.ringwald     printf("HCI Daemon started\n");
7856fe0872Smatthias.ringwald     hci_run(transport, &config);
7956fe0872Smatthias.ringwald     return NULL;
8056fe0872Smatthias.ringwald }
8156fe0872Smatthias.ringwald #endif
8256fe0872Smatthias.ringwald 
8316833f0aSmatthias.ringwald /**
8416833f0aSmatthias.ringwald  * Handler called by HCI transport
8516833f0aSmatthias.ringwald  */
8616833f0aSmatthias.ringwald static void dummy_handler(uint8_t *packet, int size){
8716833f0aSmatthias.ringwald }
8816833f0aSmatthias.ringwald 
8916833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
9016833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
9116833f0aSmatthias.ringwald }
92*22909952Smatthias.ringwald 
9316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
94*22909952Smatthias.ringwald 
95*22909952Smatthias.ringwald     if ( COMMAND_COMPLETE_EVENT(packet, hci_reset) ) {
96*22909952Smatthias.ringwald         // reset done, write page timeout
97*22909952Smatthias.ringwald         hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec
98*22909952Smatthias.ringwald         return;
99*22909952Smatthias.ringwald     }
100*22909952Smatthias.ringwald 
101*22909952Smatthias.ringwald     if ( COMMAND_COMPLETE_EVENT(packet, hci_write_page_timeout) ) {
102*22909952Smatthias.ringwald         uint8_t micro_packet = 100;
103*22909952Smatthias.ringwald         hci_stack.event_packet_handler(&micro_packet, 1);
104*22909952Smatthias.ringwald         return;
105*22909952Smatthias.ringwald     }
106*22909952Smatthias.ringwald 
10716833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
10816833f0aSmatthias.ringwald }
10916833f0aSmatthias.ringwald 
11016833f0aSmatthias.ringwald /** Register L2CAP handlers */
11116833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
11216833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
11316833f0aSmatthias.ringwald }
11416833f0aSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
11516833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
11616833f0aSmatthias.ringwald }
11716833f0aSmatthias.ringwald 
118475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config){
119475c8125Smatthias.ringwald 
120475c8125Smatthias.ringwald     // reference to use transport layer implementation
12116833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
122475c8125Smatthias.ringwald 
12302ea9861Smatthias.ringwald     // empty cmd buffer
12416833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
12516833f0aSmatthias.ringwald 
12616833f0aSmatthias.ringwald     // higher level handler
12716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
12816833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
12916833f0aSmatthias.ringwald 
13016833f0aSmatthias.ringwald     // register packet handlers with transport
13116833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
13216833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
13316833f0aSmatthias.ringwald 
13416833f0aSmatthias.ringwald     // open low-level device
135*22909952Smatthias.ringwald     transport->open(config);
13602ea9861Smatthias.ringwald 
137475c8125Smatthias.ringwald     // open unix socket
138475c8125Smatthias.ringwald 
139475c8125Smatthias.ringwald     // wait for connections
140475c8125Smatthias.ringwald 
141475c8125Smatthias.ringwald     // enter loop
142475c8125Smatthias.ringwald 
143475c8125Smatthias.ringwald     // handle events
144475c8125Smatthias.ringwald }
145475c8125Smatthias.ringwald 
146475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
147475c8125Smatthias.ringwald     return 0;
148475c8125Smatthias.ringwald }
149475c8125Smatthias.ringwald 
150475c8125Smatthias.ringwald void hci_run(){
151*22909952Smatthias.ringwald 
152*22909952Smatthias.ringwald     // send hci reset
153*22909952Smatthias.ringwald     hci_send_cmd(&hci_reset);
154*22909952Smatthias.ringwald 
155*22909952Smatthias.ringwald #if 0
156475c8125Smatthias.ringwald     while (1) {
157475c8125Smatthias.ringwald         //  construct file descriptor set to wait for
158475c8125Smatthias.ringwald         //  select
159475c8125Smatthias.ringwald 
160475c8125Smatthias.ringwald         // for each ready file in FD - call handle_data
161475c8125Smatthias.ringwald         sleep(1);
162475c8125Smatthias.ringwald     }
163*22909952Smatthias.ringwald #endif
1641f504dbdSmatthias.ringwald }
16593b8dc03Smatthias.ringwald 
16616833f0aSmatthias.ringwald 
16716833f0aSmatthias.ringwald 
16816833f0aSmatthias.ringwald 
16916833f0aSmatthias.ringwald 
17043625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
17116833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
17243625864Smatthias.ringwald }
17343625864Smatthias.ringwald 
17402ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
17516833f0aSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
17602ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
17702ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
17893b8dc03Smatthias.ringwald     int pos = 3;
17993b8dc03Smatthias.ringwald 
18093b8dc03Smatthias.ringwald     va_list argptr;
18193b8dc03Smatthias.ringwald     va_start(argptr, cmd);
18293b8dc03Smatthias.ringwald     const char *format = cmd->format;
18393b8dc03Smatthias.ringwald     uint16_t word;
18493b8dc03Smatthias.ringwald     uint32_t longword;
1853091b266Smatthias.ringwald     uint8_t * ptr;
18693b8dc03Smatthias.ringwald     while (*format) {
18793b8dc03Smatthias.ringwald         switch(*format) {
18893b8dc03Smatthias.ringwald             case '1': //  8 bit value
18993b8dc03Smatthias.ringwald             case '2': // 16 bit value
19093b8dc03Smatthias.ringwald             case 'H': // hci_handle
191554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
19202ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
19393b8dc03Smatthias.ringwald                 if (*format == '2') {
19402ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
19593b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
196554588a5Smatthias.ringwald                     // TODO
19793b8dc03Smatthias.ringwald                 }
19893b8dc03Smatthias.ringwald                 break;
19993b8dc03Smatthias.ringwald             case '3':
20093b8dc03Smatthias.ringwald             case '4':
20193b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
20293b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
20302ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
20402ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
20502ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
20693b8dc03Smatthias.ringwald                 if (*format == '4'){
20702ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
20893b8dc03Smatthias.ringwald                 }
20993b8dc03Smatthias.ringwald                 break;
21093b8dc03Smatthias.ringwald             case 'B': // bt-addr
2113091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
2123091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
2133091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
2143091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
2153091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
2163091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
2173091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
2183091b266Smatthias.ringwald                 break;
2193091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
2203091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
2213091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
2223091b266Smatthias.ringwald                 pos += 16;
22393b8dc03Smatthias.ringwald                 break;
22493b8dc03Smatthias.ringwald             default:
22593b8dc03Smatthias.ringwald                 break;
22693b8dc03Smatthias.ringwald         }
22793b8dc03Smatthias.ringwald         format++;
22893b8dc03Smatthias.ringwald     };
22993b8dc03Smatthias.ringwald     va_end(argptr);
23002ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
23102ea9861Smatthias.ringwald     // send packet
23216833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
23393b8dc03Smatthias.ringwald }