xref: /btstack/src/hci.c (revision f432a6ddd9d1b8bf125a185ef7daaa52d75ac581)
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
1868d92d03Smatthias.ringwald #define OGF_INFORMATIONAL_PARAMETERS 0x04
1993b8dc03Smatthias.ringwald 
20e521ef40Smatthias.ringwald /**
21e521ef40Smatthias.ringwald  *  Link Control Commands
22e521ef40Smatthias.ringwald  */
2393b8dc03Smatthias.ringwald hci_cmd_t hci_inquiry = {
243091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x01), "311"
253091b266Smatthias.ringwald     // LAP, Inquiry length, Num_responses
263091b266Smatthias.ringwald };
273b9efdc1Smatthias.ringwald hci_cmd_t hci_inquiry_cancel = {
283b9efdc1Smatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x02), ""
293b9efdc1Smatthias.ringwald 	// no params
303b9efdc1Smatthias.ringwald };
31aff8ac5cSmatthias.ringwald hci_cmd_t hci_create_connection = {
32aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
33aff8ac5cSmatthias.ringwald 	// BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
34aff8ac5cSmatthias.ringwald };
35*f432a6ddSmatthias.ringwald 
36*f432a6ddSmatthias.ringwald hci_cmd_t hci_accept_connection_request = {
37*f432a6ddSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x09), "B1"
38*f432a6ddSmatthias.ringwald 	// BD_ADDR, Role: become master, stay slave
39*f432a6ddSmatthias.ringwald };
403091b266Smatthias.ringwald hci_cmd_t hci_link_key_request_negative_reply = {
413091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
423091b266Smatthias.ringwald };
433091b266Smatthias.ringwald hci_cmd_t hci_pin_code_request_reply = {
443091b266Smatthias.ringwald     OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
453091b266Smatthias.ringwald     // BD_ADDR, pin length, PIN: c-string
4693b8dc03Smatthias.ringwald };
47aff8ac5cSmatthias.ringwald hci_cmd_t hci_remote_name_request = {
48aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x19), "B112"
49aff8ac5cSmatthias.ringwald 	// BD_ADDR, Page_Scan_Repetition_Mode, Reserved, Clock_Offset
50aff8ac5cSmatthias.ringwald };
51aff8ac5cSmatthias.ringwald 	hci_cmd_t hci_remote_name_request_cancel = {
52aff8ac5cSmatthias.ringwald 	OPCODE(OGF_LINK_CONTROL, 0x1A), "B"
53aff8ac5cSmatthias.ringwald 	// BD_ADDR
54aff8ac5cSmatthias.ringwald };
5593b8dc03Smatthias.ringwald 
56e521ef40Smatthias.ringwald /**
57e521ef40Smatthias.ringwald  *  Controller & Baseband Commands
58e521ef40Smatthias.ringwald  */
5993b8dc03Smatthias.ringwald hci_cmd_t hci_reset = {
6002ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
61e521ef40Smatthias.ringwald     // no params
62e521ef40Smatthias.ringwald };
63e521ef40Smatthias.ringwald hci_cmd_t hci_delete_stored_link_key = {
64e521ef40Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x12), "B1"
65e521ef40Smatthias.ringwald 	// BD_ADDR, Delete_All_Flag
6602ea9861Smatthias.ringwald };
6702ea9861Smatthias.ringwald hci_cmd_t hci_write_page_timeout = {
6802ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
6902ea9861Smatthias.ringwald     // Page_Timeout * 0.625 ms
7002ea9861Smatthias.ringwald };
71*f432a6ddSmatthias.ringwald hci_cmd_t hci_write_scan_enable = {
72*f432a6ddSmatthias.ringwald OPCODE(OGF_CONTROLLER_BASEBAND, 0x1A), "1"
73*f432a6ddSmatthias.ringwald // Scan_enable: no, inq, page, inq+page
74*f432a6ddSmatthias.ringwald };
75*f432a6ddSmatthias.ringwald 
763091b266Smatthias.ringwald hci_cmd_t hci_write_authentication_enable = {
773091b266Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
783091b266Smatthias.ringwald     // Authentication_Enable
793091b266Smatthias.ringwald };
8002ea9861Smatthias.ringwald hci_cmd_t hci_host_buffer_size = {
8102ea9861Smatthias.ringwald     OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
8202ea9861Smatthias.ringwald     // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
8393b8dc03Smatthias.ringwald };
8493b8dc03Smatthias.ringwald 
8568d92d03Smatthias.ringwald hci_cmd_t hci_read_bd_addr = {
8668d92d03Smatthias.ringwald 	OPCODE(OGF_INFORMATIONAL_PARAMETERS, 0x09), ""
8768d92d03Smatthias.ringwald 	// no params
8868d92d03Smatthias.ringwald };
8968d92d03Smatthias.ringwald 
9093b8dc03Smatthias.ringwald 
9116833f0aSmatthias.ringwald // the stack is here
9216833f0aSmatthias.ringwald static hci_stack_t       hci_stack;
9316833f0aSmatthias.ringwald 
94475c8125Smatthias.ringwald 
9543625864Smatthias.ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
968b658ebcSmatthias.ringwald     buffer[pos++] = value;
978b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
988b658ebcSmatthias.ringwald }
998b658ebcSmatthias.ringwald 
1008b658ebcSmatthias.ringwald void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
1018b658ebcSmatthias.ringwald     buffer[pos++] = value;
1028b658ebcSmatthias.ringwald     buffer[pos++] = value >> 8;
1038b658ebcSmatthias.ringwald     buffer[pos++] = value >> 16;
1048b658ebcSmatthias.ringwald     buffer[pos++] = value >> 24;
10543625864Smatthias.ringwald }
10643625864Smatthias.ringwald 
1071281a47eSmatthias.ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
1081281a47eSmatthias.ringwald     dest[0] = src[5];
1091281a47eSmatthias.ringwald     dest[1] = src[4];
1101281a47eSmatthias.ringwald     dest[2] = src[3];
1111281a47eSmatthias.ringwald     dest[3] = src[2];
1121281a47eSmatthias.ringwald     dest[4] = src[1];
1131281a47eSmatthias.ringwald     dest[5] = src[0];
1141281a47eSmatthias.ringwald }
1151281a47eSmatthias.ringwald 
11602582713Smatthias.ringwald void hexdump(void *data, int size){
11756fe0872Smatthias.ringwald     int i;
11856fe0872Smatthias.ringwald     for (i=0; i<size;i++){
11902582713Smatthias.ringwald         printf("%02X ", ((uint8_t *)data)[i]);
12056fe0872Smatthias.ringwald     }
12156fe0872Smatthias.ringwald     printf("\n");
12256fe0872Smatthias.ringwald }
12356fe0872Smatthias.ringwald 
12416833f0aSmatthias.ringwald /**
12597addcc5Smatthias.ringwald  * Linked link list
12697addcc5Smatthias.ringwald  */
12797addcc5Smatthias.ringwald 
12897addcc5Smatthias.ringwald /**
12997addcc5Smatthias.ringwald  * get link for given address
13097addcc5Smatthias.ringwald  *
13197addcc5Smatthias.ringwald  * @return connection OR NULL, if not found
13297addcc5Smatthias.ringwald  */
133145be03fSmatthias.ringwald #if 0
13497addcc5Smatthias.ringwald static hci_connection_t *link_for_addr(bd_addr_t addr){
13597addcc5Smatthias.ringwald     return NULL;
13697addcc5Smatthias.ringwald }
137145be03fSmatthias.ringwald #endif
13897addcc5Smatthias.ringwald 
13997addcc5Smatthias.ringwald /**
14016833f0aSmatthias.ringwald  * Handler called by HCI transport
14116833f0aSmatthias.ringwald  */
14216833f0aSmatthias.ringwald static void dummy_handler(uint8_t *packet, int size){
14316833f0aSmatthias.ringwald }
14416833f0aSmatthias.ringwald 
14516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){
14616833f0aSmatthias.ringwald     hci_stack.acl_packet_handler(packet, size);
14794ab26f8Smatthias.ringwald 
14894ab26f8Smatthias.ringwald     // execute main loop
14994ab26f8Smatthias.ringwald     hci_run();
15016833f0aSmatthias.ringwald }
15122909952Smatthias.ringwald 
15216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){
1531281a47eSmatthias.ringwald     bd_addr_t addr;
15422909952Smatthias.ringwald 
1553429f56bSmatthias.ringwald     // Get Num_HCI_Command_Packets
1563429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
1573429f56bSmatthias.ringwald         packet[0] == HCI_EVENT_COMMAND_STATUS){
1583429f56bSmatthias.ringwald         hci_stack.num_cmd_packets = packet[2];
15922909952Smatthias.ringwald     }
16022909952Smatthias.ringwald 
1613429f56bSmatthias.ringwald     // handle BT initialization
1623429f56bSmatthias.ringwald     if (hci_stack.state == HCI_STATE_INITIALIZING){
1637301ad89Smatthias.ringwald         // handle H4 synchronization loss on restart
1647301ad89Smatthias.ringwald         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
1657301ad89Smatthias.ringwald         //    hci_stack.substate = 0;
1667301ad89Smatthias.ringwald         // }
1677301ad89Smatthias.ringwald         // handle normal init sequence
1683429f56bSmatthias.ringwald         if (hci_stack.substate % 2){
1693429f56bSmatthias.ringwald             // odd: waiting for event
1703429f56bSmatthias.ringwald             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
1713429f56bSmatthias.ringwald                 hci_stack.substate++;
1723429f56bSmatthias.ringwald             }
1733429f56bSmatthias.ringwald         }
17422909952Smatthias.ringwald     }
17522909952Smatthias.ringwald 
1761281a47eSmatthias.ringwald     // link key request
1773429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
1781281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1791281a47eSmatthias.ringwald         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
1801281a47eSmatthias.ringwald         return;
1811281a47eSmatthias.ringwald     }
1821281a47eSmatthias.ringwald 
1831281a47eSmatthias.ringwald     // pin code request
1843429f56bSmatthias.ringwald     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
1851281a47eSmatthias.ringwald         bt_flip_addr(addr, &packet[2]);
1861281a47eSmatthias.ringwald         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
1871281a47eSmatthias.ringwald     }
1881281a47eSmatthias.ringwald 
18916833f0aSmatthias.ringwald     hci_stack.event_packet_handler(packet, size);
19094ab26f8Smatthias.ringwald 
19194ab26f8Smatthias.ringwald 	// execute main loop
19294ab26f8Smatthias.ringwald 	hci_run();
19316833f0aSmatthias.ringwald }
19416833f0aSmatthias.ringwald 
19516833f0aSmatthias.ringwald /** Register L2CAP handlers */
19616833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
19716833f0aSmatthias.ringwald     hci_stack.event_packet_handler = handler;
19816833f0aSmatthias.ringwald }
19916833f0aSmatthias.ringwald void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
20016833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = handler;
20116833f0aSmatthias.ringwald }
20216833f0aSmatthias.ringwald 
2037301ad89Smatthias.ringwald static int null_control_function(void *config){
2047301ad89Smatthias.ringwald     return 0;
2057301ad89Smatthias.ringwald }
2067301ad89Smatthias.ringwald static const char * null_control_name(void *config){
2077301ad89Smatthias.ringwald     return "Hardware unknown";
2087301ad89Smatthias.ringwald }
2097301ad89Smatthias.ringwald 
2107301ad89Smatthias.ringwald static bt_control_t null_control = {
2117301ad89Smatthias.ringwald     null_control_function,
2127301ad89Smatthias.ringwald     null_control_function,
2137301ad89Smatthias.ringwald     null_control_function,
2147301ad89Smatthias.ringwald     null_control_name
2157301ad89Smatthias.ringwald };
2167301ad89Smatthias.ringwald 
21711e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
218475c8125Smatthias.ringwald 
219475c8125Smatthias.ringwald     // reference to use transport layer implementation
22016833f0aSmatthias.ringwald     hci_stack.hci_transport = transport;
221475c8125Smatthias.ringwald 
22211e23e5fSmatthias.ringwald     // references to used control implementation
2237301ad89Smatthias.ringwald     if (control) {
22411e23e5fSmatthias.ringwald         hci_stack.control = control;
2257301ad89Smatthias.ringwald     } else {
2267301ad89Smatthias.ringwald         hci_stack.control = &null_control;
2277301ad89Smatthias.ringwald     }
22811e23e5fSmatthias.ringwald 
22911e23e5fSmatthias.ringwald     // reference to used config
23011e23e5fSmatthias.ringwald     hci_stack.config = config;
23111e23e5fSmatthias.ringwald 
23202ea9861Smatthias.ringwald     // empty cmd buffer
23316833f0aSmatthias.ringwald     hci_stack.hci_cmd_buffer = malloc(3+255);
23416833f0aSmatthias.ringwald 
23516833f0aSmatthias.ringwald     // higher level handler
23616833f0aSmatthias.ringwald     hci_stack.event_packet_handler = dummy_handler;
23716833f0aSmatthias.ringwald     hci_stack.acl_packet_handler = dummy_handler;
23816833f0aSmatthias.ringwald 
23916833f0aSmatthias.ringwald     // register packet handlers with transport
24016833f0aSmatthias.ringwald     transport->register_event_packet_handler( event_handler);
24116833f0aSmatthias.ringwald     transport->register_acl_packet_handler( acl_handler);
242475c8125Smatthias.ringwald }
243475c8125Smatthias.ringwald 
244475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){
24511e23e5fSmatthias.ringwald     if (power_mode == HCI_POWER_ON) {
2467301ad89Smatthias.ringwald 
2477301ad89Smatthias.ringwald         // set up state machine
2487301ad89Smatthias.ringwald         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
2497301ad89Smatthias.ringwald         hci_stack.state = HCI_STATE_INITIALIZING;
2507301ad89Smatthias.ringwald         hci_stack.substate = 0;
2517301ad89Smatthias.ringwald 
2527301ad89Smatthias.ringwald         // power on
25311e23e5fSmatthias.ringwald         hci_stack.control->on(hci_stack.config);
2547301ad89Smatthias.ringwald 
2557301ad89Smatthias.ringwald         // open low-level device
2567301ad89Smatthias.ringwald         hci_stack.hci_transport->open(hci_stack.config);
2577301ad89Smatthias.ringwald 
25811e23e5fSmatthias.ringwald     } else if (power_mode == HCI_POWER_OFF){
2597301ad89Smatthias.ringwald 
2607301ad89Smatthias.ringwald         // close low-level device
2617301ad89Smatthias.ringwald         hci_stack.hci_transport->close(hci_stack.config);
2627301ad89Smatthias.ringwald 
2637301ad89Smatthias.ringwald         // power off
26411e23e5fSmatthias.ringwald         hci_stack.control->off(hci_stack.config);
26511e23e5fSmatthias.ringwald     }
26668d92d03Smatthias.ringwald 
26768d92d03Smatthias.ringwald 	// trigger next/first action
26868d92d03Smatthias.ringwald 	hci_run();
26968d92d03Smatthias.ringwald 
270475c8125Smatthias.ringwald     return 0;
271475c8125Smatthias.ringwald }
272475c8125Smatthias.ringwald 
2733429f56bSmatthias.ringwald uint32_t hci_run(){
2743429f56bSmatthias.ringwald     uint8_t micro_packet;
2753429f56bSmatthias.ringwald     switch (hci_stack.state){
2763429f56bSmatthias.ringwald         case HCI_STATE_INITIALIZING:
2773429f56bSmatthias.ringwald             if (hci_stack.substate % 2) {
2783429f56bSmatthias.ringwald                 // odd: waiting for command completion
2793429f56bSmatthias.ringwald                 return 0;
2803429f56bSmatthias.ringwald             }
2813429f56bSmatthias.ringwald             if (hci_stack.num_cmd_packets == 0) {
2823429f56bSmatthias.ringwald                 // cannot send command yet
2833429f56bSmatthias.ringwald                 return 0;
2843429f56bSmatthias.ringwald             }
2853429f56bSmatthias.ringwald             switch (hci_stack.substate/2){
2863429f56bSmatthias.ringwald                 case 0:
28722909952Smatthias.ringwald                     hci_send_cmd(&hci_reset);
2883429f56bSmatthias.ringwald                     break;
2893429f56bSmatthias.ringwald 				case 1:
290*f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_read_bd_addr);
291*f432a6ddSmatthias.ringwald 					break;
292*f432a6ddSmatthias.ringwald                 case 2:
2933429f56bSmatthias.ringwald                     // ca. 15 sec
2943429f56bSmatthias.ringwald                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
2953429f56bSmatthias.ringwald                     break;
296*f432a6ddSmatthias.ringwald 				case 3:
297*f432a6ddSmatthias.ringwald 					hci_send_cmd(&hci_write_scan_enable, 3);
298*f432a6ddSmatthias.ringwald 					break;
299*f432a6ddSmatthias.ringwald                 case 4:
3003429f56bSmatthias.ringwald                     // done.
3013429f56bSmatthias.ringwald                     hci_stack.state = HCI_STATE_WORKING;
3023429f56bSmatthias.ringwald                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
3033429f56bSmatthias.ringwald                     hci_stack.event_packet_handler(&micro_packet, 1);
3043429f56bSmatthias.ringwald                     break;
3053429f56bSmatthias.ringwald                 default:
3063429f56bSmatthias.ringwald                     break;
307475c8125Smatthias.ringwald             }
3083429f56bSmatthias.ringwald             hci_stack.substate++;
3093429f56bSmatthias.ringwald             break;
3103429f56bSmatthias.ringwald         default:
3113429f56bSmatthias.ringwald             break;
3121f504dbdSmatthias.ringwald     }
31393b8dc03Smatthias.ringwald 
3143429f56bSmatthias.ringwald     // don't check for timetous yet
3153429f56bSmatthias.ringwald     return 0;
3163429f56bSmatthias.ringwald }
31716833f0aSmatthias.ringwald 
31816833f0aSmatthias.ringwald 
31943625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){
32016833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_acl_packet(packet, size);
32143625864Smatthias.ringwald }
32243625864Smatthias.ringwald 
3233429f56bSmatthias.ringwald 
3243429f56bSmatthias.ringwald /**
3253429f56bSmatthias.ringwald  * pre: numcmds >= 0 - it's allowed to send a command to the controller
3263429f56bSmatthias.ringwald  */
32702ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){
32816833f0aSmatthias.ringwald     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
32902ea9861Smatthias.ringwald     hci_cmd_buffer[0] = cmd->opcode & 0xff;
33002ea9861Smatthias.ringwald     hci_cmd_buffer[1] = cmd->opcode >> 8;
33193b8dc03Smatthias.ringwald     int pos = 3;
33293b8dc03Smatthias.ringwald 
33393b8dc03Smatthias.ringwald     va_list argptr;
33493b8dc03Smatthias.ringwald     va_start(argptr, cmd);
33593b8dc03Smatthias.ringwald     const char *format = cmd->format;
33693b8dc03Smatthias.ringwald     uint16_t word;
33793b8dc03Smatthias.ringwald     uint32_t longword;
3383091b266Smatthias.ringwald     uint8_t * ptr;
33993b8dc03Smatthias.ringwald     while (*format) {
34093b8dc03Smatthias.ringwald         switch(*format) {
34193b8dc03Smatthias.ringwald             case '1': //  8 bit value
34293b8dc03Smatthias.ringwald             case '2': // 16 bit value
34393b8dc03Smatthias.ringwald             case 'H': // hci_handle
344554588a5Smatthias.ringwald                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
34502ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = word & 0xff;
34693b8dc03Smatthias.ringwald                 if (*format == '2') {
34702ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = word >> 8;
34893b8dc03Smatthias.ringwald                 } else if (*format == 'H') {
349554588a5Smatthias.ringwald                     // TODO
35093b8dc03Smatthias.ringwald                 }
35193b8dc03Smatthias.ringwald                 break;
35293b8dc03Smatthias.ringwald             case '3':
35393b8dc03Smatthias.ringwald             case '4':
35493b8dc03Smatthias.ringwald                 longword = va_arg(argptr, uint32_t);
35593b8dc03Smatthias.ringwald                 // longword = va_arg(argptr, int);
35602ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword;
35702ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 8;
35802ea9861Smatthias.ringwald                 hci_cmd_buffer[pos++] = longword >> 16;
35993b8dc03Smatthias.ringwald                 if (*format == '4'){
36002ea9861Smatthias.ringwald                     hci_cmd_buffer[pos++] = longword >> 24;
36193b8dc03Smatthias.ringwald                 }
36293b8dc03Smatthias.ringwald                 break;
36393b8dc03Smatthias.ringwald             case 'B': // bt-addr
3643091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3653091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[5];
3663091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[4];
3673091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[3];
3683091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[2];
3693091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[1];
3703091b266Smatthias.ringwald                 hci_cmd_buffer[pos++] = ptr[0];
3713091b266Smatthias.ringwald                 break;
3723091b266Smatthias.ringwald             case 'P': // c string passed as pascal string with leading 1-byte len
3733091b266Smatthias.ringwald                 ptr = va_arg(argptr, uint8_t *);
3743091b266Smatthias.ringwald                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
3753091b266Smatthias.ringwald                 pos += 16;
37693b8dc03Smatthias.ringwald                 break;
37793b8dc03Smatthias.ringwald             default:
37893b8dc03Smatthias.ringwald                 break;
37993b8dc03Smatthias.ringwald         }
38093b8dc03Smatthias.ringwald         format++;
38193b8dc03Smatthias.ringwald     };
38293b8dc03Smatthias.ringwald     va_end(argptr);
38302ea9861Smatthias.ringwald     hci_cmd_buffer[2] = pos - 3;
38402ea9861Smatthias.ringwald     // send packet
3853429f56bSmatthias.ringwald     hci_stack.num_cmd_packets--;
38616833f0aSmatthias.ringwald     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
38793b8dc03Smatthias.ringwald }