xref: /btstack/src/hci.c (revision 1cd208ad37cdd0ed31b73972e9fb6f3d529db0f2)
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 
11093b8dc03Smatthias.ringwald 
11116833f0aSmatthias.ringwald // the stack is here
11216833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
11316833f0aSmatthias.ringwald 
114475c8125Smatthias.ringwald 
11543625864Smatthias.ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
1168b658ebcSmatthias.ringwald     buffer[pos++] = value;
1178b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
1188b658ebcSmatthias.ringwald }
1198b658ebcSmatthias.ringwald 
1208b658ebcSmatthias.ringwald void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
1218b658ebcSmatthias.ringwald     buffer[pos++] = value;
1228b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
1238b658ebcSmatthias.ringwald     buffer[pos++] = value >> 16;
1248b658ebcSmatthias.ringwald     buffer[pos++] = value >> 24;
12543625864Smatthias.ringwald }
12643625864Smatthias.ringwald 
1271281a47eSmatthias.ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
1281281a47eSmatthias.ringwald     dest[0] = src[5];
1291281a47eSmatthias.ringwald     dest[1] = src[4];
1301281a47eSmatthias.ringwald     dest[2] = src[3];
1311281a47eSmatthias.ringwald     dest[3] = src[2];
1321281a47eSmatthias.ringwald     dest[4] = src[1];
1331281a47eSmatthias.ringwald     dest[5] = src[0];
1341281a47eSmatthias.ringwald }
1351281a47eSmatthias.ringwald 
13602582713Smatthias.ringwald void hexdump(void *data, int size){
13756fe0872Smatthias.ringwald     int i;
13856fe0872Smatthias.ringwald     for (i=0; i<size;i++){
13902582713Smatthias.ringwald         printf("%02X ", ((uint8_t *)data)[i]);
14056fe0872Smatthias.ringwald     }
14156fe0872Smatthias.ringwald     printf("\n");
14256fe0872Smatthias.ringwald }
14356fe0872Smatthias.ringwald 
14416833f0aSmatthias.ringwald /**
14597addcc5Smatthias.ringwald  * Linked link list
14697addcc5Smatthias.ringwald  */
14797addcc5Smatthias.ringwald 
14897addcc5Smatthias.ringwald /**
14997addcc5Smatthias.ringwald  * get link for given address
15097addcc5Smatthias.ringwald  *
15197addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
15297addcc5Smatthias.ringwald  */
153145be03fSmatthias.ringwald #if 0
15497addcc5Smatthias.ringwald static hci_connection_t *link_for_addr(bd_addr_t addr){
15597addcc5Smatthias.ringwald     return NULL;
15697addcc5Smatthias.ringwald }
157145be03fSmatthias.ringwald #endif
15897addcc5Smatthias.ringwald 
15997addcc5Smatthias.ringwald /**
16016833f0aSmatthias.ringwald  * Handler called by HCI transport
16116833f0aSmatthias.ringwald  */
162*1cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){
16316833f0aSmatthias.ringwald }
16416833f0aSmatthias.ringwald 
16516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
16616833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
16794ab26f8Smatthias.ringwald 
16894ab26f8Smatthias.ringwald     // execute main loop
16994ab26f8Smatthias.ringwald     hci_run();
17016833f0aSmatthias.ringwald }
17122909952Smatthias.ringwald 
17216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1731281a47eSmatthias.ringwald     bd_addr_t addr;
17422909952Smatthias.ringwald 
1753429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
1763429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
1773429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
1783429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
17922909952Smatthias.ringwald     }
18022909952Smatthias.ringwald 
1813429f56bSmatthias.ringwald     // handle BT initialization
1823429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
1837301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
1847301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
1857301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
1867301ad89Smatthias.ringwald         // }
1877301ad89Smatthias.ringwald         // handle normal init sequence
1883429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
1893429f56bSmatthias.ringwald             // odd: waiting for event
1903429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
1913429f56bSmatthias.ringwald                 hci_stack.substate++;
1923429f56bSmatthias.ringwald             }
1933429f56bSmatthias.ringwald         }
19422909952Smatthias.ringwald     }
19522909952Smatthias.ringwald 
1961281a47eSmatthias.ringwald     // link key request
1973429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
1981281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1991281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
2001281a47eSmatthias.ringwald         return;
2011281a47eSmatthias.ringwald     }
2021281a47eSmatthias.ringwald 
2031281a47eSmatthias.ringwald     // pin code request
2043429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
2051281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
2061281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
2071281a47eSmatthias.ringwald     }
2081281a47eSmatthias.ringwald 
20916833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
21094ab26f8Smatthias.ringwald 
21194ab26f8Smatthias.ringwald 	// execute main loop
21294ab26f8Smatthias.ringwald 	hci_run();
21316833f0aSmatthias.ringwald }
21416833f0aSmatthias.ringwald 
21516833f0aSmatthias.ringwald /** Register L2CAP handlers */
216*1cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
21716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
21816833f0aSmatthias.ringwald }
219*1cd208adSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
22016833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
22116833f0aSmatthias.ringwald }
22216833f0aSmatthias.ringwald 
2237301ad89Smatthias.ringwald static int null_control_function(void *config){
2247301ad89Smatthias.ringwald     return 0;
2257301ad89Smatthias.ringwald }
2267301ad89Smatthias.ringwald static const char * null_control_name(void *config){
2277301ad89Smatthias.ringwald     return "Hardware unknown";
2287301ad89Smatthias.ringwald }
2297301ad89Smatthias.ringwald 
2307301ad89Smatthias.ringwald static bt_control_t null_control = {
2317301ad89Smatthias.ringwald     null_control_function,
2327301ad89Smatthias.ringwald     null_control_function,
2337301ad89Smatthias.ringwald     null_control_function,
2347301ad89Smatthias.ringwald     null_control_name
2357301ad89Smatthias.ringwald };
2367301ad89Smatthias.ringwald 
23711e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
238475c8125Smatthias.ringwald 
239475c8125Smatthias.ringwald     // reference to use transport layer implementation
24016833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
241475c8125Smatthias.ringwald 
24211e23e5fSmatthias.ringwald     // references to used control implementation
2437301ad89Smatthias.ringwald     if (control) {
24411e23e5fSmatthias.ringwald         hci_stack.control = control;
2457301ad89Smatthias.ringwald     } else {
2467301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2477301ad89Smatthias.ringwald     }
24811e23e5fSmatthias.ringwald 
24911e23e5fSmatthias.ringwald     // reference to used config
25011e23e5fSmatthias.ringwald     hci_stack.config = config;
25111e23e5fSmatthias.ringwald 
25202ea9861Smatthias.ringwald     // empty cmd buffer
25316833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
25416833f0aSmatthias.ringwald 
25516833f0aSmatthias.ringwald     // higher level handler
25616833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
25716833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
25816833f0aSmatthias.ringwald 
25916833f0aSmatthias.ringwald     // register packet handlers with transport
26016833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
26116833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
262475c8125Smatthias.ringwald }
263475c8125Smatthias.ringwald 
264475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
26511e23e5fSmatthias.ringwald     if (power_mode == HCI_POWER_ON) {
2667301ad89Smatthias.ringwald 
2677301ad89Smatthias.ringwald         // set up state machine
2687301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
2697301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
2707301ad89Smatthias.ringwald         hci_stack.substate = 0;
2717301ad89Smatthias.ringwald 
2727301ad89Smatthias.ringwald         // power on
27311e23e5fSmatthias.ringwald         hci_stack.control->on(hci_stack.config);
2747301ad89Smatthias.ringwald 
2757301ad89Smatthias.ringwald         // open low-level device
2767301ad89Smatthias.ringwald         hci_stack.hci_transport->open(hci_stack.config);
2777301ad89Smatthias.ringwald 
27811e23e5fSmatthias.ringwald     } else if (power_mode == HCI_POWER_OFF){
2797301ad89Smatthias.ringwald 
2807301ad89Smatthias.ringwald         // close low-level device
2817301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
2827301ad89Smatthias.ringwald 
2837301ad89Smatthias.ringwald         // power off
28411e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
28511e23e5fSmatthias.ringwald     }
28668d92d03Smatthias.ringwald 
28768d92d03Smatthias.ringwald 	// trigger next/first action
28868d92d03Smatthias.ringwald 	hci_run();
28968d92d03Smatthias.ringwald 
290475c8125Smatthias.ringwald     return 0;
291475c8125Smatthias.ringwald }
292475c8125Smatthias.ringwald 
2933429f56bSmatthias.ringwald uint32_t hci_run(){
2943429f56bSmatthias.ringwald     uint8_t micro_packet;
2953429f56bSmatthias.ringwald     switch (hci_stack.state){
2963429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
2973429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
2983429f56bSmatthias.ringwald                 // odd: waiting for command completion
2993429f56bSmatthias.ringwald                 return 0;
3003429f56bSmatthias.ringwald             }
3013429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
3023429f56bSmatthias.ringwald                 // cannot send command yet
3033429f56bSmatthias.ringwald                 return 0;
3043429f56bSmatthias.ringwald             }
3053429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
3063429f56bSmatthias.ringwald                 case 0:
30722909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
3083429f56bSmatthias.ringwald                     break;
3093429f56bSmatthias.ringwald 				case 1:
310f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
311f432a6ddSmatthias.ringwald 					break;
312f432a6ddSmatthias.ringwald                 case 2:
3133429f56bSmatthias.ringwald                     // ca. 15 sec
3143429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
3153429f56bSmatthias.ringwald                     break;
316f432a6ddSmatthias.ringwald 				case 3:
317bd67ef2fSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
318f432a6ddSmatthias.ringwald 					break;
319f432a6ddSmatthias.ringwald                 case 4:
3203429f56bSmatthias.ringwald                     // done.
3213429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
3223429f56bSmatthias.ringwald                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
3233429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
3243429f56bSmatthias.ringwald                     break;
3253429f56bSmatthias.ringwald                 default:
3263429f56bSmatthias.ringwald                     break;
327475c8125Smatthias.ringwald             }
3283429f56bSmatthias.ringwald             hci_stack.substate++;
3293429f56bSmatthias.ringwald             break;
3303429f56bSmatthias.ringwald         default:
3313429f56bSmatthias.ringwald             break;
3321f504dbdSmatthias.ringwald     }
33393b8dc03Smatthias.ringwald 
3343429f56bSmatthias.ringwald     // don't check for timetous yet
3353429f56bSmatthias.ringwald     return 0;
3363429f56bSmatthias.ringwald }
33716833f0aSmatthias.ringwald 
33816833f0aSmatthias.ringwald 
33943625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
34016833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
34143625864Smatthias.ringwald }
34243625864Smatthias.ringwald 
34349857181Smatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){
34449857181Smatthias.ringwald     hci_stack.num_cmd_packets--;
34549857181Smatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(packet, size);
34649857181Smatthias.ringwald }
3473429f56bSmatthias.ringwald 
348*1cd208adSmatthias.ringwald uint16_t hci_create_cmd_internal(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, va_list argptr){
349*1cd208adSmatthias.ringwald 
35002ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
35102ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
35293b8dc03Smatthias.ringwald     int pos = 3;
35393b8dc03Smatthias.ringwald 
35493b8dc03Smatthias.ringwald     const char *format = cmd->format;
35593b8dc03Smatthias.ringwald     uint16_t word;
35693b8dc03Smatthias.ringwald     uint32_t longword;
3573091b266Smatthias.ringwald     uint8_t * ptr;
35893b8dc03Smatthias.ringwald     while (*format) {
35993b8dc03Smatthias.ringwald         switch(*format) {
36093b8dc03Smatthias.ringwald             case '1': //  8 bit value
36193b8dc03Smatthias.ringwald             case '2': // 16 bit value
36293b8dc03Smatthias.ringwald             case 'H': // hci_handle
363554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
36402ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
36593b8dc03Smatthias.ringwald                 if (*format == '2') {
36602ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
36793b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
368554588a5Smatthias.ringwald                     // TODO
36993b8dc03Smatthias.ringwald                 }
37093b8dc03Smatthias.ringwald                 break;
37193b8dc03Smatthias.ringwald             case '3':
37293b8dc03Smatthias.ringwald             case '4':
37393b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
37493b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
37502ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
37602ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
37702ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
37893b8dc03Smatthias.ringwald                 if (*format == '4'){
37902ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
38093b8dc03Smatthias.ringwald                 }
38193b8dc03Smatthias.ringwald                 break;
38293b8dc03Smatthias.ringwald             case 'B': // bt-addr
3833091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3843091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
3853091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
3863091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
3873091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
3883091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
3893091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
3903091b266Smatthias.ringwald                 break;
3913091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
3923091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3933091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
3943091b266Smatthias.ringwald                 pos += 16;
39593b8dc03Smatthias.ringwald                 break;
396bd67ef2fSmatthias.ringwald             case 'N': // UTF-8 string, null terminated
397bd67ef2fSmatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
398bd67ef2fSmatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 248);
399bd67ef2fSmatthias.ringwald                 pos += 248;
400bd67ef2fSmatthias.ringwald                 break;
401bd67ef2fSmatthias.ringwald             case 'E': // Extended Inquiry Information 240 octets
402bd67ef2fSmatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
403bd67ef2fSmatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 240);
404bd67ef2fSmatthias.ringwald                 pos += 240;
405bd67ef2fSmatthias.ringwald                 break;
40693b8dc03Smatthias.ringwald             default:
40793b8dc03Smatthias.ringwald                 break;
40893b8dc03Smatthias.ringwald         }
40993b8dc03Smatthias.ringwald         format++;
41093b8dc03Smatthias.ringwald     };
41102ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
412*1cd208adSmatthias.ringwald     return pos;
413*1cd208adSmatthias.ringwald }
414*1cd208adSmatthias.ringwald 
415*1cd208adSmatthias.ringwald uint16_t hci_create_cmd(uint8_t *hci_cmd_buffer, hci_cmd_t *cmd, ...){
416*1cd208adSmatthias.ringwald     va_list argptr;
417*1cd208adSmatthias.ringwald     va_start(argptr, cmd);
418*1cd208adSmatthias.ringwald     uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
419*1cd208adSmatthias.ringwald     va_end(argptr);
420*1cd208adSmatthias.ringwald     return len;
421*1cd208adSmatthias.ringwald }
422*1cd208adSmatthias.ringwald 
423*1cd208adSmatthias.ringwald /**
424*1cd208adSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
425*1cd208adSmatthias.ringwald  */
426*1cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
427*1cd208adSmatthias.ringwald     va_list argptr;
428*1cd208adSmatthias.ringwald     va_start(argptr, cmd);
429*1cd208adSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
430*1cd208adSmatthias.ringwald     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
431*1cd208adSmatthias.ringwald     va_end(argptr);
432*1cd208adSmatthias.ringwald     return hci_send_cmd_packet(hci_cmd_buffer, size);
43393b8dc03Smatthias.ringwald }