btstack/src/hci.c

175 lines
4.6 KiB
C
Raw Normal View History

2009-04-29 22:00:24 +00:00
/*
* hci.c
*
* Created by Matthias Ringwald on 4/29/09.
*
*/
2009-05-03 21:28:40 +00:00
#include <unistd.h>
2009-05-09 17:34:17 +00:00
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
2009-04-29 22:00:24 +00:00
#include "hci.h"
2009-05-10 14:16:03 +00:00
// calculate combined ogf/ocf value
#define OPCODE(ogf, ocf) (ocf | ogf << 10)
#define OGF_LINK_CONTROL 0x01
#define OGF_CONTROLLER_BASEBAND 0x03
2009-05-09 17:34:17 +00:00
hci_cmd_t hci_inquiry = {
2009-05-10 20:57:01 +00:00
OPCODE(OGF_LINK_CONTROL, 0x01), "311"
// LAP, Inquiry length, Num_responses
};
hci_cmd_t hci_link_key_request_negative_reply = {
OPCODE(OGF_LINK_CONTROL, 0x0c), "B"
};
hci_cmd_t hci_pin_code_request_reply = {
OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P"
// BD_ADDR, pin length, PIN: c-string
2009-05-09 17:34:17 +00:00
};
hci_cmd_t hci_reset = {
OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
2009-05-09 17:34:17 +00:00
};
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
};
2009-05-10 20:57:01 +00:00
hci_cmd_t hci_write_authentication_enable = {
OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1"
// Authentication_Enable
};
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:
};
2009-05-09 17:34:17 +00:00
static hci_transport_t * hci_transport;
static uint8_t * hci_cmd_buffer;
2009-05-03 21:28:40 +00:00
2009-05-16 21:29:51 +00:00
void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
buffer[pos] = value & 0xff;
buffer[pos+1] = value >> 8;
}
void hexdump(uint8_t *data, int size){
int i;
for (i=0; i<size;i++){
printf("%02X ", data[i]);
}
printf("\n");
}
#if 0
static void *hci_daemon_thread(void *arg){
printf("HCI Daemon started\n");
hci_run(transport, &config);
return NULL;
}
#endif
2009-05-03 21:28:40 +00:00
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);
2009-05-03 21:28:40 +00:00
// open unix socket
// wait for connections
// enter loop
// handle events
}
int hci_power_control(HCI_POWER_MODE power_mode){
return 0;
2009-04-29 22:00:24 +00:00
}
2009-05-03 21:28:40 +00:00
void hci_run(){
while (1) {
// construct file descriptor set to wait for
// select
// for each ready file in FD - call handle_data
sleep(1);
}
2009-05-09 17:34:17 +00:00
}
2009-05-16 21:29:51 +00:00
int hci_send_acl_packet(uint8_t *packet, int size){
return hci_transport->send_acl_packet(packet, size);
}
int hci_send_cmd(hci_cmd_t *cmd, ...){
hci_cmd_buffer[0] = cmd->opcode & 0xff;
hci_cmd_buffer[1] = cmd->opcode >> 8;
2009-05-09 17:34:17 +00:00
int pos = 3;
va_list argptr;
va_start(argptr, cmd);
const char *format = cmd->format;
uint16_t word;
uint32_t longword;
2009-05-10 20:57:01 +00:00
uint8_t * ptr;
2009-05-09 17:34:17 +00:00
while (*format) {
switch(*format) {
case '1': // 8 bit value
case '2': // 16 bit value
case 'H': // hci_handle
2009-05-09 17:45:45 +00:00
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
hci_cmd_buffer[pos++] = word & 0xff;
2009-05-09 17:34:17 +00:00
if (*format == '2') {
hci_cmd_buffer[pos++] = word >> 8;
2009-05-09 17:34:17 +00:00
} else if (*format == 'H') {
2009-05-09 17:45:45 +00:00
// TODO
2009-05-09 17:34:17 +00:00
}
break;
case '3':
case '4':
longword = va_arg(argptr, uint32_t);
// longword = va_arg(argptr, int);
hci_cmd_buffer[pos++] = longword;
hci_cmd_buffer[pos++] = longword >> 8;
hci_cmd_buffer[pos++] = longword >> 16;
2009-05-09 17:34:17 +00:00
if (*format == '4'){
hci_cmd_buffer[pos++] = longword >> 24;
2009-05-09 17:34:17 +00:00
}
break;
case 'B': // bt-addr
2009-05-10 20:57:01 +00:00
ptr = va_arg(argptr, uint8_t *);
hci_cmd_buffer[pos++] = ptr[5];
hci_cmd_buffer[pos++] = ptr[4];
hci_cmd_buffer[pos++] = ptr[3];
hci_cmd_buffer[pos++] = ptr[2];
hci_cmd_buffer[pos++] = ptr[1];
hci_cmd_buffer[pos++] = ptr[0];
break;
case 'P': // c string passed as pascal string with leading 1-byte len
ptr = va_arg(argptr, uint8_t *);
memcpy(&hci_cmd_buffer[pos], ptr, 16);
pos += 16;
2009-05-09 17:34:17 +00:00
break;
default:
break;
}
format++;
};
va_end(argptr);
hci_cmd_buffer[2] = pos - 3;
// send packet
return hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
2009-05-03 21:28:40 +00:00
}