xref: /btstack/platform/daemon/src/btstack.c (revision c589a9bb896bd3ca6ce0b0556b913c50596ccdf8)
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 BLUEKITCHEN
24  * GMBH 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 #ifndef HAVE_UNIX_SOCKETS
91     // without unix sockets, default to TCP
92     if (!daemon_tcp_address){
93         daemon_tcp_address = "127.0.0.1";
94     }
95     if (!daemon_tcp_port){
96         daemon_tcp_port = BTSTACK_PORT;
97     }
98 #endif
99 
100     socket_connection_init();
101 
102     socket_connection_register_packet_callback(socket_packet_handler);
103 
104     // BTdaemon
105     if (daemon_tcp_address) {
106         btstack_connection = socket_connection_open_tcp(daemon_tcp_address,daemon_tcp_port);
107     } else {
108 #ifdef HAVE_UNIX_SOCKETS
109         btstack_connection = socket_connection_open_unix();
110 #endif
111     }
112     if (!btstack_connection) return -1;
113 
114     return 0;
115 }
116 
117 // stop using BTstack library
118 int bt_close(void){
119     return socket_connection_close_tcp(btstack_connection);
120 }
121 
122 // send hci cmd packet
123 int bt_send_cmd(const hci_cmd_t *cmd, ...){
124     va_list argptr;
125     va_start(argptr, cmd);
126     uint16_t len = hci_cmd_create_from_template(hci_cmd_buffer, cmd, argptr);
127     va_end(argptr);
128     socket_connection_send_packet(btstack_connection, HCI_COMMAND_DATA_PACKET, 0, hci_cmd_buffer, len);
129     return 0;
130 }
131 
132 // register packet handler
133 btstack_packet_handler_t bt_register_packet_handler(btstack_packet_handler_t handler){
134     btstack_packet_handler_t old_handler = client_packet_handler;
135     client_packet_handler = handler;
136     return old_handler;
137 }
138 
139 void bt_send_l2cap(uint16_t source_cid, uint8_t *data, uint16_t len){
140     // send
141     socket_connection_send_packet(btstack_connection, L2CAP_DATA_PACKET, source_cid, data, len);
142 }
143 
144 void bt_send_rfcomm(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
145     // send
146     socket_connection_send_packet(btstack_connection, RFCOMM_DATA_PACKET, rfcomm_cid, data, len);
147 }
148 
149 void bt_send_acl(uint8_t * data, uint16_t len){
150     // send
151     socket_connection_send_packet(btstack_connection, HCI_ACL_DATA_PACKET, 0, data, len);
152 }
153