xref: /btstack/src/hci.c (revision 3091b266fe4659428bc06046eb8edbf8c4f5b0c6)
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 = {
20*3091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x01), "311"
21*3091b266Smatthias.ringwald     // LAP, Inquiry length, Num_responses
22*3091b266Smatthias.ringwald };
23*3091b266Smatthias.ringwald 
24*3091b266Smatthias.ringwald hci_cmd_t hci_link_key_request_negative_reply = {
25*3091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
26*3091b266Smatthias.ringwald };
27*3091b266Smatthias.ringwald 
28*3091b266Smatthias.ringwald hci_cmd_t hci_pin_code_request_reply = {
29*3091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
30*3091b266Smatthias.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 
47*3091b266Smatthias.ringwald hci_cmd_t hci_write_authentication_enable = {
48*3091b266Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
49*3091b266Smatthias.ringwald     // Authentication_Enable
50*3091b266Smatthias.ringwald };
51*3091b266Smatthias.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 
58475c8125Smatthias.ringwald static hci_transport_t * hci_transport;
5902ea9861Smatthias.ringwald static uint8_t         * hci_cmd_buffer;
60475c8125Smatthias.ringwald 
6156fe0872Smatthias.ringwald void hexdump(uint8_t *data, int size){
6256fe0872Smatthias.ringwald     int i;
6356fe0872Smatthias.ringwald     for (i=0; i<size;i++){
6456fe0872Smatthias.ringwald         printf("%02X ", data[i]);
6556fe0872Smatthias.ringwald     }
6656fe0872Smatthias.ringwald     printf("\n");
6756fe0872Smatthias.ringwald }
6856fe0872Smatthias.ringwald 
6956fe0872Smatthias.ringwald #if 0
7056fe0872Smatthias.ringwald static void *hci_daemon_thread(void *arg){
7156fe0872Smatthias.ringwald     printf("HCI Daemon started\n");
7256fe0872Smatthias.ringwald     hci_run(transport, &config);
7356fe0872Smatthias.ringwald     return NULL;
7456fe0872Smatthias.ringwald }
7556fe0872Smatthias.ringwald #endif
7656fe0872Smatthias.ringwald 
77475c8125Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config){
78475c8125Smatthias.ringwald 
79475c8125Smatthias.ringwald     // reference to use transport layer implementation
80475c8125Smatthias.ringwald     hci_transport = transport;
81475c8125Smatthias.ringwald 
8202ea9861Smatthias.ringwald     // empty cmd buffer
8302ea9861Smatthias.ringwald     hci_cmd_buffer = malloc(3+255);
8402ea9861Smatthias.ringwald 
85475c8125Smatthias.ringwald     // open unix socket
86475c8125Smatthias.ringwald 
87475c8125Smatthias.ringwald     // wait for connections
88475c8125Smatthias.ringwald 
89475c8125Smatthias.ringwald     // enter loop
90475c8125Smatthias.ringwald 
91475c8125Smatthias.ringwald     // handle events
92475c8125Smatthias.ringwald }
93475c8125Smatthias.ringwald 
94475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
95475c8125Smatthias.ringwald     return 0;
96475c8125Smatthias.ringwald }
97475c8125Smatthias.ringwald 
98475c8125Smatthias.ringwald void hci_run(){
99475c8125Smatthias.ringwald     while (1) {
100475c8125Smatthias.ringwald         //  construct file descriptor set to wait for
101475c8125Smatthias.ringwald         //  select
102475c8125Smatthias.ringwald 
103475c8125Smatthias.ringwald         // for each ready file in FD - call handle_data
104475c8125Smatthias.ringwald         sleep(1);
105475c8125Smatthias.ringwald     }
1061f504dbdSmatthias.ringwald }
10793b8dc03Smatthias.ringwald 
10802ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
10902ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
11002ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
11193b8dc03Smatthias.ringwald     int pos = 3;
11293b8dc03Smatthias.ringwald 
11393b8dc03Smatthias.ringwald     va_list argptr;
11493b8dc03Smatthias.ringwald     va_start(argptr, cmd);
11593b8dc03Smatthias.ringwald     const char *format = cmd->format;
11693b8dc03Smatthias.ringwald     uint16_t word;
11793b8dc03Smatthias.ringwald     uint32_t longword;
118*3091b266Smatthias.ringwald     uint8_t * ptr;
11993b8dc03Smatthias.ringwald     while (*format) {
12093b8dc03Smatthias.ringwald         switch(*format) {
12193b8dc03Smatthias.ringwald             case '1': //  8 bit value
12293b8dc03Smatthias.ringwald             case '2': // 16 bit value
12393b8dc03Smatthias.ringwald             case 'H': // hci_handle
124554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
12502ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
12693b8dc03Smatthias.ringwald                 if (*format == '2') {
12702ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
12893b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
129554588a5Smatthias.ringwald                     // TODO
13093b8dc03Smatthias.ringwald                 }
13193b8dc03Smatthias.ringwald                 break;
13293b8dc03Smatthias.ringwald             case '3':
13393b8dc03Smatthias.ringwald             case '4':
13493b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
13593b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
13602ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
13702ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
13802ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
13993b8dc03Smatthias.ringwald                 if (*format == '4'){
14002ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
14193b8dc03Smatthias.ringwald                 }
14293b8dc03Smatthias.ringwald                 break;
14393b8dc03Smatthias.ringwald             case 'B': // bt-addr
144*3091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
145*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
146*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
147*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
148*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
149*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
150*3091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
151*3091b266Smatthias.ringwald                 break;
152*3091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
153*3091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
154*3091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
155*3091b266Smatthias.ringwald                 pos += 16;
15693b8dc03Smatthias.ringwald                 break;
15793b8dc03Smatthias.ringwald             default:
15893b8dc03Smatthias.ringwald                 break;
15993b8dc03Smatthias.ringwald         }
16093b8dc03Smatthias.ringwald         format++;
16193b8dc03Smatthias.ringwald     };
16293b8dc03Smatthias.ringwald     va_end(argptr);
16302ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
16402ea9861Smatthias.ringwald     // send packet
16502ea9861Smatthias.ringwald     return hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
16693b8dc03Smatthias.ringwald }