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"
25 #include "osi/include/eager_reader.h"
27 #include "osi/include/osi.h"
28 #include "osi/include/log.h"
29 #include "osi/include/reactor.h"
32 #define HCI_HAL_SERIAL_BUFFER_SIZE 1026
34 // Our interface and modules we import
35 static const hci_hal_t interface;
36 static const hci_hal_callbacks_t *callbacks;
37 static const vendor_t *vendor;
39 static thread_t *thread; // Not owned by us
42 static eager_reader_t *uart_stream;
43 static serial_data_type_t current_data_type;
44 static bool stream_has_interpretation;
46 static void event_uart_has_bytes(eager_reader_t *reader, void *context);
48 // Interface functions
50 static bool hal_init(const hci_hal_callbacks_t *upper_callbacks, thread_t *upper_thread) {
51 assert(upper_callbacks != NULL);
52 assert(upper_thread != NULL);
54 callbacks = upper_callbacks;
55 thread = upper_thread;
59 static bool hal_open() {
60 LOG_INFO("%s", __func__);
61 // TODO(zachoverflow): close if already open / or don't reopen (maybe at the hci layer level)
64 int number_of_ports = vendor->send_command(VENDOR_OPEN_USERIAL, &fd_array);
66 if (number_of_ports != 1) {
67 LOG_ERROR("%s opened the wrong number of ports: got %d, expected 1.", __func__, number_of_ports);
71 uart_fd = fd_array[0];
72 if (uart_fd == INVALID_FD) {
73 LOG_ERROR("%s unable to open the uart serial port.", __func__);
77 uart_stream = eager_reader_new(uart_fd, &allocator_malloc, HCI_HAL_SERIAL_BUFFER_SIZE, SIZE_MAX, "hci_single_channel");
79 LOG_ERROR("%s unable to create eager reader for the uart serial port.", __func__);
83 stream_has_interpretation = false;
84 eager_reader_register(uart_stream, thread_get_reactor(thread), event_uart_has_bytes, NULL);
93 static void hal_close() {
94 LOG_INFO("%s", __func__);
96 eager_reader_free(uart_stream);
97 vendor->send_command(VENDOR_CLOSE_USERIAL, NULL);
101 static size_t read_data(serial_data_type_t type, uint8_t *buffer, size_t max_size, bool block) {
102 if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
103 LOG_ERROR("%s invalid data type: %d", __func__, type);
105 } else if (!stream_has_interpretation) {
106 LOG_ERROR("%s with no valid stream intepretation.", __func__);
108 } else if (current_data_type != type) {
109 LOG_ERROR("%s with different type than existing interpretation.", __func__);
113 return eager_reader_read(uart_stream, buffer, max_size, block);
116 static void packet_finished(serial_data_type_t type) {
117 if (!stream_has_interpretation)
118 LOG_ERROR("%s with no existing stream interpretation.", __func__);
119 else if (current_data_type != type)
120 LOG_ERROR("%s with different type than existing interpretation.", __func__);
122 stream_has_interpretation = false;
125 static uint16_t transmit_data(serial_data_type_t type, uint8_t *data, uint16_t length) {
126 assert(data != NULL);
129 if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
130 LOG_ERROR("%s invalid data type: %d", __func__, type);
134 // Write the signal byte right before the data
136 uint8_t previous_byte = *data;
140 uint16_t transmitted_length = 0;
142 ssize_t ret = write(uart_fd, data + transmitted_length, length);
145 LOG_ERROR("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
148 // If we wrote nothing, don't loop more because we
149 // can't go to infinity or beyond
152 transmitted_length += ret;
159 // Be nice and restore the old value of that byte
160 *(data) = previous_byte;
162 // Remove the signal byte from our transmitted length, if it was actually written
163 if (transmitted_length > 0)
164 --transmitted_length;
166 return transmitted_length;
169 // Internal functions
171 // See what data is waiting, and notify the upper layer
172 static void event_uart_has_bytes(eager_reader_t *reader, UNUSED_ATTR void *context) {
173 if (stream_has_interpretation) {
174 callbacks->data_ready(current_data_type);
177 if (eager_reader_read(reader, &type_byte, 1, true) == 0) {
178 LOG_ERROR("%s could not read HCI message type", __func__);
181 if (type_byte < DATA_TYPE_ACL || type_byte > DATA_TYPE_EVENT) {
182 LOG_ERROR("%s Unknown HCI message type. Dropping this byte 0x%x, min %x, max %x", __func__, type_byte, DATA_TYPE_ACL, DATA_TYPE_EVENT);
186 stream_has_interpretation = true;
187 current_data_type = type_byte;
191 static const hci_hal_t interface = {
202 const hci_hal_t *hci_hal_h4_get_interface() {
203 vendor = vendor_get_interface();
207 const hci_hal_t *hci_hal_h4_get_test_interface(vendor_t *vendor_interface) {
208 vendor = vendor_interface;