mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-01-06 07:00:59 +00:00
unify hci_create_cmd_packet and hci_send_cmd_packet into hci_send_cmd
This commit is contained in:
parent
53089342aa
commit
02ea9861ad
63
src/hci.c
63
src/hci.c
@ -13,17 +13,35 @@
|
||||
|
||||
// calculate combined ogf/ocf value
|
||||
#define OPCODE(ogf, ocf) (ocf | ogf << 10)
|
||||
#define OGF_LINK_CONTROL 0x01
|
||||
#define OGF_CONTROLLER_BASEBAND 0x03
|
||||
|
||||
hci_cmd_t hci_inquiry = {
|
||||
OPCODE(0x01, 0x01), "311" // LAP, Inquiry length, Num_responses
|
||||
OPCODE(OGF_LINK_CONTROL, 0x01), "311" // LAP, Inquiry length, Num_responses
|
||||
};
|
||||
|
||||
hci_cmd_t hci_reset = {
|
||||
OPCODE(0x03, 0x03), ""
|
||||
OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
|
||||
};
|
||||
|
||||
hci_cmd_t hci_create_connection = {
|
||||
OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
|
||||
// BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
|
||||
};
|
||||
|
||||
hci_cmd_t hci_write_page_timeout = {
|
||||
OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
|
||||
// Page_Timeout * 0.625 ms
|
||||
};
|
||||
|
||||
hci_cmd_t hci_host_buffer_size = {
|
||||
OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
|
||||
// Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
|
||||
};
|
||||
|
||||
|
||||
static hci_transport_t *hci_transport;
|
||||
static hci_transport_t * hci_transport;
|
||||
static uint8_t * hci_cmd_buffer;
|
||||
|
||||
void hexdump(uint8_t *data, int size){
|
||||
int i;
|
||||
@ -46,6 +64,9 @@ void hci_init(hci_transport_t *transport, void *config){
|
||||
// reference to use transport layer implementation
|
||||
hci_transport = transport;
|
||||
|
||||
// empty cmd buffer
|
||||
hci_cmd_buffer = malloc(3+255);
|
||||
|
||||
// open unix socket
|
||||
|
||||
// wait for connections
|
||||
@ -59,10 +80,6 @@ int hci_power_control(HCI_POWER_MODE power_mode){
|
||||
return 0;
|
||||
}
|
||||
|
||||
int hci_send_cmd_packet(uint8_t *buffer, int size){
|
||||
return hci_transport->send_cmd_packet(buffer, size);
|
||||
}
|
||||
|
||||
void hci_run(){
|
||||
while (1) {
|
||||
// construct file descriptor set to wait for
|
||||
@ -73,10 +90,9 @@ void hci_run(){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ...){
|
||||
buffer[0] = cmd->opcode & 0xff;
|
||||
buffer[1] = cmd->opcode >> 8;
|
||||
int hci_send_cmd(hci_cmd_t *cmd, ...){
|
||||
hci_cmd_buffer[0] = cmd->opcode & 0xff;
|
||||
hci_cmd_buffer[1] = cmd->opcode >> 8;
|
||||
int pos = 3;
|
||||
|
||||
va_list argptr;
|
||||
@ -91,9 +107,9 @@ void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ..
|
||||
case '2': // 16 bit value
|
||||
case 'H': // hci_handle
|
||||
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
||||
buffer[pos++] = word & 0xff;
|
||||
hci_cmd_buffer[pos++] = word & 0xff;
|
||||
if (*format == '2') {
|
||||
buffer[pos++] = word >> 8;
|
||||
hci_cmd_buffer[pos++] = word >> 8;
|
||||
} else if (*format == 'H') {
|
||||
// TODO
|
||||
}
|
||||
@ -102,17 +118,21 @@ void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ..
|
||||
case '4':
|
||||
longword = va_arg(argptr, uint32_t);
|
||||
// longword = va_arg(argptr, int);
|
||||
buffer[pos++] = longword;
|
||||
buffer[pos++] = longword >> 8;
|
||||
buffer[pos++] = longword >> 16;
|
||||
hci_cmd_buffer[pos++] = longword;
|
||||
hci_cmd_buffer[pos++] = longword >> 8;
|
||||
hci_cmd_buffer[pos++] = longword >> 16;
|
||||
if (*format == '4'){
|
||||
buffer[pos++] = longword >> 24;
|
||||
hci_cmd_buffer[pos++] = longword >> 24;
|
||||
}
|
||||
break;
|
||||
case 'B': // bt-addr
|
||||
bt_addr = va_arg(argptr, uint8_t *);
|
||||
memcpy( &buffer[pos], bt_addr, 6);
|
||||
pos += 6;
|
||||
hci_cmd_buffer[pos++] = bt_addr[5];
|
||||
hci_cmd_buffer[pos++] = bt_addr[4];
|
||||
hci_cmd_buffer[pos++] = bt_addr[3];
|
||||
hci_cmd_buffer[pos++] = bt_addr[2];
|
||||
hci_cmd_buffer[pos++] = bt_addr[1];
|
||||
hci_cmd_buffer[pos++] = bt_addr[0];
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -120,6 +140,7 @@ void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ..
|
||||
format++;
|
||||
};
|
||||
va_end(argptr);
|
||||
buffer[2] = pos - 3;
|
||||
*cmd_len = pos;
|
||||
hci_cmd_buffer[2] = pos - 3;
|
||||
// send packet
|
||||
return hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
|
||||
}
|
12
src/hci.h
12
src/hci.h
@ -8,6 +8,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "hci_transport.h"
|
||||
|
||||
@ -21,6 +22,8 @@ typedef struct {
|
||||
const char *format;
|
||||
} hci_cmd_t;
|
||||
|
||||
typedef uint8_t bd_addr_t[6];
|
||||
|
||||
// set up HCI
|
||||
void hci_init(hci_transport_t *transport, void *config);
|
||||
|
||||
@ -33,12 +36,13 @@ void hci_run();
|
||||
//
|
||||
void hexdump(uint8_t *data, int size);
|
||||
|
||||
// create hci command packet based on a template and a list of parameters
|
||||
void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ...);
|
||||
|
||||
int hci_send_cmd_packet(uint8_t *buffer, int size);
|
||||
// create and send hci command packet based on a template and a list of parameters
|
||||
int hci_send_cmd(hci_cmd_t *cmd, ...);
|
||||
|
||||
extern hci_cmd_t hci_inquiry;
|
||||
extern hci_cmd_t hci_reset;
|
||||
extern hci_cmd_t hci_create_connection;
|
||||
extern hci_cmd_t hci_host_buffer_size;
|
||||
extern hci_cmd_t hci_write_page_timeout;
|
||||
|
||||
#define HCI_INQUIRY_LAP 0x9E8B33L // 0x9E8B33: General/Unlimited Inquiry Access Code (GIAC)
|
||||
|
20
src/main.c
20
src/main.c
@ -17,25 +17,20 @@
|
||||
|
||||
static hci_transport_t * transport;
|
||||
static hci_uart_config_t config;
|
||||
static uint8_t buffer [200];
|
||||
|
||||
#define COMMAND_COMPLETE_EVENT(event,cmd) ( event[0] == 0x0e && (event[3] | (event[4] << 8)) == cmd.opcode)
|
||||
|
||||
#if 0
|
||||
// reset done, send host buffer size
|
||||
hci_create_cmd_packet( buffer, &len, &hci_host_buffer_size, 400, 255, 1, 0, 0);
|
||||
hci_send_cmd_packet( buffer, len );
|
||||
hci_send_cmd(&hci_host_buffer_size, 400, 255, 1, 0, 0);
|
||||
// reset done, send inq
|
||||
hci_create_cmd_packet( buffer, &len, &hci_inquiry, HCI_INQUIRY_LAP, 30, 0);
|
||||
hci_send_cmd_packet( buffer, len );
|
||||
hci_send_cmd(&hci_inquiry, HCI_INQUIRY_LAP, 30, 0);
|
||||
#endif
|
||||
void event_handler(uint8_t *packet, int size){
|
||||
uint8_t len;
|
||||
// printf("Event type: %x, opcode: %x, other %x\n", packet[0], packet[3] | packet[4] << 8);
|
||||
if ( COMMAND_COMPLETE_EVENT(packet, hci_reset) ) {
|
||||
// reset done, write page timeout
|
||||
hci_create_cmd_packet( buffer, &len, &hci_write_page_timeout, 0x6000); // ca. 15 sec
|
||||
hci_send_cmd_packet( buffer, len );
|
||||
hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec
|
||||
}
|
||||
|
||||
if ( COMMAND_COMPLETE_EVENT(packet, hci_host_buffer_size) ) {
|
||||
@ -45,8 +40,7 @@ void event_handler(uint8_t *packet, int size){
|
||||
// hci_host_buffer_size done, send connect
|
||||
// bd_addr_t addr = {0x00, 0x03, 0xc9, 0x3d, 0x77, 0x43 };
|
||||
bd_addr_t addr = { 0x00, 0x16, 0xcb, 0x09, 0x94, 0xa9};
|
||||
hci_create_cmd_packet( buffer, &len, &hci_create_connection, &addr, 0x18, 0, 0, 0, 0);
|
||||
hci_send_cmd_packet( buffer, len );
|
||||
hci_send_cmd(&hci_create_connection, &addr, 0x18, 0, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -82,10 +76,8 @@ int main (int argc, const char * argv[]) {
|
||||
int transport_fd = transport->get_fd();
|
||||
|
||||
// send hci reset
|
||||
uint8_t len;
|
||||
hci_create_cmd_packet( buffer, &len, &hci_reset);
|
||||
hci_send_cmd_packet( buffer, len );
|
||||
|
||||
hci_send_cmd(&hci_reset);
|
||||
|
||||
//
|
||||
fd_set descriptors;
|
||||
FD_ZERO(&descriptors);
|
||||
|
Loading…
Reference in New Issue
Block a user