1 /******************************************************************************
3 * Copyright (C) 2014 Google, Inc.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 #define LOG_TAG "bt_hci_h4"
23 #include "osi/include/eager_reader.h"
25 #include "osi/include/osi.h"
26 #include "osi/include/log.h"
27 #include "osi/include/reactor.h"
30 #define HCI_HAL_SERIAL_BUFFER_SIZE 1026
32 // Our interface and modules we import
33 static const hci_hal_t interface;
34 static const hci_hal_callbacks_t *callbacks;
35 static const vendor_t *vendor;
37 static thread_t *thread; // Not owned by us
40 static eager_reader_t *uart_stream;
41 static serial_data_type_t current_data_type;
42 static bool stream_has_interpretation;
44 static void event_uart_has_bytes(eager_reader_t *reader, void *context);
46 // Interface functions
48 static bool hal_init(const hci_hal_callbacks_t *upper_callbacks, thread_t *upper_thread) {
49 assert(upper_callbacks != NULL);
50 assert(upper_thread != NULL);
52 callbacks = upper_callbacks;
53 thread = upper_thread;
57 static bool hal_open() {
58 LOG_INFO("%s", __func__);
59 // TODO(zachoverflow): close if already open / or don't reopen (maybe at the hci layer level)
62 int number_of_ports = vendor->send_command(VENDOR_OPEN_USERIAL, &fd_array);
64 if (number_of_ports != 1) {
65 LOG_ERROR("%s opened the wrong number of ports: got %d, expected 1.", __func__, number_of_ports);
69 uart_fd = fd_array[0];
70 if (uart_fd == INVALID_FD) {
71 LOG_ERROR("%s unable to open the uart serial port.", __func__);
75 uart_stream = eager_reader_new(uart_fd, &allocator_malloc, HCI_HAL_SERIAL_BUFFER_SIZE, SIZE_MAX, "hci_single_channel");
77 LOG_ERROR("%s unable to create eager reader for the uart serial port.", __func__);
81 stream_has_interpretation = false;
82 eager_reader_register(uart_stream, thread_get_reactor(thread), event_uart_has_bytes, NULL);
91 static void hal_close() {
92 LOG_INFO("%s", __func__);
94 eager_reader_free(uart_stream);
95 vendor->send_command(VENDOR_CLOSE_USERIAL, NULL);
99 static size_t read_data(serial_data_type_t type, uint8_t *buffer, size_t max_size, bool block) {
100 if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
101 LOG_ERROR("%s invalid data type: %d", __func__, type);
103 } else if (!stream_has_interpretation) {
104 LOG_ERROR("%s with no valid stream intepretation.", __func__);
106 } else if (current_data_type != type) {
107 LOG_ERROR("%s with different type than existing interpretation.", __func__);
111 return eager_reader_read(uart_stream, buffer, max_size, block);
114 static void packet_finished(serial_data_type_t type) {
115 if (!stream_has_interpretation)
116 LOG_ERROR("%s with no existing stream interpretation.", __func__);
117 else if (current_data_type != type)
118 LOG_ERROR("%s with different type than existing interpretation.", __func__);
120 stream_has_interpretation = false;
123 static uint16_t transmit_data(serial_data_type_t type, uint8_t *data, uint16_t length) {
124 assert(data != NULL);
127 if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
128 LOG_ERROR("%s invalid data type: %d", __func__, type);
132 // Write the signal byte right before the data
134 uint8_t previous_byte = *data;
138 uint16_t transmitted_length = 0;
140 ssize_t ret = write(uart_fd, data + transmitted_length, length);
143 LOG_ERROR("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
146 // If we wrote nothing, don't loop more because we
147 // can't go to infinity or beyond
150 transmitted_length += ret;
157 // Be nice and restore the old value of that byte
158 *(data) = previous_byte;
160 // Remove the signal byte from our transmitted length, if it was actually written
161 if (transmitted_length > 0)
162 --transmitted_length;
164 return transmitted_length;
167 // Internal functions
169 // See what data is waiting, and notify the upper layer
170 static void event_uart_has_bytes(eager_reader_t *reader, UNUSED_ATTR void *context) {
171 if (stream_has_interpretation) {
172 callbacks->data_ready(current_data_type);
175 eager_reader_read(reader, &type_byte, 1, true);
176 if (type_byte < DATA_TYPE_ACL || type_byte > DATA_TYPE_EVENT) {
177 LOG_ERROR("[h4] Unknown HCI message type. Dropping this byte 0x%x, min %x, max %x", type_byte, DATA_TYPE_ACL, DATA_TYPE_EVENT);
181 stream_has_interpretation = true;
182 current_data_type = type_byte;
186 static const hci_hal_t interface = {
197 const hci_hal_t *hci_hal_h4_get_interface() {
198 vendor = vendor_get_interface();
202 const hci_hal_t *hci_hal_h4_get_test_interface(vendor_t *vendor_interface) {
203 vendor = vendor_interface;