1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "btstack.c" 39 40 /* 41 * btstack.c 42 * 43 * Created by Matthias Ringwald on 7/1/09. 44 * 45 * BTstack client API 46 */ 47 48 #include "btstack.h" 49 50 #include "l2cap.h" 51 #include "socket_connection.h" 52 #include "btstack_run_loop.h" 53 #include "btstack_client.h" 54 55 #include <string.h> 56 #include <unistd.h> 57 #include <stdarg.h> 58 #include <stdio.h> 59 60 // static uint8_t hci_cmd_buffer[3+255]; // HCI Command Header + max payload 61 static uint8_t hci_cmd_buffer[HCI_ACL_BUFFER_SIZE]; // BTstack command packets are not size restricted 62 63 static connection_t *btstack_connection = NULL; 64 65 /** prototypes & dummy functions */ 66 static void dummy_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){}; 67 68 /** local globals :) */ 69 static void (*client_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = dummy_handler; 70 71 static const char * daemon_tcp_address = NULL; 72 static uint16_t daemon_tcp_port = BTSTACK_PORT; 73 74 // optional: if called before bt_open, TCP socket is used instead of local unix socket 75 // note: address is not copied and must be valid during bt_open 76 void bt_use_tcp(const char * address, uint16_t port){ 77 daemon_tcp_address = address; 78 daemon_tcp_port = port; 79 } 80 81 static int socket_packet_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t size){ 82 // log_info("BTstack client handler: packet type %u, data[0] %x", packet_type, data[0]); 83 (*client_packet_handler)(packet_type, channel, data, size); 84 return 0; 85 } 86 87 // init BTstack library 88 int bt_open(void){ 89 90 socket_connection_init(); 91 92 socket_connection_register_packet_callback(socket_packet_handler); 93 94 // BTdaemon 95 if (daemon_tcp_address) { 96 btstack_connection = socket_connection_open_tcp(daemon_tcp_address,daemon_tcp_port); 97 } else { 98 btstack_connection = socket_connection_open_unix(); 99 } 100 if (!btstack_connection) return -1; 101 102 return 0; 103 } 104 105 // stop using BTstack library 106 int bt_close(void){ 107 return socket_connection_close_tcp(btstack_connection); 108 } 109 110 // send hci cmd packet 111 int bt_send_cmd(const hci_cmd_t *cmd, ...){ 112 va_list argptr; 113 va_start(argptr, cmd); 114 uint16_t len = hci_cmd_create_from_template(hci_cmd_buffer, cmd, argptr); 115 va_end(argptr); 116 socket_connection_send_packet(btstack_connection, HCI_COMMAND_DATA_PACKET, 0, hci_cmd_buffer, len); 117 return 0; 118 } 119 120 // register packet handler 121 btstack_packet_handler_t bt_register_packet_handler(btstack_packet_handler_t handler){ 122 btstack_packet_handler_t old_handler = client_packet_handler; 123 client_packet_handler = handler; 124 return old_handler; 125 } 126 127 void bt_send_l2cap(uint16_t source_cid, uint8_t *data, uint16_t len){ 128 // send 129 socket_connection_send_packet(btstack_connection, L2CAP_DATA_PACKET, source_cid, data, len); 130 } 131 132 void bt_send_rfcomm(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){ 133 // send 134 socket_connection_send_packet(btstack_connection, RFCOMM_DATA_PACKET, rfcomm_cid, data, len); 135 } 136 137 void bt_send_acl(uint8_t * data, uint16_t len){ 138 // send 139 socket_connection_send_packet(btstack_connection, HCI_ACL_DATA_PACKET, 0, data, len); 140 } 141