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