xref: /btstack/src/hci.c (revision 7301ad89bcf14fb889d79d7dd71fdcc46b142969)
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){
638b658ebcSmatthias.ringwald     buffer[pos++] = value;
648b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
658b658ebcSmatthias.ringwald }
668b658ebcSmatthias.ringwald 
678b658ebcSmatthias.ringwald void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
688b658ebcSmatthias.ringwald     buffer[pos++] = value;
698b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
708b658ebcSmatthias.ringwald     buffer[pos++] = value >> 16;
718b658ebcSmatthias.ringwald     buffer[pos++] = value >> 24;
7243625864Smatthias.ringwald }
7343625864Smatthias.ringwald 
741281a47eSmatthias.ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
751281a47eSmatthias.ringwald     dest[0] = src[5];
761281a47eSmatthias.ringwald     dest[1] = src[4];
771281a47eSmatthias.ringwald     dest[2] = src[3];
781281a47eSmatthias.ringwald     dest[3] = src[2];
791281a47eSmatthias.ringwald     dest[4] = src[1];
801281a47eSmatthias.ringwald     dest[5] = src[0];
811281a47eSmatthias.ringwald }
821281a47eSmatthias.ringwald 
8302582713Smatthias.ringwald void hexdump(void *data, int size){
8456fe0872Smatthias.ringwald     int i;
8556fe0872Smatthias.ringwald     for (i=0; i<size;i++){
8602582713Smatthias.ringwald         printf("%02X ", ((uint8_t *)data)[i]);
8756fe0872Smatthias.ringwald     }
8856fe0872Smatthias.ringwald     printf("\n");
8956fe0872Smatthias.ringwald }
9056fe0872Smatthias.ringwald 
9156fe0872Smatthias.ringwald #if 0
9256fe0872Smatthias.ringwald static void *hci_daemon_thread(void *arg){
9356fe0872Smatthias.ringwald     printf("HCI Daemon started\n");
9456fe0872Smatthias.ringwald     hci_run(transport, &config);
9556fe0872Smatthias.ringwald     return NULL;
9656fe0872Smatthias.ringwald }
9756fe0872Smatthias.ringwald #endif
9856fe0872Smatthias.ringwald 
9916833f0aSmatthias.ringwald /**
10097addcc5Smatthias.ringwald  * Linked link list
10197addcc5Smatthias.ringwald  */
10297addcc5Smatthias.ringwald 
10397addcc5Smatthias.ringwald /**
10497addcc5Smatthias.ringwald  * get link for given address
10597addcc5Smatthias.ringwald  *
10697addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
10797addcc5Smatthias.ringwald  */
108145be03fSmatthias.ringwald #if 0
10997addcc5Smatthias.ringwald static hci_connection_t *link_for_addr(bd_addr_t addr){
11097addcc5Smatthias.ringwald     return NULL;
11197addcc5Smatthias.ringwald }
112145be03fSmatthias.ringwald #endif
11397addcc5Smatthias.ringwald 
11497addcc5Smatthias.ringwald /**
11516833f0aSmatthias.ringwald  * Handler called by HCI transport
11616833f0aSmatthias.ringwald  */
11716833f0aSmatthias.ringwald static void dummy_handler(uint8_t *packet, int size){
11816833f0aSmatthias.ringwald }
11916833f0aSmatthias.ringwald 
12016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
12116833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
12216833f0aSmatthias.ringwald }
12322909952Smatthias.ringwald 
12416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1251281a47eSmatthias.ringwald     bd_addr_t addr;
12622909952Smatthias.ringwald 
1273429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
1283429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
1293429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
1303429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
13122909952Smatthias.ringwald     }
13222909952Smatthias.ringwald 
1333429f56bSmatthias.ringwald     // handle BT initialization
1343429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
135*7301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
136*7301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
137*7301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
138*7301ad89Smatthias.ringwald         // }
139*7301ad89Smatthias.ringwald         // handle normal init sequence
1403429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
1413429f56bSmatthias.ringwald             // odd: waiting for event
1423429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
1433429f56bSmatthias.ringwald                 hci_stack.substate++;
1443429f56bSmatthias.ringwald             }
1453429f56bSmatthias.ringwald         }
14622909952Smatthias.ringwald     }
14722909952Smatthias.ringwald 
1481281a47eSmatthias.ringwald     // link key request
1493429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
1501281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1511281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
1521281a47eSmatthias.ringwald         return;
1531281a47eSmatthias.ringwald     }
1541281a47eSmatthias.ringwald 
1551281a47eSmatthias.ringwald     // pin code request
1563429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
1571281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1581281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
1591281a47eSmatthias.ringwald     }
1601281a47eSmatthias.ringwald 
16116833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
16216833f0aSmatthias.ringwald }
16316833f0aSmatthias.ringwald 
16416833f0aSmatthias.ringwald /** Register L2CAP handlers */
16516833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
16616833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
16716833f0aSmatthias.ringwald }
16816833f0aSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
16916833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
17016833f0aSmatthias.ringwald }
17116833f0aSmatthias.ringwald 
172*7301ad89Smatthias.ringwald static int null_control_function(void *config){
173*7301ad89Smatthias.ringwald     return 0;
174*7301ad89Smatthias.ringwald }
175*7301ad89Smatthias.ringwald static const char * null_control_name(void *config){
176*7301ad89Smatthias.ringwald     return "Hardware unknown";
177*7301ad89Smatthias.ringwald }
178*7301ad89Smatthias.ringwald 
179*7301ad89Smatthias.ringwald static bt_control_t null_control = {
180*7301ad89Smatthias.ringwald     null_control_function,
181*7301ad89Smatthias.ringwald     null_control_function,
182*7301ad89Smatthias.ringwald     null_control_function,
183*7301ad89Smatthias.ringwald     null_control_name
184*7301ad89Smatthias.ringwald };
185*7301ad89Smatthias.ringwald 
18611e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
187475c8125Smatthias.ringwald 
188475c8125Smatthias.ringwald     // reference to use transport layer implementation
18916833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
190475c8125Smatthias.ringwald 
19111e23e5fSmatthias.ringwald     // references to used control implementation
192*7301ad89Smatthias.ringwald     if (control) {
19311e23e5fSmatthias.ringwald         hci_stack.control = control;
194*7301ad89Smatthias.ringwald     } else {
195*7301ad89Smatthias.ringwald         hci_stack.control = &null_control;
196*7301ad89Smatthias.ringwald     }
19711e23e5fSmatthias.ringwald 
19811e23e5fSmatthias.ringwald     // reference to used config
19911e23e5fSmatthias.ringwald     hci_stack.config = config;
20011e23e5fSmatthias.ringwald 
20102ea9861Smatthias.ringwald     // empty cmd buffer
20216833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
20316833f0aSmatthias.ringwald 
20416833f0aSmatthias.ringwald     // higher level handler
20516833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
20616833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
20716833f0aSmatthias.ringwald 
20816833f0aSmatthias.ringwald     // register packet handlers with transport
20916833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
21016833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
21116833f0aSmatthias.ringwald 
2128cbb57e6Smatthias.ringwald     // turn on
2138cbb57e6Smatthias.ringwald     hci_power_control(HCI_POWER_ON);
214475c8125Smatthias.ringwald }
215475c8125Smatthias.ringwald 
216475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
21711e23e5fSmatthias.ringwald     if (power_mode == HCI_POWER_ON) {
218*7301ad89Smatthias.ringwald 
219*7301ad89Smatthias.ringwald         // set up state machine
220*7301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
221*7301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
222*7301ad89Smatthias.ringwald         hci_stack.substate = 0;
223*7301ad89Smatthias.ringwald 
224*7301ad89Smatthias.ringwald         // power on
22511e23e5fSmatthias.ringwald         hci_stack.control->on(hci_stack.config);
226*7301ad89Smatthias.ringwald 
227*7301ad89Smatthias.ringwald         // open low-level device
228*7301ad89Smatthias.ringwald         hci_stack.hci_transport->open(hci_stack.config);
229*7301ad89Smatthias.ringwald 
23011e23e5fSmatthias.ringwald     } else if (power_mode == HCI_POWER_OFF){
231*7301ad89Smatthias.ringwald 
232*7301ad89Smatthias.ringwald         // close low-level device
233*7301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
234*7301ad89Smatthias.ringwald 
235*7301ad89Smatthias.ringwald         // power off
23611e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
23711e23e5fSmatthias.ringwald     }
238475c8125Smatthias.ringwald     return 0;
239475c8125Smatthias.ringwald }
240475c8125Smatthias.ringwald 
2413429f56bSmatthias.ringwald uint32_t hci_run(){
2423429f56bSmatthias.ringwald     uint8_t micro_packet;
2433429f56bSmatthias.ringwald     switch (hci_stack.state){
2443429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
2453429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
2463429f56bSmatthias.ringwald                 // odd: waiting for command completion
2473429f56bSmatthias.ringwald                 return 0;
2483429f56bSmatthias.ringwald             }
2493429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
2503429f56bSmatthias.ringwald                 // cannot send command yet
2513429f56bSmatthias.ringwald                 return 0;
2523429f56bSmatthias.ringwald             }
2533429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
2543429f56bSmatthias.ringwald                 case 0:
25522909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
2563429f56bSmatthias.ringwald                     break;
2573429f56bSmatthias.ringwald                 case 1:
2583429f56bSmatthias.ringwald                     // ca. 15 sec
2593429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
2603429f56bSmatthias.ringwald                     break;
2613429f56bSmatthias.ringwald                 case 2:
2623429f56bSmatthias.ringwald                     // done.
2633429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
2643429f56bSmatthias.ringwald                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
2653429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
2663429f56bSmatthias.ringwald                     break;
2673429f56bSmatthias.ringwald                 default:
2683429f56bSmatthias.ringwald                     break;
269475c8125Smatthias.ringwald             }
2703429f56bSmatthias.ringwald             hci_stack.substate++;
2713429f56bSmatthias.ringwald             break;
2723429f56bSmatthias.ringwald         default:
2733429f56bSmatthias.ringwald             break;
2741f504dbdSmatthias.ringwald     }
27593b8dc03Smatthias.ringwald 
2763429f56bSmatthias.ringwald     // don't check for timetous yet
2773429f56bSmatthias.ringwald     return 0;
2783429f56bSmatthias.ringwald }
27916833f0aSmatthias.ringwald 
28016833f0aSmatthias.ringwald 
28143625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
28216833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
28343625864Smatthias.ringwald }
28443625864Smatthias.ringwald 
2853429f56bSmatthias.ringwald 
2863429f56bSmatthias.ringwald /**
2873429f56bSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
2883429f56bSmatthias.ringwald  */
28902ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
29016833f0aSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
29102ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
29202ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
29393b8dc03Smatthias.ringwald     int pos = 3;
29493b8dc03Smatthias.ringwald 
29593b8dc03Smatthias.ringwald     va_list argptr;
29693b8dc03Smatthias.ringwald     va_start(argptr, cmd);
29793b8dc03Smatthias.ringwald     const char *format = cmd->format;
29893b8dc03Smatthias.ringwald     uint16_t word;
29993b8dc03Smatthias.ringwald     uint32_t longword;
3003091b266Smatthias.ringwald     uint8_t * ptr;
30193b8dc03Smatthias.ringwald     while (*format) {
30293b8dc03Smatthias.ringwald         switch(*format) {
30393b8dc03Smatthias.ringwald             case '1': //  8 bit value
30493b8dc03Smatthias.ringwald             case '2': // 16 bit value
30593b8dc03Smatthias.ringwald             case 'H': // hci_handle
306554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
30702ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
30893b8dc03Smatthias.ringwald                 if (*format == '2') {
30902ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
31093b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
311554588a5Smatthias.ringwald                     // TODO
31293b8dc03Smatthias.ringwald                 }
31393b8dc03Smatthias.ringwald                 break;
31493b8dc03Smatthias.ringwald             case '3':
31593b8dc03Smatthias.ringwald             case '4':
31693b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
31793b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
31802ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
31902ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
32002ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
32193b8dc03Smatthias.ringwald                 if (*format == '4'){
32202ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
32393b8dc03Smatthias.ringwald                 }
32493b8dc03Smatthias.ringwald                 break;
32593b8dc03Smatthias.ringwald             case 'B': // bt-addr
3263091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3273091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
3283091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
3293091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
3303091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
3313091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
3323091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
3333091b266Smatthias.ringwald                 break;
3343091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
3353091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3363091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
3373091b266Smatthias.ringwald                 pos += 16;
33893b8dc03Smatthias.ringwald                 break;
33993b8dc03Smatthias.ringwald             default:
34093b8dc03Smatthias.ringwald                 break;
34193b8dc03Smatthias.ringwald         }
34293b8dc03Smatthias.ringwald         format++;
34393b8dc03Smatthias.ringwald     };
34493b8dc03Smatthias.ringwald     va_end(argptr);
34502ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
34602ea9861Smatthias.ringwald     // send packet
3473429f56bSmatthias.ringwald     hci_stack.num_cmd_packets--;
34816833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
34993b8dc03Smatthias.ringwald }