xref: /btstack/platform/daemon/src/btstack.c (revision bc37f7b0d0a3eaa5763a873c5730bc14b849aaa0)
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_init();
89 
90     socket_connection_register_packet_callback(socket_packet_handler);
91 
92     // BTdaemon
93     if (daemon_tcp_address) {
94         btstack_connection = socket_connection_open_tcp(daemon_tcp_address,daemon_tcp_port);
95     } else {
96         btstack_connection = socket_connection_open_unix();
97     }
98     if (!btstack_connection) return -1;
99 
100     return 0;
101 }
102 
103 // stop using BTstack library
104 int bt_close(void){
105     return socket_connection_close_tcp(btstack_connection);
106 }
107 
108 // send hci cmd packet
109 int bt_send_cmd(const hci_cmd_t *cmd, ...){
110     va_list argptr;
111     va_start(argptr, cmd);
112     uint16_t len = hci_cmd_create_from_template(hci_cmd_buffer, cmd, argptr);
113     va_end(argptr);
114     socket_connection_send_packet(btstack_connection, HCI_COMMAND_DATA_PACKET, 0, hci_cmd_buffer, len);
115     return 0;
116 }
117 
118 // register packet handler
119 btstack_packet_handler_t bt_register_packet_handler(btstack_packet_handler_t handler){
120     btstack_packet_handler_t old_handler = client_packet_handler;
121     client_packet_handler = handler;
122     return old_handler;
123 }
124 
125 void bt_send_l2cap(uint16_t source_cid, uint8_t *data, uint16_t len){
126     // send
127     socket_connection_send_packet(btstack_connection, L2CAP_DATA_PACKET, source_cid, data, len);
128 }
129 
130 void bt_send_rfcomm(uint16_t rfcomm_cid, uint8_t *data, uint16_t len){
131     // send
132     socket_connection_send_packet(btstack_connection, RFCOMM_DATA_PACKET, rfcomm_cid, data, len);
133 }
134 
135 void bt_send_acl(uint8_t * data, uint16_t len){
136     // send
137     socket_connection_send_packet(btstack_connection, HCI_ACL_DATA_PACKET, 0, data, len);
138 }
139