2009-07-01 21:55:08 +00:00
|
|
|
/*
|
|
|
|
* btstack.c
|
|
|
|
*
|
|
|
|
* Created by Matthias Ringwald on 7/1/09.
|
|
|
|
*
|
|
|
|
* BTstack client API
|
|
|
|
*/
|
|
|
|
|
2009-09-28 21:19:05 +00:00
|
|
|
#include <btstack/btstack.h>
|
2009-07-01 21:55:08 +00:00
|
|
|
|
2009-07-20 21:39:43 +00:00
|
|
|
#include "l2cap.h"
|
2009-07-22 21:31:35 +00:00
|
|
|
#include "socket_connection.h"
|
2009-09-28 21:19:05 +00:00
|
|
|
#include <btstack/run_loop.h>
|
2009-07-22 21:31:35 +00:00
|
|
|
|
2009-07-15 22:12:54 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
2009-07-16 22:04:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-10-05 22:06:12 +00:00
|
|
|
static uint8_t hci_cmd_buffer[3+255];
|
2009-07-15 22:12:54 +00:00
|
|
|
|
2009-07-22 21:31:35 +00:00
|
|
|
static connection_t *btstack_connection = NULL;
|
2009-07-15 22:12:54 +00:00
|
|
|
|
2009-10-05 22:06:12 +00:00
|
|
|
/** prototypes & dummy functions */
|
|
|
|
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){};
|
|
|
|
static int btstack_packet_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t size);
|
|
|
|
|
|
|
|
/** local globals :) */
|
|
|
|
static void (*client_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
|
2009-07-15 22:12:54 +00:00
|
|
|
|
|
|
|
|
2009-07-14 18:29:29 +00:00
|
|
|
// init BTstack library
|
2009-07-14 20:41:14 +00:00
|
|
|
int bt_open(){
|
2009-07-28 21:04:16 +00:00
|
|
|
|
|
|
|
// BTdaemon
|
2009-07-22 21:31:35 +00:00
|
|
|
socket_connection_register_packet_callback(btstack_packet_handler);
|
2009-08-19 20:09:30 +00:00
|
|
|
btstack_connection = socket_connection_open_unix();
|
2009-07-22 21:31:35 +00:00
|
|
|
if (!btstack_connection) return -1;
|
2009-07-28 21:04:16 +00:00
|
|
|
|
2009-07-15 22:12:54 +00:00
|
|
|
return 0;
|
2009-07-14 20:41:14 +00:00
|
|
|
}
|
2009-07-14 18:29:29 +00:00
|
|
|
|
|
|
|
// stop using BTstack library
|
2009-07-14 20:41:14 +00:00
|
|
|
int bt_close(){
|
2009-07-22 21:31:35 +00:00
|
|
|
return socket_connection_close_tcp(btstack_connection);
|
2009-07-15 22:12:54 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
// send hci cmd packet
|
|
|
|
int bt_send_cmd(hci_cmd_t *cmd, ...){
|
|
|
|
va_list argptr;
|
|
|
|
va_start(argptr, cmd);
|
|
|
|
uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
|
|
|
|
va_end(argptr);
|
|
|
|
socket_connection_send_packet(btstack_connection, HCI_COMMAND_DATA_PACKET, 0, hci_cmd_buffer, len);
|
|
|
|
return 0;
|
2009-07-28 21:04:16 +00:00
|
|
|
}
|
|
|
|
|
2009-07-31 21:41:15 +00:00
|
|
|
int btstack_packet_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t size){
|
2009-10-07 20:17:19 +00:00
|
|
|
// printf("BTstack client handler: packet type %u, data[0] %x\n", packet_type, data[0]);
|
2009-10-05 22:06:12 +00:00
|
|
|
(*client_packet_handler)(packet_type, data, size);
|
2009-07-22 21:31:35 +00:00
|
|
|
return 0;
|
2009-07-14 20:41:14 +00:00
|
|
|
}
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-10-05 22:06:12 +00:00
|
|
|
// register packet handler
|
|
|
|
void bt_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
|
|
|
|
client_packet_handler = handler;
|
2009-07-14 20:41:14 +00:00
|
|
|
}
|
2009-07-14 18:29:29 +00:00
|
|
|
|
2009-10-05 22:06:12 +00:00
|
|
|
void bt_send_l2cap(uint16_t source_cid, uint8_t *data, uint16_t len){
|
2009-07-31 21:41:15 +00:00
|
|
|
// send
|
|
|
|
socket_connection_send_packet(btstack_connection, L2CAP_DATA_PACKET, source_cid, data, len);
|
2009-07-28 21:04:16 +00:00
|
|
|
}
|
|
|
|
|
2009-10-05 22:06:12 +00:00
|
|
|
void bt_send_acl(uint8_t * data, uint16_t len){
|
2009-08-24 21:56:12 +00:00
|
|
|
// send
|
|
|
|
socket_connection_send_packet(btstack_connection, HCI_ACL_DATA_PACKET, 0, data, len);
|
|
|
|
}
|