xref: /btstack/platform/daemon/src/btstack.c (revision bf01b0119cf4808514eaec10610e1301f7c79312)
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 /*
39  *  btstack.c
40  *
41  *  Created by Matthias Ringwald on 7/1/09.
42  *
43  *  BTstack client API
44  */
45 
46 #include "btstack.h"
47 
48 #include "l2cap.h"
49 #include "socket_connection.h"
50 #include "run_loop.h"
51 
52 #include <string.h>
53 #include <unistd.h>
54 #include <stdarg.h>
55 #include <stdio.h>
56 
57 // static uint8_t hci_cmd_buffer[3+255]; // HCI Command Header + max payload
58 static uint8_t hci_cmd_buffer[HCI_ACL_BUFFER_SIZE]; // BTstack command packets are not size restricted
59 
60 static connection_t *btstack_connection = NULL;
61 
62 /** prototypes & dummy functions */
63 static void dummy_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){};
64 
65 /** local globals :) */
66 static void (*client_packet_handler)(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) = dummy_handler;
67 
68 static const char * daemon_tcp_address = NULL;
69 static uint16_t     daemon_tcp_port    = BTSTACK_PORT;
70 
71 // optional: if called before bt_open, TCP socket is used instead of local unix socket
72 //           note: address is not copied and must be valid during bt_open
73 void bt_use_tcp(const char * address, uint16_t port){
74     daemon_tcp_address = address;
75     daemon_tcp_port    = port;
76 }
77 
78 static int socket_packet_handler(connection_t *connection, uint16_t packet_type, uint16_t channel, uint8_t *data, uint16_t size){
79     // log_info("BTstack client handler: packet type %u, data[0] %x", packet_type, data[0]);
80     (*client_packet_handler)(packet_type, channel, data, size);
81     return 0;
82 }
83 
84 // init BTstack library
85 int bt_open(void){
86 
87     socket_connection_register_packet_callback(socket_packet_handler);
88 
89     // BTdaemon
90     if (daemon_tcp_address) {
91         btstack_connection = socket_connection_open_tcp(daemon_tcp_address,daemon_tcp_port);
92     } else {
93         btstack_connection = socket_connection_open_unix();
94     }
95     if (!btstack_connection) return -1;
96 
97     return 0;
98 }
99 
100 // stop using BTstack library
101 int bt_close(void){
102     return socket_connection_close_tcp(btstack_connection);
103 }
104 
105 // send hci cmd packet
106 int bt_send_cmd(const hci_cmd_t *cmd, ...){
107     va_list argptr;
108     va_start(argptr, cmd);
109     uint16_t len = hci_create_cmd_internal(hci_cmd_buffer, cmd, argptr);
110     va_end(argptr);
111     socket_connection_send_packet(btstack_connection, HCI_COMMAND_DATA_PACKET, 0, hci_cmd_buffer, len);
112     return 0;
113 }
114 
115 // register packet handler
116 btstack_packet_handler_t bt_register_packet_handler(btstack_packet_handler_t handler){
117     btstack_packet_handler_t old_handler = client_packet_handler;
118     client_packet_handler = handler;
119     return old_handler;
120 }
121 
122 void bt_send_l2cap(uint16_t source_cid, uint8_t *data, uint16_t len){
123     // send
124     socket_connection_send_packet(btstack_connection, L2CAP_DATA_PACKET, source_cid, data, len);
125 }
126 
127 void bt_send_rfcomm(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
128     // send
129     socket_connection_send_packet(btstack_connection, RFCOMM_DATA_PACKET, rfcomm_cid, data, len);
130 }
131 
132 void bt_send_acl(uint8_t * data, uint16_t len){
133     // send
134     socket_connection_send_packet(btstack_connection, HCI_ACL_DATA_PACKET, 0, data, len);
135 }
136