xref: /btstack/src/hci.c (revision 8adf0dda452dfdcc90e999c1581cd0f7e35e07d6)
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 
14e521ef40Smatthias.ringwald /**
15e521ef40Smatthias.ringwald  *  Link Control Commands
16e521ef40Smatthias.ringwald  */
1793b8dc03Smatthias.ringwald hci_cmd_t hci_inquiry = {
183091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x01), "311"
193091b266Smatthias.ringwald     // LAP, Inquiry length, Num_responses
203091b266Smatthias.ringwald };
213b9efdc1Smatthias.ringwald hci_cmd_t hci_inquiry_cancel = {
223b9efdc1Smatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x02), ""
233b9efdc1Smatthias.ringwald 	// no params
243b9efdc1Smatthias.ringwald };
25aff8ac5cSmatthias.ringwald hci_cmd_t hci_create_connection = {
26aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
27aff8ac5cSmatthias.ringwald 	// BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
28aff8ac5cSmatthias.ringwald };
29f432a6ddSmatthias.ringwald 
30f432a6ddSmatthias.ringwald hci_cmd_t hci_accept_connection_request = {
31f432a6ddSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x09), "B1"
32f432a6ddSmatthias.ringwald 	// BD_ADDR, Role: become master, stay slave
33f432a6ddSmatthias.ringwald };
343091b266Smatthias.ringwald hci_cmd_t hci_link_key_request_negative_reply = {
353091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
363091b266Smatthias.ringwald };
373091b266Smatthias.ringwald hci_cmd_t hci_pin_code_request_reply = {
383091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
393091b266Smatthias.ringwald     // BD_ADDR, pin length, PIN: c-string
4093b8dc03Smatthias.ringwald };
41aff8ac5cSmatthias.ringwald hci_cmd_t hci_remote_name_request = {
42aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x19), "B112"
43aff8ac5cSmatthias.ringwald 	// BD_ADDR, Page_Scan_Repetition_Mode, Reserved, Clock_Offset
44aff8ac5cSmatthias.ringwald };
45aff8ac5cSmatthias.ringwald 	hci_cmd_t hci_remote_name_request_cancel = {
46aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x1A), "B"
47aff8ac5cSmatthias.ringwald 	// BD_ADDR
48aff8ac5cSmatthias.ringwald };
4993b8dc03Smatthias.ringwald 
50e521ef40Smatthias.ringwald /**
51e521ef40Smatthias.ringwald  *  Controller & Baseband Commands
52e521ef40Smatthias.ringwald  */
53bd67ef2fSmatthias.ringwald hci_cmd_t hci_set_event_mask = {
54bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x01), "44"
55bd67ef2fSmatthias.ringwald     // event_mask lower 4 octets, higher 4 bytes
56bd67ef2fSmatthias.ringwald };
5793b8dc03Smatthias.ringwald hci_cmd_t hci_reset = {
5802ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
59e521ef40Smatthias.ringwald     // no params
60e521ef40Smatthias.ringwald };
61e521ef40Smatthias.ringwald hci_cmd_t hci_delete_stored_link_key = {
62e521ef40Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x12), "B1"
63e521ef40Smatthias.ringwald 	// BD_ADDR, Delete_All_Flag
6402ea9861Smatthias.ringwald };
65bd67ef2fSmatthias.ringwald hci_cmd_t hci_write_local_name = {
66bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x13), "N"
67bd67ef2fSmatthias.ringwald     // Local name (UTF-8, Null Terminated, max 248 octets)
68bd67ef2fSmatthias.ringwald };
6902ea9861Smatthias.ringwald hci_cmd_t hci_write_page_timeout = {
7002ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
7102ea9861Smatthias.ringwald     // Page_Timeout * 0.625 ms
7202ea9861Smatthias.ringwald };
73f432a6ddSmatthias.ringwald hci_cmd_t hci_write_scan_enable = {
74f432a6ddSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x1A), "1"
75f432a6ddSmatthias.ringwald     // Scan_enable: no, inq, page, inq+page
76f432a6ddSmatthias.ringwald };
773091b266Smatthias.ringwald hci_cmd_t hci_write_authentication_enable = {
783091b266Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
793091b266Smatthias.ringwald     // Authentication_Enable
803091b266Smatthias.ringwald };
81bd67ef2fSmatthias.ringwald hci_cmd_t hci_write_class_of_device = {
82bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x24), "3"
83bd67ef2fSmatthias.ringwald     // Class of Device
84bd67ef2fSmatthias.ringwald };
8502ea9861Smatthias.ringwald hci_cmd_t hci_host_buffer_size = {
8602ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
8702ea9861Smatthias.ringwald     // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
8893b8dc03Smatthias.ringwald };
8993b8dc03Smatthias.ringwald 
90bd67ef2fSmatthias.ringwald hci_cmd_t hci_write_inquiry_mode = {
91bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x45), "1"
92bd67ef2fSmatthias.ringwald     // Inquiry mode: 0x00 = standard, 0x01 = with RSSI, 0x02 = extended
93bd67ef2fSmatthias.ringwald };
94bd67ef2fSmatthias.ringwald 
95bd67ef2fSmatthias.ringwald hci_cmd_t hci_write_extended_inquiry_response = {
96bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x52), "1E"
97bd67ef2fSmatthias.ringwald     // FEC_Required, Exstended Inquiry Response
98bd67ef2fSmatthias.ringwald };
99bd67ef2fSmatthias.ringwald 
100bd67ef2fSmatthias.ringwald hci_cmd_t hci_write_simple_pairing_mode = {
101bd67ef2fSmatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x56), "1"
102bd67ef2fSmatthias.ringwald     // mode: 0 = off, 1 = on
103bd67ef2fSmatthias.ringwald };
104bd67ef2fSmatthias.ringwald 
10568d92d03Smatthias.ringwald hci_cmd_t hci_read_bd_addr = {
10668d92d03Smatthias.ringwald 	OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x09), ""
10768d92d03Smatthias.ringwald 	// no params
10868d92d03Smatthias.ringwald };
10968d92d03Smatthias.ringwald 
110*8adf0ddaSmatthias.ringwald hci_cmd_t hci_get_btstack_state = {
111*8adf0ddaSmatthias.ringwald     OPCODE(OGF_BTSTACK, HCI_BTSTACK_GET_STATE), ""
112*8adf0ddaSmatthias.ringwald     // no params ->
113*8adf0ddaSmatthias.ringwald };
114*8adf0ddaSmatthias.ringwald 
11593b8dc03Smatthias.ringwald 
11616833f0aSmatthias.ringwald // the stack is here
11716833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
11816833f0aSmatthias.ringwald 
119475c8125Smatthias.ringwald 
12043625864Smatthias.ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
1218b658ebcSmatthias.ringwald     buffer[pos++] = value;
1228b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
1238b658ebcSmatthias.ringwald }
1248b658ebcSmatthias.ringwald 
1258b658ebcSmatthias.ringwald void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
1268b658ebcSmatthias.ringwald     buffer[pos++] = value;
1278b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
1288b658ebcSmatthias.ringwald     buffer[pos++] = value >> 16;
1298b658ebcSmatthias.ringwald     buffer[pos++] = value >> 24;
13043625864Smatthias.ringwald }
13143625864Smatthias.ringwald 
1321281a47eSmatthias.ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
1331281a47eSmatthias.ringwald     dest[0] = src[5];
1341281a47eSmatthias.ringwald     dest[1] = src[4];
1351281a47eSmatthias.ringwald     dest[2] = src[3];
1361281a47eSmatthias.ringwald     dest[3] = src[2];
1371281a47eSmatthias.ringwald     dest[4] = src[1];
1381281a47eSmatthias.ringwald     dest[5] = src[0];
1391281a47eSmatthias.ringwald }
1401281a47eSmatthias.ringwald 
14102582713Smatthias.ringwald void hexdump(void *data, int size){
14256fe0872Smatthias.ringwald     int i;
14356fe0872Smatthias.ringwald     for (i=0; i<size;i++){
14402582713Smatthias.ringwald         printf("%02X ", ((uint8_t *)data)[i]);
14556fe0872Smatthias.ringwald     }
14656fe0872Smatthias.ringwald     printf("\n");
14756fe0872Smatthias.ringwald }
14856fe0872Smatthias.ringwald 
14916833f0aSmatthias.ringwald /**
15097addcc5Smatthias.ringwald  * Linked link list
15197addcc5Smatthias.ringwald  */
15297addcc5Smatthias.ringwald 
15397addcc5Smatthias.ringwald /**
15497addcc5Smatthias.ringwald  * get link for given address
15597addcc5Smatthias.ringwald  *
15697addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
15797addcc5Smatthias.ringwald  */
158145be03fSmatthias.ringwald #if 0
15997addcc5Smatthias.ringwald static hci_connection_t *link_for_addr(bd_addr_t addr){
16097addcc5Smatthias.ringwald     return NULL;
16197addcc5Smatthias.ringwald }
162145be03fSmatthias.ringwald #endif
16397addcc5Smatthias.ringwald 
16497addcc5Smatthias.ringwald /**
16516833f0aSmatthias.ringwald  * Handler called by HCI transport
16616833f0aSmatthias.ringwald  */
1671cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
16816833f0aSmatthias.ringwald }
16916833f0aSmatthias.ringwald 
17016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
17116833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
17294ab26f8Smatthias.ringwald 
17394ab26f8Smatthias.ringwald     // execute main loop
17494ab26f8Smatthias.ringwald     hci_run();
17516833f0aSmatthias.ringwald }
17622909952Smatthias.ringwald 
17716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1781281a47eSmatthias.ringwald     bd_addr_t addr;
17922909952Smatthias.ringwald 
1803429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
1813429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
1823429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
1833429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
18422909952Smatthias.ringwald     }
18522909952Smatthias.ringwald 
1863429f56bSmatthias.ringwald     // handle BT initialization
1873429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
1887301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
1897301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
1907301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
1917301ad89Smatthias.ringwald         // }
1927301ad89Smatthias.ringwald         // handle normal init sequence
1933429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
1943429f56bSmatthias.ringwald             // odd: waiting for event
1953429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
1963429f56bSmatthias.ringwald                 hci_stack.substate++;
1973429f56bSmatthias.ringwald             }
1983429f56bSmatthias.ringwald         }
19922909952Smatthias.ringwald     }
20022909952Smatthias.ringwald 
2011281a47eSmatthias.ringwald     // link key request
2023429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
2031281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
2041281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
2051281a47eSmatthias.ringwald         return;
2061281a47eSmatthias.ringwald     }
2071281a47eSmatthias.ringwald 
2081281a47eSmatthias.ringwald     // pin code request
2093429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
2101281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
2111281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
2121281a47eSmatthias.ringwald     }
2131281a47eSmatthias.ringwald 
21416833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
21594ab26f8Smatthias.ringwald 
21694ab26f8Smatthias.ringwald 	// execute main loop
21794ab26f8Smatthias.ringwald 	hci_run();
21816833f0aSmatthias.ringwald }
21916833f0aSmatthias.ringwald 
22016833f0aSmatthias.ringwald /** Register L2CAP handlers */
2211cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
22216833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
22316833f0aSmatthias.ringwald }
2241cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
22516833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
22616833f0aSmatthias.ringwald }
22716833f0aSmatthias.ringwald 
2287301ad89Smatthias.ringwald static int null_control_function(void *config){
2297301ad89Smatthias.ringwald     return 0;
2307301ad89Smatthias.ringwald }
2317301ad89Smatthias.ringwald static const char * null_control_name(void *config){
2327301ad89Smatthias.ringwald     return "Hardware unknown";
2337301ad89Smatthias.ringwald }
2347301ad89Smatthias.ringwald 
2357301ad89Smatthias.ringwald static bt_control_t null_control = {
2367301ad89Smatthias.ringwald     null_control_function,
2377301ad89Smatthias.ringwald     null_control_function,
2387301ad89Smatthias.ringwald     null_control_function,
2397301ad89Smatthias.ringwald     null_control_name
2407301ad89Smatthias.ringwald };
2417301ad89Smatthias.ringwald 
24211e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
243475c8125Smatthias.ringwald 
244475c8125Smatthias.ringwald     // reference to use transport layer implementation
24516833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
246475c8125Smatthias.ringwald 
24711e23e5fSmatthias.ringwald     // references to used control implementation
2487301ad89Smatthias.ringwald     if (control) {
24911e23e5fSmatthias.ringwald         hci_stack.control = control;
2507301ad89Smatthias.ringwald     } else {
2517301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2527301ad89Smatthias.ringwald     }
25311e23e5fSmatthias.ringwald 
25411e23e5fSmatthias.ringwald     // reference to used config
25511e23e5fSmatthias.ringwald     hci_stack.config = config;
25611e23e5fSmatthias.ringwald 
25702ea9861Smatthias.ringwald     // empty cmd buffer
25816833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
25916833f0aSmatthias.ringwald 
26016833f0aSmatthias.ringwald     // higher level handler
26116833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
26216833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
26316833f0aSmatthias.ringwald 
26416833f0aSmatthias.ringwald     // register packet handlers with transport
26516833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
26616833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
267475c8125Smatthias.ringwald }
268475c8125Smatthias.ringwald 
269475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
27011e23e5fSmatthias.ringwald     if (power_mode == HCI_POWER_ON) {
2717301ad89Smatthias.ringwald 
2727301ad89Smatthias.ringwald         // set up state machine
2737301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
2747301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
2757301ad89Smatthias.ringwald         hci_stack.substate = 0;
2767301ad89Smatthias.ringwald 
2777301ad89Smatthias.ringwald         // power on
27811e23e5fSmatthias.ringwald         hci_stack.control->on(hci_stack.config);
2797301ad89Smatthias.ringwald 
2807301ad89Smatthias.ringwald         // open low-level device
2817301ad89Smatthias.ringwald         hci_stack.hci_transport->open(hci_stack.config);
2827301ad89Smatthias.ringwald 
28311e23e5fSmatthias.ringwald     } else if (power_mode == HCI_POWER_OFF){
2847301ad89Smatthias.ringwald 
2857301ad89Smatthias.ringwald         // close low-level device
2867301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
2877301ad89Smatthias.ringwald 
2887301ad89Smatthias.ringwald         // power off
28911e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
29011e23e5fSmatthias.ringwald     }
29168d92d03Smatthias.ringwald 
29268d92d03Smatthias.ringwald 	// trigger next/first action
29368d92d03Smatthias.ringwald 	hci_run();
29468d92d03Smatthias.ringwald 
295475c8125Smatthias.ringwald     return 0;
296475c8125Smatthias.ringwald }
297475c8125Smatthias.ringwald 
2983429f56bSmatthias.ringwald uint32_t hci_run(){
2993429f56bSmatthias.ringwald     uint8_t micro_packet;
3003429f56bSmatthias.ringwald     switch (hci_stack.state){
3013429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
3023429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
3033429f56bSmatthias.ringwald                 // odd: waiting for command completion
3043429f56bSmatthias.ringwald                 return 0;
3053429f56bSmatthias.ringwald             }
3063429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3073429f56bSmatthias.ringwald                 // cannot send command yet
3083429f56bSmatthias.ringwald                 return 0;
3093429f56bSmatthias.ringwald             }
3103429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3113429f56bSmatthias.ringwald                 case 0:
31222909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3133429f56bSmatthias.ringwald                     break;
3143429f56bSmatthias.ringwald 				case 1:
315f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
316f432a6ddSmatthias.ringwald 					break;
317f432a6ddSmatthias.ringwald                 case 2:
3183429f56bSmatthias.ringwald                     // ca. 15 sec
3193429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3203429f56bSmatthias.ringwald                     break;
321f432a6ddSmatthias.ringwald 				case 3:
322bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
323f432a6ddSmatthias.ringwald 					break;
324f432a6ddSmatthias.ringwald                 case 4:
3253429f56bSmatthias.ringwald                     // done.
3263429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
327*8adf0ddaSmatthias.ringwald                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
3283429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
3293429f56bSmatthias.ringwald                     break;
3303429f56bSmatthias.ringwald                 default:
3313429f56bSmatthias.ringwald                     break;
332475c8125Smatthias.ringwald             }
3333429f56bSmatthias.ringwald             hci_stack.substate++;
3343429f56bSmatthias.ringwald             break;
3353429f56bSmatthias.ringwald         default:
3363429f56bSmatthias.ringwald             break;
3371f504dbdSmatthias.ringwald     }
33893b8dc03Smatthias.ringwald 
3393429f56bSmatthias.ringwald     // don't check for timetous yet
3403429f56bSmatthias.ringwald     return 0;
3413429f56bSmatthias.ringwald }
34216833f0aSmatthias.ringwald 
34316833f0aSmatthias.ringwald 
34443625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
345*8adf0ddaSmatthias.ringwald     if (READ_CMD_OGF(packet) != OGF_BTSTACK) {
34616833f0aSmatthias.ringwald         return hci_stack.hci_transport->send_acl_packet(packet, size);
34743625864Smatthias.ringwald     }
34843625864Smatthias.ringwald 
349*8adf0ddaSmatthias.ringwald     // BTstack internal commands
350*8adf0ddaSmatthias.ringwald     uint8_t event[3];
351*8adf0ddaSmatthias.ringwald     switch (READ_CMD_OCF(packet)){
352*8adf0ddaSmatthias.ringwald         case HCI_BTSTACK_GET_STATE:
353*8adf0ddaSmatthias.ringwald             event[0] =  HCI_EVENT_BTSTACK_STATE;
354*8adf0ddaSmatthias.ringwald             event[1] = 1;
355*8adf0ddaSmatthias.ringwald             event[2] = hci_stack.state;
356*8adf0ddaSmatthias.ringwald             hci_stack.event_packet_handler(event, 3);
357*8adf0ddaSmatthias.ringwald             break;
358*8adf0ddaSmatthias.ringwald         default:
359*8adf0ddaSmatthias.ringwald             // TODO log into hci dump as vendor specific"event"
360*8adf0ddaSmatthias.ringwald             printf("Error: command %u not implemented\n:", READ_CMD_OCF(packet));
361*8adf0ddaSmatthias.ringwald             break;
362*8adf0ddaSmatthias.ringwald     }
363*8adf0ddaSmatthias.ringwald     return 0;
364*8adf0ddaSmatthias.ringwald }
365*8adf0ddaSmatthias.ringwald 
36649857181Smatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
36749857181Smatthias.ringwald     hci_stack.num_cmd_packets--;
36849857181Smatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
36949857181Smatthias.ringwald }
3703429f56bSmatthias.ringwald 
3711cd208adSmatthias.ringwald uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr){
3721cd208adSmatthias.ringwald 
37302ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
37402ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
37593b8dc03Smatthias.ringwald     int pos = 3;
37693b8dc03Smatthias.ringwald 
37793b8dc03Smatthias.ringwald     const char *format = cmd->format;
37893b8dc03Smatthias.ringwald     uint16_t word;
37993b8dc03Smatthias.ringwald     uint32_t longword;
3803091b266Smatthias.ringwald     uint8_t * ptr;
38193b8dc03Smatthias.ringwald     while (*format) {
38293b8dc03Smatthias.ringwald         switch(*format) {
38393b8dc03Smatthias.ringwald             case '1': //  8 bit value
38493b8dc03Smatthias.ringwald             case '2': // 16 bit value
38593b8dc03Smatthias.ringwald             case 'H': // hci_handle
386554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
38702ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
38893b8dc03Smatthias.ringwald                 if (*format == '2') {
38902ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
39093b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
391554588a5Smatthias.ringwald                     // TODO
39293b8dc03Smatthias.ringwald                 }
39393b8dc03Smatthias.ringwald                 break;
39493b8dc03Smatthias.ringwald             case '3':
39593b8dc03Smatthias.ringwald             case '4':
39693b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
39793b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
39802ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
39902ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
40002ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
40193b8dc03Smatthias.ringwald                 if (*format == '4'){
40202ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
40393b8dc03Smatthias.ringwald                 }
40493b8dc03Smatthias.ringwald                 break;
40593b8dc03Smatthias.ringwald             case 'B': // bt-addr
4063091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
4073091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
4083091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
4093091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
4103091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
4113091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
4123091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
4133091b266Smatthias.ringwald                 break;
4143091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
4153091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
4163091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
4173091b266Smatthias.ringwald                 pos += 16;
41893b8dc03Smatthias.ringwald                 break;
419bd67ef2fSmatthias.ringwald             case 'N': // UTF-8 string, null terminated
420bd67ef2fSmatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
421bd67ef2fSmatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 248);
422bd67ef2fSmatthias.ringwald                 pos += 248;
423bd67ef2fSmatthias.ringwald                 break;
424bd67ef2fSmatthias.ringwald             case 'E': // Extended Inquiry Information 240 octets
425bd67ef2fSmatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
426bd67ef2fSmatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 240);
427bd67ef2fSmatthias.ringwald                 pos += 240;
428bd67ef2fSmatthias.ringwald                 break;
42993b8dc03Smatthias.ringwald             default:
43093b8dc03Smatthias.ringwald                 break;
43193b8dc03Smatthias.ringwald         }
43293b8dc03Smatthias.ringwald         format++;
43393b8dc03Smatthias.ringwald     };
43402ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
4351cd208adSmatthias.ringwald     return pos;
4361cd208adSmatthias.ringwald }
4371cd208adSmatthias.ringwald 
4381cd208adSmatthias.ringwald uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...){
4391cd208adSmatthias.ringwald     va_list argptr;
4401cd208adSmatthias.ringwald     va_start(argptr, cmd);
4411cd208adSmatthias.ringwald     uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
4421cd208adSmatthias.ringwald     va_end(argptr);
4431cd208adSmatthias.ringwald     return len;
4441cd208adSmatthias.ringwald }
4451cd208adSmatthias.ringwald 
4461cd208adSmatthias.ringwald /**
4471cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
4481cd208adSmatthias.ringwald  */
4491cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
4501cd208adSmatthias.ringwald     va_list argptr;
4511cd208adSmatthias.ringwald     va_start(argptr, cmd);
4521cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
4531cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
4541cd208adSmatthias.ringwald     va_end(argptr);
4551cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
45693b8dc03Smatthias.ringwald }